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 const char *head_ref_name;
400 struct got_repository *repo;
401 struct got_tree_entry *matched_entry;
402 struct tog_colors colors;
403 };
405 struct tog_reflist_entry {
406 TAILQ_ENTRY(tog_reflist_entry) entry;
407 struct got_reference *ref;
408 int idx;
409 };
411 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
413 struct tog_ref_view_state {
414 struct got_reflist_head simplerefs; /* SIMPLEQ */
415 struct tog_reflist_head refs; /* TAILQ */
416 struct tog_reflist_entry *first_displayed_entry;
417 struct tog_reflist_entry *last_displayed_entry;
418 struct tog_reflist_entry *selected_entry;
419 int nrefs, ndisplayed, selected, show_ids;
420 struct got_repository *repo;
421 struct tog_reflist_entry *matched_entry;
422 struct tog_colors colors;
423 };
425 /*
426 * We implement two types of views: parent views and child views.
428 * The 'Tab' key switches focus between a parent view and its child view.
429 * Child views are shown side-by-side to their parent view, provided
430 * there is enough screen estate.
432 * When a new view is opened from within a parent view, this new view
433 * becomes a child view of the parent view, replacing any existing child.
435 * When a new view is opened from within a child view, this new view
436 * becomes a parent view which will obscure the views below until the
437 * user quits the new parent view by typing 'q'.
439 * This list of views contains parent views only.
440 * Child views are only pointed to by their parent view.
441 */
442 TAILQ_HEAD(tog_view_list_head, tog_view);
444 struct tog_view {
445 TAILQ_ENTRY(tog_view) entry;
446 WINDOW *window;
447 PANEL *panel;
448 int nlines, ncols, begin_y, begin_x;
449 int lines, cols; /* copies of LINES and COLS */
450 int focussed; /* Only set on one parent or child view at a time. */
451 int dying;
452 struct tog_view *parent;
453 struct tog_view *child;
455 /*
456 * This flag is initially set on parent views when a new child view
457 * is created. It gets toggled when the 'Tab' key switches focus
458 * between parent and child.
459 * The flag indicates whether focus should be passed on to our child
460 * view if this parent view gets picked for focus after another parent
461 * view was closed. This prevents child views from losing focus in such
462 * situations.
463 */
464 int focus_child;
466 /* type-specific state */
467 enum tog_view_type type;
468 union {
469 struct tog_diff_view_state diff;
470 struct tog_log_view_state log;
471 struct tog_blame_view_state blame;
472 struct tog_tree_view_state tree;
473 struct tog_ref_view_state ref;
474 } state;
476 const struct got_error *(*show)(struct tog_view *);
477 const struct got_error *(*input)(struct tog_view **,
478 struct tog_view *, int);
479 const struct got_error *(*close)(struct tog_view *);
481 const struct got_error *(*search_start)(struct tog_view *);
482 const struct got_error *(*search_next)(struct tog_view *);
483 int searching;
484 #define TOG_SEARCH_FORWARD 1
485 #define TOG_SEARCH_BACKWARD 2
486 int search_next_done;
487 #define TOG_SEARCH_HAVE_MORE 1
488 #define TOG_SEARCH_NO_MORE 2
489 #define TOG_SEARCH_HAVE_NONE 3
490 regex_t regex;
491 regmatch_t regmatch;
492 };
494 static const struct got_error *open_diff_view(struct tog_view *,
495 struct got_object_id *, struct got_object_id *,
496 const char *, const char *, int, int, int, struct tog_view *,
497 struct got_repository *);
498 static const struct got_error *show_diff_view(struct tog_view *);
499 static const struct got_error *input_diff_view(struct tog_view **,
500 struct tog_view *, int);
501 static const struct got_error* close_diff_view(struct tog_view *);
502 static const struct got_error *search_start_diff_view(struct tog_view *);
503 static const struct got_error *search_next_diff_view(struct tog_view *);
505 static const struct got_error *open_log_view(struct tog_view *,
506 struct got_object_id *, struct got_repository *,
507 const char *, const char *, int);
508 static const struct got_error * show_log_view(struct tog_view *);
509 static const struct got_error *input_log_view(struct tog_view **,
510 struct tog_view *, int);
511 static const struct got_error *close_log_view(struct tog_view *);
512 static const struct got_error *search_start_log_view(struct tog_view *);
513 static const struct got_error *search_next_log_view(struct tog_view *);
515 static const struct got_error *open_blame_view(struct tog_view *, char *,
516 struct got_object_id *, struct got_repository *);
517 static const struct got_error *show_blame_view(struct tog_view *);
518 static const struct got_error *input_blame_view(struct tog_view **,
519 struct tog_view *, int);
520 static const struct got_error *close_blame_view(struct tog_view *);
521 static const struct got_error *search_start_blame_view(struct tog_view *);
522 static const struct got_error *search_next_blame_view(struct tog_view *);
524 static const struct got_error *open_tree_view(struct tog_view *,
525 struct got_tree_object *, struct got_object_id *, const char *,
526 struct got_repository *);
527 static const struct got_error *show_tree_view(struct tog_view *);
528 static const struct got_error *input_tree_view(struct tog_view **,
529 struct tog_view *, int);
530 static const struct got_error *close_tree_view(struct tog_view *);
531 static const struct got_error *search_start_tree_view(struct tog_view *);
532 static const struct got_error *search_next_tree_view(struct tog_view *);
534 static const struct got_error *open_ref_view(struct tog_view *,
535 struct got_repository *);
536 static const struct got_error *show_ref_view(struct tog_view *);
537 static const struct got_error *input_ref_view(struct tog_view **,
538 struct tog_view *, int);
539 static const struct got_error *close_ref_view(struct tog_view *);
540 static const struct got_error *search_start_ref_view(struct tog_view *);
541 static const struct got_error *search_next_ref_view(struct tog_view *);
543 static volatile sig_atomic_t tog_sigwinch_received;
544 static volatile sig_atomic_t tog_sigpipe_received;
545 static volatile sig_atomic_t tog_sigcont_received;
547 static void
548 tog_sigwinch(int signo)
550 tog_sigwinch_received = 1;
553 static void
554 tog_sigpipe(int signo)
556 tog_sigpipe_received = 1;
559 static void
560 tog_sigcont(int signo)
562 tog_sigcont_received = 1;
565 static const struct got_error *
566 view_close(struct tog_view *view)
568 const struct got_error *err = NULL;
570 if (view->child) {
571 view_close(view->child);
572 view->child = NULL;
574 if (view->close)
575 err = view->close(view);
576 if (view->panel)
577 del_panel(view->panel);
578 if (view->window)
579 delwin(view->window);
580 free(view);
581 return err;
584 static struct tog_view *
585 view_open(int nlines, int ncols, int begin_y, int begin_x,
586 enum tog_view_type type)
588 struct tog_view *view = calloc(1, sizeof(*view));
590 if (view == NULL)
591 return NULL;
593 view->type = type;
594 view->lines = LINES;
595 view->cols = COLS;
596 view->nlines = nlines ? nlines : LINES - begin_y;
597 view->ncols = ncols ? ncols : COLS - begin_x;
598 view->begin_y = begin_y;
599 view->begin_x = begin_x;
600 view->window = newwin(nlines, ncols, begin_y, begin_x);
601 if (view->window == NULL) {
602 view_close(view);
603 return NULL;
605 view->panel = new_panel(view->window);
606 if (view->panel == NULL ||
607 set_panel_userptr(view->panel, view) != OK) {
608 view_close(view);
609 return NULL;
612 keypad(view->window, TRUE);
613 return view;
616 static int
617 view_split_begin_x(int begin_x)
619 if (begin_x > 0 || COLS < 120)
620 return 0;
621 return (COLS - MAX(COLS / 2, 80));
624 static const struct got_error *view_resize(struct tog_view *);
626 static const struct got_error *
627 view_splitscreen(struct tog_view *view)
629 const struct got_error *err = NULL;
631 view->begin_y = 0;
632 view->begin_x = view_split_begin_x(0);
633 view->nlines = LINES;
634 view->ncols = COLS - view->begin_x;
635 view->lines = LINES;
636 view->cols = COLS;
637 err = view_resize(view);
638 if (err)
639 return err;
641 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
642 return got_error_from_errno("mvwin");
644 return NULL;
647 static const struct got_error *
648 view_fullscreen(struct tog_view *view)
650 const struct got_error *err = NULL;
652 view->begin_x = 0;
653 view->begin_y = 0;
654 view->nlines = LINES;
655 view->ncols = COLS;
656 view->lines = LINES;
657 view->cols = COLS;
658 err = view_resize(view);
659 if (err)
660 return err;
662 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
663 return got_error_from_errno("mvwin");
665 return NULL;
668 static int
669 view_is_parent_view(struct tog_view *view)
671 return view->parent == NULL;
674 static const struct got_error *
675 view_resize(struct tog_view *view)
677 int nlines, ncols;
679 if (view->lines > LINES)
680 nlines = view->nlines - (view->lines - LINES);
681 else
682 nlines = view->nlines + (LINES - view->lines);
684 if (view->cols > COLS)
685 ncols = view->ncols - (view->cols - COLS);
686 else
687 ncols = view->ncols + (COLS - view->cols);
689 if (wresize(view->window, nlines, ncols) == ERR)
690 return got_error_from_errno("wresize");
691 if (replace_panel(view->panel, view->window) == ERR)
692 return got_error_from_errno("replace_panel");
693 wclear(view->window);
695 view->nlines = nlines;
696 view->ncols = ncols;
697 view->lines = LINES;
698 view->cols = COLS;
700 if (view->child) {
701 view->child->begin_x = view_split_begin_x(view->begin_x);
702 if (view->child->begin_x == 0) {
703 view_fullscreen(view->child);
704 if (view->child->focussed)
705 show_panel(view->child->panel);
706 else
707 show_panel(view->panel);
708 } else {
709 view_splitscreen(view->child);
710 show_panel(view->child->panel);
714 return NULL;
717 static const struct got_error *
718 view_close_child(struct tog_view *view)
720 const struct got_error *err = NULL;
722 if (view->child == NULL)
723 return NULL;
725 err = view_close(view->child);
726 view->child = NULL;
727 return err;
730 static void
731 view_set_child(struct tog_view *view, struct tog_view *child)
733 view->child = child;
734 child->parent = view;
737 static int
738 view_is_splitscreen(struct tog_view *view)
740 return view->begin_x > 0;
743 static void
744 tog_resizeterm(void)
746 int cols, lines;
747 struct winsize size;
749 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
750 cols = 80; /* Default */
751 lines = 24;
752 } else {
753 cols = size.ws_col;
754 lines = size.ws_row;
756 resize_term(lines, cols);
759 static const struct got_error *
760 view_search_start(struct tog_view *view)
762 const struct got_error *err = NULL;
763 char pattern[1024];
764 int ret;
766 if (view->nlines < 1)
767 return NULL;
769 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
770 wclrtoeol(view->window);
772 nocbreak();
773 echo();
774 ret = wgetnstr(view->window, pattern, sizeof(pattern));
775 cbreak();
776 noecho();
777 if (ret == ERR)
778 return NULL;
780 if (view->searching) {
781 regfree(&view->regex);
782 view->searching = 0;
785 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
786 err = view->search_start(view);
787 if (err) {
788 regfree(&view->regex);
789 return err;
791 view->searching = TOG_SEARCH_FORWARD;
792 view->search_next_done = 0;
793 view->search_next(view);
796 return NULL;
799 static const struct got_error *
800 view_input(struct tog_view **new, int *done, struct tog_view *view,
801 struct tog_view_list_head *views)
803 const struct got_error *err = NULL;
804 struct tog_view *v;
805 int ch, errcode;
807 *new = NULL;
809 /* Clear "no matches" indicator. */
810 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
811 view->search_next_done == TOG_SEARCH_HAVE_NONE)
812 view->search_next_done = TOG_SEARCH_HAVE_MORE;
814 if (view->searching && !view->search_next_done) {
815 errcode = pthread_mutex_unlock(&tog_mutex);
816 if (errcode)
817 return got_error_set_errno(errcode,
818 "pthread_mutex_unlock");
819 pthread_yield();
820 errcode = pthread_mutex_lock(&tog_mutex);
821 if (errcode)
822 return got_error_set_errno(errcode,
823 "pthread_mutex_lock");
824 view->search_next(view);
825 return NULL;
828 nodelay(stdscr, FALSE);
829 /* Allow threads to make progress while we are waiting for input. */
830 errcode = pthread_mutex_unlock(&tog_mutex);
831 if (errcode)
832 return got_error_set_errno(errcode, "pthread_mutex_unlock");
833 ch = wgetch(view->window);
834 errcode = pthread_mutex_lock(&tog_mutex);
835 if (errcode)
836 return got_error_set_errno(errcode, "pthread_mutex_lock");
837 nodelay(stdscr, TRUE);
839 if (tog_sigwinch_received || tog_sigcont_received) {
840 tog_resizeterm();
841 tog_sigwinch_received = 0;
842 tog_sigcont_received = 0;
843 TAILQ_FOREACH(v, views, entry) {
844 err = view_resize(v);
845 if (err)
846 return err;
847 err = v->input(new, v, KEY_RESIZE);
848 if (err)
849 return err;
850 if (v->child) {
851 err = view_resize(v->child);
852 if (err)
853 return err;
854 err = v->child->input(new, v->child,
855 KEY_RESIZE);
856 if (err)
857 return err;
862 switch (ch) {
863 case ERR:
864 break;
865 case '\t':
866 if (view->child) {
867 view->focussed = 0;
868 view->child->focussed = 1;
869 view->focus_child = 1;
870 } else if (view->parent) {
871 view->focussed = 0;
872 view->parent->focussed = 1;
873 view->parent->focus_child = 0;
875 break;
876 case 'q':
877 err = view->input(new, view, ch);
878 view->dying = 1;
879 break;
880 case 'Q':
881 *done = 1;
882 break;
883 case 'f':
884 if (view_is_parent_view(view)) {
885 if (view->child == NULL)
886 break;
887 if (view_is_splitscreen(view->child)) {
888 view->focussed = 0;
889 view->child->focussed = 1;
890 err = view_fullscreen(view->child);
891 } else
892 err = view_splitscreen(view->child);
893 if (err)
894 break;
895 err = view->child->input(new, view->child,
896 KEY_RESIZE);
897 } else {
898 if (view_is_splitscreen(view)) {
899 view->parent->focussed = 0;
900 view->focussed = 1;
901 err = view_fullscreen(view);
902 } else {
903 err = view_splitscreen(view);
905 if (err)
906 break;
907 err = view->input(new, view, KEY_RESIZE);
909 break;
910 case KEY_RESIZE:
911 break;
912 case '/':
913 if (view->search_start)
914 view_search_start(view);
915 else
916 err = view->input(new, view, ch);
917 break;
918 case 'N':
919 case 'n':
920 if (view->search_next) {
921 view->searching = (ch == 'n' ?
922 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
923 view->search_next_done = 0;
924 view->search_next(view);
925 } else
926 err = view->input(new, view, ch);
927 break;
928 default:
929 err = view->input(new, view, ch);
930 break;
933 return err;
936 void
937 view_vborder(struct tog_view *view)
939 PANEL *panel;
940 struct tog_view *view_above;
942 if (view->parent)
943 return view_vborder(view->parent);
945 panel = panel_above(view->panel);
946 if (panel == NULL)
947 return;
949 view_above = panel_userptr(panel);
950 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
951 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
954 int
955 view_needs_focus_indication(struct tog_view *view)
957 if (view_is_parent_view(view)) {
958 if (view->child == NULL || view->child->focussed)
959 return 0;
960 if (!view_is_splitscreen(view->child))
961 return 0;
962 } else if (!view_is_splitscreen(view))
963 return 0;
965 return view->focussed;
968 static const struct got_error *
969 view_loop(struct tog_view *view)
971 const struct got_error *err = NULL;
972 struct tog_view_list_head views;
973 struct tog_view *new_view;
974 int fast_refresh = 10;
975 int done = 0, errcode;
977 errcode = pthread_mutex_lock(&tog_mutex);
978 if (errcode)
979 return got_error_set_errno(errcode, "pthread_mutex_lock");
981 TAILQ_INIT(&views);
982 TAILQ_INSERT_HEAD(&views, view, entry);
984 view->focussed = 1;
985 err = view->show(view);
986 if (err)
987 return err;
988 update_panels();
989 doupdate();
990 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
991 /* Refresh fast during initialization, then become slower. */
992 if (fast_refresh && fast_refresh-- == 0)
993 halfdelay(10); /* switch to once per second */
995 err = view_input(&new_view, &done, view, &views);
996 if (err)
997 break;
998 if (view->dying) {
999 struct tog_view *v, *prev = NULL;
1001 if (view_is_parent_view(view))
1002 prev = TAILQ_PREV(view, tog_view_list_head,
1003 entry);
1004 else if (view->parent)
1005 prev = view->parent;
1007 if (view->parent) {
1008 view->parent->child = NULL;
1009 view->parent->focus_child = 0;
1010 } else
1011 TAILQ_REMOVE(&views, view, entry);
1013 err = view_close(view);
1014 if (err)
1015 goto done;
1017 view = NULL;
1018 TAILQ_FOREACH(v, &views, entry) {
1019 if (v->focussed)
1020 break;
1022 if (view == NULL && new_view == NULL) {
1023 /* No view has focus. Try to pick one. */
1024 if (prev)
1025 view = prev;
1026 else if (!TAILQ_EMPTY(&views)) {
1027 view = TAILQ_LAST(&views,
1028 tog_view_list_head);
1030 if (view) {
1031 if (view->focus_child) {
1032 view->child->focussed = 1;
1033 view = view->child;
1034 } else
1035 view->focussed = 1;
1039 if (new_view) {
1040 struct tog_view *v, *t;
1041 /* Only allow one parent view per type. */
1042 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1043 if (v->type != new_view->type)
1044 continue;
1045 TAILQ_REMOVE(&views, v, entry);
1046 err = view_close(v);
1047 if (err)
1048 goto done;
1049 break;
1051 TAILQ_INSERT_TAIL(&views, new_view, entry);
1052 view = new_view;
1054 if (view) {
1055 if (view_is_parent_view(view)) {
1056 if (view->child && view->child->focussed)
1057 view = view->child;
1058 } else {
1059 if (view->parent && view->parent->focussed)
1060 view = view->parent;
1062 show_panel(view->panel);
1063 if (view->child && view_is_splitscreen(view->child))
1064 show_panel(view->child->panel);
1065 if (view->parent && view_is_splitscreen(view)) {
1066 err = view->parent->show(view->parent);
1067 if (err)
1068 goto done;
1070 err = view->show(view);
1071 if (err)
1072 goto done;
1073 if (view->child) {
1074 err = view->child->show(view->child);
1075 if (err)
1076 goto done;
1078 update_panels();
1079 doupdate();
1082 done:
1083 while (!TAILQ_EMPTY(&views)) {
1084 view = TAILQ_FIRST(&views);
1085 TAILQ_REMOVE(&views, view, entry);
1086 view_close(view);
1089 errcode = pthread_mutex_unlock(&tog_mutex);
1090 if (errcode && err == NULL)
1091 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1093 return err;
1096 __dead static void
1097 usage_log(void)
1099 endwin();
1100 fprintf(stderr,
1101 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1102 getprogname());
1103 exit(1);
1106 /* Create newly allocated wide-character string equivalent to a byte string. */
1107 static const struct got_error *
1108 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1110 char *vis = NULL;
1111 const struct got_error *err = NULL;
1113 *ws = NULL;
1114 *wlen = mbstowcs(NULL, s, 0);
1115 if (*wlen == (size_t)-1) {
1116 int vislen;
1117 if (errno != EILSEQ)
1118 return got_error_from_errno("mbstowcs");
1120 /* byte string invalid in current encoding; try to "fix" it */
1121 err = got_mbsavis(&vis, &vislen, s);
1122 if (err)
1123 return err;
1124 *wlen = mbstowcs(NULL, vis, 0);
1125 if (*wlen == (size_t)-1) {
1126 err = got_error_from_errno("mbstowcs"); /* give up */
1127 goto done;
1131 *ws = calloc(*wlen + 1, sizeof(**ws));
1132 if (*ws == NULL) {
1133 err = got_error_from_errno("calloc");
1134 goto done;
1137 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1138 err = got_error_from_errno("mbstowcs");
1139 done:
1140 free(vis);
1141 if (err) {
1142 free(*ws);
1143 *ws = NULL;
1144 *wlen = 0;
1146 return err;
1149 /* Format a line for display, ensuring that it won't overflow a width limit. */
1150 static const struct got_error *
1151 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1152 int col_tab_align)
1154 const struct got_error *err = NULL;
1155 int cols = 0;
1156 wchar_t *wline = NULL;
1157 size_t wlen;
1158 int i;
1160 *wlinep = NULL;
1161 *widthp = 0;
1163 err = mbs2ws(&wline, &wlen, line);
1164 if (err)
1165 return err;
1167 i = 0;
1168 while (i < wlen) {
1169 int width = wcwidth(wline[i]);
1171 if (width == 0) {
1172 i++;
1173 continue;
1176 if (width == 1 || width == 2) {
1177 if (cols + width > wlimit)
1178 break;
1179 cols += width;
1180 i++;
1181 } else if (width == -1) {
1182 if (wline[i] == L'\t') {
1183 width = TABSIZE -
1184 ((cols + col_tab_align) % TABSIZE);
1185 if (cols + width > wlimit)
1186 break;
1187 cols += width;
1189 i++;
1190 } else {
1191 err = got_error_from_errno("wcwidth");
1192 goto done;
1195 wline[i] = L'\0';
1196 if (widthp)
1197 *widthp = cols;
1198 done:
1199 if (err)
1200 free(wline);
1201 else
1202 *wlinep = wline;
1203 return err;
1206 static const struct got_error*
1207 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1208 struct got_object_id *id, struct got_repository *repo)
1210 static const struct got_error *err = NULL;
1211 struct got_reflist_entry *re;
1212 char *s;
1213 const char *name;
1215 *refs_str = NULL;
1217 SIMPLEQ_FOREACH(re, refs, entry) {
1218 struct got_tag_object *tag = NULL;
1219 struct got_object_id *ref_id;
1220 int cmp;
1222 name = got_ref_get_name(re->ref);
1223 if (strcmp(name, GOT_REF_HEAD) == 0)
1224 continue;
1225 if (strncmp(name, "refs/", 5) == 0)
1226 name += 5;
1227 if (strncmp(name, "got/", 4) == 0)
1228 continue;
1229 if (strncmp(name, "heads/", 6) == 0)
1230 name += 6;
1231 if (strncmp(name, "remotes/", 8) == 0) {
1232 name += 8;
1233 s = strstr(name, "/" GOT_REF_HEAD);
1234 if (s != NULL && s[strlen(s)] == '\0')
1235 continue;
1237 err = got_ref_resolve(&ref_id, repo, re->ref);
1238 if (err)
1239 break;
1240 if (strncmp(name, "tags/", 5) == 0) {
1241 err = got_object_open_as_tag(&tag, repo, ref_id);
1242 if (err) {
1243 if (err->code != GOT_ERR_OBJ_TYPE) {
1244 free(ref_id);
1245 break;
1247 /* Ref points at something other than a tag. */
1248 err = NULL;
1249 tag = NULL;
1252 cmp = got_object_id_cmp(tag ?
1253 got_object_tag_get_object_id(tag) : ref_id, id);
1254 free(ref_id);
1255 if (tag)
1256 got_object_tag_close(tag);
1257 if (cmp != 0)
1258 continue;
1259 s = *refs_str;
1260 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1261 s ? ", " : "", name) == -1) {
1262 err = got_error_from_errno("asprintf");
1263 free(s);
1264 *refs_str = NULL;
1265 break;
1267 free(s);
1270 return err;
1273 static const struct got_error *
1274 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1275 int col_tab_align)
1277 char *smallerthan, *at;
1279 smallerthan = strchr(author, '<');
1280 if (smallerthan && smallerthan[1] != '\0')
1281 author = smallerthan + 1;
1282 at = strchr(author, '@');
1283 if (at)
1284 *at = '\0';
1285 return format_line(wauthor, author_width, author, limit, col_tab_align);
1288 static const struct got_error *
1289 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1290 struct got_object_id *id, const size_t date_display_cols,
1291 int author_display_cols)
1293 struct tog_log_view_state *s = &view->state.log;
1294 const struct got_error *err = NULL;
1295 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1296 char *logmsg0 = NULL, *logmsg = NULL;
1297 char *author = NULL;
1298 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1299 int author_width, logmsg_width;
1300 char *newline, *line = NULL;
1301 int col, limit;
1302 const int avail = view->ncols;
1303 struct tm tm;
1304 time_t committer_time;
1305 struct tog_color *tc;
1307 committer_time = got_object_commit_get_committer_time(commit);
1308 if (localtime_r(&committer_time, &tm) == NULL)
1309 return got_error_from_errno("localtime_r");
1310 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1311 >= sizeof(datebuf))
1312 return got_error(GOT_ERR_NO_SPACE);
1314 if (avail <= date_display_cols)
1315 limit = MIN(sizeof(datebuf) - 1, avail);
1316 else
1317 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1318 tc = get_color(&s->colors, TOG_COLOR_DATE);
1319 if (tc)
1320 wattr_on(view->window,
1321 COLOR_PAIR(tc->colorpair), NULL);
1322 waddnstr(view->window, datebuf, limit);
1323 if (tc)
1324 wattr_off(view->window,
1325 COLOR_PAIR(tc->colorpair), NULL);
1326 col = limit;
1327 if (col > avail)
1328 goto done;
1330 if (avail >= 120) {
1331 char *id_str;
1332 err = got_object_id_str(&id_str, id);
1333 if (err)
1334 goto done;
1335 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1336 if (tc)
1337 wattr_on(view->window,
1338 COLOR_PAIR(tc->colorpair), NULL);
1339 wprintw(view->window, "%.8s ", id_str);
1340 if (tc)
1341 wattr_off(view->window,
1342 COLOR_PAIR(tc->colorpair), NULL);
1343 free(id_str);
1344 col += 9;
1345 if (col > avail)
1346 goto done;
1349 author = strdup(got_object_commit_get_author(commit));
1350 if (author == NULL) {
1351 err = got_error_from_errno("strdup");
1352 goto done;
1354 err = format_author(&wauthor, &author_width, author, avail - col, col);
1355 if (err)
1356 goto done;
1357 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1358 if (tc)
1359 wattr_on(view->window,
1360 COLOR_PAIR(tc->colorpair), NULL);
1361 waddwstr(view->window, wauthor);
1362 if (tc)
1363 wattr_off(view->window,
1364 COLOR_PAIR(tc->colorpair), NULL);
1365 col += author_width;
1366 while (col < avail && author_width < author_display_cols + 2) {
1367 waddch(view->window, ' ');
1368 col++;
1369 author_width++;
1371 if (col > avail)
1372 goto done;
1374 err = got_object_commit_get_logmsg(&logmsg0, commit);
1375 if (err)
1376 goto done;
1377 logmsg = logmsg0;
1378 while (*logmsg == '\n')
1379 logmsg++;
1380 newline = strchr(logmsg, '\n');
1381 if (newline)
1382 *newline = '\0';
1383 limit = avail - col;
1384 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1385 if (err)
1386 goto done;
1387 waddwstr(view->window, wlogmsg);
1388 col += logmsg_width;
1389 while (col < avail) {
1390 waddch(view->window, ' ');
1391 col++;
1393 done:
1394 free(logmsg0);
1395 free(wlogmsg);
1396 free(author);
1397 free(wauthor);
1398 free(line);
1399 return err;
1402 static struct commit_queue_entry *
1403 alloc_commit_queue_entry(struct got_commit_object *commit,
1404 struct got_object_id *id)
1406 struct commit_queue_entry *entry;
1408 entry = calloc(1, sizeof(*entry));
1409 if (entry == NULL)
1410 return NULL;
1412 entry->id = id;
1413 entry->commit = commit;
1414 return entry;
1417 static void
1418 pop_commit(struct commit_queue *commits)
1420 struct commit_queue_entry *entry;
1422 entry = TAILQ_FIRST(&commits->head);
1423 TAILQ_REMOVE(&commits->head, entry, entry);
1424 got_object_commit_close(entry->commit);
1425 commits->ncommits--;
1426 /* Don't free entry->id! It is owned by the commit graph. */
1427 free(entry);
1430 static void
1431 free_commits(struct commit_queue *commits)
1433 while (!TAILQ_EMPTY(&commits->head))
1434 pop_commit(commits);
1437 static const struct got_error *
1438 match_commit(int *have_match, struct got_object_id *id,
1439 struct got_commit_object *commit, regex_t *regex)
1441 const struct got_error *err = NULL;
1442 regmatch_t regmatch;
1443 char *id_str = NULL, *logmsg = NULL;
1445 *have_match = 0;
1447 err = got_object_id_str(&id_str, id);
1448 if (err)
1449 return err;
1451 err = got_object_commit_get_logmsg(&logmsg, commit);
1452 if (err)
1453 goto done;
1455 if (regexec(regex, got_object_commit_get_author(commit), 1,
1456 &regmatch, 0) == 0 ||
1457 regexec(regex, got_object_commit_get_committer(commit), 1,
1458 &regmatch, 0) == 0 ||
1459 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1460 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1461 *have_match = 1;
1462 done:
1463 free(id_str);
1464 free(logmsg);
1465 return err;
1468 static const struct got_error *
1469 queue_commits(struct tog_log_thread_args *a)
1471 const struct got_error *err = NULL;
1474 * We keep all commits open throughout the lifetime of the log
1475 * view in order to avoid having to re-fetch commits from disk
1476 * while updating the display.
1478 do {
1479 struct got_object_id *id;
1480 struct got_commit_object *commit;
1481 struct commit_queue_entry *entry;
1482 int errcode;
1484 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1485 NULL, NULL);
1486 if (err || id == NULL)
1487 break;
1489 err = got_object_open_as_commit(&commit, a->repo, id);
1490 if (err)
1491 break;
1492 entry = alloc_commit_queue_entry(commit, id);
1493 if (entry == NULL) {
1494 err = got_error_from_errno("alloc_commit_queue_entry");
1495 break;
1498 errcode = pthread_mutex_lock(&tog_mutex);
1499 if (errcode) {
1500 err = got_error_set_errno(errcode,
1501 "pthread_mutex_lock");
1502 break;
1505 entry->idx = a->commits->ncommits;
1506 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1507 a->commits->ncommits++;
1509 if (*a->searching == TOG_SEARCH_FORWARD &&
1510 !*a->search_next_done) {
1511 int have_match;
1512 err = match_commit(&have_match, id, commit, a->regex);
1513 if (err)
1514 break;
1515 if (have_match)
1516 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1519 errcode = pthread_mutex_unlock(&tog_mutex);
1520 if (errcode && err == NULL)
1521 err = got_error_set_errno(errcode,
1522 "pthread_mutex_unlock");
1523 if (err)
1524 break;
1525 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1527 return err;
1530 static void
1531 select_commit(struct tog_log_view_state *s)
1533 struct commit_queue_entry *entry;
1534 int ncommits = 0;
1536 entry = s->first_displayed_entry;
1537 while (entry) {
1538 if (ncommits == s->selected) {
1539 s->selected_entry = entry;
1540 break;
1542 entry = TAILQ_NEXT(entry, entry);
1543 ncommits++;
1547 static const struct got_error *
1548 draw_commits(struct tog_view *view)
1550 const struct got_error *err = NULL;
1551 struct tog_log_view_state *s = &view->state.log;
1552 struct commit_queue_entry *entry = s->selected_entry;
1553 const int limit = view->nlines;
1554 int width;
1555 int ncommits, author_cols = 4;
1556 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1557 char *refs_str = NULL;
1558 wchar_t *wline;
1559 struct tog_color *tc;
1560 static const size_t date_display_cols = 12;
1562 if (s->selected_entry &&
1563 !(view->searching && view->search_next_done == 0)) {
1564 err = got_object_id_str(&id_str, s->selected_entry->id);
1565 if (err)
1566 return err;
1567 err = build_refs_str(&refs_str, &s->refs,
1568 s->selected_entry->id, s->repo);
1569 if (err)
1570 goto done;
1573 if (s->thread_args.commits_needed == 0)
1574 halfdelay(10); /* disable fast refresh */
1576 if (s->thread_args.commits_needed > 0) {
1577 if (asprintf(&ncommits_str, " [%d/%d] %s",
1578 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1579 (view->searching && !view->search_next_done) ?
1580 "searching..." : "loading...") == -1) {
1581 err = got_error_from_errno("asprintf");
1582 goto done;
1584 } else {
1585 const char *search_str = NULL;
1587 if (view->searching) {
1588 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1589 search_str = "no more matches";
1590 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1591 search_str = "no matches found";
1592 else if (!view->search_next_done)
1593 search_str = "searching...";
1596 if (asprintf(&ncommits_str, " [%d/%d] %s",
1597 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1598 search_str ? search_str :
1599 (refs_str ? refs_str : "")) == -1) {
1600 err = got_error_from_errno("asprintf");
1601 goto done;
1605 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1606 if (asprintf(&header, "commit %s %s%s",
1607 id_str ? id_str : "........................................",
1608 s->in_repo_path, ncommits_str) == -1) {
1609 err = got_error_from_errno("asprintf");
1610 header = NULL;
1611 goto done;
1613 } else if (asprintf(&header, "commit %s%s",
1614 id_str ? id_str : "........................................",
1615 ncommits_str) == -1) {
1616 err = got_error_from_errno("asprintf");
1617 header = NULL;
1618 goto done;
1620 err = format_line(&wline, &width, header, view->ncols, 0);
1621 if (err)
1622 goto done;
1624 werase(view->window);
1626 if (view_needs_focus_indication(view))
1627 wstandout(view->window);
1628 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1629 if (tc)
1630 wattr_on(view->window,
1631 COLOR_PAIR(tc->colorpair), NULL);
1632 waddwstr(view->window, wline);
1633 if (tc)
1634 wattr_off(view->window,
1635 COLOR_PAIR(tc->colorpair), NULL);
1636 while (width < view->ncols) {
1637 waddch(view->window, ' ');
1638 width++;
1640 if (view_needs_focus_indication(view))
1641 wstandend(view->window);
1642 free(wline);
1643 if (limit <= 1)
1644 goto done;
1646 /* Grow author column size if necessary. */
1647 entry = s->first_displayed_entry;
1648 ncommits = 0;
1649 while (entry) {
1650 char *author;
1651 wchar_t *wauthor;
1652 int width;
1653 if (ncommits >= limit - 1)
1654 break;
1655 author = strdup(got_object_commit_get_author(entry->commit));
1656 if (author == NULL) {
1657 err = got_error_from_errno("strdup");
1658 goto done;
1660 err = format_author(&wauthor, &width, author, COLS,
1661 date_display_cols);
1662 if (author_cols < width)
1663 author_cols = width;
1664 free(wauthor);
1665 free(author);
1666 ncommits++;
1667 entry = TAILQ_NEXT(entry, entry);
1670 entry = s->first_displayed_entry;
1671 s->last_displayed_entry = s->first_displayed_entry;
1672 ncommits = 0;
1673 while (entry) {
1674 if (ncommits >= limit - 1)
1675 break;
1676 if (ncommits == s->selected)
1677 wstandout(view->window);
1678 err = draw_commit(view, entry->commit, entry->id,
1679 date_display_cols, author_cols);
1680 if (ncommits == s->selected)
1681 wstandend(view->window);
1682 if (err)
1683 goto done;
1684 ncommits++;
1685 s->last_displayed_entry = entry;
1686 entry = TAILQ_NEXT(entry, entry);
1689 view_vborder(view);
1690 done:
1691 free(id_str);
1692 free(refs_str);
1693 free(ncommits_str);
1694 free(header);
1695 return err;
1698 static void
1699 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1701 struct commit_queue_entry *entry;
1702 int nscrolled = 0;
1704 entry = TAILQ_FIRST(&s->commits.head);
1705 if (s->first_displayed_entry == entry)
1706 return;
1708 entry = s->first_displayed_entry;
1709 while (entry && nscrolled < maxscroll) {
1710 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1711 if (entry) {
1712 s->first_displayed_entry = entry;
1713 nscrolled++;
1718 static const struct got_error *
1719 trigger_log_thread(struct tog_view *view, int wait)
1721 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1722 int errcode;
1724 halfdelay(1); /* fast refresh while loading commits */
1726 while (ta->commits_needed > 0) {
1727 if (ta->log_complete)
1728 break;
1730 /* Wake the log thread. */
1731 errcode = pthread_cond_signal(&ta->need_commits);
1732 if (errcode)
1733 return got_error_set_errno(errcode,
1734 "pthread_cond_signal");
1737 * The mutex will be released while the view loop waits
1738 * in wgetch(), at which time the log thread will run.
1740 if (!wait)
1741 break;
1743 /* Display progress update in log view. */
1744 show_log_view(view);
1745 update_panels();
1746 doupdate();
1748 /* Wait right here while next commit is being loaded. */
1749 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1750 if (errcode)
1751 return got_error_set_errno(errcode,
1752 "pthread_cond_wait");
1754 /* Display progress update in log view. */
1755 show_log_view(view);
1756 update_panels();
1757 doupdate();
1760 return NULL;
1763 static const struct got_error *
1764 log_scroll_down(struct tog_view *view, int maxscroll)
1766 struct tog_log_view_state *s = &view->state.log;
1767 const struct got_error *err = NULL;
1768 struct commit_queue_entry *pentry;
1769 int nscrolled = 0, ncommits_needed;
1771 if (s->last_displayed_entry == NULL)
1772 return NULL;
1774 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1775 if (s->commits.ncommits < ncommits_needed &&
1776 !s->thread_args.log_complete) {
1778 * Ask the log thread for required amount of commits.
1780 s->thread_args.commits_needed += maxscroll;
1781 err = trigger_log_thread(view, 1);
1782 if (err)
1783 return err;
1786 do {
1787 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1788 if (pentry == NULL)
1789 break;
1791 s->last_displayed_entry = pentry;
1793 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1794 if (pentry == NULL)
1795 break;
1796 s->first_displayed_entry = pentry;
1797 } while (++nscrolled < maxscroll);
1799 return err;
1802 static const struct got_error *
1803 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1804 struct got_commit_object *commit, struct got_object_id *commit_id,
1805 struct tog_view *log_view, struct got_repository *repo)
1807 const struct got_error *err;
1808 struct got_object_qid *parent_id;
1809 struct tog_view *diff_view;
1811 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1812 if (diff_view == NULL)
1813 return got_error_from_errno("view_open");
1815 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1816 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1817 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1818 if (err == NULL)
1819 *new_view = diff_view;
1820 return err;
1823 static const struct got_error *
1824 tree_view_visit_subtree(struct tog_tree_view_state *s,
1825 struct got_tree_object *subtree)
1827 struct tog_parent_tree *parent;
1829 parent = calloc(1, sizeof(*parent));
1830 if (parent == NULL)
1831 return got_error_from_errno("calloc");
1833 parent->tree = s->tree;
1834 parent->first_displayed_entry = s->first_displayed_entry;
1835 parent->selected_entry = s->selected_entry;
1836 parent->selected = s->selected;
1837 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1838 s->tree = subtree;
1839 s->selected = 0;
1840 s->first_displayed_entry = NULL;
1841 return NULL;
1844 static const struct got_error *
1845 tree_view_walk_path(struct tog_tree_view_state *s,
1846 struct got_object_id *commit_id, const char *path)
1848 const struct got_error *err = NULL;
1849 struct got_tree_object *tree = NULL;
1850 const char *p;
1851 char *slash, *subpath = NULL;
1853 /* Walk the path and open corresponding tree objects. */
1854 p = path;
1855 while (*p) {
1856 struct got_tree_entry *te;
1857 struct got_object_id *tree_id;
1858 char *te_name;
1860 while (p[0] == '/')
1861 p++;
1863 /* Ensure the correct subtree entry is selected. */
1864 slash = strchr(p, '/');
1865 if (slash == NULL)
1866 te_name = strdup(p);
1867 else
1868 te_name = strndup(p, slash - p);
1869 if (te_name == NULL) {
1870 err = got_error_from_errno("strndup");
1871 break;
1873 te = got_object_tree_find_entry(s->tree, te_name);
1874 if (te == NULL) {
1875 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1876 free(te_name);
1877 break;
1879 free(te_name);
1880 s->first_displayed_entry = s->selected_entry = te;
1882 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1883 break; /* jump to this file's entry */
1885 slash = strchr(p, '/');
1886 if (slash)
1887 subpath = strndup(path, slash - path);
1888 else
1889 subpath = strdup(path);
1890 if (subpath == NULL) {
1891 err = got_error_from_errno("strdup");
1892 break;
1895 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1896 subpath);
1897 if (err)
1898 break;
1900 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1901 free(tree_id);
1902 if (err)
1903 break;
1905 err = tree_view_visit_subtree(s, tree);
1906 if (err) {
1907 got_object_tree_close(tree);
1908 break;
1910 if (slash == NULL)
1911 break;
1912 free(subpath);
1913 subpath = NULL;
1914 p = slash;
1917 free(subpath);
1918 return err;
1921 static const struct got_error *
1922 browse_commit_tree(struct tog_view **new_view, int begin_x,
1923 struct commit_queue_entry *entry, const char *path,
1924 const char *head_ref_name, struct got_repository *repo)
1926 const struct got_error *err = NULL;
1927 struct got_tree_object *tree;
1928 struct tog_tree_view_state *s;
1929 struct tog_view *tree_view;
1931 err = got_object_open_as_tree(&tree, repo,
1932 got_object_commit_get_tree_id(entry->commit));
1933 if (err)
1934 return err;
1936 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1937 if (tree_view == NULL)
1938 return got_error_from_errno("view_open");
1940 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1941 if (err) {
1942 got_object_tree_close(tree);
1943 return err;
1945 s = &tree_view->state.tree;
1947 *new_view = tree_view;
1949 if (got_path_is_root_dir(path))
1950 return NULL;
1952 return tree_view_walk_path(s, entry->id, path);
1955 static const struct got_error *
1956 block_signals_used_by_main_thread(void)
1958 sigset_t sigset;
1959 int errcode;
1961 if (sigemptyset(&sigset) == -1)
1962 return got_error_from_errno("sigemptyset");
1964 /* tog handles SIGWINCH and SIGCONT */
1965 if (sigaddset(&sigset, SIGWINCH) == -1)
1966 return got_error_from_errno("sigaddset");
1967 if (sigaddset(&sigset, SIGCONT) == -1)
1968 return got_error_from_errno("sigaddset");
1970 /* ncurses handles SIGTSTP */
1971 if (sigaddset(&sigset, SIGTSTP) == -1)
1972 return got_error_from_errno("sigaddset");
1974 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1975 if (errcode)
1976 return got_error_set_errno(errcode, "pthread_sigmask");
1978 return NULL;
1981 static void *
1982 log_thread(void *arg)
1984 const struct got_error *err = NULL;
1985 int errcode = 0;
1986 struct tog_log_thread_args *a = arg;
1987 int done = 0;
1989 err = block_signals_used_by_main_thread();
1990 if (err)
1991 return (void *)err;
1993 while (!done && !err && !tog_sigpipe_received) {
1994 err = queue_commits(a);
1995 if (err) {
1996 if (err->code != GOT_ERR_ITER_COMPLETED)
1997 return (void *)err;
1998 err = NULL;
1999 done = 1;
2000 } else if (a->commits_needed > 0)
2001 a->commits_needed--;
2003 errcode = pthread_mutex_lock(&tog_mutex);
2004 if (errcode) {
2005 err = got_error_set_errno(errcode,
2006 "pthread_mutex_lock");
2007 break;
2008 } else if (*a->quit)
2009 done = 1;
2010 else if (*a->first_displayed_entry == NULL) {
2011 *a->first_displayed_entry =
2012 TAILQ_FIRST(&a->commits->head);
2013 *a->selected_entry = *a->first_displayed_entry;
2016 errcode = pthread_cond_signal(&a->commit_loaded);
2017 if (errcode) {
2018 err = got_error_set_errno(errcode,
2019 "pthread_cond_signal");
2020 pthread_mutex_unlock(&tog_mutex);
2021 break;
2024 if (done)
2025 a->commits_needed = 0;
2026 else {
2027 if (a->commits_needed == 0) {
2028 errcode = pthread_cond_wait(&a->need_commits,
2029 &tog_mutex);
2030 if (errcode)
2031 err = got_error_set_errno(errcode,
2032 "pthread_cond_wait");
2033 if (*a->quit)
2034 done = 1;
2038 errcode = pthread_mutex_unlock(&tog_mutex);
2039 if (errcode && err == NULL)
2040 err = got_error_set_errno(errcode,
2041 "pthread_mutex_unlock");
2043 a->log_complete = 1;
2044 return (void *)err;
2047 static const struct got_error *
2048 stop_log_thread(struct tog_log_view_state *s)
2050 const struct got_error *err = NULL;
2051 int errcode;
2053 if (s->thread) {
2054 s->quit = 1;
2055 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2056 if (errcode)
2057 return got_error_set_errno(errcode,
2058 "pthread_cond_signal");
2059 errcode = pthread_mutex_unlock(&tog_mutex);
2060 if (errcode)
2061 return got_error_set_errno(errcode,
2062 "pthread_mutex_unlock");
2063 errcode = pthread_join(s->thread, (void **)&err);
2064 if (errcode)
2065 return got_error_set_errno(errcode, "pthread_join");
2066 errcode = pthread_mutex_lock(&tog_mutex);
2067 if (errcode)
2068 return got_error_set_errno(errcode,
2069 "pthread_mutex_lock");
2070 s->thread = NULL;
2073 if (s->thread_args.repo) {
2074 got_repo_close(s->thread_args.repo);
2075 s->thread_args.repo = NULL;
2078 if (s->thread_args.graph) {
2079 got_commit_graph_close(s->thread_args.graph);
2080 s->thread_args.graph = NULL;
2083 return err;
2086 static const struct got_error *
2087 close_log_view(struct tog_view *view)
2089 const struct got_error *err = NULL;
2090 struct tog_log_view_state *s = &view->state.log;
2091 int errcode;
2093 err = stop_log_thread(s);
2095 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2096 if (errcode && err == NULL)
2097 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2099 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2100 if (errcode && err == NULL)
2101 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2103 free_commits(&s->commits);
2104 free(s->in_repo_path);
2105 s->in_repo_path = NULL;
2106 free(s->start_id);
2107 s->start_id = NULL;
2108 got_ref_list_free(&s->refs);
2109 return err;
2112 static const struct got_error *
2113 search_start_log_view(struct tog_view *view)
2115 struct tog_log_view_state *s = &view->state.log;
2117 s->matched_entry = NULL;
2118 s->search_entry = NULL;
2119 return NULL;
2122 static const struct got_error *
2123 search_next_log_view(struct tog_view *view)
2125 const struct got_error *err = NULL;
2126 struct tog_log_view_state *s = &view->state.log;
2127 struct commit_queue_entry *entry;
2129 /* Display progress update in log view. */
2130 show_log_view(view);
2131 update_panels();
2132 doupdate();
2134 if (s->search_entry) {
2135 int errcode, ch;
2136 errcode = pthread_mutex_unlock(&tog_mutex);
2137 if (errcode)
2138 return got_error_set_errno(errcode,
2139 "pthread_mutex_unlock");
2140 ch = wgetch(view->window);
2141 errcode = pthread_mutex_lock(&tog_mutex);
2142 if (errcode)
2143 return got_error_set_errno(errcode,
2144 "pthread_mutex_lock");
2145 if (ch == KEY_BACKSPACE) {
2146 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2147 return NULL;
2149 if (view->searching == TOG_SEARCH_FORWARD)
2150 entry = TAILQ_NEXT(s->search_entry, entry);
2151 else
2152 entry = TAILQ_PREV(s->search_entry,
2153 commit_queue_head, entry);
2154 } else if (s->matched_entry) {
2155 if (view->searching == TOG_SEARCH_FORWARD)
2156 entry = TAILQ_NEXT(s->matched_entry, entry);
2157 else
2158 entry = TAILQ_PREV(s->matched_entry,
2159 commit_queue_head, entry);
2160 } else {
2161 if (view->searching == TOG_SEARCH_FORWARD)
2162 entry = TAILQ_FIRST(&s->commits.head);
2163 else
2164 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2167 while (1) {
2168 int have_match = 0;
2170 if (entry == NULL) {
2171 if (s->thread_args.log_complete ||
2172 view->searching == TOG_SEARCH_BACKWARD) {
2173 view->search_next_done =
2174 (s->matched_entry == NULL ?
2175 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2176 s->search_entry = NULL;
2177 return NULL;
2180 * Poke the log thread for more commits and return,
2181 * allowing the main loop to make progress. Search
2182 * will resume at s->search_entry once we come back.
2184 s->thread_args.commits_needed++;
2185 return trigger_log_thread(view, 0);
2188 err = match_commit(&have_match, entry->id, entry->commit,
2189 &view->regex);
2190 if (err)
2191 break;
2192 if (have_match) {
2193 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2194 s->matched_entry = entry;
2195 break;
2198 s->search_entry = entry;
2199 if (view->searching == TOG_SEARCH_FORWARD)
2200 entry = TAILQ_NEXT(entry, entry);
2201 else
2202 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2205 if (s->matched_entry) {
2206 int cur = s->selected_entry->idx;
2207 while (cur < s->matched_entry->idx) {
2208 err = input_log_view(NULL, view, KEY_DOWN);
2209 if (err)
2210 return err;
2211 cur++;
2213 while (cur > s->matched_entry->idx) {
2214 err = input_log_view(NULL, view, KEY_UP);
2215 if (err)
2216 return err;
2217 cur--;
2221 s->search_entry = NULL;
2223 return NULL;
2226 static const struct got_error *
2227 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2228 struct got_repository *repo, const char *head_ref_name,
2229 const char *in_repo_path, int log_branches)
2231 const struct got_error *err = NULL;
2232 struct tog_log_view_state *s = &view->state.log;
2233 struct got_repository *thread_repo = NULL;
2234 struct got_commit_graph *thread_graph = NULL;
2235 int errcode;
2237 SIMPLEQ_INIT(&s->refs);
2239 if (in_repo_path != s->in_repo_path) {
2240 free(s->in_repo_path);
2241 s->in_repo_path = strdup(in_repo_path);
2242 if (s->in_repo_path == NULL)
2243 return got_error_from_errno("strdup");
2246 /* The commit queue only contains commits being displayed. */
2247 TAILQ_INIT(&s->commits.head);
2248 s->commits.ncommits = 0;
2250 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2251 if (err)
2252 goto done;
2254 s->repo = repo;
2255 s->head_ref_name = head_ref_name;
2256 s->start_id = got_object_id_dup(start_id);
2257 if (s->start_id == NULL) {
2258 err = got_error_from_errno("got_object_id_dup");
2259 goto done;
2261 s->log_branches = log_branches;
2263 SIMPLEQ_INIT(&s->colors);
2264 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2265 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2266 get_color_value("TOG_COLOR_COMMIT"));
2267 if (err)
2268 goto done;
2269 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2270 get_color_value("TOG_COLOR_AUTHOR"));
2271 if (err) {
2272 free_colors(&s->colors);
2273 goto done;
2275 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2276 get_color_value("TOG_COLOR_DATE"));
2277 if (err) {
2278 free_colors(&s->colors);
2279 goto done;
2283 view->show = show_log_view;
2284 view->input = input_log_view;
2285 view->close = close_log_view;
2286 view->search_start = search_start_log_view;
2287 view->search_next = search_next_log_view;
2289 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2290 if (err)
2291 goto done;
2292 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2293 !s->log_branches);
2294 if (err)
2295 goto done;
2296 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2297 s->repo, NULL, NULL);
2298 if (err)
2299 goto done;
2301 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2302 if (errcode) {
2303 err = got_error_set_errno(errcode, "pthread_cond_init");
2304 goto done;
2306 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2307 if (errcode) {
2308 err = got_error_set_errno(errcode, "pthread_cond_init");
2309 goto done;
2312 s->thread_args.commits_needed = view->nlines;
2313 s->thread_args.graph = thread_graph;
2314 s->thread_args.commits = &s->commits;
2315 s->thread_args.in_repo_path = s->in_repo_path;
2316 s->thread_args.start_id = s->start_id;
2317 s->thread_args.repo = thread_repo;
2318 s->thread_args.log_complete = 0;
2319 s->thread_args.quit = &s->quit;
2320 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2321 s->thread_args.selected_entry = &s->selected_entry;
2322 s->thread_args.searching = &view->searching;
2323 s->thread_args.search_next_done = &view->search_next_done;
2324 s->thread_args.regex = &view->regex;
2325 done:
2326 if (err)
2327 close_log_view(view);
2328 return err;
2331 static const struct got_error *
2332 show_log_view(struct tog_view *view)
2334 const struct got_error *err;
2335 struct tog_log_view_state *s = &view->state.log;
2337 if (s->thread == NULL) {
2338 int errcode = pthread_create(&s->thread, NULL, log_thread,
2339 &s->thread_args);
2340 if (errcode)
2341 return got_error_set_errno(errcode, "pthread_create");
2342 if (s->thread_args.commits_needed > 0) {
2343 err = trigger_log_thread(view, 1);
2344 if (err)
2345 return err;
2349 return draw_commits(view);
2352 static const struct got_error *
2353 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2355 const struct got_error *err = NULL;
2356 struct tog_log_view_state *s = &view->state.log;
2357 struct tog_view *diff_view = NULL, *tree_view = NULL;
2358 struct tog_view *ref_view = NULL;
2359 int begin_x = 0;
2361 switch (ch) {
2362 case 'q':
2363 s->quit = 1;
2364 break;
2365 case 'k':
2366 case KEY_UP:
2367 case '<':
2368 case ',':
2369 if (s->first_displayed_entry == NULL)
2370 break;
2371 if (s->selected > 0)
2372 s->selected--;
2373 else
2374 log_scroll_up(s, 1);
2375 select_commit(s);
2376 break;
2377 case KEY_PPAGE:
2378 case CTRL('b'):
2379 if (s->first_displayed_entry == NULL)
2380 break;
2381 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2382 s->selected = 0;
2383 else
2384 log_scroll_up(s, view->nlines - 1);
2385 select_commit(s);
2386 break;
2387 case 'j':
2388 case KEY_DOWN:
2389 case '>':
2390 case '.':
2391 if (s->first_displayed_entry == NULL)
2392 break;
2393 if (s->selected < MIN(view->nlines - 2,
2394 s->commits.ncommits - 1))
2395 s->selected++;
2396 else {
2397 err = log_scroll_down(view, 1);
2398 if (err)
2399 break;
2401 select_commit(s);
2402 break;
2403 case KEY_NPAGE:
2404 case CTRL('f'): {
2405 struct commit_queue_entry *first;
2406 first = s->first_displayed_entry;
2407 if (first == NULL)
2408 break;
2409 err = log_scroll_down(view, view->nlines - 1);
2410 if (err)
2411 break;
2412 if (first == s->first_displayed_entry &&
2413 s->selected < MIN(view->nlines - 2,
2414 s->commits.ncommits - 1)) {
2415 /* can't scroll further down */
2416 s->selected = MIN(view->nlines - 2,
2417 s->commits.ncommits - 1);
2419 select_commit(s);
2420 break;
2422 case KEY_RESIZE:
2423 if (s->selected > view->nlines - 2)
2424 s->selected = view->nlines - 2;
2425 if (s->selected > s->commits.ncommits - 1)
2426 s->selected = s->commits.ncommits - 1;
2427 select_commit(s);
2428 if (s->commits.ncommits < view->nlines - 1 &&
2429 !s->thread_args.log_complete) {
2430 s->thread_args.commits_needed += (view->nlines - 1) -
2431 s->commits.ncommits;
2432 err = trigger_log_thread(view, 1);
2434 break;
2435 case KEY_ENTER:
2436 case ' ':
2437 case '\r':
2438 if (s->selected_entry == NULL)
2439 break;
2440 if (view_is_parent_view(view))
2441 begin_x = view_split_begin_x(view->begin_x);
2442 err = open_diff_view_for_commit(&diff_view, begin_x,
2443 s->selected_entry->commit, s->selected_entry->id,
2444 view, s->repo);
2445 if (err)
2446 break;
2447 view->focussed = 0;
2448 diff_view->focussed = 1;
2449 if (view_is_parent_view(view)) {
2450 err = view_close_child(view);
2451 if (err)
2452 return err;
2453 view_set_child(view, diff_view);
2454 view->focus_child = 1;
2455 } else
2456 *new_view = diff_view;
2457 break;
2458 case 't':
2459 if (s->selected_entry == NULL)
2460 break;
2461 if (view_is_parent_view(view))
2462 begin_x = view_split_begin_x(view->begin_x);
2463 err = browse_commit_tree(&tree_view, begin_x,
2464 s->selected_entry, s->in_repo_path, s->head_ref_name,
2465 s->repo);
2466 if (err)
2467 break;
2468 view->focussed = 0;
2469 tree_view->focussed = 1;
2470 if (view_is_parent_view(view)) {
2471 err = view_close_child(view);
2472 if (err)
2473 return err;
2474 view_set_child(view, tree_view);
2475 view->focus_child = 1;
2476 } else
2477 *new_view = tree_view;
2478 break;
2479 case KEY_BACKSPACE:
2480 case CTRL('l'):
2481 case 'B':
2482 if (ch == KEY_BACKSPACE &&
2483 got_path_is_root_dir(s->in_repo_path))
2484 break;
2485 err = stop_log_thread(s);
2486 if (err)
2487 return err;
2488 if (ch == KEY_BACKSPACE) {
2489 char *parent_path;
2490 err = got_path_dirname(&parent_path, s->in_repo_path);
2491 if (err)
2492 return err;
2493 free(s->in_repo_path);
2494 s->in_repo_path = parent_path;
2495 s->thread_args.in_repo_path = s->in_repo_path;
2496 } else if (ch == CTRL('l')) {
2497 struct got_object_id *start_id;
2498 err = got_repo_match_object_id(&start_id, NULL,
2499 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2500 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2501 if (err)
2502 return err;
2503 free(s->start_id);
2504 s->start_id = start_id;
2505 s->thread_args.start_id = s->start_id;
2506 } else /* 'B' */
2507 s->log_branches = !s->log_branches;
2509 err = got_repo_open(&s->thread_args.repo,
2510 got_repo_get_path(s->repo), NULL);
2511 if (err)
2512 return err;
2513 err = got_commit_graph_open(&s->thread_args.graph,
2514 s->in_repo_path, !s->log_branches);
2515 if (err)
2516 return err;
2517 err = got_commit_graph_iter_start(s->thread_args.graph,
2518 s->start_id, s->repo, NULL, NULL);
2519 if (err)
2520 return err;
2521 free_commits(&s->commits);
2522 s->first_displayed_entry = NULL;
2523 s->last_displayed_entry = NULL;
2524 s->selected_entry = NULL;
2525 s->selected = 0;
2526 s->thread_args.log_complete = 0;
2527 s->quit = 0;
2528 s->thread_args.commits_needed = view->nlines;
2529 break;
2530 case 'r':
2531 if (view_is_parent_view(view))
2532 begin_x = view_split_begin_x(view->begin_x);
2533 ref_view = view_open(view->nlines, view->ncols,
2534 view->begin_y, begin_x, TOG_VIEW_REF);
2535 if (ref_view == NULL)
2536 return got_error_from_errno("view_open");
2537 err = open_ref_view(ref_view, s->repo);
2538 if (err) {
2539 view_close(ref_view);
2540 return err;
2542 view->focussed = 0;
2543 ref_view->focussed = 1;
2544 if (view_is_parent_view(view)) {
2545 err = view_close_child(view);
2546 if (err)
2547 return err;
2548 view_set_child(view, ref_view);
2549 view->focus_child = 1;
2550 } else
2551 *new_view = ref_view;
2552 break;
2553 default:
2554 break;
2557 return err;
2560 static const struct got_error *
2561 apply_unveil(const char *repo_path, const char *worktree_path)
2563 const struct got_error *error;
2565 #ifdef PROFILE
2566 if (unveil("gmon.out", "rwc") != 0)
2567 return got_error_from_errno2("unveil", "gmon.out");
2568 #endif
2569 if (repo_path && unveil(repo_path, "r") != 0)
2570 return got_error_from_errno2("unveil", repo_path);
2572 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2573 return got_error_from_errno2("unveil", worktree_path);
2575 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2576 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2578 error = got_privsep_unveil_exec_helpers();
2579 if (error != NULL)
2580 return error;
2582 if (unveil(NULL, NULL) != 0)
2583 return got_error_from_errno("unveil");
2585 return NULL;
2588 static void
2589 init_curses(void)
2591 initscr();
2592 cbreak();
2593 halfdelay(1); /* Do fast refresh while initial view is loading. */
2594 noecho();
2595 nonl();
2596 intrflush(stdscr, FALSE);
2597 keypad(stdscr, TRUE);
2598 curs_set(0);
2599 if (getenv("TOG_COLORS") != NULL) {
2600 start_color();
2601 use_default_colors();
2603 signal(SIGWINCH, tog_sigwinch);
2604 signal(SIGPIPE, tog_sigpipe);
2605 signal(SIGCONT, tog_sigcont);
2608 static const struct got_error *
2609 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2610 struct got_repository *repo, struct got_worktree *worktree)
2612 const struct got_error *err = NULL;
2614 if (argc == 0) {
2615 *in_repo_path = strdup("/");
2616 if (*in_repo_path == NULL)
2617 return got_error_from_errno("strdup");
2618 return NULL;
2621 if (worktree) {
2622 const char *prefix = got_worktree_get_path_prefix(worktree);
2623 char *p;
2625 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2626 if (err)
2627 return err;
2628 if (asprintf(in_repo_path, "%s%s%s", prefix,
2629 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2630 p) == -1) {
2631 err = got_error_from_errno("asprintf");
2632 *in_repo_path = NULL;
2634 free(p);
2635 } else
2636 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2638 return err;
2641 static const struct got_error *
2642 cmd_log(int argc, char *argv[])
2644 const struct got_error *error;
2645 struct got_repository *repo = NULL;
2646 struct got_worktree *worktree = NULL;
2647 struct got_object_id *start_id = NULL;
2648 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2649 char *start_commit = NULL, *label = NULL;
2650 struct got_reference *ref = NULL;
2651 const char *head_ref_name = NULL;
2652 int ch, log_branches = 0;
2653 struct tog_view *view;
2655 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2656 switch (ch) {
2657 case 'b':
2658 log_branches = 1;
2659 break;
2660 case 'c':
2661 start_commit = optarg;
2662 break;
2663 case 'r':
2664 repo_path = realpath(optarg, NULL);
2665 if (repo_path == NULL)
2666 return got_error_from_errno2("realpath",
2667 optarg);
2668 break;
2669 default:
2670 usage_log();
2671 /* NOTREACHED */
2675 argc -= optind;
2676 argv += optind;
2678 if (argc > 1)
2679 usage_log();
2681 cwd = getcwd(NULL, 0);
2682 if (cwd == NULL)
2683 return got_error_from_errno("getcwd");
2685 error = got_worktree_open(&worktree, cwd);
2686 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2687 goto done;
2689 if (repo_path == NULL) {
2690 if (worktree)
2691 repo_path =
2692 strdup(got_worktree_get_repo_path(worktree));
2693 else
2694 repo_path = strdup(cwd);
2696 if (repo_path == NULL) {
2697 error = got_error_from_errno("strdup");
2698 goto done;
2701 error = got_repo_open(&repo, repo_path, NULL);
2702 if (error != NULL)
2703 goto done;
2705 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2706 repo, worktree);
2707 if (error)
2708 goto done;
2710 init_curses();
2712 error = apply_unveil(got_repo_get_path(repo),
2713 worktree ? got_worktree_get_root_path(worktree) : NULL);
2714 if (error)
2715 goto done;
2717 if (start_commit == NULL) {
2718 error = got_repo_match_object_id(&start_id, &label,
2719 worktree ? got_worktree_get_head_ref_name(worktree) :
2720 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2721 if (error)
2722 goto done;
2723 head_ref_name = label;
2724 } else {
2725 error = got_ref_open(&ref, repo, start_commit, 0);
2726 if (error == NULL)
2727 head_ref_name = got_ref_get_name(ref);
2728 else if (error->code != GOT_ERR_NOT_REF)
2729 goto done;
2730 error = got_repo_match_object_id(&start_id, NULL,
2731 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2732 if (error)
2733 goto done;
2736 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2737 if (view == NULL) {
2738 error = got_error_from_errno("view_open");
2739 goto done;
2741 error = open_log_view(view, start_id, repo, head_ref_name,
2742 in_repo_path, log_branches);
2743 if (error)
2744 goto done;
2745 if (worktree) {
2746 /* Release work tree lock. */
2747 got_worktree_close(worktree);
2748 worktree = NULL;
2750 error = view_loop(view);
2751 done:
2752 free(in_repo_path);
2753 free(repo_path);
2754 free(cwd);
2755 free(start_id);
2756 free(label);
2757 if (ref)
2758 got_ref_close(ref);
2759 if (repo)
2760 got_repo_close(repo);
2761 if (worktree)
2762 got_worktree_close(worktree);
2763 return error;
2766 __dead static void
2767 usage_diff(void)
2769 endwin();
2770 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2771 "[-w] object1 object2\n", getprogname());
2772 exit(1);
2775 static char *
2776 parse_next_line(FILE *f, size_t *len)
2778 char *line;
2779 size_t linelen;
2780 size_t lineno;
2781 const char delim[3] = { '\0', '\0', '\0'};
2783 line = fparseln(f, &linelen, &lineno, delim, 0);
2784 if (len)
2785 *len = linelen;
2786 return line;
2789 static int
2790 match_line(const char *line, regex_t *regex, size_t nmatch,
2791 regmatch_t *regmatch)
2793 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2796 struct tog_color *
2797 match_color(struct tog_colors *colors, const char *line)
2799 struct tog_color *tc = NULL;
2801 SIMPLEQ_FOREACH(tc, colors, entry) {
2802 if (match_line(line, &tc->regex, 0, NULL))
2803 return tc;
2806 return NULL;
2809 static const struct got_error *
2810 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2811 WINDOW *window, regmatch_t *regmatch)
2813 const struct got_error *err = NULL;
2814 wchar_t *wline;
2815 int width;
2816 char *s;
2818 *wtotal = 0;
2820 s = strndup(line, regmatch->rm_so);
2821 if (s == NULL)
2822 return got_error_from_errno("strndup");
2824 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2825 if (err) {
2826 free(s);
2827 return err;
2829 waddwstr(window, wline);
2830 free(wline);
2831 free(s);
2832 wlimit -= width;
2833 *wtotal += width;
2835 if (wlimit > 0) {
2836 s = strndup(line + regmatch->rm_so,
2837 regmatch->rm_eo - regmatch->rm_so);
2838 if (s == NULL) {
2839 err = got_error_from_errno("strndup");
2840 free(s);
2841 return err;
2843 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2844 if (err) {
2845 free(s);
2846 return err;
2848 wattr_on(window, A_STANDOUT, NULL);
2849 waddwstr(window, wline);
2850 wattr_off(window, A_STANDOUT, NULL);
2851 free(wline);
2852 free(s);
2853 wlimit -= width;
2854 *wtotal += width;
2857 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2858 err = format_line(&wline, &width,
2859 line + regmatch->rm_eo, wlimit, col_tab_align);
2860 if (err)
2861 return err;
2862 waddwstr(window, wline);
2863 free(wline);
2864 *wtotal += width;
2867 return NULL;
2870 static const struct got_error *
2871 draw_file(struct tog_view *view, const char *header)
2873 struct tog_diff_view_state *s = &view->state.diff;
2874 regmatch_t *regmatch = &view->regmatch;
2875 const struct got_error *err;
2876 int nprinted = 0;
2877 char *line;
2878 struct tog_color *tc;
2879 size_t len;
2880 wchar_t *wline;
2881 int width;
2882 int max_lines = view->nlines;
2883 int nlines = s->nlines;
2884 off_t line_offset;
2886 line_offset = s->line_offsets[s->first_displayed_line - 1];
2887 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2888 return got_error_from_errno("fseek");
2890 werase(view->window);
2892 if (header) {
2893 if (asprintf(&line, "[%d/%d] %s",
2894 s->first_displayed_line - 1 + s->selected_line, nlines,
2895 header) == -1)
2896 return got_error_from_errno("asprintf");
2897 err = format_line(&wline, &width, line, view->ncols, 0);
2898 free(line);
2899 if (err)
2900 return err;
2902 if (view_needs_focus_indication(view))
2903 wstandout(view->window);
2904 waddwstr(view->window, wline);
2905 free(wline);
2906 wline = NULL;
2907 if (view_needs_focus_indication(view))
2908 wstandend(view->window);
2909 if (width <= view->ncols - 1)
2910 waddch(view->window, '\n');
2912 if (max_lines <= 1)
2913 return NULL;
2914 max_lines--;
2917 s->eof = 0;
2918 while (max_lines > 0 && nprinted < max_lines) {
2919 line = parse_next_line(s->f, &len);
2920 if (line == NULL) {
2921 s->eof = 1;
2922 break;
2925 tc = match_color(&s->colors, line);
2926 if (tc)
2927 wattr_on(view->window,
2928 COLOR_PAIR(tc->colorpair), NULL);
2929 if (s->first_displayed_line + nprinted == s->matched_line &&
2930 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2931 err = add_matched_line(&width, line, view->ncols, 0,
2932 view->window, regmatch);
2933 if (err) {
2934 free(line);
2935 return err;
2937 } else {
2938 err = format_line(&wline, &width, line, view->ncols, 0);
2939 if (err) {
2940 free(line);
2941 return err;
2943 waddwstr(view->window, wline);
2944 free(wline);
2945 wline = NULL;
2947 if (tc)
2948 wattr_off(view->window,
2949 COLOR_PAIR(tc->colorpair), NULL);
2950 if (width <= view->ncols - 1)
2951 waddch(view->window, '\n');
2952 nprinted++;
2953 free(line);
2955 if (nprinted >= 1)
2956 s->last_displayed_line = s->first_displayed_line +
2957 (nprinted - 1);
2958 else
2959 s->last_displayed_line = s->first_displayed_line;
2961 view_vborder(view);
2963 if (s->eof) {
2964 while (nprinted < view->nlines) {
2965 waddch(view->window, '\n');
2966 nprinted++;
2969 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2970 if (err) {
2971 return err;
2974 wstandout(view->window);
2975 waddwstr(view->window, wline);
2976 free(wline);
2977 wline = NULL;
2978 wstandend(view->window);
2981 return NULL;
2984 static char *
2985 get_datestr(time_t *time, char *datebuf)
2987 struct tm mytm, *tm;
2988 char *p, *s;
2990 tm = gmtime_r(time, &mytm);
2991 if (tm == NULL)
2992 return NULL;
2993 s = asctime_r(tm, datebuf);
2994 if (s == NULL)
2995 return NULL;
2996 p = strchr(s, '\n');
2997 if (p)
2998 *p = '\0';
2999 return s;
3002 static const struct got_error *
3003 get_changed_paths(struct got_pathlist_head *paths,
3004 struct got_commit_object *commit, struct got_repository *repo)
3006 const struct got_error *err = NULL;
3007 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3008 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3009 struct got_object_qid *qid;
3011 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3012 if (qid != NULL) {
3013 struct got_commit_object *pcommit;
3014 err = got_object_open_as_commit(&pcommit, repo,
3015 qid->id);
3016 if (err)
3017 return err;
3019 tree_id1 = got_object_commit_get_tree_id(pcommit);
3020 got_object_commit_close(pcommit);
3024 if (tree_id1) {
3025 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3026 if (err)
3027 goto done;
3030 tree_id2 = got_object_commit_get_tree_id(commit);
3031 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3032 if (err)
3033 goto done;
3035 err = got_diff_tree(tree1, tree2, "", "", repo,
3036 got_diff_tree_collect_changed_paths, paths, 0);
3037 done:
3038 if (tree1)
3039 got_object_tree_close(tree1);
3040 if (tree2)
3041 got_object_tree_close(tree2);
3042 return err;
3045 static const struct got_error *
3046 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3048 off_t *p;
3050 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3051 if (p == NULL)
3052 return got_error_from_errno("reallocarray");
3053 *line_offsets = p;
3054 (*line_offsets)[*nlines] = off;
3055 (*nlines)++;
3056 return NULL;
3059 static const struct got_error *
3060 write_commit_info(off_t **line_offsets, size_t *nlines,
3061 struct got_object_id *commit_id, struct got_reflist_head *refs,
3062 struct got_repository *repo, FILE *outfile)
3064 const struct got_error *err = NULL;
3065 char datebuf[26], *datestr;
3066 struct got_commit_object *commit;
3067 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3068 time_t committer_time;
3069 const char *author, *committer;
3070 char *refs_str = NULL;
3071 struct got_pathlist_head changed_paths;
3072 struct got_pathlist_entry *pe;
3073 off_t outoff = 0;
3074 int n;
3076 TAILQ_INIT(&changed_paths);
3078 if (refs) {
3079 err = build_refs_str(&refs_str, refs, commit_id, repo);
3080 if (err)
3081 return err;
3084 err = got_object_open_as_commit(&commit, repo, commit_id);
3085 if (err)
3086 return err;
3088 err = got_object_id_str(&id_str, commit_id);
3089 if (err) {
3090 err = got_error_from_errno("got_object_id_str");
3091 goto done;
3094 err = add_line_offset(line_offsets, nlines, 0);
3095 if (err)
3096 goto done;
3098 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3099 refs_str ? refs_str : "", refs_str ? ")" : "");
3100 if (n < 0) {
3101 err = got_error_from_errno("fprintf");
3102 goto done;
3104 outoff += n;
3105 err = add_line_offset(line_offsets, nlines, outoff);
3106 if (err)
3107 goto done;
3109 n = fprintf(outfile, "from: %s\n",
3110 got_object_commit_get_author(commit));
3111 if (n < 0) {
3112 err = got_error_from_errno("fprintf");
3113 goto done;
3115 outoff += n;
3116 err = add_line_offset(line_offsets, nlines, outoff);
3117 if (err)
3118 goto done;
3120 committer_time = got_object_commit_get_committer_time(commit);
3121 datestr = get_datestr(&committer_time, datebuf);
3122 if (datestr) {
3123 n = fprintf(outfile, "date: %s UTC\n", datestr);
3124 if (n < 0) {
3125 err = got_error_from_errno("fprintf");
3126 goto done;
3128 outoff += n;
3129 err = add_line_offset(line_offsets, nlines, outoff);
3130 if (err)
3131 goto done;
3133 author = got_object_commit_get_author(commit);
3134 committer = got_object_commit_get_committer(commit);
3135 if (strcmp(author, committer) != 0) {
3136 n = fprintf(outfile, "via: %s\n", committer);
3137 if (n < 0) {
3138 err = got_error_from_errno("fprintf");
3139 goto done;
3141 outoff += n;
3142 err = add_line_offset(line_offsets, nlines, outoff);
3143 if (err)
3144 goto done;
3146 err = got_object_commit_get_logmsg(&logmsg, commit);
3147 if (err)
3148 goto done;
3149 s = logmsg;
3150 while ((line = strsep(&s, "\n")) != NULL) {
3151 n = fprintf(outfile, "%s\n", line);
3152 if (n < 0) {
3153 err = got_error_from_errno("fprintf");
3154 goto done;
3156 outoff += n;
3157 err = add_line_offset(line_offsets, nlines, outoff);
3158 if (err)
3159 goto done;
3162 err = get_changed_paths(&changed_paths, commit, repo);
3163 if (err)
3164 goto done;
3165 TAILQ_FOREACH(pe, &changed_paths, entry) {
3166 struct got_diff_changed_path *cp = pe->data;
3167 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3168 if (n < 0) {
3169 err = got_error_from_errno("fprintf");
3170 goto done;
3172 outoff += n;
3173 err = add_line_offset(line_offsets, nlines, outoff);
3174 if (err)
3175 goto done;
3176 free((char *)pe->path);
3177 free(pe->data);
3180 fputc('\n', outfile);
3181 outoff++;
3182 err = add_line_offset(line_offsets, nlines, outoff);
3183 done:
3184 got_pathlist_free(&changed_paths);
3185 free(id_str);
3186 free(logmsg);
3187 free(refs_str);
3188 got_object_commit_close(commit);
3189 if (err) {
3190 free(*line_offsets);
3191 *line_offsets = NULL;
3192 *nlines = 0;
3194 return err;
3197 static const struct got_error *
3198 create_diff(struct tog_diff_view_state *s)
3200 const struct got_error *err = NULL;
3201 FILE *f = NULL;
3202 int obj_type;
3204 free(s->line_offsets);
3205 s->line_offsets = malloc(sizeof(off_t));
3206 if (s->line_offsets == NULL)
3207 return got_error_from_errno("malloc");
3208 s->nlines = 0;
3210 f = got_opentemp();
3211 if (f == NULL) {
3212 err = got_error_from_errno("got_opentemp");
3213 goto done;
3215 if (s->f && fclose(s->f) != 0) {
3216 err = got_error_from_errno("fclose");
3217 goto done;
3219 s->f = f;
3221 if (s->id1)
3222 err = got_object_get_type(&obj_type, s->repo, s->id1);
3223 else
3224 err = got_object_get_type(&obj_type, s->repo, s->id2);
3225 if (err)
3226 goto done;
3228 switch (obj_type) {
3229 case GOT_OBJ_TYPE_BLOB:
3230 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3231 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3232 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3233 break;
3234 case GOT_OBJ_TYPE_TREE:
3235 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3236 s->id1, s->id2, "", "", s->diff_context,
3237 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3238 break;
3239 case GOT_OBJ_TYPE_COMMIT: {
3240 const struct got_object_id_queue *parent_ids;
3241 struct got_object_qid *pid;
3242 struct got_commit_object *commit2;
3244 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3245 if (err)
3246 goto done;
3247 /* Show commit info if we're diffing to a parent/root commit. */
3248 if (s->id1 == NULL) {
3249 err = write_commit_info(&s->line_offsets, &s->nlines,
3250 s->id2, &s->refs, s->repo, s->f);
3251 if (err)
3252 goto done;
3253 } else {
3254 parent_ids = got_object_commit_get_parent_ids(commit2);
3255 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3256 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3257 err = write_commit_info(
3258 &s->line_offsets, &s->nlines,
3259 s->id2, &s->refs, s->repo, s->f);
3260 if (err)
3261 goto done;
3262 break;
3266 got_object_commit_close(commit2);
3268 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3269 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3270 s->force_text_diff, s->repo, s->f);
3271 break;
3273 default:
3274 err = got_error(GOT_ERR_OBJ_TYPE);
3275 break;
3277 if (err)
3278 goto done;
3279 done:
3280 if (s->f && fflush(s->f) != 0 && err == NULL)
3281 err = got_error_from_errno("fflush");
3282 return err;
3285 static void
3286 diff_view_indicate_progress(struct tog_view *view)
3288 mvwaddstr(view->window, 0, 0, "diffing...");
3289 update_panels();
3290 doupdate();
3293 static const struct got_error *
3294 search_start_diff_view(struct tog_view *view)
3296 struct tog_diff_view_state *s = &view->state.diff;
3298 s->matched_line = 0;
3299 return NULL;
3302 static const struct got_error *
3303 search_next_diff_view(struct tog_view *view)
3305 struct tog_diff_view_state *s = &view->state.diff;
3306 int lineno;
3308 if (!view->searching) {
3309 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3310 return NULL;
3313 if (s->matched_line) {
3314 if (view->searching == TOG_SEARCH_FORWARD)
3315 lineno = s->matched_line + 1;
3316 else
3317 lineno = s->matched_line - 1;
3318 } else {
3319 if (view->searching == TOG_SEARCH_FORWARD)
3320 lineno = 1;
3321 else
3322 lineno = s->nlines;
3325 while (1) {
3326 char *line = NULL;
3327 off_t offset;
3328 size_t len;
3330 if (lineno <= 0 || lineno > s->nlines) {
3331 if (s->matched_line == 0) {
3332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3333 free(line);
3334 break;
3337 if (view->searching == TOG_SEARCH_FORWARD)
3338 lineno = 1;
3339 else
3340 lineno = s->nlines;
3343 offset = s->line_offsets[lineno - 1];
3344 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3345 free(line);
3346 return got_error_from_errno("fseeko");
3348 free(line);
3349 line = parse_next_line(s->f, &len);
3350 if (line &&
3351 match_line(line, &view->regex, 1, &view->regmatch)) {
3352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3353 s->matched_line = lineno;
3354 free(line);
3355 break;
3357 free(line);
3358 if (view->searching == TOG_SEARCH_FORWARD)
3359 lineno++;
3360 else
3361 lineno--;
3364 if (s->matched_line) {
3365 s->first_displayed_line = s->matched_line;
3366 s->selected_line = 1;
3369 return NULL;
3372 static const struct got_error *
3373 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3374 struct got_object_id *id2, const char *label1, const char *label2,
3375 int diff_context, int ignore_whitespace, int force_text_diff,
3376 struct tog_view *log_view, struct got_repository *repo)
3378 const struct got_error *err;
3379 struct tog_diff_view_state *s = &view->state.diff;
3381 SIMPLEQ_INIT(&s->refs);
3383 if (id1 != NULL && id2 != NULL) {
3384 int type1, type2;
3385 err = got_object_get_type(&type1, repo, id1);
3386 if (err)
3387 return err;
3388 err = got_object_get_type(&type2, repo, id2);
3389 if (err)
3390 return err;
3392 if (type1 != type2)
3393 return got_error(GOT_ERR_OBJ_TYPE);
3395 s->first_displayed_line = 1;
3396 s->last_displayed_line = view->nlines;
3397 s->selected_line = 1;
3398 s->repo = repo;
3399 s->id1 = id1;
3400 s->id2 = id2;
3401 s->label1 = label1;
3402 s->label2 = label2;
3404 if (id1) {
3405 s->id1 = got_object_id_dup(id1);
3406 if (s->id1 == NULL)
3407 return got_error_from_errno("got_object_id_dup");
3408 } else
3409 s->id1 = NULL;
3411 s->id2 = got_object_id_dup(id2);
3412 if (s->id2 == NULL) {
3413 free(s->id1);
3414 s->id1 = NULL;
3415 return got_error_from_errno("got_object_id_dup");
3417 s->f = NULL;
3418 s->first_displayed_line = 1;
3419 s->last_displayed_line = view->nlines;
3420 s->diff_context = diff_context;
3421 s->ignore_whitespace = ignore_whitespace;
3422 s->force_text_diff = force_text_diff;
3423 s->log_view = log_view;
3424 s->repo = repo;
3426 SIMPLEQ_INIT(&s->colors);
3427 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3428 err = add_color(&s->colors,
3429 "^-", TOG_COLOR_DIFF_MINUS,
3430 get_color_value("TOG_COLOR_DIFF_MINUS"));
3431 if (err)
3432 return err;
3433 err = add_color(&s->colors, "^\\+",
3434 TOG_COLOR_DIFF_PLUS,
3435 get_color_value("TOG_COLOR_DIFF_PLUS"));
3436 if (err) {
3437 free_colors(&s->colors);
3438 return err;
3440 err = add_color(&s->colors,
3441 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3442 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3443 if (err) {
3444 free_colors(&s->colors);
3445 return err;
3448 err = add_color(&s->colors,
3449 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3450 TOG_COLOR_DIFF_META,
3451 get_color_value("TOG_COLOR_DIFF_META"));
3452 if (err) {
3453 free_colors(&s->colors);
3454 return err;
3457 err = add_color(&s->colors,
3458 "^(from|via): ", TOG_COLOR_AUTHOR,
3459 get_color_value("TOG_COLOR_AUTHOR"));
3460 if (err) {
3461 free_colors(&s->colors);
3462 return err;
3465 err = add_color(&s->colors,
3466 "^date: ", TOG_COLOR_DATE,
3467 get_color_value("TOG_COLOR_DATE"));
3468 if (err) {
3469 free_colors(&s->colors);
3470 return err;
3474 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3475 if (err) {
3476 free(s->id1);
3477 s->id1 = NULL;
3478 free(s->id2);
3479 s->id2 = NULL;
3480 free_colors(&s->colors);
3481 return err;
3484 if (log_view && view_is_splitscreen(view))
3485 show_log_view(log_view); /* draw vborder */
3486 diff_view_indicate_progress(view);
3488 s->line_offsets = NULL;
3489 s->nlines = 0;
3490 err = create_diff(s);
3491 if (err) {
3492 free(s->id1);
3493 s->id1 = NULL;
3494 free(s->id2);
3495 s->id2 = NULL;
3496 free_colors(&s->colors);
3497 got_ref_list_free(&s->refs);
3498 return err;
3501 view->show = show_diff_view;
3502 view->input = input_diff_view;
3503 view->close = close_diff_view;
3504 view->search_start = search_start_diff_view;
3505 view->search_next = search_next_diff_view;
3507 return NULL;
3510 static const struct got_error *
3511 close_diff_view(struct tog_view *view)
3513 const struct got_error *err = NULL;
3514 struct tog_diff_view_state *s = &view->state.diff;
3516 free(s->id1);
3517 s->id1 = NULL;
3518 free(s->id2);
3519 s->id2 = NULL;
3520 if (s->f && fclose(s->f) == EOF)
3521 err = got_error_from_errno("fclose");
3522 free_colors(&s->colors);
3523 free(s->line_offsets);
3524 s->line_offsets = NULL;
3525 s->nlines = 0;
3526 got_ref_list_free(&s->refs);
3527 return err;
3530 static const struct got_error *
3531 show_diff_view(struct tog_view *view)
3533 const struct got_error *err;
3534 struct tog_diff_view_state *s = &view->state.diff;
3535 char *id_str1 = NULL, *id_str2, *header;
3536 const char *label1, *label2;
3538 if (s->id1) {
3539 err = got_object_id_str(&id_str1, s->id1);
3540 if (err)
3541 return err;
3542 label1 = s->label1 ? : id_str1;
3543 } else
3544 label1 = "/dev/null";
3546 err = got_object_id_str(&id_str2, s->id2);
3547 if (err)
3548 return err;
3549 label2 = s->label2 ? : id_str2;
3551 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3552 err = got_error_from_errno("asprintf");
3553 free(id_str1);
3554 free(id_str2);
3555 return err;
3557 free(id_str1);
3558 free(id_str2);
3560 return draw_file(view, header);
3563 static const struct got_error *
3564 set_selected_commit(struct tog_diff_view_state *s,
3565 struct commit_queue_entry *entry)
3567 const struct got_error *err;
3568 const struct got_object_id_queue *parent_ids;
3569 struct got_commit_object *selected_commit;
3570 struct got_object_qid *pid;
3572 free(s->id2);
3573 s->id2 = got_object_id_dup(entry->id);
3574 if (s->id2 == NULL)
3575 return got_error_from_errno("got_object_id_dup");
3577 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3578 if (err)
3579 return err;
3580 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3581 free(s->id1);
3582 pid = SIMPLEQ_FIRST(parent_ids);
3583 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3584 got_object_commit_close(selected_commit);
3585 return NULL;
3588 static const struct got_error *
3589 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3591 const struct got_error *err = NULL;
3592 struct tog_diff_view_state *s = &view->state.diff;
3593 struct tog_log_view_state *ls;
3594 struct commit_queue_entry *old_selected_entry;
3595 int i;
3597 switch (ch) {
3598 case 'a':
3599 case 'w':
3600 if (ch == 'a')
3601 s->force_text_diff = !s->force_text_diff;
3602 if (ch == 'w')
3603 s->ignore_whitespace = !s->ignore_whitespace;
3604 wclear(view->window);
3605 s->first_displayed_line = 1;
3606 s->last_displayed_line = view->nlines;
3607 diff_view_indicate_progress(view);
3608 err = create_diff(s);
3609 break;
3610 case 'k':
3611 case KEY_UP:
3612 if (s->first_displayed_line > 1)
3613 s->first_displayed_line--;
3614 break;
3615 case KEY_PPAGE:
3616 case CTRL('b'):
3617 if (s->first_displayed_line == 1)
3618 break;
3619 i = 0;
3620 while (i++ < view->nlines - 1 &&
3621 s->first_displayed_line > 1)
3622 s->first_displayed_line--;
3623 break;
3624 case 'j':
3625 case KEY_DOWN:
3626 if (!s->eof)
3627 s->first_displayed_line++;
3628 break;
3629 case KEY_NPAGE:
3630 case CTRL('f'):
3631 case ' ':
3632 if (s->eof)
3633 break;
3634 i = 0;
3635 while (!s->eof && i++ < view->nlines - 1) {
3636 char *line;
3637 line = parse_next_line(s->f, NULL);
3638 s->first_displayed_line++;
3639 if (line == NULL)
3640 break;
3642 break;
3643 case '[':
3644 if (s->diff_context > 0) {
3645 s->diff_context--;
3646 diff_view_indicate_progress(view);
3647 err = create_diff(s);
3648 if (s->first_displayed_line + view->nlines - 1 >
3649 s->nlines) {
3650 s->first_displayed_line = 1;
3651 s->last_displayed_line = view->nlines;
3654 break;
3655 case ']':
3656 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3657 s->diff_context++;
3658 diff_view_indicate_progress(view);
3659 err = create_diff(s);
3661 break;
3662 case '<':
3663 case ',':
3664 if (s->log_view == NULL)
3665 break;
3666 ls = &s->log_view->state.log;
3667 old_selected_entry = ls->selected_entry;
3669 err = input_log_view(NULL, s->log_view, KEY_UP);
3670 if (err)
3671 break;
3673 if (old_selected_entry == ls->selected_entry)
3674 break;
3676 err = set_selected_commit(s, ls->selected_entry);
3677 if (err)
3678 break;
3680 s->first_displayed_line = 1;
3681 s->last_displayed_line = view->nlines;
3683 diff_view_indicate_progress(view);
3684 err = create_diff(s);
3685 break;
3686 case '>':
3687 case '.':
3688 if (s->log_view == NULL)
3689 break;
3690 ls = &s->log_view->state.log;
3691 old_selected_entry = ls->selected_entry;
3693 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3694 if (err)
3695 break;
3697 if (old_selected_entry == ls->selected_entry)
3698 break;
3700 err = set_selected_commit(s, ls->selected_entry);
3701 if (err)
3702 break;
3704 s->first_displayed_line = 1;
3705 s->last_displayed_line = view->nlines;
3707 diff_view_indicate_progress(view);
3708 err = create_diff(s);
3709 break;
3710 default:
3711 break;
3714 return err;
3717 static const struct got_error *
3718 cmd_diff(int argc, char *argv[])
3720 const struct got_error *error = NULL;
3721 struct got_repository *repo = NULL;
3722 struct got_worktree *worktree = NULL;
3723 struct got_object_id *id1 = NULL, *id2 = NULL;
3724 char *repo_path = NULL, *cwd = NULL;
3725 char *id_str1 = NULL, *id_str2 = NULL;
3726 char *label1 = NULL, *label2 = NULL;
3727 int diff_context = 3, ignore_whitespace = 0;
3728 int ch, force_text_diff = 0;
3729 const char *errstr;
3730 struct tog_view *view;
3732 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3733 switch (ch) {
3734 case 'a':
3735 force_text_diff = 1;
3736 break;
3737 case 'C':
3738 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3739 &errstr);
3740 if (errstr != NULL)
3741 err(1, "-C option %s", errstr);
3742 break;
3743 case 'r':
3744 repo_path = realpath(optarg, NULL);
3745 if (repo_path == NULL)
3746 return got_error_from_errno2("realpath",
3747 optarg);
3748 got_path_strip_trailing_slashes(repo_path);
3749 break;
3750 case 'w':
3751 ignore_whitespace = 1;
3752 break;
3753 default:
3754 usage_diff();
3755 /* NOTREACHED */
3759 argc -= optind;
3760 argv += optind;
3762 if (argc == 0) {
3763 usage_diff(); /* TODO show local worktree changes */
3764 } else if (argc == 2) {
3765 id_str1 = argv[0];
3766 id_str2 = argv[1];
3767 } else
3768 usage_diff();
3770 cwd = getcwd(NULL, 0);
3771 if (cwd == NULL)
3772 return got_error_from_errno("getcwd");
3774 error = got_worktree_open(&worktree, cwd);
3775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3776 goto done;
3778 if (repo_path == NULL) {
3779 if (worktree)
3780 repo_path =
3781 strdup(got_worktree_get_repo_path(worktree));
3782 else
3783 repo_path = strdup(cwd);
3785 if (repo_path == NULL) {
3786 error = got_error_from_errno("strdup");
3787 goto done;
3790 error = got_repo_open(&repo, repo_path, NULL);
3791 if (error)
3792 goto done;
3794 init_curses();
3796 error = apply_unveil(got_repo_get_path(repo), NULL);
3797 if (error)
3798 goto done;
3800 error = got_repo_match_object_id(&id1, &label1, id_str1,
3801 GOT_OBJ_TYPE_ANY, 1, repo);
3802 if (error)
3803 goto done;
3805 error = got_repo_match_object_id(&id2, &label2, id_str2,
3806 GOT_OBJ_TYPE_ANY, 1, repo);
3807 if (error)
3808 goto done;
3810 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3811 if (view == NULL) {
3812 error = got_error_from_errno("view_open");
3813 goto done;
3815 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3816 ignore_whitespace, force_text_diff, NULL, repo);
3817 if (error)
3818 goto done;
3819 error = view_loop(view);
3820 done:
3821 free(label1);
3822 free(label2);
3823 free(repo_path);
3824 free(cwd);
3825 if (repo)
3826 got_repo_close(repo);
3827 if (worktree)
3828 got_worktree_close(worktree);
3829 return error;
3832 __dead static void
3833 usage_blame(void)
3835 endwin();
3836 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3837 getprogname());
3838 exit(1);
3841 struct tog_blame_line {
3842 int annotated;
3843 struct got_object_id *id;
3846 static const struct got_error *
3847 draw_blame(struct tog_view *view)
3849 struct tog_blame_view_state *s = &view->state.blame;
3850 struct tog_blame *blame = &s->blame;
3851 regmatch_t *regmatch = &view->regmatch;
3852 const struct got_error *err;
3853 int lineno = 0, nprinted = 0;
3854 char *line;
3855 size_t len;
3856 wchar_t *wline;
3857 int width;
3858 struct tog_blame_line *blame_line;
3859 struct got_object_id *prev_id = NULL;
3860 char *id_str;
3861 struct tog_color *tc;
3863 err = got_object_id_str(&id_str, s->blamed_commit->id);
3864 if (err)
3865 return err;
3867 rewind(blame->f);
3868 werase(view->window);
3870 if (asprintf(&line, "commit %s", id_str) == -1) {
3871 err = got_error_from_errno("asprintf");
3872 free(id_str);
3873 return err;
3876 err = format_line(&wline, &width, line, view->ncols, 0);
3877 free(line);
3878 line = NULL;
3879 if (err)
3880 return err;
3881 if (view_needs_focus_indication(view))
3882 wstandout(view->window);
3883 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3884 if (tc)
3885 wattr_on(view->window,
3886 COLOR_PAIR(tc->colorpair), NULL);
3887 waddwstr(view->window, wline);
3888 if (tc)
3889 wattr_off(view->window,
3890 COLOR_PAIR(tc->colorpair), NULL);
3891 if (view_needs_focus_indication(view))
3892 wstandend(view->window);
3893 free(wline);
3894 wline = NULL;
3895 if (width < view->ncols - 1)
3896 waddch(view->window, '\n');
3898 if (asprintf(&line, "[%d/%d] %s%s",
3899 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3900 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3901 free(id_str);
3902 return got_error_from_errno("asprintf");
3904 free(id_str);
3905 err = format_line(&wline, &width, line, view->ncols, 0);
3906 free(line);
3907 line = NULL;
3908 if (err)
3909 return err;
3910 waddwstr(view->window, wline);
3911 free(wline);
3912 wline = NULL;
3913 if (width < view->ncols - 1)
3914 waddch(view->window, '\n');
3916 s->eof = 0;
3917 while (nprinted < view->nlines - 2) {
3918 line = parse_next_line(blame->f, &len);
3919 if (line == NULL) {
3920 s->eof = 1;
3921 break;
3923 if (++lineno < s->first_displayed_line) {
3924 free(line);
3925 continue;
3928 if (view->focussed && nprinted == s->selected_line - 1)
3929 wstandout(view->window);
3931 if (blame->nlines > 0) {
3932 blame_line = &blame->lines[lineno - 1];
3933 if (blame_line->annotated && prev_id &&
3934 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3935 !(view->focussed &&
3936 nprinted == s->selected_line - 1)) {
3937 waddstr(view->window, " ");
3938 } else if (blame_line->annotated) {
3939 char *id_str;
3940 err = got_object_id_str(&id_str, blame_line->id);
3941 if (err) {
3942 free(line);
3943 return err;
3945 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3946 if (tc)
3947 wattr_on(view->window,
3948 COLOR_PAIR(tc->colorpair), NULL);
3949 wprintw(view->window, "%.8s", id_str);
3950 if (tc)
3951 wattr_off(view->window,
3952 COLOR_PAIR(tc->colorpair), NULL);
3953 free(id_str);
3954 prev_id = blame_line->id;
3955 } else {
3956 waddstr(view->window, "........");
3957 prev_id = NULL;
3959 } else {
3960 waddstr(view->window, "........");
3961 prev_id = NULL;
3964 if (view->focussed && nprinted == s->selected_line - 1)
3965 wstandend(view->window);
3966 waddstr(view->window, " ");
3968 if (view->ncols <= 9) {
3969 width = 9;
3970 wline = wcsdup(L"");
3971 if (wline == NULL) {
3972 err = got_error_from_errno("wcsdup");
3973 free(line);
3974 return err;
3976 } else if (s->first_displayed_line + nprinted ==
3977 s->matched_line &&
3978 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3979 err = add_matched_line(&width, line, view->ncols - 9, 9,
3980 view->window, regmatch);
3981 if (err) {
3982 free(line);
3983 return err;
3985 width += 9;
3986 } else {
3987 err = format_line(&wline, &width, line,
3988 view->ncols - 9, 9);
3989 waddwstr(view->window, wline);
3990 free(wline);
3991 wline = NULL;
3992 width += 9;
3995 if (width <= view->ncols - 1)
3996 waddch(view->window, '\n');
3997 if (++nprinted == 1)
3998 s->first_displayed_line = lineno;
3999 free(line);
4001 s->last_displayed_line = lineno;
4003 view_vborder(view);
4005 return NULL;
4008 static const struct got_error *
4009 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4011 const struct got_error *err = NULL;
4012 struct tog_blame_cb_args *a = arg;
4013 struct tog_blame_line *line;
4014 int errcode;
4016 if (nlines != a->nlines ||
4017 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4018 return got_error(GOT_ERR_RANGE);
4020 errcode = pthread_mutex_lock(&tog_mutex);
4021 if (errcode)
4022 return got_error_set_errno(errcode, "pthread_mutex_lock");
4024 if (*a->quit) { /* user has quit the blame view */
4025 err = got_error(GOT_ERR_ITER_COMPLETED);
4026 goto done;
4029 if (lineno == -1)
4030 goto done; /* no change in this commit */
4032 line = &a->lines[lineno - 1];
4033 if (line->annotated)
4034 goto done;
4036 line->id = got_object_id_dup(id);
4037 if (line->id == NULL) {
4038 err = got_error_from_errno("got_object_id_dup");
4039 goto done;
4041 line->annotated = 1;
4042 done:
4043 errcode = pthread_mutex_unlock(&tog_mutex);
4044 if (errcode)
4045 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4046 return err;
4049 static void *
4050 blame_thread(void *arg)
4052 const struct got_error *err;
4053 struct tog_blame_thread_args *ta = arg;
4054 struct tog_blame_cb_args *a = ta->cb_args;
4055 int errcode;
4057 err = block_signals_used_by_main_thread();
4058 if (err)
4059 return (void *)err;
4061 err = got_blame(ta->path, a->commit_id, ta->repo,
4062 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4063 if (err && err->code == GOT_ERR_CANCELLED)
4064 err = NULL;
4066 errcode = pthread_mutex_lock(&tog_mutex);
4067 if (errcode)
4068 return (void *)got_error_set_errno(errcode,
4069 "pthread_mutex_lock");
4071 got_repo_close(ta->repo);
4072 ta->repo = NULL;
4073 *ta->complete = 1;
4075 errcode = pthread_mutex_unlock(&tog_mutex);
4076 if (errcode && err == NULL)
4077 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4079 return (void *)err;
4082 static struct got_object_id *
4083 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4084 int first_displayed_line, int selected_line)
4086 struct tog_blame_line *line;
4088 if (nlines <= 0)
4089 return NULL;
4091 line = &lines[first_displayed_line - 1 + selected_line - 1];
4092 if (!line->annotated)
4093 return NULL;
4095 return line->id;
4098 static const struct got_error *
4099 stop_blame(struct tog_blame *blame)
4101 const struct got_error *err = NULL;
4102 int i;
4104 if (blame->thread) {
4105 int errcode;
4106 errcode = pthread_mutex_unlock(&tog_mutex);
4107 if (errcode)
4108 return got_error_set_errno(errcode,
4109 "pthread_mutex_unlock");
4110 errcode = pthread_join(blame->thread, (void **)&err);
4111 if (errcode)
4112 return got_error_set_errno(errcode, "pthread_join");
4113 errcode = pthread_mutex_lock(&tog_mutex);
4114 if (errcode)
4115 return got_error_set_errno(errcode,
4116 "pthread_mutex_lock");
4117 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4118 err = NULL;
4119 blame->thread = NULL;
4121 if (blame->thread_args.repo) {
4122 got_repo_close(blame->thread_args.repo);
4123 blame->thread_args.repo = NULL;
4125 if (blame->f) {
4126 if (fclose(blame->f) != 0 && err == NULL)
4127 err = got_error_from_errno("fclose");
4128 blame->f = NULL;
4130 if (blame->lines) {
4131 for (i = 0; i < blame->nlines; i++)
4132 free(blame->lines[i].id);
4133 free(blame->lines);
4134 blame->lines = NULL;
4136 free(blame->cb_args.commit_id);
4137 blame->cb_args.commit_id = NULL;
4139 return err;
4142 static const struct got_error *
4143 cancel_blame_view(void *arg)
4145 const struct got_error *err = NULL;
4146 int *done = arg;
4147 int errcode;
4149 errcode = pthread_mutex_lock(&tog_mutex);
4150 if (errcode)
4151 return got_error_set_errno(errcode,
4152 "pthread_mutex_unlock");
4154 if (*done)
4155 err = got_error(GOT_ERR_CANCELLED);
4157 errcode = pthread_mutex_unlock(&tog_mutex);
4158 if (errcode)
4159 return got_error_set_errno(errcode,
4160 "pthread_mutex_lock");
4162 return err;
4165 static const struct got_error *
4166 run_blame(struct tog_view *view)
4168 struct tog_blame_view_state *s = &view->state.blame;
4169 struct tog_blame *blame = &s->blame;
4170 const struct got_error *err = NULL;
4171 struct got_blob_object *blob = NULL;
4172 struct got_repository *thread_repo = NULL;
4173 struct got_object_id *obj_id = NULL;
4174 int obj_type;
4176 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4177 s->path);
4178 if (err)
4179 return err;
4181 err = got_object_get_type(&obj_type, s->repo, obj_id);
4182 if (err)
4183 goto done;
4185 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4186 err = got_error(GOT_ERR_OBJ_TYPE);
4187 goto done;
4190 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4191 if (err)
4192 goto done;
4193 blame->f = got_opentemp();
4194 if (blame->f == NULL) {
4195 err = got_error_from_errno("got_opentemp");
4196 goto done;
4198 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4199 &blame->line_offsets, blame->f, blob);
4200 if (err || blame->nlines == 0)
4201 goto done;
4203 /* Don't include \n at EOF in the blame line count. */
4204 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4205 blame->nlines--;
4207 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4208 if (blame->lines == NULL) {
4209 err = got_error_from_errno("calloc");
4210 goto done;
4213 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4214 if (err)
4215 goto done;
4217 blame->cb_args.view = view;
4218 blame->cb_args.lines = blame->lines;
4219 blame->cb_args.nlines = blame->nlines;
4220 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4221 if (blame->cb_args.commit_id == NULL) {
4222 err = got_error_from_errno("got_object_id_dup");
4223 goto done;
4225 blame->cb_args.quit = &s->done;
4227 blame->thread_args.path = s->path;
4228 blame->thread_args.repo = thread_repo;
4229 blame->thread_args.cb_args = &blame->cb_args;
4230 blame->thread_args.complete = &s->blame_complete;
4231 blame->thread_args.cancel_cb = cancel_blame_view;
4232 blame->thread_args.cancel_arg = &s->done;
4233 s->blame_complete = 0;
4235 done:
4236 if (blob)
4237 got_object_blob_close(blob);
4238 free(obj_id);
4239 if (err)
4240 stop_blame(blame);
4241 return err;
4244 static const struct got_error *
4245 open_blame_view(struct tog_view *view, char *path,
4246 struct got_object_id *commit_id, struct got_repository *repo)
4248 const struct got_error *err = NULL;
4249 struct tog_blame_view_state *s = &view->state.blame;
4251 SIMPLEQ_INIT(&s->blamed_commits);
4253 s->path = strdup(path);
4254 if (s->path == NULL)
4255 return got_error_from_errno("strdup");
4257 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4258 if (err) {
4259 free(s->path);
4260 return err;
4263 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4264 s->first_displayed_line = 1;
4265 s->last_displayed_line = view->nlines;
4266 s->selected_line = 1;
4267 s->blame_complete = 0;
4268 s->repo = repo;
4269 s->commit_id = commit_id;
4270 memset(&s->blame, 0, sizeof(s->blame));
4272 SIMPLEQ_INIT(&s->colors);
4273 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4274 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4275 get_color_value("TOG_COLOR_COMMIT"));
4276 if (err)
4277 return err;
4280 view->show = show_blame_view;
4281 view->input = input_blame_view;
4282 view->close = close_blame_view;
4283 view->search_start = search_start_blame_view;
4284 view->search_next = search_next_blame_view;
4286 return run_blame(view);
4289 static const struct got_error *
4290 close_blame_view(struct tog_view *view)
4292 const struct got_error *err = NULL;
4293 struct tog_blame_view_state *s = &view->state.blame;
4295 if (s->blame.thread)
4296 err = stop_blame(&s->blame);
4298 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4299 struct got_object_qid *blamed_commit;
4300 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4301 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4302 got_object_qid_free(blamed_commit);
4305 free(s->path);
4306 free_colors(&s->colors);
4308 return err;
4311 static const struct got_error *
4312 search_start_blame_view(struct tog_view *view)
4314 struct tog_blame_view_state *s = &view->state.blame;
4316 s->matched_line = 0;
4317 return NULL;
4320 static const struct got_error *
4321 search_next_blame_view(struct tog_view *view)
4323 struct tog_blame_view_state *s = &view->state.blame;
4324 int lineno;
4326 if (!view->searching) {
4327 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4328 return NULL;
4331 if (s->matched_line) {
4332 if (view->searching == TOG_SEARCH_FORWARD)
4333 lineno = s->matched_line + 1;
4334 else
4335 lineno = s->matched_line - 1;
4336 } else {
4337 if (view->searching == TOG_SEARCH_FORWARD)
4338 lineno = 1;
4339 else
4340 lineno = s->blame.nlines;
4343 while (1) {
4344 char *line = NULL;
4345 off_t offset;
4346 size_t len;
4348 if (lineno <= 0 || lineno > s->blame.nlines) {
4349 if (s->matched_line == 0) {
4350 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4351 free(line);
4352 break;
4355 if (view->searching == TOG_SEARCH_FORWARD)
4356 lineno = 1;
4357 else
4358 lineno = s->blame.nlines;
4361 offset = s->blame.line_offsets[lineno - 1];
4362 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4363 free(line);
4364 return got_error_from_errno("fseeko");
4366 free(line);
4367 line = parse_next_line(s->blame.f, &len);
4368 if (line &&
4369 match_line(line, &view->regex, 1, &view->regmatch)) {
4370 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4371 s->matched_line = lineno;
4372 free(line);
4373 break;
4375 free(line);
4376 if (view->searching == TOG_SEARCH_FORWARD)
4377 lineno++;
4378 else
4379 lineno--;
4382 if (s->matched_line) {
4383 s->first_displayed_line = s->matched_line;
4384 s->selected_line = 1;
4387 return NULL;
4390 static const struct got_error *
4391 show_blame_view(struct tog_view *view)
4393 const struct got_error *err = NULL;
4394 struct tog_blame_view_state *s = &view->state.blame;
4395 int errcode;
4397 if (s->blame.thread == NULL) {
4398 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4399 &s->blame.thread_args);
4400 if (errcode)
4401 return got_error_set_errno(errcode, "pthread_create");
4403 halfdelay(1); /* fast refresh while annotating */
4406 if (s->blame_complete)
4407 halfdelay(10); /* disable fast refresh */
4409 err = draw_blame(view);
4411 view_vborder(view);
4412 return err;
4415 static const struct got_error *
4416 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4418 const struct got_error *err = NULL, *thread_err = NULL;
4419 struct tog_view *diff_view;
4420 struct tog_blame_view_state *s = &view->state.blame;
4421 int begin_x = 0;
4423 switch (ch) {
4424 case 'q':
4425 s->done = 1;
4426 break;
4427 case 'k':
4428 case KEY_UP:
4429 if (s->selected_line > 1)
4430 s->selected_line--;
4431 else if (s->selected_line == 1 &&
4432 s->first_displayed_line > 1)
4433 s->first_displayed_line--;
4434 break;
4435 case KEY_PPAGE:
4436 case CTRL('b'):
4437 if (s->first_displayed_line == 1) {
4438 s->selected_line = 1;
4439 break;
4441 if (s->first_displayed_line > view->nlines - 2)
4442 s->first_displayed_line -=
4443 (view->nlines - 2);
4444 else
4445 s->first_displayed_line = 1;
4446 break;
4447 case 'j':
4448 case KEY_DOWN:
4449 if (s->selected_line < view->nlines - 2 &&
4450 s->first_displayed_line +
4451 s->selected_line <= s->blame.nlines)
4452 s->selected_line++;
4453 else if (s->last_displayed_line <
4454 s->blame.nlines)
4455 s->first_displayed_line++;
4456 break;
4457 case 'b':
4458 case 'p': {
4459 struct got_object_id *id = NULL;
4460 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4461 s->first_displayed_line, s->selected_line);
4462 if (id == NULL)
4463 break;
4464 if (ch == 'p') {
4465 struct got_commit_object *commit;
4466 struct got_object_qid *pid;
4467 struct got_object_id *blob_id = NULL;
4468 int obj_type;
4469 err = got_object_open_as_commit(&commit,
4470 s->repo, id);
4471 if (err)
4472 break;
4473 pid = SIMPLEQ_FIRST(
4474 got_object_commit_get_parent_ids(commit));
4475 if (pid == NULL) {
4476 got_object_commit_close(commit);
4477 break;
4479 /* Check if path history ends here. */
4480 err = got_object_id_by_path(&blob_id, s->repo,
4481 pid->id, s->path);
4482 if (err) {
4483 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4484 err = NULL;
4485 got_object_commit_close(commit);
4486 break;
4488 err = got_object_get_type(&obj_type, s->repo,
4489 blob_id);
4490 free(blob_id);
4491 /* Can't blame non-blob type objects. */
4492 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4493 got_object_commit_close(commit);
4494 break;
4496 err = got_object_qid_alloc(&s->blamed_commit,
4497 pid->id);
4498 got_object_commit_close(commit);
4499 } else {
4500 if (got_object_id_cmp(id,
4501 s->blamed_commit->id) == 0)
4502 break;
4503 err = got_object_qid_alloc(&s->blamed_commit,
4504 id);
4506 if (err)
4507 break;
4508 s->done = 1;
4509 thread_err = stop_blame(&s->blame);
4510 s->done = 0;
4511 if (thread_err)
4512 break;
4513 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4514 s->blamed_commit, entry);
4515 err = run_blame(view);
4516 if (err)
4517 break;
4518 break;
4520 case 'B': {
4521 struct got_object_qid *first;
4522 first = SIMPLEQ_FIRST(&s->blamed_commits);
4523 if (!got_object_id_cmp(first->id, s->commit_id))
4524 break;
4525 s->done = 1;
4526 thread_err = stop_blame(&s->blame);
4527 s->done = 0;
4528 if (thread_err)
4529 break;
4530 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4531 got_object_qid_free(s->blamed_commit);
4532 s->blamed_commit =
4533 SIMPLEQ_FIRST(&s->blamed_commits);
4534 err = run_blame(view);
4535 if (err)
4536 break;
4537 break;
4539 case KEY_ENTER:
4540 case '\r': {
4541 struct got_object_id *id = NULL;
4542 struct got_object_qid *pid;
4543 struct got_commit_object *commit = NULL;
4544 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4545 s->first_displayed_line, s->selected_line);
4546 if (id == NULL)
4547 break;
4548 err = got_object_open_as_commit(&commit, s->repo, id);
4549 if (err)
4550 break;
4551 pid = SIMPLEQ_FIRST(
4552 got_object_commit_get_parent_ids(commit));
4553 if (view_is_parent_view(view))
4554 begin_x = view_split_begin_x(view->begin_x);
4555 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4556 if (diff_view == NULL) {
4557 got_object_commit_close(commit);
4558 err = got_error_from_errno("view_open");
4559 break;
4561 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4562 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4563 got_object_commit_close(commit);
4564 if (err) {
4565 view_close(diff_view);
4566 break;
4568 view->focussed = 0;
4569 diff_view->focussed = 1;
4570 if (view_is_parent_view(view)) {
4571 err = view_close_child(view);
4572 if (err)
4573 break;
4574 view_set_child(view, diff_view);
4575 view->focus_child = 1;
4576 } else
4577 *new_view = diff_view;
4578 if (err)
4579 break;
4580 break;
4582 case KEY_NPAGE:
4583 case CTRL('f'):
4584 case ' ':
4585 if (s->last_displayed_line >= s->blame.nlines &&
4586 s->selected_line >= MIN(s->blame.nlines,
4587 view->nlines - 2)) {
4588 break;
4590 if (s->last_displayed_line >= s->blame.nlines &&
4591 s->selected_line < view->nlines - 2) {
4592 s->selected_line = MIN(s->blame.nlines,
4593 view->nlines - 2);
4594 break;
4596 if (s->last_displayed_line + view->nlines - 2
4597 <= s->blame.nlines)
4598 s->first_displayed_line +=
4599 view->nlines - 2;
4600 else
4601 s->first_displayed_line =
4602 s->blame.nlines -
4603 (view->nlines - 3);
4604 break;
4605 case KEY_RESIZE:
4606 if (s->selected_line > view->nlines - 2) {
4607 s->selected_line = MIN(s->blame.nlines,
4608 view->nlines - 2);
4610 break;
4611 default:
4612 break;
4614 return thread_err ? thread_err : err;
4617 static const struct got_error *
4618 cmd_blame(int argc, char *argv[])
4620 const struct got_error *error;
4621 struct got_repository *repo = NULL;
4622 struct got_worktree *worktree = NULL;
4623 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4624 char *link_target = NULL;
4625 struct got_object_id *commit_id = NULL;
4626 char *commit_id_str = NULL;
4627 int ch;
4628 struct tog_view *view;
4630 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4631 switch (ch) {
4632 case 'c':
4633 commit_id_str = optarg;
4634 break;
4635 case 'r':
4636 repo_path = realpath(optarg, NULL);
4637 if (repo_path == NULL)
4638 return got_error_from_errno2("realpath",
4639 optarg);
4640 break;
4641 default:
4642 usage_blame();
4643 /* NOTREACHED */
4647 argc -= optind;
4648 argv += optind;
4650 if (argc != 1)
4651 usage_blame();
4653 cwd = getcwd(NULL, 0);
4654 if (cwd == NULL)
4655 return got_error_from_errno("getcwd");
4657 error = got_worktree_open(&worktree, cwd);
4658 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4659 goto done;
4661 if (repo_path == NULL) {
4662 if (worktree)
4663 repo_path =
4664 strdup(got_worktree_get_repo_path(worktree));
4665 else
4666 repo_path = strdup(cwd);
4668 if (repo_path == NULL) {
4669 error = got_error_from_errno("strdup");
4670 goto done;
4673 error = got_repo_open(&repo, repo_path, NULL);
4674 if (error != NULL)
4675 goto done;
4677 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4678 worktree);
4679 if (error)
4680 goto done;
4682 init_curses();
4684 error = apply_unveil(got_repo_get_path(repo), NULL);
4685 if (error)
4686 goto done;
4688 if (commit_id_str == NULL) {
4689 struct got_reference *head_ref;
4690 error = got_ref_open(&head_ref, repo, worktree ?
4691 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4692 if (error != NULL)
4693 goto done;
4694 error = got_ref_resolve(&commit_id, repo, head_ref);
4695 got_ref_close(head_ref);
4696 } else {
4697 error = got_repo_match_object_id(&commit_id, NULL,
4698 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4700 if (error != NULL)
4701 goto done;
4703 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4704 if (view == NULL) {
4705 error = got_error_from_errno("view_open");
4706 goto done;
4709 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4710 commit_id, repo);
4711 if (error)
4712 goto done;
4714 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4715 commit_id, repo);
4716 if (error)
4717 goto done;
4718 if (worktree) {
4719 /* Release work tree lock. */
4720 got_worktree_close(worktree);
4721 worktree = NULL;
4723 error = view_loop(view);
4724 done:
4725 free(repo_path);
4726 free(in_repo_path);
4727 free(link_target);
4728 free(cwd);
4729 free(commit_id);
4730 if (worktree)
4731 got_worktree_close(worktree);
4732 if (repo)
4733 got_repo_close(repo);
4734 return error;
4737 static const struct got_error *
4738 draw_tree_entries(struct tog_view *view, const char *parent_path)
4740 struct tog_tree_view_state *s = &view->state.tree;
4741 const struct got_error *err = NULL;
4742 struct got_tree_entry *te;
4743 wchar_t *wline;
4744 struct tog_color *tc;
4745 int width, n, i, nentries;
4746 int limit = view->nlines;
4748 s->ndisplayed = 0;
4750 werase(view->window);
4752 if (limit == 0)
4753 return NULL;
4755 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4756 if (err)
4757 return err;
4758 if (view_needs_focus_indication(view))
4759 wstandout(view->window);
4760 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4761 if (tc)
4762 wattr_on(view->window,
4763 COLOR_PAIR(tc->colorpair), NULL);
4764 waddwstr(view->window, wline);
4765 if (tc)
4766 wattr_off(view->window,
4767 COLOR_PAIR(tc->colorpair), NULL);
4768 if (view_needs_focus_indication(view))
4769 wstandend(view->window);
4770 free(wline);
4771 wline = NULL;
4772 if (width < view->ncols - 1)
4773 waddch(view->window, '\n');
4774 if (--limit <= 0)
4775 return NULL;
4776 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4777 if (err)
4778 return err;
4779 waddwstr(view->window, wline);
4780 free(wline);
4781 wline = NULL;
4782 if (width < view->ncols - 1)
4783 waddch(view->window, '\n');
4784 if (--limit <= 0)
4785 return NULL;
4786 waddch(view->window, '\n');
4787 if (--limit <= 0)
4788 return NULL;
4790 if (s->first_displayed_entry == NULL) {
4791 te = got_object_tree_get_first_entry(s->tree);
4792 if (s->selected == 0) {
4793 if (view->focussed)
4794 wstandout(view->window);
4795 s->selected_entry = NULL;
4797 waddstr(view->window, " ..\n"); /* parent directory */
4798 if (s->selected == 0 && view->focussed)
4799 wstandend(view->window);
4800 s->ndisplayed++;
4801 if (--limit <= 0)
4802 return NULL;
4803 n = 1;
4804 } else {
4805 n = 0;
4806 te = s->first_displayed_entry;
4809 nentries = got_object_tree_get_nentries(s->tree);
4810 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4811 char *line = NULL, *id_str = NULL, *link_target = NULL;
4812 const char *modestr = "";
4813 mode_t mode;
4815 te = got_object_tree_get_entry(s->tree, i);
4816 mode = got_tree_entry_get_mode(te);
4818 if (s->show_ids) {
4819 err = got_object_id_str(&id_str,
4820 got_tree_entry_get_id(te));
4821 if (err)
4822 return got_error_from_errno(
4823 "got_object_id_str");
4825 if (got_object_tree_entry_is_submodule(te))
4826 modestr = "$";
4827 else if (S_ISLNK(mode)) {
4828 int i;
4830 err = got_tree_entry_get_symlink_target(&link_target,
4831 te, s->repo);
4832 if (err) {
4833 free(id_str);
4834 return err;
4836 for (i = 0; i < strlen(link_target); i++) {
4837 if (!isprint((unsigned char)link_target[i]))
4838 link_target[i] = '?';
4840 modestr = "@";
4842 else if (S_ISDIR(mode))
4843 modestr = "/";
4844 else if (mode & S_IXUSR)
4845 modestr = "*";
4846 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4847 got_tree_entry_get_name(te), modestr,
4848 link_target ? " -> ": "",
4849 link_target ? link_target : "") == -1) {
4850 free(id_str);
4851 free(link_target);
4852 return got_error_from_errno("asprintf");
4854 free(id_str);
4855 free(link_target);
4856 err = format_line(&wline, &width, line, view->ncols, 0);
4857 if (err) {
4858 free(line);
4859 break;
4861 if (n == s->selected) {
4862 if (view->focussed)
4863 wstandout(view->window);
4864 s->selected_entry = te;
4866 tc = match_color(&s->colors, line);
4867 if (tc)
4868 wattr_on(view->window,
4869 COLOR_PAIR(tc->colorpair), NULL);
4870 waddwstr(view->window, wline);
4871 if (tc)
4872 wattr_off(view->window,
4873 COLOR_PAIR(tc->colorpair), NULL);
4874 if (width < view->ncols - 1)
4875 waddch(view->window, '\n');
4876 if (n == s->selected && view->focussed)
4877 wstandend(view->window);
4878 free(line);
4879 free(wline);
4880 wline = NULL;
4881 n++;
4882 s->ndisplayed++;
4883 s->last_displayed_entry = te;
4884 if (--limit <= 0)
4885 break;
4888 return err;
4891 static void
4892 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4894 struct got_tree_entry *te;
4895 int isroot = s->tree == s->root;
4896 int i = 0;
4898 if (s->first_displayed_entry == NULL)
4899 return;
4901 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4902 while (i++ < maxscroll) {
4903 if (te == NULL) {
4904 if (!isroot)
4905 s->first_displayed_entry = NULL;
4906 break;
4908 s->first_displayed_entry = te;
4909 te = got_tree_entry_get_prev(s->tree, te);
4913 static void
4914 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4916 struct got_tree_entry *next, *last;
4917 int n = 0;
4919 if (s->first_displayed_entry)
4920 next = got_tree_entry_get_next(s->tree,
4921 s->first_displayed_entry);
4922 else
4923 next = got_object_tree_get_first_entry(s->tree);
4925 last = s->last_displayed_entry;
4926 while (next && last && n++ < maxscroll) {
4927 last = got_tree_entry_get_next(s->tree, last);
4928 if (last) {
4929 s->first_displayed_entry = next;
4930 next = got_tree_entry_get_next(s->tree, next);
4935 static const struct got_error *
4936 tree_entry_path(char **path, struct tog_parent_trees *parents,
4937 struct got_tree_entry *te)
4939 const struct got_error *err = NULL;
4940 struct tog_parent_tree *pt;
4941 size_t len = 2; /* for leading slash and NUL */
4943 TAILQ_FOREACH(pt, parents, entry)
4944 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4945 + 1 /* slash */;
4946 if (te)
4947 len += strlen(got_tree_entry_get_name(te));
4949 *path = calloc(1, len);
4950 if (path == NULL)
4951 return got_error_from_errno("calloc");
4953 (*path)[0] = '/';
4954 pt = TAILQ_LAST(parents, tog_parent_trees);
4955 while (pt) {
4956 const char *name = got_tree_entry_get_name(pt->selected_entry);
4957 if (strlcat(*path, name, len) >= len) {
4958 err = got_error(GOT_ERR_NO_SPACE);
4959 goto done;
4961 if (strlcat(*path, "/", len) >= len) {
4962 err = got_error(GOT_ERR_NO_SPACE);
4963 goto done;
4965 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4967 if (te) {
4968 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4969 err = got_error(GOT_ERR_NO_SPACE);
4970 goto done;
4973 done:
4974 if (err) {
4975 free(*path);
4976 *path = NULL;
4978 return err;
4981 static const struct got_error *
4982 blame_tree_entry(struct tog_view **new_view, int begin_x,
4983 struct got_tree_entry *te, struct tog_parent_trees *parents,
4984 struct got_object_id *commit_id, struct got_repository *repo)
4986 const struct got_error *err = NULL;
4987 char *path;
4988 struct tog_view *blame_view;
4990 *new_view = NULL;
4992 err = tree_entry_path(&path, parents, te);
4993 if (err)
4994 return err;
4996 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4997 if (blame_view == NULL) {
4998 err = got_error_from_errno("view_open");
4999 goto done;
5002 err = open_blame_view(blame_view, path, commit_id, repo);
5003 if (err) {
5004 if (err->code == GOT_ERR_CANCELLED)
5005 err = NULL;
5006 view_close(blame_view);
5007 } else
5008 *new_view = blame_view;
5009 done:
5010 free(path);
5011 return err;
5014 static const struct got_error *
5015 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5016 struct tog_tree_view_state *s)
5018 struct tog_view *log_view;
5019 const struct got_error *err = NULL;
5020 char *path;
5022 *new_view = NULL;
5024 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5025 if (log_view == NULL)
5026 return got_error_from_errno("view_open");
5028 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5029 if (err)
5030 return err;
5032 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5033 path, 0);
5034 if (err)
5035 view_close(log_view);
5036 else
5037 *new_view = log_view;
5038 free(path);
5039 return err;
5042 static const struct got_error *
5043 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5044 struct got_object_id *commit_id, const char *head_ref_name,
5045 struct got_repository *repo)
5047 const struct got_error *err = NULL;
5048 char *commit_id_str = NULL;
5049 struct tog_tree_view_state *s = &view->state.tree;
5051 TAILQ_INIT(&s->parents);
5053 err = got_object_id_str(&commit_id_str, commit_id);
5054 if (err != NULL)
5055 goto done;
5057 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5058 err = got_error_from_errno("asprintf");
5059 goto done;
5062 s->root = s->tree = root;
5063 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5064 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5065 s->commit_id = got_object_id_dup(commit_id);
5066 if (s->commit_id == NULL) {
5067 err = got_error_from_errno("got_object_id_dup");
5068 goto done;
5070 s->head_ref_name = head_ref_name;
5071 s->repo = repo;
5073 SIMPLEQ_INIT(&s->colors);
5075 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5076 err = add_color(&s->colors, "\\$$",
5077 TOG_COLOR_TREE_SUBMODULE,
5078 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5079 if (err)
5080 goto done;
5081 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5082 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5083 if (err) {
5084 free_colors(&s->colors);
5085 goto done;
5087 err = add_color(&s->colors, "/$",
5088 TOG_COLOR_TREE_DIRECTORY,
5089 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5090 if (err) {
5091 free_colors(&s->colors);
5092 goto done;
5095 err = add_color(&s->colors, "\\*$",
5096 TOG_COLOR_TREE_EXECUTABLE,
5097 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5098 if (err) {
5099 free_colors(&s->colors);
5100 goto done;
5103 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5104 get_color_value("TOG_COLOR_COMMIT"));
5105 if (err) {
5106 free_colors(&s->colors);
5107 goto done;
5111 view->show = show_tree_view;
5112 view->input = input_tree_view;
5113 view->close = close_tree_view;
5114 view->search_start = search_start_tree_view;
5115 view->search_next = search_next_tree_view;
5116 done:
5117 free(commit_id_str);
5118 if (err) {
5119 free(s->tree_label);
5120 s->tree_label = NULL;
5122 return err;
5125 static const struct got_error *
5126 close_tree_view(struct tog_view *view)
5128 struct tog_tree_view_state *s = &view->state.tree;
5130 free_colors(&s->colors);
5131 free(s->tree_label);
5132 s->tree_label = NULL;
5133 free(s->commit_id);
5134 s->commit_id = NULL;
5135 while (!TAILQ_EMPTY(&s->parents)) {
5136 struct tog_parent_tree *parent;
5137 parent = TAILQ_FIRST(&s->parents);
5138 TAILQ_REMOVE(&s->parents, parent, entry);
5139 free(parent);
5142 if (s->tree != s->root)
5143 got_object_tree_close(s->tree);
5144 got_object_tree_close(s->root);
5145 return NULL;
5148 static const struct got_error *
5149 search_start_tree_view(struct tog_view *view)
5151 struct tog_tree_view_state *s = &view->state.tree;
5153 s->matched_entry = NULL;
5154 return NULL;
5157 static int
5158 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5160 regmatch_t regmatch;
5162 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5163 0) == 0;
5166 static const struct got_error *
5167 search_next_tree_view(struct tog_view *view)
5169 struct tog_tree_view_state *s = &view->state.tree;
5170 struct got_tree_entry *te = NULL;
5172 if (!view->searching) {
5173 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5174 return NULL;
5177 if (s->matched_entry) {
5178 if (view->searching == TOG_SEARCH_FORWARD) {
5179 if (s->selected_entry)
5180 te = got_tree_entry_get_next(s->tree,
5181 s->selected_entry);
5182 else
5183 te = got_object_tree_get_first_entry(s->tree);
5184 } else {
5185 if (s->selected_entry == NULL)
5186 te = got_object_tree_get_last_entry(s->tree);
5187 else
5188 te = got_tree_entry_get_prev(s->tree,
5189 s->selected_entry);
5191 } else {
5192 if (view->searching == TOG_SEARCH_FORWARD)
5193 te = got_object_tree_get_first_entry(s->tree);
5194 else
5195 te = got_object_tree_get_last_entry(s->tree);
5198 while (1) {
5199 if (te == NULL) {
5200 if (s->matched_entry == NULL) {
5201 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5202 return NULL;
5204 if (view->searching == TOG_SEARCH_FORWARD)
5205 te = got_object_tree_get_first_entry(s->tree);
5206 else
5207 te = got_object_tree_get_last_entry(s->tree);
5210 if (match_tree_entry(te, &view->regex)) {
5211 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5212 s->matched_entry = te;
5213 break;
5216 if (view->searching == TOG_SEARCH_FORWARD)
5217 te = got_tree_entry_get_next(s->tree, te);
5218 else
5219 te = got_tree_entry_get_prev(s->tree, te);
5222 if (s->matched_entry) {
5223 s->first_displayed_entry = s->matched_entry;
5224 s->selected = 0;
5227 return NULL;
5230 static const struct got_error *
5231 show_tree_view(struct tog_view *view)
5233 const struct got_error *err = NULL;
5234 struct tog_tree_view_state *s = &view->state.tree;
5235 char *parent_path;
5237 err = tree_entry_path(&parent_path, &s->parents, NULL);
5238 if (err)
5239 return err;
5241 err = draw_tree_entries(view, parent_path);
5242 free(parent_path);
5244 view_vborder(view);
5245 return err;
5248 static const struct got_error *
5249 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5251 const struct got_error *err = NULL;
5252 struct tog_tree_view_state *s = &view->state.tree;
5253 struct tog_view *log_view, *ref_view;
5254 int begin_x = 0;
5256 switch (ch) {
5257 case 'i':
5258 s->show_ids = !s->show_ids;
5259 break;
5260 case 'l':
5261 if (!s->selected_entry)
5262 break;
5263 if (view_is_parent_view(view))
5264 begin_x = view_split_begin_x(view->begin_x);
5265 err = log_selected_tree_entry(&log_view, begin_x, s);
5266 view->focussed = 0;
5267 log_view->focussed = 1;
5268 if (view_is_parent_view(view)) {
5269 err = view_close_child(view);
5270 if (err)
5271 return err;
5272 view_set_child(view, log_view);
5273 view->focus_child = 1;
5274 } else
5275 *new_view = log_view;
5276 break;
5277 case 'r':
5278 if (view_is_parent_view(view))
5279 begin_x = view_split_begin_x(view->begin_x);
5280 ref_view = view_open(view->nlines, view->ncols,
5281 view->begin_y, begin_x, TOG_VIEW_REF);
5282 if (ref_view == NULL)
5283 return got_error_from_errno("view_open");
5284 err = open_ref_view(ref_view, s->repo);
5285 if (err) {
5286 view_close(ref_view);
5287 return err;
5289 view->focussed = 0;
5290 ref_view->focussed = 1;
5291 if (view_is_parent_view(view)) {
5292 err = view_close_child(view);
5293 if (err)
5294 return err;
5295 view_set_child(view, ref_view);
5296 view->focus_child = 1;
5297 } else
5298 *new_view = ref_view;
5299 break;
5300 case 'k':
5301 case KEY_UP:
5302 if (s->selected > 0) {
5303 s->selected--;
5304 break;
5306 tree_scroll_up(s, 1);
5307 break;
5308 case KEY_PPAGE:
5309 case CTRL('b'):
5310 if (s->tree == s->root) {
5311 if (got_object_tree_get_first_entry(s->tree) ==
5312 s->first_displayed_entry)
5313 s->selected = 0;
5314 } else {
5315 if (s->first_displayed_entry == NULL)
5316 s->selected = 0;
5318 tree_scroll_up(s, MAX(0, view->nlines - 3));
5319 break;
5320 case 'j':
5321 case KEY_DOWN:
5322 if (s->selected < s->ndisplayed - 1) {
5323 s->selected++;
5324 break;
5326 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5327 == NULL)
5328 /* can't scroll any further */
5329 break;
5330 tree_scroll_down(s, 1);
5331 break;
5332 case KEY_NPAGE:
5333 case CTRL('f'):
5334 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5335 == NULL) {
5336 /* can't scroll any further; move cursor down */
5337 if (s->selected < s->ndisplayed - 1)
5338 s->selected = s->ndisplayed - 1;
5339 break;
5341 tree_scroll_down(s, view->nlines - 3);
5342 break;
5343 case KEY_ENTER:
5344 case '\r':
5345 case KEY_BACKSPACE:
5346 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5347 struct tog_parent_tree *parent;
5348 /* user selected '..' */
5349 if (s->tree == s->root)
5350 break;
5351 parent = TAILQ_FIRST(&s->parents);
5352 TAILQ_REMOVE(&s->parents, parent,
5353 entry);
5354 got_object_tree_close(s->tree);
5355 s->tree = parent->tree;
5356 s->first_displayed_entry =
5357 parent->first_displayed_entry;
5358 s->selected_entry =
5359 parent->selected_entry;
5360 s->selected = parent->selected;
5361 free(parent);
5362 } else if (S_ISDIR(got_tree_entry_get_mode(
5363 s->selected_entry))) {
5364 struct got_tree_object *subtree;
5365 err = got_object_open_as_tree(&subtree, s->repo,
5366 got_tree_entry_get_id(s->selected_entry));
5367 if (err)
5368 break;
5369 err = tree_view_visit_subtree(s, subtree);
5370 if (err) {
5371 got_object_tree_close(subtree);
5372 break;
5374 } else if (S_ISREG(got_tree_entry_get_mode(
5375 s->selected_entry))) {
5376 struct tog_view *blame_view;
5377 int begin_x = view_is_parent_view(view) ?
5378 view_split_begin_x(view->begin_x) : 0;
5380 err = blame_tree_entry(&blame_view, begin_x,
5381 s->selected_entry, &s->parents,
5382 s->commit_id, s->repo);
5383 if (err)
5384 break;
5385 view->focussed = 0;
5386 blame_view->focussed = 1;
5387 if (view_is_parent_view(view)) {
5388 err = view_close_child(view);
5389 if (err)
5390 return err;
5391 view_set_child(view, blame_view);
5392 view->focus_child = 1;
5393 } else
5394 *new_view = blame_view;
5396 break;
5397 case KEY_RESIZE:
5398 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5399 s->selected = view->nlines - 4;
5400 break;
5401 default:
5402 break;
5405 return err;
5408 __dead static void
5409 usage_tree(void)
5411 endwin();
5412 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5413 getprogname());
5414 exit(1);
5417 static const struct got_error *
5418 cmd_tree(int argc, char *argv[])
5420 const struct got_error *error;
5421 struct got_repository *repo = NULL;
5422 struct got_worktree *worktree = NULL;
5423 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5424 struct got_object_id *commit_id = NULL;
5425 const char *commit_id_arg = NULL;
5426 char *label = NULL;
5427 struct got_commit_object *commit = NULL;
5428 struct got_tree_object *tree = NULL;
5429 struct got_reference *ref = NULL;
5430 const char *head_ref_name = NULL;
5431 int ch;
5432 struct tog_view *view;
5434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5435 switch (ch) {
5436 case 'c':
5437 commit_id_arg = optarg;
5438 break;
5439 case 'r':
5440 repo_path = realpath(optarg, NULL);
5441 if (repo_path == NULL)
5442 return got_error_from_errno2("realpath",
5443 optarg);
5444 break;
5445 default:
5446 usage_tree();
5447 /* NOTREACHED */
5451 argc -= optind;
5452 argv += optind;
5454 if (argc > 1)
5455 usage_tree();
5457 cwd = getcwd(NULL, 0);
5458 if (cwd == NULL)
5459 return got_error_from_errno("getcwd");
5461 error = got_worktree_open(&worktree, cwd);
5462 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5463 goto done;
5465 if (repo_path == NULL) {
5466 if (worktree)
5467 repo_path =
5468 strdup(got_worktree_get_repo_path(worktree));
5469 else
5470 repo_path = strdup(cwd);
5472 if (repo_path == NULL) {
5473 error = got_error_from_errno("strdup");
5474 goto done;
5477 error = got_repo_open(&repo, repo_path, NULL);
5478 if (error != NULL)
5479 goto done;
5481 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5482 repo, worktree);
5483 if (error)
5484 goto done;
5486 init_curses();
5488 error = apply_unveil(got_repo_get_path(repo), NULL);
5489 if (error)
5490 goto done;
5492 if (commit_id_arg == NULL) {
5493 error = got_repo_match_object_id(&commit_id, &label,
5494 worktree ? got_worktree_get_head_ref_name(worktree) :
5495 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5496 if (error)
5497 goto done;
5498 head_ref_name = label;
5499 } else {
5500 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5501 if (error == NULL)
5502 head_ref_name = got_ref_get_name(ref);
5503 else if (error->code != GOT_ERR_NOT_REF)
5504 goto done;
5505 error = got_repo_match_object_id(&commit_id, NULL,
5506 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5507 if (error)
5508 goto done;
5511 error = got_object_open_as_commit(&commit, repo, commit_id);
5512 if (error)
5513 goto done;
5515 error = got_object_open_as_tree(&tree, repo,
5516 got_object_commit_get_tree_id(commit));
5517 if (error)
5518 goto done;
5520 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5521 if (view == NULL) {
5522 error = got_error_from_errno("view_open");
5523 goto done;
5525 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5526 if (error)
5527 goto done;
5528 if (!got_path_is_root_dir(in_repo_path)) {
5529 error = tree_view_walk_path(&view->state.tree, commit_id,
5530 in_repo_path);
5531 if (error)
5532 goto done;
5535 if (worktree) {
5536 /* Release work tree lock. */
5537 got_worktree_close(worktree);
5538 worktree = NULL;
5540 error = view_loop(view);
5541 done:
5542 free(repo_path);
5543 free(cwd);
5544 free(commit_id);
5545 free(label);
5546 if (ref)
5547 got_ref_close(ref);
5548 if (commit)
5549 got_object_commit_close(commit);
5550 if (tree)
5551 got_object_tree_close(tree);
5552 if (repo)
5553 got_repo_close(repo);
5554 return error;
5557 static const struct got_error *
5558 ref_view_load_refs(struct tog_ref_view_state *s)
5560 const struct got_error *err;
5561 struct got_reflist_entry *sre;
5562 struct tog_reflist_entry *re;
5564 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5565 got_ref_cmp_by_name, NULL);
5566 if (err)
5567 return err;
5569 s->nrefs = 0;
5570 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5571 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5572 continue;
5574 re = malloc(sizeof(*re));
5575 if (re == NULL)
5576 return got_error_from_errno("malloc");
5578 re->ref = sre->ref;
5579 re->idx = s->nrefs++;
5580 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5583 return NULL;
5586 void
5587 ref_view_free_refs(struct tog_ref_view_state *s)
5589 struct tog_reflist_entry *re;
5591 while (!TAILQ_EMPTY(&s->refs)) {
5592 re = TAILQ_FIRST(&s->refs);
5593 TAILQ_REMOVE(&s->refs, re, entry);
5594 free(re);
5596 got_ref_list_free(&s->simplerefs);
5599 static const struct got_error *
5600 open_ref_view(struct tog_view *view, struct got_repository *repo)
5602 const struct got_error *err = NULL;
5603 struct tog_ref_view_state *s = &view->state.ref;
5605 s->selected_entry = 0;
5606 s->repo = repo;
5608 SIMPLEQ_INIT(&s->simplerefs);
5609 TAILQ_INIT(&s->refs);
5610 SIMPLEQ_INIT(&s->colors);
5612 err = ref_view_load_refs(s);
5613 if (err)
5614 return err;
5616 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5618 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5619 err = add_color(&s->colors, "^refs/heads/",
5620 TOG_COLOR_REFS_HEADS,
5621 get_color_value("TOG_COLOR_REFS_HEADS"));
5622 if (err)
5623 goto done;
5625 err = add_color(&s->colors, "^refs/tags/",
5626 TOG_COLOR_REFS_TAGS,
5627 get_color_value("TOG_COLOR_REFS_TAGS"));
5628 if (err)
5629 goto done;
5631 err = add_color(&s->colors, "^refs/remotes/",
5632 TOG_COLOR_REFS_REMOTES,
5633 get_color_value("TOG_COLOR_REFS_REMOTES"));
5634 if (err)
5635 goto done;
5638 view->show = show_ref_view;
5639 view->input = input_ref_view;
5640 view->close = close_ref_view;
5641 view->search_start = search_start_ref_view;
5642 view->search_next = search_next_ref_view;
5643 done:
5644 if (err)
5645 free_colors(&s->colors);
5646 return err;
5649 static const struct got_error *
5650 close_ref_view(struct tog_view *view)
5652 struct tog_ref_view_state *s = &view->state.ref;
5654 ref_view_free_refs(s);
5655 free_colors(&s->colors);
5657 return NULL;
5660 static const struct got_error *
5661 resolve_reflist_entry(struct got_object_id **commit_id,
5662 struct tog_reflist_entry *re, struct got_repository *repo)
5664 const struct got_error *err = NULL;
5665 struct got_object_id *obj_id;
5666 struct got_tag_object *tag = NULL;
5667 int obj_type;
5669 *commit_id = NULL;
5671 err = got_ref_resolve(&obj_id, repo, re->ref);
5672 if (err)
5673 return err;
5675 err = got_object_get_type(&obj_type, repo, obj_id);
5676 if (err)
5677 goto done;
5679 switch (obj_type) {
5680 case GOT_OBJ_TYPE_COMMIT:
5681 *commit_id = obj_id;
5682 break;
5683 case GOT_OBJ_TYPE_TAG:
5684 err = got_object_open_as_tag(&tag, repo, obj_id);
5685 if (err)
5686 goto done;
5687 free(obj_id);
5688 err = got_object_get_type(&obj_type, repo,
5689 got_object_tag_get_object_id(tag));
5690 if (err)
5691 goto done;
5692 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5693 err = got_error(GOT_ERR_OBJ_TYPE);
5694 goto done;
5696 *commit_id = got_object_id_dup(
5697 got_object_tag_get_object_id(tag));
5698 if (*commit_id == NULL) {
5699 err = got_error_from_errno("got_object_id_dup");
5700 goto done;
5702 break;
5703 default:
5704 err = got_error(GOT_ERR_OBJ_TYPE);
5705 break;
5708 done:
5709 if (tag)
5710 got_object_tag_close(tag);
5711 if (err) {
5712 free(*commit_id);
5713 *commit_id = NULL;
5715 return err;
5718 static const struct got_error *
5719 log_ref_entry(struct tog_view **new_view, int begin_x,
5720 struct tog_reflist_entry *re, struct got_repository *repo)
5722 struct tog_view *log_view;
5723 const struct got_error *err = NULL;
5724 struct got_object_id *commit_id = NULL;
5726 *new_view = NULL;
5728 err = resolve_reflist_entry(&commit_id, re, repo);
5729 if (err) {
5730 if (err->code != GOT_ERR_OBJ_TYPE)
5731 return err;
5732 else
5733 return NULL;
5736 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5737 if (log_view == NULL) {
5738 err = got_error_from_errno("view_open");
5739 goto done;
5742 err = open_log_view(log_view, commit_id, repo,
5743 got_ref_get_name(re->ref), "", 0);
5744 done:
5745 if (err)
5746 view_close(log_view);
5747 else
5748 *new_view = log_view;
5749 free(commit_id);
5750 return err;
5753 static void
5754 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5756 struct tog_reflist_entry *re;
5757 int i = 0;
5759 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5760 return;
5762 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5763 while (i++ < maxscroll) {
5764 if (re == NULL)
5765 break;
5766 s->first_displayed_entry = re;
5767 re = TAILQ_PREV(re, tog_reflist_head, entry);
5771 static void
5772 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5774 struct tog_reflist_entry *next, *last;
5775 int n = 0;
5777 if (s->first_displayed_entry)
5778 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5779 else
5780 next = TAILQ_FIRST(&s->refs);
5782 last = s->last_displayed_entry;
5783 while (next && last && n++ < maxscroll) {
5784 last = TAILQ_NEXT(last, entry);
5785 if (last) {
5786 s->first_displayed_entry = next;
5787 next = TAILQ_NEXT(next, entry);
5792 static const struct got_error *
5793 search_start_ref_view(struct tog_view *view)
5795 struct tog_ref_view_state *s = &view->state.ref;
5797 s->matched_entry = NULL;
5798 return NULL;
5801 static int
5802 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5804 regmatch_t regmatch;
5806 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5807 0) == 0;
5810 static const struct got_error *
5811 search_next_ref_view(struct tog_view *view)
5813 struct tog_ref_view_state *s = &view->state.ref;
5814 struct tog_reflist_entry *re = NULL;
5816 if (!view->searching) {
5817 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5818 return NULL;
5821 if (s->matched_entry) {
5822 if (view->searching == TOG_SEARCH_FORWARD) {
5823 if (s->selected_entry)
5824 re = TAILQ_NEXT(s->selected_entry, entry);
5825 else
5826 re = TAILQ_PREV(s->selected_entry,
5827 tog_reflist_head, entry);
5828 } else {
5829 if (s->selected_entry == NULL)
5830 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5831 else
5832 re = TAILQ_PREV(s->selected_entry,
5833 tog_reflist_head, entry);
5835 } else {
5836 if (view->searching == TOG_SEARCH_FORWARD)
5837 re = TAILQ_FIRST(&s->refs);
5838 else
5839 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5842 while (1) {
5843 if (re == NULL) {
5844 if (s->matched_entry == NULL) {
5845 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5846 return NULL;
5848 if (view->searching == TOG_SEARCH_FORWARD)
5849 re = TAILQ_FIRST(&s->refs);
5850 else
5851 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5854 if (match_reflist_entry(re, &view->regex)) {
5855 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5856 s->matched_entry = re;
5857 break;
5860 if (view->searching == TOG_SEARCH_FORWARD)
5861 re = TAILQ_NEXT(re, entry);
5862 else
5863 re = TAILQ_PREV(re, tog_reflist_head, entry);
5866 if (s->matched_entry) {
5867 s->first_displayed_entry = s->matched_entry;
5868 s->selected = 0;
5871 return NULL;
5874 static const struct got_error *
5875 show_ref_view(struct tog_view *view)
5877 const struct got_error *err = NULL;
5878 struct tog_ref_view_state *s = &view->state.ref;
5879 struct tog_reflist_entry *re;
5880 char *line = NULL;
5881 wchar_t *wline;
5882 struct tog_color *tc;
5883 int width, n;
5884 int limit = view->nlines;
5886 werase(view->window);
5888 s->ndisplayed = 0;
5890 if (limit == 0)
5891 return NULL;
5893 re = s->first_displayed_entry;
5895 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5896 s->nrefs) == -1)
5897 return got_error_from_errno("asprintf");
5899 err = format_line(&wline, &width, line, view->ncols, 0);
5900 if (err) {
5901 free(line);
5902 return err;
5904 if (view_needs_focus_indication(view))
5905 wstandout(view->window);
5906 waddwstr(view->window, wline);
5907 if (view_needs_focus_indication(view))
5908 wstandend(view->window);
5909 free(wline);
5910 wline = NULL;
5911 free(line);
5912 line = NULL;
5913 if (width < view->ncols - 1)
5914 waddch(view->window, '\n');
5915 if (--limit <= 0)
5916 return NULL;
5918 n = 0;
5919 while (re && limit > 0) {
5920 char *line = NULL;
5922 if (got_ref_is_symbolic(re->ref)) {
5923 if (asprintf(&line, "%s -> %s",
5924 got_ref_get_name(re->ref),
5925 got_ref_get_symref_target(re->ref)) == -1)
5926 return got_error_from_errno("asprintf");
5927 } else if (s->show_ids) {
5928 struct got_object_id *id;
5929 char *id_str;
5930 err = got_ref_resolve(&id, s->repo, re->ref);
5931 if (err)
5932 return err;
5933 err = got_object_id_str(&id_str, id);
5934 if (err) {
5935 free(id);
5936 return err;
5938 if (asprintf(&line, "%s: %s",
5939 got_ref_get_name(re->ref), id_str) == -1) {
5940 err = got_error_from_errno("asprintf");
5941 free(id);
5942 free(id_str);
5943 return err;
5945 free(id);
5946 free(id_str);
5947 } else {
5948 line = strdup(got_ref_get_name(re->ref));
5949 if (line == NULL)
5950 return got_error_from_errno("strdup");
5953 err = format_line(&wline, &width, line, view->ncols, 0);
5954 if (err) {
5955 free(line);
5956 return err;
5958 if (n == s->selected) {
5959 if (view->focussed)
5960 wstandout(view->window);
5961 s->selected_entry = re;
5963 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5964 if (tc)
5965 wattr_on(view->window,
5966 COLOR_PAIR(tc->colorpair), NULL);
5967 waddwstr(view->window, wline);
5968 if (tc)
5969 wattr_off(view->window,
5970 COLOR_PAIR(tc->colorpair), NULL);
5971 if (width < view->ncols - 1)
5972 waddch(view->window, '\n');
5973 if (n == s->selected && view->focussed)
5974 wstandend(view->window);
5975 free(line);
5976 free(wline);
5977 wline = NULL;
5978 n++;
5979 s->ndisplayed++;
5980 s->last_displayed_entry = re;
5982 limit--;
5983 re = TAILQ_NEXT(re, entry);
5986 view_vborder(view);
5987 return err;
5990 static const struct got_error *
5991 browse_ref_tree(struct tog_view **new_view, int begin_x,
5992 struct tog_reflist_entry *re, struct got_repository *repo)
5994 const struct got_error *err = NULL;
5995 struct got_object_id *commit_id = NULL, *tree_id = NULL;
5996 struct got_tree_object *tree = NULL;
5997 struct tog_view *tree_view;
5999 *new_view = NULL;
6001 err = resolve_reflist_entry(&commit_id, re, repo);
6002 if (err) {
6003 if (err->code != GOT_ERR_OBJ_TYPE)
6004 return err;
6005 else
6006 return NULL;
6009 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6010 if (err)
6011 goto done;
6013 err = got_object_open_as_tree(&tree, repo, tree_id);
6014 if (err)
6015 goto done;
6017 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6018 if (tree_view == NULL) {
6019 err = got_error_from_errno("view_open");
6020 goto done;
6023 err = open_tree_view(tree_view, tree, commit_id,
6024 got_ref_get_name(re->ref), 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 (view->nlines >= 2 && s->selected >= view->nlines - 1)
6130 s->selected = view->nlines - 2;
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;