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 focus between a parent view and its child view.
428 * Child views are shown side-by-side to their parent view, provided
429 * there is enough screen estate.
431 * When a new view is opened from within a parent view, this new view
432 * becomes a child view of the parent view, replacing any existing child.
434 * When a new view is opened from within a child view, this new view
435 * becomes a parent view which will obscure the views below until the
436 * user quits the new parent view by typing 'q'.
438 * This list of views contains parent views only.
439 * Child views are only pointed to by their parent view.
440 */
441 TAILQ_HEAD(tog_view_list_head, tog_view);
443 struct tog_view {
444 TAILQ_ENTRY(tog_view) entry;
445 WINDOW *window;
446 PANEL *panel;
447 int nlines, ncols, begin_y, begin_x;
448 int lines, cols; /* copies of LINES and COLS */
449 int focussed; /* Only set on one parent or child view at a time. */
450 int dying;
451 struct tog_view *parent;
452 struct tog_view *child;
454 /*
455 * This flag is initially set on parent views when a new child view
456 * is created. It gets toggled when the 'Tab' key switches focus
457 * between parent and child.
458 * The flag indicates whether focus should be passed on to our child
459 * view if this parent view gets picked for focus after another parent
460 * view was closed. This prevents child views from losing focus in such
461 * situations.
462 */
463 int focus_child;
465 /* type-specific state */
466 enum tog_view_type type;
467 union {
468 struct tog_diff_view_state diff;
469 struct tog_log_view_state log;
470 struct tog_blame_view_state blame;
471 struct tog_tree_view_state tree;
472 struct tog_ref_view_state ref;
473 } state;
475 const struct got_error *(*show)(struct tog_view *);
476 const struct got_error *(*input)(struct tog_view **,
477 struct tog_view *, int);
478 const struct got_error *(*close)(struct tog_view *);
480 const struct got_error *(*search_start)(struct tog_view *);
481 const struct got_error *(*search_next)(struct tog_view *);
482 int searching;
483 #define TOG_SEARCH_FORWARD 1
484 #define TOG_SEARCH_BACKWARD 2
485 int search_next_done;
486 #define TOG_SEARCH_HAVE_MORE 1
487 #define TOG_SEARCH_NO_MORE 2
488 #define TOG_SEARCH_HAVE_NONE 3
489 regex_t regex;
490 regmatch_t regmatch;
491 };
493 static const struct got_error *open_diff_view(struct tog_view *,
494 struct got_object_id *, struct got_object_id *,
495 const char *, const char *, int, int, int, struct tog_view *,
496 struct got_repository *);
497 static const struct got_error *show_diff_view(struct tog_view *);
498 static const struct got_error *input_diff_view(struct tog_view **,
499 struct tog_view *, int);
500 static const struct got_error* close_diff_view(struct tog_view *);
501 static const struct got_error *search_start_diff_view(struct tog_view *);
502 static const struct got_error *search_next_diff_view(struct tog_view *);
504 static const struct got_error *open_log_view(struct tog_view *,
505 struct got_object_id *, struct got_repository *,
506 const char *, const char *, int);
507 static const struct got_error * show_log_view(struct tog_view *);
508 static const struct got_error *input_log_view(struct tog_view **,
509 struct tog_view *, int);
510 static const struct got_error *close_log_view(struct tog_view *);
511 static const struct got_error *search_start_log_view(struct tog_view *);
512 static const struct got_error *search_next_log_view(struct tog_view *);
514 static const struct got_error *open_blame_view(struct tog_view *, char *,
515 struct got_object_id *, struct got_repository *);
516 static const struct got_error *show_blame_view(struct tog_view *);
517 static const struct got_error *input_blame_view(struct tog_view **,
518 struct tog_view *, int);
519 static const struct got_error *close_blame_view(struct tog_view *);
520 static const struct got_error *search_start_blame_view(struct tog_view *);
521 static const struct got_error *search_next_blame_view(struct tog_view *);
523 static const struct got_error *open_tree_view(struct tog_view *,
524 struct got_tree_object *, struct got_object_id *, struct got_repository *);
525 static const struct got_error *show_tree_view(struct tog_view *);
526 static const struct got_error *input_tree_view(struct tog_view **,
527 struct tog_view *, int);
528 static const struct got_error *close_tree_view(struct tog_view *);
529 static const struct got_error *search_start_tree_view(struct tog_view *);
530 static const struct got_error *search_next_tree_view(struct tog_view *);
532 static const struct got_error *open_ref_view(struct tog_view *,
533 struct got_repository *);
534 static const struct got_error *show_ref_view(struct tog_view *);
535 static const struct got_error *input_ref_view(struct tog_view **,
536 struct tog_view *, int);
537 static const struct got_error *close_ref_view(struct tog_view *);
538 static const struct got_error *search_start_ref_view(struct tog_view *);
539 static const struct got_error *search_next_ref_view(struct tog_view *);
541 static volatile sig_atomic_t tog_sigwinch_received;
542 static volatile sig_atomic_t tog_sigpipe_received;
543 static volatile sig_atomic_t tog_sigcont_received;
545 static void
546 tog_sigwinch(int signo)
548 tog_sigwinch_received = 1;
551 static void
552 tog_sigpipe(int signo)
554 tog_sigpipe_received = 1;
557 static void
558 tog_sigcont(int signo)
560 tog_sigcont_received = 1;
563 static const struct got_error *
564 view_close(struct tog_view *view)
566 const struct got_error *err = NULL;
568 if (view->child) {
569 view_close(view->child);
570 view->child = NULL;
572 if (view->close)
573 err = view->close(view);
574 if (view->panel)
575 del_panel(view->panel);
576 if (view->window)
577 delwin(view->window);
578 free(view);
579 return err;
582 static struct tog_view *
583 view_open(int nlines, int ncols, int begin_y, int begin_x,
584 enum tog_view_type type)
586 struct tog_view *view = calloc(1, sizeof(*view));
588 if (view == NULL)
589 return NULL;
591 view->type = type;
592 view->lines = LINES;
593 view->cols = COLS;
594 view->nlines = nlines ? nlines : LINES - begin_y;
595 view->ncols = ncols ? ncols : COLS - begin_x;
596 view->begin_y = begin_y;
597 view->begin_x = begin_x;
598 view->window = newwin(nlines, ncols, begin_y, begin_x);
599 if (view->window == NULL) {
600 view_close(view);
601 return NULL;
603 view->panel = new_panel(view->window);
604 if (view->panel == NULL ||
605 set_panel_userptr(view->panel, view) != OK) {
606 view_close(view);
607 return NULL;
610 keypad(view->window, TRUE);
611 return view;
614 static int
615 view_split_begin_x(int begin_x)
617 if (begin_x > 0 || COLS < 120)
618 return 0;
619 return (COLS - MAX(COLS / 2, 80));
622 static const struct got_error *view_resize(struct tog_view *);
624 static const struct got_error *
625 view_splitscreen(struct tog_view *view)
627 const struct got_error *err = NULL;
629 view->begin_y = 0;
630 view->begin_x = view_split_begin_x(0);
631 view->nlines = LINES;
632 view->ncols = COLS - view->begin_x;
633 view->lines = LINES;
634 view->cols = COLS;
635 err = view_resize(view);
636 if (err)
637 return err;
639 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
640 return got_error_from_errno("mvwin");
642 return NULL;
645 static const struct got_error *
646 view_fullscreen(struct tog_view *view)
648 const struct got_error *err = NULL;
650 view->begin_x = 0;
651 view->begin_y = 0;
652 view->nlines = LINES;
653 view->ncols = COLS;
654 view->lines = LINES;
655 view->cols = COLS;
656 err = view_resize(view);
657 if (err)
658 return err;
660 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
661 return got_error_from_errno("mvwin");
663 return NULL;
666 static int
667 view_is_parent_view(struct tog_view *view)
669 return view->parent == NULL;
672 static const struct got_error *
673 view_resize(struct tog_view *view)
675 int nlines, ncols;
677 if (view->lines > LINES)
678 nlines = view->nlines - (view->lines - LINES);
679 else
680 nlines = view->nlines + (LINES - view->lines);
682 if (view->cols > COLS)
683 ncols = view->ncols - (view->cols - COLS);
684 else
685 ncols = view->ncols + (COLS - view->cols);
687 if (wresize(view->window, nlines, ncols) == ERR)
688 return got_error_from_errno("wresize");
689 if (replace_panel(view->panel, view->window) == ERR)
690 return got_error_from_errno("replace_panel");
691 wclear(view->window);
693 view->nlines = nlines;
694 view->ncols = ncols;
695 view->lines = LINES;
696 view->cols = COLS;
698 if (view->child) {
699 view->child->begin_x = view_split_begin_x(view->begin_x);
700 if (view->child->begin_x == 0) {
701 view_fullscreen(view->child);
702 if (view->child->focussed)
703 show_panel(view->child->panel);
704 else
705 show_panel(view->panel);
706 } else {
707 view_splitscreen(view->child);
708 show_panel(view->child->panel);
712 return NULL;
715 static const struct got_error *
716 view_close_child(struct tog_view *view)
718 const struct got_error *err = NULL;
720 if (view->child == NULL)
721 return NULL;
723 err = view_close(view->child);
724 view->child = NULL;
725 return err;
728 static void
729 view_set_child(struct tog_view *view, struct tog_view *child)
731 view->child = child;
732 child->parent = view;
735 static int
736 view_is_splitscreen(struct tog_view *view)
738 return view->begin_x > 0;
741 static void
742 tog_resizeterm(void)
744 int cols, lines;
745 struct winsize size;
747 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
748 cols = 80; /* Default */
749 lines = 24;
750 } else {
751 cols = size.ws_col;
752 lines = size.ws_row;
754 resize_term(lines, cols);
757 static const struct got_error *
758 view_search_start(struct tog_view *view)
760 const struct got_error *err = NULL;
761 char pattern[1024];
762 int ret;
764 if (view->nlines < 1)
765 return NULL;
767 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
768 wclrtoeol(view->window);
770 nocbreak();
771 echo();
772 ret = wgetnstr(view->window, pattern, sizeof(pattern));
773 cbreak();
774 noecho();
775 if (ret == ERR)
776 return NULL;
778 if (view->searching) {
779 regfree(&view->regex);
780 view->searching = 0;
783 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
784 err = view->search_start(view);
785 if (err) {
786 regfree(&view->regex);
787 return err;
789 view->searching = TOG_SEARCH_FORWARD;
790 view->search_next_done = 0;
791 view->search_next(view);
794 return NULL;
797 static const struct got_error *
798 view_input(struct tog_view **new, int *done, struct tog_view *view,
799 struct tog_view_list_head *views)
801 const struct got_error *err = NULL;
802 struct tog_view *v;
803 int ch, errcode;
805 *new = NULL;
807 /* Clear "no matches" indicator. */
808 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
809 view->search_next_done == TOG_SEARCH_HAVE_NONE)
810 view->search_next_done = TOG_SEARCH_HAVE_MORE;
812 if (view->searching && !view->search_next_done) {
813 errcode = pthread_mutex_unlock(&tog_mutex);
814 if (errcode)
815 return got_error_set_errno(errcode,
816 "pthread_mutex_unlock");
817 pthread_yield();
818 errcode = pthread_mutex_lock(&tog_mutex);
819 if (errcode)
820 return got_error_set_errno(errcode,
821 "pthread_mutex_lock");
822 view->search_next(view);
823 return NULL;
826 nodelay(stdscr, FALSE);
827 /* Allow threads to make progress while we are waiting for input. */
828 errcode = pthread_mutex_unlock(&tog_mutex);
829 if (errcode)
830 return got_error_set_errno(errcode, "pthread_mutex_unlock");
831 ch = wgetch(view->window);
832 errcode = pthread_mutex_lock(&tog_mutex);
833 if (errcode)
834 return got_error_set_errno(errcode, "pthread_mutex_lock");
835 nodelay(stdscr, TRUE);
837 if (tog_sigwinch_received || tog_sigcont_received) {
838 tog_resizeterm();
839 tog_sigwinch_received = 0;
840 tog_sigcont_received = 0;
841 TAILQ_FOREACH(v, views, entry) {
842 err = view_resize(v);
843 if (err)
844 return err;
845 err = v->input(new, v, KEY_RESIZE);
846 if (err)
847 return err;
851 switch (ch) {
852 case ERR:
853 break;
854 case '\t':
855 if (view->child) {
856 view->focussed = 0;
857 view->child->focussed = 1;
858 view->focus_child = 1;
859 } else if (view->parent) {
860 view->focussed = 0;
861 view->parent->focussed = 1;
862 view->parent->focus_child = 0;
864 break;
865 case 'q':
866 err = view->input(new, view, ch);
867 view->dying = 1;
868 break;
869 case 'Q':
870 *done = 1;
871 break;
872 case 'f':
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL)
875 break;
876 if (view_is_splitscreen(view->child)) {
877 view->focussed = 0;
878 view->child->focussed = 1;
879 err = view_fullscreen(view->child);
880 } else
881 err = view_splitscreen(view->child);
882 if (err)
883 break;
884 err = view->child->input(new, view->child,
885 KEY_RESIZE);
886 } else {
887 if (view_is_splitscreen(view)) {
888 view->parent->focussed = 0;
889 view->focussed = 1;
890 err = view_fullscreen(view);
891 } else {
892 err = view_splitscreen(view);
894 if (err)
895 break;
896 err = view->input(new, view, KEY_RESIZE);
898 break;
899 case KEY_RESIZE:
900 break;
901 case '/':
902 if (view->search_start)
903 view_search_start(view);
904 else
905 err = view->input(new, view, ch);
906 break;
907 case 'N':
908 case 'n':
909 if (view->search_next) {
910 view->searching = (ch == 'n' ?
911 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
912 view->search_next_done = 0;
913 view->search_next(view);
914 } else
915 err = view->input(new, view, ch);
916 break;
917 default:
918 err = view->input(new, view, ch);
919 break;
922 return err;
925 void
926 view_vborder(struct tog_view *view)
928 PANEL *panel;
929 struct tog_view *view_above;
931 if (view->parent)
932 return view_vborder(view->parent);
934 panel = panel_above(view->panel);
935 if (panel == NULL)
936 return;
938 view_above = panel_userptr(panel);
939 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
940 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
943 int
944 view_needs_focus_indication(struct tog_view *view)
946 if (view_is_parent_view(view)) {
947 if (view->child == NULL || view->child->focussed)
948 return 0;
949 if (!view_is_splitscreen(view->child))
950 return 0;
951 } else if (!view_is_splitscreen(view))
952 return 0;
954 return view->focussed;
957 static const struct got_error *
958 view_loop(struct tog_view *view)
960 const struct got_error *err = NULL;
961 struct tog_view_list_head views;
962 struct tog_view *new_view;
963 int fast_refresh = 10;
964 int done = 0, errcode;
966 errcode = pthread_mutex_lock(&tog_mutex);
967 if (errcode)
968 return got_error_set_errno(errcode, "pthread_mutex_lock");
970 TAILQ_INIT(&views);
971 TAILQ_INSERT_HEAD(&views, view, entry);
973 view->focussed = 1;
974 err = view->show(view);
975 if (err)
976 return err;
977 update_panels();
978 doupdate();
979 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
980 /* Refresh fast during initialization, then become slower. */
981 if (fast_refresh && fast_refresh-- == 0)
982 halfdelay(10); /* switch to once per second */
984 err = view_input(&new_view, &done, view, &views);
985 if (err)
986 break;
987 if (view->dying) {
988 struct tog_view *v, *prev = NULL;
990 if (view_is_parent_view(view))
991 prev = TAILQ_PREV(view, tog_view_list_head,
992 entry);
993 else if (view->parent)
994 prev = view->parent;
996 if (view->parent) {
997 view->parent->child = NULL;
998 view->parent->focus_child = 0;
999 } else
1000 TAILQ_REMOVE(&views, view, entry);
1002 err = view_close(view);
1003 if (err)
1004 goto done;
1006 view = NULL;
1007 TAILQ_FOREACH(v, &views, entry) {
1008 if (v->focussed)
1009 break;
1011 if (view == NULL && new_view == NULL) {
1012 /* No view has focus. Try to pick one. */
1013 if (prev)
1014 view = prev;
1015 else if (!TAILQ_EMPTY(&views)) {
1016 view = TAILQ_LAST(&views,
1017 tog_view_list_head);
1019 if (view) {
1020 if (view->focus_child) {
1021 view->child->focussed = 1;
1022 view = view->child;
1023 } else
1024 view->focussed = 1;
1028 if (new_view) {
1029 struct tog_view *v, *t;
1030 /* Only allow one parent view per type. */
1031 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1032 if (v->type != new_view->type)
1033 continue;
1034 TAILQ_REMOVE(&views, v, entry);
1035 err = view_close(v);
1036 if (err)
1037 goto done;
1038 break;
1040 TAILQ_INSERT_TAIL(&views, new_view, entry);
1041 view = new_view;
1043 if (view) {
1044 if (view_is_parent_view(view)) {
1045 if (view->child && view->child->focussed)
1046 view = view->child;
1047 } else {
1048 if (view->parent && view->parent->focussed)
1049 view = view->parent;
1051 show_panel(view->panel);
1052 if (view->child && view_is_splitscreen(view->child))
1053 show_panel(view->child->panel);
1054 if (view->parent && view_is_splitscreen(view)) {
1055 err = view->parent->show(view->parent);
1056 if (err)
1057 goto done;
1059 err = view->show(view);
1060 if (err)
1061 goto done;
1062 if (view->child) {
1063 err = view->child->show(view->child);
1064 if (err)
1065 goto done;
1067 update_panels();
1068 doupdate();
1071 done:
1072 while (!TAILQ_EMPTY(&views)) {
1073 view = TAILQ_FIRST(&views);
1074 TAILQ_REMOVE(&views, view, entry);
1075 view_close(view);
1078 errcode = pthread_mutex_unlock(&tog_mutex);
1079 if (errcode && err == NULL)
1080 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1082 return err;
1085 __dead static void
1086 usage_log(void)
1088 endwin();
1089 fprintf(stderr,
1090 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1091 getprogname());
1092 exit(1);
1095 /* Create newly allocated wide-character string equivalent to a byte string. */
1096 static const struct got_error *
1097 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1099 char *vis = NULL;
1100 const struct got_error *err = NULL;
1102 *ws = NULL;
1103 *wlen = mbstowcs(NULL, s, 0);
1104 if (*wlen == (size_t)-1) {
1105 int vislen;
1106 if (errno != EILSEQ)
1107 return got_error_from_errno("mbstowcs");
1109 /* byte string invalid in current encoding; try to "fix" it */
1110 err = got_mbsavis(&vis, &vislen, s);
1111 if (err)
1112 return err;
1113 *wlen = mbstowcs(NULL, vis, 0);
1114 if (*wlen == (size_t)-1) {
1115 err = got_error_from_errno("mbstowcs"); /* give up */
1116 goto done;
1120 *ws = calloc(*wlen + 1, sizeof(**ws));
1121 if (*ws == NULL) {
1122 err = got_error_from_errno("calloc");
1123 goto done;
1126 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1127 err = got_error_from_errno("mbstowcs");
1128 done:
1129 free(vis);
1130 if (err) {
1131 free(*ws);
1132 *ws = NULL;
1133 *wlen = 0;
1135 return err;
1138 /* Format a line for display, ensuring that it won't overflow a width limit. */
1139 static const struct got_error *
1140 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1141 int col_tab_align)
1143 const struct got_error *err = NULL;
1144 int cols = 0;
1145 wchar_t *wline = NULL;
1146 size_t wlen;
1147 int i;
1149 *wlinep = NULL;
1150 *widthp = 0;
1152 err = mbs2ws(&wline, &wlen, line);
1153 if (err)
1154 return err;
1156 i = 0;
1157 while (i < wlen) {
1158 int width = wcwidth(wline[i]);
1160 if (width == 0) {
1161 i++;
1162 continue;
1165 if (width == 1 || width == 2) {
1166 if (cols + width > wlimit)
1167 break;
1168 cols += width;
1169 i++;
1170 } else if (width == -1) {
1171 if (wline[i] == L'\t') {
1172 width = TABSIZE -
1173 ((cols + col_tab_align) % TABSIZE);
1174 if (cols + width > wlimit)
1175 break;
1176 cols += width;
1178 i++;
1179 } else {
1180 err = got_error_from_errno("wcwidth");
1181 goto done;
1184 wline[i] = L'\0';
1185 if (widthp)
1186 *widthp = cols;
1187 done:
1188 if (err)
1189 free(wline);
1190 else
1191 *wlinep = wline;
1192 return err;
1195 static const struct got_error*
1196 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1197 struct got_object_id *id, struct got_repository *repo)
1199 static const struct got_error *err = NULL;
1200 struct got_reflist_entry *re;
1201 char *s;
1202 const char *name;
1204 *refs_str = NULL;
1206 SIMPLEQ_FOREACH(re, refs, entry) {
1207 struct got_tag_object *tag = NULL;
1208 struct got_object_id *ref_id;
1209 int cmp;
1211 name = got_ref_get_name(re->ref);
1212 if (strcmp(name, GOT_REF_HEAD) == 0)
1213 continue;
1214 if (strncmp(name, "refs/", 5) == 0)
1215 name += 5;
1216 if (strncmp(name, "got/", 4) == 0)
1217 continue;
1218 if (strncmp(name, "heads/", 6) == 0)
1219 name += 6;
1220 if (strncmp(name, "remotes/", 8) == 0) {
1221 name += 8;
1222 s = strstr(name, "/" GOT_REF_HEAD);
1223 if (s != NULL && s[strlen(s)] == '\0')
1224 continue;
1226 err = got_ref_resolve(&ref_id, repo, re->ref);
1227 if (err)
1228 break;
1229 if (strncmp(name, "tags/", 5) == 0) {
1230 err = got_object_open_as_tag(&tag, repo, ref_id);
1231 if (err) {
1232 if (err->code != GOT_ERR_OBJ_TYPE) {
1233 free(ref_id);
1234 break;
1236 /* Ref points at something other than a tag. */
1237 err = NULL;
1238 tag = NULL;
1241 cmp = got_object_id_cmp(tag ?
1242 got_object_tag_get_object_id(tag) : ref_id, id);
1243 free(ref_id);
1244 if (tag)
1245 got_object_tag_close(tag);
1246 if (cmp != 0)
1247 continue;
1248 s = *refs_str;
1249 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1250 s ? ", " : "", name) == -1) {
1251 err = got_error_from_errno("asprintf");
1252 free(s);
1253 *refs_str = NULL;
1254 break;
1256 free(s);
1259 return err;
1262 static const struct got_error *
1263 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1264 int col_tab_align)
1266 char *smallerthan, *at;
1268 smallerthan = strchr(author, '<');
1269 if (smallerthan && smallerthan[1] != '\0')
1270 author = smallerthan + 1;
1271 at = strchr(author, '@');
1272 if (at)
1273 *at = '\0';
1274 return format_line(wauthor, author_width, author, limit, col_tab_align);
1277 static const struct got_error *
1278 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1279 struct got_object_id *id, const size_t date_display_cols,
1280 int author_display_cols)
1282 struct tog_log_view_state *s = &view->state.log;
1283 const struct got_error *err = NULL;
1284 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1285 char *logmsg0 = NULL, *logmsg = NULL;
1286 char *author = NULL;
1287 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1288 int author_width, logmsg_width;
1289 char *newline, *line = NULL;
1290 int col, limit;
1291 const int avail = view->ncols;
1292 struct tm tm;
1293 time_t committer_time;
1294 struct tog_color *tc;
1296 committer_time = got_object_commit_get_committer_time(commit);
1297 if (localtime_r(&committer_time, &tm) == NULL)
1298 return got_error_from_errno("localtime_r");
1299 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1300 >= sizeof(datebuf))
1301 return got_error(GOT_ERR_NO_SPACE);
1303 if (avail <= date_display_cols)
1304 limit = MIN(sizeof(datebuf) - 1, avail);
1305 else
1306 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1307 tc = get_color(&s->colors, TOG_COLOR_DATE);
1308 if (tc)
1309 wattr_on(view->window,
1310 COLOR_PAIR(tc->colorpair), NULL);
1311 waddnstr(view->window, datebuf, limit);
1312 if (tc)
1313 wattr_off(view->window,
1314 COLOR_PAIR(tc->colorpair), NULL);
1315 col = limit;
1316 if (col > avail)
1317 goto done;
1319 if (avail >= 120) {
1320 char *id_str;
1321 err = got_object_id_str(&id_str, id);
1322 if (err)
1323 goto done;
1324 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1325 if (tc)
1326 wattr_on(view->window,
1327 COLOR_PAIR(tc->colorpair), NULL);
1328 wprintw(view->window, "%.8s ", id_str);
1329 if (tc)
1330 wattr_off(view->window,
1331 COLOR_PAIR(tc->colorpair), NULL);
1332 free(id_str);
1333 col += 9;
1334 if (col > avail)
1335 goto done;
1338 author = strdup(got_object_commit_get_author(commit));
1339 if (author == NULL) {
1340 err = got_error_from_errno("strdup");
1341 goto done;
1343 err = format_author(&wauthor, &author_width, author, avail - col, col);
1344 if (err)
1345 goto done;
1346 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1347 if (tc)
1348 wattr_on(view->window,
1349 COLOR_PAIR(tc->colorpair), NULL);
1350 waddwstr(view->window, wauthor);
1351 if (tc)
1352 wattr_off(view->window,
1353 COLOR_PAIR(tc->colorpair), NULL);
1354 col += author_width;
1355 while (col < avail && author_width < author_display_cols + 2) {
1356 waddch(view->window, ' ');
1357 col++;
1358 author_width++;
1360 if (col > avail)
1361 goto done;
1363 err = got_object_commit_get_logmsg(&logmsg0, commit);
1364 if (err)
1365 goto done;
1366 logmsg = logmsg0;
1367 while (*logmsg == '\n')
1368 logmsg++;
1369 newline = strchr(logmsg, '\n');
1370 if (newline)
1371 *newline = '\0';
1372 limit = avail - col;
1373 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1374 if (err)
1375 goto done;
1376 waddwstr(view->window, wlogmsg);
1377 col += logmsg_width;
1378 while (col < avail) {
1379 waddch(view->window, ' ');
1380 col++;
1382 done:
1383 free(logmsg0);
1384 free(wlogmsg);
1385 free(author);
1386 free(wauthor);
1387 free(line);
1388 return err;
1391 static struct commit_queue_entry *
1392 alloc_commit_queue_entry(struct got_commit_object *commit,
1393 struct got_object_id *id)
1395 struct commit_queue_entry *entry;
1397 entry = calloc(1, sizeof(*entry));
1398 if (entry == NULL)
1399 return NULL;
1401 entry->id = id;
1402 entry->commit = commit;
1403 return entry;
1406 static void
1407 pop_commit(struct commit_queue *commits)
1409 struct commit_queue_entry *entry;
1411 entry = TAILQ_FIRST(&commits->head);
1412 TAILQ_REMOVE(&commits->head, entry, entry);
1413 got_object_commit_close(entry->commit);
1414 commits->ncommits--;
1415 /* Don't free entry->id! It is owned by the commit graph. */
1416 free(entry);
1419 static void
1420 free_commits(struct commit_queue *commits)
1422 while (!TAILQ_EMPTY(&commits->head))
1423 pop_commit(commits);
1426 static const struct got_error *
1427 match_commit(int *have_match, struct got_object_id *id,
1428 struct got_commit_object *commit, regex_t *regex)
1430 const struct got_error *err = NULL;
1431 regmatch_t regmatch;
1432 char *id_str = NULL, *logmsg = NULL;
1434 *have_match = 0;
1436 err = got_object_id_str(&id_str, id);
1437 if (err)
1438 return err;
1440 err = got_object_commit_get_logmsg(&logmsg, commit);
1441 if (err)
1442 goto done;
1444 if (regexec(regex, got_object_commit_get_author(commit), 1,
1445 &regmatch, 0) == 0 ||
1446 regexec(regex, got_object_commit_get_committer(commit), 1,
1447 &regmatch, 0) == 0 ||
1448 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1449 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1450 *have_match = 1;
1451 done:
1452 free(id_str);
1453 free(logmsg);
1454 return err;
1457 static const struct got_error *
1458 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1459 int minqueue, struct got_repository *repo, const char *path,
1460 int *searching, int *search_next_done, regex_t *regex)
1462 const struct got_error *err = NULL;
1463 int nqueued = 0;
1466 * We keep all commits open throughout the lifetime of the log
1467 * view in order to avoid having to re-fetch commits from disk
1468 * while updating the display.
1470 while (nqueued < minqueue ||
1471 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1472 struct got_object_id *id;
1473 struct got_commit_object *commit;
1474 struct commit_queue_entry *entry;
1475 int errcode;
1477 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1478 if (err || id == NULL)
1479 break;
1481 err = got_object_open_as_commit(&commit, repo, id);
1482 if (err)
1483 break;
1484 entry = alloc_commit_queue_entry(commit, id);
1485 if (entry == NULL) {
1486 err = got_error_from_errno("alloc_commit_queue_entry");
1487 break;
1490 errcode = pthread_mutex_lock(&tog_mutex);
1491 if (errcode) {
1492 err = got_error_set_errno(errcode,
1493 "pthread_mutex_lock");
1494 break;
1497 entry->idx = commits->ncommits;
1498 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1499 nqueued++;
1500 commits->ncommits++;
1502 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1503 int have_match;
1504 err = match_commit(&have_match, id, commit, regex);
1505 if (err)
1506 break;
1507 if (have_match)
1508 *search_next_done = TOG_SEARCH_HAVE_MORE;
1511 errcode = pthread_mutex_unlock(&tog_mutex);
1512 if (errcode && err == NULL)
1513 err = got_error_set_errno(errcode,
1514 "pthread_mutex_unlock");
1515 if (err)
1516 break;
1519 return err;
1522 static void
1523 select_commit(struct tog_log_view_state *s)
1525 struct commit_queue_entry *entry;
1526 int ncommits = 0;
1528 entry = s->first_displayed_entry;
1529 while (entry) {
1530 if (ncommits == s->selected) {
1531 s->selected_entry = entry;
1532 break;
1534 entry = TAILQ_NEXT(entry, entry);
1535 ncommits++;
1539 static const struct got_error *
1540 draw_commits(struct tog_view *view)
1542 const struct got_error *err = NULL;
1543 struct tog_log_view_state *s = &view->state.log;
1544 struct commit_queue_entry *entry = s->selected_entry;
1545 const int limit = view->nlines;
1546 int width;
1547 int ncommits, author_cols = 4;
1548 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1549 char *refs_str = NULL;
1550 wchar_t *wline;
1551 struct tog_color *tc;
1552 static const size_t date_display_cols = 12;
1554 if (s->selected_entry &&
1555 !(view->searching && view->search_next_done == 0)) {
1556 err = got_object_id_str(&id_str, s->selected_entry->id);
1557 if (err)
1558 return err;
1559 err = build_refs_str(&refs_str, &s->refs,
1560 s->selected_entry->id, s->repo);
1561 if (err)
1562 goto done;
1565 if (s->thread_args.commits_needed == 0)
1566 halfdelay(10); /* disable fast refresh */
1568 if (s->thread_args.commits_needed > 0) {
1569 if (asprintf(&ncommits_str, " [%d/%d] %s",
1570 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1571 (view->searching && !view->search_next_done) ?
1572 "searching..." : "loading...") == -1) {
1573 err = got_error_from_errno("asprintf");
1574 goto done;
1576 } else {
1577 const char *search_str = NULL;
1579 if (view->searching) {
1580 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1581 search_str = "no more matches";
1582 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1583 search_str = "no matches found";
1584 else if (!view->search_next_done)
1585 search_str = "searching...";
1588 if (asprintf(&ncommits_str, " [%d/%d] %s",
1589 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1590 search_str ? search_str :
1591 (refs_str ? refs_str : "")) == -1) {
1592 err = got_error_from_errno("asprintf");
1593 goto done;
1597 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1598 if (asprintf(&header, "commit %s %s%s",
1599 id_str ? id_str : "........................................",
1600 s->in_repo_path, ncommits_str) == -1) {
1601 err = got_error_from_errno("asprintf");
1602 header = NULL;
1603 goto done;
1605 } else if (asprintf(&header, "commit %s%s",
1606 id_str ? id_str : "........................................",
1607 ncommits_str) == -1) {
1608 err = got_error_from_errno("asprintf");
1609 header = NULL;
1610 goto done;
1612 err = format_line(&wline, &width, header, view->ncols, 0);
1613 if (err)
1614 goto done;
1616 werase(view->window);
1618 if (view_needs_focus_indication(view))
1619 wstandout(view->window);
1620 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1621 if (tc)
1622 wattr_on(view->window,
1623 COLOR_PAIR(tc->colorpair), NULL);
1624 waddwstr(view->window, wline);
1625 if (tc)
1626 wattr_off(view->window,
1627 COLOR_PAIR(tc->colorpair), NULL);
1628 while (width < view->ncols) {
1629 waddch(view->window, ' ');
1630 width++;
1632 if (view_needs_focus_indication(view))
1633 wstandend(view->window);
1634 free(wline);
1635 if (limit <= 1)
1636 goto done;
1638 /* Grow author column size if necessary. */
1639 entry = s->first_displayed_entry;
1640 ncommits = 0;
1641 while (entry) {
1642 char *author;
1643 wchar_t *wauthor;
1644 int width;
1645 if (ncommits >= limit - 1)
1646 break;
1647 author = strdup(got_object_commit_get_author(entry->commit));
1648 if (author == NULL) {
1649 err = got_error_from_errno("strdup");
1650 goto done;
1652 err = format_author(&wauthor, &width, author, COLS,
1653 date_display_cols);
1654 if (author_cols < width)
1655 author_cols = width;
1656 free(wauthor);
1657 free(author);
1658 ncommits++;
1659 entry = TAILQ_NEXT(entry, entry);
1662 entry = s->first_displayed_entry;
1663 s->last_displayed_entry = s->first_displayed_entry;
1664 ncommits = 0;
1665 while (entry) {
1666 if (ncommits >= limit - 1)
1667 break;
1668 if (ncommits == s->selected)
1669 wstandout(view->window);
1670 err = draw_commit(view, entry->commit, entry->id,
1671 date_display_cols, author_cols);
1672 if (ncommits == s->selected)
1673 wstandend(view->window);
1674 if (err)
1675 goto done;
1676 ncommits++;
1677 s->last_displayed_entry = entry;
1678 entry = TAILQ_NEXT(entry, entry);
1681 view_vborder(view);
1682 done:
1683 free(id_str);
1684 free(refs_str);
1685 free(ncommits_str);
1686 free(header);
1687 return err;
1690 static void
1691 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1693 struct commit_queue_entry *entry;
1694 int nscrolled = 0;
1696 entry = TAILQ_FIRST(&s->commits.head);
1697 if (s->first_displayed_entry == entry)
1698 return;
1700 entry = s->first_displayed_entry;
1701 while (entry && nscrolled < maxscroll) {
1702 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1703 if (entry) {
1704 s->first_displayed_entry = entry;
1705 nscrolled++;
1710 static const struct got_error *
1711 trigger_log_thread(struct tog_view *view, int wait)
1713 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1714 int errcode;
1716 halfdelay(1); /* fast refresh while loading commits */
1718 while (ta->commits_needed > 0) {
1719 if (ta->log_complete)
1720 break;
1722 /* Wake the log thread. */
1723 errcode = pthread_cond_signal(&ta->need_commits);
1724 if (errcode)
1725 return got_error_set_errno(errcode,
1726 "pthread_cond_signal");
1729 * The mutex will be released while the view loop waits
1730 * in wgetch(), at which time the log thread will run.
1732 if (!wait)
1733 break;
1735 /* Display progress update in log view. */
1736 show_log_view(view);
1737 update_panels();
1738 doupdate();
1740 /* Wait right here while next commit is being loaded. */
1741 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1742 if (errcode)
1743 return got_error_set_errno(errcode,
1744 "pthread_cond_wait");
1746 /* Display progress update in log view. */
1747 show_log_view(view);
1748 update_panels();
1749 doupdate();
1752 return NULL;
1755 static const struct got_error *
1756 log_scroll_down(struct tog_view *view, int maxscroll)
1758 struct tog_log_view_state *s = &view->state.log;
1759 const struct got_error *err = NULL;
1760 struct commit_queue_entry *pentry;
1761 int nscrolled = 0, ncommits_needed;
1763 if (s->last_displayed_entry == NULL)
1764 return NULL;
1766 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1767 if (s->commits.ncommits < ncommits_needed &&
1768 !s->thread_args.log_complete) {
1770 * Ask the log thread for required amount of commits.
1772 s->thread_args.commits_needed += maxscroll;
1773 err = trigger_log_thread(view, 1);
1774 if (err)
1775 return err;
1778 do {
1779 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1780 if (pentry == NULL)
1781 break;
1783 s->last_displayed_entry = pentry;
1785 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1786 if (pentry == NULL)
1787 break;
1788 s->first_displayed_entry = pentry;
1789 } while (++nscrolled < maxscroll);
1791 return err;
1794 static const struct got_error *
1795 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1796 struct got_commit_object *commit, struct got_object_id *commit_id,
1797 struct tog_view *log_view, struct got_repository *repo)
1799 const struct got_error *err;
1800 struct got_object_qid *parent_id;
1801 struct tog_view *diff_view;
1803 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1804 if (diff_view == NULL)
1805 return got_error_from_errno("view_open");
1807 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1808 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1809 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1810 if (err == NULL)
1811 *new_view = diff_view;
1812 return err;
1815 static const struct got_error *
1816 tree_view_visit_subtree(struct tog_tree_view_state *s,
1817 struct got_tree_object *subtree)
1819 struct tog_parent_tree *parent;
1821 parent = calloc(1, sizeof(*parent));
1822 if (parent == NULL)
1823 return got_error_from_errno("calloc");
1825 parent->tree = s->tree;
1826 parent->first_displayed_entry = s->first_displayed_entry;
1827 parent->selected_entry = s->selected_entry;
1828 parent->selected = s->selected;
1829 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1830 s->tree = subtree;
1831 s->selected = 0;
1832 s->first_displayed_entry = NULL;
1833 return NULL;
1836 static const struct got_error *
1837 tree_view_walk_path(struct tog_tree_view_state *s,
1838 struct got_object_id *commit_id, const char *path)
1840 const struct got_error *err = NULL;
1841 struct got_tree_object *tree = NULL;
1842 const char *p;
1843 char *slash, *subpath = NULL;
1845 /* Walk the path and open corresponding tree objects. */
1846 p = path;
1847 while (*p) {
1848 struct got_tree_entry *te;
1849 struct got_object_id *tree_id;
1850 char *te_name;
1852 while (p[0] == '/')
1853 p++;
1855 /* Ensure the correct subtree entry is selected. */
1856 slash = strchr(p, '/');
1857 if (slash == NULL)
1858 te_name = strdup(p);
1859 else
1860 te_name = strndup(p, slash - p);
1861 if (te_name == NULL) {
1862 err = got_error_from_errno("strndup");
1863 break;
1865 te = got_object_tree_find_entry(s->tree, te_name);
1866 if (te == NULL) {
1867 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1868 free(te_name);
1869 break;
1871 free(te_name);
1872 s->first_displayed_entry = s->selected_entry = te;
1874 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1875 break; /* jump to this file's entry */
1877 slash = strchr(p, '/');
1878 if (slash)
1879 subpath = strndup(path, slash - path);
1880 else
1881 subpath = strdup(path);
1882 if (subpath == NULL) {
1883 err = got_error_from_errno("strdup");
1884 break;
1887 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1888 subpath);
1889 if (err)
1890 break;
1892 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1893 free(tree_id);
1894 if (err)
1895 break;
1897 err = tree_view_visit_subtree(s, tree);
1898 if (err) {
1899 got_object_tree_close(tree);
1900 break;
1902 if (slash == NULL)
1903 break;
1904 free(subpath);
1905 subpath = NULL;
1906 p = slash;
1909 free(subpath);
1910 return err;
1913 static const struct got_error *
1914 browse_commit_tree(struct tog_view **new_view, int begin_x,
1915 struct commit_queue_entry *entry, const char *path,
1916 struct got_repository *repo)
1918 const struct got_error *err = NULL;
1919 struct got_tree_object *tree;
1920 struct tog_tree_view_state *s;
1921 struct tog_view *tree_view;
1923 err = got_object_open_as_tree(&tree, repo,
1924 got_object_commit_get_tree_id(entry->commit));
1925 if (err)
1926 return err;
1928 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1929 if (tree_view == NULL)
1930 return got_error_from_errno("view_open");
1932 err = open_tree_view(tree_view, tree, entry->id, repo);
1933 if (err) {
1934 got_object_tree_close(tree);
1935 return err;
1937 s = &tree_view->state.tree;
1939 *new_view = tree_view;
1941 if (got_path_is_root_dir(path))
1942 return NULL;
1944 return tree_view_walk_path(s, entry->id, path);
1947 static const struct got_error *
1948 block_signals_used_by_main_thread(void)
1950 sigset_t sigset;
1951 int errcode;
1953 if (sigemptyset(&sigset) == -1)
1954 return got_error_from_errno("sigemptyset");
1956 /* tog handles SIGWINCH and SIGCONT */
1957 if (sigaddset(&sigset, SIGWINCH) == -1)
1958 return got_error_from_errno("sigaddset");
1959 if (sigaddset(&sigset, SIGCONT) == -1)
1960 return got_error_from_errno("sigaddset");
1962 /* ncurses handles SIGTSTP */
1963 if (sigaddset(&sigset, SIGTSTP) == -1)
1964 return got_error_from_errno("sigaddset");
1966 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1967 if (errcode)
1968 return got_error_set_errno(errcode, "pthread_sigmask");
1970 return NULL;
1973 static void *
1974 log_thread(void *arg)
1976 const struct got_error *err = NULL;
1977 int errcode = 0;
1978 struct tog_log_thread_args *a = arg;
1979 int done = 0;
1981 err = block_signals_used_by_main_thread();
1982 if (err)
1983 return (void *)err;
1985 while (!done && !err && !tog_sigpipe_received) {
1986 err = queue_commits(a->graph, a->commits, 1, a->repo,
1987 a->in_repo_path, a->searching, a->search_next_done,
1988 a->regex);
1989 if (err) {
1990 if (err->code != GOT_ERR_ITER_COMPLETED)
1991 return (void *)err;
1992 err = NULL;
1993 done = 1;
1994 } else if (a->commits_needed > 0)
1995 a->commits_needed--;
1997 errcode = pthread_mutex_lock(&tog_mutex);
1998 if (errcode) {
1999 err = got_error_set_errno(errcode,
2000 "pthread_mutex_lock");
2001 break;
2002 } else if (*a->quit)
2003 done = 1;
2004 else if (*a->first_displayed_entry == NULL) {
2005 *a->first_displayed_entry =
2006 TAILQ_FIRST(&a->commits->head);
2007 *a->selected_entry = *a->first_displayed_entry;
2010 errcode = pthread_cond_signal(&a->commit_loaded);
2011 if (errcode) {
2012 err = got_error_set_errno(errcode,
2013 "pthread_cond_signal");
2014 pthread_mutex_unlock(&tog_mutex);
2015 break;
2018 if (done)
2019 a->commits_needed = 0;
2020 else {
2021 if (a->commits_needed == 0) {
2022 errcode = pthread_cond_wait(&a->need_commits,
2023 &tog_mutex);
2024 if (errcode)
2025 err = got_error_set_errno(errcode,
2026 "pthread_cond_wait");
2030 errcode = pthread_mutex_unlock(&tog_mutex);
2031 if (errcode && err == NULL)
2032 err = got_error_set_errno(errcode,
2033 "pthread_mutex_unlock");
2035 a->log_complete = 1;
2036 return (void *)err;
2039 static const struct got_error *
2040 stop_log_thread(struct tog_log_view_state *s)
2042 const struct got_error *err = NULL;
2043 int errcode;
2045 if (s->thread) {
2046 s->quit = 1;
2047 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2048 if (errcode)
2049 return got_error_set_errno(errcode,
2050 "pthread_cond_signal");
2051 errcode = pthread_mutex_unlock(&tog_mutex);
2052 if (errcode)
2053 return got_error_set_errno(errcode,
2054 "pthread_mutex_unlock");
2055 errcode = pthread_join(s->thread, (void **)&err);
2056 if (errcode)
2057 return got_error_set_errno(errcode, "pthread_join");
2058 errcode = pthread_mutex_lock(&tog_mutex);
2059 if (errcode)
2060 return got_error_set_errno(errcode,
2061 "pthread_mutex_lock");
2062 s->thread = NULL;
2065 if (s->thread_args.repo) {
2066 got_repo_close(s->thread_args.repo);
2067 s->thread_args.repo = NULL;
2070 if (s->thread_args.graph) {
2071 got_commit_graph_close(s->thread_args.graph);
2072 s->thread_args.graph = NULL;
2075 return err;
2078 static const struct got_error *
2079 close_log_view(struct tog_view *view)
2081 const struct got_error *err = NULL;
2082 struct tog_log_view_state *s = &view->state.log;
2083 int errcode;
2085 err = stop_log_thread(s);
2087 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2088 if (errcode && err == NULL)
2089 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2091 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2092 if (errcode && err == NULL)
2093 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2095 free_commits(&s->commits);
2096 free(s->in_repo_path);
2097 s->in_repo_path = NULL;
2098 free(s->start_id);
2099 s->start_id = NULL;
2100 got_ref_list_free(&s->refs);
2101 return err;
2104 static const struct got_error *
2105 search_start_log_view(struct tog_view *view)
2107 struct tog_log_view_state *s = &view->state.log;
2109 s->matched_entry = NULL;
2110 s->search_entry = NULL;
2111 return NULL;
2114 static const struct got_error *
2115 search_next_log_view(struct tog_view *view)
2117 const struct got_error *err = NULL;
2118 struct tog_log_view_state *s = &view->state.log;
2119 struct commit_queue_entry *entry;
2121 /* Display progress update in log view. */
2122 show_log_view(view);
2123 update_panels();
2124 doupdate();
2126 if (s->search_entry) {
2127 int errcode, ch;
2128 errcode = pthread_mutex_unlock(&tog_mutex);
2129 if (errcode)
2130 return got_error_set_errno(errcode,
2131 "pthread_mutex_unlock");
2132 ch = wgetch(view->window);
2133 errcode = pthread_mutex_lock(&tog_mutex);
2134 if (errcode)
2135 return got_error_set_errno(errcode,
2136 "pthread_mutex_lock");
2137 if (ch == KEY_BACKSPACE) {
2138 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2139 return NULL;
2141 if (view->searching == TOG_SEARCH_FORWARD)
2142 entry = TAILQ_NEXT(s->search_entry, entry);
2143 else
2144 entry = TAILQ_PREV(s->search_entry,
2145 commit_queue_head, entry);
2146 } else if (s->matched_entry) {
2147 if (view->searching == TOG_SEARCH_FORWARD)
2148 entry = TAILQ_NEXT(s->matched_entry, entry);
2149 else
2150 entry = TAILQ_PREV(s->matched_entry,
2151 commit_queue_head, entry);
2152 } else {
2153 if (view->searching == TOG_SEARCH_FORWARD)
2154 entry = TAILQ_FIRST(&s->commits.head);
2155 else
2156 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2159 while (1) {
2160 int have_match = 0;
2162 if (entry == NULL) {
2163 if (s->thread_args.log_complete ||
2164 view->searching == TOG_SEARCH_BACKWARD) {
2165 view->search_next_done =
2166 (s->matched_entry == NULL ?
2167 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2168 s->search_entry = NULL;
2169 return NULL;
2172 * Poke the log thread for more commits and return,
2173 * allowing the main loop to make progress. Search
2174 * will resume at s->search_entry once we come back.
2176 s->thread_args.commits_needed++;
2177 return trigger_log_thread(view, 0);
2180 err = match_commit(&have_match, entry->id, entry->commit,
2181 &view->regex);
2182 if (err)
2183 break;
2184 if (have_match) {
2185 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2186 s->matched_entry = entry;
2187 break;
2190 s->search_entry = entry;
2191 if (view->searching == TOG_SEARCH_FORWARD)
2192 entry = TAILQ_NEXT(entry, entry);
2193 else
2194 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2197 if (s->matched_entry) {
2198 int cur = s->selected_entry->idx;
2199 while (cur < s->matched_entry->idx) {
2200 err = input_log_view(NULL, view, KEY_DOWN);
2201 if (err)
2202 return err;
2203 cur++;
2205 while (cur > s->matched_entry->idx) {
2206 err = input_log_view(NULL, view, KEY_UP);
2207 if (err)
2208 return err;
2209 cur--;
2213 s->search_entry = NULL;
2215 return NULL;
2218 static const struct got_error *
2219 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2220 struct got_repository *repo, const char *head_ref_name,
2221 const char *in_repo_path, int log_branches)
2223 const struct got_error *err = NULL;
2224 struct tog_log_view_state *s = &view->state.log;
2225 struct got_repository *thread_repo = NULL;
2226 struct got_commit_graph *thread_graph = NULL;
2227 int errcode;
2229 SIMPLEQ_INIT(&s->refs);
2231 if (in_repo_path != s->in_repo_path) {
2232 free(s->in_repo_path);
2233 s->in_repo_path = strdup(in_repo_path);
2234 if (s->in_repo_path == NULL)
2235 return got_error_from_errno("strdup");
2238 /* The commit queue only contains commits being displayed. */
2239 TAILQ_INIT(&s->commits.head);
2240 s->commits.ncommits = 0;
2242 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2243 if (err)
2244 goto done;
2246 s->repo = repo;
2247 s->head_ref_name = head_ref_name;
2248 s->start_id = got_object_id_dup(start_id);
2249 if (s->start_id == NULL) {
2250 err = got_error_from_errno("got_object_id_dup");
2251 goto done;
2253 s->log_branches = log_branches;
2255 SIMPLEQ_INIT(&s->colors);
2256 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2257 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2258 get_color_value("TOG_COLOR_COMMIT"));
2259 if (err)
2260 goto done;
2261 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2262 get_color_value("TOG_COLOR_AUTHOR"));
2263 if (err) {
2264 free_colors(&s->colors);
2265 goto done;
2267 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2268 get_color_value("TOG_COLOR_DATE"));
2269 if (err) {
2270 free_colors(&s->colors);
2271 goto done;
2275 view->show = show_log_view;
2276 view->input = input_log_view;
2277 view->close = close_log_view;
2278 view->search_start = search_start_log_view;
2279 view->search_next = search_next_log_view;
2281 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2282 if (err)
2283 goto done;
2284 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2285 !s->log_branches);
2286 if (err)
2287 goto done;
2288 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2289 s->repo, NULL, NULL);
2290 if (err)
2291 goto done;
2293 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2294 if (errcode) {
2295 err = got_error_set_errno(errcode, "pthread_cond_init");
2296 goto done;
2298 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2299 if (errcode) {
2300 err = got_error_set_errno(errcode, "pthread_cond_init");
2301 goto done;
2304 s->thread_args.commits_needed = view->nlines;
2305 s->thread_args.graph = thread_graph;
2306 s->thread_args.commits = &s->commits;
2307 s->thread_args.in_repo_path = s->in_repo_path;
2308 s->thread_args.start_id = s->start_id;
2309 s->thread_args.repo = thread_repo;
2310 s->thread_args.log_complete = 0;
2311 s->thread_args.quit = &s->quit;
2312 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2313 s->thread_args.selected_entry = &s->selected_entry;
2314 s->thread_args.searching = &view->searching;
2315 s->thread_args.search_next_done = &view->search_next_done;
2316 s->thread_args.regex = &view->regex;
2317 done:
2318 if (err)
2319 close_log_view(view);
2320 return err;
2323 static const struct got_error *
2324 show_log_view(struct tog_view *view)
2326 const struct got_error *err;
2327 struct tog_log_view_state *s = &view->state.log;
2329 if (s->thread == NULL) {
2330 int errcode = pthread_create(&s->thread, NULL, log_thread,
2331 &s->thread_args);
2332 if (errcode)
2333 return got_error_set_errno(errcode, "pthread_create");
2334 if (s->thread_args.commits_needed > 0) {
2335 err = trigger_log_thread(view, 1);
2336 if (err)
2337 return err;
2341 return draw_commits(view);
2344 static const struct got_error *
2345 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2347 const struct got_error *err = NULL;
2348 struct tog_log_view_state *s = &view->state.log;
2349 char *parent_path, *in_repo_path = NULL;
2350 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2351 struct tog_view *ref_view = NULL;
2352 int begin_x = 0;
2353 struct got_object_id *start_id;
2355 switch (ch) {
2356 case 'q':
2357 s->quit = 1;
2358 break;
2359 case 'k':
2360 case KEY_UP:
2361 case '<':
2362 case ',':
2363 if (s->first_displayed_entry == NULL)
2364 break;
2365 if (s->selected > 0)
2366 s->selected--;
2367 else
2368 log_scroll_up(s, 1);
2369 select_commit(s);
2370 break;
2371 case KEY_PPAGE:
2372 case CTRL('b'):
2373 if (s->first_displayed_entry == NULL)
2374 break;
2375 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2376 s->selected = 0;
2377 else
2378 log_scroll_up(s, view->nlines - 1);
2379 select_commit(s);
2380 break;
2381 case 'j':
2382 case KEY_DOWN:
2383 case '>':
2384 case '.':
2385 if (s->first_displayed_entry == NULL)
2386 break;
2387 if (s->selected < MIN(view->nlines - 2,
2388 s->commits.ncommits - 1))
2389 s->selected++;
2390 else {
2391 err = log_scroll_down(view, 1);
2392 if (err)
2393 break;
2395 select_commit(s);
2396 break;
2397 case KEY_NPAGE:
2398 case CTRL('f'): {
2399 struct commit_queue_entry *first;
2400 first = s->first_displayed_entry;
2401 if (first == NULL)
2402 break;
2403 err = log_scroll_down(view, view->nlines - 1);
2404 if (err)
2405 break;
2406 if (first == s->first_displayed_entry &&
2407 s->selected < MIN(view->nlines - 2,
2408 s->commits.ncommits - 1)) {
2409 /* can't scroll further down */
2410 s->selected = MIN(view->nlines - 2,
2411 s->commits.ncommits - 1);
2413 select_commit(s);
2414 break;
2416 case KEY_RESIZE:
2417 if (s->selected > view->nlines - 2)
2418 s->selected = view->nlines - 2;
2419 if (s->selected > s->commits.ncommits - 1)
2420 s->selected = s->commits.ncommits - 1;
2421 select_commit(s);
2422 if (s->commits.ncommits < view->nlines - 1 &&
2423 !s->thread_args.log_complete) {
2424 s->thread_args.commits_needed += (view->nlines - 1) -
2425 s->commits.ncommits;
2426 err = trigger_log_thread(view, 1);
2428 break;
2429 case KEY_ENTER:
2430 case ' ':
2431 case '\r':
2432 if (s->selected_entry == NULL)
2433 break;
2434 if (view_is_parent_view(view))
2435 begin_x = view_split_begin_x(view->begin_x);
2436 err = open_diff_view_for_commit(&diff_view, begin_x,
2437 s->selected_entry->commit, s->selected_entry->id,
2438 view, s->repo);
2439 if (err)
2440 break;
2441 view->focussed = 0;
2442 diff_view->focussed = 1;
2443 if (view_is_parent_view(view)) {
2444 err = view_close_child(view);
2445 if (err)
2446 return err;
2447 view_set_child(view, diff_view);
2448 view->focus_child = 1;
2449 } else
2450 *new_view = diff_view;
2451 break;
2452 case 't':
2453 if (s->selected_entry == NULL)
2454 break;
2455 if (view_is_parent_view(view))
2456 begin_x = view_split_begin_x(view->begin_x);
2457 err = browse_commit_tree(&tree_view, begin_x,
2458 s->selected_entry, s->in_repo_path, s->repo);
2459 if (err)
2460 break;
2461 view->focussed = 0;
2462 tree_view->focussed = 1;
2463 if (view_is_parent_view(view)) {
2464 err = view_close_child(view);
2465 if (err)
2466 return err;
2467 view_set_child(view, tree_view);
2468 view->focus_child = 1;
2469 } else
2470 *new_view = tree_view;
2471 break;
2472 case KEY_BACKSPACE:
2473 if (got_path_cmp(s->in_repo_path, "/",
2474 strlen(s->in_repo_path), 1) == 0)
2475 break;
2476 err = got_path_dirname(&parent_path, s->in_repo_path);
2477 if (err)
2478 return err;
2479 err = stop_log_thread(s);
2480 if (err) {
2481 free(parent_path);
2482 return err;
2484 lv = view_open(view->nlines, view->ncols,
2485 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2486 if (lv == NULL) {
2487 free(parent_path);
2488 return got_error_from_errno("view_open");
2490 err = open_log_view(lv, s->start_id, s->repo, s->head_ref_name,
2491 parent_path, s->log_branches);
2492 free(parent_path);
2493 if (err)
2494 return err;;
2495 view->focussed = 0;
2496 lv->focussed = 1;
2497 if (view_is_parent_view(view))
2498 *new_view = lv;
2499 else
2500 view_set_child(view->parent, lv);
2501 break;
2502 case CTRL('l'):
2503 err = stop_log_thread(s);
2504 if (err)
2505 return err;
2506 lv = view_open(view->nlines, view->ncols,
2507 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2508 if (lv == NULL)
2509 return got_error_from_errno("view_open");
2510 err = got_repo_match_object_id(&start_id, NULL,
2511 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2512 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2513 if (err) {
2514 view_close(lv);
2515 return err;
2517 in_repo_path = strdup(s->in_repo_path);
2518 if (in_repo_path == NULL) {
2519 free(start_id);
2520 view_close(lv);
2521 return got_error_from_errno("strdup");
2523 err = open_log_view(lv, start_id, s->repo, s->head_ref_name,
2524 in_repo_path, s->log_branches);
2525 if (err) {
2526 free(start_id);
2527 view_close(lv);
2528 return err;;
2530 view->dying = 1;
2531 *new_view = lv;
2532 break;
2533 case 'B':
2534 s->log_branches = !s->log_branches;
2535 err = stop_log_thread(s);
2536 if (err)
2537 return err;
2538 lv = view_open(view->nlines, view->ncols,
2539 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2540 if (lv == NULL)
2541 return got_error_from_errno("view_open");
2542 err = open_log_view(lv, s->start_id, s->repo,
2543 s->head_ref_name, s->in_repo_path, s->log_branches);
2544 if (err) {
2545 view_close(lv);
2546 return err;;
2548 view->dying = 1;
2549 *new_view = lv;
2550 break;
2551 case 'r':
2552 if (view_is_parent_view(view))
2553 begin_x = view_split_begin_x(view->begin_x);
2554 ref_view = view_open(view->nlines, view->ncols,
2555 view->begin_y, begin_x, TOG_VIEW_REF);
2556 if (ref_view == NULL)
2557 return got_error_from_errno("view_open");
2558 err = open_ref_view(ref_view, s->repo);
2559 if (err) {
2560 view_close(ref_view);
2561 return err;
2563 view->focussed = 0;
2564 ref_view->focussed = 1;
2565 if (view_is_parent_view(view)) {
2566 err = view_close_child(view);
2567 if (err)
2568 return err;
2569 view_set_child(view, ref_view);
2570 view->focus_child = 1;
2571 } else
2572 *new_view = ref_view;
2573 break;
2574 default:
2575 break;
2578 return err;
2581 static const struct got_error *
2582 apply_unveil(const char *repo_path, const char *worktree_path)
2584 const struct got_error *error;
2586 #ifdef PROFILE
2587 if (unveil("gmon.out", "rwc") != 0)
2588 return got_error_from_errno2("unveil", "gmon.out");
2589 #endif
2590 if (repo_path && unveil(repo_path, "r") != 0)
2591 return got_error_from_errno2("unveil", repo_path);
2593 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2594 return got_error_from_errno2("unveil", worktree_path);
2596 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2597 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2599 error = got_privsep_unveil_exec_helpers();
2600 if (error != NULL)
2601 return error;
2603 if (unveil(NULL, NULL) != 0)
2604 return got_error_from_errno("unveil");
2606 return NULL;
2609 static void
2610 init_curses(void)
2612 initscr();
2613 cbreak();
2614 halfdelay(1); /* Do fast refresh while initial view is loading. */
2615 noecho();
2616 nonl();
2617 intrflush(stdscr, FALSE);
2618 keypad(stdscr, TRUE);
2619 curs_set(0);
2620 if (getenv("TOG_COLORS") != NULL) {
2621 start_color();
2622 use_default_colors();
2624 signal(SIGWINCH, tog_sigwinch);
2625 signal(SIGPIPE, tog_sigpipe);
2626 signal(SIGCONT, tog_sigcont);
2629 static const struct got_error *
2630 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2631 struct got_repository *repo, struct got_worktree *worktree)
2633 const struct got_error *err = NULL;
2635 if (argc == 0) {
2636 *in_repo_path = strdup("/");
2637 if (*in_repo_path == NULL)
2638 return got_error_from_errno("strdup");
2639 return NULL;
2642 if (worktree) {
2643 const char *prefix = got_worktree_get_path_prefix(worktree);
2644 char *p;
2646 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2647 if (err)
2648 return err;
2649 if (asprintf(in_repo_path, "%s%s%s", prefix,
2650 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2651 p) == -1) {
2652 err = got_error_from_errno("asprintf");
2653 *in_repo_path = NULL;
2655 free(p);
2656 } else
2657 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2659 return err;
2662 static const struct got_error *
2663 cmd_log(int argc, char *argv[])
2665 const struct got_error *error;
2666 struct got_repository *repo = NULL;
2667 struct got_worktree *worktree = NULL;
2668 struct got_object_id *start_id = NULL;
2669 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2670 char *start_commit = NULL, *label = NULL;
2671 struct got_reference *ref = NULL;
2672 const char *head_ref_name = NULL;
2673 int ch, log_branches = 0;
2674 struct tog_view *view;
2676 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2677 switch (ch) {
2678 case 'b':
2679 log_branches = 1;
2680 break;
2681 case 'c':
2682 start_commit = optarg;
2683 break;
2684 case 'r':
2685 repo_path = realpath(optarg, NULL);
2686 if (repo_path == NULL)
2687 return got_error_from_errno2("realpath",
2688 optarg);
2689 break;
2690 default:
2691 usage_log();
2692 /* NOTREACHED */
2696 argc -= optind;
2697 argv += optind;
2699 if (argc > 1)
2700 usage_log();
2702 cwd = getcwd(NULL, 0);
2703 if (cwd == NULL)
2704 return got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2707 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2708 goto done;
2710 if (repo_path == NULL) {
2711 if (worktree)
2712 repo_path =
2713 strdup(got_worktree_get_repo_path(worktree));
2714 else
2715 repo_path = strdup(cwd);
2717 if (repo_path == NULL) {
2718 error = got_error_from_errno("strdup");
2719 goto done;
2722 error = got_repo_open(&repo, repo_path, NULL);
2723 if (error != NULL)
2724 goto done;
2726 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2727 repo, worktree);
2728 if (error)
2729 goto done;
2731 init_curses();
2733 error = apply_unveil(got_repo_get_path(repo),
2734 worktree ? got_worktree_get_root_path(worktree) : NULL);
2735 if (error)
2736 goto done;
2738 if (start_commit == NULL) {
2739 error = got_repo_match_object_id(&start_id, &label,
2740 worktree ? got_worktree_get_head_ref_name(worktree) :
2741 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2742 if (error)
2743 goto done;
2744 head_ref_name = label;
2745 } else {
2746 error = got_ref_open(&ref, repo, start_commit, 0);
2747 if (error == NULL)
2748 head_ref_name = got_ref_get_name(ref);
2749 else if (error->code != GOT_ERR_NOT_REF)
2750 goto done;
2751 error = got_repo_match_object_id(&start_id, NULL,
2752 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2753 if (error)
2754 goto done;
2757 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2758 if (view == NULL) {
2759 error = got_error_from_errno("view_open");
2760 goto done;
2762 error = open_log_view(view, start_id, repo, head_ref_name,
2763 in_repo_path, log_branches);
2764 if (error)
2765 goto done;
2766 if (worktree) {
2767 /* Release work tree lock. */
2768 got_worktree_close(worktree);
2769 worktree = NULL;
2771 error = view_loop(view);
2772 done:
2773 free(in_repo_path);
2774 free(repo_path);
2775 free(cwd);
2776 free(start_id);
2777 free(label);
2778 if (ref)
2779 got_ref_close(ref);
2780 if (repo)
2781 got_repo_close(repo);
2782 if (worktree)
2783 got_worktree_close(worktree);
2784 return error;
2787 __dead static void
2788 usage_diff(void)
2790 endwin();
2791 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2792 "[-w] object1 object2\n", getprogname());
2793 exit(1);
2796 static char *
2797 parse_next_line(FILE *f, size_t *len)
2799 char *line;
2800 size_t linelen;
2801 size_t lineno;
2802 const char delim[3] = { '\0', '\0', '\0'};
2804 line = fparseln(f, &linelen, &lineno, delim, 0);
2805 if (len)
2806 *len = linelen;
2807 return line;
2810 static int
2811 match_line(const char *line, regex_t *regex, size_t nmatch,
2812 regmatch_t *regmatch)
2814 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2817 struct tog_color *
2818 match_color(struct tog_colors *colors, const char *line)
2820 struct tog_color *tc = NULL;
2822 SIMPLEQ_FOREACH(tc, colors, entry) {
2823 if (match_line(line, &tc->regex, 0, NULL))
2824 return tc;
2827 return NULL;
2830 static const struct got_error *
2831 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2832 WINDOW *window, regmatch_t *regmatch)
2834 const struct got_error *err = NULL;
2835 wchar_t *wline;
2836 int width;
2837 char *s;
2839 *wtotal = 0;
2841 s = strndup(line, regmatch->rm_so);
2842 if (s == NULL)
2843 return got_error_from_errno("strndup");
2845 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2846 if (err) {
2847 free(s);
2848 return err;
2850 waddwstr(window, wline);
2851 free(wline);
2852 free(s);
2853 wlimit -= width;
2854 *wtotal += width;
2856 if (wlimit > 0) {
2857 s = strndup(line + regmatch->rm_so,
2858 regmatch->rm_eo - regmatch->rm_so);
2859 if (s == NULL) {
2860 err = got_error_from_errno("strndup");
2861 free(s);
2862 return err;
2864 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2865 if (err) {
2866 free(s);
2867 return err;
2869 wattr_on(window, A_STANDOUT, NULL);
2870 waddwstr(window, wline);
2871 wattr_off(window, A_STANDOUT, NULL);
2872 free(wline);
2873 free(s);
2874 wlimit -= width;
2875 *wtotal += width;
2878 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2879 err = format_line(&wline, &width,
2880 line + regmatch->rm_eo, wlimit, col_tab_align);
2881 if (err)
2882 return err;
2883 waddwstr(window, wline);
2884 free(wline);
2885 *wtotal += width;
2888 return NULL;
2891 static const struct got_error *
2892 draw_file(struct tog_view *view, const char *header)
2894 struct tog_diff_view_state *s = &view->state.diff;
2895 regmatch_t *regmatch = &view->regmatch;
2896 const struct got_error *err;
2897 int nprinted = 0;
2898 char *line;
2899 struct tog_color *tc;
2900 size_t len;
2901 wchar_t *wline;
2902 int width;
2903 int max_lines = view->nlines;
2904 int nlines = s->nlines;
2905 off_t line_offset;
2907 line_offset = s->line_offsets[s->first_displayed_line - 1];
2908 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2909 return got_error_from_errno("fseek");
2911 werase(view->window);
2913 if (header) {
2914 if (asprintf(&line, "[%d/%d] %s",
2915 s->first_displayed_line - 1 + s->selected_line, nlines,
2916 header) == -1)
2917 return got_error_from_errno("asprintf");
2918 err = format_line(&wline, &width, line, view->ncols, 0);
2919 free(line);
2920 if (err)
2921 return err;
2923 if (view_needs_focus_indication(view))
2924 wstandout(view->window);
2925 waddwstr(view->window, wline);
2926 free(wline);
2927 wline = NULL;
2928 if (view_needs_focus_indication(view))
2929 wstandend(view->window);
2930 if (width <= view->ncols - 1)
2931 waddch(view->window, '\n');
2933 if (max_lines <= 1)
2934 return NULL;
2935 max_lines--;
2938 s->eof = 0;
2939 while (max_lines > 0 && nprinted < max_lines) {
2940 line = parse_next_line(s->f, &len);
2941 if (line == NULL) {
2942 s->eof = 1;
2943 break;
2946 tc = match_color(&s->colors, line);
2947 if (tc)
2948 wattr_on(view->window,
2949 COLOR_PAIR(tc->colorpair), NULL);
2950 if (s->first_displayed_line + nprinted == s->matched_line &&
2951 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2952 err = add_matched_line(&width, line, view->ncols, 0,
2953 view->window, regmatch);
2954 if (err) {
2955 free(line);
2956 return err;
2958 } else {
2959 err = format_line(&wline, &width, line, view->ncols, 0);
2960 if (err) {
2961 free(line);
2962 return err;
2964 waddwstr(view->window, wline);
2965 free(wline);
2966 wline = NULL;
2968 if (tc)
2969 wattr_off(view->window,
2970 COLOR_PAIR(tc->colorpair), NULL);
2971 if (width <= view->ncols - 1)
2972 waddch(view->window, '\n');
2973 nprinted++;
2974 free(line);
2976 if (nprinted >= 1)
2977 s->last_displayed_line = s->first_displayed_line +
2978 (nprinted - 1);
2979 else
2980 s->last_displayed_line = s->first_displayed_line;
2982 view_vborder(view);
2984 if (s->eof) {
2985 while (nprinted < view->nlines) {
2986 waddch(view->window, '\n');
2987 nprinted++;
2990 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2991 if (err) {
2992 return err;
2995 wstandout(view->window);
2996 waddwstr(view->window, wline);
2997 free(wline);
2998 wline = NULL;
2999 wstandend(view->window);
3002 return NULL;
3005 static char *
3006 get_datestr(time_t *time, char *datebuf)
3008 struct tm mytm, *tm;
3009 char *p, *s;
3011 tm = gmtime_r(time, &mytm);
3012 if (tm == NULL)
3013 return NULL;
3014 s = asctime_r(tm, datebuf);
3015 if (s == NULL)
3016 return NULL;
3017 p = strchr(s, '\n');
3018 if (p)
3019 *p = '\0';
3020 return s;
3023 static const struct got_error *
3024 get_changed_paths(struct got_pathlist_head *paths,
3025 struct got_commit_object *commit, struct got_repository *repo)
3027 const struct got_error *err = NULL;
3028 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3029 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3030 struct got_object_qid *qid;
3032 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3033 if (qid != NULL) {
3034 struct got_commit_object *pcommit;
3035 err = got_object_open_as_commit(&pcommit, repo,
3036 qid->id);
3037 if (err)
3038 return err;
3040 tree_id1 = got_object_commit_get_tree_id(pcommit);
3041 got_object_commit_close(pcommit);
3045 if (tree_id1) {
3046 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3047 if (err)
3048 goto done;
3051 tree_id2 = got_object_commit_get_tree_id(commit);
3052 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3053 if (err)
3054 goto done;
3056 err = got_diff_tree(tree1, tree2, "", "", repo,
3057 got_diff_tree_collect_changed_paths, paths, 0);
3058 done:
3059 if (tree1)
3060 got_object_tree_close(tree1);
3061 if (tree2)
3062 got_object_tree_close(tree2);
3063 return err;
3066 static const struct got_error *
3067 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3069 off_t *p;
3071 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3072 if (p == NULL)
3073 return got_error_from_errno("reallocarray");
3074 *line_offsets = p;
3075 (*line_offsets)[*nlines] = off;
3076 (*nlines)++;
3077 return NULL;
3080 static const struct got_error *
3081 write_commit_info(off_t **line_offsets, size_t *nlines,
3082 struct got_object_id *commit_id, struct got_reflist_head *refs,
3083 struct got_repository *repo, FILE *outfile)
3085 const struct got_error *err = NULL;
3086 char datebuf[26], *datestr;
3087 struct got_commit_object *commit;
3088 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3089 time_t committer_time;
3090 const char *author, *committer;
3091 char *refs_str = NULL;
3092 struct got_pathlist_head changed_paths;
3093 struct got_pathlist_entry *pe;
3094 off_t outoff = 0;
3095 int n;
3097 TAILQ_INIT(&changed_paths);
3099 if (refs) {
3100 err = build_refs_str(&refs_str, refs, commit_id, repo);
3101 if (err)
3102 return err;
3105 err = got_object_open_as_commit(&commit, repo, commit_id);
3106 if (err)
3107 return err;
3109 err = got_object_id_str(&id_str, commit_id);
3110 if (err) {
3111 err = got_error_from_errno("got_object_id_str");
3112 goto done;
3115 err = add_line_offset(line_offsets, nlines, 0);
3116 if (err)
3117 goto done;
3119 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3120 refs_str ? refs_str : "", refs_str ? ")" : "");
3121 if (n < 0) {
3122 err = got_error_from_errno("fprintf");
3123 goto done;
3125 outoff += n;
3126 err = add_line_offset(line_offsets, nlines, outoff);
3127 if (err)
3128 goto done;
3130 n = fprintf(outfile, "from: %s\n",
3131 got_object_commit_get_author(commit));
3132 if (n < 0) {
3133 err = got_error_from_errno("fprintf");
3134 goto done;
3136 outoff += n;
3137 err = add_line_offset(line_offsets, nlines, outoff);
3138 if (err)
3139 goto done;
3141 committer_time = got_object_commit_get_committer_time(commit);
3142 datestr = get_datestr(&committer_time, datebuf);
3143 if (datestr) {
3144 n = fprintf(outfile, "date: %s UTC\n", datestr);
3145 if (n < 0) {
3146 err = got_error_from_errno("fprintf");
3147 goto done;
3149 outoff += n;
3150 err = add_line_offset(line_offsets, nlines, outoff);
3151 if (err)
3152 goto done;
3154 author = got_object_commit_get_author(commit);
3155 committer = got_object_commit_get_committer(commit);
3156 if (strcmp(author, committer) != 0) {
3157 n = fprintf(outfile, "via: %s\n", committer);
3158 if (n < 0) {
3159 err = got_error_from_errno("fprintf");
3160 goto done;
3162 outoff += n;
3163 err = add_line_offset(line_offsets, nlines, outoff);
3164 if (err)
3165 goto done;
3167 err = got_object_commit_get_logmsg(&logmsg, commit);
3168 if (err)
3169 goto done;
3170 s = logmsg;
3171 while ((line = strsep(&s, "\n")) != NULL) {
3172 n = fprintf(outfile, "%s\n", line);
3173 if (n < 0) {
3174 err = got_error_from_errno("fprintf");
3175 goto done;
3177 outoff += n;
3178 err = add_line_offset(line_offsets, nlines, outoff);
3179 if (err)
3180 goto done;
3183 err = get_changed_paths(&changed_paths, commit, repo);
3184 if (err)
3185 goto done;
3186 TAILQ_FOREACH(pe, &changed_paths, entry) {
3187 struct got_diff_changed_path *cp = pe->data;
3188 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3189 if (n < 0) {
3190 err = got_error_from_errno("fprintf");
3191 goto done;
3193 outoff += n;
3194 err = add_line_offset(line_offsets, nlines, outoff);
3195 if (err)
3196 goto done;
3197 free((char *)pe->path);
3198 free(pe->data);
3201 fputc('\n', outfile);
3202 outoff++;
3203 err = add_line_offset(line_offsets, nlines, outoff);
3204 done:
3205 got_pathlist_free(&changed_paths);
3206 free(id_str);
3207 free(logmsg);
3208 free(refs_str);
3209 got_object_commit_close(commit);
3210 if (err) {
3211 free(*line_offsets);
3212 *line_offsets = NULL;
3213 *nlines = 0;
3215 return err;
3218 static const struct got_error *
3219 create_diff(struct tog_diff_view_state *s)
3221 const struct got_error *err = NULL;
3222 FILE *f = NULL;
3223 int obj_type;
3225 free(s->line_offsets);
3226 s->line_offsets = malloc(sizeof(off_t));
3227 if (s->line_offsets == NULL)
3228 return got_error_from_errno("malloc");
3229 s->nlines = 0;
3231 f = got_opentemp();
3232 if (f == NULL) {
3233 err = got_error_from_errno("got_opentemp");
3234 goto done;
3236 if (s->f && fclose(s->f) != 0) {
3237 err = got_error_from_errno("fclose");
3238 goto done;
3240 s->f = f;
3242 if (s->id1)
3243 err = got_object_get_type(&obj_type, s->repo, s->id1);
3244 else
3245 err = got_object_get_type(&obj_type, s->repo, s->id2);
3246 if (err)
3247 goto done;
3249 switch (obj_type) {
3250 case GOT_OBJ_TYPE_BLOB:
3251 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3252 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3253 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3254 break;
3255 case GOT_OBJ_TYPE_TREE:
3256 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3257 s->id1, s->id2, "", "", s->diff_context,
3258 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3259 break;
3260 case GOT_OBJ_TYPE_COMMIT: {
3261 const struct got_object_id_queue *parent_ids;
3262 struct got_object_qid *pid;
3263 struct got_commit_object *commit2;
3265 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3266 if (err)
3267 goto done;
3268 /* Show commit info if we're diffing to a parent/root commit. */
3269 if (s->id1 == NULL) {
3270 err = write_commit_info(&s->line_offsets, &s->nlines,
3271 s->id2, &s->refs, s->repo, s->f);
3272 if (err)
3273 goto done;
3274 } else {
3275 parent_ids = got_object_commit_get_parent_ids(commit2);
3276 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3277 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3278 err = write_commit_info(
3279 &s->line_offsets, &s->nlines,
3280 s->id2, &s->refs, s->repo, s->f);
3281 if (err)
3282 goto done;
3283 break;
3287 got_object_commit_close(commit2);
3289 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3290 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3291 s->force_text_diff, s->repo, s->f);
3292 break;
3294 default:
3295 err = got_error(GOT_ERR_OBJ_TYPE);
3296 break;
3298 if (err)
3299 goto done;
3300 done:
3301 if (s->f && fflush(s->f) != 0 && err == NULL)
3302 err = got_error_from_errno("fflush");
3303 return err;
3306 static void
3307 diff_view_indicate_progress(struct tog_view *view)
3309 mvwaddstr(view->window, 0, 0, "diffing...");
3310 update_panels();
3311 doupdate();
3314 static const struct got_error *
3315 search_start_diff_view(struct tog_view *view)
3317 struct tog_diff_view_state *s = &view->state.diff;
3319 s->matched_line = 0;
3320 return NULL;
3323 static const struct got_error *
3324 search_next_diff_view(struct tog_view *view)
3326 struct tog_diff_view_state *s = &view->state.diff;
3327 int lineno;
3329 if (!view->searching) {
3330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3331 return NULL;
3334 if (s->matched_line) {
3335 if (view->searching == TOG_SEARCH_FORWARD)
3336 lineno = s->matched_line + 1;
3337 else
3338 lineno = s->matched_line - 1;
3339 } else {
3340 if (view->searching == TOG_SEARCH_FORWARD)
3341 lineno = 1;
3342 else
3343 lineno = s->nlines;
3346 while (1) {
3347 char *line = NULL;
3348 off_t offset;
3349 size_t len;
3351 if (lineno <= 0 || lineno > s->nlines) {
3352 if (s->matched_line == 0) {
3353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3354 free(line);
3355 break;
3358 if (view->searching == TOG_SEARCH_FORWARD)
3359 lineno = 1;
3360 else
3361 lineno = s->nlines;
3364 offset = s->line_offsets[lineno - 1];
3365 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3366 free(line);
3367 return got_error_from_errno("fseeko");
3369 free(line);
3370 line = parse_next_line(s->f, &len);
3371 if (line &&
3372 match_line(line, &view->regex, 1, &view->regmatch)) {
3373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3374 s->matched_line = lineno;
3375 free(line);
3376 break;
3378 free(line);
3379 if (view->searching == TOG_SEARCH_FORWARD)
3380 lineno++;
3381 else
3382 lineno--;
3385 if (s->matched_line) {
3386 s->first_displayed_line = s->matched_line;
3387 s->selected_line = 1;
3390 return NULL;
3393 static const struct got_error *
3394 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3395 struct got_object_id *id2, const char *label1, const char *label2,
3396 int diff_context, int ignore_whitespace, int force_text_diff,
3397 struct tog_view *log_view, struct got_repository *repo)
3399 const struct got_error *err;
3400 struct tog_diff_view_state *s = &view->state.diff;
3402 SIMPLEQ_INIT(&s->refs);
3404 if (id1 != NULL && id2 != NULL) {
3405 int type1, type2;
3406 err = got_object_get_type(&type1, repo, id1);
3407 if (err)
3408 return err;
3409 err = got_object_get_type(&type2, repo, id2);
3410 if (err)
3411 return err;
3413 if (type1 != type2)
3414 return got_error(GOT_ERR_OBJ_TYPE);
3416 s->first_displayed_line = 1;
3417 s->last_displayed_line = view->nlines;
3418 s->selected_line = 1;
3419 s->repo = repo;
3420 s->id1 = id1;
3421 s->id2 = id2;
3422 s->label1 = label1;
3423 s->label2 = label2;
3425 if (id1) {
3426 s->id1 = got_object_id_dup(id1);
3427 if (s->id1 == NULL)
3428 return got_error_from_errno("got_object_id_dup");
3429 } else
3430 s->id1 = NULL;
3432 s->id2 = got_object_id_dup(id2);
3433 if (s->id2 == NULL) {
3434 free(s->id1);
3435 s->id1 = NULL;
3436 return got_error_from_errno("got_object_id_dup");
3438 s->f = NULL;
3439 s->first_displayed_line = 1;
3440 s->last_displayed_line = view->nlines;
3441 s->diff_context = diff_context;
3442 s->ignore_whitespace = ignore_whitespace;
3443 s->force_text_diff = force_text_diff;
3444 s->log_view = log_view;
3445 s->repo = repo;
3447 SIMPLEQ_INIT(&s->colors);
3448 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3449 err = add_color(&s->colors,
3450 "^-", TOG_COLOR_DIFF_MINUS,
3451 get_color_value("TOG_COLOR_DIFF_MINUS"));
3452 if (err)
3453 return err;
3454 err = add_color(&s->colors, "^\\+",
3455 TOG_COLOR_DIFF_PLUS,
3456 get_color_value("TOG_COLOR_DIFF_PLUS"));
3457 if (err) {
3458 free_colors(&s->colors);
3459 return err;
3461 err = add_color(&s->colors,
3462 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3463 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3464 if (err) {
3465 free_colors(&s->colors);
3466 return err;
3469 err = add_color(&s->colors,
3470 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3471 TOG_COLOR_DIFF_META,
3472 get_color_value("TOG_COLOR_DIFF_META"));
3473 if (err) {
3474 free_colors(&s->colors);
3475 return err;
3478 err = add_color(&s->colors,
3479 "^(from|via): ", TOG_COLOR_AUTHOR,
3480 get_color_value("TOG_COLOR_AUTHOR"));
3481 if (err) {
3482 free_colors(&s->colors);
3483 return err;
3486 err = add_color(&s->colors,
3487 "^date: ", TOG_COLOR_DATE,
3488 get_color_value("TOG_COLOR_DATE"));
3489 if (err) {
3490 free_colors(&s->colors);
3491 return err;
3495 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3496 if (err) {
3497 free(s->id1);
3498 s->id1 = NULL;
3499 free(s->id2);
3500 s->id2 = NULL;
3501 free_colors(&s->colors);
3502 return err;
3505 if (log_view && view_is_splitscreen(view))
3506 show_log_view(log_view); /* draw vborder */
3507 diff_view_indicate_progress(view);
3509 s->line_offsets = NULL;
3510 s->nlines = 0;
3511 err = create_diff(s);
3512 if (err) {
3513 free(s->id1);
3514 s->id1 = NULL;
3515 free(s->id2);
3516 s->id2 = NULL;
3517 free_colors(&s->colors);
3518 got_ref_list_free(&s->refs);
3519 return err;
3522 view->show = show_diff_view;
3523 view->input = input_diff_view;
3524 view->close = close_diff_view;
3525 view->search_start = search_start_diff_view;
3526 view->search_next = search_next_diff_view;
3528 return NULL;
3531 static const struct got_error *
3532 close_diff_view(struct tog_view *view)
3534 const struct got_error *err = NULL;
3535 struct tog_diff_view_state *s = &view->state.diff;
3537 free(s->id1);
3538 s->id1 = NULL;
3539 free(s->id2);
3540 s->id2 = NULL;
3541 if (s->f && fclose(s->f) == EOF)
3542 err = got_error_from_errno("fclose");
3543 free_colors(&s->colors);
3544 free(s->line_offsets);
3545 s->line_offsets = NULL;
3546 s->nlines = 0;
3547 got_ref_list_free(&s->refs);
3548 return err;
3551 static const struct got_error *
3552 show_diff_view(struct tog_view *view)
3554 const struct got_error *err;
3555 struct tog_diff_view_state *s = &view->state.diff;
3556 char *id_str1 = NULL, *id_str2, *header;
3557 const char *label1, *label2;
3559 if (s->id1) {
3560 err = got_object_id_str(&id_str1, s->id1);
3561 if (err)
3562 return err;
3563 label1 = s->label1 ? : id_str1;
3564 } else
3565 label1 = "/dev/null";
3567 err = got_object_id_str(&id_str2, s->id2);
3568 if (err)
3569 return err;
3570 label2 = s->label2 ? : id_str2;
3572 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3573 err = got_error_from_errno("asprintf");
3574 free(id_str1);
3575 free(id_str2);
3576 return err;
3578 free(id_str1);
3579 free(id_str2);
3581 return draw_file(view, header);
3584 static const struct got_error *
3585 set_selected_commit(struct tog_diff_view_state *s,
3586 struct commit_queue_entry *entry)
3588 const struct got_error *err;
3589 const struct got_object_id_queue *parent_ids;
3590 struct got_commit_object *selected_commit;
3591 struct got_object_qid *pid;
3593 free(s->id2);
3594 s->id2 = got_object_id_dup(entry->id);
3595 if (s->id2 == NULL)
3596 return got_error_from_errno("got_object_id_dup");
3598 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3599 if (err)
3600 return err;
3601 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3602 free(s->id1);
3603 pid = SIMPLEQ_FIRST(parent_ids);
3604 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3605 got_object_commit_close(selected_commit);
3606 return NULL;
3609 static const struct got_error *
3610 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3612 const struct got_error *err = NULL;
3613 struct tog_diff_view_state *s = &view->state.diff;
3614 struct tog_log_view_state *ls;
3615 struct commit_queue_entry *old_selected_entry;
3616 int i;
3618 switch (ch) {
3619 case 'a':
3620 case 'w':
3621 if (ch == 'a')
3622 s->force_text_diff = !s->force_text_diff;
3623 if (ch == 'w')
3624 s->ignore_whitespace = !s->ignore_whitespace;
3625 wclear(view->window);
3626 s->first_displayed_line = 1;
3627 s->last_displayed_line = view->nlines;
3628 diff_view_indicate_progress(view);
3629 err = create_diff(s);
3630 break;
3631 case 'k':
3632 case KEY_UP:
3633 if (s->first_displayed_line > 1)
3634 s->first_displayed_line--;
3635 break;
3636 case KEY_PPAGE:
3637 case CTRL('b'):
3638 if (s->first_displayed_line == 1)
3639 break;
3640 i = 0;
3641 while (i++ < view->nlines - 1 &&
3642 s->first_displayed_line > 1)
3643 s->first_displayed_line--;
3644 break;
3645 case 'j':
3646 case KEY_DOWN:
3647 if (!s->eof)
3648 s->first_displayed_line++;
3649 break;
3650 case KEY_NPAGE:
3651 case CTRL('f'):
3652 case ' ':
3653 if (s->eof)
3654 break;
3655 i = 0;
3656 while (!s->eof && i++ < view->nlines - 1) {
3657 char *line;
3658 line = parse_next_line(s->f, NULL);
3659 s->first_displayed_line++;
3660 if (line == NULL)
3661 break;
3663 break;
3664 case '[':
3665 if (s->diff_context > 0) {
3666 s->diff_context--;
3667 diff_view_indicate_progress(view);
3668 err = create_diff(s);
3669 if (s->first_displayed_line + view->nlines - 1 >
3670 s->nlines) {
3671 s->first_displayed_line = 1;
3672 s->last_displayed_line = view->nlines;
3675 break;
3676 case ']':
3677 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3678 s->diff_context++;
3679 diff_view_indicate_progress(view);
3680 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;
3688 old_selected_entry = ls->selected_entry;
3690 err = input_log_view(NULL, s->log_view, KEY_UP);
3691 if (err)
3692 break;
3694 if (old_selected_entry == ls->selected_entry)
3695 break;
3697 err = set_selected_commit(s, ls->selected_entry);
3698 if (err)
3699 break;
3701 s->first_displayed_line = 1;
3702 s->last_displayed_line = view->nlines;
3704 diff_view_indicate_progress(view);
3705 err = create_diff(s);
3706 break;
3707 case '>':
3708 case '.':
3709 if (s->log_view == NULL)
3710 break;
3711 ls = &s->log_view->state.log;
3712 old_selected_entry = ls->selected_entry;
3714 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3715 if (err)
3716 break;
3718 if (old_selected_entry == ls->selected_entry)
3719 break;
3721 err = set_selected_commit(s, ls->selected_entry);
3722 if (err)
3723 break;
3725 s->first_displayed_line = 1;
3726 s->last_displayed_line = view->nlines;
3728 diff_view_indicate_progress(view);
3729 err = create_diff(s);
3730 break;
3731 default:
3732 break;
3735 return err;
3738 static const struct got_error *
3739 cmd_diff(int argc, char *argv[])
3741 const struct got_error *error = NULL;
3742 struct got_repository *repo = NULL;
3743 struct got_worktree *worktree = NULL;
3744 struct got_object_id *id1 = NULL, *id2 = NULL;
3745 char *repo_path = NULL, *cwd = NULL;
3746 char *id_str1 = NULL, *id_str2 = NULL;
3747 char *label1 = NULL, *label2 = NULL;
3748 int diff_context = 3, ignore_whitespace = 0;
3749 int ch, force_text_diff = 0;
3750 const char *errstr;
3751 struct tog_view *view;
3753 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3754 switch (ch) {
3755 case 'a':
3756 force_text_diff = 1;
3757 break;
3758 case 'C':
3759 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3760 &errstr);
3761 if (errstr != NULL)
3762 err(1, "-C option %s", errstr);
3763 break;
3764 case 'r':
3765 repo_path = realpath(optarg, NULL);
3766 if (repo_path == NULL)
3767 return got_error_from_errno2("realpath",
3768 optarg);
3769 got_path_strip_trailing_slashes(repo_path);
3770 break;
3771 case 'w':
3772 ignore_whitespace = 1;
3773 break;
3774 default:
3775 usage_diff();
3776 /* NOTREACHED */
3780 argc -= optind;
3781 argv += optind;
3783 if (argc == 0) {
3784 usage_diff(); /* TODO show local worktree changes */
3785 } else if (argc == 2) {
3786 id_str1 = argv[0];
3787 id_str2 = argv[1];
3788 } else
3789 usage_diff();
3791 cwd = getcwd(NULL, 0);
3792 if (cwd == NULL)
3793 return got_error_from_errno("getcwd");
3795 error = got_worktree_open(&worktree, cwd);
3796 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3797 goto done;
3799 if (repo_path == NULL) {
3800 if (worktree)
3801 repo_path =
3802 strdup(got_worktree_get_repo_path(worktree));
3803 else
3804 repo_path = strdup(cwd);
3806 if (repo_path == NULL) {
3807 error = got_error_from_errno("strdup");
3808 goto done;
3811 error = got_repo_open(&repo, repo_path, NULL);
3812 if (error)
3813 goto done;
3815 init_curses();
3817 error = apply_unveil(got_repo_get_path(repo), NULL);
3818 if (error)
3819 goto done;
3821 error = got_repo_match_object_id(&id1, &label1, id_str1,
3822 GOT_OBJ_TYPE_ANY, 1, repo);
3823 if (error)
3824 goto done;
3826 error = got_repo_match_object_id(&id2, &label2, id_str2,
3827 GOT_OBJ_TYPE_ANY, 1, repo);
3828 if (error)
3829 goto done;
3831 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3832 if (view == NULL) {
3833 error = got_error_from_errno("view_open");
3834 goto done;
3836 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3837 ignore_whitespace, force_text_diff, NULL, repo);
3838 if (error)
3839 goto done;
3840 error = view_loop(view);
3841 done:
3842 free(label1);
3843 free(label2);
3844 free(repo_path);
3845 free(cwd);
3846 if (repo)
3847 got_repo_close(repo);
3848 if (worktree)
3849 got_worktree_close(worktree);
3850 return error;
3853 __dead static void
3854 usage_blame(void)
3856 endwin();
3857 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3858 getprogname());
3859 exit(1);
3862 struct tog_blame_line {
3863 int annotated;
3864 struct got_object_id *id;
3867 static const struct got_error *
3868 draw_blame(struct tog_view *view)
3870 struct tog_blame_view_state *s = &view->state.blame;
3871 struct tog_blame *blame = &s->blame;
3872 regmatch_t *regmatch = &view->regmatch;
3873 const struct got_error *err;
3874 int lineno = 0, nprinted = 0;
3875 char *line;
3876 size_t len;
3877 wchar_t *wline;
3878 int width;
3879 struct tog_blame_line *blame_line;
3880 struct got_object_id *prev_id = NULL;
3881 char *id_str;
3882 struct tog_color *tc;
3884 err = got_object_id_str(&id_str, s->blamed_commit->id);
3885 if (err)
3886 return err;
3888 rewind(blame->f);
3889 werase(view->window);
3891 if (asprintf(&line, "commit %s", id_str) == -1) {
3892 err = got_error_from_errno("asprintf");
3893 free(id_str);
3894 return err;
3897 err = format_line(&wline, &width, line, view->ncols, 0);
3898 free(line);
3899 line = NULL;
3900 if (err)
3901 return err;
3902 if (view_needs_focus_indication(view))
3903 wstandout(view->window);
3904 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3905 if (tc)
3906 wattr_on(view->window,
3907 COLOR_PAIR(tc->colorpair), NULL);
3908 waddwstr(view->window, wline);
3909 if (tc)
3910 wattr_off(view->window,
3911 COLOR_PAIR(tc->colorpair), NULL);
3912 if (view_needs_focus_indication(view))
3913 wstandend(view->window);
3914 free(wline);
3915 wline = NULL;
3916 if (width < view->ncols - 1)
3917 waddch(view->window, '\n');
3919 if (asprintf(&line, "[%d/%d] %s%s",
3920 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3921 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3922 free(id_str);
3923 return got_error_from_errno("asprintf");
3925 free(id_str);
3926 err = format_line(&wline, &width, line, view->ncols, 0);
3927 free(line);
3928 line = NULL;
3929 if (err)
3930 return err;
3931 waddwstr(view->window, wline);
3932 free(wline);
3933 wline = NULL;
3934 if (width < view->ncols - 1)
3935 waddch(view->window, '\n');
3937 s->eof = 0;
3938 while (nprinted < view->nlines - 2) {
3939 line = parse_next_line(blame->f, &len);
3940 if (line == NULL) {
3941 s->eof = 1;
3942 break;
3944 if (++lineno < s->first_displayed_line) {
3945 free(line);
3946 continue;
3949 if (view->focussed && nprinted == s->selected_line - 1)
3950 wstandout(view->window);
3952 if (blame->nlines > 0) {
3953 blame_line = &blame->lines[lineno - 1];
3954 if (blame_line->annotated && prev_id &&
3955 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3956 !(view->focussed &&
3957 nprinted == s->selected_line - 1)) {
3958 waddstr(view->window, " ");
3959 } else if (blame_line->annotated) {
3960 char *id_str;
3961 err = got_object_id_str(&id_str, blame_line->id);
3962 if (err) {
3963 free(line);
3964 return err;
3966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3967 if (tc)
3968 wattr_on(view->window,
3969 COLOR_PAIR(tc->colorpair), NULL);
3970 wprintw(view->window, "%.8s", id_str);
3971 if (tc)
3972 wattr_off(view->window,
3973 COLOR_PAIR(tc->colorpair), NULL);
3974 free(id_str);
3975 prev_id = blame_line->id;
3976 } else {
3977 waddstr(view->window, "........");
3978 prev_id = NULL;
3980 } else {
3981 waddstr(view->window, "........");
3982 prev_id = NULL;
3985 if (view->focussed && nprinted == s->selected_line - 1)
3986 wstandend(view->window);
3987 waddstr(view->window, " ");
3989 if (view->ncols <= 9) {
3990 width = 9;
3991 wline = wcsdup(L"");
3992 if (wline == NULL) {
3993 err = got_error_from_errno("wcsdup");
3994 free(line);
3995 return err;
3997 } else if (s->first_displayed_line + nprinted ==
3998 s->matched_line &&
3999 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4000 err = add_matched_line(&width, line, view->ncols - 9, 9,
4001 view->window, regmatch);
4002 if (err) {
4003 free(line);
4004 return err;
4006 width += 9;
4007 } else {
4008 err = format_line(&wline, &width, line,
4009 view->ncols - 9, 9);
4010 waddwstr(view->window, wline);
4011 free(wline);
4012 wline = NULL;
4013 width += 9;
4016 if (width <= view->ncols - 1)
4017 waddch(view->window, '\n');
4018 if (++nprinted == 1)
4019 s->first_displayed_line = lineno;
4020 free(line);
4022 s->last_displayed_line = lineno;
4024 view_vborder(view);
4026 return NULL;
4029 static const struct got_error *
4030 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4032 const struct got_error *err = NULL;
4033 struct tog_blame_cb_args *a = arg;
4034 struct tog_blame_line *line;
4035 int errcode;
4037 if (nlines != a->nlines ||
4038 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4039 return got_error(GOT_ERR_RANGE);
4041 errcode = pthread_mutex_lock(&tog_mutex);
4042 if (errcode)
4043 return got_error_set_errno(errcode, "pthread_mutex_lock");
4045 if (*a->quit) { /* user has quit the blame view */
4046 err = got_error(GOT_ERR_ITER_COMPLETED);
4047 goto done;
4050 if (lineno == -1)
4051 goto done; /* no change in this commit */
4053 line = &a->lines[lineno - 1];
4054 if (line->annotated)
4055 goto done;
4057 line->id = got_object_id_dup(id);
4058 if (line->id == NULL) {
4059 err = got_error_from_errno("got_object_id_dup");
4060 goto done;
4062 line->annotated = 1;
4063 done:
4064 errcode = pthread_mutex_unlock(&tog_mutex);
4065 if (errcode)
4066 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4067 return err;
4070 static void *
4071 blame_thread(void *arg)
4073 const struct got_error *err;
4074 struct tog_blame_thread_args *ta = arg;
4075 struct tog_blame_cb_args *a = ta->cb_args;
4076 int errcode;
4078 err = block_signals_used_by_main_thread();
4079 if (err)
4080 return (void *)err;
4082 err = got_blame(ta->path, a->commit_id, ta->repo,
4083 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4084 if (err && err->code == GOT_ERR_CANCELLED)
4085 err = NULL;
4087 errcode = pthread_mutex_lock(&tog_mutex);
4088 if (errcode)
4089 return (void *)got_error_set_errno(errcode,
4090 "pthread_mutex_lock");
4092 got_repo_close(ta->repo);
4093 ta->repo = NULL;
4094 *ta->complete = 1;
4096 errcode = pthread_mutex_unlock(&tog_mutex);
4097 if (errcode && err == NULL)
4098 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4100 return (void *)err;
4103 static struct got_object_id *
4104 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4105 int first_displayed_line, int selected_line)
4107 struct tog_blame_line *line;
4109 if (nlines <= 0)
4110 return NULL;
4112 line = &lines[first_displayed_line - 1 + selected_line - 1];
4113 if (!line->annotated)
4114 return NULL;
4116 return line->id;
4119 static const struct got_error *
4120 stop_blame(struct tog_blame *blame)
4122 const struct got_error *err = NULL;
4123 int i;
4125 if (blame->thread) {
4126 int errcode;
4127 errcode = pthread_mutex_unlock(&tog_mutex);
4128 if (errcode)
4129 return got_error_set_errno(errcode,
4130 "pthread_mutex_unlock");
4131 errcode = pthread_join(blame->thread, (void **)&err);
4132 if (errcode)
4133 return got_error_set_errno(errcode, "pthread_join");
4134 errcode = pthread_mutex_lock(&tog_mutex);
4135 if (errcode)
4136 return got_error_set_errno(errcode,
4137 "pthread_mutex_lock");
4138 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4139 err = NULL;
4140 blame->thread = NULL;
4142 if (blame->thread_args.repo) {
4143 got_repo_close(blame->thread_args.repo);
4144 blame->thread_args.repo = NULL;
4146 if (blame->f) {
4147 if (fclose(blame->f) != 0 && err == NULL)
4148 err = got_error_from_errno("fclose");
4149 blame->f = NULL;
4151 if (blame->lines) {
4152 for (i = 0; i < blame->nlines; i++)
4153 free(blame->lines[i].id);
4154 free(blame->lines);
4155 blame->lines = NULL;
4157 free(blame->cb_args.commit_id);
4158 blame->cb_args.commit_id = NULL;
4160 return err;
4163 static const struct got_error *
4164 cancel_blame_view(void *arg)
4166 const struct got_error *err = NULL;
4167 int *done = arg;
4168 int errcode;
4170 errcode = pthread_mutex_lock(&tog_mutex);
4171 if (errcode)
4172 return got_error_set_errno(errcode,
4173 "pthread_mutex_unlock");
4175 if (*done)
4176 err = got_error(GOT_ERR_CANCELLED);
4178 errcode = pthread_mutex_unlock(&tog_mutex);
4179 if (errcode)
4180 return got_error_set_errno(errcode,
4181 "pthread_mutex_lock");
4183 return err;
4186 static const struct got_error *
4187 run_blame(struct tog_view *view)
4189 struct tog_blame_view_state *s = &view->state.blame;
4190 struct tog_blame *blame = &s->blame;
4191 const struct got_error *err = NULL;
4192 struct got_blob_object *blob = NULL;
4193 struct got_repository *thread_repo = NULL;
4194 struct got_object_id *obj_id = NULL;
4195 int obj_type;
4197 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4198 s->path);
4199 if (err)
4200 return err;
4202 err = got_object_get_type(&obj_type, s->repo, obj_id);
4203 if (err)
4204 goto done;
4206 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4207 err = got_error(GOT_ERR_OBJ_TYPE);
4208 goto done;
4211 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4212 if (err)
4213 goto done;
4214 blame->f = got_opentemp();
4215 if (blame->f == NULL) {
4216 err = got_error_from_errno("got_opentemp");
4217 goto done;
4219 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4220 &blame->line_offsets, blame->f, blob);
4221 if (err || blame->nlines == 0)
4222 goto done;
4224 /* Don't include \n at EOF in the blame line count. */
4225 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4226 blame->nlines--;
4228 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4229 if (blame->lines == NULL) {
4230 err = got_error_from_errno("calloc");
4231 goto done;
4234 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4235 if (err)
4236 goto done;
4238 blame->cb_args.view = view;
4239 blame->cb_args.lines = blame->lines;
4240 blame->cb_args.nlines = blame->nlines;
4241 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4242 if (blame->cb_args.commit_id == NULL) {
4243 err = got_error_from_errno("got_object_id_dup");
4244 goto done;
4246 blame->cb_args.quit = &s->done;
4248 blame->thread_args.path = s->path;
4249 blame->thread_args.repo = thread_repo;
4250 blame->thread_args.cb_args = &blame->cb_args;
4251 blame->thread_args.complete = &s->blame_complete;
4252 blame->thread_args.cancel_cb = cancel_blame_view;
4253 blame->thread_args.cancel_arg = &s->done;
4254 s->blame_complete = 0;
4256 done:
4257 if (blob)
4258 got_object_blob_close(blob);
4259 free(obj_id);
4260 if (err)
4261 stop_blame(blame);
4262 return err;
4265 static const struct got_error *
4266 open_blame_view(struct tog_view *view, char *path,
4267 struct got_object_id *commit_id, struct got_repository *repo)
4269 const struct got_error *err = NULL;
4270 struct tog_blame_view_state *s = &view->state.blame;
4272 SIMPLEQ_INIT(&s->blamed_commits);
4274 s->path = strdup(path);
4275 if (s->path == NULL)
4276 return got_error_from_errno("strdup");
4278 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4279 if (err) {
4280 free(s->path);
4281 return err;
4284 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4285 s->first_displayed_line = 1;
4286 s->last_displayed_line = view->nlines;
4287 s->selected_line = 1;
4288 s->blame_complete = 0;
4289 s->repo = repo;
4290 s->commit_id = commit_id;
4291 memset(&s->blame, 0, sizeof(s->blame));
4293 SIMPLEQ_INIT(&s->colors);
4294 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4295 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4296 get_color_value("TOG_COLOR_COMMIT"));
4297 if (err)
4298 return err;
4301 view->show = show_blame_view;
4302 view->input = input_blame_view;
4303 view->close = close_blame_view;
4304 view->search_start = search_start_blame_view;
4305 view->search_next = search_next_blame_view;
4307 return run_blame(view);
4310 static const struct got_error *
4311 close_blame_view(struct tog_view *view)
4313 const struct got_error *err = NULL;
4314 struct tog_blame_view_state *s = &view->state.blame;
4316 if (s->blame.thread)
4317 err = stop_blame(&s->blame);
4319 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4320 struct got_object_qid *blamed_commit;
4321 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4322 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4323 got_object_qid_free(blamed_commit);
4326 free(s->path);
4327 free_colors(&s->colors);
4329 return err;
4332 static const struct got_error *
4333 search_start_blame_view(struct tog_view *view)
4335 struct tog_blame_view_state *s = &view->state.blame;
4337 s->matched_line = 0;
4338 return NULL;
4341 static const struct got_error *
4342 search_next_blame_view(struct tog_view *view)
4344 struct tog_blame_view_state *s = &view->state.blame;
4345 int lineno;
4347 if (!view->searching) {
4348 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4349 return NULL;
4352 if (s->matched_line) {
4353 if (view->searching == TOG_SEARCH_FORWARD)
4354 lineno = s->matched_line + 1;
4355 else
4356 lineno = s->matched_line - 1;
4357 } else {
4358 if (view->searching == TOG_SEARCH_FORWARD)
4359 lineno = 1;
4360 else
4361 lineno = s->blame.nlines;
4364 while (1) {
4365 char *line = NULL;
4366 off_t offset;
4367 size_t len;
4369 if (lineno <= 0 || lineno > s->blame.nlines) {
4370 if (s->matched_line == 0) {
4371 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4372 free(line);
4373 break;
4376 if (view->searching == TOG_SEARCH_FORWARD)
4377 lineno = 1;
4378 else
4379 lineno = s->blame.nlines;
4382 offset = s->blame.line_offsets[lineno - 1];
4383 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4384 free(line);
4385 return got_error_from_errno("fseeko");
4387 free(line);
4388 line = parse_next_line(s->blame.f, &len);
4389 if (line &&
4390 match_line(line, &view->regex, 1, &view->regmatch)) {
4391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4392 s->matched_line = lineno;
4393 free(line);
4394 break;
4396 free(line);
4397 if (view->searching == TOG_SEARCH_FORWARD)
4398 lineno++;
4399 else
4400 lineno--;
4403 if (s->matched_line) {
4404 s->first_displayed_line = s->matched_line;
4405 s->selected_line = 1;
4408 return NULL;
4411 static const struct got_error *
4412 show_blame_view(struct tog_view *view)
4414 const struct got_error *err = NULL;
4415 struct tog_blame_view_state *s = &view->state.blame;
4416 int errcode;
4418 if (s->blame.thread == NULL) {
4419 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4420 &s->blame.thread_args);
4421 if (errcode)
4422 return got_error_set_errno(errcode, "pthread_create");
4424 halfdelay(1); /* fast refresh while annotating */
4427 if (s->blame_complete)
4428 halfdelay(10); /* disable fast refresh */
4430 err = draw_blame(view);
4432 view_vborder(view);
4433 return err;
4436 static const struct got_error *
4437 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4439 const struct got_error *err = NULL, *thread_err = NULL;
4440 struct tog_view *diff_view;
4441 struct tog_blame_view_state *s = &view->state.blame;
4442 int begin_x = 0;
4444 switch (ch) {
4445 case 'q':
4446 s->done = 1;
4447 break;
4448 case 'k':
4449 case KEY_UP:
4450 if (s->selected_line > 1)
4451 s->selected_line--;
4452 else if (s->selected_line == 1 &&
4453 s->first_displayed_line > 1)
4454 s->first_displayed_line--;
4455 break;
4456 case KEY_PPAGE:
4457 case CTRL('b'):
4458 if (s->first_displayed_line == 1) {
4459 s->selected_line = 1;
4460 break;
4462 if (s->first_displayed_line > view->nlines - 2)
4463 s->first_displayed_line -=
4464 (view->nlines - 2);
4465 else
4466 s->first_displayed_line = 1;
4467 break;
4468 case 'j':
4469 case KEY_DOWN:
4470 if (s->selected_line < view->nlines - 2 &&
4471 s->first_displayed_line +
4472 s->selected_line <= s->blame.nlines)
4473 s->selected_line++;
4474 else if (s->last_displayed_line <
4475 s->blame.nlines)
4476 s->first_displayed_line++;
4477 break;
4478 case 'b':
4479 case 'p': {
4480 struct got_object_id *id = NULL;
4481 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4482 s->first_displayed_line, s->selected_line);
4483 if (id == NULL)
4484 break;
4485 if (ch == 'p') {
4486 struct got_commit_object *commit;
4487 struct got_object_qid *pid;
4488 struct got_object_id *blob_id = NULL;
4489 int obj_type;
4490 err = got_object_open_as_commit(&commit,
4491 s->repo, id);
4492 if (err)
4493 break;
4494 pid = SIMPLEQ_FIRST(
4495 got_object_commit_get_parent_ids(commit));
4496 if (pid == NULL) {
4497 got_object_commit_close(commit);
4498 break;
4500 /* Check if path history ends here. */
4501 err = got_object_id_by_path(&blob_id, s->repo,
4502 pid->id, s->path);
4503 if (err) {
4504 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4505 err = NULL;
4506 got_object_commit_close(commit);
4507 break;
4509 err = got_object_get_type(&obj_type, s->repo,
4510 blob_id);
4511 free(blob_id);
4512 /* Can't blame non-blob type objects. */
4513 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4514 got_object_commit_close(commit);
4515 break;
4517 err = got_object_qid_alloc(&s->blamed_commit,
4518 pid->id);
4519 got_object_commit_close(commit);
4520 } else {
4521 if (got_object_id_cmp(id,
4522 s->blamed_commit->id) == 0)
4523 break;
4524 err = got_object_qid_alloc(&s->blamed_commit,
4525 id);
4527 if (err)
4528 break;
4529 s->done = 1;
4530 thread_err = stop_blame(&s->blame);
4531 s->done = 0;
4532 if (thread_err)
4533 break;
4534 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4535 s->blamed_commit, entry);
4536 err = run_blame(view);
4537 if (err)
4538 break;
4539 break;
4541 case 'B': {
4542 struct got_object_qid *first;
4543 first = SIMPLEQ_FIRST(&s->blamed_commits);
4544 if (!got_object_id_cmp(first->id, s->commit_id))
4545 break;
4546 s->done = 1;
4547 thread_err = stop_blame(&s->blame);
4548 s->done = 0;
4549 if (thread_err)
4550 break;
4551 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4552 got_object_qid_free(s->blamed_commit);
4553 s->blamed_commit =
4554 SIMPLEQ_FIRST(&s->blamed_commits);
4555 err = run_blame(view);
4556 if (err)
4557 break;
4558 break;
4560 case KEY_ENTER:
4561 case '\r': {
4562 struct got_object_id *id = NULL;
4563 struct got_object_qid *pid;
4564 struct got_commit_object *commit = NULL;
4565 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4566 s->first_displayed_line, s->selected_line);
4567 if (id == NULL)
4568 break;
4569 err = got_object_open_as_commit(&commit, s->repo, id);
4570 if (err)
4571 break;
4572 pid = SIMPLEQ_FIRST(
4573 got_object_commit_get_parent_ids(commit));
4574 if (view_is_parent_view(view))
4575 begin_x = view_split_begin_x(view->begin_x);
4576 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4577 if (diff_view == NULL) {
4578 got_object_commit_close(commit);
4579 err = got_error_from_errno("view_open");
4580 break;
4582 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4583 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4584 got_object_commit_close(commit);
4585 if (err) {
4586 view_close(diff_view);
4587 break;
4589 view->focussed = 0;
4590 diff_view->focussed = 1;
4591 if (view_is_parent_view(view)) {
4592 err = view_close_child(view);
4593 if (err)
4594 break;
4595 view_set_child(view, diff_view);
4596 view->focus_child = 1;
4597 } else
4598 *new_view = diff_view;
4599 if (err)
4600 break;
4601 break;
4603 case KEY_NPAGE:
4604 case CTRL('f'):
4605 case ' ':
4606 if (s->last_displayed_line >= s->blame.nlines &&
4607 s->selected_line >= MIN(s->blame.nlines,
4608 view->nlines - 2)) {
4609 break;
4611 if (s->last_displayed_line >= s->blame.nlines &&
4612 s->selected_line < view->nlines - 2) {
4613 s->selected_line = MIN(s->blame.nlines,
4614 view->nlines - 2);
4615 break;
4617 if (s->last_displayed_line + view->nlines - 2
4618 <= s->blame.nlines)
4619 s->first_displayed_line +=
4620 view->nlines - 2;
4621 else
4622 s->first_displayed_line =
4623 s->blame.nlines -
4624 (view->nlines - 3);
4625 break;
4626 case KEY_RESIZE:
4627 if (s->selected_line > view->nlines - 2) {
4628 s->selected_line = MIN(s->blame.nlines,
4629 view->nlines - 2);
4631 break;
4632 default:
4633 break;
4635 return thread_err ? thread_err : err;
4638 static const struct got_error *
4639 cmd_blame(int argc, char *argv[])
4641 const struct got_error *error;
4642 struct got_repository *repo = NULL;
4643 struct got_worktree *worktree = NULL;
4644 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4645 char *link_target = NULL;
4646 struct got_object_id *commit_id = NULL;
4647 char *commit_id_str = NULL;
4648 int ch;
4649 struct tog_view *view;
4651 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4652 switch (ch) {
4653 case 'c':
4654 commit_id_str = optarg;
4655 break;
4656 case 'r':
4657 repo_path = realpath(optarg, NULL);
4658 if (repo_path == NULL)
4659 return got_error_from_errno2("realpath",
4660 optarg);
4661 break;
4662 default:
4663 usage_blame();
4664 /* NOTREACHED */
4668 argc -= optind;
4669 argv += optind;
4671 if (argc != 1)
4672 usage_blame();
4674 cwd = getcwd(NULL, 0);
4675 if (cwd == NULL)
4676 return got_error_from_errno("getcwd");
4678 error = got_worktree_open(&worktree, cwd);
4679 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4680 goto done;
4682 if (repo_path == NULL) {
4683 if (worktree)
4684 repo_path =
4685 strdup(got_worktree_get_repo_path(worktree));
4686 else
4687 repo_path = strdup(cwd);
4689 if (repo_path == NULL) {
4690 error = got_error_from_errno("strdup");
4691 goto done;
4694 error = got_repo_open(&repo, repo_path, NULL);
4695 if (error != NULL)
4696 goto done;
4698 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4699 worktree);
4700 if (error)
4701 goto done;
4703 init_curses();
4705 error = apply_unveil(got_repo_get_path(repo), NULL);
4706 if (error)
4707 goto done;
4709 if (commit_id_str == NULL) {
4710 struct got_reference *head_ref;
4711 error = got_ref_open(&head_ref, repo, worktree ?
4712 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4713 if (error != NULL)
4714 goto done;
4715 error = got_ref_resolve(&commit_id, repo, head_ref);
4716 got_ref_close(head_ref);
4717 } else {
4718 error = got_repo_match_object_id(&commit_id, NULL,
4719 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4721 if (error != NULL)
4722 goto done;
4724 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4725 if (view == NULL) {
4726 error = got_error_from_errno("view_open");
4727 goto done;
4730 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4731 commit_id, repo);
4732 if (error)
4733 goto done;
4735 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4736 commit_id, repo);
4737 if (error)
4738 goto done;
4739 if (worktree) {
4740 /* Release work tree lock. */
4741 got_worktree_close(worktree);
4742 worktree = NULL;
4744 error = view_loop(view);
4745 done:
4746 free(repo_path);
4747 free(in_repo_path);
4748 free(link_target);
4749 free(cwd);
4750 free(commit_id);
4751 if (worktree)
4752 got_worktree_close(worktree);
4753 if (repo)
4754 got_repo_close(repo);
4755 return error;
4758 static const struct got_error *
4759 draw_tree_entries(struct tog_view *view, const char *parent_path)
4761 struct tog_tree_view_state *s = &view->state.tree;
4762 const struct got_error *err = NULL;
4763 struct got_tree_entry *te;
4764 wchar_t *wline;
4765 struct tog_color *tc;
4766 int width, n, i, nentries;
4767 int limit = view->nlines;
4769 s->ndisplayed = 0;
4771 werase(view->window);
4773 if (limit == 0)
4774 return NULL;
4776 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4777 if (err)
4778 return err;
4779 if (view_needs_focus_indication(view))
4780 wstandout(view->window);
4781 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4782 if (tc)
4783 wattr_on(view->window,
4784 COLOR_PAIR(tc->colorpair), NULL);
4785 waddwstr(view->window, wline);
4786 if (tc)
4787 wattr_off(view->window,
4788 COLOR_PAIR(tc->colorpair), NULL);
4789 if (view_needs_focus_indication(view))
4790 wstandend(view->window);
4791 free(wline);
4792 wline = NULL;
4793 if (width < view->ncols - 1)
4794 waddch(view->window, '\n');
4795 if (--limit <= 0)
4796 return NULL;
4797 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4798 if (err)
4799 return err;
4800 waddwstr(view->window, wline);
4801 free(wline);
4802 wline = NULL;
4803 if (width < view->ncols - 1)
4804 waddch(view->window, '\n');
4805 if (--limit <= 0)
4806 return NULL;
4807 waddch(view->window, '\n');
4808 if (--limit <= 0)
4809 return NULL;
4811 if (s->first_displayed_entry == NULL) {
4812 te = got_object_tree_get_first_entry(s->tree);
4813 if (s->selected == 0) {
4814 if (view->focussed)
4815 wstandout(view->window);
4816 s->selected_entry = NULL;
4818 waddstr(view->window, " ..\n"); /* parent directory */
4819 if (s->selected == 0 && view->focussed)
4820 wstandend(view->window);
4821 s->ndisplayed++;
4822 if (--limit <= 0)
4823 return NULL;
4824 n = 1;
4825 } else {
4826 n = 0;
4827 te = s->first_displayed_entry;
4830 nentries = got_object_tree_get_nentries(s->tree);
4831 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4832 char *line = NULL, *id_str = NULL, *link_target = NULL;
4833 const char *modestr = "";
4834 mode_t mode;
4836 te = got_object_tree_get_entry(s->tree, i);
4837 mode = got_tree_entry_get_mode(te);
4839 if (s->show_ids) {
4840 err = got_object_id_str(&id_str,
4841 got_tree_entry_get_id(te));
4842 if (err)
4843 return got_error_from_errno(
4844 "got_object_id_str");
4846 if (got_object_tree_entry_is_submodule(te))
4847 modestr = "$";
4848 else if (S_ISLNK(mode)) {
4849 int i;
4851 err = got_tree_entry_get_symlink_target(&link_target,
4852 te, s->repo);
4853 if (err) {
4854 free(id_str);
4855 return err;
4857 for (i = 0; i < strlen(link_target); i++) {
4858 if (!isprint((unsigned char)link_target[i]))
4859 link_target[i] = '?';
4861 modestr = "@";
4863 else if (S_ISDIR(mode))
4864 modestr = "/";
4865 else if (mode & S_IXUSR)
4866 modestr = "*";
4867 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4868 got_tree_entry_get_name(te), modestr,
4869 link_target ? " -> ": "",
4870 link_target ? link_target : "") == -1) {
4871 free(id_str);
4872 free(link_target);
4873 return got_error_from_errno("asprintf");
4875 free(id_str);
4876 free(link_target);
4877 err = format_line(&wline, &width, line, view->ncols, 0);
4878 if (err) {
4879 free(line);
4880 break;
4882 if (n == s->selected) {
4883 if (view->focussed)
4884 wstandout(view->window);
4885 s->selected_entry = te;
4887 tc = match_color(&s->colors, line);
4888 if (tc)
4889 wattr_on(view->window,
4890 COLOR_PAIR(tc->colorpair), NULL);
4891 waddwstr(view->window, wline);
4892 if (tc)
4893 wattr_off(view->window,
4894 COLOR_PAIR(tc->colorpair), NULL);
4895 if (width < view->ncols - 1)
4896 waddch(view->window, '\n');
4897 if (n == s->selected && view->focussed)
4898 wstandend(view->window);
4899 free(line);
4900 free(wline);
4901 wline = NULL;
4902 n++;
4903 s->ndisplayed++;
4904 s->last_displayed_entry = te;
4905 if (--limit <= 0)
4906 break;
4909 return err;
4912 static void
4913 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4915 struct got_tree_entry *te;
4916 int isroot = s->tree == s->root;
4917 int i = 0;
4919 if (s->first_displayed_entry == NULL)
4920 return;
4922 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4923 while (i++ < maxscroll) {
4924 if (te == NULL) {
4925 if (!isroot)
4926 s->first_displayed_entry = NULL;
4927 break;
4929 s->first_displayed_entry = te;
4930 te = got_tree_entry_get_prev(s->tree, te);
4934 static void
4935 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4937 struct got_tree_entry *next, *last;
4938 int n = 0;
4940 if (s->first_displayed_entry)
4941 next = got_tree_entry_get_next(s->tree,
4942 s->first_displayed_entry);
4943 else
4944 next = got_object_tree_get_first_entry(s->tree);
4946 last = s->last_displayed_entry;
4947 while (next && last && n++ < maxscroll) {
4948 last = got_tree_entry_get_next(s->tree, last);
4949 if (last) {
4950 s->first_displayed_entry = next;
4951 next = got_tree_entry_get_next(s->tree, next);
4956 static const struct got_error *
4957 tree_entry_path(char **path, struct tog_parent_trees *parents,
4958 struct got_tree_entry *te)
4960 const struct got_error *err = NULL;
4961 struct tog_parent_tree *pt;
4962 size_t len = 2; /* for leading slash and NUL */
4964 TAILQ_FOREACH(pt, parents, entry)
4965 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4966 + 1 /* slash */;
4967 if (te)
4968 len += strlen(got_tree_entry_get_name(te));
4970 *path = calloc(1, len);
4971 if (path == NULL)
4972 return got_error_from_errno("calloc");
4974 (*path)[0] = '/';
4975 pt = TAILQ_LAST(parents, tog_parent_trees);
4976 while (pt) {
4977 const char *name = got_tree_entry_get_name(pt->selected_entry);
4978 if (strlcat(*path, name, len) >= len) {
4979 err = got_error(GOT_ERR_NO_SPACE);
4980 goto done;
4982 if (strlcat(*path, "/", len) >= len) {
4983 err = got_error(GOT_ERR_NO_SPACE);
4984 goto done;
4986 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4988 if (te) {
4989 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4990 err = got_error(GOT_ERR_NO_SPACE);
4991 goto done;
4994 done:
4995 if (err) {
4996 free(*path);
4997 *path = NULL;
4999 return err;
5002 static const struct got_error *
5003 blame_tree_entry(struct tog_view **new_view, int begin_x,
5004 struct got_tree_entry *te, struct tog_parent_trees *parents,
5005 struct got_object_id *commit_id, struct got_repository *repo)
5007 const struct got_error *err = NULL;
5008 char *path;
5009 struct tog_view *blame_view;
5011 *new_view = NULL;
5013 err = tree_entry_path(&path, parents, te);
5014 if (err)
5015 return err;
5017 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5018 if (blame_view == NULL) {
5019 err = got_error_from_errno("view_open");
5020 goto done;
5023 err = open_blame_view(blame_view, path, commit_id, repo);
5024 if (err) {
5025 if (err->code == GOT_ERR_CANCELLED)
5026 err = NULL;
5027 view_close(blame_view);
5028 } else
5029 *new_view = blame_view;
5030 done:
5031 free(path);
5032 return err;
5035 static const struct got_error *
5036 log_tree_entry(struct tog_view **new_view, int begin_x,
5037 struct got_tree_entry *te, struct tog_parent_trees *parents,
5038 struct got_object_id *commit_id, struct got_repository *repo)
5040 struct tog_view *log_view;
5041 const struct got_error *err = NULL;
5042 char *path;
5044 *new_view = NULL;
5046 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5047 if (log_view == NULL)
5048 return got_error_from_errno("view_open");
5050 err = tree_entry_path(&path, parents, te);
5051 if (err)
5052 return err;
5054 err = open_log_view(log_view, commit_id, repo, NULL, path, 0);
5055 if (err)
5056 view_close(log_view);
5057 else
5058 *new_view = log_view;
5059 free(path);
5060 return err;
5063 static const struct got_error *
5064 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5065 struct got_object_id *commit_id, struct got_repository *repo)
5067 const struct got_error *err = NULL;
5068 char *commit_id_str = NULL;
5069 struct tog_tree_view_state *s = &view->state.tree;
5071 TAILQ_INIT(&s->parents);
5073 err = got_object_id_str(&commit_id_str, commit_id);
5074 if (err != NULL)
5075 goto done;
5077 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5078 err = got_error_from_errno("asprintf");
5079 goto done;
5082 s->root = s->tree = root;
5083 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5084 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5085 s->commit_id = got_object_id_dup(commit_id);
5086 if (s->commit_id == NULL) {
5087 err = got_error_from_errno("got_object_id_dup");
5088 goto done;
5090 s->repo = repo;
5092 SIMPLEQ_INIT(&s->colors);
5094 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5095 err = add_color(&s->colors, "\\$$",
5096 TOG_COLOR_TREE_SUBMODULE,
5097 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5098 if (err)
5099 goto done;
5100 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5101 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5102 if (err) {
5103 free_colors(&s->colors);
5104 goto done;
5106 err = add_color(&s->colors, "/$",
5107 TOG_COLOR_TREE_DIRECTORY,
5108 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5109 if (err) {
5110 free_colors(&s->colors);
5111 goto done;
5114 err = add_color(&s->colors, "\\*$",
5115 TOG_COLOR_TREE_EXECUTABLE,
5116 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5117 if (err) {
5118 free_colors(&s->colors);
5119 goto done;
5122 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5123 get_color_value("TOG_COLOR_COMMIT"));
5124 if (err) {
5125 free_colors(&s->colors);
5126 goto done;
5130 view->show = show_tree_view;
5131 view->input = input_tree_view;
5132 view->close = close_tree_view;
5133 view->search_start = search_start_tree_view;
5134 view->search_next = search_next_tree_view;
5135 done:
5136 free(commit_id_str);
5137 if (err) {
5138 free(s->tree_label);
5139 s->tree_label = NULL;
5141 return err;
5144 static const struct got_error *
5145 close_tree_view(struct tog_view *view)
5147 struct tog_tree_view_state *s = &view->state.tree;
5149 free_colors(&s->colors);
5150 free(s->tree_label);
5151 s->tree_label = NULL;
5152 free(s->commit_id);
5153 s->commit_id = NULL;
5154 while (!TAILQ_EMPTY(&s->parents)) {
5155 struct tog_parent_tree *parent;
5156 parent = TAILQ_FIRST(&s->parents);
5157 TAILQ_REMOVE(&s->parents, parent, entry);
5158 free(parent);
5161 if (s->tree != s->root)
5162 got_object_tree_close(s->tree);
5163 got_object_tree_close(s->root);
5164 return NULL;
5167 static const struct got_error *
5168 search_start_tree_view(struct tog_view *view)
5170 struct tog_tree_view_state *s = &view->state.tree;
5172 s->matched_entry = NULL;
5173 return NULL;
5176 static int
5177 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5179 regmatch_t regmatch;
5181 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5182 0) == 0;
5185 static const struct got_error *
5186 search_next_tree_view(struct tog_view *view)
5188 struct tog_tree_view_state *s = &view->state.tree;
5189 struct got_tree_entry *te = NULL;
5191 if (!view->searching) {
5192 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5193 return NULL;
5196 if (s->matched_entry) {
5197 if (view->searching == TOG_SEARCH_FORWARD) {
5198 if (s->selected_entry)
5199 te = got_tree_entry_get_next(s->tree,
5200 s->selected_entry);
5201 else
5202 te = got_object_tree_get_first_entry(s->tree);
5203 } else {
5204 if (s->selected_entry == NULL)
5205 te = got_object_tree_get_last_entry(s->tree);
5206 else
5207 te = got_tree_entry_get_prev(s->tree,
5208 s->selected_entry);
5210 } else {
5211 if (view->searching == TOG_SEARCH_FORWARD)
5212 te = got_object_tree_get_first_entry(s->tree);
5213 else
5214 te = got_object_tree_get_last_entry(s->tree);
5217 while (1) {
5218 if (te == NULL) {
5219 if (s->matched_entry == NULL) {
5220 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5221 return NULL;
5223 if (view->searching == TOG_SEARCH_FORWARD)
5224 te = got_object_tree_get_first_entry(s->tree);
5225 else
5226 te = got_object_tree_get_last_entry(s->tree);
5229 if (match_tree_entry(te, &view->regex)) {
5230 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5231 s->matched_entry = te;
5232 break;
5235 if (view->searching == TOG_SEARCH_FORWARD)
5236 te = got_tree_entry_get_next(s->tree, te);
5237 else
5238 te = got_tree_entry_get_prev(s->tree, te);
5241 if (s->matched_entry) {
5242 s->first_displayed_entry = s->matched_entry;
5243 s->selected = 0;
5246 return NULL;
5249 static const struct got_error *
5250 show_tree_view(struct tog_view *view)
5252 const struct got_error *err = NULL;
5253 struct tog_tree_view_state *s = &view->state.tree;
5254 char *parent_path;
5256 err = tree_entry_path(&parent_path, &s->parents, NULL);
5257 if (err)
5258 return err;
5260 err = draw_tree_entries(view, parent_path);
5261 free(parent_path);
5263 view_vborder(view);
5264 return err;
5267 static const struct got_error *
5268 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5270 const struct got_error *err = NULL;
5271 struct tog_tree_view_state *s = &view->state.tree;
5272 struct tog_view *log_view, *ref_view;
5273 int begin_x = 0;
5275 switch (ch) {
5276 case 'i':
5277 s->show_ids = !s->show_ids;
5278 break;
5279 case 'l':
5280 if (!s->selected_entry)
5281 break;
5282 if (view_is_parent_view(view))
5283 begin_x = view_split_begin_x(view->begin_x);
5284 err = log_tree_entry(&log_view, begin_x, s->selected_entry,
5285 &s->parents, s->commit_id, s->repo);
5286 view->focussed = 0;
5287 log_view->focussed = 1;
5288 if (view_is_parent_view(view)) {
5289 err = view_close_child(view);
5290 if (err)
5291 return err;
5292 view_set_child(view, log_view);
5293 view->focus_child = 1;
5294 } else
5295 *new_view = log_view;
5296 break;
5297 case 'r':
5298 if (view_is_parent_view(view))
5299 begin_x = view_split_begin_x(view->begin_x);
5300 ref_view = view_open(view->nlines, view->ncols,
5301 view->begin_y, begin_x, TOG_VIEW_REF);
5302 if (ref_view == NULL)
5303 return got_error_from_errno("view_open");
5304 err = open_ref_view(ref_view, s->repo);
5305 if (err) {
5306 view_close(ref_view);
5307 return err;
5309 view->focussed = 0;
5310 ref_view->focussed = 1;
5311 if (view_is_parent_view(view)) {
5312 err = view_close_child(view);
5313 if (err)
5314 return err;
5315 view_set_child(view, ref_view);
5316 view->focus_child = 1;
5317 } else
5318 *new_view = ref_view;
5319 break;
5320 case 'k':
5321 case KEY_UP:
5322 if (s->selected > 0) {
5323 s->selected--;
5324 break;
5326 tree_scroll_up(s, 1);
5327 break;
5328 case KEY_PPAGE:
5329 case CTRL('b'):
5330 if (s->tree == s->root) {
5331 if (got_object_tree_get_first_entry(s->tree) ==
5332 s->first_displayed_entry)
5333 s->selected = 0;
5334 } else {
5335 if (s->first_displayed_entry == NULL)
5336 s->selected = 0;
5338 tree_scroll_up(s, MAX(0, view->nlines - 3));
5339 break;
5340 case 'j':
5341 case KEY_DOWN:
5342 if (s->selected < s->ndisplayed - 1) {
5343 s->selected++;
5344 break;
5346 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5347 == NULL)
5348 /* can't scroll any further */
5349 break;
5350 tree_scroll_down(s, 1);
5351 break;
5352 case KEY_NPAGE:
5353 case CTRL('f'):
5354 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5355 == NULL) {
5356 /* can't scroll any further; move cursor down */
5357 if (s->selected < s->ndisplayed - 1)
5358 s->selected = s->ndisplayed - 1;
5359 break;
5361 tree_scroll_down(s, view->nlines - 3);
5362 break;
5363 case KEY_ENTER:
5364 case '\r':
5365 case KEY_BACKSPACE:
5366 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5367 struct tog_parent_tree *parent;
5368 /* user selected '..' */
5369 if (s->tree == s->root)
5370 break;
5371 parent = TAILQ_FIRST(&s->parents);
5372 TAILQ_REMOVE(&s->parents, parent,
5373 entry);
5374 got_object_tree_close(s->tree);
5375 s->tree = parent->tree;
5376 s->first_displayed_entry =
5377 parent->first_displayed_entry;
5378 s->selected_entry =
5379 parent->selected_entry;
5380 s->selected = parent->selected;
5381 free(parent);
5382 } else if (S_ISDIR(got_tree_entry_get_mode(
5383 s->selected_entry))) {
5384 struct got_tree_object *subtree;
5385 err = got_object_open_as_tree(&subtree, s->repo,
5386 got_tree_entry_get_id(s->selected_entry));
5387 if (err)
5388 break;
5389 err = tree_view_visit_subtree(s, subtree);
5390 if (err) {
5391 got_object_tree_close(subtree);
5392 break;
5394 } else if (S_ISREG(got_tree_entry_get_mode(
5395 s->selected_entry))) {
5396 struct tog_view *blame_view;
5397 int begin_x = view_is_parent_view(view) ?
5398 view_split_begin_x(view->begin_x) : 0;
5400 err = blame_tree_entry(&blame_view, begin_x,
5401 s->selected_entry, &s->parents,
5402 s->commit_id, s->repo);
5403 if (err)
5404 break;
5405 view->focussed = 0;
5406 blame_view->focussed = 1;
5407 if (view_is_parent_view(view)) {
5408 err = view_close_child(view);
5409 if (err)
5410 return err;
5411 view_set_child(view, blame_view);
5412 view->focus_child = 1;
5413 } else
5414 *new_view = blame_view;
5416 break;
5417 case KEY_RESIZE:
5418 if (s->selected > view->nlines)
5419 s->selected = s->ndisplayed - 1;
5420 break;
5421 default:
5422 break;
5425 return err;
5428 __dead static void
5429 usage_tree(void)
5431 endwin();
5432 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5433 getprogname());
5434 exit(1);
5437 static const struct got_error *
5438 cmd_tree(int argc, char *argv[])
5440 const struct got_error *error;
5441 struct got_repository *repo = NULL;
5442 struct got_worktree *worktree = NULL;
5443 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5444 struct got_object_id *commit_id = NULL;
5445 char *commit_id_arg = NULL;
5446 struct got_commit_object *commit = NULL;
5447 struct got_tree_object *tree = NULL;
5448 int ch;
5449 struct tog_view *view;
5451 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5452 switch (ch) {
5453 case 'c':
5454 commit_id_arg = optarg;
5455 break;
5456 case 'r':
5457 repo_path = realpath(optarg, NULL);
5458 if (repo_path == NULL)
5459 return got_error_from_errno2("realpath",
5460 optarg);
5461 break;
5462 default:
5463 usage_tree();
5464 /* NOTREACHED */
5468 argc -= optind;
5469 argv += optind;
5471 if (argc > 1)
5472 usage_tree();
5474 cwd = getcwd(NULL, 0);
5475 if (cwd == NULL)
5476 return got_error_from_errno("getcwd");
5478 error = got_worktree_open(&worktree, cwd);
5479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5480 goto done;
5482 if (repo_path == NULL) {
5483 if (worktree)
5484 repo_path =
5485 strdup(got_worktree_get_repo_path(worktree));
5486 else
5487 repo_path = strdup(cwd);
5489 if (repo_path == NULL) {
5490 error = got_error_from_errno("strdup");
5491 goto done;
5494 error = got_repo_open(&repo, repo_path, NULL);
5495 if (error != NULL)
5496 goto done;
5498 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5499 repo, worktree);
5500 if (error)
5501 goto done;
5503 init_curses();
5505 error = apply_unveil(got_repo_get_path(repo), NULL);
5506 if (error)
5507 goto done;
5509 error = got_repo_match_object_id(&commit_id, NULL,
5510 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5511 GOT_OBJ_TYPE_COMMIT, 1, repo);
5512 if (error)
5513 goto done;
5515 error = got_object_open_as_commit(&commit, repo, commit_id);
5516 if (error)
5517 goto done;
5519 error = got_object_open_as_tree(&tree, repo,
5520 got_object_commit_get_tree_id(commit));
5521 if (error)
5522 goto done;
5524 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5525 if (view == NULL) {
5526 error = got_error_from_errno("view_open");
5527 goto done;
5529 error = open_tree_view(view, tree, commit_id, repo);
5530 if (error)
5531 goto done;
5532 if (!got_path_is_root_dir(in_repo_path)) {
5533 error = tree_view_walk_path(&view->state.tree, commit_id,
5534 in_repo_path);
5535 if (error)
5536 goto done;
5539 if (worktree) {
5540 /* Release work tree lock. */
5541 got_worktree_close(worktree);
5542 worktree = NULL;
5544 error = view_loop(view);
5545 done:
5546 free(repo_path);
5547 free(cwd);
5548 free(commit_id);
5549 if (commit)
5550 got_object_commit_close(commit);
5551 if (tree)
5552 got_object_tree_close(tree);
5553 if (repo)
5554 got_repo_close(repo);
5555 return error;
5558 static const struct got_error *
5559 ref_view_load_refs(struct tog_ref_view_state *s)
5561 const struct got_error *err;
5562 struct got_reflist_entry *sre;
5563 struct tog_reflist_entry *re;
5565 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5566 got_ref_cmp_by_name, NULL);
5567 if (err)
5568 return err;
5570 s->nrefs = 0;
5571 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5572 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5573 continue;
5575 re = malloc(sizeof(*re));
5576 if (re == NULL)
5577 return got_error_from_errno("malloc");
5579 re->ref = sre->ref;
5580 re->idx = s->nrefs++;
5581 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5584 return NULL;
5587 void
5588 ref_view_free_refs(struct tog_ref_view_state *s)
5590 struct tog_reflist_entry *re;
5592 while (!TAILQ_EMPTY(&s->refs)) {
5593 re = TAILQ_FIRST(&s->refs);
5594 TAILQ_REMOVE(&s->refs, re, entry);
5595 free(re);
5597 got_ref_list_free(&s->simplerefs);
5600 static const struct got_error *
5601 open_ref_view(struct tog_view *view, struct got_repository *repo)
5603 const struct got_error *err = NULL;
5604 struct tog_ref_view_state *s = &view->state.ref;
5606 s->selected_entry = 0;
5607 s->repo = repo;
5609 SIMPLEQ_INIT(&s->simplerefs);
5610 TAILQ_INIT(&s->refs);
5611 SIMPLEQ_INIT(&s->colors);
5613 err = ref_view_load_refs(s);
5614 if (err)
5615 return err;
5617 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5619 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5620 err = add_color(&s->colors, "^refs/heads/",
5621 TOG_COLOR_REFS_HEADS,
5622 get_color_value("TOG_COLOR_REFS_HEADS"));
5623 if (err)
5624 goto done;
5626 err = add_color(&s->colors, "^refs/tags/",
5627 TOG_COLOR_REFS_TAGS,
5628 get_color_value("TOG_COLOR_REFS_TAGS"));
5629 if (err)
5630 goto done;
5632 err = add_color(&s->colors, "^refs/remotes/",
5633 TOG_COLOR_REFS_REMOTES,
5634 get_color_value("TOG_COLOR_REFS_REMOTES"));
5635 if (err)
5636 goto done;
5639 view->show = show_ref_view;
5640 view->input = input_ref_view;
5641 view->close = close_ref_view;
5642 view->search_start = search_start_ref_view;
5643 view->search_next = search_next_ref_view;
5644 done:
5645 if (err)
5646 free_colors(&s->colors);
5647 return err;
5650 static const struct got_error *
5651 close_ref_view(struct tog_view *view)
5653 struct tog_ref_view_state *s = &view->state.ref;
5655 ref_view_free_refs(s);
5656 free_colors(&s->colors);
5658 return NULL;
5661 static const struct got_error *
5662 resolve_reflist_entry(struct got_object_id **commit_id,
5663 struct tog_reflist_entry *re, struct got_repository *repo)
5665 const struct got_error *err = NULL;
5666 struct got_object_id *obj_id;
5667 struct got_tag_object *tag = NULL;
5668 int obj_type;
5670 *commit_id = NULL;
5672 err = got_ref_resolve(&obj_id, repo, re->ref);
5673 if (err)
5674 return err;
5676 err = got_object_get_type(&obj_type, repo, obj_id);
5677 if (err)
5678 goto done;
5680 switch (obj_type) {
5681 case GOT_OBJ_TYPE_COMMIT:
5682 *commit_id = obj_id;
5683 break;
5684 case GOT_OBJ_TYPE_TAG:
5685 err = got_object_open_as_tag(&tag, repo, obj_id);
5686 if (err)
5687 goto done;
5688 free(obj_id);
5689 err = got_object_get_type(&obj_type, repo,
5690 got_object_tag_get_object_id(tag));
5691 if (err)
5692 goto done;
5693 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5694 err = got_error(GOT_ERR_OBJ_TYPE);
5695 goto done;
5697 *commit_id = got_object_id_dup(
5698 got_object_tag_get_object_id(tag));
5699 if (*commit_id == NULL) {
5700 err = got_error_from_errno("got_object_id_dup");
5701 goto done;
5703 break;
5704 default:
5705 err = got_error(GOT_ERR_OBJ_TYPE);
5706 break;
5709 done:
5710 if (tag)
5711 got_object_tag_close(tag);
5712 if (err) {
5713 free(*commit_id);
5714 *commit_id = NULL;
5716 return err;
5719 static const struct got_error *
5720 log_ref_entry(struct tog_view **new_view, int begin_x,
5721 struct tog_reflist_entry *re, struct got_repository *repo)
5723 struct tog_view *log_view;
5724 const struct got_error *err = NULL;
5725 struct got_object_id *commit_id = NULL;
5727 *new_view = NULL;
5729 err = resolve_reflist_entry(&commit_id, re, repo);
5730 if (err) {
5731 if (err->code != GOT_ERR_OBJ_TYPE)
5732 return err;
5733 else
5734 return NULL;
5737 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5738 if (log_view == NULL) {
5739 err = got_error_from_errno("view_open");
5740 goto done;
5743 err = open_log_view(log_view, commit_id, repo,
5744 got_ref_get_name(re->ref), "", 0);
5745 done:
5746 if (err)
5747 view_close(log_view);
5748 else
5749 *new_view = log_view;
5750 free(commit_id);
5751 return err;
5754 static void
5755 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5757 struct tog_reflist_entry *re;
5758 int i = 0;
5760 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5761 return;
5763 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5764 while (i++ < maxscroll) {
5765 if (re == NULL)
5766 break;
5767 s->first_displayed_entry = re;
5768 re = TAILQ_PREV(re, tog_reflist_head, entry);
5772 static void
5773 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5775 struct tog_reflist_entry *next, *last;
5776 int n = 0;
5778 if (s->first_displayed_entry)
5779 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5780 else
5781 next = TAILQ_FIRST(&s->refs);
5783 last = s->last_displayed_entry;
5784 while (next && last && n++ < maxscroll) {
5785 last = TAILQ_NEXT(last, entry);
5786 if (last) {
5787 s->first_displayed_entry = next;
5788 next = TAILQ_NEXT(next, entry);
5793 static const struct got_error *
5794 search_start_ref_view(struct tog_view *view)
5796 struct tog_ref_view_state *s = &view->state.ref;
5798 s->matched_entry = NULL;
5799 return NULL;
5802 static int
5803 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5805 regmatch_t regmatch;
5807 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5808 0) == 0;
5811 static const struct got_error *
5812 search_next_ref_view(struct tog_view *view)
5814 struct tog_ref_view_state *s = &view->state.ref;
5815 struct tog_reflist_entry *re = NULL;
5817 if (!view->searching) {
5818 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5819 return NULL;
5822 if (s->matched_entry) {
5823 if (view->searching == TOG_SEARCH_FORWARD) {
5824 if (s->selected_entry)
5825 re = TAILQ_NEXT(s->selected_entry, entry);
5826 else
5827 re = TAILQ_PREV(s->selected_entry,
5828 tog_reflist_head, entry);
5829 } else {
5830 if (s->selected_entry == NULL)
5831 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5832 else
5833 re = TAILQ_PREV(s->selected_entry,
5834 tog_reflist_head, entry);
5836 } else {
5837 if (view->searching == TOG_SEARCH_FORWARD)
5838 re = TAILQ_FIRST(&s->refs);
5839 else
5840 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5843 while (1) {
5844 if (re == NULL) {
5845 if (s->matched_entry == NULL) {
5846 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5847 return NULL;
5849 if (view->searching == TOG_SEARCH_FORWARD)
5850 re = TAILQ_FIRST(&s->refs);
5851 else
5852 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5855 if (match_reflist_entry(re, &view->regex)) {
5856 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5857 s->matched_entry = re;
5858 break;
5861 if (view->searching == TOG_SEARCH_FORWARD)
5862 re = TAILQ_NEXT(re, entry);
5863 else
5864 re = TAILQ_PREV(re, tog_reflist_head, entry);
5867 if (s->matched_entry) {
5868 s->first_displayed_entry = s->matched_entry;
5869 s->selected = 0;
5872 return NULL;
5875 static const struct got_error *
5876 show_ref_view(struct tog_view *view)
5878 const struct got_error *err = NULL;
5879 struct tog_ref_view_state *s = &view->state.ref;
5880 struct tog_reflist_entry *re;
5881 char *line = NULL;
5882 wchar_t *wline;
5883 struct tog_color *tc;
5884 int width, n;
5885 int limit = view->nlines;
5887 werase(view->window);
5889 s->ndisplayed = 0;
5891 if (limit == 0)
5892 return NULL;
5894 re = s->first_displayed_entry;
5896 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5897 s->nrefs) == -1)
5898 return got_error_from_errno("asprintf");
5900 err = format_line(&wline, &width, line, view->ncols, 0);
5901 if (err) {
5902 free(line);
5903 return err;
5905 if (view_needs_focus_indication(view))
5906 wstandout(view->window);
5907 waddwstr(view->window, wline);
5908 if (view_needs_focus_indication(view))
5909 wstandend(view->window);
5910 free(wline);
5911 wline = NULL;
5912 free(line);
5913 line = NULL;
5914 if (width < view->ncols - 1)
5915 waddch(view->window, '\n');
5916 if (--limit <= 0)
5917 return NULL;
5919 n = 0;
5920 while (re && limit > 0) {
5921 char *line = NULL;
5923 if (got_ref_is_symbolic(re->ref)) {
5924 if (asprintf(&line, "%s -> %s",
5925 got_ref_get_name(re->ref),
5926 got_ref_get_symref_target(re->ref)) == -1)
5927 return got_error_from_errno("asprintf");
5928 } else if (s->show_ids) {
5929 struct got_object_id *id;
5930 char *id_str;
5931 err = got_ref_resolve(&id, s->repo, re->ref);
5932 if (err)
5933 return err;
5934 err = got_object_id_str(&id_str, id);
5935 if (err) {
5936 free(id);
5937 return err;
5939 if (asprintf(&line, "%s: %s",
5940 got_ref_get_name(re->ref), id_str) == -1) {
5941 err = got_error_from_errno("asprintf");
5942 free(id);
5943 free(id_str);
5944 return err;
5946 free(id);
5947 free(id_str);
5948 } else {
5949 line = strdup(got_ref_get_name(re->ref));
5950 if (line == NULL)
5951 return got_error_from_errno("strdup");
5954 err = format_line(&wline, &width, line, view->ncols, 0);
5955 if (err) {
5956 free(line);
5957 return err;
5959 if (n == s->selected) {
5960 if (view->focussed)
5961 wstandout(view->window);
5962 s->selected_entry = re;
5964 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5965 if (tc)
5966 wattr_on(view->window,
5967 COLOR_PAIR(tc->colorpair), NULL);
5968 waddwstr(view->window, wline);
5969 if (tc)
5970 wattr_off(view->window,
5971 COLOR_PAIR(tc->colorpair), NULL);
5972 if (width < view->ncols - 1)
5973 waddch(view->window, '\n');
5974 if (n == s->selected && view->focussed)
5975 wstandend(view->window);
5976 free(line);
5977 free(wline);
5978 wline = NULL;
5979 n++;
5980 s->ndisplayed++;
5981 s->last_displayed_entry = re;
5983 limit--;
5984 re = TAILQ_NEXT(re, entry);
5987 view_vborder(view);
5988 return err;
5991 static const struct got_error *
5992 browse_ref_tree(struct tog_view **new_view, int begin_x,
5993 struct tog_reflist_entry *re, struct got_repository *repo)
5995 const struct got_error *err = NULL;
5996 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5997 struct got_tree_object *tree = NULL;
5998 struct tog_view *tree_view;
6000 *new_view = NULL;
6002 err = resolve_reflist_entry(&commit_id, re, repo);
6003 if (err) {
6004 if (err->code != GOT_ERR_OBJ_TYPE)
6005 return err;
6006 else
6007 return NULL;
6010 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6011 if (err)
6012 goto done;
6014 err = got_object_open_as_tree(&tree, repo, tree_id);
6015 if (err)
6016 goto done;
6018 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6019 if (tree_view == NULL) {
6020 err = got_error_from_errno("view_open");
6021 goto done;
6024 err = open_tree_view(tree_view, tree, commit_id, repo);
6025 if (err)
6026 goto done;
6028 *new_view = tree_view;
6029 done:
6030 free(commit_id);
6031 free(tree_id);
6032 if (err) {
6033 if (tree)
6034 got_object_tree_close(tree);
6036 return err;
6038 static const struct got_error *
6039 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6041 const struct got_error *err = NULL;
6042 struct tog_ref_view_state *s = &view->state.ref;
6043 struct tog_view *log_view, *tree_view;
6044 int begin_x = 0;
6046 switch (ch) {
6047 case 'i':
6048 s->show_ids = !s->show_ids;
6049 break;
6050 case KEY_ENTER:
6051 case '\r':
6052 if (!s->selected_entry)
6053 break;
6054 if (view_is_parent_view(view))
6055 begin_x = view_split_begin_x(view->begin_x);
6056 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6057 s->repo);
6058 view->focussed = 0;
6059 log_view->focussed = 1;
6060 if (view_is_parent_view(view)) {
6061 err = view_close_child(view);
6062 if (err)
6063 return err;
6064 view_set_child(view, log_view);
6065 view->focus_child = 1;
6066 } else
6067 *new_view = log_view;
6068 break;
6069 case 't':
6070 if (!s->selected_entry)
6071 break;
6072 if (view_is_parent_view(view))
6073 begin_x = view_split_begin_x(view->begin_x);
6074 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6075 s->repo);
6076 if (err || tree_view == NULL)
6077 break;
6078 view->focussed = 0;
6079 tree_view->focussed = 1;
6080 if (view_is_parent_view(view)) {
6081 err = view_close_child(view);
6082 if (err)
6083 return err;
6084 view_set_child(view, tree_view);
6085 view->focus_child = 1;
6086 } else
6087 *new_view = tree_view;
6088 break;
6089 case 'k':
6090 case KEY_UP:
6091 if (s->selected > 0) {
6092 s->selected--;
6093 break;
6095 ref_scroll_up(s, 1);
6096 break;
6097 case KEY_PPAGE:
6098 case CTRL('b'):
6099 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6100 s->selected = 0;
6101 ref_scroll_up(s, MAX(0, view->nlines - 1));
6102 break;
6103 case 'j':
6104 case KEY_DOWN:
6105 if (s->selected < s->ndisplayed - 1) {
6106 s->selected++;
6107 break;
6109 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6110 /* can't scroll any further */
6111 break;
6112 ref_scroll_down(s, 1);
6113 break;
6114 case KEY_NPAGE:
6115 case CTRL('f'):
6116 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6117 /* can't scroll any further; move cursor down */
6118 if (s->selected < s->ndisplayed - 1)
6119 s->selected = s->ndisplayed - 1;
6120 break;
6122 ref_scroll_down(s, view->nlines - 1);
6123 break;
6124 case CTRL('l'):
6125 ref_view_free_refs(s);
6126 err = ref_view_load_refs(s);
6127 break;
6128 case KEY_RESIZE:
6129 if (s->selected > view->nlines)
6130 s->selected = s->ndisplayed - 1;
6131 break;
6132 default:
6133 break;
6136 return err;
6139 __dead static void
6140 usage_ref(void)
6142 endwin();
6143 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6144 getprogname());
6145 exit(1);
6148 static const struct got_error *
6149 cmd_ref(int argc, char *argv[])
6151 const struct got_error *error;
6152 struct got_repository *repo = NULL;
6153 struct got_worktree *worktree = NULL;
6154 char *cwd = NULL, *repo_path = NULL;
6155 int ch;
6156 struct tog_view *view;
6158 while ((ch = getopt(argc, argv, "r:")) != -1) {
6159 switch (ch) {
6160 case 'r':
6161 repo_path = realpath(optarg, NULL);
6162 if (repo_path == NULL)
6163 return got_error_from_errno2("realpath",
6164 optarg);
6165 break;
6166 default:
6167 usage_ref();
6168 /* NOTREACHED */
6172 argc -= optind;
6173 argv += optind;
6175 if (argc > 1)
6176 usage_ref();
6178 cwd = getcwd(NULL, 0);
6179 if (cwd == NULL)
6180 return got_error_from_errno("getcwd");
6182 error = got_worktree_open(&worktree, cwd);
6183 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6184 goto done;
6186 if (repo_path == NULL) {
6187 if (worktree)
6188 repo_path =
6189 strdup(got_worktree_get_repo_path(worktree));
6190 else
6191 repo_path = strdup(cwd);
6193 if (repo_path == NULL) {
6194 error = got_error_from_errno("strdup");
6195 goto done;
6198 error = got_repo_open(&repo, repo_path, NULL);
6199 if (error != NULL)
6200 goto done;
6202 init_curses();
6204 error = apply_unveil(got_repo_get_path(repo), NULL);
6205 if (error)
6206 goto done;
6208 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6209 if (view == NULL) {
6210 error = got_error_from_errno("view_open");
6211 goto done;
6214 error = open_ref_view(view, repo);
6215 if (error)
6216 goto done;
6218 if (worktree) {
6219 /* Release work tree lock. */
6220 got_worktree_close(worktree);
6221 worktree = NULL;
6223 error = view_loop(view);
6224 done:
6225 free(repo_path);
6226 free(cwd);
6227 if (repo)
6228 got_repo_close(repo);
6229 return error;
6232 static void
6233 list_commands(FILE *fp)
6235 int i;
6237 fprintf(fp, "commands:");
6238 for (i = 0; i < nitems(tog_commands); i++) {
6239 struct tog_cmd *cmd = &tog_commands[i];
6240 fprintf(fp, " %s", cmd->name);
6242 fputc('\n', fp);
6245 __dead static void
6246 usage(int hflag, int status)
6248 FILE *fp = (status == 0) ? stdout : stderr;
6250 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6251 getprogname());
6252 if (hflag) {
6253 fprintf(fp, "lazy usage: %s path\n", getprogname());
6254 list_commands(fp);
6256 exit(status);
6259 static char **
6260 make_argv(int argc, ...)
6262 va_list ap;
6263 char **argv;
6264 int i;
6266 va_start(ap, argc);
6268 argv = calloc(argc, sizeof(char *));
6269 if (argv == NULL)
6270 err(1, "calloc");
6271 for (i = 0; i < argc; i++) {
6272 argv[i] = strdup(va_arg(ap, char *));
6273 if (argv[i] == NULL)
6274 err(1, "strdup");
6277 va_end(ap);
6278 return argv;
6282 * Try to convert 'tog path' into a 'tog log path' command.
6283 * The user could simply have mistyped the command rather than knowingly
6284 * provided a path. So check whether argv[0] can in fact be resolved
6285 * to a path in the HEAD commit and print a special error if not.
6286 * This hack is for mpi@ <3
6288 static const struct got_error *
6289 tog_log_with_path(int argc, char *argv[])
6291 const struct got_error *error = NULL;
6292 struct tog_cmd *cmd = NULL;
6293 struct got_repository *repo = NULL;
6294 struct got_worktree *worktree = NULL;
6295 struct got_object_id *commit_id = NULL, *id = NULL;
6296 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6297 char *commit_id_str = NULL, **cmd_argv = NULL;
6299 cwd = getcwd(NULL, 0);
6300 if (cwd == NULL)
6301 return got_error_from_errno("getcwd");
6303 error = got_worktree_open(&worktree, cwd);
6304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6305 goto done;
6307 if (worktree)
6308 repo_path = strdup(got_worktree_get_repo_path(worktree));
6309 else
6310 repo_path = strdup(cwd);
6311 if (repo_path == NULL) {
6312 error = got_error_from_errno("strdup");
6313 goto done;
6316 error = got_repo_open(&repo, repo_path, NULL);
6317 if (error != NULL)
6318 goto done;
6320 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6321 repo, worktree);
6322 if (error)
6323 goto done;
6325 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6326 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6327 GOT_OBJ_TYPE_COMMIT, 1, repo);
6328 if (error)
6329 goto done;
6331 if (worktree) {
6332 got_worktree_close(worktree);
6333 worktree = NULL;
6336 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6337 if (error) {
6338 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6339 goto done;
6340 fprintf(stderr, "%s: '%s' is no known command or path\n",
6341 getprogname(), argv[0]);
6342 usage(1, 1);
6343 /* not reached */
6346 got_repo_close(repo);
6347 repo = NULL;
6349 error = got_object_id_str(&commit_id_str, commit_id);
6350 if (error)
6351 goto done;
6353 cmd = &tog_commands[0]; /* log */
6354 argc = 4;
6355 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6356 error = cmd->cmd_main(argc, cmd_argv);
6357 done:
6358 if (repo)
6359 got_repo_close(repo);
6360 if (worktree)
6361 got_worktree_close(worktree);
6362 free(id);
6363 free(commit_id_str);
6364 free(commit_id);
6365 free(cwd);
6366 free(repo_path);
6367 free(in_repo_path);
6368 if (cmd_argv) {
6369 int i;
6370 for (i = 0; i < argc; i++)
6371 free(cmd_argv[i]);
6372 free(cmd_argv);
6374 return error;
6377 int
6378 main(int argc, char *argv[])
6380 const struct got_error *error = NULL;
6381 struct tog_cmd *cmd = NULL;
6382 int ch, hflag = 0, Vflag = 0;
6383 char **cmd_argv = NULL;
6384 static struct option longopts[] = {
6385 { "version", no_argument, NULL, 'V' },
6386 { NULL, 0, NULL, 0}
6389 setlocale(LC_CTYPE, "");
6391 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6392 switch (ch) {
6393 case 'h':
6394 hflag = 1;
6395 break;
6396 case 'V':
6397 Vflag = 1;
6398 break;
6399 default:
6400 usage(hflag, 1);
6401 /* NOTREACHED */
6405 argc -= optind;
6406 argv += optind;
6407 optind = 1;
6408 optreset = 1;
6410 if (Vflag) {
6411 got_version_print_str();
6412 return 0;
6415 #ifndef PROFILE
6416 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6417 NULL) == -1)
6418 err(1, "pledge");
6419 #endif
6421 if (argc == 0) {
6422 if (hflag)
6423 usage(hflag, 0);
6424 /* Build an argument vector which runs a default command. */
6425 cmd = &tog_commands[0];
6426 argc = 1;
6427 cmd_argv = make_argv(argc, cmd->name);
6428 } else {
6429 int i;
6431 /* Did the user specify a command? */
6432 for (i = 0; i < nitems(tog_commands); i++) {
6433 if (strncmp(tog_commands[i].name, argv[0],
6434 strlen(argv[0])) == 0) {
6435 cmd = &tog_commands[i];
6436 break;
6441 if (cmd == NULL) {
6442 if (argc != 1)
6443 usage(0, 1);
6444 /* No command specified; try log with a path */
6445 error = tog_log_with_path(argc, argv);
6446 } else {
6447 if (hflag)
6448 cmd->cmd_usage();
6449 else
6450 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6453 endwin();
6454 putchar('\n');
6455 if (cmd_argv) {
6456 int i;
6457 for (i = 0; i < argc; i++)
6458 free(cmd_argv[i]);
6459 free(cmd_argv);
6462 if (error && error->code != GOT_ERR_CANCELLED)
6463 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6464 return 0;