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 got_commit_graph *graph, struct commit_queue *commits,
1470 int minqueue, struct got_repository *repo, const char *path,
1471 int *searching, int *search_next_done, regex_t *regex)
1473 const struct got_error *err = NULL;
1474 int nqueued = 0;
1477 * We keep all commits open throughout the lifetime of the log
1478 * view in order to avoid having to re-fetch commits from disk
1479 * while updating the display.
1481 while (nqueued < minqueue ||
1482 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1483 struct got_object_id *id;
1484 struct got_commit_object *commit;
1485 struct commit_queue_entry *entry;
1486 int errcode;
1488 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1489 if (err || id == NULL)
1490 break;
1492 err = got_object_open_as_commit(&commit, repo, id);
1493 if (err)
1494 break;
1495 entry = alloc_commit_queue_entry(commit, id);
1496 if (entry == NULL) {
1497 err = got_error_from_errno("alloc_commit_queue_entry");
1498 break;
1501 errcode = pthread_mutex_lock(&tog_mutex);
1502 if (errcode) {
1503 err = got_error_set_errno(errcode,
1504 "pthread_mutex_lock");
1505 break;
1508 entry->idx = commits->ncommits;
1509 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1510 nqueued++;
1511 commits->ncommits++;
1513 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1514 int have_match;
1515 err = match_commit(&have_match, id, commit, regex);
1516 if (err)
1517 break;
1518 if (have_match)
1519 *search_next_done = TOG_SEARCH_HAVE_MORE;
1522 errcode = pthread_mutex_unlock(&tog_mutex);
1523 if (errcode && err == NULL)
1524 err = got_error_set_errno(errcode,
1525 "pthread_mutex_unlock");
1526 if (err)
1527 break;
1530 return err;
1533 static void
1534 select_commit(struct tog_log_view_state *s)
1536 struct commit_queue_entry *entry;
1537 int ncommits = 0;
1539 entry = s->first_displayed_entry;
1540 while (entry) {
1541 if (ncommits == s->selected) {
1542 s->selected_entry = entry;
1543 break;
1545 entry = TAILQ_NEXT(entry, entry);
1546 ncommits++;
1550 static const struct got_error *
1551 draw_commits(struct tog_view *view)
1553 const struct got_error *err = NULL;
1554 struct tog_log_view_state *s = &view->state.log;
1555 struct commit_queue_entry *entry = s->selected_entry;
1556 const int limit = view->nlines;
1557 int width;
1558 int ncommits, author_cols = 4;
1559 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1560 char *refs_str = NULL;
1561 wchar_t *wline;
1562 struct tog_color *tc;
1563 static const size_t date_display_cols = 12;
1565 if (s->selected_entry &&
1566 !(view->searching && view->search_next_done == 0)) {
1567 err = got_object_id_str(&id_str, s->selected_entry->id);
1568 if (err)
1569 return err;
1570 err = build_refs_str(&refs_str, &s->refs,
1571 s->selected_entry->id, s->repo);
1572 if (err)
1573 goto done;
1576 if (s->thread_args.commits_needed == 0)
1577 halfdelay(10); /* disable fast refresh */
1579 if (s->thread_args.commits_needed > 0) {
1580 if (asprintf(&ncommits_str, " [%d/%d] %s",
1581 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1582 (view->searching && !view->search_next_done) ?
1583 "searching..." : "loading...") == -1) {
1584 err = got_error_from_errno("asprintf");
1585 goto done;
1587 } else {
1588 const char *search_str = NULL;
1590 if (view->searching) {
1591 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1592 search_str = "no more matches";
1593 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1594 search_str = "no matches found";
1595 else if (!view->search_next_done)
1596 search_str = "searching...";
1599 if (asprintf(&ncommits_str, " [%d/%d] %s",
1600 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1601 search_str ? search_str :
1602 (refs_str ? refs_str : "")) == -1) {
1603 err = got_error_from_errno("asprintf");
1604 goto done;
1608 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1609 if (asprintf(&header, "commit %s %s%s",
1610 id_str ? id_str : "........................................",
1611 s->in_repo_path, ncommits_str) == -1) {
1612 err = got_error_from_errno("asprintf");
1613 header = NULL;
1614 goto done;
1616 } else if (asprintf(&header, "commit %s%s",
1617 id_str ? id_str : "........................................",
1618 ncommits_str) == -1) {
1619 err = got_error_from_errno("asprintf");
1620 header = NULL;
1621 goto done;
1623 err = format_line(&wline, &width, header, view->ncols, 0);
1624 if (err)
1625 goto done;
1627 werase(view->window);
1629 if (view_needs_focus_indication(view))
1630 wstandout(view->window);
1631 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1632 if (tc)
1633 wattr_on(view->window,
1634 COLOR_PAIR(tc->colorpair), NULL);
1635 waddwstr(view->window, wline);
1636 if (tc)
1637 wattr_off(view->window,
1638 COLOR_PAIR(tc->colorpair), NULL);
1639 while (width < view->ncols) {
1640 waddch(view->window, ' ');
1641 width++;
1643 if (view_needs_focus_indication(view))
1644 wstandend(view->window);
1645 free(wline);
1646 if (limit <= 1)
1647 goto done;
1649 /* Grow author column size if necessary. */
1650 entry = s->first_displayed_entry;
1651 ncommits = 0;
1652 while (entry) {
1653 char *author;
1654 wchar_t *wauthor;
1655 int width;
1656 if (ncommits >= limit - 1)
1657 break;
1658 author = strdup(got_object_commit_get_author(entry->commit));
1659 if (author == NULL) {
1660 err = got_error_from_errno("strdup");
1661 goto done;
1663 err = format_author(&wauthor, &width, author, COLS,
1664 date_display_cols);
1665 if (author_cols < width)
1666 author_cols = width;
1667 free(wauthor);
1668 free(author);
1669 ncommits++;
1670 entry = TAILQ_NEXT(entry, entry);
1673 entry = s->first_displayed_entry;
1674 s->last_displayed_entry = s->first_displayed_entry;
1675 ncommits = 0;
1676 while (entry) {
1677 if (ncommits >= limit - 1)
1678 break;
1679 if (ncommits == s->selected)
1680 wstandout(view->window);
1681 err = draw_commit(view, entry->commit, entry->id,
1682 date_display_cols, author_cols);
1683 if (ncommits == s->selected)
1684 wstandend(view->window);
1685 if (err)
1686 goto done;
1687 ncommits++;
1688 s->last_displayed_entry = entry;
1689 entry = TAILQ_NEXT(entry, entry);
1692 view_vborder(view);
1693 done:
1694 free(id_str);
1695 free(refs_str);
1696 free(ncommits_str);
1697 free(header);
1698 return err;
1701 static void
1702 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1704 struct commit_queue_entry *entry;
1705 int nscrolled = 0;
1707 entry = TAILQ_FIRST(&s->commits.head);
1708 if (s->first_displayed_entry == entry)
1709 return;
1711 entry = s->first_displayed_entry;
1712 while (entry && nscrolled < maxscroll) {
1713 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1714 if (entry) {
1715 s->first_displayed_entry = entry;
1716 nscrolled++;
1721 static const struct got_error *
1722 trigger_log_thread(struct tog_view *view, int wait)
1724 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1725 int errcode;
1727 halfdelay(1); /* fast refresh while loading commits */
1729 while (ta->commits_needed > 0) {
1730 if (ta->log_complete)
1731 break;
1733 /* Wake the log thread. */
1734 errcode = pthread_cond_signal(&ta->need_commits);
1735 if (errcode)
1736 return got_error_set_errno(errcode,
1737 "pthread_cond_signal");
1740 * The mutex will be released while the view loop waits
1741 * in wgetch(), at which time the log thread will run.
1743 if (!wait)
1744 break;
1746 /* Display progress update in log view. */
1747 show_log_view(view);
1748 update_panels();
1749 doupdate();
1751 /* Wait right here while next commit is being loaded. */
1752 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1753 if (errcode)
1754 return got_error_set_errno(errcode,
1755 "pthread_cond_wait");
1757 /* Display progress update in log view. */
1758 show_log_view(view);
1759 update_panels();
1760 doupdate();
1763 return NULL;
1766 static const struct got_error *
1767 log_scroll_down(struct tog_view *view, int maxscroll)
1769 struct tog_log_view_state *s = &view->state.log;
1770 const struct got_error *err = NULL;
1771 struct commit_queue_entry *pentry;
1772 int nscrolled = 0, ncommits_needed;
1774 if (s->last_displayed_entry == NULL)
1775 return NULL;
1777 ncommits_needed = (s->last_displayed_entry)->idx + 1 + maxscroll;
1778 if (s->commits.ncommits < ncommits_needed &&
1779 !s->thread_args.log_complete) {
1781 * Ask the log thread for required amount of commits.
1783 s->thread_args.commits_needed += maxscroll;
1784 err = trigger_log_thread(view, 1);
1785 if (err)
1786 return err;
1789 do {
1790 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1791 if (pentry == NULL)
1792 break;
1794 s->last_displayed_entry = pentry;
1796 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1797 if (pentry == NULL)
1798 break;
1799 s->first_displayed_entry = pentry;
1800 } while (++nscrolled < maxscroll);
1802 return err;
1805 static const struct got_error *
1806 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1807 struct got_commit_object *commit, struct got_object_id *commit_id,
1808 struct tog_view *log_view, struct got_repository *repo)
1810 const struct got_error *err;
1811 struct got_object_qid *parent_id;
1812 struct tog_view *diff_view;
1814 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1815 if (diff_view == NULL)
1816 return got_error_from_errno("view_open");
1818 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1819 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1820 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1821 if (err == NULL)
1822 *new_view = diff_view;
1823 return err;
1826 static const struct got_error *
1827 tree_view_visit_subtree(struct tog_tree_view_state *s,
1828 struct got_tree_object *subtree)
1830 struct tog_parent_tree *parent;
1832 parent = calloc(1, sizeof(*parent));
1833 if (parent == NULL)
1834 return got_error_from_errno("calloc");
1836 parent->tree = s->tree;
1837 parent->first_displayed_entry = s->first_displayed_entry;
1838 parent->selected_entry = s->selected_entry;
1839 parent->selected = s->selected;
1840 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1841 s->tree = subtree;
1842 s->selected = 0;
1843 s->first_displayed_entry = NULL;
1844 return NULL;
1847 static const struct got_error *
1848 tree_view_walk_path(struct tog_tree_view_state *s,
1849 struct got_object_id *commit_id, const char *path)
1851 const struct got_error *err = NULL;
1852 struct got_tree_object *tree = NULL;
1853 const char *p;
1854 char *slash, *subpath = NULL;
1856 /* Walk the path and open corresponding tree objects. */
1857 p = path;
1858 while (*p) {
1859 struct got_tree_entry *te;
1860 struct got_object_id *tree_id;
1861 char *te_name;
1863 while (p[0] == '/')
1864 p++;
1866 /* Ensure the correct subtree entry is selected. */
1867 slash = strchr(p, '/');
1868 if (slash == NULL)
1869 te_name = strdup(p);
1870 else
1871 te_name = strndup(p, slash - p);
1872 if (te_name == NULL) {
1873 err = got_error_from_errno("strndup");
1874 break;
1876 te = got_object_tree_find_entry(s->tree, te_name);
1877 if (te == NULL) {
1878 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1879 free(te_name);
1880 break;
1882 free(te_name);
1883 s->first_displayed_entry = s->selected_entry = te;
1885 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1886 break; /* jump to this file's entry */
1888 slash = strchr(p, '/');
1889 if (slash)
1890 subpath = strndup(path, slash - path);
1891 else
1892 subpath = strdup(path);
1893 if (subpath == NULL) {
1894 err = got_error_from_errno("strdup");
1895 break;
1898 err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1899 subpath);
1900 if (err)
1901 break;
1903 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1904 free(tree_id);
1905 if (err)
1906 break;
1908 err = tree_view_visit_subtree(s, tree);
1909 if (err) {
1910 got_object_tree_close(tree);
1911 break;
1913 if (slash == NULL)
1914 break;
1915 free(subpath);
1916 subpath = NULL;
1917 p = slash;
1920 free(subpath);
1921 return err;
1924 static const struct got_error *
1925 browse_commit_tree(struct tog_view **new_view, int begin_x,
1926 struct commit_queue_entry *entry, const char *path,
1927 const char *head_ref_name, struct got_repository *repo)
1929 const struct got_error *err = NULL;
1930 struct got_tree_object *tree;
1931 struct tog_tree_view_state *s;
1932 struct tog_view *tree_view;
1934 err = got_object_open_as_tree(&tree, repo,
1935 got_object_commit_get_tree_id(entry->commit));
1936 if (err)
1937 return err;
1939 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1940 if (tree_view == NULL)
1941 return got_error_from_errno("view_open");
1943 err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1944 if (err) {
1945 got_object_tree_close(tree);
1946 return err;
1948 s = &tree_view->state.tree;
1950 *new_view = tree_view;
1952 if (got_path_is_root_dir(path))
1953 return NULL;
1955 return tree_view_walk_path(s, entry->id, path);
1958 static const struct got_error *
1959 block_signals_used_by_main_thread(void)
1961 sigset_t sigset;
1962 int errcode;
1964 if (sigemptyset(&sigset) == -1)
1965 return got_error_from_errno("sigemptyset");
1967 /* tog handles SIGWINCH and SIGCONT */
1968 if (sigaddset(&sigset, SIGWINCH) == -1)
1969 return got_error_from_errno("sigaddset");
1970 if (sigaddset(&sigset, SIGCONT) == -1)
1971 return got_error_from_errno("sigaddset");
1973 /* ncurses handles SIGTSTP */
1974 if (sigaddset(&sigset, SIGTSTP) == -1)
1975 return got_error_from_errno("sigaddset");
1977 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1978 if (errcode)
1979 return got_error_set_errno(errcode, "pthread_sigmask");
1981 return NULL;
1984 static void *
1985 log_thread(void *arg)
1987 const struct got_error *err = NULL;
1988 int errcode = 0;
1989 struct tog_log_thread_args *a = arg;
1990 int done = 0;
1992 err = block_signals_used_by_main_thread();
1993 if (err)
1994 return (void *)err;
1996 while (!done && !err && !tog_sigpipe_received) {
1997 err = queue_commits(a->graph, a->commits, 1, a->repo,
1998 a->in_repo_path, a->searching, a->search_next_done,
1999 a->regex);
2000 if (err) {
2001 if (err->code != GOT_ERR_ITER_COMPLETED)
2002 return (void *)err;
2003 err = NULL;
2004 done = 1;
2005 } else if (a->commits_needed > 0)
2006 a->commits_needed--;
2008 errcode = pthread_mutex_lock(&tog_mutex);
2009 if (errcode) {
2010 err = got_error_set_errno(errcode,
2011 "pthread_mutex_lock");
2012 break;
2013 } else if (*a->quit)
2014 done = 1;
2015 else if (*a->first_displayed_entry == NULL) {
2016 *a->first_displayed_entry =
2017 TAILQ_FIRST(&a->commits->head);
2018 *a->selected_entry = *a->first_displayed_entry;
2021 errcode = pthread_cond_signal(&a->commit_loaded);
2022 if (errcode) {
2023 err = got_error_set_errno(errcode,
2024 "pthread_cond_signal");
2025 pthread_mutex_unlock(&tog_mutex);
2026 break;
2029 if (done)
2030 a->commits_needed = 0;
2031 else {
2032 if (a->commits_needed == 0) {
2033 errcode = pthread_cond_wait(&a->need_commits,
2034 &tog_mutex);
2035 if (errcode)
2036 err = got_error_set_errno(errcode,
2037 "pthread_cond_wait");
2038 if (*a->quit)
2039 done = 1;
2043 errcode = pthread_mutex_unlock(&tog_mutex);
2044 if (errcode && err == NULL)
2045 err = got_error_set_errno(errcode,
2046 "pthread_mutex_unlock");
2048 a->log_complete = 1;
2049 return (void *)err;
2052 static const struct got_error *
2053 stop_log_thread(struct tog_log_view_state *s)
2055 const struct got_error *err = NULL;
2056 int errcode;
2058 if (s->thread) {
2059 s->quit = 1;
2060 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2061 if (errcode)
2062 return got_error_set_errno(errcode,
2063 "pthread_cond_signal");
2064 errcode = pthread_mutex_unlock(&tog_mutex);
2065 if (errcode)
2066 return got_error_set_errno(errcode,
2067 "pthread_mutex_unlock");
2068 errcode = pthread_join(s->thread, (void **)&err);
2069 if (errcode)
2070 return got_error_set_errno(errcode, "pthread_join");
2071 errcode = pthread_mutex_lock(&tog_mutex);
2072 if (errcode)
2073 return got_error_set_errno(errcode,
2074 "pthread_mutex_lock");
2075 s->thread = NULL;
2078 if (s->thread_args.repo) {
2079 got_repo_close(s->thread_args.repo);
2080 s->thread_args.repo = NULL;
2083 if (s->thread_args.graph) {
2084 got_commit_graph_close(s->thread_args.graph);
2085 s->thread_args.graph = NULL;
2088 return err;
2091 static const struct got_error *
2092 close_log_view(struct tog_view *view)
2094 const struct got_error *err = NULL;
2095 struct tog_log_view_state *s = &view->state.log;
2096 int errcode;
2098 err = stop_log_thread(s);
2100 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2101 if (errcode && err == NULL)
2102 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2104 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2105 if (errcode && err == NULL)
2106 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2108 free_commits(&s->commits);
2109 free(s->in_repo_path);
2110 s->in_repo_path = NULL;
2111 free(s->start_id);
2112 s->start_id = NULL;
2113 got_ref_list_free(&s->refs);
2114 return err;
2117 static const struct got_error *
2118 search_start_log_view(struct tog_view *view)
2120 struct tog_log_view_state *s = &view->state.log;
2122 s->matched_entry = NULL;
2123 s->search_entry = NULL;
2124 return NULL;
2127 static const struct got_error *
2128 search_next_log_view(struct tog_view *view)
2130 const struct got_error *err = NULL;
2131 struct tog_log_view_state *s = &view->state.log;
2132 struct commit_queue_entry *entry;
2134 /* Display progress update in log view. */
2135 show_log_view(view);
2136 update_panels();
2137 doupdate();
2139 if (s->search_entry) {
2140 int errcode, ch;
2141 errcode = pthread_mutex_unlock(&tog_mutex);
2142 if (errcode)
2143 return got_error_set_errno(errcode,
2144 "pthread_mutex_unlock");
2145 ch = wgetch(view->window);
2146 errcode = pthread_mutex_lock(&tog_mutex);
2147 if (errcode)
2148 return got_error_set_errno(errcode,
2149 "pthread_mutex_lock");
2150 if (ch == KEY_BACKSPACE) {
2151 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2152 return NULL;
2154 if (view->searching == TOG_SEARCH_FORWARD)
2155 entry = TAILQ_NEXT(s->search_entry, entry);
2156 else
2157 entry = TAILQ_PREV(s->search_entry,
2158 commit_queue_head, entry);
2159 } else if (s->matched_entry) {
2160 if (view->searching == TOG_SEARCH_FORWARD)
2161 entry = TAILQ_NEXT(s->matched_entry, entry);
2162 else
2163 entry = TAILQ_PREV(s->matched_entry,
2164 commit_queue_head, entry);
2165 } else {
2166 if (view->searching == TOG_SEARCH_FORWARD)
2167 entry = TAILQ_FIRST(&s->commits.head);
2168 else
2169 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2172 while (1) {
2173 int have_match = 0;
2175 if (entry == NULL) {
2176 if (s->thread_args.log_complete ||
2177 view->searching == TOG_SEARCH_BACKWARD) {
2178 view->search_next_done =
2179 (s->matched_entry == NULL ?
2180 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2181 s->search_entry = NULL;
2182 return NULL;
2185 * Poke the log thread for more commits and return,
2186 * allowing the main loop to make progress. Search
2187 * will resume at s->search_entry once we come back.
2189 s->thread_args.commits_needed++;
2190 return trigger_log_thread(view, 0);
2193 err = match_commit(&have_match, entry->id, entry->commit,
2194 &view->regex);
2195 if (err)
2196 break;
2197 if (have_match) {
2198 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2199 s->matched_entry = entry;
2200 break;
2203 s->search_entry = entry;
2204 if (view->searching == TOG_SEARCH_FORWARD)
2205 entry = TAILQ_NEXT(entry, entry);
2206 else
2207 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2210 if (s->matched_entry) {
2211 int cur = s->selected_entry->idx;
2212 while (cur < s->matched_entry->idx) {
2213 err = input_log_view(NULL, view, KEY_DOWN);
2214 if (err)
2215 return err;
2216 cur++;
2218 while (cur > s->matched_entry->idx) {
2219 err = input_log_view(NULL, view, KEY_UP);
2220 if (err)
2221 return err;
2222 cur--;
2226 s->search_entry = NULL;
2228 return NULL;
2231 static const struct got_error *
2232 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2233 struct got_repository *repo, const char *head_ref_name,
2234 const char *in_repo_path, int log_branches)
2236 const struct got_error *err = NULL;
2237 struct tog_log_view_state *s = &view->state.log;
2238 struct got_repository *thread_repo = NULL;
2239 struct got_commit_graph *thread_graph = NULL;
2240 int errcode;
2242 SIMPLEQ_INIT(&s->refs);
2244 if (in_repo_path != s->in_repo_path) {
2245 free(s->in_repo_path);
2246 s->in_repo_path = strdup(in_repo_path);
2247 if (s->in_repo_path == NULL)
2248 return got_error_from_errno("strdup");
2251 /* The commit queue only contains commits being displayed. */
2252 TAILQ_INIT(&s->commits.head);
2253 s->commits.ncommits = 0;
2255 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2256 if (err)
2257 goto done;
2259 s->repo = repo;
2260 s->head_ref_name = head_ref_name;
2261 s->start_id = got_object_id_dup(start_id);
2262 if (s->start_id == NULL) {
2263 err = got_error_from_errno("got_object_id_dup");
2264 goto done;
2266 s->log_branches = log_branches;
2268 SIMPLEQ_INIT(&s->colors);
2269 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2270 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2271 get_color_value("TOG_COLOR_COMMIT"));
2272 if (err)
2273 goto done;
2274 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2275 get_color_value("TOG_COLOR_AUTHOR"));
2276 if (err) {
2277 free_colors(&s->colors);
2278 goto done;
2280 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2281 get_color_value("TOG_COLOR_DATE"));
2282 if (err) {
2283 free_colors(&s->colors);
2284 goto done;
2288 view->show = show_log_view;
2289 view->input = input_log_view;
2290 view->close = close_log_view;
2291 view->search_start = search_start_log_view;
2292 view->search_next = search_next_log_view;
2294 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2295 if (err)
2296 goto done;
2297 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2298 !s->log_branches);
2299 if (err)
2300 goto done;
2301 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2302 s->repo, NULL, NULL);
2303 if (err)
2304 goto done;
2306 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2307 if (errcode) {
2308 err = got_error_set_errno(errcode, "pthread_cond_init");
2309 goto done;
2311 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2312 if (errcode) {
2313 err = got_error_set_errno(errcode, "pthread_cond_init");
2314 goto done;
2317 s->thread_args.commits_needed = view->nlines;
2318 s->thread_args.graph = thread_graph;
2319 s->thread_args.commits = &s->commits;
2320 s->thread_args.in_repo_path = s->in_repo_path;
2321 s->thread_args.start_id = s->start_id;
2322 s->thread_args.repo = thread_repo;
2323 s->thread_args.log_complete = 0;
2324 s->thread_args.quit = &s->quit;
2325 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2326 s->thread_args.selected_entry = &s->selected_entry;
2327 s->thread_args.searching = &view->searching;
2328 s->thread_args.search_next_done = &view->search_next_done;
2329 s->thread_args.regex = &view->regex;
2330 done:
2331 if (err)
2332 close_log_view(view);
2333 return err;
2336 static const struct got_error *
2337 show_log_view(struct tog_view *view)
2339 const struct got_error *err;
2340 struct tog_log_view_state *s = &view->state.log;
2342 if (s->thread == NULL) {
2343 int errcode = pthread_create(&s->thread, NULL, log_thread,
2344 &s->thread_args);
2345 if (errcode)
2346 return got_error_set_errno(errcode, "pthread_create");
2347 if (s->thread_args.commits_needed > 0) {
2348 err = trigger_log_thread(view, 1);
2349 if (err)
2350 return err;
2354 return draw_commits(view);
2357 static const struct got_error *
2358 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2360 const struct got_error *err = NULL;
2361 struct tog_log_view_state *s = &view->state.log;
2362 struct tog_view *diff_view = NULL, *tree_view = NULL;
2363 struct tog_view *ref_view = NULL;
2364 int begin_x = 0;
2366 switch (ch) {
2367 case 'q':
2368 s->quit = 1;
2369 break;
2370 case 'k':
2371 case KEY_UP:
2372 case '<':
2373 case ',':
2374 if (s->first_displayed_entry == NULL)
2375 break;
2376 if (s->selected > 0)
2377 s->selected--;
2378 else
2379 log_scroll_up(s, 1);
2380 select_commit(s);
2381 break;
2382 case KEY_PPAGE:
2383 case CTRL('b'):
2384 if (s->first_displayed_entry == NULL)
2385 break;
2386 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2387 s->selected = 0;
2388 else
2389 log_scroll_up(s, view->nlines - 1);
2390 select_commit(s);
2391 break;
2392 case 'j':
2393 case KEY_DOWN:
2394 case '>':
2395 case '.':
2396 if (s->first_displayed_entry == NULL)
2397 break;
2398 if (s->selected < MIN(view->nlines - 2,
2399 s->commits.ncommits - 1))
2400 s->selected++;
2401 else {
2402 err = log_scroll_down(view, 1);
2403 if (err)
2404 break;
2406 select_commit(s);
2407 break;
2408 case KEY_NPAGE:
2409 case CTRL('f'): {
2410 struct commit_queue_entry *first;
2411 first = s->first_displayed_entry;
2412 if (first == NULL)
2413 break;
2414 err = log_scroll_down(view, view->nlines - 1);
2415 if (err)
2416 break;
2417 if (first == s->first_displayed_entry &&
2418 s->selected < MIN(view->nlines - 2,
2419 s->commits.ncommits - 1)) {
2420 /* can't scroll further down */
2421 s->selected = MIN(view->nlines - 2,
2422 s->commits.ncommits - 1);
2424 select_commit(s);
2425 break;
2427 case KEY_RESIZE:
2428 if (s->selected > view->nlines - 2)
2429 s->selected = view->nlines - 2;
2430 if (s->selected > s->commits.ncommits - 1)
2431 s->selected = s->commits.ncommits - 1;
2432 select_commit(s);
2433 if (s->commits.ncommits < view->nlines - 1 &&
2434 !s->thread_args.log_complete) {
2435 s->thread_args.commits_needed += (view->nlines - 1) -
2436 s->commits.ncommits;
2437 err = trigger_log_thread(view, 1);
2439 break;
2440 case KEY_ENTER:
2441 case ' ':
2442 case '\r':
2443 if (s->selected_entry == NULL)
2444 break;
2445 if (view_is_parent_view(view))
2446 begin_x = view_split_begin_x(view->begin_x);
2447 err = open_diff_view_for_commit(&diff_view, begin_x,
2448 s->selected_entry->commit, s->selected_entry->id,
2449 view, s->repo);
2450 if (err)
2451 break;
2452 view->focussed = 0;
2453 diff_view->focussed = 1;
2454 if (view_is_parent_view(view)) {
2455 err = view_close_child(view);
2456 if (err)
2457 return err;
2458 view_set_child(view, diff_view);
2459 view->focus_child = 1;
2460 } else
2461 *new_view = diff_view;
2462 break;
2463 case 't':
2464 if (s->selected_entry == NULL)
2465 break;
2466 if (view_is_parent_view(view))
2467 begin_x = view_split_begin_x(view->begin_x);
2468 err = browse_commit_tree(&tree_view, begin_x,
2469 s->selected_entry, s->in_repo_path, s->head_ref_name,
2470 s->repo);
2471 if (err)
2472 break;
2473 view->focussed = 0;
2474 tree_view->focussed = 1;
2475 if (view_is_parent_view(view)) {
2476 err = view_close_child(view);
2477 if (err)
2478 return err;
2479 view_set_child(view, tree_view);
2480 view->focus_child = 1;
2481 } else
2482 *new_view = tree_view;
2483 break;
2484 case KEY_BACKSPACE:
2485 case CTRL('l'):
2486 case 'B':
2487 if (ch == KEY_BACKSPACE &&
2488 got_path_is_root_dir(s->in_repo_path))
2489 break;
2490 err = stop_log_thread(s);
2491 if (err)
2492 return err;
2493 if (ch == KEY_BACKSPACE) {
2494 char *parent_path;
2495 err = got_path_dirname(&parent_path, s->in_repo_path);
2496 if (err)
2497 return err;
2498 free(s->in_repo_path);
2499 s->in_repo_path = parent_path;
2500 s->thread_args.in_repo_path = s->in_repo_path;
2501 } else if (ch == CTRL('l')) {
2502 struct got_object_id *start_id;
2503 err = got_repo_match_object_id(&start_id, NULL,
2504 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2505 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2506 if (err)
2507 return err;
2508 free(s->start_id);
2509 s->start_id = start_id;
2510 s->thread_args.start_id = s->start_id;
2511 } else /* 'B' */
2512 s->log_branches = !s->log_branches;
2514 err = got_repo_open(&s->thread_args.repo,
2515 got_repo_get_path(s->repo), NULL);
2516 if (err)
2517 return err;
2518 err = got_commit_graph_open(&s->thread_args.graph,
2519 s->in_repo_path, !s->log_branches);
2520 if (err)
2521 return err;
2522 err = got_commit_graph_iter_start(s->thread_args.graph,
2523 s->start_id, s->repo, NULL, NULL);
2524 if (err)
2525 return err;
2526 free_commits(&s->commits);
2527 s->first_displayed_entry = NULL;
2528 s->last_displayed_entry = NULL;
2529 s->selected_entry = NULL;
2530 s->selected = 0;
2531 s->thread_args.log_complete = 0;
2532 s->quit = 0;
2533 s->thread_args.commits_needed = view->nlines;
2534 break;
2535 case 'r':
2536 if (view_is_parent_view(view))
2537 begin_x = view_split_begin_x(view->begin_x);
2538 ref_view = view_open(view->nlines, view->ncols,
2539 view->begin_y, begin_x, TOG_VIEW_REF);
2540 if (ref_view == NULL)
2541 return got_error_from_errno("view_open");
2542 err = open_ref_view(ref_view, s->repo);
2543 if (err) {
2544 view_close(ref_view);
2545 return err;
2547 view->focussed = 0;
2548 ref_view->focussed = 1;
2549 if (view_is_parent_view(view)) {
2550 err = view_close_child(view);
2551 if (err)
2552 return err;
2553 view_set_child(view, ref_view);
2554 view->focus_child = 1;
2555 } else
2556 *new_view = ref_view;
2557 break;
2558 default:
2559 break;
2562 return err;
2565 static const struct got_error *
2566 apply_unveil(const char *repo_path, const char *worktree_path)
2568 const struct got_error *error;
2570 #ifdef PROFILE
2571 if (unveil("gmon.out", "rwc") != 0)
2572 return got_error_from_errno2("unveil", "gmon.out");
2573 #endif
2574 if (repo_path && unveil(repo_path, "r") != 0)
2575 return got_error_from_errno2("unveil", repo_path);
2577 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2578 return got_error_from_errno2("unveil", worktree_path);
2580 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2581 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2583 error = got_privsep_unveil_exec_helpers();
2584 if (error != NULL)
2585 return error;
2587 if (unveil(NULL, NULL) != 0)
2588 return got_error_from_errno("unveil");
2590 return NULL;
2593 static void
2594 init_curses(void)
2596 initscr();
2597 cbreak();
2598 halfdelay(1); /* Do fast refresh while initial view is loading. */
2599 noecho();
2600 nonl();
2601 intrflush(stdscr, FALSE);
2602 keypad(stdscr, TRUE);
2603 curs_set(0);
2604 if (getenv("TOG_COLORS") != NULL) {
2605 start_color();
2606 use_default_colors();
2608 signal(SIGWINCH, tog_sigwinch);
2609 signal(SIGPIPE, tog_sigpipe);
2610 signal(SIGCONT, tog_sigcont);
2613 static const struct got_error *
2614 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2615 struct got_repository *repo, struct got_worktree *worktree)
2617 const struct got_error *err = NULL;
2619 if (argc == 0) {
2620 *in_repo_path = strdup("/");
2621 if (*in_repo_path == NULL)
2622 return got_error_from_errno("strdup");
2623 return NULL;
2626 if (worktree) {
2627 const char *prefix = got_worktree_get_path_prefix(worktree);
2628 char *p;
2630 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2631 if (err)
2632 return err;
2633 if (asprintf(in_repo_path, "%s%s%s", prefix,
2634 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2635 p) == -1) {
2636 err = got_error_from_errno("asprintf");
2637 *in_repo_path = NULL;
2639 free(p);
2640 } else
2641 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2643 return err;
2646 static const struct got_error *
2647 cmd_log(int argc, char *argv[])
2649 const struct got_error *error;
2650 struct got_repository *repo = NULL;
2651 struct got_worktree *worktree = NULL;
2652 struct got_object_id *start_id = NULL;
2653 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2654 char *start_commit = NULL, *label = NULL;
2655 struct got_reference *ref = NULL;
2656 const char *head_ref_name = NULL;
2657 int ch, log_branches = 0;
2658 struct tog_view *view;
2660 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2661 switch (ch) {
2662 case 'b':
2663 log_branches = 1;
2664 break;
2665 case 'c':
2666 start_commit = optarg;
2667 break;
2668 case 'r':
2669 repo_path = realpath(optarg, NULL);
2670 if (repo_path == NULL)
2671 return got_error_from_errno2("realpath",
2672 optarg);
2673 break;
2674 default:
2675 usage_log();
2676 /* NOTREACHED */
2680 argc -= optind;
2681 argv += optind;
2683 if (argc > 1)
2684 usage_log();
2686 cwd = getcwd(NULL, 0);
2687 if (cwd == NULL)
2688 return got_error_from_errno("getcwd");
2690 error = got_worktree_open(&worktree, cwd);
2691 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2692 goto done;
2694 if (repo_path == NULL) {
2695 if (worktree)
2696 repo_path =
2697 strdup(got_worktree_get_repo_path(worktree));
2698 else
2699 repo_path = strdup(cwd);
2701 if (repo_path == NULL) {
2702 error = got_error_from_errno("strdup");
2703 goto done;
2706 error = got_repo_open(&repo, repo_path, NULL);
2707 if (error != NULL)
2708 goto done;
2710 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2711 repo, worktree);
2712 if (error)
2713 goto done;
2715 init_curses();
2717 error = apply_unveil(got_repo_get_path(repo),
2718 worktree ? got_worktree_get_root_path(worktree) : NULL);
2719 if (error)
2720 goto done;
2722 if (start_commit == NULL) {
2723 error = got_repo_match_object_id(&start_id, &label,
2724 worktree ? got_worktree_get_head_ref_name(worktree) :
2725 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2726 if (error)
2727 goto done;
2728 head_ref_name = label;
2729 } else {
2730 error = got_ref_open(&ref, repo, start_commit, 0);
2731 if (error == NULL)
2732 head_ref_name = got_ref_get_name(ref);
2733 else if (error->code != GOT_ERR_NOT_REF)
2734 goto done;
2735 error = got_repo_match_object_id(&start_id, NULL,
2736 start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2737 if (error)
2738 goto done;
2741 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2742 if (view == NULL) {
2743 error = got_error_from_errno("view_open");
2744 goto done;
2746 error = open_log_view(view, start_id, repo, head_ref_name,
2747 in_repo_path, log_branches);
2748 if (error)
2749 goto done;
2750 if (worktree) {
2751 /* Release work tree lock. */
2752 got_worktree_close(worktree);
2753 worktree = NULL;
2755 error = view_loop(view);
2756 done:
2757 free(in_repo_path);
2758 free(repo_path);
2759 free(cwd);
2760 free(start_id);
2761 free(label);
2762 if (ref)
2763 got_ref_close(ref);
2764 if (repo)
2765 got_repo_close(repo);
2766 if (worktree)
2767 got_worktree_close(worktree);
2768 return error;
2771 __dead static void
2772 usage_diff(void)
2774 endwin();
2775 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2776 "[-w] object1 object2\n", getprogname());
2777 exit(1);
2780 static char *
2781 parse_next_line(FILE *f, size_t *len)
2783 char *line;
2784 size_t linelen;
2785 size_t lineno;
2786 const char delim[3] = { '\0', '\0', '\0'};
2788 line = fparseln(f, &linelen, &lineno, delim, 0);
2789 if (len)
2790 *len = linelen;
2791 return line;
2794 static int
2795 match_line(const char *line, regex_t *regex, size_t nmatch,
2796 regmatch_t *regmatch)
2798 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2801 struct tog_color *
2802 match_color(struct tog_colors *colors, const char *line)
2804 struct tog_color *tc = NULL;
2806 SIMPLEQ_FOREACH(tc, colors, entry) {
2807 if (match_line(line, &tc->regex, 0, NULL))
2808 return tc;
2811 return NULL;
2814 static const struct got_error *
2815 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2816 WINDOW *window, regmatch_t *regmatch)
2818 const struct got_error *err = NULL;
2819 wchar_t *wline;
2820 int width;
2821 char *s;
2823 *wtotal = 0;
2825 s = strndup(line, regmatch->rm_so);
2826 if (s == NULL)
2827 return got_error_from_errno("strndup");
2829 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2830 if (err) {
2831 free(s);
2832 return err;
2834 waddwstr(window, wline);
2835 free(wline);
2836 free(s);
2837 wlimit -= width;
2838 *wtotal += width;
2840 if (wlimit > 0) {
2841 s = strndup(line + regmatch->rm_so,
2842 regmatch->rm_eo - regmatch->rm_so);
2843 if (s == NULL) {
2844 err = got_error_from_errno("strndup");
2845 free(s);
2846 return err;
2848 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2849 if (err) {
2850 free(s);
2851 return err;
2853 wattr_on(window, A_STANDOUT, NULL);
2854 waddwstr(window, wline);
2855 wattr_off(window, A_STANDOUT, NULL);
2856 free(wline);
2857 free(s);
2858 wlimit -= width;
2859 *wtotal += width;
2862 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2863 err = format_line(&wline, &width,
2864 line + regmatch->rm_eo, wlimit, col_tab_align);
2865 if (err)
2866 return err;
2867 waddwstr(window, wline);
2868 free(wline);
2869 *wtotal += width;
2872 return NULL;
2875 static const struct got_error *
2876 draw_file(struct tog_view *view, const char *header)
2878 struct tog_diff_view_state *s = &view->state.diff;
2879 regmatch_t *regmatch = &view->regmatch;
2880 const struct got_error *err;
2881 int nprinted = 0;
2882 char *line;
2883 struct tog_color *tc;
2884 size_t len;
2885 wchar_t *wline;
2886 int width;
2887 int max_lines = view->nlines;
2888 int nlines = s->nlines;
2889 off_t line_offset;
2891 line_offset = s->line_offsets[s->first_displayed_line - 1];
2892 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2893 return got_error_from_errno("fseek");
2895 werase(view->window);
2897 if (header) {
2898 if (asprintf(&line, "[%d/%d] %s",
2899 s->first_displayed_line - 1 + s->selected_line, nlines,
2900 header) == -1)
2901 return got_error_from_errno("asprintf");
2902 err = format_line(&wline, &width, line, view->ncols, 0);
2903 free(line);
2904 if (err)
2905 return err;
2907 if (view_needs_focus_indication(view))
2908 wstandout(view->window);
2909 waddwstr(view->window, wline);
2910 free(wline);
2911 wline = NULL;
2912 if (view_needs_focus_indication(view))
2913 wstandend(view->window);
2914 if (width <= view->ncols - 1)
2915 waddch(view->window, '\n');
2917 if (max_lines <= 1)
2918 return NULL;
2919 max_lines--;
2922 s->eof = 0;
2923 while (max_lines > 0 && nprinted < max_lines) {
2924 line = parse_next_line(s->f, &len);
2925 if (line == NULL) {
2926 s->eof = 1;
2927 break;
2930 tc = match_color(&s->colors, line);
2931 if (tc)
2932 wattr_on(view->window,
2933 COLOR_PAIR(tc->colorpair), NULL);
2934 if (s->first_displayed_line + nprinted == s->matched_line &&
2935 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2936 err = add_matched_line(&width, line, view->ncols, 0,
2937 view->window, regmatch);
2938 if (err) {
2939 free(line);
2940 return err;
2942 } else {
2943 err = format_line(&wline, &width, line, view->ncols, 0);
2944 if (err) {
2945 free(line);
2946 return err;
2948 waddwstr(view->window, wline);
2949 free(wline);
2950 wline = NULL;
2952 if (tc)
2953 wattr_off(view->window,
2954 COLOR_PAIR(tc->colorpair), NULL);
2955 if (width <= view->ncols - 1)
2956 waddch(view->window, '\n');
2957 nprinted++;
2958 free(line);
2960 if (nprinted >= 1)
2961 s->last_displayed_line = s->first_displayed_line +
2962 (nprinted - 1);
2963 else
2964 s->last_displayed_line = s->first_displayed_line;
2966 view_vborder(view);
2968 if (s->eof) {
2969 while (nprinted < view->nlines) {
2970 waddch(view->window, '\n');
2971 nprinted++;
2974 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2975 if (err) {
2976 return err;
2979 wstandout(view->window);
2980 waddwstr(view->window, wline);
2981 free(wline);
2982 wline = NULL;
2983 wstandend(view->window);
2986 return NULL;
2989 static char *
2990 get_datestr(time_t *time, char *datebuf)
2992 struct tm mytm, *tm;
2993 char *p, *s;
2995 tm = gmtime_r(time, &mytm);
2996 if (tm == NULL)
2997 return NULL;
2998 s = asctime_r(tm, datebuf);
2999 if (s == NULL)
3000 return NULL;
3001 p = strchr(s, '\n');
3002 if (p)
3003 *p = '\0';
3004 return s;
3007 static const struct got_error *
3008 get_changed_paths(struct got_pathlist_head *paths,
3009 struct got_commit_object *commit, struct got_repository *repo)
3011 const struct got_error *err = NULL;
3012 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3013 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3014 struct got_object_qid *qid;
3016 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3017 if (qid != NULL) {
3018 struct got_commit_object *pcommit;
3019 err = got_object_open_as_commit(&pcommit, repo,
3020 qid->id);
3021 if (err)
3022 return err;
3024 tree_id1 = got_object_commit_get_tree_id(pcommit);
3025 got_object_commit_close(pcommit);
3029 if (tree_id1) {
3030 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3031 if (err)
3032 goto done;
3035 tree_id2 = got_object_commit_get_tree_id(commit);
3036 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3037 if (err)
3038 goto done;
3040 err = got_diff_tree(tree1, tree2, "", "", repo,
3041 got_diff_tree_collect_changed_paths, paths, 0);
3042 done:
3043 if (tree1)
3044 got_object_tree_close(tree1);
3045 if (tree2)
3046 got_object_tree_close(tree2);
3047 return err;
3050 static const struct got_error *
3051 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3053 off_t *p;
3055 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3056 if (p == NULL)
3057 return got_error_from_errno("reallocarray");
3058 *line_offsets = p;
3059 (*line_offsets)[*nlines] = off;
3060 (*nlines)++;
3061 return NULL;
3064 static const struct got_error *
3065 write_commit_info(off_t **line_offsets, size_t *nlines,
3066 struct got_object_id *commit_id, struct got_reflist_head *refs,
3067 struct got_repository *repo, FILE *outfile)
3069 const struct got_error *err = NULL;
3070 char datebuf[26], *datestr;
3071 struct got_commit_object *commit;
3072 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3073 time_t committer_time;
3074 const char *author, *committer;
3075 char *refs_str = NULL;
3076 struct got_pathlist_head changed_paths;
3077 struct got_pathlist_entry *pe;
3078 off_t outoff = 0;
3079 int n;
3081 TAILQ_INIT(&changed_paths);
3083 if (refs) {
3084 err = build_refs_str(&refs_str, refs, commit_id, repo);
3085 if (err)
3086 return err;
3089 err = got_object_open_as_commit(&commit, repo, commit_id);
3090 if (err)
3091 return err;
3093 err = got_object_id_str(&id_str, commit_id);
3094 if (err) {
3095 err = got_error_from_errno("got_object_id_str");
3096 goto done;
3099 err = add_line_offset(line_offsets, nlines, 0);
3100 if (err)
3101 goto done;
3103 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3104 refs_str ? refs_str : "", refs_str ? ")" : "");
3105 if (n < 0) {
3106 err = got_error_from_errno("fprintf");
3107 goto done;
3109 outoff += n;
3110 err = add_line_offset(line_offsets, nlines, outoff);
3111 if (err)
3112 goto done;
3114 n = fprintf(outfile, "from: %s\n",
3115 got_object_commit_get_author(commit));
3116 if (n < 0) {
3117 err = got_error_from_errno("fprintf");
3118 goto done;
3120 outoff += n;
3121 err = add_line_offset(line_offsets, nlines, outoff);
3122 if (err)
3123 goto done;
3125 committer_time = got_object_commit_get_committer_time(commit);
3126 datestr = get_datestr(&committer_time, datebuf);
3127 if (datestr) {
3128 n = fprintf(outfile, "date: %s UTC\n", datestr);
3129 if (n < 0) {
3130 err = got_error_from_errno("fprintf");
3131 goto done;
3133 outoff += n;
3134 err = add_line_offset(line_offsets, nlines, outoff);
3135 if (err)
3136 goto done;
3138 author = got_object_commit_get_author(commit);
3139 committer = got_object_commit_get_committer(commit);
3140 if (strcmp(author, committer) != 0) {
3141 n = fprintf(outfile, "via: %s\n", committer);
3142 if (n < 0) {
3143 err = got_error_from_errno("fprintf");
3144 goto done;
3146 outoff += n;
3147 err = add_line_offset(line_offsets, nlines, outoff);
3148 if (err)
3149 goto done;
3151 err = got_object_commit_get_logmsg(&logmsg, commit);
3152 if (err)
3153 goto done;
3154 s = logmsg;
3155 while ((line = strsep(&s, "\n")) != NULL) {
3156 n = fprintf(outfile, "%s\n", line);
3157 if (n < 0) {
3158 err = got_error_from_errno("fprintf");
3159 goto done;
3161 outoff += n;
3162 err = add_line_offset(line_offsets, nlines, outoff);
3163 if (err)
3164 goto done;
3167 err = get_changed_paths(&changed_paths, commit, repo);
3168 if (err)
3169 goto done;
3170 TAILQ_FOREACH(pe, &changed_paths, entry) {
3171 struct got_diff_changed_path *cp = pe->data;
3172 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3173 if (n < 0) {
3174 err = got_error_from_errno("fprintf");
3175 goto done;
3177 outoff += n;
3178 err = add_line_offset(line_offsets, nlines, outoff);
3179 if (err)
3180 goto done;
3181 free((char *)pe->path);
3182 free(pe->data);
3185 fputc('\n', outfile);
3186 outoff++;
3187 err = add_line_offset(line_offsets, nlines, outoff);
3188 done:
3189 got_pathlist_free(&changed_paths);
3190 free(id_str);
3191 free(logmsg);
3192 free(refs_str);
3193 got_object_commit_close(commit);
3194 if (err) {
3195 free(*line_offsets);
3196 *line_offsets = NULL;
3197 *nlines = 0;
3199 return err;
3202 static const struct got_error *
3203 create_diff(struct tog_diff_view_state *s)
3205 const struct got_error *err = NULL;
3206 FILE *f = NULL;
3207 int obj_type;
3209 free(s->line_offsets);
3210 s->line_offsets = malloc(sizeof(off_t));
3211 if (s->line_offsets == NULL)
3212 return got_error_from_errno("malloc");
3213 s->nlines = 0;
3215 f = got_opentemp();
3216 if (f == NULL) {
3217 err = got_error_from_errno("got_opentemp");
3218 goto done;
3220 if (s->f && fclose(s->f) != 0) {
3221 err = got_error_from_errno("fclose");
3222 goto done;
3224 s->f = f;
3226 if (s->id1)
3227 err = got_object_get_type(&obj_type, s->repo, s->id1);
3228 else
3229 err = got_object_get_type(&obj_type, s->repo, s->id2);
3230 if (err)
3231 goto done;
3233 switch (obj_type) {
3234 case GOT_OBJ_TYPE_BLOB:
3235 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3236 s->id1, s->id2, s->label1, s->label2, s->diff_context,
3237 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3238 break;
3239 case GOT_OBJ_TYPE_TREE:
3240 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3241 s->id1, s->id2, "", "", s->diff_context,
3242 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3243 break;
3244 case GOT_OBJ_TYPE_COMMIT: {
3245 const struct got_object_id_queue *parent_ids;
3246 struct got_object_qid *pid;
3247 struct got_commit_object *commit2;
3249 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3250 if (err)
3251 goto done;
3252 /* Show commit info if we're diffing to a parent/root commit. */
3253 if (s->id1 == NULL) {
3254 err = write_commit_info(&s->line_offsets, &s->nlines,
3255 s->id2, &s->refs, s->repo, s->f);
3256 if (err)
3257 goto done;
3258 } else {
3259 parent_ids = got_object_commit_get_parent_ids(commit2);
3260 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3261 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3262 err = write_commit_info(
3263 &s->line_offsets, &s->nlines,
3264 s->id2, &s->refs, s->repo, s->f);
3265 if (err)
3266 goto done;
3267 break;
3271 got_object_commit_close(commit2);
3273 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3274 s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3275 s->force_text_diff, s->repo, s->f);
3276 break;
3278 default:
3279 err = got_error(GOT_ERR_OBJ_TYPE);
3280 break;
3282 if (err)
3283 goto done;
3284 done:
3285 if (s->f && fflush(s->f) != 0 && err == NULL)
3286 err = got_error_from_errno("fflush");
3287 return err;
3290 static void
3291 diff_view_indicate_progress(struct tog_view *view)
3293 mvwaddstr(view->window, 0, 0, "diffing...");
3294 update_panels();
3295 doupdate();
3298 static const struct got_error *
3299 search_start_diff_view(struct tog_view *view)
3301 struct tog_diff_view_state *s = &view->state.diff;
3303 s->matched_line = 0;
3304 return NULL;
3307 static const struct got_error *
3308 search_next_diff_view(struct tog_view *view)
3310 struct tog_diff_view_state *s = &view->state.diff;
3311 int lineno;
3313 if (!view->searching) {
3314 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3315 return NULL;
3318 if (s->matched_line) {
3319 if (view->searching == TOG_SEARCH_FORWARD)
3320 lineno = s->matched_line + 1;
3321 else
3322 lineno = s->matched_line - 1;
3323 } else {
3324 if (view->searching == TOG_SEARCH_FORWARD)
3325 lineno = 1;
3326 else
3327 lineno = s->nlines;
3330 while (1) {
3331 char *line = NULL;
3332 off_t offset;
3333 size_t len;
3335 if (lineno <= 0 || lineno > s->nlines) {
3336 if (s->matched_line == 0) {
3337 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3338 free(line);
3339 break;
3342 if (view->searching == TOG_SEARCH_FORWARD)
3343 lineno = 1;
3344 else
3345 lineno = s->nlines;
3348 offset = s->line_offsets[lineno - 1];
3349 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3350 free(line);
3351 return got_error_from_errno("fseeko");
3353 free(line);
3354 line = parse_next_line(s->f, &len);
3355 if (line &&
3356 match_line(line, &view->regex, 1, &view->regmatch)) {
3357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3358 s->matched_line = lineno;
3359 free(line);
3360 break;
3362 free(line);
3363 if (view->searching == TOG_SEARCH_FORWARD)
3364 lineno++;
3365 else
3366 lineno--;
3369 if (s->matched_line) {
3370 s->first_displayed_line = s->matched_line;
3371 s->selected_line = 1;
3374 return NULL;
3377 static const struct got_error *
3378 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3379 struct got_object_id *id2, const char *label1, const char *label2,
3380 int diff_context, int ignore_whitespace, int force_text_diff,
3381 struct tog_view *log_view, struct got_repository *repo)
3383 const struct got_error *err;
3384 struct tog_diff_view_state *s = &view->state.diff;
3386 SIMPLEQ_INIT(&s->refs);
3388 if (id1 != NULL && id2 != NULL) {
3389 int type1, type2;
3390 err = got_object_get_type(&type1, repo, id1);
3391 if (err)
3392 return err;
3393 err = got_object_get_type(&type2, repo, id2);
3394 if (err)
3395 return err;
3397 if (type1 != type2)
3398 return got_error(GOT_ERR_OBJ_TYPE);
3400 s->first_displayed_line = 1;
3401 s->last_displayed_line = view->nlines;
3402 s->selected_line = 1;
3403 s->repo = repo;
3404 s->id1 = id1;
3405 s->id2 = id2;
3406 s->label1 = label1;
3407 s->label2 = label2;
3409 if (id1) {
3410 s->id1 = got_object_id_dup(id1);
3411 if (s->id1 == NULL)
3412 return got_error_from_errno("got_object_id_dup");
3413 } else
3414 s->id1 = NULL;
3416 s->id2 = got_object_id_dup(id2);
3417 if (s->id2 == NULL) {
3418 free(s->id1);
3419 s->id1 = NULL;
3420 return got_error_from_errno("got_object_id_dup");
3422 s->f = NULL;
3423 s->first_displayed_line = 1;
3424 s->last_displayed_line = view->nlines;
3425 s->diff_context = diff_context;
3426 s->ignore_whitespace = ignore_whitespace;
3427 s->force_text_diff = force_text_diff;
3428 s->log_view = log_view;
3429 s->repo = repo;
3431 SIMPLEQ_INIT(&s->colors);
3432 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3433 err = add_color(&s->colors,
3434 "^-", TOG_COLOR_DIFF_MINUS,
3435 get_color_value("TOG_COLOR_DIFF_MINUS"));
3436 if (err)
3437 return err;
3438 err = add_color(&s->colors, "^\\+",
3439 TOG_COLOR_DIFF_PLUS,
3440 get_color_value("TOG_COLOR_DIFF_PLUS"));
3441 if (err) {
3442 free_colors(&s->colors);
3443 return err;
3445 err = add_color(&s->colors,
3446 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3447 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3448 if (err) {
3449 free_colors(&s->colors);
3450 return err;
3453 err = add_color(&s->colors,
3454 "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3455 TOG_COLOR_DIFF_META,
3456 get_color_value("TOG_COLOR_DIFF_META"));
3457 if (err) {
3458 free_colors(&s->colors);
3459 return err;
3462 err = add_color(&s->colors,
3463 "^(from|via): ", TOG_COLOR_AUTHOR,
3464 get_color_value("TOG_COLOR_AUTHOR"));
3465 if (err) {
3466 free_colors(&s->colors);
3467 return err;
3470 err = add_color(&s->colors,
3471 "^date: ", TOG_COLOR_DATE,
3472 get_color_value("TOG_COLOR_DATE"));
3473 if (err) {
3474 free_colors(&s->colors);
3475 return err;
3479 err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3480 if (err) {
3481 free(s->id1);
3482 s->id1 = NULL;
3483 free(s->id2);
3484 s->id2 = NULL;
3485 free_colors(&s->colors);
3486 return err;
3489 if (log_view && view_is_splitscreen(view))
3490 show_log_view(log_view); /* draw vborder */
3491 diff_view_indicate_progress(view);
3493 s->line_offsets = NULL;
3494 s->nlines = 0;
3495 err = create_diff(s);
3496 if (err) {
3497 free(s->id1);
3498 s->id1 = NULL;
3499 free(s->id2);
3500 s->id2 = NULL;
3501 free_colors(&s->colors);
3502 got_ref_list_free(&s->refs);
3503 return err;
3506 view->show = show_diff_view;
3507 view->input = input_diff_view;
3508 view->close = close_diff_view;
3509 view->search_start = search_start_diff_view;
3510 view->search_next = search_next_diff_view;
3512 return NULL;
3515 static const struct got_error *
3516 close_diff_view(struct tog_view *view)
3518 const struct got_error *err = NULL;
3519 struct tog_diff_view_state *s = &view->state.diff;
3521 free(s->id1);
3522 s->id1 = NULL;
3523 free(s->id2);
3524 s->id2 = NULL;
3525 if (s->f && fclose(s->f) == EOF)
3526 err = got_error_from_errno("fclose");
3527 free_colors(&s->colors);
3528 free(s->line_offsets);
3529 s->line_offsets = NULL;
3530 s->nlines = 0;
3531 got_ref_list_free(&s->refs);
3532 return err;
3535 static const struct got_error *
3536 show_diff_view(struct tog_view *view)
3538 const struct got_error *err;
3539 struct tog_diff_view_state *s = &view->state.diff;
3540 char *id_str1 = NULL, *id_str2, *header;
3541 const char *label1, *label2;
3543 if (s->id1) {
3544 err = got_object_id_str(&id_str1, s->id1);
3545 if (err)
3546 return err;
3547 label1 = s->label1 ? : id_str1;
3548 } else
3549 label1 = "/dev/null";
3551 err = got_object_id_str(&id_str2, s->id2);
3552 if (err)
3553 return err;
3554 label2 = s->label2 ? : id_str2;
3556 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3557 err = got_error_from_errno("asprintf");
3558 free(id_str1);
3559 free(id_str2);
3560 return err;
3562 free(id_str1);
3563 free(id_str2);
3565 return draw_file(view, header);
3568 static const struct got_error *
3569 set_selected_commit(struct tog_diff_view_state *s,
3570 struct commit_queue_entry *entry)
3572 const struct got_error *err;
3573 const struct got_object_id_queue *parent_ids;
3574 struct got_commit_object *selected_commit;
3575 struct got_object_qid *pid;
3577 free(s->id2);
3578 s->id2 = got_object_id_dup(entry->id);
3579 if (s->id2 == NULL)
3580 return got_error_from_errno("got_object_id_dup");
3582 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3583 if (err)
3584 return err;
3585 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3586 free(s->id1);
3587 pid = SIMPLEQ_FIRST(parent_ids);
3588 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3589 got_object_commit_close(selected_commit);
3590 return NULL;
3593 static const struct got_error *
3594 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3596 const struct got_error *err = NULL;
3597 struct tog_diff_view_state *s = &view->state.diff;
3598 struct tog_log_view_state *ls;
3599 struct commit_queue_entry *old_selected_entry;
3600 int i;
3602 switch (ch) {
3603 case 'a':
3604 case 'w':
3605 if (ch == 'a')
3606 s->force_text_diff = !s->force_text_diff;
3607 if (ch == 'w')
3608 s->ignore_whitespace = !s->ignore_whitespace;
3609 wclear(view->window);
3610 s->first_displayed_line = 1;
3611 s->last_displayed_line = view->nlines;
3612 diff_view_indicate_progress(view);
3613 err = create_diff(s);
3614 break;
3615 case 'k':
3616 case KEY_UP:
3617 if (s->first_displayed_line > 1)
3618 s->first_displayed_line--;
3619 break;
3620 case KEY_PPAGE:
3621 case CTRL('b'):
3622 if (s->first_displayed_line == 1)
3623 break;
3624 i = 0;
3625 while (i++ < view->nlines - 1 &&
3626 s->first_displayed_line > 1)
3627 s->first_displayed_line--;
3628 break;
3629 case 'j':
3630 case KEY_DOWN:
3631 if (!s->eof)
3632 s->first_displayed_line++;
3633 break;
3634 case KEY_NPAGE:
3635 case CTRL('f'):
3636 case ' ':
3637 if (s->eof)
3638 break;
3639 i = 0;
3640 while (!s->eof && i++ < view->nlines - 1) {
3641 char *line;
3642 line = parse_next_line(s->f, NULL);
3643 s->first_displayed_line++;
3644 if (line == NULL)
3645 break;
3647 break;
3648 case '[':
3649 if (s->diff_context > 0) {
3650 s->diff_context--;
3651 diff_view_indicate_progress(view);
3652 err = create_diff(s);
3653 if (s->first_displayed_line + view->nlines - 1 >
3654 s->nlines) {
3655 s->first_displayed_line = 1;
3656 s->last_displayed_line = view->nlines;
3659 break;
3660 case ']':
3661 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3662 s->diff_context++;
3663 diff_view_indicate_progress(view);
3664 err = create_diff(s);
3666 break;
3667 case '<':
3668 case ',':
3669 if (s->log_view == NULL)
3670 break;
3671 ls = &s->log_view->state.log;
3672 old_selected_entry = ls->selected_entry;
3674 err = input_log_view(NULL, s->log_view, KEY_UP);
3675 if (err)
3676 break;
3678 if (old_selected_entry == ls->selected_entry)
3679 break;
3681 err = set_selected_commit(s, ls->selected_entry);
3682 if (err)
3683 break;
3685 s->first_displayed_line = 1;
3686 s->last_displayed_line = view->nlines;
3688 diff_view_indicate_progress(view);
3689 err = create_diff(s);
3690 break;
3691 case '>':
3692 case '.':
3693 if (s->log_view == NULL)
3694 break;
3695 ls = &s->log_view->state.log;
3696 old_selected_entry = ls->selected_entry;
3698 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3699 if (err)
3700 break;
3702 if (old_selected_entry == ls->selected_entry)
3703 break;
3705 err = set_selected_commit(s, ls->selected_entry);
3706 if (err)
3707 break;
3709 s->first_displayed_line = 1;
3710 s->last_displayed_line = view->nlines;
3712 diff_view_indicate_progress(view);
3713 err = create_diff(s);
3714 break;
3715 default:
3716 break;
3719 return err;
3722 static const struct got_error *
3723 cmd_diff(int argc, char *argv[])
3725 const struct got_error *error = NULL;
3726 struct got_repository *repo = NULL;
3727 struct got_worktree *worktree = NULL;
3728 struct got_object_id *id1 = NULL, *id2 = NULL;
3729 char *repo_path = NULL, *cwd = NULL;
3730 char *id_str1 = NULL, *id_str2 = NULL;
3731 char *label1 = NULL, *label2 = NULL;
3732 int diff_context = 3, ignore_whitespace = 0;
3733 int ch, force_text_diff = 0;
3734 const char *errstr;
3735 struct tog_view *view;
3737 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3738 switch (ch) {
3739 case 'a':
3740 force_text_diff = 1;
3741 break;
3742 case 'C':
3743 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3744 &errstr);
3745 if (errstr != NULL)
3746 err(1, "-C option %s", errstr);
3747 break;
3748 case 'r':
3749 repo_path = realpath(optarg, NULL);
3750 if (repo_path == NULL)
3751 return got_error_from_errno2("realpath",
3752 optarg);
3753 got_path_strip_trailing_slashes(repo_path);
3754 break;
3755 case 'w':
3756 ignore_whitespace = 1;
3757 break;
3758 default:
3759 usage_diff();
3760 /* NOTREACHED */
3764 argc -= optind;
3765 argv += optind;
3767 if (argc == 0) {
3768 usage_diff(); /* TODO show local worktree changes */
3769 } else if (argc == 2) {
3770 id_str1 = argv[0];
3771 id_str2 = argv[1];
3772 } else
3773 usage_diff();
3775 cwd = getcwd(NULL, 0);
3776 if (cwd == NULL)
3777 return got_error_from_errno("getcwd");
3779 error = got_worktree_open(&worktree, cwd);
3780 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3781 goto done;
3783 if (repo_path == NULL) {
3784 if (worktree)
3785 repo_path =
3786 strdup(got_worktree_get_repo_path(worktree));
3787 else
3788 repo_path = strdup(cwd);
3790 if (repo_path == NULL) {
3791 error = got_error_from_errno("strdup");
3792 goto done;
3795 error = got_repo_open(&repo, repo_path, NULL);
3796 if (error)
3797 goto done;
3799 init_curses();
3801 error = apply_unveil(got_repo_get_path(repo), NULL);
3802 if (error)
3803 goto done;
3805 error = got_repo_match_object_id(&id1, &label1, id_str1,
3806 GOT_OBJ_TYPE_ANY, 1, repo);
3807 if (error)
3808 goto done;
3810 error = got_repo_match_object_id(&id2, &label2, id_str2,
3811 GOT_OBJ_TYPE_ANY, 1, repo);
3812 if (error)
3813 goto done;
3815 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3816 if (view == NULL) {
3817 error = got_error_from_errno("view_open");
3818 goto done;
3820 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3821 ignore_whitespace, force_text_diff, NULL, repo);
3822 if (error)
3823 goto done;
3824 error = view_loop(view);
3825 done:
3826 free(label1);
3827 free(label2);
3828 free(repo_path);
3829 free(cwd);
3830 if (repo)
3831 got_repo_close(repo);
3832 if (worktree)
3833 got_worktree_close(worktree);
3834 return error;
3837 __dead static void
3838 usage_blame(void)
3840 endwin();
3841 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3842 getprogname());
3843 exit(1);
3846 struct tog_blame_line {
3847 int annotated;
3848 struct got_object_id *id;
3851 static const struct got_error *
3852 draw_blame(struct tog_view *view)
3854 struct tog_blame_view_state *s = &view->state.blame;
3855 struct tog_blame *blame = &s->blame;
3856 regmatch_t *regmatch = &view->regmatch;
3857 const struct got_error *err;
3858 int lineno = 0, nprinted = 0;
3859 char *line;
3860 size_t len;
3861 wchar_t *wline;
3862 int width;
3863 struct tog_blame_line *blame_line;
3864 struct got_object_id *prev_id = NULL;
3865 char *id_str;
3866 struct tog_color *tc;
3868 err = got_object_id_str(&id_str, s->blamed_commit->id);
3869 if (err)
3870 return err;
3872 rewind(blame->f);
3873 werase(view->window);
3875 if (asprintf(&line, "commit %s", id_str) == -1) {
3876 err = got_error_from_errno("asprintf");
3877 free(id_str);
3878 return err;
3881 err = format_line(&wline, &width, line, view->ncols, 0);
3882 free(line);
3883 line = NULL;
3884 if (err)
3885 return err;
3886 if (view_needs_focus_indication(view))
3887 wstandout(view->window);
3888 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3889 if (tc)
3890 wattr_on(view->window,
3891 COLOR_PAIR(tc->colorpair), NULL);
3892 waddwstr(view->window, wline);
3893 if (tc)
3894 wattr_off(view->window,
3895 COLOR_PAIR(tc->colorpair), NULL);
3896 if (view_needs_focus_indication(view))
3897 wstandend(view->window);
3898 free(wline);
3899 wline = NULL;
3900 if (width < view->ncols - 1)
3901 waddch(view->window, '\n');
3903 if (asprintf(&line, "[%d/%d] %s%s",
3904 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3905 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3906 free(id_str);
3907 return got_error_from_errno("asprintf");
3909 free(id_str);
3910 err = format_line(&wline, &width, line, view->ncols, 0);
3911 free(line);
3912 line = NULL;
3913 if (err)
3914 return err;
3915 waddwstr(view->window, wline);
3916 free(wline);
3917 wline = NULL;
3918 if (width < view->ncols - 1)
3919 waddch(view->window, '\n');
3921 s->eof = 0;
3922 while (nprinted < view->nlines - 2) {
3923 line = parse_next_line(blame->f, &len);
3924 if (line == NULL) {
3925 s->eof = 1;
3926 break;
3928 if (++lineno < s->first_displayed_line) {
3929 free(line);
3930 continue;
3933 if (view->focussed && nprinted == s->selected_line - 1)
3934 wstandout(view->window);
3936 if (blame->nlines > 0) {
3937 blame_line = &blame->lines[lineno - 1];
3938 if (blame_line->annotated && prev_id &&
3939 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3940 !(view->focussed &&
3941 nprinted == s->selected_line - 1)) {
3942 waddstr(view->window, " ");
3943 } else if (blame_line->annotated) {
3944 char *id_str;
3945 err = got_object_id_str(&id_str, blame_line->id);
3946 if (err) {
3947 free(line);
3948 return err;
3950 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3951 if (tc)
3952 wattr_on(view->window,
3953 COLOR_PAIR(tc->colorpair), NULL);
3954 wprintw(view->window, "%.8s", id_str);
3955 if (tc)
3956 wattr_off(view->window,
3957 COLOR_PAIR(tc->colorpair), NULL);
3958 free(id_str);
3959 prev_id = blame_line->id;
3960 } else {
3961 waddstr(view->window, "........");
3962 prev_id = NULL;
3964 } else {
3965 waddstr(view->window, "........");
3966 prev_id = NULL;
3969 if (view->focussed && nprinted == s->selected_line - 1)
3970 wstandend(view->window);
3971 waddstr(view->window, " ");
3973 if (view->ncols <= 9) {
3974 width = 9;
3975 wline = wcsdup(L"");
3976 if (wline == NULL) {
3977 err = got_error_from_errno("wcsdup");
3978 free(line);
3979 return err;
3981 } else if (s->first_displayed_line + nprinted ==
3982 s->matched_line &&
3983 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3984 err = add_matched_line(&width, line, view->ncols - 9, 9,
3985 view->window, regmatch);
3986 if (err) {
3987 free(line);
3988 return err;
3990 width += 9;
3991 } else {
3992 err = format_line(&wline, &width, line,
3993 view->ncols - 9, 9);
3994 waddwstr(view->window, wline);
3995 free(wline);
3996 wline = NULL;
3997 width += 9;
4000 if (width <= view->ncols - 1)
4001 waddch(view->window, '\n');
4002 if (++nprinted == 1)
4003 s->first_displayed_line = lineno;
4004 free(line);
4006 s->last_displayed_line = lineno;
4008 view_vborder(view);
4010 return NULL;
4013 static const struct got_error *
4014 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4016 const struct got_error *err = NULL;
4017 struct tog_blame_cb_args *a = arg;
4018 struct tog_blame_line *line;
4019 int errcode;
4021 if (nlines != a->nlines ||
4022 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4023 return got_error(GOT_ERR_RANGE);
4025 errcode = pthread_mutex_lock(&tog_mutex);
4026 if (errcode)
4027 return got_error_set_errno(errcode, "pthread_mutex_lock");
4029 if (*a->quit) { /* user has quit the blame view */
4030 err = got_error(GOT_ERR_ITER_COMPLETED);
4031 goto done;
4034 if (lineno == -1)
4035 goto done; /* no change in this commit */
4037 line = &a->lines[lineno - 1];
4038 if (line->annotated)
4039 goto done;
4041 line->id = got_object_id_dup(id);
4042 if (line->id == NULL) {
4043 err = got_error_from_errno("got_object_id_dup");
4044 goto done;
4046 line->annotated = 1;
4047 done:
4048 errcode = pthread_mutex_unlock(&tog_mutex);
4049 if (errcode)
4050 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4051 return err;
4054 static void *
4055 blame_thread(void *arg)
4057 const struct got_error *err;
4058 struct tog_blame_thread_args *ta = arg;
4059 struct tog_blame_cb_args *a = ta->cb_args;
4060 int errcode;
4062 err = block_signals_used_by_main_thread();
4063 if (err)
4064 return (void *)err;
4066 err = got_blame(ta->path, a->commit_id, ta->repo,
4067 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4068 if (err && err->code == GOT_ERR_CANCELLED)
4069 err = NULL;
4071 errcode = pthread_mutex_lock(&tog_mutex);
4072 if (errcode)
4073 return (void *)got_error_set_errno(errcode,
4074 "pthread_mutex_lock");
4076 got_repo_close(ta->repo);
4077 ta->repo = NULL;
4078 *ta->complete = 1;
4080 errcode = pthread_mutex_unlock(&tog_mutex);
4081 if (errcode && err == NULL)
4082 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4084 return (void *)err;
4087 static struct got_object_id *
4088 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4089 int first_displayed_line, int selected_line)
4091 struct tog_blame_line *line;
4093 if (nlines <= 0)
4094 return NULL;
4096 line = &lines[first_displayed_line - 1 + selected_line - 1];
4097 if (!line->annotated)
4098 return NULL;
4100 return line->id;
4103 static const struct got_error *
4104 stop_blame(struct tog_blame *blame)
4106 const struct got_error *err = NULL;
4107 int i;
4109 if (blame->thread) {
4110 int errcode;
4111 errcode = pthread_mutex_unlock(&tog_mutex);
4112 if (errcode)
4113 return got_error_set_errno(errcode,
4114 "pthread_mutex_unlock");
4115 errcode = pthread_join(blame->thread, (void **)&err);
4116 if (errcode)
4117 return got_error_set_errno(errcode, "pthread_join");
4118 errcode = pthread_mutex_lock(&tog_mutex);
4119 if (errcode)
4120 return got_error_set_errno(errcode,
4121 "pthread_mutex_lock");
4122 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4123 err = NULL;
4124 blame->thread = NULL;
4126 if (blame->thread_args.repo) {
4127 got_repo_close(blame->thread_args.repo);
4128 blame->thread_args.repo = NULL;
4130 if (blame->f) {
4131 if (fclose(blame->f) != 0 && err == NULL)
4132 err = got_error_from_errno("fclose");
4133 blame->f = NULL;
4135 if (blame->lines) {
4136 for (i = 0; i < blame->nlines; i++)
4137 free(blame->lines[i].id);
4138 free(blame->lines);
4139 blame->lines = NULL;
4141 free(blame->cb_args.commit_id);
4142 blame->cb_args.commit_id = NULL;
4144 return err;
4147 static const struct got_error *
4148 cancel_blame_view(void *arg)
4150 const struct got_error *err = NULL;
4151 int *done = arg;
4152 int errcode;
4154 errcode = pthread_mutex_lock(&tog_mutex);
4155 if (errcode)
4156 return got_error_set_errno(errcode,
4157 "pthread_mutex_unlock");
4159 if (*done)
4160 err = got_error(GOT_ERR_CANCELLED);
4162 errcode = pthread_mutex_unlock(&tog_mutex);
4163 if (errcode)
4164 return got_error_set_errno(errcode,
4165 "pthread_mutex_lock");
4167 return err;
4170 static const struct got_error *
4171 run_blame(struct tog_view *view)
4173 struct tog_blame_view_state *s = &view->state.blame;
4174 struct tog_blame *blame = &s->blame;
4175 const struct got_error *err = NULL;
4176 struct got_blob_object *blob = NULL;
4177 struct got_repository *thread_repo = NULL;
4178 struct got_object_id *obj_id = NULL;
4179 int obj_type;
4181 err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4182 s->path);
4183 if (err)
4184 return err;
4186 err = got_object_get_type(&obj_type, s->repo, obj_id);
4187 if (err)
4188 goto done;
4190 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4191 err = got_error(GOT_ERR_OBJ_TYPE);
4192 goto done;
4195 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4196 if (err)
4197 goto done;
4198 blame->f = got_opentemp();
4199 if (blame->f == NULL) {
4200 err = got_error_from_errno("got_opentemp");
4201 goto done;
4203 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4204 &blame->line_offsets, blame->f, blob);
4205 if (err || blame->nlines == 0)
4206 goto done;
4208 /* Don't include \n at EOF in the blame line count. */
4209 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4210 blame->nlines--;
4212 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4213 if (blame->lines == NULL) {
4214 err = got_error_from_errno("calloc");
4215 goto done;
4218 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4219 if (err)
4220 goto done;
4222 blame->cb_args.view = view;
4223 blame->cb_args.lines = blame->lines;
4224 blame->cb_args.nlines = blame->nlines;
4225 blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4226 if (blame->cb_args.commit_id == NULL) {
4227 err = got_error_from_errno("got_object_id_dup");
4228 goto done;
4230 blame->cb_args.quit = &s->done;
4232 blame->thread_args.path = s->path;
4233 blame->thread_args.repo = thread_repo;
4234 blame->thread_args.cb_args = &blame->cb_args;
4235 blame->thread_args.complete = &s->blame_complete;
4236 blame->thread_args.cancel_cb = cancel_blame_view;
4237 blame->thread_args.cancel_arg = &s->done;
4238 s->blame_complete = 0;
4240 done:
4241 if (blob)
4242 got_object_blob_close(blob);
4243 free(obj_id);
4244 if (err)
4245 stop_blame(blame);
4246 return err;
4249 static const struct got_error *
4250 open_blame_view(struct tog_view *view, char *path,
4251 struct got_object_id *commit_id, struct got_repository *repo)
4253 const struct got_error *err = NULL;
4254 struct tog_blame_view_state *s = &view->state.blame;
4256 SIMPLEQ_INIT(&s->blamed_commits);
4258 s->path = strdup(path);
4259 if (s->path == NULL)
4260 return got_error_from_errno("strdup");
4262 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4263 if (err) {
4264 free(s->path);
4265 return err;
4268 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4269 s->first_displayed_line = 1;
4270 s->last_displayed_line = view->nlines;
4271 s->selected_line = 1;
4272 s->blame_complete = 0;
4273 s->repo = repo;
4274 s->commit_id = commit_id;
4275 memset(&s->blame, 0, sizeof(s->blame));
4277 SIMPLEQ_INIT(&s->colors);
4278 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4279 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4280 get_color_value("TOG_COLOR_COMMIT"));
4281 if (err)
4282 return err;
4285 view->show = show_blame_view;
4286 view->input = input_blame_view;
4287 view->close = close_blame_view;
4288 view->search_start = search_start_blame_view;
4289 view->search_next = search_next_blame_view;
4291 return run_blame(view);
4294 static const struct got_error *
4295 close_blame_view(struct tog_view *view)
4297 const struct got_error *err = NULL;
4298 struct tog_blame_view_state *s = &view->state.blame;
4300 if (s->blame.thread)
4301 err = stop_blame(&s->blame);
4303 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4304 struct got_object_qid *blamed_commit;
4305 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4306 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4307 got_object_qid_free(blamed_commit);
4310 free(s->path);
4311 free_colors(&s->colors);
4313 return err;
4316 static const struct got_error *
4317 search_start_blame_view(struct tog_view *view)
4319 struct tog_blame_view_state *s = &view->state.blame;
4321 s->matched_line = 0;
4322 return NULL;
4325 static const struct got_error *
4326 search_next_blame_view(struct tog_view *view)
4328 struct tog_blame_view_state *s = &view->state.blame;
4329 int lineno;
4331 if (!view->searching) {
4332 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4333 return NULL;
4336 if (s->matched_line) {
4337 if (view->searching == TOG_SEARCH_FORWARD)
4338 lineno = s->matched_line + 1;
4339 else
4340 lineno = s->matched_line - 1;
4341 } else {
4342 if (view->searching == TOG_SEARCH_FORWARD)
4343 lineno = 1;
4344 else
4345 lineno = s->blame.nlines;
4348 while (1) {
4349 char *line = NULL;
4350 off_t offset;
4351 size_t len;
4353 if (lineno <= 0 || lineno > s->blame.nlines) {
4354 if (s->matched_line == 0) {
4355 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4356 free(line);
4357 break;
4360 if (view->searching == TOG_SEARCH_FORWARD)
4361 lineno = 1;
4362 else
4363 lineno = s->blame.nlines;
4366 offset = s->blame.line_offsets[lineno - 1];
4367 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4368 free(line);
4369 return got_error_from_errno("fseeko");
4371 free(line);
4372 line = parse_next_line(s->blame.f, &len);
4373 if (line &&
4374 match_line(line, &view->regex, 1, &view->regmatch)) {
4375 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4376 s->matched_line = lineno;
4377 free(line);
4378 break;
4380 free(line);
4381 if (view->searching == TOG_SEARCH_FORWARD)
4382 lineno++;
4383 else
4384 lineno--;
4387 if (s->matched_line) {
4388 s->first_displayed_line = s->matched_line;
4389 s->selected_line = 1;
4392 return NULL;
4395 static const struct got_error *
4396 show_blame_view(struct tog_view *view)
4398 const struct got_error *err = NULL;
4399 struct tog_blame_view_state *s = &view->state.blame;
4400 int errcode;
4402 if (s->blame.thread == NULL) {
4403 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4404 &s->blame.thread_args);
4405 if (errcode)
4406 return got_error_set_errno(errcode, "pthread_create");
4408 halfdelay(1); /* fast refresh while annotating */
4411 if (s->blame_complete)
4412 halfdelay(10); /* disable fast refresh */
4414 err = draw_blame(view);
4416 view_vborder(view);
4417 return err;
4420 static const struct got_error *
4421 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4423 const struct got_error *err = NULL, *thread_err = NULL;
4424 struct tog_view *diff_view;
4425 struct tog_blame_view_state *s = &view->state.blame;
4426 int begin_x = 0;
4428 switch (ch) {
4429 case 'q':
4430 s->done = 1;
4431 break;
4432 case 'k':
4433 case KEY_UP:
4434 if (s->selected_line > 1)
4435 s->selected_line--;
4436 else if (s->selected_line == 1 &&
4437 s->first_displayed_line > 1)
4438 s->first_displayed_line--;
4439 break;
4440 case KEY_PPAGE:
4441 case CTRL('b'):
4442 if (s->first_displayed_line == 1) {
4443 s->selected_line = 1;
4444 break;
4446 if (s->first_displayed_line > view->nlines - 2)
4447 s->first_displayed_line -=
4448 (view->nlines - 2);
4449 else
4450 s->first_displayed_line = 1;
4451 break;
4452 case 'j':
4453 case KEY_DOWN:
4454 if (s->selected_line < view->nlines - 2 &&
4455 s->first_displayed_line +
4456 s->selected_line <= s->blame.nlines)
4457 s->selected_line++;
4458 else if (s->last_displayed_line <
4459 s->blame.nlines)
4460 s->first_displayed_line++;
4461 break;
4462 case 'b':
4463 case 'p': {
4464 struct got_object_id *id = NULL;
4465 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4466 s->first_displayed_line, s->selected_line);
4467 if (id == NULL)
4468 break;
4469 if (ch == 'p') {
4470 struct got_commit_object *commit;
4471 struct got_object_qid *pid;
4472 struct got_object_id *blob_id = NULL;
4473 int obj_type;
4474 err = got_object_open_as_commit(&commit,
4475 s->repo, id);
4476 if (err)
4477 break;
4478 pid = SIMPLEQ_FIRST(
4479 got_object_commit_get_parent_ids(commit));
4480 if (pid == NULL) {
4481 got_object_commit_close(commit);
4482 break;
4484 /* Check if path history ends here. */
4485 err = got_object_id_by_path(&blob_id, s->repo,
4486 pid->id, s->path);
4487 if (err) {
4488 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4489 err = NULL;
4490 got_object_commit_close(commit);
4491 break;
4493 err = got_object_get_type(&obj_type, s->repo,
4494 blob_id);
4495 free(blob_id);
4496 /* Can't blame non-blob type objects. */
4497 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4498 got_object_commit_close(commit);
4499 break;
4501 err = got_object_qid_alloc(&s->blamed_commit,
4502 pid->id);
4503 got_object_commit_close(commit);
4504 } else {
4505 if (got_object_id_cmp(id,
4506 s->blamed_commit->id) == 0)
4507 break;
4508 err = got_object_qid_alloc(&s->blamed_commit,
4509 id);
4511 if (err)
4512 break;
4513 s->done = 1;
4514 thread_err = stop_blame(&s->blame);
4515 s->done = 0;
4516 if (thread_err)
4517 break;
4518 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4519 s->blamed_commit, entry);
4520 err = run_blame(view);
4521 if (err)
4522 break;
4523 break;
4525 case 'B': {
4526 struct got_object_qid *first;
4527 first = SIMPLEQ_FIRST(&s->blamed_commits);
4528 if (!got_object_id_cmp(first->id, s->commit_id))
4529 break;
4530 s->done = 1;
4531 thread_err = stop_blame(&s->blame);
4532 s->done = 0;
4533 if (thread_err)
4534 break;
4535 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4536 got_object_qid_free(s->blamed_commit);
4537 s->blamed_commit =
4538 SIMPLEQ_FIRST(&s->blamed_commits);
4539 err = run_blame(view);
4540 if (err)
4541 break;
4542 break;
4544 case KEY_ENTER:
4545 case '\r': {
4546 struct got_object_id *id = NULL;
4547 struct got_object_qid *pid;
4548 struct got_commit_object *commit = NULL;
4549 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4550 s->first_displayed_line, s->selected_line);
4551 if (id == NULL)
4552 break;
4553 err = got_object_open_as_commit(&commit, s->repo, id);
4554 if (err)
4555 break;
4556 pid = SIMPLEQ_FIRST(
4557 got_object_commit_get_parent_ids(commit));
4558 if (view_is_parent_view(view))
4559 begin_x = view_split_begin_x(view->begin_x);
4560 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4561 if (diff_view == NULL) {
4562 got_object_commit_close(commit);
4563 err = got_error_from_errno("view_open");
4564 break;
4566 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4567 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4568 got_object_commit_close(commit);
4569 if (err) {
4570 view_close(diff_view);
4571 break;
4573 view->focussed = 0;
4574 diff_view->focussed = 1;
4575 if (view_is_parent_view(view)) {
4576 err = view_close_child(view);
4577 if (err)
4578 break;
4579 view_set_child(view, diff_view);
4580 view->focus_child = 1;
4581 } else
4582 *new_view = diff_view;
4583 if (err)
4584 break;
4585 break;
4587 case KEY_NPAGE:
4588 case CTRL('f'):
4589 case ' ':
4590 if (s->last_displayed_line >= s->blame.nlines &&
4591 s->selected_line >= MIN(s->blame.nlines,
4592 view->nlines - 2)) {
4593 break;
4595 if (s->last_displayed_line >= s->blame.nlines &&
4596 s->selected_line < view->nlines - 2) {
4597 s->selected_line = MIN(s->blame.nlines,
4598 view->nlines - 2);
4599 break;
4601 if (s->last_displayed_line + view->nlines - 2
4602 <= s->blame.nlines)
4603 s->first_displayed_line +=
4604 view->nlines - 2;
4605 else
4606 s->first_displayed_line =
4607 s->blame.nlines -
4608 (view->nlines - 3);
4609 break;
4610 case KEY_RESIZE:
4611 if (s->selected_line > view->nlines - 2) {
4612 s->selected_line = MIN(s->blame.nlines,
4613 view->nlines - 2);
4615 break;
4616 default:
4617 break;
4619 return thread_err ? thread_err : err;
4622 static const struct got_error *
4623 cmd_blame(int argc, char *argv[])
4625 const struct got_error *error;
4626 struct got_repository *repo = NULL;
4627 struct got_worktree *worktree = NULL;
4628 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4629 char *link_target = NULL;
4630 struct got_object_id *commit_id = NULL;
4631 char *commit_id_str = NULL;
4632 int ch;
4633 struct tog_view *view;
4635 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4636 switch (ch) {
4637 case 'c':
4638 commit_id_str = optarg;
4639 break;
4640 case 'r':
4641 repo_path = realpath(optarg, NULL);
4642 if (repo_path == NULL)
4643 return got_error_from_errno2("realpath",
4644 optarg);
4645 break;
4646 default:
4647 usage_blame();
4648 /* NOTREACHED */
4652 argc -= optind;
4653 argv += optind;
4655 if (argc != 1)
4656 usage_blame();
4658 cwd = getcwd(NULL, 0);
4659 if (cwd == NULL)
4660 return got_error_from_errno("getcwd");
4662 error = got_worktree_open(&worktree, cwd);
4663 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4664 goto done;
4666 if (repo_path == NULL) {
4667 if (worktree)
4668 repo_path =
4669 strdup(got_worktree_get_repo_path(worktree));
4670 else
4671 repo_path = strdup(cwd);
4673 if (repo_path == NULL) {
4674 error = got_error_from_errno("strdup");
4675 goto done;
4678 error = got_repo_open(&repo, repo_path, NULL);
4679 if (error != NULL)
4680 goto done;
4682 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4683 worktree);
4684 if (error)
4685 goto done;
4687 init_curses();
4689 error = apply_unveil(got_repo_get_path(repo), NULL);
4690 if (error)
4691 goto done;
4693 if (commit_id_str == NULL) {
4694 struct got_reference *head_ref;
4695 error = got_ref_open(&head_ref, repo, worktree ?
4696 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4697 if (error != NULL)
4698 goto done;
4699 error = got_ref_resolve(&commit_id, repo, head_ref);
4700 got_ref_close(head_ref);
4701 } else {
4702 error = got_repo_match_object_id(&commit_id, NULL,
4703 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4705 if (error != NULL)
4706 goto done;
4708 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4709 if (view == NULL) {
4710 error = got_error_from_errno("view_open");
4711 goto done;
4714 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4715 commit_id, repo);
4716 if (error)
4717 goto done;
4719 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4720 commit_id, repo);
4721 if (error)
4722 goto done;
4723 if (worktree) {
4724 /* Release work tree lock. */
4725 got_worktree_close(worktree);
4726 worktree = NULL;
4728 error = view_loop(view);
4729 done:
4730 free(repo_path);
4731 free(in_repo_path);
4732 free(link_target);
4733 free(cwd);
4734 free(commit_id);
4735 if (worktree)
4736 got_worktree_close(worktree);
4737 if (repo)
4738 got_repo_close(repo);
4739 return error;
4742 static const struct got_error *
4743 draw_tree_entries(struct tog_view *view, const char *parent_path)
4745 struct tog_tree_view_state *s = &view->state.tree;
4746 const struct got_error *err = NULL;
4747 struct got_tree_entry *te;
4748 wchar_t *wline;
4749 struct tog_color *tc;
4750 int width, n, i, nentries;
4751 int limit = view->nlines;
4753 s->ndisplayed = 0;
4755 werase(view->window);
4757 if (limit == 0)
4758 return NULL;
4760 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4761 if (err)
4762 return err;
4763 if (view_needs_focus_indication(view))
4764 wstandout(view->window);
4765 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4766 if (tc)
4767 wattr_on(view->window,
4768 COLOR_PAIR(tc->colorpair), NULL);
4769 waddwstr(view->window, wline);
4770 if (tc)
4771 wattr_off(view->window,
4772 COLOR_PAIR(tc->colorpair), NULL);
4773 if (view_needs_focus_indication(view))
4774 wstandend(view->window);
4775 free(wline);
4776 wline = NULL;
4777 if (width < view->ncols - 1)
4778 waddch(view->window, '\n');
4779 if (--limit <= 0)
4780 return NULL;
4781 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4782 if (err)
4783 return err;
4784 waddwstr(view->window, wline);
4785 free(wline);
4786 wline = NULL;
4787 if (width < view->ncols - 1)
4788 waddch(view->window, '\n');
4789 if (--limit <= 0)
4790 return NULL;
4791 waddch(view->window, '\n');
4792 if (--limit <= 0)
4793 return NULL;
4795 if (s->first_displayed_entry == NULL) {
4796 te = got_object_tree_get_first_entry(s->tree);
4797 if (s->selected == 0) {
4798 if (view->focussed)
4799 wstandout(view->window);
4800 s->selected_entry = NULL;
4802 waddstr(view->window, " ..\n"); /* parent directory */
4803 if (s->selected == 0 && view->focussed)
4804 wstandend(view->window);
4805 s->ndisplayed++;
4806 if (--limit <= 0)
4807 return NULL;
4808 n = 1;
4809 } else {
4810 n = 0;
4811 te = s->first_displayed_entry;
4814 nentries = got_object_tree_get_nentries(s->tree);
4815 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4816 char *line = NULL, *id_str = NULL, *link_target = NULL;
4817 const char *modestr = "";
4818 mode_t mode;
4820 te = got_object_tree_get_entry(s->tree, i);
4821 mode = got_tree_entry_get_mode(te);
4823 if (s->show_ids) {
4824 err = got_object_id_str(&id_str,
4825 got_tree_entry_get_id(te));
4826 if (err)
4827 return got_error_from_errno(
4828 "got_object_id_str");
4830 if (got_object_tree_entry_is_submodule(te))
4831 modestr = "$";
4832 else if (S_ISLNK(mode)) {
4833 int i;
4835 err = got_tree_entry_get_symlink_target(&link_target,
4836 te, s->repo);
4837 if (err) {
4838 free(id_str);
4839 return err;
4841 for (i = 0; i < strlen(link_target); i++) {
4842 if (!isprint((unsigned char)link_target[i]))
4843 link_target[i] = '?';
4845 modestr = "@";
4847 else if (S_ISDIR(mode))
4848 modestr = "/";
4849 else if (mode & S_IXUSR)
4850 modestr = "*";
4851 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4852 got_tree_entry_get_name(te), modestr,
4853 link_target ? " -> ": "",
4854 link_target ? link_target : "") == -1) {
4855 free(id_str);
4856 free(link_target);
4857 return got_error_from_errno("asprintf");
4859 free(id_str);
4860 free(link_target);
4861 err = format_line(&wline, &width, line, view->ncols, 0);
4862 if (err) {
4863 free(line);
4864 break;
4866 if (n == s->selected) {
4867 if (view->focussed)
4868 wstandout(view->window);
4869 s->selected_entry = te;
4871 tc = match_color(&s->colors, line);
4872 if (tc)
4873 wattr_on(view->window,
4874 COLOR_PAIR(tc->colorpair), NULL);
4875 waddwstr(view->window, wline);
4876 if (tc)
4877 wattr_off(view->window,
4878 COLOR_PAIR(tc->colorpair), NULL);
4879 if (width < view->ncols - 1)
4880 waddch(view->window, '\n');
4881 if (n == s->selected && view->focussed)
4882 wstandend(view->window);
4883 free(line);
4884 free(wline);
4885 wline = NULL;
4886 n++;
4887 s->ndisplayed++;
4888 s->last_displayed_entry = te;
4889 if (--limit <= 0)
4890 break;
4893 return err;
4896 static void
4897 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4899 struct got_tree_entry *te;
4900 int isroot = s->tree == s->root;
4901 int i = 0;
4903 if (s->first_displayed_entry == NULL)
4904 return;
4906 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4907 while (i++ < maxscroll) {
4908 if (te == NULL) {
4909 if (!isroot)
4910 s->first_displayed_entry = NULL;
4911 break;
4913 s->first_displayed_entry = te;
4914 te = got_tree_entry_get_prev(s->tree, te);
4918 static void
4919 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4921 struct got_tree_entry *next, *last;
4922 int n = 0;
4924 if (s->first_displayed_entry)
4925 next = got_tree_entry_get_next(s->tree,
4926 s->first_displayed_entry);
4927 else
4928 next = got_object_tree_get_first_entry(s->tree);
4930 last = s->last_displayed_entry;
4931 while (next && last && n++ < maxscroll) {
4932 last = got_tree_entry_get_next(s->tree, last);
4933 if (last) {
4934 s->first_displayed_entry = next;
4935 next = got_tree_entry_get_next(s->tree, next);
4940 static const struct got_error *
4941 tree_entry_path(char **path, struct tog_parent_trees *parents,
4942 struct got_tree_entry *te)
4944 const struct got_error *err = NULL;
4945 struct tog_parent_tree *pt;
4946 size_t len = 2; /* for leading slash and NUL */
4948 TAILQ_FOREACH(pt, parents, entry)
4949 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4950 + 1 /* slash */;
4951 if (te)
4952 len += strlen(got_tree_entry_get_name(te));
4954 *path = calloc(1, len);
4955 if (path == NULL)
4956 return got_error_from_errno("calloc");
4958 (*path)[0] = '/';
4959 pt = TAILQ_LAST(parents, tog_parent_trees);
4960 while (pt) {
4961 const char *name = got_tree_entry_get_name(pt->selected_entry);
4962 if (strlcat(*path, name, len) >= len) {
4963 err = got_error(GOT_ERR_NO_SPACE);
4964 goto done;
4966 if (strlcat(*path, "/", len) >= len) {
4967 err = got_error(GOT_ERR_NO_SPACE);
4968 goto done;
4970 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4972 if (te) {
4973 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4974 err = got_error(GOT_ERR_NO_SPACE);
4975 goto done;
4978 done:
4979 if (err) {
4980 free(*path);
4981 *path = NULL;
4983 return err;
4986 static const struct got_error *
4987 blame_tree_entry(struct tog_view **new_view, int begin_x,
4988 struct got_tree_entry *te, struct tog_parent_trees *parents,
4989 struct got_object_id *commit_id, struct got_repository *repo)
4991 const struct got_error *err = NULL;
4992 char *path;
4993 struct tog_view *blame_view;
4995 *new_view = NULL;
4997 err = tree_entry_path(&path, parents, te);
4998 if (err)
4999 return err;
5001 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5002 if (blame_view == NULL) {
5003 err = got_error_from_errno("view_open");
5004 goto done;
5007 err = open_blame_view(blame_view, path, commit_id, repo);
5008 if (err) {
5009 if (err->code == GOT_ERR_CANCELLED)
5010 err = NULL;
5011 view_close(blame_view);
5012 } else
5013 *new_view = blame_view;
5014 done:
5015 free(path);
5016 return err;
5019 static const struct got_error *
5020 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5021 struct tog_tree_view_state *s)
5023 struct tog_view *log_view;
5024 const struct got_error *err = NULL;
5025 char *path;
5027 *new_view = NULL;
5029 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5030 if (log_view == NULL)
5031 return got_error_from_errno("view_open");
5033 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5034 if (err)
5035 return err;
5037 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5038 path, 0);
5039 if (err)
5040 view_close(log_view);
5041 else
5042 *new_view = log_view;
5043 free(path);
5044 return err;
5047 static const struct got_error *
5048 open_tree_view(struct tog_view *view, struct got_tree_object *root,
5049 struct got_object_id *commit_id, const char *head_ref_name,
5050 struct got_repository *repo)
5052 const struct got_error *err = NULL;
5053 char *commit_id_str = NULL;
5054 struct tog_tree_view_state *s = &view->state.tree;
5056 TAILQ_INIT(&s->parents);
5058 err = got_object_id_str(&commit_id_str, commit_id);
5059 if (err != NULL)
5060 goto done;
5062 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5063 err = got_error_from_errno("asprintf");
5064 goto done;
5067 s->root = s->tree = root;
5068 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5069 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5070 s->commit_id = got_object_id_dup(commit_id);
5071 if (s->commit_id == NULL) {
5072 err = got_error_from_errno("got_object_id_dup");
5073 goto done;
5075 s->head_ref_name = head_ref_name;
5076 s->repo = repo;
5078 SIMPLEQ_INIT(&s->colors);
5080 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5081 err = add_color(&s->colors, "\\$$",
5082 TOG_COLOR_TREE_SUBMODULE,
5083 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5084 if (err)
5085 goto done;
5086 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5087 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5088 if (err) {
5089 free_colors(&s->colors);
5090 goto done;
5092 err = add_color(&s->colors, "/$",
5093 TOG_COLOR_TREE_DIRECTORY,
5094 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5095 if (err) {
5096 free_colors(&s->colors);
5097 goto done;
5100 err = add_color(&s->colors, "\\*$",
5101 TOG_COLOR_TREE_EXECUTABLE,
5102 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5103 if (err) {
5104 free_colors(&s->colors);
5105 goto done;
5108 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5109 get_color_value("TOG_COLOR_COMMIT"));
5110 if (err) {
5111 free_colors(&s->colors);
5112 goto done;
5116 view->show = show_tree_view;
5117 view->input = input_tree_view;
5118 view->close = close_tree_view;
5119 view->search_start = search_start_tree_view;
5120 view->search_next = search_next_tree_view;
5121 done:
5122 free(commit_id_str);
5123 if (err) {
5124 free(s->tree_label);
5125 s->tree_label = NULL;
5127 return err;
5130 static const struct got_error *
5131 close_tree_view(struct tog_view *view)
5133 struct tog_tree_view_state *s = &view->state.tree;
5135 free_colors(&s->colors);
5136 free(s->tree_label);
5137 s->tree_label = NULL;
5138 free(s->commit_id);
5139 s->commit_id = NULL;
5140 while (!TAILQ_EMPTY(&s->parents)) {
5141 struct tog_parent_tree *parent;
5142 parent = TAILQ_FIRST(&s->parents);
5143 TAILQ_REMOVE(&s->parents, parent, entry);
5144 free(parent);
5147 if (s->tree != s->root)
5148 got_object_tree_close(s->tree);
5149 got_object_tree_close(s->root);
5150 return NULL;
5153 static const struct got_error *
5154 search_start_tree_view(struct tog_view *view)
5156 struct tog_tree_view_state *s = &view->state.tree;
5158 s->matched_entry = NULL;
5159 return NULL;
5162 static int
5163 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5165 regmatch_t regmatch;
5167 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5168 0) == 0;
5171 static const struct got_error *
5172 search_next_tree_view(struct tog_view *view)
5174 struct tog_tree_view_state *s = &view->state.tree;
5175 struct got_tree_entry *te = NULL;
5177 if (!view->searching) {
5178 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5179 return NULL;
5182 if (s->matched_entry) {
5183 if (view->searching == TOG_SEARCH_FORWARD) {
5184 if (s->selected_entry)
5185 te = got_tree_entry_get_next(s->tree,
5186 s->selected_entry);
5187 else
5188 te = got_object_tree_get_first_entry(s->tree);
5189 } else {
5190 if (s->selected_entry == NULL)
5191 te = got_object_tree_get_last_entry(s->tree);
5192 else
5193 te = got_tree_entry_get_prev(s->tree,
5194 s->selected_entry);
5196 } else {
5197 if (view->searching == TOG_SEARCH_FORWARD)
5198 te = got_object_tree_get_first_entry(s->tree);
5199 else
5200 te = got_object_tree_get_last_entry(s->tree);
5203 while (1) {
5204 if (te == NULL) {
5205 if (s->matched_entry == NULL) {
5206 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5207 return NULL;
5209 if (view->searching == TOG_SEARCH_FORWARD)
5210 te = got_object_tree_get_first_entry(s->tree);
5211 else
5212 te = got_object_tree_get_last_entry(s->tree);
5215 if (match_tree_entry(te, &view->regex)) {
5216 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5217 s->matched_entry = te;
5218 break;
5221 if (view->searching == TOG_SEARCH_FORWARD)
5222 te = got_tree_entry_get_next(s->tree, te);
5223 else
5224 te = got_tree_entry_get_prev(s->tree, te);
5227 if (s->matched_entry) {
5228 s->first_displayed_entry = s->matched_entry;
5229 s->selected = 0;
5232 return NULL;
5235 static const struct got_error *
5236 show_tree_view(struct tog_view *view)
5238 const struct got_error *err = NULL;
5239 struct tog_tree_view_state *s = &view->state.tree;
5240 char *parent_path;
5242 err = tree_entry_path(&parent_path, &s->parents, NULL);
5243 if (err)
5244 return err;
5246 err = draw_tree_entries(view, parent_path);
5247 free(parent_path);
5249 view_vborder(view);
5250 return err;
5253 static const struct got_error *
5254 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5256 const struct got_error *err = NULL;
5257 struct tog_tree_view_state *s = &view->state.tree;
5258 struct tog_view *log_view, *ref_view;
5259 int begin_x = 0;
5261 switch (ch) {
5262 case 'i':
5263 s->show_ids = !s->show_ids;
5264 break;
5265 case 'l':
5266 if (!s->selected_entry)
5267 break;
5268 if (view_is_parent_view(view))
5269 begin_x = view_split_begin_x(view->begin_x);
5270 err = log_selected_tree_entry(&log_view, begin_x, s);
5271 view->focussed = 0;
5272 log_view->focussed = 1;
5273 if (view_is_parent_view(view)) {
5274 err = view_close_child(view);
5275 if (err)
5276 return err;
5277 view_set_child(view, log_view);
5278 view->focus_child = 1;
5279 } else
5280 *new_view = log_view;
5281 break;
5282 case 'r':
5283 if (view_is_parent_view(view))
5284 begin_x = view_split_begin_x(view->begin_x);
5285 ref_view = view_open(view->nlines, view->ncols,
5286 view->begin_y, begin_x, TOG_VIEW_REF);
5287 if (ref_view == NULL)
5288 return got_error_from_errno("view_open");
5289 err = open_ref_view(ref_view, s->repo);
5290 if (err) {
5291 view_close(ref_view);
5292 return err;
5294 view->focussed = 0;
5295 ref_view->focussed = 1;
5296 if (view_is_parent_view(view)) {
5297 err = view_close_child(view);
5298 if (err)
5299 return err;
5300 view_set_child(view, ref_view);
5301 view->focus_child = 1;
5302 } else
5303 *new_view = ref_view;
5304 break;
5305 case 'k':
5306 case KEY_UP:
5307 if (s->selected > 0) {
5308 s->selected--;
5309 break;
5311 tree_scroll_up(s, 1);
5312 break;
5313 case KEY_PPAGE:
5314 case CTRL('b'):
5315 if (s->tree == s->root) {
5316 if (got_object_tree_get_first_entry(s->tree) ==
5317 s->first_displayed_entry)
5318 s->selected = 0;
5319 } else {
5320 if (s->first_displayed_entry == NULL)
5321 s->selected = 0;
5323 tree_scroll_up(s, MAX(0, view->nlines - 3));
5324 break;
5325 case 'j':
5326 case KEY_DOWN:
5327 if (s->selected < s->ndisplayed - 1) {
5328 s->selected++;
5329 break;
5331 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5332 == NULL)
5333 /* can't scroll any further */
5334 break;
5335 tree_scroll_down(s, 1);
5336 break;
5337 case KEY_NPAGE:
5338 case CTRL('f'):
5339 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5340 == NULL) {
5341 /* can't scroll any further; move cursor down */
5342 if (s->selected < s->ndisplayed - 1)
5343 s->selected = s->ndisplayed - 1;
5344 break;
5346 tree_scroll_down(s, view->nlines - 3);
5347 break;
5348 case KEY_ENTER:
5349 case '\r':
5350 case KEY_BACKSPACE:
5351 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5352 struct tog_parent_tree *parent;
5353 /* user selected '..' */
5354 if (s->tree == s->root)
5355 break;
5356 parent = TAILQ_FIRST(&s->parents);
5357 TAILQ_REMOVE(&s->parents, parent,
5358 entry);
5359 got_object_tree_close(s->tree);
5360 s->tree = parent->tree;
5361 s->first_displayed_entry =
5362 parent->first_displayed_entry;
5363 s->selected_entry =
5364 parent->selected_entry;
5365 s->selected = parent->selected;
5366 free(parent);
5367 } else if (S_ISDIR(got_tree_entry_get_mode(
5368 s->selected_entry))) {
5369 struct got_tree_object *subtree;
5370 err = got_object_open_as_tree(&subtree, s->repo,
5371 got_tree_entry_get_id(s->selected_entry));
5372 if (err)
5373 break;
5374 err = tree_view_visit_subtree(s, subtree);
5375 if (err) {
5376 got_object_tree_close(subtree);
5377 break;
5379 } else if (S_ISREG(got_tree_entry_get_mode(
5380 s->selected_entry))) {
5381 struct tog_view *blame_view;
5382 int begin_x = view_is_parent_view(view) ?
5383 view_split_begin_x(view->begin_x) : 0;
5385 err = blame_tree_entry(&blame_view, begin_x,
5386 s->selected_entry, &s->parents,
5387 s->commit_id, s->repo);
5388 if (err)
5389 break;
5390 view->focussed = 0;
5391 blame_view->focussed = 1;
5392 if (view_is_parent_view(view)) {
5393 err = view_close_child(view);
5394 if (err)
5395 return err;
5396 view_set_child(view, blame_view);
5397 view->focus_child = 1;
5398 } else
5399 *new_view = blame_view;
5401 break;
5402 case KEY_RESIZE:
5403 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5404 s->selected = view->nlines - 4;
5405 break;
5406 default:
5407 break;
5410 return err;
5413 __dead static void
5414 usage_tree(void)
5416 endwin();
5417 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5418 getprogname());
5419 exit(1);
5422 static const struct got_error *
5423 cmd_tree(int argc, char *argv[])
5425 const struct got_error *error;
5426 struct got_repository *repo = NULL;
5427 struct got_worktree *worktree = NULL;
5428 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5429 struct got_object_id *commit_id = NULL;
5430 const char *commit_id_arg = NULL;
5431 char *label = NULL;
5432 struct got_commit_object *commit = NULL;
5433 struct got_tree_object *tree = NULL;
5434 struct got_reference *ref = NULL;
5435 const char *head_ref_name = NULL;
5436 int ch;
5437 struct tog_view *view;
5439 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5440 switch (ch) {
5441 case 'c':
5442 commit_id_arg = optarg;
5443 break;
5444 case 'r':
5445 repo_path = realpath(optarg, NULL);
5446 if (repo_path == NULL)
5447 return got_error_from_errno2("realpath",
5448 optarg);
5449 break;
5450 default:
5451 usage_tree();
5452 /* NOTREACHED */
5456 argc -= optind;
5457 argv += optind;
5459 if (argc > 1)
5460 usage_tree();
5462 cwd = getcwd(NULL, 0);
5463 if (cwd == NULL)
5464 return got_error_from_errno("getcwd");
5466 error = got_worktree_open(&worktree, cwd);
5467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5468 goto done;
5470 if (repo_path == NULL) {
5471 if (worktree)
5472 repo_path =
5473 strdup(got_worktree_get_repo_path(worktree));
5474 else
5475 repo_path = strdup(cwd);
5477 if (repo_path == NULL) {
5478 error = got_error_from_errno("strdup");
5479 goto done;
5482 error = got_repo_open(&repo, repo_path, NULL);
5483 if (error != NULL)
5484 goto done;
5486 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5487 repo, worktree);
5488 if (error)
5489 goto done;
5491 init_curses();
5493 error = apply_unveil(got_repo_get_path(repo), NULL);
5494 if (error)
5495 goto done;
5497 if (commit_id_arg == NULL) {
5498 error = got_repo_match_object_id(&commit_id, &label,
5499 worktree ? got_worktree_get_head_ref_name(worktree) :
5500 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5501 if (error)
5502 goto done;
5503 head_ref_name = label;
5504 } else {
5505 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5506 if (error == NULL)
5507 head_ref_name = got_ref_get_name(ref);
5508 else if (error->code != GOT_ERR_NOT_REF)
5509 goto done;
5510 error = got_repo_match_object_id(&commit_id, NULL,
5511 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5512 if (error)
5513 goto done;
5516 error = got_object_open_as_commit(&commit, repo, commit_id);
5517 if (error)
5518 goto done;
5520 error = got_object_open_as_tree(&tree, repo,
5521 got_object_commit_get_tree_id(commit));
5522 if (error)
5523 goto done;
5525 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5526 if (view == NULL) {
5527 error = got_error_from_errno("view_open");
5528 goto done;
5530 error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5531 if (error)
5532 goto done;
5533 if (!got_path_is_root_dir(in_repo_path)) {
5534 error = tree_view_walk_path(&view->state.tree, commit_id,
5535 in_repo_path);
5536 if (error)
5537 goto done;
5540 if (worktree) {
5541 /* Release work tree lock. */
5542 got_worktree_close(worktree);
5543 worktree = NULL;
5545 error = view_loop(view);
5546 done:
5547 free(repo_path);
5548 free(cwd);
5549 free(commit_id);
5550 free(label);
5551 if (ref)
5552 got_ref_close(ref);
5553 if (commit)
5554 got_object_commit_close(commit);
5555 if (tree)
5556 got_object_tree_close(tree);
5557 if (repo)
5558 got_repo_close(repo);
5559 return error;
5562 static const struct got_error *
5563 ref_view_load_refs(struct tog_ref_view_state *s)
5565 const struct got_error *err;
5566 struct got_reflist_entry *sre;
5567 struct tog_reflist_entry *re;
5569 err = got_ref_list(&s->simplerefs, s->repo, NULL,
5570 got_ref_cmp_by_name, NULL);
5571 if (err)
5572 return err;
5574 s->nrefs = 0;
5575 SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5576 if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5577 continue;
5579 re = malloc(sizeof(*re));
5580 if (re == NULL)
5581 return got_error_from_errno("malloc");
5583 re->ref = sre->ref;
5584 re->idx = s->nrefs++;
5585 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5588 return NULL;
5591 void
5592 ref_view_free_refs(struct tog_ref_view_state *s)
5594 struct tog_reflist_entry *re;
5596 while (!TAILQ_EMPTY(&s->refs)) {
5597 re = TAILQ_FIRST(&s->refs);
5598 TAILQ_REMOVE(&s->refs, re, entry);
5599 free(re);
5601 got_ref_list_free(&s->simplerefs);
5604 static const struct got_error *
5605 open_ref_view(struct tog_view *view, struct got_repository *repo)
5607 const struct got_error *err = NULL;
5608 struct tog_ref_view_state *s = &view->state.ref;
5610 s->selected_entry = 0;
5611 s->repo = repo;
5613 SIMPLEQ_INIT(&s->simplerefs);
5614 TAILQ_INIT(&s->refs);
5615 SIMPLEQ_INIT(&s->colors);
5617 err = ref_view_load_refs(s);
5618 if (err)
5619 return err;
5621 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5623 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5624 err = add_color(&s->colors, "^refs/heads/",
5625 TOG_COLOR_REFS_HEADS,
5626 get_color_value("TOG_COLOR_REFS_HEADS"));
5627 if (err)
5628 goto done;
5630 err = add_color(&s->colors, "^refs/tags/",
5631 TOG_COLOR_REFS_TAGS,
5632 get_color_value("TOG_COLOR_REFS_TAGS"));
5633 if (err)
5634 goto done;
5636 err = add_color(&s->colors, "^refs/remotes/",
5637 TOG_COLOR_REFS_REMOTES,
5638 get_color_value("TOG_COLOR_REFS_REMOTES"));
5639 if (err)
5640 goto done;
5643 view->show = show_ref_view;
5644 view->input = input_ref_view;
5645 view->close = close_ref_view;
5646 view->search_start = search_start_ref_view;
5647 view->search_next = search_next_ref_view;
5648 done:
5649 if (err)
5650 free_colors(&s->colors);
5651 return err;
5654 static const struct got_error *
5655 close_ref_view(struct tog_view *view)
5657 struct tog_ref_view_state *s = &view->state.ref;
5659 ref_view_free_refs(s);
5660 free_colors(&s->colors);
5662 return NULL;
5665 static const struct got_error *
5666 resolve_reflist_entry(struct got_object_id **commit_id,
5667 struct tog_reflist_entry *re, struct got_repository *repo)
5669 const struct got_error *err = NULL;
5670 struct got_object_id *obj_id;
5671 struct got_tag_object *tag = NULL;
5672 int obj_type;
5674 *commit_id = NULL;
5676 err = got_ref_resolve(&obj_id, repo, re->ref);
5677 if (err)
5678 return err;
5680 err = got_object_get_type(&obj_type, repo, obj_id);
5681 if (err)
5682 goto done;
5684 switch (obj_type) {
5685 case GOT_OBJ_TYPE_COMMIT:
5686 *commit_id = obj_id;
5687 break;
5688 case GOT_OBJ_TYPE_TAG:
5689 err = got_object_open_as_tag(&tag, repo, obj_id);
5690 if (err)
5691 goto done;
5692 free(obj_id);
5693 err = got_object_get_type(&obj_type, repo,
5694 got_object_tag_get_object_id(tag));
5695 if (err)
5696 goto done;
5697 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5698 err = got_error(GOT_ERR_OBJ_TYPE);
5699 goto done;
5701 *commit_id = got_object_id_dup(
5702 got_object_tag_get_object_id(tag));
5703 if (*commit_id == NULL) {
5704 err = got_error_from_errno("got_object_id_dup");
5705 goto done;
5707 break;
5708 default:
5709 err = got_error(GOT_ERR_OBJ_TYPE);
5710 break;
5713 done:
5714 if (tag)
5715 got_object_tag_close(tag);
5716 if (err) {
5717 free(*commit_id);
5718 *commit_id = NULL;
5720 return err;
5723 static const struct got_error *
5724 log_ref_entry(struct tog_view **new_view, int begin_x,
5725 struct tog_reflist_entry *re, struct got_repository *repo)
5727 struct tog_view *log_view;
5728 const struct got_error *err = NULL;
5729 struct got_object_id *commit_id = NULL;
5731 *new_view = NULL;
5733 err = resolve_reflist_entry(&commit_id, re, repo);
5734 if (err) {
5735 if (err->code != GOT_ERR_OBJ_TYPE)
5736 return err;
5737 else
5738 return NULL;
5741 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5742 if (log_view == NULL) {
5743 err = got_error_from_errno("view_open");
5744 goto done;
5747 err = open_log_view(log_view, commit_id, repo,
5748 got_ref_get_name(re->ref), "", 0);
5749 done:
5750 if (err)
5751 view_close(log_view);
5752 else
5753 *new_view = log_view;
5754 free(commit_id);
5755 return err;
5758 static void
5759 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5761 struct tog_reflist_entry *re;
5762 int i = 0;
5764 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5765 return;
5767 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5768 while (i++ < maxscroll) {
5769 if (re == NULL)
5770 break;
5771 s->first_displayed_entry = re;
5772 re = TAILQ_PREV(re, tog_reflist_head, entry);
5776 static void
5777 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5779 struct tog_reflist_entry *next, *last;
5780 int n = 0;
5782 if (s->first_displayed_entry)
5783 next = TAILQ_NEXT(s->first_displayed_entry, entry);
5784 else
5785 next = TAILQ_FIRST(&s->refs);
5787 last = s->last_displayed_entry;
5788 while (next && last && n++ < maxscroll) {
5789 last = TAILQ_NEXT(last, entry);
5790 if (last) {
5791 s->first_displayed_entry = next;
5792 next = TAILQ_NEXT(next, entry);
5797 static const struct got_error *
5798 search_start_ref_view(struct tog_view *view)
5800 struct tog_ref_view_state *s = &view->state.ref;
5802 s->matched_entry = NULL;
5803 return NULL;
5806 static int
5807 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5809 regmatch_t regmatch;
5811 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
5812 0) == 0;
5815 static const struct got_error *
5816 search_next_ref_view(struct tog_view *view)
5818 struct tog_ref_view_state *s = &view->state.ref;
5819 struct tog_reflist_entry *re = NULL;
5821 if (!view->searching) {
5822 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5823 return NULL;
5826 if (s->matched_entry) {
5827 if (view->searching == TOG_SEARCH_FORWARD) {
5828 if (s->selected_entry)
5829 re = TAILQ_NEXT(s->selected_entry, entry);
5830 else
5831 re = TAILQ_PREV(s->selected_entry,
5832 tog_reflist_head, entry);
5833 } else {
5834 if (s->selected_entry == NULL)
5835 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5836 else
5837 re = TAILQ_PREV(s->selected_entry,
5838 tog_reflist_head, entry);
5840 } else {
5841 if (view->searching == TOG_SEARCH_FORWARD)
5842 re = TAILQ_FIRST(&s->refs);
5843 else
5844 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5847 while (1) {
5848 if (re == NULL) {
5849 if (s->matched_entry == NULL) {
5850 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5851 return NULL;
5853 if (view->searching == TOG_SEARCH_FORWARD)
5854 re = TAILQ_FIRST(&s->refs);
5855 else
5856 re = TAILQ_LAST(&s->refs, tog_reflist_head);
5859 if (match_reflist_entry(re, &view->regex)) {
5860 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5861 s->matched_entry = re;
5862 break;
5865 if (view->searching == TOG_SEARCH_FORWARD)
5866 re = TAILQ_NEXT(re, entry);
5867 else
5868 re = TAILQ_PREV(re, tog_reflist_head, entry);
5871 if (s->matched_entry) {
5872 s->first_displayed_entry = s->matched_entry;
5873 s->selected = 0;
5876 return NULL;
5879 static const struct got_error *
5880 show_ref_view(struct tog_view *view)
5882 const struct got_error *err = NULL;
5883 struct tog_ref_view_state *s = &view->state.ref;
5884 struct tog_reflist_entry *re;
5885 char *line = NULL;
5886 wchar_t *wline;
5887 struct tog_color *tc;
5888 int width, n;
5889 int limit = view->nlines;
5891 werase(view->window);
5893 s->ndisplayed = 0;
5895 if (limit == 0)
5896 return NULL;
5898 re = s->first_displayed_entry;
5900 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5901 s->nrefs) == -1)
5902 return got_error_from_errno("asprintf");
5904 err = format_line(&wline, &width, line, view->ncols, 0);
5905 if (err) {
5906 free(line);
5907 return err;
5909 if (view_needs_focus_indication(view))
5910 wstandout(view->window);
5911 waddwstr(view->window, wline);
5912 if (view_needs_focus_indication(view))
5913 wstandend(view->window);
5914 free(wline);
5915 wline = NULL;
5916 free(line);
5917 line = NULL;
5918 if (width < view->ncols - 1)
5919 waddch(view->window, '\n');
5920 if (--limit <= 0)
5921 return NULL;
5923 n = 0;
5924 while (re && limit > 0) {
5925 char *line = NULL;
5927 if (got_ref_is_symbolic(re->ref)) {
5928 if (asprintf(&line, "%s -> %s",
5929 got_ref_get_name(re->ref),
5930 got_ref_get_symref_target(re->ref)) == -1)
5931 return got_error_from_errno("asprintf");
5932 } else if (s->show_ids) {
5933 struct got_object_id *id;
5934 char *id_str;
5935 err = got_ref_resolve(&id, s->repo, re->ref);
5936 if (err)
5937 return err;
5938 err = got_object_id_str(&id_str, id);
5939 if (err) {
5940 free(id);
5941 return err;
5943 if (asprintf(&line, "%s: %s",
5944 got_ref_get_name(re->ref), id_str) == -1) {
5945 err = got_error_from_errno("asprintf");
5946 free(id);
5947 free(id_str);
5948 return err;
5950 free(id);
5951 free(id_str);
5952 } else {
5953 line = strdup(got_ref_get_name(re->ref));
5954 if (line == NULL)
5955 return got_error_from_errno("strdup");
5958 err = format_line(&wline, &width, line, view->ncols, 0);
5959 if (err) {
5960 free(line);
5961 return err;
5963 if (n == s->selected) {
5964 if (view->focussed)
5965 wstandout(view->window);
5966 s->selected_entry = re;
5968 tc = match_color(&s->colors, got_ref_get_name(re->ref));
5969 if (tc)
5970 wattr_on(view->window,
5971 COLOR_PAIR(tc->colorpair), NULL);
5972 waddwstr(view->window, wline);
5973 if (tc)
5974 wattr_off(view->window,
5975 COLOR_PAIR(tc->colorpair), NULL);
5976 if (width < view->ncols - 1)
5977 waddch(view->window, '\n');
5978 if (n == s->selected && view->focussed)
5979 wstandend(view->window);
5980 free(line);
5981 free(wline);
5982 wline = NULL;
5983 n++;
5984 s->ndisplayed++;
5985 s->last_displayed_entry = re;
5987 limit--;
5988 re = TAILQ_NEXT(re, entry);
5991 view_vborder(view);
5992 return err;
5995 static const struct got_error *
5996 browse_ref_tree(struct tog_view **new_view, int begin_x,
5997 struct tog_reflist_entry *re, struct got_repository *repo)
5999 const struct got_error *err = NULL;
6000 struct got_object_id *commit_id = NULL, *tree_id = NULL;
6001 struct got_tree_object *tree = NULL;
6002 struct tog_view *tree_view;
6004 *new_view = NULL;
6006 err = resolve_reflist_entry(&commit_id, re, repo);
6007 if (err) {
6008 if (err->code != GOT_ERR_OBJ_TYPE)
6009 return err;
6010 else
6011 return NULL;
6014 err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6015 if (err)
6016 goto done;
6018 err = got_object_open_as_tree(&tree, repo, tree_id);
6019 if (err)
6020 goto done;
6022 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6023 if (tree_view == NULL) {
6024 err = got_error_from_errno("view_open");
6025 goto done;
6028 err = open_tree_view(tree_view, tree, commit_id,
6029 got_ref_get_name(re->ref), repo);
6030 if (err)
6031 goto done;
6033 *new_view = tree_view;
6034 done:
6035 free(commit_id);
6036 free(tree_id);
6037 if (err) {
6038 if (tree)
6039 got_object_tree_close(tree);
6041 return err;
6043 static const struct got_error *
6044 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6046 const struct got_error *err = NULL;
6047 struct tog_ref_view_state *s = &view->state.ref;
6048 struct tog_view *log_view, *tree_view;
6049 int begin_x = 0;
6051 switch (ch) {
6052 case 'i':
6053 s->show_ids = !s->show_ids;
6054 break;
6055 case KEY_ENTER:
6056 case '\r':
6057 if (!s->selected_entry)
6058 break;
6059 if (view_is_parent_view(view))
6060 begin_x = view_split_begin_x(view->begin_x);
6061 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6062 s->repo);
6063 view->focussed = 0;
6064 log_view->focussed = 1;
6065 if (view_is_parent_view(view)) {
6066 err = view_close_child(view);
6067 if (err)
6068 return err;
6069 view_set_child(view, log_view);
6070 view->focus_child = 1;
6071 } else
6072 *new_view = log_view;
6073 break;
6074 case 't':
6075 if (!s->selected_entry)
6076 break;
6077 if (view_is_parent_view(view))
6078 begin_x = view_split_begin_x(view->begin_x);
6079 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6080 s->repo);
6081 if (err || tree_view == NULL)
6082 break;
6083 view->focussed = 0;
6084 tree_view->focussed = 1;
6085 if (view_is_parent_view(view)) {
6086 err = view_close_child(view);
6087 if (err)
6088 return err;
6089 view_set_child(view, tree_view);
6090 view->focus_child = 1;
6091 } else
6092 *new_view = tree_view;
6093 break;
6094 case 'k':
6095 case KEY_UP:
6096 if (s->selected > 0) {
6097 s->selected--;
6098 break;
6100 ref_scroll_up(s, 1);
6101 break;
6102 case KEY_PPAGE:
6103 case CTRL('b'):
6104 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6105 s->selected = 0;
6106 ref_scroll_up(s, MAX(0, view->nlines - 1));
6107 break;
6108 case 'j':
6109 case KEY_DOWN:
6110 if (s->selected < s->ndisplayed - 1) {
6111 s->selected++;
6112 break;
6114 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6115 /* can't scroll any further */
6116 break;
6117 ref_scroll_down(s, 1);
6118 break;
6119 case KEY_NPAGE:
6120 case CTRL('f'):
6121 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6122 /* can't scroll any further; move cursor down */
6123 if (s->selected < s->ndisplayed - 1)
6124 s->selected = s->ndisplayed - 1;
6125 break;
6127 ref_scroll_down(s, view->nlines - 1);
6128 break;
6129 case CTRL('l'):
6130 ref_view_free_refs(s);
6131 err = ref_view_load_refs(s);
6132 break;
6133 case KEY_RESIZE:
6134 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6135 s->selected = view->nlines - 2;
6136 break;
6137 default:
6138 break;
6141 return err;
6144 __dead static void
6145 usage_ref(void)
6147 endwin();
6148 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6149 getprogname());
6150 exit(1);
6153 static const struct got_error *
6154 cmd_ref(int argc, char *argv[])
6156 const struct got_error *error;
6157 struct got_repository *repo = NULL;
6158 struct got_worktree *worktree = NULL;
6159 char *cwd = NULL, *repo_path = NULL;
6160 int ch;
6161 struct tog_view *view;
6163 while ((ch = getopt(argc, argv, "r:")) != -1) {
6164 switch (ch) {
6165 case 'r':
6166 repo_path = realpath(optarg, NULL);
6167 if (repo_path == NULL)
6168 return got_error_from_errno2("realpath",
6169 optarg);
6170 break;
6171 default:
6172 usage_ref();
6173 /* NOTREACHED */
6177 argc -= optind;
6178 argv += optind;
6180 if (argc > 1)
6181 usage_ref();
6183 cwd = getcwd(NULL, 0);
6184 if (cwd == NULL)
6185 return got_error_from_errno("getcwd");
6187 error = got_worktree_open(&worktree, cwd);
6188 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6189 goto done;
6191 if (repo_path == NULL) {
6192 if (worktree)
6193 repo_path =
6194 strdup(got_worktree_get_repo_path(worktree));
6195 else
6196 repo_path = strdup(cwd);
6198 if (repo_path == NULL) {
6199 error = got_error_from_errno("strdup");
6200 goto done;
6203 error = got_repo_open(&repo, repo_path, NULL);
6204 if (error != NULL)
6205 goto done;
6207 init_curses();
6209 error = apply_unveil(got_repo_get_path(repo), NULL);
6210 if (error)
6211 goto done;
6213 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6214 if (view == NULL) {
6215 error = got_error_from_errno("view_open");
6216 goto done;
6219 error = open_ref_view(view, repo);
6220 if (error)
6221 goto done;
6223 if (worktree) {
6224 /* Release work tree lock. */
6225 got_worktree_close(worktree);
6226 worktree = NULL;
6228 error = view_loop(view);
6229 done:
6230 free(repo_path);
6231 free(cwd);
6232 if (repo)
6233 got_repo_close(repo);
6234 return error;
6237 static void
6238 list_commands(FILE *fp)
6240 int i;
6242 fprintf(fp, "commands:");
6243 for (i = 0; i < nitems(tog_commands); i++) {
6244 struct tog_cmd *cmd = &tog_commands[i];
6245 fprintf(fp, " %s", cmd->name);
6247 fputc('\n', fp);
6250 __dead static void
6251 usage(int hflag, int status)
6253 FILE *fp = (status == 0) ? stdout : stderr;
6255 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6256 getprogname());
6257 if (hflag) {
6258 fprintf(fp, "lazy usage: %s path\n", getprogname());
6259 list_commands(fp);
6261 exit(status);
6264 static char **
6265 make_argv(int argc, ...)
6267 va_list ap;
6268 char **argv;
6269 int i;
6271 va_start(ap, argc);
6273 argv = calloc(argc, sizeof(char *));
6274 if (argv == NULL)
6275 err(1, "calloc");
6276 for (i = 0; i < argc; i++) {
6277 argv[i] = strdup(va_arg(ap, char *));
6278 if (argv[i] == NULL)
6279 err(1, "strdup");
6282 va_end(ap);
6283 return argv;
6287 * Try to convert 'tog path' into a 'tog log path' command.
6288 * The user could simply have mistyped the command rather than knowingly
6289 * provided a path. So check whether argv[0] can in fact be resolved
6290 * to a path in the HEAD commit and print a special error if not.
6291 * This hack is for mpi@ <3
6293 static const struct got_error *
6294 tog_log_with_path(int argc, char *argv[])
6296 const struct got_error *error = NULL;
6297 struct tog_cmd *cmd = NULL;
6298 struct got_repository *repo = NULL;
6299 struct got_worktree *worktree = NULL;
6300 struct got_object_id *commit_id = NULL, *id = NULL;
6301 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6302 char *commit_id_str = NULL, **cmd_argv = NULL;
6304 cwd = getcwd(NULL, 0);
6305 if (cwd == NULL)
6306 return got_error_from_errno("getcwd");
6308 error = got_worktree_open(&worktree, cwd);
6309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6310 goto done;
6312 if (worktree)
6313 repo_path = strdup(got_worktree_get_repo_path(worktree));
6314 else
6315 repo_path = strdup(cwd);
6316 if (repo_path == NULL) {
6317 error = got_error_from_errno("strdup");
6318 goto done;
6321 error = got_repo_open(&repo, repo_path, NULL);
6322 if (error != NULL)
6323 goto done;
6325 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6326 repo, worktree);
6327 if (error)
6328 goto done;
6330 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6331 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6332 GOT_OBJ_TYPE_COMMIT, 1, repo);
6333 if (error)
6334 goto done;
6336 if (worktree) {
6337 got_worktree_close(worktree);
6338 worktree = NULL;
6341 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6342 if (error) {
6343 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6344 goto done;
6345 fprintf(stderr, "%s: '%s' is no known command or path\n",
6346 getprogname(), argv[0]);
6347 usage(1, 1);
6348 /* not reached */
6351 got_repo_close(repo);
6352 repo = NULL;
6354 error = got_object_id_str(&commit_id_str, commit_id);
6355 if (error)
6356 goto done;
6358 cmd = &tog_commands[0]; /* log */
6359 argc = 4;
6360 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6361 error = cmd->cmd_main(argc, cmd_argv);
6362 done:
6363 if (repo)
6364 got_repo_close(repo);
6365 if (worktree)
6366 got_worktree_close(worktree);
6367 free(id);
6368 free(commit_id_str);
6369 free(commit_id);
6370 free(cwd);
6371 free(repo_path);
6372 free(in_repo_path);
6373 if (cmd_argv) {
6374 int i;
6375 for (i = 0; i < argc; i++)
6376 free(cmd_argv[i]);
6377 free(cmd_argv);
6379 return error;
6382 int
6383 main(int argc, char *argv[])
6385 const struct got_error *error = NULL;
6386 struct tog_cmd *cmd = NULL;
6387 int ch, hflag = 0, Vflag = 0;
6388 char **cmd_argv = NULL;
6389 static struct option longopts[] = {
6390 { "version", no_argument, NULL, 'V' },
6391 { NULL, 0, NULL, 0}
6394 setlocale(LC_CTYPE, "");
6396 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6397 switch (ch) {
6398 case 'h':
6399 hflag = 1;
6400 break;
6401 case 'V':
6402 Vflag = 1;
6403 break;
6404 default:
6405 usage(hflag, 1);
6406 /* NOTREACHED */
6410 argc -= optind;
6411 argv += optind;
6412 optind = 1;
6413 optreset = 1;
6415 if (Vflag) {
6416 got_version_print_str();
6417 return 0;
6420 #ifndef PROFILE
6421 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6422 NULL) == -1)
6423 err(1, "pledge");
6424 #endif
6426 if (argc == 0) {
6427 if (hflag)
6428 usage(hflag, 0);
6429 /* Build an argument vector which runs a default command. */
6430 cmd = &tog_commands[0];
6431 argc = 1;
6432 cmd_argv = make_argv(argc, cmd->name);
6433 } else {
6434 int i;
6436 /* Did the user specify a command? */
6437 for (i = 0; i < nitems(tog_commands); i++) {
6438 if (strncmp(tog_commands[i].name, argv[0],
6439 strlen(argv[0])) == 0) {
6440 cmd = &tog_commands[i];
6441 break;
6446 if (cmd == NULL) {
6447 if (argc != 1)
6448 usage(0, 1);
6449 /* No command specified; try log with a path */
6450 error = tog_log_with_path(argc, argv);
6451 } else {
6452 if (hflag)
6453 cmd->cmd_usage();
6454 else
6455 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6458 endwin();
6459 putchar('\n');
6460 if (cmd_argv) {
6461 int i;
6462 for (i = 0; i < argc; i++)
6463 free(cmd_argv[i]);
6464 free(cmd_argv);
6467 if (error && error->code != GOT_ERR_CANCELLED)
6468 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6469 return 0;