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 /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.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 <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.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 const 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 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int *pack_fds;
342 int log_complete;
343 sig_atomic_t *quit;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
346 int *searching;
347 int *search_next_done;
348 regex_t *regex;
349 };
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
356 int selected;
357 char *in_repo_path;
358 char *head_ref_name;
359 int log_branches;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
362 sig_atomic_t quit;
363 pthread_t thread;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
368 };
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
388 int nlines;
390 struct tog_view *view;
391 struct got_object_id *commit_id;
392 int *quit;
393 };
395 struct tog_blame_thread_args {
396 const char *path;
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
399 int *complete;
400 got_cancel_cb cancel_cb;
401 void *cancel_arg;
402 };
404 struct tog_blame {
405 FILE *f;
406 off_t filesize;
407 struct tog_blame_line *lines;
408 int nlines;
409 off_t *line_offsets;
410 pthread_t thread;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
413 const char *path;
414 int *pack_fds;
415 };
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
420 int selected_line;
421 int blame_complete;
422 int eof;
423 int done;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
426 char *path;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
430 int matched_line;
431 struct tog_colors colors;
432 };
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
439 int selected;
440 };
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
445 char *tree_label;
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 char *head_ref_name;
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
458 };
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
463 int idx;
464 };
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 /*
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
495 */
496 TAILQ_HEAD(tog_view_list_head, tog_view);
498 struct tog_view {
499 TAILQ_ENTRY(tog_view) entry;
500 WINDOW *window;
501 PANEL *panel;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
506 int dying;
507 struct tog_view *parent;
508 struct tog_view *child;
510 /*
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
517 * situations.
518 */
519 int focus_child;
521 /* type-specific state */
522 enum tog_view_type type;
523 union {
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
529 } state;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
538 int search_started;
539 int searching;
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
546 regex_t regex;
547 regmatch_t regmatch;
548 };
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
604 static void
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
610 static void
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
616 static void
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
622 static void
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
628 static void
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
634 static int
635 tog_fatal_signal_received()
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
647 if (view->child) {
648 view_close(view->child);
649 view->child = NULL;
651 if (view->close)
652 err = view->close(view);
653 if (view->panel)
654 del_panel(view->panel);
655 if (view->window)
656 delwin(view->window);
657 free(view);
658 return err;
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
667 if (view == NULL)
668 return NULL;
670 view->type = type;
671 view->lines = LINES;
672 view->cols = COLS;
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
679 view_close(view);
680 return NULL;
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
685 view_close(view);
686 return NULL;
689 keypad(view->window, TRUE);
690 return view;
693 static int
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
697 return 0;
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
708 view->begin_y = 0;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
712 view->lines = LINES;
713 view->cols = COLS;
714 err = view_resize(view);
715 if (err)
716 return err;
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
721 return NULL;
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
729 view->begin_x = 0;
730 view->begin_y = 0;
731 view->nlines = LINES;
732 view->ncols = COLS;
733 view->lines = LINES;
734 view->cols = COLS;
735 err = view_resize(view);
736 if (err)
737 return err;
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
742 return NULL;
745 static int
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static const struct got_error *
752 view_resize(struct tog_view *view)
754 int nlines, ncols;
756 if (view->lines > LINES)
757 nlines = view->nlines - (view->lines - LINES);
758 else
759 nlines = view->nlines + (LINES - view->lines);
761 if (view->cols > COLS)
762 ncols = view->ncols - (view->cols - COLS);
763 else
764 ncols = view->ncols + (COLS - view->cols);
766 if (wresize(view->window, nlines, ncols) == ERR)
767 return got_error_from_errno("wresize");
768 if (replace_panel(view->panel, view->window) == ERR)
769 return got_error_from_errno("replace_panel");
770 wclear(view->window);
772 view->nlines = nlines;
773 view->ncols = ncols;
774 view->lines = LINES;
775 view->cols = COLS;
777 if (view->child) {
778 view->child->begin_x = view_split_begin_x(view->begin_x);
779 if (view->child->begin_x == 0) {
780 view_fullscreen(view->child);
781 if (view->child->focussed)
782 show_panel(view->child->panel);
783 else
784 show_panel(view->panel);
785 } else {
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
791 return NULL;
794 static const struct got_error *
795 view_close_child(struct tog_view *view)
797 const struct got_error *err = NULL;
799 if (view->child == NULL)
800 return NULL;
802 err = view_close(view->child);
803 view->child = NULL;
804 return err;
807 static void
808 view_set_child(struct tog_view *view, struct tog_view *child)
810 view->child = child;
811 child->parent = view;
814 static int
815 view_is_splitscreen(struct tog_view *view)
817 return view->begin_x > 0;
820 static void
821 tog_resizeterm(void)
823 int cols, lines;
824 struct winsize size;
826 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
827 cols = 80; /* Default */
828 lines = 24;
829 } else {
830 cols = size.ws_col;
831 lines = size.ws_row;
833 resize_term(lines, cols);
836 static const struct got_error *
837 view_search_start(struct tog_view *view)
839 const struct got_error *err = NULL;
840 char pattern[1024];
841 int ret;
843 if (view->search_started) {
844 regfree(&view->regex);
845 view->searching = 0;
846 memset(&view->regmatch, 0, sizeof(view->regmatch));
848 view->search_started = 0;
850 if (view->nlines < 1)
851 return NULL;
853 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
854 wclrtoeol(view->window);
856 nocbreak();
857 echo();
858 ret = wgetnstr(view->window, pattern, sizeof(pattern));
859 cbreak();
860 noecho();
861 if (ret == ERR)
862 return NULL;
864 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
865 err = view->search_start(view);
866 if (err) {
867 regfree(&view->regex);
868 return err;
870 view->search_started = 1;
871 view->searching = TOG_SEARCH_FORWARD;
872 view->search_next_done = 0;
873 view->search_next(view);
876 return NULL;
879 static const struct got_error *
880 view_input(struct tog_view **new, int *done, struct tog_view *view,
881 struct tog_view_list_head *views)
883 const struct got_error *err = NULL;
884 struct tog_view *v;
885 int ch, errcode;
887 *new = NULL;
889 /* Clear "no matches" indicator. */
890 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
891 view->search_next_done == TOG_SEARCH_HAVE_NONE)
892 view->search_next_done = TOG_SEARCH_HAVE_MORE;
894 if (view->searching && !view->search_next_done) {
895 errcode = pthread_mutex_unlock(&tog_mutex);
896 if (errcode)
897 return got_error_set_errno(errcode,
898 "pthread_mutex_unlock");
899 sched_yield();
900 errcode = pthread_mutex_lock(&tog_mutex);
901 if (errcode)
902 return got_error_set_errno(errcode,
903 "pthread_mutex_lock");
904 view->search_next(view);
905 return NULL;
908 nodelay(stdscr, FALSE);
909 /* Allow threads to make progress while we are waiting for input. */
910 errcode = pthread_mutex_unlock(&tog_mutex);
911 if (errcode)
912 return got_error_set_errno(errcode, "pthread_mutex_unlock");
913 ch = wgetch(view->window);
914 errcode = pthread_mutex_lock(&tog_mutex);
915 if (errcode)
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
917 nodelay(stdscr, TRUE);
919 if (tog_sigwinch_received || tog_sigcont_received) {
920 tog_resizeterm();
921 tog_sigwinch_received = 0;
922 tog_sigcont_received = 0;
923 TAILQ_FOREACH(v, views, entry) {
924 err = view_resize(v);
925 if (err)
926 return err;
927 err = v->input(new, v, KEY_RESIZE);
928 if (err)
929 return err;
930 if (v->child) {
931 err = view_resize(v->child);
932 if (err)
933 return err;
934 err = v->child->input(new, v->child,
935 KEY_RESIZE);
936 if (err)
937 return err;
942 switch (ch) {
943 case '\t':
944 if (view->child) {
945 view->focussed = 0;
946 view->child->focussed = 1;
947 view->focus_child = 1;
948 } else if (view->parent) {
949 view->focussed = 0;
950 view->parent->focussed = 1;
951 view->parent->focus_child = 0;
953 break;
954 case 'q':
955 err = view->input(new, view, ch);
956 view->dying = 1;
957 break;
958 case 'Q':
959 *done = 1;
960 break;
961 case 'f':
962 if (view_is_parent_view(view)) {
963 if (view->child == NULL)
964 break;
965 if (view_is_splitscreen(view->child)) {
966 view->focussed = 0;
967 view->child->focussed = 1;
968 err = view_fullscreen(view->child);
969 } else
970 err = view_splitscreen(view->child);
971 if (err)
972 break;
973 err = view->child->input(new, view->child,
974 KEY_RESIZE);
975 } else {
976 if (view_is_splitscreen(view)) {
977 view->parent->focussed = 0;
978 view->focussed = 1;
979 err = view_fullscreen(view);
980 } else {
981 err = view_splitscreen(view);
983 if (err)
984 break;
985 err = view->input(new, view, KEY_RESIZE);
987 break;
988 case KEY_RESIZE:
989 break;
990 case '/':
991 if (view->search_start)
992 view_search_start(view);
993 else
994 err = view->input(new, view, ch);
995 break;
996 case 'N':
997 case 'n':
998 if (view->search_started && view->search_next) {
999 view->searching = (ch == 'n' ?
1000 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1001 view->search_next_done = 0;
1002 view->search_next(view);
1003 } else
1004 err = view->input(new, view, ch);
1005 break;
1006 default:
1007 err = view->input(new, view, ch);
1008 break;
1011 return err;
1014 void
1015 view_vborder(struct tog_view *view)
1017 PANEL *panel;
1018 const struct tog_view *view_above;
1020 if (view->parent)
1021 return view_vborder(view->parent);
1023 panel = panel_above(view->panel);
1024 if (panel == NULL)
1025 return;
1027 view_above = panel_userptr(panel);
1028 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1029 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1032 int
1033 view_needs_focus_indication(struct tog_view *view)
1035 if (view_is_parent_view(view)) {
1036 if (view->child == NULL || view->child->focussed)
1037 return 0;
1038 if (!view_is_splitscreen(view->child))
1039 return 0;
1040 } else if (!view_is_splitscreen(view))
1041 return 0;
1043 return view->focussed;
1046 static const struct got_error *
1047 view_loop(struct tog_view *view)
1049 const struct got_error *err = NULL;
1050 struct tog_view_list_head views;
1051 struct tog_view *new_view;
1052 int fast_refresh = 10;
1053 int done = 0, errcode;
1055 errcode = pthread_mutex_lock(&tog_mutex);
1056 if (errcode)
1057 return got_error_set_errno(errcode, "pthread_mutex_lock");
1059 TAILQ_INIT(&views);
1060 TAILQ_INSERT_HEAD(&views, view, entry);
1062 view->focussed = 1;
1063 err = view->show(view);
1064 if (err)
1065 return err;
1066 update_panels();
1067 doupdate();
1068 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1069 /* Refresh fast during initialization, then become slower. */
1070 if (fast_refresh && fast_refresh-- == 0)
1071 halfdelay(10); /* switch to once per second */
1073 err = view_input(&new_view, &done, view, &views);
1074 if (err)
1075 break;
1076 if (view->dying) {
1077 struct tog_view *v, *prev = NULL;
1079 if (view_is_parent_view(view))
1080 prev = TAILQ_PREV(view, tog_view_list_head,
1081 entry);
1082 else if (view->parent)
1083 prev = view->parent;
1085 if (view->parent) {
1086 view->parent->child = NULL;
1087 view->parent->focus_child = 0;
1088 } else
1089 TAILQ_REMOVE(&views, view, entry);
1091 err = view_close(view);
1092 if (err)
1093 goto done;
1095 view = NULL;
1096 TAILQ_FOREACH(v, &views, entry) {
1097 if (v->focussed)
1098 break;
1100 if (view == NULL && new_view == NULL) {
1101 /* No view has focus. Try to pick one. */
1102 if (prev)
1103 view = prev;
1104 else if (!TAILQ_EMPTY(&views)) {
1105 view = TAILQ_LAST(&views,
1106 tog_view_list_head);
1108 if (view) {
1109 if (view->focus_child) {
1110 view->child->focussed = 1;
1111 view = view->child;
1112 } else
1113 view->focussed = 1;
1117 if (new_view) {
1118 struct tog_view *v, *t;
1119 /* Only allow one parent view per type. */
1120 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1121 if (v->type != new_view->type)
1122 continue;
1123 TAILQ_REMOVE(&views, v, entry);
1124 err = view_close(v);
1125 if (err)
1126 goto done;
1127 break;
1129 TAILQ_INSERT_TAIL(&views, new_view, entry);
1130 view = new_view;
1132 if (view) {
1133 if (view_is_parent_view(view)) {
1134 if (view->child && view->child->focussed)
1135 view = view->child;
1136 } else {
1137 if (view->parent && view->parent->focussed)
1138 view = view->parent;
1140 show_panel(view->panel);
1141 if (view->child && view_is_splitscreen(view->child))
1142 show_panel(view->child->panel);
1143 if (view->parent && view_is_splitscreen(view)) {
1144 err = view->parent->show(view->parent);
1145 if (err)
1146 goto done;
1148 err = view->show(view);
1149 if (err)
1150 goto done;
1151 if (view->child) {
1152 err = view->child->show(view->child);
1153 if (err)
1154 goto done;
1156 update_panels();
1157 doupdate();
1160 done:
1161 while (!TAILQ_EMPTY(&views)) {
1162 view = TAILQ_FIRST(&views);
1163 TAILQ_REMOVE(&views, view, entry);
1164 view_close(view);
1167 errcode = pthread_mutex_unlock(&tog_mutex);
1168 if (errcode && err == NULL)
1169 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1171 return err;
1174 __dead static void
1175 usage_log(void)
1177 endwin();
1178 fprintf(stderr,
1179 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1180 getprogname());
1181 exit(1);
1184 /* Create newly allocated wide-character string equivalent to a byte string. */
1185 static const struct got_error *
1186 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1188 char *vis = NULL;
1189 const struct got_error *err = NULL;
1191 *ws = NULL;
1192 *wlen = mbstowcs(NULL, s, 0);
1193 if (*wlen == (size_t)-1) {
1194 int vislen;
1195 if (errno != EILSEQ)
1196 return got_error_from_errno("mbstowcs");
1198 /* byte string invalid in current encoding; try to "fix" it */
1199 err = got_mbsavis(&vis, &vislen, s);
1200 if (err)
1201 return err;
1202 *wlen = mbstowcs(NULL, vis, 0);
1203 if (*wlen == (size_t)-1) {
1204 err = got_error_from_errno("mbstowcs"); /* give up */
1205 goto done;
1209 *ws = calloc(*wlen + 1, sizeof(**ws));
1210 if (*ws == NULL) {
1211 err = got_error_from_errno("calloc");
1212 goto done;
1215 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1216 err = got_error_from_errno("mbstowcs");
1217 done:
1218 free(vis);
1219 if (err) {
1220 free(*ws);
1221 *ws = NULL;
1222 *wlen = 0;
1224 return err;
1227 static const struct got_error *
1228 expand_tab(char **ptr, const char *src)
1230 char *dst;
1231 size_t len, n, idx = 0, sz = 0;
1233 *ptr = NULL;
1234 n = len = strlen(src);
1235 dst = malloc((n + 1) * sizeof(char));
1236 if (dst == NULL)
1237 return got_error_from_errno("malloc");
1239 while (idx < len && src[idx]) {
1240 const char c = src[idx];
1242 if (c == '\t') {
1243 size_t nb = TABSIZE - sz % TABSIZE;
1244 n += nb;
1245 dst = reallocarray(dst, n, sizeof(char));
1246 if (dst == NULL)
1247 return got_error_from_errno("reallocarray");
1248 memcpy(dst + sz, " ", nb);
1249 sz += nb;
1250 } else
1251 dst[sz++] = src[idx];
1252 ++idx;
1255 dst[sz] = '\0';
1256 *ptr = dst;
1257 return NULL;
1260 /* Format a line for display, ensuring that it won't overflow a width limit. */
1261 static const struct got_error *
1262 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1263 int col_tab_align, int expand)
1265 const struct got_error *err = NULL;
1266 int cols = 0;
1267 wchar_t *wline = NULL;
1268 char *exstr = NULL;
1269 size_t wlen;
1270 int i;
1272 *wlinep = NULL;
1273 *widthp = 0;
1275 if (expand) {
1276 err = expand_tab(&exstr, line);
1277 if (err)
1278 return err;
1281 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1282 free(exstr);
1283 if (err)
1284 return err;
1286 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1287 wline[wlen - 1] = L'\0';
1288 wlen--;
1290 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1291 wline[wlen - 1] = L'\0';
1292 wlen--;
1295 i = 0;
1296 while (i < wlen) {
1297 int width = wcwidth(wline[i]);
1299 if (width == 0) {
1300 i++;
1301 continue;
1304 if (width == 1 || width == 2) {
1305 if (cols + width > wlimit)
1306 break;
1307 cols += width;
1308 i++;
1309 } else if (width == -1) {
1310 if (wline[i] == L'\t') {
1311 width = TABSIZE -
1312 ((cols + col_tab_align) % TABSIZE);
1313 } else {
1314 width = 1;
1315 wline[i] = L'.';
1317 if (cols + width > wlimit)
1318 break;
1319 cols += width;
1320 i++;
1321 } else {
1322 err = got_error_from_errno("wcwidth");
1323 goto done;
1326 wline[i] = L'\0';
1327 if (widthp)
1328 *widthp = cols;
1329 done:
1330 if (err)
1331 free(wline);
1332 else
1333 *wlinep = wline;
1334 return err;
1337 /* Skip the leading nscroll columns of a wide character string. */
1338 const struct got_error *
1339 scroll_wline(wchar_t **wlinep, wchar_t *wline, int nscroll,
1340 int col_tab_align)
1342 int cols = 0;
1343 size_t wlen = wcslen(wline);
1344 int i = 0, j = 0;
1346 *wlinep = wline;
1348 while (i < wlen && cols < nscroll) {
1349 int width = wcwidth(wline[i]);
1351 if (width == 0) {
1352 i++;
1353 continue;
1356 if (width == 1 || width == 2) {
1357 if (cols + width > nscroll)
1358 break;
1359 cols += width;
1360 i++;
1361 } else if (width == -1) {
1362 if (wline[i] == L'\t') {
1363 width = TABSIZE -
1364 ((cols + col_tab_align) % TABSIZE);
1365 } else {
1366 width = 1;
1367 wline[i] = L'.';
1369 if (cols + width > nscroll)
1370 break;
1371 cols += width;
1372 i++;
1373 } else
1374 return got_error_from_errno("wcwidth");
1375 j++;
1378 *wlinep = &wline[j];
1379 return NULL;
1382 static const struct got_error*
1383 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1384 struct got_object_id *id, struct got_repository *repo)
1386 static const struct got_error *err = NULL;
1387 struct got_reflist_entry *re;
1388 char *s;
1389 const char *name;
1391 *refs_str = NULL;
1393 TAILQ_FOREACH(re, refs, entry) {
1394 struct got_tag_object *tag = NULL;
1395 struct got_object_id *ref_id;
1396 int cmp;
1398 name = got_ref_get_name(re->ref);
1399 if (strcmp(name, GOT_REF_HEAD) == 0)
1400 continue;
1401 if (strncmp(name, "refs/", 5) == 0)
1402 name += 5;
1403 if (strncmp(name, "got/", 4) == 0 &&
1404 strncmp(name, "got/backup/", 11) != 0)
1405 continue;
1406 if (strncmp(name, "heads/", 6) == 0)
1407 name += 6;
1408 if (strncmp(name, "remotes/", 8) == 0) {
1409 name += 8;
1410 s = strstr(name, "/" GOT_REF_HEAD);
1411 if (s != NULL && s[strlen(s)] == '\0')
1412 continue;
1414 err = got_ref_resolve(&ref_id, repo, re->ref);
1415 if (err)
1416 break;
1417 if (strncmp(name, "tags/", 5) == 0) {
1418 err = got_object_open_as_tag(&tag, repo, ref_id);
1419 if (err) {
1420 if (err->code != GOT_ERR_OBJ_TYPE) {
1421 free(ref_id);
1422 break;
1424 /* Ref points at something other than a tag. */
1425 err = NULL;
1426 tag = NULL;
1429 cmp = got_object_id_cmp(tag ?
1430 got_object_tag_get_object_id(tag) : ref_id, id);
1431 free(ref_id);
1432 if (tag)
1433 got_object_tag_close(tag);
1434 if (cmp != 0)
1435 continue;
1436 s = *refs_str;
1437 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1438 s ? ", " : "", name) == -1) {
1439 err = got_error_from_errno("asprintf");
1440 free(s);
1441 *refs_str = NULL;
1442 break;
1444 free(s);
1447 return err;
1450 static const struct got_error *
1451 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1452 int col_tab_align)
1454 char *smallerthan;
1456 smallerthan = strchr(author, '<');
1457 if (smallerthan && smallerthan[1] != '\0')
1458 author = smallerthan + 1;
1459 author[strcspn(author, "@>")] = '\0';
1460 return format_line(wauthor, author_width, author, limit, col_tab_align,
1461 0);
1464 static const struct got_error *
1465 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1466 struct got_object_id *id, const size_t date_display_cols,
1467 int author_display_cols)
1469 struct tog_log_view_state *s = &view->state.log;
1470 const struct got_error *err = NULL;
1471 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1472 char *logmsg0 = NULL, *logmsg = NULL;
1473 char *author = NULL;
1474 wchar_t *wlogmsg = NULL, *wauthor = NULL, *scrolled_wline;
1475 int author_width, logmsg_width;
1476 char *newline, *line = NULL;
1477 int col, limit;
1478 const int avail = view->ncols;
1479 struct tm tm;
1480 time_t committer_time;
1481 struct tog_color *tc;
1483 committer_time = got_object_commit_get_committer_time(commit);
1484 if (gmtime_r(&committer_time, &tm) == NULL)
1485 return got_error_from_errno("gmtime_r");
1486 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1487 return got_error(GOT_ERR_NO_SPACE);
1489 if (avail <= date_display_cols)
1490 limit = MIN(sizeof(datebuf) - 1, avail);
1491 else
1492 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1493 tc = get_color(&s->colors, TOG_COLOR_DATE);
1494 if (tc)
1495 wattr_on(view->window,
1496 COLOR_PAIR(tc->colorpair), NULL);
1497 waddnstr(view->window, datebuf, limit);
1498 if (tc)
1499 wattr_off(view->window,
1500 COLOR_PAIR(tc->colorpair), NULL);
1501 col = limit;
1502 if (col > avail)
1503 goto done;
1505 if (avail >= 120) {
1506 char *id_str;
1507 err = got_object_id_str(&id_str, id);
1508 if (err)
1509 goto done;
1510 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1511 if (tc)
1512 wattr_on(view->window,
1513 COLOR_PAIR(tc->colorpair), NULL);
1514 wprintw(view->window, "%.8s ", id_str);
1515 if (tc)
1516 wattr_off(view->window,
1517 COLOR_PAIR(tc->colorpair), NULL);
1518 free(id_str);
1519 col += 9;
1520 if (col > avail)
1521 goto done;
1524 author = strdup(got_object_commit_get_author(commit));
1525 if (author == NULL) {
1526 err = got_error_from_errno("strdup");
1527 goto done;
1529 err = format_author(&wauthor, &author_width, author, avail - col, col);
1530 if (err)
1531 goto done;
1532 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1533 if (tc)
1534 wattr_on(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1536 waddwstr(view->window, wauthor);
1537 if (tc)
1538 wattr_off(view->window,
1539 COLOR_PAIR(tc->colorpair), NULL);
1540 col += author_width;
1541 while (col < avail && author_width < author_display_cols + 2) {
1542 waddch(view->window, ' ');
1543 col++;
1544 author_width++;
1546 if (col > avail)
1547 goto done;
1549 err = got_object_commit_get_logmsg(&logmsg0, commit);
1550 if (err)
1551 goto done;
1552 logmsg = logmsg0;
1553 while (*logmsg == '\n')
1554 logmsg++;
1555 newline = strchr(logmsg, '\n');
1556 if (newline)
1557 *newline = '\0';
1558 limit = view->x + avail - col;
1559 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col, 1);
1560 if (err)
1561 goto done;
1562 err = scroll_wline(&scrolled_wline, wlogmsg, view->x, col);
1563 if (err)
1564 goto done;
1565 waddwstr(view->window, scrolled_wline);
1566 logmsg_width = wcswidth(scrolled_wline, wcslen(scrolled_wline));
1567 col += MAX(logmsg_width, 0);
1568 while (col < avail) {
1569 waddch(view->window, ' ');
1570 col++;
1572 done:
1573 free(logmsg0);
1574 free(wlogmsg);
1575 free(author);
1576 free(wauthor);
1577 free(line);
1578 return err;
1581 static struct commit_queue_entry *
1582 alloc_commit_queue_entry(struct got_commit_object *commit,
1583 struct got_object_id *id)
1585 struct commit_queue_entry *entry;
1587 entry = calloc(1, sizeof(*entry));
1588 if (entry == NULL)
1589 return NULL;
1591 entry->id = id;
1592 entry->commit = commit;
1593 return entry;
1596 static void
1597 pop_commit(struct commit_queue *commits)
1599 struct commit_queue_entry *entry;
1601 entry = TAILQ_FIRST(&commits->head);
1602 TAILQ_REMOVE(&commits->head, entry, entry);
1603 got_object_commit_close(entry->commit);
1604 commits->ncommits--;
1605 /* Don't free entry->id! It is owned by the commit graph. */
1606 free(entry);
1609 static void
1610 free_commits(struct commit_queue *commits)
1612 while (!TAILQ_EMPTY(&commits->head))
1613 pop_commit(commits);
1616 static const struct got_error *
1617 match_commit(int *have_match, struct got_object_id *id,
1618 struct got_commit_object *commit, regex_t *regex)
1620 const struct got_error *err = NULL;
1621 regmatch_t regmatch;
1622 char *id_str = NULL, *logmsg = NULL;
1624 *have_match = 0;
1626 err = got_object_id_str(&id_str, id);
1627 if (err)
1628 return err;
1630 err = got_object_commit_get_logmsg(&logmsg, commit);
1631 if (err)
1632 goto done;
1634 if (regexec(regex, got_object_commit_get_author(commit), 1,
1635 &regmatch, 0) == 0 ||
1636 regexec(regex, got_object_commit_get_committer(commit), 1,
1637 &regmatch, 0) == 0 ||
1638 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1639 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1640 *have_match = 1;
1641 done:
1642 free(id_str);
1643 free(logmsg);
1644 return err;
1647 static const struct got_error *
1648 queue_commits(struct tog_log_thread_args *a)
1650 const struct got_error *err = NULL;
1653 * We keep all commits open throughout the lifetime of the log
1654 * view in order to avoid having to re-fetch commits from disk
1655 * while updating the display.
1657 do {
1658 struct got_object_id *id;
1659 struct got_commit_object *commit;
1660 struct commit_queue_entry *entry;
1661 int errcode;
1663 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1664 NULL, NULL);
1665 if (err || id == NULL)
1666 break;
1668 err = got_object_open_as_commit(&commit, a->repo, id);
1669 if (err)
1670 break;
1671 entry = alloc_commit_queue_entry(commit, id);
1672 if (entry == NULL) {
1673 err = got_error_from_errno("alloc_commit_queue_entry");
1674 break;
1677 errcode = pthread_mutex_lock(&tog_mutex);
1678 if (errcode) {
1679 err = got_error_set_errno(errcode,
1680 "pthread_mutex_lock");
1681 break;
1684 entry->idx = a->commits->ncommits;
1685 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1686 a->commits->ncommits++;
1688 if (*a->searching == TOG_SEARCH_FORWARD &&
1689 !*a->search_next_done) {
1690 int have_match;
1691 err = match_commit(&have_match, id, commit, a->regex);
1692 if (err)
1693 break;
1694 if (have_match)
1695 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1698 errcode = pthread_mutex_unlock(&tog_mutex);
1699 if (errcode && err == NULL)
1700 err = got_error_set_errno(errcode,
1701 "pthread_mutex_unlock");
1702 if (err)
1703 break;
1704 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1706 return err;
1709 static void
1710 select_commit(struct tog_log_view_state *s)
1712 struct commit_queue_entry *entry;
1713 int ncommits = 0;
1715 entry = s->first_displayed_entry;
1716 while (entry) {
1717 if (ncommits == s->selected) {
1718 s->selected_entry = entry;
1719 break;
1721 entry = TAILQ_NEXT(entry, entry);
1722 ncommits++;
1726 static const struct got_error *
1727 draw_commits(struct tog_view *view)
1729 const struct got_error *err = NULL;
1730 struct tog_log_view_state *s = &view->state.log;
1731 struct commit_queue_entry *entry = s->selected_entry;
1732 const int limit = view->nlines;
1733 int width;
1734 int ncommits, author_cols = 4;
1735 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1736 char *refs_str = NULL;
1737 wchar_t *wline;
1738 struct tog_color *tc;
1739 static const size_t date_display_cols = 12;
1741 if (s->selected_entry &&
1742 !(view->searching && view->search_next_done == 0)) {
1743 struct got_reflist_head *refs;
1744 err = got_object_id_str(&id_str, s->selected_entry->id);
1745 if (err)
1746 return err;
1747 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1748 s->selected_entry->id);
1749 if (refs) {
1750 err = build_refs_str(&refs_str, refs,
1751 s->selected_entry->id, s->repo);
1752 if (err)
1753 goto done;
1757 if (s->thread_args.commits_needed == 0)
1758 halfdelay(10); /* disable fast refresh */
1760 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1761 if (asprintf(&ncommits_str, " [%d/%d] %s",
1762 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1763 (view->searching && !view->search_next_done) ?
1764 "searching..." : "loading...") == -1) {
1765 err = got_error_from_errno("asprintf");
1766 goto done;
1768 } else {
1769 const char *search_str = NULL;
1771 if (view->searching) {
1772 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1773 search_str = "no more matches";
1774 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1775 search_str = "no matches found";
1776 else if (!view->search_next_done)
1777 search_str = "searching...";
1780 if (asprintf(&ncommits_str, " [%d/%d] %s",
1781 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1782 search_str ? search_str :
1783 (refs_str ? refs_str : "")) == -1) {
1784 err = got_error_from_errno("asprintf");
1785 goto done;
1789 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1790 if (asprintf(&header, "commit %s %s%s",
1791 id_str ? id_str : "........................................",
1792 s->in_repo_path, ncommits_str) == -1) {
1793 err = got_error_from_errno("asprintf");
1794 header = NULL;
1795 goto done;
1797 } else if (asprintf(&header, "commit %s%s",
1798 id_str ? id_str : "........................................",
1799 ncommits_str) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 header = NULL;
1802 goto done;
1804 err = format_line(&wline, &width, header, view->ncols, 0, 0);
1805 if (err)
1806 goto done;
1808 werase(view->window);
1810 if (view_needs_focus_indication(view))
1811 wstandout(view->window);
1812 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1813 if (tc)
1814 wattr_on(view->window,
1815 COLOR_PAIR(tc->colorpair), NULL);
1816 waddwstr(view->window, wline);
1817 if (tc)
1818 wattr_off(view->window,
1819 COLOR_PAIR(tc->colorpair), NULL);
1820 while (width < view->ncols) {
1821 waddch(view->window, ' ');
1822 width++;
1824 if (view_needs_focus_indication(view))
1825 wstandend(view->window);
1826 free(wline);
1827 if (limit <= 1)
1828 goto done;
1830 /* Grow author column size if necessary, and set view->maxx. */
1831 entry = s->first_displayed_entry;
1832 ncommits = 0;
1833 view->maxx = 0;
1834 while (entry) {
1835 char *author, *eol, *msg, *msg0;
1836 wchar_t *wauthor, *wmsg;
1837 int width;
1838 if (ncommits >= limit - 1)
1839 break;
1840 author = strdup(got_object_commit_get_author(entry->commit));
1841 if (author == NULL) {
1842 err = got_error_from_errno("strdup");
1843 goto done;
1845 err = format_author(&wauthor, &width, author, COLS,
1846 date_display_cols);
1847 if (author_cols < width)
1848 author_cols = width;
1849 free(wauthor);
1850 free(author);
1851 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1852 if (err)
1853 goto done;
1854 msg = msg0;
1855 while (*msg == '\n')
1856 ++msg;
1857 if ((eol = strchr(msg, '\n')))
1858 *eol = '\0';
1859 err = format_line(&wmsg, &width, msg, INT_MAX,
1860 date_display_cols + author_cols, 0);
1861 if (err)
1862 goto done;
1863 view->maxx = MAX(view->maxx, width);
1864 free(msg0);
1865 free(wmsg);
1866 ncommits++;
1867 entry = TAILQ_NEXT(entry, entry);
1870 entry = s->first_displayed_entry;
1871 s->last_displayed_entry = s->first_displayed_entry;
1872 ncommits = 0;
1873 while (entry) {
1874 if (ncommits >= limit - 1)
1875 break;
1876 if (ncommits == s->selected)
1877 wstandout(view->window);
1878 err = draw_commit(view, entry->commit, entry->id,
1879 date_display_cols, author_cols);
1880 if (ncommits == s->selected)
1881 wstandend(view->window);
1882 if (err)
1883 goto done;
1884 ncommits++;
1885 s->last_displayed_entry = entry;
1886 entry = TAILQ_NEXT(entry, entry);
1889 view_vborder(view);
1890 done:
1891 free(id_str);
1892 free(refs_str);
1893 free(ncommits_str);
1894 free(header);
1895 return err;
1898 static void
1899 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1901 struct commit_queue_entry *entry;
1902 int nscrolled = 0;
1904 entry = TAILQ_FIRST(&s->commits.head);
1905 if (s->first_displayed_entry == entry)
1906 return;
1908 entry = s->first_displayed_entry;
1909 while (entry && nscrolled < maxscroll) {
1910 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1911 if (entry) {
1912 s->first_displayed_entry = entry;
1913 nscrolled++;
1918 static const struct got_error *
1919 trigger_log_thread(struct tog_view *view, int wait)
1921 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1922 int errcode;
1924 halfdelay(1); /* fast refresh while loading commits */
1926 while (ta->commits_needed > 0 || ta->load_all) {
1927 if (ta->log_complete)
1928 break;
1930 /* Wake the log thread. */
1931 errcode = pthread_cond_signal(&ta->need_commits);
1932 if (errcode)
1933 return got_error_set_errno(errcode,
1934 "pthread_cond_signal");
1937 * The mutex will be released while the view loop waits
1938 * in wgetch(), at which time the log thread will run.
1940 if (!wait)
1941 break;
1943 /* Display progress update in log view. */
1944 show_log_view(view);
1945 update_panels();
1946 doupdate();
1948 /* Wait right here while next commit is being loaded. */
1949 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1950 if (errcode)
1951 return got_error_set_errno(errcode,
1952 "pthread_cond_wait");
1954 /* Display progress update in log view. */
1955 show_log_view(view);
1956 update_panels();
1957 doupdate();
1960 return NULL;
1963 static const struct got_error *
1964 log_scroll_down(struct tog_view *view, int maxscroll)
1966 struct tog_log_view_state *s = &view->state.log;
1967 const struct got_error *err = NULL;
1968 struct commit_queue_entry *pentry;
1969 int nscrolled = 0, ncommits_needed;
1971 if (s->last_displayed_entry == NULL)
1972 return NULL;
1974 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1975 if (s->commits.ncommits < ncommits_needed &&
1976 !s->thread_args.log_complete) {
1978 * Ask the log thread for required amount of commits.
1980 s->thread_args.commits_needed += maxscroll;
1981 err = trigger_log_thread(view, 1);
1982 if (err)
1983 return err;
1986 do {
1987 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1988 if (pentry == NULL)
1989 break;
1991 s->last_displayed_entry = pentry;
1993 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1994 if (pentry == NULL)
1995 break;
1996 s->first_displayed_entry = pentry;
1997 } while (++nscrolled < maxscroll);
1999 return err;
2002 static const struct got_error *
2003 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2004 struct got_commit_object *commit, struct got_object_id *commit_id,
2005 struct tog_view *log_view, struct got_repository *repo)
2007 const struct got_error *err;
2008 struct got_object_qid *parent_id;
2009 struct tog_view *diff_view;
2011 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2012 if (diff_view == NULL)
2013 return got_error_from_errno("view_open");
2015 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2016 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2017 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2018 if (err == NULL)
2019 *new_view = diff_view;
2020 return err;
2023 static const struct got_error *
2024 tree_view_visit_subtree(struct tog_tree_view_state *s,
2025 struct got_tree_object *subtree)
2027 struct tog_parent_tree *parent;
2029 parent = calloc(1, sizeof(*parent));
2030 if (parent == NULL)
2031 return got_error_from_errno("calloc");
2033 parent->tree = s->tree;
2034 parent->first_displayed_entry = s->first_displayed_entry;
2035 parent->selected_entry = s->selected_entry;
2036 parent->selected = s->selected;
2037 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2038 s->tree = subtree;
2039 s->selected = 0;
2040 s->first_displayed_entry = NULL;
2041 return NULL;
2044 static const struct got_error *
2045 tree_view_walk_path(struct tog_tree_view_state *s,
2046 struct got_commit_object *commit, const char *path)
2048 const struct got_error *err = NULL;
2049 struct got_tree_object *tree = NULL;
2050 const char *p;
2051 char *slash, *subpath = NULL;
2053 /* Walk the path and open corresponding tree objects. */
2054 p = path;
2055 while (*p) {
2056 struct got_tree_entry *te;
2057 struct got_object_id *tree_id;
2058 char *te_name;
2060 while (p[0] == '/')
2061 p++;
2063 /* Ensure the correct subtree entry is selected. */
2064 slash = strchr(p, '/');
2065 if (slash == NULL)
2066 te_name = strdup(p);
2067 else
2068 te_name = strndup(p, slash - p);
2069 if (te_name == NULL) {
2070 err = got_error_from_errno("strndup");
2071 break;
2073 te = got_object_tree_find_entry(s->tree, te_name);
2074 if (te == NULL) {
2075 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2076 free(te_name);
2077 break;
2079 free(te_name);
2080 s->first_displayed_entry = s->selected_entry = te;
2082 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2083 break; /* jump to this file's entry */
2085 slash = strchr(p, '/');
2086 if (slash)
2087 subpath = strndup(path, slash - path);
2088 else
2089 subpath = strdup(path);
2090 if (subpath == NULL) {
2091 err = got_error_from_errno("strdup");
2092 break;
2095 err = got_object_id_by_path(&tree_id, s->repo, commit,
2096 subpath);
2097 if (err)
2098 break;
2100 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2101 free(tree_id);
2102 if (err)
2103 break;
2105 err = tree_view_visit_subtree(s, tree);
2106 if (err) {
2107 got_object_tree_close(tree);
2108 break;
2110 if (slash == NULL)
2111 break;
2112 free(subpath);
2113 subpath = NULL;
2114 p = slash;
2117 free(subpath);
2118 return err;
2121 static const struct got_error *
2122 browse_commit_tree(struct tog_view **new_view, int begin_x,
2123 struct commit_queue_entry *entry, const char *path,
2124 const char *head_ref_name, struct got_repository *repo)
2126 const struct got_error *err = NULL;
2127 struct tog_tree_view_state *s;
2128 struct tog_view *tree_view;
2130 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2131 if (tree_view == NULL)
2132 return got_error_from_errno("view_open");
2134 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2135 if (err)
2136 return err;
2137 s = &tree_view->state.tree;
2139 *new_view = tree_view;
2141 if (got_path_is_root_dir(path))
2142 return NULL;
2144 return tree_view_walk_path(s, entry->commit, path);
2147 static const struct got_error *
2148 block_signals_used_by_main_thread(void)
2150 sigset_t sigset;
2151 int errcode;
2153 if (sigemptyset(&sigset) == -1)
2154 return got_error_from_errno("sigemptyset");
2156 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2157 if (sigaddset(&sigset, SIGWINCH) == -1)
2158 return got_error_from_errno("sigaddset");
2159 if (sigaddset(&sigset, SIGCONT) == -1)
2160 return got_error_from_errno("sigaddset");
2161 if (sigaddset(&sigset, SIGINT) == -1)
2162 return got_error_from_errno("sigaddset");
2163 if (sigaddset(&sigset, SIGTERM) == -1)
2164 return got_error_from_errno("sigaddset");
2166 /* ncurses handles SIGTSTP */
2167 if (sigaddset(&sigset, SIGTSTP) == -1)
2168 return got_error_from_errno("sigaddset");
2170 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2171 if (errcode)
2172 return got_error_set_errno(errcode, "pthread_sigmask");
2174 return NULL;
2177 static void *
2178 log_thread(void *arg)
2180 const struct got_error *err = NULL;
2181 int errcode = 0;
2182 struct tog_log_thread_args *a = arg;
2183 int done = 0;
2185 err = block_signals_used_by_main_thread();
2186 if (err)
2187 return (void *)err;
2189 while (!done && !err && !tog_fatal_signal_received()) {
2190 err = queue_commits(a);
2191 if (err) {
2192 if (err->code != GOT_ERR_ITER_COMPLETED)
2193 return (void *)err;
2194 err = NULL;
2195 done = 1;
2196 } else if (a->commits_needed > 0 && !a->load_all)
2197 a->commits_needed--;
2199 errcode = pthread_mutex_lock(&tog_mutex);
2200 if (errcode) {
2201 err = got_error_set_errno(errcode,
2202 "pthread_mutex_lock");
2203 break;
2204 } else if (*a->quit)
2205 done = 1;
2206 else if (*a->first_displayed_entry == NULL) {
2207 *a->first_displayed_entry =
2208 TAILQ_FIRST(&a->commits->head);
2209 *a->selected_entry = *a->first_displayed_entry;
2212 errcode = pthread_cond_signal(&a->commit_loaded);
2213 if (errcode) {
2214 err = got_error_set_errno(errcode,
2215 "pthread_cond_signal");
2216 pthread_mutex_unlock(&tog_mutex);
2217 break;
2220 if (done)
2221 a->commits_needed = 0;
2222 else {
2223 if (a->commits_needed == 0 && !a->load_all) {
2224 errcode = pthread_cond_wait(&a->need_commits,
2225 &tog_mutex);
2226 if (errcode)
2227 err = got_error_set_errno(errcode,
2228 "pthread_cond_wait");
2229 if (*a->quit)
2230 done = 1;
2234 errcode = pthread_mutex_unlock(&tog_mutex);
2235 if (errcode && err == NULL)
2236 err = got_error_set_errno(errcode,
2237 "pthread_mutex_unlock");
2239 a->log_complete = 1;
2240 return (void *)err;
2243 static const struct got_error *
2244 stop_log_thread(struct tog_log_view_state *s)
2246 const struct got_error *err = NULL;
2247 int errcode;
2249 if (s->thread) {
2250 s->quit = 1;
2251 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2252 if (errcode)
2253 return got_error_set_errno(errcode,
2254 "pthread_cond_signal");
2255 errcode = pthread_mutex_unlock(&tog_mutex);
2256 if (errcode)
2257 return got_error_set_errno(errcode,
2258 "pthread_mutex_unlock");
2259 errcode = pthread_join(s->thread, (void **)&err);
2260 if (errcode)
2261 return got_error_set_errno(errcode, "pthread_join");
2262 errcode = pthread_mutex_lock(&tog_mutex);
2263 if (errcode)
2264 return got_error_set_errno(errcode,
2265 "pthread_mutex_lock");
2266 s->thread = NULL;
2269 if (s->thread_args.repo) {
2270 err = got_repo_close(s->thread_args.repo);
2271 s->thread_args.repo = NULL;
2274 if (s->thread_args.pack_fds) {
2275 const struct got_error *pack_err =
2276 got_repo_pack_fds_close(s->thread_args.pack_fds);
2277 if (err == NULL)
2278 err = pack_err;
2279 s->thread_args.pack_fds = NULL;
2282 if (s->thread_args.graph) {
2283 got_commit_graph_close(s->thread_args.graph);
2284 s->thread_args.graph = NULL;
2287 return err;
2290 static const struct got_error *
2291 close_log_view(struct tog_view *view)
2293 const struct got_error *err = NULL;
2294 struct tog_log_view_state *s = &view->state.log;
2295 int errcode;
2297 err = stop_log_thread(s);
2299 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2300 if (errcode && err == NULL)
2301 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2303 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2304 if (errcode && err == NULL)
2305 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2307 free_commits(&s->commits);
2308 free(s->in_repo_path);
2309 s->in_repo_path = NULL;
2310 free(s->start_id);
2311 s->start_id = NULL;
2312 free(s->head_ref_name);
2313 s->head_ref_name = NULL;
2314 return err;
2317 static const struct got_error *
2318 search_start_log_view(struct tog_view *view)
2320 struct tog_log_view_state *s = &view->state.log;
2322 s->matched_entry = NULL;
2323 s->search_entry = NULL;
2324 return NULL;
2327 static const struct got_error *
2328 search_next_log_view(struct tog_view *view)
2330 const struct got_error *err = NULL;
2331 struct tog_log_view_state *s = &view->state.log;
2332 struct commit_queue_entry *entry;
2334 /* Display progress update in log view. */
2335 show_log_view(view);
2336 update_panels();
2337 doupdate();
2339 if (s->search_entry) {
2340 int errcode, ch;
2341 errcode = pthread_mutex_unlock(&tog_mutex);
2342 if (errcode)
2343 return got_error_set_errno(errcode,
2344 "pthread_mutex_unlock");
2345 ch = wgetch(view->window);
2346 errcode = pthread_mutex_lock(&tog_mutex);
2347 if (errcode)
2348 return got_error_set_errno(errcode,
2349 "pthread_mutex_lock");
2350 if (ch == KEY_BACKSPACE) {
2351 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2352 return NULL;
2354 if (view->searching == TOG_SEARCH_FORWARD)
2355 entry = TAILQ_NEXT(s->search_entry, entry);
2356 else
2357 entry = TAILQ_PREV(s->search_entry,
2358 commit_queue_head, entry);
2359 } else if (s->matched_entry) {
2360 if (view->searching == TOG_SEARCH_FORWARD)
2361 entry = TAILQ_NEXT(s->matched_entry, entry);
2362 else
2363 entry = TAILQ_PREV(s->matched_entry,
2364 commit_queue_head, entry);
2365 } else {
2366 entry = s->selected_entry;
2369 while (1) {
2370 int have_match = 0;
2372 if (entry == NULL) {
2373 if (s->thread_args.log_complete ||
2374 view->searching == TOG_SEARCH_BACKWARD) {
2375 view->search_next_done =
2376 (s->matched_entry == NULL ?
2377 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2378 s->search_entry = NULL;
2379 return NULL;
2382 * Poke the log thread for more commits and return,
2383 * allowing the main loop to make progress. Search
2384 * will resume at s->search_entry once we come back.
2386 s->thread_args.commits_needed++;
2387 return trigger_log_thread(view, 0);
2390 err = match_commit(&have_match, entry->id, entry->commit,
2391 &view->regex);
2392 if (err)
2393 break;
2394 if (have_match) {
2395 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2396 s->matched_entry = entry;
2397 break;
2400 s->search_entry = entry;
2401 if (view->searching == TOG_SEARCH_FORWARD)
2402 entry = TAILQ_NEXT(entry, entry);
2403 else
2404 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2407 if (s->matched_entry) {
2408 int cur = s->selected_entry->idx;
2409 while (cur < s->matched_entry->idx) {
2410 err = input_log_view(NULL, view, KEY_DOWN);
2411 if (err)
2412 return err;
2413 cur++;
2415 while (cur > s->matched_entry->idx) {
2416 err = input_log_view(NULL, view, KEY_UP);
2417 if (err)
2418 return err;
2419 cur--;
2423 s->search_entry = NULL;
2425 return NULL;
2428 static const struct got_error *
2429 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2430 struct got_repository *repo, const char *head_ref_name,
2431 const char *in_repo_path, int log_branches)
2433 const struct got_error *err = NULL;
2434 struct tog_log_view_state *s = &view->state.log;
2435 struct got_repository *thread_repo = NULL;
2436 struct got_commit_graph *thread_graph = NULL;
2437 int errcode;
2439 if (in_repo_path != s->in_repo_path) {
2440 free(s->in_repo_path);
2441 s->in_repo_path = strdup(in_repo_path);
2442 if (s->in_repo_path == NULL)
2443 return got_error_from_errno("strdup");
2446 /* The commit queue only contains commits being displayed. */
2447 TAILQ_INIT(&s->commits.head);
2448 s->commits.ncommits = 0;
2450 s->repo = repo;
2451 if (head_ref_name) {
2452 s->head_ref_name = strdup(head_ref_name);
2453 if (s->head_ref_name == NULL) {
2454 err = got_error_from_errno("strdup");
2455 goto done;
2458 s->start_id = got_object_id_dup(start_id);
2459 if (s->start_id == NULL) {
2460 err = got_error_from_errno("got_object_id_dup");
2461 goto done;
2463 s->log_branches = log_branches;
2465 STAILQ_INIT(&s->colors);
2466 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2467 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2468 get_color_value("TOG_COLOR_COMMIT"));
2469 if (err)
2470 goto done;
2471 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2472 get_color_value("TOG_COLOR_AUTHOR"));
2473 if (err) {
2474 free_colors(&s->colors);
2475 goto done;
2477 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2478 get_color_value("TOG_COLOR_DATE"));
2479 if (err) {
2480 free_colors(&s->colors);
2481 goto done;
2485 view->show = show_log_view;
2486 view->input = input_log_view;
2487 view->close = close_log_view;
2488 view->search_start = search_start_log_view;
2489 view->search_next = search_next_log_view;
2491 if (s->thread_args.pack_fds == NULL) {
2492 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2493 if (err)
2494 goto done;
2496 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2497 s->thread_args.pack_fds);
2498 if (err)
2499 goto done;
2500 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2501 !s->log_branches);
2502 if (err)
2503 goto done;
2504 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2505 s->repo, NULL, NULL);
2506 if (err)
2507 goto done;
2509 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2510 if (errcode) {
2511 err = got_error_set_errno(errcode, "pthread_cond_init");
2512 goto done;
2514 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2515 if (errcode) {
2516 err = got_error_set_errno(errcode, "pthread_cond_init");
2517 goto done;
2520 s->thread_args.commits_needed = view->nlines;
2521 s->thread_args.graph = thread_graph;
2522 s->thread_args.commits = &s->commits;
2523 s->thread_args.in_repo_path = s->in_repo_path;
2524 s->thread_args.start_id = s->start_id;
2525 s->thread_args.repo = thread_repo;
2526 s->thread_args.log_complete = 0;
2527 s->thread_args.quit = &s->quit;
2528 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2529 s->thread_args.selected_entry = &s->selected_entry;
2530 s->thread_args.searching = &view->searching;
2531 s->thread_args.search_next_done = &view->search_next_done;
2532 s->thread_args.regex = &view->regex;
2533 done:
2534 if (err)
2535 close_log_view(view);
2536 return err;
2539 static const struct got_error *
2540 show_log_view(struct tog_view *view)
2542 const struct got_error *err;
2543 struct tog_log_view_state *s = &view->state.log;
2545 if (s->thread == NULL) {
2546 int errcode = pthread_create(&s->thread, NULL, log_thread,
2547 &s->thread_args);
2548 if (errcode)
2549 return got_error_set_errno(errcode, "pthread_create");
2550 if (s->thread_args.commits_needed > 0) {
2551 err = trigger_log_thread(view, 1);
2552 if (err)
2553 return err;
2557 return draw_commits(view);
2560 static const struct got_error *
2561 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2563 const struct got_error *err = NULL;
2564 struct tog_log_view_state *s = &view->state.log;
2565 struct tog_view *diff_view = NULL, *tree_view = NULL;
2566 struct tog_view *ref_view = NULL;
2567 struct commit_queue_entry *entry;
2568 int begin_x = 0, n, nscroll = view->nlines - 1;
2570 if (s->thread_args.load_all) {
2571 if (ch == KEY_BACKSPACE)
2572 s->thread_args.load_all = 0;
2573 else if (s->thread_args.log_complete) {
2574 s->thread_args.load_all = 0;
2575 log_scroll_down(view, s->commits.ncommits);
2576 s->selected = MIN(view->nlines - 2,
2577 s->commits.ncommits - 1);
2578 select_commit(s);
2580 return NULL;
2583 switch (ch) {
2584 case 'q':
2585 s->quit = 1;
2586 break;
2587 case '0':
2588 view->x = 0;
2589 break;
2590 case '$':
2591 view->x = MAX(view->maxx - view->ncols / 2, 0);
2592 break;
2593 case KEY_RIGHT:
2594 case 'l':
2595 if (view->x + view->ncols / 2 < view->maxx)
2596 view->x += 2; /* move two columns right */
2597 break;
2598 case KEY_LEFT:
2599 case 'h':
2600 view->x -= MIN(view->x, 2); /* move two columns back */
2601 break;
2602 case 'k':
2603 case KEY_UP:
2604 case '<':
2605 case ',':
2606 case CTRL('p'):
2607 if (s->first_displayed_entry == NULL)
2608 break;
2609 if (s->selected > 0)
2610 s->selected--;
2611 else
2612 log_scroll_up(s, 1);
2613 select_commit(s);
2614 break;
2615 case 'g':
2616 case KEY_HOME:
2617 s->selected = 0;
2618 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2619 select_commit(s);
2620 break;
2621 case CTRL('u'):
2622 case 'u':
2623 nscroll /= 2;
2624 /* FALL THROUGH */
2625 case KEY_PPAGE:
2626 case CTRL('b'):
2627 if (s->first_displayed_entry == NULL)
2628 break;
2629 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2630 s->selected = MAX(0, s->selected - nscroll - 1);
2631 else
2632 log_scroll_up(s, nscroll);
2633 select_commit(s);
2634 break;
2635 case 'j':
2636 case KEY_DOWN:
2637 case '>':
2638 case '.':
2639 case CTRL('n'):
2640 if (s->first_displayed_entry == NULL)
2641 break;
2642 if (s->selected < MIN(view->nlines - 2,
2643 s->commits.ncommits - 1))
2644 s->selected++;
2645 else {
2646 err = log_scroll_down(view, 1);
2647 if (err)
2648 break;
2650 select_commit(s);
2651 break;
2652 case 'G':
2653 case KEY_END: {
2654 /* We don't know yet how many commits, so we're forced to
2655 * traverse them all. */
2656 if (!s->thread_args.log_complete) {
2657 s->thread_args.load_all = 1;
2658 return trigger_log_thread(view, 0);
2661 s->selected = 0;
2662 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2663 for (n = 0; n < view->nlines - 1; n++) {
2664 if (entry == NULL)
2665 break;
2666 s->first_displayed_entry = entry;
2667 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2669 if (n > 0)
2670 s->selected = n - 1;
2671 select_commit(s);
2672 break;
2674 case CTRL('d'):
2675 case 'd':
2676 nscroll /= 2;
2677 /* FALL THROUGH */
2678 case KEY_NPAGE:
2679 case CTRL('f'): {
2680 struct commit_queue_entry *first;
2681 first = s->first_displayed_entry;
2682 if (first == NULL)
2683 break;
2684 err = log_scroll_down(view, nscroll);
2685 if (err)
2686 break;
2687 if (first == s->first_displayed_entry &&
2688 s->selected < MIN(view->nlines - 2,
2689 s->commits.ncommits - 1)) {
2690 /* can't scroll further down */
2691 s->selected += MIN(s->last_displayed_entry->idx -
2692 s->selected_entry->idx, nscroll + 1);
2694 select_commit(s);
2695 break;
2697 case KEY_RESIZE:
2698 if (s->selected > view->nlines - 2)
2699 s->selected = view->nlines - 2;
2700 if (s->selected > s->commits.ncommits - 1)
2701 s->selected = s->commits.ncommits - 1;
2702 select_commit(s);
2703 if (s->commits.ncommits < view->nlines - 1 &&
2704 !s->thread_args.log_complete) {
2705 s->thread_args.commits_needed += (view->nlines - 1) -
2706 s->commits.ncommits;
2707 err = trigger_log_thread(view, 1);
2709 break;
2710 case KEY_ENTER:
2711 case ' ':
2712 case '\r':
2713 if (s->selected_entry == NULL)
2714 break;
2715 if (view_is_parent_view(view))
2716 begin_x = view_split_begin_x(view->begin_x);
2717 err = open_diff_view_for_commit(&diff_view, begin_x,
2718 s->selected_entry->commit, s->selected_entry->id,
2719 view, s->repo);
2720 if (err)
2721 break;
2722 view->focussed = 0;
2723 diff_view->focussed = 1;
2724 if (view_is_parent_view(view)) {
2725 err = view_close_child(view);
2726 if (err)
2727 return err;
2728 view_set_child(view, diff_view);
2729 view->focus_child = 1;
2730 } else
2731 *new_view = diff_view;
2732 break;
2733 case 't':
2734 if (s->selected_entry == NULL)
2735 break;
2736 if (view_is_parent_view(view))
2737 begin_x = view_split_begin_x(view->begin_x);
2738 err = browse_commit_tree(&tree_view, begin_x,
2739 s->selected_entry, s->in_repo_path, s->head_ref_name,
2740 s->repo);
2741 if (err)
2742 break;
2743 view->focussed = 0;
2744 tree_view->focussed = 1;
2745 if (view_is_parent_view(view)) {
2746 err = view_close_child(view);
2747 if (err)
2748 return err;
2749 view_set_child(view, tree_view);
2750 view->focus_child = 1;
2751 } else
2752 *new_view = tree_view;
2753 break;
2754 case KEY_BACKSPACE:
2755 case CTRL('l'):
2756 case 'B':
2757 if (ch == KEY_BACKSPACE &&
2758 got_path_is_root_dir(s->in_repo_path))
2759 break;
2760 err = stop_log_thread(s);
2761 if (err)
2762 return err;
2763 if (ch == KEY_BACKSPACE) {
2764 char *parent_path;
2765 err = got_path_dirname(&parent_path, s->in_repo_path);
2766 if (err)
2767 return err;
2768 free(s->in_repo_path);
2769 s->in_repo_path = parent_path;
2770 s->thread_args.in_repo_path = s->in_repo_path;
2771 } else if (ch == CTRL('l')) {
2772 struct got_object_id *start_id;
2773 err = got_repo_match_object_id(&start_id, NULL,
2774 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2775 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2776 if (err)
2777 return err;
2778 free(s->start_id);
2779 s->start_id = start_id;
2780 s->thread_args.start_id = s->start_id;
2781 } else /* 'B' */
2782 s->log_branches = !s->log_branches;
2784 err = got_repo_open(&s->thread_args.repo,
2785 got_repo_get_path(s->repo), NULL,
2786 s->thread_args.pack_fds);
2787 if (err)
2788 return err;
2789 tog_free_refs();
2790 err = tog_load_refs(s->repo, 0);
2791 if (err)
2792 return err;
2793 err = got_commit_graph_open(&s->thread_args.graph,
2794 s->in_repo_path, !s->log_branches);
2795 if (err)
2796 return err;
2797 err = got_commit_graph_iter_start(s->thread_args.graph,
2798 s->start_id, s->repo, NULL, NULL);
2799 if (err)
2800 return err;
2801 free_commits(&s->commits);
2802 s->first_displayed_entry = NULL;
2803 s->last_displayed_entry = NULL;
2804 s->selected_entry = NULL;
2805 s->selected = 0;
2806 s->thread_args.log_complete = 0;
2807 s->quit = 0;
2808 s->thread_args.commits_needed = view->nlines;
2809 break;
2810 case 'r':
2811 if (view_is_parent_view(view))
2812 begin_x = view_split_begin_x(view->begin_x);
2813 ref_view = view_open(view->nlines, view->ncols,
2814 view->begin_y, begin_x, TOG_VIEW_REF);
2815 if (ref_view == NULL)
2816 return got_error_from_errno("view_open");
2817 err = open_ref_view(ref_view, s->repo);
2818 if (err) {
2819 view_close(ref_view);
2820 return err;
2822 view->focussed = 0;
2823 ref_view->focussed = 1;
2824 if (view_is_parent_view(view)) {
2825 err = view_close_child(view);
2826 if (err)
2827 return err;
2828 view_set_child(view, ref_view);
2829 view->focus_child = 1;
2830 } else
2831 *new_view = ref_view;
2832 break;
2833 default:
2834 break;
2837 return err;
2840 static const struct got_error *
2841 apply_unveil(const char *repo_path, const char *worktree_path)
2843 const struct got_error *error;
2845 #ifdef PROFILE
2846 if (unveil("gmon.out", "rwc") != 0)
2847 return got_error_from_errno2("unveil", "gmon.out");
2848 #endif
2849 if (repo_path && unveil(repo_path, "r") != 0)
2850 return got_error_from_errno2("unveil", repo_path);
2852 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2853 return got_error_from_errno2("unveil", worktree_path);
2855 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2856 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2858 error = got_privsep_unveil_exec_helpers();
2859 if (error != NULL)
2860 return error;
2862 if (unveil(NULL, NULL) != 0)
2863 return got_error_from_errno("unveil");
2865 return NULL;
2868 static void
2869 init_curses(void)
2872 * Override default signal handlers before starting ncurses.
2873 * This should prevent ncurses from installing its own
2874 * broken cleanup() signal handler.
2876 signal(SIGWINCH, tog_sigwinch);
2877 signal(SIGPIPE, tog_sigpipe);
2878 signal(SIGCONT, tog_sigcont);
2879 signal(SIGINT, tog_sigint);
2880 signal(SIGTERM, tog_sigterm);
2882 initscr();
2883 cbreak();
2884 halfdelay(1); /* Do fast refresh while initial view is loading. */
2885 noecho();
2886 nonl();
2887 intrflush(stdscr, FALSE);
2888 keypad(stdscr, TRUE);
2889 curs_set(0);
2890 if (getenv("TOG_COLORS") != NULL) {
2891 start_color();
2892 use_default_colors();
2896 static const struct got_error *
2897 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2898 struct got_repository *repo, struct got_worktree *worktree)
2900 const struct got_error *err = NULL;
2902 if (argc == 0) {
2903 *in_repo_path = strdup("/");
2904 if (*in_repo_path == NULL)
2905 return got_error_from_errno("strdup");
2906 return NULL;
2909 if (worktree) {
2910 const char *prefix = got_worktree_get_path_prefix(worktree);
2911 char *p;
2913 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2914 if (err)
2915 return err;
2916 if (asprintf(in_repo_path, "%s%s%s", prefix,
2917 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2918 p) == -1) {
2919 err = got_error_from_errno("asprintf");
2920 *in_repo_path = NULL;
2922 free(p);
2923 } else
2924 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2926 return err;
2929 static const struct got_error *
2930 cmd_log(int argc, char *argv[])
2932 const struct got_error *error;
2933 struct got_repository *repo = NULL;
2934 struct got_worktree *worktree = NULL;
2935 struct got_object_id *start_id = NULL;
2936 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2937 char *start_commit = NULL, *label = NULL;
2938 struct got_reference *ref = NULL;
2939 const char *head_ref_name = NULL;
2940 int ch, log_branches = 0;
2941 struct tog_view *view;
2942 int *pack_fds = NULL;
2944 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2945 switch (ch) {
2946 case 'b':
2947 log_branches = 1;
2948 break;
2949 case 'c':
2950 start_commit = optarg;
2951 break;
2952 case 'r':
2953 repo_path = realpath(optarg, NULL);
2954 if (repo_path == NULL)
2955 return got_error_from_errno2("realpath",
2956 optarg);
2957 break;
2958 default:
2959 usage_log();
2960 /* NOTREACHED */
2964 argc -= optind;
2965 argv += optind;
2967 if (argc > 1)
2968 usage_log();
2970 error = got_repo_pack_fds_open(&pack_fds);
2971 if (error != NULL)
2972 goto done;
2974 if (repo_path == NULL) {
2975 cwd = getcwd(NULL, 0);
2976 if (cwd == NULL)
2977 return got_error_from_errno("getcwd");
2978 error = got_worktree_open(&worktree, cwd);
2979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2980 goto done;
2981 if (worktree)
2982 repo_path =
2983 strdup(got_worktree_get_repo_path(worktree));
2984 else
2985 repo_path = strdup(cwd);
2986 if (repo_path == NULL) {
2987 error = got_error_from_errno("strdup");
2988 goto done;
2992 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2993 if (error != NULL)
2994 goto done;
2996 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2997 repo, worktree);
2998 if (error)
2999 goto done;
3001 init_curses();
3003 error = apply_unveil(got_repo_get_path(repo),
3004 worktree ? got_worktree_get_root_path(worktree) : NULL);
3005 if (error)
3006 goto done;
3008 /* already loaded by tog_log_with_path()? */
3009 if (TAILQ_EMPTY(&tog_refs)) {
3010 error = tog_load_refs(repo, 0);
3011 if (error)
3012 goto done;
3015 if (start_commit == NULL) {
3016 error = got_repo_match_object_id(&start_id, &label,
3017 worktree ? got_worktree_get_head_ref_name(worktree) :
3018 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3019 if (error)
3020 goto done;
3021 head_ref_name = label;
3022 } else {
3023 error = got_ref_open(&ref, repo, start_commit, 0);
3024 if (error == NULL)
3025 head_ref_name = got_ref_get_name(ref);
3026 else if (error->code != GOT_ERR_NOT_REF)
3027 goto done;
3028 error = got_repo_match_object_id(&start_id, NULL,
3029 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3030 if (error)
3031 goto done;
3034 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3035 if (view == NULL) {
3036 error = got_error_from_errno("view_open");
3037 goto done;
3039 error = open_log_view(view, start_id, repo, head_ref_name,
3040 in_repo_path, log_branches);
3041 if (error)
3042 goto done;
3043 if (worktree) {
3044 /* Release work tree lock. */
3045 got_worktree_close(worktree);
3046 worktree = NULL;
3048 error = view_loop(view);
3049 done:
3050 free(in_repo_path);
3051 free(repo_path);
3052 free(cwd);
3053 free(start_id);
3054 free(label);
3055 if (ref)
3056 got_ref_close(ref);
3057 if (repo) {
3058 const struct got_error *close_err = got_repo_close(repo);
3059 if (error == NULL)
3060 error = close_err;
3062 if (worktree)
3063 got_worktree_close(worktree);
3064 if (pack_fds) {
3065 const struct got_error *pack_err =
3066 got_repo_pack_fds_close(pack_fds);
3067 if (error == NULL)
3068 error = pack_err;
3070 tog_free_refs();
3071 return error;
3074 __dead static void
3075 usage_diff(void)
3077 endwin();
3078 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3079 "[-w] object1 object2\n", getprogname());
3080 exit(1);
3083 static int
3084 match_line(const char *line, regex_t *regex, size_t nmatch,
3085 regmatch_t *regmatch)
3087 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3090 struct tog_color *
3091 match_color(struct tog_colors *colors, const char *line)
3093 struct tog_color *tc = NULL;
3095 STAILQ_FOREACH(tc, colors, entry) {
3096 if (match_line(line, &tc->regex, 0, NULL))
3097 return tc;
3100 return NULL;
3103 static const struct got_error *
3104 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3105 WINDOW *window, int skip, regmatch_t *regmatch)
3107 const struct got_error *err = NULL;
3108 wchar_t *wline;
3109 int rme, rms, n, width;
3111 *wtotal = 0;
3112 rms = regmatch->rm_so;
3113 rme = regmatch->rm_eo;
3115 err = format_line(&wline, &width, line, wlimit + skip,
3116 col_tab_align, 1);
3117 if (err)
3118 return err;
3120 /* draw up to matched token if we haven't scrolled past it */
3121 n = MAX(rms - skip, 0);
3122 if (n) {
3123 waddnwstr(window, wline + skip, n);
3124 wlimit -= n;
3125 *wtotal += n;
3128 if (wlimit > 0) {
3129 int len = rme - rms;
3130 n = 0;
3131 if (skip > rms) {
3132 n = skip - rms;
3133 len = MAX(len - n, 0);
3135 /* draw (visible part of) matched token (if scrolled into it) */
3136 if (len) {
3137 wattron(window, A_STANDOUT);
3138 waddnwstr(window, wline + rms + n, len);
3139 wattroff(window, A_STANDOUT);
3140 wlimit -= len;
3141 *wtotal += len;
3145 if (wlimit > 0 && skip < width) { /* draw rest of line */
3146 n = 0;
3147 if (skip > rme)
3148 n = MIN(skip - rme, width - rme);
3149 waddnwstr(window, wline + rme + n, wlimit);
3152 *wtotal = width;
3153 free(wline);
3154 return NULL;
3157 static const struct got_error *
3158 draw_file(struct tog_view *view, const char *header)
3160 struct tog_diff_view_state *s = &view->state.diff;
3161 regmatch_t *regmatch = &view->regmatch;
3162 const struct got_error *err;
3163 int nprinted = 0;
3164 char *line;
3165 size_t linesize = 0;
3166 ssize_t linelen;
3167 struct tog_color *tc;
3168 wchar_t *wline;
3169 int width;
3170 int max_lines = view->nlines;
3171 int nlines = s->nlines;
3172 off_t line_offset;
3174 line_offset = s->line_offsets[s->first_displayed_line - 1];
3175 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3176 return got_error_from_errno("fseek");
3178 werase(view->window);
3180 if (header) {
3181 if (asprintf(&line, "[%d/%d] %s",
3182 s->first_displayed_line - 1 + s->selected_line, nlines,
3183 header) == -1)
3184 return got_error_from_errno("asprintf");
3185 err = format_line(&wline, &width, line, view->ncols, 0, 0);
3186 free(line);
3187 if (err)
3188 return err;
3190 if (view_needs_focus_indication(view))
3191 wstandout(view->window);
3192 waddwstr(view->window, wline);
3193 free(wline);
3194 wline = NULL;
3195 if (view_needs_focus_indication(view))
3196 wstandend(view->window);
3197 if (width <= view->ncols - 1)
3198 waddch(view->window, '\n');
3200 if (max_lines <= 1)
3201 return NULL;
3202 max_lines--;
3205 s->eof = 0;
3206 view->maxx = 0;
3207 line = NULL;
3208 while (max_lines > 0 && nprinted < max_lines) {
3209 linelen = getline(&line, &linesize, s->f);
3210 if (linelen == -1) {
3211 if (feof(s->f)) {
3212 s->eof = 1;
3213 break;
3215 free(line);
3216 return got_ferror(s->f, GOT_ERR_IO);
3219 view->maxx = MAX(view->maxx, linelen);
3221 tc = match_color(&s->colors, line);
3222 if (tc)
3223 wattr_on(view->window,
3224 COLOR_PAIR(tc->colorpair), NULL);
3225 if (s->first_displayed_line + nprinted == s->matched_line &&
3226 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3227 err = add_matched_line(&width, line, view->ncols, 0,
3228 view->window, view->x, regmatch);
3229 if (err) {
3230 free(line);
3231 return err;
3233 } else {
3234 err = format_line(&wline, &width, line,
3235 view->x + view->ncols, 0, view->x ? 1 : 0);
3236 if (err) {
3237 free(line);
3238 return err;
3240 if (view->x < width - 1)
3241 waddwstr(view->window, wline + view->x);
3242 free(wline);
3243 wline = NULL;
3245 if (tc)
3246 wattr_off(view->window,
3247 COLOR_PAIR(tc->colorpair), NULL);
3248 if (width - view->x <= view->ncols - 1)
3249 waddch(view->window, '\n');
3250 nprinted++;
3252 free(line);
3253 if (nprinted >= 1)
3254 s->last_displayed_line = s->first_displayed_line +
3255 (nprinted - 1);
3256 else
3257 s->last_displayed_line = s->first_displayed_line;
3259 view_vborder(view);
3261 if (s->eof) {
3262 while (nprinted < view->nlines) {
3263 waddch(view->window, '\n');
3264 nprinted++;
3267 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols,
3268 0, 0);
3269 if (err) {
3270 return err;
3273 wstandout(view->window);
3274 waddwstr(view->window, wline);
3275 free(wline);
3276 wline = NULL;
3277 wstandend(view->window);
3280 return NULL;
3283 static char *
3284 get_datestr(time_t *time, char *datebuf)
3286 struct tm mytm, *tm;
3287 char *p, *s;
3289 tm = gmtime_r(time, &mytm);
3290 if (tm == NULL)
3291 return NULL;
3292 s = asctime_r(tm, datebuf);
3293 if (s == NULL)
3294 return NULL;
3295 p = strchr(s, '\n');
3296 if (p)
3297 *p = '\0';
3298 return s;
3301 static const struct got_error *
3302 get_changed_paths(struct got_pathlist_head *paths,
3303 struct got_commit_object *commit, struct got_repository *repo)
3305 const struct got_error *err = NULL;
3306 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3307 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3308 struct got_object_qid *qid;
3310 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3311 if (qid != NULL) {
3312 struct got_commit_object *pcommit;
3313 err = got_object_open_as_commit(&pcommit, repo,
3314 &qid->id);
3315 if (err)
3316 return err;
3318 tree_id1 = got_object_id_dup(
3319 got_object_commit_get_tree_id(pcommit));
3320 if (tree_id1 == NULL) {
3321 got_object_commit_close(pcommit);
3322 return got_error_from_errno("got_object_id_dup");
3324 got_object_commit_close(pcommit);
3328 if (tree_id1) {
3329 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3330 if (err)
3331 goto done;
3334 tree_id2 = got_object_commit_get_tree_id(commit);
3335 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3336 if (err)
3337 goto done;
3339 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3340 got_diff_tree_collect_changed_paths, paths, 0);
3341 done:
3342 if (tree1)
3343 got_object_tree_close(tree1);
3344 if (tree2)
3345 got_object_tree_close(tree2);
3346 free(tree_id1);
3347 return err;
3350 static const struct got_error *
3351 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3353 off_t *p;
3355 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3356 if (p == NULL)
3357 return got_error_from_errno("reallocarray");
3358 *line_offsets = p;
3359 (*line_offsets)[*nlines] = off;
3360 (*nlines)++;
3361 return NULL;
3364 static const struct got_error *
3365 write_commit_info(off_t **line_offsets, size_t *nlines,
3366 struct got_object_id *commit_id, struct got_reflist_head *refs,
3367 struct got_repository *repo, FILE *outfile)
3369 const struct got_error *err = NULL;
3370 char datebuf[26], *datestr;
3371 struct got_commit_object *commit;
3372 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3373 time_t committer_time;
3374 const char *author, *committer;
3375 char *refs_str = NULL;
3376 struct got_pathlist_head changed_paths;
3377 struct got_pathlist_entry *pe;
3378 off_t outoff = 0;
3379 int n;
3381 TAILQ_INIT(&changed_paths);
3383 if (refs) {
3384 err = build_refs_str(&refs_str, refs, commit_id, repo);
3385 if (err)
3386 return err;
3389 err = got_object_open_as_commit(&commit, repo, commit_id);
3390 if (err)
3391 return err;
3393 err = got_object_id_str(&id_str, commit_id);
3394 if (err) {
3395 err = got_error_from_errno("got_object_id_str");
3396 goto done;
3399 err = add_line_offset(line_offsets, nlines, 0);
3400 if (err)
3401 goto done;
3403 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3404 refs_str ? refs_str : "", refs_str ? ")" : "");
3405 if (n < 0) {
3406 err = got_error_from_errno("fprintf");
3407 goto done;
3409 outoff += n;
3410 err = add_line_offset(line_offsets, nlines, outoff);
3411 if (err)
3412 goto done;
3414 n = fprintf(outfile, "from: %s\n",
3415 got_object_commit_get_author(commit));
3416 if (n < 0) {
3417 err = got_error_from_errno("fprintf");
3418 goto done;
3420 outoff += n;
3421 err = add_line_offset(line_offsets, nlines, outoff);
3422 if (err)
3423 goto done;
3425 committer_time = got_object_commit_get_committer_time(commit);
3426 datestr = get_datestr(&committer_time, datebuf);
3427 if (datestr) {
3428 n = fprintf(outfile, "date: %s UTC\n", datestr);
3429 if (n < 0) {
3430 err = got_error_from_errno("fprintf");
3431 goto done;
3433 outoff += n;
3434 err = add_line_offset(line_offsets, nlines, outoff);
3435 if (err)
3436 goto done;
3438 author = got_object_commit_get_author(commit);
3439 committer = got_object_commit_get_committer(commit);
3440 if (strcmp(author, committer) != 0) {
3441 n = fprintf(outfile, "via: %s\n", committer);
3442 if (n < 0) {
3443 err = got_error_from_errno("fprintf");
3444 goto done;
3446 outoff += n;
3447 err = add_line_offset(line_offsets, nlines, outoff);
3448 if (err)
3449 goto done;
3451 if (got_object_commit_get_nparents(commit) > 1) {
3452 const struct got_object_id_queue *parent_ids;
3453 struct got_object_qid *qid;
3454 int pn = 1;
3455 parent_ids = got_object_commit_get_parent_ids(commit);
3456 STAILQ_FOREACH(qid, parent_ids, entry) {
3457 err = got_object_id_str(&id_str, &qid->id);
3458 if (err)
3459 goto done;
3460 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3461 if (n < 0) {
3462 err = got_error_from_errno("fprintf");
3463 goto done;
3465 outoff += n;
3466 err = add_line_offset(line_offsets, nlines, outoff);
3467 if (err)
3468 goto done;
3469 free(id_str);
3470 id_str = NULL;
3474 err = got_object_commit_get_logmsg(&logmsg, commit);
3475 if (err)
3476 goto done;
3477 s = logmsg;
3478 while ((line = strsep(&s, "\n")) != NULL) {
3479 n = fprintf(outfile, "%s\n", line);
3480 if (n < 0) {
3481 err = got_error_from_errno("fprintf");
3482 goto done;
3484 outoff += n;
3485 err = add_line_offset(line_offsets, nlines, outoff);
3486 if (err)
3487 goto done;
3490 err = get_changed_paths(&changed_paths, commit, repo);
3491 if (err)
3492 goto done;
3493 TAILQ_FOREACH(pe, &changed_paths, entry) {
3494 struct got_diff_changed_path *cp = pe->data;
3495 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3496 if (n < 0) {
3497 err = got_error_from_errno("fprintf");
3498 goto done;
3500 outoff += n;
3501 err = add_line_offset(line_offsets, nlines, outoff);
3502 if (err)
3503 goto done;
3504 free((char *)pe->path);
3505 free(pe->data);
3508 fputc('\n', outfile);
3509 outoff++;
3510 err = add_line_offset(line_offsets, nlines, outoff);
3511 done:
3512 got_pathlist_free(&changed_paths);
3513 free(id_str);
3514 free(logmsg);
3515 free(refs_str);
3516 got_object_commit_close(commit);
3517 if (err) {
3518 free(*line_offsets);
3519 *line_offsets = NULL;
3520 *nlines = 0;
3522 return err;
3525 static const struct got_error *
3526 create_diff(struct tog_diff_view_state *s)
3528 const struct got_error *err = NULL;
3529 FILE *f = NULL;
3530 int obj_type;
3532 free(s->line_offsets);
3533 s->line_offsets = malloc(sizeof(off_t));
3534 if (s->line_offsets == NULL)
3535 return got_error_from_errno("malloc");
3536 s->nlines = 0;
3538 f = got_opentemp();
3539 if (f == NULL) {
3540 err = got_error_from_errno("got_opentemp");
3541 goto done;
3543 if (s->f && fclose(s->f) == EOF) {
3544 err = got_error_from_errno("fclose");
3545 goto done;
3547 s->f = f;
3549 if (s->id1)
3550 err = got_object_get_type(&obj_type, s->repo, s->id1);
3551 else
3552 err = got_object_get_type(&obj_type, s->repo, s->id2);
3553 if (err)
3554 goto done;
3556 switch (obj_type) {
3557 case GOT_OBJ_TYPE_BLOB:
3558 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3559 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3560 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3561 s->repo, s->f);
3562 break;
3563 case GOT_OBJ_TYPE_TREE:
3564 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3565 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3566 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3567 break;
3568 case GOT_OBJ_TYPE_COMMIT: {
3569 const struct got_object_id_queue *parent_ids;
3570 struct got_object_qid *pid;
3571 struct got_commit_object *commit2;
3572 struct got_reflist_head *refs;
3574 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3575 if (err)
3576 goto done;
3577 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3578 /* Show commit info if we're diffing to a parent/root commit. */
3579 if (s->id1 == NULL) {
3580 err = write_commit_info(&s->line_offsets, &s->nlines,
3581 s->id2, refs, s->repo, s->f);
3582 if (err)
3583 goto done;
3584 } else {
3585 parent_ids = got_object_commit_get_parent_ids(commit2);
3586 STAILQ_FOREACH(pid, parent_ids, entry) {
3587 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3588 err = write_commit_info(
3589 &s->line_offsets, &s->nlines,
3590 s->id2, refs, s->repo, s->f);
3591 if (err)
3592 goto done;
3593 break;
3597 got_object_commit_close(commit2);
3599 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3600 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3601 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3602 break;
3604 default:
3605 err = got_error(GOT_ERR_OBJ_TYPE);
3606 break;
3608 if (err)
3609 goto done;
3610 done:
3611 if (s->f && fflush(s->f) != 0 && err == NULL)
3612 err = got_error_from_errno("fflush");
3613 return err;
3616 static void
3617 diff_view_indicate_progress(struct tog_view *view)
3619 mvwaddstr(view->window, 0, 0, "diffing...");
3620 update_panels();
3621 doupdate();
3624 static const struct got_error *
3625 search_start_diff_view(struct tog_view *view)
3627 struct tog_diff_view_state *s = &view->state.diff;
3629 s->matched_line = 0;
3630 return NULL;
3633 static const struct got_error *
3634 search_next_diff_view(struct tog_view *view)
3636 struct tog_diff_view_state *s = &view->state.diff;
3637 const struct got_error *err = NULL;
3638 int lineno;
3639 char *exstr = NULL, *line = NULL;
3640 size_t linesize = 0;
3641 ssize_t linelen;
3643 if (!view->searching) {
3644 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3645 return NULL;
3648 if (s->matched_line) {
3649 if (view->searching == TOG_SEARCH_FORWARD)
3650 lineno = s->matched_line + 1;
3651 else
3652 lineno = s->matched_line - 1;
3653 } else
3654 lineno = s->first_displayed_line;
3656 while (1) {
3657 off_t offset;
3659 if (lineno <= 0 || lineno > s->nlines) {
3660 if (s->matched_line == 0) {
3661 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3662 break;
3665 if (view->searching == TOG_SEARCH_FORWARD)
3666 lineno = 1;
3667 else
3668 lineno = s->nlines;
3671 offset = s->line_offsets[lineno - 1];
3672 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3673 free(line);
3674 return got_error_from_errno("fseeko");
3676 linelen = getline(&line, &linesize, s->f);
3677 err = expand_tab(&exstr, line);
3678 if (err)
3679 break;
3680 if (linelen != -1 &&
3681 match_line(exstr, &view->regex, 1, &view->regmatch)) {
3682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3683 s->matched_line = lineno;
3684 break;
3686 free(exstr);
3687 exstr = NULL;
3688 if (view->searching == TOG_SEARCH_FORWARD)
3689 lineno++;
3690 else
3691 lineno--;
3693 free(line);
3694 free(exstr);
3696 if (s->matched_line) {
3697 s->first_displayed_line = s->matched_line;
3698 s->selected_line = 1;
3701 return err;
3704 static const struct got_error *
3705 close_diff_view(struct tog_view *view)
3707 const struct got_error *err = NULL;
3708 struct tog_diff_view_state *s = &view->state.diff;
3710 free(s->id1);
3711 s->id1 = NULL;
3712 free(s->id2);
3713 s->id2 = NULL;
3714 if (s->f && fclose(s->f) == EOF)
3715 err = got_error_from_errno("fclose");
3716 s->f = NULL;
3717 if (s->f1 && fclose(s->f1) == EOF)
3718 err = got_error_from_errno("fclose");
3719 s->f1 = NULL;
3720 if (s->f2 && fclose(s->f2) == EOF)
3721 err = got_error_from_errno("fclose");
3722 s->f2 = NULL;
3723 free_colors(&s->colors);
3724 free(s->line_offsets);
3725 s->line_offsets = NULL;
3726 s->nlines = 0;
3727 return err;
3730 static const struct got_error *
3731 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3732 struct got_object_id *id2, const char *label1, const char *label2,
3733 int diff_context, int ignore_whitespace, int force_text_diff,
3734 struct tog_view *log_view, struct got_repository *repo)
3736 const struct got_error *err;
3737 struct tog_diff_view_state *s = &view->state.diff;
3739 memset(s, 0, sizeof(*s));
3741 if (id1 != NULL && id2 != NULL) {
3742 int type1, type2;
3743 err = got_object_get_type(&type1, repo, id1);
3744 if (err)
3745 return err;
3746 err = got_object_get_type(&type2, repo, id2);
3747 if (err)
3748 return err;
3750 if (type1 != type2)
3751 return got_error(GOT_ERR_OBJ_TYPE);
3753 s->first_displayed_line = 1;
3754 s->last_displayed_line = view->nlines;
3755 s->selected_line = 1;
3756 s->repo = repo;
3757 s->id1 = id1;
3758 s->id2 = id2;
3759 s->label1 = label1;
3760 s->label2 = label2;
3762 if (id1) {
3763 s->id1 = got_object_id_dup(id1);
3764 if (s->id1 == NULL)
3765 return got_error_from_errno("got_object_id_dup");
3766 s->f1 = got_opentemp();
3767 if (s->f1 == NULL) {
3768 err = got_error_from_errno("got_opentemp");
3769 goto done;
3771 } else
3772 s->id1 = NULL;
3774 s->id2 = got_object_id_dup(id2);
3775 if (s->id2 == NULL) {
3776 err = got_error_from_errno("got_object_id_dup");
3777 goto done;
3780 s->f2 = got_opentemp();
3781 if (s->f2 == NULL) {
3782 err = got_error_from_errno("got_opentemp");
3783 goto done;
3786 s->first_displayed_line = 1;
3787 s->last_displayed_line = view->nlines;
3788 s->diff_context = diff_context;
3789 s->ignore_whitespace = ignore_whitespace;
3790 s->force_text_diff = force_text_diff;
3791 s->log_view = log_view;
3792 s->repo = repo;
3794 STAILQ_INIT(&s->colors);
3795 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3796 err = add_color(&s->colors,
3797 "^-", TOG_COLOR_DIFF_MINUS,
3798 get_color_value("TOG_COLOR_DIFF_MINUS"));
3799 if (err)
3800 goto done;
3801 err = add_color(&s->colors, "^\\+",
3802 TOG_COLOR_DIFF_PLUS,
3803 get_color_value("TOG_COLOR_DIFF_PLUS"));
3804 if (err)
3805 goto done;
3806 err = add_color(&s->colors,
3807 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3808 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3809 if (err)
3810 goto done;
3812 err = add_color(&s->colors,
3813 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3814 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3815 get_color_value("TOG_COLOR_DIFF_META"));
3816 if (err)
3817 goto done;
3819 err = add_color(&s->colors,
3820 "^(from|via): ", TOG_COLOR_AUTHOR,
3821 get_color_value("TOG_COLOR_AUTHOR"));
3822 if (err)
3823 goto done;
3825 err = add_color(&s->colors,
3826 "^date: ", TOG_COLOR_DATE,
3827 get_color_value("TOG_COLOR_DATE"));
3828 if (err)
3829 goto done;
3832 if (log_view && view_is_splitscreen(view))
3833 show_log_view(log_view); /* draw vborder */
3834 diff_view_indicate_progress(view);
3836 err = create_diff(s);
3838 view->show = show_diff_view;
3839 view->input = input_diff_view;
3840 view->close = close_diff_view;
3841 view->search_start = search_start_diff_view;
3842 view->search_next = search_next_diff_view;
3843 done:
3844 if (err)
3845 close_diff_view(view);
3846 return err;
3849 static const struct got_error *
3850 show_diff_view(struct tog_view *view)
3852 const struct got_error *err;
3853 struct tog_diff_view_state *s = &view->state.diff;
3854 char *id_str1 = NULL, *id_str2, *header;
3855 const char *label1, *label2;
3857 if (s->id1) {
3858 err = got_object_id_str(&id_str1, s->id1);
3859 if (err)
3860 return err;
3861 label1 = s->label1 ? : id_str1;
3862 } else
3863 label1 = "/dev/null";
3865 err = got_object_id_str(&id_str2, s->id2);
3866 if (err)
3867 return err;
3868 label2 = s->label2 ? : id_str2;
3870 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3871 err = got_error_from_errno("asprintf");
3872 free(id_str1);
3873 free(id_str2);
3874 return err;
3876 free(id_str1);
3877 free(id_str2);
3879 err = draw_file(view, header);
3880 free(header);
3881 return err;
3884 static const struct got_error *
3885 set_selected_commit(struct tog_diff_view_state *s,
3886 struct commit_queue_entry *entry)
3888 const struct got_error *err;
3889 const struct got_object_id_queue *parent_ids;
3890 struct got_commit_object *selected_commit;
3891 struct got_object_qid *pid;
3893 free(s->id2);
3894 s->id2 = got_object_id_dup(entry->id);
3895 if (s->id2 == NULL)
3896 return got_error_from_errno("got_object_id_dup");
3898 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3899 if (err)
3900 return err;
3901 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3902 free(s->id1);
3903 pid = STAILQ_FIRST(parent_ids);
3904 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3905 got_object_commit_close(selected_commit);
3906 return NULL;
3909 static const struct got_error *
3910 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3912 const struct got_error *err = NULL;
3913 struct tog_diff_view_state *s = &view->state.diff;
3914 struct tog_log_view_state *ls;
3915 struct commit_queue_entry *old_selected_entry;
3916 char *line = NULL;
3917 size_t linesize = 0;
3918 ssize_t linelen;
3919 int i, nscroll = view->nlines - 1;
3921 switch (ch) {
3922 case '0':
3923 view->x = 0;
3924 break;
3925 case '$':
3926 view->x = MAX(view->maxx - view->ncols / 3, 0);
3927 break;
3928 case KEY_RIGHT:
3929 case 'l':
3930 if (view->x + view->ncols / 3 < view->maxx)
3931 view->x += 2; /* move two columns right */
3932 break;
3933 case KEY_LEFT:
3934 case 'h':
3935 view->x -= MIN(view->x, 2); /* move two columns back */
3936 break;
3937 case 'a':
3938 case 'w':
3939 if (ch == 'a')
3940 s->force_text_diff = !s->force_text_diff;
3941 if (ch == 'w')
3942 s->ignore_whitespace = !s->ignore_whitespace;
3943 wclear(view->window);
3944 s->first_displayed_line = 1;
3945 s->last_displayed_line = view->nlines;
3946 s->matched_line = 0;
3947 diff_view_indicate_progress(view);
3948 err = create_diff(s);
3949 break;
3950 case 'g':
3951 case KEY_HOME:
3952 s->first_displayed_line = 1;
3953 break;
3954 case 'G':
3955 case KEY_END:
3956 if (s->eof)
3957 break;
3959 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3960 s->eof = 1;
3961 break;
3962 case 'k':
3963 case KEY_UP:
3964 case CTRL('p'):
3965 if (s->first_displayed_line > 1)
3966 s->first_displayed_line--;
3967 break;
3968 case CTRL('u'):
3969 case 'u':
3970 nscroll /= 2;
3971 /* FALL THROUGH */
3972 case KEY_PPAGE:
3973 case CTRL('b'):
3974 if (s->first_displayed_line == 1)
3975 break;
3976 i = 0;
3977 while (i++ < nscroll && s->first_displayed_line > 1)
3978 s->first_displayed_line--;
3979 break;
3980 case 'j':
3981 case KEY_DOWN:
3982 case CTRL('n'):
3983 if (!s->eof)
3984 s->first_displayed_line++;
3985 break;
3986 case CTRL('d'):
3987 case 'd':
3988 nscroll /= 2;
3989 /* FALL THROUGH */
3990 case KEY_NPAGE:
3991 case CTRL('f'):
3992 case ' ':
3993 if (s->eof)
3994 break;
3995 i = 0;
3996 while (!s->eof && i++ < nscroll) {
3997 linelen = getline(&line, &linesize, s->f);
3998 s->first_displayed_line++;
3999 if (linelen == -1) {
4000 if (feof(s->f)) {
4001 s->eof = 1;
4002 } else
4003 err = got_ferror(s->f, GOT_ERR_IO);
4004 break;
4007 free(line);
4008 break;
4009 case '[':
4010 if (s->diff_context > 0) {
4011 s->diff_context--;
4012 s->matched_line = 0;
4013 diff_view_indicate_progress(view);
4014 err = create_diff(s);
4015 if (s->first_displayed_line + view->nlines - 1 >
4016 s->nlines) {
4017 s->first_displayed_line = 1;
4018 s->last_displayed_line = view->nlines;
4021 break;
4022 case ']':
4023 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4024 s->diff_context++;
4025 s->matched_line = 0;
4026 diff_view_indicate_progress(view);
4027 err = create_diff(s);
4029 break;
4030 case '<':
4031 case ',':
4032 if (s->log_view == NULL)
4033 break;
4034 ls = &s->log_view->state.log;
4035 old_selected_entry = ls->selected_entry;
4037 err = input_log_view(NULL, s->log_view, KEY_UP);
4038 if (err)
4039 break;
4041 if (old_selected_entry == ls->selected_entry)
4042 break;
4044 err = set_selected_commit(s, ls->selected_entry);
4045 if (err)
4046 break;
4048 s->first_displayed_line = 1;
4049 s->last_displayed_line = view->nlines;
4050 s->matched_line = 0;
4051 view->x = 0;
4053 diff_view_indicate_progress(view);
4054 err = create_diff(s);
4055 break;
4056 case '>':
4057 case '.':
4058 if (s->log_view == NULL)
4059 break;
4060 ls = &s->log_view->state.log;
4061 old_selected_entry = ls->selected_entry;
4063 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4064 if (err)
4065 break;
4067 if (old_selected_entry == ls->selected_entry)
4068 break;
4070 err = set_selected_commit(s, ls->selected_entry);
4071 if (err)
4072 break;
4074 s->first_displayed_line = 1;
4075 s->last_displayed_line = view->nlines;
4076 s->matched_line = 0;
4077 view->x = 0;
4079 diff_view_indicate_progress(view);
4080 err = create_diff(s);
4081 break;
4082 default:
4083 break;
4086 return err;
4089 static const struct got_error *
4090 cmd_diff(int argc, char *argv[])
4092 const struct got_error *error = NULL;
4093 struct got_repository *repo = NULL;
4094 struct got_worktree *worktree = NULL;
4095 struct got_object_id *id1 = NULL, *id2 = NULL;
4096 char *repo_path = NULL, *cwd = NULL;
4097 char *id_str1 = NULL, *id_str2 = NULL;
4098 char *label1 = NULL, *label2 = NULL;
4099 int diff_context = 3, ignore_whitespace = 0;
4100 int ch, force_text_diff = 0;
4101 const char *errstr;
4102 struct tog_view *view;
4103 int *pack_fds = NULL;
4105 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4106 switch (ch) {
4107 case 'a':
4108 force_text_diff = 1;
4109 break;
4110 case 'C':
4111 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4112 &errstr);
4113 if (errstr != NULL)
4114 errx(1, "number of context lines is %s: %s",
4115 errstr, errstr);
4116 break;
4117 case 'r':
4118 repo_path = realpath(optarg, NULL);
4119 if (repo_path == NULL)
4120 return got_error_from_errno2("realpath",
4121 optarg);
4122 got_path_strip_trailing_slashes(repo_path);
4123 break;
4124 case 'w':
4125 ignore_whitespace = 1;
4126 break;
4127 default:
4128 usage_diff();
4129 /* NOTREACHED */
4133 argc -= optind;
4134 argv += optind;
4136 if (argc == 0) {
4137 usage_diff(); /* TODO show local worktree changes */
4138 } else if (argc == 2) {
4139 id_str1 = argv[0];
4140 id_str2 = argv[1];
4141 } else
4142 usage_diff();
4144 error = got_repo_pack_fds_open(&pack_fds);
4145 if (error)
4146 goto done;
4148 if (repo_path == NULL) {
4149 cwd = getcwd(NULL, 0);
4150 if (cwd == NULL)
4151 return got_error_from_errno("getcwd");
4152 error = got_worktree_open(&worktree, cwd);
4153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4154 goto done;
4155 if (worktree)
4156 repo_path =
4157 strdup(got_worktree_get_repo_path(worktree));
4158 else
4159 repo_path = strdup(cwd);
4160 if (repo_path == NULL) {
4161 error = got_error_from_errno("strdup");
4162 goto done;
4166 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4167 if (error)
4168 goto done;
4170 init_curses();
4172 error = apply_unveil(got_repo_get_path(repo), NULL);
4173 if (error)
4174 goto done;
4176 error = tog_load_refs(repo, 0);
4177 if (error)
4178 goto done;
4180 error = got_repo_match_object_id(&id1, &label1, id_str1,
4181 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4182 if (error)
4183 goto done;
4185 error = got_repo_match_object_id(&id2, &label2, id_str2,
4186 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4187 if (error)
4188 goto done;
4190 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4191 if (view == NULL) {
4192 error = got_error_from_errno("view_open");
4193 goto done;
4195 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4196 ignore_whitespace, force_text_diff, NULL, repo);
4197 if (error)
4198 goto done;
4199 error = view_loop(view);
4200 done:
4201 free(label1);
4202 free(label2);
4203 free(repo_path);
4204 free(cwd);
4205 if (repo) {
4206 const struct got_error *close_err = got_repo_close(repo);
4207 if (error == NULL)
4208 error = close_err;
4210 if (worktree)
4211 got_worktree_close(worktree);
4212 if (pack_fds) {
4213 const struct got_error *pack_err =
4214 got_repo_pack_fds_close(pack_fds);
4215 if (error == NULL)
4216 error = pack_err;
4218 tog_free_refs();
4219 return error;
4222 __dead static void
4223 usage_blame(void)
4225 endwin();
4226 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4227 getprogname());
4228 exit(1);
4231 struct tog_blame_line {
4232 int annotated;
4233 struct got_object_id *id;
4236 static const struct got_error *
4237 draw_blame(struct tog_view *view)
4239 struct tog_blame_view_state *s = &view->state.blame;
4240 struct tog_blame *blame = &s->blame;
4241 regmatch_t *regmatch = &view->regmatch;
4242 const struct got_error *err;
4243 int lineno = 0, nprinted = 0, i;
4244 char *line = NULL;
4245 size_t linesize = 0;
4246 ssize_t linelen;
4247 wchar_t *wline;
4248 int width;
4249 struct tog_blame_line *blame_line;
4250 struct got_object_id *prev_id = NULL;
4251 char *id_str;
4252 struct tog_color *tc;
4254 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4255 if (err)
4256 return err;
4258 rewind(blame->f);
4259 werase(view->window);
4261 if (asprintf(&line, "commit %s", id_str) == -1) {
4262 err = got_error_from_errno("asprintf");
4263 free(id_str);
4264 return err;
4267 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4268 free(line);
4269 line = NULL;
4270 if (err)
4271 return err;
4272 if (view_needs_focus_indication(view))
4273 wstandout(view->window);
4274 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4275 if (tc)
4276 wattr_on(view->window,
4277 COLOR_PAIR(tc->colorpair), NULL);
4278 waddwstr(view->window, wline);
4279 if (tc)
4280 wattr_off(view->window,
4281 COLOR_PAIR(tc->colorpair), NULL);
4282 if (view_needs_focus_indication(view))
4283 wstandend(view->window);
4284 free(wline);
4285 wline = NULL;
4286 if (width < view->ncols - 1)
4287 waddch(view->window, '\n');
4289 if (asprintf(&line, "[%d/%d] %s%s",
4290 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4291 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4292 free(id_str);
4293 return got_error_from_errno("asprintf");
4295 free(id_str);
4296 err = format_line(&wline, &width, line, view->ncols, 0, 0);
4297 free(line);
4298 line = NULL;
4299 if (err)
4300 return err;
4301 waddwstr(view->window, wline);
4302 free(wline);
4303 wline = NULL;
4304 if (width < view->ncols - 1)
4305 waddch(view->window, '\n');
4307 s->eof = 0;
4308 view->maxx = 0;
4309 while (nprinted < view->nlines - 2) {
4310 linelen = getline(&line, &linesize, blame->f);
4311 if (linelen == -1) {
4312 if (feof(blame->f)) {
4313 s->eof = 1;
4314 break;
4316 free(line);
4317 return got_ferror(blame->f, GOT_ERR_IO);
4319 if (++lineno < s->first_displayed_line)
4320 continue;
4322 view->maxx = MAX(view->maxx, linelen);
4324 if (view->focussed && nprinted == s->selected_line - 1)
4325 wstandout(view->window);
4327 if (blame->nlines > 0) {
4328 blame_line = &blame->lines[lineno - 1];
4329 if (blame_line->annotated && prev_id &&
4330 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4331 !(view->focussed &&
4332 nprinted == s->selected_line - 1)) {
4333 waddstr(view->window, " ");
4334 } else if (blame_line->annotated) {
4335 char *id_str;
4336 err = got_object_id_str(&id_str, blame_line->id);
4337 if (err) {
4338 free(line);
4339 return err;
4341 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4342 if (tc)
4343 wattr_on(view->window,
4344 COLOR_PAIR(tc->colorpair), NULL);
4345 wprintw(view->window, "%.8s", id_str);
4346 if (tc)
4347 wattr_off(view->window,
4348 COLOR_PAIR(tc->colorpair), NULL);
4349 free(id_str);
4350 prev_id = blame_line->id;
4351 } else {
4352 waddstr(view->window, "........");
4353 prev_id = NULL;
4355 } else {
4356 waddstr(view->window, "........");
4357 prev_id = NULL;
4360 if (view->focussed && nprinted == s->selected_line - 1)
4361 wstandend(view->window);
4362 waddstr(view->window, " ");
4364 if (view->ncols <= 9) {
4365 width = 9;
4366 wline = wcsdup(L"");
4367 if (wline == NULL) {
4368 err = got_error_from_errno("wcsdup");
4369 free(line);
4370 return err;
4372 } else if (s->first_displayed_line + nprinted ==
4373 s->matched_line &&
4374 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4375 err = add_matched_line(&width, line, view->ncols - 9, 9,
4376 view->window, view->x, regmatch);
4377 if (err) {
4378 free(line);
4379 return err;
4381 width += 9;
4382 } else {
4383 err = format_line(&wline, &width, line,
4384 view->x + view->ncols - 9, 9, 1);
4385 if (err) {
4386 free(line);
4387 return err;
4389 if (view->x < width) {
4390 waddwstr(view->window, wline + view->x);
4391 for (i = 0; i < view->x; i++)
4392 width -= wcwidth(wline[i]);
4394 width += 9;
4395 free(wline);
4396 wline = NULL;
4399 if (width <= view->ncols - 1)
4400 waddch(view->window, '\n');
4401 if (++nprinted == 1)
4402 s->first_displayed_line = lineno;
4404 free(line);
4405 s->last_displayed_line = lineno;
4407 view_vborder(view);
4409 return NULL;
4412 static const struct got_error *
4413 blame_cb(void *arg, int nlines, int lineno,
4414 struct got_commit_object *commit, struct got_object_id *id)
4416 const struct got_error *err = NULL;
4417 struct tog_blame_cb_args *a = arg;
4418 struct tog_blame_line *line;
4419 int errcode;
4421 if (nlines != a->nlines ||
4422 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4423 return got_error(GOT_ERR_RANGE);
4425 errcode = pthread_mutex_lock(&tog_mutex);
4426 if (errcode)
4427 return got_error_set_errno(errcode, "pthread_mutex_lock");
4429 if (*a->quit) { /* user has quit the blame view */
4430 err = got_error(GOT_ERR_ITER_COMPLETED);
4431 goto done;
4434 if (lineno == -1)
4435 goto done; /* no change in this commit */
4437 line = &a->lines[lineno - 1];
4438 if (line->annotated)
4439 goto done;
4441 line->id = got_object_id_dup(id);
4442 if (line->id == NULL) {
4443 err = got_error_from_errno("got_object_id_dup");
4444 goto done;
4446 line->annotated = 1;
4447 done:
4448 errcode = pthread_mutex_unlock(&tog_mutex);
4449 if (errcode)
4450 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4451 return err;
4454 static void *
4455 blame_thread(void *arg)
4457 const struct got_error *err, *close_err;
4458 struct tog_blame_thread_args *ta = arg;
4459 struct tog_blame_cb_args *a = ta->cb_args;
4460 int errcode;
4462 err = block_signals_used_by_main_thread();
4463 if (err)
4464 return (void *)err;
4466 err = got_blame(ta->path, a->commit_id, ta->repo,
4467 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4468 if (err && err->code == GOT_ERR_CANCELLED)
4469 err = NULL;
4471 errcode = pthread_mutex_lock(&tog_mutex);
4472 if (errcode)
4473 return (void *)got_error_set_errno(errcode,
4474 "pthread_mutex_lock");
4476 close_err = got_repo_close(ta->repo);
4477 if (err == NULL)
4478 err = close_err;
4479 ta->repo = NULL;
4480 *ta->complete = 1;
4482 errcode = pthread_mutex_unlock(&tog_mutex);
4483 if (errcode && err == NULL)
4484 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4486 return (void *)err;
4489 static struct got_object_id *
4490 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4491 int first_displayed_line, int selected_line)
4493 struct tog_blame_line *line;
4495 if (nlines <= 0)
4496 return NULL;
4498 line = &lines[first_displayed_line - 1 + selected_line - 1];
4499 if (!line->annotated)
4500 return NULL;
4502 return line->id;
4505 static const struct got_error *
4506 stop_blame(struct tog_blame *blame)
4508 const struct got_error *err = NULL;
4509 int i;
4511 if (blame->thread) {
4512 int errcode;
4513 errcode = pthread_mutex_unlock(&tog_mutex);
4514 if (errcode)
4515 return got_error_set_errno(errcode,
4516 "pthread_mutex_unlock");
4517 errcode = pthread_join(blame->thread, (void **)&err);
4518 if (errcode)
4519 return got_error_set_errno(errcode, "pthread_join");
4520 errcode = pthread_mutex_lock(&tog_mutex);
4521 if (errcode)
4522 return got_error_set_errno(errcode,
4523 "pthread_mutex_lock");
4524 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4525 err = NULL;
4526 blame->thread = NULL;
4528 if (blame->thread_args.repo) {
4529 const struct got_error *close_err;
4530 close_err = got_repo_close(blame->thread_args.repo);
4531 if (err == NULL)
4532 err = close_err;
4533 blame->thread_args.repo = NULL;
4535 if (blame->f) {
4536 if (fclose(blame->f) == EOF && err == NULL)
4537 err = got_error_from_errno("fclose");
4538 blame->f = NULL;
4540 if (blame->lines) {
4541 for (i = 0; i < blame->nlines; i++)
4542 free(blame->lines[i].id);
4543 free(blame->lines);
4544 blame->lines = NULL;
4546 free(blame->cb_args.commit_id);
4547 blame->cb_args.commit_id = NULL;
4548 if (blame->pack_fds) {
4549 const struct got_error *pack_err =
4550 got_repo_pack_fds_close(blame->pack_fds);
4551 if (err == NULL)
4552 err = pack_err;
4553 blame->pack_fds = NULL;
4555 return err;
4558 static const struct got_error *
4559 cancel_blame_view(void *arg)
4561 const struct got_error *err = NULL;
4562 int *done = arg;
4563 int errcode;
4565 errcode = pthread_mutex_lock(&tog_mutex);
4566 if (errcode)
4567 return got_error_set_errno(errcode,
4568 "pthread_mutex_unlock");
4570 if (*done)
4571 err = got_error(GOT_ERR_CANCELLED);
4573 errcode = pthread_mutex_unlock(&tog_mutex);
4574 if (errcode)
4575 return got_error_set_errno(errcode,
4576 "pthread_mutex_lock");
4578 return err;
4581 static const struct got_error *
4582 run_blame(struct tog_view *view)
4584 struct tog_blame_view_state *s = &view->state.blame;
4585 struct tog_blame *blame = &s->blame;
4586 const struct got_error *err = NULL;
4587 struct got_commit_object *commit = NULL;
4588 struct got_blob_object *blob = NULL;
4589 struct got_repository *thread_repo = NULL;
4590 struct got_object_id *obj_id = NULL;
4591 int obj_type;
4592 int *pack_fds = NULL;
4594 err = got_object_open_as_commit(&commit, s->repo,
4595 &s->blamed_commit->id);
4596 if (err)
4597 return err;
4599 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4600 if (err)
4601 goto done;
4603 err = got_object_get_type(&obj_type, s->repo, obj_id);
4604 if (err)
4605 goto done;
4607 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4608 err = got_error(GOT_ERR_OBJ_TYPE);
4609 goto done;
4612 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4613 if (err)
4614 goto done;
4615 blame->f = got_opentemp();
4616 if (blame->f == NULL) {
4617 err = got_error_from_errno("got_opentemp");
4618 goto done;
4620 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4621 &blame->line_offsets, blame->f, blob);
4622 if (err)
4623 goto done;
4624 if (blame->nlines == 0) {
4625 s->blame_complete = 1;
4626 goto done;
4629 /* Don't include \n at EOF in the blame line count. */
4630 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4631 blame->nlines--;
4633 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4634 if (blame->lines == NULL) {
4635 err = got_error_from_errno("calloc");
4636 goto done;
4639 err = got_repo_pack_fds_open(&pack_fds);
4640 if (err)
4641 goto done;
4642 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4643 pack_fds);
4644 if (err)
4645 goto done;
4647 blame->pack_fds = pack_fds;
4648 blame->cb_args.view = view;
4649 blame->cb_args.lines = blame->lines;
4650 blame->cb_args.nlines = blame->nlines;
4651 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4652 if (blame->cb_args.commit_id == NULL) {
4653 err = got_error_from_errno("got_object_id_dup");
4654 goto done;
4656 blame->cb_args.quit = &s->done;
4658 blame->thread_args.path = s->path;
4659 blame->thread_args.repo = thread_repo;
4660 blame->thread_args.cb_args = &blame->cb_args;
4661 blame->thread_args.complete = &s->blame_complete;
4662 blame->thread_args.cancel_cb = cancel_blame_view;
4663 blame->thread_args.cancel_arg = &s->done;
4664 s->blame_complete = 0;
4666 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4667 s->first_displayed_line = 1;
4668 s->last_displayed_line = view->nlines;
4669 s->selected_line = 1;
4671 s->matched_line = 0;
4673 done:
4674 if (commit)
4675 got_object_commit_close(commit);
4676 if (blob)
4677 got_object_blob_close(blob);
4678 free(obj_id);
4679 if (err)
4680 stop_blame(blame);
4681 return err;
4684 static const struct got_error *
4685 open_blame_view(struct tog_view *view, char *path,
4686 struct got_object_id *commit_id, struct got_repository *repo)
4688 const struct got_error *err = NULL;
4689 struct tog_blame_view_state *s = &view->state.blame;
4691 STAILQ_INIT(&s->blamed_commits);
4693 s->path = strdup(path);
4694 if (s->path == NULL)
4695 return got_error_from_errno("strdup");
4697 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4698 if (err) {
4699 free(s->path);
4700 return err;
4703 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4704 s->first_displayed_line = 1;
4705 s->last_displayed_line = view->nlines;
4706 s->selected_line = 1;
4707 s->blame_complete = 0;
4708 s->repo = repo;
4709 s->commit_id = commit_id;
4710 memset(&s->blame, 0, sizeof(s->blame));
4712 STAILQ_INIT(&s->colors);
4713 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4714 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4715 get_color_value("TOG_COLOR_COMMIT"));
4716 if (err)
4717 return err;
4720 view->show = show_blame_view;
4721 view->input = input_blame_view;
4722 view->close = close_blame_view;
4723 view->search_start = search_start_blame_view;
4724 view->search_next = search_next_blame_view;
4726 return run_blame(view);
4729 static const struct got_error *
4730 close_blame_view(struct tog_view *view)
4732 const struct got_error *err = NULL;
4733 struct tog_blame_view_state *s = &view->state.blame;
4735 if (s->blame.thread)
4736 err = stop_blame(&s->blame);
4738 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4739 struct got_object_qid *blamed_commit;
4740 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4741 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4742 got_object_qid_free(blamed_commit);
4745 free(s->path);
4746 free_colors(&s->colors);
4747 return err;
4750 static const struct got_error *
4751 search_start_blame_view(struct tog_view *view)
4753 struct tog_blame_view_state *s = &view->state.blame;
4755 s->matched_line = 0;
4756 return NULL;
4759 static const struct got_error *
4760 search_next_blame_view(struct tog_view *view)
4762 struct tog_blame_view_state *s = &view->state.blame;
4763 const struct got_error *err = NULL;
4764 int lineno;
4765 char *exstr = NULL, *line = NULL;
4766 size_t linesize = 0;
4767 ssize_t linelen;
4769 if (!view->searching) {
4770 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4771 return NULL;
4774 if (s->matched_line) {
4775 if (view->searching == TOG_SEARCH_FORWARD)
4776 lineno = s->matched_line + 1;
4777 else
4778 lineno = s->matched_line - 1;
4779 } else
4780 lineno = s->first_displayed_line - 1 + s->selected_line;
4782 while (1) {
4783 off_t offset;
4785 if (lineno <= 0 || lineno > s->blame.nlines) {
4786 if (s->matched_line == 0) {
4787 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4788 break;
4791 if (view->searching == TOG_SEARCH_FORWARD)
4792 lineno = 1;
4793 else
4794 lineno = s->blame.nlines;
4797 offset = s->blame.line_offsets[lineno - 1];
4798 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4799 free(line);
4800 return got_error_from_errno("fseeko");
4802 linelen = getline(&line, &linesize, s->blame.f);
4803 err = expand_tab(&exstr, line);
4804 if (err)
4805 break;
4806 if (linelen != -1 &&
4807 match_line(exstr, &view->regex, 1, &view->regmatch)) {
4808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4809 s->matched_line = lineno;
4810 break;
4812 free(exstr);
4813 exstr = NULL;
4814 if (view->searching == TOG_SEARCH_FORWARD)
4815 lineno++;
4816 else
4817 lineno--;
4819 free(line);
4820 free(exstr);
4822 if (s->matched_line) {
4823 s->first_displayed_line = s->matched_line;
4824 s->selected_line = 1;
4827 return err;
4830 static const struct got_error *
4831 show_blame_view(struct tog_view *view)
4833 const struct got_error *err = NULL;
4834 struct tog_blame_view_state *s = &view->state.blame;
4835 int errcode;
4837 if (s->blame.thread == NULL && !s->blame_complete) {
4838 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4839 &s->blame.thread_args);
4840 if (errcode)
4841 return got_error_set_errno(errcode, "pthread_create");
4843 halfdelay(1); /* fast refresh while annotating */
4846 if (s->blame_complete)
4847 halfdelay(10); /* disable fast refresh */
4849 err = draw_blame(view);
4851 view_vborder(view);
4852 return err;
4855 static const struct got_error *
4856 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4858 const struct got_error *err = NULL, *thread_err = NULL;
4859 struct tog_view *diff_view;
4860 struct tog_blame_view_state *s = &view->state.blame;
4861 int begin_x = 0, nscroll = view->nlines - 2;
4863 switch (ch) {
4864 case '0':
4865 view->x = 0;
4866 break;
4867 case '$':
4868 view->x = MAX(view->maxx - view->ncols / 3, 0);
4869 break;
4870 case KEY_RIGHT:
4871 case 'l':
4872 if (view->x + view->ncols / 3 < view->maxx)
4873 view->x += 2; /* move two columns right */
4874 break;
4875 case KEY_LEFT:
4876 case 'h':
4877 view->x -= MIN(view->x, 2); /* move two columns back */
4878 break;
4879 case 'q':
4880 s->done = 1;
4881 break;
4882 case 'g':
4883 case KEY_HOME:
4884 s->selected_line = 1;
4885 s->first_displayed_line = 1;
4886 break;
4887 case 'G':
4888 case KEY_END:
4889 if (s->blame.nlines < view->nlines - 2) {
4890 s->selected_line = s->blame.nlines;
4891 s->first_displayed_line = 1;
4892 } else {
4893 s->selected_line = view->nlines - 2;
4894 s->first_displayed_line = s->blame.nlines -
4895 (view->nlines - 3);
4897 break;
4898 case 'k':
4899 case KEY_UP:
4900 case CTRL('p'):
4901 if (s->selected_line > 1)
4902 s->selected_line--;
4903 else if (s->selected_line == 1 &&
4904 s->first_displayed_line > 1)
4905 s->first_displayed_line--;
4906 break;
4907 case CTRL('u'):
4908 case 'u':
4909 nscroll /= 2;
4910 /* FALL THROUGH */
4911 case KEY_PPAGE:
4912 case CTRL('b'):
4913 if (s->first_displayed_line == 1) {
4914 s->selected_line = MAX(1, s->selected_line - nscroll);
4915 break;
4917 if (s->first_displayed_line > nscroll)
4918 s->first_displayed_line -= nscroll;
4919 else
4920 s->first_displayed_line = 1;
4921 break;
4922 case 'j':
4923 case KEY_DOWN:
4924 case CTRL('n'):
4925 if (s->selected_line < view->nlines - 2 &&
4926 s->first_displayed_line +
4927 s->selected_line <= s->blame.nlines)
4928 s->selected_line++;
4929 else if (s->last_displayed_line <
4930 s->blame.nlines)
4931 s->first_displayed_line++;
4932 break;
4933 case 'b':
4934 case 'p': {
4935 struct got_object_id *id = NULL;
4936 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4937 s->first_displayed_line, s->selected_line);
4938 if (id == NULL)
4939 break;
4940 if (ch == 'p') {
4941 struct got_commit_object *commit, *pcommit;
4942 struct got_object_qid *pid;
4943 struct got_object_id *blob_id = NULL;
4944 int obj_type;
4945 err = got_object_open_as_commit(&commit,
4946 s->repo, id);
4947 if (err)
4948 break;
4949 pid = STAILQ_FIRST(
4950 got_object_commit_get_parent_ids(commit));
4951 if (pid == NULL) {
4952 got_object_commit_close(commit);
4953 break;
4955 /* Check if path history ends here. */
4956 err = got_object_open_as_commit(&pcommit,
4957 s->repo, &pid->id);
4958 if (err)
4959 break;
4960 err = got_object_id_by_path(&blob_id, s->repo,
4961 pcommit, s->path);
4962 got_object_commit_close(pcommit);
4963 if (err) {
4964 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4965 err = NULL;
4966 got_object_commit_close(commit);
4967 break;
4969 err = got_object_get_type(&obj_type, s->repo,
4970 blob_id);
4971 free(blob_id);
4972 /* Can't blame non-blob type objects. */
4973 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4974 got_object_commit_close(commit);
4975 break;
4977 err = got_object_qid_alloc(&s->blamed_commit,
4978 &pid->id);
4979 got_object_commit_close(commit);
4980 } else {
4981 if (got_object_id_cmp(id,
4982 &s->blamed_commit->id) == 0)
4983 break;
4984 err = got_object_qid_alloc(&s->blamed_commit,
4985 id);
4987 if (err)
4988 break;
4989 s->done = 1;
4990 thread_err = stop_blame(&s->blame);
4991 s->done = 0;
4992 if (thread_err)
4993 break;
4994 STAILQ_INSERT_HEAD(&s->blamed_commits,
4995 s->blamed_commit, entry);
4996 err = run_blame(view);
4997 if (err)
4998 break;
4999 break;
5001 case 'B': {
5002 struct got_object_qid *first;
5003 first = STAILQ_FIRST(&s->blamed_commits);
5004 if (!got_object_id_cmp(&first->id, s->commit_id))
5005 break;
5006 s->done = 1;
5007 thread_err = stop_blame(&s->blame);
5008 s->done = 0;
5009 if (thread_err)
5010 break;
5011 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5012 got_object_qid_free(s->blamed_commit);
5013 s->blamed_commit =
5014 STAILQ_FIRST(&s->blamed_commits);
5015 err = run_blame(view);
5016 if (err)
5017 break;
5018 break;
5020 case KEY_ENTER:
5021 case '\r': {
5022 struct got_object_id *id = NULL;
5023 struct got_object_qid *pid;
5024 struct got_commit_object *commit = NULL;
5025 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5026 s->first_displayed_line, s->selected_line);
5027 if (id == NULL)
5028 break;
5029 err = got_object_open_as_commit(&commit, s->repo, id);
5030 if (err)
5031 break;
5032 pid = STAILQ_FIRST(
5033 got_object_commit_get_parent_ids(commit));
5034 if (view_is_parent_view(view))
5035 begin_x = view_split_begin_x(view->begin_x);
5036 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5037 if (diff_view == NULL) {
5038 got_object_commit_close(commit);
5039 err = got_error_from_errno("view_open");
5040 break;
5042 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5043 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5044 got_object_commit_close(commit);
5045 if (err) {
5046 view_close(diff_view);
5047 break;
5049 view->focussed = 0;
5050 diff_view->focussed = 1;
5051 if (view_is_parent_view(view)) {
5052 err = view_close_child(view);
5053 if (err)
5054 break;
5055 view_set_child(view, diff_view);
5056 view->focus_child = 1;
5057 } else
5058 *new_view = diff_view;
5059 if (err)
5060 break;
5061 break;
5063 case CTRL('d'):
5064 case 'd':
5065 nscroll /= 2;
5066 /* FALL THROUGH */
5067 case KEY_NPAGE:
5068 case CTRL('f'):
5069 case ' ':
5070 if (s->last_displayed_line >= s->blame.nlines &&
5071 s->selected_line >= MIN(s->blame.nlines,
5072 view->nlines - 2)) {
5073 break;
5075 if (s->last_displayed_line >= s->blame.nlines &&
5076 s->selected_line < view->nlines - 2) {
5077 s->selected_line +=
5078 MIN(nscroll, s->last_displayed_line -
5079 s->first_displayed_line - s->selected_line + 1);
5081 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5082 s->first_displayed_line += nscroll;
5083 else
5084 s->first_displayed_line =
5085 s->blame.nlines - (view->nlines - 3);
5086 break;
5087 case KEY_RESIZE:
5088 if (s->selected_line > view->nlines - 2) {
5089 s->selected_line = MIN(s->blame.nlines,
5090 view->nlines - 2);
5092 break;
5093 default:
5094 break;
5096 return thread_err ? thread_err : err;
5099 static const struct got_error *
5100 cmd_blame(int argc, char *argv[])
5102 const struct got_error *error;
5103 struct got_repository *repo = NULL;
5104 struct got_worktree *worktree = NULL;
5105 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5106 char *link_target = NULL;
5107 struct got_object_id *commit_id = NULL;
5108 struct got_commit_object *commit = NULL;
5109 char *commit_id_str = NULL;
5110 int ch;
5111 struct tog_view *view;
5112 int *pack_fds = NULL;
5114 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5115 switch (ch) {
5116 case 'c':
5117 commit_id_str = optarg;
5118 break;
5119 case 'r':
5120 repo_path = realpath(optarg, NULL);
5121 if (repo_path == NULL)
5122 return got_error_from_errno2("realpath",
5123 optarg);
5124 break;
5125 default:
5126 usage_blame();
5127 /* NOTREACHED */
5131 argc -= optind;
5132 argv += optind;
5134 if (argc != 1)
5135 usage_blame();
5137 error = got_repo_pack_fds_open(&pack_fds);
5138 if (error != NULL)
5139 goto done;
5141 if (repo_path == NULL) {
5142 cwd = getcwd(NULL, 0);
5143 if (cwd == NULL)
5144 return got_error_from_errno("getcwd");
5145 error = got_worktree_open(&worktree, cwd);
5146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5147 goto done;
5148 if (worktree)
5149 repo_path =
5150 strdup(got_worktree_get_repo_path(worktree));
5151 else
5152 repo_path = strdup(cwd);
5153 if (repo_path == NULL) {
5154 error = got_error_from_errno("strdup");
5155 goto done;
5159 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5160 if (error != NULL)
5161 goto done;
5163 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5164 worktree);
5165 if (error)
5166 goto done;
5168 init_curses();
5170 error = apply_unveil(got_repo_get_path(repo), NULL);
5171 if (error)
5172 goto done;
5174 error = tog_load_refs(repo, 0);
5175 if (error)
5176 goto done;
5178 if (commit_id_str == NULL) {
5179 struct got_reference *head_ref;
5180 error = got_ref_open(&head_ref, repo, worktree ?
5181 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5182 if (error != NULL)
5183 goto done;
5184 error = got_ref_resolve(&commit_id, repo, head_ref);
5185 got_ref_close(head_ref);
5186 } else {
5187 error = got_repo_match_object_id(&commit_id, NULL,
5188 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5190 if (error != NULL)
5191 goto done;
5193 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5194 if (view == NULL) {
5195 error = got_error_from_errno("view_open");
5196 goto done;
5199 error = got_object_open_as_commit(&commit, repo, commit_id);
5200 if (error)
5201 goto done;
5203 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5204 commit, repo);
5205 if (error)
5206 goto done;
5208 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5209 commit_id, repo);
5210 if (error)
5211 goto done;
5212 if (worktree) {
5213 /* Release work tree lock. */
5214 got_worktree_close(worktree);
5215 worktree = NULL;
5217 error = view_loop(view);
5218 done:
5219 free(repo_path);
5220 free(in_repo_path);
5221 free(link_target);
5222 free(cwd);
5223 free(commit_id);
5224 if (commit)
5225 got_object_commit_close(commit);
5226 if (worktree)
5227 got_worktree_close(worktree);
5228 if (repo) {
5229 const struct got_error *close_err = got_repo_close(repo);
5230 if (error == NULL)
5231 error = close_err;
5233 if (pack_fds) {
5234 const struct got_error *pack_err =
5235 got_repo_pack_fds_close(pack_fds);
5236 if (error == NULL)
5237 error = pack_err;
5239 tog_free_refs();
5240 return error;
5243 static const struct got_error *
5244 draw_tree_entries(struct tog_view *view, const char *parent_path)
5246 struct tog_tree_view_state *s = &view->state.tree;
5247 const struct got_error *err = NULL;
5248 struct got_tree_entry *te;
5249 wchar_t *wline;
5250 struct tog_color *tc;
5251 int width, n, i, nentries;
5252 int limit = view->nlines;
5254 s->ndisplayed = 0;
5256 werase(view->window);
5258 if (limit == 0)
5259 return NULL;
5261 err = format_line(&wline, &width, s->tree_label, view->ncols, 0, 0);
5262 if (err)
5263 return err;
5264 if (view_needs_focus_indication(view))
5265 wstandout(view->window);
5266 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5267 if (tc)
5268 wattr_on(view->window,
5269 COLOR_PAIR(tc->colorpair), NULL);
5270 waddwstr(view->window, wline);
5271 if (tc)
5272 wattr_off(view->window,
5273 COLOR_PAIR(tc->colorpair), NULL);
5274 if (view_needs_focus_indication(view))
5275 wstandend(view->window);
5276 free(wline);
5277 wline = NULL;
5278 if (width < view->ncols - 1)
5279 waddch(view->window, '\n');
5280 if (--limit <= 0)
5281 return NULL;
5282 err = format_line(&wline, &width, parent_path, view->ncols, 0, 0);
5283 if (err)
5284 return err;
5285 waddwstr(view->window, wline);
5286 free(wline);
5287 wline = NULL;
5288 if (width < view->ncols - 1)
5289 waddch(view->window, '\n');
5290 if (--limit <= 0)
5291 return NULL;
5292 waddch(view->window, '\n');
5293 if (--limit <= 0)
5294 return NULL;
5296 if (s->first_displayed_entry == NULL) {
5297 te = got_object_tree_get_first_entry(s->tree);
5298 if (s->selected == 0) {
5299 if (view->focussed)
5300 wstandout(view->window);
5301 s->selected_entry = NULL;
5303 waddstr(view->window, " ..\n"); /* parent directory */
5304 if (s->selected == 0 && view->focussed)
5305 wstandend(view->window);
5306 s->ndisplayed++;
5307 if (--limit <= 0)
5308 return NULL;
5309 n = 1;
5310 } else {
5311 n = 0;
5312 te = s->first_displayed_entry;
5315 nentries = got_object_tree_get_nentries(s->tree);
5316 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5317 char *line = NULL, *id_str = NULL, *link_target = NULL;
5318 const char *modestr = "";
5319 mode_t mode;
5321 te = got_object_tree_get_entry(s->tree, i);
5322 mode = got_tree_entry_get_mode(te);
5324 if (s->show_ids) {
5325 err = got_object_id_str(&id_str,
5326 got_tree_entry_get_id(te));
5327 if (err)
5328 return got_error_from_errno(
5329 "got_object_id_str");
5331 if (got_object_tree_entry_is_submodule(te))
5332 modestr = "$";
5333 else if (S_ISLNK(mode)) {
5334 int i;
5336 err = got_tree_entry_get_symlink_target(&link_target,
5337 te, s->repo);
5338 if (err) {
5339 free(id_str);
5340 return err;
5342 for (i = 0; i < strlen(link_target); i++) {
5343 if (!isprint((unsigned char)link_target[i]))
5344 link_target[i] = '?';
5346 modestr = "@";
5348 else if (S_ISDIR(mode))
5349 modestr = "/";
5350 else if (mode & S_IXUSR)
5351 modestr = "*";
5352 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5353 got_tree_entry_get_name(te), modestr,
5354 link_target ? " -> ": "",
5355 link_target ? link_target : "") == -1) {
5356 free(id_str);
5357 free(link_target);
5358 return got_error_from_errno("asprintf");
5360 free(id_str);
5361 free(link_target);
5362 err = format_line(&wline, &width, line, view->ncols, 0, 0);
5363 if (err) {
5364 free(line);
5365 break;
5367 if (n == s->selected) {
5368 if (view->focussed)
5369 wstandout(view->window);
5370 s->selected_entry = te;
5372 tc = match_color(&s->colors, line);
5373 if (tc)
5374 wattr_on(view->window,
5375 COLOR_PAIR(tc->colorpair), NULL);
5376 waddwstr(view->window, wline);
5377 if (tc)
5378 wattr_off(view->window,
5379 COLOR_PAIR(tc->colorpair), NULL);
5380 if (width < view->ncols - 1)
5381 waddch(view->window, '\n');
5382 if (n == s->selected && view->focussed)
5383 wstandend(view->window);
5384 free(line);
5385 free(wline);
5386 wline = NULL;
5387 n++;
5388 s->ndisplayed++;
5389 s->last_displayed_entry = te;
5390 if (--limit <= 0)
5391 break;
5394 return err;
5397 static void
5398 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5400 struct got_tree_entry *te;
5401 int isroot = s->tree == s->root;
5402 int i = 0;
5404 if (s->first_displayed_entry == NULL)
5405 return;
5407 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5408 while (i++ < maxscroll) {
5409 if (te == NULL) {
5410 if (!isroot)
5411 s->first_displayed_entry = NULL;
5412 break;
5414 s->first_displayed_entry = te;
5415 te = got_tree_entry_get_prev(s->tree, te);
5419 static void
5420 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5422 struct got_tree_entry *next, *last;
5423 int n = 0;
5425 if (s->first_displayed_entry)
5426 next = got_tree_entry_get_next(s->tree,
5427 s->first_displayed_entry);
5428 else
5429 next = got_object_tree_get_first_entry(s->tree);
5431 last = s->last_displayed_entry;
5432 while (next && last && n++ < maxscroll) {
5433 last = got_tree_entry_get_next(s->tree, last);
5434 if (last) {
5435 s->first_displayed_entry = next;
5436 next = got_tree_entry_get_next(s->tree, next);
5441 static const struct got_error *
5442 tree_entry_path(char **path, struct tog_parent_trees *parents,
5443 struct got_tree_entry *te)
5445 const struct got_error *err = NULL;
5446 struct tog_parent_tree *pt;
5447 size_t len = 2; /* for leading slash and NUL */
5449 TAILQ_FOREACH(pt, parents, entry)
5450 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5451 + 1 /* slash */;
5452 if (te)
5453 len += strlen(got_tree_entry_get_name(te));
5455 *path = calloc(1, len);
5456 if (path == NULL)
5457 return got_error_from_errno("calloc");
5459 (*path)[0] = '/';
5460 pt = TAILQ_LAST(parents, tog_parent_trees);
5461 while (pt) {
5462 const char *name = got_tree_entry_get_name(pt->selected_entry);
5463 if (strlcat(*path, name, len) >= len) {
5464 err = got_error(GOT_ERR_NO_SPACE);
5465 goto done;
5467 if (strlcat(*path, "/", len) >= len) {
5468 err = got_error(GOT_ERR_NO_SPACE);
5469 goto done;
5471 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5473 if (te) {
5474 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5475 err = got_error(GOT_ERR_NO_SPACE);
5476 goto done;
5479 done:
5480 if (err) {
5481 free(*path);
5482 *path = NULL;
5484 return err;
5487 static const struct got_error *
5488 blame_tree_entry(struct tog_view **new_view, int begin_x,
5489 struct got_tree_entry *te, struct tog_parent_trees *parents,
5490 struct got_object_id *commit_id, struct got_repository *repo)
5492 const struct got_error *err = NULL;
5493 char *path;
5494 struct tog_view *blame_view;
5496 *new_view = NULL;
5498 err = tree_entry_path(&path, parents, te);
5499 if (err)
5500 return err;
5502 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5503 if (blame_view == NULL) {
5504 err = got_error_from_errno("view_open");
5505 goto done;
5508 err = open_blame_view(blame_view, path, commit_id, repo);
5509 if (err) {
5510 if (err->code == GOT_ERR_CANCELLED)
5511 err = NULL;
5512 view_close(blame_view);
5513 } else
5514 *new_view = blame_view;
5515 done:
5516 free(path);
5517 return err;
5520 static const struct got_error *
5521 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5522 struct tog_tree_view_state *s)
5524 struct tog_view *log_view;
5525 const struct got_error *err = NULL;
5526 char *path;
5528 *new_view = NULL;
5530 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5531 if (log_view == NULL)
5532 return got_error_from_errno("view_open");
5534 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5535 if (err)
5536 return err;
5538 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5539 path, 0);
5540 if (err)
5541 view_close(log_view);
5542 else
5543 *new_view = log_view;
5544 free(path);
5545 return err;
5548 static const struct got_error *
5549 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5550 const char *head_ref_name, struct got_repository *repo)
5552 const struct got_error *err = NULL;
5553 char *commit_id_str = NULL;
5554 struct tog_tree_view_state *s = &view->state.tree;
5555 struct got_commit_object *commit = NULL;
5557 TAILQ_INIT(&s->parents);
5558 STAILQ_INIT(&s->colors);
5560 s->commit_id = got_object_id_dup(commit_id);
5561 if (s->commit_id == NULL)
5562 return got_error_from_errno("got_object_id_dup");
5564 err = got_object_open_as_commit(&commit, repo, commit_id);
5565 if (err)
5566 goto done;
5569 * The root is opened here and will be closed when the view is closed.
5570 * Any visited subtrees and their path-wise parents are opened and
5571 * closed on demand.
5573 err = got_object_open_as_tree(&s->root, repo,
5574 got_object_commit_get_tree_id(commit));
5575 if (err)
5576 goto done;
5577 s->tree = s->root;
5579 err = got_object_id_str(&commit_id_str, commit_id);
5580 if (err != NULL)
5581 goto done;
5583 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5584 err = got_error_from_errno("asprintf");
5585 goto done;
5588 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5589 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5590 if (head_ref_name) {
5591 s->head_ref_name = strdup(head_ref_name);
5592 if (s->head_ref_name == NULL) {
5593 err = got_error_from_errno("strdup");
5594 goto done;
5597 s->repo = repo;
5599 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5600 err = add_color(&s->colors, "\\$$",
5601 TOG_COLOR_TREE_SUBMODULE,
5602 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5603 if (err)
5604 goto done;
5605 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5606 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5607 if (err)
5608 goto done;
5609 err = add_color(&s->colors, "/$",
5610 TOG_COLOR_TREE_DIRECTORY,
5611 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5612 if (err)
5613 goto done;
5615 err = add_color(&s->colors, "\\*$",
5616 TOG_COLOR_TREE_EXECUTABLE,
5617 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5618 if (err)
5619 goto done;
5621 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5622 get_color_value("TOG_COLOR_COMMIT"));
5623 if (err)
5624 goto done;
5627 view->show = show_tree_view;
5628 view->input = input_tree_view;
5629 view->close = close_tree_view;
5630 view->search_start = search_start_tree_view;
5631 view->search_next = search_next_tree_view;
5632 done:
5633 free(commit_id_str);
5634 if (commit)
5635 got_object_commit_close(commit);
5636 if (err)
5637 close_tree_view(view);
5638 return err;
5641 static const struct got_error *
5642 close_tree_view(struct tog_view *view)
5644 struct tog_tree_view_state *s = &view->state.tree;
5646 free_colors(&s->colors);
5647 free(s->tree_label);
5648 s->tree_label = NULL;
5649 free(s->commit_id);
5650 s->commit_id = NULL;
5651 free(s->head_ref_name);
5652 s->head_ref_name = NULL;
5653 while (!TAILQ_EMPTY(&s->parents)) {
5654 struct tog_parent_tree *parent;
5655 parent = TAILQ_FIRST(&s->parents);
5656 TAILQ_REMOVE(&s->parents, parent, entry);
5657 if (parent->tree != s->root)
5658 got_object_tree_close(parent->tree);
5659 free(parent);
5662 if (s->tree != NULL && s->tree != s->root)
5663 got_object_tree_close(s->tree);
5664 if (s->root)
5665 got_object_tree_close(s->root);
5666 return NULL;
5669 static const struct got_error *
5670 search_start_tree_view(struct tog_view *view)
5672 struct tog_tree_view_state *s = &view->state.tree;
5674 s->matched_entry = NULL;
5675 return NULL;
5678 static int
5679 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5681 regmatch_t regmatch;
5683 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5684 0) == 0;
5687 static const struct got_error *
5688 search_next_tree_view(struct tog_view *view)
5690 struct tog_tree_view_state *s = &view->state.tree;
5691 struct got_tree_entry *te = NULL;
5693 if (!view->searching) {
5694 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5695 return NULL;
5698 if (s->matched_entry) {
5699 if (view->searching == TOG_SEARCH_FORWARD) {
5700 if (s->selected_entry)
5701 te = got_tree_entry_get_next(s->tree,
5702 s->selected_entry);
5703 else
5704 te = got_object_tree_get_first_entry(s->tree);
5705 } else {
5706 if (s->selected_entry == NULL)
5707 te = got_object_tree_get_last_entry(s->tree);
5708 else
5709 te = got_tree_entry_get_prev(s->tree,
5710 s->selected_entry);
5712 } else {
5713 if (s->selected_entry)
5714 te = s->selected_entry;
5715 else if (view->searching == TOG_SEARCH_FORWARD)
5716 te = got_object_tree_get_first_entry(s->tree);
5717 else
5718 te = got_object_tree_get_last_entry(s->tree);
5721 while (1) {
5722 if (te == NULL) {
5723 if (s->matched_entry == NULL) {
5724 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5725 return NULL;
5727 if (view->searching == TOG_SEARCH_FORWARD)
5728 te = got_object_tree_get_first_entry(s->tree);
5729 else
5730 te = got_object_tree_get_last_entry(s->tree);
5733 if (match_tree_entry(te, &view->regex)) {
5734 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5735 s->matched_entry = te;
5736 break;
5739 if (view->searching == TOG_SEARCH_FORWARD)
5740 te = got_tree_entry_get_next(s->tree, te);
5741 else
5742 te = got_tree_entry_get_prev(s->tree, te);
5745 if (s->matched_entry) {
5746 s->first_displayed_entry = s->matched_entry;
5747 s->selected = 0;
5750 return NULL;
5753 static const struct got_error *
5754 show_tree_view(struct tog_view *view)
5756 const struct got_error *err = NULL;
5757 struct tog_tree_view_state *s = &view->state.tree;
5758 char *parent_path;
5760 err = tree_entry_path(&parent_path, &s->parents, NULL);
5761 if (err)
5762 return err;
5764 err = draw_tree_entries(view, parent_path);
5765 free(parent_path);
5767 view_vborder(view);
5768 return err;
5771 static const struct got_error *
5772 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5774 const struct got_error *err = NULL;
5775 struct tog_tree_view_state *s = &view->state.tree;
5776 struct tog_view *log_view, *ref_view;
5777 struct got_tree_entry *te;
5778 int begin_x = 0, n, nscroll = view->nlines - 3;
5780 switch (ch) {
5781 case 'i':
5782 s->show_ids = !s->show_ids;
5783 break;
5784 case 'l':
5785 if (!s->selected_entry)
5786 break;
5787 if (view_is_parent_view(view))
5788 begin_x = view_split_begin_x(view->begin_x);
5789 err = log_selected_tree_entry(&log_view, begin_x, s);
5790 view->focussed = 0;
5791 log_view->focussed = 1;
5792 if (view_is_parent_view(view)) {
5793 err = view_close_child(view);
5794 if (err)
5795 return err;
5796 view_set_child(view, log_view);
5797 view->focus_child = 1;
5798 } else
5799 *new_view = log_view;
5800 break;
5801 case 'r':
5802 if (view_is_parent_view(view))
5803 begin_x = view_split_begin_x(view->begin_x);
5804 ref_view = view_open(view->nlines, view->ncols,
5805 view->begin_y, begin_x, TOG_VIEW_REF);
5806 if (ref_view == NULL)
5807 return got_error_from_errno("view_open");
5808 err = open_ref_view(ref_view, s->repo);
5809 if (err) {
5810 view_close(ref_view);
5811 return err;
5813 view->focussed = 0;
5814 ref_view->focussed = 1;
5815 if (view_is_parent_view(view)) {
5816 err = view_close_child(view);
5817 if (err)
5818 return err;
5819 view_set_child(view, ref_view);
5820 view->focus_child = 1;
5821 } else
5822 *new_view = ref_view;
5823 break;
5824 case 'g':
5825 case KEY_HOME:
5826 s->selected = 0;
5827 if (s->tree == s->root)
5828 s->first_displayed_entry =
5829 got_object_tree_get_first_entry(s->tree);
5830 else
5831 s->first_displayed_entry = NULL;
5832 break;
5833 case 'G':
5834 case KEY_END:
5835 s->selected = 0;
5836 te = got_object_tree_get_last_entry(s->tree);
5837 for (n = 0; n < view->nlines - 3; n++) {
5838 if (te == NULL) {
5839 if(s->tree != s->root) {
5840 s->first_displayed_entry = NULL;
5841 n++;
5843 break;
5845 s->first_displayed_entry = te;
5846 te = got_tree_entry_get_prev(s->tree, te);
5848 if (n > 0)
5849 s->selected = n - 1;
5850 break;
5851 case 'k':
5852 case KEY_UP:
5853 case CTRL('p'):
5854 if (s->selected > 0) {
5855 s->selected--;
5856 break;
5858 tree_scroll_up(s, 1);
5859 break;
5860 case CTRL('u'):
5861 case 'u':
5862 nscroll /= 2;
5863 /* FALL THROUGH */
5864 case KEY_PPAGE:
5865 case CTRL('b'):
5866 if (s->tree == s->root) {
5867 if (got_object_tree_get_first_entry(s->tree) ==
5868 s->first_displayed_entry)
5869 s->selected -= MIN(s->selected, nscroll);
5870 } else {
5871 if (s->first_displayed_entry == NULL)
5872 s->selected -= MIN(s->selected, nscroll);
5874 tree_scroll_up(s, MAX(0, nscroll));
5875 break;
5876 case 'j':
5877 case KEY_DOWN:
5878 case CTRL('n'):
5879 if (s->selected < s->ndisplayed - 1) {
5880 s->selected++;
5881 break;
5883 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5884 == NULL)
5885 /* can't scroll any further */
5886 break;
5887 tree_scroll_down(s, 1);
5888 break;
5889 case CTRL('d'):
5890 case 'd':
5891 nscroll /= 2;
5892 /* FALL THROUGH */
5893 case KEY_NPAGE:
5894 case CTRL('f'):
5895 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5896 == NULL) {
5897 /* can't scroll any further; move cursor down */
5898 if (s->selected < s->ndisplayed - 1)
5899 s->selected += MIN(nscroll,
5900 s->ndisplayed - s->selected - 1);
5901 break;
5903 tree_scroll_down(s, nscroll);
5904 break;
5905 case KEY_ENTER:
5906 case '\r':
5907 case KEY_BACKSPACE:
5908 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5909 struct tog_parent_tree *parent;
5910 /* user selected '..' */
5911 if (s->tree == s->root)
5912 break;
5913 parent = TAILQ_FIRST(&s->parents);
5914 TAILQ_REMOVE(&s->parents, parent,
5915 entry);
5916 got_object_tree_close(s->tree);
5917 s->tree = parent->tree;
5918 s->first_displayed_entry =
5919 parent->first_displayed_entry;
5920 s->selected_entry =
5921 parent->selected_entry;
5922 s->selected = parent->selected;
5923 free(parent);
5924 } else if (S_ISDIR(got_tree_entry_get_mode(
5925 s->selected_entry))) {
5926 struct got_tree_object *subtree;
5927 err = got_object_open_as_tree(&subtree, s->repo,
5928 got_tree_entry_get_id(s->selected_entry));
5929 if (err)
5930 break;
5931 err = tree_view_visit_subtree(s, subtree);
5932 if (err) {
5933 got_object_tree_close(subtree);
5934 break;
5936 } else if (S_ISREG(got_tree_entry_get_mode(
5937 s->selected_entry))) {
5938 struct tog_view *blame_view;
5939 int begin_x = view_is_parent_view(view) ?
5940 view_split_begin_x(view->begin_x) : 0;
5942 err = blame_tree_entry(&blame_view, begin_x,
5943 s->selected_entry, &s->parents,
5944 s->commit_id, s->repo);
5945 if (err)
5946 break;
5947 view->focussed = 0;
5948 blame_view->focussed = 1;
5949 if (view_is_parent_view(view)) {
5950 err = view_close_child(view);
5951 if (err)
5952 return err;
5953 view_set_child(view, blame_view);
5954 view->focus_child = 1;
5955 } else
5956 *new_view = blame_view;
5958 break;
5959 case KEY_RESIZE:
5960 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5961 s->selected = view->nlines - 4;
5962 break;
5963 default:
5964 break;
5967 return err;
5970 __dead static void
5971 usage_tree(void)
5973 endwin();
5974 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5975 getprogname());
5976 exit(1);
5979 static const struct got_error *
5980 cmd_tree(int argc, char *argv[])
5982 const struct got_error *error;
5983 struct got_repository *repo = NULL;
5984 struct got_worktree *worktree = NULL;
5985 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5986 struct got_object_id *commit_id = NULL;
5987 struct got_commit_object *commit = NULL;
5988 const char *commit_id_arg = NULL;
5989 char *label = NULL;
5990 struct got_reference *ref = NULL;
5991 const char *head_ref_name = NULL;
5992 int ch;
5993 struct tog_view *view;
5994 int *pack_fds = NULL;
5996 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5997 switch (ch) {
5998 case 'c':
5999 commit_id_arg = optarg;
6000 break;
6001 case 'r':
6002 repo_path = realpath(optarg, NULL);
6003 if (repo_path == NULL)
6004 return got_error_from_errno2("realpath",
6005 optarg);
6006 break;
6007 default:
6008 usage_tree();
6009 /* NOTREACHED */
6013 argc -= optind;
6014 argv += optind;
6016 if (argc > 1)
6017 usage_tree();
6019 error = got_repo_pack_fds_open(&pack_fds);
6020 if (error != NULL)
6021 goto done;
6023 if (repo_path == NULL) {
6024 cwd = getcwd(NULL, 0);
6025 if (cwd == NULL)
6026 return got_error_from_errno("getcwd");
6027 error = got_worktree_open(&worktree, cwd);
6028 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6029 goto done;
6030 if (worktree)
6031 repo_path =
6032 strdup(got_worktree_get_repo_path(worktree));
6033 else
6034 repo_path = strdup(cwd);
6035 if (repo_path == NULL) {
6036 error = got_error_from_errno("strdup");
6037 goto done;
6041 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6042 if (error != NULL)
6043 goto done;
6045 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6046 repo, worktree);
6047 if (error)
6048 goto done;
6050 init_curses();
6052 error = apply_unveil(got_repo_get_path(repo), NULL);
6053 if (error)
6054 goto done;
6056 error = tog_load_refs(repo, 0);
6057 if (error)
6058 goto done;
6060 if (commit_id_arg == NULL) {
6061 error = got_repo_match_object_id(&commit_id, &label,
6062 worktree ? got_worktree_get_head_ref_name(worktree) :
6063 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6064 if (error)
6065 goto done;
6066 head_ref_name = label;
6067 } else {
6068 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6069 if (error == NULL)
6070 head_ref_name = got_ref_get_name(ref);
6071 else if (error->code != GOT_ERR_NOT_REF)
6072 goto done;
6073 error = got_repo_match_object_id(&commit_id, NULL,
6074 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6075 if (error)
6076 goto done;
6079 error = got_object_open_as_commit(&commit, repo, commit_id);
6080 if (error)
6081 goto done;
6083 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6084 if (view == NULL) {
6085 error = got_error_from_errno("view_open");
6086 goto done;
6088 error = open_tree_view(view, commit_id, head_ref_name, repo);
6089 if (error)
6090 goto done;
6091 if (!got_path_is_root_dir(in_repo_path)) {
6092 error = tree_view_walk_path(&view->state.tree, commit,
6093 in_repo_path);
6094 if (error)
6095 goto done;
6098 if (worktree) {
6099 /* Release work tree lock. */
6100 got_worktree_close(worktree);
6101 worktree = NULL;
6103 error = view_loop(view);
6104 done:
6105 free(repo_path);
6106 free(cwd);
6107 free(commit_id);
6108 free(label);
6109 if (ref)
6110 got_ref_close(ref);
6111 if (repo) {
6112 const struct got_error *close_err = got_repo_close(repo);
6113 if (error == NULL)
6114 error = close_err;
6116 if (pack_fds) {
6117 const struct got_error *pack_err =
6118 got_repo_pack_fds_close(pack_fds);
6119 if (error == NULL)
6120 error = pack_err;
6122 tog_free_refs();
6123 return error;
6126 static const struct got_error *
6127 ref_view_load_refs(struct tog_ref_view_state *s)
6129 struct got_reflist_entry *sre;
6130 struct tog_reflist_entry *re;
6132 s->nrefs = 0;
6133 TAILQ_FOREACH(sre, &tog_refs, entry) {
6134 if (strncmp(got_ref_get_name(sre->ref),
6135 "refs/got/", 9) == 0 &&
6136 strncmp(got_ref_get_name(sre->ref),
6137 "refs/got/backup/", 16) != 0)
6138 continue;
6140 re = malloc(sizeof(*re));
6141 if (re == NULL)
6142 return got_error_from_errno("malloc");
6144 re->ref = got_ref_dup(sre->ref);
6145 if (re->ref == NULL)
6146 return got_error_from_errno("got_ref_dup");
6147 re->idx = s->nrefs++;
6148 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6151 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6152 return NULL;
6155 void
6156 ref_view_free_refs(struct tog_ref_view_state *s)
6158 struct tog_reflist_entry *re;
6160 while (!TAILQ_EMPTY(&s->refs)) {
6161 re = TAILQ_FIRST(&s->refs);
6162 TAILQ_REMOVE(&s->refs, re, entry);
6163 got_ref_close(re->ref);
6164 free(re);
6168 static const struct got_error *
6169 open_ref_view(struct tog_view *view, struct got_repository *repo)
6171 const struct got_error *err = NULL;
6172 struct tog_ref_view_state *s = &view->state.ref;
6174 s->selected_entry = 0;
6175 s->repo = repo;
6177 TAILQ_INIT(&s->refs);
6178 STAILQ_INIT(&s->colors);
6180 err = ref_view_load_refs(s);
6181 if (err)
6182 return err;
6184 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6185 err = add_color(&s->colors, "^refs/heads/",
6186 TOG_COLOR_REFS_HEADS,
6187 get_color_value("TOG_COLOR_REFS_HEADS"));
6188 if (err)
6189 goto done;
6191 err = add_color(&s->colors, "^refs/tags/",
6192 TOG_COLOR_REFS_TAGS,
6193 get_color_value("TOG_COLOR_REFS_TAGS"));
6194 if (err)
6195 goto done;
6197 err = add_color(&s->colors, "^refs/remotes/",
6198 TOG_COLOR_REFS_REMOTES,
6199 get_color_value("TOG_COLOR_REFS_REMOTES"));
6200 if (err)
6201 goto done;
6203 err = add_color(&s->colors, "^refs/got/backup/",
6204 TOG_COLOR_REFS_BACKUP,
6205 get_color_value("TOG_COLOR_REFS_BACKUP"));
6206 if (err)
6207 goto done;
6210 view->show = show_ref_view;
6211 view->input = input_ref_view;
6212 view->close = close_ref_view;
6213 view->search_start = search_start_ref_view;
6214 view->search_next = search_next_ref_view;
6215 done:
6216 if (err)
6217 free_colors(&s->colors);
6218 return err;
6221 static const struct got_error *
6222 close_ref_view(struct tog_view *view)
6224 struct tog_ref_view_state *s = &view->state.ref;
6226 ref_view_free_refs(s);
6227 free_colors(&s->colors);
6229 return NULL;
6232 static const struct got_error *
6233 resolve_reflist_entry(struct got_object_id **commit_id,
6234 struct tog_reflist_entry *re, struct got_repository *repo)
6236 const struct got_error *err = NULL;
6237 struct got_object_id *obj_id;
6238 struct got_tag_object *tag = NULL;
6239 int obj_type;
6241 *commit_id = NULL;
6243 err = got_ref_resolve(&obj_id, repo, re->ref);
6244 if (err)
6245 return err;
6247 err = got_object_get_type(&obj_type, repo, obj_id);
6248 if (err)
6249 goto done;
6251 switch (obj_type) {
6252 case GOT_OBJ_TYPE_COMMIT:
6253 *commit_id = obj_id;
6254 break;
6255 case GOT_OBJ_TYPE_TAG:
6256 err = got_object_open_as_tag(&tag, repo, obj_id);
6257 if (err)
6258 goto done;
6259 free(obj_id);
6260 err = got_object_get_type(&obj_type, repo,
6261 got_object_tag_get_object_id(tag));
6262 if (err)
6263 goto done;
6264 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6265 err = got_error(GOT_ERR_OBJ_TYPE);
6266 goto done;
6268 *commit_id = got_object_id_dup(
6269 got_object_tag_get_object_id(tag));
6270 if (*commit_id == NULL) {
6271 err = got_error_from_errno("got_object_id_dup");
6272 goto done;
6274 break;
6275 default:
6276 err = got_error(GOT_ERR_OBJ_TYPE);
6277 break;
6280 done:
6281 if (tag)
6282 got_object_tag_close(tag);
6283 if (err) {
6284 free(*commit_id);
6285 *commit_id = NULL;
6287 return err;
6290 static const struct got_error *
6291 log_ref_entry(struct tog_view **new_view, int begin_x,
6292 struct tog_reflist_entry *re, struct got_repository *repo)
6294 struct tog_view *log_view;
6295 const struct got_error *err = NULL;
6296 struct got_object_id *commit_id = NULL;
6298 *new_view = NULL;
6300 err = resolve_reflist_entry(&commit_id, re, repo);
6301 if (err) {
6302 if (err->code != GOT_ERR_OBJ_TYPE)
6303 return err;
6304 else
6305 return NULL;
6308 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6309 if (log_view == NULL) {
6310 err = got_error_from_errno("view_open");
6311 goto done;
6314 err = open_log_view(log_view, commit_id, repo,
6315 got_ref_get_name(re->ref), "", 0);
6316 done:
6317 if (err)
6318 view_close(log_view);
6319 else
6320 *new_view = log_view;
6321 free(commit_id);
6322 return err;
6325 static void
6326 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6328 struct tog_reflist_entry *re;
6329 int i = 0;
6331 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6332 return;
6334 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6335 while (i++ < maxscroll) {
6336 if (re == NULL)
6337 break;
6338 s->first_displayed_entry = re;
6339 re = TAILQ_PREV(re, tog_reflist_head, entry);
6343 static void
6344 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6346 struct tog_reflist_entry *next, *last;
6347 int n = 0;
6349 if (s->first_displayed_entry)
6350 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6351 else
6352 next = TAILQ_FIRST(&s->refs);
6354 last = s->last_displayed_entry;
6355 while (next && last && n++ < maxscroll) {
6356 last = TAILQ_NEXT(last, entry);
6357 if (last) {
6358 s->first_displayed_entry = next;
6359 next = TAILQ_NEXT(next, entry);
6364 static const struct got_error *
6365 search_start_ref_view(struct tog_view *view)
6367 struct tog_ref_view_state *s = &view->state.ref;
6369 s->matched_entry = NULL;
6370 return NULL;
6373 static int
6374 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6376 regmatch_t regmatch;
6378 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6379 0) == 0;
6382 static const struct got_error *
6383 search_next_ref_view(struct tog_view *view)
6385 struct tog_ref_view_state *s = &view->state.ref;
6386 struct tog_reflist_entry *re = NULL;
6388 if (!view->searching) {
6389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6390 return NULL;
6393 if (s->matched_entry) {
6394 if (view->searching == TOG_SEARCH_FORWARD) {
6395 if (s->selected_entry)
6396 re = TAILQ_NEXT(s->selected_entry, entry);
6397 else
6398 re = TAILQ_PREV(s->selected_entry,
6399 tog_reflist_head, entry);
6400 } else {
6401 if (s->selected_entry == NULL)
6402 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6403 else
6404 re = TAILQ_PREV(s->selected_entry,
6405 tog_reflist_head, entry);
6407 } else {
6408 if (s->selected_entry)
6409 re = s->selected_entry;
6410 else if (view->searching == TOG_SEARCH_FORWARD)
6411 re = TAILQ_FIRST(&s->refs);
6412 else
6413 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6416 while (1) {
6417 if (re == NULL) {
6418 if (s->matched_entry == NULL) {
6419 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6420 return NULL;
6422 if (view->searching == TOG_SEARCH_FORWARD)
6423 re = TAILQ_FIRST(&s->refs);
6424 else
6425 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6428 if (match_reflist_entry(re, &view->regex)) {
6429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6430 s->matched_entry = re;
6431 break;
6434 if (view->searching == TOG_SEARCH_FORWARD)
6435 re = TAILQ_NEXT(re, entry);
6436 else
6437 re = TAILQ_PREV(re, tog_reflist_head, entry);
6440 if (s->matched_entry) {
6441 s->first_displayed_entry = s->matched_entry;
6442 s->selected = 0;
6445 return NULL;
6448 static const struct got_error *
6449 show_ref_view(struct tog_view *view)
6451 const struct got_error *err = NULL;
6452 struct tog_ref_view_state *s = &view->state.ref;
6453 struct tog_reflist_entry *re;
6454 char *line = NULL;
6455 wchar_t *wline;
6456 struct tog_color *tc;
6457 int width, n;
6458 int limit = view->nlines;
6460 werase(view->window);
6462 s->ndisplayed = 0;
6464 if (limit == 0)
6465 return NULL;
6467 re = s->first_displayed_entry;
6469 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6470 s->nrefs) == -1)
6471 return got_error_from_errno("asprintf");
6473 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6474 if (err) {
6475 free(line);
6476 return err;
6478 if (view_needs_focus_indication(view))
6479 wstandout(view->window);
6480 waddwstr(view->window, wline);
6481 if (view_needs_focus_indication(view))
6482 wstandend(view->window);
6483 free(wline);
6484 wline = NULL;
6485 free(line);
6486 line = NULL;
6487 if (width < view->ncols - 1)
6488 waddch(view->window, '\n');
6489 if (--limit <= 0)
6490 return NULL;
6492 n = 0;
6493 while (re && limit > 0) {
6494 char *line = NULL;
6495 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6497 if (s->show_date) {
6498 struct got_commit_object *ci;
6499 struct got_tag_object *tag;
6500 struct got_object_id *id;
6501 struct tm tm;
6502 time_t t;
6504 err = got_ref_resolve(&id, s->repo, re->ref);
6505 if (err)
6506 return err;
6507 err = got_object_open_as_tag(&tag, s->repo, id);
6508 if (err) {
6509 if (err->code != GOT_ERR_OBJ_TYPE) {
6510 free(id);
6511 return err;
6513 err = got_object_open_as_commit(&ci, s->repo,
6514 id);
6515 if (err) {
6516 free(id);
6517 return err;
6519 t = got_object_commit_get_committer_time(ci);
6520 got_object_commit_close(ci);
6521 } else {
6522 t = got_object_tag_get_tagger_time(tag);
6523 got_object_tag_close(tag);
6525 free(id);
6526 if (gmtime_r(&t, &tm) == NULL)
6527 return got_error_from_errno("gmtime_r");
6528 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6529 return got_error(GOT_ERR_NO_SPACE);
6531 if (got_ref_is_symbolic(re->ref)) {
6532 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6533 ymd : "", got_ref_get_name(re->ref),
6534 got_ref_get_symref_target(re->ref)) == -1)
6535 return got_error_from_errno("asprintf");
6536 } else if (s->show_ids) {
6537 struct got_object_id *id;
6538 char *id_str;
6539 err = got_ref_resolve(&id, s->repo, re->ref);
6540 if (err)
6541 return err;
6542 err = got_object_id_str(&id_str, id);
6543 if (err) {
6544 free(id);
6545 return err;
6547 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6548 got_ref_get_name(re->ref), id_str) == -1) {
6549 err = got_error_from_errno("asprintf");
6550 free(id);
6551 free(id_str);
6552 return err;
6554 free(id);
6555 free(id_str);
6556 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6557 got_ref_get_name(re->ref)) == -1)
6558 return got_error_from_errno("asprintf");
6560 err = format_line(&wline, &width, line, view->ncols, 0, 0);
6561 if (err) {
6562 free(line);
6563 return err;
6565 if (n == s->selected) {
6566 if (view->focussed)
6567 wstandout(view->window);
6568 s->selected_entry = re;
6570 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6571 if (tc)
6572 wattr_on(view->window,
6573 COLOR_PAIR(tc->colorpair), NULL);
6574 waddwstr(view->window, wline);
6575 if (tc)
6576 wattr_off(view->window,
6577 COLOR_PAIR(tc->colorpair), NULL);
6578 if (width < view->ncols - 1)
6579 waddch(view->window, '\n');
6580 if (n == s->selected && view->focussed)
6581 wstandend(view->window);
6582 free(line);
6583 free(wline);
6584 wline = NULL;
6585 n++;
6586 s->ndisplayed++;
6587 s->last_displayed_entry = re;
6589 limit--;
6590 re = TAILQ_NEXT(re, entry);
6593 view_vborder(view);
6594 return err;
6597 static const struct got_error *
6598 browse_ref_tree(struct tog_view **new_view, int begin_x,
6599 struct tog_reflist_entry *re, struct got_repository *repo)
6601 const struct got_error *err = NULL;
6602 struct got_object_id *commit_id = NULL;
6603 struct tog_view *tree_view;
6605 *new_view = NULL;
6607 err = resolve_reflist_entry(&commit_id, re, repo);
6608 if (err) {
6609 if (err->code != GOT_ERR_OBJ_TYPE)
6610 return err;
6611 else
6612 return NULL;
6616 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6617 if (tree_view == NULL) {
6618 err = got_error_from_errno("view_open");
6619 goto done;
6622 err = open_tree_view(tree_view, commit_id,
6623 got_ref_get_name(re->ref), repo);
6624 if (err)
6625 goto done;
6627 *new_view = tree_view;
6628 done:
6629 free(commit_id);
6630 return err;
6632 static const struct got_error *
6633 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6635 const struct got_error *err = NULL;
6636 struct tog_ref_view_state *s = &view->state.ref;
6637 struct tog_view *log_view, *tree_view;
6638 struct tog_reflist_entry *re;
6639 int begin_x = 0, n, nscroll = view->nlines - 1;
6641 switch (ch) {
6642 case 'i':
6643 s->show_ids = !s->show_ids;
6644 break;
6645 case 'm':
6646 s->show_date = !s->show_date;
6647 break;
6648 case 'o':
6649 s->sort_by_date = !s->sort_by_date;
6650 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6651 got_ref_cmp_by_commit_timestamp_descending :
6652 tog_ref_cmp_by_name, s->repo);
6653 if (err)
6654 break;
6655 got_reflist_object_id_map_free(tog_refs_idmap);
6656 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6657 &tog_refs, s->repo);
6658 if (err)
6659 break;
6660 ref_view_free_refs(s);
6661 err = ref_view_load_refs(s);
6662 break;
6663 case KEY_ENTER:
6664 case '\r':
6665 if (!s->selected_entry)
6666 break;
6667 if (view_is_parent_view(view))
6668 begin_x = view_split_begin_x(view->begin_x);
6669 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6670 s->repo);
6671 view->focussed = 0;
6672 log_view->focussed = 1;
6673 if (view_is_parent_view(view)) {
6674 err = view_close_child(view);
6675 if (err)
6676 return err;
6677 view_set_child(view, log_view);
6678 view->focus_child = 1;
6679 } else
6680 *new_view = log_view;
6681 break;
6682 case 't':
6683 if (!s->selected_entry)
6684 break;
6685 if (view_is_parent_view(view))
6686 begin_x = view_split_begin_x(view->begin_x);
6687 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6688 s->repo);
6689 if (err || tree_view == NULL)
6690 break;
6691 view->focussed = 0;
6692 tree_view->focussed = 1;
6693 if (view_is_parent_view(view)) {
6694 err = view_close_child(view);
6695 if (err)
6696 return err;
6697 view_set_child(view, tree_view);
6698 view->focus_child = 1;
6699 } else
6700 *new_view = tree_view;
6701 break;
6702 case 'g':
6703 case KEY_HOME:
6704 s->selected = 0;
6705 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6706 break;
6707 case 'G':
6708 case KEY_END:
6709 s->selected = 0;
6710 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6711 for (n = 0; n < view->nlines - 1; n++) {
6712 if (re == NULL)
6713 break;
6714 s->first_displayed_entry = re;
6715 re = TAILQ_PREV(re, tog_reflist_head, entry);
6717 if (n > 0)
6718 s->selected = n - 1;
6719 break;
6720 case 'k':
6721 case KEY_UP:
6722 case CTRL('p'):
6723 if (s->selected > 0) {
6724 s->selected--;
6725 break;
6727 ref_scroll_up(s, 1);
6728 break;
6729 case CTRL('u'):
6730 case 'u':
6731 nscroll /= 2;
6732 /* FALL THROUGH */
6733 case KEY_PPAGE:
6734 case CTRL('b'):
6735 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6736 s->selected -= MIN(nscroll, s->selected);
6737 ref_scroll_up(s, MAX(0, nscroll));
6738 break;
6739 case 'j':
6740 case KEY_DOWN:
6741 case CTRL('n'):
6742 if (s->selected < s->ndisplayed - 1) {
6743 s->selected++;
6744 break;
6746 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6747 /* can't scroll any further */
6748 break;
6749 ref_scroll_down(s, 1);
6750 break;
6751 case CTRL('d'):
6752 case 'd':
6753 nscroll /= 2;
6754 /* FALL THROUGH */
6755 case KEY_NPAGE:
6756 case CTRL('f'):
6757 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6758 /* can't scroll any further; move cursor down */
6759 if (s->selected < s->ndisplayed - 1)
6760 s->selected += MIN(nscroll,
6761 s->ndisplayed - s->selected - 1);
6762 break;
6764 ref_scroll_down(s, nscroll);
6765 break;
6766 case CTRL('l'):
6767 tog_free_refs();
6768 err = tog_load_refs(s->repo, s->sort_by_date);
6769 if (err)
6770 break;
6771 ref_view_free_refs(s);
6772 err = ref_view_load_refs(s);
6773 break;
6774 case KEY_RESIZE:
6775 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6776 s->selected = view->nlines - 2;
6777 break;
6778 default:
6779 break;
6782 return err;
6785 __dead static void
6786 usage_ref(void)
6788 endwin();
6789 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6790 getprogname());
6791 exit(1);
6794 static const struct got_error *
6795 cmd_ref(int argc, char *argv[])
6797 const struct got_error *error;
6798 struct got_repository *repo = NULL;
6799 struct got_worktree *worktree = NULL;
6800 char *cwd = NULL, *repo_path = NULL;
6801 int ch;
6802 struct tog_view *view;
6803 int *pack_fds = NULL;
6805 while ((ch = getopt(argc, argv, "r:")) != -1) {
6806 switch (ch) {
6807 case 'r':
6808 repo_path = realpath(optarg, NULL);
6809 if (repo_path == NULL)
6810 return got_error_from_errno2("realpath",
6811 optarg);
6812 break;
6813 default:
6814 usage_ref();
6815 /* NOTREACHED */
6819 argc -= optind;
6820 argv += optind;
6822 if (argc > 1)
6823 usage_ref();
6825 error = got_repo_pack_fds_open(&pack_fds);
6826 if (error != NULL)
6827 goto done;
6829 if (repo_path == NULL) {
6830 cwd = getcwd(NULL, 0);
6831 if (cwd == NULL)
6832 return got_error_from_errno("getcwd");
6833 error = got_worktree_open(&worktree, cwd);
6834 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6835 goto done;
6836 if (worktree)
6837 repo_path =
6838 strdup(got_worktree_get_repo_path(worktree));
6839 else
6840 repo_path = strdup(cwd);
6841 if (repo_path == NULL) {
6842 error = got_error_from_errno("strdup");
6843 goto done;
6847 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6848 if (error != NULL)
6849 goto done;
6851 init_curses();
6853 error = apply_unveil(got_repo_get_path(repo), NULL);
6854 if (error)
6855 goto done;
6857 error = tog_load_refs(repo, 0);
6858 if (error)
6859 goto done;
6861 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6862 if (view == NULL) {
6863 error = got_error_from_errno("view_open");
6864 goto done;
6867 error = open_ref_view(view, repo);
6868 if (error)
6869 goto done;
6871 if (worktree) {
6872 /* Release work tree lock. */
6873 got_worktree_close(worktree);
6874 worktree = NULL;
6876 error = view_loop(view);
6877 done:
6878 free(repo_path);
6879 free(cwd);
6880 if (repo) {
6881 const struct got_error *close_err = got_repo_close(repo);
6882 if (close_err)
6883 error = close_err;
6885 if (pack_fds) {
6886 const struct got_error *pack_err =
6887 got_repo_pack_fds_close(pack_fds);
6888 if (error == NULL)
6889 error = pack_err;
6891 tog_free_refs();
6892 return error;
6895 static void
6896 list_commands(FILE *fp)
6898 size_t i;
6900 fprintf(fp, "commands:");
6901 for (i = 0; i < nitems(tog_commands); i++) {
6902 const struct tog_cmd *cmd = &tog_commands[i];
6903 fprintf(fp, " %s", cmd->name);
6905 fputc('\n', fp);
6908 __dead static void
6909 usage(int hflag, int status)
6911 FILE *fp = (status == 0) ? stdout : stderr;
6913 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6914 getprogname());
6915 if (hflag) {
6916 fprintf(fp, "lazy usage: %s path\n", getprogname());
6917 list_commands(fp);
6919 exit(status);
6922 static char **
6923 make_argv(int argc, ...)
6925 va_list ap;
6926 char **argv;
6927 int i;
6929 va_start(ap, argc);
6931 argv = calloc(argc, sizeof(char *));
6932 if (argv == NULL)
6933 err(1, "calloc");
6934 for (i = 0; i < argc; i++) {
6935 argv[i] = strdup(va_arg(ap, char *));
6936 if (argv[i] == NULL)
6937 err(1, "strdup");
6940 va_end(ap);
6941 return argv;
6945 * Try to convert 'tog path' into a 'tog log path' command.
6946 * The user could simply have mistyped the command rather than knowingly
6947 * provided a path. So check whether argv[0] can in fact be resolved
6948 * to a path in the HEAD commit and print a special error if not.
6949 * This hack is for mpi@ <3
6951 static const struct got_error *
6952 tog_log_with_path(int argc, char *argv[])
6954 const struct got_error *error = NULL, *close_err;
6955 const struct tog_cmd *cmd = NULL;
6956 struct got_repository *repo = NULL;
6957 struct got_worktree *worktree = NULL;
6958 struct got_object_id *commit_id = NULL, *id = NULL;
6959 struct got_commit_object *commit = NULL;
6960 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6961 char *commit_id_str = NULL, **cmd_argv = NULL;
6962 int *pack_fds = NULL;
6964 cwd = getcwd(NULL, 0);
6965 if (cwd == NULL)
6966 return got_error_from_errno("getcwd");
6968 error = got_repo_pack_fds_open(&pack_fds);
6969 if (error != NULL)
6970 goto done;
6972 error = got_worktree_open(&worktree, cwd);
6973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6974 goto done;
6976 if (worktree)
6977 repo_path = strdup(got_worktree_get_repo_path(worktree));
6978 else
6979 repo_path = strdup(cwd);
6980 if (repo_path == NULL) {
6981 error = got_error_from_errno("strdup");
6982 goto done;
6985 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6986 if (error != NULL)
6987 goto done;
6989 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6990 repo, worktree);
6991 if (error)
6992 goto done;
6994 error = tog_load_refs(repo, 0);
6995 if (error)
6996 goto done;
6997 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6998 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6999 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7000 if (error)
7001 goto done;
7003 if (worktree) {
7004 got_worktree_close(worktree);
7005 worktree = NULL;
7008 error = got_object_open_as_commit(&commit, repo, commit_id);
7009 if (error)
7010 goto done;
7012 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7013 if (error) {
7014 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7015 goto done;
7016 fprintf(stderr, "%s: '%s' is no known command or path\n",
7017 getprogname(), argv[0]);
7018 usage(1, 1);
7019 /* not reached */
7022 close_err = got_repo_close(repo);
7023 if (error == NULL)
7024 error = close_err;
7025 repo = NULL;
7027 error = got_object_id_str(&commit_id_str, commit_id);
7028 if (error)
7029 goto done;
7031 cmd = &tog_commands[0]; /* log */
7032 argc = 4;
7033 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7034 error = cmd->cmd_main(argc, cmd_argv);
7035 done:
7036 if (repo) {
7037 close_err = got_repo_close(repo);
7038 if (error == NULL)
7039 error = close_err;
7041 if (commit)
7042 got_object_commit_close(commit);
7043 if (worktree)
7044 got_worktree_close(worktree);
7045 if (pack_fds) {
7046 const struct got_error *pack_err =
7047 got_repo_pack_fds_close(pack_fds);
7048 if (error == NULL)
7049 error = pack_err;
7051 free(id);
7052 free(commit_id_str);
7053 free(commit_id);
7054 free(cwd);
7055 free(repo_path);
7056 free(in_repo_path);
7057 if (cmd_argv) {
7058 int i;
7059 for (i = 0; i < argc; i++)
7060 free(cmd_argv[i]);
7061 free(cmd_argv);
7063 tog_free_refs();
7064 return error;
7067 int
7068 main(int argc, char *argv[])
7070 const struct got_error *error = NULL;
7071 const struct tog_cmd *cmd = NULL;
7072 int ch, hflag = 0, Vflag = 0;
7073 char **cmd_argv = NULL;
7074 static const struct option longopts[] = {
7075 { "version", no_argument, NULL, 'V' },
7076 { NULL, 0, NULL, 0}
7079 setlocale(LC_CTYPE, "");
7081 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7082 switch (ch) {
7083 case 'h':
7084 hflag = 1;
7085 break;
7086 case 'V':
7087 Vflag = 1;
7088 break;
7089 default:
7090 usage(hflag, 1);
7091 /* NOTREACHED */
7095 argc -= optind;
7096 argv += optind;
7097 optind = 1;
7098 optreset = 1;
7100 if (Vflag) {
7101 got_version_print_str();
7102 return 0;
7105 #ifndef PROFILE
7106 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7107 NULL) == -1)
7108 err(1, "pledge");
7109 #endif
7111 if (argc == 0) {
7112 if (hflag)
7113 usage(hflag, 0);
7114 /* Build an argument vector which runs a default command. */
7115 cmd = &tog_commands[0];
7116 argc = 1;
7117 cmd_argv = make_argv(argc, cmd->name);
7118 } else {
7119 size_t i;
7121 /* Did the user specify a command? */
7122 for (i = 0; i < nitems(tog_commands); i++) {
7123 if (strncmp(tog_commands[i].name, argv[0],
7124 strlen(argv[0])) == 0) {
7125 cmd = &tog_commands[i];
7126 break;
7131 if (cmd == NULL) {
7132 if (argc != 1)
7133 usage(0, 1);
7134 /* No command specified; try log with a path */
7135 error = tog_log_with_path(argc, argv);
7136 } else {
7137 if (hflag)
7138 cmd->cmd_usage();
7139 else
7140 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7143 endwin();
7144 putchar('\n');
7145 if (cmd_argv) {
7146 int i;
7147 for (i = 0; i < argc; i++)
7148 free(cmd_argv[i]);
7149 free(cmd_argv);
7152 if (error && error->code != GOT_ERR_CANCELLED)
7153 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7154 return 0;