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;
1307 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1308 v = view->parent;
1310 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1311 wclrtoeol(v->window);
1313 nodelay(v->window, FALSE); /* block for search term input */
1314 nocbreak();
1315 echo();
1316 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1317 wrefresh(v->window);
1318 cbreak();
1319 noecho();
1320 nodelay(v->window, TRUE);
1321 if (ret == ERR)
1322 return NULL;
1324 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1325 err = view->search_start(view);
1326 if (err) {
1327 regfree(&view->regex);
1328 return err;
1330 view->search_started = 1;
1331 view->searching = TOG_SEARCH_FORWARD;
1332 view->search_next_done = 0;
1333 view->search_next(view);
1336 return NULL;
1339 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1340 static const struct got_error *
1341 switch_split(struct tog_view *view)
1343 const struct got_error *err = NULL;
1344 struct tog_view *v = NULL;
1346 if (view->parent)
1347 v = view->parent;
1348 else
1349 v = view;
1351 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1352 v->mode = TOG_VIEW_SPLIT_VERT;
1353 else
1354 v->mode = TOG_VIEW_SPLIT_HRZN;
1356 if (!v->child)
1357 return NULL;
1358 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1359 v->mode = TOG_VIEW_SPLIT_NONE;
1361 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1362 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1363 v->child->begin_y = v->child->resized_y;
1364 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1365 v->child->begin_x = v->child->resized_x;
1368 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1369 v->ncols = COLS;
1370 v->child->ncols = COLS;
1371 v->child->nscrolled = LINES - v->child->nlines;
1373 err = view_init_hsplit(v, v->child->begin_y);
1374 if (err)
1375 return err;
1377 v->child->mode = v->mode;
1378 v->child->nlines = v->lines - v->child->begin_y;
1379 v->focus_child = 1;
1381 err = view_fullscreen(v);
1382 if (err)
1383 return err;
1384 err = view_splitscreen(v->child);
1385 if (err)
1386 return err;
1388 if (v->mode == TOG_VIEW_SPLIT_NONE)
1389 v->mode = TOG_VIEW_SPLIT_VERT;
1390 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1391 err = offset_selection_down(v);
1392 if (err)
1393 return err;
1394 err = offset_selection_down(v->child);
1395 if (err)
1396 return err;
1397 } else {
1398 offset_selection_up(v);
1399 offset_selection_up(v->child);
1401 if (v->resize)
1402 err = v->resize(v, 0);
1403 else if (v->child->resize)
1404 err = v->child->resize(v->child, 0);
1406 return err;
1410 * Compute view->count from numeric input. Assign total to view->count and
1411 * return first non-numeric key entered.
1413 static int
1414 get_compound_key(struct tog_view *view, int c)
1416 struct tog_view *v = view;
1417 int x, n = 0;
1419 if (view_is_hsplit_top(view))
1420 v = view->child;
1421 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1422 v = view->parent;
1424 view->count = 0;
1425 cbreak(); /* block for input */
1426 nodelay(view->window, FALSE);
1427 wmove(v->window, v->nlines - 1, 0);
1428 wclrtoeol(v->window);
1429 waddch(v->window, ':');
1431 do {
1432 x = getcurx(v->window);
1433 if (x != ERR && x < view->ncols) {
1434 waddch(v->window, c);
1435 wrefresh(v->window);
1439 * Don't overflow. Max valid request should be the greatest
1440 * between the longest and total lines; cap at 10 million.
1442 if (n >= 9999999)
1443 n = 9999999;
1444 else
1445 n = n * 10 + (c - '0');
1446 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1448 if (c == 'G' || c == 'g') { /* nG key map */
1449 view->gline = view->hiline = n;
1450 n = 0;
1451 c = 0;
1454 /* Massage excessive or inapplicable values at the input handler. */
1455 view->count = n;
1457 return c;
1460 static const struct got_error *
1461 view_input(struct tog_view **new, int *done, struct tog_view *view,
1462 struct tog_view_list_head *views)
1464 const struct got_error *err = NULL;
1465 struct tog_view *v;
1466 int ch, errcode;
1468 *new = NULL;
1470 /* Clear "no matches" indicator. */
1471 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1472 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1473 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1474 view->count = 0;
1477 if (view->searching && !view->search_next_done) {
1478 errcode = pthread_mutex_unlock(&tog_mutex);
1479 if (errcode)
1480 return got_error_set_errno(errcode,
1481 "pthread_mutex_unlock");
1482 sched_yield();
1483 errcode = pthread_mutex_lock(&tog_mutex);
1484 if (errcode)
1485 return got_error_set_errno(errcode,
1486 "pthread_mutex_lock");
1487 view->search_next(view);
1488 return NULL;
1491 /* Allow threads to make progress while we are waiting for input. */
1492 errcode = pthread_mutex_unlock(&tog_mutex);
1493 if (errcode)
1494 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1495 /* If we have an unfinished count, let C-g or backspace abort. */
1496 if (view->count && --view->count) {
1497 cbreak();
1498 nodelay(view->window, TRUE);
1499 ch = wgetch(view->window);
1500 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1501 view->count = 0;
1502 else
1503 ch = view->ch;
1504 } else {
1505 ch = wgetch(view->window);
1506 if (ch >= '1' && ch <= '9')
1507 view->ch = ch = get_compound_key(view, ch);
1509 if (view->hiline && ch != ERR && ch != 0)
1510 view->hiline = 0; /* key pressed, clear line highlight */
1511 nodelay(view->window, TRUE);
1512 errcode = pthread_mutex_lock(&tog_mutex);
1513 if (errcode)
1514 return got_error_set_errno(errcode, "pthread_mutex_lock");
1516 if (tog_sigwinch_received || tog_sigcont_received) {
1517 tog_resizeterm();
1518 tog_sigwinch_received = 0;
1519 tog_sigcont_received = 0;
1520 TAILQ_FOREACH(v, views, entry) {
1521 err = view_resize(v);
1522 if (err)
1523 return err;
1524 err = v->input(new, v, KEY_RESIZE);
1525 if (err)
1526 return err;
1527 if (v->child) {
1528 err = view_resize(v->child);
1529 if (err)
1530 return err;
1531 err = v->child->input(new, v->child,
1532 KEY_RESIZE);
1533 if (err)
1534 return err;
1535 if (v->child->resized_x || v->child->resized_y) {
1536 err = view_resize_split(v, 0);
1537 if (err)
1538 return err;
1544 switch (ch) {
1545 case '?':
1546 case 'H':
1547 case KEY_F(1):
1548 if (view->type == TOG_VIEW_HELP)
1549 err = view->reset(view);
1550 else
1551 err = view_request_new(new, view, TOG_VIEW_HELP);
1552 break;
1553 case '\t':
1554 view->count = 0;
1555 if (view->child) {
1556 view->focussed = 0;
1557 view->child->focussed = 1;
1558 view->focus_child = 1;
1559 } else if (view->parent) {
1560 view->focussed = 0;
1561 view->parent->focussed = 1;
1562 view->parent->focus_child = 0;
1563 if (!view_is_splitscreen(view)) {
1564 if (view->parent->resize) {
1565 err = view->parent->resize(view->parent,
1566 0);
1567 if (err)
1568 return err;
1570 offset_selection_up(view->parent);
1571 err = view_fullscreen(view->parent);
1572 if (err)
1573 return err;
1576 break;
1577 case 'q':
1578 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1579 if (view->parent->resize) {
1580 /* might need more commits to fill fullscreen */
1581 err = view->parent->resize(view->parent, 0);
1582 if (err)
1583 break;
1585 offset_selection_up(view->parent);
1587 err = view->input(new, view, ch);
1588 view->dying = 1;
1589 break;
1590 case 'Q':
1591 *done = 1;
1592 break;
1593 case 'F':
1594 view->count = 0;
1595 if (view_is_parent_view(view)) {
1596 if (view->child == NULL)
1597 break;
1598 if (view_is_splitscreen(view->child)) {
1599 view->focussed = 0;
1600 view->child->focussed = 1;
1601 err = view_fullscreen(view->child);
1602 } else {
1603 err = view_splitscreen(view->child);
1604 if (!err)
1605 err = view_resize_split(view, 0);
1607 if (err)
1608 break;
1609 err = view->child->input(new, view->child,
1610 KEY_RESIZE);
1611 } else {
1612 if (view_is_splitscreen(view)) {
1613 view->parent->focussed = 0;
1614 view->focussed = 1;
1615 err = view_fullscreen(view);
1616 } else {
1617 err = view_splitscreen(view);
1618 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1619 err = view_resize(view->parent);
1620 if (!err)
1621 err = view_resize_split(view, 0);
1623 if (err)
1624 break;
1625 err = view->input(new, view, KEY_RESIZE);
1627 if (err)
1628 break;
1629 if (view->resize) {
1630 err = view->resize(view, 0);
1631 if (err)
1632 break;
1634 if (view->parent)
1635 err = offset_selection_down(view->parent);
1636 if (!err)
1637 err = offset_selection_down(view);
1638 break;
1639 case 'S':
1640 view->count = 0;
1641 err = switch_split(view);
1642 break;
1643 case '-':
1644 err = view_resize_split(view, -1);
1645 break;
1646 case '+':
1647 err = view_resize_split(view, 1);
1648 break;
1649 case KEY_RESIZE:
1650 break;
1651 case '/':
1652 view->count = 0;
1653 if (view->search_start)
1654 view_search_start(view);
1655 else
1656 err = view->input(new, view, ch);
1657 break;
1658 case 'N':
1659 case 'n':
1660 if (view->search_started && view->search_next) {
1661 view->searching = (ch == 'n' ?
1662 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1663 view->search_next_done = 0;
1664 view->search_next(view);
1665 } else
1666 err = view->input(new, view, ch);
1667 break;
1668 case 'A':
1669 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1670 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1671 else
1672 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1673 TAILQ_FOREACH(v, views, entry) {
1674 if (v->reset) {
1675 err = v->reset(v);
1676 if (err)
1677 return err;
1679 if (v->child && v->child->reset) {
1680 err = v->child->reset(v->child);
1681 if (err)
1682 return err;
1685 break;
1686 default:
1687 err = view->input(new, view, ch);
1688 break;
1691 return err;
1694 static int
1695 view_needs_focus_indication(struct tog_view *view)
1697 if (view_is_parent_view(view)) {
1698 if (view->child == NULL || view->child->focussed)
1699 return 0;
1700 if (!view_is_splitscreen(view->child))
1701 return 0;
1702 } else if (!view_is_splitscreen(view))
1703 return 0;
1705 return view->focussed;
1708 static const struct got_error *
1709 view_loop(struct tog_view *view)
1711 const struct got_error *err = NULL;
1712 struct tog_view_list_head views;
1713 struct tog_view *new_view;
1714 char *mode;
1715 int fast_refresh = 10;
1716 int done = 0, errcode;
1718 mode = getenv("TOG_VIEW_SPLIT_MODE");
1719 if (!mode || !(*mode == 'h' || *mode == 'H'))
1720 view->mode = TOG_VIEW_SPLIT_VERT;
1721 else
1722 view->mode = TOG_VIEW_SPLIT_HRZN;
1724 errcode = pthread_mutex_lock(&tog_mutex);
1725 if (errcode)
1726 return got_error_set_errno(errcode, "pthread_mutex_lock");
1728 TAILQ_INIT(&views);
1729 TAILQ_INSERT_HEAD(&views, view, entry);
1731 view->focussed = 1;
1732 err = view->show(view);
1733 if (err)
1734 return err;
1735 update_panels();
1736 doupdate();
1737 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1738 !tog_fatal_signal_received()) {
1739 /* Refresh fast during initialization, then become slower. */
1740 if (fast_refresh && fast_refresh-- == 0)
1741 halfdelay(10); /* switch to once per second */
1743 err = view_input(&new_view, &done, view, &views);
1744 if (err)
1745 break;
1746 if (view->dying) {
1747 struct tog_view *v, *prev = NULL;
1749 if (view_is_parent_view(view))
1750 prev = TAILQ_PREV(view, tog_view_list_head,
1751 entry);
1752 else if (view->parent)
1753 prev = view->parent;
1755 if (view->parent) {
1756 view->parent->child = NULL;
1757 view->parent->focus_child = 0;
1758 /* Restore fullscreen line height. */
1759 view->parent->nlines = view->parent->lines;
1760 err = view_resize(view->parent);
1761 if (err)
1762 break;
1763 /* Make resized splits persist. */
1764 view_transfer_size(view->parent, view);
1765 } else
1766 TAILQ_REMOVE(&views, view, entry);
1768 err = view_close(view);
1769 if (err)
1770 goto done;
1772 view = NULL;
1773 TAILQ_FOREACH(v, &views, entry) {
1774 if (v->focussed)
1775 break;
1777 if (view == NULL && new_view == NULL) {
1778 /* No view has focus. Try to pick one. */
1779 if (prev)
1780 view = prev;
1781 else if (!TAILQ_EMPTY(&views)) {
1782 view = TAILQ_LAST(&views,
1783 tog_view_list_head);
1785 if (view) {
1786 if (view->focus_child) {
1787 view->child->focussed = 1;
1788 view = view->child;
1789 } else
1790 view->focussed = 1;
1794 if (new_view) {
1795 struct tog_view *v, *t;
1796 /* Only allow one parent view per type. */
1797 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1798 if (v->type != new_view->type)
1799 continue;
1800 TAILQ_REMOVE(&views, v, entry);
1801 err = view_close(v);
1802 if (err)
1803 goto done;
1804 break;
1806 TAILQ_INSERT_TAIL(&views, new_view, entry);
1807 view = new_view;
1809 if (view) {
1810 if (view_is_parent_view(view)) {
1811 if (view->child && view->child->focussed)
1812 view = view->child;
1813 } else {
1814 if (view->parent && view->parent->focussed)
1815 view = view->parent;
1817 show_panel(view->panel);
1818 if (view->child && view_is_splitscreen(view->child))
1819 show_panel(view->child->panel);
1820 if (view->parent && view_is_splitscreen(view)) {
1821 err = view->parent->show(view->parent);
1822 if (err)
1823 goto done;
1825 err = view->show(view);
1826 if (err)
1827 goto done;
1828 if (view->child) {
1829 err = view->child->show(view->child);
1830 if (err)
1831 goto done;
1833 update_panels();
1834 doupdate();
1837 done:
1838 while (!TAILQ_EMPTY(&views)) {
1839 const struct got_error *close_err;
1840 view = TAILQ_FIRST(&views);
1841 TAILQ_REMOVE(&views, view, entry);
1842 close_err = view_close(view);
1843 if (close_err && err == NULL)
1844 err = close_err;
1847 errcode = pthread_mutex_unlock(&tog_mutex);
1848 if (errcode && err == NULL)
1849 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1851 return err;
1854 __dead static void
1855 usage_log(void)
1857 endwin();
1858 fprintf(stderr,
1859 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1860 getprogname());
1861 exit(1);
1864 /* Create newly allocated wide-character string equivalent to a byte string. */
1865 static const struct got_error *
1866 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1868 char *vis = NULL;
1869 const struct got_error *err = NULL;
1871 *ws = NULL;
1872 *wlen = mbstowcs(NULL, s, 0);
1873 if (*wlen == (size_t)-1) {
1874 int vislen;
1875 if (errno != EILSEQ)
1876 return got_error_from_errno("mbstowcs");
1878 /* byte string invalid in current encoding; try to "fix" it */
1879 err = got_mbsavis(&vis, &vislen, s);
1880 if (err)
1881 return err;
1882 *wlen = mbstowcs(NULL, vis, 0);
1883 if (*wlen == (size_t)-1) {
1884 err = got_error_from_errno("mbstowcs"); /* give up */
1885 goto done;
1889 *ws = calloc(*wlen + 1, sizeof(**ws));
1890 if (*ws == NULL) {
1891 err = got_error_from_errno("calloc");
1892 goto done;
1895 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1896 err = got_error_from_errno("mbstowcs");
1897 done:
1898 free(vis);
1899 if (err) {
1900 free(*ws);
1901 *ws = NULL;
1902 *wlen = 0;
1904 return err;
1907 static const struct got_error *
1908 expand_tab(char **ptr, const char *src)
1910 char *dst;
1911 size_t len, n, idx = 0, sz = 0;
1913 *ptr = NULL;
1914 n = len = strlen(src);
1915 dst = malloc(n + 1);
1916 if (dst == NULL)
1917 return got_error_from_errno("malloc");
1919 while (idx < len && src[idx]) {
1920 const char c = src[idx];
1922 if (c == '\t') {
1923 size_t nb = TABSIZE - sz % TABSIZE;
1924 char *p;
1926 p = realloc(dst, n + nb);
1927 if (p == NULL) {
1928 free(dst);
1929 return got_error_from_errno("realloc");
1932 dst = p;
1933 n += nb;
1934 memset(dst + sz, ' ', nb);
1935 sz += nb;
1936 } else
1937 dst[sz++] = src[idx];
1938 ++idx;
1941 dst[sz] = '\0';
1942 *ptr = dst;
1943 return NULL;
1947 * Advance at most n columns from wline starting at offset off.
1948 * Return the index to the first character after the span operation.
1949 * Return the combined column width of all spanned wide character in
1950 * *rcol.
1952 static int
1953 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1955 int width, i, cols = 0;
1957 if (n == 0) {
1958 *rcol = cols;
1959 return off;
1962 for (i = off; wline[i] != L'\0'; ++i) {
1963 if (wline[i] == L'\t')
1964 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1965 else
1966 width = wcwidth(wline[i]);
1968 if (width == -1) {
1969 width = 1;
1970 wline[i] = L'.';
1973 if (cols + width > n)
1974 break;
1975 cols += width;
1978 *rcol = cols;
1979 return i;
1983 * Format a line for display, ensuring that it won't overflow a width limit.
1984 * With scrolling, the width returned refers to the scrolled version of the
1985 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1987 static const struct got_error *
1988 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1989 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1991 const struct got_error *err = NULL;
1992 int cols;
1993 wchar_t *wline = NULL;
1994 char *exstr = NULL;
1995 size_t wlen;
1996 int i, scrollx;
1998 *wlinep = NULL;
1999 *widthp = 0;
2001 if (expand) {
2002 err = expand_tab(&exstr, line);
2003 if (err)
2004 return err;
2007 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2008 free(exstr);
2009 if (err)
2010 return err;
2012 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2014 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2015 wline[wlen - 1] = L'\0';
2016 wlen--;
2018 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2019 wline[wlen - 1] = L'\0';
2020 wlen--;
2023 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2024 wline[i] = L'\0';
2026 if (widthp)
2027 *widthp = cols;
2028 if (scrollxp)
2029 *scrollxp = scrollx;
2030 if (err)
2031 free(wline);
2032 else
2033 *wlinep = wline;
2034 return err;
2037 static const struct got_error*
2038 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2039 struct got_object_id *id, struct got_repository *repo)
2041 static const struct got_error *err = NULL;
2042 struct got_reflist_entry *re;
2043 char *s;
2044 const char *name;
2046 *refs_str = NULL;
2048 TAILQ_FOREACH(re, refs, entry) {
2049 struct got_tag_object *tag = NULL;
2050 struct got_object_id *ref_id;
2051 int cmp;
2053 name = got_ref_get_name(re->ref);
2054 if (strcmp(name, GOT_REF_HEAD) == 0)
2055 continue;
2056 if (strncmp(name, "refs/", 5) == 0)
2057 name += 5;
2058 if (strncmp(name, "got/", 4) == 0 &&
2059 strncmp(name, "got/backup/", 11) != 0)
2060 continue;
2061 if (strncmp(name, "heads/", 6) == 0)
2062 name += 6;
2063 if (strncmp(name, "remotes/", 8) == 0) {
2064 name += 8;
2065 s = strstr(name, "/" GOT_REF_HEAD);
2066 if (s != NULL && s[strlen(s)] == '\0')
2067 continue;
2069 err = got_ref_resolve(&ref_id, repo, re->ref);
2070 if (err)
2071 break;
2072 if (strncmp(name, "tags/", 5) == 0) {
2073 err = got_object_open_as_tag(&tag, repo, ref_id);
2074 if (err) {
2075 if (err->code != GOT_ERR_OBJ_TYPE) {
2076 free(ref_id);
2077 break;
2079 /* Ref points at something other than a tag. */
2080 err = NULL;
2081 tag = NULL;
2084 cmp = got_object_id_cmp(tag ?
2085 got_object_tag_get_object_id(tag) : ref_id, id);
2086 free(ref_id);
2087 if (tag)
2088 got_object_tag_close(tag);
2089 if (cmp != 0)
2090 continue;
2091 s = *refs_str;
2092 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2093 s ? ", " : "", name) == -1) {
2094 err = got_error_from_errno("asprintf");
2095 free(s);
2096 *refs_str = NULL;
2097 break;
2099 free(s);
2102 return err;
2105 static const struct got_error *
2106 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2107 int col_tab_align)
2109 char *smallerthan;
2111 smallerthan = strchr(author, '<');
2112 if (smallerthan && smallerthan[1] != '\0')
2113 author = smallerthan + 1;
2114 author[strcspn(author, "@>")] = '\0';
2115 return format_line(wauthor, author_width, NULL, author, 0, limit,
2116 col_tab_align, 0);
2119 static const struct got_error *
2120 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2121 struct got_object_id *id, const size_t date_display_cols,
2122 int author_display_cols)
2124 struct tog_log_view_state *s = &view->state.log;
2125 const struct got_error *err = NULL;
2126 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2127 char *logmsg0 = NULL, *logmsg = NULL;
2128 char *author = NULL;
2129 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2130 int author_width, logmsg_width;
2131 char *newline, *line = NULL;
2132 int col, limit, scrollx;
2133 const int avail = view->ncols;
2134 struct tm tm;
2135 time_t committer_time;
2136 struct tog_color *tc;
2138 committer_time = got_object_commit_get_committer_time(commit);
2139 if (gmtime_r(&committer_time, &tm) == NULL)
2140 return got_error_from_errno("gmtime_r");
2141 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2142 return got_error(GOT_ERR_NO_SPACE);
2144 if (avail <= date_display_cols)
2145 limit = MIN(sizeof(datebuf) - 1, avail);
2146 else
2147 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2148 tc = get_color(&s->colors, TOG_COLOR_DATE);
2149 if (tc)
2150 wattr_on(view->window,
2151 COLOR_PAIR(tc->colorpair), NULL);
2152 waddnstr(view->window, datebuf, limit);
2153 if (tc)
2154 wattr_off(view->window,
2155 COLOR_PAIR(tc->colorpair), NULL);
2156 col = limit;
2157 if (col > avail)
2158 goto done;
2160 if (avail >= 120) {
2161 char *id_str;
2162 err = got_object_id_str(&id_str, id);
2163 if (err)
2164 goto done;
2165 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2166 if (tc)
2167 wattr_on(view->window,
2168 COLOR_PAIR(tc->colorpair), NULL);
2169 wprintw(view->window, "%.8s ", id_str);
2170 if (tc)
2171 wattr_off(view->window,
2172 COLOR_PAIR(tc->colorpair), NULL);
2173 free(id_str);
2174 col += 9;
2175 if (col > avail)
2176 goto done;
2179 if (s->use_committer)
2180 author = strdup(got_object_commit_get_committer(commit));
2181 else
2182 author = strdup(got_object_commit_get_author(commit));
2183 if (author == NULL) {
2184 err = got_error_from_errno("strdup");
2185 goto done;
2187 err = format_author(&wauthor, &author_width, author, avail - col, col);
2188 if (err)
2189 goto done;
2190 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2191 if (tc)
2192 wattr_on(view->window,
2193 COLOR_PAIR(tc->colorpair), NULL);
2194 waddwstr(view->window, wauthor);
2195 col += author_width;
2196 while (col < avail && author_width < author_display_cols + 2) {
2197 waddch(view->window, ' ');
2198 col++;
2199 author_width++;
2201 if (tc)
2202 wattr_off(view->window,
2203 COLOR_PAIR(tc->colorpair), NULL);
2204 if (col > avail)
2205 goto done;
2207 err = got_object_commit_get_logmsg(&logmsg0, commit);
2208 if (err)
2209 goto done;
2210 logmsg = logmsg0;
2211 while (*logmsg == '\n')
2212 logmsg++;
2213 newline = strchr(logmsg, '\n');
2214 if (newline)
2215 *newline = '\0';
2216 limit = avail - col;
2217 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2218 limit--; /* for the border */
2219 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2220 limit, col, 1);
2221 if (err)
2222 goto done;
2223 waddwstr(view->window, &wlogmsg[scrollx]);
2224 col += MAX(logmsg_width, 0);
2225 while (col < avail) {
2226 waddch(view->window, ' ');
2227 col++;
2229 done:
2230 free(logmsg0);
2231 free(wlogmsg);
2232 free(author);
2233 free(wauthor);
2234 free(line);
2235 return err;
2238 static struct commit_queue_entry *
2239 alloc_commit_queue_entry(struct got_commit_object *commit,
2240 struct got_object_id *id)
2242 struct commit_queue_entry *entry;
2243 struct got_object_id *dup;
2245 entry = calloc(1, sizeof(*entry));
2246 if (entry == NULL)
2247 return NULL;
2249 dup = got_object_id_dup(id);
2250 if (dup == NULL) {
2251 free(entry);
2252 return NULL;
2255 entry->id = dup;
2256 entry->commit = commit;
2257 return entry;
2260 static void
2261 pop_commit(struct commit_queue *commits)
2263 struct commit_queue_entry *entry;
2265 entry = TAILQ_FIRST(&commits->head);
2266 TAILQ_REMOVE(&commits->head, entry, entry);
2267 got_object_commit_close(entry->commit);
2268 commits->ncommits--;
2269 free(entry->id);
2270 free(entry);
2273 static void
2274 free_commits(struct commit_queue *commits)
2276 while (!TAILQ_EMPTY(&commits->head))
2277 pop_commit(commits);
2280 static const struct got_error *
2281 match_commit(int *have_match, struct got_object_id *id,
2282 struct got_commit_object *commit, regex_t *regex)
2284 const struct got_error *err = NULL;
2285 regmatch_t regmatch;
2286 char *id_str = NULL, *logmsg = NULL;
2288 *have_match = 0;
2290 err = got_object_id_str(&id_str, id);
2291 if (err)
2292 return err;
2294 err = got_object_commit_get_logmsg(&logmsg, commit);
2295 if (err)
2296 goto done;
2298 if (regexec(regex, got_object_commit_get_author(commit), 1,
2299 &regmatch, 0) == 0 ||
2300 regexec(regex, got_object_commit_get_committer(commit), 1,
2301 &regmatch, 0) == 0 ||
2302 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2303 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2304 *have_match = 1;
2305 done:
2306 free(id_str);
2307 free(logmsg);
2308 return err;
2311 static const struct got_error *
2312 queue_commits(struct tog_log_thread_args *a)
2314 const struct got_error *err = NULL;
2317 * We keep all commits open throughout the lifetime of the log
2318 * view in order to avoid having to re-fetch commits from disk
2319 * while updating the display.
2321 do {
2322 struct got_object_id id;
2323 struct got_commit_object *commit;
2324 struct commit_queue_entry *entry;
2325 int limit_match = 0;
2326 int errcode;
2328 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2329 NULL, NULL);
2330 if (err)
2331 break;
2333 err = got_object_open_as_commit(&commit, a->repo, &id);
2334 if (err)
2335 break;
2336 entry = alloc_commit_queue_entry(commit, &id);
2337 if (entry == NULL) {
2338 err = got_error_from_errno("alloc_commit_queue_entry");
2339 break;
2342 errcode = pthread_mutex_lock(&tog_mutex);
2343 if (errcode) {
2344 err = got_error_set_errno(errcode,
2345 "pthread_mutex_lock");
2346 break;
2349 entry->idx = a->real_commits->ncommits;
2350 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2351 a->real_commits->ncommits++;
2353 if (*a->limiting) {
2354 err = match_commit(&limit_match, &id, commit,
2355 a->limit_regex);
2356 if (err)
2357 break;
2359 if (limit_match) {
2360 struct commit_queue_entry *matched;
2362 matched = alloc_commit_queue_entry(
2363 entry->commit, entry->id);
2364 if (matched == NULL) {
2365 err = got_error_from_errno(
2366 "alloc_commit_queue_entry");
2367 break;
2369 matched->commit = entry->commit;
2370 got_object_commit_retain(entry->commit);
2372 matched->idx = a->limit_commits->ncommits;
2373 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2374 matched, entry);
2375 a->limit_commits->ncommits++;
2379 * This is how we signal log_thread() that we
2380 * have found a match, and that it should be
2381 * counted as a new entry for the view.
2383 a->limit_match = limit_match;
2386 if (*a->searching == TOG_SEARCH_FORWARD &&
2387 !*a->search_next_done) {
2388 int have_match;
2389 err = match_commit(&have_match, &id, commit, a->regex);
2390 if (err)
2391 break;
2393 if (*a->limiting) {
2394 if (limit_match && have_match)
2395 *a->search_next_done =
2396 TOG_SEARCH_HAVE_MORE;
2397 } else if (have_match)
2398 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2401 errcode = pthread_mutex_unlock(&tog_mutex);
2402 if (errcode && err == NULL)
2403 err = got_error_set_errno(errcode,
2404 "pthread_mutex_unlock");
2405 if (err)
2406 break;
2407 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2409 return err;
2412 static void
2413 select_commit(struct tog_log_view_state *s)
2415 struct commit_queue_entry *entry;
2416 int ncommits = 0;
2418 entry = s->first_displayed_entry;
2419 while (entry) {
2420 if (ncommits == s->selected) {
2421 s->selected_entry = entry;
2422 break;
2424 entry = TAILQ_NEXT(entry, entry);
2425 ncommits++;
2429 static const struct got_error *
2430 draw_commits(struct tog_view *view)
2432 const struct got_error *err = NULL;
2433 struct tog_log_view_state *s = &view->state.log;
2434 struct commit_queue_entry *entry = s->selected_entry;
2435 int limit = view->nlines;
2436 int width;
2437 int ncommits, author_cols = 4;
2438 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2439 char *refs_str = NULL;
2440 wchar_t *wline;
2441 struct tog_color *tc;
2442 static const size_t date_display_cols = 12;
2444 if (view_is_hsplit_top(view))
2445 --limit; /* account for border */
2447 if (s->selected_entry &&
2448 !(view->searching && view->search_next_done == 0)) {
2449 struct got_reflist_head *refs;
2450 err = got_object_id_str(&id_str, s->selected_entry->id);
2451 if (err)
2452 return err;
2453 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2454 s->selected_entry->id);
2455 if (refs) {
2456 err = build_refs_str(&refs_str, refs,
2457 s->selected_entry->id, s->repo);
2458 if (err)
2459 goto done;
2463 if (s->thread_args.commits_needed == 0)
2464 halfdelay(10); /* disable fast refresh */
2466 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2467 if (asprintf(&ncommits_str, " [%d/%d] %s",
2468 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2469 (view->searching && !view->search_next_done) ?
2470 "searching..." : "loading...") == -1) {
2471 err = got_error_from_errno("asprintf");
2472 goto done;
2474 } else {
2475 const char *search_str = NULL;
2476 const char *limit_str = NULL;
2478 if (view->searching) {
2479 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2480 search_str = "no more matches";
2481 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2482 search_str = "no matches found";
2483 else if (!view->search_next_done)
2484 search_str = "searching...";
2487 if (s->limit_view && s->commits->ncommits == 0)
2488 limit_str = "no matches found";
2490 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2491 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2492 search_str ? search_str : (refs_str ? refs_str : ""),
2493 limit_str ? limit_str : "") == -1) {
2494 err = got_error_from_errno("asprintf");
2495 goto done;
2499 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2500 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2501 "........................................",
2502 s->in_repo_path, ncommits_str) == -1) {
2503 err = got_error_from_errno("asprintf");
2504 header = NULL;
2505 goto done;
2507 } else if (asprintf(&header, "commit %s%s",
2508 id_str ? id_str : "........................................",
2509 ncommits_str) == -1) {
2510 err = got_error_from_errno("asprintf");
2511 header = NULL;
2512 goto done;
2514 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2515 if (err)
2516 goto done;
2518 werase(view->window);
2520 if (view_needs_focus_indication(view))
2521 wstandout(view->window);
2522 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2523 if (tc)
2524 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2525 waddwstr(view->window, wline);
2526 while (width < view->ncols) {
2527 waddch(view->window, ' ');
2528 width++;
2530 if (tc)
2531 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2532 if (view_needs_focus_indication(view))
2533 wstandend(view->window);
2534 free(wline);
2535 if (limit <= 1)
2536 goto done;
2538 /* Grow author column size if necessary, and set view->maxx. */
2539 entry = s->first_displayed_entry;
2540 ncommits = 0;
2541 view->maxx = 0;
2542 while (entry) {
2543 struct got_commit_object *c = entry->commit;
2544 char *author, *eol, *msg, *msg0;
2545 wchar_t *wauthor, *wmsg;
2546 int width;
2547 if (ncommits >= limit - 1)
2548 break;
2549 if (s->use_committer)
2550 author = strdup(got_object_commit_get_committer(c));
2551 else
2552 author = strdup(got_object_commit_get_author(c));
2553 if (author == NULL) {
2554 err = got_error_from_errno("strdup");
2555 goto done;
2557 err = format_author(&wauthor, &width, author, COLS,
2558 date_display_cols);
2559 if (author_cols < width)
2560 author_cols = width;
2561 free(wauthor);
2562 free(author);
2563 if (err)
2564 goto done;
2565 err = got_object_commit_get_logmsg(&msg0, c);
2566 if (err)
2567 goto done;
2568 msg = msg0;
2569 while (*msg == '\n')
2570 ++msg;
2571 if ((eol = strchr(msg, '\n')))
2572 *eol = '\0';
2573 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2574 date_display_cols + author_cols, 0);
2575 if (err)
2576 goto done;
2577 view->maxx = MAX(view->maxx, width);
2578 free(msg0);
2579 free(wmsg);
2580 ncommits++;
2581 entry = TAILQ_NEXT(entry, entry);
2584 entry = s->first_displayed_entry;
2585 s->last_displayed_entry = s->first_displayed_entry;
2586 ncommits = 0;
2587 while (entry) {
2588 if (ncommits >= limit - 1)
2589 break;
2590 if (ncommits == s->selected)
2591 wstandout(view->window);
2592 err = draw_commit(view, entry->commit, entry->id,
2593 date_display_cols, author_cols);
2594 if (ncommits == s->selected)
2595 wstandend(view->window);
2596 if (err)
2597 goto done;
2598 ncommits++;
2599 s->last_displayed_entry = entry;
2600 entry = TAILQ_NEXT(entry, entry);
2603 view_border(view);
2604 done:
2605 free(id_str);
2606 free(refs_str);
2607 free(ncommits_str);
2608 free(header);
2609 return err;
2612 static void
2613 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2615 struct commit_queue_entry *entry;
2616 int nscrolled = 0;
2618 entry = TAILQ_FIRST(&s->commits->head);
2619 if (s->first_displayed_entry == entry)
2620 return;
2622 entry = s->first_displayed_entry;
2623 while (entry && nscrolled < maxscroll) {
2624 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2625 if (entry) {
2626 s->first_displayed_entry = entry;
2627 nscrolled++;
2632 static const struct got_error *
2633 trigger_log_thread(struct tog_view *view, int wait)
2635 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2636 int errcode;
2638 halfdelay(1); /* fast refresh while loading commits */
2640 while (!ta->log_complete && !tog_thread_error &&
2641 (ta->commits_needed > 0 || ta->load_all)) {
2642 /* Wake the log thread. */
2643 errcode = pthread_cond_signal(&ta->need_commits);
2644 if (errcode)
2645 return got_error_set_errno(errcode,
2646 "pthread_cond_signal");
2649 * The mutex will be released while the view loop waits
2650 * in wgetch(), at which time the log thread will run.
2652 if (!wait)
2653 break;
2655 /* Display progress update in log view. */
2656 show_log_view(view);
2657 update_panels();
2658 doupdate();
2660 /* Wait right here while next commit is being loaded. */
2661 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2662 if (errcode)
2663 return got_error_set_errno(errcode,
2664 "pthread_cond_wait");
2666 /* Display progress update in log view. */
2667 show_log_view(view);
2668 update_panels();
2669 doupdate();
2672 return NULL;
2675 static const struct got_error *
2676 request_log_commits(struct tog_view *view)
2678 struct tog_log_view_state *state = &view->state.log;
2679 const struct got_error *err = NULL;
2681 if (state->thread_args.log_complete)
2682 return NULL;
2684 state->thread_args.commits_needed += view->nscrolled;
2685 err = trigger_log_thread(view, 1);
2686 view->nscrolled = 0;
2688 return err;
2691 static const struct got_error *
2692 log_scroll_down(struct tog_view *view, int maxscroll)
2694 struct tog_log_view_state *s = &view->state.log;
2695 const struct got_error *err = NULL;
2696 struct commit_queue_entry *pentry;
2697 int nscrolled = 0, ncommits_needed;
2699 if (s->last_displayed_entry == NULL)
2700 return NULL;
2702 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2703 if (s->commits->ncommits < ncommits_needed &&
2704 !s->thread_args.log_complete) {
2706 * Ask the log thread for required amount of commits.
2708 s->thread_args.commits_needed +=
2709 ncommits_needed - s->commits->ncommits;
2710 err = trigger_log_thread(view, 1);
2711 if (err)
2712 return err;
2715 do {
2716 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2717 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2718 break;
2720 s->last_displayed_entry = pentry ?
2721 pentry : s->last_displayed_entry;;
2723 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2724 if (pentry == NULL)
2725 break;
2726 s->first_displayed_entry = pentry;
2727 } while (++nscrolled < maxscroll);
2729 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2730 view->nscrolled += nscrolled;
2731 else
2732 view->nscrolled = 0;
2734 return err;
2737 static const struct got_error *
2738 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2739 struct got_commit_object *commit, struct got_object_id *commit_id,
2740 struct tog_view *log_view, struct got_repository *repo)
2742 const struct got_error *err;
2743 struct got_object_qid *parent_id;
2744 struct tog_view *diff_view;
2746 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2747 if (diff_view == NULL)
2748 return got_error_from_errno("view_open");
2750 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2751 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2752 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2753 if (err == NULL)
2754 *new_view = diff_view;
2755 return err;
2758 static const struct got_error *
2759 tree_view_visit_subtree(struct tog_tree_view_state *s,
2760 struct got_tree_object *subtree)
2762 struct tog_parent_tree *parent;
2764 parent = calloc(1, sizeof(*parent));
2765 if (parent == NULL)
2766 return got_error_from_errno("calloc");
2768 parent->tree = s->tree;
2769 parent->first_displayed_entry = s->first_displayed_entry;
2770 parent->selected_entry = s->selected_entry;
2771 parent->selected = s->selected;
2772 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2773 s->tree = subtree;
2774 s->selected = 0;
2775 s->first_displayed_entry = NULL;
2776 return NULL;
2779 static const struct got_error *
2780 tree_view_walk_path(struct tog_tree_view_state *s,
2781 struct got_commit_object *commit, const char *path)
2783 const struct got_error *err = NULL;
2784 struct got_tree_object *tree = NULL;
2785 const char *p;
2786 char *slash, *subpath = NULL;
2788 /* Walk the path and open corresponding tree objects. */
2789 p = path;
2790 while (*p) {
2791 struct got_tree_entry *te;
2792 struct got_object_id *tree_id;
2793 char *te_name;
2795 while (p[0] == '/')
2796 p++;
2798 /* Ensure the correct subtree entry is selected. */
2799 slash = strchr(p, '/');
2800 if (slash == NULL)
2801 te_name = strdup(p);
2802 else
2803 te_name = strndup(p, slash - p);
2804 if (te_name == NULL) {
2805 err = got_error_from_errno("strndup");
2806 break;
2808 te = got_object_tree_find_entry(s->tree, te_name);
2809 if (te == NULL) {
2810 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2811 free(te_name);
2812 break;
2814 free(te_name);
2815 s->first_displayed_entry = s->selected_entry = te;
2817 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2818 break; /* jump to this file's entry */
2820 slash = strchr(p, '/');
2821 if (slash)
2822 subpath = strndup(path, slash - path);
2823 else
2824 subpath = strdup(path);
2825 if (subpath == NULL) {
2826 err = got_error_from_errno("strdup");
2827 break;
2830 err = got_object_id_by_path(&tree_id, s->repo, commit,
2831 subpath);
2832 if (err)
2833 break;
2835 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2836 free(tree_id);
2837 if (err)
2838 break;
2840 err = tree_view_visit_subtree(s, tree);
2841 if (err) {
2842 got_object_tree_close(tree);
2843 break;
2845 if (slash == NULL)
2846 break;
2847 free(subpath);
2848 subpath = NULL;
2849 p = slash;
2852 free(subpath);
2853 return err;
2856 static const struct got_error *
2857 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2858 struct commit_queue_entry *entry, const char *path,
2859 const char *head_ref_name, struct got_repository *repo)
2861 const struct got_error *err = NULL;
2862 struct tog_tree_view_state *s;
2863 struct tog_view *tree_view;
2865 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2866 if (tree_view == NULL)
2867 return got_error_from_errno("view_open");
2869 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2870 if (err)
2871 return err;
2872 s = &tree_view->state.tree;
2874 *new_view = tree_view;
2876 if (got_path_is_root_dir(path))
2877 return NULL;
2879 return tree_view_walk_path(s, entry->commit, path);
2882 static const struct got_error *
2883 block_signals_used_by_main_thread(void)
2885 sigset_t sigset;
2886 int errcode;
2888 if (sigemptyset(&sigset) == -1)
2889 return got_error_from_errno("sigemptyset");
2891 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2892 if (sigaddset(&sigset, SIGWINCH) == -1)
2893 return got_error_from_errno("sigaddset");
2894 if (sigaddset(&sigset, SIGCONT) == -1)
2895 return got_error_from_errno("sigaddset");
2896 if (sigaddset(&sigset, SIGINT) == -1)
2897 return got_error_from_errno("sigaddset");
2898 if (sigaddset(&sigset, SIGTERM) == -1)
2899 return got_error_from_errno("sigaddset");
2901 /* ncurses handles SIGTSTP */
2902 if (sigaddset(&sigset, SIGTSTP) == -1)
2903 return got_error_from_errno("sigaddset");
2905 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2906 if (errcode)
2907 return got_error_set_errno(errcode, "pthread_sigmask");
2909 return NULL;
2912 static void *
2913 log_thread(void *arg)
2915 const struct got_error *err = NULL;
2916 int errcode = 0;
2917 struct tog_log_thread_args *a = arg;
2918 int done = 0;
2921 * Sync startup with main thread such that we begin our
2922 * work once view_input() has released the mutex.
2924 errcode = pthread_mutex_lock(&tog_mutex);
2925 if (errcode) {
2926 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2927 return (void *)err;
2930 err = block_signals_used_by_main_thread();
2931 if (err) {
2932 pthread_mutex_unlock(&tog_mutex);
2933 goto done;
2936 while (!done && !err && !tog_fatal_signal_received()) {
2937 errcode = pthread_mutex_unlock(&tog_mutex);
2938 if (errcode) {
2939 err = got_error_set_errno(errcode,
2940 "pthread_mutex_unlock");
2941 goto done;
2943 err = queue_commits(a);
2944 if (err) {
2945 if (err->code != GOT_ERR_ITER_COMPLETED)
2946 goto done;
2947 err = NULL;
2948 done = 1;
2949 } else if (a->commits_needed > 0 && !a->load_all) {
2950 if (*a->limiting) {
2951 if (a->limit_match)
2952 a->commits_needed--;
2953 } else
2954 a->commits_needed--;
2957 errcode = pthread_mutex_lock(&tog_mutex);
2958 if (errcode) {
2959 err = got_error_set_errno(errcode,
2960 "pthread_mutex_lock");
2961 goto done;
2962 } else if (*a->quit)
2963 done = 1;
2964 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2965 *a->first_displayed_entry =
2966 TAILQ_FIRST(&a->limit_commits->head);
2967 *a->selected_entry = *a->first_displayed_entry;
2968 } else if (*a->first_displayed_entry == NULL) {
2969 *a->first_displayed_entry =
2970 TAILQ_FIRST(&a->real_commits->head);
2971 *a->selected_entry = *a->first_displayed_entry;
2974 errcode = pthread_cond_signal(&a->commit_loaded);
2975 if (errcode) {
2976 err = got_error_set_errno(errcode,
2977 "pthread_cond_signal");
2978 pthread_mutex_unlock(&tog_mutex);
2979 goto done;
2982 if (done)
2983 a->commits_needed = 0;
2984 else {
2985 if (a->commits_needed == 0 && !a->load_all) {
2986 errcode = pthread_cond_wait(&a->need_commits,
2987 &tog_mutex);
2988 if (errcode) {
2989 err = got_error_set_errno(errcode,
2990 "pthread_cond_wait");
2991 pthread_mutex_unlock(&tog_mutex);
2992 goto done;
2994 if (*a->quit)
2995 done = 1;
2999 a->log_complete = 1;
3000 errcode = pthread_mutex_unlock(&tog_mutex);
3001 if (errcode)
3002 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3003 done:
3004 if (err) {
3005 tog_thread_error = 1;
3006 pthread_cond_signal(&a->commit_loaded);
3008 return (void *)err;
3011 static const struct got_error *
3012 stop_log_thread(struct tog_log_view_state *s)
3014 const struct got_error *err = NULL, *thread_err = NULL;
3015 int errcode;
3017 if (s->thread) {
3018 s->quit = 1;
3019 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3020 if (errcode)
3021 return got_error_set_errno(errcode,
3022 "pthread_cond_signal");
3023 errcode = pthread_mutex_unlock(&tog_mutex);
3024 if (errcode)
3025 return got_error_set_errno(errcode,
3026 "pthread_mutex_unlock");
3027 errcode = pthread_join(s->thread, (void **)&thread_err);
3028 if (errcode)
3029 return got_error_set_errno(errcode, "pthread_join");
3030 errcode = pthread_mutex_lock(&tog_mutex);
3031 if (errcode)
3032 return got_error_set_errno(errcode,
3033 "pthread_mutex_lock");
3034 s->thread = NULL;
3037 if (s->thread_args.repo) {
3038 err = got_repo_close(s->thread_args.repo);
3039 s->thread_args.repo = NULL;
3042 if (s->thread_args.pack_fds) {
3043 const struct got_error *pack_err =
3044 got_repo_pack_fds_close(s->thread_args.pack_fds);
3045 if (err == NULL)
3046 err = pack_err;
3047 s->thread_args.pack_fds = NULL;
3050 if (s->thread_args.graph) {
3051 got_commit_graph_close(s->thread_args.graph);
3052 s->thread_args.graph = NULL;
3055 return err ? err : thread_err;
3058 static const struct got_error *
3059 close_log_view(struct tog_view *view)
3061 const struct got_error *err = NULL;
3062 struct tog_log_view_state *s = &view->state.log;
3063 int errcode;
3065 err = stop_log_thread(s);
3067 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3068 if (errcode && err == NULL)
3069 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3071 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3072 if (errcode && err == NULL)
3073 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3075 free_commits(&s->limit_commits);
3076 free_commits(&s->real_commits);
3077 free(s->in_repo_path);
3078 s->in_repo_path = NULL;
3079 free(s->start_id);
3080 s->start_id = NULL;
3081 free(s->head_ref_name);
3082 s->head_ref_name = NULL;
3083 return err;
3087 * We use two queues to implement the limit feature: first consists of
3088 * commits matching the current limit_regex; second is the real queue
3089 * of all known commits (real_commits). When the user starts limiting,
3090 * we swap queues such that all movement and displaying functionality
3091 * works with very slight change.
3093 static const struct got_error *
3094 limit_log_view(struct tog_view *view)
3096 struct tog_log_view_state *s = &view->state.log;
3097 struct commit_queue_entry *entry;
3098 struct tog_view *v = view;
3099 const struct got_error *err = NULL;
3100 char pattern[1024];
3101 int ret;
3103 if (view_is_hsplit_top(view))
3104 v = view->child;
3105 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3106 v = view->parent;
3108 /* Get the pattern */
3109 wmove(v->window, v->nlines - 1, 0);
3110 wclrtoeol(v->window);
3111 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3112 nodelay(v->window, FALSE);
3113 nocbreak();
3114 echo();
3115 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3116 cbreak();
3117 noecho();
3118 nodelay(v->window, TRUE);
3119 if (ret == ERR)
3120 return NULL;
3122 if (*pattern == '\0') {
3124 * Safety measure for the situation where the user
3125 * resets limit without previously limiting anything.
3127 if (!s->limit_view)
3128 return NULL;
3131 * User could have pressed Ctrl+L, which refreshed the
3132 * commit queues, it means we can't save previously
3133 * (before limit took place) displayed entries,
3134 * because they would point to already free'ed memory,
3135 * so we are forced to always select first entry of
3136 * the queue.
3138 s->commits = &s->real_commits;
3139 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3140 s->selected_entry = s->first_displayed_entry;
3141 s->selected = 0;
3142 s->limit_view = 0;
3144 return NULL;
3147 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3148 return NULL;
3150 s->limit_view = 1;
3152 /* Clear the screen while loading limit view */
3153 s->first_displayed_entry = NULL;
3154 s->last_displayed_entry = NULL;
3155 s->selected_entry = NULL;
3156 s->commits = &s->limit_commits;
3158 /* Prepare limit queue for new search */
3159 free_commits(&s->limit_commits);
3160 s->limit_commits.ncommits = 0;
3162 /* First process commits, which are in queue already */
3163 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3164 int have_match = 0;
3166 err = match_commit(&have_match, entry->id,
3167 entry->commit, &s->limit_regex);
3168 if (err)
3169 return err;
3171 if (have_match) {
3172 struct commit_queue_entry *matched;
3174 matched = alloc_commit_queue_entry(entry->commit,
3175 entry->id);
3176 if (matched == NULL) {
3177 err = got_error_from_errno(
3178 "alloc_commit_queue_entry");
3179 break;
3181 matched->commit = entry->commit;
3182 got_object_commit_retain(entry->commit);
3184 matched->idx = s->limit_commits.ncommits;
3185 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3186 matched, entry);
3187 s->limit_commits.ncommits++;
3191 /* Second process all the commits, until we fill the screen */
3192 if (s->limit_commits.ncommits < view->nlines - 1 &&
3193 !s->thread_args.log_complete) {
3194 s->thread_args.commits_needed +=
3195 view->nlines - s->limit_commits.ncommits - 1;
3196 err = trigger_log_thread(view, 1);
3197 if (err)
3198 return err;
3201 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3202 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3203 s->selected = 0;
3205 return NULL;
3208 static const struct got_error *
3209 search_start_log_view(struct tog_view *view)
3211 struct tog_log_view_state *s = &view->state.log;
3213 s->matched_entry = NULL;
3214 s->search_entry = NULL;
3215 return NULL;
3218 static const struct got_error *
3219 search_next_log_view(struct tog_view *view)
3221 const struct got_error *err = NULL;
3222 struct tog_log_view_state *s = &view->state.log;
3223 struct commit_queue_entry *entry;
3225 /* Display progress update in log view. */
3226 show_log_view(view);
3227 update_panels();
3228 doupdate();
3230 if (s->search_entry) {
3231 int errcode, ch;
3232 errcode = pthread_mutex_unlock(&tog_mutex);
3233 if (errcode)
3234 return got_error_set_errno(errcode,
3235 "pthread_mutex_unlock");
3236 ch = wgetch(view->window);
3237 errcode = pthread_mutex_lock(&tog_mutex);
3238 if (errcode)
3239 return got_error_set_errno(errcode,
3240 "pthread_mutex_lock");
3241 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3242 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3243 return NULL;
3245 if (view->searching == TOG_SEARCH_FORWARD)
3246 entry = TAILQ_NEXT(s->search_entry, entry);
3247 else
3248 entry = TAILQ_PREV(s->search_entry,
3249 commit_queue_head, entry);
3250 } else if (s->matched_entry) {
3252 * If the user has moved the cursor after we hit a match,
3253 * the position from where we should continue searching
3254 * might have changed.
3256 if (view->searching == TOG_SEARCH_FORWARD)
3257 entry = TAILQ_NEXT(s->selected_entry, entry);
3258 else
3259 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3260 entry);
3261 } else {
3262 entry = s->selected_entry;
3265 while (1) {
3266 int have_match = 0;
3268 if (entry == NULL) {
3269 if (s->thread_args.log_complete ||
3270 view->searching == TOG_SEARCH_BACKWARD) {
3271 view->search_next_done =
3272 (s->matched_entry == NULL ?
3273 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3274 s->search_entry = NULL;
3275 return NULL;
3278 * Poke the log thread for more commits and return,
3279 * allowing the main loop to make progress. Search
3280 * will resume at s->search_entry once we come back.
3282 s->thread_args.commits_needed++;
3283 return trigger_log_thread(view, 0);
3286 err = match_commit(&have_match, entry->id, entry->commit,
3287 &view->regex);
3288 if (err)
3289 break;
3290 if (have_match) {
3291 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3292 s->matched_entry = entry;
3293 break;
3296 s->search_entry = entry;
3297 if (view->searching == TOG_SEARCH_FORWARD)
3298 entry = TAILQ_NEXT(entry, entry);
3299 else
3300 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3303 if (s->matched_entry) {
3304 int cur = s->selected_entry->idx;
3305 while (cur < s->matched_entry->idx) {
3306 err = input_log_view(NULL, view, KEY_DOWN);
3307 if (err)
3308 return err;
3309 cur++;
3311 while (cur > s->matched_entry->idx) {
3312 err = input_log_view(NULL, view, KEY_UP);
3313 if (err)
3314 return err;
3315 cur--;
3319 s->search_entry = NULL;
3321 return NULL;
3324 static const struct got_error *
3325 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3326 struct got_repository *repo, const char *head_ref_name,
3327 const char *in_repo_path, int log_branches)
3329 const struct got_error *err = NULL;
3330 struct tog_log_view_state *s = &view->state.log;
3331 struct got_repository *thread_repo = NULL;
3332 struct got_commit_graph *thread_graph = NULL;
3333 int errcode;
3335 if (in_repo_path != s->in_repo_path) {
3336 free(s->in_repo_path);
3337 s->in_repo_path = strdup(in_repo_path);
3338 if (s->in_repo_path == NULL)
3339 return got_error_from_errno("strdup");
3342 /* The commit queue only contains commits being displayed. */
3343 TAILQ_INIT(&s->real_commits.head);
3344 s->real_commits.ncommits = 0;
3345 s->commits = &s->real_commits;
3347 TAILQ_INIT(&s->limit_commits.head);
3348 s->limit_view = 0;
3349 s->limit_commits.ncommits = 0;
3351 s->repo = repo;
3352 if (head_ref_name) {
3353 s->head_ref_name = strdup(head_ref_name);
3354 if (s->head_ref_name == NULL) {
3355 err = got_error_from_errno("strdup");
3356 goto done;
3359 s->start_id = got_object_id_dup(start_id);
3360 if (s->start_id == NULL) {
3361 err = got_error_from_errno("got_object_id_dup");
3362 goto done;
3364 s->log_branches = log_branches;
3366 STAILQ_INIT(&s->colors);
3367 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3368 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3369 get_color_value("TOG_COLOR_COMMIT"));
3370 if (err)
3371 goto done;
3372 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3373 get_color_value("TOG_COLOR_AUTHOR"));
3374 if (err) {
3375 free_colors(&s->colors);
3376 goto done;
3378 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3379 get_color_value("TOG_COLOR_DATE"));
3380 if (err) {
3381 free_colors(&s->colors);
3382 goto done;
3386 view->show = show_log_view;
3387 view->input = input_log_view;
3388 view->resize = resize_log_view;
3389 view->close = close_log_view;
3390 view->search_start = search_start_log_view;
3391 view->search_next = search_next_log_view;
3393 if (s->thread_args.pack_fds == NULL) {
3394 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3395 if (err)
3396 goto done;
3398 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3399 s->thread_args.pack_fds);
3400 if (err)
3401 goto done;
3402 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3403 !s->log_branches);
3404 if (err)
3405 goto done;
3406 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3407 s->repo, NULL, NULL);
3408 if (err)
3409 goto done;
3411 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3412 if (errcode) {
3413 err = got_error_set_errno(errcode, "pthread_cond_init");
3414 goto done;
3416 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3417 if (errcode) {
3418 err = got_error_set_errno(errcode, "pthread_cond_init");
3419 goto done;
3422 s->thread_args.commits_needed = view->nlines;
3423 s->thread_args.graph = thread_graph;
3424 s->thread_args.real_commits = &s->real_commits;
3425 s->thread_args.limit_commits = &s->limit_commits;
3426 s->thread_args.in_repo_path = s->in_repo_path;
3427 s->thread_args.start_id = s->start_id;
3428 s->thread_args.repo = thread_repo;
3429 s->thread_args.log_complete = 0;
3430 s->thread_args.quit = &s->quit;
3431 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3432 s->thread_args.selected_entry = &s->selected_entry;
3433 s->thread_args.searching = &view->searching;
3434 s->thread_args.search_next_done = &view->search_next_done;
3435 s->thread_args.regex = &view->regex;
3436 s->thread_args.limiting = &s->limit_view;
3437 s->thread_args.limit_regex = &s->limit_regex;
3438 s->thread_args.limit_commits = &s->limit_commits;
3439 done:
3440 if (err)
3441 close_log_view(view);
3442 return err;
3445 static const struct got_error *
3446 show_log_view(struct tog_view *view)
3448 const struct got_error *err;
3449 struct tog_log_view_state *s = &view->state.log;
3451 if (s->thread == NULL) {
3452 int errcode = pthread_create(&s->thread, NULL, log_thread,
3453 &s->thread_args);
3454 if (errcode)
3455 return got_error_set_errno(errcode, "pthread_create");
3456 if (s->thread_args.commits_needed > 0) {
3457 err = trigger_log_thread(view, 1);
3458 if (err)
3459 return err;
3463 return draw_commits(view);
3466 static void
3467 log_move_cursor_up(struct tog_view *view, int page, int home)
3469 struct tog_log_view_state *s = &view->state.log;
3471 if (s->first_displayed_entry == NULL)
3472 return;
3473 if (s->selected_entry->idx == 0)
3474 view->count = 0;
3476 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3477 || home)
3478 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3480 if (!page && !home && s->selected > 0)
3481 --s->selected;
3482 else
3483 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3485 select_commit(s);
3486 return;
3489 static const struct got_error *
3490 log_move_cursor_down(struct tog_view *view, int page)
3492 struct tog_log_view_state *s = &view->state.log;
3493 const struct got_error *err = NULL;
3494 int eos = view->nlines - 2;
3496 if (s->first_displayed_entry == NULL)
3497 return NULL;
3499 if (s->thread_args.log_complete &&
3500 s->selected_entry->idx >= s->commits->ncommits - 1)
3501 return NULL;
3503 if (view_is_hsplit_top(view))
3504 --eos; /* border consumes the last line */
3506 if (!page) {
3507 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3508 ++s->selected;
3509 else
3510 err = log_scroll_down(view, 1);
3511 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3512 struct commit_queue_entry *entry;
3513 int n;
3515 s->selected = 0;
3516 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3517 s->last_displayed_entry = entry;
3518 for (n = 0; n <= eos; n++) {
3519 if (entry == NULL)
3520 break;
3521 s->first_displayed_entry = entry;
3522 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3524 if (n > 0)
3525 s->selected = n - 1;
3526 } else {
3527 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3528 s->thread_args.log_complete)
3529 s->selected += MIN(page,
3530 s->commits->ncommits - s->selected_entry->idx - 1);
3531 else
3532 err = log_scroll_down(view, page);
3534 if (err)
3535 return err;
3538 * We might necessarily overshoot in horizontal
3539 * splits; if so, select the last displayed commit.
3541 if (s->first_displayed_entry && s->last_displayed_entry) {
3542 s->selected = MIN(s->selected,
3543 s->last_displayed_entry->idx -
3544 s->first_displayed_entry->idx);
3547 select_commit(s);
3549 if (s->thread_args.log_complete &&
3550 s->selected_entry->idx == s->commits->ncommits - 1)
3551 view->count = 0;
3553 return NULL;
3556 static void
3557 view_get_split(struct tog_view *view, int *y, int *x)
3559 *x = 0;
3560 *y = 0;
3562 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3563 if (view->child && view->child->resized_y)
3564 *y = view->child->resized_y;
3565 else if (view->resized_y)
3566 *y = view->resized_y;
3567 else
3568 *y = view_split_begin_y(view->lines);
3569 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3570 if (view->child && view->child->resized_x)
3571 *x = view->child->resized_x;
3572 else if (view->resized_x)
3573 *x = view->resized_x;
3574 else
3575 *x = view_split_begin_x(view->begin_x);
3579 /* Split view horizontally at y and offset view->state->selected line. */
3580 static const struct got_error *
3581 view_init_hsplit(struct tog_view *view, int y)
3583 const struct got_error *err = NULL;
3585 view->nlines = y;
3586 view->ncols = COLS;
3587 err = view_resize(view);
3588 if (err)
3589 return err;
3591 err = offset_selection_down(view);
3593 return err;
3596 static const struct got_error *
3597 log_goto_line(struct tog_view *view, int nlines)
3599 const struct got_error *err = NULL;
3600 struct tog_log_view_state *s = &view->state.log;
3601 int g, idx = s->selected_entry->idx;
3603 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3604 return NULL;
3606 g = view->gline;
3607 view->gline = 0;
3609 if (g >= s->first_displayed_entry->idx + 1 &&
3610 g <= s->last_displayed_entry->idx + 1 &&
3611 g - s->first_displayed_entry->idx - 1 < nlines) {
3612 s->selected = g - s->first_displayed_entry->idx - 1;
3613 select_commit(s);
3614 return NULL;
3617 if (idx + 1 < g) {
3618 err = log_move_cursor_down(view, g - idx - 1);
3619 if (!err && g > s->selected_entry->idx + 1)
3620 err = log_move_cursor_down(view,
3621 g - s->first_displayed_entry->idx - 1);
3622 if (err)
3623 return err;
3624 } else if (idx + 1 > g)
3625 log_move_cursor_up(view, idx - g + 1, 0);
3627 if (g < nlines && s->first_displayed_entry->idx == 0)
3628 s->selected = g - 1;
3630 select_commit(s);
3631 return NULL;
3635 static const struct got_error *
3636 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3638 const struct got_error *err = NULL;
3639 struct tog_log_view_state *s = &view->state.log;
3640 int eos, nscroll;
3642 if (s->thread_args.load_all) {
3643 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3644 s->thread_args.load_all = 0;
3645 else if (s->thread_args.log_complete) {
3646 err = log_move_cursor_down(view, s->commits->ncommits);
3647 s->thread_args.load_all = 0;
3649 if (err)
3650 return err;
3653 eos = nscroll = view->nlines - 1;
3654 if (view_is_hsplit_top(view))
3655 --eos; /* border */
3657 if (view->gline)
3658 return log_goto_line(view, eos);
3660 switch (ch) {
3661 case '&':
3662 err = limit_log_view(view);
3663 break;
3664 case 'q':
3665 s->quit = 1;
3666 break;
3667 case '0':
3668 view->x = 0;
3669 break;
3670 case '$':
3671 view->x = MAX(view->maxx - view->ncols / 2, 0);
3672 view->count = 0;
3673 break;
3674 case KEY_RIGHT:
3675 case 'l':
3676 if (view->x + view->ncols / 2 < view->maxx)
3677 view->x += 2; /* move two columns right */
3678 else
3679 view->count = 0;
3680 break;
3681 case KEY_LEFT:
3682 case 'h':
3683 view->x -= MIN(view->x, 2); /* move two columns back */
3684 if (view->x <= 0)
3685 view->count = 0;
3686 break;
3687 case 'k':
3688 case KEY_UP:
3689 case '<':
3690 case ',':
3691 case CTRL('p'):
3692 log_move_cursor_up(view, 0, 0);
3693 break;
3694 case 'g':
3695 case KEY_HOME:
3696 log_move_cursor_up(view, 0, 1);
3697 view->count = 0;
3698 break;
3699 case CTRL('u'):
3700 case 'u':
3701 nscroll /= 2;
3702 /* FALL THROUGH */
3703 case KEY_PPAGE:
3704 case CTRL('b'):
3705 case 'b':
3706 log_move_cursor_up(view, nscroll, 0);
3707 break;
3708 case 'j':
3709 case KEY_DOWN:
3710 case '>':
3711 case '.':
3712 case CTRL('n'):
3713 err = log_move_cursor_down(view, 0);
3714 break;
3715 case '@':
3716 s->use_committer = !s->use_committer;
3717 break;
3718 case 'G':
3719 case KEY_END: {
3720 /* We don't know yet how many commits, so we're forced to
3721 * traverse them all. */
3722 view->count = 0;
3723 s->thread_args.load_all = 1;
3724 if (!s->thread_args.log_complete)
3725 return trigger_log_thread(view, 0);
3726 err = log_move_cursor_down(view, s->commits->ncommits);
3727 s->thread_args.load_all = 0;
3728 break;
3730 case CTRL('d'):
3731 case 'd':
3732 nscroll /= 2;
3733 /* FALL THROUGH */
3734 case KEY_NPAGE:
3735 case CTRL('f'):
3736 case 'f':
3737 case ' ':
3738 err = log_move_cursor_down(view, nscroll);
3739 break;
3740 case KEY_RESIZE:
3741 if (s->selected > view->nlines - 2)
3742 s->selected = view->nlines - 2;
3743 if (s->selected > s->commits->ncommits - 1)
3744 s->selected = s->commits->ncommits - 1;
3745 select_commit(s);
3746 if (s->commits->ncommits < view->nlines - 1 &&
3747 !s->thread_args.log_complete) {
3748 s->thread_args.commits_needed += (view->nlines - 1) -
3749 s->commits->ncommits;
3750 err = trigger_log_thread(view, 1);
3752 break;
3753 case KEY_ENTER:
3754 case '\r':
3755 view->count = 0;
3756 if (s->selected_entry == NULL)
3757 break;
3758 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3759 break;
3760 case 'T':
3761 view->count = 0;
3762 if (s->selected_entry == NULL)
3763 break;
3764 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3765 break;
3766 case KEY_BACKSPACE:
3767 case CTRL('l'):
3768 case 'B':
3769 view->count = 0;
3770 if (ch == KEY_BACKSPACE &&
3771 got_path_is_root_dir(s->in_repo_path))
3772 break;
3773 err = stop_log_thread(s);
3774 if (err)
3775 return err;
3776 if (ch == KEY_BACKSPACE) {
3777 char *parent_path;
3778 err = got_path_dirname(&parent_path, s->in_repo_path);
3779 if (err)
3780 return err;
3781 free(s->in_repo_path);
3782 s->in_repo_path = parent_path;
3783 s->thread_args.in_repo_path = s->in_repo_path;
3784 } else if (ch == CTRL('l')) {
3785 struct got_object_id *start_id;
3786 err = got_repo_match_object_id(&start_id, NULL,
3787 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3788 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3789 if (err) {
3790 if (s->head_ref_name == NULL ||
3791 err->code != GOT_ERR_NOT_REF)
3792 return err;
3793 /* Try to cope with deleted references. */
3794 free(s->head_ref_name);
3795 s->head_ref_name = NULL;
3796 err = got_repo_match_object_id(&start_id,
3797 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3798 &tog_refs, s->repo);
3799 if (err)
3800 return err;
3802 free(s->start_id);
3803 s->start_id = start_id;
3804 s->thread_args.start_id = s->start_id;
3805 } else /* 'B' */
3806 s->log_branches = !s->log_branches;
3808 if (s->thread_args.pack_fds == NULL) {
3809 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3810 if (err)
3811 return err;
3813 err = got_repo_open(&s->thread_args.repo,
3814 got_repo_get_path(s->repo), NULL,
3815 s->thread_args.pack_fds);
3816 if (err)
3817 return err;
3818 tog_free_refs();
3819 err = tog_load_refs(s->repo, 0);
3820 if (err)
3821 return err;
3822 err = got_commit_graph_open(&s->thread_args.graph,
3823 s->in_repo_path, !s->log_branches);
3824 if (err)
3825 return err;
3826 err = got_commit_graph_iter_start(s->thread_args.graph,
3827 s->start_id, s->repo, NULL, NULL);
3828 if (err)
3829 return err;
3830 free_commits(&s->real_commits);
3831 free_commits(&s->limit_commits);
3832 s->first_displayed_entry = NULL;
3833 s->last_displayed_entry = NULL;
3834 s->selected_entry = NULL;
3835 s->selected = 0;
3836 s->thread_args.log_complete = 0;
3837 s->quit = 0;
3838 s->thread_args.commits_needed = view->lines;
3839 s->matched_entry = NULL;
3840 s->search_entry = NULL;
3841 view->offset = 0;
3842 break;
3843 case 'R':
3844 view->count = 0;
3845 err = view_request_new(new_view, view, TOG_VIEW_REF);
3846 break;
3847 default:
3848 view->count = 0;
3849 break;
3852 return err;
3855 static const struct got_error *
3856 apply_unveil(const char *repo_path, const char *worktree_path)
3858 const struct got_error *error;
3860 #ifdef PROFILE
3861 if (unveil("gmon.out", "rwc") != 0)
3862 return got_error_from_errno2("unveil", "gmon.out");
3863 #endif
3864 if (repo_path && unveil(repo_path, "r") != 0)
3865 return got_error_from_errno2("unveil", repo_path);
3867 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3868 return got_error_from_errno2("unveil", worktree_path);
3870 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3871 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3873 error = got_privsep_unveil_exec_helpers();
3874 if (error != NULL)
3875 return error;
3877 if (unveil(NULL, NULL) != 0)
3878 return got_error_from_errno("unveil");
3880 return NULL;
3883 static void
3884 init_curses(void)
3887 * Override default signal handlers before starting ncurses.
3888 * This should prevent ncurses from installing its own
3889 * broken cleanup() signal handler.
3891 signal(SIGWINCH, tog_sigwinch);
3892 signal(SIGPIPE, tog_sigpipe);
3893 signal(SIGCONT, tog_sigcont);
3894 signal(SIGINT, tog_sigint);
3895 signal(SIGTERM, tog_sigterm);
3897 initscr();
3898 cbreak();
3899 halfdelay(1); /* Do fast refresh while initial view is loading. */
3900 noecho();
3901 nonl();
3902 intrflush(stdscr, FALSE);
3903 keypad(stdscr, TRUE);
3904 curs_set(0);
3905 if (getenv("TOG_COLORS") != NULL) {
3906 start_color();
3907 use_default_colors();
3911 static const struct got_error *
3912 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3913 struct got_repository *repo, struct got_worktree *worktree)
3915 const struct got_error *err = NULL;
3917 if (argc == 0) {
3918 *in_repo_path = strdup("/");
3919 if (*in_repo_path == NULL)
3920 return got_error_from_errno("strdup");
3921 return NULL;
3924 if (worktree) {
3925 const char *prefix = got_worktree_get_path_prefix(worktree);
3926 char *p;
3928 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3929 if (err)
3930 return err;
3931 if (asprintf(in_repo_path, "%s%s%s", prefix,
3932 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3933 p) == -1) {
3934 err = got_error_from_errno("asprintf");
3935 *in_repo_path = NULL;
3937 free(p);
3938 } else
3939 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3941 return err;
3944 static const struct got_error *
3945 cmd_log(int argc, char *argv[])
3947 const struct got_error *error;
3948 struct got_repository *repo = NULL;
3949 struct got_worktree *worktree = NULL;
3950 struct got_object_id *start_id = NULL;
3951 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3952 char *start_commit = NULL, *label = NULL;
3953 struct got_reference *ref = NULL;
3954 const char *head_ref_name = NULL;
3955 int ch, log_branches = 0;
3956 struct tog_view *view;
3957 int *pack_fds = NULL;
3959 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3960 switch (ch) {
3961 case 'b':
3962 log_branches = 1;
3963 break;
3964 case 'c':
3965 start_commit = optarg;
3966 break;
3967 case 'r':
3968 repo_path = realpath(optarg, NULL);
3969 if (repo_path == NULL)
3970 return got_error_from_errno2("realpath",
3971 optarg);
3972 break;
3973 default:
3974 usage_log();
3975 /* NOTREACHED */
3979 argc -= optind;
3980 argv += optind;
3982 if (argc > 1)
3983 usage_log();
3985 error = got_repo_pack_fds_open(&pack_fds);
3986 if (error != NULL)
3987 goto done;
3989 if (repo_path == NULL) {
3990 cwd = getcwd(NULL, 0);
3991 if (cwd == NULL)
3992 return got_error_from_errno("getcwd");
3993 error = got_worktree_open(&worktree, cwd);
3994 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3995 goto done;
3996 if (worktree)
3997 repo_path =
3998 strdup(got_worktree_get_repo_path(worktree));
3999 else
4000 repo_path = strdup(cwd);
4001 if (repo_path == NULL) {
4002 error = got_error_from_errno("strdup");
4003 goto done;
4007 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4008 if (error != NULL)
4009 goto done;
4011 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4012 repo, worktree);
4013 if (error)
4014 goto done;
4016 init_curses();
4018 error = apply_unveil(got_repo_get_path(repo),
4019 worktree ? got_worktree_get_root_path(worktree) : NULL);
4020 if (error)
4021 goto done;
4023 /* already loaded by tog_log_with_path()? */
4024 if (TAILQ_EMPTY(&tog_refs)) {
4025 error = tog_load_refs(repo, 0);
4026 if (error)
4027 goto done;
4030 if (start_commit == NULL) {
4031 error = got_repo_match_object_id(&start_id, &label,
4032 worktree ? got_worktree_get_head_ref_name(worktree) :
4033 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4034 if (error)
4035 goto done;
4036 head_ref_name = label;
4037 } else {
4038 error = got_ref_open(&ref, repo, start_commit, 0);
4039 if (error == NULL)
4040 head_ref_name = got_ref_get_name(ref);
4041 else if (error->code != GOT_ERR_NOT_REF)
4042 goto done;
4043 error = got_repo_match_object_id(&start_id, NULL,
4044 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4045 if (error)
4046 goto done;
4049 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4050 if (view == NULL) {
4051 error = got_error_from_errno("view_open");
4052 goto done;
4054 error = open_log_view(view, start_id, repo, head_ref_name,
4055 in_repo_path, log_branches);
4056 if (error)
4057 goto done;
4058 if (worktree) {
4059 /* Release work tree lock. */
4060 got_worktree_close(worktree);
4061 worktree = NULL;
4063 error = view_loop(view);
4064 done:
4065 free(in_repo_path);
4066 free(repo_path);
4067 free(cwd);
4068 free(start_id);
4069 free(label);
4070 if (ref)
4071 got_ref_close(ref);
4072 if (repo) {
4073 const struct got_error *close_err = got_repo_close(repo);
4074 if (error == NULL)
4075 error = close_err;
4077 if (worktree)
4078 got_worktree_close(worktree);
4079 if (pack_fds) {
4080 const struct got_error *pack_err =
4081 got_repo_pack_fds_close(pack_fds);
4082 if (error == NULL)
4083 error = pack_err;
4085 tog_free_refs();
4086 return error;
4089 __dead static void
4090 usage_diff(void)
4092 endwin();
4093 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4094 "object1 object2\n", getprogname());
4095 exit(1);
4098 static int
4099 match_line(const char *line, regex_t *regex, size_t nmatch,
4100 regmatch_t *regmatch)
4102 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4105 static struct tog_color *
4106 match_color(struct tog_colors *colors, const char *line)
4108 struct tog_color *tc = NULL;
4110 STAILQ_FOREACH(tc, colors, entry) {
4111 if (match_line(line, &tc->regex, 0, NULL))
4112 return tc;
4115 return NULL;
4118 static const struct got_error *
4119 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4120 WINDOW *window, int skipcol, regmatch_t *regmatch)
4122 const struct got_error *err = NULL;
4123 char *exstr = NULL;
4124 wchar_t *wline = NULL;
4125 int rme, rms, n, width, scrollx;
4126 int width0 = 0, width1 = 0, width2 = 0;
4127 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4129 *wtotal = 0;
4131 rms = regmatch->rm_so;
4132 rme = regmatch->rm_eo;
4134 err = expand_tab(&exstr, line);
4135 if (err)
4136 return err;
4138 /* Split the line into 3 segments, according to match offsets. */
4139 seg0 = strndup(exstr, rms);
4140 if (seg0 == NULL) {
4141 err = got_error_from_errno("strndup");
4142 goto done;
4144 seg1 = strndup(exstr + rms, rme - rms);
4145 if (seg1 == NULL) {
4146 err = got_error_from_errno("strndup");
4147 goto done;
4149 seg2 = strdup(exstr + rme);
4150 if (seg2 == NULL) {
4151 err = got_error_from_errno("strndup");
4152 goto done;
4155 /* draw up to matched token if we haven't scrolled past it */
4156 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4157 col_tab_align, 1);
4158 if (err)
4159 goto done;
4160 n = MAX(width0 - skipcol, 0);
4161 if (n) {
4162 free(wline);
4163 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4164 wlimit, col_tab_align, 1);
4165 if (err)
4166 goto done;
4167 waddwstr(window, &wline[scrollx]);
4168 wlimit -= width;
4169 *wtotal += width;
4172 if (wlimit > 0) {
4173 int i = 0, w = 0;
4174 size_t wlen;
4176 free(wline);
4177 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4178 col_tab_align, 1);
4179 if (err)
4180 goto done;
4181 wlen = wcslen(wline);
4182 while (i < wlen) {
4183 width = wcwidth(wline[i]);
4184 if (width == -1) {
4185 /* should not happen, tabs are expanded */
4186 err = got_error(GOT_ERR_RANGE);
4187 goto done;
4189 if (width0 + w + width > skipcol)
4190 break;
4191 w += width;
4192 i++;
4194 /* draw (visible part of) matched token (if scrolled into it) */
4195 if (width1 - w > 0) {
4196 wattron(window, A_STANDOUT);
4197 waddwstr(window, &wline[i]);
4198 wattroff(window, A_STANDOUT);
4199 wlimit -= (width1 - w);
4200 *wtotal += (width1 - w);
4204 if (wlimit > 0) { /* draw rest of line */
4205 free(wline);
4206 if (skipcol > width0 + width1) {
4207 err = format_line(&wline, &width2, &scrollx, seg2,
4208 skipcol - (width0 + width1), wlimit,
4209 col_tab_align, 1);
4210 if (err)
4211 goto done;
4212 waddwstr(window, &wline[scrollx]);
4213 } else {
4214 err = format_line(&wline, &width2, NULL, seg2, 0,
4215 wlimit, col_tab_align, 1);
4216 if (err)
4217 goto done;
4218 waddwstr(window, wline);
4220 *wtotal += width2;
4222 done:
4223 free(wline);
4224 free(exstr);
4225 free(seg0);
4226 free(seg1);
4227 free(seg2);
4228 return err;
4231 static int
4232 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4234 FILE *f = NULL;
4235 int *eof, *first, *selected;
4237 if (view->type == TOG_VIEW_DIFF) {
4238 struct tog_diff_view_state *s = &view->state.diff;
4240 first = &s->first_displayed_line;
4241 selected = first;
4242 eof = &s->eof;
4243 f = s->f;
4244 } else if (view->type == TOG_VIEW_HELP) {
4245 struct tog_help_view_state *s = &view->state.help;
4247 first = &s->first_displayed_line;
4248 selected = first;
4249 eof = &s->eof;
4250 f = s->f;
4251 } else if (view->type == TOG_VIEW_BLAME) {
4252 struct tog_blame_view_state *s = &view->state.blame;
4254 first = &s->first_displayed_line;
4255 selected = &s->selected_line;
4256 eof = &s->eof;
4257 f = s->blame.f;
4258 } else
4259 return 0;
4261 /* Center gline in the middle of the page like vi(1). */
4262 if (*lineno < view->gline - (view->nlines - 3) / 2)
4263 return 0;
4264 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4265 rewind(f);
4266 *eof = 0;
4267 *first = 1;
4268 *lineno = 0;
4269 *nprinted = 0;
4270 return 0;
4273 *selected = view->gline <= (view->nlines - 3) / 2 ?
4274 view->gline : (view->nlines - 3) / 2 + 1;
4275 view->gline = 0;
4277 return 1;
4280 static const struct got_error *
4281 draw_file(struct tog_view *view, const char *header)
4283 struct tog_diff_view_state *s = &view->state.diff;
4284 regmatch_t *regmatch = &view->regmatch;
4285 const struct got_error *err;
4286 int nprinted = 0;
4287 char *line;
4288 size_t linesize = 0;
4289 ssize_t linelen;
4290 wchar_t *wline;
4291 int width;
4292 int max_lines = view->nlines;
4293 int nlines = s->nlines;
4294 off_t line_offset;
4296 s->lineno = s->first_displayed_line - 1;
4297 line_offset = s->lines[s->first_displayed_line - 1].offset;
4298 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4299 return got_error_from_errno("fseek");
4301 werase(view->window);
4303 if (view->gline > s->nlines - 1)
4304 view->gline = s->nlines - 1;
4306 if (header) {
4307 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4308 1 : view->gline - (view->nlines - 3) / 2 :
4309 s->lineno + s->selected_line;
4311 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4312 return got_error_from_errno("asprintf");
4313 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4314 0, 0);
4315 free(line);
4316 if (err)
4317 return err;
4319 if (view_needs_focus_indication(view))
4320 wstandout(view->window);
4321 waddwstr(view->window, wline);
4322 free(wline);
4323 wline = NULL;
4324 while (width++ < view->ncols)
4325 waddch(view->window, ' ');
4326 if (view_needs_focus_indication(view))
4327 wstandend(view->window);
4329 if (max_lines <= 1)
4330 return NULL;
4331 max_lines--;
4334 s->eof = 0;
4335 view->maxx = 0;
4336 line = NULL;
4337 while (max_lines > 0 && nprinted < max_lines) {
4338 enum got_diff_line_type linetype;
4339 attr_t attr = 0;
4341 linelen = getline(&line, &linesize, s->f);
4342 if (linelen == -1) {
4343 if (feof(s->f)) {
4344 s->eof = 1;
4345 break;
4347 free(line);
4348 return got_ferror(s->f, GOT_ERR_IO);
4351 if (++s->lineno < s->first_displayed_line)
4352 continue;
4353 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4354 continue;
4355 if (s->lineno == view->hiline)
4356 attr = A_STANDOUT;
4358 /* Set view->maxx based on full line length. */
4359 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4360 view->x ? 1 : 0);
4361 if (err) {
4362 free(line);
4363 return err;
4365 view->maxx = MAX(view->maxx, width);
4366 free(wline);
4367 wline = NULL;
4369 linetype = s->lines[s->lineno].type;
4370 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4371 linetype < GOT_DIFF_LINE_CONTEXT)
4372 attr |= COLOR_PAIR(linetype);
4373 if (attr)
4374 wattron(view->window, attr);
4375 if (s->first_displayed_line + nprinted == s->matched_line &&
4376 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4377 err = add_matched_line(&width, line, view->ncols, 0,
4378 view->window, view->x, regmatch);
4379 if (err) {
4380 free(line);
4381 return err;
4383 } else {
4384 int skip;
4385 err = format_line(&wline, &width, &skip, line,
4386 view->x, view->ncols, 0, view->x ? 1 : 0);
4387 if (err) {
4388 free(line);
4389 return err;
4391 waddwstr(view->window, &wline[skip]);
4392 free(wline);
4393 wline = NULL;
4395 if (s->lineno == view->hiline) {
4396 /* highlight full gline length */
4397 while (width++ < view->ncols)
4398 waddch(view->window, ' ');
4399 } else {
4400 if (width <= view->ncols - 1)
4401 waddch(view->window, '\n');
4403 if (attr)
4404 wattroff(view->window, attr);
4405 if (++nprinted == 1)
4406 s->first_displayed_line = s->lineno;
4408 free(line);
4409 if (nprinted >= 1)
4410 s->last_displayed_line = s->first_displayed_line +
4411 (nprinted - 1);
4412 else
4413 s->last_displayed_line = s->first_displayed_line;
4415 view_border(view);
4417 if (s->eof) {
4418 while (nprinted < view->nlines) {
4419 waddch(view->window, '\n');
4420 nprinted++;
4423 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4424 view->ncols, 0, 0);
4425 if (err) {
4426 return err;
4429 wstandout(view->window);
4430 waddwstr(view->window, wline);
4431 free(wline);
4432 wline = NULL;
4433 wstandend(view->window);
4436 return NULL;
4439 static char *
4440 get_datestr(time_t *time, char *datebuf)
4442 struct tm mytm, *tm;
4443 char *p, *s;
4445 tm = gmtime_r(time, &mytm);
4446 if (tm == NULL)
4447 return NULL;
4448 s = asctime_r(tm, datebuf);
4449 if (s == NULL)
4450 return NULL;
4451 p = strchr(s, '\n');
4452 if (p)
4453 *p = '\0';
4454 return s;
4457 static const struct got_error *
4458 get_changed_paths(struct got_pathlist_head *paths,
4459 struct got_commit_object *commit, struct got_repository *repo)
4461 const struct got_error *err = NULL;
4462 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4463 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4464 struct got_object_qid *qid;
4466 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4467 if (qid != NULL) {
4468 struct got_commit_object *pcommit;
4469 err = got_object_open_as_commit(&pcommit, repo,
4470 &qid->id);
4471 if (err)
4472 return err;
4474 tree_id1 = got_object_id_dup(
4475 got_object_commit_get_tree_id(pcommit));
4476 if (tree_id1 == NULL) {
4477 got_object_commit_close(pcommit);
4478 return got_error_from_errno("got_object_id_dup");
4480 got_object_commit_close(pcommit);
4484 if (tree_id1) {
4485 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4486 if (err)
4487 goto done;
4490 tree_id2 = got_object_commit_get_tree_id(commit);
4491 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4492 if (err)
4493 goto done;
4495 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4496 got_diff_tree_collect_changed_paths, paths, 0);
4497 done:
4498 if (tree1)
4499 got_object_tree_close(tree1);
4500 if (tree2)
4501 got_object_tree_close(tree2);
4502 free(tree_id1);
4503 return err;
4506 static const struct got_error *
4507 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4508 off_t off, uint8_t type)
4510 struct got_diff_line *p;
4512 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4513 if (p == NULL)
4514 return got_error_from_errno("reallocarray");
4515 *lines = p;
4516 (*lines)[*nlines].offset = off;
4517 (*lines)[*nlines].type = type;
4518 (*nlines)++;
4520 return NULL;
4523 static const struct got_error *
4524 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4525 struct got_object_id *commit_id, struct got_reflist_head *refs,
4526 struct got_repository *repo, FILE *outfile)
4528 const struct got_error *err = NULL;
4529 char datebuf[26], *datestr;
4530 struct got_commit_object *commit;
4531 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4532 time_t committer_time;
4533 const char *author, *committer;
4534 char *refs_str = NULL;
4535 struct got_pathlist_head changed_paths;
4536 struct got_pathlist_entry *pe;
4537 off_t outoff = 0;
4538 int n;
4540 TAILQ_INIT(&changed_paths);
4542 if (refs) {
4543 err = build_refs_str(&refs_str, refs, commit_id, repo);
4544 if (err)
4545 return err;
4548 err = got_object_open_as_commit(&commit, repo, commit_id);
4549 if (err)
4550 return err;
4552 err = got_object_id_str(&id_str, commit_id);
4553 if (err) {
4554 err = got_error_from_errno("got_object_id_str");
4555 goto done;
4558 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4559 if (err)
4560 goto done;
4562 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4563 refs_str ? refs_str : "", refs_str ? ")" : "");
4564 if (n < 0) {
4565 err = got_error_from_errno("fprintf");
4566 goto done;
4568 outoff += n;
4569 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4570 if (err)
4571 goto done;
4573 n = fprintf(outfile, "from: %s\n",
4574 got_object_commit_get_author(commit));
4575 if (n < 0) {
4576 err = got_error_from_errno("fprintf");
4577 goto done;
4579 outoff += n;
4580 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4581 if (err)
4582 goto done;
4584 committer_time = got_object_commit_get_committer_time(commit);
4585 datestr = get_datestr(&committer_time, datebuf);
4586 if (datestr) {
4587 n = fprintf(outfile, "date: %s UTC\n", datestr);
4588 if (n < 0) {
4589 err = got_error_from_errno("fprintf");
4590 goto done;
4592 outoff += n;
4593 err = add_line_metadata(lines, nlines, outoff,
4594 GOT_DIFF_LINE_DATE);
4595 if (err)
4596 goto done;
4598 author = got_object_commit_get_author(commit);
4599 committer = got_object_commit_get_committer(commit);
4600 if (strcmp(author, committer) != 0) {
4601 n = fprintf(outfile, "via: %s\n", committer);
4602 if (n < 0) {
4603 err = got_error_from_errno("fprintf");
4604 goto done;
4606 outoff += n;
4607 err = add_line_metadata(lines, nlines, outoff,
4608 GOT_DIFF_LINE_AUTHOR);
4609 if (err)
4610 goto done;
4612 if (got_object_commit_get_nparents(commit) > 1) {
4613 const struct got_object_id_queue *parent_ids;
4614 struct got_object_qid *qid;
4615 int pn = 1;
4616 parent_ids = got_object_commit_get_parent_ids(commit);
4617 STAILQ_FOREACH(qid, parent_ids, entry) {
4618 err = got_object_id_str(&id_str, &qid->id);
4619 if (err)
4620 goto done;
4621 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4622 if (n < 0) {
4623 err = got_error_from_errno("fprintf");
4624 goto done;
4626 outoff += n;
4627 err = add_line_metadata(lines, nlines, outoff,
4628 GOT_DIFF_LINE_META);
4629 if (err)
4630 goto done;
4631 free(id_str);
4632 id_str = NULL;
4636 err = got_object_commit_get_logmsg(&logmsg, commit);
4637 if (err)
4638 goto done;
4639 s = logmsg;
4640 while ((line = strsep(&s, "\n")) != NULL) {
4641 n = fprintf(outfile, "%s\n", line);
4642 if (n < 0) {
4643 err = got_error_from_errno("fprintf");
4644 goto done;
4646 outoff += n;
4647 err = add_line_metadata(lines, nlines, outoff,
4648 GOT_DIFF_LINE_LOGMSG);
4649 if (err)
4650 goto done;
4653 err = get_changed_paths(&changed_paths, commit, repo);
4654 if (err)
4655 goto done;
4656 TAILQ_FOREACH(pe, &changed_paths, entry) {
4657 struct got_diff_changed_path *cp = pe->data;
4658 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4659 if (n < 0) {
4660 err = got_error_from_errno("fprintf");
4661 goto done;
4663 outoff += n;
4664 err = add_line_metadata(lines, nlines, outoff,
4665 GOT_DIFF_LINE_CHANGES);
4666 if (err)
4667 goto done;
4668 free((char *)pe->path);
4669 free(pe->data);
4672 fputc('\n', outfile);
4673 outoff++;
4674 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4675 done:
4676 got_pathlist_free(&changed_paths);
4677 free(id_str);
4678 free(logmsg);
4679 free(refs_str);
4680 got_object_commit_close(commit);
4681 if (err) {
4682 free(*lines);
4683 *lines = NULL;
4684 *nlines = 0;
4686 return err;
4689 static const struct got_error *
4690 create_diff(struct tog_diff_view_state *s)
4692 const struct got_error *err = NULL;
4693 FILE *f = NULL;
4694 int obj_type;
4696 free(s->lines);
4697 s->lines = malloc(sizeof(*s->lines));
4698 if (s->lines == NULL)
4699 return got_error_from_errno("malloc");
4700 s->nlines = 0;
4702 f = got_opentemp();
4703 if (f == NULL) {
4704 err = got_error_from_errno("got_opentemp");
4705 goto done;
4707 if (s->f && fclose(s->f) == EOF) {
4708 err = got_error_from_errno("fclose");
4709 goto done;
4711 s->f = f;
4713 if (s->id1)
4714 err = got_object_get_type(&obj_type, s->repo, s->id1);
4715 else
4716 err = got_object_get_type(&obj_type, s->repo, s->id2);
4717 if (err)
4718 goto done;
4720 switch (obj_type) {
4721 case GOT_OBJ_TYPE_BLOB:
4722 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4723 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4724 s->label1, s->label2, tog_diff_algo, s->diff_context,
4725 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4726 break;
4727 case GOT_OBJ_TYPE_TREE:
4728 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4729 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4730 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4731 s->force_text_diff, s->repo, s->f);
4732 break;
4733 case GOT_OBJ_TYPE_COMMIT: {
4734 const struct got_object_id_queue *parent_ids;
4735 struct got_object_qid *pid;
4736 struct got_commit_object *commit2;
4737 struct got_reflist_head *refs;
4739 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4740 if (err)
4741 goto done;
4742 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4743 /* Show commit info if we're diffing to a parent/root commit. */
4744 if (s->id1 == NULL) {
4745 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4746 refs, s->repo, s->f);
4747 if (err)
4748 goto done;
4749 } else {
4750 parent_ids = got_object_commit_get_parent_ids(commit2);
4751 STAILQ_FOREACH(pid, parent_ids, entry) {
4752 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4753 err = write_commit_info(&s->lines,
4754 &s->nlines, s->id2, refs, s->repo,
4755 s->f);
4756 if (err)
4757 goto done;
4758 break;
4762 got_object_commit_close(commit2);
4764 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4765 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4766 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4767 s->force_text_diff, s->repo, s->f);
4768 break;
4770 default:
4771 err = got_error(GOT_ERR_OBJ_TYPE);
4772 break;
4774 done:
4775 if (s->f && fflush(s->f) != 0 && err == NULL)
4776 err = got_error_from_errno("fflush");
4777 return err;
4780 static void
4781 diff_view_indicate_progress(struct tog_view *view)
4783 mvwaddstr(view->window, 0, 0, "diffing...");
4784 update_panels();
4785 doupdate();
4788 static const struct got_error *
4789 search_start_diff_view(struct tog_view *view)
4791 struct tog_diff_view_state *s = &view->state.diff;
4793 s->matched_line = 0;
4794 return NULL;
4797 static void
4798 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4799 size_t *nlines, int **first, int **last, int **match, int **selected)
4801 struct tog_diff_view_state *s = &view->state.diff;
4803 *f = s->f;
4804 *nlines = s->nlines;
4805 *line_offsets = NULL;
4806 *match = &s->matched_line;
4807 *first = &s->first_displayed_line;
4808 *last = &s->last_displayed_line;
4809 *selected = &s->selected_line;
4812 static const struct got_error *
4813 search_next_view_match(struct tog_view *view)
4815 const struct got_error *err = NULL;
4816 FILE *f;
4817 int lineno;
4818 char *line = NULL;
4819 size_t linesize = 0;
4820 ssize_t linelen;
4821 off_t *line_offsets;
4822 size_t nlines = 0;
4823 int *first, *last, *match, *selected;
4825 if (!view->search_setup)
4826 return got_error_msg(GOT_ERR_NOT_IMPL,
4827 "view search not supported");
4828 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4829 &match, &selected);
4831 if (!view->searching) {
4832 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4833 return NULL;
4836 if (*match) {
4837 if (view->searching == TOG_SEARCH_FORWARD)
4838 lineno = *match + 1;
4839 else
4840 lineno = *match - 1;
4841 } else
4842 lineno = *first - 1 + *selected;
4844 while (1) {
4845 off_t offset;
4847 if (lineno <= 0 || lineno > nlines) {
4848 if (*match == 0) {
4849 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4850 break;
4853 if (view->searching == TOG_SEARCH_FORWARD)
4854 lineno = 1;
4855 else
4856 lineno = nlines;
4859 offset = view->type == TOG_VIEW_DIFF ?
4860 view->state.diff.lines[lineno - 1].offset :
4861 line_offsets[lineno - 1];
4862 if (fseeko(f, offset, SEEK_SET) != 0) {
4863 free(line);
4864 return got_error_from_errno("fseeko");
4866 linelen = getline(&line, &linesize, f);
4867 if (linelen != -1) {
4868 char *exstr;
4869 err = expand_tab(&exstr, line);
4870 if (err)
4871 break;
4872 if (match_line(exstr, &view->regex, 1,
4873 &view->regmatch)) {
4874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4875 *match = lineno;
4876 free(exstr);
4877 break;
4879 free(exstr);
4881 if (view->searching == TOG_SEARCH_FORWARD)
4882 lineno++;
4883 else
4884 lineno--;
4886 free(line);
4888 if (*match) {
4889 *first = *match;
4890 *selected = 1;
4893 return err;
4896 static const struct got_error *
4897 close_diff_view(struct tog_view *view)
4899 const struct got_error *err = NULL;
4900 struct tog_diff_view_state *s = &view->state.diff;
4902 free(s->id1);
4903 s->id1 = NULL;
4904 free(s->id2);
4905 s->id2 = NULL;
4906 if (s->f && fclose(s->f) == EOF)
4907 err = got_error_from_errno("fclose");
4908 s->f = NULL;
4909 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4910 err = got_error_from_errno("fclose");
4911 s->f1 = NULL;
4912 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4913 err = got_error_from_errno("fclose");
4914 s->f2 = NULL;
4915 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4916 err = got_error_from_errno("close");
4917 s->fd1 = -1;
4918 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4919 err = got_error_from_errno("close");
4920 s->fd2 = -1;
4921 free(s->lines);
4922 s->lines = NULL;
4923 s->nlines = 0;
4924 return err;
4927 static const struct got_error *
4928 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4929 struct got_object_id *id2, const char *label1, const char *label2,
4930 int diff_context, int ignore_whitespace, int force_text_diff,
4931 struct tog_view *parent_view, struct got_repository *repo)
4933 const struct got_error *err;
4934 struct tog_diff_view_state *s = &view->state.diff;
4936 memset(s, 0, sizeof(*s));
4937 s->fd1 = -1;
4938 s->fd2 = -1;
4940 if (id1 != NULL && id2 != NULL) {
4941 int type1, type2;
4942 err = got_object_get_type(&type1, repo, id1);
4943 if (err)
4944 return err;
4945 err = got_object_get_type(&type2, repo, id2);
4946 if (err)
4947 return err;
4949 if (type1 != type2)
4950 return got_error(GOT_ERR_OBJ_TYPE);
4952 s->first_displayed_line = 1;
4953 s->last_displayed_line = view->nlines;
4954 s->selected_line = 1;
4955 s->repo = repo;
4956 s->id1 = id1;
4957 s->id2 = id2;
4958 s->label1 = label1;
4959 s->label2 = label2;
4961 if (id1) {
4962 s->id1 = got_object_id_dup(id1);
4963 if (s->id1 == NULL)
4964 return got_error_from_errno("got_object_id_dup");
4965 } else
4966 s->id1 = NULL;
4968 s->id2 = got_object_id_dup(id2);
4969 if (s->id2 == NULL) {
4970 err = got_error_from_errno("got_object_id_dup");
4971 goto done;
4974 s->f1 = got_opentemp();
4975 if (s->f1 == NULL) {
4976 err = got_error_from_errno("got_opentemp");
4977 goto done;
4980 s->f2 = got_opentemp();
4981 if (s->f2 == NULL) {
4982 err = got_error_from_errno("got_opentemp");
4983 goto done;
4986 s->fd1 = got_opentempfd();
4987 if (s->fd1 == -1) {
4988 err = got_error_from_errno("got_opentempfd");
4989 goto done;
4992 s->fd2 = got_opentempfd();
4993 if (s->fd2 == -1) {
4994 err = got_error_from_errno("got_opentempfd");
4995 goto done;
4998 s->first_displayed_line = 1;
4999 s->last_displayed_line = view->nlines;
5000 s->diff_context = diff_context;
5001 s->ignore_whitespace = ignore_whitespace;
5002 s->force_text_diff = force_text_diff;
5003 s->parent_view = parent_view;
5004 s->repo = repo;
5006 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5007 int rc;
5009 rc = init_pair(GOT_DIFF_LINE_MINUS,
5010 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5011 if (rc != ERR)
5012 rc = init_pair(GOT_DIFF_LINE_PLUS,
5013 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5014 if (rc != ERR)
5015 rc = init_pair(GOT_DIFF_LINE_HUNK,
5016 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5017 if (rc != ERR)
5018 rc = init_pair(GOT_DIFF_LINE_META,
5019 get_color_value("TOG_COLOR_DIFF_META"), -1);
5020 if (rc != ERR)
5021 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5022 get_color_value("TOG_COLOR_DIFF_META"), -1);
5023 if (rc != ERR)
5024 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5025 get_color_value("TOG_COLOR_DIFF_META"), -1);
5026 if (rc != ERR)
5027 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5028 get_color_value("TOG_COLOR_DIFF_META"), -1);
5029 if (rc != ERR)
5030 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5031 get_color_value("TOG_COLOR_AUTHOR"), -1);
5032 if (rc != ERR)
5033 rc = init_pair(GOT_DIFF_LINE_DATE,
5034 get_color_value("TOG_COLOR_DATE"), -1);
5035 if (rc == ERR) {
5036 err = got_error(GOT_ERR_RANGE);
5037 goto done;
5041 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5042 view_is_splitscreen(view))
5043 show_log_view(parent_view); /* draw border */
5044 diff_view_indicate_progress(view);
5046 err = create_diff(s);
5048 view->show = show_diff_view;
5049 view->input = input_diff_view;
5050 view->reset = reset_diff_view;
5051 view->close = close_diff_view;
5052 view->search_start = search_start_diff_view;
5053 view->search_setup = search_setup_diff_view;
5054 view->search_next = search_next_view_match;
5055 done:
5056 if (err)
5057 close_diff_view(view);
5058 return err;
5061 static const struct got_error *
5062 show_diff_view(struct tog_view *view)
5064 const struct got_error *err;
5065 struct tog_diff_view_state *s = &view->state.diff;
5066 char *id_str1 = NULL, *id_str2, *header;
5067 const char *label1, *label2;
5069 if (s->id1) {
5070 err = got_object_id_str(&id_str1, s->id1);
5071 if (err)
5072 return err;
5073 label1 = s->label1 ? s->label1 : id_str1;
5074 } else
5075 label1 = "/dev/null";
5077 err = got_object_id_str(&id_str2, s->id2);
5078 if (err)
5079 return err;
5080 label2 = s->label2 ? s->label2 : id_str2;
5082 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5083 err = got_error_from_errno("asprintf");
5084 free(id_str1);
5085 free(id_str2);
5086 return err;
5088 free(id_str1);
5089 free(id_str2);
5091 err = draw_file(view, header);
5092 free(header);
5093 return err;
5096 static const struct got_error *
5097 set_selected_commit(struct tog_diff_view_state *s,
5098 struct commit_queue_entry *entry)
5100 const struct got_error *err;
5101 const struct got_object_id_queue *parent_ids;
5102 struct got_commit_object *selected_commit;
5103 struct got_object_qid *pid;
5105 free(s->id2);
5106 s->id2 = got_object_id_dup(entry->id);
5107 if (s->id2 == NULL)
5108 return got_error_from_errno("got_object_id_dup");
5110 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5111 if (err)
5112 return err;
5113 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5114 free(s->id1);
5115 pid = STAILQ_FIRST(parent_ids);
5116 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5117 got_object_commit_close(selected_commit);
5118 return NULL;
5121 static const struct got_error *
5122 reset_diff_view(struct tog_view *view)
5124 struct tog_diff_view_state *s = &view->state.diff;
5126 view->count = 0;
5127 wclear(view->window);
5128 s->first_displayed_line = 1;
5129 s->last_displayed_line = view->nlines;
5130 s->matched_line = 0;
5131 diff_view_indicate_progress(view);
5132 return create_diff(s);
5135 static void
5136 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5138 int start, i;
5140 i = start = s->first_displayed_line - 1;
5142 while (s->lines[i].type != type) {
5143 if (i == 0)
5144 i = s->nlines - 1;
5145 if (--i == start)
5146 return; /* do nothing, requested type not in file */
5149 s->selected_line = 1;
5150 s->first_displayed_line = i;
5153 static void
5154 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5156 int start, i;
5158 i = start = s->first_displayed_line + 1;
5160 while (s->lines[i].type != type) {
5161 if (i == s->nlines - 1)
5162 i = 0;
5163 if (++i == start)
5164 return; /* do nothing, requested type not in file */
5167 s->selected_line = 1;
5168 s->first_displayed_line = i;
5171 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5172 int, int, int);
5173 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5174 int, int);
5176 static const struct got_error *
5177 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5179 const struct got_error *err = NULL;
5180 struct tog_diff_view_state *s = &view->state.diff;
5181 struct tog_log_view_state *ls;
5182 struct commit_queue_entry *old_selected_entry;
5183 char *line = NULL;
5184 size_t linesize = 0;
5185 ssize_t linelen;
5186 int i, nscroll = view->nlines - 1, up = 0;
5188 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5190 switch (ch) {
5191 case '0':
5192 view->x = 0;
5193 break;
5194 case '$':
5195 view->x = MAX(view->maxx - view->ncols / 3, 0);
5196 view->count = 0;
5197 break;
5198 case KEY_RIGHT:
5199 case 'l':
5200 if (view->x + view->ncols / 3 < view->maxx)
5201 view->x += 2; /* move two columns right */
5202 else
5203 view->count = 0;
5204 break;
5205 case KEY_LEFT:
5206 case 'h':
5207 view->x -= MIN(view->x, 2); /* move two columns back */
5208 if (view->x <= 0)
5209 view->count = 0;
5210 break;
5211 case 'a':
5212 case 'w':
5213 if (ch == 'a')
5214 s->force_text_diff = !s->force_text_diff;
5215 if (ch == 'w')
5216 s->ignore_whitespace = !s->ignore_whitespace;
5217 err = reset_diff_view(view);
5218 break;
5219 case 'g':
5220 case KEY_HOME:
5221 s->first_displayed_line = 1;
5222 view->count = 0;
5223 break;
5224 case 'G':
5225 case KEY_END:
5226 view->count = 0;
5227 if (s->eof)
5228 break;
5230 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5231 s->eof = 1;
5232 break;
5233 case 'k':
5234 case KEY_UP:
5235 case CTRL('p'):
5236 if (s->first_displayed_line > 1)
5237 s->first_displayed_line--;
5238 else
5239 view->count = 0;
5240 break;
5241 case CTRL('u'):
5242 case 'u':
5243 nscroll /= 2;
5244 /* FALL THROUGH */
5245 case KEY_PPAGE:
5246 case CTRL('b'):
5247 case 'b':
5248 if (s->first_displayed_line == 1) {
5249 view->count = 0;
5250 break;
5252 i = 0;
5253 while (i++ < nscroll && s->first_displayed_line > 1)
5254 s->first_displayed_line--;
5255 break;
5256 case 'j':
5257 case KEY_DOWN:
5258 case CTRL('n'):
5259 if (!s->eof)
5260 s->first_displayed_line++;
5261 else
5262 view->count = 0;
5263 break;
5264 case CTRL('d'):
5265 case 'd':
5266 nscroll /= 2;
5267 /* FALL THROUGH */
5268 case KEY_NPAGE:
5269 case CTRL('f'):
5270 case 'f':
5271 case ' ':
5272 if (s->eof) {
5273 view->count = 0;
5274 break;
5276 i = 0;
5277 while (!s->eof && i++ < nscroll) {
5278 linelen = getline(&line, &linesize, s->f);
5279 s->first_displayed_line++;
5280 if (linelen == -1) {
5281 if (feof(s->f)) {
5282 s->eof = 1;
5283 } else
5284 err = got_ferror(s->f, GOT_ERR_IO);
5285 break;
5288 free(line);
5289 break;
5290 case '(':
5291 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5292 break;
5293 case ')':
5294 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5295 break;
5296 case '{':
5297 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5298 break;
5299 case '}':
5300 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5301 break;
5302 case '[':
5303 if (s->diff_context > 0) {
5304 s->diff_context--;
5305 s->matched_line = 0;
5306 diff_view_indicate_progress(view);
5307 err = create_diff(s);
5308 if (s->first_displayed_line + view->nlines - 1 >
5309 s->nlines) {
5310 s->first_displayed_line = 1;
5311 s->last_displayed_line = view->nlines;
5313 } else
5314 view->count = 0;
5315 break;
5316 case ']':
5317 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5318 s->diff_context++;
5319 s->matched_line = 0;
5320 diff_view_indicate_progress(view);
5321 err = create_diff(s);
5322 } else
5323 view->count = 0;
5324 break;
5325 case '<':
5326 case ',':
5327 case 'K':
5328 up = 1;
5329 /* FALL THROUGH */
5330 case '>':
5331 case '.':
5332 case 'J':
5333 if (s->parent_view == NULL) {
5334 view->count = 0;
5335 break;
5337 s->parent_view->count = view->count;
5339 if (s->parent_view->type == TOG_VIEW_LOG) {
5340 ls = &s->parent_view->state.log;
5341 old_selected_entry = ls->selected_entry;
5343 err = input_log_view(NULL, s->parent_view,
5344 up ? KEY_UP : KEY_DOWN);
5345 if (err)
5346 break;
5347 view->count = s->parent_view->count;
5349 if (old_selected_entry == ls->selected_entry)
5350 break;
5352 err = set_selected_commit(s, ls->selected_entry);
5353 if (err)
5354 break;
5355 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5356 struct tog_blame_view_state *bs;
5357 struct got_object_id *id, *prev_id;
5359 bs = &s->parent_view->state.blame;
5360 prev_id = get_annotation_for_line(bs->blame.lines,
5361 bs->blame.nlines, bs->last_diffed_line);
5363 err = input_blame_view(&view, s->parent_view,
5364 up ? KEY_UP : KEY_DOWN);
5365 if (err)
5366 break;
5367 view->count = s->parent_view->count;
5369 if (prev_id == NULL)
5370 break;
5371 id = get_selected_commit_id(bs->blame.lines,
5372 bs->blame.nlines, bs->first_displayed_line,
5373 bs->selected_line);
5374 if (id == NULL)
5375 break;
5377 if (!got_object_id_cmp(prev_id, id))
5378 break;
5380 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5381 if (err)
5382 break;
5384 s->first_displayed_line = 1;
5385 s->last_displayed_line = view->nlines;
5386 s->matched_line = 0;
5387 view->x = 0;
5389 diff_view_indicate_progress(view);
5390 err = create_diff(s);
5391 break;
5392 default:
5393 view->count = 0;
5394 break;
5397 return err;
5400 static const struct got_error *
5401 cmd_diff(int argc, char *argv[])
5403 const struct got_error *error = NULL;
5404 struct got_repository *repo = NULL;
5405 struct got_worktree *worktree = NULL;
5406 struct got_object_id *id1 = NULL, *id2 = NULL;
5407 char *repo_path = NULL, *cwd = NULL;
5408 char *id_str1 = NULL, *id_str2 = NULL;
5409 char *label1 = NULL, *label2 = NULL;
5410 int diff_context = 3, ignore_whitespace = 0;
5411 int ch, force_text_diff = 0;
5412 const char *errstr;
5413 struct tog_view *view;
5414 int *pack_fds = NULL;
5416 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5417 switch (ch) {
5418 case 'a':
5419 force_text_diff = 1;
5420 break;
5421 case 'C':
5422 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5423 &errstr);
5424 if (errstr != NULL)
5425 errx(1, "number of context lines is %s: %s",
5426 errstr, errstr);
5427 break;
5428 case 'r':
5429 repo_path = realpath(optarg, NULL);
5430 if (repo_path == NULL)
5431 return got_error_from_errno2("realpath",
5432 optarg);
5433 got_path_strip_trailing_slashes(repo_path);
5434 break;
5435 case 'w':
5436 ignore_whitespace = 1;
5437 break;
5438 default:
5439 usage_diff();
5440 /* NOTREACHED */
5444 argc -= optind;
5445 argv += optind;
5447 if (argc == 0) {
5448 usage_diff(); /* TODO show local worktree changes */
5449 } else if (argc == 2) {
5450 id_str1 = argv[0];
5451 id_str2 = argv[1];
5452 } else
5453 usage_diff();
5455 error = got_repo_pack_fds_open(&pack_fds);
5456 if (error)
5457 goto done;
5459 if (repo_path == NULL) {
5460 cwd = getcwd(NULL, 0);
5461 if (cwd == NULL)
5462 return got_error_from_errno("getcwd");
5463 error = got_worktree_open(&worktree, cwd);
5464 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5465 goto done;
5466 if (worktree)
5467 repo_path =
5468 strdup(got_worktree_get_repo_path(worktree));
5469 else
5470 repo_path = strdup(cwd);
5471 if (repo_path == NULL) {
5472 error = got_error_from_errno("strdup");
5473 goto done;
5477 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5478 if (error)
5479 goto done;
5481 init_curses();
5483 error = apply_unveil(got_repo_get_path(repo), NULL);
5484 if (error)
5485 goto done;
5487 error = tog_load_refs(repo, 0);
5488 if (error)
5489 goto done;
5491 error = got_repo_match_object_id(&id1, &label1, id_str1,
5492 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5493 if (error)
5494 goto done;
5496 error = got_repo_match_object_id(&id2, &label2, id_str2,
5497 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5498 if (error)
5499 goto done;
5501 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5502 if (view == NULL) {
5503 error = got_error_from_errno("view_open");
5504 goto done;
5506 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5507 ignore_whitespace, force_text_diff, NULL, repo);
5508 if (error)
5509 goto done;
5510 error = view_loop(view);
5511 done:
5512 free(label1);
5513 free(label2);
5514 free(repo_path);
5515 free(cwd);
5516 if (repo) {
5517 const struct got_error *close_err = got_repo_close(repo);
5518 if (error == NULL)
5519 error = close_err;
5521 if (worktree)
5522 got_worktree_close(worktree);
5523 if (pack_fds) {
5524 const struct got_error *pack_err =
5525 got_repo_pack_fds_close(pack_fds);
5526 if (error == NULL)
5527 error = pack_err;
5529 tog_free_refs();
5530 return error;
5533 __dead static void
5534 usage_blame(void)
5536 endwin();
5537 fprintf(stderr,
5538 "usage: %s blame [-c commit] [-r repository-path] path\n",
5539 getprogname());
5540 exit(1);
5543 struct tog_blame_line {
5544 int annotated;
5545 struct got_object_id *id;
5548 static const struct got_error *
5549 draw_blame(struct tog_view *view)
5551 struct tog_blame_view_state *s = &view->state.blame;
5552 struct tog_blame *blame = &s->blame;
5553 regmatch_t *regmatch = &view->regmatch;
5554 const struct got_error *err;
5555 int lineno = 0, nprinted = 0;
5556 char *line = NULL;
5557 size_t linesize = 0;
5558 ssize_t linelen;
5559 wchar_t *wline;
5560 int width;
5561 struct tog_blame_line *blame_line;
5562 struct got_object_id *prev_id = NULL;
5563 char *id_str;
5564 struct tog_color *tc;
5566 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5567 if (err)
5568 return err;
5570 rewind(blame->f);
5571 werase(view->window);
5573 if (asprintf(&line, "commit %s", id_str) == -1) {
5574 err = got_error_from_errno("asprintf");
5575 free(id_str);
5576 return err;
5579 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5580 free(line);
5581 line = NULL;
5582 if (err)
5583 return err;
5584 if (view_needs_focus_indication(view))
5585 wstandout(view->window);
5586 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5587 if (tc)
5588 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5589 waddwstr(view->window, wline);
5590 while (width++ < view->ncols)
5591 waddch(view->window, ' ');
5592 if (tc)
5593 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5594 if (view_needs_focus_indication(view))
5595 wstandend(view->window);
5596 free(wline);
5597 wline = NULL;
5599 if (view->gline > blame->nlines)
5600 view->gline = blame->nlines;
5602 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5603 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5604 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5605 free(id_str);
5606 return got_error_from_errno("asprintf");
5608 free(id_str);
5609 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5610 free(line);
5611 line = NULL;
5612 if (err)
5613 return err;
5614 waddwstr(view->window, wline);
5615 free(wline);
5616 wline = NULL;
5617 if (width < view->ncols - 1)
5618 waddch(view->window, '\n');
5620 s->eof = 0;
5621 view->maxx = 0;
5622 while (nprinted < view->nlines - 2) {
5623 linelen = getline(&line, &linesize, blame->f);
5624 if (linelen == -1) {
5625 if (feof(blame->f)) {
5626 s->eof = 1;
5627 break;
5629 free(line);
5630 return got_ferror(blame->f, GOT_ERR_IO);
5632 if (++lineno < s->first_displayed_line)
5633 continue;
5634 if (view->gline && !gotoline(view, &lineno, &nprinted))
5635 continue;
5637 /* Set view->maxx based on full line length. */
5638 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5639 if (err) {
5640 free(line);
5641 return err;
5643 free(wline);
5644 wline = NULL;
5645 view->maxx = MAX(view->maxx, width);
5647 if (nprinted == s->selected_line - 1)
5648 wstandout(view->window);
5650 if (blame->nlines > 0) {
5651 blame_line = &blame->lines[lineno - 1];
5652 if (blame_line->annotated && prev_id &&
5653 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5654 !(nprinted == s->selected_line - 1)) {
5655 waddstr(view->window, " ");
5656 } else if (blame_line->annotated) {
5657 char *id_str;
5658 err = got_object_id_str(&id_str,
5659 blame_line->id);
5660 if (err) {
5661 free(line);
5662 return err;
5664 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5665 if (tc)
5666 wattr_on(view->window,
5667 COLOR_PAIR(tc->colorpair), NULL);
5668 wprintw(view->window, "%.8s", id_str);
5669 if (tc)
5670 wattr_off(view->window,
5671 COLOR_PAIR(tc->colorpair), NULL);
5672 free(id_str);
5673 prev_id = blame_line->id;
5674 } else {
5675 waddstr(view->window, "........");
5676 prev_id = NULL;
5678 } else {
5679 waddstr(view->window, "........");
5680 prev_id = NULL;
5683 if (nprinted == s->selected_line - 1)
5684 wstandend(view->window);
5685 waddstr(view->window, " ");
5687 if (view->ncols <= 9) {
5688 width = 9;
5689 } else if (s->first_displayed_line + nprinted ==
5690 s->matched_line &&
5691 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5692 err = add_matched_line(&width, line, view->ncols - 9, 9,
5693 view->window, view->x, regmatch);
5694 if (err) {
5695 free(line);
5696 return err;
5698 width += 9;
5699 } else {
5700 int skip;
5701 err = format_line(&wline, &width, &skip, line,
5702 view->x, view->ncols - 9, 9, 1);
5703 if (err) {
5704 free(line);
5705 return err;
5707 waddwstr(view->window, &wline[skip]);
5708 width += 9;
5709 free(wline);
5710 wline = NULL;
5713 if (width <= view->ncols - 1)
5714 waddch(view->window, '\n');
5715 if (++nprinted == 1)
5716 s->first_displayed_line = lineno;
5718 free(line);
5719 s->last_displayed_line = lineno;
5721 view_border(view);
5723 return NULL;
5726 static const struct got_error *
5727 blame_cb(void *arg, int nlines, int lineno,
5728 struct got_commit_object *commit, struct got_object_id *id)
5730 const struct got_error *err = NULL;
5731 struct tog_blame_cb_args *a = arg;
5732 struct tog_blame_line *line;
5733 int errcode;
5735 if (nlines != a->nlines ||
5736 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5737 return got_error(GOT_ERR_RANGE);
5739 errcode = pthread_mutex_lock(&tog_mutex);
5740 if (errcode)
5741 return got_error_set_errno(errcode, "pthread_mutex_lock");
5743 if (*a->quit) { /* user has quit the blame view */
5744 err = got_error(GOT_ERR_ITER_COMPLETED);
5745 goto done;
5748 if (lineno == -1)
5749 goto done; /* no change in this commit */
5751 line = &a->lines[lineno - 1];
5752 if (line->annotated)
5753 goto done;
5755 line->id = got_object_id_dup(id);
5756 if (line->id == NULL) {
5757 err = got_error_from_errno("got_object_id_dup");
5758 goto done;
5760 line->annotated = 1;
5761 done:
5762 errcode = pthread_mutex_unlock(&tog_mutex);
5763 if (errcode)
5764 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5765 return err;
5768 static void *
5769 blame_thread(void *arg)
5771 const struct got_error *err, *close_err;
5772 struct tog_blame_thread_args *ta = arg;
5773 struct tog_blame_cb_args *a = ta->cb_args;
5774 int errcode, fd1 = -1, fd2 = -1;
5775 FILE *f1 = NULL, *f2 = NULL;
5777 fd1 = got_opentempfd();
5778 if (fd1 == -1)
5779 return (void *)got_error_from_errno("got_opentempfd");
5781 fd2 = got_opentempfd();
5782 if (fd2 == -1) {
5783 err = got_error_from_errno("got_opentempfd");
5784 goto done;
5787 f1 = got_opentemp();
5788 if (f1 == NULL) {
5789 err = (void *)got_error_from_errno("got_opentemp");
5790 goto done;
5792 f2 = got_opentemp();
5793 if (f2 == NULL) {
5794 err = (void *)got_error_from_errno("got_opentemp");
5795 goto done;
5798 err = block_signals_used_by_main_thread();
5799 if (err)
5800 goto done;
5802 err = got_blame(ta->path, a->commit_id, ta->repo,
5803 tog_diff_algo, blame_cb, ta->cb_args,
5804 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5805 if (err && err->code == GOT_ERR_CANCELLED)
5806 err = NULL;
5808 errcode = pthread_mutex_lock(&tog_mutex);
5809 if (errcode) {
5810 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5811 goto done;
5814 close_err = got_repo_close(ta->repo);
5815 if (err == NULL)
5816 err = close_err;
5817 ta->repo = NULL;
5818 *ta->complete = 1;
5820 errcode = pthread_mutex_unlock(&tog_mutex);
5821 if (errcode && err == NULL)
5822 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5824 done:
5825 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5826 err = got_error_from_errno("close");
5827 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5828 err = got_error_from_errno("close");
5829 if (f1 && fclose(f1) == EOF && err == NULL)
5830 err = got_error_from_errno("fclose");
5831 if (f2 && fclose(f2) == EOF && err == NULL)
5832 err = got_error_from_errno("fclose");
5834 return (void *)err;
5837 static struct got_object_id *
5838 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5839 int first_displayed_line, int selected_line)
5841 struct tog_blame_line *line;
5843 if (nlines <= 0)
5844 return NULL;
5846 line = &lines[first_displayed_line - 1 + selected_line - 1];
5847 if (!line->annotated)
5848 return NULL;
5850 return line->id;
5853 static struct got_object_id *
5854 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5855 int lineno)
5857 struct tog_blame_line *line;
5859 if (nlines <= 0 || lineno >= nlines)
5860 return NULL;
5862 line = &lines[lineno - 1];
5863 if (!line->annotated)
5864 return NULL;
5866 return line->id;
5869 static const struct got_error *
5870 stop_blame(struct tog_blame *blame)
5872 const struct got_error *err = NULL;
5873 int i;
5875 if (blame->thread) {
5876 int errcode;
5877 errcode = pthread_mutex_unlock(&tog_mutex);
5878 if (errcode)
5879 return got_error_set_errno(errcode,
5880 "pthread_mutex_unlock");
5881 errcode = pthread_join(blame->thread, (void **)&err);
5882 if (errcode)
5883 return got_error_set_errno(errcode, "pthread_join");
5884 errcode = pthread_mutex_lock(&tog_mutex);
5885 if (errcode)
5886 return got_error_set_errno(errcode,
5887 "pthread_mutex_lock");
5888 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5889 err = NULL;
5890 blame->thread = NULL;
5892 if (blame->thread_args.repo) {
5893 const struct got_error *close_err;
5894 close_err = got_repo_close(blame->thread_args.repo);
5895 if (err == NULL)
5896 err = close_err;
5897 blame->thread_args.repo = NULL;
5899 if (blame->f) {
5900 if (fclose(blame->f) == EOF && err == NULL)
5901 err = got_error_from_errno("fclose");
5902 blame->f = NULL;
5904 if (blame->lines) {
5905 for (i = 0; i < blame->nlines; i++)
5906 free(blame->lines[i].id);
5907 free(blame->lines);
5908 blame->lines = NULL;
5910 free(blame->cb_args.commit_id);
5911 blame->cb_args.commit_id = NULL;
5912 if (blame->pack_fds) {
5913 const struct got_error *pack_err =
5914 got_repo_pack_fds_close(blame->pack_fds);
5915 if (err == NULL)
5916 err = pack_err;
5917 blame->pack_fds = NULL;
5919 return err;
5922 static const struct got_error *
5923 cancel_blame_view(void *arg)
5925 const struct got_error *err = NULL;
5926 int *done = arg;
5927 int errcode;
5929 errcode = pthread_mutex_lock(&tog_mutex);
5930 if (errcode)
5931 return got_error_set_errno(errcode,
5932 "pthread_mutex_unlock");
5934 if (*done)
5935 err = got_error(GOT_ERR_CANCELLED);
5937 errcode = pthread_mutex_unlock(&tog_mutex);
5938 if (errcode)
5939 return got_error_set_errno(errcode,
5940 "pthread_mutex_lock");
5942 return err;
5945 static const struct got_error *
5946 run_blame(struct tog_view *view)
5948 struct tog_blame_view_state *s = &view->state.blame;
5949 struct tog_blame *blame = &s->blame;
5950 const struct got_error *err = NULL;
5951 struct got_commit_object *commit = NULL;
5952 struct got_blob_object *blob = NULL;
5953 struct got_repository *thread_repo = NULL;
5954 struct got_object_id *obj_id = NULL;
5955 int obj_type, fd = -1;
5956 int *pack_fds = NULL;
5958 err = got_object_open_as_commit(&commit, s->repo,
5959 &s->blamed_commit->id);
5960 if (err)
5961 return err;
5963 fd = got_opentempfd();
5964 if (fd == -1) {
5965 err = got_error_from_errno("got_opentempfd");
5966 goto done;
5969 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5970 if (err)
5971 goto done;
5973 err = got_object_get_type(&obj_type, s->repo, obj_id);
5974 if (err)
5975 goto done;
5977 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5978 err = got_error(GOT_ERR_OBJ_TYPE);
5979 goto done;
5982 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5983 if (err)
5984 goto done;
5985 blame->f = got_opentemp();
5986 if (blame->f == NULL) {
5987 err = got_error_from_errno("got_opentemp");
5988 goto done;
5990 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5991 &blame->line_offsets, blame->f, blob);
5992 if (err)
5993 goto done;
5994 if (blame->nlines == 0) {
5995 s->blame_complete = 1;
5996 goto done;
5999 /* Don't include \n at EOF in the blame line count. */
6000 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6001 blame->nlines--;
6003 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6004 if (blame->lines == NULL) {
6005 err = got_error_from_errno("calloc");
6006 goto done;
6009 err = got_repo_pack_fds_open(&pack_fds);
6010 if (err)
6011 goto done;
6012 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6013 pack_fds);
6014 if (err)
6015 goto done;
6017 blame->pack_fds = pack_fds;
6018 blame->cb_args.view = view;
6019 blame->cb_args.lines = blame->lines;
6020 blame->cb_args.nlines = blame->nlines;
6021 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6022 if (blame->cb_args.commit_id == NULL) {
6023 err = got_error_from_errno("got_object_id_dup");
6024 goto done;
6026 blame->cb_args.quit = &s->done;
6028 blame->thread_args.path = s->path;
6029 blame->thread_args.repo = thread_repo;
6030 blame->thread_args.cb_args = &blame->cb_args;
6031 blame->thread_args.complete = &s->blame_complete;
6032 blame->thread_args.cancel_cb = cancel_blame_view;
6033 blame->thread_args.cancel_arg = &s->done;
6034 s->blame_complete = 0;
6036 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6037 s->first_displayed_line = 1;
6038 s->last_displayed_line = view->nlines;
6039 s->selected_line = 1;
6041 s->matched_line = 0;
6043 done:
6044 if (commit)
6045 got_object_commit_close(commit);
6046 if (fd != -1 && close(fd) == -1 && err == NULL)
6047 err = got_error_from_errno("close");
6048 if (blob)
6049 got_object_blob_close(blob);
6050 free(obj_id);
6051 if (err)
6052 stop_blame(blame);
6053 return err;
6056 static const struct got_error *
6057 open_blame_view(struct tog_view *view, char *path,
6058 struct got_object_id *commit_id, struct got_repository *repo)
6060 const struct got_error *err = NULL;
6061 struct tog_blame_view_state *s = &view->state.blame;
6063 STAILQ_INIT(&s->blamed_commits);
6065 s->path = strdup(path);
6066 if (s->path == NULL)
6067 return got_error_from_errno("strdup");
6069 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6070 if (err) {
6071 free(s->path);
6072 return err;
6075 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6076 s->first_displayed_line = 1;
6077 s->last_displayed_line = view->nlines;
6078 s->selected_line = 1;
6079 s->blame_complete = 0;
6080 s->repo = repo;
6081 s->commit_id = commit_id;
6082 memset(&s->blame, 0, sizeof(s->blame));
6084 STAILQ_INIT(&s->colors);
6085 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6086 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6087 get_color_value("TOG_COLOR_COMMIT"));
6088 if (err)
6089 return err;
6092 view->show = show_blame_view;
6093 view->input = input_blame_view;
6094 view->reset = reset_blame_view;
6095 view->close = close_blame_view;
6096 view->search_start = search_start_blame_view;
6097 view->search_setup = search_setup_blame_view;
6098 view->search_next = search_next_view_match;
6100 return run_blame(view);
6103 static const struct got_error *
6104 close_blame_view(struct tog_view *view)
6106 const struct got_error *err = NULL;
6107 struct tog_blame_view_state *s = &view->state.blame;
6109 if (s->blame.thread)
6110 err = stop_blame(&s->blame);
6112 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6113 struct got_object_qid *blamed_commit;
6114 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6115 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6116 got_object_qid_free(blamed_commit);
6119 free(s->path);
6120 free_colors(&s->colors);
6121 return err;
6124 static const struct got_error *
6125 search_start_blame_view(struct tog_view *view)
6127 struct tog_blame_view_state *s = &view->state.blame;
6129 s->matched_line = 0;
6130 return NULL;
6133 static void
6134 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6135 size_t *nlines, int **first, int **last, int **match, int **selected)
6137 struct tog_blame_view_state *s = &view->state.blame;
6139 *f = s->blame.f;
6140 *nlines = s->blame.nlines;
6141 *line_offsets = s->blame.line_offsets;
6142 *match = &s->matched_line;
6143 *first = &s->first_displayed_line;
6144 *last = &s->last_displayed_line;
6145 *selected = &s->selected_line;
6148 static const struct got_error *
6149 show_blame_view(struct tog_view *view)
6151 const struct got_error *err = NULL;
6152 struct tog_blame_view_state *s = &view->state.blame;
6153 int errcode;
6155 if (s->blame.thread == NULL && !s->blame_complete) {
6156 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6157 &s->blame.thread_args);
6158 if (errcode)
6159 return got_error_set_errno(errcode, "pthread_create");
6161 halfdelay(1); /* fast refresh while annotating */
6164 if (s->blame_complete)
6165 halfdelay(10); /* disable fast refresh */
6167 err = draw_blame(view);
6169 view_border(view);
6170 return err;
6173 static const struct got_error *
6174 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6175 struct got_repository *repo, struct got_object_id *id)
6177 struct tog_view *log_view;
6178 const struct got_error *err = NULL;
6180 *new_view = NULL;
6182 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6183 if (log_view == NULL)
6184 return got_error_from_errno("view_open");
6186 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6187 if (err)
6188 view_close(log_view);
6189 else
6190 *new_view = log_view;
6192 return err;
6195 static const struct got_error *
6196 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6198 const struct got_error *err = NULL, *thread_err = NULL;
6199 struct tog_view *diff_view;
6200 struct tog_blame_view_state *s = &view->state.blame;
6201 int eos, nscroll, begin_y = 0, begin_x = 0;
6203 eos = nscroll = view->nlines - 2;
6204 if (view_is_hsplit_top(view))
6205 --eos; /* border */
6207 switch (ch) {
6208 case '0':
6209 view->x = 0;
6210 break;
6211 case '$':
6212 view->x = MAX(view->maxx - view->ncols / 3, 0);
6213 view->count = 0;
6214 break;
6215 case KEY_RIGHT:
6216 case 'l':
6217 if (view->x + view->ncols / 3 < view->maxx)
6218 view->x += 2; /* move two columns right */
6219 else
6220 view->count = 0;
6221 break;
6222 case KEY_LEFT:
6223 case 'h':
6224 view->x -= MIN(view->x, 2); /* move two columns back */
6225 if (view->x <= 0)
6226 view->count = 0;
6227 break;
6228 case 'q':
6229 s->done = 1;
6230 break;
6231 case 'g':
6232 case KEY_HOME:
6233 s->selected_line = 1;
6234 s->first_displayed_line = 1;
6235 view->count = 0;
6236 break;
6237 case 'G':
6238 case KEY_END:
6239 if (s->blame.nlines < eos) {
6240 s->selected_line = s->blame.nlines;
6241 s->first_displayed_line = 1;
6242 } else {
6243 s->selected_line = eos;
6244 s->first_displayed_line = s->blame.nlines - (eos - 1);
6246 view->count = 0;
6247 break;
6248 case 'k':
6249 case KEY_UP:
6250 case CTRL('p'):
6251 if (s->selected_line > 1)
6252 s->selected_line--;
6253 else if (s->selected_line == 1 &&
6254 s->first_displayed_line > 1)
6255 s->first_displayed_line--;
6256 else
6257 view->count = 0;
6258 break;
6259 case CTRL('u'):
6260 case 'u':
6261 nscroll /= 2;
6262 /* FALL THROUGH */
6263 case KEY_PPAGE:
6264 case CTRL('b'):
6265 case 'b':
6266 if (s->first_displayed_line == 1) {
6267 if (view->count > 1)
6268 nscroll += nscroll;
6269 s->selected_line = MAX(1, s->selected_line - nscroll);
6270 view->count = 0;
6271 break;
6273 if (s->first_displayed_line > nscroll)
6274 s->first_displayed_line -= nscroll;
6275 else
6276 s->first_displayed_line = 1;
6277 break;
6278 case 'j':
6279 case KEY_DOWN:
6280 case CTRL('n'):
6281 if (s->selected_line < eos && s->first_displayed_line +
6282 s->selected_line <= s->blame.nlines)
6283 s->selected_line++;
6284 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6285 s->first_displayed_line++;
6286 else
6287 view->count = 0;
6288 break;
6289 case 'c':
6290 case 'p': {
6291 struct got_object_id *id = NULL;
6293 view->count = 0;
6294 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6295 s->first_displayed_line, s->selected_line);
6296 if (id == NULL)
6297 break;
6298 if (ch == 'p') {
6299 struct got_commit_object *commit, *pcommit;
6300 struct got_object_qid *pid;
6301 struct got_object_id *blob_id = NULL;
6302 int obj_type;
6303 err = got_object_open_as_commit(&commit,
6304 s->repo, id);
6305 if (err)
6306 break;
6307 pid = STAILQ_FIRST(
6308 got_object_commit_get_parent_ids(commit));
6309 if (pid == NULL) {
6310 got_object_commit_close(commit);
6311 break;
6313 /* Check if path history ends here. */
6314 err = got_object_open_as_commit(&pcommit,
6315 s->repo, &pid->id);
6316 if (err)
6317 break;
6318 err = got_object_id_by_path(&blob_id, s->repo,
6319 pcommit, s->path);
6320 got_object_commit_close(pcommit);
6321 if (err) {
6322 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6323 err = NULL;
6324 got_object_commit_close(commit);
6325 break;
6327 err = got_object_get_type(&obj_type, s->repo,
6328 blob_id);
6329 free(blob_id);
6330 /* Can't blame non-blob type objects. */
6331 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6332 got_object_commit_close(commit);
6333 break;
6335 err = got_object_qid_alloc(&s->blamed_commit,
6336 &pid->id);
6337 got_object_commit_close(commit);
6338 } else {
6339 if (got_object_id_cmp(id,
6340 &s->blamed_commit->id) == 0)
6341 break;
6342 err = got_object_qid_alloc(&s->blamed_commit,
6343 id);
6345 if (err)
6346 break;
6347 s->done = 1;
6348 thread_err = stop_blame(&s->blame);
6349 s->done = 0;
6350 if (thread_err)
6351 break;
6352 STAILQ_INSERT_HEAD(&s->blamed_commits,
6353 s->blamed_commit, entry);
6354 err = run_blame(view);
6355 if (err)
6356 break;
6357 break;
6359 case 'C': {
6360 struct got_object_qid *first;
6362 view->count = 0;
6363 first = STAILQ_FIRST(&s->blamed_commits);
6364 if (!got_object_id_cmp(&first->id, s->commit_id))
6365 break;
6366 s->done = 1;
6367 thread_err = stop_blame(&s->blame);
6368 s->done = 0;
6369 if (thread_err)
6370 break;
6371 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6372 got_object_qid_free(s->blamed_commit);
6373 s->blamed_commit =
6374 STAILQ_FIRST(&s->blamed_commits);
6375 err = run_blame(view);
6376 if (err)
6377 break;
6378 break;
6380 case 'L':
6381 view->count = 0;
6382 s->id_to_log = get_selected_commit_id(s->blame.lines,
6383 s->blame.nlines, s->first_displayed_line, s->selected_line);
6384 if (s->id_to_log)
6385 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6386 break;
6387 case KEY_ENTER:
6388 case '\r': {
6389 struct got_object_id *id = NULL;
6390 struct got_object_qid *pid;
6391 struct got_commit_object *commit = NULL;
6393 view->count = 0;
6394 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6395 s->first_displayed_line, s->selected_line);
6396 if (id == NULL)
6397 break;
6398 err = got_object_open_as_commit(&commit, s->repo, id);
6399 if (err)
6400 break;
6401 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6402 if (*new_view) {
6403 /* traversed from diff view, release diff resources */
6404 err = close_diff_view(*new_view);
6405 if (err)
6406 break;
6407 diff_view = *new_view;
6408 } else {
6409 if (view_is_parent_view(view))
6410 view_get_split(view, &begin_y, &begin_x);
6412 diff_view = view_open(0, 0, begin_y, begin_x,
6413 TOG_VIEW_DIFF);
6414 if (diff_view == NULL) {
6415 got_object_commit_close(commit);
6416 err = got_error_from_errno("view_open");
6417 break;
6420 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6421 id, NULL, NULL, 3, 0, 0, view, s->repo);
6422 got_object_commit_close(commit);
6423 if (err) {
6424 view_close(diff_view);
6425 break;
6427 s->last_diffed_line = s->first_displayed_line - 1 +
6428 s->selected_line;
6429 if (*new_view)
6430 break; /* still open from active diff view */
6431 if (view_is_parent_view(view) &&
6432 view->mode == TOG_VIEW_SPLIT_HRZN) {
6433 err = view_init_hsplit(view, begin_y);
6434 if (err)
6435 break;
6438 view->focussed = 0;
6439 diff_view->focussed = 1;
6440 diff_view->mode = view->mode;
6441 diff_view->nlines = view->lines - begin_y;
6442 if (view_is_parent_view(view)) {
6443 view_transfer_size(diff_view, view);
6444 err = view_close_child(view);
6445 if (err)
6446 break;
6447 err = view_set_child(view, diff_view);
6448 if (err)
6449 break;
6450 view->focus_child = 1;
6451 } else
6452 *new_view = diff_view;
6453 if (err)
6454 break;
6455 break;
6457 case CTRL('d'):
6458 case 'd':
6459 nscroll /= 2;
6460 /* FALL THROUGH */
6461 case KEY_NPAGE:
6462 case CTRL('f'):
6463 case 'f':
6464 case ' ':
6465 if (s->last_displayed_line >= s->blame.nlines &&
6466 s->selected_line >= MIN(s->blame.nlines,
6467 view->nlines - 2)) {
6468 view->count = 0;
6469 break;
6471 if (s->last_displayed_line >= s->blame.nlines &&
6472 s->selected_line < view->nlines - 2) {
6473 s->selected_line +=
6474 MIN(nscroll, s->last_displayed_line -
6475 s->first_displayed_line - s->selected_line + 1);
6477 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6478 s->first_displayed_line += nscroll;
6479 else
6480 s->first_displayed_line =
6481 s->blame.nlines - (view->nlines - 3);
6482 break;
6483 case KEY_RESIZE:
6484 if (s->selected_line > view->nlines - 2) {
6485 s->selected_line = MIN(s->blame.nlines,
6486 view->nlines - 2);
6488 break;
6489 default:
6490 view->count = 0;
6491 break;
6493 return thread_err ? thread_err : err;
6496 static const struct got_error *
6497 reset_blame_view(struct tog_view *view)
6499 const struct got_error *err;
6500 struct tog_blame_view_state *s = &view->state.blame;
6502 view->count = 0;
6503 s->done = 1;
6504 err = stop_blame(&s->blame);
6505 s->done = 0;
6506 if (err)
6507 return err;
6508 return run_blame(view);
6511 static const struct got_error *
6512 cmd_blame(int argc, char *argv[])
6514 const struct got_error *error;
6515 struct got_repository *repo = NULL;
6516 struct got_worktree *worktree = NULL;
6517 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6518 char *link_target = NULL;
6519 struct got_object_id *commit_id = NULL;
6520 struct got_commit_object *commit = NULL;
6521 char *commit_id_str = NULL;
6522 int ch;
6523 struct tog_view *view;
6524 int *pack_fds = NULL;
6526 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6527 switch (ch) {
6528 case 'c':
6529 commit_id_str = optarg;
6530 break;
6531 case 'r':
6532 repo_path = realpath(optarg, NULL);
6533 if (repo_path == NULL)
6534 return got_error_from_errno2("realpath",
6535 optarg);
6536 break;
6537 default:
6538 usage_blame();
6539 /* NOTREACHED */
6543 argc -= optind;
6544 argv += optind;
6546 if (argc != 1)
6547 usage_blame();
6549 error = got_repo_pack_fds_open(&pack_fds);
6550 if (error != NULL)
6551 goto done;
6553 if (repo_path == NULL) {
6554 cwd = getcwd(NULL, 0);
6555 if (cwd == NULL)
6556 return got_error_from_errno("getcwd");
6557 error = got_worktree_open(&worktree, cwd);
6558 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6559 goto done;
6560 if (worktree)
6561 repo_path =
6562 strdup(got_worktree_get_repo_path(worktree));
6563 else
6564 repo_path = strdup(cwd);
6565 if (repo_path == NULL) {
6566 error = got_error_from_errno("strdup");
6567 goto done;
6571 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6572 if (error != NULL)
6573 goto done;
6575 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6576 worktree);
6577 if (error)
6578 goto done;
6580 init_curses();
6582 error = apply_unveil(got_repo_get_path(repo), NULL);
6583 if (error)
6584 goto done;
6586 error = tog_load_refs(repo, 0);
6587 if (error)
6588 goto done;
6590 if (commit_id_str == NULL) {
6591 struct got_reference *head_ref;
6592 error = got_ref_open(&head_ref, repo, worktree ?
6593 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6594 if (error != NULL)
6595 goto done;
6596 error = got_ref_resolve(&commit_id, repo, head_ref);
6597 got_ref_close(head_ref);
6598 } else {
6599 error = got_repo_match_object_id(&commit_id, NULL,
6600 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6602 if (error != NULL)
6603 goto done;
6605 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6606 if (view == NULL) {
6607 error = got_error_from_errno("view_open");
6608 goto done;
6611 error = got_object_open_as_commit(&commit, repo, commit_id);
6612 if (error)
6613 goto done;
6615 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6616 commit, repo);
6617 if (error)
6618 goto done;
6620 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6621 commit_id, repo);
6622 if (error)
6623 goto done;
6624 if (worktree) {
6625 /* Release work tree lock. */
6626 got_worktree_close(worktree);
6627 worktree = NULL;
6629 error = view_loop(view);
6630 done:
6631 free(repo_path);
6632 free(in_repo_path);
6633 free(link_target);
6634 free(cwd);
6635 free(commit_id);
6636 if (commit)
6637 got_object_commit_close(commit);
6638 if (worktree)
6639 got_worktree_close(worktree);
6640 if (repo) {
6641 const struct got_error *close_err = got_repo_close(repo);
6642 if (error == NULL)
6643 error = close_err;
6645 if (pack_fds) {
6646 const struct got_error *pack_err =
6647 got_repo_pack_fds_close(pack_fds);
6648 if (error == NULL)
6649 error = pack_err;
6651 tog_free_refs();
6652 return error;
6655 static const struct got_error *
6656 draw_tree_entries(struct tog_view *view, const char *parent_path)
6658 struct tog_tree_view_state *s = &view->state.tree;
6659 const struct got_error *err = NULL;
6660 struct got_tree_entry *te;
6661 wchar_t *wline;
6662 char *index = NULL;
6663 struct tog_color *tc;
6664 int width, n, nentries, i = 1;
6665 int limit = view->nlines;
6667 s->ndisplayed = 0;
6668 if (view_is_hsplit_top(view))
6669 --limit; /* border */
6671 werase(view->window);
6673 if (limit == 0)
6674 return NULL;
6676 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6677 0, 0);
6678 if (err)
6679 return err;
6680 if (view_needs_focus_indication(view))
6681 wstandout(view->window);
6682 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6683 if (tc)
6684 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6685 waddwstr(view->window, wline);
6686 free(wline);
6687 wline = NULL;
6688 while (width++ < view->ncols)
6689 waddch(view->window, ' ');
6690 if (tc)
6691 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6692 if (view_needs_focus_indication(view))
6693 wstandend(view->window);
6694 if (--limit <= 0)
6695 return NULL;
6697 i += s->selected;
6698 if (s->first_displayed_entry) {
6699 i += got_tree_entry_get_index(s->first_displayed_entry);
6700 if (s->tree != s->root)
6701 ++i; /* account for ".." entry */
6703 nentries = got_object_tree_get_nentries(s->tree);
6704 if (asprintf(&index, "[%d/%d] %s",
6705 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6706 return got_error_from_errno("asprintf");
6707 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6708 free(index);
6709 if (err)
6710 return err;
6711 waddwstr(view->window, wline);
6712 free(wline);
6713 wline = NULL;
6714 if (width < view->ncols - 1)
6715 waddch(view->window, '\n');
6716 if (--limit <= 0)
6717 return NULL;
6718 waddch(view->window, '\n');
6719 if (--limit <= 0)
6720 return NULL;
6722 if (s->first_displayed_entry == NULL) {
6723 te = got_object_tree_get_first_entry(s->tree);
6724 if (s->selected == 0) {
6725 if (view->focussed)
6726 wstandout(view->window);
6727 s->selected_entry = NULL;
6729 waddstr(view->window, " ..\n"); /* parent directory */
6730 if (s->selected == 0 && view->focussed)
6731 wstandend(view->window);
6732 s->ndisplayed++;
6733 if (--limit <= 0)
6734 return NULL;
6735 n = 1;
6736 } else {
6737 n = 0;
6738 te = s->first_displayed_entry;
6741 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6742 char *line = NULL, *id_str = NULL, *link_target = NULL;
6743 const char *modestr = "";
6744 mode_t mode;
6746 te = got_object_tree_get_entry(s->tree, i);
6747 mode = got_tree_entry_get_mode(te);
6749 if (s->show_ids) {
6750 err = got_object_id_str(&id_str,
6751 got_tree_entry_get_id(te));
6752 if (err)
6753 return got_error_from_errno(
6754 "got_object_id_str");
6756 if (got_object_tree_entry_is_submodule(te))
6757 modestr = "$";
6758 else if (S_ISLNK(mode)) {
6759 int i;
6761 err = got_tree_entry_get_symlink_target(&link_target,
6762 te, s->repo);
6763 if (err) {
6764 free(id_str);
6765 return err;
6767 for (i = 0; i < strlen(link_target); i++) {
6768 if (!isprint((unsigned char)link_target[i]))
6769 link_target[i] = '?';
6771 modestr = "@";
6773 else if (S_ISDIR(mode))
6774 modestr = "/";
6775 else if (mode & S_IXUSR)
6776 modestr = "*";
6777 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6778 got_tree_entry_get_name(te), modestr,
6779 link_target ? " -> ": "",
6780 link_target ? link_target : "") == -1) {
6781 free(id_str);
6782 free(link_target);
6783 return got_error_from_errno("asprintf");
6785 free(id_str);
6786 free(link_target);
6787 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6788 0, 0);
6789 if (err) {
6790 free(line);
6791 break;
6793 if (n == s->selected) {
6794 if (view->focussed)
6795 wstandout(view->window);
6796 s->selected_entry = te;
6798 tc = match_color(&s->colors, line);
6799 if (tc)
6800 wattr_on(view->window,
6801 COLOR_PAIR(tc->colorpair), NULL);
6802 waddwstr(view->window, wline);
6803 if (tc)
6804 wattr_off(view->window,
6805 COLOR_PAIR(tc->colorpair), NULL);
6806 if (width < view->ncols - 1)
6807 waddch(view->window, '\n');
6808 if (n == s->selected && view->focussed)
6809 wstandend(view->window);
6810 free(line);
6811 free(wline);
6812 wline = NULL;
6813 n++;
6814 s->ndisplayed++;
6815 s->last_displayed_entry = te;
6816 if (--limit <= 0)
6817 break;
6820 return err;
6823 static void
6824 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6826 struct got_tree_entry *te;
6827 int isroot = s->tree == s->root;
6828 int i = 0;
6830 if (s->first_displayed_entry == NULL)
6831 return;
6833 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6834 while (i++ < maxscroll) {
6835 if (te == NULL) {
6836 if (!isroot)
6837 s->first_displayed_entry = NULL;
6838 break;
6840 s->first_displayed_entry = te;
6841 te = got_tree_entry_get_prev(s->tree, te);
6845 static const struct got_error *
6846 tree_scroll_down(struct tog_view *view, int maxscroll)
6848 struct tog_tree_view_state *s = &view->state.tree;
6849 struct got_tree_entry *next, *last;
6850 int n = 0;
6852 if (s->first_displayed_entry)
6853 next = got_tree_entry_get_next(s->tree,
6854 s->first_displayed_entry);
6855 else
6856 next = got_object_tree_get_first_entry(s->tree);
6858 last = s->last_displayed_entry;
6859 while (next && n++ < maxscroll) {
6860 if (last) {
6861 s->last_displayed_entry = last;
6862 last = got_tree_entry_get_next(s->tree, last);
6864 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6865 s->first_displayed_entry = next;
6866 next = got_tree_entry_get_next(s->tree, next);
6870 return NULL;
6873 static const struct got_error *
6874 tree_entry_path(char **path, struct tog_parent_trees *parents,
6875 struct got_tree_entry *te)
6877 const struct got_error *err = NULL;
6878 struct tog_parent_tree *pt;
6879 size_t len = 2; /* for leading slash and NUL */
6881 TAILQ_FOREACH(pt, parents, entry)
6882 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6883 + 1 /* slash */;
6884 if (te)
6885 len += strlen(got_tree_entry_get_name(te));
6887 *path = calloc(1, len);
6888 if (path == NULL)
6889 return got_error_from_errno("calloc");
6891 (*path)[0] = '/';
6892 pt = TAILQ_LAST(parents, tog_parent_trees);
6893 while (pt) {
6894 const char *name = got_tree_entry_get_name(pt->selected_entry);
6895 if (strlcat(*path, name, len) >= len) {
6896 err = got_error(GOT_ERR_NO_SPACE);
6897 goto done;
6899 if (strlcat(*path, "/", len) >= len) {
6900 err = got_error(GOT_ERR_NO_SPACE);
6901 goto done;
6903 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6905 if (te) {
6906 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6907 err = got_error(GOT_ERR_NO_SPACE);
6908 goto done;
6911 done:
6912 if (err) {
6913 free(*path);
6914 *path = NULL;
6916 return err;
6919 static const struct got_error *
6920 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6921 struct got_tree_entry *te, struct tog_parent_trees *parents,
6922 struct got_object_id *commit_id, struct got_repository *repo)
6924 const struct got_error *err = NULL;
6925 char *path;
6926 struct tog_view *blame_view;
6928 *new_view = NULL;
6930 err = tree_entry_path(&path, parents, te);
6931 if (err)
6932 return err;
6934 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6935 if (blame_view == NULL) {
6936 err = got_error_from_errno("view_open");
6937 goto done;
6940 err = open_blame_view(blame_view, path, commit_id, repo);
6941 if (err) {
6942 if (err->code == GOT_ERR_CANCELLED)
6943 err = NULL;
6944 view_close(blame_view);
6945 } else
6946 *new_view = blame_view;
6947 done:
6948 free(path);
6949 return err;
6952 static const struct got_error *
6953 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6954 struct tog_tree_view_state *s)
6956 struct tog_view *log_view;
6957 const struct got_error *err = NULL;
6958 char *path;
6960 *new_view = NULL;
6962 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6963 if (log_view == NULL)
6964 return got_error_from_errno("view_open");
6966 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6967 if (err)
6968 return err;
6970 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6971 path, 0);
6972 if (err)
6973 view_close(log_view);
6974 else
6975 *new_view = log_view;
6976 free(path);
6977 return err;
6980 static const struct got_error *
6981 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6982 const char *head_ref_name, struct got_repository *repo)
6984 const struct got_error *err = NULL;
6985 char *commit_id_str = NULL;
6986 struct tog_tree_view_state *s = &view->state.tree;
6987 struct got_commit_object *commit = NULL;
6989 TAILQ_INIT(&s->parents);
6990 STAILQ_INIT(&s->colors);
6992 s->commit_id = got_object_id_dup(commit_id);
6993 if (s->commit_id == NULL)
6994 return got_error_from_errno("got_object_id_dup");
6996 err = got_object_open_as_commit(&commit, repo, commit_id);
6997 if (err)
6998 goto done;
7001 * The root is opened here and will be closed when the view is closed.
7002 * Any visited subtrees and their path-wise parents are opened and
7003 * closed on demand.
7005 err = got_object_open_as_tree(&s->root, repo,
7006 got_object_commit_get_tree_id(commit));
7007 if (err)
7008 goto done;
7009 s->tree = s->root;
7011 err = got_object_id_str(&commit_id_str, commit_id);
7012 if (err != NULL)
7013 goto done;
7015 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7016 err = got_error_from_errno("asprintf");
7017 goto done;
7020 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7021 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7022 if (head_ref_name) {
7023 s->head_ref_name = strdup(head_ref_name);
7024 if (s->head_ref_name == NULL) {
7025 err = got_error_from_errno("strdup");
7026 goto done;
7029 s->repo = repo;
7031 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7032 err = add_color(&s->colors, "\\$$",
7033 TOG_COLOR_TREE_SUBMODULE,
7034 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7035 if (err)
7036 goto done;
7037 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7038 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7039 if (err)
7040 goto done;
7041 err = add_color(&s->colors, "/$",
7042 TOG_COLOR_TREE_DIRECTORY,
7043 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7044 if (err)
7045 goto done;
7047 err = add_color(&s->colors, "\\*$",
7048 TOG_COLOR_TREE_EXECUTABLE,
7049 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7050 if (err)
7051 goto done;
7053 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7054 get_color_value("TOG_COLOR_COMMIT"));
7055 if (err)
7056 goto done;
7059 view->show = show_tree_view;
7060 view->input = input_tree_view;
7061 view->close = close_tree_view;
7062 view->search_start = search_start_tree_view;
7063 view->search_next = search_next_tree_view;
7064 done:
7065 free(commit_id_str);
7066 if (commit)
7067 got_object_commit_close(commit);
7068 if (err)
7069 close_tree_view(view);
7070 return err;
7073 static const struct got_error *
7074 close_tree_view(struct tog_view *view)
7076 struct tog_tree_view_state *s = &view->state.tree;
7078 free_colors(&s->colors);
7079 free(s->tree_label);
7080 s->tree_label = NULL;
7081 free(s->commit_id);
7082 s->commit_id = NULL;
7083 free(s->head_ref_name);
7084 s->head_ref_name = NULL;
7085 while (!TAILQ_EMPTY(&s->parents)) {
7086 struct tog_parent_tree *parent;
7087 parent = TAILQ_FIRST(&s->parents);
7088 TAILQ_REMOVE(&s->parents, parent, entry);
7089 if (parent->tree != s->root)
7090 got_object_tree_close(parent->tree);
7091 free(parent);
7094 if (s->tree != NULL && s->tree != s->root)
7095 got_object_tree_close(s->tree);
7096 if (s->root)
7097 got_object_tree_close(s->root);
7098 return NULL;
7101 static const struct got_error *
7102 search_start_tree_view(struct tog_view *view)
7104 struct tog_tree_view_state *s = &view->state.tree;
7106 s->matched_entry = NULL;
7107 return NULL;
7110 static int
7111 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7113 regmatch_t regmatch;
7115 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7116 0) == 0;
7119 static const struct got_error *
7120 search_next_tree_view(struct tog_view *view)
7122 struct tog_tree_view_state *s = &view->state.tree;
7123 struct got_tree_entry *te = NULL;
7125 if (!view->searching) {
7126 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7127 return NULL;
7130 if (s->matched_entry) {
7131 if (view->searching == TOG_SEARCH_FORWARD) {
7132 if (s->selected_entry)
7133 te = got_tree_entry_get_next(s->tree,
7134 s->selected_entry);
7135 else
7136 te = got_object_tree_get_first_entry(s->tree);
7137 } else {
7138 if (s->selected_entry == NULL)
7139 te = got_object_tree_get_last_entry(s->tree);
7140 else
7141 te = got_tree_entry_get_prev(s->tree,
7142 s->selected_entry);
7144 } else {
7145 if (s->selected_entry)
7146 te = s->selected_entry;
7147 else if (view->searching == TOG_SEARCH_FORWARD)
7148 te = got_object_tree_get_first_entry(s->tree);
7149 else
7150 te = got_object_tree_get_last_entry(s->tree);
7153 while (1) {
7154 if (te == NULL) {
7155 if (s->matched_entry == NULL) {
7156 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7157 return NULL;
7159 if (view->searching == TOG_SEARCH_FORWARD)
7160 te = got_object_tree_get_first_entry(s->tree);
7161 else
7162 te = got_object_tree_get_last_entry(s->tree);
7165 if (match_tree_entry(te, &view->regex)) {
7166 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7167 s->matched_entry = te;
7168 break;
7171 if (view->searching == TOG_SEARCH_FORWARD)
7172 te = got_tree_entry_get_next(s->tree, te);
7173 else
7174 te = got_tree_entry_get_prev(s->tree, te);
7177 if (s->matched_entry) {
7178 s->first_displayed_entry = s->matched_entry;
7179 s->selected = 0;
7182 return NULL;
7185 static const struct got_error *
7186 show_tree_view(struct tog_view *view)
7188 const struct got_error *err = NULL;
7189 struct tog_tree_view_state *s = &view->state.tree;
7190 char *parent_path;
7192 err = tree_entry_path(&parent_path, &s->parents, NULL);
7193 if (err)
7194 return err;
7196 err = draw_tree_entries(view, parent_path);
7197 free(parent_path);
7199 view_border(view);
7200 return err;
7203 static const struct got_error *
7204 tree_goto_line(struct tog_view *view, int nlines)
7206 const struct got_error *err = NULL;
7207 struct tog_tree_view_state *s = &view->state.tree;
7208 struct got_tree_entry **fte, **lte, **ste;
7209 int g, last, first = 1, i = 1;
7210 int root = s->tree == s->root;
7211 int off = root ? 1 : 2;
7213 g = view->gline;
7214 view->gline = 0;
7216 if (g == 0)
7217 g = 1;
7218 else if (g > got_object_tree_get_nentries(s->tree))
7219 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7221 fte = &s->first_displayed_entry;
7222 lte = &s->last_displayed_entry;
7223 ste = &s->selected_entry;
7225 if (*fte != NULL) {
7226 first = got_tree_entry_get_index(*fte);
7227 first += off; /* account for ".." */
7229 last = got_tree_entry_get_index(*lte);
7230 last += off;
7232 if (g >= first && g <= last && g - first < nlines) {
7233 s->selected = g - first;
7234 return NULL; /* gline is on the current page */
7237 if (*ste != NULL) {
7238 i = got_tree_entry_get_index(*ste);
7239 i += off;
7242 if (i < g) {
7243 err = tree_scroll_down(view, g - i);
7244 if (err)
7245 return err;
7246 if (got_tree_entry_get_index(*lte) >=
7247 got_object_tree_get_nentries(s->tree) - 1 &&
7248 first + s->selected < g &&
7249 s->selected < s->ndisplayed - 1) {
7250 first = got_tree_entry_get_index(*fte);
7251 first += off;
7252 s->selected = g - first;
7254 } else if (i > g)
7255 tree_scroll_up(s, i - g);
7257 if (g < nlines &&
7258 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7259 s->selected = g - 1;
7261 return NULL;
7264 static const struct got_error *
7265 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7267 const struct got_error *err = NULL;
7268 struct tog_tree_view_state *s = &view->state.tree;
7269 struct got_tree_entry *te;
7270 int n, nscroll = view->nlines - 3;
7272 if (view->gline)
7273 return tree_goto_line(view, nscroll);
7275 switch (ch) {
7276 case 'i':
7277 s->show_ids = !s->show_ids;
7278 view->count = 0;
7279 break;
7280 case 'L':
7281 view->count = 0;
7282 if (!s->selected_entry)
7283 break;
7284 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7285 break;
7286 case 'R':
7287 view->count = 0;
7288 err = view_request_new(new_view, view, TOG_VIEW_REF);
7289 break;
7290 case 'g':
7291 case KEY_HOME:
7292 s->selected = 0;
7293 view->count = 0;
7294 if (s->tree == s->root)
7295 s->first_displayed_entry =
7296 got_object_tree_get_first_entry(s->tree);
7297 else
7298 s->first_displayed_entry = NULL;
7299 break;
7300 case 'G':
7301 case KEY_END: {
7302 int eos = view->nlines - 3;
7304 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7305 --eos; /* border */
7306 s->selected = 0;
7307 view->count = 0;
7308 te = got_object_tree_get_last_entry(s->tree);
7309 for (n = 0; n < eos; n++) {
7310 if (te == NULL) {
7311 if (s->tree != s->root) {
7312 s->first_displayed_entry = NULL;
7313 n++;
7315 break;
7317 s->first_displayed_entry = te;
7318 te = got_tree_entry_get_prev(s->tree, te);
7320 if (n > 0)
7321 s->selected = n - 1;
7322 break;
7324 case 'k':
7325 case KEY_UP:
7326 case CTRL('p'):
7327 if (s->selected > 0) {
7328 s->selected--;
7329 break;
7331 tree_scroll_up(s, 1);
7332 if (s->selected_entry == NULL ||
7333 (s->tree == s->root && s->selected_entry ==
7334 got_object_tree_get_first_entry(s->tree)))
7335 view->count = 0;
7336 break;
7337 case CTRL('u'):
7338 case 'u':
7339 nscroll /= 2;
7340 /* FALL THROUGH */
7341 case KEY_PPAGE:
7342 case CTRL('b'):
7343 case 'b':
7344 if (s->tree == s->root) {
7345 if (got_object_tree_get_first_entry(s->tree) ==
7346 s->first_displayed_entry)
7347 s->selected -= MIN(s->selected, nscroll);
7348 } else {
7349 if (s->first_displayed_entry == NULL)
7350 s->selected -= MIN(s->selected, nscroll);
7352 tree_scroll_up(s, MAX(0, nscroll));
7353 if (s->selected_entry == NULL ||
7354 (s->tree == s->root && s->selected_entry ==
7355 got_object_tree_get_first_entry(s->tree)))
7356 view->count = 0;
7357 break;
7358 case 'j':
7359 case KEY_DOWN:
7360 case CTRL('n'):
7361 if (s->selected < s->ndisplayed - 1) {
7362 s->selected++;
7363 break;
7365 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7366 == NULL) {
7367 /* can't scroll any further */
7368 view->count = 0;
7369 break;
7371 tree_scroll_down(view, 1);
7372 break;
7373 case CTRL('d'):
7374 case 'd':
7375 nscroll /= 2;
7376 /* FALL THROUGH */
7377 case KEY_NPAGE:
7378 case CTRL('f'):
7379 case 'f':
7380 case ' ':
7381 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7382 == NULL) {
7383 /* can't scroll any further; move cursor down */
7384 if (s->selected < s->ndisplayed - 1)
7385 s->selected += MIN(nscroll,
7386 s->ndisplayed - s->selected - 1);
7387 else
7388 view->count = 0;
7389 break;
7391 tree_scroll_down(view, nscroll);
7392 break;
7393 case KEY_ENTER:
7394 case '\r':
7395 case KEY_BACKSPACE:
7396 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7397 struct tog_parent_tree *parent;
7398 /* user selected '..' */
7399 if (s->tree == s->root) {
7400 view->count = 0;
7401 break;
7403 parent = TAILQ_FIRST(&s->parents);
7404 TAILQ_REMOVE(&s->parents, parent,
7405 entry);
7406 got_object_tree_close(s->tree);
7407 s->tree = parent->tree;
7408 s->first_displayed_entry =
7409 parent->first_displayed_entry;
7410 s->selected_entry =
7411 parent->selected_entry;
7412 s->selected = parent->selected;
7413 if (s->selected > view->nlines - 3) {
7414 err = offset_selection_down(view);
7415 if (err)
7416 break;
7418 free(parent);
7419 } else if (S_ISDIR(got_tree_entry_get_mode(
7420 s->selected_entry))) {
7421 struct got_tree_object *subtree;
7422 view->count = 0;
7423 err = got_object_open_as_tree(&subtree, s->repo,
7424 got_tree_entry_get_id(s->selected_entry));
7425 if (err)
7426 break;
7427 err = tree_view_visit_subtree(s, subtree);
7428 if (err) {
7429 got_object_tree_close(subtree);
7430 break;
7432 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7433 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7434 break;
7435 case KEY_RESIZE:
7436 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7437 s->selected = view->nlines - 4;
7438 view->count = 0;
7439 break;
7440 default:
7441 view->count = 0;
7442 break;
7445 return err;
7448 __dead static void
7449 usage_tree(void)
7451 endwin();
7452 fprintf(stderr,
7453 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7454 getprogname());
7455 exit(1);
7458 static const struct got_error *
7459 cmd_tree(int argc, char *argv[])
7461 const struct got_error *error;
7462 struct got_repository *repo = NULL;
7463 struct got_worktree *worktree = NULL;
7464 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7465 struct got_object_id *commit_id = NULL;
7466 struct got_commit_object *commit = NULL;
7467 const char *commit_id_arg = NULL;
7468 char *label = NULL;
7469 struct got_reference *ref = NULL;
7470 const char *head_ref_name = NULL;
7471 int ch;
7472 struct tog_view *view;
7473 int *pack_fds = NULL;
7475 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7476 switch (ch) {
7477 case 'c':
7478 commit_id_arg = optarg;
7479 break;
7480 case 'r':
7481 repo_path = realpath(optarg, NULL);
7482 if (repo_path == NULL)
7483 return got_error_from_errno2("realpath",
7484 optarg);
7485 break;
7486 default:
7487 usage_tree();
7488 /* NOTREACHED */
7492 argc -= optind;
7493 argv += optind;
7495 if (argc > 1)
7496 usage_tree();
7498 error = got_repo_pack_fds_open(&pack_fds);
7499 if (error != NULL)
7500 goto done;
7502 if (repo_path == NULL) {
7503 cwd = getcwd(NULL, 0);
7504 if (cwd == NULL)
7505 return got_error_from_errno("getcwd");
7506 error = got_worktree_open(&worktree, cwd);
7507 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7508 goto done;
7509 if (worktree)
7510 repo_path =
7511 strdup(got_worktree_get_repo_path(worktree));
7512 else
7513 repo_path = strdup(cwd);
7514 if (repo_path == NULL) {
7515 error = got_error_from_errno("strdup");
7516 goto done;
7520 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7521 if (error != NULL)
7522 goto done;
7524 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7525 repo, worktree);
7526 if (error)
7527 goto done;
7529 init_curses();
7531 error = apply_unveil(got_repo_get_path(repo), NULL);
7532 if (error)
7533 goto done;
7535 error = tog_load_refs(repo, 0);
7536 if (error)
7537 goto done;
7539 if (commit_id_arg == NULL) {
7540 error = got_repo_match_object_id(&commit_id, &label,
7541 worktree ? got_worktree_get_head_ref_name(worktree) :
7542 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7543 if (error)
7544 goto done;
7545 head_ref_name = label;
7546 } else {
7547 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7548 if (error == NULL)
7549 head_ref_name = got_ref_get_name(ref);
7550 else if (error->code != GOT_ERR_NOT_REF)
7551 goto done;
7552 error = got_repo_match_object_id(&commit_id, NULL,
7553 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7554 if (error)
7555 goto done;
7558 error = got_object_open_as_commit(&commit, repo, commit_id);
7559 if (error)
7560 goto done;
7562 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7563 if (view == NULL) {
7564 error = got_error_from_errno("view_open");
7565 goto done;
7567 error = open_tree_view(view, commit_id, head_ref_name, repo);
7568 if (error)
7569 goto done;
7570 if (!got_path_is_root_dir(in_repo_path)) {
7571 error = tree_view_walk_path(&view->state.tree, commit,
7572 in_repo_path);
7573 if (error)
7574 goto done;
7577 if (worktree) {
7578 /* Release work tree lock. */
7579 got_worktree_close(worktree);
7580 worktree = NULL;
7582 error = view_loop(view);
7583 done:
7584 free(repo_path);
7585 free(cwd);
7586 free(commit_id);
7587 free(label);
7588 if (ref)
7589 got_ref_close(ref);
7590 if (repo) {
7591 const struct got_error *close_err = got_repo_close(repo);
7592 if (error == NULL)
7593 error = close_err;
7595 if (pack_fds) {
7596 const struct got_error *pack_err =
7597 got_repo_pack_fds_close(pack_fds);
7598 if (error == NULL)
7599 error = pack_err;
7601 tog_free_refs();
7602 return error;
7605 static const struct got_error *
7606 ref_view_load_refs(struct tog_ref_view_state *s)
7608 struct got_reflist_entry *sre;
7609 struct tog_reflist_entry *re;
7611 s->nrefs = 0;
7612 TAILQ_FOREACH(sre, &tog_refs, entry) {
7613 if (strncmp(got_ref_get_name(sre->ref),
7614 "refs/got/", 9) == 0 &&
7615 strncmp(got_ref_get_name(sre->ref),
7616 "refs/got/backup/", 16) != 0)
7617 continue;
7619 re = malloc(sizeof(*re));
7620 if (re == NULL)
7621 return got_error_from_errno("malloc");
7623 re->ref = got_ref_dup(sre->ref);
7624 if (re->ref == NULL)
7625 return got_error_from_errno("got_ref_dup");
7626 re->idx = s->nrefs++;
7627 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7630 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7631 return NULL;
7634 static void
7635 ref_view_free_refs(struct tog_ref_view_state *s)
7637 struct tog_reflist_entry *re;
7639 while (!TAILQ_EMPTY(&s->refs)) {
7640 re = TAILQ_FIRST(&s->refs);
7641 TAILQ_REMOVE(&s->refs, re, entry);
7642 got_ref_close(re->ref);
7643 free(re);
7647 static const struct got_error *
7648 open_ref_view(struct tog_view *view, struct got_repository *repo)
7650 const struct got_error *err = NULL;
7651 struct tog_ref_view_state *s = &view->state.ref;
7653 s->selected_entry = 0;
7654 s->repo = repo;
7656 TAILQ_INIT(&s->refs);
7657 STAILQ_INIT(&s->colors);
7659 err = ref_view_load_refs(s);
7660 if (err)
7661 return err;
7663 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7664 err = add_color(&s->colors, "^refs/heads/",
7665 TOG_COLOR_REFS_HEADS,
7666 get_color_value("TOG_COLOR_REFS_HEADS"));
7667 if (err)
7668 goto done;
7670 err = add_color(&s->colors, "^refs/tags/",
7671 TOG_COLOR_REFS_TAGS,
7672 get_color_value("TOG_COLOR_REFS_TAGS"));
7673 if (err)
7674 goto done;
7676 err = add_color(&s->colors, "^refs/remotes/",
7677 TOG_COLOR_REFS_REMOTES,
7678 get_color_value("TOG_COLOR_REFS_REMOTES"));
7679 if (err)
7680 goto done;
7682 err = add_color(&s->colors, "^refs/got/backup/",
7683 TOG_COLOR_REFS_BACKUP,
7684 get_color_value("TOG_COLOR_REFS_BACKUP"));
7685 if (err)
7686 goto done;
7689 view->show = show_ref_view;
7690 view->input = input_ref_view;
7691 view->close = close_ref_view;
7692 view->search_start = search_start_ref_view;
7693 view->search_next = search_next_ref_view;
7694 done:
7695 if (err)
7696 free_colors(&s->colors);
7697 return err;
7700 static const struct got_error *
7701 close_ref_view(struct tog_view *view)
7703 struct tog_ref_view_state *s = &view->state.ref;
7705 ref_view_free_refs(s);
7706 free_colors(&s->colors);
7708 return NULL;
7711 static const struct got_error *
7712 resolve_reflist_entry(struct got_object_id **commit_id,
7713 struct tog_reflist_entry *re, struct got_repository *repo)
7715 const struct got_error *err = NULL;
7716 struct got_object_id *obj_id;
7717 struct got_tag_object *tag = NULL;
7718 int obj_type;
7720 *commit_id = NULL;
7722 err = got_ref_resolve(&obj_id, repo, re->ref);
7723 if (err)
7724 return err;
7726 err = got_object_get_type(&obj_type, repo, obj_id);
7727 if (err)
7728 goto done;
7730 switch (obj_type) {
7731 case GOT_OBJ_TYPE_COMMIT:
7732 *commit_id = obj_id;
7733 break;
7734 case GOT_OBJ_TYPE_TAG:
7735 err = got_object_open_as_tag(&tag, repo, obj_id);
7736 if (err)
7737 goto done;
7738 free(obj_id);
7739 err = got_object_get_type(&obj_type, repo,
7740 got_object_tag_get_object_id(tag));
7741 if (err)
7742 goto done;
7743 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7744 err = got_error(GOT_ERR_OBJ_TYPE);
7745 goto done;
7747 *commit_id = got_object_id_dup(
7748 got_object_tag_get_object_id(tag));
7749 if (*commit_id == NULL) {
7750 err = got_error_from_errno("got_object_id_dup");
7751 goto done;
7753 break;
7754 default:
7755 err = got_error(GOT_ERR_OBJ_TYPE);
7756 break;
7759 done:
7760 if (tag)
7761 got_object_tag_close(tag);
7762 if (err) {
7763 free(*commit_id);
7764 *commit_id = NULL;
7766 return err;
7769 static const struct got_error *
7770 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7771 struct tog_reflist_entry *re, struct got_repository *repo)
7773 struct tog_view *log_view;
7774 const struct got_error *err = NULL;
7775 struct got_object_id *commit_id = NULL;
7777 *new_view = NULL;
7779 err = resolve_reflist_entry(&commit_id, re, repo);
7780 if (err) {
7781 if (err->code != GOT_ERR_OBJ_TYPE)
7782 return err;
7783 else
7784 return NULL;
7787 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7788 if (log_view == NULL) {
7789 err = got_error_from_errno("view_open");
7790 goto done;
7793 err = open_log_view(log_view, commit_id, repo,
7794 got_ref_get_name(re->ref), "", 0);
7795 done:
7796 if (err)
7797 view_close(log_view);
7798 else
7799 *new_view = log_view;
7800 free(commit_id);
7801 return err;
7804 static void
7805 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7807 struct tog_reflist_entry *re;
7808 int i = 0;
7810 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7811 return;
7813 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7814 while (i++ < maxscroll) {
7815 if (re == NULL)
7816 break;
7817 s->first_displayed_entry = re;
7818 re = TAILQ_PREV(re, tog_reflist_head, entry);
7822 static const struct got_error *
7823 ref_scroll_down(struct tog_view *view, int maxscroll)
7825 struct tog_ref_view_state *s = &view->state.ref;
7826 struct tog_reflist_entry *next, *last;
7827 int n = 0;
7829 if (s->first_displayed_entry)
7830 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7831 else
7832 next = TAILQ_FIRST(&s->refs);
7834 last = s->last_displayed_entry;
7835 while (next && n++ < maxscroll) {
7836 if (last) {
7837 s->last_displayed_entry = last;
7838 last = TAILQ_NEXT(last, entry);
7840 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7841 s->first_displayed_entry = next;
7842 next = TAILQ_NEXT(next, entry);
7846 return NULL;
7849 static const struct got_error *
7850 search_start_ref_view(struct tog_view *view)
7852 struct tog_ref_view_state *s = &view->state.ref;
7854 s->matched_entry = NULL;
7855 return NULL;
7858 static int
7859 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7861 regmatch_t regmatch;
7863 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7864 0) == 0;
7867 static const struct got_error *
7868 search_next_ref_view(struct tog_view *view)
7870 struct tog_ref_view_state *s = &view->state.ref;
7871 struct tog_reflist_entry *re = NULL;
7873 if (!view->searching) {
7874 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7875 return NULL;
7878 if (s->matched_entry) {
7879 if (view->searching == TOG_SEARCH_FORWARD) {
7880 if (s->selected_entry)
7881 re = TAILQ_NEXT(s->selected_entry, entry);
7882 else
7883 re = TAILQ_PREV(s->selected_entry,
7884 tog_reflist_head, entry);
7885 } else {
7886 if (s->selected_entry == NULL)
7887 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7888 else
7889 re = TAILQ_PREV(s->selected_entry,
7890 tog_reflist_head, entry);
7892 } else {
7893 if (s->selected_entry)
7894 re = s->selected_entry;
7895 else if (view->searching == TOG_SEARCH_FORWARD)
7896 re = TAILQ_FIRST(&s->refs);
7897 else
7898 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7901 while (1) {
7902 if (re == NULL) {
7903 if (s->matched_entry == NULL) {
7904 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7905 return NULL;
7907 if (view->searching == TOG_SEARCH_FORWARD)
7908 re = TAILQ_FIRST(&s->refs);
7909 else
7910 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7913 if (match_reflist_entry(re, &view->regex)) {
7914 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7915 s->matched_entry = re;
7916 break;
7919 if (view->searching == TOG_SEARCH_FORWARD)
7920 re = TAILQ_NEXT(re, entry);
7921 else
7922 re = TAILQ_PREV(re, tog_reflist_head, entry);
7925 if (s->matched_entry) {
7926 s->first_displayed_entry = s->matched_entry;
7927 s->selected = 0;
7930 return NULL;
7933 static const struct got_error *
7934 show_ref_view(struct tog_view *view)
7936 const struct got_error *err = NULL;
7937 struct tog_ref_view_state *s = &view->state.ref;
7938 struct tog_reflist_entry *re;
7939 char *line = NULL;
7940 wchar_t *wline;
7941 struct tog_color *tc;
7942 int width, n;
7943 int limit = view->nlines;
7945 werase(view->window);
7947 s->ndisplayed = 0;
7948 if (view_is_hsplit_top(view))
7949 --limit; /* border */
7951 if (limit == 0)
7952 return NULL;
7954 re = s->first_displayed_entry;
7956 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7957 s->nrefs) == -1)
7958 return got_error_from_errno("asprintf");
7960 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7961 if (err) {
7962 free(line);
7963 return err;
7965 if (view_needs_focus_indication(view))
7966 wstandout(view->window);
7967 waddwstr(view->window, wline);
7968 while (width++ < view->ncols)
7969 waddch(view->window, ' ');
7970 if (view_needs_focus_indication(view))
7971 wstandend(view->window);
7972 free(wline);
7973 wline = NULL;
7974 free(line);
7975 line = NULL;
7976 if (--limit <= 0)
7977 return NULL;
7979 n = 0;
7980 while (re && limit > 0) {
7981 char *line = NULL;
7982 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7984 if (s->show_date) {
7985 struct got_commit_object *ci;
7986 struct got_tag_object *tag;
7987 struct got_object_id *id;
7988 struct tm tm;
7989 time_t t;
7991 err = got_ref_resolve(&id, s->repo, re->ref);
7992 if (err)
7993 return err;
7994 err = got_object_open_as_tag(&tag, s->repo, id);
7995 if (err) {
7996 if (err->code != GOT_ERR_OBJ_TYPE) {
7997 free(id);
7998 return err;
8000 err = got_object_open_as_commit(&ci, s->repo,
8001 id);
8002 if (err) {
8003 free(id);
8004 return err;
8006 t = got_object_commit_get_committer_time(ci);
8007 got_object_commit_close(ci);
8008 } else {
8009 t = got_object_tag_get_tagger_time(tag);
8010 got_object_tag_close(tag);
8012 free(id);
8013 if (gmtime_r(&t, &tm) == NULL)
8014 return got_error_from_errno("gmtime_r");
8015 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8016 return got_error(GOT_ERR_NO_SPACE);
8018 if (got_ref_is_symbolic(re->ref)) {
8019 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8020 ymd : "", got_ref_get_name(re->ref),
8021 got_ref_get_symref_target(re->ref)) == -1)
8022 return got_error_from_errno("asprintf");
8023 } else if (s->show_ids) {
8024 struct got_object_id *id;
8025 char *id_str;
8026 err = got_ref_resolve(&id, s->repo, re->ref);
8027 if (err)
8028 return err;
8029 err = got_object_id_str(&id_str, id);
8030 if (err) {
8031 free(id);
8032 return err;
8034 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8035 got_ref_get_name(re->ref), id_str) == -1) {
8036 err = got_error_from_errno("asprintf");
8037 free(id);
8038 free(id_str);
8039 return err;
8041 free(id);
8042 free(id_str);
8043 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8044 got_ref_get_name(re->ref)) == -1)
8045 return got_error_from_errno("asprintf");
8047 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8048 0, 0);
8049 if (err) {
8050 free(line);
8051 return err;
8053 if (n == s->selected) {
8054 if (view->focussed)
8055 wstandout(view->window);
8056 s->selected_entry = re;
8058 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8059 if (tc)
8060 wattr_on(view->window,
8061 COLOR_PAIR(tc->colorpair), NULL);
8062 waddwstr(view->window, wline);
8063 if (tc)
8064 wattr_off(view->window,
8065 COLOR_PAIR(tc->colorpair), NULL);
8066 if (width < view->ncols - 1)
8067 waddch(view->window, '\n');
8068 if (n == s->selected && view->focussed)
8069 wstandend(view->window);
8070 free(line);
8071 free(wline);
8072 wline = NULL;
8073 n++;
8074 s->ndisplayed++;
8075 s->last_displayed_entry = re;
8077 limit--;
8078 re = TAILQ_NEXT(re, entry);
8081 view_border(view);
8082 return err;
8085 static const struct got_error *
8086 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8087 struct tog_reflist_entry *re, struct got_repository *repo)
8089 const struct got_error *err = NULL;
8090 struct got_object_id *commit_id = NULL;
8091 struct tog_view *tree_view;
8093 *new_view = NULL;
8095 err = resolve_reflist_entry(&commit_id, re, repo);
8096 if (err) {
8097 if (err->code != GOT_ERR_OBJ_TYPE)
8098 return err;
8099 else
8100 return NULL;
8104 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8105 if (tree_view == NULL) {
8106 err = got_error_from_errno("view_open");
8107 goto done;
8110 err = open_tree_view(tree_view, commit_id,
8111 got_ref_get_name(re->ref), repo);
8112 if (err)
8113 goto done;
8115 *new_view = tree_view;
8116 done:
8117 free(commit_id);
8118 return err;
8121 static const struct got_error *
8122 ref_goto_line(struct tog_view *view, int nlines)
8124 const struct got_error *err = NULL;
8125 struct tog_ref_view_state *s = &view->state.ref;
8126 int g, idx = s->selected_entry->idx;
8128 g = view->gline;
8129 view->gline = 0;
8131 if (g == 0)
8132 g = 1;
8133 else if (g > s->nrefs)
8134 g = s->nrefs;
8136 if (g >= s->first_displayed_entry->idx + 1 &&
8137 g <= s->last_displayed_entry->idx + 1 &&
8138 g - s->first_displayed_entry->idx - 1 < nlines) {
8139 s->selected = g - s->first_displayed_entry->idx - 1;
8140 return NULL;
8143 if (idx + 1 < g) {
8144 err = ref_scroll_down(view, g - idx - 1);
8145 if (err)
8146 return err;
8147 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8148 s->first_displayed_entry->idx + s->selected < g &&
8149 s->selected < s->ndisplayed - 1)
8150 s->selected = g - s->first_displayed_entry->idx - 1;
8151 } else if (idx + 1 > g)
8152 ref_scroll_up(s, idx - g + 1);
8154 if (g < nlines && s->first_displayed_entry->idx == 0)
8155 s->selected = g - 1;
8157 return NULL;
8161 static const struct got_error *
8162 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8164 const struct got_error *err = NULL;
8165 struct tog_ref_view_state *s = &view->state.ref;
8166 struct tog_reflist_entry *re;
8167 int n, nscroll = view->nlines - 1;
8169 if (view->gline)
8170 return ref_goto_line(view, nscroll);
8172 switch (ch) {
8173 case 'i':
8174 s->show_ids = !s->show_ids;
8175 view->count = 0;
8176 break;
8177 case 'm':
8178 s->show_date = !s->show_date;
8179 view->count = 0;
8180 break;
8181 case 'o':
8182 s->sort_by_date = !s->sort_by_date;
8183 view->count = 0;
8184 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8185 got_ref_cmp_by_commit_timestamp_descending :
8186 tog_ref_cmp_by_name, s->repo);
8187 if (err)
8188 break;
8189 got_reflist_object_id_map_free(tog_refs_idmap);
8190 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8191 &tog_refs, s->repo);
8192 if (err)
8193 break;
8194 ref_view_free_refs(s);
8195 err = ref_view_load_refs(s);
8196 break;
8197 case KEY_ENTER:
8198 case '\r':
8199 view->count = 0;
8200 if (!s->selected_entry)
8201 break;
8202 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8203 break;
8204 case 'T':
8205 view->count = 0;
8206 if (!s->selected_entry)
8207 break;
8208 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8209 break;
8210 case 'g':
8211 case KEY_HOME:
8212 s->selected = 0;
8213 view->count = 0;
8214 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8215 break;
8216 case 'G':
8217 case KEY_END: {
8218 int eos = view->nlines - 1;
8220 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8221 --eos; /* border */
8222 s->selected = 0;
8223 view->count = 0;
8224 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8225 for (n = 0; n < eos; n++) {
8226 if (re == NULL)
8227 break;
8228 s->first_displayed_entry = re;
8229 re = TAILQ_PREV(re, tog_reflist_head, entry);
8231 if (n > 0)
8232 s->selected = n - 1;
8233 break;
8235 case 'k':
8236 case KEY_UP:
8237 case CTRL('p'):
8238 if (s->selected > 0) {
8239 s->selected--;
8240 break;
8242 ref_scroll_up(s, 1);
8243 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8244 view->count = 0;
8245 break;
8246 case CTRL('u'):
8247 case 'u':
8248 nscroll /= 2;
8249 /* FALL THROUGH */
8250 case KEY_PPAGE:
8251 case CTRL('b'):
8252 case 'b':
8253 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8254 s->selected -= MIN(nscroll, s->selected);
8255 ref_scroll_up(s, MAX(0, nscroll));
8256 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8257 view->count = 0;
8258 break;
8259 case 'j':
8260 case KEY_DOWN:
8261 case CTRL('n'):
8262 if (s->selected < s->ndisplayed - 1) {
8263 s->selected++;
8264 break;
8266 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8267 /* can't scroll any further */
8268 view->count = 0;
8269 break;
8271 ref_scroll_down(view, 1);
8272 break;
8273 case CTRL('d'):
8274 case 'd':
8275 nscroll /= 2;
8276 /* FALL THROUGH */
8277 case KEY_NPAGE:
8278 case CTRL('f'):
8279 case 'f':
8280 case ' ':
8281 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8282 /* can't scroll any further; move cursor down */
8283 if (s->selected < s->ndisplayed - 1)
8284 s->selected += MIN(nscroll,
8285 s->ndisplayed - s->selected - 1);
8286 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8287 s->selected += s->ndisplayed - s->selected - 1;
8288 view->count = 0;
8289 break;
8291 ref_scroll_down(view, nscroll);
8292 break;
8293 case CTRL('l'):
8294 view->count = 0;
8295 tog_free_refs();
8296 err = tog_load_refs(s->repo, s->sort_by_date);
8297 if (err)
8298 break;
8299 ref_view_free_refs(s);
8300 err = ref_view_load_refs(s);
8301 break;
8302 case KEY_RESIZE:
8303 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8304 s->selected = view->nlines - 2;
8305 break;
8306 default:
8307 view->count = 0;
8308 break;
8311 return err;
8314 __dead static void
8315 usage_ref(void)
8317 endwin();
8318 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8319 getprogname());
8320 exit(1);
8323 static const struct got_error *
8324 cmd_ref(int argc, char *argv[])
8326 const struct got_error *error;
8327 struct got_repository *repo = NULL;
8328 struct got_worktree *worktree = NULL;
8329 char *cwd = NULL, *repo_path = NULL;
8330 int ch;
8331 struct tog_view *view;
8332 int *pack_fds = NULL;
8334 while ((ch = getopt(argc, argv, "r:")) != -1) {
8335 switch (ch) {
8336 case 'r':
8337 repo_path = realpath(optarg, NULL);
8338 if (repo_path == NULL)
8339 return got_error_from_errno2("realpath",
8340 optarg);
8341 break;
8342 default:
8343 usage_ref();
8344 /* NOTREACHED */
8348 argc -= optind;
8349 argv += optind;
8351 if (argc > 1)
8352 usage_ref();
8354 error = got_repo_pack_fds_open(&pack_fds);
8355 if (error != NULL)
8356 goto done;
8358 if (repo_path == NULL) {
8359 cwd = getcwd(NULL, 0);
8360 if (cwd == NULL)
8361 return got_error_from_errno("getcwd");
8362 error = got_worktree_open(&worktree, cwd);
8363 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8364 goto done;
8365 if (worktree)
8366 repo_path =
8367 strdup(got_worktree_get_repo_path(worktree));
8368 else
8369 repo_path = strdup(cwd);
8370 if (repo_path == NULL) {
8371 error = got_error_from_errno("strdup");
8372 goto done;
8376 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8377 if (error != NULL)
8378 goto done;
8380 init_curses();
8382 error = apply_unveil(got_repo_get_path(repo), NULL);
8383 if (error)
8384 goto done;
8386 error = tog_load_refs(repo, 0);
8387 if (error)
8388 goto done;
8390 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8391 if (view == NULL) {
8392 error = got_error_from_errno("view_open");
8393 goto done;
8396 error = open_ref_view(view, repo);
8397 if (error)
8398 goto done;
8400 if (worktree) {
8401 /* Release work tree lock. */
8402 got_worktree_close(worktree);
8403 worktree = NULL;
8405 error = view_loop(view);
8406 done:
8407 free(repo_path);
8408 free(cwd);
8409 if (repo) {
8410 const struct got_error *close_err = got_repo_close(repo);
8411 if (close_err)
8412 error = close_err;
8414 if (pack_fds) {
8415 const struct got_error *pack_err =
8416 got_repo_pack_fds_close(pack_fds);
8417 if (error == NULL)
8418 error = pack_err;
8420 tog_free_refs();
8421 return error;
8424 static const struct got_error*
8425 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8426 const char *str)
8428 size_t len;
8430 if (win == NULL)
8431 win = stdscr;
8433 len = strlen(str);
8434 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8436 if (focus)
8437 wstandout(win);
8438 if (mvwprintw(win, y, x, "%s", str) == ERR)
8439 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8440 if (focus)
8441 wstandend(win);
8443 return NULL;
8446 static const struct got_error *
8447 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8449 off_t *p;
8451 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8452 if (p == NULL) {
8453 free(*line_offsets);
8454 *line_offsets = NULL;
8455 return got_error_from_errno("reallocarray");
8458 *line_offsets = p;
8459 (*line_offsets)[*nlines] = off;
8460 ++(*nlines);
8461 return NULL;
8464 static const struct got_error *
8465 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8467 *ret = 0;
8469 for (;n > 0; --n, ++km) {
8470 char *t0, *t, *k;
8471 size_t len = 1;
8473 if (km->keys == NULL)
8474 continue;
8476 t = t0 = strdup(km->keys);
8477 if (t0 == NULL)
8478 return got_error_from_errno("strdup");
8480 len += strlen(t);
8481 while ((k = strsep(&t, " ")) != NULL)
8482 len += strlen(k) > 1 ? 2 : 0;
8483 free(t0);
8484 *ret = MAX(*ret, len);
8487 return NULL;
8491 * Write keymap section headers, keys, and key info in km to f.
8492 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8493 * wrap control and symbolic keys in guillemets, else use <>.
8495 static const struct got_error *
8496 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8498 int n, len = width;
8500 if (km->keys) {
8501 static const char *u8_glyph[] = {
8502 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8503 "\xe2\x80\xba" /* U+203A (utf8 >) */
8505 char *t0, *t, *k;
8506 int cs, s, first = 1;
8508 cs = got_locale_is_utf8();
8510 t = t0 = strdup(km->keys);
8511 if (t0 == NULL)
8512 return got_error_from_errno("strdup");
8514 len = strlen(km->keys);
8515 while ((k = strsep(&t, " ")) != NULL) {
8516 s = strlen(k) > 1; /* control or symbolic key */
8517 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8518 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8519 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8520 if (n < 0) {
8521 free(t0);
8522 return got_error_from_errno("fprintf");
8524 first = 0;
8525 len += s ? 2 : 0;
8526 *off += n;
8528 free(t0);
8530 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8531 if (n < 0)
8532 return got_error_from_errno("fprintf");
8533 *off += n;
8535 return NULL;
8538 static const struct got_error *
8539 format_help(struct tog_help_view_state *s)
8541 const struct got_error *err = NULL;
8542 off_t off = 0;
8543 int i, max, n, show = s->all;
8544 static const struct tog_key_map km[] = {
8545 #define KEYMAP_(info, type) { NULL, (info), type }
8546 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8547 GENERATE_HELP
8548 #undef KEYMAP_
8549 #undef KEY_
8552 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8553 if (err)
8554 return err;
8556 n = nitems(km);
8557 err = max_key_str(&max, km, n);
8558 if (err)
8559 return err;
8561 for (i = 0; i < n; ++i) {
8562 if (km[i].keys == NULL) {
8563 show = s->all;
8564 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8565 km[i].type == s->type || s->all)
8566 show = 1;
8568 if (show) {
8569 err = format_help_line(&off, s->f, &km[i], max);
8570 if (err)
8571 return err;
8572 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8573 if (err)
8574 return err;
8577 fputc('\n', s->f);
8578 ++off;
8579 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8580 return err;
8583 static const struct got_error *
8584 create_help(struct tog_help_view_state *s)
8586 FILE *f;
8587 const struct got_error *err;
8589 free(s->line_offsets);
8590 s->line_offsets = NULL;
8591 s->nlines = 0;
8593 f = got_opentemp();
8594 if (f == NULL)
8595 return got_error_from_errno("got_opentemp");
8596 s->f = f;
8598 err = format_help(s);
8599 if (err)
8600 return err;
8602 if (s->f && fflush(s->f) != 0)
8603 return got_error_from_errno("fflush");
8605 return NULL;
8608 static const struct got_error *
8609 search_start_help_view(struct tog_view *view)
8611 view->state.help.matched_line = 0;
8612 return NULL;
8615 static void
8616 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8617 size_t *nlines, int **first, int **last, int **match, int **selected)
8619 struct tog_help_view_state *s = &view->state.help;
8621 *f = s->f;
8622 *nlines = s->nlines;
8623 *line_offsets = s->line_offsets;
8624 *match = &s->matched_line;
8625 *first = &s->first_displayed_line;
8626 *last = &s->last_displayed_line;
8627 *selected = &s->selected_line;
8630 static const struct got_error *
8631 show_help_view(struct tog_view *view)
8633 struct tog_help_view_state *s = &view->state.help;
8634 const struct got_error *err;
8635 regmatch_t *regmatch = &view->regmatch;
8636 wchar_t *wline;
8637 char *line;
8638 ssize_t linelen;
8639 size_t linesz = 0;
8640 int width, nprinted = 0, rc = 0;
8641 int eos = view->nlines;
8643 if (view_is_hsplit_top(view))
8644 --eos; /* account for border */
8646 s->lineno = 0;
8647 rewind(s->f);
8648 werase(view->window);
8650 if (view->gline > s->nlines - 1)
8651 view->gline = s->nlines - 1;
8653 err = win_draw_center(view->window, 0, 0, view->ncols,
8654 view_needs_focus_indication(view),
8655 "tog help (press q to return to tog)");
8656 if (err)
8657 return err;
8658 if (eos <= 1)
8659 return NULL;
8660 waddstr(view->window, "\n\n");
8661 eos -= 2;
8663 s->eof = 0;
8664 view->maxx = 0;
8665 line = NULL;
8666 while (eos > 0 && nprinted < eos) {
8667 attr_t attr = 0;
8669 linelen = getline(&line, &linesz, s->f);
8670 if (linelen == -1) {
8671 if (!feof(s->f)) {
8672 free(line);
8673 return got_ferror(s->f, GOT_ERR_IO);
8675 s->eof = 1;
8676 break;
8678 if (++s->lineno < s->first_displayed_line)
8679 continue;
8680 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8681 continue;
8682 if (s->lineno == view->hiline)
8683 attr = A_STANDOUT;
8685 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8686 view->x ? 1 : 0);
8687 if (err) {
8688 free(line);
8689 return err;
8691 view->maxx = MAX(view->maxx, width);
8692 free(wline);
8693 wline = NULL;
8695 if (attr)
8696 wattron(view->window, attr);
8697 if (s->first_displayed_line + nprinted == s->matched_line &&
8698 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8699 err = add_matched_line(&width, line, view->ncols - 1, 0,
8700 view->window, view->x, regmatch);
8701 if (err) {
8702 free(line);
8703 return err;
8705 } else {
8706 int skip;
8708 err = format_line(&wline, &width, &skip, line,
8709 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8710 if (err) {
8711 free(line);
8712 return err;
8714 rc = waddwstr(view->window, &wline[skip]);
8715 free(wline);
8716 wline = NULL;
8717 if (rc == ERR)
8718 return got_error_msg(GOT_ERR_IO, "waddwstr");
8720 if (s->lineno == view->hiline) {
8721 while (width++ < view->ncols)
8722 waddch(view->window, ' ');
8723 } else {
8724 if (width <= view->ncols)
8725 waddch(view->window, '\n');
8727 if (attr)
8728 wattroff(view->window, attr);
8729 if (++nprinted == 1)
8730 s->first_displayed_line = s->lineno;
8732 free(line);
8733 if (nprinted > 0)
8734 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8735 else
8736 s->last_displayed_line = s->first_displayed_line;
8738 view_border(view);
8740 if (s->eof) {
8741 rc = waddnstr(view->window,
8742 "See the tog(1) manual page for full documentation",
8743 view->ncols - 1);
8744 if (rc == ERR)
8745 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8746 } else {
8747 wmove(view->window, view->nlines - 1, 0);
8748 wclrtoeol(view->window);
8749 wstandout(view->window);
8750 rc = waddnstr(view->window, "scroll down for more...",
8751 view->ncols - 1);
8752 if (rc == ERR)
8753 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8754 if (getcurx(view->window) < view->ncols - 6) {
8755 rc = wprintw(view->window, "[%.0f%%]",
8756 100.00 * s->last_displayed_line / s->nlines);
8757 if (rc == ERR)
8758 return got_error_msg(GOT_ERR_IO, "wprintw");
8760 wstandend(view->window);
8763 return NULL;
8766 static const struct got_error *
8767 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8769 struct tog_help_view_state *s = &view->state.help;
8770 const struct got_error *err = NULL;
8771 char *line = NULL;
8772 ssize_t linelen;
8773 size_t linesz = 0;
8774 int eos, nscroll;
8776 eos = nscroll = view->nlines;
8777 if (view_is_hsplit_top(view))
8778 --eos; /* border */
8780 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8782 switch (ch) {
8783 case '0':
8784 view->x = 0;
8785 break;
8786 case '$':
8787 view->x = MAX(view->maxx - view->ncols / 3, 0);
8788 view->count = 0;
8789 break;
8790 case KEY_RIGHT:
8791 case 'l':
8792 if (view->x + view->ncols / 3 < view->maxx)
8793 view->x += 2;
8794 else
8795 view->count = 0;
8796 break;
8797 case KEY_LEFT:
8798 case 'h':
8799 view->x -= MIN(view->x, 2);
8800 if (view->x <= 0)
8801 view->count = 0;
8802 break;
8803 case 'g':
8804 case KEY_HOME:
8805 s->first_displayed_line = 1;
8806 view->count = 0;
8807 break;
8808 case 'G':
8809 case KEY_END:
8810 view->count = 0;
8811 if (s->eof)
8812 break;
8813 s->first_displayed_line = (s->nlines - eos) + 3;
8814 s->eof = 1;
8815 break;
8816 case 'k':
8817 case KEY_UP:
8818 if (s->first_displayed_line > 1)
8819 --s->first_displayed_line;
8820 else
8821 view->count = 0;
8822 break;
8823 case CTRL('u'):
8824 case 'u':
8825 nscroll /= 2;
8826 /* FALL THROUGH */
8827 case KEY_PPAGE:
8828 case CTRL('b'):
8829 case 'b':
8830 if (s->first_displayed_line == 1) {
8831 view->count = 0;
8832 break;
8834 while (--nscroll > 0 && s->first_displayed_line > 1)
8835 s->first_displayed_line--;
8836 break;
8837 case 'j':
8838 case KEY_DOWN:
8839 case CTRL('n'):
8840 if (!s->eof)
8841 ++s->first_displayed_line;
8842 else
8843 view->count = 0;
8844 break;
8845 case CTRL('d'):
8846 case 'd':
8847 nscroll /= 2;
8848 /* FALL THROUGH */
8849 case KEY_NPAGE:
8850 case CTRL('f'):
8851 case 'f':
8852 case ' ':
8853 if (s->eof) {
8854 view->count = 0;
8855 break;
8857 while (!s->eof && --nscroll > 0) {
8858 linelen = getline(&line, &linesz, s->f);
8859 s->first_displayed_line++;
8860 if (linelen == -1) {
8861 if (feof(s->f))
8862 s->eof = 1;
8863 else
8864 err = got_ferror(s->f, GOT_ERR_IO);
8865 break;
8868 free(line);
8869 break;
8870 default:
8871 view->count = 0;
8872 break;
8875 return err;
8878 static const struct got_error *
8879 close_help_view(struct tog_view *view)
8881 struct tog_help_view_state *s = &view->state.help;
8883 free(s->line_offsets);
8884 s->line_offsets = NULL;
8885 if (fclose(s->f) == EOF)
8886 return got_error_from_errno("fclose");
8888 return NULL;
8891 static const struct got_error *
8892 reset_help_view(struct tog_view *view)
8894 struct tog_help_view_state *s = &view->state.help;
8897 if (s->f && fclose(s->f) == EOF)
8898 return got_error_from_errno("fclose");
8900 wclear(view->window);
8901 view->count = 0;
8902 view->x = 0;
8903 s->all = !s->all;
8904 s->first_displayed_line = 1;
8905 s->last_displayed_line = view->nlines;
8906 s->matched_line = 0;
8908 return create_help(s);
8911 static const struct got_error *
8912 open_help_view(struct tog_view *view, struct tog_view *parent)
8914 const struct got_error *err = NULL;
8915 struct tog_help_view_state *s = &view->state.help;
8917 s->type = (enum tog_keymap_type)parent->type;
8918 s->first_displayed_line = 1;
8919 s->last_displayed_line = view->nlines;
8920 s->selected_line = 1;
8922 view->show = show_help_view;
8923 view->input = input_help_view;
8924 view->reset = reset_help_view;
8925 view->close = close_help_view;
8926 view->search_start = search_start_help_view;
8927 view->search_setup = search_setup_help_view;
8928 view->search_next = search_next_view_match;
8930 err = create_help(s);
8931 return err;
8934 static const struct got_error *
8935 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8936 enum tog_view_type request, int y, int x)
8938 const struct got_error *err = NULL;
8940 *new_view = NULL;
8942 switch (request) {
8943 case TOG_VIEW_DIFF:
8944 if (view->type == TOG_VIEW_LOG) {
8945 struct tog_log_view_state *s = &view->state.log;
8947 err = open_diff_view_for_commit(new_view, y, x,
8948 s->selected_entry->commit, s->selected_entry->id,
8949 view, s->repo);
8950 } else
8951 return got_error_msg(GOT_ERR_NOT_IMPL,
8952 "parent/child view pair not supported");
8953 break;
8954 case TOG_VIEW_BLAME:
8955 if (view->type == TOG_VIEW_TREE) {
8956 struct tog_tree_view_state *s = &view->state.tree;
8958 err = blame_tree_entry(new_view, y, x,
8959 s->selected_entry, &s->parents, s->commit_id,
8960 s->repo);
8961 } else
8962 return got_error_msg(GOT_ERR_NOT_IMPL,
8963 "parent/child view pair not supported");
8964 break;
8965 case TOG_VIEW_LOG:
8966 if (view->type == TOG_VIEW_BLAME)
8967 err = log_annotated_line(new_view, y, x,
8968 view->state.blame.repo, view->state.blame.id_to_log);
8969 else if (view->type == TOG_VIEW_TREE)
8970 err = log_selected_tree_entry(new_view, y, x,
8971 &view->state.tree);
8972 else if (view->type == TOG_VIEW_REF)
8973 err = log_ref_entry(new_view, y, x,
8974 view->state.ref.selected_entry,
8975 view->state.ref.repo);
8976 else
8977 return got_error_msg(GOT_ERR_NOT_IMPL,
8978 "parent/child view pair not supported");
8979 break;
8980 case TOG_VIEW_TREE:
8981 if (view->type == TOG_VIEW_LOG)
8982 err = browse_commit_tree(new_view, y, x,
8983 view->state.log.selected_entry,
8984 view->state.log.in_repo_path,
8985 view->state.log.head_ref_name,
8986 view->state.log.repo);
8987 else if (view->type == TOG_VIEW_REF)
8988 err = browse_ref_tree(new_view, y, x,
8989 view->state.ref.selected_entry,
8990 view->state.ref.repo);
8991 else
8992 return got_error_msg(GOT_ERR_NOT_IMPL,
8993 "parent/child view pair not supported");
8994 break;
8995 case TOG_VIEW_REF:
8996 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8997 if (*new_view == NULL)
8998 return got_error_from_errno("view_open");
8999 if (view->type == TOG_VIEW_LOG)
9000 err = open_ref_view(*new_view, view->state.log.repo);
9001 else if (view->type == TOG_VIEW_TREE)
9002 err = open_ref_view(*new_view, view->state.tree.repo);
9003 else
9004 err = got_error_msg(GOT_ERR_NOT_IMPL,
9005 "parent/child view pair not supported");
9006 if (err)
9007 view_close(*new_view);
9008 break;
9009 case TOG_VIEW_HELP:
9010 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9011 if (*new_view == NULL)
9012 return got_error_from_errno("view_open");
9013 err = open_help_view(*new_view, view);
9014 if (err)
9015 view_close(*new_view);
9016 break;
9017 default:
9018 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9021 return err;
9025 * If view was scrolled down to move the selected line into view when opening a
9026 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9028 static void
9029 offset_selection_up(struct tog_view *view)
9031 switch (view->type) {
9032 case TOG_VIEW_BLAME: {
9033 struct tog_blame_view_state *s = &view->state.blame;
9034 if (s->first_displayed_line == 1) {
9035 s->selected_line = MAX(s->selected_line - view->offset,
9036 1);
9037 break;
9039 if (s->first_displayed_line > view->offset)
9040 s->first_displayed_line -= view->offset;
9041 else
9042 s->first_displayed_line = 1;
9043 s->selected_line += view->offset;
9044 break;
9046 case TOG_VIEW_LOG:
9047 log_scroll_up(&view->state.log, view->offset);
9048 view->state.log.selected += view->offset;
9049 break;
9050 case TOG_VIEW_REF:
9051 ref_scroll_up(&view->state.ref, view->offset);
9052 view->state.ref.selected += view->offset;
9053 break;
9054 case TOG_VIEW_TREE:
9055 tree_scroll_up(&view->state.tree, view->offset);
9056 view->state.tree.selected += view->offset;
9057 break;
9058 default:
9059 break;
9062 view->offset = 0;
9066 * If the selected line is in the section of screen covered by the bottom split,
9067 * scroll down offset lines to move it into view and index its new position.
9069 static const struct got_error *
9070 offset_selection_down(struct tog_view *view)
9072 const struct got_error *err = NULL;
9073 const struct got_error *(*scrolld)(struct tog_view *, int);
9074 int *selected = NULL;
9075 int header, offset;
9077 switch (view->type) {
9078 case TOG_VIEW_BLAME: {
9079 struct tog_blame_view_state *s = &view->state.blame;
9080 header = 3;
9081 scrolld = NULL;
9082 if (s->selected_line > view->nlines - header) {
9083 offset = abs(view->nlines - s->selected_line - header);
9084 s->first_displayed_line += offset;
9085 s->selected_line -= offset;
9086 view->offset = offset;
9088 break;
9090 case TOG_VIEW_LOG: {
9091 struct tog_log_view_state *s = &view->state.log;
9092 scrolld = &log_scroll_down;
9093 header = view_is_parent_view(view) ? 3 : 2;
9094 selected = &s->selected;
9095 break;
9097 case TOG_VIEW_REF: {
9098 struct tog_ref_view_state *s = &view->state.ref;
9099 scrolld = &ref_scroll_down;
9100 header = 3;
9101 selected = &s->selected;
9102 break;
9104 case TOG_VIEW_TREE: {
9105 struct tog_tree_view_state *s = &view->state.tree;
9106 scrolld = &tree_scroll_down;
9107 header = 5;
9108 selected = &s->selected;
9109 break;
9111 default:
9112 selected = NULL;
9113 scrolld = NULL;
9114 header = 0;
9115 break;
9118 if (selected && *selected > view->nlines - header) {
9119 offset = abs(view->nlines - *selected - header);
9120 view->offset = offset;
9121 if (scrolld && offset) {
9122 err = scrolld(view, offset);
9123 *selected -= offset;
9127 return err;
9130 static void
9131 list_commands(FILE *fp)
9133 size_t i;
9135 fprintf(fp, "commands:");
9136 for (i = 0; i < nitems(tog_commands); i++) {
9137 const struct tog_cmd *cmd = &tog_commands[i];
9138 fprintf(fp, " %s", cmd->name);
9140 fputc('\n', fp);
9143 __dead static void
9144 usage(int hflag, int status)
9146 FILE *fp = (status == 0) ? stdout : stderr;
9148 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9149 getprogname());
9150 if (hflag) {
9151 fprintf(fp, "lazy usage: %s path\n", getprogname());
9152 list_commands(fp);
9154 exit(status);
9157 static char **
9158 make_argv(int argc, ...)
9160 va_list ap;
9161 char **argv;
9162 int i;
9164 va_start(ap, argc);
9166 argv = calloc(argc, sizeof(char *));
9167 if (argv == NULL)
9168 err(1, "calloc");
9169 for (i = 0; i < argc; i++) {
9170 argv[i] = strdup(va_arg(ap, char *));
9171 if (argv[i] == NULL)
9172 err(1, "strdup");
9175 va_end(ap);
9176 return argv;
9180 * Try to convert 'tog path' into a 'tog log path' command.
9181 * The user could simply have mistyped the command rather than knowingly
9182 * provided a path. So check whether argv[0] can in fact be resolved
9183 * to a path in the HEAD commit and print a special error if not.
9184 * This hack is for mpi@ <3
9186 static const struct got_error *
9187 tog_log_with_path(int argc, char *argv[])
9189 const struct got_error *error = NULL, *close_err;
9190 const struct tog_cmd *cmd = NULL;
9191 struct got_repository *repo = NULL;
9192 struct got_worktree *worktree = NULL;
9193 struct got_object_id *commit_id = NULL, *id = NULL;
9194 struct got_commit_object *commit = NULL;
9195 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9196 char *commit_id_str = NULL, **cmd_argv = NULL;
9197 int *pack_fds = NULL;
9199 cwd = getcwd(NULL, 0);
9200 if (cwd == NULL)
9201 return got_error_from_errno("getcwd");
9203 error = got_repo_pack_fds_open(&pack_fds);
9204 if (error != NULL)
9205 goto done;
9207 error = got_worktree_open(&worktree, cwd);
9208 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9209 goto done;
9211 if (worktree)
9212 repo_path = strdup(got_worktree_get_repo_path(worktree));
9213 else
9214 repo_path = strdup(cwd);
9215 if (repo_path == NULL) {
9216 error = got_error_from_errno("strdup");
9217 goto done;
9220 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9221 if (error != NULL)
9222 goto done;
9224 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9225 repo, worktree);
9226 if (error)
9227 goto done;
9229 error = tog_load_refs(repo, 0);
9230 if (error)
9231 goto done;
9232 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9233 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9234 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9235 if (error)
9236 goto done;
9238 if (worktree) {
9239 got_worktree_close(worktree);
9240 worktree = NULL;
9243 error = got_object_open_as_commit(&commit, repo, commit_id);
9244 if (error)
9245 goto done;
9247 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9248 if (error) {
9249 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9250 goto done;
9251 fprintf(stderr, "%s: '%s' is no known command or path\n",
9252 getprogname(), argv[0]);
9253 usage(1, 1);
9254 /* not reached */
9257 error = got_object_id_str(&commit_id_str, commit_id);
9258 if (error)
9259 goto done;
9261 cmd = &tog_commands[0]; /* log */
9262 argc = 4;
9263 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9264 error = cmd->cmd_main(argc, cmd_argv);
9265 done:
9266 if (repo) {
9267 close_err = got_repo_close(repo);
9268 if (error == NULL)
9269 error = close_err;
9271 if (commit)
9272 got_object_commit_close(commit);
9273 if (worktree)
9274 got_worktree_close(worktree);
9275 if (pack_fds) {
9276 const struct got_error *pack_err =
9277 got_repo_pack_fds_close(pack_fds);
9278 if (error == NULL)
9279 error = pack_err;
9281 free(id);
9282 free(commit_id_str);
9283 free(commit_id);
9284 free(cwd);
9285 free(repo_path);
9286 free(in_repo_path);
9287 if (cmd_argv) {
9288 int i;
9289 for (i = 0; i < argc; i++)
9290 free(cmd_argv[i]);
9291 free(cmd_argv);
9293 tog_free_refs();
9294 return error;
9297 int
9298 main(int argc, char *argv[])
9300 const struct got_error *error = NULL;
9301 const struct tog_cmd *cmd = NULL;
9302 int ch, hflag = 0, Vflag = 0;
9303 char **cmd_argv = NULL;
9304 static const struct option longopts[] = {
9305 { "version", no_argument, NULL, 'V' },
9306 { NULL, 0, NULL, 0}
9308 char *diff_algo_str = NULL;
9310 if (!isatty(STDIN_FILENO))
9311 errx(1, "standard input is not a tty");
9313 setlocale(LC_CTYPE, "");
9315 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9316 switch (ch) {
9317 case 'h':
9318 hflag = 1;
9319 break;
9320 case 'V':
9321 Vflag = 1;
9322 break;
9323 default:
9324 usage(hflag, 1);
9325 /* NOTREACHED */
9329 argc -= optind;
9330 argv += optind;
9331 optind = 1;
9332 optreset = 1;
9334 if (Vflag) {
9335 got_version_print_str();
9336 return 0;
9339 #ifndef PROFILE
9340 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9341 NULL) == -1)
9342 err(1, "pledge");
9343 #endif
9345 if (argc == 0) {
9346 if (hflag)
9347 usage(hflag, 0);
9348 /* Build an argument vector which runs a default command. */
9349 cmd = &tog_commands[0];
9350 argc = 1;
9351 cmd_argv = make_argv(argc, cmd->name);
9352 } else {
9353 size_t i;
9355 /* Did the user specify a command? */
9356 for (i = 0; i < nitems(tog_commands); i++) {
9357 if (strncmp(tog_commands[i].name, argv[0],
9358 strlen(argv[0])) == 0) {
9359 cmd = &tog_commands[i];
9360 break;
9365 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9366 if (diff_algo_str) {
9367 if (strcasecmp(diff_algo_str, "patience") == 0)
9368 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9369 if (strcasecmp(diff_algo_str, "myers") == 0)
9370 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9373 if (cmd == NULL) {
9374 if (argc != 1)
9375 usage(0, 1);
9376 /* No command specified; try log with a path */
9377 error = tog_log_with_path(argc, argv);
9378 } else {
9379 if (hflag)
9380 cmd->cmd_usage();
9381 else
9382 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9385 endwin();
9386 putchar('\n');
9387 if (cmd_argv) {
9388 int i;
9389 for (i = 0; i < argc; i++)
9390 free(cmd_argv[i]);
9391 free(cmd_argv);
9394 if (error && error->code != GOT_ERR_CANCELLED &&
9395 error->code != GOT_ERR_EOF &&
9396 error->code != GOT_ERR_PRIVSEP_EXIT &&
9397 error->code != GOT_ERR_PRIVSEP_PIPE &&
9398 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9399 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9400 return 0;