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 TOG_VIEW_HELP
107 };
109 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
110 enum tog_keymap_type {
111 TOG_KEYMAP_KEYS = -2,
112 TOG_KEYMAP_GLOBAL,
113 TOG_KEYMAP_DIFF,
114 TOG_KEYMAP_LOG,
115 TOG_KEYMAP_BLAME,
116 TOG_KEYMAP_TREE,
117 TOG_KEYMAP_REF,
118 TOG_KEYMAP_HELP
119 };
121 enum tog_view_mode {
122 TOG_VIEW_SPLIT_NONE,
123 TOG_VIEW_SPLIT_VERT,
124 TOG_VIEW_SPLIT_HRZN
125 };
127 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
129 #define TOG_EOF_STRING "(END)"
131 struct commit_queue_entry {
132 TAILQ_ENTRY(commit_queue_entry) entry;
133 struct got_object_id *id;
134 struct got_commit_object *commit;
135 int idx;
136 };
137 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
138 struct commit_queue {
139 int ncommits;
140 struct commit_queue_head head;
141 };
143 struct tog_color {
144 STAILQ_ENTRY(tog_color) entry;
145 regex_t regex;
146 short colorpair;
147 };
148 STAILQ_HEAD(tog_colors, tog_color);
150 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
151 static struct got_reflist_object_id_map *tog_refs_idmap;
152 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
154 static const struct got_error *
155 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
156 struct got_reference* re2)
158 const char *name1 = got_ref_get_name(re1);
159 const char *name2 = got_ref_get_name(re2);
160 int isbackup1, isbackup2;
162 /* Sort backup refs towards the bottom of the list. */
163 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
164 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
165 if (!isbackup1 && isbackup2) {
166 *cmp = -1;
167 return NULL;
168 } else if (isbackup1 && !isbackup2) {
169 *cmp = 1;
170 return NULL;
173 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
174 return NULL;
177 static const struct got_error *
178 tog_load_refs(struct got_repository *repo, int sort_by_date)
180 const struct got_error *err;
182 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
183 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
184 repo);
185 if (err)
186 return err;
188 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
189 repo);
192 static void
193 tog_free_refs(void)
195 if (tog_refs_idmap) {
196 got_reflist_object_id_map_free(tog_refs_idmap);
197 tog_refs_idmap = NULL;
199 got_ref_list_free(&tog_refs);
202 static const struct got_error *
203 add_color(struct tog_colors *colors, const char *pattern,
204 int idx, short color)
206 const struct got_error *err = NULL;
207 struct tog_color *tc;
208 int regerr = 0;
210 if (idx < 1 || idx > COLOR_PAIRS - 1)
211 return NULL;
213 init_pair(idx, color, -1);
215 tc = calloc(1, sizeof(*tc));
216 if (tc == NULL)
217 return got_error_from_errno("calloc");
218 regerr = regcomp(&tc->regex, pattern,
219 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
220 if (regerr) {
221 static char regerr_msg[512];
222 static char err_msg[512];
223 regerror(regerr, &tc->regex, regerr_msg,
224 sizeof(regerr_msg));
225 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
226 regerr_msg);
227 err = got_error_msg(GOT_ERR_REGEX, err_msg);
228 free(tc);
229 return err;
231 tc->colorpair = idx;
232 STAILQ_INSERT_HEAD(colors, tc, entry);
233 return NULL;
236 static void
237 free_colors(struct tog_colors *colors)
239 struct tog_color *tc;
241 while (!STAILQ_EMPTY(colors)) {
242 tc = STAILQ_FIRST(colors);
243 STAILQ_REMOVE_HEAD(colors, entry);
244 regfree(&tc->regex);
245 free(tc);
249 static struct tog_color *
250 get_color(struct tog_colors *colors, int colorpair)
252 struct tog_color *tc = NULL;
254 STAILQ_FOREACH(tc, colors, entry) {
255 if (tc->colorpair == colorpair)
256 return tc;
259 return NULL;
262 static int
263 default_color_value(const char *envvar)
265 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
268 return COLOR_CYAN;
269 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
272 return COLOR_GREEN;
273 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
274 return COLOR_MAGENTA;
275 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
276 return COLOR_MAGENTA;
277 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
278 return COLOR_CYAN;
279 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
280 return COLOR_GREEN;
281 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
282 return COLOR_GREEN;
283 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
284 return COLOR_CYAN;
285 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
286 return COLOR_YELLOW;
287 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
288 return COLOR_GREEN;
289 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
290 return COLOR_MAGENTA;
291 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
292 return COLOR_YELLOW;
293 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
294 return COLOR_CYAN;
296 return -1;
299 static int
300 get_color_value(const char *envvar)
302 const char *val = getenv(envvar);
304 if (val == NULL)
305 return default_color_value(envvar);
307 if (strcasecmp(val, "black") == 0)
308 return COLOR_BLACK;
309 if (strcasecmp(val, "red") == 0)
310 return COLOR_RED;
311 if (strcasecmp(val, "green") == 0)
312 return COLOR_GREEN;
313 if (strcasecmp(val, "yellow") == 0)
314 return COLOR_YELLOW;
315 if (strcasecmp(val, "blue") == 0)
316 return COLOR_BLUE;
317 if (strcasecmp(val, "magenta") == 0)
318 return COLOR_MAGENTA;
319 if (strcasecmp(val, "cyan") == 0)
320 return COLOR_CYAN;
321 if (strcasecmp(val, "white") == 0)
322 return COLOR_WHITE;
323 if (strcasecmp(val, "default") == 0)
324 return -1;
326 return default_color_value(envvar);
329 struct tog_diff_view_state {
330 struct got_object_id *id1, *id2;
331 const char *label1, *label2;
332 FILE *f, *f1, *f2;
333 int fd1, fd2;
334 int lineno;
335 int first_displayed_line;
336 int last_displayed_line;
337 int eof;
338 int diff_context;
339 int ignore_whitespace;
340 int force_text_diff;
341 struct got_repository *repo;
342 struct got_diff_line *lines;
343 size_t nlines;
344 int matched_line;
345 int selected_line;
347 /* passed from log or blame view; may be NULL */
348 struct tog_view *parent_view;
349 };
351 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
352 static volatile sig_atomic_t tog_thread_error;
354 struct tog_log_thread_args {
355 pthread_cond_t need_commits;
356 pthread_cond_t commit_loaded;
357 int commits_needed;
358 int load_all;
359 struct got_commit_graph *graph;
360 struct commit_queue *real_commits;
361 const char *in_repo_path;
362 struct got_object_id *start_id;
363 struct got_repository *repo;
364 int *pack_fds;
365 int log_complete;
366 sig_atomic_t *quit;
367 struct commit_queue_entry **first_displayed_entry;
368 struct commit_queue_entry **selected_entry;
369 int *searching;
370 int *search_next_done;
371 regex_t *regex;
372 int *limiting;
373 int limit_match;
374 regex_t *limit_regex;
375 struct commit_queue *limit_commits;
376 };
378 struct tog_log_view_state {
379 struct commit_queue *commits;
380 struct commit_queue_entry *first_displayed_entry;
381 struct commit_queue_entry *last_displayed_entry;
382 struct commit_queue_entry *selected_entry;
383 struct commit_queue real_commits;
384 int selected;
385 char *in_repo_path;
386 char *head_ref_name;
387 int log_branches;
388 struct got_repository *repo;
389 struct got_object_id *start_id;
390 sig_atomic_t quit;
391 pthread_t thread;
392 struct tog_log_thread_args thread_args;
393 struct commit_queue_entry *matched_entry;
394 struct commit_queue_entry *search_entry;
395 struct tog_colors colors;
396 int use_committer;
397 int limit_view;
398 regex_t limit_regex;
399 struct commit_queue limit_commits;
400 };
402 #define TOG_COLOR_DIFF_MINUS 1
403 #define TOG_COLOR_DIFF_PLUS 2
404 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
405 #define TOG_COLOR_DIFF_META 4
406 #define TOG_COLOR_TREE_SUBMODULE 5
407 #define TOG_COLOR_TREE_SYMLINK 6
408 #define TOG_COLOR_TREE_DIRECTORY 7
409 #define TOG_COLOR_TREE_EXECUTABLE 8
410 #define TOG_COLOR_COMMIT 9
411 #define TOG_COLOR_AUTHOR 10
412 #define TOG_COLOR_DATE 11
413 #define TOG_COLOR_REFS_HEADS 12
414 #define TOG_COLOR_REFS_TAGS 13
415 #define TOG_COLOR_REFS_REMOTES 14
416 #define TOG_COLOR_REFS_BACKUP 15
418 struct tog_blame_cb_args {
419 struct tog_blame_line *lines; /* one per line */
420 int nlines;
422 struct tog_view *view;
423 struct got_object_id *commit_id;
424 int *quit;
425 };
427 struct tog_blame_thread_args {
428 const char *path;
429 struct got_repository *repo;
430 struct tog_blame_cb_args *cb_args;
431 int *complete;
432 got_cancel_cb cancel_cb;
433 void *cancel_arg;
434 };
436 struct tog_blame {
437 FILE *f;
438 off_t filesize;
439 struct tog_blame_line *lines;
440 int nlines;
441 off_t *line_offsets;
442 pthread_t thread;
443 struct tog_blame_thread_args thread_args;
444 struct tog_blame_cb_args cb_args;
445 const char *path;
446 int *pack_fds;
447 };
449 struct tog_blame_view_state {
450 int first_displayed_line;
451 int last_displayed_line;
452 int selected_line;
453 int last_diffed_line;
454 int blame_complete;
455 int eof;
456 int done;
457 struct got_object_id_queue blamed_commits;
458 struct got_object_qid *blamed_commit;
459 char *path;
460 struct got_repository *repo;
461 struct got_object_id *commit_id;
462 struct got_object_id *id_to_log;
463 struct tog_blame blame;
464 int matched_line;
465 struct tog_colors colors;
466 };
468 struct tog_parent_tree {
469 TAILQ_ENTRY(tog_parent_tree) entry;
470 struct got_tree_object *tree;
471 struct got_tree_entry *first_displayed_entry;
472 struct got_tree_entry *selected_entry;
473 int selected;
474 };
476 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
478 struct tog_tree_view_state {
479 char *tree_label;
480 struct got_object_id *commit_id;/* commit which this tree belongs to */
481 struct got_tree_object *root; /* the commit's root tree entry */
482 struct got_tree_object *tree; /* currently displayed (sub-)tree */
483 struct got_tree_entry *first_displayed_entry;
484 struct got_tree_entry *last_displayed_entry;
485 struct got_tree_entry *selected_entry;
486 int ndisplayed, selected, show_ids;
487 struct tog_parent_trees parents; /* parent trees of current sub-tree */
488 char *head_ref_name;
489 struct got_repository *repo;
490 struct got_tree_entry *matched_entry;
491 struct tog_colors colors;
492 };
494 struct tog_reflist_entry {
495 TAILQ_ENTRY(tog_reflist_entry) entry;
496 struct got_reference *ref;
497 int idx;
498 };
500 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
502 struct tog_ref_view_state {
503 struct tog_reflist_head refs;
504 struct tog_reflist_entry *first_displayed_entry;
505 struct tog_reflist_entry *last_displayed_entry;
506 struct tog_reflist_entry *selected_entry;
507 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
508 struct got_repository *repo;
509 struct tog_reflist_entry *matched_entry;
510 struct tog_colors colors;
511 };
513 struct tog_help_view_state {
514 FILE *f;
515 off_t *line_offsets;
516 size_t nlines;
517 int lineno;
518 int first_displayed_line;
519 int last_displayed_line;
520 int eof;
521 int matched_line;
522 int selected_line;
523 int all;
524 enum tog_keymap_type type;
525 };
527 #define GENERATE_HELP \
528 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
529 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
530 KEY_("k C-p Up", "Move cursor or page up one line"), \
531 KEY_("j C-n Down", "Move cursor or page down one line"), \
532 KEY_("C-b b PgUp", "Scroll the view up one page"), \
533 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
534 KEY_("C-u u", "Scroll the view up one half page"), \
535 KEY_("C-d d", "Scroll the view down one half page"), \
536 KEY_("g Home", "Go to line N (default: first line)"), \
537 KEY_("G End", "Go to line N (default: last line)"), \
538 KEY_("l Right", "Scroll the view right"), \
539 KEY_("h Left", "Scroll the view left"), \
540 KEY_("$", "Scroll view to the rightmost position"), \
541 KEY_("0", "Scroll view to the leftmost position"), \
542 KEY_("-", "Decrease size of the focussed split"), \
543 KEY_("+", "Increase size of the focussed split"), \
544 KEY_("Tab", "Switch focus between views"), \
545 KEY_("F", "Toggle fullscreen mode"), \
546 KEY_("/", "Open prompt to enter search term"), \
547 KEY_("n", "Find next line/token matching the current search term"), \
548 KEY_("N", "Find previous line/token matching the current search term"),\
549 KEY_("q", "Quit the focussed view; Quit help screen"), \
550 KEY_("Q", "Quit tog"), \
552 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
553 KEY_("< ,", "Move cursor up one commit"), \
554 KEY_("> .", "Move cursor down one commit"), \
555 KEY_("Enter", "Open diff view of the selected commit"), \
556 KEY_("B", "Reload the log view and toggle display of merged commits"), \
557 KEY_("R", "Open ref view of all repository references"), \
558 KEY_("T", "Display tree view of the repository from the selected" \
559 " commit"), \
560 KEY_("@", "Toggle between displaying author and committer name"), \
561 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
562 KEY_("C-g Backspace", "Cancel current search or log operation"), \
563 KEY_("C-l", "Reload the log view with new commits in the repository"), \
565 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
566 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
567 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
568 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
569 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
570 " data"), \
571 KEY_("(", "Go to the previous file in the diff"), \
572 KEY_(")", "Go to the next file in the diff"), \
573 KEY_("{", "Go to the previous hunk in the diff"), \
574 KEY_("}", "Go to the next hunk in the diff"), \
575 KEY_("[", "Decrease the number of context lines"), \
576 KEY_("]", "Increase the number of context lines"), \
577 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
579 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
580 KEY_("Enter", "Display diff view of the selected line's commit"), \
581 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
582 KEY_("L", "Open log view for the currently selected annotated line"), \
583 KEY_("C", "Reload view with the previously blamed commit"), \
584 KEY_("c", "Reload view with the version of the file found in the" \
585 " selected line's commit"), \
586 KEY_("p", "Reload view with the version of the file found in the" \
587 " selected line's parent commit"), \
589 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
590 KEY_("Enter", "Enter selected directory or open blame view of the" \
591 " selected file"), \
592 KEY_("L", "Open log view for the selected entry"), \
593 KEY_("R", "Open ref view of all repository references"), \
594 KEY_("i", "Show object IDs for all tree entries"), \
595 KEY_("Backspace", "Return to the parent directory"), \
597 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
598 KEY_("Enter", "Display log view of the selected reference"), \
599 KEY_("T", "Display tree view of the selected reference"), \
600 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
601 KEY_("m", "Toggle display of last modified date for each reference"), \
602 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
603 KEY_("C-l", "Reload view with all repository references")
605 struct tog_key_map {
606 const char *keys;
607 const char *info;
608 enum tog_keymap_type type;
609 };
611 /*
612 * We implement two types of views: parent views and child views.
614 * The 'Tab' key switches focus between a parent view and its child view.
615 * Child views are shown side-by-side to their parent view, provided
616 * there is enough screen estate.
618 * When a new view is opened from within a parent view, this new view
619 * becomes a child view of the parent view, replacing any existing child.
621 * When a new view is opened from within a child view, this new view
622 * becomes a parent view which will obscure the views below until the
623 * user quits the new parent view by typing 'q'.
625 * This list of views contains parent views only.
626 * Child views are only pointed to by their parent view.
627 */
628 TAILQ_HEAD(tog_view_list_head, tog_view);
630 struct tog_view {
631 TAILQ_ENTRY(tog_view) entry;
632 WINDOW *window;
633 PANEL *panel;
634 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
635 int resized_y, resized_x; /* begin_y/x based on user resizing */
636 int maxx, x; /* max column and current start column */
637 int lines, cols; /* copies of LINES and COLS */
638 int nscrolled, offset; /* lines scrolled and hsplit line offset */
639 int gline, hiline; /* navigate to and highlight this nG line */
640 int ch, count; /* current keymap and count prefix */
641 int resized; /* set when in a resize event */
642 int focussed; /* Only set on one parent or child view at a time. */
643 int dying;
644 struct tog_view *parent;
645 struct tog_view *child;
647 /*
648 * This flag is initially set on parent views when a new child view
649 * is created. It gets toggled when the 'Tab' key switches focus
650 * between parent and child.
651 * The flag indicates whether focus should be passed on to our child
652 * view if this parent view gets picked for focus after another parent
653 * view was closed. This prevents child views from losing focus in such
654 * situations.
655 */
656 int focus_child;
658 enum tog_view_mode mode;
659 /* type-specific state */
660 enum tog_view_type type;
661 union {
662 struct tog_diff_view_state diff;
663 struct tog_log_view_state log;
664 struct tog_blame_view_state blame;
665 struct tog_tree_view_state tree;
666 struct tog_ref_view_state ref;
667 struct tog_help_view_state help;
668 } state;
670 const struct got_error *(*show)(struct tog_view *);
671 const struct got_error *(*input)(struct tog_view **,
672 struct tog_view *, int);
673 const struct got_error *(*reset)(struct tog_view *);
674 const struct got_error *(*resize)(struct tog_view *, int);
675 const struct got_error *(*close)(struct tog_view *);
677 const struct got_error *(*search_start)(struct tog_view *);
678 const struct got_error *(*search_next)(struct tog_view *);
679 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
680 int **, int **, int **, int **);
681 int search_started;
682 int searching;
683 #define TOG_SEARCH_FORWARD 1
684 #define TOG_SEARCH_BACKWARD 2
685 int search_next_done;
686 #define TOG_SEARCH_HAVE_MORE 1
687 #define TOG_SEARCH_NO_MORE 2
688 #define TOG_SEARCH_HAVE_NONE 3
689 regex_t regex;
690 regmatch_t regmatch;
691 };
693 static const struct got_error *open_diff_view(struct tog_view *,
694 struct got_object_id *, struct got_object_id *,
695 const char *, const char *, int, int, int, struct tog_view *,
696 struct got_repository *);
697 static const struct got_error *show_diff_view(struct tog_view *);
698 static const struct got_error *input_diff_view(struct tog_view **,
699 struct tog_view *, int);
700 static const struct got_error *reset_diff_view(struct tog_view *);
701 static const struct got_error* close_diff_view(struct tog_view *);
702 static const struct got_error *search_start_diff_view(struct tog_view *);
703 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
704 size_t *, int **, int **, int **, int **);
705 static const struct got_error *search_next_view_match(struct tog_view *);
707 static const struct got_error *open_log_view(struct tog_view *,
708 struct got_object_id *, struct got_repository *,
709 const char *, const char *, int);
710 static const struct got_error * show_log_view(struct tog_view *);
711 static const struct got_error *input_log_view(struct tog_view **,
712 struct tog_view *, int);
713 static const struct got_error *resize_log_view(struct tog_view *, int);
714 static const struct got_error *close_log_view(struct tog_view *);
715 static const struct got_error *search_start_log_view(struct tog_view *);
716 static const struct got_error *search_next_log_view(struct tog_view *);
718 static const struct got_error *open_blame_view(struct tog_view *, char *,
719 struct got_object_id *, struct got_repository *);
720 static const struct got_error *show_blame_view(struct tog_view *);
721 static const struct got_error *input_blame_view(struct tog_view **,
722 struct tog_view *, int);
723 static const struct got_error *reset_blame_view(struct tog_view *);
724 static const struct got_error *close_blame_view(struct tog_view *);
725 static const struct got_error *search_start_blame_view(struct tog_view *);
726 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
727 size_t *, int **, int **, int **, int **);
729 static const struct got_error *open_tree_view(struct tog_view *,
730 struct got_object_id *, const char *, struct got_repository *);
731 static const struct got_error *show_tree_view(struct tog_view *);
732 static const struct got_error *input_tree_view(struct tog_view **,
733 struct tog_view *, int);
734 static const struct got_error *close_tree_view(struct tog_view *);
735 static const struct got_error *search_start_tree_view(struct tog_view *);
736 static const struct got_error *search_next_tree_view(struct tog_view *);
738 static const struct got_error *open_ref_view(struct tog_view *,
739 struct got_repository *);
740 static const struct got_error *show_ref_view(struct tog_view *);
741 static const struct got_error *input_ref_view(struct tog_view **,
742 struct tog_view *, int);
743 static const struct got_error *close_ref_view(struct tog_view *);
744 static const struct got_error *search_start_ref_view(struct tog_view *);
745 static const struct got_error *search_next_ref_view(struct tog_view *);
747 static const struct got_error *open_help_view(struct tog_view *,
748 struct tog_view *);
749 static const struct got_error *show_help_view(struct tog_view *);
750 static const struct got_error *input_help_view(struct tog_view **,
751 struct tog_view *, int);
752 static const struct got_error *reset_help_view(struct tog_view *);
753 static const struct got_error* close_help_view(struct tog_view *);
754 static const struct got_error *search_start_help_view(struct tog_view *);
755 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
756 size_t *, int **, int **, int **, int **);
758 static volatile sig_atomic_t tog_sigwinch_received;
759 static volatile sig_atomic_t tog_sigpipe_received;
760 static volatile sig_atomic_t tog_sigcont_received;
761 static volatile sig_atomic_t tog_sigint_received;
762 static volatile sig_atomic_t tog_sigterm_received;
764 static void
765 tog_sigwinch(int signo)
767 tog_sigwinch_received = 1;
770 static void
771 tog_sigpipe(int signo)
773 tog_sigpipe_received = 1;
776 static void
777 tog_sigcont(int signo)
779 tog_sigcont_received = 1;
782 static void
783 tog_sigint(int signo)
785 tog_sigint_received = 1;
788 static void
789 tog_sigterm(int signo)
791 tog_sigterm_received = 1;
794 static int
795 tog_fatal_signal_received(void)
797 return (tog_sigpipe_received ||
798 tog_sigint_received || tog_sigterm_received);
801 static const struct got_error *
802 view_close(struct tog_view *view)
804 const struct got_error *err = NULL, *child_err = NULL;
806 if (view->child) {
807 child_err = view_close(view->child);
808 view->child = NULL;
810 if (view->close)
811 err = view->close(view);
812 if (view->panel)
813 del_panel(view->panel);
814 if (view->window)
815 delwin(view->window);
816 free(view);
817 return err ? err : child_err;
820 static struct tog_view *
821 view_open(int nlines, int ncols, int begin_y, int begin_x,
822 enum tog_view_type type)
824 struct tog_view *view = calloc(1, sizeof(*view));
826 if (view == NULL)
827 return NULL;
829 view->type = type;
830 view->lines = LINES;
831 view->cols = COLS;
832 view->nlines = nlines ? nlines : LINES - begin_y;
833 view->ncols = ncols ? ncols : COLS - begin_x;
834 view->begin_y = begin_y;
835 view->begin_x = begin_x;
836 view->window = newwin(nlines, ncols, begin_y, begin_x);
837 if (view->window == NULL) {
838 view_close(view);
839 return NULL;
841 view->panel = new_panel(view->window);
842 if (view->panel == NULL ||
843 set_panel_userptr(view->panel, view) != OK) {
844 view_close(view);
845 return NULL;
848 keypad(view->window, TRUE);
849 return view;
852 static int
853 view_split_begin_x(int begin_x)
855 if (begin_x > 0 || COLS < 120)
856 return 0;
857 return (COLS - MAX(COLS / 2, 80));
860 /* XXX Stub till we decide what to do. */
861 static int
862 view_split_begin_y(int lines)
864 return lines * HSPLIT_SCALE;
867 static const struct got_error *view_resize(struct tog_view *);
869 static const struct got_error *
870 view_splitscreen(struct tog_view *view)
872 const struct got_error *err = NULL;
874 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
875 if (view->resized_y && view->resized_y < view->lines)
876 view->begin_y = view->resized_y;
877 else
878 view->begin_y = view_split_begin_y(view->nlines);
879 view->begin_x = 0;
880 } else if (!view->resized) {
881 if (view->resized_x && view->resized_x < view->cols - 1 &&
882 view->cols > 119)
883 view->begin_x = view->resized_x;
884 else
885 view->begin_x = view_split_begin_x(0);
886 view->begin_y = 0;
888 view->nlines = LINES - view->begin_y;
889 view->ncols = COLS - view->begin_x;
890 view->lines = LINES;
891 view->cols = COLS;
892 err = view_resize(view);
893 if (err)
894 return err;
896 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
897 view->parent->nlines = view->begin_y;
899 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
900 return got_error_from_errno("mvwin");
902 return NULL;
905 static const struct got_error *
906 view_fullscreen(struct tog_view *view)
908 const struct got_error *err = NULL;
910 view->begin_x = 0;
911 view->begin_y = view->resized ? view->begin_y : 0;
912 view->nlines = view->resized ? view->nlines : LINES;
913 view->ncols = COLS;
914 view->lines = LINES;
915 view->cols = COLS;
916 err = view_resize(view);
917 if (err)
918 return err;
920 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
921 return got_error_from_errno("mvwin");
923 return NULL;
926 static int
927 view_is_parent_view(struct tog_view *view)
929 return view->parent == NULL;
932 static int
933 view_is_splitscreen(struct tog_view *view)
935 return view->begin_x > 0 || view->begin_y > 0;
938 static int
939 view_is_fullscreen(struct tog_view *view)
941 return view->nlines == LINES && view->ncols == COLS;
944 static int
945 view_is_hsplit_top(struct tog_view *view)
947 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
948 view_is_splitscreen(view->child);
951 static void
952 view_border(struct tog_view *view)
954 PANEL *panel;
955 const struct tog_view *view_above;
957 if (view->parent)
958 return view_border(view->parent);
960 panel = panel_above(view->panel);
961 if (panel == NULL)
962 return;
964 view_above = panel_userptr(panel);
965 if (view->mode == TOG_VIEW_SPLIT_HRZN)
966 mvwhline(view->window, view_above->begin_y - 1,
967 view->begin_x, got_locale_is_utf8() ?
968 ACS_HLINE : '-', view->ncols);
969 else
970 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
971 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
974 static const struct got_error *view_init_hsplit(struct tog_view *, int);
975 static const struct got_error *request_log_commits(struct tog_view *);
976 static const struct got_error *offset_selection_down(struct tog_view *);
977 static void offset_selection_up(struct tog_view *);
978 static void view_get_split(struct tog_view *, int *, int *);
980 static const struct got_error *
981 view_resize(struct tog_view *view)
983 const struct got_error *err = NULL;
984 int dif, nlines, ncols;
986 dif = LINES - view->lines; /* line difference */
988 if (view->lines > LINES)
989 nlines = view->nlines - (view->lines - LINES);
990 else
991 nlines = view->nlines + (LINES - view->lines);
992 if (view->cols > COLS)
993 ncols = view->ncols - (view->cols - COLS);
994 else
995 ncols = view->ncols + (COLS - view->cols);
997 if (view->child) {
998 int hs = view->child->begin_y;
1000 if (!view_is_fullscreen(view))
1001 view->child->begin_x = view_split_begin_x(view->begin_x);
1002 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1003 view->child->begin_x == 0) {
1004 ncols = COLS;
1006 view_fullscreen(view->child);
1007 if (view->child->focussed)
1008 show_panel(view->child->panel);
1009 else
1010 show_panel(view->panel);
1011 } else {
1012 ncols = view->child->begin_x;
1014 view_splitscreen(view->child);
1015 show_panel(view->child->panel);
1018 * XXX This is ugly and needs to be moved into the above
1019 * logic but "works" for now and my attempts at moving it
1020 * break either 'tab' or 'F' key maps in horizontal splits.
1022 if (hs) {
1023 err = view_splitscreen(view->child);
1024 if (err)
1025 return err;
1026 if (dif < 0) { /* top split decreased */
1027 err = offset_selection_down(view);
1028 if (err)
1029 return err;
1031 view_border(view);
1032 update_panels();
1033 doupdate();
1034 show_panel(view->child->panel);
1035 nlines = view->nlines;
1037 } else if (view->parent == NULL)
1038 ncols = COLS;
1040 if (view->resize && dif > 0) {
1041 err = view->resize(view, dif);
1042 if (err)
1043 return err;
1046 if (wresize(view->window, nlines, ncols) == ERR)
1047 return got_error_from_errno("wresize");
1048 if (replace_panel(view->panel, view->window) == ERR)
1049 return got_error_from_errno("replace_panel");
1050 wclear(view->window);
1052 view->nlines = nlines;
1053 view->ncols = ncols;
1054 view->lines = LINES;
1055 view->cols = COLS;
1057 return NULL;
1060 static const struct got_error *
1061 resize_log_view(struct tog_view *view, int increase)
1063 struct tog_log_view_state *s = &view->state.log;
1064 const struct got_error *err = NULL;
1065 int n = 0;
1067 if (s->selected_entry)
1068 n = s->selected_entry->idx + view->lines - s->selected;
1071 * Request commits to account for the increased
1072 * height so we have enough to populate the view.
1074 if (s->commits->ncommits < n) {
1075 view->nscrolled = n - s->commits->ncommits + increase + 1;
1076 err = request_log_commits(view);
1079 return err;
1082 static void
1083 view_adjust_offset(struct tog_view *view, int n)
1085 if (n == 0)
1086 return;
1088 if (view->parent && view->parent->offset) {
1089 if (view->parent->offset + n >= 0)
1090 view->parent->offset += n;
1091 else
1092 view->parent->offset = 0;
1093 } else if (view->offset) {
1094 if (view->offset - n >= 0)
1095 view->offset -= n;
1096 else
1097 view->offset = 0;
1101 static const struct got_error *
1102 view_resize_split(struct tog_view *view, int resize)
1104 const struct got_error *err = NULL;
1105 struct tog_view *v = NULL;
1107 if (view->parent)
1108 v = view->parent;
1109 else
1110 v = view;
1112 if (!v->child || !view_is_splitscreen(v->child))
1113 return NULL;
1115 v->resized = v->child->resized = resize; /* lock for resize event */
1117 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1118 if (v->child->resized_y)
1119 v->child->begin_y = v->child->resized_y;
1120 if (view->parent)
1121 v->child->begin_y -= resize;
1122 else
1123 v->child->begin_y += resize;
1124 if (v->child->begin_y < 3) {
1125 view->count = 0;
1126 v->child->begin_y = 3;
1127 } else if (v->child->begin_y > LINES - 1) {
1128 view->count = 0;
1129 v->child->begin_y = LINES - 1;
1131 v->ncols = COLS;
1132 v->child->ncols = COLS;
1133 view_adjust_offset(view, resize);
1134 err = view_init_hsplit(v, v->child->begin_y);
1135 if (err)
1136 return err;
1137 v->child->resized_y = v->child->begin_y;
1138 } else {
1139 if (v->child->resized_x)
1140 v->child->begin_x = v->child->resized_x;
1141 if (view->parent)
1142 v->child->begin_x -= resize;
1143 else
1144 v->child->begin_x += resize;
1145 if (v->child->begin_x < 11) {
1146 view->count = 0;
1147 v->child->begin_x = 11;
1148 } else if (v->child->begin_x > COLS - 1) {
1149 view->count = 0;
1150 v->child->begin_x = COLS - 1;
1152 v->child->resized_x = v->child->begin_x;
1155 v->child->mode = v->mode;
1156 v->child->nlines = v->lines - v->child->begin_y;
1157 v->child->ncols = v->cols - v->child->begin_x;
1158 v->focus_child = 1;
1160 err = view_fullscreen(v);
1161 if (err)
1162 return err;
1163 err = view_splitscreen(v->child);
1164 if (err)
1165 return err;
1167 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1168 err = offset_selection_down(v->child);
1169 if (err)
1170 return err;
1173 if (v->resize)
1174 err = v->resize(v, 0);
1175 else if (v->child->resize)
1176 err = v->child->resize(v->child, 0);
1178 v->resized = v->child->resized = 0;
1180 return err;
1183 static void
1184 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1186 struct tog_view *v = src->child ? src->child : src;
1188 dst->resized_x = v->resized_x;
1189 dst->resized_y = v->resized_y;
1192 static const struct got_error *
1193 view_close_child(struct tog_view *view)
1195 const struct got_error *err = NULL;
1197 if (view->child == NULL)
1198 return NULL;
1200 err = view_close(view->child);
1201 view->child = NULL;
1202 return err;
1205 static const struct got_error *
1206 view_set_child(struct tog_view *view, struct tog_view *child)
1208 const struct got_error *err = NULL;
1210 view->child = child;
1211 child->parent = view;
1213 err = view_resize(view);
1214 if (err)
1215 return err;
1217 if (view->child->resized_x || view->child->resized_y)
1218 err = view_resize_split(view, 0);
1220 return err;
1223 static const struct got_error *view_dispatch_request(struct tog_view **,
1224 struct tog_view *, enum tog_view_type, int, int);
1226 static const struct got_error *
1227 view_request_new(struct tog_view **requested, struct tog_view *view,
1228 enum tog_view_type request)
1230 struct tog_view *new_view = NULL;
1231 const struct got_error *err;
1232 int y = 0, x = 0;
1234 *requested = NULL;
1236 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1237 view_get_split(view, &y, &x);
1239 err = view_dispatch_request(&new_view, view, request, y, x);
1240 if (err)
1241 return err;
1243 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1244 request != TOG_VIEW_HELP) {
1245 err = view_init_hsplit(view, y);
1246 if (err)
1247 return err;
1250 view->focussed = 0;
1251 new_view->focussed = 1;
1252 new_view->mode = view->mode;
1253 new_view->nlines = request == TOG_VIEW_HELP ?
1254 view->lines : view->lines - y;
1256 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1257 view_transfer_size(new_view, view);
1258 err = view_close_child(view);
1259 if (err)
1260 return err;
1261 err = view_set_child(view, new_view);
1262 if (err)
1263 return err;
1264 view->focus_child = 1;
1265 } else
1266 *requested = new_view;
1268 return NULL;
1271 static void
1272 tog_resizeterm(void)
1274 int cols, lines;
1275 struct winsize size;
1277 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1278 cols = 80; /* Default */
1279 lines = 24;
1280 } else {
1281 cols = size.ws_col;
1282 lines = size.ws_row;
1284 resize_term(lines, cols);
1287 static const struct got_error *
1288 view_search_start(struct tog_view *view)
1290 const struct got_error *err = NULL;
1291 struct tog_view *v = view;
1292 char pattern[1024];
1293 int ret;
1295 if (view->search_started) {
1296 regfree(&view->regex);
1297 view->searching = 0;
1298 memset(&view->regmatch, 0, sizeof(view->regmatch));
1300 view->search_started = 0;
1302 if (view->nlines < 1)
1303 return NULL;
1305 if (view_is_hsplit_top(view))
1306 v = view->child;
1308 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1309 wclrtoeol(v->window);
1311 nodelay(view->window, FALSE); /* block for search term input */
1312 nocbreak();
1313 echo();
1314 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1315 wrefresh(v->window);
1316 cbreak();
1317 noecho();
1318 nodelay(view->window, TRUE);
1319 if (ret == ERR)
1320 return NULL;
1322 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1323 err = view->search_start(view);
1324 if (err) {
1325 regfree(&view->regex);
1326 return err;
1328 view->search_started = 1;
1329 view->searching = TOG_SEARCH_FORWARD;
1330 view->search_next_done = 0;
1331 view->search_next(view);
1334 return NULL;
1337 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1338 static const struct got_error *
1339 switch_split(struct tog_view *view)
1341 const struct got_error *err = NULL;
1342 struct tog_view *v = NULL;
1344 if (view->parent)
1345 v = view->parent;
1346 else
1347 v = view;
1349 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1350 v->mode = TOG_VIEW_SPLIT_VERT;
1351 else
1352 v->mode = TOG_VIEW_SPLIT_HRZN;
1354 if (!v->child)
1355 return NULL;
1356 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1357 v->mode = TOG_VIEW_SPLIT_NONE;
1359 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1360 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1361 v->child->begin_y = v->child->resized_y;
1362 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1363 v->child->begin_x = v->child->resized_x;
1366 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1367 v->ncols = COLS;
1368 v->child->ncols = COLS;
1369 v->child->nscrolled = LINES - v->child->nlines;
1371 err = view_init_hsplit(v, v->child->begin_y);
1372 if (err)
1373 return err;
1375 v->child->mode = v->mode;
1376 v->child->nlines = v->lines - v->child->begin_y;
1377 v->focus_child = 1;
1379 err = view_fullscreen(v);
1380 if (err)
1381 return err;
1382 err = view_splitscreen(v->child);
1383 if (err)
1384 return err;
1386 if (v->mode == TOG_VIEW_SPLIT_NONE)
1387 v->mode = TOG_VIEW_SPLIT_VERT;
1388 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1389 err = offset_selection_down(v);
1390 if (err)
1391 return err;
1392 err = offset_selection_down(v->child);
1393 if (err)
1394 return err;
1395 } else {
1396 offset_selection_up(v);
1397 offset_selection_up(v->child);
1399 if (v->resize)
1400 err = v->resize(v, 0);
1401 else if (v->child->resize)
1402 err = v->child->resize(v->child, 0);
1404 return err;
1408 * Compute view->count from numeric input. Assign total to view->count and
1409 * return first non-numeric key entered.
1411 static int
1412 get_compound_key(struct tog_view *view, int c)
1414 struct tog_view *v = view;
1415 int x, n = 0;
1417 if (view_is_hsplit_top(view))
1418 v = view->child;
1419 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1420 v = view->parent;
1422 view->count = 0;
1423 cbreak(); /* block for input */
1424 nodelay(view->window, FALSE);
1425 wmove(v->window, v->nlines - 1, 0);
1426 wclrtoeol(v->window);
1427 waddch(v->window, ':');
1429 do {
1430 x = getcurx(v->window);
1431 if (x != ERR && x < view->ncols) {
1432 waddch(v->window, c);
1433 wrefresh(v->window);
1437 * Don't overflow. Max valid request should be the greatest
1438 * between the longest and total lines; cap at 10 million.
1440 if (n >= 9999999)
1441 n = 9999999;
1442 else
1443 n = n * 10 + (c - '0');
1444 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1446 if (c == 'G' || c == 'g') { /* nG key map */
1447 view->gline = view->hiline = n;
1448 n = 0;
1449 c = 0;
1452 /* Massage excessive or inapplicable values at the input handler. */
1453 view->count = n;
1455 return c;
1458 static const struct got_error *
1459 view_input(struct tog_view **new, int *done, struct tog_view *view,
1460 struct tog_view_list_head *views)
1462 const struct got_error *err = NULL;
1463 struct tog_view *v;
1464 int ch, errcode;
1466 *new = NULL;
1468 /* Clear "no matches" indicator. */
1469 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1470 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1471 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1472 view->count = 0;
1475 if (view->searching && !view->search_next_done) {
1476 errcode = pthread_mutex_unlock(&tog_mutex);
1477 if (errcode)
1478 return got_error_set_errno(errcode,
1479 "pthread_mutex_unlock");
1480 sched_yield();
1481 errcode = pthread_mutex_lock(&tog_mutex);
1482 if (errcode)
1483 return got_error_set_errno(errcode,
1484 "pthread_mutex_lock");
1485 view->search_next(view);
1486 return NULL;
1489 /* Allow threads to make progress while we are waiting for input. */
1490 errcode = pthread_mutex_unlock(&tog_mutex);
1491 if (errcode)
1492 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1493 /* If we have an unfinished count, let C-g or backspace abort. */
1494 if (view->count && --view->count) {
1495 cbreak();
1496 nodelay(view->window, TRUE);
1497 ch = wgetch(view->window);
1498 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1499 view->count = 0;
1500 else
1501 ch = view->ch;
1502 } else {
1503 ch = wgetch(view->window);
1504 if (ch >= '1' && ch <= '9')
1505 view->ch = ch = get_compound_key(view, ch);
1507 if (view->hiline && ch != ERR && ch != 0)
1508 view->hiline = 0; /* key pressed, clear line highlight */
1509 nodelay(view->window, TRUE);
1510 errcode = pthread_mutex_lock(&tog_mutex);
1511 if (errcode)
1512 return got_error_set_errno(errcode, "pthread_mutex_lock");
1514 if (tog_sigwinch_received || tog_sigcont_received) {
1515 tog_resizeterm();
1516 tog_sigwinch_received = 0;
1517 tog_sigcont_received = 0;
1518 TAILQ_FOREACH(v, views, entry) {
1519 err = view_resize(v);
1520 if (err)
1521 return err;
1522 err = v->input(new, v, KEY_RESIZE);
1523 if (err)
1524 return err;
1525 if (v->child) {
1526 err = view_resize(v->child);
1527 if (err)
1528 return err;
1529 err = v->child->input(new, v->child,
1530 KEY_RESIZE);
1531 if (err)
1532 return err;
1533 if (v->child->resized_x || v->child->resized_y) {
1534 err = view_resize_split(v, 0);
1535 if (err)
1536 return err;
1542 switch (ch) {
1543 case '?':
1544 case 'H':
1545 case KEY_F(1):
1546 if (view->type == TOG_VIEW_HELP)
1547 err = view->reset(view);
1548 else
1549 err = view_request_new(new, view, TOG_VIEW_HELP);
1550 break;
1551 case '\t':
1552 view->count = 0;
1553 if (view->child) {
1554 view->focussed = 0;
1555 view->child->focussed = 1;
1556 view->focus_child = 1;
1557 } else if (view->parent) {
1558 view->focussed = 0;
1559 view->parent->focussed = 1;
1560 view->parent->focus_child = 0;
1561 if (!view_is_splitscreen(view)) {
1562 if (view->parent->resize) {
1563 err = view->parent->resize(view->parent,
1564 0);
1565 if (err)
1566 return err;
1568 offset_selection_up(view->parent);
1569 err = view_fullscreen(view->parent);
1570 if (err)
1571 return err;
1574 break;
1575 case 'q':
1576 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1577 if (view->parent->resize) {
1578 /* might need more commits to fill fullscreen */
1579 err = view->parent->resize(view->parent, 0);
1580 if (err)
1581 break;
1583 offset_selection_up(view->parent);
1585 err = view->input(new, view, ch);
1586 view->dying = 1;
1587 break;
1588 case 'Q':
1589 *done = 1;
1590 break;
1591 case 'F':
1592 view->count = 0;
1593 if (view_is_parent_view(view)) {
1594 if (view->child == NULL)
1595 break;
1596 if (view_is_splitscreen(view->child)) {
1597 view->focussed = 0;
1598 view->child->focussed = 1;
1599 err = view_fullscreen(view->child);
1600 } else {
1601 err = view_splitscreen(view->child);
1602 if (!err)
1603 err = view_resize_split(view, 0);
1605 if (err)
1606 break;
1607 err = view->child->input(new, view->child,
1608 KEY_RESIZE);
1609 } else {
1610 if (view_is_splitscreen(view)) {
1611 view->parent->focussed = 0;
1612 view->focussed = 1;
1613 err = view_fullscreen(view);
1614 } else {
1615 err = view_splitscreen(view);
1616 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1617 err = view_resize(view->parent);
1618 if (!err)
1619 err = view_resize_split(view, 0);
1621 if (err)
1622 break;
1623 err = view->input(new, view, KEY_RESIZE);
1625 if (err)
1626 break;
1627 if (view->resize) {
1628 err = view->resize(view, 0);
1629 if (err)
1630 break;
1632 if (view->parent)
1633 err = offset_selection_down(view->parent);
1634 if (!err)
1635 err = offset_selection_down(view);
1636 break;
1637 case 'S':
1638 view->count = 0;
1639 err = switch_split(view);
1640 break;
1641 case '-':
1642 err = view_resize_split(view, -1);
1643 break;
1644 case '+':
1645 err = view_resize_split(view, 1);
1646 break;
1647 case KEY_RESIZE:
1648 break;
1649 case '/':
1650 view->count = 0;
1651 if (view->search_start)
1652 view_search_start(view);
1653 else
1654 err = view->input(new, view, ch);
1655 break;
1656 case 'N':
1657 case 'n':
1658 if (view->search_started && view->search_next) {
1659 view->searching = (ch == 'n' ?
1660 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1661 view->search_next_done = 0;
1662 view->search_next(view);
1663 } else
1664 err = view->input(new, view, ch);
1665 break;
1666 case 'A':
1667 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1668 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1669 else
1670 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1671 TAILQ_FOREACH(v, views, entry) {
1672 if (v->reset) {
1673 err = v->reset(v);
1674 if (err)
1675 return err;
1677 if (v->child && v->child->reset) {
1678 err = v->child->reset(v->child);
1679 if (err)
1680 return err;
1683 break;
1684 default:
1685 err = view->input(new, view, ch);
1686 break;
1689 return err;
1692 static int
1693 view_needs_focus_indication(struct tog_view *view)
1695 if (view_is_parent_view(view)) {
1696 if (view->child == NULL || view->child->focussed)
1697 return 0;
1698 if (!view_is_splitscreen(view->child))
1699 return 0;
1700 } else if (!view_is_splitscreen(view))
1701 return 0;
1703 return view->focussed;
1706 static const struct got_error *
1707 view_loop(struct tog_view *view)
1709 const struct got_error *err = NULL;
1710 struct tog_view_list_head views;
1711 struct tog_view *new_view;
1712 char *mode;
1713 int fast_refresh = 10;
1714 int done = 0, errcode;
1716 mode = getenv("TOG_VIEW_SPLIT_MODE");
1717 if (!mode || !(*mode == 'h' || *mode == 'H'))
1718 view->mode = TOG_VIEW_SPLIT_VERT;
1719 else
1720 view->mode = TOG_VIEW_SPLIT_HRZN;
1722 errcode = pthread_mutex_lock(&tog_mutex);
1723 if (errcode)
1724 return got_error_set_errno(errcode, "pthread_mutex_lock");
1726 TAILQ_INIT(&views);
1727 TAILQ_INSERT_HEAD(&views, view, entry);
1729 view->focussed = 1;
1730 err = view->show(view);
1731 if (err)
1732 return err;
1733 update_panels();
1734 doupdate();
1735 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1736 !tog_fatal_signal_received()) {
1737 /* Refresh fast during initialization, then become slower. */
1738 if (fast_refresh && fast_refresh-- == 0)
1739 halfdelay(10); /* switch to once per second */
1741 err = view_input(&new_view, &done, view, &views);
1742 if (err)
1743 break;
1744 if (view->dying) {
1745 struct tog_view *v, *prev = NULL;
1747 if (view_is_parent_view(view))
1748 prev = TAILQ_PREV(view, tog_view_list_head,
1749 entry);
1750 else if (view->parent)
1751 prev = view->parent;
1753 if (view->parent) {
1754 view->parent->child = NULL;
1755 view->parent->focus_child = 0;
1756 /* Restore fullscreen line height. */
1757 view->parent->nlines = view->parent->lines;
1758 err = view_resize(view->parent);
1759 if (err)
1760 break;
1761 /* Make resized splits persist. */
1762 view_transfer_size(view->parent, view);
1763 } else
1764 TAILQ_REMOVE(&views, view, entry);
1766 err = view_close(view);
1767 if (err)
1768 goto done;
1770 view = NULL;
1771 TAILQ_FOREACH(v, &views, entry) {
1772 if (v->focussed)
1773 break;
1775 if (view == NULL && new_view == NULL) {
1776 /* No view has focus. Try to pick one. */
1777 if (prev)
1778 view = prev;
1779 else if (!TAILQ_EMPTY(&views)) {
1780 view = TAILQ_LAST(&views,
1781 tog_view_list_head);
1783 if (view) {
1784 if (view->focus_child) {
1785 view->child->focussed = 1;
1786 view = view->child;
1787 } else
1788 view->focussed = 1;
1792 if (new_view) {
1793 struct tog_view *v, *t;
1794 /* Only allow one parent view per type. */
1795 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1796 if (v->type != new_view->type)
1797 continue;
1798 TAILQ_REMOVE(&views, v, entry);
1799 err = view_close(v);
1800 if (err)
1801 goto done;
1802 break;
1804 TAILQ_INSERT_TAIL(&views, new_view, entry);
1805 view = new_view;
1807 if (view) {
1808 if (view_is_parent_view(view)) {
1809 if (view->child && view->child->focussed)
1810 view = view->child;
1811 } else {
1812 if (view->parent && view->parent->focussed)
1813 view = view->parent;
1815 show_panel(view->panel);
1816 if (view->child && view_is_splitscreen(view->child))
1817 show_panel(view->child->panel);
1818 if (view->parent && view_is_splitscreen(view)) {
1819 err = view->parent->show(view->parent);
1820 if (err)
1821 goto done;
1823 err = view->show(view);
1824 if (err)
1825 goto done;
1826 if (view->child) {
1827 err = view->child->show(view->child);
1828 if (err)
1829 goto done;
1831 update_panels();
1832 doupdate();
1835 done:
1836 while (!TAILQ_EMPTY(&views)) {
1837 const struct got_error *close_err;
1838 view = TAILQ_FIRST(&views);
1839 TAILQ_REMOVE(&views, view, entry);
1840 close_err = view_close(view);
1841 if (close_err && err == NULL)
1842 err = close_err;
1845 errcode = pthread_mutex_unlock(&tog_mutex);
1846 if (errcode && err == NULL)
1847 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1849 return err;
1852 __dead static void
1853 usage_log(void)
1855 endwin();
1856 fprintf(stderr,
1857 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1858 getprogname());
1859 exit(1);
1862 /* Create newly allocated wide-character string equivalent to a byte string. */
1863 static const struct got_error *
1864 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1866 char *vis = NULL;
1867 const struct got_error *err = NULL;
1869 *ws = NULL;
1870 *wlen = mbstowcs(NULL, s, 0);
1871 if (*wlen == (size_t)-1) {
1872 int vislen;
1873 if (errno != EILSEQ)
1874 return got_error_from_errno("mbstowcs");
1876 /* byte string invalid in current encoding; try to "fix" it */
1877 err = got_mbsavis(&vis, &vislen, s);
1878 if (err)
1879 return err;
1880 *wlen = mbstowcs(NULL, vis, 0);
1881 if (*wlen == (size_t)-1) {
1882 err = got_error_from_errno("mbstowcs"); /* give up */
1883 goto done;
1887 *ws = calloc(*wlen + 1, sizeof(**ws));
1888 if (*ws == NULL) {
1889 err = got_error_from_errno("calloc");
1890 goto done;
1893 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1894 err = got_error_from_errno("mbstowcs");
1895 done:
1896 free(vis);
1897 if (err) {
1898 free(*ws);
1899 *ws = NULL;
1900 *wlen = 0;
1902 return err;
1905 static const struct got_error *
1906 expand_tab(char **ptr, const char *src)
1908 char *dst;
1909 size_t len, n, idx = 0, sz = 0;
1911 *ptr = NULL;
1912 n = len = strlen(src);
1913 dst = malloc(n + 1);
1914 if (dst == NULL)
1915 return got_error_from_errno("malloc");
1917 while (idx < len && src[idx]) {
1918 const char c = src[idx];
1920 if (c == '\t') {
1921 size_t nb = TABSIZE - sz % TABSIZE;
1922 char *p;
1924 p = realloc(dst, n + nb);
1925 if (p == NULL) {
1926 free(dst);
1927 return got_error_from_errno("realloc");
1930 dst = p;
1931 n += nb;
1932 memset(dst + sz, ' ', nb);
1933 sz += nb;
1934 } else
1935 dst[sz++] = src[idx];
1936 ++idx;
1939 dst[sz] = '\0';
1940 *ptr = dst;
1941 return NULL;
1945 * Advance at most n columns from wline starting at offset off.
1946 * Return the index to the first character after the span operation.
1947 * Return the combined column width of all spanned wide character in
1948 * *rcol.
1950 static int
1951 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1953 int width, i, cols = 0;
1955 if (n == 0) {
1956 *rcol = cols;
1957 return off;
1960 for (i = off; wline[i] != L'\0'; ++i) {
1961 if (wline[i] == L'\t')
1962 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1963 else
1964 width = wcwidth(wline[i]);
1966 if (width == -1) {
1967 width = 1;
1968 wline[i] = L'.';
1971 if (cols + width > n)
1972 break;
1973 cols += width;
1976 *rcol = cols;
1977 return i;
1981 * Format a line for display, ensuring that it won't overflow a width limit.
1982 * With scrolling, the width returned refers to the scrolled version of the
1983 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1985 static const struct got_error *
1986 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1987 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1989 const struct got_error *err = NULL;
1990 int cols;
1991 wchar_t *wline = NULL;
1992 char *exstr = NULL;
1993 size_t wlen;
1994 int i, scrollx;
1996 *wlinep = NULL;
1997 *widthp = 0;
1999 if (expand) {
2000 err = expand_tab(&exstr, line);
2001 if (err)
2002 return err;
2005 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2006 free(exstr);
2007 if (err)
2008 return err;
2010 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2012 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2013 wline[wlen - 1] = L'\0';
2014 wlen--;
2016 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2017 wline[wlen - 1] = L'\0';
2018 wlen--;
2021 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2022 wline[i] = L'\0';
2024 if (widthp)
2025 *widthp = cols;
2026 if (scrollxp)
2027 *scrollxp = scrollx;
2028 if (err)
2029 free(wline);
2030 else
2031 *wlinep = wline;
2032 return err;
2035 static const struct got_error*
2036 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2037 struct got_object_id *id, struct got_repository *repo)
2039 static const struct got_error *err = NULL;
2040 struct got_reflist_entry *re;
2041 char *s;
2042 const char *name;
2044 *refs_str = NULL;
2046 TAILQ_FOREACH(re, refs, entry) {
2047 struct got_tag_object *tag = NULL;
2048 struct got_object_id *ref_id;
2049 int cmp;
2051 name = got_ref_get_name(re->ref);
2052 if (strcmp(name, GOT_REF_HEAD) == 0)
2053 continue;
2054 if (strncmp(name, "refs/", 5) == 0)
2055 name += 5;
2056 if (strncmp(name, "got/", 4) == 0 &&
2057 strncmp(name, "got/backup/", 11) != 0)
2058 continue;
2059 if (strncmp(name, "heads/", 6) == 0)
2060 name += 6;
2061 if (strncmp(name, "remotes/", 8) == 0) {
2062 name += 8;
2063 s = strstr(name, "/" GOT_REF_HEAD);
2064 if (s != NULL && s[strlen(s)] == '\0')
2065 continue;
2067 err = got_ref_resolve(&ref_id, repo, re->ref);
2068 if (err)
2069 break;
2070 if (strncmp(name, "tags/", 5) == 0) {
2071 err = got_object_open_as_tag(&tag, repo, ref_id);
2072 if (err) {
2073 if (err->code != GOT_ERR_OBJ_TYPE) {
2074 free(ref_id);
2075 break;
2077 /* Ref points at something other than a tag. */
2078 err = NULL;
2079 tag = NULL;
2082 cmp = got_object_id_cmp(tag ?
2083 got_object_tag_get_object_id(tag) : ref_id, id);
2084 free(ref_id);
2085 if (tag)
2086 got_object_tag_close(tag);
2087 if (cmp != 0)
2088 continue;
2089 s = *refs_str;
2090 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2091 s ? ", " : "", name) == -1) {
2092 err = got_error_from_errno("asprintf");
2093 free(s);
2094 *refs_str = NULL;
2095 break;
2097 free(s);
2100 return err;
2103 static const struct got_error *
2104 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2105 int col_tab_align)
2107 char *smallerthan;
2109 smallerthan = strchr(author, '<');
2110 if (smallerthan && smallerthan[1] != '\0')
2111 author = smallerthan + 1;
2112 author[strcspn(author, "@>")] = '\0';
2113 return format_line(wauthor, author_width, NULL, author, 0, limit,
2114 col_tab_align, 0);
2117 static const struct got_error *
2118 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2119 struct got_object_id *id, const size_t date_display_cols,
2120 int author_display_cols)
2122 struct tog_log_view_state *s = &view->state.log;
2123 const struct got_error *err = NULL;
2124 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2125 char *logmsg0 = NULL, *logmsg = NULL;
2126 char *author = NULL;
2127 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2128 int author_width, logmsg_width;
2129 char *newline, *line = NULL;
2130 int col, limit, scrollx;
2131 const int avail = view->ncols;
2132 struct tm tm;
2133 time_t committer_time;
2134 struct tog_color *tc;
2136 committer_time = got_object_commit_get_committer_time(commit);
2137 if (gmtime_r(&committer_time, &tm) == NULL)
2138 return got_error_from_errno("gmtime_r");
2139 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2140 return got_error(GOT_ERR_NO_SPACE);
2142 if (avail <= date_display_cols)
2143 limit = MIN(sizeof(datebuf) - 1, avail);
2144 else
2145 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2146 tc = get_color(&s->colors, TOG_COLOR_DATE);
2147 if (tc)
2148 wattr_on(view->window,
2149 COLOR_PAIR(tc->colorpair), NULL);
2150 waddnstr(view->window, datebuf, limit);
2151 if (tc)
2152 wattr_off(view->window,
2153 COLOR_PAIR(tc->colorpair), NULL);
2154 col = limit;
2155 if (col > avail)
2156 goto done;
2158 if (avail >= 120) {
2159 char *id_str;
2160 err = got_object_id_str(&id_str, id);
2161 if (err)
2162 goto done;
2163 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2164 if (tc)
2165 wattr_on(view->window,
2166 COLOR_PAIR(tc->colorpair), NULL);
2167 wprintw(view->window, "%.8s ", id_str);
2168 if (tc)
2169 wattr_off(view->window,
2170 COLOR_PAIR(tc->colorpair), NULL);
2171 free(id_str);
2172 col += 9;
2173 if (col > avail)
2174 goto done;
2177 if (s->use_committer)
2178 author = strdup(got_object_commit_get_committer(commit));
2179 else
2180 author = strdup(got_object_commit_get_author(commit));
2181 if (author == NULL) {
2182 err = got_error_from_errno("strdup");
2183 goto done;
2185 err = format_author(&wauthor, &author_width, author, avail - col, col);
2186 if (err)
2187 goto done;
2188 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2189 if (tc)
2190 wattr_on(view->window,
2191 COLOR_PAIR(tc->colorpair), NULL);
2192 waddwstr(view->window, wauthor);
2193 col += author_width;
2194 while (col < avail && author_width < author_display_cols + 2) {
2195 waddch(view->window, ' ');
2196 col++;
2197 author_width++;
2199 if (tc)
2200 wattr_off(view->window,
2201 COLOR_PAIR(tc->colorpair), NULL);
2202 if (col > avail)
2203 goto done;
2205 err = got_object_commit_get_logmsg(&logmsg0, commit);
2206 if (err)
2207 goto done;
2208 logmsg = logmsg0;
2209 while (*logmsg == '\n')
2210 logmsg++;
2211 newline = strchr(logmsg, '\n');
2212 if (newline)
2213 *newline = '\0';
2214 limit = avail - col;
2215 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2216 limit--; /* for the border */
2217 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2218 limit, col, 1);
2219 if (err)
2220 goto done;
2221 waddwstr(view->window, &wlogmsg[scrollx]);
2222 col += MAX(logmsg_width, 0);
2223 while (col < avail) {
2224 waddch(view->window, ' ');
2225 col++;
2227 done:
2228 free(logmsg0);
2229 free(wlogmsg);
2230 free(author);
2231 free(wauthor);
2232 free(line);
2233 return err;
2236 static struct commit_queue_entry *
2237 alloc_commit_queue_entry(struct got_commit_object *commit,
2238 struct got_object_id *id)
2240 struct commit_queue_entry *entry;
2241 struct got_object_id *dup;
2243 entry = calloc(1, sizeof(*entry));
2244 if (entry == NULL)
2245 return NULL;
2247 dup = got_object_id_dup(id);
2248 if (dup == NULL) {
2249 free(entry);
2250 return NULL;
2253 entry->id = dup;
2254 entry->commit = commit;
2255 return entry;
2258 static void
2259 pop_commit(struct commit_queue *commits)
2261 struct commit_queue_entry *entry;
2263 entry = TAILQ_FIRST(&commits->head);
2264 TAILQ_REMOVE(&commits->head, entry, entry);
2265 got_object_commit_close(entry->commit);
2266 commits->ncommits--;
2267 free(entry->id);
2268 free(entry);
2271 static void
2272 free_commits(struct commit_queue *commits)
2274 while (!TAILQ_EMPTY(&commits->head))
2275 pop_commit(commits);
2278 static const struct got_error *
2279 match_commit(int *have_match, struct got_object_id *id,
2280 struct got_commit_object *commit, regex_t *regex)
2282 const struct got_error *err = NULL;
2283 regmatch_t regmatch;
2284 char *id_str = NULL, *logmsg = NULL;
2286 *have_match = 0;
2288 err = got_object_id_str(&id_str, id);
2289 if (err)
2290 return err;
2292 err = got_object_commit_get_logmsg(&logmsg, commit);
2293 if (err)
2294 goto done;
2296 if (regexec(regex, got_object_commit_get_author(commit), 1,
2297 &regmatch, 0) == 0 ||
2298 regexec(regex, got_object_commit_get_committer(commit), 1,
2299 &regmatch, 0) == 0 ||
2300 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2301 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2302 *have_match = 1;
2303 done:
2304 free(id_str);
2305 free(logmsg);
2306 return err;
2309 static const struct got_error *
2310 queue_commits(struct tog_log_thread_args *a)
2312 const struct got_error *err = NULL;
2315 * We keep all commits open throughout the lifetime of the log
2316 * view in order to avoid having to re-fetch commits from disk
2317 * while updating the display.
2319 do {
2320 struct got_object_id id;
2321 struct got_commit_object *commit;
2322 struct commit_queue_entry *entry;
2323 int limit_match = 0;
2324 int errcode;
2326 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2327 NULL, NULL);
2328 if (err)
2329 break;
2331 err = got_object_open_as_commit(&commit, a->repo, &id);
2332 if (err)
2333 break;
2334 entry = alloc_commit_queue_entry(commit, &id);
2335 if (entry == NULL) {
2336 err = got_error_from_errno("alloc_commit_queue_entry");
2337 break;
2340 errcode = pthread_mutex_lock(&tog_mutex);
2341 if (errcode) {
2342 err = got_error_set_errno(errcode,
2343 "pthread_mutex_lock");
2344 break;
2347 entry->idx = a->real_commits->ncommits;
2348 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2349 a->real_commits->ncommits++;
2351 if (*a->limiting) {
2352 err = match_commit(&limit_match, &id, commit,
2353 a->limit_regex);
2354 if (err)
2355 break;
2357 if (limit_match) {
2358 struct commit_queue_entry *matched;
2360 matched = alloc_commit_queue_entry(
2361 entry->commit, entry->id);
2362 if (matched == NULL) {
2363 err = got_error_from_errno(
2364 "alloc_commit_queue_entry");
2365 break;
2367 matched->commit = entry->commit;
2368 got_object_commit_retain(entry->commit);
2370 matched->idx = a->limit_commits->ncommits;
2371 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2372 matched, entry);
2373 a->limit_commits->ncommits++;
2377 * This is how we signal log_thread() that we
2378 * have found a match, and that it should be
2379 * counted as a new entry for the view.
2381 a->limit_match = limit_match;
2384 if (*a->searching == TOG_SEARCH_FORWARD &&
2385 !*a->search_next_done) {
2386 int have_match;
2387 err = match_commit(&have_match, &id, commit, a->regex);
2388 if (err)
2389 break;
2391 if (*a->limiting) {
2392 if (limit_match && have_match)
2393 *a->search_next_done =
2394 TOG_SEARCH_HAVE_MORE;
2395 } else if (have_match)
2396 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2399 errcode = pthread_mutex_unlock(&tog_mutex);
2400 if (errcode && err == NULL)
2401 err = got_error_set_errno(errcode,
2402 "pthread_mutex_unlock");
2403 if (err)
2404 break;
2405 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2407 return err;
2410 static void
2411 select_commit(struct tog_log_view_state *s)
2413 struct commit_queue_entry *entry;
2414 int ncommits = 0;
2416 entry = s->first_displayed_entry;
2417 while (entry) {
2418 if (ncommits == s->selected) {
2419 s->selected_entry = entry;
2420 break;
2422 entry = TAILQ_NEXT(entry, entry);
2423 ncommits++;
2427 static const struct got_error *
2428 draw_commits(struct tog_view *view)
2430 const struct got_error *err = NULL;
2431 struct tog_log_view_state *s = &view->state.log;
2432 struct commit_queue_entry *entry = s->selected_entry;
2433 int limit = view->nlines;
2434 int width;
2435 int ncommits, author_cols = 4;
2436 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2437 char *refs_str = NULL;
2438 wchar_t *wline;
2439 struct tog_color *tc;
2440 static const size_t date_display_cols = 12;
2442 if (view_is_hsplit_top(view))
2443 --limit; /* account for border */
2445 if (s->selected_entry &&
2446 !(view->searching && view->search_next_done == 0)) {
2447 struct got_reflist_head *refs;
2448 err = got_object_id_str(&id_str, s->selected_entry->id);
2449 if (err)
2450 return err;
2451 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2452 s->selected_entry->id);
2453 if (refs) {
2454 err = build_refs_str(&refs_str, refs,
2455 s->selected_entry->id, s->repo);
2456 if (err)
2457 goto done;
2461 if (s->thread_args.commits_needed == 0)
2462 halfdelay(10); /* disable fast refresh */
2464 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2465 if (asprintf(&ncommits_str, " [%d/%d] %s",
2466 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2467 (view->searching && !view->search_next_done) ?
2468 "searching..." : "loading...") == -1) {
2469 err = got_error_from_errno("asprintf");
2470 goto done;
2472 } else {
2473 const char *search_str = NULL;
2474 const char *limit_str = NULL;
2476 if (view->searching) {
2477 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2478 search_str = "no more matches";
2479 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2480 search_str = "no matches found";
2481 else if (!view->search_next_done)
2482 search_str = "searching...";
2485 if (s->limit_view && s->commits->ncommits == 0)
2486 limit_str = "no matches found";
2488 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2489 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2490 search_str ? search_str : (refs_str ? refs_str : ""),
2491 limit_str ? limit_str : "") == -1) {
2492 err = got_error_from_errno("asprintf");
2493 goto done;
2497 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2498 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2499 "........................................",
2500 s->in_repo_path, ncommits_str) == -1) {
2501 err = got_error_from_errno("asprintf");
2502 header = NULL;
2503 goto done;
2505 } else if (asprintf(&header, "commit %s%s",
2506 id_str ? id_str : "........................................",
2507 ncommits_str) == -1) {
2508 err = got_error_from_errno("asprintf");
2509 header = NULL;
2510 goto done;
2512 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2513 if (err)
2514 goto done;
2516 werase(view->window);
2518 if (view_needs_focus_indication(view))
2519 wstandout(view->window);
2520 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2521 if (tc)
2522 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2523 waddwstr(view->window, wline);
2524 while (width < view->ncols) {
2525 waddch(view->window, ' ');
2526 width++;
2528 if (tc)
2529 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2530 if (view_needs_focus_indication(view))
2531 wstandend(view->window);
2532 free(wline);
2533 if (limit <= 1)
2534 goto done;
2536 /* Grow author column size if necessary, and set view->maxx. */
2537 entry = s->first_displayed_entry;
2538 ncommits = 0;
2539 view->maxx = 0;
2540 while (entry) {
2541 struct got_commit_object *c = entry->commit;
2542 char *author, *eol, *msg, *msg0;
2543 wchar_t *wauthor, *wmsg;
2544 int width;
2545 if (ncommits >= limit - 1)
2546 break;
2547 if (s->use_committer)
2548 author = strdup(got_object_commit_get_committer(c));
2549 else
2550 author = strdup(got_object_commit_get_author(c));
2551 if (author == NULL) {
2552 err = got_error_from_errno("strdup");
2553 goto done;
2555 err = format_author(&wauthor, &width, author, COLS,
2556 date_display_cols);
2557 if (author_cols < width)
2558 author_cols = width;
2559 free(wauthor);
2560 free(author);
2561 if (err)
2562 goto done;
2563 err = got_object_commit_get_logmsg(&msg0, c);
2564 if (err)
2565 goto done;
2566 msg = msg0;
2567 while (*msg == '\n')
2568 ++msg;
2569 if ((eol = strchr(msg, '\n')))
2570 *eol = '\0';
2571 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2572 date_display_cols + author_cols, 0);
2573 if (err)
2574 goto done;
2575 view->maxx = MAX(view->maxx, width);
2576 free(msg0);
2577 free(wmsg);
2578 ncommits++;
2579 entry = TAILQ_NEXT(entry, entry);
2582 entry = s->first_displayed_entry;
2583 s->last_displayed_entry = s->first_displayed_entry;
2584 ncommits = 0;
2585 while (entry) {
2586 if (ncommits >= limit - 1)
2587 break;
2588 if (ncommits == s->selected)
2589 wstandout(view->window);
2590 err = draw_commit(view, entry->commit, entry->id,
2591 date_display_cols, author_cols);
2592 if (ncommits == s->selected)
2593 wstandend(view->window);
2594 if (err)
2595 goto done;
2596 ncommits++;
2597 s->last_displayed_entry = entry;
2598 entry = TAILQ_NEXT(entry, entry);
2601 view_border(view);
2602 done:
2603 free(id_str);
2604 free(refs_str);
2605 free(ncommits_str);
2606 free(header);
2607 return err;
2610 static void
2611 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2613 struct commit_queue_entry *entry;
2614 int nscrolled = 0;
2616 entry = TAILQ_FIRST(&s->commits->head);
2617 if (s->first_displayed_entry == entry)
2618 return;
2620 entry = s->first_displayed_entry;
2621 while (entry && nscrolled < maxscroll) {
2622 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2623 if (entry) {
2624 s->first_displayed_entry = entry;
2625 nscrolled++;
2630 static const struct got_error *
2631 trigger_log_thread(struct tog_view *view, int wait)
2633 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2634 int errcode;
2636 halfdelay(1); /* fast refresh while loading commits */
2638 while (!ta->log_complete && !tog_thread_error &&
2639 (ta->commits_needed > 0 || ta->load_all)) {
2640 /* Wake the log thread. */
2641 errcode = pthread_cond_signal(&ta->need_commits);
2642 if (errcode)
2643 return got_error_set_errno(errcode,
2644 "pthread_cond_signal");
2647 * The mutex will be released while the view loop waits
2648 * in wgetch(), at which time the log thread will run.
2650 if (!wait)
2651 break;
2653 /* Display progress update in log view. */
2654 show_log_view(view);
2655 update_panels();
2656 doupdate();
2658 /* Wait right here while next commit is being loaded. */
2659 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2660 if (errcode)
2661 return got_error_set_errno(errcode,
2662 "pthread_cond_wait");
2664 /* Display progress update in log view. */
2665 show_log_view(view);
2666 update_panels();
2667 doupdate();
2670 return NULL;
2673 static const struct got_error *
2674 request_log_commits(struct tog_view *view)
2676 struct tog_log_view_state *state = &view->state.log;
2677 const struct got_error *err = NULL;
2679 if (state->thread_args.log_complete)
2680 return NULL;
2682 state->thread_args.commits_needed += view->nscrolled;
2683 err = trigger_log_thread(view, 1);
2684 view->nscrolled = 0;
2686 return err;
2689 static const struct got_error *
2690 log_scroll_down(struct tog_view *view, int maxscroll)
2692 struct tog_log_view_state *s = &view->state.log;
2693 const struct got_error *err = NULL;
2694 struct commit_queue_entry *pentry;
2695 int nscrolled = 0, ncommits_needed;
2697 if (s->last_displayed_entry == NULL)
2698 return NULL;
2700 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2701 if (s->commits->ncommits < ncommits_needed &&
2702 !s->thread_args.log_complete) {
2704 * Ask the log thread for required amount of commits.
2706 s->thread_args.commits_needed +=
2707 ncommits_needed - s->commits->ncommits;
2708 err = trigger_log_thread(view, 1);
2709 if (err)
2710 return err;
2713 do {
2714 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2715 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2716 break;
2718 s->last_displayed_entry = pentry ?
2719 pentry : s->last_displayed_entry;;
2721 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2722 if (pentry == NULL)
2723 break;
2724 s->first_displayed_entry = pentry;
2725 } while (++nscrolled < maxscroll);
2727 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2728 view->nscrolled += nscrolled;
2729 else
2730 view->nscrolled = 0;
2732 return err;
2735 static const struct got_error *
2736 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2737 struct got_commit_object *commit, struct got_object_id *commit_id,
2738 struct tog_view *log_view, struct got_repository *repo)
2740 const struct got_error *err;
2741 struct got_object_qid *parent_id;
2742 struct tog_view *diff_view;
2744 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2745 if (diff_view == NULL)
2746 return got_error_from_errno("view_open");
2748 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2749 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2750 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2751 if (err == NULL)
2752 *new_view = diff_view;
2753 return err;
2756 static const struct got_error *
2757 tree_view_visit_subtree(struct tog_tree_view_state *s,
2758 struct got_tree_object *subtree)
2760 struct tog_parent_tree *parent;
2762 parent = calloc(1, sizeof(*parent));
2763 if (parent == NULL)
2764 return got_error_from_errno("calloc");
2766 parent->tree = s->tree;
2767 parent->first_displayed_entry = s->first_displayed_entry;
2768 parent->selected_entry = s->selected_entry;
2769 parent->selected = s->selected;
2770 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2771 s->tree = subtree;
2772 s->selected = 0;
2773 s->first_displayed_entry = NULL;
2774 return NULL;
2777 static const struct got_error *
2778 tree_view_walk_path(struct tog_tree_view_state *s,
2779 struct got_commit_object *commit, const char *path)
2781 const struct got_error *err = NULL;
2782 struct got_tree_object *tree = NULL;
2783 const char *p;
2784 char *slash, *subpath = NULL;
2786 /* Walk the path and open corresponding tree objects. */
2787 p = path;
2788 while (*p) {
2789 struct got_tree_entry *te;
2790 struct got_object_id *tree_id;
2791 char *te_name;
2793 while (p[0] == '/')
2794 p++;
2796 /* Ensure the correct subtree entry is selected. */
2797 slash = strchr(p, '/');
2798 if (slash == NULL)
2799 te_name = strdup(p);
2800 else
2801 te_name = strndup(p, slash - p);
2802 if (te_name == NULL) {
2803 err = got_error_from_errno("strndup");
2804 break;
2806 te = got_object_tree_find_entry(s->tree, te_name);
2807 if (te == NULL) {
2808 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2809 free(te_name);
2810 break;
2812 free(te_name);
2813 s->first_displayed_entry = s->selected_entry = te;
2815 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2816 break; /* jump to this file's entry */
2818 slash = strchr(p, '/');
2819 if (slash)
2820 subpath = strndup(path, slash - path);
2821 else
2822 subpath = strdup(path);
2823 if (subpath == NULL) {
2824 err = got_error_from_errno("strdup");
2825 break;
2828 err = got_object_id_by_path(&tree_id, s->repo, commit,
2829 subpath);
2830 if (err)
2831 break;
2833 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2834 free(tree_id);
2835 if (err)
2836 break;
2838 err = tree_view_visit_subtree(s, tree);
2839 if (err) {
2840 got_object_tree_close(tree);
2841 break;
2843 if (slash == NULL)
2844 break;
2845 free(subpath);
2846 subpath = NULL;
2847 p = slash;
2850 free(subpath);
2851 return err;
2854 static const struct got_error *
2855 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2856 struct commit_queue_entry *entry, const char *path,
2857 const char *head_ref_name, struct got_repository *repo)
2859 const struct got_error *err = NULL;
2860 struct tog_tree_view_state *s;
2861 struct tog_view *tree_view;
2863 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2864 if (tree_view == NULL)
2865 return got_error_from_errno("view_open");
2867 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2868 if (err)
2869 return err;
2870 s = &tree_view->state.tree;
2872 *new_view = tree_view;
2874 if (got_path_is_root_dir(path))
2875 return NULL;
2877 return tree_view_walk_path(s, entry->commit, path);
2880 static const struct got_error *
2881 block_signals_used_by_main_thread(void)
2883 sigset_t sigset;
2884 int errcode;
2886 if (sigemptyset(&sigset) == -1)
2887 return got_error_from_errno("sigemptyset");
2889 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2890 if (sigaddset(&sigset, SIGWINCH) == -1)
2891 return got_error_from_errno("sigaddset");
2892 if (sigaddset(&sigset, SIGCONT) == -1)
2893 return got_error_from_errno("sigaddset");
2894 if (sigaddset(&sigset, SIGINT) == -1)
2895 return got_error_from_errno("sigaddset");
2896 if (sigaddset(&sigset, SIGTERM) == -1)
2897 return got_error_from_errno("sigaddset");
2899 /* ncurses handles SIGTSTP */
2900 if (sigaddset(&sigset, SIGTSTP) == -1)
2901 return got_error_from_errno("sigaddset");
2903 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2904 if (errcode)
2905 return got_error_set_errno(errcode, "pthread_sigmask");
2907 return NULL;
2910 static void *
2911 log_thread(void *arg)
2913 const struct got_error *err = NULL;
2914 int errcode = 0;
2915 struct tog_log_thread_args *a = arg;
2916 int done = 0;
2919 * Sync startup with main thread such that we begin our
2920 * work once view_input() has released the mutex.
2922 errcode = pthread_mutex_lock(&tog_mutex);
2923 if (errcode) {
2924 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2925 return (void *)err;
2928 err = block_signals_used_by_main_thread();
2929 if (err) {
2930 pthread_mutex_unlock(&tog_mutex);
2931 goto done;
2934 while (!done && !err && !tog_fatal_signal_received()) {
2935 errcode = pthread_mutex_unlock(&tog_mutex);
2936 if (errcode) {
2937 err = got_error_set_errno(errcode,
2938 "pthread_mutex_unlock");
2939 goto done;
2941 err = queue_commits(a);
2942 if (err) {
2943 if (err->code != GOT_ERR_ITER_COMPLETED)
2944 goto done;
2945 err = NULL;
2946 done = 1;
2947 } else if (a->commits_needed > 0 && !a->load_all) {
2948 if (*a->limiting) {
2949 if (a->limit_match)
2950 a->commits_needed--;
2951 } else
2952 a->commits_needed--;
2955 errcode = pthread_mutex_lock(&tog_mutex);
2956 if (errcode) {
2957 err = got_error_set_errno(errcode,
2958 "pthread_mutex_lock");
2959 goto done;
2960 } else if (*a->quit)
2961 done = 1;
2962 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2963 *a->first_displayed_entry =
2964 TAILQ_FIRST(&a->limit_commits->head);
2965 *a->selected_entry = *a->first_displayed_entry;
2966 } else if (*a->first_displayed_entry == NULL) {
2967 *a->first_displayed_entry =
2968 TAILQ_FIRST(&a->real_commits->head);
2969 *a->selected_entry = *a->first_displayed_entry;
2972 errcode = pthread_cond_signal(&a->commit_loaded);
2973 if (errcode) {
2974 err = got_error_set_errno(errcode,
2975 "pthread_cond_signal");
2976 pthread_mutex_unlock(&tog_mutex);
2977 goto done;
2980 if (done)
2981 a->commits_needed = 0;
2982 else {
2983 if (a->commits_needed == 0 && !a->load_all) {
2984 errcode = pthread_cond_wait(&a->need_commits,
2985 &tog_mutex);
2986 if (errcode) {
2987 err = got_error_set_errno(errcode,
2988 "pthread_cond_wait");
2989 pthread_mutex_unlock(&tog_mutex);
2990 goto done;
2992 if (*a->quit)
2993 done = 1;
2997 a->log_complete = 1;
2998 errcode = pthread_mutex_unlock(&tog_mutex);
2999 if (errcode)
3000 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3001 done:
3002 if (err) {
3003 tog_thread_error = 1;
3004 pthread_cond_signal(&a->commit_loaded);
3006 return (void *)err;
3009 static const struct got_error *
3010 stop_log_thread(struct tog_log_view_state *s)
3012 const struct got_error *err = NULL, *thread_err = NULL;
3013 int errcode;
3015 if (s->thread) {
3016 s->quit = 1;
3017 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3018 if (errcode)
3019 return got_error_set_errno(errcode,
3020 "pthread_cond_signal");
3021 errcode = pthread_mutex_unlock(&tog_mutex);
3022 if (errcode)
3023 return got_error_set_errno(errcode,
3024 "pthread_mutex_unlock");
3025 errcode = pthread_join(s->thread, (void **)&thread_err);
3026 if (errcode)
3027 return got_error_set_errno(errcode, "pthread_join");
3028 errcode = pthread_mutex_lock(&tog_mutex);
3029 if (errcode)
3030 return got_error_set_errno(errcode,
3031 "pthread_mutex_lock");
3032 s->thread = NULL;
3035 if (s->thread_args.repo) {
3036 err = got_repo_close(s->thread_args.repo);
3037 s->thread_args.repo = NULL;
3040 if (s->thread_args.pack_fds) {
3041 const struct got_error *pack_err =
3042 got_repo_pack_fds_close(s->thread_args.pack_fds);
3043 if (err == NULL)
3044 err = pack_err;
3045 s->thread_args.pack_fds = NULL;
3048 if (s->thread_args.graph) {
3049 got_commit_graph_close(s->thread_args.graph);
3050 s->thread_args.graph = NULL;
3053 return err ? err : thread_err;
3056 static const struct got_error *
3057 close_log_view(struct tog_view *view)
3059 const struct got_error *err = NULL;
3060 struct tog_log_view_state *s = &view->state.log;
3061 int errcode;
3063 err = stop_log_thread(s);
3065 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3066 if (errcode && err == NULL)
3067 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3069 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3070 if (errcode && err == NULL)
3071 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3073 free_commits(&s->limit_commits);
3074 free_commits(&s->real_commits);
3075 free(s->in_repo_path);
3076 s->in_repo_path = NULL;
3077 free(s->start_id);
3078 s->start_id = NULL;
3079 free(s->head_ref_name);
3080 s->head_ref_name = NULL;
3081 return err;
3085 * We use two queues to implement the limit feature: first consists of
3086 * commits matching the current limit_regex; second is the real queue
3087 * of all known commits (real_commits). When the user starts limiting,
3088 * we swap queues such that all movement and displaying functionality
3089 * works with very slight change.
3091 static const struct got_error *
3092 limit_log_view(struct tog_view *view)
3094 struct tog_log_view_state *s = &view->state.log;
3095 struct commit_queue_entry *entry;
3096 struct tog_view *v = view;
3097 const struct got_error *err = NULL;
3098 char pattern[1024];
3099 int ret;
3101 if (view_is_hsplit_top(view))
3102 v = view->child;
3103 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3104 v = view->parent;
3106 /* Get the pattern */
3107 wmove(v->window, v->nlines - 1, 0);
3108 wclrtoeol(v->window);
3109 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3110 nodelay(v->window, FALSE);
3111 nocbreak();
3112 echo();
3113 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3114 cbreak();
3115 noecho();
3116 nodelay(v->window, TRUE);
3117 if (ret == ERR)
3118 return NULL;
3120 if (*pattern == '\0') {
3122 * Safety measure for the situation where the user
3123 * resets limit without previously limiting anything.
3125 if (!s->limit_view)
3126 return NULL;
3129 * User could have pressed Ctrl+L, which refreshed the
3130 * commit queues, it means we can't save previously
3131 * (before limit took place) displayed entries,
3132 * because they would point to already free'ed memory,
3133 * so we are forced to always select first entry of
3134 * the queue.
3136 s->commits = &s->real_commits;
3137 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3138 s->selected_entry = s->first_displayed_entry;
3139 s->selected = 0;
3140 s->limit_view = 0;
3142 return NULL;
3145 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3146 return NULL;
3148 s->limit_view = 1;
3150 /* Clear the screen while loading limit view */
3151 s->first_displayed_entry = NULL;
3152 s->last_displayed_entry = NULL;
3153 s->selected_entry = NULL;
3154 s->commits = &s->limit_commits;
3156 /* Prepare limit queue for new search */
3157 free_commits(&s->limit_commits);
3158 s->limit_commits.ncommits = 0;
3160 /* First process commits, which are in queue already */
3161 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3162 int have_match = 0;
3164 err = match_commit(&have_match, entry->id,
3165 entry->commit, &s->limit_regex);
3166 if (err)
3167 return err;
3169 if (have_match) {
3170 struct commit_queue_entry *matched;
3172 matched = alloc_commit_queue_entry(entry->commit,
3173 entry->id);
3174 if (matched == NULL) {
3175 err = got_error_from_errno(
3176 "alloc_commit_queue_entry");
3177 break;
3179 matched->commit = entry->commit;
3180 got_object_commit_retain(entry->commit);
3182 matched->idx = s->limit_commits.ncommits;
3183 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3184 matched, entry);
3185 s->limit_commits.ncommits++;
3189 /* Second process all the commits, until we fill the screen */
3190 if (s->limit_commits.ncommits < view->nlines - 1 &&
3191 !s->thread_args.log_complete) {
3192 s->thread_args.commits_needed +=
3193 view->nlines - s->limit_commits.ncommits - 1;
3194 err = trigger_log_thread(view, 1);
3195 if (err)
3196 return err;
3199 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3200 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3201 s->selected = 0;
3203 return NULL;
3206 static const struct got_error *
3207 search_start_log_view(struct tog_view *view)
3209 struct tog_log_view_state *s = &view->state.log;
3211 s->matched_entry = NULL;
3212 s->search_entry = NULL;
3213 return NULL;
3216 static const struct got_error *
3217 search_next_log_view(struct tog_view *view)
3219 const struct got_error *err = NULL;
3220 struct tog_log_view_state *s = &view->state.log;
3221 struct commit_queue_entry *entry;
3223 /* Display progress update in log view. */
3224 show_log_view(view);
3225 update_panels();
3226 doupdate();
3228 if (s->search_entry) {
3229 int errcode, ch;
3230 errcode = pthread_mutex_unlock(&tog_mutex);
3231 if (errcode)
3232 return got_error_set_errno(errcode,
3233 "pthread_mutex_unlock");
3234 ch = wgetch(view->window);
3235 errcode = pthread_mutex_lock(&tog_mutex);
3236 if (errcode)
3237 return got_error_set_errno(errcode,
3238 "pthread_mutex_lock");
3239 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3240 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3241 return NULL;
3243 if (view->searching == TOG_SEARCH_FORWARD)
3244 entry = TAILQ_NEXT(s->search_entry, entry);
3245 else
3246 entry = TAILQ_PREV(s->search_entry,
3247 commit_queue_head, entry);
3248 } else if (s->matched_entry) {
3250 * If the user has moved the cursor after we hit a match,
3251 * the position from where we should continue searching
3252 * might have changed.
3254 if (view->searching == TOG_SEARCH_FORWARD)
3255 entry = TAILQ_NEXT(s->selected_entry, entry);
3256 else
3257 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3258 entry);
3259 } else {
3260 entry = s->selected_entry;
3263 while (1) {
3264 int have_match = 0;
3266 if (entry == NULL) {
3267 if (s->thread_args.log_complete ||
3268 view->searching == TOG_SEARCH_BACKWARD) {
3269 view->search_next_done =
3270 (s->matched_entry == NULL ?
3271 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3272 s->search_entry = NULL;
3273 return NULL;
3276 * Poke the log thread for more commits and return,
3277 * allowing the main loop to make progress. Search
3278 * will resume at s->search_entry once we come back.
3280 s->thread_args.commits_needed++;
3281 return trigger_log_thread(view, 0);
3284 err = match_commit(&have_match, entry->id, entry->commit,
3285 &view->regex);
3286 if (err)
3287 break;
3288 if (have_match) {
3289 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3290 s->matched_entry = entry;
3291 break;
3294 s->search_entry = entry;
3295 if (view->searching == TOG_SEARCH_FORWARD)
3296 entry = TAILQ_NEXT(entry, entry);
3297 else
3298 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3301 if (s->matched_entry) {
3302 int cur = s->selected_entry->idx;
3303 while (cur < s->matched_entry->idx) {
3304 err = input_log_view(NULL, view, KEY_DOWN);
3305 if (err)
3306 return err;
3307 cur++;
3309 while (cur > s->matched_entry->idx) {
3310 err = input_log_view(NULL, view, KEY_UP);
3311 if (err)
3312 return err;
3313 cur--;
3317 s->search_entry = NULL;
3319 return NULL;
3322 static const struct got_error *
3323 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3324 struct got_repository *repo, const char *head_ref_name,
3325 const char *in_repo_path, int log_branches)
3327 const struct got_error *err = NULL;
3328 struct tog_log_view_state *s = &view->state.log;
3329 struct got_repository *thread_repo = NULL;
3330 struct got_commit_graph *thread_graph = NULL;
3331 int errcode;
3333 if (in_repo_path != s->in_repo_path) {
3334 free(s->in_repo_path);
3335 s->in_repo_path = strdup(in_repo_path);
3336 if (s->in_repo_path == NULL)
3337 return got_error_from_errno("strdup");
3340 /* The commit queue only contains commits being displayed. */
3341 TAILQ_INIT(&s->real_commits.head);
3342 s->real_commits.ncommits = 0;
3343 s->commits = &s->real_commits;
3345 TAILQ_INIT(&s->limit_commits.head);
3346 s->limit_view = 0;
3347 s->limit_commits.ncommits = 0;
3349 s->repo = repo;
3350 if (head_ref_name) {
3351 s->head_ref_name = strdup(head_ref_name);
3352 if (s->head_ref_name == NULL) {
3353 err = got_error_from_errno("strdup");
3354 goto done;
3357 s->start_id = got_object_id_dup(start_id);
3358 if (s->start_id == NULL) {
3359 err = got_error_from_errno("got_object_id_dup");
3360 goto done;
3362 s->log_branches = log_branches;
3364 STAILQ_INIT(&s->colors);
3365 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3366 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3367 get_color_value("TOG_COLOR_COMMIT"));
3368 if (err)
3369 goto done;
3370 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3371 get_color_value("TOG_COLOR_AUTHOR"));
3372 if (err) {
3373 free_colors(&s->colors);
3374 goto done;
3376 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3377 get_color_value("TOG_COLOR_DATE"));
3378 if (err) {
3379 free_colors(&s->colors);
3380 goto done;
3384 view->show = show_log_view;
3385 view->input = input_log_view;
3386 view->resize = resize_log_view;
3387 view->close = close_log_view;
3388 view->search_start = search_start_log_view;
3389 view->search_next = search_next_log_view;
3391 if (s->thread_args.pack_fds == NULL) {
3392 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3393 if (err)
3394 goto done;
3396 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3397 s->thread_args.pack_fds);
3398 if (err)
3399 goto done;
3400 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3401 !s->log_branches);
3402 if (err)
3403 goto done;
3404 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3405 s->repo, NULL, NULL);
3406 if (err)
3407 goto done;
3409 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3410 if (errcode) {
3411 err = got_error_set_errno(errcode, "pthread_cond_init");
3412 goto done;
3414 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3415 if (errcode) {
3416 err = got_error_set_errno(errcode, "pthread_cond_init");
3417 goto done;
3420 s->thread_args.commits_needed = view->nlines;
3421 s->thread_args.graph = thread_graph;
3422 s->thread_args.real_commits = &s->real_commits;
3423 s->thread_args.limit_commits = &s->limit_commits;
3424 s->thread_args.in_repo_path = s->in_repo_path;
3425 s->thread_args.start_id = s->start_id;
3426 s->thread_args.repo = thread_repo;
3427 s->thread_args.log_complete = 0;
3428 s->thread_args.quit = &s->quit;
3429 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3430 s->thread_args.selected_entry = &s->selected_entry;
3431 s->thread_args.searching = &view->searching;
3432 s->thread_args.search_next_done = &view->search_next_done;
3433 s->thread_args.regex = &view->regex;
3434 s->thread_args.limiting = &s->limit_view;
3435 s->thread_args.limit_regex = &s->limit_regex;
3436 s->thread_args.limit_commits = &s->limit_commits;
3437 done:
3438 if (err)
3439 close_log_view(view);
3440 return err;
3443 static const struct got_error *
3444 show_log_view(struct tog_view *view)
3446 const struct got_error *err;
3447 struct tog_log_view_state *s = &view->state.log;
3449 if (s->thread == NULL) {
3450 int errcode = pthread_create(&s->thread, NULL, log_thread,
3451 &s->thread_args);
3452 if (errcode)
3453 return got_error_set_errno(errcode, "pthread_create");
3454 if (s->thread_args.commits_needed > 0) {
3455 err = trigger_log_thread(view, 1);
3456 if (err)
3457 return err;
3461 return draw_commits(view);
3464 static void
3465 log_move_cursor_up(struct tog_view *view, int page, int home)
3467 struct tog_log_view_state *s = &view->state.log;
3469 if (s->first_displayed_entry == NULL)
3470 return;
3471 if (s->selected_entry->idx == 0)
3472 view->count = 0;
3474 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3475 || home)
3476 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3478 if (!page && !home && s->selected > 0)
3479 --s->selected;
3480 else
3481 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3483 select_commit(s);
3484 return;
3487 static const struct got_error *
3488 log_move_cursor_down(struct tog_view *view, int page)
3490 struct tog_log_view_state *s = &view->state.log;
3491 const struct got_error *err = NULL;
3492 int eos = view->nlines - 2;
3494 if (s->first_displayed_entry == NULL)
3495 return NULL;
3497 if (s->thread_args.log_complete &&
3498 s->selected_entry->idx >= s->commits->ncommits - 1)
3499 return NULL;
3501 if (view_is_hsplit_top(view))
3502 --eos; /* border consumes the last line */
3504 if (!page) {
3505 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3506 ++s->selected;
3507 else
3508 err = log_scroll_down(view, 1);
3509 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3510 struct commit_queue_entry *entry;
3511 int n;
3513 s->selected = 0;
3514 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3515 s->last_displayed_entry = entry;
3516 for (n = 0; n <= eos; n++) {
3517 if (entry == NULL)
3518 break;
3519 s->first_displayed_entry = entry;
3520 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3522 if (n > 0)
3523 s->selected = n - 1;
3524 } else {
3525 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3526 s->thread_args.log_complete)
3527 s->selected += MIN(page,
3528 s->commits->ncommits - s->selected_entry->idx - 1);
3529 else
3530 err = log_scroll_down(view, page);
3532 if (err)
3533 return err;
3536 * We might necessarily overshoot in horizontal
3537 * splits; if so, select the last displayed commit.
3539 if (s->first_displayed_entry && s->last_displayed_entry) {
3540 s->selected = MIN(s->selected,
3541 s->last_displayed_entry->idx -
3542 s->first_displayed_entry->idx);
3545 select_commit(s);
3547 if (s->thread_args.log_complete &&
3548 s->selected_entry->idx == s->commits->ncommits - 1)
3549 view->count = 0;
3551 return NULL;
3554 static void
3555 view_get_split(struct tog_view *view, int *y, int *x)
3557 *x = 0;
3558 *y = 0;
3560 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3561 if (view->child && view->child->resized_y)
3562 *y = view->child->resized_y;
3563 else if (view->resized_y)
3564 *y = view->resized_y;
3565 else
3566 *y = view_split_begin_y(view->lines);
3567 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3568 if (view->child && view->child->resized_x)
3569 *x = view->child->resized_x;
3570 else if (view->resized_x)
3571 *x = view->resized_x;
3572 else
3573 *x = view_split_begin_x(view->begin_x);
3577 /* Split view horizontally at y and offset view->state->selected line. */
3578 static const struct got_error *
3579 view_init_hsplit(struct tog_view *view, int y)
3581 const struct got_error *err = NULL;
3583 view->nlines = y;
3584 view->ncols = COLS;
3585 err = view_resize(view);
3586 if (err)
3587 return err;
3589 err = offset_selection_down(view);
3591 return err;
3594 static const struct got_error *
3595 log_goto_line(struct tog_view *view, int nlines)
3597 const struct got_error *err = NULL;
3598 struct tog_log_view_state *s = &view->state.log;
3599 int g, idx = s->selected_entry->idx;
3601 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3602 return NULL;
3604 g = view->gline;
3605 view->gline = 0;
3607 if (g >= s->first_displayed_entry->idx + 1 &&
3608 g <= s->last_displayed_entry->idx + 1 &&
3609 g - s->first_displayed_entry->idx - 1 < nlines) {
3610 s->selected = g - s->first_displayed_entry->idx - 1;
3611 select_commit(s);
3612 return NULL;
3615 if (idx + 1 < g) {
3616 err = log_move_cursor_down(view, g - idx - 1);
3617 if (!err && g > s->selected_entry->idx + 1)
3618 err = log_move_cursor_down(view,
3619 g - s->first_displayed_entry->idx - 1);
3620 if (err)
3621 return err;
3622 } else if (idx + 1 > g)
3623 log_move_cursor_up(view, idx - g + 1, 0);
3625 if (g < nlines && s->first_displayed_entry->idx == 0)
3626 s->selected = g - 1;
3628 select_commit(s);
3629 return NULL;
3633 static const struct got_error *
3634 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3636 const struct got_error *err = NULL;
3637 struct tog_log_view_state *s = &view->state.log;
3638 int eos, nscroll;
3640 if (s->thread_args.load_all) {
3641 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3642 s->thread_args.load_all = 0;
3643 else if (s->thread_args.log_complete) {
3644 err = log_move_cursor_down(view, s->commits->ncommits);
3645 s->thread_args.load_all = 0;
3647 if (err)
3648 return err;
3651 eos = nscroll = view->nlines - 1;
3652 if (view_is_hsplit_top(view))
3653 --eos; /* border */
3655 if (view->gline)
3656 return log_goto_line(view, eos);
3658 switch (ch) {
3659 case '&':
3660 err = limit_log_view(view);
3661 break;
3662 case 'q':
3663 s->quit = 1;
3664 break;
3665 case '0':
3666 view->x = 0;
3667 break;
3668 case '$':
3669 view->x = MAX(view->maxx - view->ncols / 2, 0);
3670 view->count = 0;
3671 break;
3672 case KEY_RIGHT:
3673 case 'l':
3674 if (view->x + view->ncols / 2 < view->maxx)
3675 view->x += 2; /* move two columns right */
3676 else
3677 view->count = 0;
3678 break;
3679 case KEY_LEFT:
3680 case 'h':
3681 view->x -= MIN(view->x, 2); /* move two columns back */
3682 if (view->x <= 0)
3683 view->count = 0;
3684 break;
3685 case 'k':
3686 case KEY_UP:
3687 case '<':
3688 case ',':
3689 case CTRL('p'):
3690 log_move_cursor_up(view, 0, 0);
3691 break;
3692 case 'g':
3693 case KEY_HOME:
3694 log_move_cursor_up(view, 0, 1);
3695 view->count = 0;
3696 break;
3697 case CTRL('u'):
3698 case 'u':
3699 nscroll /= 2;
3700 /* FALL THROUGH */
3701 case KEY_PPAGE:
3702 case CTRL('b'):
3703 case 'b':
3704 log_move_cursor_up(view, nscroll, 0);
3705 break;
3706 case 'j':
3707 case KEY_DOWN:
3708 case '>':
3709 case '.':
3710 case CTRL('n'):
3711 err = log_move_cursor_down(view, 0);
3712 break;
3713 case '@':
3714 s->use_committer = !s->use_committer;
3715 break;
3716 case 'G':
3717 case KEY_END: {
3718 /* We don't know yet how many commits, so we're forced to
3719 * traverse them all. */
3720 view->count = 0;
3721 s->thread_args.load_all = 1;
3722 if (!s->thread_args.log_complete)
3723 return trigger_log_thread(view, 0);
3724 err = log_move_cursor_down(view, s->commits->ncommits);
3725 s->thread_args.load_all = 0;
3726 break;
3728 case CTRL('d'):
3729 case 'd':
3730 nscroll /= 2;
3731 /* FALL THROUGH */
3732 case KEY_NPAGE:
3733 case CTRL('f'):
3734 case 'f':
3735 case ' ':
3736 err = log_move_cursor_down(view, nscroll);
3737 break;
3738 case KEY_RESIZE:
3739 if (s->selected > view->nlines - 2)
3740 s->selected = view->nlines - 2;
3741 if (s->selected > s->commits->ncommits - 1)
3742 s->selected = s->commits->ncommits - 1;
3743 select_commit(s);
3744 if (s->commits->ncommits < view->nlines - 1 &&
3745 !s->thread_args.log_complete) {
3746 s->thread_args.commits_needed += (view->nlines - 1) -
3747 s->commits->ncommits;
3748 err = trigger_log_thread(view, 1);
3750 break;
3751 case KEY_ENTER:
3752 case '\r':
3753 view->count = 0;
3754 if (s->selected_entry == NULL)
3755 break;
3756 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3757 break;
3758 case 'T':
3759 view->count = 0;
3760 if (s->selected_entry == NULL)
3761 break;
3762 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3763 break;
3764 case KEY_BACKSPACE:
3765 case CTRL('l'):
3766 case 'B':
3767 view->count = 0;
3768 if (ch == KEY_BACKSPACE &&
3769 got_path_is_root_dir(s->in_repo_path))
3770 break;
3771 err = stop_log_thread(s);
3772 if (err)
3773 return err;
3774 if (ch == KEY_BACKSPACE) {
3775 char *parent_path;
3776 err = got_path_dirname(&parent_path, s->in_repo_path);
3777 if (err)
3778 return err;
3779 free(s->in_repo_path);
3780 s->in_repo_path = parent_path;
3781 s->thread_args.in_repo_path = s->in_repo_path;
3782 } else if (ch == CTRL('l')) {
3783 struct got_object_id *start_id;
3784 err = got_repo_match_object_id(&start_id, NULL,
3785 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3786 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3787 if (err) {
3788 if (s->head_ref_name == NULL ||
3789 err->code != GOT_ERR_NOT_REF)
3790 return err;
3791 /* Try to cope with deleted references. */
3792 free(s->head_ref_name);
3793 s->head_ref_name = NULL;
3794 err = got_repo_match_object_id(&start_id,
3795 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3796 &tog_refs, s->repo);
3797 if (err)
3798 return err;
3800 free(s->start_id);
3801 s->start_id = start_id;
3802 s->thread_args.start_id = s->start_id;
3803 } else /* 'B' */
3804 s->log_branches = !s->log_branches;
3806 if (s->thread_args.pack_fds == NULL) {
3807 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3808 if (err)
3809 return err;
3811 err = got_repo_open(&s->thread_args.repo,
3812 got_repo_get_path(s->repo), NULL,
3813 s->thread_args.pack_fds);
3814 if (err)
3815 return err;
3816 tog_free_refs();
3817 err = tog_load_refs(s->repo, 0);
3818 if (err)
3819 return err;
3820 err = got_commit_graph_open(&s->thread_args.graph,
3821 s->in_repo_path, !s->log_branches);
3822 if (err)
3823 return err;
3824 err = got_commit_graph_iter_start(s->thread_args.graph,
3825 s->start_id, s->repo, NULL, NULL);
3826 if (err)
3827 return err;
3828 free_commits(&s->real_commits);
3829 free_commits(&s->limit_commits);
3830 s->first_displayed_entry = NULL;
3831 s->last_displayed_entry = NULL;
3832 s->selected_entry = NULL;
3833 s->selected = 0;
3834 s->thread_args.log_complete = 0;
3835 s->quit = 0;
3836 s->thread_args.commits_needed = view->lines;
3837 s->matched_entry = NULL;
3838 s->search_entry = NULL;
3839 view->offset = 0;
3840 break;
3841 case 'R':
3842 view->count = 0;
3843 err = view_request_new(new_view, view, TOG_VIEW_REF);
3844 break;
3845 default:
3846 view->count = 0;
3847 break;
3850 return err;
3853 static const struct got_error *
3854 apply_unveil(const char *repo_path, const char *worktree_path)
3856 const struct got_error *error;
3858 #ifdef PROFILE
3859 if (unveil("gmon.out", "rwc") != 0)
3860 return got_error_from_errno2("unveil", "gmon.out");
3861 #endif
3862 if (repo_path && unveil(repo_path, "r") != 0)
3863 return got_error_from_errno2("unveil", repo_path);
3865 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3866 return got_error_from_errno2("unveil", worktree_path);
3868 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3869 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3871 error = got_privsep_unveil_exec_helpers();
3872 if (error != NULL)
3873 return error;
3875 if (unveil(NULL, NULL) != 0)
3876 return got_error_from_errno("unveil");
3878 return NULL;
3881 static void
3882 init_curses(void)
3885 * Override default signal handlers before starting ncurses.
3886 * This should prevent ncurses from installing its own
3887 * broken cleanup() signal handler.
3889 signal(SIGWINCH, tog_sigwinch);
3890 signal(SIGPIPE, tog_sigpipe);
3891 signal(SIGCONT, tog_sigcont);
3892 signal(SIGINT, tog_sigint);
3893 signal(SIGTERM, tog_sigterm);
3895 initscr();
3896 cbreak();
3897 halfdelay(1); /* Do fast refresh while initial view is loading. */
3898 noecho();
3899 nonl();
3900 intrflush(stdscr, FALSE);
3901 keypad(stdscr, TRUE);
3902 curs_set(0);
3903 if (getenv("TOG_COLORS") != NULL) {
3904 start_color();
3905 use_default_colors();
3909 static const struct got_error *
3910 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3911 struct got_repository *repo, struct got_worktree *worktree)
3913 const struct got_error *err = NULL;
3915 if (argc == 0) {
3916 *in_repo_path = strdup("/");
3917 if (*in_repo_path == NULL)
3918 return got_error_from_errno("strdup");
3919 return NULL;
3922 if (worktree) {
3923 const char *prefix = got_worktree_get_path_prefix(worktree);
3924 char *p;
3926 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3927 if (err)
3928 return err;
3929 if (asprintf(in_repo_path, "%s%s%s", prefix,
3930 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3931 p) == -1) {
3932 err = got_error_from_errno("asprintf");
3933 *in_repo_path = NULL;
3935 free(p);
3936 } else
3937 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3939 return err;
3942 static const struct got_error *
3943 cmd_log(int argc, char *argv[])
3945 const struct got_error *error;
3946 struct got_repository *repo = NULL;
3947 struct got_worktree *worktree = NULL;
3948 struct got_object_id *start_id = NULL;
3949 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3950 char *start_commit = NULL, *label = NULL;
3951 struct got_reference *ref = NULL;
3952 const char *head_ref_name = NULL;
3953 int ch, log_branches = 0;
3954 struct tog_view *view;
3955 int *pack_fds = NULL;
3957 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3958 switch (ch) {
3959 case 'b':
3960 log_branches = 1;
3961 break;
3962 case 'c':
3963 start_commit = optarg;
3964 break;
3965 case 'r':
3966 repo_path = realpath(optarg, NULL);
3967 if (repo_path == NULL)
3968 return got_error_from_errno2("realpath",
3969 optarg);
3970 break;
3971 default:
3972 usage_log();
3973 /* NOTREACHED */
3977 argc -= optind;
3978 argv += optind;
3980 if (argc > 1)
3981 usage_log();
3983 error = got_repo_pack_fds_open(&pack_fds);
3984 if (error != NULL)
3985 goto done;
3987 if (repo_path == NULL) {
3988 cwd = getcwd(NULL, 0);
3989 if (cwd == NULL)
3990 return got_error_from_errno("getcwd");
3991 error = got_worktree_open(&worktree, cwd);
3992 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3993 goto done;
3994 if (worktree)
3995 repo_path =
3996 strdup(got_worktree_get_repo_path(worktree));
3997 else
3998 repo_path = strdup(cwd);
3999 if (repo_path == NULL) {
4000 error = got_error_from_errno("strdup");
4001 goto done;
4005 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4006 if (error != NULL)
4007 goto done;
4009 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4010 repo, worktree);
4011 if (error)
4012 goto done;
4014 init_curses();
4016 error = apply_unveil(got_repo_get_path(repo),
4017 worktree ? got_worktree_get_root_path(worktree) : NULL);
4018 if (error)
4019 goto done;
4021 /* already loaded by tog_log_with_path()? */
4022 if (TAILQ_EMPTY(&tog_refs)) {
4023 error = tog_load_refs(repo, 0);
4024 if (error)
4025 goto done;
4028 if (start_commit == NULL) {
4029 error = got_repo_match_object_id(&start_id, &label,
4030 worktree ? got_worktree_get_head_ref_name(worktree) :
4031 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4032 if (error)
4033 goto done;
4034 head_ref_name = label;
4035 } else {
4036 error = got_ref_open(&ref, repo, start_commit, 0);
4037 if (error == NULL)
4038 head_ref_name = got_ref_get_name(ref);
4039 else if (error->code != GOT_ERR_NOT_REF)
4040 goto done;
4041 error = got_repo_match_object_id(&start_id, NULL,
4042 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4043 if (error)
4044 goto done;
4047 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4048 if (view == NULL) {
4049 error = got_error_from_errno("view_open");
4050 goto done;
4052 error = open_log_view(view, start_id, repo, head_ref_name,
4053 in_repo_path, log_branches);
4054 if (error)
4055 goto done;
4056 if (worktree) {
4057 /* Release work tree lock. */
4058 got_worktree_close(worktree);
4059 worktree = NULL;
4061 error = view_loop(view);
4062 done:
4063 free(in_repo_path);
4064 free(repo_path);
4065 free(cwd);
4066 free(start_id);
4067 free(label);
4068 if (ref)
4069 got_ref_close(ref);
4070 if (repo) {
4071 const struct got_error *close_err = got_repo_close(repo);
4072 if (error == NULL)
4073 error = close_err;
4075 if (worktree)
4076 got_worktree_close(worktree);
4077 if (pack_fds) {
4078 const struct got_error *pack_err =
4079 got_repo_pack_fds_close(pack_fds);
4080 if (error == NULL)
4081 error = pack_err;
4083 tog_free_refs();
4084 return error;
4087 __dead static void
4088 usage_diff(void)
4090 endwin();
4091 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4092 "object1 object2\n", getprogname());
4093 exit(1);
4096 static int
4097 match_line(const char *line, regex_t *regex, size_t nmatch,
4098 regmatch_t *regmatch)
4100 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4103 static struct tog_color *
4104 match_color(struct tog_colors *colors, const char *line)
4106 struct tog_color *tc = NULL;
4108 STAILQ_FOREACH(tc, colors, entry) {
4109 if (match_line(line, &tc->regex, 0, NULL))
4110 return tc;
4113 return NULL;
4116 static const struct got_error *
4117 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4118 WINDOW *window, int skipcol, regmatch_t *regmatch)
4120 const struct got_error *err = NULL;
4121 char *exstr = NULL;
4122 wchar_t *wline = NULL;
4123 int rme, rms, n, width, scrollx;
4124 int width0 = 0, width1 = 0, width2 = 0;
4125 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4127 *wtotal = 0;
4129 rms = regmatch->rm_so;
4130 rme = regmatch->rm_eo;
4132 err = expand_tab(&exstr, line);
4133 if (err)
4134 return err;
4136 /* Split the line into 3 segments, according to match offsets. */
4137 seg0 = strndup(exstr, rms);
4138 if (seg0 == NULL) {
4139 err = got_error_from_errno("strndup");
4140 goto done;
4142 seg1 = strndup(exstr + rms, rme - rms);
4143 if (seg1 == NULL) {
4144 err = got_error_from_errno("strndup");
4145 goto done;
4147 seg2 = strdup(exstr + rme);
4148 if (seg2 == NULL) {
4149 err = got_error_from_errno("strndup");
4150 goto done;
4153 /* draw up to matched token if we haven't scrolled past it */
4154 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4155 col_tab_align, 1);
4156 if (err)
4157 goto done;
4158 n = MAX(width0 - skipcol, 0);
4159 if (n) {
4160 free(wline);
4161 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4162 wlimit, col_tab_align, 1);
4163 if (err)
4164 goto done;
4165 waddwstr(window, &wline[scrollx]);
4166 wlimit -= width;
4167 *wtotal += width;
4170 if (wlimit > 0) {
4171 int i = 0, w = 0;
4172 size_t wlen;
4174 free(wline);
4175 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4176 col_tab_align, 1);
4177 if (err)
4178 goto done;
4179 wlen = wcslen(wline);
4180 while (i < wlen) {
4181 width = wcwidth(wline[i]);
4182 if (width == -1) {
4183 /* should not happen, tabs are expanded */
4184 err = got_error(GOT_ERR_RANGE);
4185 goto done;
4187 if (width0 + w + width > skipcol)
4188 break;
4189 w += width;
4190 i++;
4192 /* draw (visible part of) matched token (if scrolled into it) */
4193 if (width1 - w > 0) {
4194 wattron(window, A_STANDOUT);
4195 waddwstr(window, &wline[i]);
4196 wattroff(window, A_STANDOUT);
4197 wlimit -= (width1 - w);
4198 *wtotal += (width1 - w);
4202 if (wlimit > 0) { /* draw rest of line */
4203 free(wline);
4204 if (skipcol > width0 + width1) {
4205 err = format_line(&wline, &width2, &scrollx, seg2,
4206 skipcol - (width0 + width1), wlimit,
4207 col_tab_align, 1);
4208 if (err)
4209 goto done;
4210 waddwstr(window, &wline[scrollx]);
4211 } else {
4212 err = format_line(&wline, &width2, NULL, seg2, 0,
4213 wlimit, col_tab_align, 1);
4214 if (err)
4215 goto done;
4216 waddwstr(window, wline);
4218 *wtotal += width2;
4220 done:
4221 free(wline);
4222 free(exstr);
4223 free(seg0);
4224 free(seg1);
4225 free(seg2);
4226 return err;
4229 static int
4230 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4232 FILE *f = NULL;
4233 int *eof, *first, *selected;
4235 if (view->type == TOG_VIEW_DIFF) {
4236 struct tog_diff_view_state *s = &view->state.diff;
4238 first = &s->first_displayed_line;
4239 selected = first;
4240 eof = &s->eof;
4241 f = s->f;
4242 } else if (view->type == TOG_VIEW_HELP) {
4243 struct tog_help_view_state *s = &view->state.help;
4245 first = &s->first_displayed_line;
4246 selected = first;
4247 eof = &s->eof;
4248 f = s->f;
4249 } else if (view->type == TOG_VIEW_BLAME) {
4250 struct tog_blame_view_state *s = &view->state.blame;
4252 first = &s->first_displayed_line;
4253 selected = &s->selected_line;
4254 eof = &s->eof;
4255 f = s->blame.f;
4256 } else
4257 return 0;
4259 /* Center gline in the middle of the page like vi(1). */
4260 if (*lineno < view->gline - (view->nlines - 3) / 2)
4261 return 0;
4262 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4263 rewind(f);
4264 *eof = 0;
4265 *first = 1;
4266 *lineno = 0;
4267 *nprinted = 0;
4268 return 0;
4271 *selected = view->gline <= (view->nlines - 3) / 2 ?
4272 view->gline : (view->nlines - 3) / 2 + 1;
4273 view->gline = 0;
4275 return 1;
4278 static const struct got_error *
4279 draw_file(struct tog_view *view, const char *header)
4281 struct tog_diff_view_state *s = &view->state.diff;
4282 regmatch_t *regmatch = &view->regmatch;
4283 const struct got_error *err;
4284 int nprinted = 0;
4285 char *line;
4286 size_t linesize = 0;
4287 ssize_t linelen;
4288 wchar_t *wline;
4289 int width;
4290 int max_lines = view->nlines;
4291 int nlines = s->nlines;
4292 off_t line_offset;
4294 s->lineno = s->first_displayed_line - 1;
4295 line_offset = s->lines[s->first_displayed_line - 1].offset;
4296 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4297 return got_error_from_errno("fseek");
4299 werase(view->window);
4301 if (view->gline > s->nlines - 1)
4302 view->gline = s->nlines - 1;
4304 if (header) {
4305 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4306 1 : view->gline - (view->nlines - 3) / 2 :
4307 s->lineno + s->selected_line;
4309 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4310 return got_error_from_errno("asprintf");
4311 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4312 0, 0);
4313 free(line);
4314 if (err)
4315 return err;
4317 if (view_needs_focus_indication(view))
4318 wstandout(view->window);
4319 waddwstr(view->window, wline);
4320 free(wline);
4321 wline = NULL;
4322 while (width++ < view->ncols)
4323 waddch(view->window, ' ');
4324 if (view_needs_focus_indication(view))
4325 wstandend(view->window);
4327 if (max_lines <= 1)
4328 return NULL;
4329 max_lines--;
4332 s->eof = 0;
4333 view->maxx = 0;
4334 line = NULL;
4335 while (max_lines > 0 && nprinted < max_lines) {
4336 enum got_diff_line_type linetype;
4337 attr_t attr = 0;
4339 linelen = getline(&line, &linesize, s->f);
4340 if (linelen == -1) {
4341 if (feof(s->f)) {
4342 s->eof = 1;
4343 break;
4345 free(line);
4346 return got_ferror(s->f, GOT_ERR_IO);
4349 if (++s->lineno < s->first_displayed_line)
4350 continue;
4351 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4352 continue;
4353 if (s->lineno == view->hiline)
4354 attr = A_STANDOUT;
4356 /* Set view->maxx based on full line length. */
4357 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4358 view->x ? 1 : 0);
4359 if (err) {
4360 free(line);
4361 return err;
4363 view->maxx = MAX(view->maxx, width);
4364 free(wline);
4365 wline = NULL;
4367 linetype = s->lines[s->lineno].type;
4368 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4369 linetype < GOT_DIFF_LINE_CONTEXT)
4370 attr |= COLOR_PAIR(linetype);
4371 if (attr)
4372 wattron(view->window, attr);
4373 if (s->first_displayed_line + nprinted == s->matched_line &&
4374 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4375 err = add_matched_line(&width, line, view->ncols, 0,
4376 view->window, view->x, regmatch);
4377 if (err) {
4378 free(line);
4379 return err;
4381 } else {
4382 int skip;
4383 err = format_line(&wline, &width, &skip, line,
4384 view->x, view->ncols, 0, view->x ? 1 : 0);
4385 if (err) {
4386 free(line);
4387 return err;
4389 waddwstr(view->window, &wline[skip]);
4390 free(wline);
4391 wline = NULL;
4393 if (s->lineno == view->hiline) {
4394 /* highlight full gline length */
4395 while (width++ < view->ncols)
4396 waddch(view->window, ' ');
4397 } else {
4398 if (width <= view->ncols - 1)
4399 waddch(view->window, '\n');
4401 if (attr)
4402 wattroff(view->window, attr);
4403 if (++nprinted == 1)
4404 s->first_displayed_line = s->lineno;
4406 free(line);
4407 if (nprinted >= 1)
4408 s->last_displayed_line = s->first_displayed_line +
4409 (nprinted - 1);
4410 else
4411 s->last_displayed_line = s->first_displayed_line;
4413 view_border(view);
4415 if (s->eof) {
4416 while (nprinted < view->nlines) {
4417 waddch(view->window, '\n');
4418 nprinted++;
4421 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4422 view->ncols, 0, 0);
4423 if (err) {
4424 return err;
4427 wstandout(view->window);
4428 waddwstr(view->window, wline);
4429 free(wline);
4430 wline = NULL;
4431 wstandend(view->window);
4434 return NULL;
4437 static char *
4438 get_datestr(time_t *time, char *datebuf)
4440 struct tm mytm, *tm;
4441 char *p, *s;
4443 tm = gmtime_r(time, &mytm);
4444 if (tm == NULL)
4445 return NULL;
4446 s = asctime_r(tm, datebuf);
4447 if (s == NULL)
4448 return NULL;
4449 p = strchr(s, '\n');
4450 if (p)
4451 *p = '\0';
4452 return s;
4455 static const struct got_error *
4456 get_changed_paths(struct got_pathlist_head *paths,
4457 struct got_commit_object *commit, struct got_repository *repo)
4459 const struct got_error *err = NULL;
4460 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4461 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4462 struct got_object_qid *qid;
4464 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4465 if (qid != NULL) {
4466 struct got_commit_object *pcommit;
4467 err = got_object_open_as_commit(&pcommit, repo,
4468 &qid->id);
4469 if (err)
4470 return err;
4472 tree_id1 = got_object_id_dup(
4473 got_object_commit_get_tree_id(pcommit));
4474 if (tree_id1 == NULL) {
4475 got_object_commit_close(pcommit);
4476 return got_error_from_errno("got_object_id_dup");
4478 got_object_commit_close(pcommit);
4482 if (tree_id1) {
4483 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4484 if (err)
4485 goto done;
4488 tree_id2 = got_object_commit_get_tree_id(commit);
4489 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4490 if (err)
4491 goto done;
4493 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4494 got_diff_tree_collect_changed_paths, paths, 0);
4495 done:
4496 if (tree1)
4497 got_object_tree_close(tree1);
4498 if (tree2)
4499 got_object_tree_close(tree2);
4500 free(tree_id1);
4501 return err;
4504 static const struct got_error *
4505 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4506 off_t off, uint8_t type)
4508 struct got_diff_line *p;
4510 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4511 if (p == NULL)
4512 return got_error_from_errno("reallocarray");
4513 *lines = p;
4514 (*lines)[*nlines].offset = off;
4515 (*lines)[*nlines].type = type;
4516 (*nlines)++;
4518 return NULL;
4521 static const struct got_error *
4522 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4523 struct got_object_id *commit_id, struct got_reflist_head *refs,
4524 struct got_repository *repo, FILE *outfile)
4526 const struct got_error *err = NULL;
4527 char datebuf[26], *datestr;
4528 struct got_commit_object *commit;
4529 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4530 time_t committer_time;
4531 const char *author, *committer;
4532 char *refs_str = NULL;
4533 struct got_pathlist_head changed_paths;
4534 struct got_pathlist_entry *pe;
4535 off_t outoff = 0;
4536 int n;
4538 TAILQ_INIT(&changed_paths);
4540 if (refs) {
4541 err = build_refs_str(&refs_str, refs, commit_id, repo);
4542 if (err)
4543 return err;
4546 err = got_object_open_as_commit(&commit, repo, commit_id);
4547 if (err)
4548 return err;
4550 err = got_object_id_str(&id_str, commit_id);
4551 if (err) {
4552 err = got_error_from_errno("got_object_id_str");
4553 goto done;
4556 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4557 if (err)
4558 goto done;
4560 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4561 refs_str ? refs_str : "", refs_str ? ")" : "");
4562 if (n < 0) {
4563 err = got_error_from_errno("fprintf");
4564 goto done;
4566 outoff += n;
4567 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4568 if (err)
4569 goto done;
4571 n = fprintf(outfile, "from: %s\n",
4572 got_object_commit_get_author(commit));
4573 if (n < 0) {
4574 err = got_error_from_errno("fprintf");
4575 goto done;
4577 outoff += n;
4578 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4579 if (err)
4580 goto done;
4582 committer_time = got_object_commit_get_committer_time(commit);
4583 datestr = get_datestr(&committer_time, datebuf);
4584 if (datestr) {
4585 n = fprintf(outfile, "date: %s UTC\n", datestr);
4586 if (n < 0) {
4587 err = got_error_from_errno("fprintf");
4588 goto done;
4590 outoff += n;
4591 err = add_line_metadata(lines, nlines, outoff,
4592 GOT_DIFF_LINE_DATE);
4593 if (err)
4594 goto done;
4596 author = got_object_commit_get_author(commit);
4597 committer = got_object_commit_get_committer(commit);
4598 if (strcmp(author, committer) != 0) {
4599 n = fprintf(outfile, "via: %s\n", committer);
4600 if (n < 0) {
4601 err = got_error_from_errno("fprintf");
4602 goto done;
4604 outoff += n;
4605 err = add_line_metadata(lines, nlines, outoff,
4606 GOT_DIFF_LINE_AUTHOR);
4607 if (err)
4608 goto done;
4610 if (got_object_commit_get_nparents(commit) > 1) {
4611 const struct got_object_id_queue *parent_ids;
4612 struct got_object_qid *qid;
4613 int pn = 1;
4614 parent_ids = got_object_commit_get_parent_ids(commit);
4615 STAILQ_FOREACH(qid, parent_ids, entry) {
4616 err = got_object_id_str(&id_str, &qid->id);
4617 if (err)
4618 goto done;
4619 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4620 if (n < 0) {
4621 err = got_error_from_errno("fprintf");
4622 goto done;
4624 outoff += n;
4625 err = add_line_metadata(lines, nlines, outoff,
4626 GOT_DIFF_LINE_META);
4627 if (err)
4628 goto done;
4629 free(id_str);
4630 id_str = NULL;
4634 err = got_object_commit_get_logmsg(&logmsg, commit);
4635 if (err)
4636 goto done;
4637 s = logmsg;
4638 while ((line = strsep(&s, "\n")) != NULL) {
4639 n = fprintf(outfile, "%s\n", line);
4640 if (n < 0) {
4641 err = got_error_from_errno("fprintf");
4642 goto done;
4644 outoff += n;
4645 err = add_line_metadata(lines, nlines, outoff,
4646 GOT_DIFF_LINE_LOGMSG);
4647 if (err)
4648 goto done;
4651 err = get_changed_paths(&changed_paths, commit, repo);
4652 if (err)
4653 goto done;
4654 TAILQ_FOREACH(pe, &changed_paths, entry) {
4655 struct got_diff_changed_path *cp = pe->data;
4656 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4657 if (n < 0) {
4658 err = got_error_from_errno("fprintf");
4659 goto done;
4661 outoff += n;
4662 err = add_line_metadata(lines, nlines, outoff,
4663 GOT_DIFF_LINE_CHANGES);
4664 if (err)
4665 goto done;
4666 free((char *)pe->path);
4667 free(pe->data);
4670 fputc('\n', outfile);
4671 outoff++;
4672 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4673 done:
4674 got_pathlist_free(&changed_paths);
4675 free(id_str);
4676 free(logmsg);
4677 free(refs_str);
4678 got_object_commit_close(commit);
4679 if (err) {
4680 free(*lines);
4681 *lines = NULL;
4682 *nlines = 0;
4684 return err;
4687 static const struct got_error *
4688 create_diff(struct tog_diff_view_state *s)
4690 const struct got_error *err = NULL;
4691 FILE *f = NULL;
4692 int obj_type;
4694 free(s->lines);
4695 s->lines = malloc(sizeof(*s->lines));
4696 if (s->lines == NULL)
4697 return got_error_from_errno("malloc");
4698 s->nlines = 0;
4700 f = got_opentemp();
4701 if (f == NULL) {
4702 err = got_error_from_errno("got_opentemp");
4703 goto done;
4705 if (s->f && fclose(s->f) == EOF) {
4706 err = got_error_from_errno("fclose");
4707 goto done;
4709 s->f = f;
4711 if (s->id1)
4712 err = got_object_get_type(&obj_type, s->repo, s->id1);
4713 else
4714 err = got_object_get_type(&obj_type, s->repo, s->id2);
4715 if (err)
4716 goto done;
4718 switch (obj_type) {
4719 case GOT_OBJ_TYPE_BLOB:
4720 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4721 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4722 s->label1, s->label2, tog_diff_algo, s->diff_context,
4723 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4724 break;
4725 case GOT_OBJ_TYPE_TREE:
4726 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4727 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4728 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4729 s->force_text_diff, s->repo, s->f);
4730 break;
4731 case GOT_OBJ_TYPE_COMMIT: {
4732 const struct got_object_id_queue *parent_ids;
4733 struct got_object_qid *pid;
4734 struct got_commit_object *commit2;
4735 struct got_reflist_head *refs;
4737 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4738 if (err)
4739 goto done;
4740 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4741 /* Show commit info if we're diffing to a parent/root commit. */
4742 if (s->id1 == NULL) {
4743 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4744 refs, s->repo, s->f);
4745 if (err)
4746 goto done;
4747 } else {
4748 parent_ids = got_object_commit_get_parent_ids(commit2);
4749 STAILQ_FOREACH(pid, parent_ids, entry) {
4750 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4751 err = write_commit_info(&s->lines,
4752 &s->nlines, s->id2, refs, s->repo,
4753 s->f);
4754 if (err)
4755 goto done;
4756 break;
4760 got_object_commit_close(commit2);
4762 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4763 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4764 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4765 s->force_text_diff, s->repo, s->f);
4766 break;
4768 default:
4769 err = got_error(GOT_ERR_OBJ_TYPE);
4770 break;
4772 done:
4773 if (s->f && fflush(s->f) != 0 && err == NULL)
4774 err = got_error_from_errno("fflush");
4775 return err;
4778 static void
4779 diff_view_indicate_progress(struct tog_view *view)
4781 mvwaddstr(view->window, 0, 0, "diffing...");
4782 update_panels();
4783 doupdate();
4786 static const struct got_error *
4787 search_start_diff_view(struct tog_view *view)
4789 struct tog_diff_view_state *s = &view->state.diff;
4791 s->matched_line = 0;
4792 return NULL;
4795 static void
4796 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4797 size_t *nlines, int **first, int **last, int **match, int **selected)
4799 struct tog_diff_view_state *s = &view->state.diff;
4801 *f = s->f;
4802 *nlines = s->nlines;
4803 *line_offsets = NULL;
4804 *match = &s->matched_line;
4805 *first = &s->first_displayed_line;
4806 *last = &s->last_displayed_line;
4807 *selected = &s->selected_line;
4810 static const struct got_error *
4811 search_next_view_match(struct tog_view *view)
4813 const struct got_error *err = NULL;
4814 FILE *f;
4815 int lineno;
4816 char *line = NULL;
4817 size_t linesize = 0;
4818 ssize_t linelen;
4819 off_t *line_offsets;
4820 size_t nlines = 0;
4821 int *first, *last, *match, *selected;
4823 if (!view->search_setup)
4824 return got_error_msg(GOT_ERR_NOT_IMPL,
4825 "view search not supported");
4826 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4827 &match, &selected);
4829 if (!view->searching) {
4830 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4831 return NULL;
4834 if (*match) {
4835 if (view->searching == TOG_SEARCH_FORWARD)
4836 lineno = *match + 1;
4837 else
4838 lineno = *match - 1;
4839 } else
4840 lineno = *first - 1 + *selected;
4842 while (1) {
4843 off_t offset;
4845 if (lineno <= 0 || lineno > nlines) {
4846 if (*match == 0) {
4847 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4848 break;
4851 if (view->searching == TOG_SEARCH_FORWARD)
4852 lineno = 1;
4853 else
4854 lineno = nlines;
4857 offset = view->type == TOG_VIEW_DIFF ?
4858 view->state.diff.lines[lineno - 1].offset :
4859 line_offsets[lineno - 1];
4860 if (fseeko(f, offset, SEEK_SET) != 0) {
4861 free(line);
4862 return got_error_from_errno("fseeko");
4864 linelen = getline(&line, &linesize, f);
4865 if (linelen != -1) {
4866 char *exstr;
4867 err = expand_tab(&exstr, line);
4868 if (err)
4869 break;
4870 if (match_line(exstr, &view->regex, 1,
4871 &view->regmatch)) {
4872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4873 *match = lineno;
4874 free(exstr);
4875 break;
4877 free(exstr);
4879 if (view->searching == TOG_SEARCH_FORWARD)
4880 lineno++;
4881 else
4882 lineno--;
4884 free(line);
4886 if (*match) {
4887 *first = *match;
4888 *selected = 1;
4891 return err;
4894 static const struct got_error *
4895 close_diff_view(struct tog_view *view)
4897 const struct got_error *err = NULL;
4898 struct tog_diff_view_state *s = &view->state.diff;
4900 free(s->id1);
4901 s->id1 = NULL;
4902 free(s->id2);
4903 s->id2 = NULL;
4904 if (s->f && fclose(s->f) == EOF)
4905 err = got_error_from_errno("fclose");
4906 s->f = NULL;
4907 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4908 err = got_error_from_errno("fclose");
4909 s->f1 = NULL;
4910 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4911 err = got_error_from_errno("fclose");
4912 s->f2 = NULL;
4913 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4914 err = got_error_from_errno("close");
4915 s->fd1 = -1;
4916 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4917 err = got_error_from_errno("close");
4918 s->fd2 = -1;
4919 free(s->lines);
4920 s->lines = NULL;
4921 s->nlines = 0;
4922 return err;
4925 static const struct got_error *
4926 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4927 struct got_object_id *id2, const char *label1, const char *label2,
4928 int diff_context, int ignore_whitespace, int force_text_diff,
4929 struct tog_view *parent_view, struct got_repository *repo)
4931 const struct got_error *err;
4932 struct tog_diff_view_state *s = &view->state.diff;
4934 memset(s, 0, sizeof(*s));
4935 s->fd1 = -1;
4936 s->fd2 = -1;
4938 if (id1 != NULL && id2 != NULL) {
4939 int type1, type2;
4940 err = got_object_get_type(&type1, repo, id1);
4941 if (err)
4942 return err;
4943 err = got_object_get_type(&type2, repo, id2);
4944 if (err)
4945 return err;
4947 if (type1 != type2)
4948 return got_error(GOT_ERR_OBJ_TYPE);
4950 s->first_displayed_line = 1;
4951 s->last_displayed_line = view->nlines;
4952 s->selected_line = 1;
4953 s->repo = repo;
4954 s->id1 = id1;
4955 s->id2 = id2;
4956 s->label1 = label1;
4957 s->label2 = label2;
4959 if (id1) {
4960 s->id1 = got_object_id_dup(id1);
4961 if (s->id1 == NULL)
4962 return got_error_from_errno("got_object_id_dup");
4963 } else
4964 s->id1 = NULL;
4966 s->id2 = got_object_id_dup(id2);
4967 if (s->id2 == NULL) {
4968 err = got_error_from_errno("got_object_id_dup");
4969 goto done;
4972 s->f1 = got_opentemp();
4973 if (s->f1 == NULL) {
4974 err = got_error_from_errno("got_opentemp");
4975 goto done;
4978 s->f2 = got_opentemp();
4979 if (s->f2 == NULL) {
4980 err = got_error_from_errno("got_opentemp");
4981 goto done;
4984 s->fd1 = got_opentempfd();
4985 if (s->fd1 == -1) {
4986 err = got_error_from_errno("got_opentempfd");
4987 goto done;
4990 s->fd2 = got_opentempfd();
4991 if (s->fd2 == -1) {
4992 err = got_error_from_errno("got_opentempfd");
4993 goto done;
4996 s->first_displayed_line = 1;
4997 s->last_displayed_line = view->nlines;
4998 s->diff_context = diff_context;
4999 s->ignore_whitespace = ignore_whitespace;
5000 s->force_text_diff = force_text_diff;
5001 s->parent_view = parent_view;
5002 s->repo = repo;
5004 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5005 int rc;
5007 rc = init_pair(GOT_DIFF_LINE_MINUS,
5008 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5009 if (rc != ERR)
5010 rc = init_pair(GOT_DIFF_LINE_PLUS,
5011 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5012 if (rc != ERR)
5013 rc = init_pair(GOT_DIFF_LINE_HUNK,
5014 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5015 if (rc != ERR)
5016 rc = init_pair(GOT_DIFF_LINE_META,
5017 get_color_value("TOG_COLOR_DIFF_META"), -1);
5018 if (rc != ERR)
5019 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5020 get_color_value("TOG_COLOR_DIFF_META"), -1);
5021 if (rc != ERR)
5022 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5023 get_color_value("TOG_COLOR_DIFF_META"), -1);
5024 if (rc != ERR)
5025 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5026 get_color_value("TOG_COLOR_DIFF_META"), -1);
5027 if (rc != ERR)
5028 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5029 get_color_value("TOG_COLOR_AUTHOR"), -1);
5030 if (rc != ERR)
5031 rc = init_pair(GOT_DIFF_LINE_DATE,
5032 get_color_value("TOG_COLOR_DATE"), -1);
5033 if (rc == ERR) {
5034 err = got_error(GOT_ERR_RANGE);
5035 goto done;
5039 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5040 view_is_splitscreen(view))
5041 show_log_view(parent_view); /* draw border */
5042 diff_view_indicate_progress(view);
5044 err = create_diff(s);
5046 view->show = show_diff_view;
5047 view->input = input_diff_view;
5048 view->reset = reset_diff_view;
5049 view->close = close_diff_view;
5050 view->search_start = search_start_diff_view;
5051 view->search_setup = search_setup_diff_view;
5052 view->search_next = search_next_view_match;
5053 done:
5054 if (err)
5055 close_diff_view(view);
5056 return err;
5059 static const struct got_error *
5060 show_diff_view(struct tog_view *view)
5062 const struct got_error *err;
5063 struct tog_diff_view_state *s = &view->state.diff;
5064 char *id_str1 = NULL, *id_str2, *header;
5065 const char *label1, *label2;
5067 if (s->id1) {
5068 err = got_object_id_str(&id_str1, s->id1);
5069 if (err)
5070 return err;
5071 label1 = s->label1 ? s->label1 : id_str1;
5072 } else
5073 label1 = "/dev/null";
5075 err = got_object_id_str(&id_str2, s->id2);
5076 if (err)
5077 return err;
5078 label2 = s->label2 ? s->label2 : id_str2;
5080 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5081 err = got_error_from_errno("asprintf");
5082 free(id_str1);
5083 free(id_str2);
5084 return err;
5086 free(id_str1);
5087 free(id_str2);
5089 err = draw_file(view, header);
5090 free(header);
5091 return err;
5094 static const struct got_error *
5095 set_selected_commit(struct tog_diff_view_state *s,
5096 struct commit_queue_entry *entry)
5098 const struct got_error *err;
5099 const struct got_object_id_queue *parent_ids;
5100 struct got_commit_object *selected_commit;
5101 struct got_object_qid *pid;
5103 free(s->id2);
5104 s->id2 = got_object_id_dup(entry->id);
5105 if (s->id2 == NULL)
5106 return got_error_from_errno("got_object_id_dup");
5108 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5109 if (err)
5110 return err;
5111 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5112 free(s->id1);
5113 pid = STAILQ_FIRST(parent_ids);
5114 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5115 got_object_commit_close(selected_commit);
5116 return NULL;
5119 static const struct got_error *
5120 reset_diff_view(struct tog_view *view)
5122 struct tog_diff_view_state *s = &view->state.diff;
5124 view->count = 0;
5125 wclear(view->window);
5126 s->first_displayed_line = 1;
5127 s->last_displayed_line = view->nlines;
5128 s->matched_line = 0;
5129 diff_view_indicate_progress(view);
5130 return create_diff(s);
5133 static void
5134 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5136 int start, i;
5138 i = start = s->first_displayed_line - 1;
5140 while (s->lines[i].type != type) {
5141 if (i == 0)
5142 i = s->nlines - 1;
5143 if (--i == start)
5144 return; /* do nothing, requested type not in file */
5147 s->selected_line = 1;
5148 s->first_displayed_line = i;
5151 static void
5152 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5154 int start, i;
5156 i = start = s->first_displayed_line + 1;
5158 while (s->lines[i].type != type) {
5159 if (i == s->nlines - 1)
5160 i = 0;
5161 if (++i == start)
5162 return; /* do nothing, requested type not in file */
5165 s->selected_line = 1;
5166 s->first_displayed_line = i;
5169 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5170 int, int, int);
5171 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5172 int, int);
5174 static const struct got_error *
5175 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5177 const struct got_error *err = NULL;
5178 struct tog_diff_view_state *s = &view->state.diff;
5179 struct tog_log_view_state *ls;
5180 struct commit_queue_entry *old_selected_entry;
5181 char *line = NULL;
5182 size_t linesize = 0;
5183 ssize_t linelen;
5184 int i, nscroll = view->nlines - 1, up = 0;
5186 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5188 switch (ch) {
5189 case '0':
5190 view->x = 0;
5191 break;
5192 case '$':
5193 view->x = MAX(view->maxx - view->ncols / 3, 0);
5194 view->count = 0;
5195 break;
5196 case KEY_RIGHT:
5197 case 'l':
5198 if (view->x + view->ncols / 3 < view->maxx)
5199 view->x += 2; /* move two columns right */
5200 else
5201 view->count = 0;
5202 break;
5203 case KEY_LEFT:
5204 case 'h':
5205 view->x -= MIN(view->x, 2); /* move two columns back */
5206 if (view->x <= 0)
5207 view->count = 0;
5208 break;
5209 case 'a':
5210 case 'w':
5211 if (ch == 'a')
5212 s->force_text_diff = !s->force_text_diff;
5213 if (ch == 'w')
5214 s->ignore_whitespace = !s->ignore_whitespace;
5215 err = reset_diff_view(view);
5216 break;
5217 case 'g':
5218 case KEY_HOME:
5219 s->first_displayed_line = 1;
5220 view->count = 0;
5221 break;
5222 case 'G':
5223 case KEY_END:
5224 view->count = 0;
5225 if (s->eof)
5226 break;
5228 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5229 s->eof = 1;
5230 break;
5231 case 'k':
5232 case KEY_UP:
5233 case CTRL('p'):
5234 if (s->first_displayed_line > 1)
5235 s->first_displayed_line--;
5236 else
5237 view->count = 0;
5238 break;
5239 case CTRL('u'):
5240 case 'u':
5241 nscroll /= 2;
5242 /* FALL THROUGH */
5243 case KEY_PPAGE:
5244 case CTRL('b'):
5245 case 'b':
5246 if (s->first_displayed_line == 1) {
5247 view->count = 0;
5248 break;
5250 i = 0;
5251 while (i++ < nscroll && s->first_displayed_line > 1)
5252 s->first_displayed_line--;
5253 break;
5254 case 'j':
5255 case KEY_DOWN:
5256 case CTRL('n'):
5257 if (!s->eof)
5258 s->first_displayed_line++;
5259 else
5260 view->count = 0;
5261 break;
5262 case CTRL('d'):
5263 case 'd':
5264 nscroll /= 2;
5265 /* FALL THROUGH */
5266 case KEY_NPAGE:
5267 case CTRL('f'):
5268 case 'f':
5269 case ' ':
5270 if (s->eof) {
5271 view->count = 0;
5272 break;
5274 i = 0;
5275 while (!s->eof && i++ < nscroll) {
5276 linelen = getline(&line, &linesize, s->f);
5277 s->first_displayed_line++;
5278 if (linelen == -1) {
5279 if (feof(s->f)) {
5280 s->eof = 1;
5281 } else
5282 err = got_ferror(s->f, GOT_ERR_IO);
5283 break;
5286 free(line);
5287 break;
5288 case '(':
5289 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5290 break;
5291 case ')':
5292 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5293 break;
5294 case '{':
5295 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5296 break;
5297 case '}':
5298 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5299 break;
5300 case '[':
5301 if (s->diff_context > 0) {
5302 s->diff_context--;
5303 s->matched_line = 0;
5304 diff_view_indicate_progress(view);
5305 err = create_diff(s);
5306 if (s->first_displayed_line + view->nlines - 1 >
5307 s->nlines) {
5308 s->first_displayed_line = 1;
5309 s->last_displayed_line = view->nlines;
5311 } else
5312 view->count = 0;
5313 break;
5314 case ']':
5315 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5316 s->diff_context++;
5317 s->matched_line = 0;
5318 diff_view_indicate_progress(view);
5319 err = create_diff(s);
5320 } else
5321 view->count = 0;
5322 break;
5323 case '<':
5324 case ',':
5325 case 'K':
5326 up = 1;
5327 /* FALL THROUGH */
5328 case '>':
5329 case '.':
5330 case 'J':
5331 if (s->parent_view == NULL) {
5332 view->count = 0;
5333 break;
5335 s->parent_view->count = view->count;
5337 if (s->parent_view->type == TOG_VIEW_LOG) {
5338 ls = &s->parent_view->state.log;
5339 old_selected_entry = ls->selected_entry;
5341 err = input_log_view(NULL, s->parent_view,
5342 up ? KEY_UP : KEY_DOWN);
5343 if (err)
5344 break;
5345 view->count = s->parent_view->count;
5347 if (old_selected_entry == ls->selected_entry)
5348 break;
5350 err = set_selected_commit(s, ls->selected_entry);
5351 if (err)
5352 break;
5353 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5354 struct tog_blame_view_state *bs;
5355 struct got_object_id *id, *prev_id;
5357 bs = &s->parent_view->state.blame;
5358 prev_id = get_annotation_for_line(bs->blame.lines,
5359 bs->blame.nlines, bs->last_diffed_line);
5361 err = input_blame_view(&view, s->parent_view,
5362 up ? KEY_UP : KEY_DOWN);
5363 if (err)
5364 break;
5365 view->count = s->parent_view->count;
5367 if (prev_id == NULL)
5368 break;
5369 id = get_selected_commit_id(bs->blame.lines,
5370 bs->blame.nlines, bs->first_displayed_line,
5371 bs->selected_line);
5372 if (id == NULL)
5373 break;
5375 if (!got_object_id_cmp(prev_id, id))
5376 break;
5378 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5379 if (err)
5380 break;
5382 s->first_displayed_line = 1;
5383 s->last_displayed_line = view->nlines;
5384 s->matched_line = 0;
5385 view->x = 0;
5387 diff_view_indicate_progress(view);
5388 err = create_diff(s);
5389 break;
5390 default:
5391 view->count = 0;
5392 break;
5395 return err;
5398 static const struct got_error *
5399 cmd_diff(int argc, char *argv[])
5401 const struct got_error *error = NULL;
5402 struct got_repository *repo = NULL;
5403 struct got_worktree *worktree = NULL;
5404 struct got_object_id *id1 = NULL, *id2 = NULL;
5405 char *repo_path = NULL, *cwd = NULL;
5406 char *id_str1 = NULL, *id_str2 = NULL;
5407 char *label1 = NULL, *label2 = NULL;
5408 int diff_context = 3, ignore_whitespace = 0;
5409 int ch, force_text_diff = 0;
5410 const char *errstr;
5411 struct tog_view *view;
5412 int *pack_fds = NULL;
5414 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5415 switch (ch) {
5416 case 'a':
5417 force_text_diff = 1;
5418 break;
5419 case 'C':
5420 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5421 &errstr);
5422 if (errstr != NULL)
5423 errx(1, "number of context lines is %s: %s",
5424 errstr, errstr);
5425 break;
5426 case 'r':
5427 repo_path = realpath(optarg, NULL);
5428 if (repo_path == NULL)
5429 return got_error_from_errno2("realpath",
5430 optarg);
5431 got_path_strip_trailing_slashes(repo_path);
5432 break;
5433 case 'w':
5434 ignore_whitespace = 1;
5435 break;
5436 default:
5437 usage_diff();
5438 /* NOTREACHED */
5442 argc -= optind;
5443 argv += optind;
5445 if (argc == 0) {
5446 usage_diff(); /* TODO show local worktree changes */
5447 } else if (argc == 2) {
5448 id_str1 = argv[0];
5449 id_str2 = argv[1];
5450 } else
5451 usage_diff();
5453 error = got_repo_pack_fds_open(&pack_fds);
5454 if (error)
5455 goto done;
5457 if (repo_path == NULL) {
5458 cwd = getcwd(NULL, 0);
5459 if (cwd == NULL)
5460 return got_error_from_errno("getcwd");
5461 error = got_worktree_open(&worktree, cwd);
5462 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5463 goto done;
5464 if (worktree)
5465 repo_path =
5466 strdup(got_worktree_get_repo_path(worktree));
5467 else
5468 repo_path = strdup(cwd);
5469 if (repo_path == NULL) {
5470 error = got_error_from_errno("strdup");
5471 goto done;
5475 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5476 if (error)
5477 goto done;
5479 init_curses();
5481 error = apply_unveil(got_repo_get_path(repo), NULL);
5482 if (error)
5483 goto done;
5485 error = tog_load_refs(repo, 0);
5486 if (error)
5487 goto done;
5489 error = got_repo_match_object_id(&id1, &label1, id_str1,
5490 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5491 if (error)
5492 goto done;
5494 error = got_repo_match_object_id(&id2, &label2, id_str2,
5495 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5496 if (error)
5497 goto done;
5499 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5500 if (view == NULL) {
5501 error = got_error_from_errno("view_open");
5502 goto done;
5504 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5505 ignore_whitespace, force_text_diff, NULL, repo);
5506 if (error)
5507 goto done;
5508 error = view_loop(view);
5509 done:
5510 free(label1);
5511 free(label2);
5512 free(repo_path);
5513 free(cwd);
5514 if (repo) {
5515 const struct got_error *close_err = got_repo_close(repo);
5516 if (error == NULL)
5517 error = close_err;
5519 if (worktree)
5520 got_worktree_close(worktree);
5521 if (pack_fds) {
5522 const struct got_error *pack_err =
5523 got_repo_pack_fds_close(pack_fds);
5524 if (error == NULL)
5525 error = pack_err;
5527 tog_free_refs();
5528 return error;
5531 __dead static void
5532 usage_blame(void)
5534 endwin();
5535 fprintf(stderr,
5536 "usage: %s blame [-c commit] [-r repository-path] path\n",
5537 getprogname());
5538 exit(1);
5541 struct tog_blame_line {
5542 int annotated;
5543 struct got_object_id *id;
5546 static const struct got_error *
5547 draw_blame(struct tog_view *view)
5549 struct tog_blame_view_state *s = &view->state.blame;
5550 struct tog_blame *blame = &s->blame;
5551 regmatch_t *regmatch = &view->regmatch;
5552 const struct got_error *err;
5553 int lineno = 0, nprinted = 0;
5554 char *line = NULL;
5555 size_t linesize = 0;
5556 ssize_t linelen;
5557 wchar_t *wline;
5558 int width;
5559 struct tog_blame_line *blame_line;
5560 struct got_object_id *prev_id = NULL;
5561 char *id_str;
5562 struct tog_color *tc;
5564 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5565 if (err)
5566 return err;
5568 rewind(blame->f);
5569 werase(view->window);
5571 if (asprintf(&line, "commit %s", id_str) == -1) {
5572 err = got_error_from_errno("asprintf");
5573 free(id_str);
5574 return err;
5577 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5578 free(line);
5579 line = NULL;
5580 if (err)
5581 return err;
5582 if (view_needs_focus_indication(view))
5583 wstandout(view->window);
5584 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5585 if (tc)
5586 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5587 waddwstr(view->window, wline);
5588 while (width++ < view->ncols)
5589 waddch(view->window, ' ');
5590 if (tc)
5591 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5592 if (view_needs_focus_indication(view))
5593 wstandend(view->window);
5594 free(wline);
5595 wline = NULL;
5597 if (view->gline > blame->nlines)
5598 view->gline = blame->nlines;
5600 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5601 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5602 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5603 free(id_str);
5604 return got_error_from_errno("asprintf");
5606 free(id_str);
5607 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5608 free(line);
5609 line = NULL;
5610 if (err)
5611 return err;
5612 waddwstr(view->window, wline);
5613 free(wline);
5614 wline = NULL;
5615 if (width < view->ncols - 1)
5616 waddch(view->window, '\n');
5618 s->eof = 0;
5619 view->maxx = 0;
5620 while (nprinted < view->nlines - 2) {
5621 linelen = getline(&line, &linesize, blame->f);
5622 if (linelen == -1) {
5623 if (feof(blame->f)) {
5624 s->eof = 1;
5625 break;
5627 free(line);
5628 return got_ferror(blame->f, GOT_ERR_IO);
5630 if (++lineno < s->first_displayed_line)
5631 continue;
5632 if (view->gline && !gotoline(view, &lineno, &nprinted))
5633 continue;
5635 /* Set view->maxx based on full line length. */
5636 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5637 if (err) {
5638 free(line);
5639 return err;
5641 free(wline);
5642 wline = NULL;
5643 view->maxx = MAX(view->maxx, width);
5645 if (nprinted == s->selected_line - 1)
5646 wstandout(view->window);
5648 if (blame->nlines > 0) {
5649 blame_line = &blame->lines[lineno - 1];
5650 if (blame_line->annotated && prev_id &&
5651 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5652 !(nprinted == s->selected_line - 1)) {
5653 waddstr(view->window, " ");
5654 } else if (blame_line->annotated) {
5655 char *id_str;
5656 err = got_object_id_str(&id_str,
5657 blame_line->id);
5658 if (err) {
5659 free(line);
5660 return err;
5662 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5663 if (tc)
5664 wattr_on(view->window,
5665 COLOR_PAIR(tc->colorpair), NULL);
5666 wprintw(view->window, "%.8s", id_str);
5667 if (tc)
5668 wattr_off(view->window,
5669 COLOR_PAIR(tc->colorpair), NULL);
5670 free(id_str);
5671 prev_id = blame_line->id;
5672 } else {
5673 waddstr(view->window, "........");
5674 prev_id = NULL;
5676 } else {
5677 waddstr(view->window, "........");
5678 prev_id = NULL;
5681 if (nprinted == s->selected_line - 1)
5682 wstandend(view->window);
5683 waddstr(view->window, " ");
5685 if (view->ncols <= 9) {
5686 width = 9;
5687 } else if (s->first_displayed_line + nprinted ==
5688 s->matched_line &&
5689 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5690 err = add_matched_line(&width, line, view->ncols - 9, 9,
5691 view->window, view->x, regmatch);
5692 if (err) {
5693 free(line);
5694 return err;
5696 width += 9;
5697 } else {
5698 int skip;
5699 err = format_line(&wline, &width, &skip, line,
5700 view->x, view->ncols - 9, 9, 1);
5701 if (err) {
5702 free(line);
5703 return err;
5705 waddwstr(view->window, &wline[skip]);
5706 width += 9;
5707 free(wline);
5708 wline = NULL;
5711 if (width <= view->ncols - 1)
5712 waddch(view->window, '\n');
5713 if (++nprinted == 1)
5714 s->first_displayed_line = lineno;
5716 free(line);
5717 s->last_displayed_line = lineno;
5719 view_border(view);
5721 return NULL;
5724 static const struct got_error *
5725 blame_cb(void *arg, int nlines, int lineno,
5726 struct got_commit_object *commit, struct got_object_id *id)
5728 const struct got_error *err = NULL;
5729 struct tog_blame_cb_args *a = arg;
5730 struct tog_blame_line *line;
5731 int errcode;
5733 if (nlines != a->nlines ||
5734 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5735 return got_error(GOT_ERR_RANGE);
5737 errcode = pthread_mutex_lock(&tog_mutex);
5738 if (errcode)
5739 return got_error_set_errno(errcode, "pthread_mutex_lock");
5741 if (*a->quit) { /* user has quit the blame view */
5742 err = got_error(GOT_ERR_ITER_COMPLETED);
5743 goto done;
5746 if (lineno == -1)
5747 goto done; /* no change in this commit */
5749 line = &a->lines[lineno - 1];
5750 if (line->annotated)
5751 goto done;
5753 line->id = got_object_id_dup(id);
5754 if (line->id == NULL) {
5755 err = got_error_from_errno("got_object_id_dup");
5756 goto done;
5758 line->annotated = 1;
5759 done:
5760 errcode = pthread_mutex_unlock(&tog_mutex);
5761 if (errcode)
5762 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5763 return err;
5766 static void *
5767 blame_thread(void *arg)
5769 const struct got_error *err, *close_err;
5770 struct tog_blame_thread_args *ta = arg;
5771 struct tog_blame_cb_args *a = ta->cb_args;
5772 int errcode, fd1 = -1, fd2 = -1;
5773 FILE *f1 = NULL, *f2 = NULL;
5775 fd1 = got_opentempfd();
5776 if (fd1 == -1)
5777 return (void *)got_error_from_errno("got_opentempfd");
5779 fd2 = got_opentempfd();
5780 if (fd2 == -1) {
5781 err = got_error_from_errno("got_opentempfd");
5782 goto done;
5785 f1 = got_opentemp();
5786 if (f1 == NULL) {
5787 err = (void *)got_error_from_errno("got_opentemp");
5788 goto done;
5790 f2 = got_opentemp();
5791 if (f2 == NULL) {
5792 err = (void *)got_error_from_errno("got_opentemp");
5793 goto done;
5796 err = block_signals_used_by_main_thread();
5797 if (err)
5798 goto done;
5800 err = got_blame(ta->path, a->commit_id, ta->repo,
5801 tog_diff_algo, blame_cb, ta->cb_args,
5802 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5803 if (err && err->code == GOT_ERR_CANCELLED)
5804 err = NULL;
5806 errcode = pthread_mutex_lock(&tog_mutex);
5807 if (errcode) {
5808 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5809 goto done;
5812 close_err = got_repo_close(ta->repo);
5813 if (err == NULL)
5814 err = close_err;
5815 ta->repo = NULL;
5816 *ta->complete = 1;
5818 errcode = pthread_mutex_unlock(&tog_mutex);
5819 if (errcode && err == NULL)
5820 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5822 done:
5823 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5824 err = got_error_from_errno("close");
5825 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5826 err = got_error_from_errno("close");
5827 if (f1 && fclose(f1) == EOF && err == NULL)
5828 err = got_error_from_errno("fclose");
5829 if (f2 && fclose(f2) == EOF && err == NULL)
5830 err = got_error_from_errno("fclose");
5832 return (void *)err;
5835 static struct got_object_id *
5836 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5837 int first_displayed_line, int selected_line)
5839 struct tog_blame_line *line;
5841 if (nlines <= 0)
5842 return NULL;
5844 line = &lines[first_displayed_line - 1 + selected_line - 1];
5845 if (!line->annotated)
5846 return NULL;
5848 return line->id;
5851 static struct got_object_id *
5852 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5853 int lineno)
5855 struct tog_blame_line *line;
5857 if (nlines <= 0 || lineno >= nlines)
5858 return NULL;
5860 line = &lines[lineno - 1];
5861 if (!line->annotated)
5862 return NULL;
5864 return line->id;
5867 static const struct got_error *
5868 stop_blame(struct tog_blame *blame)
5870 const struct got_error *err = NULL;
5871 int i;
5873 if (blame->thread) {
5874 int errcode;
5875 errcode = pthread_mutex_unlock(&tog_mutex);
5876 if (errcode)
5877 return got_error_set_errno(errcode,
5878 "pthread_mutex_unlock");
5879 errcode = pthread_join(blame->thread, (void **)&err);
5880 if (errcode)
5881 return got_error_set_errno(errcode, "pthread_join");
5882 errcode = pthread_mutex_lock(&tog_mutex);
5883 if (errcode)
5884 return got_error_set_errno(errcode,
5885 "pthread_mutex_lock");
5886 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5887 err = NULL;
5888 blame->thread = NULL;
5890 if (blame->thread_args.repo) {
5891 const struct got_error *close_err;
5892 close_err = got_repo_close(blame->thread_args.repo);
5893 if (err == NULL)
5894 err = close_err;
5895 blame->thread_args.repo = NULL;
5897 if (blame->f) {
5898 if (fclose(blame->f) == EOF && err == NULL)
5899 err = got_error_from_errno("fclose");
5900 blame->f = NULL;
5902 if (blame->lines) {
5903 for (i = 0; i < blame->nlines; i++)
5904 free(blame->lines[i].id);
5905 free(blame->lines);
5906 blame->lines = NULL;
5908 free(blame->cb_args.commit_id);
5909 blame->cb_args.commit_id = NULL;
5910 if (blame->pack_fds) {
5911 const struct got_error *pack_err =
5912 got_repo_pack_fds_close(blame->pack_fds);
5913 if (err == NULL)
5914 err = pack_err;
5915 blame->pack_fds = NULL;
5917 return err;
5920 static const struct got_error *
5921 cancel_blame_view(void *arg)
5923 const struct got_error *err = NULL;
5924 int *done = arg;
5925 int errcode;
5927 errcode = pthread_mutex_lock(&tog_mutex);
5928 if (errcode)
5929 return got_error_set_errno(errcode,
5930 "pthread_mutex_unlock");
5932 if (*done)
5933 err = got_error(GOT_ERR_CANCELLED);
5935 errcode = pthread_mutex_unlock(&tog_mutex);
5936 if (errcode)
5937 return got_error_set_errno(errcode,
5938 "pthread_mutex_lock");
5940 return err;
5943 static const struct got_error *
5944 run_blame(struct tog_view *view)
5946 struct tog_blame_view_state *s = &view->state.blame;
5947 struct tog_blame *blame = &s->blame;
5948 const struct got_error *err = NULL;
5949 struct got_commit_object *commit = NULL;
5950 struct got_blob_object *blob = NULL;
5951 struct got_repository *thread_repo = NULL;
5952 struct got_object_id *obj_id = NULL;
5953 int obj_type, fd = -1;
5954 int *pack_fds = NULL;
5956 err = got_object_open_as_commit(&commit, s->repo,
5957 &s->blamed_commit->id);
5958 if (err)
5959 return err;
5961 fd = got_opentempfd();
5962 if (fd == -1) {
5963 err = got_error_from_errno("got_opentempfd");
5964 goto done;
5967 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5968 if (err)
5969 goto done;
5971 err = got_object_get_type(&obj_type, s->repo, obj_id);
5972 if (err)
5973 goto done;
5975 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5976 err = got_error(GOT_ERR_OBJ_TYPE);
5977 goto done;
5980 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5981 if (err)
5982 goto done;
5983 blame->f = got_opentemp();
5984 if (blame->f == NULL) {
5985 err = got_error_from_errno("got_opentemp");
5986 goto done;
5988 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5989 &blame->line_offsets, blame->f, blob);
5990 if (err)
5991 goto done;
5992 if (blame->nlines == 0) {
5993 s->blame_complete = 1;
5994 goto done;
5997 /* Don't include \n at EOF in the blame line count. */
5998 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5999 blame->nlines--;
6001 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6002 if (blame->lines == NULL) {
6003 err = got_error_from_errno("calloc");
6004 goto done;
6007 err = got_repo_pack_fds_open(&pack_fds);
6008 if (err)
6009 goto done;
6010 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6011 pack_fds);
6012 if (err)
6013 goto done;
6015 blame->pack_fds = pack_fds;
6016 blame->cb_args.view = view;
6017 blame->cb_args.lines = blame->lines;
6018 blame->cb_args.nlines = blame->nlines;
6019 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6020 if (blame->cb_args.commit_id == NULL) {
6021 err = got_error_from_errno("got_object_id_dup");
6022 goto done;
6024 blame->cb_args.quit = &s->done;
6026 blame->thread_args.path = s->path;
6027 blame->thread_args.repo = thread_repo;
6028 blame->thread_args.cb_args = &blame->cb_args;
6029 blame->thread_args.complete = &s->blame_complete;
6030 blame->thread_args.cancel_cb = cancel_blame_view;
6031 blame->thread_args.cancel_arg = &s->done;
6032 s->blame_complete = 0;
6034 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6035 s->first_displayed_line = 1;
6036 s->last_displayed_line = view->nlines;
6037 s->selected_line = 1;
6039 s->matched_line = 0;
6041 done:
6042 if (commit)
6043 got_object_commit_close(commit);
6044 if (fd != -1 && close(fd) == -1 && err == NULL)
6045 err = got_error_from_errno("close");
6046 if (blob)
6047 got_object_blob_close(blob);
6048 free(obj_id);
6049 if (err)
6050 stop_blame(blame);
6051 return err;
6054 static const struct got_error *
6055 open_blame_view(struct tog_view *view, char *path,
6056 struct got_object_id *commit_id, struct got_repository *repo)
6058 const struct got_error *err = NULL;
6059 struct tog_blame_view_state *s = &view->state.blame;
6061 STAILQ_INIT(&s->blamed_commits);
6063 s->path = strdup(path);
6064 if (s->path == NULL)
6065 return got_error_from_errno("strdup");
6067 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6068 if (err) {
6069 free(s->path);
6070 return err;
6073 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6074 s->first_displayed_line = 1;
6075 s->last_displayed_line = view->nlines;
6076 s->selected_line = 1;
6077 s->blame_complete = 0;
6078 s->repo = repo;
6079 s->commit_id = commit_id;
6080 memset(&s->blame, 0, sizeof(s->blame));
6082 STAILQ_INIT(&s->colors);
6083 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6084 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6085 get_color_value("TOG_COLOR_COMMIT"));
6086 if (err)
6087 return err;
6090 view->show = show_blame_view;
6091 view->input = input_blame_view;
6092 view->reset = reset_blame_view;
6093 view->close = close_blame_view;
6094 view->search_start = search_start_blame_view;
6095 view->search_setup = search_setup_blame_view;
6096 view->search_next = search_next_view_match;
6098 return run_blame(view);
6101 static const struct got_error *
6102 close_blame_view(struct tog_view *view)
6104 const struct got_error *err = NULL;
6105 struct tog_blame_view_state *s = &view->state.blame;
6107 if (s->blame.thread)
6108 err = stop_blame(&s->blame);
6110 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6111 struct got_object_qid *blamed_commit;
6112 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6113 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6114 got_object_qid_free(blamed_commit);
6117 free(s->path);
6118 free_colors(&s->colors);
6119 return err;
6122 static const struct got_error *
6123 search_start_blame_view(struct tog_view *view)
6125 struct tog_blame_view_state *s = &view->state.blame;
6127 s->matched_line = 0;
6128 return NULL;
6131 static void
6132 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6133 size_t *nlines, int **first, int **last, int **match, int **selected)
6135 struct tog_blame_view_state *s = &view->state.blame;
6137 *f = s->blame.f;
6138 *nlines = s->blame.nlines;
6139 *line_offsets = s->blame.line_offsets;
6140 *match = &s->matched_line;
6141 *first = &s->first_displayed_line;
6142 *last = &s->last_displayed_line;
6143 *selected = &s->selected_line;
6146 static const struct got_error *
6147 show_blame_view(struct tog_view *view)
6149 const struct got_error *err = NULL;
6150 struct tog_blame_view_state *s = &view->state.blame;
6151 int errcode;
6153 if (s->blame.thread == NULL && !s->blame_complete) {
6154 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6155 &s->blame.thread_args);
6156 if (errcode)
6157 return got_error_set_errno(errcode, "pthread_create");
6159 halfdelay(1); /* fast refresh while annotating */
6162 if (s->blame_complete)
6163 halfdelay(10); /* disable fast refresh */
6165 err = draw_blame(view);
6167 view_border(view);
6168 return err;
6171 static const struct got_error *
6172 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6173 struct got_repository *repo, struct got_object_id *id)
6175 struct tog_view *log_view;
6176 const struct got_error *err = NULL;
6178 *new_view = NULL;
6180 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6181 if (log_view == NULL)
6182 return got_error_from_errno("view_open");
6184 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6185 if (err)
6186 view_close(log_view);
6187 else
6188 *new_view = log_view;
6190 return err;
6193 static const struct got_error *
6194 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6196 const struct got_error *err = NULL, *thread_err = NULL;
6197 struct tog_view *diff_view;
6198 struct tog_blame_view_state *s = &view->state.blame;
6199 int eos, nscroll, begin_y = 0, begin_x = 0;
6201 eos = nscroll = view->nlines - 2;
6202 if (view_is_hsplit_top(view))
6203 --eos; /* border */
6205 switch (ch) {
6206 case '0':
6207 view->x = 0;
6208 break;
6209 case '$':
6210 view->x = MAX(view->maxx - view->ncols / 3, 0);
6211 view->count = 0;
6212 break;
6213 case KEY_RIGHT:
6214 case 'l':
6215 if (view->x + view->ncols / 3 < view->maxx)
6216 view->x += 2; /* move two columns right */
6217 else
6218 view->count = 0;
6219 break;
6220 case KEY_LEFT:
6221 case 'h':
6222 view->x -= MIN(view->x, 2); /* move two columns back */
6223 if (view->x <= 0)
6224 view->count = 0;
6225 break;
6226 case 'q':
6227 s->done = 1;
6228 break;
6229 case 'g':
6230 case KEY_HOME:
6231 s->selected_line = 1;
6232 s->first_displayed_line = 1;
6233 view->count = 0;
6234 break;
6235 case 'G':
6236 case KEY_END:
6237 if (s->blame.nlines < eos) {
6238 s->selected_line = s->blame.nlines;
6239 s->first_displayed_line = 1;
6240 } else {
6241 s->selected_line = eos;
6242 s->first_displayed_line = s->blame.nlines - (eos - 1);
6244 view->count = 0;
6245 break;
6246 case 'k':
6247 case KEY_UP:
6248 case CTRL('p'):
6249 if (s->selected_line > 1)
6250 s->selected_line--;
6251 else if (s->selected_line == 1 &&
6252 s->first_displayed_line > 1)
6253 s->first_displayed_line--;
6254 else
6255 view->count = 0;
6256 break;
6257 case CTRL('u'):
6258 case 'u':
6259 nscroll /= 2;
6260 /* FALL THROUGH */
6261 case KEY_PPAGE:
6262 case CTRL('b'):
6263 case 'b':
6264 if (s->first_displayed_line == 1) {
6265 if (view->count > 1)
6266 nscroll += nscroll;
6267 s->selected_line = MAX(1, s->selected_line - nscroll);
6268 view->count = 0;
6269 break;
6271 if (s->first_displayed_line > nscroll)
6272 s->first_displayed_line -= nscroll;
6273 else
6274 s->first_displayed_line = 1;
6275 break;
6276 case 'j':
6277 case KEY_DOWN:
6278 case CTRL('n'):
6279 if (s->selected_line < eos && s->first_displayed_line +
6280 s->selected_line <= s->blame.nlines)
6281 s->selected_line++;
6282 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6283 s->first_displayed_line++;
6284 else
6285 view->count = 0;
6286 break;
6287 case 'c':
6288 case 'p': {
6289 struct got_object_id *id = NULL;
6291 view->count = 0;
6292 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6293 s->first_displayed_line, s->selected_line);
6294 if (id == NULL)
6295 break;
6296 if (ch == 'p') {
6297 struct got_commit_object *commit, *pcommit;
6298 struct got_object_qid *pid;
6299 struct got_object_id *blob_id = NULL;
6300 int obj_type;
6301 err = got_object_open_as_commit(&commit,
6302 s->repo, id);
6303 if (err)
6304 break;
6305 pid = STAILQ_FIRST(
6306 got_object_commit_get_parent_ids(commit));
6307 if (pid == NULL) {
6308 got_object_commit_close(commit);
6309 break;
6311 /* Check if path history ends here. */
6312 err = got_object_open_as_commit(&pcommit,
6313 s->repo, &pid->id);
6314 if (err)
6315 break;
6316 err = got_object_id_by_path(&blob_id, s->repo,
6317 pcommit, s->path);
6318 got_object_commit_close(pcommit);
6319 if (err) {
6320 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6321 err = NULL;
6322 got_object_commit_close(commit);
6323 break;
6325 err = got_object_get_type(&obj_type, s->repo,
6326 blob_id);
6327 free(blob_id);
6328 /* Can't blame non-blob type objects. */
6329 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6330 got_object_commit_close(commit);
6331 break;
6333 err = got_object_qid_alloc(&s->blamed_commit,
6334 &pid->id);
6335 got_object_commit_close(commit);
6336 } else {
6337 if (got_object_id_cmp(id,
6338 &s->blamed_commit->id) == 0)
6339 break;
6340 err = got_object_qid_alloc(&s->blamed_commit,
6341 id);
6343 if (err)
6344 break;
6345 s->done = 1;
6346 thread_err = stop_blame(&s->blame);
6347 s->done = 0;
6348 if (thread_err)
6349 break;
6350 STAILQ_INSERT_HEAD(&s->blamed_commits,
6351 s->blamed_commit, entry);
6352 err = run_blame(view);
6353 if (err)
6354 break;
6355 break;
6357 case 'C': {
6358 struct got_object_qid *first;
6360 view->count = 0;
6361 first = STAILQ_FIRST(&s->blamed_commits);
6362 if (!got_object_id_cmp(&first->id, s->commit_id))
6363 break;
6364 s->done = 1;
6365 thread_err = stop_blame(&s->blame);
6366 s->done = 0;
6367 if (thread_err)
6368 break;
6369 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6370 got_object_qid_free(s->blamed_commit);
6371 s->blamed_commit =
6372 STAILQ_FIRST(&s->blamed_commits);
6373 err = run_blame(view);
6374 if (err)
6375 break;
6376 break;
6378 case 'L':
6379 view->count = 0;
6380 s->id_to_log = get_selected_commit_id(s->blame.lines,
6381 s->blame.nlines, s->first_displayed_line, s->selected_line);
6382 if (s->id_to_log)
6383 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6384 break;
6385 case KEY_ENTER:
6386 case '\r': {
6387 struct got_object_id *id = NULL;
6388 struct got_object_qid *pid;
6389 struct got_commit_object *commit = NULL;
6391 view->count = 0;
6392 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6393 s->first_displayed_line, s->selected_line);
6394 if (id == NULL)
6395 break;
6396 err = got_object_open_as_commit(&commit, s->repo, id);
6397 if (err)
6398 break;
6399 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6400 if (*new_view) {
6401 /* traversed from diff view, release diff resources */
6402 err = close_diff_view(*new_view);
6403 if (err)
6404 break;
6405 diff_view = *new_view;
6406 } else {
6407 if (view_is_parent_view(view))
6408 view_get_split(view, &begin_y, &begin_x);
6410 diff_view = view_open(0, 0, begin_y, begin_x,
6411 TOG_VIEW_DIFF);
6412 if (diff_view == NULL) {
6413 got_object_commit_close(commit);
6414 err = got_error_from_errno("view_open");
6415 break;
6418 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6419 id, NULL, NULL, 3, 0, 0, view, s->repo);
6420 got_object_commit_close(commit);
6421 if (err) {
6422 view_close(diff_view);
6423 break;
6425 s->last_diffed_line = s->first_displayed_line - 1 +
6426 s->selected_line;
6427 if (*new_view)
6428 break; /* still open from active diff view */
6429 if (view_is_parent_view(view) &&
6430 view->mode == TOG_VIEW_SPLIT_HRZN) {
6431 err = view_init_hsplit(view, begin_y);
6432 if (err)
6433 break;
6436 view->focussed = 0;
6437 diff_view->focussed = 1;
6438 diff_view->mode = view->mode;
6439 diff_view->nlines = view->lines - begin_y;
6440 if (view_is_parent_view(view)) {
6441 view_transfer_size(diff_view, view);
6442 err = view_close_child(view);
6443 if (err)
6444 break;
6445 err = view_set_child(view, diff_view);
6446 if (err)
6447 break;
6448 view->focus_child = 1;
6449 } else
6450 *new_view = diff_view;
6451 if (err)
6452 break;
6453 break;
6455 case CTRL('d'):
6456 case 'd':
6457 nscroll /= 2;
6458 /* FALL THROUGH */
6459 case KEY_NPAGE:
6460 case CTRL('f'):
6461 case 'f':
6462 case ' ':
6463 if (s->last_displayed_line >= s->blame.nlines &&
6464 s->selected_line >= MIN(s->blame.nlines,
6465 view->nlines - 2)) {
6466 view->count = 0;
6467 break;
6469 if (s->last_displayed_line >= s->blame.nlines &&
6470 s->selected_line < view->nlines - 2) {
6471 s->selected_line +=
6472 MIN(nscroll, s->last_displayed_line -
6473 s->first_displayed_line - s->selected_line + 1);
6475 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6476 s->first_displayed_line += nscroll;
6477 else
6478 s->first_displayed_line =
6479 s->blame.nlines - (view->nlines - 3);
6480 break;
6481 case KEY_RESIZE:
6482 if (s->selected_line > view->nlines - 2) {
6483 s->selected_line = MIN(s->blame.nlines,
6484 view->nlines - 2);
6486 break;
6487 default:
6488 view->count = 0;
6489 break;
6491 return thread_err ? thread_err : err;
6494 static const struct got_error *
6495 reset_blame_view(struct tog_view *view)
6497 const struct got_error *err;
6498 struct tog_blame_view_state *s = &view->state.blame;
6500 view->count = 0;
6501 s->done = 1;
6502 err = stop_blame(&s->blame);
6503 s->done = 0;
6504 if (err)
6505 return err;
6506 return run_blame(view);
6509 static const struct got_error *
6510 cmd_blame(int argc, char *argv[])
6512 const struct got_error *error;
6513 struct got_repository *repo = NULL;
6514 struct got_worktree *worktree = NULL;
6515 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6516 char *link_target = NULL;
6517 struct got_object_id *commit_id = NULL;
6518 struct got_commit_object *commit = NULL;
6519 char *commit_id_str = NULL;
6520 int ch;
6521 struct tog_view *view;
6522 int *pack_fds = NULL;
6524 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6525 switch (ch) {
6526 case 'c':
6527 commit_id_str = optarg;
6528 break;
6529 case 'r':
6530 repo_path = realpath(optarg, NULL);
6531 if (repo_path == NULL)
6532 return got_error_from_errno2("realpath",
6533 optarg);
6534 break;
6535 default:
6536 usage_blame();
6537 /* NOTREACHED */
6541 argc -= optind;
6542 argv += optind;
6544 if (argc != 1)
6545 usage_blame();
6547 error = got_repo_pack_fds_open(&pack_fds);
6548 if (error != NULL)
6549 goto done;
6551 if (repo_path == NULL) {
6552 cwd = getcwd(NULL, 0);
6553 if (cwd == NULL)
6554 return got_error_from_errno("getcwd");
6555 error = got_worktree_open(&worktree, cwd);
6556 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6557 goto done;
6558 if (worktree)
6559 repo_path =
6560 strdup(got_worktree_get_repo_path(worktree));
6561 else
6562 repo_path = strdup(cwd);
6563 if (repo_path == NULL) {
6564 error = got_error_from_errno("strdup");
6565 goto done;
6569 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6570 if (error != NULL)
6571 goto done;
6573 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6574 worktree);
6575 if (error)
6576 goto done;
6578 init_curses();
6580 error = apply_unveil(got_repo_get_path(repo), NULL);
6581 if (error)
6582 goto done;
6584 error = tog_load_refs(repo, 0);
6585 if (error)
6586 goto done;
6588 if (commit_id_str == NULL) {
6589 struct got_reference *head_ref;
6590 error = got_ref_open(&head_ref, repo, worktree ?
6591 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6592 if (error != NULL)
6593 goto done;
6594 error = got_ref_resolve(&commit_id, repo, head_ref);
6595 got_ref_close(head_ref);
6596 } else {
6597 error = got_repo_match_object_id(&commit_id, NULL,
6598 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6600 if (error != NULL)
6601 goto done;
6603 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6604 if (view == NULL) {
6605 error = got_error_from_errno("view_open");
6606 goto done;
6609 error = got_object_open_as_commit(&commit, repo, commit_id);
6610 if (error)
6611 goto done;
6613 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6614 commit, repo);
6615 if (error)
6616 goto done;
6618 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6619 commit_id, repo);
6620 if (error)
6621 goto done;
6622 if (worktree) {
6623 /* Release work tree lock. */
6624 got_worktree_close(worktree);
6625 worktree = NULL;
6627 error = view_loop(view);
6628 done:
6629 free(repo_path);
6630 free(in_repo_path);
6631 free(link_target);
6632 free(cwd);
6633 free(commit_id);
6634 if (commit)
6635 got_object_commit_close(commit);
6636 if (worktree)
6637 got_worktree_close(worktree);
6638 if (repo) {
6639 const struct got_error *close_err = got_repo_close(repo);
6640 if (error == NULL)
6641 error = close_err;
6643 if (pack_fds) {
6644 const struct got_error *pack_err =
6645 got_repo_pack_fds_close(pack_fds);
6646 if (error == NULL)
6647 error = pack_err;
6649 tog_free_refs();
6650 return error;
6653 static const struct got_error *
6654 draw_tree_entries(struct tog_view *view, const char *parent_path)
6656 struct tog_tree_view_state *s = &view->state.tree;
6657 const struct got_error *err = NULL;
6658 struct got_tree_entry *te;
6659 wchar_t *wline;
6660 char *index = NULL;
6661 struct tog_color *tc;
6662 int width, n, nentries, i = 1;
6663 int limit = view->nlines;
6665 s->ndisplayed = 0;
6666 if (view_is_hsplit_top(view))
6667 --limit; /* border */
6669 werase(view->window);
6671 if (limit == 0)
6672 return NULL;
6674 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6675 0, 0);
6676 if (err)
6677 return err;
6678 if (view_needs_focus_indication(view))
6679 wstandout(view->window);
6680 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6681 if (tc)
6682 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6683 waddwstr(view->window, wline);
6684 free(wline);
6685 wline = NULL;
6686 while (width++ < view->ncols)
6687 waddch(view->window, ' ');
6688 if (tc)
6689 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6690 if (view_needs_focus_indication(view))
6691 wstandend(view->window);
6692 if (--limit <= 0)
6693 return NULL;
6695 i += s->selected;
6696 if (s->first_displayed_entry) {
6697 i += got_tree_entry_get_index(s->first_displayed_entry);
6698 if (s->tree != s->root)
6699 ++i; /* account for ".." entry */
6701 nentries = got_object_tree_get_nentries(s->tree);
6702 if (asprintf(&index, "[%d/%d] %s",
6703 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6704 return got_error_from_errno("asprintf");
6705 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6706 free(index);
6707 if (err)
6708 return err;
6709 waddwstr(view->window, wline);
6710 free(wline);
6711 wline = NULL;
6712 if (width < view->ncols - 1)
6713 waddch(view->window, '\n');
6714 if (--limit <= 0)
6715 return NULL;
6716 waddch(view->window, '\n');
6717 if (--limit <= 0)
6718 return NULL;
6720 if (s->first_displayed_entry == NULL) {
6721 te = got_object_tree_get_first_entry(s->tree);
6722 if (s->selected == 0) {
6723 if (view->focussed)
6724 wstandout(view->window);
6725 s->selected_entry = NULL;
6727 waddstr(view->window, " ..\n"); /* parent directory */
6728 if (s->selected == 0 && view->focussed)
6729 wstandend(view->window);
6730 s->ndisplayed++;
6731 if (--limit <= 0)
6732 return NULL;
6733 n = 1;
6734 } else {
6735 n = 0;
6736 te = s->first_displayed_entry;
6739 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6740 char *line = NULL, *id_str = NULL, *link_target = NULL;
6741 const char *modestr = "";
6742 mode_t mode;
6744 te = got_object_tree_get_entry(s->tree, i);
6745 mode = got_tree_entry_get_mode(te);
6747 if (s->show_ids) {
6748 err = got_object_id_str(&id_str,
6749 got_tree_entry_get_id(te));
6750 if (err)
6751 return got_error_from_errno(
6752 "got_object_id_str");
6754 if (got_object_tree_entry_is_submodule(te))
6755 modestr = "$";
6756 else if (S_ISLNK(mode)) {
6757 int i;
6759 err = got_tree_entry_get_symlink_target(&link_target,
6760 te, s->repo);
6761 if (err) {
6762 free(id_str);
6763 return err;
6765 for (i = 0; i < strlen(link_target); i++) {
6766 if (!isprint((unsigned char)link_target[i]))
6767 link_target[i] = '?';
6769 modestr = "@";
6771 else if (S_ISDIR(mode))
6772 modestr = "/";
6773 else if (mode & S_IXUSR)
6774 modestr = "*";
6775 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6776 got_tree_entry_get_name(te), modestr,
6777 link_target ? " -> ": "",
6778 link_target ? link_target : "") == -1) {
6779 free(id_str);
6780 free(link_target);
6781 return got_error_from_errno("asprintf");
6783 free(id_str);
6784 free(link_target);
6785 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6786 0, 0);
6787 if (err) {
6788 free(line);
6789 break;
6791 if (n == s->selected) {
6792 if (view->focussed)
6793 wstandout(view->window);
6794 s->selected_entry = te;
6796 tc = match_color(&s->colors, line);
6797 if (tc)
6798 wattr_on(view->window,
6799 COLOR_PAIR(tc->colorpair), NULL);
6800 waddwstr(view->window, wline);
6801 if (tc)
6802 wattr_off(view->window,
6803 COLOR_PAIR(tc->colorpair), NULL);
6804 if (width < view->ncols - 1)
6805 waddch(view->window, '\n');
6806 if (n == s->selected && view->focussed)
6807 wstandend(view->window);
6808 free(line);
6809 free(wline);
6810 wline = NULL;
6811 n++;
6812 s->ndisplayed++;
6813 s->last_displayed_entry = te;
6814 if (--limit <= 0)
6815 break;
6818 return err;
6821 static void
6822 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6824 struct got_tree_entry *te;
6825 int isroot = s->tree == s->root;
6826 int i = 0;
6828 if (s->first_displayed_entry == NULL)
6829 return;
6831 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6832 while (i++ < maxscroll) {
6833 if (te == NULL) {
6834 if (!isroot)
6835 s->first_displayed_entry = NULL;
6836 break;
6838 s->first_displayed_entry = te;
6839 te = got_tree_entry_get_prev(s->tree, te);
6843 static const struct got_error *
6844 tree_scroll_down(struct tog_view *view, int maxscroll)
6846 struct tog_tree_view_state *s = &view->state.tree;
6847 struct got_tree_entry *next, *last;
6848 int n = 0;
6850 if (s->first_displayed_entry)
6851 next = got_tree_entry_get_next(s->tree,
6852 s->first_displayed_entry);
6853 else
6854 next = got_object_tree_get_first_entry(s->tree);
6856 last = s->last_displayed_entry;
6857 while (next && n++ < maxscroll) {
6858 if (last) {
6859 s->last_displayed_entry = last;
6860 last = got_tree_entry_get_next(s->tree, last);
6862 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6863 s->first_displayed_entry = next;
6864 next = got_tree_entry_get_next(s->tree, next);
6868 return NULL;
6871 static const struct got_error *
6872 tree_entry_path(char **path, struct tog_parent_trees *parents,
6873 struct got_tree_entry *te)
6875 const struct got_error *err = NULL;
6876 struct tog_parent_tree *pt;
6877 size_t len = 2; /* for leading slash and NUL */
6879 TAILQ_FOREACH(pt, parents, entry)
6880 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6881 + 1 /* slash */;
6882 if (te)
6883 len += strlen(got_tree_entry_get_name(te));
6885 *path = calloc(1, len);
6886 if (path == NULL)
6887 return got_error_from_errno("calloc");
6889 (*path)[0] = '/';
6890 pt = TAILQ_LAST(parents, tog_parent_trees);
6891 while (pt) {
6892 const char *name = got_tree_entry_get_name(pt->selected_entry);
6893 if (strlcat(*path, name, len) >= len) {
6894 err = got_error(GOT_ERR_NO_SPACE);
6895 goto done;
6897 if (strlcat(*path, "/", len) >= len) {
6898 err = got_error(GOT_ERR_NO_SPACE);
6899 goto done;
6901 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6903 if (te) {
6904 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6905 err = got_error(GOT_ERR_NO_SPACE);
6906 goto done;
6909 done:
6910 if (err) {
6911 free(*path);
6912 *path = NULL;
6914 return err;
6917 static const struct got_error *
6918 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6919 struct got_tree_entry *te, struct tog_parent_trees *parents,
6920 struct got_object_id *commit_id, struct got_repository *repo)
6922 const struct got_error *err = NULL;
6923 char *path;
6924 struct tog_view *blame_view;
6926 *new_view = NULL;
6928 err = tree_entry_path(&path, parents, te);
6929 if (err)
6930 return err;
6932 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6933 if (blame_view == NULL) {
6934 err = got_error_from_errno("view_open");
6935 goto done;
6938 err = open_blame_view(blame_view, path, commit_id, repo);
6939 if (err) {
6940 if (err->code == GOT_ERR_CANCELLED)
6941 err = NULL;
6942 view_close(blame_view);
6943 } else
6944 *new_view = blame_view;
6945 done:
6946 free(path);
6947 return err;
6950 static const struct got_error *
6951 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6952 struct tog_tree_view_state *s)
6954 struct tog_view *log_view;
6955 const struct got_error *err = NULL;
6956 char *path;
6958 *new_view = NULL;
6960 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6961 if (log_view == NULL)
6962 return got_error_from_errno("view_open");
6964 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6965 if (err)
6966 return err;
6968 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6969 path, 0);
6970 if (err)
6971 view_close(log_view);
6972 else
6973 *new_view = log_view;
6974 free(path);
6975 return err;
6978 static const struct got_error *
6979 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6980 const char *head_ref_name, struct got_repository *repo)
6982 const struct got_error *err = NULL;
6983 char *commit_id_str = NULL;
6984 struct tog_tree_view_state *s = &view->state.tree;
6985 struct got_commit_object *commit = NULL;
6987 TAILQ_INIT(&s->parents);
6988 STAILQ_INIT(&s->colors);
6990 s->commit_id = got_object_id_dup(commit_id);
6991 if (s->commit_id == NULL)
6992 return got_error_from_errno("got_object_id_dup");
6994 err = got_object_open_as_commit(&commit, repo, commit_id);
6995 if (err)
6996 goto done;
6999 * The root is opened here and will be closed when the view is closed.
7000 * Any visited subtrees and their path-wise parents are opened and
7001 * closed on demand.
7003 err = got_object_open_as_tree(&s->root, repo,
7004 got_object_commit_get_tree_id(commit));
7005 if (err)
7006 goto done;
7007 s->tree = s->root;
7009 err = got_object_id_str(&commit_id_str, commit_id);
7010 if (err != NULL)
7011 goto done;
7013 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7014 err = got_error_from_errno("asprintf");
7015 goto done;
7018 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7019 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7020 if (head_ref_name) {
7021 s->head_ref_name = strdup(head_ref_name);
7022 if (s->head_ref_name == NULL) {
7023 err = got_error_from_errno("strdup");
7024 goto done;
7027 s->repo = repo;
7029 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7030 err = add_color(&s->colors, "\\$$",
7031 TOG_COLOR_TREE_SUBMODULE,
7032 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7033 if (err)
7034 goto done;
7035 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7036 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7037 if (err)
7038 goto done;
7039 err = add_color(&s->colors, "/$",
7040 TOG_COLOR_TREE_DIRECTORY,
7041 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7042 if (err)
7043 goto done;
7045 err = add_color(&s->colors, "\\*$",
7046 TOG_COLOR_TREE_EXECUTABLE,
7047 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7048 if (err)
7049 goto done;
7051 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7052 get_color_value("TOG_COLOR_COMMIT"));
7053 if (err)
7054 goto done;
7057 view->show = show_tree_view;
7058 view->input = input_tree_view;
7059 view->close = close_tree_view;
7060 view->search_start = search_start_tree_view;
7061 view->search_next = search_next_tree_view;
7062 done:
7063 free(commit_id_str);
7064 if (commit)
7065 got_object_commit_close(commit);
7066 if (err)
7067 close_tree_view(view);
7068 return err;
7071 static const struct got_error *
7072 close_tree_view(struct tog_view *view)
7074 struct tog_tree_view_state *s = &view->state.tree;
7076 free_colors(&s->colors);
7077 free(s->tree_label);
7078 s->tree_label = NULL;
7079 free(s->commit_id);
7080 s->commit_id = NULL;
7081 free(s->head_ref_name);
7082 s->head_ref_name = NULL;
7083 while (!TAILQ_EMPTY(&s->parents)) {
7084 struct tog_parent_tree *parent;
7085 parent = TAILQ_FIRST(&s->parents);
7086 TAILQ_REMOVE(&s->parents, parent, entry);
7087 if (parent->tree != s->root)
7088 got_object_tree_close(parent->tree);
7089 free(parent);
7092 if (s->tree != NULL && s->tree != s->root)
7093 got_object_tree_close(s->tree);
7094 if (s->root)
7095 got_object_tree_close(s->root);
7096 return NULL;
7099 static const struct got_error *
7100 search_start_tree_view(struct tog_view *view)
7102 struct tog_tree_view_state *s = &view->state.tree;
7104 s->matched_entry = NULL;
7105 return NULL;
7108 static int
7109 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7111 regmatch_t regmatch;
7113 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7114 0) == 0;
7117 static const struct got_error *
7118 search_next_tree_view(struct tog_view *view)
7120 struct tog_tree_view_state *s = &view->state.tree;
7121 struct got_tree_entry *te = NULL;
7123 if (!view->searching) {
7124 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7125 return NULL;
7128 if (s->matched_entry) {
7129 if (view->searching == TOG_SEARCH_FORWARD) {
7130 if (s->selected_entry)
7131 te = got_tree_entry_get_next(s->tree,
7132 s->selected_entry);
7133 else
7134 te = got_object_tree_get_first_entry(s->tree);
7135 } else {
7136 if (s->selected_entry == NULL)
7137 te = got_object_tree_get_last_entry(s->tree);
7138 else
7139 te = got_tree_entry_get_prev(s->tree,
7140 s->selected_entry);
7142 } else {
7143 if (s->selected_entry)
7144 te = s->selected_entry;
7145 else if (view->searching == TOG_SEARCH_FORWARD)
7146 te = got_object_tree_get_first_entry(s->tree);
7147 else
7148 te = got_object_tree_get_last_entry(s->tree);
7151 while (1) {
7152 if (te == NULL) {
7153 if (s->matched_entry == NULL) {
7154 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7155 return NULL;
7157 if (view->searching == TOG_SEARCH_FORWARD)
7158 te = got_object_tree_get_first_entry(s->tree);
7159 else
7160 te = got_object_tree_get_last_entry(s->tree);
7163 if (match_tree_entry(te, &view->regex)) {
7164 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7165 s->matched_entry = te;
7166 break;
7169 if (view->searching == TOG_SEARCH_FORWARD)
7170 te = got_tree_entry_get_next(s->tree, te);
7171 else
7172 te = got_tree_entry_get_prev(s->tree, te);
7175 if (s->matched_entry) {
7176 s->first_displayed_entry = s->matched_entry;
7177 s->selected = 0;
7180 return NULL;
7183 static const struct got_error *
7184 show_tree_view(struct tog_view *view)
7186 const struct got_error *err = NULL;
7187 struct tog_tree_view_state *s = &view->state.tree;
7188 char *parent_path;
7190 err = tree_entry_path(&parent_path, &s->parents, NULL);
7191 if (err)
7192 return err;
7194 err = draw_tree_entries(view, parent_path);
7195 free(parent_path);
7197 view_border(view);
7198 return err;
7201 static const struct got_error *
7202 tree_goto_line(struct tog_view *view, int nlines)
7204 const struct got_error *err = NULL;
7205 struct tog_tree_view_state *s = &view->state.tree;
7206 struct got_tree_entry **fte, **lte, **ste;
7207 int g, last, first = 1, i = 1;
7208 int root = s->tree == s->root;
7209 int off = root ? 1 : 2;
7211 g = view->gline;
7212 view->gline = 0;
7214 if (g == 0)
7215 g = 1;
7216 else if (g > got_object_tree_get_nentries(s->tree))
7217 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7219 fte = &s->first_displayed_entry;
7220 lte = &s->last_displayed_entry;
7221 ste = &s->selected_entry;
7223 if (*fte != NULL) {
7224 first = got_tree_entry_get_index(*fte);
7225 first += off; /* account for ".." */
7227 last = got_tree_entry_get_index(*lte);
7228 last += off;
7230 if (g >= first && g <= last && g - first < nlines) {
7231 s->selected = g - first;
7232 return NULL; /* gline is on the current page */
7235 if (*ste != NULL) {
7236 i = got_tree_entry_get_index(*ste);
7237 i += off;
7240 if (i < g) {
7241 err = tree_scroll_down(view, g - i);
7242 if (err)
7243 return err;
7244 if (got_tree_entry_get_index(*lte) >=
7245 got_object_tree_get_nentries(s->tree) - 1 &&
7246 first + s->selected < g &&
7247 s->selected < s->ndisplayed - 1) {
7248 first = got_tree_entry_get_index(*fte);
7249 first += off;
7250 s->selected = g - first;
7252 } else if (i > g)
7253 tree_scroll_up(s, i - g);
7255 if (g < nlines &&
7256 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7257 s->selected = g - 1;
7259 return NULL;
7262 static const struct got_error *
7263 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7265 const struct got_error *err = NULL;
7266 struct tog_tree_view_state *s = &view->state.tree;
7267 struct got_tree_entry *te;
7268 int n, nscroll = view->nlines - 3;
7270 if (view->gline)
7271 return tree_goto_line(view, nscroll);
7273 switch (ch) {
7274 case 'i':
7275 s->show_ids = !s->show_ids;
7276 view->count = 0;
7277 break;
7278 case 'L':
7279 view->count = 0;
7280 if (!s->selected_entry)
7281 break;
7282 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7283 break;
7284 case 'R':
7285 view->count = 0;
7286 err = view_request_new(new_view, view, TOG_VIEW_REF);
7287 break;
7288 case 'g':
7289 case KEY_HOME:
7290 s->selected = 0;
7291 view->count = 0;
7292 if (s->tree == s->root)
7293 s->first_displayed_entry =
7294 got_object_tree_get_first_entry(s->tree);
7295 else
7296 s->first_displayed_entry = NULL;
7297 break;
7298 case 'G':
7299 case KEY_END: {
7300 int eos = view->nlines - 3;
7302 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7303 --eos; /* border */
7304 s->selected = 0;
7305 view->count = 0;
7306 te = got_object_tree_get_last_entry(s->tree);
7307 for (n = 0; n < eos; n++) {
7308 if (te == NULL) {
7309 if (s->tree != s->root) {
7310 s->first_displayed_entry = NULL;
7311 n++;
7313 break;
7315 s->first_displayed_entry = te;
7316 te = got_tree_entry_get_prev(s->tree, te);
7318 if (n > 0)
7319 s->selected = n - 1;
7320 break;
7322 case 'k':
7323 case KEY_UP:
7324 case CTRL('p'):
7325 if (s->selected > 0) {
7326 s->selected--;
7327 break;
7329 tree_scroll_up(s, 1);
7330 if (s->selected_entry == NULL ||
7331 (s->tree == s->root && s->selected_entry ==
7332 got_object_tree_get_first_entry(s->tree)))
7333 view->count = 0;
7334 break;
7335 case CTRL('u'):
7336 case 'u':
7337 nscroll /= 2;
7338 /* FALL THROUGH */
7339 case KEY_PPAGE:
7340 case CTRL('b'):
7341 case 'b':
7342 if (s->tree == s->root) {
7343 if (got_object_tree_get_first_entry(s->tree) ==
7344 s->first_displayed_entry)
7345 s->selected -= MIN(s->selected, nscroll);
7346 } else {
7347 if (s->first_displayed_entry == NULL)
7348 s->selected -= MIN(s->selected, nscroll);
7350 tree_scroll_up(s, MAX(0, nscroll));
7351 if (s->selected_entry == NULL ||
7352 (s->tree == s->root && s->selected_entry ==
7353 got_object_tree_get_first_entry(s->tree)))
7354 view->count = 0;
7355 break;
7356 case 'j':
7357 case KEY_DOWN:
7358 case CTRL('n'):
7359 if (s->selected < s->ndisplayed - 1) {
7360 s->selected++;
7361 break;
7363 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7364 == NULL) {
7365 /* can't scroll any further */
7366 view->count = 0;
7367 break;
7369 tree_scroll_down(view, 1);
7370 break;
7371 case CTRL('d'):
7372 case 'd':
7373 nscroll /= 2;
7374 /* FALL THROUGH */
7375 case KEY_NPAGE:
7376 case CTRL('f'):
7377 case 'f':
7378 case ' ':
7379 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7380 == NULL) {
7381 /* can't scroll any further; move cursor down */
7382 if (s->selected < s->ndisplayed - 1)
7383 s->selected += MIN(nscroll,
7384 s->ndisplayed - s->selected - 1);
7385 else
7386 view->count = 0;
7387 break;
7389 tree_scroll_down(view, nscroll);
7390 break;
7391 case KEY_ENTER:
7392 case '\r':
7393 case KEY_BACKSPACE:
7394 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7395 struct tog_parent_tree *parent;
7396 /* user selected '..' */
7397 if (s->tree == s->root) {
7398 view->count = 0;
7399 break;
7401 parent = TAILQ_FIRST(&s->parents);
7402 TAILQ_REMOVE(&s->parents, parent,
7403 entry);
7404 got_object_tree_close(s->tree);
7405 s->tree = parent->tree;
7406 s->first_displayed_entry =
7407 parent->first_displayed_entry;
7408 s->selected_entry =
7409 parent->selected_entry;
7410 s->selected = parent->selected;
7411 if (s->selected > view->nlines - 3) {
7412 err = offset_selection_down(view);
7413 if (err)
7414 break;
7416 free(parent);
7417 } else if (S_ISDIR(got_tree_entry_get_mode(
7418 s->selected_entry))) {
7419 struct got_tree_object *subtree;
7420 view->count = 0;
7421 err = got_object_open_as_tree(&subtree, s->repo,
7422 got_tree_entry_get_id(s->selected_entry));
7423 if (err)
7424 break;
7425 err = tree_view_visit_subtree(s, subtree);
7426 if (err) {
7427 got_object_tree_close(subtree);
7428 break;
7430 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7431 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7432 break;
7433 case KEY_RESIZE:
7434 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7435 s->selected = view->nlines - 4;
7436 view->count = 0;
7437 break;
7438 default:
7439 view->count = 0;
7440 break;
7443 return err;
7446 __dead static void
7447 usage_tree(void)
7449 endwin();
7450 fprintf(stderr,
7451 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7452 getprogname());
7453 exit(1);
7456 static const struct got_error *
7457 cmd_tree(int argc, char *argv[])
7459 const struct got_error *error;
7460 struct got_repository *repo = NULL;
7461 struct got_worktree *worktree = NULL;
7462 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7463 struct got_object_id *commit_id = NULL;
7464 struct got_commit_object *commit = NULL;
7465 const char *commit_id_arg = NULL;
7466 char *label = NULL;
7467 struct got_reference *ref = NULL;
7468 const char *head_ref_name = NULL;
7469 int ch;
7470 struct tog_view *view;
7471 int *pack_fds = NULL;
7473 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7474 switch (ch) {
7475 case 'c':
7476 commit_id_arg = optarg;
7477 break;
7478 case 'r':
7479 repo_path = realpath(optarg, NULL);
7480 if (repo_path == NULL)
7481 return got_error_from_errno2("realpath",
7482 optarg);
7483 break;
7484 default:
7485 usage_tree();
7486 /* NOTREACHED */
7490 argc -= optind;
7491 argv += optind;
7493 if (argc > 1)
7494 usage_tree();
7496 error = got_repo_pack_fds_open(&pack_fds);
7497 if (error != NULL)
7498 goto done;
7500 if (repo_path == NULL) {
7501 cwd = getcwd(NULL, 0);
7502 if (cwd == NULL)
7503 return got_error_from_errno("getcwd");
7504 error = got_worktree_open(&worktree, cwd);
7505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7506 goto done;
7507 if (worktree)
7508 repo_path =
7509 strdup(got_worktree_get_repo_path(worktree));
7510 else
7511 repo_path = strdup(cwd);
7512 if (repo_path == NULL) {
7513 error = got_error_from_errno("strdup");
7514 goto done;
7518 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7519 if (error != NULL)
7520 goto done;
7522 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7523 repo, worktree);
7524 if (error)
7525 goto done;
7527 init_curses();
7529 error = apply_unveil(got_repo_get_path(repo), NULL);
7530 if (error)
7531 goto done;
7533 error = tog_load_refs(repo, 0);
7534 if (error)
7535 goto done;
7537 if (commit_id_arg == NULL) {
7538 error = got_repo_match_object_id(&commit_id, &label,
7539 worktree ? got_worktree_get_head_ref_name(worktree) :
7540 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7541 if (error)
7542 goto done;
7543 head_ref_name = label;
7544 } else {
7545 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7546 if (error == NULL)
7547 head_ref_name = got_ref_get_name(ref);
7548 else if (error->code != GOT_ERR_NOT_REF)
7549 goto done;
7550 error = got_repo_match_object_id(&commit_id, NULL,
7551 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7552 if (error)
7553 goto done;
7556 error = got_object_open_as_commit(&commit, repo, commit_id);
7557 if (error)
7558 goto done;
7560 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7561 if (view == NULL) {
7562 error = got_error_from_errno("view_open");
7563 goto done;
7565 error = open_tree_view(view, commit_id, head_ref_name, repo);
7566 if (error)
7567 goto done;
7568 if (!got_path_is_root_dir(in_repo_path)) {
7569 error = tree_view_walk_path(&view->state.tree, commit,
7570 in_repo_path);
7571 if (error)
7572 goto done;
7575 if (worktree) {
7576 /* Release work tree lock. */
7577 got_worktree_close(worktree);
7578 worktree = NULL;
7580 error = view_loop(view);
7581 done:
7582 free(repo_path);
7583 free(cwd);
7584 free(commit_id);
7585 free(label);
7586 if (ref)
7587 got_ref_close(ref);
7588 if (repo) {
7589 const struct got_error *close_err = got_repo_close(repo);
7590 if (error == NULL)
7591 error = close_err;
7593 if (pack_fds) {
7594 const struct got_error *pack_err =
7595 got_repo_pack_fds_close(pack_fds);
7596 if (error == NULL)
7597 error = pack_err;
7599 tog_free_refs();
7600 return error;
7603 static const struct got_error *
7604 ref_view_load_refs(struct tog_ref_view_state *s)
7606 struct got_reflist_entry *sre;
7607 struct tog_reflist_entry *re;
7609 s->nrefs = 0;
7610 TAILQ_FOREACH(sre, &tog_refs, entry) {
7611 if (strncmp(got_ref_get_name(sre->ref),
7612 "refs/got/", 9) == 0 &&
7613 strncmp(got_ref_get_name(sre->ref),
7614 "refs/got/backup/", 16) != 0)
7615 continue;
7617 re = malloc(sizeof(*re));
7618 if (re == NULL)
7619 return got_error_from_errno("malloc");
7621 re->ref = got_ref_dup(sre->ref);
7622 if (re->ref == NULL)
7623 return got_error_from_errno("got_ref_dup");
7624 re->idx = s->nrefs++;
7625 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7628 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7629 return NULL;
7632 static void
7633 ref_view_free_refs(struct tog_ref_view_state *s)
7635 struct tog_reflist_entry *re;
7637 while (!TAILQ_EMPTY(&s->refs)) {
7638 re = TAILQ_FIRST(&s->refs);
7639 TAILQ_REMOVE(&s->refs, re, entry);
7640 got_ref_close(re->ref);
7641 free(re);
7645 static const struct got_error *
7646 open_ref_view(struct tog_view *view, struct got_repository *repo)
7648 const struct got_error *err = NULL;
7649 struct tog_ref_view_state *s = &view->state.ref;
7651 s->selected_entry = 0;
7652 s->repo = repo;
7654 TAILQ_INIT(&s->refs);
7655 STAILQ_INIT(&s->colors);
7657 err = ref_view_load_refs(s);
7658 if (err)
7659 return err;
7661 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7662 err = add_color(&s->colors, "^refs/heads/",
7663 TOG_COLOR_REFS_HEADS,
7664 get_color_value("TOG_COLOR_REFS_HEADS"));
7665 if (err)
7666 goto done;
7668 err = add_color(&s->colors, "^refs/tags/",
7669 TOG_COLOR_REFS_TAGS,
7670 get_color_value("TOG_COLOR_REFS_TAGS"));
7671 if (err)
7672 goto done;
7674 err = add_color(&s->colors, "^refs/remotes/",
7675 TOG_COLOR_REFS_REMOTES,
7676 get_color_value("TOG_COLOR_REFS_REMOTES"));
7677 if (err)
7678 goto done;
7680 err = add_color(&s->colors, "^refs/got/backup/",
7681 TOG_COLOR_REFS_BACKUP,
7682 get_color_value("TOG_COLOR_REFS_BACKUP"));
7683 if (err)
7684 goto done;
7687 view->show = show_ref_view;
7688 view->input = input_ref_view;
7689 view->close = close_ref_view;
7690 view->search_start = search_start_ref_view;
7691 view->search_next = search_next_ref_view;
7692 done:
7693 if (err)
7694 free_colors(&s->colors);
7695 return err;
7698 static const struct got_error *
7699 close_ref_view(struct tog_view *view)
7701 struct tog_ref_view_state *s = &view->state.ref;
7703 ref_view_free_refs(s);
7704 free_colors(&s->colors);
7706 return NULL;
7709 static const struct got_error *
7710 resolve_reflist_entry(struct got_object_id **commit_id,
7711 struct tog_reflist_entry *re, struct got_repository *repo)
7713 const struct got_error *err = NULL;
7714 struct got_object_id *obj_id;
7715 struct got_tag_object *tag = NULL;
7716 int obj_type;
7718 *commit_id = NULL;
7720 err = got_ref_resolve(&obj_id, repo, re->ref);
7721 if (err)
7722 return err;
7724 err = got_object_get_type(&obj_type, repo, obj_id);
7725 if (err)
7726 goto done;
7728 switch (obj_type) {
7729 case GOT_OBJ_TYPE_COMMIT:
7730 *commit_id = obj_id;
7731 break;
7732 case GOT_OBJ_TYPE_TAG:
7733 err = got_object_open_as_tag(&tag, repo, obj_id);
7734 if (err)
7735 goto done;
7736 free(obj_id);
7737 err = got_object_get_type(&obj_type, repo,
7738 got_object_tag_get_object_id(tag));
7739 if (err)
7740 goto done;
7741 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7742 err = got_error(GOT_ERR_OBJ_TYPE);
7743 goto done;
7745 *commit_id = got_object_id_dup(
7746 got_object_tag_get_object_id(tag));
7747 if (*commit_id == NULL) {
7748 err = got_error_from_errno("got_object_id_dup");
7749 goto done;
7751 break;
7752 default:
7753 err = got_error(GOT_ERR_OBJ_TYPE);
7754 break;
7757 done:
7758 if (tag)
7759 got_object_tag_close(tag);
7760 if (err) {
7761 free(*commit_id);
7762 *commit_id = NULL;
7764 return err;
7767 static const struct got_error *
7768 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7769 struct tog_reflist_entry *re, struct got_repository *repo)
7771 struct tog_view *log_view;
7772 const struct got_error *err = NULL;
7773 struct got_object_id *commit_id = NULL;
7775 *new_view = NULL;
7777 err = resolve_reflist_entry(&commit_id, re, repo);
7778 if (err) {
7779 if (err->code != GOT_ERR_OBJ_TYPE)
7780 return err;
7781 else
7782 return NULL;
7785 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7786 if (log_view == NULL) {
7787 err = got_error_from_errno("view_open");
7788 goto done;
7791 err = open_log_view(log_view, commit_id, repo,
7792 got_ref_get_name(re->ref), "", 0);
7793 done:
7794 if (err)
7795 view_close(log_view);
7796 else
7797 *new_view = log_view;
7798 free(commit_id);
7799 return err;
7802 static void
7803 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7805 struct tog_reflist_entry *re;
7806 int i = 0;
7808 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7809 return;
7811 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7812 while (i++ < maxscroll) {
7813 if (re == NULL)
7814 break;
7815 s->first_displayed_entry = re;
7816 re = TAILQ_PREV(re, tog_reflist_head, entry);
7820 static const struct got_error *
7821 ref_scroll_down(struct tog_view *view, int maxscroll)
7823 struct tog_ref_view_state *s = &view->state.ref;
7824 struct tog_reflist_entry *next, *last;
7825 int n = 0;
7827 if (s->first_displayed_entry)
7828 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7829 else
7830 next = TAILQ_FIRST(&s->refs);
7832 last = s->last_displayed_entry;
7833 while (next && n++ < maxscroll) {
7834 if (last) {
7835 s->last_displayed_entry = last;
7836 last = TAILQ_NEXT(last, entry);
7838 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7839 s->first_displayed_entry = next;
7840 next = TAILQ_NEXT(next, entry);
7844 return NULL;
7847 static const struct got_error *
7848 search_start_ref_view(struct tog_view *view)
7850 struct tog_ref_view_state *s = &view->state.ref;
7852 s->matched_entry = NULL;
7853 return NULL;
7856 static int
7857 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7859 regmatch_t regmatch;
7861 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7862 0) == 0;
7865 static const struct got_error *
7866 search_next_ref_view(struct tog_view *view)
7868 struct tog_ref_view_state *s = &view->state.ref;
7869 struct tog_reflist_entry *re = NULL;
7871 if (!view->searching) {
7872 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7873 return NULL;
7876 if (s->matched_entry) {
7877 if (view->searching == TOG_SEARCH_FORWARD) {
7878 if (s->selected_entry)
7879 re = TAILQ_NEXT(s->selected_entry, entry);
7880 else
7881 re = TAILQ_PREV(s->selected_entry,
7882 tog_reflist_head, entry);
7883 } else {
7884 if (s->selected_entry == NULL)
7885 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7886 else
7887 re = TAILQ_PREV(s->selected_entry,
7888 tog_reflist_head, entry);
7890 } else {
7891 if (s->selected_entry)
7892 re = s->selected_entry;
7893 else if (view->searching == TOG_SEARCH_FORWARD)
7894 re = TAILQ_FIRST(&s->refs);
7895 else
7896 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7899 while (1) {
7900 if (re == NULL) {
7901 if (s->matched_entry == NULL) {
7902 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7903 return NULL;
7905 if (view->searching == TOG_SEARCH_FORWARD)
7906 re = TAILQ_FIRST(&s->refs);
7907 else
7908 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7911 if (match_reflist_entry(re, &view->regex)) {
7912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7913 s->matched_entry = re;
7914 break;
7917 if (view->searching == TOG_SEARCH_FORWARD)
7918 re = TAILQ_NEXT(re, entry);
7919 else
7920 re = TAILQ_PREV(re, tog_reflist_head, entry);
7923 if (s->matched_entry) {
7924 s->first_displayed_entry = s->matched_entry;
7925 s->selected = 0;
7928 return NULL;
7931 static const struct got_error *
7932 show_ref_view(struct tog_view *view)
7934 const struct got_error *err = NULL;
7935 struct tog_ref_view_state *s = &view->state.ref;
7936 struct tog_reflist_entry *re;
7937 char *line = NULL;
7938 wchar_t *wline;
7939 struct tog_color *tc;
7940 int width, n;
7941 int limit = view->nlines;
7943 werase(view->window);
7945 s->ndisplayed = 0;
7946 if (view_is_hsplit_top(view))
7947 --limit; /* border */
7949 if (limit == 0)
7950 return NULL;
7952 re = s->first_displayed_entry;
7954 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7955 s->nrefs) == -1)
7956 return got_error_from_errno("asprintf");
7958 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7959 if (err) {
7960 free(line);
7961 return err;
7963 if (view_needs_focus_indication(view))
7964 wstandout(view->window);
7965 waddwstr(view->window, wline);
7966 while (width++ < view->ncols)
7967 waddch(view->window, ' ');
7968 if (view_needs_focus_indication(view))
7969 wstandend(view->window);
7970 free(wline);
7971 wline = NULL;
7972 free(line);
7973 line = NULL;
7974 if (--limit <= 0)
7975 return NULL;
7977 n = 0;
7978 while (re && limit > 0) {
7979 char *line = NULL;
7980 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7982 if (s->show_date) {
7983 struct got_commit_object *ci;
7984 struct got_tag_object *tag;
7985 struct got_object_id *id;
7986 struct tm tm;
7987 time_t t;
7989 err = got_ref_resolve(&id, s->repo, re->ref);
7990 if (err)
7991 return err;
7992 err = got_object_open_as_tag(&tag, s->repo, id);
7993 if (err) {
7994 if (err->code != GOT_ERR_OBJ_TYPE) {
7995 free(id);
7996 return err;
7998 err = got_object_open_as_commit(&ci, s->repo,
7999 id);
8000 if (err) {
8001 free(id);
8002 return err;
8004 t = got_object_commit_get_committer_time(ci);
8005 got_object_commit_close(ci);
8006 } else {
8007 t = got_object_tag_get_tagger_time(tag);
8008 got_object_tag_close(tag);
8010 free(id);
8011 if (gmtime_r(&t, &tm) == NULL)
8012 return got_error_from_errno("gmtime_r");
8013 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8014 return got_error(GOT_ERR_NO_SPACE);
8016 if (got_ref_is_symbolic(re->ref)) {
8017 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8018 ymd : "", got_ref_get_name(re->ref),
8019 got_ref_get_symref_target(re->ref)) == -1)
8020 return got_error_from_errno("asprintf");
8021 } else if (s->show_ids) {
8022 struct got_object_id *id;
8023 char *id_str;
8024 err = got_ref_resolve(&id, s->repo, re->ref);
8025 if (err)
8026 return err;
8027 err = got_object_id_str(&id_str, id);
8028 if (err) {
8029 free(id);
8030 return err;
8032 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8033 got_ref_get_name(re->ref), id_str) == -1) {
8034 err = got_error_from_errno("asprintf");
8035 free(id);
8036 free(id_str);
8037 return err;
8039 free(id);
8040 free(id_str);
8041 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8042 got_ref_get_name(re->ref)) == -1)
8043 return got_error_from_errno("asprintf");
8045 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8046 0, 0);
8047 if (err) {
8048 free(line);
8049 return err;
8051 if (n == s->selected) {
8052 if (view->focussed)
8053 wstandout(view->window);
8054 s->selected_entry = re;
8056 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8057 if (tc)
8058 wattr_on(view->window,
8059 COLOR_PAIR(tc->colorpair), NULL);
8060 waddwstr(view->window, wline);
8061 if (tc)
8062 wattr_off(view->window,
8063 COLOR_PAIR(tc->colorpair), NULL);
8064 if (width < view->ncols - 1)
8065 waddch(view->window, '\n');
8066 if (n == s->selected && view->focussed)
8067 wstandend(view->window);
8068 free(line);
8069 free(wline);
8070 wline = NULL;
8071 n++;
8072 s->ndisplayed++;
8073 s->last_displayed_entry = re;
8075 limit--;
8076 re = TAILQ_NEXT(re, entry);
8079 view_border(view);
8080 return err;
8083 static const struct got_error *
8084 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8085 struct tog_reflist_entry *re, struct got_repository *repo)
8087 const struct got_error *err = NULL;
8088 struct got_object_id *commit_id = NULL;
8089 struct tog_view *tree_view;
8091 *new_view = NULL;
8093 err = resolve_reflist_entry(&commit_id, re, repo);
8094 if (err) {
8095 if (err->code != GOT_ERR_OBJ_TYPE)
8096 return err;
8097 else
8098 return NULL;
8102 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8103 if (tree_view == NULL) {
8104 err = got_error_from_errno("view_open");
8105 goto done;
8108 err = open_tree_view(tree_view, commit_id,
8109 got_ref_get_name(re->ref), repo);
8110 if (err)
8111 goto done;
8113 *new_view = tree_view;
8114 done:
8115 free(commit_id);
8116 return err;
8119 static const struct got_error *
8120 ref_goto_line(struct tog_view *view, int nlines)
8122 const struct got_error *err = NULL;
8123 struct tog_ref_view_state *s = &view->state.ref;
8124 int g, idx = s->selected_entry->idx;
8126 g = view->gline;
8127 view->gline = 0;
8129 if (g == 0)
8130 g = 1;
8131 else if (g > s->nrefs)
8132 g = s->nrefs;
8134 if (g >= s->first_displayed_entry->idx + 1 &&
8135 g <= s->last_displayed_entry->idx + 1 &&
8136 g - s->first_displayed_entry->idx - 1 < nlines) {
8137 s->selected = g - s->first_displayed_entry->idx - 1;
8138 return NULL;
8141 if (idx + 1 < g) {
8142 err = ref_scroll_down(view, g - idx - 1);
8143 if (err)
8144 return err;
8145 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8146 s->first_displayed_entry->idx + s->selected < g &&
8147 s->selected < s->ndisplayed - 1)
8148 s->selected = g - s->first_displayed_entry->idx - 1;
8149 } else if (idx + 1 > g)
8150 ref_scroll_up(s, idx - g + 1);
8152 if (g < nlines && s->first_displayed_entry->idx == 0)
8153 s->selected = g - 1;
8155 return NULL;
8159 static const struct got_error *
8160 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8162 const struct got_error *err = NULL;
8163 struct tog_ref_view_state *s = &view->state.ref;
8164 struct tog_reflist_entry *re;
8165 int n, nscroll = view->nlines - 1;
8167 if (view->gline)
8168 return ref_goto_line(view, nscroll);
8170 switch (ch) {
8171 case 'i':
8172 s->show_ids = !s->show_ids;
8173 view->count = 0;
8174 break;
8175 case 'm':
8176 s->show_date = !s->show_date;
8177 view->count = 0;
8178 break;
8179 case 'o':
8180 s->sort_by_date = !s->sort_by_date;
8181 view->count = 0;
8182 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8183 got_ref_cmp_by_commit_timestamp_descending :
8184 tog_ref_cmp_by_name, s->repo);
8185 if (err)
8186 break;
8187 got_reflist_object_id_map_free(tog_refs_idmap);
8188 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8189 &tog_refs, s->repo);
8190 if (err)
8191 break;
8192 ref_view_free_refs(s);
8193 err = ref_view_load_refs(s);
8194 break;
8195 case KEY_ENTER:
8196 case '\r':
8197 view->count = 0;
8198 if (!s->selected_entry)
8199 break;
8200 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8201 break;
8202 case 'T':
8203 view->count = 0;
8204 if (!s->selected_entry)
8205 break;
8206 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8207 break;
8208 case 'g':
8209 case KEY_HOME:
8210 s->selected = 0;
8211 view->count = 0;
8212 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8213 break;
8214 case 'G':
8215 case KEY_END: {
8216 int eos = view->nlines - 1;
8218 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8219 --eos; /* border */
8220 s->selected = 0;
8221 view->count = 0;
8222 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8223 for (n = 0; n < eos; n++) {
8224 if (re == NULL)
8225 break;
8226 s->first_displayed_entry = re;
8227 re = TAILQ_PREV(re, tog_reflist_head, entry);
8229 if (n > 0)
8230 s->selected = n - 1;
8231 break;
8233 case 'k':
8234 case KEY_UP:
8235 case CTRL('p'):
8236 if (s->selected > 0) {
8237 s->selected--;
8238 break;
8240 ref_scroll_up(s, 1);
8241 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8242 view->count = 0;
8243 break;
8244 case CTRL('u'):
8245 case 'u':
8246 nscroll /= 2;
8247 /* FALL THROUGH */
8248 case KEY_PPAGE:
8249 case CTRL('b'):
8250 case 'b':
8251 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8252 s->selected -= MIN(nscroll, s->selected);
8253 ref_scroll_up(s, MAX(0, nscroll));
8254 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8255 view->count = 0;
8256 break;
8257 case 'j':
8258 case KEY_DOWN:
8259 case CTRL('n'):
8260 if (s->selected < s->ndisplayed - 1) {
8261 s->selected++;
8262 break;
8264 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8265 /* can't scroll any further */
8266 view->count = 0;
8267 break;
8269 ref_scroll_down(view, 1);
8270 break;
8271 case CTRL('d'):
8272 case 'd':
8273 nscroll /= 2;
8274 /* FALL THROUGH */
8275 case KEY_NPAGE:
8276 case CTRL('f'):
8277 case 'f':
8278 case ' ':
8279 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8280 /* can't scroll any further; move cursor down */
8281 if (s->selected < s->ndisplayed - 1)
8282 s->selected += MIN(nscroll,
8283 s->ndisplayed - s->selected - 1);
8284 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8285 s->selected += s->ndisplayed - s->selected - 1;
8286 view->count = 0;
8287 break;
8289 ref_scroll_down(view, nscroll);
8290 break;
8291 case CTRL('l'):
8292 view->count = 0;
8293 tog_free_refs();
8294 err = tog_load_refs(s->repo, s->sort_by_date);
8295 if (err)
8296 break;
8297 ref_view_free_refs(s);
8298 err = ref_view_load_refs(s);
8299 break;
8300 case KEY_RESIZE:
8301 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8302 s->selected = view->nlines - 2;
8303 break;
8304 default:
8305 view->count = 0;
8306 break;
8309 return err;
8312 __dead static void
8313 usage_ref(void)
8315 endwin();
8316 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8317 getprogname());
8318 exit(1);
8321 static const struct got_error *
8322 cmd_ref(int argc, char *argv[])
8324 const struct got_error *error;
8325 struct got_repository *repo = NULL;
8326 struct got_worktree *worktree = NULL;
8327 char *cwd = NULL, *repo_path = NULL;
8328 int ch;
8329 struct tog_view *view;
8330 int *pack_fds = NULL;
8332 while ((ch = getopt(argc, argv, "r:")) != -1) {
8333 switch (ch) {
8334 case 'r':
8335 repo_path = realpath(optarg, NULL);
8336 if (repo_path == NULL)
8337 return got_error_from_errno2("realpath",
8338 optarg);
8339 break;
8340 default:
8341 usage_ref();
8342 /* NOTREACHED */
8346 argc -= optind;
8347 argv += optind;
8349 if (argc > 1)
8350 usage_ref();
8352 error = got_repo_pack_fds_open(&pack_fds);
8353 if (error != NULL)
8354 goto done;
8356 if (repo_path == NULL) {
8357 cwd = getcwd(NULL, 0);
8358 if (cwd == NULL)
8359 return got_error_from_errno("getcwd");
8360 error = got_worktree_open(&worktree, cwd);
8361 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8362 goto done;
8363 if (worktree)
8364 repo_path =
8365 strdup(got_worktree_get_repo_path(worktree));
8366 else
8367 repo_path = strdup(cwd);
8368 if (repo_path == NULL) {
8369 error = got_error_from_errno("strdup");
8370 goto done;
8374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8375 if (error != NULL)
8376 goto done;
8378 init_curses();
8380 error = apply_unveil(got_repo_get_path(repo), NULL);
8381 if (error)
8382 goto done;
8384 error = tog_load_refs(repo, 0);
8385 if (error)
8386 goto done;
8388 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8389 if (view == NULL) {
8390 error = got_error_from_errno("view_open");
8391 goto done;
8394 error = open_ref_view(view, repo);
8395 if (error)
8396 goto done;
8398 if (worktree) {
8399 /* Release work tree lock. */
8400 got_worktree_close(worktree);
8401 worktree = NULL;
8403 error = view_loop(view);
8404 done:
8405 free(repo_path);
8406 free(cwd);
8407 if (repo) {
8408 const struct got_error *close_err = got_repo_close(repo);
8409 if (close_err)
8410 error = close_err;
8412 if (pack_fds) {
8413 const struct got_error *pack_err =
8414 got_repo_pack_fds_close(pack_fds);
8415 if (error == NULL)
8416 error = pack_err;
8418 tog_free_refs();
8419 return error;
8422 static const struct got_error*
8423 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8424 const char *str)
8426 size_t len;
8428 if (win == NULL)
8429 win = stdscr;
8431 len = strlen(str);
8432 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8434 if (focus)
8435 wstandout(win);
8436 if (mvwprintw(win, y, x, "%s", str) == ERR)
8437 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8438 if (focus)
8439 wstandend(win);
8441 return NULL;
8444 static const struct got_error *
8445 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8447 off_t *p;
8449 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8450 if (p == NULL) {
8451 free(*line_offsets);
8452 *line_offsets = NULL;
8453 return got_error_from_errno("reallocarray");
8456 *line_offsets = p;
8457 (*line_offsets)[*nlines] = off;
8458 ++(*nlines);
8459 return NULL;
8462 static const struct got_error *
8463 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8465 *ret = 0;
8467 for (;n > 0; --n, ++km) {
8468 char *t0, *t, *k;
8469 size_t len = 1;
8471 if (km->keys == NULL)
8472 continue;
8474 t = t0 = strdup(km->keys);
8475 if (t0 == NULL)
8476 return got_error_from_errno("strdup");
8478 len += strlen(t);
8479 while ((k = strsep(&t, " ")) != NULL)
8480 len += strlen(k) > 1 ? 2 : 0;
8481 free(t0);
8482 *ret = MAX(*ret, len);
8485 return NULL;
8489 * Write keymap section headers, keys, and key info in km to f.
8490 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8491 * wrap control and symbolic keys in guillemets, else use <>.
8493 static const struct got_error *
8494 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8496 int n, len = width;
8498 if (km->keys) {
8499 static const char *u8_glyph[] = {
8500 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8501 "\xe2\x80\xba" /* U+203A (utf8 >) */
8503 char *t0, *t, *k;
8504 int cs, s, first = 1;
8506 cs = got_locale_is_utf8();
8508 t = t0 = strdup(km->keys);
8509 if (t0 == NULL)
8510 return got_error_from_errno("strdup");
8512 len = strlen(km->keys);
8513 while ((k = strsep(&t, " ")) != NULL) {
8514 s = strlen(k) > 1; /* control or symbolic key */
8515 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8516 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8517 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8518 if (n < 0) {
8519 free(t0);
8520 return got_error_from_errno("fprintf");
8522 first = 0;
8523 len += s ? 2 : 0;
8524 *off += n;
8526 free(t0);
8528 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8529 if (n < 0)
8530 return got_error_from_errno("fprintf");
8531 *off += n;
8533 return NULL;
8536 static const struct got_error *
8537 format_help(struct tog_help_view_state *s)
8539 const struct got_error *err = NULL;
8540 off_t off = 0;
8541 int i, max, n, show = s->all;
8542 static const struct tog_key_map km[] = {
8543 #define KEYMAP_(info, type) { NULL, (info), type }
8544 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8545 GENERATE_HELP
8546 #undef KEYMAP_
8547 #undef KEY_
8550 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8551 if (err)
8552 return err;
8554 n = nitems(km);
8555 err = max_key_str(&max, km, n);
8556 if (err)
8557 return err;
8559 for (i = 0; i < n; ++i) {
8560 if (km[i].keys == NULL) {
8561 show = s->all;
8562 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8563 km[i].type == s->type || s->all)
8564 show = 1;
8566 if (show) {
8567 err = format_help_line(&off, s->f, &km[i], max);
8568 if (err)
8569 return err;
8570 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8571 if (err)
8572 return err;
8575 fputc('\n', s->f);
8576 ++off;
8577 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8578 return err;
8581 static const struct got_error *
8582 create_help(struct tog_help_view_state *s)
8584 FILE *f;
8585 const struct got_error *err;
8587 free(s->line_offsets);
8588 s->line_offsets = NULL;
8589 s->nlines = 0;
8591 f = got_opentemp();
8592 if (f == NULL)
8593 return got_error_from_errno("got_opentemp");
8594 s->f = f;
8596 err = format_help(s);
8597 if (err)
8598 return err;
8600 if (s->f && fflush(s->f) != 0)
8601 return got_error_from_errno("fflush");
8603 return NULL;
8606 static const struct got_error *
8607 search_start_help_view(struct tog_view *view)
8609 view->state.help.matched_line = 0;
8610 return NULL;
8613 static void
8614 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8615 size_t *nlines, int **first, int **last, int **match, int **selected)
8617 struct tog_help_view_state *s = &view->state.help;
8619 *f = s->f;
8620 *nlines = s->nlines;
8621 *line_offsets = s->line_offsets;
8622 *match = &s->matched_line;
8623 *first = &s->first_displayed_line;
8624 *last = &s->last_displayed_line;
8625 *selected = &s->selected_line;
8628 static const struct got_error *
8629 show_help_view(struct tog_view *view)
8631 struct tog_help_view_state *s = &view->state.help;
8632 const struct got_error *err;
8633 regmatch_t *regmatch = &view->regmatch;
8634 wchar_t *wline;
8635 char *line;
8636 ssize_t linelen;
8637 size_t linesz = 0;
8638 int width, nprinted = 0, rc = 0;
8639 int eos = view->nlines;
8641 if (view_is_hsplit_top(view))
8642 --eos; /* account for border */
8644 s->lineno = 0;
8645 rewind(s->f);
8646 werase(view->window);
8648 if (view->gline > s->nlines - 1)
8649 view->gline = s->nlines - 1;
8651 err = win_draw_center(view->window, 0, 0, view->ncols,
8652 view_needs_focus_indication(view),
8653 "tog help (press q to return to tog)");
8654 if (err)
8655 return err;
8656 if (eos <= 1)
8657 return NULL;
8658 waddstr(view->window, "\n\n");
8659 eos -= 2;
8661 s->eof = 0;
8662 view->maxx = 0;
8663 line = NULL;
8664 while (eos > 0 && nprinted < eos) {
8665 attr_t attr = 0;
8667 linelen = getline(&line, &linesz, s->f);
8668 if (linelen == -1) {
8669 if (!feof(s->f)) {
8670 free(line);
8671 return got_ferror(s->f, GOT_ERR_IO);
8673 s->eof = 1;
8674 break;
8676 if (++s->lineno < s->first_displayed_line)
8677 continue;
8678 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8679 continue;
8680 if (s->lineno == view->hiline)
8681 attr = A_STANDOUT;
8683 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8684 view->x ? 1 : 0);
8685 if (err) {
8686 free(line);
8687 return err;
8689 view->maxx = MAX(view->maxx, width);
8690 free(wline);
8691 wline = NULL;
8693 if (attr)
8694 wattron(view->window, attr);
8695 if (s->first_displayed_line + nprinted == s->matched_line &&
8696 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8697 err = add_matched_line(&width, line, view->ncols - 1, 0,
8698 view->window, view->x, regmatch);
8699 if (err) {
8700 free(line);
8701 return err;
8703 } else {
8704 int skip;
8706 err = format_line(&wline, &width, &skip, line,
8707 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8708 if (err) {
8709 free(line);
8710 return err;
8712 rc = waddwstr(view->window, &wline[skip]);
8713 free(wline);
8714 wline = NULL;
8715 if (rc == ERR)
8716 return got_error_msg(GOT_ERR_IO, "waddwstr");
8718 if (s->lineno == view->hiline) {
8719 while (width++ < view->ncols)
8720 waddch(view->window, ' ');
8721 } else {
8722 if (width <= view->ncols)
8723 waddch(view->window, '\n');
8725 if (attr)
8726 wattroff(view->window, attr);
8727 if (++nprinted == 1)
8728 s->first_displayed_line = s->lineno;
8730 free(line);
8731 if (nprinted > 0)
8732 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8733 else
8734 s->last_displayed_line = s->first_displayed_line;
8736 view_border(view);
8738 if (s->eof) {
8739 rc = waddnstr(view->window,
8740 "See the tog(1) manual page for full documentation",
8741 view->ncols - 1);
8742 if (rc == ERR)
8743 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8744 } else {
8745 wmove(view->window, view->nlines - 1, 0);
8746 wclrtoeol(view->window);
8747 wstandout(view->window);
8748 rc = waddnstr(view->window, "scroll down for more...",
8749 view->ncols - 1);
8750 if (rc == ERR)
8751 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8752 if (getcurx(view->window) < view->ncols - 6) {
8753 rc = wprintw(view->window, "[%.0f%%]",
8754 100.00 * s->last_displayed_line / s->nlines);
8755 if (rc == ERR)
8756 return got_error_msg(GOT_ERR_IO, "wprintw");
8758 wstandend(view->window);
8761 return NULL;
8764 static const struct got_error *
8765 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8767 struct tog_help_view_state *s = &view->state.help;
8768 const struct got_error *err = NULL;
8769 char *line = NULL;
8770 ssize_t linelen;
8771 size_t linesz = 0;
8772 int eos, nscroll;
8774 eos = nscroll = view->nlines;
8775 if (view_is_hsplit_top(view))
8776 --eos; /* border */
8778 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8780 switch (ch) {
8781 case '0':
8782 view->x = 0;
8783 break;
8784 case '$':
8785 view->x = MAX(view->maxx - view->ncols / 3, 0);
8786 view->count = 0;
8787 break;
8788 case KEY_RIGHT:
8789 case 'l':
8790 if (view->x + view->ncols / 3 < view->maxx)
8791 view->x += 2;
8792 else
8793 view->count = 0;
8794 break;
8795 case KEY_LEFT:
8796 case 'h':
8797 view->x -= MIN(view->x, 2);
8798 if (view->x <= 0)
8799 view->count = 0;
8800 break;
8801 case 'g':
8802 case KEY_HOME:
8803 s->first_displayed_line = 1;
8804 view->count = 0;
8805 break;
8806 case 'G':
8807 case KEY_END:
8808 view->count = 0;
8809 if (s->eof)
8810 break;
8811 s->first_displayed_line = (s->nlines - eos) + 3;
8812 s->eof = 1;
8813 break;
8814 case 'k':
8815 case KEY_UP:
8816 if (s->first_displayed_line > 1)
8817 --s->first_displayed_line;
8818 else
8819 view->count = 0;
8820 break;
8821 case CTRL('u'):
8822 case 'u':
8823 nscroll /= 2;
8824 /* FALL THROUGH */
8825 case KEY_PPAGE:
8826 case CTRL('b'):
8827 case 'b':
8828 if (s->first_displayed_line == 1) {
8829 view->count = 0;
8830 break;
8832 while (--nscroll > 0 && s->first_displayed_line > 1)
8833 s->first_displayed_line--;
8834 break;
8835 case 'j':
8836 case KEY_DOWN:
8837 case CTRL('n'):
8838 if (!s->eof)
8839 ++s->first_displayed_line;
8840 else
8841 view->count = 0;
8842 break;
8843 case CTRL('d'):
8844 case 'd':
8845 nscroll /= 2;
8846 /* FALL THROUGH */
8847 case KEY_NPAGE:
8848 case CTRL('f'):
8849 case 'f':
8850 case ' ':
8851 if (s->eof) {
8852 view->count = 0;
8853 break;
8855 while (!s->eof && --nscroll > 0) {
8856 linelen = getline(&line, &linesz, s->f);
8857 s->first_displayed_line++;
8858 if (linelen == -1) {
8859 if (feof(s->f))
8860 s->eof = 1;
8861 else
8862 err = got_ferror(s->f, GOT_ERR_IO);
8863 break;
8866 free(line);
8867 break;
8868 default:
8869 view->count = 0;
8870 break;
8873 return err;
8876 static const struct got_error *
8877 close_help_view(struct tog_view *view)
8879 struct tog_help_view_state *s = &view->state.help;
8881 free(s->line_offsets);
8882 s->line_offsets = NULL;
8883 if (fclose(s->f) == EOF)
8884 return got_error_from_errno("fclose");
8886 return NULL;
8889 static const struct got_error *
8890 reset_help_view(struct tog_view *view)
8892 struct tog_help_view_state *s = &view->state.help;
8895 if (s->f && fclose(s->f) == EOF)
8896 return got_error_from_errno("fclose");
8898 wclear(view->window);
8899 view->count = 0;
8900 view->x = 0;
8901 s->all = !s->all;
8902 s->first_displayed_line = 1;
8903 s->last_displayed_line = view->nlines;
8904 s->matched_line = 0;
8906 return create_help(s);
8909 static const struct got_error *
8910 open_help_view(struct tog_view *view, struct tog_view *parent)
8912 const struct got_error *err = NULL;
8913 struct tog_help_view_state *s = &view->state.help;
8915 s->type = (enum tog_keymap_type)parent->type;
8916 s->first_displayed_line = 1;
8917 s->last_displayed_line = view->nlines;
8918 s->selected_line = 1;
8920 view->show = show_help_view;
8921 view->input = input_help_view;
8922 view->reset = reset_help_view;
8923 view->close = close_help_view;
8924 view->search_start = search_start_help_view;
8925 view->search_setup = search_setup_help_view;
8926 view->search_next = search_next_view_match;
8928 err = create_help(s);
8929 return err;
8932 static const struct got_error *
8933 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8934 enum tog_view_type request, int y, int x)
8936 const struct got_error *err = NULL;
8938 *new_view = NULL;
8940 switch (request) {
8941 case TOG_VIEW_DIFF:
8942 if (view->type == TOG_VIEW_LOG) {
8943 struct tog_log_view_state *s = &view->state.log;
8945 err = open_diff_view_for_commit(new_view, y, x,
8946 s->selected_entry->commit, s->selected_entry->id,
8947 view, s->repo);
8948 } else
8949 return got_error_msg(GOT_ERR_NOT_IMPL,
8950 "parent/child view pair not supported");
8951 break;
8952 case TOG_VIEW_BLAME:
8953 if (view->type == TOG_VIEW_TREE) {
8954 struct tog_tree_view_state *s = &view->state.tree;
8956 err = blame_tree_entry(new_view, y, x,
8957 s->selected_entry, &s->parents, s->commit_id,
8958 s->repo);
8959 } else
8960 return got_error_msg(GOT_ERR_NOT_IMPL,
8961 "parent/child view pair not supported");
8962 break;
8963 case TOG_VIEW_LOG:
8964 if (view->type == TOG_VIEW_BLAME)
8965 err = log_annotated_line(new_view, y, x,
8966 view->state.blame.repo, view->state.blame.id_to_log);
8967 else if (view->type == TOG_VIEW_TREE)
8968 err = log_selected_tree_entry(new_view, y, x,
8969 &view->state.tree);
8970 else if (view->type == TOG_VIEW_REF)
8971 err = log_ref_entry(new_view, y, x,
8972 view->state.ref.selected_entry,
8973 view->state.ref.repo);
8974 else
8975 return got_error_msg(GOT_ERR_NOT_IMPL,
8976 "parent/child view pair not supported");
8977 break;
8978 case TOG_VIEW_TREE:
8979 if (view->type == TOG_VIEW_LOG)
8980 err = browse_commit_tree(new_view, y, x,
8981 view->state.log.selected_entry,
8982 view->state.log.in_repo_path,
8983 view->state.log.head_ref_name,
8984 view->state.log.repo);
8985 else if (view->type == TOG_VIEW_REF)
8986 err = browse_ref_tree(new_view, y, x,
8987 view->state.ref.selected_entry,
8988 view->state.ref.repo);
8989 else
8990 return got_error_msg(GOT_ERR_NOT_IMPL,
8991 "parent/child view pair not supported");
8992 break;
8993 case TOG_VIEW_REF:
8994 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8995 if (*new_view == NULL)
8996 return got_error_from_errno("view_open");
8997 if (view->type == TOG_VIEW_LOG)
8998 err = open_ref_view(*new_view, view->state.log.repo);
8999 else if (view->type == TOG_VIEW_TREE)
9000 err = open_ref_view(*new_view, view->state.tree.repo);
9001 else
9002 err = got_error_msg(GOT_ERR_NOT_IMPL,
9003 "parent/child view pair not supported");
9004 if (err)
9005 view_close(*new_view);
9006 break;
9007 case TOG_VIEW_HELP:
9008 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9009 if (*new_view == NULL)
9010 return got_error_from_errno("view_open");
9011 err = open_help_view(*new_view, view);
9012 if (err)
9013 view_close(*new_view);
9014 break;
9015 default:
9016 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9019 return err;
9023 * If view was scrolled down to move the selected line into view when opening a
9024 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9026 static void
9027 offset_selection_up(struct tog_view *view)
9029 switch (view->type) {
9030 case TOG_VIEW_BLAME: {
9031 struct tog_blame_view_state *s = &view->state.blame;
9032 if (s->first_displayed_line == 1) {
9033 s->selected_line = MAX(s->selected_line - view->offset,
9034 1);
9035 break;
9037 if (s->first_displayed_line > view->offset)
9038 s->first_displayed_line -= view->offset;
9039 else
9040 s->first_displayed_line = 1;
9041 s->selected_line += view->offset;
9042 break;
9044 case TOG_VIEW_LOG:
9045 log_scroll_up(&view->state.log, view->offset);
9046 view->state.log.selected += view->offset;
9047 break;
9048 case TOG_VIEW_REF:
9049 ref_scroll_up(&view->state.ref, view->offset);
9050 view->state.ref.selected += view->offset;
9051 break;
9052 case TOG_VIEW_TREE:
9053 tree_scroll_up(&view->state.tree, view->offset);
9054 view->state.tree.selected += view->offset;
9055 break;
9056 default:
9057 break;
9060 view->offset = 0;
9064 * If the selected line is in the section of screen covered by the bottom split,
9065 * scroll down offset lines to move it into view and index its new position.
9067 static const struct got_error *
9068 offset_selection_down(struct tog_view *view)
9070 const struct got_error *err = NULL;
9071 const struct got_error *(*scrolld)(struct tog_view *, int);
9072 int *selected = NULL;
9073 int header, offset;
9075 switch (view->type) {
9076 case TOG_VIEW_BLAME: {
9077 struct tog_blame_view_state *s = &view->state.blame;
9078 header = 3;
9079 scrolld = NULL;
9080 if (s->selected_line > view->nlines - header) {
9081 offset = abs(view->nlines - s->selected_line - header);
9082 s->first_displayed_line += offset;
9083 s->selected_line -= offset;
9084 view->offset = offset;
9086 break;
9088 case TOG_VIEW_LOG: {
9089 struct tog_log_view_state *s = &view->state.log;
9090 scrolld = &log_scroll_down;
9091 header = view_is_parent_view(view) ? 3 : 2;
9092 selected = &s->selected;
9093 break;
9095 case TOG_VIEW_REF: {
9096 struct tog_ref_view_state *s = &view->state.ref;
9097 scrolld = &ref_scroll_down;
9098 header = 3;
9099 selected = &s->selected;
9100 break;
9102 case TOG_VIEW_TREE: {
9103 struct tog_tree_view_state *s = &view->state.tree;
9104 scrolld = &tree_scroll_down;
9105 header = 5;
9106 selected = &s->selected;
9107 break;
9109 default:
9110 selected = NULL;
9111 scrolld = NULL;
9112 header = 0;
9113 break;
9116 if (selected && *selected > view->nlines - header) {
9117 offset = abs(view->nlines - *selected - header);
9118 view->offset = offset;
9119 if (scrolld && offset) {
9120 err = scrolld(view, offset);
9121 *selected -= offset;
9125 return err;
9128 static void
9129 list_commands(FILE *fp)
9131 size_t i;
9133 fprintf(fp, "commands:");
9134 for (i = 0; i < nitems(tog_commands); i++) {
9135 const struct tog_cmd *cmd = &tog_commands[i];
9136 fprintf(fp, " %s", cmd->name);
9138 fputc('\n', fp);
9141 __dead static void
9142 usage(int hflag, int status)
9144 FILE *fp = (status == 0) ? stdout : stderr;
9146 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9147 getprogname());
9148 if (hflag) {
9149 fprintf(fp, "lazy usage: %s path\n", getprogname());
9150 list_commands(fp);
9152 exit(status);
9155 static char **
9156 make_argv(int argc, ...)
9158 va_list ap;
9159 char **argv;
9160 int i;
9162 va_start(ap, argc);
9164 argv = calloc(argc, sizeof(char *));
9165 if (argv == NULL)
9166 err(1, "calloc");
9167 for (i = 0; i < argc; i++) {
9168 argv[i] = strdup(va_arg(ap, char *));
9169 if (argv[i] == NULL)
9170 err(1, "strdup");
9173 va_end(ap);
9174 return argv;
9178 * Try to convert 'tog path' into a 'tog log path' command.
9179 * The user could simply have mistyped the command rather than knowingly
9180 * provided a path. So check whether argv[0] can in fact be resolved
9181 * to a path in the HEAD commit and print a special error if not.
9182 * This hack is for mpi@ <3
9184 static const struct got_error *
9185 tog_log_with_path(int argc, char *argv[])
9187 const struct got_error *error = NULL, *close_err;
9188 const struct tog_cmd *cmd = NULL;
9189 struct got_repository *repo = NULL;
9190 struct got_worktree *worktree = NULL;
9191 struct got_object_id *commit_id = NULL, *id = NULL;
9192 struct got_commit_object *commit = NULL;
9193 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9194 char *commit_id_str = NULL, **cmd_argv = NULL;
9195 int *pack_fds = NULL;
9197 cwd = getcwd(NULL, 0);
9198 if (cwd == NULL)
9199 return got_error_from_errno("getcwd");
9201 error = got_repo_pack_fds_open(&pack_fds);
9202 if (error != NULL)
9203 goto done;
9205 error = got_worktree_open(&worktree, cwd);
9206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9207 goto done;
9209 if (worktree)
9210 repo_path = strdup(got_worktree_get_repo_path(worktree));
9211 else
9212 repo_path = strdup(cwd);
9213 if (repo_path == NULL) {
9214 error = got_error_from_errno("strdup");
9215 goto done;
9218 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9219 if (error != NULL)
9220 goto done;
9222 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9223 repo, worktree);
9224 if (error)
9225 goto done;
9227 error = tog_load_refs(repo, 0);
9228 if (error)
9229 goto done;
9230 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9231 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9232 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9233 if (error)
9234 goto done;
9236 if (worktree) {
9237 got_worktree_close(worktree);
9238 worktree = NULL;
9241 error = got_object_open_as_commit(&commit, repo, commit_id);
9242 if (error)
9243 goto done;
9245 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9246 if (error) {
9247 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9248 goto done;
9249 fprintf(stderr, "%s: '%s' is no known command or path\n",
9250 getprogname(), argv[0]);
9251 usage(1, 1);
9252 /* not reached */
9255 error = got_object_id_str(&commit_id_str, commit_id);
9256 if (error)
9257 goto done;
9259 cmd = &tog_commands[0]; /* log */
9260 argc = 4;
9261 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9262 error = cmd->cmd_main(argc, cmd_argv);
9263 done:
9264 if (repo) {
9265 close_err = got_repo_close(repo);
9266 if (error == NULL)
9267 error = close_err;
9269 if (commit)
9270 got_object_commit_close(commit);
9271 if (worktree)
9272 got_worktree_close(worktree);
9273 if (pack_fds) {
9274 const struct got_error *pack_err =
9275 got_repo_pack_fds_close(pack_fds);
9276 if (error == NULL)
9277 error = pack_err;
9279 free(id);
9280 free(commit_id_str);
9281 free(commit_id);
9282 free(cwd);
9283 free(repo_path);
9284 free(in_repo_path);
9285 if (cmd_argv) {
9286 int i;
9287 for (i = 0; i < argc; i++)
9288 free(cmd_argv[i]);
9289 free(cmd_argv);
9291 tog_free_refs();
9292 return error;
9295 int
9296 main(int argc, char *argv[])
9298 const struct got_error *error = NULL;
9299 const struct tog_cmd *cmd = NULL;
9300 int ch, hflag = 0, Vflag = 0;
9301 char **cmd_argv = NULL;
9302 static const struct option longopts[] = {
9303 { "version", no_argument, NULL, 'V' },
9304 { NULL, 0, NULL, 0}
9306 char *diff_algo_str = NULL;
9308 if (!isatty(STDIN_FILENO))
9309 errx(1, "standard input is not a tty");
9311 setlocale(LC_CTYPE, "");
9313 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9314 switch (ch) {
9315 case 'h':
9316 hflag = 1;
9317 break;
9318 case 'V':
9319 Vflag = 1;
9320 break;
9321 default:
9322 usage(hflag, 1);
9323 /* NOTREACHED */
9327 argc -= optind;
9328 argv += optind;
9329 optind = 1;
9330 optreset = 1;
9332 if (Vflag) {
9333 got_version_print_str();
9334 return 0;
9337 #ifndef PROFILE
9338 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9339 NULL) == -1)
9340 err(1, "pledge");
9341 #endif
9343 if (argc == 0) {
9344 if (hflag)
9345 usage(hflag, 0);
9346 /* Build an argument vector which runs a default command. */
9347 cmd = &tog_commands[0];
9348 argc = 1;
9349 cmd_argv = make_argv(argc, cmd->name);
9350 } else {
9351 size_t i;
9353 /* Did the user specify a command? */
9354 for (i = 0; i < nitems(tog_commands); i++) {
9355 if (strncmp(tog_commands[i].name, argv[0],
9356 strlen(argv[0])) == 0) {
9357 cmd = &tog_commands[i];
9358 break;
9363 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9364 if (diff_algo_str) {
9365 if (strcasecmp(diff_algo_str, "patience") == 0)
9366 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9367 if (strcasecmp(diff_algo_str, "myers") == 0)
9368 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9371 if (cmd == NULL) {
9372 if (argc != 1)
9373 usage(0, 1);
9374 /* No command specified; try log with a path */
9375 error = tog_log_with_path(argc, argv);
9376 } else {
9377 if (hflag)
9378 cmd->cmd_usage();
9379 else
9380 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9383 endwin();
9384 putchar('\n');
9385 if (cmd_argv) {
9386 int i;
9387 for (i = 0; i < argc; i++)
9388 free(cmd_argv[i]);
9389 free(cmd_argv);
9392 if (error && error->code != GOT_ERR_CANCELLED &&
9393 error->code != GOT_ERR_EOF &&
9394 error->code != GOT_ERR_PRIVSEP_EXIT &&
9395 error->code != GOT_ERR_PRIVSEP_PIPE &&
9396 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9397 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9398 return 0;