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 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 #endif
26 #include <curses.h>
27 #include <panel.h>
28 #include <locale.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #include <string.h>
35 #include <err.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
43 #include <sched.h>
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
52 #include "got_diff.h"
53 #include "got_opentemp.h"
54 #include "got_utf8.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_path.h"
60 #include "got_worktree.h"
62 #ifndef MIN
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #endif
66 #ifndef MAX
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #endif
70 #ifndef CTRL
71 #define CTRL(x) ((x) & 0x1f)
72 #endif
74 #ifndef nitems
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 #endif
78 struct tog_cmd {
79 const char *name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 };
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
103 };
105 enum tog_view_type {
106 TOG_VIEW_DIFF,
107 TOG_VIEW_LOG,
108 TOG_VIEW_BLAME,
109 TOG_VIEW_TREE,
110 TOG_VIEW_REF,
111 TOG_VIEW_HELP
112 };
114 /* Match _DIFF to _HELP with enum tog_view_type TOG_VIEW_* counterparts. */
115 enum tog_keymap_type {
116 TOG_KEYMAP_KEYS = -2,
117 TOG_KEYMAP_GLOBAL,
118 TOG_KEYMAP_DIFF,
119 TOG_KEYMAP_LOG,
120 TOG_KEYMAP_BLAME,
121 TOG_KEYMAP_TREE,
122 TOG_KEYMAP_REF,
123 TOG_KEYMAP_HELP
124 };
126 enum tog_view_mode {
127 TOG_VIEW_SPLIT_NONE,
128 TOG_VIEW_SPLIT_VERT,
129 TOG_VIEW_SPLIT_HRZN
130 };
132 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
134 #define TOG_EOF_STRING "(END)"
136 struct commit_queue_entry {
137 TAILQ_ENTRY(commit_queue_entry) entry;
138 struct got_object_id *id;
139 struct got_commit_object *commit;
140 int idx;
141 };
142 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
143 struct commit_queue {
144 int ncommits;
145 struct commit_queue_head head;
146 };
148 struct tog_color {
149 STAILQ_ENTRY(tog_color) entry;
150 regex_t regex;
151 short colorpair;
152 };
153 STAILQ_HEAD(tog_colors, tog_color);
155 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
156 static struct got_reflist_object_id_map *tog_refs_idmap;
157 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
159 static const struct got_error *
160 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
161 struct got_reference* re2)
163 const char *name1 = got_ref_get_name(re1);
164 const char *name2 = got_ref_get_name(re2);
165 int isbackup1, isbackup2;
167 /* Sort backup refs towards the bottom of the list. */
168 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
169 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
170 if (!isbackup1 && isbackup2) {
171 *cmp = -1;
172 return NULL;
173 } else if (isbackup1 && !isbackup2) {
174 *cmp = 1;
175 return NULL;
178 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
179 return NULL;
182 static const struct got_error *
183 tog_load_refs(struct got_repository *repo, int sort_by_date)
185 const struct got_error *err;
187 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
188 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
189 repo);
190 if (err)
191 return err;
193 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
194 repo);
197 static void
198 tog_free_refs(void)
200 if (tog_refs_idmap) {
201 got_reflist_object_id_map_free(tog_refs_idmap);
202 tog_refs_idmap = NULL;
204 got_ref_list_free(&tog_refs);
207 static const struct got_error *
208 add_color(struct tog_colors *colors, const char *pattern,
209 int idx, short color)
211 const struct got_error *err = NULL;
212 struct tog_color *tc;
213 int regerr = 0;
215 if (idx < 1 || idx > COLOR_PAIRS - 1)
216 return NULL;
218 init_pair(idx, color, -1);
220 tc = calloc(1, sizeof(*tc));
221 if (tc == NULL)
222 return got_error_from_errno("calloc");
223 regerr = regcomp(&tc->regex, pattern,
224 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
225 if (regerr) {
226 static char regerr_msg[512];
227 static char err_msg[512];
228 regerror(regerr, &tc->regex, regerr_msg,
229 sizeof(regerr_msg));
230 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
231 regerr_msg);
232 err = got_error_msg(GOT_ERR_REGEX, err_msg);
233 free(tc);
234 return err;
236 tc->colorpair = idx;
237 STAILQ_INSERT_HEAD(colors, tc, entry);
238 return NULL;
241 static void
242 free_colors(struct tog_colors *colors)
244 struct tog_color *tc;
246 while (!STAILQ_EMPTY(colors)) {
247 tc = STAILQ_FIRST(colors);
248 STAILQ_REMOVE_HEAD(colors, entry);
249 regfree(&tc->regex);
250 free(tc);
254 static struct tog_color *
255 get_color(struct tog_colors *colors, int colorpair)
257 struct tog_color *tc = NULL;
259 STAILQ_FOREACH(tc, colors, entry) {
260 if (tc->colorpair == colorpair)
261 return tc;
264 return NULL;
267 static int
268 default_color_value(const char *envvar)
270 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
271 return COLOR_MAGENTA;
272 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
273 return COLOR_CYAN;
274 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
275 return COLOR_YELLOW;
276 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
277 return COLOR_GREEN;
278 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
279 return COLOR_MAGENTA;
280 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
283 return COLOR_CYAN;
284 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
285 return COLOR_GREEN;
286 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
287 return COLOR_GREEN;
288 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
289 return COLOR_CYAN;
290 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
291 return COLOR_YELLOW;
292 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
293 return COLOR_GREEN;
294 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
295 return COLOR_MAGENTA;
296 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
297 return COLOR_YELLOW;
298 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
299 return COLOR_CYAN;
301 return -1;
304 static int
305 get_color_value(const char *envvar)
307 const char *val = getenv(envvar);
309 if (val == NULL)
310 return default_color_value(envvar);
312 if (strcasecmp(val, "black") == 0)
313 return COLOR_BLACK;
314 if (strcasecmp(val, "red") == 0)
315 return COLOR_RED;
316 if (strcasecmp(val, "green") == 0)
317 return COLOR_GREEN;
318 if (strcasecmp(val, "yellow") == 0)
319 return COLOR_YELLOW;
320 if (strcasecmp(val, "blue") == 0)
321 return COLOR_BLUE;
322 if (strcasecmp(val, "magenta") == 0)
323 return COLOR_MAGENTA;
324 if (strcasecmp(val, "cyan") == 0)
325 return COLOR_CYAN;
326 if (strcasecmp(val, "white") == 0)
327 return COLOR_WHITE;
328 if (strcasecmp(val, "default") == 0)
329 return -1;
331 return default_color_value(envvar);
334 struct tog_diff_view_state {
335 struct got_object_id *id1, *id2;
336 const char *label1, *label2;
337 FILE *f, *f1, *f2;
338 int fd1, fd2;
339 int lineno;
340 int first_displayed_line;
341 int last_displayed_line;
342 int eof;
343 int diff_context;
344 int ignore_whitespace;
345 int force_text_diff;
346 struct got_repository *repo;
347 struct got_diff_line *lines;
348 size_t nlines;
349 int matched_line;
350 int selected_line;
352 /* passed from log or blame view; may be NULL */
353 struct tog_view *parent_view;
354 };
356 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
357 static volatile sig_atomic_t tog_thread_error;
359 struct tog_log_thread_args {
360 pthread_cond_t need_commits;
361 pthread_cond_t commit_loaded;
362 int commits_needed;
363 int load_all;
364 struct got_commit_graph *graph;
365 struct commit_queue *real_commits;
366 const char *in_repo_path;
367 struct got_object_id *start_id;
368 struct got_repository *repo;
369 int *pack_fds;
370 int log_complete;
371 sig_atomic_t *quit;
372 struct commit_queue_entry **first_displayed_entry;
373 struct commit_queue_entry **selected_entry;
374 int *searching;
375 int *search_next_done;
376 regex_t *regex;
377 int *limiting;
378 int limit_match;
379 regex_t *limit_regex;
380 struct commit_queue *limit_commits;
381 };
383 struct tog_log_view_state {
384 struct commit_queue *commits;
385 struct commit_queue_entry *first_displayed_entry;
386 struct commit_queue_entry *last_displayed_entry;
387 struct commit_queue_entry *selected_entry;
388 struct commit_queue real_commits;
389 int selected;
390 char *in_repo_path;
391 char *head_ref_name;
392 int log_branches;
393 struct got_repository *repo;
394 struct got_object_id *start_id;
395 sig_atomic_t quit;
396 pthread_t thread;
397 struct tog_log_thread_args thread_args;
398 struct commit_queue_entry *matched_entry;
399 struct commit_queue_entry *search_entry;
400 struct tog_colors colors;
401 int use_committer;
402 int limit_view;
403 regex_t limit_regex;
404 struct commit_queue limit_commits;
405 };
407 #define TOG_COLOR_DIFF_MINUS 1
408 #define TOG_COLOR_DIFF_PLUS 2
409 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
410 #define TOG_COLOR_DIFF_META 4
411 #define TOG_COLOR_TREE_SUBMODULE 5
412 #define TOG_COLOR_TREE_SYMLINK 6
413 #define TOG_COLOR_TREE_DIRECTORY 7
414 #define TOG_COLOR_TREE_EXECUTABLE 8
415 #define TOG_COLOR_COMMIT 9
416 #define TOG_COLOR_AUTHOR 10
417 #define TOG_COLOR_DATE 11
418 #define TOG_COLOR_REFS_HEADS 12
419 #define TOG_COLOR_REFS_TAGS 13
420 #define TOG_COLOR_REFS_REMOTES 14
421 #define TOG_COLOR_REFS_BACKUP 15
423 struct tog_blame_cb_args {
424 struct tog_blame_line *lines; /* one per line */
425 int nlines;
427 struct tog_view *view;
428 struct got_object_id *commit_id;
429 int *quit;
430 };
432 struct tog_blame_thread_args {
433 const char *path;
434 struct got_repository *repo;
435 struct tog_blame_cb_args *cb_args;
436 int *complete;
437 got_cancel_cb cancel_cb;
438 void *cancel_arg;
439 };
441 struct tog_blame {
442 FILE *f;
443 off_t filesize;
444 struct tog_blame_line *lines;
445 int nlines;
446 off_t *line_offsets;
447 pthread_t thread;
448 struct tog_blame_thread_args thread_args;
449 struct tog_blame_cb_args cb_args;
450 const char *path;
451 int *pack_fds;
452 };
454 struct tog_blame_view_state {
455 int first_displayed_line;
456 int last_displayed_line;
457 int selected_line;
458 int last_diffed_line;
459 int blame_complete;
460 int eof;
461 int done;
462 struct got_object_id_queue blamed_commits;
463 struct got_object_qid *blamed_commit;
464 char *path;
465 struct got_repository *repo;
466 struct got_object_id *commit_id;
467 struct got_object_id *id_to_log;
468 struct tog_blame blame;
469 int matched_line;
470 struct tog_colors colors;
471 };
473 struct tog_parent_tree {
474 TAILQ_ENTRY(tog_parent_tree) entry;
475 struct got_tree_object *tree;
476 struct got_tree_entry *first_displayed_entry;
477 struct got_tree_entry *selected_entry;
478 int selected;
479 };
481 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
483 struct tog_tree_view_state {
484 char *tree_label;
485 struct got_object_id *commit_id;/* commit which this tree belongs to */
486 struct got_tree_object *root; /* the commit's root tree entry */
487 struct got_tree_object *tree; /* currently displayed (sub-)tree */
488 struct got_tree_entry *first_displayed_entry;
489 struct got_tree_entry *last_displayed_entry;
490 struct got_tree_entry *selected_entry;
491 int ndisplayed, selected, show_ids;
492 struct tog_parent_trees parents; /* parent trees of current sub-tree */
493 char *head_ref_name;
494 struct got_repository *repo;
495 struct got_tree_entry *matched_entry;
496 struct tog_colors colors;
497 };
499 struct tog_reflist_entry {
500 TAILQ_ENTRY(tog_reflist_entry) entry;
501 struct got_reference *ref;
502 int idx;
503 };
505 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
507 struct tog_ref_view_state {
508 struct tog_reflist_head refs;
509 struct tog_reflist_entry *first_displayed_entry;
510 struct tog_reflist_entry *last_displayed_entry;
511 struct tog_reflist_entry *selected_entry;
512 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
513 struct got_repository *repo;
514 struct tog_reflist_entry *matched_entry;
515 struct tog_colors colors;
516 };
518 struct tog_help_view_state {
519 FILE *f;
520 off_t *line_offsets;
521 size_t nlines;
522 int lineno;
523 int first_displayed_line;
524 int last_displayed_line;
525 int eof;
526 int matched_line;
527 int selected_line;
528 int all;
529 enum tog_keymap_type type;
530 };
532 #define GENERATE_HELP \
533 KEYMAP_("Global", TOG_KEYMAP_GLOBAL), \
534 KEY_("H F1", "Open view-specific help (double tap for all help)"), \
535 KEY_("k C-p Up", "Move cursor or page up one line"), \
536 KEY_("j C-n Down", "Move cursor or page down one line"), \
537 KEY_("C-b b PgUp", "Scroll the view up one page"), \
538 KEY_("C-f f PgDn Space", "Scroll the view down one page"), \
539 KEY_("C-u u", "Scroll the view up one half page"), \
540 KEY_("C-d d", "Scroll the view down one half page"), \
541 KEY_("g Home", "Go to line N (default: first line)"), \
542 KEY_("G End", "Go to line N (default: last line)"), \
543 KEY_("l Right", "Scroll the view right"), \
544 KEY_("h Left", "Scroll the view left"), \
545 KEY_("$", "Scroll view to the rightmost position"), \
546 KEY_("0", "Scroll view to the leftmost position"), \
547 KEY_("-", "Decrease size of the focussed split"), \
548 KEY_("+", "Increase size of the focussed split"), \
549 KEY_("Tab", "Switch focus between views"), \
550 KEY_("F", "Toggle fullscreen mode"), \
551 KEY_("/", "Open prompt to enter search term"), \
552 KEY_("n", "Find next line/token matching the current search term"), \
553 KEY_("N", "Find previous line/token matching the current search term"),\
554 KEY_("q", "Quit the focussed view; Quit help screen"), \
555 KEY_("Q", "Quit tog"), \
557 KEYMAP_("Log view", TOG_KEYMAP_LOG), \
558 KEY_("< ,", "Move cursor up one commit"), \
559 KEY_("> .", "Move cursor down one commit"), \
560 KEY_("Enter", "Open diff view of the selected commit"), \
561 KEY_("B", "Reload the log view and toggle display of merged commits"), \
562 KEY_("R", "Open ref view of all repository references"), \
563 KEY_("T", "Display tree view of the repository from the selected" \
564 " commit"), \
565 KEY_("@", "Toggle between displaying author and committer name"), \
566 KEY_("&", "Open prompt to enter term to limit commits displayed"), \
567 KEY_("C-g Backspace", "Cancel current search or log operation"), \
568 KEY_("C-l", "Reload the log view with new commits in the repository"), \
570 KEYMAP_("Diff view", TOG_KEYMAP_DIFF), \
571 KEY_("K < ,", "Display diff of next line in the file/log entry"), \
572 KEY_("J > .", "Display diff of previous line in the file/log entry"), \
573 KEY_("A", "Toggle between Myers and Patience diff algorithm"), \
574 KEY_("a", "Toggle treatment of file as ASCII irrespective of binary" \
575 " data"), \
576 KEY_("(", "Go to the previous file in the diff"), \
577 KEY_(")", "Go to the next file in the diff"), \
578 KEY_("{", "Go to the previous hunk in the diff"), \
579 KEY_("}", "Go to the next hunk in the diff"), \
580 KEY_("[", "Decrease the number of context lines"), \
581 KEY_("]", "Increase the number of context lines"), \
582 KEY_("w", "Toggle ignore whitespace-only changes in the diff"), \
584 KEYMAP_("Blame view", TOG_KEYMAP_BLAME), \
585 KEY_("Enter", "Display diff view of the selected line's commit"), \
586 KEY_("A", "Toggle diff algorithm between Myers and Patience"), \
587 KEY_("L", "Open log view for the currently selected annotated line"), \
588 KEY_("C", "Reload view with the previously blamed commit"), \
589 KEY_("c", "Reload view with the version of the file found in the" \
590 " selected line's commit"), \
591 KEY_("p", "Reload view with the version of the file found in the" \
592 " selected line's parent commit"), \
594 KEYMAP_("Tree view", TOG_KEYMAP_TREE), \
595 KEY_("Enter", "Enter selected directory or open blame view of the" \
596 " selected file"), \
597 KEY_("L", "Open log view for the selected entry"), \
598 KEY_("R", "Open ref view of all repository references"), \
599 KEY_("i", "Show object IDs for all tree entries"), \
600 KEY_("Backspace", "Return to the parent directory"), \
602 KEYMAP_("Ref view", TOG_KEYMAP_REF), \
603 KEY_("Enter", "Display log view of the selected reference"), \
604 KEY_("T", "Display tree view of the selected reference"), \
605 KEY_("i", "Toggle display of IDs for all non-symbolic references"), \
606 KEY_("m", "Toggle display of last modified date for each reference"), \
607 KEY_("o", "Toggle reference sort order (name -> timestamp)"), \
608 KEY_("C-l", "Reload view with all repository references")
610 struct tog_key_map {
611 const char *keys;
612 const char *info;
613 enum tog_keymap_type type;
614 };
616 /*
617 * We implement two types of views: parent views and child views.
619 * The 'Tab' key switches focus between a parent view and its child view.
620 * Child views are shown side-by-side to their parent view, provided
621 * there is enough screen estate.
623 * When a new view is opened from within a parent view, this new view
624 * becomes a child view of the parent view, replacing any existing child.
626 * When a new view is opened from within a child view, this new view
627 * becomes a parent view which will obscure the views below until the
628 * user quits the new parent view by typing 'q'.
630 * This list of views contains parent views only.
631 * Child views are only pointed to by their parent view.
632 */
633 TAILQ_HEAD(tog_view_list_head, tog_view);
635 struct tog_view {
636 TAILQ_ENTRY(tog_view) entry;
637 WINDOW *window;
638 PANEL *panel;
639 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
640 int resized_y, resized_x; /* begin_y/x based on user resizing */
641 int maxx, x; /* max column and current start column */
642 int lines, cols; /* copies of LINES and COLS */
643 int nscrolled, offset; /* lines scrolled and hsplit line offset */
644 int gline, hiline; /* navigate to and highlight this nG line */
645 int ch, count; /* current keymap and count prefix */
646 int resized; /* set when in a resize event */
647 int focussed; /* Only set on one parent or child view at a time. */
648 int dying;
649 struct tog_view *parent;
650 struct tog_view *child;
652 /*
653 * This flag is initially set on parent views when a new child view
654 * is created. It gets toggled when the 'Tab' key switches focus
655 * between parent and child.
656 * The flag indicates whether focus should be passed on to our child
657 * view if this parent view gets picked for focus after another parent
658 * view was closed. This prevents child views from losing focus in such
659 * situations.
660 */
661 int focus_child;
663 enum tog_view_mode mode;
664 /* type-specific state */
665 enum tog_view_type type;
666 union {
667 struct tog_diff_view_state diff;
668 struct tog_log_view_state log;
669 struct tog_blame_view_state blame;
670 struct tog_tree_view_state tree;
671 struct tog_ref_view_state ref;
672 struct tog_help_view_state help;
673 } state;
675 const struct got_error *(*show)(struct tog_view *);
676 const struct got_error *(*input)(struct tog_view **,
677 struct tog_view *, int);
678 const struct got_error *(*reset)(struct tog_view *);
679 const struct got_error *(*resize)(struct tog_view *, int);
680 const struct got_error *(*close)(struct tog_view *);
682 const struct got_error *(*search_start)(struct tog_view *);
683 const struct got_error *(*search_next)(struct tog_view *);
684 void (*search_setup)(struct tog_view *, FILE **, off_t **, size_t *,
685 int **, int **, int **, int **);
686 int search_started;
687 int searching;
688 #define TOG_SEARCH_FORWARD 1
689 #define TOG_SEARCH_BACKWARD 2
690 int search_next_done;
691 #define TOG_SEARCH_HAVE_MORE 1
692 #define TOG_SEARCH_NO_MORE 2
693 #define TOG_SEARCH_HAVE_NONE 3
694 regex_t regex;
695 regmatch_t regmatch;
696 };
698 static const struct got_error *open_diff_view(struct tog_view *,
699 struct got_object_id *, struct got_object_id *,
700 const char *, const char *, int, int, int, struct tog_view *,
701 struct got_repository *);
702 static const struct got_error *show_diff_view(struct tog_view *);
703 static const struct got_error *input_diff_view(struct tog_view **,
704 struct tog_view *, int);
705 static const struct got_error *reset_diff_view(struct tog_view *);
706 static const struct got_error* close_diff_view(struct tog_view *);
707 static const struct got_error *search_start_diff_view(struct tog_view *);
708 static void search_setup_diff_view(struct tog_view *, FILE **, off_t **,
709 size_t *, int **, int **, int **, int **);
710 static const struct got_error *search_next_view_match(struct tog_view *);
712 static const struct got_error *open_log_view(struct tog_view *,
713 struct got_object_id *, struct got_repository *,
714 const char *, const char *, int);
715 static const struct got_error * show_log_view(struct tog_view *);
716 static const struct got_error *input_log_view(struct tog_view **,
717 struct tog_view *, int);
718 static const struct got_error *resize_log_view(struct tog_view *, int);
719 static const struct got_error *close_log_view(struct tog_view *);
720 static const struct got_error *search_start_log_view(struct tog_view *);
721 static const struct got_error *search_next_log_view(struct tog_view *);
723 static const struct got_error *open_blame_view(struct tog_view *, char *,
724 struct got_object_id *, struct got_repository *);
725 static const struct got_error *show_blame_view(struct tog_view *);
726 static const struct got_error *input_blame_view(struct tog_view **,
727 struct tog_view *, int);
728 static const struct got_error *reset_blame_view(struct tog_view *);
729 static const struct got_error *close_blame_view(struct tog_view *);
730 static const struct got_error *search_start_blame_view(struct tog_view *);
731 static void search_setup_blame_view(struct tog_view *, FILE **, off_t **,
732 size_t *, int **, int **, int **, int **);
734 static const struct got_error *open_tree_view(struct tog_view *,
735 struct got_object_id *, const char *, struct got_repository *);
736 static const struct got_error *show_tree_view(struct tog_view *);
737 static const struct got_error *input_tree_view(struct tog_view **,
738 struct tog_view *, int);
739 static const struct got_error *close_tree_view(struct tog_view *);
740 static const struct got_error *search_start_tree_view(struct tog_view *);
741 static const struct got_error *search_next_tree_view(struct tog_view *);
743 static const struct got_error *open_ref_view(struct tog_view *,
744 struct got_repository *);
745 static const struct got_error *show_ref_view(struct tog_view *);
746 static const struct got_error *input_ref_view(struct tog_view **,
747 struct tog_view *, int);
748 static const struct got_error *close_ref_view(struct tog_view *);
749 static const struct got_error *search_start_ref_view(struct tog_view *);
750 static const struct got_error *search_next_ref_view(struct tog_view *);
752 static const struct got_error *open_help_view(struct tog_view *,
753 struct tog_view *);
754 static const struct got_error *show_help_view(struct tog_view *);
755 static const struct got_error *input_help_view(struct tog_view **,
756 struct tog_view *, int);
757 static const struct got_error *reset_help_view(struct tog_view *);
758 static const struct got_error* close_help_view(struct tog_view *);
759 static const struct got_error *search_start_help_view(struct tog_view *);
760 static void search_setup_help_view(struct tog_view *, FILE **, off_t **,
761 size_t *, int **, int **, int **, int **);
763 static volatile sig_atomic_t tog_sigwinch_received;
764 static volatile sig_atomic_t tog_sigpipe_received;
765 static volatile sig_atomic_t tog_sigcont_received;
766 static volatile sig_atomic_t tog_sigint_received;
767 static volatile sig_atomic_t tog_sigterm_received;
769 static void
770 tog_sigwinch(int signo)
772 tog_sigwinch_received = 1;
775 static void
776 tog_sigpipe(int signo)
778 tog_sigpipe_received = 1;
781 static void
782 tog_sigcont(int signo)
784 tog_sigcont_received = 1;
787 static void
788 tog_sigint(int signo)
790 tog_sigint_received = 1;
793 static void
794 tog_sigterm(int signo)
796 tog_sigterm_received = 1;
799 static int
800 tog_fatal_signal_received(void)
802 return (tog_sigpipe_received ||
803 tog_sigint_received || tog_sigterm_received);
806 static const struct got_error *
807 view_close(struct tog_view *view)
809 const struct got_error *err = NULL, *child_err = NULL;
811 if (view->child) {
812 child_err = view_close(view->child);
813 view->child = NULL;
815 if (view->close)
816 err = view->close(view);
817 if (view->panel)
818 del_panel(view->panel);
819 if (view->window)
820 delwin(view->window);
821 free(view);
822 return err ? err : child_err;
825 static struct tog_view *
826 view_open(int nlines, int ncols, int begin_y, int begin_x,
827 enum tog_view_type type)
829 struct tog_view *view = calloc(1, sizeof(*view));
831 if (view == NULL)
832 return NULL;
834 view->type = type;
835 view->lines = LINES;
836 view->cols = COLS;
837 view->nlines = nlines ? nlines : LINES - begin_y;
838 view->ncols = ncols ? ncols : COLS - begin_x;
839 view->begin_y = begin_y;
840 view->begin_x = begin_x;
841 view->window = newwin(nlines, ncols, begin_y, begin_x);
842 if (view->window == NULL) {
843 view_close(view);
844 return NULL;
846 view->panel = new_panel(view->window);
847 if (view->panel == NULL ||
848 set_panel_userptr(view->panel, view) != OK) {
849 view_close(view);
850 return NULL;
853 keypad(view->window, TRUE);
854 return view;
857 static int
858 view_split_begin_x(int begin_x)
860 if (begin_x > 0 || COLS < 120)
861 return 0;
862 return (COLS - MAX(COLS / 2, 80));
865 /* XXX Stub till we decide what to do. */
866 static int
867 view_split_begin_y(int lines)
869 return lines * HSPLIT_SCALE;
872 static const struct got_error *view_resize(struct tog_view *);
874 static const struct got_error *
875 view_splitscreen(struct tog_view *view)
877 const struct got_error *err = NULL;
879 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
880 if (view->resized_y && view->resized_y < view->lines)
881 view->begin_y = view->resized_y;
882 else
883 view->begin_y = view_split_begin_y(view->nlines);
884 view->begin_x = 0;
885 } else if (!view->resized) {
886 if (view->resized_x && view->resized_x < view->cols - 1 &&
887 view->cols > 119)
888 view->begin_x = view->resized_x;
889 else
890 view->begin_x = view_split_begin_x(0);
891 view->begin_y = 0;
893 view->nlines = LINES - view->begin_y;
894 view->ncols = COLS - view->begin_x;
895 view->lines = LINES;
896 view->cols = COLS;
897 err = view_resize(view);
898 if (err)
899 return err;
901 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
902 view->parent->nlines = view->begin_y;
904 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
905 return got_error_from_errno("mvwin");
907 return NULL;
910 static const struct got_error *
911 view_fullscreen(struct tog_view *view)
913 const struct got_error *err = NULL;
915 view->begin_x = 0;
916 view->begin_y = view->resized ? view->begin_y : 0;
917 view->nlines = view->resized ? view->nlines : LINES;
918 view->ncols = COLS;
919 view->lines = LINES;
920 view->cols = COLS;
921 err = view_resize(view);
922 if (err)
923 return err;
925 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
926 return got_error_from_errno("mvwin");
928 return NULL;
931 static int
932 view_is_parent_view(struct tog_view *view)
934 return view->parent == NULL;
937 static int
938 view_is_splitscreen(struct tog_view *view)
940 return view->begin_x > 0 || view->begin_y > 0;
943 static int
944 view_is_fullscreen(struct tog_view *view)
946 return view->nlines == LINES && view->ncols == COLS;
949 static int
950 view_is_hsplit_top(struct tog_view *view)
952 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
953 view_is_splitscreen(view->child);
956 static void
957 view_border(struct tog_view *view)
959 PANEL *panel;
960 const struct tog_view *view_above;
962 if (view->parent)
963 return view_border(view->parent);
965 panel = panel_above(view->panel);
966 if (panel == NULL)
967 return;
969 view_above = panel_userptr(panel);
970 if (view->mode == TOG_VIEW_SPLIT_HRZN)
971 mvwhline(view->window, view_above->begin_y - 1,
972 view->begin_x, got_locale_is_utf8() ?
973 ACS_HLINE : '-', view->ncols);
974 else
975 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
976 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
979 static const struct got_error *view_init_hsplit(struct tog_view *, int);
980 static const struct got_error *request_log_commits(struct tog_view *);
981 static const struct got_error *offset_selection_down(struct tog_view *);
982 static void offset_selection_up(struct tog_view *);
983 static void view_get_split(struct tog_view *, int *, int *);
985 static const struct got_error *
986 view_resize(struct tog_view *view)
988 const struct got_error *err = NULL;
989 int dif, nlines, ncols;
991 dif = LINES - view->lines; /* line difference */
993 if (view->lines > LINES)
994 nlines = view->nlines - (view->lines - LINES);
995 else
996 nlines = view->nlines + (LINES - view->lines);
997 if (view->cols > COLS)
998 ncols = view->ncols - (view->cols - COLS);
999 else
1000 ncols = view->ncols + (COLS - view->cols);
1002 if (view->child) {
1003 int hs = view->child->begin_y;
1005 if (!view_is_fullscreen(view))
1006 view->child->begin_x = view_split_begin_x(view->begin_x);
1007 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
1008 view->child->begin_x == 0) {
1009 ncols = COLS;
1011 view_fullscreen(view->child);
1012 if (view->child->focussed)
1013 show_panel(view->child->panel);
1014 else
1015 show_panel(view->panel);
1016 } else {
1017 ncols = view->child->begin_x;
1019 view_splitscreen(view->child);
1020 show_panel(view->child->panel);
1023 * XXX This is ugly and needs to be moved into the above
1024 * logic but "works" for now and my attempts at moving it
1025 * break either 'tab' or 'F' key maps in horizontal splits.
1027 if (hs) {
1028 err = view_splitscreen(view->child);
1029 if (err)
1030 return err;
1031 if (dif < 0) { /* top split decreased */
1032 err = offset_selection_down(view);
1033 if (err)
1034 return err;
1036 view_border(view);
1037 update_panels();
1038 doupdate();
1039 show_panel(view->child->panel);
1040 nlines = view->nlines;
1042 } else if (view->parent == NULL)
1043 ncols = COLS;
1045 if (view->resize && dif > 0) {
1046 err = view->resize(view, dif);
1047 if (err)
1048 return err;
1051 if (wresize(view->window, nlines, ncols) == ERR)
1052 return got_error_from_errno("wresize");
1053 if (replace_panel(view->panel, view->window) == ERR)
1054 return got_error_from_errno("replace_panel");
1055 wclear(view->window);
1057 view->nlines = nlines;
1058 view->ncols = ncols;
1059 view->lines = LINES;
1060 view->cols = COLS;
1062 return NULL;
1065 static const struct got_error *
1066 resize_log_view(struct tog_view *view, int increase)
1068 struct tog_log_view_state *s = &view->state.log;
1069 const struct got_error *err = NULL;
1070 int n = 0;
1072 if (s->selected_entry)
1073 n = s->selected_entry->idx + view->lines - s->selected;
1076 * Request commits to account for the increased
1077 * height so we have enough to populate the view.
1079 if (s->commits->ncommits < n) {
1080 view->nscrolled = n - s->commits->ncommits + increase + 1;
1081 err = request_log_commits(view);
1084 return err;
1087 static void
1088 view_adjust_offset(struct tog_view *view, int n)
1090 if (n == 0)
1091 return;
1093 if (view->parent && view->parent->offset) {
1094 if (view->parent->offset + n >= 0)
1095 view->parent->offset += n;
1096 else
1097 view->parent->offset = 0;
1098 } else if (view->offset) {
1099 if (view->offset - n >= 0)
1100 view->offset -= n;
1101 else
1102 view->offset = 0;
1106 static const struct got_error *
1107 view_resize_split(struct tog_view *view, int resize)
1109 const struct got_error *err = NULL;
1110 struct tog_view *v = NULL;
1112 if (view->parent)
1113 v = view->parent;
1114 else
1115 v = view;
1117 if (!v->child || !view_is_splitscreen(v->child))
1118 return NULL;
1120 v->resized = v->child->resized = resize; /* lock for resize event */
1122 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
1123 if (v->child->resized_y)
1124 v->child->begin_y = v->child->resized_y;
1125 if (view->parent)
1126 v->child->begin_y -= resize;
1127 else
1128 v->child->begin_y += resize;
1129 if (v->child->begin_y < 3) {
1130 view->count = 0;
1131 v->child->begin_y = 3;
1132 } else if (v->child->begin_y > LINES - 1) {
1133 view->count = 0;
1134 v->child->begin_y = LINES - 1;
1136 v->ncols = COLS;
1137 v->child->ncols = COLS;
1138 view_adjust_offset(view, resize);
1139 err = view_init_hsplit(v, v->child->begin_y);
1140 if (err)
1141 return err;
1142 v->child->resized_y = v->child->begin_y;
1143 } else {
1144 if (v->child->resized_x)
1145 v->child->begin_x = v->child->resized_x;
1146 if (view->parent)
1147 v->child->begin_x -= resize;
1148 else
1149 v->child->begin_x += resize;
1150 if (v->child->begin_x < 11) {
1151 view->count = 0;
1152 v->child->begin_x = 11;
1153 } else if (v->child->begin_x > COLS - 1) {
1154 view->count = 0;
1155 v->child->begin_x = COLS - 1;
1157 v->child->resized_x = v->child->begin_x;
1160 v->child->mode = v->mode;
1161 v->child->nlines = v->lines - v->child->begin_y;
1162 v->child->ncols = v->cols - v->child->begin_x;
1163 v->focus_child = 1;
1165 err = view_fullscreen(v);
1166 if (err)
1167 return err;
1168 err = view_splitscreen(v->child);
1169 if (err)
1170 return err;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1173 err = offset_selection_down(v->child);
1174 if (err)
1175 return err;
1178 if (v->resize)
1179 err = v->resize(v, 0);
1180 else if (v->child->resize)
1181 err = v->child->resize(v->child, 0);
1183 v->resized = v->child->resized = 0;
1185 return err;
1188 static void
1189 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1191 struct tog_view *v = src->child ? src->child : src;
1193 dst->resized_x = v->resized_x;
1194 dst->resized_y = v->resized_y;
1197 static const struct got_error *
1198 view_close_child(struct tog_view *view)
1200 const struct got_error *err = NULL;
1202 if (view->child == NULL)
1203 return NULL;
1205 err = view_close(view->child);
1206 view->child = NULL;
1207 return err;
1210 static const struct got_error *
1211 view_set_child(struct tog_view *view, struct tog_view *child)
1213 const struct got_error *err = NULL;
1215 view->child = child;
1216 child->parent = view;
1218 err = view_resize(view);
1219 if (err)
1220 return err;
1222 if (view->child->resized_x || view->child->resized_y)
1223 err = view_resize_split(view, 0);
1225 return err;
1228 static const struct got_error *view_dispatch_request(struct tog_view **,
1229 struct tog_view *, enum tog_view_type, int, int);
1231 static const struct got_error *
1232 view_request_new(struct tog_view **requested, struct tog_view *view,
1233 enum tog_view_type request)
1235 struct tog_view *new_view = NULL;
1236 const struct got_error *err;
1237 int y = 0, x = 0;
1239 *requested = NULL;
1241 if (view_is_parent_view(view) && request != TOG_VIEW_HELP)
1242 view_get_split(view, &y, &x);
1244 err = view_dispatch_request(&new_view, view, request, y, x);
1245 if (err)
1246 return err;
1248 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN &&
1249 request != TOG_VIEW_HELP) {
1250 err = view_init_hsplit(view, y);
1251 if (err)
1252 return err;
1255 view->focussed = 0;
1256 new_view->focussed = 1;
1257 new_view->mode = view->mode;
1258 new_view->nlines = request == TOG_VIEW_HELP ?
1259 view->lines : view->lines - y;
1261 if (view_is_parent_view(view) && request != TOG_VIEW_HELP) {
1262 view_transfer_size(new_view, view);
1263 err = view_close_child(view);
1264 if (err)
1265 return err;
1266 err = view_set_child(view, new_view);
1267 if (err)
1268 return err;
1269 view->focus_child = 1;
1270 } else
1271 *requested = new_view;
1273 return NULL;
1276 static void
1277 tog_resizeterm(void)
1279 int cols, lines;
1280 struct winsize size;
1282 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1283 cols = 80; /* Default */
1284 lines = 24;
1285 } else {
1286 cols = size.ws_col;
1287 lines = size.ws_row;
1289 resize_term(lines, cols);
1292 static const struct got_error *
1293 view_search_start(struct tog_view *view)
1295 const struct got_error *err = NULL;
1296 struct tog_view *v = view;
1297 char pattern[1024];
1298 int ret;
1300 if (view->search_started) {
1301 regfree(&view->regex);
1302 view->searching = 0;
1303 memset(&view->regmatch, 0, sizeof(view->regmatch));
1305 view->search_started = 0;
1307 if (view->nlines < 1)
1308 return NULL;
1310 if (view_is_hsplit_top(view))
1311 v = view->child;
1313 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1314 wclrtoeol(v->window);
1316 nodelay(view->window, FALSE); /* block for search term input */
1317 nocbreak();
1318 echo();
1319 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1320 wrefresh(v->window);
1321 cbreak();
1322 noecho();
1323 nodelay(view->window, TRUE);
1324 if (ret == ERR)
1325 return NULL;
1327 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1328 err = view->search_start(view);
1329 if (err) {
1330 regfree(&view->regex);
1331 return err;
1333 view->search_started = 1;
1334 view->searching = TOG_SEARCH_FORWARD;
1335 view->search_next_done = 0;
1336 view->search_next(view);
1339 return NULL;
1342 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1343 static const struct got_error *
1344 switch_split(struct tog_view *view)
1346 const struct got_error *err = NULL;
1347 struct tog_view *v = NULL;
1349 if (view->parent)
1350 v = view->parent;
1351 else
1352 v = view;
1354 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1355 v->mode = TOG_VIEW_SPLIT_VERT;
1356 else
1357 v->mode = TOG_VIEW_SPLIT_HRZN;
1359 if (!v->child)
1360 return NULL;
1361 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1362 v->mode = TOG_VIEW_SPLIT_NONE;
1364 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1365 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1366 v->child->begin_y = v->child->resized_y;
1367 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1368 v->child->begin_x = v->child->resized_x;
1371 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1372 v->ncols = COLS;
1373 v->child->ncols = COLS;
1374 v->child->nscrolled = LINES - v->child->nlines;
1376 err = view_init_hsplit(v, v->child->begin_y);
1377 if (err)
1378 return err;
1380 v->child->mode = v->mode;
1381 v->child->nlines = v->lines - v->child->begin_y;
1382 v->focus_child = 1;
1384 err = view_fullscreen(v);
1385 if (err)
1386 return err;
1387 err = view_splitscreen(v->child);
1388 if (err)
1389 return err;
1391 if (v->mode == TOG_VIEW_SPLIT_NONE)
1392 v->mode = TOG_VIEW_SPLIT_VERT;
1393 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1394 err = offset_selection_down(v);
1395 if (err)
1396 return err;
1397 err = offset_selection_down(v->child);
1398 if (err)
1399 return err;
1400 } else {
1401 offset_selection_up(v);
1402 offset_selection_up(v->child);
1404 if (v->resize)
1405 err = v->resize(v, 0);
1406 else if (v->child->resize)
1407 err = v->child->resize(v->child, 0);
1409 return err;
1413 * Compute view->count from numeric input. Assign total to view->count and
1414 * return first non-numeric key entered.
1416 static int
1417 get_compound_key(struct tog_view *view, int c)
1419 struct tog_view *v = view;
1420 int x, n = 0;
1422 if (view_is_hsplit_top(view))
1423 v = view->child;
1424 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1425 v = view->parent;
1427 view->count = 0;
1428 cbreak(); /* block for input */
1429 nodelay(view->window, FALSE);
1430 wmove(v->window, v->nlines - 1, 0);
1431 wclrtoeol(v->window);
1432 waddch(v->window, ':');
1434 do {
1435 x = getcurx(v->window);
1436 if (x != ERR && x < view->ncols) {
1437 waddch(v->window, c);
1438 wrefresh(v->window);
1442 * Don't overflow. Max valid request should be the greatest
1443 * between the longest and total lines; cap at 10 million.
1445 if (n >= 9999999)
1446 n = 9999999;
1447 else
1448 n = n * 10 + (c - '0');
1449 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1451 if (c == 'G' || c == 'g') { /* nG key map */
1452 view->gline = view->hiline = n;
1453 n = 0;
1454 c = 0;
1457 /* Massage excessive or inapplicable values at the input handler. */
1458 view->count = n;
1460 return c;
1463 static const struct got_error *
1464 view_input(struct tog_view **new, int *done, struct tog_view *view,
1465 struct tog_view_list_head *views)
1467 const struct got_error *err = NULL;
1468 struct tog_view *v;
1469 int ch, errcode;
1471 *new = NULL;
1473 /* Clear "no matches" indicator. */
1474 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1475 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1476 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1477 view->count = 0;
1480 if (view->searching && !view->search_next_done) {
1481 errcode = pthread_mutex_unlock(&tog_mutex);
1482 if (errcode)
1483 return got_error_set_errno(errcode,
1484 "pthread_mutex_unlock");
1485 sched_yield();
1486 errcode = pthread_mutex_lock(&tog_mutex);
1487 if (errcode)
1488 return got_error_set_errno(errcode,
1489 "pthread_mutex_lock");
1490 view->search_next(view);
1491 return NULL;
1494 /* Allow threads to make progress while we are waiting for input. */
1495 errcode = pthread_mutex_unlock(&tog_mutex);
1496 if (errcode)
1497 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1498 /* If we have an unfinished count, let C-g or backspace abort. */
1499 if (view->count && --view->count) {
1500 cbreak();
1501 nodelay(view->window, TRUE);
1502 ch = wgetch(view->window);
1503 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1504 view->count = 0;
1505 else
1506 ch = view->ch;
1507 } else {
1508 ch = wgetch(view->window);
1509 if (ch >= '1' && ch <= '9')
1510 view->ch = ch = get_compound_key(view, ch);
1512 if (view->hiline && ch != ERR && ch != 0)
1513 view->hiline = 0; /* key pressed, clear line highlight */
1514 nodelay(view->window, TRUE);
1515 errcode = pthread_mutex_lock(&tog_mutex);
1516 if (errcode)
1517 return got_error_set_errno(errcode, "pthread_mutex_lock");
1519 if (tog_sigwinch_received || tog_sigcont_received) {
1520 tog_resizeterm();
1521 tog_sigwinch_received = 0;
1522 tog_sigcont_received = 0;
1523 TAILQ_FOREACH(v, views, entry) {
1524 err = view_resize(v);
1525 if (err)
1526 return err;
1527 err = v->input(new, v, KEY_RESIZE);
1528 if (err)
1529 return err;
1530 if (v->child) {
1531 err = view_resize(v->child);
1532 if (err)
1533 return err;
1534 err = v->child->input(new, v->child,
1535 KEY_RESIZE);
1536 if (err)
1537 return err;
1538 if (v->child->resized_x || v->child->resized_y) {
1539 err = view_resize_split(v, 0);
1540 if (err)
1541 return err;
1547 switch (ch) {
1548 case '?':
1549 case 'H':
1550 case KEY_F(1):
1551 if (view->type == TOG_VIEW_HELP)
1552 err = view->reset(view);
1553 else
1554 err = view_request_new(new, view, TOG_VIEW_HELP);
1555 break;
1556 case '\t':
1557 view->count = 0;
1558 if (view->child) {
1559 view->focussed = 0;
1560 view->child->focussed = 1;
1561 view->focus_child = 1;
1562 } else if (view->parent) {
1563 view->focussed = 0;
1564 view->parent->focussed = 1;
1565 view->parent->focus_child = 0;
1566 if (!view_is_splitscreen(view)) {
1567 if (view->parent->resize) {
1568 err = view->parent->resize(view->parent,
1569 0);
1570 if (err)
1571 return err;
1573 offset_selection_up(view->parent);
1574 err = view_fullscreen(view->parent);
1575 if (err)
1576 return err;
1579 break;
1580 case 'q':
1581 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1582 if (view->parent->resize) {
1583 /* might need more commits to fill fullscreen */
1584 err = view->parent->resize(view->parent, 0);
1585 if (err)
1586 break;
1588 offset_selection_up(view->parent);
1590 err = view->input(new, view, ch);
1591 view->dying = 1;
1592 break;
1593 case 'Q':
1594 *done = 1;
1595 break;
1596 case 'F':
1597 view->count = 0;
1598 if (view_is_parent_view(view)) {
1599 if (view->child == NULL)
1600 break;
1601 if (view_is_splitscreen(view->child)) {
1602 view->focussed = 0;
1603 view->child->focussed = 1;
1604 err = view_fullscreen(view->child);
1605 } else {
1606 err = view_splitscreen(view->child);
1607 if (!err)
1608 err = view_resize_split(view, 0);
1610 if (err)
1611 break;
1612 err = view->child->input(new, view->child,
1613 KEY_RESIZE);
1614 } else {
1615 if (view_is_splitscreen(view)) {
1616 view->parent->focussed = 0;
1617 view->focussed = 1;
1618 err = view_fullscreen(view);
1619 } else {
1620 err = view_splitscreen(view);
1621 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1622 err = view_resize(view->parent);
1623 if (!err)
1624 err = view_resize_split(view, 0);
1626 if (err)
1627 break;
1628 err = view->input(new, view, KEY_RESIZE);
1630 if (err)
1631 break;
1632 if (view->resize) {
1633 err = view->resize(view, 0);
1634 if (err)
1635 break;
1637 if (view->parent)
1638 err = offset_selection_down(view->parent);
1639 if (!err)
1640 err = offset_selection_down(view);
1641 break;
1642 case 'S':
1643 view->count = 0;
1644 err = switch_split(view);
1645 break;
1646 case '-':
1647 err = view_resize_split(view, -1);
1648 break;
1649 case '+':
1650 err = view_resize_split(view, 1);
1651 break;
1652 case KEY_RESIZE:
1653 break;
1654 case '/':
1655 view->count = 0;
1656 if (view->search_start)
1657 view_search_start(view);
1658 else
1659 err = view->input(new, view, ch);
1660 break;
1661 case 'N':
1662 case 'n':
1663 if (view->search_started && view->search_next) {
1664 view->searching = (ch == 'n' ?
1665 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1666 view->search_next_done = 0;
1667 view->search_next(view);
1668 } else
1669 err = view->input(new, view, ch);
1670 break;
1671 case 'A':
1672 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1673 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1674 else
1675 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1676 TAILQ_FOREACH(v, views, entry) {
1677 if (v->reset) {
1678 err = v->reset(v);
1679 if (err)
1680 return err;
1682 if (v->child && v->child->reset) {
1683 err = v->child->reset(v->child);
1684 if (err)
1685 return err;
1688 break;
1689 default:
1690 err = view->input(new, view, ch);
1691 break;
1694 return err;
1697 static int
1698 view_needs_focus_indication(struct tog_view *view)
1700 if (view_is_parent_view(view)) {
1701 if (view->child == NULL || view->child->focussed)
1702 return 0;
1703 if (!view_is_splitscreen(view->child))
1704 return 0;
1705 } else if (!view_is_splitscreen(view))
1706 return 0;
1708 return view->focussed;
1711 static const struct got_error *
1712 view_loop(struct tog_view *view)
1714 const struct got_error *err = NULL;
1715 struct tog_view_list_head views;
1716 struct tog_view *new_view;
1717 char *mode;
1718 int fast_refresh = 10;
1719 int done = 0, errcode;
1721 mode = getenv("TOG_VIEW_SPLIT_MODE");
1722 if (!mode || !(*mode == 'h' || *mode == 'H'))
1723 view->mode = TOG_VIEW_SPLIT_VERT;
1724 else
1725 view->mode = TOG_VIEW_SPLIT_HRZN;
1727 errcode = pthread_mutex_lock(&tog_mutex);
1728 if (errcode)
1729 return got_error_set_errno(errcode, "pthread_mutex_lock");
1731 TAILQ_INIT(&views);
1732 TAILQ_INSERT_HEAD(&views, view, entry);
1734 view->focussed = 1;
1735 err = view->show(view);
1736 if (err)
1737 return err;
1738 update_panels();
1739 doupdate();
1740 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1741 !tog_fatal_signal_received()) {
1742 /* Refresh fast during initialization, then become slower. */
1743 if (fast_refresh && fast_refresh-- == 0)
1744 halfdelay(10); /* switch to once per second */
1746 err = view_input(&new_view, &done, view, &views);
1747 if (err)
1748 break;
1749 if (view->dying) {
1750 struct tog_view *v, *prev = NULL;
1752 if (view_is_parent_view(view))
1753 prev = TAILQ_PREV(view, tog_view_list_head,
1754 entry);
1755 else if (view->parent)
1756 prev = view->parent;
1758 if (view->parent) {
1759 view->parent->child = NULL;
1760 view->parent->focus_child = 0;
1761 /* Restore fullscreen line height. */
1762 view->parent->nlines = view->parent->lines;
1763 err = view_resize(view->parent);
1764 if (err)
1765 break;
1766 /* Make resized splits persist. */
1767 view_transfer_size(view->parent, view);
1768 } else
1769 TAILQ_REMOVE(&views, view, entry);
1771 err = view_close(view);
1772 if (err)
1773 goto done;
1775 view = NULL;
1776 TAILQ_FOREACH(v, &views, entry) {
1777 if (v->focussed)
1778 break;
1780 if (view == NULL && new_view == NULL) {
1781 /* No view has focus. Try to pick one. */
1782 if (prev)
1783 view = prev;
1784 else if (!TAILQ_EMPTY(&views)) {
1785 view = TAILQ_LAST(&views,
1786 tog_view_list_head);
1788 if (view) {
1789 if (view->focus_child) {
1790 view->child->focussed = 1;
1791 view = view->child;
1792 } else
1793 view->focussed = 1;
1797 if (new_view) {
1798 struct tog_view *v, *t;
1799 /* Only allow one parent view per type. */
1800 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1801 if (v->type != new_view->type)
1802 continue;
1803 TAILQ_REMOVE(&views, v, entry);
1804 err = view_close(v);
1805 if (err)
1806 goto done;
1807 break;
1809 TAILQ_INSERT_TAIL(&views, new_view, entry);
1810 view = new_view;
1812 if (view) {
1813 if (view_is_parent_view(view)) {
1814 if (view->child && view->child->focussed)
1815 view = view->child;
1816 } else {
1817 if (view->parent && view->parent->focussed)
1818 view = view->parent;
1820 show_panel(view->panel);
1821 if (view->child && view_is_splitscreen(view->child))
1822 show_panel(view->child->panel);
1823 if (view->parent && view_is_splitscreen(view)) {
1824 err = view->parent->show(view->parent);
1825 if (err)
1826 goto done;
1828 err = view->show(view);
1829 if (err)
1830 goto done;
1831 if (view->child) {
1832 err = view->child->show(view->child);
1833 if (err)
1834 goto done;
1836 update_panels();
1837 doupdate();
1840 done:
1841 while (!TAILQ_EMPTY(&views)) {
1842 const struct got_error *close_err;
1843 view = TAILQ_FIRST(&views);
1844 TAILQ_REMOVE(&views, view, entry);
1845 close_err = view_close(view);
1846 if (close_err && err == NULL)
1847 err = close_err;
1850 errcode = pthread_mutex_unlock(&tog_mutex);
1851 if (errcode && err == NULL)
1852 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1854 return err;
1857 __dead static void
1858 usage_log(void)
1860 endwin();
1861 fprintf(stderr,
1862 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1863 getprogname());
1864 exit(1);
1867 /* Create newly allocated wide-character string equivalent to a byte string. */
1868 static const struct got_error *
1869 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1871 char *vis = NULL;
1872 const struct got_error *err = NULL;
1874 *ws = NULL;
1875 *wlen = mbstowcs(NULL, s, 0);
1876 if (*wlen == (size_t)-1) {
1877 int vislen;
1878 if (errno != EILSEQ)
1879 return got_error_from_errno("mbstowcs");
1881 /* byte string invalid in current encoding; try to "fix" it */
1882 err = got_mbsavis(&vis, &vislen, s);
1883 if (err)
1884 return err;
1885 *wlen = mbstowcs(NULL, vis, 0);
1886 if (*wlen == (size_t)-1) {
1887 err = got_error_from_errno("mbstowcs"); /* give up */
1888 goto done;
1892 *ws = calloc(*wlen + 1, sizeof(**ws));
1893 if (*ws == NULL) {
1894 err = got_error_from_errno("calloc");
1895 goto done;
1898 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1899 err = got_error_from_errno("mbstowcs");
1900 done:
1901 free(vis);
1902 if (err) {
1903 free(*ws);
1904 *ws = NULL;
1905 *wlen = 0;
1907 return err;
1910 static const struct got_error *
1911 expand_tab(char **ptr, const char *src)
1913 char *dst;
1914 size_t len, n, idx = 0, sz = 0;
1916 *ptr = NULL;
1917 n = len = strlen(src);
1918 dst = malloc(n + 1);
1919 if (dst == NULL)
1920 return got_error_from_errno("malloc");
1922 while (idx < len && src[idx]) {
1923 const char c = src[idx];
1925 if (c == '\t') {
1926 size_t nb = TABSIZE - sz % TABSIZE;
1927 char *p;
1929 p = realloc(dst, n + nb);
1930 if (p == NULL) {
1931 free(dst);
1932 return got_error_from_errno("realloc");
1935 dst = p;
1936 n += nb;
1937 memset(dst + sz, ' ', nb);
1938 sz += nb;
1939 } else
1940 dst[sz++] = src[idx];
1941 ++idx;
1944 dst[sz] = '\0';
1945 *ptr = dst;
1946 return NULL;
1950 * Advance at most n columns from wline starting at offset off.
1951 * Return the index to the first character after the span operation.
1952 * Return the combined column width of all spanned wide character in
1953 * *rcol.
1955 static int
1956 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1958 int width, i, cols = 0;
1960 if (n == 0) {
1961 *rcol = cols;
1962 return off;
1965 for (i = off; wline[i] != L'\0'; ++i) {
1966 if (wline[i] == L'\t')
1967 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1968 else
1969 width = wcwidth(wline[i]);
1971 if (width == -1) {
1972 width = 1;
1973 wline[i] = L'.';
1976 if (cols + width > n)
1977 break;
1978 cols += width;
1981 *rcol = cols;
1982 return i;
1986 * Format a line for display, ensuring that it won't overflow a width limit.
1987 * With scrolling, the width returned refers to the scrolled version of the
1988 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1990 static const struct got_error *
1991 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1992 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1994 const struct got_error *err = NULL;
1995 int cols;
1996 wchar_t *wline = NULL;
1997 char *exstr = NULL;
1998 size_t wlen;
1999 int i, scrollx;
2001 *wlinep = NULL;
2002 *widthp = 0;
2004 if (expand) {
2005 err = expand_tab(&exstr, line);
2006 if (err)
2007 return err;
2010 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2011 free(exstr);
2012 if (err)
2013 return err;
2015 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2017 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2018 wline[wlen - 1] = L'\0';
2019 wlen--;
2021 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2022 wline[wlen - 1] = L'\0';
2023 wlen--;
2026 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2027 wline[i] = L'\0';
2029 if (widthp)
2030 *widthp = cols;
2031 if (scrollxp)
2032 *scrollxp = scrollx;
2033 if (err)
2034 free(wline);
2035 else
2036 *wlinep = wline;
2037 return err;
2040 static const struct got_error*
2041 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2042 struct got_object_id *id, struct got_repository *repo)
2044 static const struct got_error *err = NULL;
2045 struct got_reflist_entry *re;
2046 char *s;
2047 const char *name;
2049 *refs_str = NULL;
2051 TAILQ_FOREACH(re, refs, entry) {
2052 struct got_tag_object *tag = NULL;
2053 struct got_object_id *ref_id;
2054 int cmp;
2056 name = got_ref_get_name(re->ref);
2057 if (strcmp(name, GOT_REF_HEAD) == 0)
2058 continue;
2059 if (strncmp(name, "refs/", 5) == 0)
2060 name += 5;
2061 if (strncmp(name, "got/", 4) == 0 &&
2062 strncmp(name, "got/backup/", 11) != 0)
2063 continue;
2064 if (strncmp(name, "heads/", 6) == 0)
2065 name += 6;
2066 if (strncmp(name, "remotes/", 8) == 0) {
2067 name += 8;
2068 s = strstr(name, "/" GOT_REF_HEAD);
2069 if (s != NULL && s[strlen(s)] == '\0')
2070 continue;
2072 err = got_ref_resolve(&ref_id, repo, re->ref);
2073 if (err)
2074 break;
2075 if (strncmp(name, "tags/", 5) == 0) {
2076 err = got_object_open_as_tag(&tag, repo, ref_id);
2077 if (err) {
2078 if (err->code != GOT_ERR_OBJ_TYPE) {
2079 free(ref_id);
2080 break;
2082 /* Ref points at something other than a tag. */
2083 err = NULL;
2084 tag = NULL;
2087 cmp = got_object_id_cmp(tag ?
2088 got_object_tag_get_object_id(tag) : ref_id, id);
2089 free(ref_id);
2090 if (tag)
2091 got_object_tag_close(tag);
2092 if (cmp != 0)
2093 continue;
2094 s = *refs_str;
2095 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2096 s ? ", " : "", name) == -1) {
2097 err = got_error_from_errno("asprintf");
2098 free(s);
2099 *refs_str = NULL;
2100 break;
2102 free(s);
2105 return err;
2108 static const struct got_error *
2109 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2110 int col_tab_align)
2112 char *smallerthan;
2114 smallerthan = strchr(author, '<');
2115 if (smallerthan && smallerthan[1] != '\0')
2116 author = smallerthan + 1;
2117 author[strcspn(author, "@>")] = '\0';
2118 return format_line(wauthor, author_width, NULL, author, 0, limit,
2119 col_tab_align, 0);
2122 static const struct got_error *
2123 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2124 struct got_object_id *id, const size_t date_display_cols,
2125 int author_display_cols)
2127 struct tog_log_view_state *s = &view->state.log;
2128 const struct got_error *err = NULL;
2129 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2130 char *logmsg0 = NULL, *logmsg = NULL;
2131 char *author = NULL;
2132 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2133 int author_width, logmsg_width;
2134 char *newline, *line = NULL;
2135 int col, limit, scrollx;
2136 const int avail = view->ncols;
2137 struct tm tm;
2138 time_t committer_time;
2139 struct tog_color *tc;
2141 committer_time = got_object_commit_get_committer_time(commit);
2142 if (gmtime_r(&committer_time, &tm) == NULL)
2143 return got_error_from_errno("gmtime_r");
2144 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2145 return got_error(GOT_ERR_NO_SPACE);
2147 if (avail <= date_display_cols)
2148 limit = MIN(sizeof(datebuf) - 1, avail);
2149 else
2150 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2151 tc = get_color(&s->colors, TOG_COLOR_DATE);
2152 if (tc)
2153 wattr_on(view->window,
2154 COLOR_PAIR(tc->colorpair), NULL);
2155 waddnstr(view->window, datebuf, limit);
2156 if (tc)
2157 wattr_off(view->window,
2158 COLOR_PAIR(tc->colorpair), NULL);
2159 col = limit;
2160 if (col > avail)
2161 goto done;
2163 if (avail >= 120) {
2164 char *id_str;
2165 err = got_object_id_str(&id_str, id);
2166 if (err)
2167 goto done;
2168 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2169 if (tc)
2170 wattr_on(view->window,
2171 COLOR_PAIR(tc->colorpair), NULL);
2172 wprintw(view->window, "%.8s ", id_str);
2173 if (tc)
2174 wattr_off(view->window,
2175 COLOR_PAIR(tc->colorpair), NULL);
2176 free(id_str);
2177 col += 9;
2178 if (col > avail)
2179 goto done;
2182 if (s->use_committer)
2183 author = strdup(got_object_commit_get_committer(commit));
2184 else
2185 author = strdup(got_object_commit_get_author(commit));
2186 if (author == NULL) {
2187 err = got_error_from_errno("strdup");
2188 goto done;
2190 err = format_author(&wauthor, &author_width, author, avail - col, col);
2191 if (err)
2192 goto done;
2193 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2194 if (tc)
2195 wattr_on(view->window,
2196 COLOR_PAIR(tc->colorpair), NULL);
2197 waddwstr(view->window, wauthor);
2198 col += author_width;
2199 while (col < avail && author_width < author_display_cols + 2) {
2200 waddch(view->window, ' ');
2201 col++;
2202 author_width++;
2204 if (tc)
2205 wattr_off(view->window,
2206 COLOR_PAIR(tc->colorpair), NULL);
2207 if (col > avail)
2208 goto done;
2210 err = got_object_commit_get_logmsg(&logmsg0, commit);
2211 if (err)
2212 goto done;
2213 logmsg = logmsg0;
2214 while (*logmsg == '\n')
2215 logmsg++;
2216 newline = strchr(logmsg, '\n');
2217 if (newline)
2218 *newline = '\0';
2219 limit = avail - col;
2220 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2221 limit--; /* for the border */
2222 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2223 limit, col, 1);
2224 if (err)
2225 goto done;
2226 waddwstr(view->window, &wlogmsg[scrollx]);
2227 col += MAX(logmsg_width, 0);
2228 while (col < avail) {
2229 waddch(view->window, ' ');
2230 col++;
2232 done:
2233 free(logmsg0);
2234 free(wlogmsg);
2235 free(author);
2236 free(wauthor);
2237 free(line);
2238 return err;
2241 static struct commit_queue_entry *
2242 alloc_commit_queue_entry(struct got_commit_object *commit,
2243 struct got_object_id *id)
2245 struct commit_queue_entry *entry;
2246 struct got_object_id *dup;
2248 entry = calloc(1, sizeof(*entry));
2249 if (entry == NULL)
2250 return NULL;
2252 dup = got_object_id_dup(id);
2253 if (dup == NULL) {
2254 free(entry);
2255 return NULL;
2258 entry->id = dup;
2259 entry->commit = commit;
2260 return entry;
2263 static void
2264 pop_commit(struct commit_queue *commits)
2266 struct commit_queue_entry *entry;
2268 entry = TAILQ_FIRST(&commits->head);
2269 TAILQ_REMOVE(&commits->head, entry, entry);
2270 got_object_commit_close(entry->commit);
2271 commits->ncommits--;
2272 free(entry->id);
2273 free(entry);
2276 static void
2277 free_commits(struct commit_queue *commits)
2279 while (!TAILQ_EMPTY(&commits->head))
2280 pop_commit(commits);
2283 static const struct got_error *
2284 match_commit(int *have_match, struct got_object_id *id,
2285 struct got_commit_object *commit, regex_t *regex)
2287 const struct got_error *err = NULL;
2288 regmatch_t regmatch;
2289 char *id_str = NULL, *logmsg = NULL;
2291 *have_match = 0;
2293 err = got_object_id_str(&id_str, id);
2294 if (err)
2295 return err;
2297 err = got_object_commit_get_logmsg(&logmsg, commit);
2298 if (err)
2299 goto done;
2301 if (regexec(regex, got_object_commit_get_author(commit), 1,
2302 &regmatch, 0) == 0 ||
2303 regexec(regex, got_object_commit_get_committer(commit), 1,
2304 &regmatch, 0) == 0 ||
2305 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2306 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2307 *have_match = 1;
2308 done:
2309 free(id_str);
2310 free(logmsg);
2311 return err;
2314 static const struct got_error *
2315 queue_commits(struct tog_log_thread_args *a)
2317 const struct got_error *err = NULL;
2320 * We keep all commits open throughout the lifetime of the log
2321 * view in order to avoid having to re-fetch commits from disk
2322 * while updating the display.
2324 do {
2325 struct got_object_id id;
2326 struct got_commit_object *commit;
2327 struct commit_queue_entry *entry;
2328 int limit_match = 0;
2329 int errcode;
2331 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2332 NULL, NULL);
2333 if (err)
2334 break;
2336 err = got_object_open_as_commit(&commit, a->repo, &id);
2337 if (err)
2338 break;
2339 entry = alloc_commit_queue_entry(commit, &id);
2340 if (entry == NULL) {
2341 err = got_error_from_errno("alloc_commit_queue_entry");
2342 break;
2345 errcode = pthread_mutex_lock(&tog_mutex);
2346 if (errcode) {
2347 err = got_error_set_errno(errcode,
2348 "pthread_mutex_lock");
2349 break;
2352 entry->idx = a->real_commits->ncommits;
2353 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2354 a->real_commits->ncommits++;
2356 if (*a->limiting) {
2357 err = match_commit(&limit_match, &id, commit,
2358 a->limit_regex);
2359 if (err)
2360 break;
2362 if (limit_match) {
2363 struct commit_queue_entry *matched;
2365 matched = alloc_commit_queue_entry(
2366 entry->commit, entry->id);
2367 if (matched == NULL) {
2368 err = got_error_from_errno(
2369 "alloc_commit_queue_entry");
2370 break;
2372 matched->commit = entry->commit;
2373 got_object_commit_retain(entry->commit);
2375 matched->idx = a->limit_commits->ncommits;
2376 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2377 matched, entry);
2378 a->limit_commits->ncommits++;
2382 * This is how we signal log_thread() that we
2383 * have found a match, and that it should be
2384 * counted as a new entry for the view.
2386 a->limit_match = limit_match;
2389 if (*a->searching == TOG_SEARCH_FORWARD &&
2390 !*a->search_next_done) {
2391 int have_match;
2392 err = match_commit(&have_match, &id, commit, a->regex);
2393 if (err)
2394 break;
2396 if (*a->limiting) {
2397 if (limit_match && have_match)
2398 *a->search_next_done =
2399 TOG_SEARCH_HAVE_MORE;
2400 } else if (have_match)
2401 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2404 errcode = pthread_mutex_unlock(&tog_mutex);
2405 if (errcode && err == NULL)
2406 err = got_error_set_errno(errcode,
2407 "pthread_mutex_unlock");
2408 if (err)
2409 break;
2410 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2412 return err;
2415 static void
2416 select_commit(struct tog_log_view_state *s)
2418 struct commit_queue_entry *entry;
2419 int ncommits = 0;
2421 entry = s->first_displayed_entry;
2422 while (entry) {
2423 if (ncommits == s->selected) {
2424 s->selected_entry = entry;
2425 break;
2427 entry = TAILQ_NEXT(entry, entry);
2428 ncommits++;
2432 static const struct got_error *
2433 draw_commits(struct tog_view *view)
2435 const struct got_error *err = NULL;
2436 struct tog_log_view_state *s = &view->state.log;
2437 struct commit_queue_entry *entry = s->selected_entry;
2438 int limit = view->nlines;
2439 int width;
2440 int ncommits, author_cols = 4;
2441 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2442 char *refs_str = NULL;
2443 wchar_t *wline;
2444 struct tog_color *tc;
2445 static const size_t date_display_cols = 12;
2447 if (view_is_hsplit_top(view))
2448 --limit; /* account for border */
2450 if (s->selected_entry &&
2451 !(view->searching && view->search_next_done == 0)) {
2452 struct got_reflist_head *refs;
2453 err = got_object_id_str(&id_str, s->selected_entry->id);
2454 if (err)
2455 return err;
2456 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2457 s->selected_entry->id);
2458 if (refs) {
2459 err = build_refs_str(&refs_str, refs,
2460 s->selected_entry->id, s->repo);
2461 if (err)
2462 goto done;
2466 if (s->thread_args.commits_needed == 0)
2467 halfdelay(10); /* disable fast refresh */
2469 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2470 if (asprintf(&ncommits_str, " [%d/%d] %s",
2471 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2472 (view->searching && !view->search_next_done) ?
2473 "searching..." : "loading...") == -1) {
2474 err = got_error_from_errno("asprintf");
2475 goto done;
2477 } else {
2478 const char *search_str = NULL;
2479 const char *limit_str = NULL;
2481 if (view->searching) {
2482 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2483 search_str = "no more matches";
2484 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2485 search_str = "no matches found";
2486 else if (!view->search_next_done)
2487 search_str = "searching...";
2490 if (s->limit_view && s->commits->ncommits == 0)
2491 limit_str = "no matches found";
2493 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2494 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2495 search_str ? search_str : (refs_str ? refs_str : ""),
2496 limit_str ? limit_str : "") == -1) {
2497 err = got_error_from_errno("asprintf");
2498 goto done;
2502 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2503 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2504 "........................................",
2505 s->in_repo_path, ncommits_str) == -1) {
2506 err = got_error_from_errno("asprintf");
2507 header = NULL;
2508 goto done;
2510 } else if (asprintf(&header, "commit %s%s",
2511 id_str ? id_str : "........................................",
2512 ncommits_str) == -1) {
2513 err = got_error_from_errno("asprintf");
2514 header = NULL;
2515 goto done;
2517 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2518 if (err)
2519 goto done;
2521 werase(view->window);
2523 if (view_needs_focus_indication(view))
2524 wstandout(view->window);
2525 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2526 if (tc)
2527 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2528 waddwstr(view->window, wline);
2529 while (width < view->ncols) {
2530 waddch(view->window, ' ');
2531 width++;
2533 if (tc)
2534 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2535 if (view_needs_focus_indication(view))
2536 wstandend(view->window);
2537 free(wline);
2538 if (limit <= 1)
2539 goto done;
2541 /* Grow author column size if necessary, and set view->maxx. */
2542 entry = s->first_displayed_entry;
2543 ncommits = 0;
2544 view->maxx = 0;
2545 while (entry) {
2546 struct got_commit_object *c = entry->commit;
2547 char *author, *eol, *msg, *msg0;
2548 wchar_t *wauthor, *wmsg;
2549 int width;
2550 if (ncommits >= limit - 1)
2551 break;
2552 if (s->use_committer)
2553 author = strdup(got_object_commit_get_committer(c));
2554 else
2555 author = strdup(got_object_commit_get_author(c));
2556 if (author == NULL) {
2557 err = got_error_from_errno("strdup");
2558 goto done;
2560 err = format_author(&wauthor, &width, author, COLS,
2561 date_display_cols);
2562 if (author_cols < width)
2563 author_cols = width;
2564 free(wauthor);
2565 free(author);
2566 if (err)
2567 goto done;
2568 err = got_object_commit_get_logmsg(&msg0, c);
2569 if (err)
2570 goto done;
2571 msg = msg0;
2572 while (*msg == '\n')
2573 ++msg;
2574 if ((eol = strchr(msg, '\n')))
2575 *eol = '\0';
2576 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2577 date_display_cols + author_cols, 0);
2578 if (err)
2579 goto done;
2580 view->maxx = MAX(view->maxx, width);
2581 free(msg0);
2582 free(wmsg);
2583 ncommits++;
2584 entry = TAILQ_NEXT(entry, entry);
2587 entry = s->first_displayed_entry;
2588 s->last_displayed_entry = s->first_displayed_entry;
2589 ncommits = 0;
2590 while (entry) {
2591 if (ncommits >= limit - 1)
2592 break;
2593 if (ncommits == s->selected)
2594 wstandout(view->window);
2595 err = draw_commit(view, entry->commit, entry->id,
2596 date_display_cols, author_cols);
2597 if (ncommits == s->selected)
2598 wstandend(view->window);
2599 if (err)
2600 goto done;
2601 ncommits++;
2602 s->last_displayed_entry = entry;
2603 entry = TAILQ_NEXT(entry, entry);
2606 view_border(view);
2607 done:
2608 free(id_str);
2609 free(refs_str);
2610 free(ncommits_str);
2611 free(header);
2612 return err;
2615 static void
2616 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2618 struct commit_queue_entry *entry;
2619 int nscrolled = 0;
2621 entry = TAILQ_FIRST(&s->commits->head);
2622 if (s->first_displayed_entry == entry)
2623 return;
2625 entry = s->first_displayed_entry;
2626 while (entry && nscrolled < maxscroll) {
2627 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2628 if (entry) {
2629 s->first_displayed_entry = entry;
2630 nscrolled++;
2635 static const struct got_error *
2636 trigger_log_thread(struct tog_view *view, int wait)
2638 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2639 int errcode;
2641 halfdelay(1); /* fast refresh while loading commits */
2643 while (!ta->log_complete && !tog_thread_error &&
2644 (ta->commits_needed > 0 || ta->load_all)) {
2645 /* Wake the log thread. */
2646 errcode = pthread_cond_signal(&ta->need_commits);
2647 if (errcode)
2648 return got_error_set_errno(errcode,
2649 "pthread_cond_signal");
2652 * The mutex will be released while the view loop waits
2653 * in wgetch(), at which time the log thread will run.
2655 if (!wait)
2656 break;
2658 /* Display progress update in log view. */
2659 show_log_view(view);
2660 update_panels();
2661 doupdate();
2663 /* Wait right here while next commit is being loaded. */
2664 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2665 if (errcode)
2666 return got_error_set_errno(errcode,
2667 "pthread_cond_wait");
2669 /* Display progress update in log view. */
2670 show_log_view(view);
2671 update_panels();
2672 doupdate();
2675 return NULL;
2678 static const struct got_error *
2679 request_log_commits(struct tog_view *view)
2681 struct tog_log_view_state *state = &view->state.log;
2682 const struct got_error *err = NULL;
2684 if (state->thread_args.log_complete)
2685 return NULL;
2687 state->thread_args.commits_needed += view->nscrolled;
2688 err = trigger_log_thread(view, 1);
2689 view->nscrolled = 0;
2691 return err;
2694 static const struct got_error *
2695 log_scroll_down(struct tog_view *view, int maxscroll)
2697 struct tog_log_view_state *s = &view->state.log;
2698 const struct got_error *err = NULL;
2699 struct commit_queue_entry *pentry;
2700 int nscrolled = 0, ncommits_needed;
2702 if (s->last_displayed_entry == NULL)
2703 return NULL;
2705 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2706 if (s->commits->ncommits < ncommits_needed &&
2707 !s->thread_args.log_complete) {
2709 * Ask the log thread for required amount of commits.
2711 s->thread_args.commits_needed +=
2712 ncommits_needed - s->commits->ncommits;
2713 err = trigger_log_thread(view, 1);
2714 if (err)
2715 return err;
2718 do {
2719 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2720 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2721 break;
2723 s->last_displayed_entry = pentry ?
2724 pentry : s->last_displayed_entry;;
2726 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2727 if (pentry == NULL)
2728 break;
2729 s->first_displayed_entry = pentry;
2730 } while (++nscrolled < maxscroll);
2732 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2733 view->nscrolled += nscrolled;
2734 else
2735 view->nscrolled = 0;
2737 return err;
2740 static const struct got_error *
2741 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2742 struct got_commit_object *commit, struct got_object_id *commit_id,
2743 struct tog_view *log_view, struct got_repository *repo)
2745 const struct got_error *err;
2746 struct got_object_qid *parent_id;
2747 struct tog_view *diff_view;
2749 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2750 if (diff_view == NULL)
2751 return got_error_from_errno("view_open");
2753 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2754 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2755 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2756 if (err == NULL)
2757 *new_view = diff_view;
2758 return err;
2761 static const struct got_error *
2762 tree_view_visit_subtree(struct tog_tree_view_state *s,
2763 struct got_tree_object *subtree)
2765 struct tog_parent_tree *parent;
2767 parent = calloc(1, sizeof(*parent));
2768 if (parent == NULL)
2769 return got_error_from_errno("calloc");
2771 parent->tree = s->tree;
2772 parent->first_displayed_entry = s->first_displayed_entry;
2773 parent->selected_entry = s->selected_entry;
2774 parent->selected = s->selected;
2775 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2776 s->tree = subtree;
2777 s->selected = 0;
2778 s->first_displayed_entry = NULL;
2779 return NULL;
2782 static const struct got_error *
2783 tree_view_walk_path(struct tog_tree_view_state *s,
2784 struct got_commit_object *commit, const char *path)
2786 const struct got_error *err = NULL;
2787 struct got_tree_object *tree = NULL;
2788 const char *p;
2789 char *slash, *subpath = NULL;
2791 /* Walk the path and open corresponding tree objects. */
2792 p = path;
2793 while (*p) {
2794 struct got_tree_entry *te;
2795 struct got_object_id *tree_id;
2796 char *te_name;
2798 while (p[0] == '/')
2799 p++;
2801 /* Ensure the correct subtree entry is selected. */
2802 slash = strchr(p, '/');
2803 if (slash == NULL)
2804 te_name = strdup(p);
2805 else
2806 te_name = strndup(p, slash - p);
2807 if (te_name == NULL) {
2808 err = got_error_from_errno("strndup");
2809 break;
2811 te = got_object_tree_find_entry(s->tree, te_name);
2812 if (te == NULL) {
2813 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2814 free(te_name);
2815 break;
2817 free(te_name);
2818 s->first_displayed_entry = s->selected_entry = te;
2820 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2821 break; /* jump to this file's entry */
2823 slash = strchr(p, '/');
2824 if (slash)
2825 subpath = strndup(path, slash - path);
2826 else
2827 subpath = strdup(path);
2828 if (subpath == NULL) {
2829 err = got_error_from_errno("strdup");
2830 break;
2833 err = got_object_id_by_path(&tree_id, s->repo, commit,
2834 subpath);
2835 if (err)
2836 break;
2838 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2839 free(tree_id);
2840 if (err)
2841 break;
2843 err = tree_view_visit_subtree(s, tree);
2844 if (err) {
2845 got_object_tree_close(tree);
2846 break;
2848 if (slash == NULL)
2849 break;
2850 free(subpath);
2851 subpath = NULL;
2852 p = slash;
2855 free(subpath);
2856 return err;
2859 static const struct got_error *
2860 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2861 struct commit_queue_entry *entry, const char *path,
2862 const char *head_ref_name, struct got_repository *repo)
2864 const struct got_error *err = NULL;
2865 struct tog_tree_view_state *s;
2866 struct tog_view *tree_view;
2868 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2869 if (tree_view == NULL)
2870 return got_error_from_errno("view_open");
2872 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2873 if (err)
2874 return err;
2875 s = &tree_view->state.tree;
2877 *new_view = tree_view;
2879 if (got_path_is_root_dir(path))
2880 return NULL;
2882 return tree_view_walk_path(s, entry->commit, path);
2885 static const struct got_error *
2886 block_signals_used_by_main_thread(void)
2888 sigset_t sigset;
2889 int errcode;
2891 if (sigemptyset(&sigset) == -1)
2892 return got_error_from_errno("sigemptyset");
2894 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2895 if (sigaddset(&sigset, SIGWINCH) == -1)
2896 return got_error_from_errno("sigaddset");
2897 if (sigaddset(&sigset, SIGCONT) == -1)
2898 return got_error_from_errno("sigaddset");
2899 if (sigaddset(&sigset, SIGINT) == -1)
2900 return got_error_from_errno("sigaddset");
2901 if (sigaddset(&sigset, SIGTERM) == -1)
2902 return got_error_from_errno("sigaddset");
2904 /* ncurses handles SIGTSTP */
2905 if (sigaddset(&sigset, SIGTSTP) == -1)
2906 return got_error_from_errno("sigaddset");
2908 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2909 if (errcode)
2910 return got_error_set_errno(errcode, "pthread_sigmask");
2912 return NULL;
2915 static void *
2916 log_thread(void *arg)
2918 const struct got_error *err = NULL;
2919 int errcode = 0;
2920 struct tog_log_thread_args *a = arg;
2921 int done = 0;
2924 * Sync startup with main thread such that we begin our
2925 * work once view_input() has released the mutex.
2927 errcode = pthread_mutex_lock(&tog_mutex);
2928 if (errcode) {
2929 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2930 return (void *)err;
2933 err = block_signals_used_by_main_thread();
2934 if (err) {
2935 pthread_mutex_unlock(&tog_mutex);
2936 goto done;
2939 while (!done && !err && !tog_fatal_signal_received()) {
2940 errcode = pthread_mutex_unlock(&tog_mutex);
2941 if (errcode) {
2942 err = got_error_set_errno(errcode,
2943 "pthread_mutex_unlock");
2944 goto done;
2946 err = queue_commits(a);
2947 if (err) {
2948 if (err->code != GOT_ERR_ITER_COMPLETED)
2949 goto done;
2950 err = NULL;
2951 done = 1;
2952 } else if (a->commits_needed > 0 && !a->load_all) {
2953 if (*a->limiting) {
2954 if (a->limit_match)
2955 a->commits_needed--;
2956 } else
2957 a->commits_needed--;
2960 errcode = pthread_mutex_lock(&tog_mutex);
2961 if (errcode) {
2962 err = got_error_set_errno(errcode,
2963 "pthread_mutex_lock");
2964 goto done;
2965 } else if (*a->quit)
2966 done = 1;
2967 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2968 *a->first_displayed_entry =
2969 TAILQ_FIRST(&a->limit_commits->head);
2970 *a->selected_entry = *a->first_displayed_entry;
2971 } else if (*a->first_displayed_entry == NULL) {
2972 *a->first_displayed_entry =
2973 TAILQ_FIRST(&a->real_commits->head);
2974 *a->selected_entry = *a->first_displayed_entry;
2977 errcode = pthread_cond_signal(&a->commit_loaded);
2978 if (errcode) {
2979 err = got_error_set_errno(errcode,
2980 "pthread_cond_signal");
2981 pthread_mutex_unlock(&tog_mutex);
2982 goto done;
2985 if (done)
2986 a->commits_needed = 0;
2987 else {
2988 if (a->commits_needed == 0 && !a->load_all) {
2989 errcode = pthread_cond_wait(&a->need_commits,
2990 &tog_mutex);
2991 if (errcode) {
2992 err = got_error_set_errno(errcode,
2993 "pthread_cond_wait");
2994 pthread_mutex_unlock(&tog_mutex);
2995 goto done;
2997 if (*a->quit)
2998 done = 1;
3002 a->log_complete = 1;
3003 errcode = pthread_mutex_unlock(&tog_mutex);
3004 if (errcode)
3005 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3006 done:
3007 if (err) {
3008 tog_thread_error = 1;
3009 pthread_cond_signal(&a->commit_loaded);
3011 return (void *)err;
3014 static const struct got_error *
3015 stop_log_thread(struct tog_log_view_state *s)
3017 const struct got_error *err = NULL, *thread_err = NULL;
3018 int errcode;
3020 if (s->thread) {
3021 s->quit = 1;
3022 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3023 if (errcode)
3024 return got_error_set_errno(errcode,
3025 "pthread_cond_signal");
3026 errcode = pthread_mutex_unlock(&tog_mutex);
3027 if (errcode)
3028 return got_error_set_errno(errcode,
3029 "pthread_mutex_unlock");
3030 errcode = pthread_join(s->thread, (void **)&thread_err);
3031 if (errcode)
3032 return got_error_set_errno(errcode, "pthread_join");
3033 errcode = pthread_mutex_lock(&tog_mutex);
3034 if (errcode)
3035 return got_error_set_errno(errcode,
3036 "pthread_mutex_lock");
3037 s->thread = 0; //NULL;
3040 if (s->thread_args.repo) {
3041 err = got_repo_close(s->thread_args.repo);
3042 s->thread_args.repo = NULL;
3045 if (s->thread_args.pack_fds) {
3046 const struct got_error *pack_err =
3047 got_repo_pack_fds_close(s->thread_args.pack_fds);
3048 if (err == NULL)
3049 err = pack_err;
3050 s->thread_args.pack_fds = NULL;
3053 if (s->thread_args.graph) {
3054 got_commit_graph_close(s->thread_args.graph);
3055 s->thread_args.graph = NULL;
3058 return err ? err : thread_err;
3061 static const struct got_error *
3062 close_log_view(struct tog_view *view)
3064 const struct got_error *err = NULL;
3065 struct tog_log_view_state *s = &view->state.log;
3066 int errcode;
3068 err = stop_log_thread(s);
3070 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3071 if (errcode && err == NULL)
3072 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3074 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3075 if (errcode && err == NULL)
3076 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3078 free_commits(&s->limit_commits);
3079 free_commits(&s->real_commits);
3080 free(s->in_repo_path);
3081 s->in_repo_path = NULL;
3082 free(s->start_id);
3083 s->start_id = NULL;
3084 free(s->head_ref_name);
3085 s->head_ref_name = NULL;
3086 return err;
3090 * We use two queues to implement the limit feature: first consists of
3091 * commits matching the current limit_regex; second is the real queue
3092 * of all known commits (real_commits). When the user starts limiting,
3093 * we swap queues such that all movement and displaying functionality
3094 * works with very slight change.
3096 static const struct got_error *
3097 limit_log_view(struct tog_view *view)
3099 struct tog_log_view_state *s = &view->state.log;
3100 struct commit_queue_entry *entry;
3101 struct tog_view *v = view;
3102 const struct got_error *err = NULL;
3103 char pattern[1024];
3104 int ret;
3106 if (view_is_hsplit_top(view))
3107 v = view->child;
3108 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3109 v = view->parent;
3111 /* Get the pattern */
3112 wmove(v->window, v->nlines - 1, 0);
3113 wclrtoeol(v->window);
3114 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3115 nodelay(v->window, FALSE);
3116 nocbreak();
3117 echo();
3118 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3119 cbreak();
3120 noecho();
3121 nodelay(v->window, TRUE);
3122 if (ret == ERR)
3123 return NULL;
3125 if (*pattern == '\0') {
3127 * Safety measure for the situation where the user
3128 * resets limit without previously limiting anything.
3130 if (!s->limit_view)
3131 return NULL;
3134 * User could have pressed Ctrl+L, which refreshed the
3135 * commit queues, it means we can't save previously
3136 * (before limit took place) displayed entries,
3137 * because they would point to already free'ed memory,
3138 * so we are forced to always select first entry of
3139 * the queue.
3141 s->commits = &s->real_commits;
3142 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3143 s->selected_entry = s->first_displayed_entry;
3144 s->selected = 0;
3145 s->limit_view = 0;
3147 return NULL;
3150 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3151 return NULL;
3153 s->limit_view = 1;
3155 /* Clear the screen while loading limit view */
3156 s->first_displayed_entry = NULL;
3157 s->last_displayed_entry = NULL;
3158 s->selected_entry = NULL;
3159 s->commits = &s->limit_commits;
3161 /* Prepare limit queue for new search */
3162 free_commits(&s->limit_commits);
3163 s->limit_commits.ncommits = 0;
3165 /* First process commits, which are in queue already */
3166 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3167 int have_match = 0;
3169 err = match_commit(&have_match, entry->id,
3170 entry->commit, &s->limit_regex);
3171 if (err)
3172 return err;
3174 if (have_match) {
3175 struct commit_queue_entry *matched;
3177 matched = alloc_commit_queue_entry(entry->commit,
3178 entry->id);
3179 if (matched == NULL) {
3180 err = got_error_from_errno(
3181 "alloc_commit_queue_entry");
3182 break;
3184 matched->commit = entry->commit;
3185 got_object_commit_retain(entry->commit);
3187 matched->idx = s->limit_commits.ncommits;
3188 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3189 matched, entry);
3190 s->limit_commits.ncommits++;
3194 /* Second process all the commits, until we fill the screen */
3195 if (s->limit_commits.ncommits < view->nlines - 1 &&
3196 !s->thread_args.log_complete) {
3197 s->thread_args.commits_needed +=
3198 view->nlines - s->limit_commits.ncommits - 1;
3199 err = trigger_log_thread(view, 1);
3200 if (err)
3201 return err;
3204 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3205 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3206 s->selected = 0;
3208 return NULL;
3211 static const struct got_error *
3212 search_start_log_view(struct tog_view *view)
3214 struct tog_log_view_state *s = &view->state.log;
3216 s->matched_entry = NULL;
3217 s->search_entry = NULL;
3218 return NULL;
3221 static const struct got_error *
3222 search_next_log_view(struct tog_view *view)
3224 const struct got_error *err = NULL;
3225 struct tog_log_view_state *s = &view->state.log;
3226 struct commit_queue_entry *entry;
3228 /* Display progress update in log view. */
3229 show_log_view(view);
3230 update_panels();
3231 doupdate();
3233 if (s->search_entry) {
3234 int errcode, ch;
3235 errcode = pthread_mutex_unlock(&tog_mutex);
3236 if (errcode)
3237 return got_error_set_errno(errcode,
3238 "pthread_mutex_unlock");
3239 ch = wgetch(view->window);
3240 errcode = pthread_mutex_lock(&tog_mutex);
3241 if (errcode)
3242 return got_error_set_errno(errcode,
3243 "pthread_mutex_lock");
3244 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3245 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3246 return NULL;
3248 if (view->searching == TOG_SEARCH_FORWARD)
3249 entry = TAILQ_NEXT(s->search_entry, entry);
3250 else
3251 entry = TAILQ_PREV(s->search_entry,
3252 commit_queue_head, entry);
3253 } else if (s->matched_entry) {
3255 * If the user has moved the cursor after we hit a match,
3256 * the position from where we should continue searching
3257 * might have changed.
3259 if (view->searching == TOG_SEARCH_FORWARD)
3260 entry = TAILQ_NEXT(s->selected_entry, entry);
3261 else
3262 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3263 entry);
3264 } else {
3265 entry = s->selected_entry;
3268 while (1) {
3269 int have_match = 0;
3271 if (entry == NULL) {
3272 if (s->thread_args.log_complete ||
3273 view->searching == TOG_SEARCH_BACKWARD) {
3274 view->search_next_done =
3275 (s->matched_entry == NULL ?
3276 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3277 s->search_entry = NULL;
3278 return NULL;
3281 * Poke the log thread for more commits and return,
3282 * allowing the main loop to make progress. Search
3283 * will resume at s->search_entry once we come back.
3285 s->thread_args.commits_needed++;
3286 return trigger_log_thread(view, 0);
3289 err = match_commit(&have_match, entry->id, entry->commit,
3290 &view->regex);
3291 if (err)
3292 break;
3293 if (have_match) {
3294 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3295 s->matched_entry = entry;
3296 break;
3299 s->search_entry = entry;
3300 if (view->searching == TOG_SEARCH_FORWARD)
3301 entry = TAILQ_NEXT(entry, entry);
3302 else
3303 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3306 if (s->matched_entry) {
3307 int cur = s->selected_entry->idx;
3308 while (cur < s->matched_entry->idx) {
3309 err = input_log_view(NULL, view, KEY_DOWN);
3310 if (err)
3311 return err;
3312 cur++;
3314 while (cur > s->matched_entry->idx) {
3315 err = input_log_view(NULL, view, KEY_UP);
3316 if (err)
3317 return err;
3318 cur--;
3322 s->search_entry = NULL;
3324 return NULL;
3327 static const struct got_error *
3328 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3329 struct got_repository *repo, const char *head_ref_name,
3330 const char *in_repo_path, int log_branches)
3332 const struct got_error *err = NULL;
3333 struct tog_log_view_state *s = &view->state.log;
3334 struct got_repository *thread_repo = NULL;
3335 struct got_commit_graph *thread_graph = NULL;
3336 int errcode;
3338 if (in_repo_path != s->in_repo_path) {
3339 free(s->in_repo_path);
3340 s->in_repo_path = strdup(in_repo_path);
3341 if (s->in_repo_path == NULL)
3342 return got_error_from_errno("strdup");
3345 /* The commit queue only contains commits being displayed. */
3346 TAILQ_INIT(&s->real_commits.head);
3347 s->real_commits.ncommits = 0;
3348 s->commits = &s->real_commits;
3350 TAILQ_INIT(&s->limit_commits.head);
3351 s->limit_view = 0;
3352 s->limit_commits.ncommits = 0;
3354 s->repo = repo;
3355 if (head_ref_name) {
3356 s->head_ref_name = strdup(head_ref_name);
3357 if (s->head_ref_name == NULL) {
3358 err = got_error_from_errno("strdup");
3359 goto done;
3362 s->start_id = got_object_id_dup(start_id);
3363 if (s->start_id == NULL) {
3364 err = got_error_from_errno("got_object_id_dup");
3365 goto done;
3367 s->log_branches = log_branches;
3369 STAILQ_INIT(&s->colors);
3370 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3371 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3372 get_color_value("TOG_COLOR_COMMIT"));
3373 if (err)
3374 goto done;
3375 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3376 get_color_value("TOG_COLOR_AUTHOR"));
3377 if (err) {
3378 free_colors(&s->colors);
3379 goto done;
3381 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3382 get_color_value("TOG_COLOR_DATE"));
3383 if (err) {
3384 free_colors(&s->colors);
3385 goto done;
3389 view->show = show_log_view;
3390 view->input = input_log_view;
3391 view->resize = resize_log_view;
3392 view->close = close_log_view;
3393 view->search_start = search_start_log_view;
3394 view->search_next = search_next_log_view;
3396 if (s->thread_args.pack_fds == NULL) {
3397 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3398 if (err)
3399 goto done;
3401 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3402 s->thread_args.pack_fds);
3403 if (err)
3404 goto done;
3405 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3406 !s->log_branches);
3407 if (err)
3408 goto done;
3409 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3410 s->repo, NULL, NULL);
3411 if (err)
3412 goto done;
3414 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3415 if (errcode) {
3416 err = got_error_set_errno(errcode, "pthread_cond_init");
3417 goto done;
3419 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3420 if (errcode) {
3421 err = got_error_set_errno(errcode, "pthread_cond_init");
3422 goto done;
3425 s->thread_args.commits_needed = view->nlines;
3426 s->thread_args.graph = thread_graph;
3427 s->thread_args.real_commits = &s->real_commits;
3428 s->thread_args.limit_commits = &s->limit_commits;
3429 s->thread_args.in_repo_path = s->in_repo_path;
3430 s->thread_args.start_id = s->start_id;
3431 s->thread_args.repo = thread_repo;
3432 s->thread_args.log_complete = 0;
3433 s->thread_args.quit = &s->quit;
3434 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3435 s->thread_args.selected_entry = &s->selected_entry;
3436 s->thread_args.searching = &view->searching;
3437 s->thread_args.search_next_done = &view->search_next_done;
3438 s->thread_args.regex = &view->regex;
3439 s->thread_args.limiting = &s->limit_view;
3440 s->thread_args.limit_regex = &s->limit_regex;
3441 s->thread_args.limit_commits = &s->limit_commits;
3442 done:
3443 if (err)
3444 close_log_view(view);
3445 return err;
3448 static const struct got_error *
3449 show_log_view(struct tog_view *view)
3451 const struct got_error *err;
3452 struct tog_log_view_state *s = &view->state.log;
3454 if (s->thread == 0) { //NULL) {
3455 int errcode = pthread_create(&s->thread, NULL, log_thread,
3456 &s->thread_args);
3457 if (errcode)
3458 return got_error_set_errno(errcode, "pthread_create");
3459 if (s->thread_args.commits_needed > 0) {
3460 err = trigger_log_thread(view, 1);
3461 if (err)
3462 return err;
3466 return draw_commits(view);
3469 static void
3470 log_move_cursor_up(struct tog_view *view, int page, int home)
3472 struct tog_log_view_state *s = &view->state.log;
3474 if (s->first_displayed_entry == NULL)
3475 return;
3476 if (s->selected_entry->idx == 0)
3477 view->count = 0;
3479 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3480 || home)
3481 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3483 if (!page && !home && s->selected > 0)
3484 --s->selected;
3485 else
3486 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3488 select_commit(s);
3489 return;
3492 static const struct got_error *
3493 log_move_cursor_down(struct tog_view *view, int page)
3495 struct tog_log_view_state *s = &view->state.log;
3496 const struct got_error *err = NULL;
3497 int eos = view->nlines - 2;
3499 if (s->first_displayed_entry == NULL)
3500 return NULL;
3502 if (s->thread_args.log_complete &&
3503 s->selected_entry->idx >= s->commits->ncommits - 1)
3504 return NULL;
3506 if (view_is_hsplit_top(view))
3507 --eos; /* border consumes the last line */
3509 if (!page) {
3510 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3511 ++s->selected;
3512 else
3513 err = log_scroll_down(view, 1);
3514 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3515 struct commit_queue_entry *entry;
3516 int n;
3518 s->selected = 0;
3519 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3520 s->last_displayed_entry = entry;
3521 for (n = 0; n <= eos; n++) {
3522 if (entry == NULL)
3523 break;
3524 s->first_displayed_entry = entry;
3525 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3527 if (n > 0)
3528 s->selected = n - 1;
3529 } else {
3530 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3531 s->thread_args.log_complete)
3532 s->selected += MIN(page,
3533 s->commits->ncommits - s->selected_entry->idx - 1);
3534 else
3535 err = log_scroll_down(view, page);
3537 if (err)
3538 return err;
3541 * We might necessarily overshoot in horizontal
3542 * splits; if so, select the last displayed commit.
3544 if (s->first_displayed_entry && s->last_displayed_entry) {
3545 s->selected = MIN(s->selected,
3546 s->last_displayed_entry->idx -
3547 s->first_displayed_entry->idx);
3550 select_commit(s);
3552 if (s->thread_args.log_complete &&
3553 s->selected_entry->idx == s->commits->ncommits - 1)
3554 view->count = 0;
3556 return NULL;
3559 static void
3560 view_get_split(struct tog_view *view, int *y, int *x)
3562 *x = 0;
3563 *y = 0;
3565 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3566 if (view->child && view->child->resized_y)
3567 *y = view->child->resized_y;
3568 else if (view->resized_y)
3569 *y = view->resized_y;
3570 else
3571 *y = view_split_begin_y(view->lines);
3572 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3573 if (view->child && view->child->resized_x)
3574 *x = view->child->resized_x;
3575 else if (view->resized_x)
3576 *x = view->resized_x;
3577 else
3578 *x = view_split_begin_x(view->begin_x);
3582 /* Split view horizontally at y and offset view->state->selected line. */
3583 static const struct got_error *
3584 view_init_hsplit(struct tog_view *view, int y)
3586 const struct got_error *err = NULL;
3588 view->nlines = y;
3589 view->ncols = COLS;
3590 err = view_resize(view);
3591 if (err)
3592 return err;
3594 err = offset_selection_down(view);
3596 return err;
3599 static const struct got_error *
3600 log_goto_line(struct tog_view *view, int nlines)
3602 const struct got_error *err = NULL;
3603 struct tog_log_view_state *s = &view->state.log;
3604 int g, idx = s->selected_entry->idx;
3606 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3607 return NULL;
3609 g = view->gline;
3610 view->gline = 0;
3612 if (g >= s->first_displayed_entry->idx + 1 &&
3613 g <= s->last_displayed_entry->idx + 1 &&
3614 g - s->first_displayed_entry->idx - 1 < nlines) {
3615 s->selected = g - s->first_displayed_entry->idx - 1;
3616 select_commit(s);
3617 return NULL;
3620 if (idx + 1 < g) {
3621 err = log_move_cursor_down(view, g - idx - 1);
3622 if (!err && g > s->selected_entry->idx + 1)
3623 err = log_move_cursor_down(view,
3624 g - s->first_displayed_entry->idx - 1);
3625 if (err)
3626 return err;
3627 } else if (idx + 1 > g)
3628 log_move_cursor_up(view, idx - g + 1, 0);
3630 if (g < nlines && s->first_displayed_entry->idx == 0)
3631 s->selected = g - 1;
3633 select_commit(s);
3634 return NULL;
3638 static const struct got_error *
3639 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3641 const struct got_error *err = NULL;
3642 struct tog_log_view_state *s = &view->state.log;
3643 int eos, nscroll;
3645 if (s->thread_args.load_all) {
3646 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3647 s->thread_args.load_all = 0;
3648 else if (s->thread_args.log_complete) {
3649 err = log_move_cursor_down(view, s->commits->ncommits);
3650 s->thread_args.load_all = 0;
3652 if (err)
3653 return err;
3656 eos = nscroll = view->nlines - 1;
3657 if (view_is_hsplit_top(view))
3658 --eos; /* border */
3660 if (view->gline)
3661 return log_goto_line(view, eos);
3663 switch (ch) {
3664 case '&':
3665 err = limit_log_view(view);
3666 break;
3667 case 'q':
3668 s->quit = 1;
3669 break;
3670 case '0':
3671 view->x = 0;
3672 break;
3673 case '$':
3674 view->x = MAX(view->maxx - view->ncols / 2, 0);
3675 view->count = 0;
3676 break;
3677 case KEY_RIGHT:
3678 case 'l':
3679 if (view->x + view->ncols / 2 < view->maxx)
3680 view->x += 2; /* move two columns right */
3681 else
3682 view->count = 0;
3683 break;
3684 case KEY_LEFT:
3685 case 'h':
3686 view->x -= MIN(view->x, 2); /* move two columns back */
3687 if (view->x <= 0)
3688 view->count = 0;
3689 break;
3690 case 'k':
3691 case KEY_UP:
3692 case '<':
3693 case ',':
3694 case CTRL('p'):
3695 log_move_cursor_up(view, 0, 0);
3696 break;
3697 case 'g':
3698 case KEY_HOME:
3699 log_move_cursor_up(view, 0, 1);
3700 view->count = 0;
3701 break;
3702 case CTRL('u'):
3703 case 'u':
3704 nscroll /= 2;
3705 /* FALL THROUGH */
3706 case KEY_PPAGE:
3707 case CTRL('b'):
3708 case 'b':
3709 log_move_cursor_up(view, nscroll, 0);
3710 break;
3711 case 'j':
3712 case KEY_DOWN:
3713 case '>':
3714 case '.':
3715 case CTRL('n'):
3716 err = log_move_cursor_down(view, 0);
3717 break;
3718 case '@':
3719 s->use_committer = !s->use_committer;
3720 break;
3721 case 'G':
3722 case KEY_END: {
3723 /* We don't know yet how many commits, so we're forced to
3724 * traverse them all. */
3725 view->count = 0;
3726 s->thread_args.load_all = 1;
3727 if (!s->thread_args.log_complete)
3728 return trigger_log_thread(view, 0);
3729 err = log_move_cursor_down(view, s->commits->ncommits);
3730 s->thread_args.load_all = 0;
3731 break;
3733 case CTRL('d'):
3734 case 'd':
3735 nscroll /= 2;
3736 /* FALL THROUGH */
3737 case KEY_NPAGE:
3738 case CTRL('f'):
3739 case 'f':
3740 case ' ':
3741 err = log_move_cursor_down(view, nscroll);
3742 break;
3743 case KEY_RESIZE:
3744 if (s->selected > view->nlines - 2)
3745 s->selected = view->nlines - 2;
3746 if (s->selected > s->commits->ncommits - 1)
3747 s->selected = s->commits->ncommits - 1;
3748 select_commit(s);
3749 if (s->commits->ncommits < view->nlines - 1 &&
3750 !s->thread_args.log_complete) {
3751 s->thread_args.commits_needed += (view->nlines - 1) -
3752 s->commits->ncommits;
3753 err = trigger_log_thread(view, 1);
3755 break;
3756 case KEY_ENTER:
3757 case '\r':
3758 view->count = 0;
3759 if (s->selected_entry == NULL)
3760 break;
3761 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3762 break;
3763 case 'T':
3764 view->count = 0;
3765 if (s->selected_entry == NULL)
3766 break;
3767 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3768 break;
3769 case KEY_BACKSPACE:
3770 case CTRL('l'):
3771 case 'B':
3772 view->count = 0;
3773 if (ch == KEY_BACKSPACE &&
3774 got_path_is_root_dir(s->in_repo_path))
3775 break;
3776 err = stop_log_thread(s);
3777 if (err)
3778 return err;
3779 if (ch == KEY_BACKSPACE) {
3780 char *parent_path;
3781 err = got_path_dirname(&parent_path, s->in_repo_path);
3782 if (err)
3783 return err;
3784 free(s->in_repo_path);
3785 s->in_repo_path = parent_path;
3786 s->thread_args.in_repo_path = s->in_repo_path;
3787 } else if (ch == CTRL('l')) {
3788 struct got_object_id *start_id;
3789 err = got_repo_match_object_id(&start_id, NULL,
3790 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3791 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3792 if (err) {
3793 if (s->head_ref_name == NULL ||
3794 err->code != GOT_ERR_NOT_REF)
3795 return err;
3796 /* Try to cope with deleted references. */
3797 free(s->head_ref_name);
3798 s->head_ref_name = NULL;
3799 err = got_repo_match_object_id(&start_id,
3800 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3801 &tog_refs, s->repo);
3802 if (err)
3803 return err;
3805 free(s->start_id);
3806 s->start_id = start_id;
3807 s->thread_args.start_id = s->start_id;
3808 } else /* 'B' */
3809 s->log_branches = !s->log_branches;
3811 if (s->thread_args.pack_fds == NULL) {
3812 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3813 if (err)
3814 return err;
3816 err = got_repo_open(&s->thread_args.repo,
3817 got_repo_get_path(s->repo), NULL,
3818 s->thread_args.pack_fds);
3819 if (err)
3820 return err;
3821 tog_free_refs();
3822 err = tog_load_refs(s->repo, 0);
3823 if (err)
3824 return err;
3825 err = got_commit_graph_open(&s->thread_args.graph,
3826 s->in_repo_path, !s->log_branches);
3827 if (err)
3828 return err;
3829 err = got_commit_graph_iter_start(s->thread_args.graph,
3830 s->start_id, s->repo, NULL, NULL);
3831 if (err)
3832 return err;
3833 free_commits(&s->real_commits);
3834 free_commits(&s->limit_commits);
3835 s->first_displayed_entry = NULL;
3836 s->last_displayed_entry = NULL;
3837 s->selected_entry = NULL;
3838 s->selected = 0;
3839 s->thread_args.log_complete = 0;
3840 s->quit = 0;
3841 s->thread_args.commits_needed = view->lines;
3842 s->matched_entry = NULL;
3843 s->search_entry = NULL;
3844 view->offset = 0;
3845 break;
3846 case 'R':
3847 view->count = 0;
3848 err = view_request_new(new_view, view, TOG_VIEW_REF);
3849 break;
3850 default:
3851 view->count = 0;
3852 break;
3855 return err;
3858 static const struct got_error *
3859 apply_unveil(const char *repo_path, const char *worktree_path)
3861 const struct got_error *error;
3863 #ifdef PROFILE
3864 if (unveil("gmon.out", "rwc") != 0)
3865 return got_error_from_errno2("unveil", "gmon.out");
3866 #endif
3867 if (repo_path && unveil(repo_path, "r") != 0)
3868 return got_error_from_errno2("unveil", repo_path);
3870 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3871 return got_error_from_errno2("unveil", worktree_path);
3873 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3874 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3876 error = got_privsep_unveil_exec_helpers();
3877 if (error != NULL)
3878 return error;
3880 if (unveil(NULL, NULL) != 0)
3881 return got_error_from_errno("unveil");
3883 return NULL;
3886 static void
3887 init_curses(void)
3890 * Override default signal handlers before starting ncurses.
3891 * This should prevent ncurses from installing its own
3892 * broken cleanup() signal handler.
3894 signal(SIGWINCH, tog_sigwinch);
3895 signal(SIGPIPE, tog_sigpipe);
3896 signal(SIGCONT, tog_sigcont);
3897 signal(SIGINT, tog_sigint);
3898 signal(SIGTERM, tog_sigterm);
3900 initscr();
3901 cbreak();
3902 halfdelay(1); /* Do fast refresh while initial view is loading. */
3903 noecho();
3904 nonl();
3905 intrflush(stdscr, FALSE);
3906 keypad(stdscr, TRUE);
3907 curs_set(0);
3908 if (getenv("TOG_COLORS") != NULL) {
3909 start_color();
3910 use_default_colors();
3914 static const struct got_error *
3915 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3916 struct got_repository *repo, struct got_worktree *worktree)
3918 const struct got_error *err = NULL;
3920 if (argc == 0) {
3921 *in_repo_path = strdup("/");
3922 if (*in_repo_path == NULL)
3923 return got_error_from_errno("strdup");
3924 return NULL;
3927 if (worktree) {
3928 const char *prefix = got_worktree_get_path_prefix(worktree);
3929 char *p;
3931 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3932 if (err)
3933 return err;
3934 if (asprintf(in_repo_path, "%s%s%s", prefix,
3935 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3936 p) == -1) {
3937 err = got_error_from_errno("asprintf");
3938 *in_repo_path = NULL;
3940 free(p);
3941 } else
3942 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3944 return err;
3947 static const struct got_error *
3948 cmd_log(int argc, char *argv[])
3950 const struct got_error *error;
3951 struct got_repository *repo = NULL;
3952 struct got_worktree *worktree = NULL;
3953 struct got_object_id *start_id = NULL;
3954 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3955 char *start_commit = NULL, *label = NULL;
3956 struct got_reference *ref = NULL;
3957 const char *head_ref_name = NULL;
3958 int ch, log_branches = 0;
3959 struct tog_view *view;
3960 int *pack_fds = NULL;
3962 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3963 switch (ch) {
3964 case 'b':
3965 log_branches = 1;
3966 break;
3967 case 'c':
3968 start_commit = optarg;
3969 break;
3970 case 'r':
3971 repo_path = realpath(optarg, NULL);
3972 if (repo_path == NULL)
3973 return got_error_from_errno2("realpath",
3974 optarg);
3975 break;
3976 default:
3977 usage_log();
3978 /* NOTREACHED */
3982 argc -= optind;
3983 argv += optind;
3985 if (argc > 1)
3986 usage_log();
3988 error = got_repo_pack_fds_open(&pack_fds);
3989 if (error != NULL)
3990 goto done;
3992 if (repo_path == NULL) {
3993 cwd = getcwd(NULL, 0);
3994 if (cwd == NULL)
3995 return got_error_from_errno("getcwd");
3996 error = got_worktree_open(&worktree, cwd);
3997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3998 goto done;
3999 if (worktree)
4000 repo_path =
4001 strdup(got_worktree_get_repo_path(worktree));
4002 else
4003 repo_path = strdup(cwd);
4004 if (repo_path == NULL) {
4005 error = got_error_from_errno("strdup");
4006 goto done;
4010 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4011 if (error != NULL)
4012 goto done;
4014 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4015 repo, worktree);
4016 if (error)
4017 goto done;
4019 init_curses();
4021 error = apply_unveil(got_repo_get_path(repo),
4022 worktree ? got_worktree_get_root_path(worktree) : NULL);
4023 if (error)
4024 goto done;
4026 /* already loaded by tog_log_with_path()? */
4027 if (TAILQ_EMPTY(&tog_refs)) {
4028 error = tog_load_refs(repo, 0);
4029 if (error)
4030 goto done;
4033 if (start_commit == NULL) {
4034 error = got_repo_match_object_id(&start_id, &label,
4035 worktree ? got_worktree_get_head_ref_name(worktree) :
4036 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4037 if (error)
4038 goto done;
4039 head_ref_name = label;
4040 } else {
4041 error = got_ref_open(&ref, repo, start_commit, 0);
4042 if (error == NULL)
4043 head_ref_name = got_ref_get_name(ref);
4044 else if (error->code != GOT_ERR_NOT_REF)
4045 goto done;
4046 error = got_repo_match_object_id(&start_id, NULL,
4047 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4048 if (error)
4049 goto done;
4052 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4053 if (view == NULL) {
4054 error = got_error_from_errno("view_open");
4055 goto done;
4057 error = open_log_view(view, start_id, repo, head_ref_name,
4058 in_repo_path, log_branches);
4059 if (error)
4060 goto done;
4061 if (worktree) {
4062 /* Release work tree lock. */
4063 got_worktree_close(worktree);
4064 worktree = NULL;
4066 error = view_loop(view);
4067 done:
4068 free(in_repo_path);
4069 free(repo_path);
4070 free(cwd);
4071 free(start_id);
4072 free(label);
4073 if (ref)
4074 got_ref_close(ref);
4075 if (repo) {
4076 const struct got_error *close_err = got_repo_close(repo);
4077 if (error == NULL)
4078 error = close_err;
4080 if (worktree)
4081 got_worktree_close(worktree);
4082 if (pack_fds) {
4083 const struct got_error *pack_err =
4084 got_repo_pack_fds_close(pack_fds);
4085 if (error == NULL)
4086 error = pack_err;
4088 tog_free_refs();
4089 return error;
4092 __dead static void
4093 usage_diff(void)
4095 endwin();
4096 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4097 "object1 object2\n", getprogname());
4098 exit(1);
4101 static int
4102 match_line(const char *line, regex_t *regex, size_t nmatch,
4103 regmatch_t *regmatch)
4105 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4108 static struct tog_color *
4109 match_color(struct tog_colors *colors, const char *line)
4111 struct tog_color *tc = NULL;
4113 STAILQ_FOREACH(tc, colors, entry) {
4114 if (match_line(line, &tc->regex, 0, NULL))
4115 return tc;
4118 return NULL;
4121 static const struct got_error *
4122 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4123 WINDOW *window, int skipcol, regmatch_t *regmatch)
4125 const struct got_error *err = NULL;
4126 char *exstr = NULL;
4127 wchar_t *wline = NULL;
4128 int rme, rms, n, width, scrollx;
4129 int width0 = 0, width1 = 0, width2 = 0;
4130 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4132 *wtotal = 0;
4134 rms = regmatch->rm_so;
4135 rme = regmatch->rm_eo;
4137 err = expand_tab(&exstr, line);
4138 if (err)
4139 return err;
4141 /* Split the line into 3 segments, according to match offsets. */
4142 seg0 = strndup(exstr, rms);
4143 if (seg0 == NULL) {
4144 err = got_error_from_errno("strndup");
4145 goto done;
4147 seg1 = strndup(exstr + rms, rme - rms);
4148 if (seg1 == NULL) {
4149 err = got_error_from_errno("strndup");
4150 goto done;
4152 seg2 = strdup(exstr + rme);
4153 if (seg2 == NULL) {
4154 err = got_error_from_errno("strndup");
4155 goto done;
4158 /* draw up to matched token if we haven't scrolled past it */
4159 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4160 col_tab_align, 1);
4161 if (err)
4162 goto done;
4163 n = MAX(width0 - skipcol, 0);
4164 if (n) {
4165 free(wline);
4166 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4167 wlimit, col_tab_align, 1);
4168 if (err)
4169 goto done;
4170 waddwstr(window, &wline[scrollx]);
4171 wlimit -= width;
4172 *wtotal += width;
4175 if (wlimit > 0) {
4176 int i = 0, w = 0;
4177 size_t wlen;
4179 free(wline);
4180 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4181 col_tab_align, 1);
4182 if (err)
4183 goto done;
4184 wlen = wcslen(wline);
4185 while (i < wlen) {
4186 width = wcwidth(wline[i]);
4187 if (width == -1) {
4188 /* should not happen, tabs are expanded */
4189 err = got_error(GOT_ERR_RANGE);
4190 goto done;
4192 if (width0 + w + width > skipcol)
4193 break;
4194 w += width;
4195 i++;
4197 /* draw (visible part of) matched token (if scrolled into it) */
4198 if (width1 - w > 0) {
4199 wattron(window, A_STANDOUT);
4200 waddwstr(window, &wline[i]);
4201 wattroff(window, A_STANDOUT);
4202 wlimit -= (width1 - w);
4203 *wtotal += (width1 - w);
4207 if (wlimit > 0) { /* draw rest of line */
4208 free(wline);
4209 if (skipcol > width0 + width1) {
4210 err = format_line(&wline, &width2, &scrollx, seg2,
4211 skipcol - (width0 + width1), wlimit,
4212 col_tab_align, 1);
4213 if (err)
4214 goto done;
4215 waddwstr(window, &wline[scrollx]);
4216 } else {
4217 err = format_line(&wline, &width2, NULL, seg2, 0,
4218 wlimit, col_tab_align, 1);
4219 if (err)
4220 goto done;
4221 waddwstr(window, wline);
4223 *wtotal += width2;
4225 done:
4226 free(wline);
4227 free(exstr);
4228 free(seg0);
4229 free(seg1);
4230 free(seg2);
4231 return err;
4234 static int
4235 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4237 FILE *f = NULL;
4238 int *eof, *first, *selected;
4240 if (view->type == TOG_VIEW_DIFF) {
4241 struct tog_diff_view_state *s = &view->state.diff;
4243 first = &s->first_displayed_line;
4244 selected = first;
4245 eof = &s->eof;
4246 f = s->f;
4247 } else if (view->type == TOG_VIEW_HELP) {
4248 struct tog_help_view_state *s = &view->state.help;
4250 first = &s->first_displayed_line;
4251 selected = first;
4252 eof = &s->eof;
4253 f = s->f;
4254 } else if (view->type == TOG_VIEW_BLAME) {
4255 struct tog_blame_view_state *s = &view->state.blame;
4257 first = &s->first_displayed_line;
4258 selected = &s->selected_line;
4259 eof = &s->eof;
4260 f = s->blame.f;
4261 } else
4262 return 0;
4264 /* Center gline in the middle of the page like vi(1). */
4265 if (*lineno < view->gline - (view->nlines - 3) / 2)
4266 return 0;
4267 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4268 rewind(f);
4269 *eof = 0;
4270 *first = 1;
4271 *lineno = 0;
4272 *nprinted = 0;
4273 return 0;
4276 *selected = view->gline <= (view->nlines - 3) / 2 ?
4277 view->gline : (view->nlines - 3) / 2 + 1;
4278 view->gline = 0;
4280 return 1;
4283 static const struct got_error *
4284 draw_file(struct tog_view *view, const char *header)
4286 struct tog_diff_view_state *s = &view->state.diff;
4287 regmatch_t *regmatch = &view->regmatch;
4288 const struct got_error *err;
4289 int nprinted = 0;
4290 char *line;
4291 size_t linesize = 0;
4292 ssize_t linelen;
4293 wchar_t *wline;
4294 int width;
4295 int max_lines = view->nlines;
4296 int nlines = s->nlines;
4297 off_t line_offset;
4299 s->lineno = s->first_displayed_line - 1;
4300 line_offset = s->lines[s->first_displayed_line - 1].offset;
4301 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4302 return got_error_from_errno("fseek");
4304 werase(view->window);
4306 if (view->gline > s->nlines - 1)
4307 view->gline = s->nlines - 1;
4309 if (header) {
4310 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4311 1 : view->gline - (view->nlines - 3) / 2 :
4312 s->lineno + s->selected_line;
4314 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4315 return got_error_from_errno("asprintf");
4316 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4317 0, 0);
4318 free(line);
4319 if (err)
4320 return err;
4322 if (view_needs_focus_indication(view))
4323 wstandout(view->window);
4324 waddwstr(view->window, wline);
4325 free(wline);
4326 wline = NULL;
4327 while (width++ < view->ncols)
4328 waddch(view->window, ' ');
4329 if (view_needs_focus_indication(view))
4330 wstandend(view->window);
4332 if (max_lines <= 1)
4333 return NULL;
4334 max_lines--;
4337 s->eof = 0;
4338 view->maxx = 0;
4339 line = NULL;
4340 while (max_lines > 0 && nprinted < max_lines) {
4341 enum got_diff_line_type linetype;
4342 attr_t attr = 0;
4344 linelen = getline(&line, &linesize, s->f);
4345 if (linelen == -1) {
4346 if (feof(s->f)) {
4347 s->eof = 1;
4348 break;
4350 free(line);
4351 return got_ferror(s->f, GOT_ERR_IO);
4354 if (++s->lineno < s->first_displayed_line)
4355 continue;
4356 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4357 continue;
4358 if (s->lineno == view->hiline)
4359 attr = A_STANDOUT;
4361 /* Set view->maxx based on full line length. */
4362 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4363 view->x ? 1 : 0);
4364 if (err) {
4365 free(line);
4366 return err;
4368 view->maxx = MAX(view->maxx, width);
4369 free(wline);
4370 wline = NULL;
4372 linetype = s->lines[s->lineno].type;
4373 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4374 linetype < GOT_DIFF_LINE_CONTEXT)
4375 attr |= COLOR_PAIR(linetype);
4376 if (attr)
4377 wattron(view->window, attr);
4378 if (s->first_displayed_line + nprinted == s->matched_line &&
4379 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4380 err = add_matched_line(&width, line, view->ncols, 0,
4381 view->window, view->x, regmatch);
4382 if (err) {
4383 free(line);
4384 return err;
4386 } else {
4387 int skip;
4388 err = format_line(&wline, &width, &skip, line,
4389 view->x, view->ncols, 0, view->x ? 1 : 0);
4390 if (err) {
4391 free(line);
4392 return err;
4394 waddwstr(view->window, &wline[skip]);
4395 free(wline);
4396 wline = NULL;
4398 if (s->lineno == view->hiline) {
4399 /* highlight full gline length */
4400 while (width++ < view->ncols)
4401 waddch(view->window, ' ');
4402 } else {
4403 if (width <= view->ncols - 1)
4404 waddch(view->window, '\n');
4406 if (attr)
4407 wattroff(view->window, attr);
4408 if (++nprinted == 1)
4409 s->first_displayed_line = s->lineno;
4411 free(line);
4412 if (nprinted >= 1)
4413 s->last_displayed_line = s->first_displayed_line +
4414 (nprinted - 1);
4415 else
4416 s->last_displayed_line = s->first_displayed_line;
4418 view_border(view);
4420 if (s->eof) {
4421 while (nprinted < view->nlines) {
4422 waddch(view->window, '\n');
4423 nprinted++;
4426 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4427 view->ncols, 0, 0);
4428 if (err) {
4429 return err;
4432 wstandout(view->window);
4433 waddwstr(view->window, wline);
4434 free(wline);
4435 wline = NULL;
4436 wstandend(view->window);
4439 return NULL;
4442 static char *
4443 get_datestr(time_t *time, char *datebuf)
4445 struct tm mytm, *tm;
4446 char *p, *s;
4448 tm = gmtime_r(time, &mytm);
4449 if (tm == NULL)
4450 return NULL;
4451 s = asctime_r(tm, datebuf);
4452 if (s == NULL)
4453 return NULL;
4454 p = strchr(s, '\n');
4455 if (p)
4456 *p = '\0';
4457 return s;
4460 static const struct got_error *
4461 get_changed_paths(struct got_pathlist_head *paths,
4462 struct got_commit_object *commit, struct got_repository *repo)
4464 const struct got_error *err = NULL;
4465 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4466 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4467 struct got_object_qid *qid;
4469 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4470 if (qid != NULL) {
4471 struct got_commit_object *pcommit;
4472 err = got_object_open_as_commit(&pcommit, repo,
4473 &qid->id);
4474 if (err)
4475 return err;
4477 tree_id1 = got_object_id_dup(
4478 got_object_commit_get_tree_id(pcommit));
4479 if (tree_id1 == NULL) {
4480 got_object_commit_close(pcommit);
4481 return got_error_from_errno("got_object_id_dup");
4483 got_object_commit_close(pcommit);
4487 if (tree_id1) {
4488 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4489 if (err)
4490 goto done;
4493 tree_id2 = got_object_commit_get_tree_id(commit);
4494 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4495 if (err)
4496 goto done;
4498 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4499 got_diff_tree_collect_changed_paths, paths, 0);
4500 done:
4501 if (tree1)
4502 got_object_tree_close(tree1);
4503 if (tree2)
4504 got_object_tree_close(tree2);
4505 free(tree_id1);
4506 return err;
4509 static const struct got_error *
4510 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4511 off_t off, uint8_t type)
4513 struct got_diff_line *p;
4515 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4516 if (p == NULL)
4517 return got_error_from_errno("reallocarray");
4518 *lines = p;
4519 (*lines)[*nlines].offset = off;
4520 (*lines)[*nlines].type = type;
4521 (*nlines)++;
4523 return NULL;
4526 static const struct got_error *
4527 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4528 struct got_object_id *commit_id, struct got_reflist_head *refs,
4529 struct got_repository *repo, FILE *outfile)
4531 const struct got_error *err = NULL;
4532 char datebuf[26], *datestr;
4533 struct got_commit_object *commit;
4534 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4535 time_t committer_time;
4536 const char *author, *committer;
4537 char *refs_str = NULL;
4538 struct got_pathlist_head changed_paths;
4539 struct got_pathlist_entry *pe;
4540 off_t outoff = 0;
4541 int n;
4543 TAILQ_INIT(&changed_paths);
4545 if (refs) {
4546 err = build_refs_str(&refs_str, refs, commit_id, repo);
4547 if (err)
4548 return err;
4551 err = got_object_open_as_commit(&commit, repo, commit_id);
4552 if (err)
4553 return err;
4555 err = got_object_id_str(&id_str, commit_id);
4556 if (err) {
4557 err = got_error_from_errno("got_object_id_str");
4558 goto done;
4561 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4562 if (err)
4563 goto done;
4565 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4566 refs_str ? refs_str : "", refs_str ? ")" : "");
4567 if (n < 0) {
4568 err = got_error_from_errno("fprintf");
4569 goto done;
4571 outoff += n;
4572 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4573 if (err)
4574 goto done;
4576 n = fprintf(outfile, "from: %s\n",
4577 got_object_commit_get_author(commit));
4578 if (n < 0) {
4579 err = got_error_from_errno("fprintf");
4580 goto done;
4582 outoff += n;
4583 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4584 if (err)
4585 goto done;
4587 committer_time = got_object_commit_get_committer_time(commit);
4588 datestr = get_datestr(&committer_time, datebuf);
4589 if (datestr) {
4590 n = fprintf(outfile, "date: %s UTC\n", datestr);
4591 if (n < 0) {
4592 err = got_error_from_errno("fprintf");
4593 goto done;
4595 outoff += n;
4596 err = add_line_metadata(lines, nlines, outoff,
4597 GOT_DIFF_LINE_DATE);
4598 if (err)
4599 goto done;
4601 author = got_object_commit_get_author(commit);
4602 committer = got_object_commit_get_committer(commit);
4603 if (strcmp(author, committer) != 0) {
4604 n = fprintf(outfile, "via: %s\n", committer);
4605 if (n < 0) {
4606 err = got_error_from_errno("fprintf");
4607 goto done;
4609 outoff += n;
4610 err = add_line_metadata(lines, nlines, outoff,
4611 GOT_DIFF_LINE_AUTHOR);
4612 if (err)
4613 goto done;
4615 if (got_object_commit_get_nparents(commit) > 1) {
4616 const struct got_object_id_queue *parent_ids;
4617 struct got_object_qid *qid;
4618 int pn = 1;
4619 parent_ids = got_object_commit_get_parent_ids(commit);
4620 STAILQ_FOREACH(qid, parent_ids, entry) {
4621 err = got_object_id_str(&id_str, &qid->id);
4622 if (err)
4623 goto done;
4624 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4625 if (n < 0) {
4626 err = got_error_from_errno("fprintf");
4627 goto done;
4629 outoff += n;
4630 err = add_line_metadata(lines, nlines, outoff,
4631 GOT_DIFF_LINE_META);
4632 if (err)
4633 goto done;
4634 free(id_str);
4635 id_str = NULL;
4639 err = got_object_commit_get_logmsg(&logmsg, commit);
4640 if (err)
4641 goto done;
4642 s = logmsg;
4643 while ((line = strsep(&s, "\n")) != NULL) {
4644 n = fprintf(outfile, "%s\n", line);
4645 if (n < 0) {
4646 err = got_error_from_errno("fprintf");
4647 goto done;
4649 outoff += n;
4650 err = add_line_metadata(lines, nlines, outoff,
4651 GOT_DIFF_LINE_LOGMSG);
4652 if (err)
4653 goto done;
4656 err = get_changed_paths(&changed_paths, commit, repo);
4657 if (err)
4658 goto done;
4659 TAILQ_FOREACH(pe, &changed_paths, entry) {
4660 struct got_diff_changed_path *cp = pe->data;
4661 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4662 if (n < 0) {
4663 err = got_error_from_errno("fprintf");
4664 goto done;
4666 outoff += n;
4667 err = add_line_metadata(lines, nlines, outoff,
4668 GOT_DIFF_LINE_CHANGES);
4669 if (err)
4670 goto done;
4671 free((char *)pe->path);
4672 free(pe->data);
4675 fputc('\n', outfile);
4676 outoff++;
4677 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4678 done:
4679 got_pathlist_free(&changed_paths);
4680 free(id_str);
4681 free(logmsg);
4682 free(refs_str);
4683 got_object_commit_close(commit);
4684 if (err) {
4685 free(*lines);
4686 *lines = NULL;
4687 *nlines = 0;
4689 return err;
4692 static const struct got_error *
4693 create_diff(struct tog_diff_view_state *s)
4695 const struct got_error *err = NULL;
4696 FILE *f = NULL;
4697 int obj_type;
4699 free(s->lines);
4700 s->lines = malloc(sizeof(*s->lines));
4701 if (s->lines == NULL)
4702 return got_error_from_errno("malloc");
4703 s->nlines = 0;
4705 f = got_opentemp();
4706 if (f == NULL) {
4707 err = got_error_from_errno("got_opentemp");
4708 goto done;
4710 if (s->f && fclose(s->f) == EOF) {
4711 err = got_error_from_errno("fclose");
4712 goto done;
4714 s->f = f;
4716 if (s->id1)
4717 err = got_object_get_type(&obj_type, s->repo, s->id1);
4718 else
4719 err = got_object_get_type(&obj_type, s->repo, s->id2);
4720 if (err)
4721 goto done;
4723 switch (obj_type) {
4724 case GOT_OBJ_TYPE_BLOB:
4725 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4726 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4727 s->label1, s->label2, tog_diff_algo, s->diff_context,
4728 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4729 break;
4730 case GOT_OBJ_TYPE_TREE:
4731 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4732 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4733 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4734 s->force_text_diff, s->repo, s->f);
4735 break;
4736 case GOT_OBJ_TYPE_COMMIT: {
4737 const struct got_object_id_queue *parent_ids;
4738 struct got_object_qid *pid;
4739 struct got_commit_object *commit2;
4740 struct got_reflist_head *refs;
4742 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4743 if (err)
4744 goto done;
4745 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4746 /* Show commit info if we're diffing to a parent/root commit. */
4747 if (s->id1 == NULL) {
4748 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4749 refs, s->repo, s->f);
4750 if (err)
4751 goto done;
4752 } else {
4753 parent_ids = got_object_commit_get_parent_ids(commit2);
4754 STAILQ_FOREACH(pid, parent_ids, entry) {
4755 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4756 err = write_commit_info(&s->lines,
4757 &s->nlines, s->id2, refs, s->repo,
4758 s->f);
4759 if (err)
4760 goto done;
4761 break;
4765 got_object_commit_close(commit2);
4767 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4768 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4769 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4770 s->force_text_diff, s->repo, s->f);
4771 break;
4773 default:
4774 err = got_error(GOT_ERR_OBJ_TYPE);
4775 break;
4777 done:
4778 if (s->f && fflush(s->f) != 0 && err == NULL)
4779 err = got_error_from_errno("fflush");
4780 return err;
4783 static void
4784 diff_view_indicate_progress(struct tog_view *view)
4786 mvwaddstr(view->window, 0, 0, "diffing...");
4787 update_panels();
4788 doupdate();
4791 static const struct got_error *
4792 search_start_diff_view(struct tog_view *view)
4794 struct tog_diff_view_state *s = &view->state.diff;
4796 s->matched_line = 0;
4797 return NULL;
4800 static void
4801 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4802 size_t *nlines, int **first, int **last, int **match, int **selected)
4804 struct tog_diff_view_state *s = &view->state.diff;
4806 *f = s->f;
4807 *nlines = s->nlines;
4808 *line_offsets = NULL;
4809 *match = &s->matched_line;
4810 *first = &s->first_displayed_line;
4811 *last = &s->last_displayed_line;
4812 *selected = &s->selected_line;
4815 static const struct got_error *
4816 search_next_view_match(struct tog_view *view)
4818 const struct got_error *err = NULL;
4819 FILE *f;
4820 int lineno;
4821 char *line = NULL;
4822 size_t linesize = 0;
4823 ssize_t linelen;
4824 off_t *line_offsets;
4825 size_t nlines = 0;
4826 int *first, *last, *match, *selected;
4828 if (!view->search_setup)
4829 return got_error_msg(GOT_ERR_NOT_IMPL,
4830 "view search not supported");
4831 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4832 &match, &selected);
4834 if (!view->searching) {
4835 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4836 return NULL;
4839 if (*match) {
4840 if (view->searching == TOG_SEARCH_FORWARD)
4841 lineno = *match + 1;
4842 else
4843 lineno = *match - 1;
4844 } else
4845 lineno = *first - 1 + *selected;
4847 while (1) {
4848 off_t offset;
4850 if (lineno <= 0 || lineno > nlines) {
4851 if (*match == 0) {
4852 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4853 break;
4856 if (view->searching == TOG_SEARCH_FORWARD)
4857 lineno = 1;
4858 else
4859 lineno = nlines;
4862 offset = view->type == TOG_VIEW_DIFF ?
4863 view->state.diff.lines[lineno - 1].offset :
4864 line_offsets[lineno - 1];
4865 if (fseeko(f, offset, SEEK_SET) != 0) {
4866 free(line);
4867 return got_error_from_errno("fseeko");
4869 linelen = getline(&line, &linesize, f);
4870 if (linelen != -1) {
4871 char *exstr;
4872 err = expand_tab(&exstr, line);
4873 if (err)
4874 break;
4875 if (match_line(exstr, &view->regex, 1,
4876 &view->regmatch)) {
4877 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4878 *match = lineno;
4879 free(exstr);
4880 break;
4882 free(exstr);
4884 if (view->searching == TOG_SEARCH_FORWARD)
4885 lineno++;
4886 else
4887 lineno--;
4889 free(line);
4891 if (*match) {
4892 *first = *match;
4893 *selected = 1;
4896 return err;
4899 static const struct got_error *
4900 close_diff_view(struct tog_view *view)
4902 const struct got_error *err = NULL;
4903 struct tog_diff_view_state *s = &view->state.diff;
4905 free(s->id1);
4906 s->id1 = NULL;
4907 free(s->id2);
4908 s->id2 = NULL;
4909 if (s->f && fclose(s->f) == EOF)
4910 err = got_error_from_errno("fclose");
4911 s->f = NULL;
4912 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4913 err = got_error_from_errno("fclose");
4914 s->f1 = NULL;
4915 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4916 err = got_error_from_errno("fclose");
4917 s->f2 = NULL;
4918 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4919 err = got_error_from_errno("close");
4920 s->fd1 = -1;
4921 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4922 err = got_error_from_errno("close");
4923 s->fd2 = -1;
4924 free(s->lines);
4925 s->lines = NULL;
4926 s->nlines = 0;
4927 return err;
4930 static const struct got_error *
4931 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4932 struct got_object_id *id2, const char *label1, const char *label2,
4933 int diff_context, int ignore_whitespace, int force_text_diff,
4934 struct tog_view *parent_view, struct got_repository *repo)
4936 const struct got_error *err;
4937 struct tog_diff_view_state *s = &view->state.diff;
4939 memset(s, 0, sizeof(*s));
4940 s->fd1 = -1;
4941 s->fd2 = -1;
4943 if (id1 != NULL && id2 != NULL) {
4944 int type1, type2;
4945 err = got_object_get_type(&type1, repo, id1);
4946 if (err)
4947 return err;
4948 err = got_object_get_type(&type2, repo, id2);
4949 if (err)
4950 return err;
4952 if (type1 != type2)
4953 return got_error(GOT_ERR_OBJ_TYPE);
4955 s->first_displayed_line = 1;
4956 s->last_displayed_line = view->nlines;
4957 s->selected_line = 1;
4958 s->repo = repo;
4959 s->id1 = id1;
4960 s->id2 = id2;
4961 s->label1 = label1;
4962 s->label2 = label2;
4964 if (id1) {
4965 s->id1 = got_object_id_dup(id1);
4966 if (s->id1 == NULL)
4967 return got_error_from_errno("got_object_id_dup");
4968 } else
4969 s->id1 = NULL;
4971 s->id2 = got_object_id_dup(id2);
4972 if (s->id2 == NULL) {
4973 err = got_error_from_errno("got_object_id_dup");
4974 goto done;
4977 s->f1 = got_opentemp();
4978 if (s->f1 == NULL) {
4979 err = got_error_from_errno("got_opentemp");
4980 goto done;
4983 s->f2 = got_opentemp();
4984 if (s->f2 == NULL) {
4985 err = got_error_from_errno("got_opentemp");
4986 goto done;
4989 s->fd1 = got_opentempfd();
4990 if (s->fd1 == -1) {
4991 err = got_error_from_errno("got_opentempfd");
4992 goto done;
4995 s->fd2 = got_opentempfd();
4996 if (s->fd2 == -1) {
4997 err = got_error_from_errno("got_opentempfd");
4998 goto done;
5001 s->first_displayed_line = 1;
5002 s->last_displayed_line = view->nlines;
5003 s->diff_context = diff_context;
5004 s->ignore_whitespace = ignore_whitespace;
5005 s->force_text_diff = force_text_diff;
5006 s->parent_view = parent_view;
5007 s->repo = repo;
5009 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5010 int rc;
5012 rc = init_pair(GOT_DIFF_LINE_MINUS,
5013 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5014 if (rc != ERR)
5015 rc = init_pair(GOT_DIFF_LINE_PLUS,
5016 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5017 if (rc != ERR)
5018 rc = init_pair(GOT_DIFF_LINE_HUNK,
5019 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5020 if (rc != ERR)
5021 rc = init_pair(GOT_DIFF_LINE_META,
5022 get_color_value("TOG_COLOR_DIFF_META"), -1);
5023 if (rc != ERR)
5024 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5025 get_color_value("TOG_COLOR_DIFF_META"), -1);
5026 if (rc != ERR)
5027 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5028 get_color_value("TOG_COLOR_DIFF_META"), -1);
5029 if (rc != ERR)
5030 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5031 get_color_value("TOG_COLOR_DIFF_META"), -1);
5032 if (rc != ERR)
5033 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5034 get_color_value("TOG_COLOR_AUTHOR"), -1);
5035 if (rc != ERR)
5036 rc = init_pair(GOT_DIFF_LINE_DATE,
5037 get_color_value("TOG_COLOR_DATE"), -1);
5038 if (rc == ERR) {
5039 err = got_error(GOT_ERR_RANGE);
5040 goto done;
5044 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5045 view_is_splitscreen(view))
5046 show_log_view(parent_view); /* draw border */
5047 diff_view_indicate_progress(view);
5049 err = create_diff(s);
5051 view->show = show_diff_view;
5052 view->input = input_diff_view;
5053 view->reset = reset_diff_view;
5054 view->close = close_diff_view;
5055 view->search_start = search_start_diff_view;
5056 view->search_setup = search_setup_diff_view;
5057 view->search_next = search_next_view_match;
5058 done:
5059 if (err)
5060 close_diff_view(view);
5061 return err;
5064 static const struct got_error *
5065 show_diff_view(struct tog_view *view)
5067 const struct got_error *err;
5068 struct tog_diff_view_state *s = &view->state.diff;
5069 char *id_str1 = NULL, *id_str2, *header;
5070 const char *label1, *label2;
5072 if (s->id1) {
5073 err = got_object_id_str(&id_str1, s->id1);
5074 if (err)
5075 return err;
5076 label1 = s->label1 ? s->label1 : id_str1;
5077 } else
5078 label1 = "/dev/null";
5080 err = got_object_id_str(&id_str2, s->id2);
5081 if (err)
5082 return err;
5083 label2 = s->label2 ? s->label2 : id_str2;
5085 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5086 err = got_error_from_errno("asprintf");
5087 free(id_str1);
5088 free(id_str2);
5089 return err;
5091 free(id_str1);
5092 free(id_str2);
5094 err = draw_file(view, header);
5095 free(header);
5096 return err;
5099 static const struct got_error *
5100 set_selected_commit(struct tog_diff_view_state *s,
5101 struct commit_queue_entry *entry)
5103 const struct got_error *err;
5104 const struct got_object_id_queue *parent_ids;
5105 struct got_commit_object *selected_commit;
5106 struct got_object_qid *pid;
5108 free(s->id2);
5109 s->id2 = got_object_id_dup(entry->id);
5110 if (s->id2 == NULL)
5111 return got_error_from_errno("got_object_id_dup");
5113 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5114 if (err)
5115 return err;
5116 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5117 free(s->id1);
5118 pid = STAILQ_FIRST(parent_ids);
5119 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5120 got_object_commit_close(selected_commit);
5121 return NULL;
5124 static const struct got_error *
5125 reset_diff_view(struct tog_view *view)
5127 struct tog_diff_view_state *s = &view->state.diff;
5129 view->count = 0;
5130 wclear(view->window);
5131 s->first_displayed_line = 1;
5132 s->last_displayed_line = view->nlines;
5133 s->matched_line = 0;
5134 diff_view_indicate_progress(view);
5135 return create_diff(s);
5138 static void
5139 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5141 int start, i;
5143 i = start = s->first_displayed_line - 1;
5145 while (s->lines[i].type != type) {
5146 if (i == 0)
5147 i = s->nlines - 1;
5148 if (--i == start)
5149 return; /* do nothing, requested type not in file */
5152 s->selected_line = 1;
5153 s->first_displayed_line = i;
5156 static void
5157 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5159 int start, i;
5161 i = start = s->first_displayed_line + 1;
5163 while (s->lines[i].type != type) {
5164 if (i == s->nlines - 1)
5165 i = 0;
5166 if (++i == start)
5167 return; /* do nothing, requested type not in file */
5170 s->selected_line = 1;
5171 s->first_displayed_line = i;
5174 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5175 int, int, int);
5176 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5177 int, int);
5179 static const struct got_error *
5180 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5182 const struct got_error *err = NULL;
5183 struct tog_diff_view_state *s = &view->state.diff;
5184 struct tog_log_view_state *ls;
5185 struct commit_queue_entry *old_selected_entry;
5186 char *line = NULL;
5187 size_t linesize = 0;
5188 ssize_t linelen;
5189 int i, nscroll = view->nlines - 1, up = 0;
5191 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5193 switch (ch) {
5194 case '0':
5195 view->x = 0;
5196 break;
5197 case '$':
5198 view->x = MAX(view->maxx - view->ncols / 3, 0);
5199 view->count = 0;
5200 break;
5201 case KEY_RIGHT:
5202 case 'l':
5203 if (view->x + view->ncols / 3 < view->maxx)
5204 view->x += 2; /* move two columns right */
5205 else
5206 view->count = 0;
5207 break;
5208 case KEY_LEFT:
5209 case 'h':
5210 view->x -= MIN(view->x, 2); /* move two columns back */
5211 if (view->x <= 0)
5212 view->count = 0;
5213 break;
5214 case 'a':
5215 case 'w':
5216 if (ch == 'a')
5217 s->force_text_diff = !s->force_text_diff;
5218 if (ch == 'w')
5219 s->ignore_whitespace = !s->ignore_whitespace;
5220 err = reset_diff_view(view);
5221 break;
5222 case 'g':
5223 case KEY_HOME:
5224 s->first_displayed_line = 1;
5225 view->count = 0;
5226 break;
5227 case 'G':
5228 case KEY_END:
5229 view->count = 0;
5230 if (s->eof)
5231 break;
5233 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5234 s->eof = 1;
5235 break;
5236 case 'k':
5237 case KEY_UP:
5238 case CTRL('p'):
5239 if (s->first_displayed_line > 1)
5240 s->first_displayed_line--;
5241 else
5242 view->count = 0;
5243 break;
5244 case CTRL('u'):
5245 case 'u':
5246 nscroll /= 2;
5247 /* FALL THROUGH */
5248 case KEY_PPAGE:
5249 case CTRL('b'):
5250 case 'b':
5251 if (s->first_displayed_line == 1) {
5252 view->count = 0;
5253 break;
5255 i = 0;
5256 while (i++ < nscroll && s->first_displayed_line > 1)
5257 s->first_displayed_line--;
5258 break;
5259 case 'j':
5260 case KEY_DOWN:
5261 case CTRL('n'):
5262 if (!s->eof)
5263 s->first_displayed_line++;
5264 else
5265 view->count = 0;
5266 break;
5267 case CTRL('d'):
5268 case 'd':
5269 nscroll /= 2;
5270 /* FALL THROUGH */
5271 case KEY_NPAGE:
5272 case CTRL('f'):
5273 case 'f':
5274 case ' ':
5275 if (s->eof) {
5276 view->count = 0;
5277 break;
5279 i = 0;
5280 while (!s->eof && i++ < nscroll) {
5281 linelen = getline(&line, &linesize, s->f);
5282 s->first_displayed_line++;
5283 if (linelen == -1) {
5284 if (feof(s->f)) {
5285 s->eof = 1;
5286 } else
5287 err = got_ferror(s->f, GOT_ERR_IO);
5288 break;
5291 free(line);
5292 break;
5293 case '(':
5294 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5295 break;
5296 case ')':
5297 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5298 break;
5299 case '{':
5300 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5301 break;
5302 case '}':
5303 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5304 break;
5305 case '[':
5306 if (s->diff_context > 0) {
5307 s->diff_context--;
5308 s->matched_line = 0;
5309 diff_view_indicate_progress(view);
5310 err = create_diff(s);
5311 if (s->first_displayed_line + view->nlines - 1 >
5312 s->nlines) {
5313 s->first_displayed_line = 1;
5314 s->last_displayed_line = view->nlines;
5316 } else
5317 view->count = 0;
5318 break;
5319 case ']':
5320 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5321 s->diff_context++;
5322 s->matched_line = 0;
5323 diff_view_indicate_progress(view);
5324 err = create_diff(s);
5325 } else
5326 view->count = 0;
5327 break;
5328 case '<':
5329 case ',':
5330 case 'K':
5331 up = 1;
5332 /* FALL THROUGH */
5333 case '>':
5334 case '.':
5335 case 'J':
5336 if (s->parent_view == NULL) {
5337 view->count = 0;
5338 break;
5340 s->parent_view->count = view->count;
5342 if (s->parent_view->type == TOG_VIEW_LOG) {
5343 ls = &s->parent_view->state.log;
5344 old_selected_entry = ls->selected_entry;
5346 err = input_log_view(NULL, s->parent_view,
5347 up ? KEY_UP : KEY_DOWN);
5348 if (err)
5349 break;
5350 view->count = s->parent_view->count;
5352 if (old_selected_entry == ls->selected_entry)
5353 break;
5355 err = set_selected_commit(s, ls->selected_entry);
5356 if (err)
5357 break;
5358 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5359 struct tog_blame_view_state *bs;
5360 struct got_object_id *id, *prev_id;
5362 bs = &s->parent_view->state.blame;
5363 prev_id = get_annotation_for_line(bs->blame.lines,
5364 bs->blame.nlines, bs->last_diffed_line);
5366 err = input_blame_view(&view, s->parent_view,
5367 up ? KEY_UP : KEY_DOWN);
5368 if (err)
5369 break;
5370 view->count = s->parent_view->count;
5372 if (prev_id == NULL)
5373 break;
5374 id = get_selected_commit_id(bs->blame.lines,
5375 bs->blame.nlines, bs->first_displayed_line,
5376 bs->selected_line);
5377 if (id == NULL)
5378 break;
5380 if (!got_object_id_cmp(prev_id, id))
5381 break;
5383 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5384 if (err)
5385 break;
5387 s->first_displayed_line = 1;
5388 s->last_displayed_line = view->nlines;
5389 s->matched_line = 0;
5390 view->x = 0;
5392 diff_view_indicate_progress(view);
5393 err = create_diff(s);
5394 break;
5395 default:
5396 view->count = 0;
5397 break;
5400 return err;
5403 static const struct got_error *
5404 cmd_diff(int argc, char *argv[])
5406 const struct got_error *error = NULL;
5407 struct got_repository *repo = NULL;
5408 struct got_worktree *worktree = NULL;
5409 struct got_object_id *id1 = NULL, *id2 = NULL;
5410 char *repo_path = NULL, *cwd = NULL;
5411 char *id_str1 = NULL, *id_str2 = NULL;
5412 char *label1 = NULL, *label2 = NULL;
5413 int diff_context = 3, ignore_whitespace = 0;
5414 int ch, force_text_diff = 0;
5415 const char *errstr;
5416 struct tog_view *view;
5417 int *pack_fds = NULL;
5419 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5420 switch (ch) {
5421 case 'a':
5422 force_text_diff = 1;
5423 break;
5424 case 'C':
5425 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5426 &errstr);
5427 if (errstr != NULL)
5428 errx(1, "number of context lines is %s: %s",
5429 errstr, errstr);
5430 break;
5431 case 'r':
5432 repo_path = realpath(optarg, NULL);
5433 if (repo_path == NULL)
5434 return got_error_from_errno2("realpath",
5435 optarg);
5436 got_path_strip_trailing_slashes(repo_path);
5437 break;
5438 case 'w':
5439 ignore_whitespace = 1;
5440 break;
5441 default:
5442 usage_diff();
5443 /* NOTREACHED */
5447 argc -= optind;
5448 argv += optind;
5450 if (argc == 0) {
5451 usage_diff(); /* TODO show local worktree changes */
5452 } else if (argc == 2) {
5453 id_str1 = argv[0];
5454 id_str2 = argv[1];
5455 } else
5456 usage_diff();
5458 error = got_repo_pack_fds_open(&pack_fds);
5459 if (error)
5460 goto done;
5462 if (repo_path == NULL) {
5463 cwd = getcwd(NULL, 0);
5464 if (cwd == NULL)
5465 return got_error_from_errno("getcwd");
5466 error = got_worktree_open(&worktree, cwd);
5467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5468 goto done;
5469 if (worktree)
5470 repo_path =
5471 strdup(got_worktree_get_repo_path(worktree));
5472 else
5473 repo_path = strdup(cwd);
5474 if (repo_path == NULL) {
5475 error = got_error_from_errno("strdup");
5476 goto done;
5480 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5481 if (error)
5482 goto done;
5484 init_curses();
5486 error = apply_unveil(got_repo_get_path(repo), NULL);
5487 if (error)
5488 goto done;
5490 error = tog_load_refs(repo, 0);
5491 if (error)
5492 goto done;
5494 error = got_repo_match_object_id(&id1, &label1, id_str1,
5495 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5496 if (error)
5497 goto done;
5499 error = got_repo_match_object_id(&id2, &label2, id_str2,
5500 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5501 if (error)
5502 goto done;
5504 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5505 if (view == NULL) {
5506 error = got_error_from_errno("view_open");
5507 goto done;
5509 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5510 ignore_whitespace, force_text_diff, NULL, repo);
5511 if (error)
5512 goto done;
5513 error = view_loop(view);
5514 done:
5515 free(label1);
5516 free(label2);
5517 free(repo_path);
5518 free(cwd);
5519 if (repo) {
5520 const struct got_error *close_err = got_repo_close(repo);
5521 if (error == NULL)
5522 error = close_err;
5524 if (worktree)
5525 got_worktree_close(worktree);
5526 if (pack_fds) {
5527 const struct got_error *pack_err =
5528 got_repo_pack_fds_close(pack_fds);
5529 if (error == NULL)
5530 error = pack_err;
5532 tog_free_refs();
5533 return error;
5536 __dead static void
5537 usage_blame(void)
5539 endwin();
5540 fprintf(stderr,
5541 "usage: %s blame [-c commit] [-r repository-path] path\n",
5542 getprogname());
5543 exit(1);
5546 struct tog_blame_line {
5547 int annotated;
5548 struct got_object_id *id;
5551 static const struct got_error *
5552 draw_blame(struct tog_view *view)
5554 struct tog_blame_view_state *s = &view->state.blame;
5555 struct tog_blame *blame = &s->blame;
5556 regmatch_t *regmatch = &view->regmatch;
5557 const struct got_error *err;
5558 int lineno = 0, nprinted = 0;
5559 char *line = NULL;
5560 size_t linesize = 0;
5561 ssize_t linelen;
5562 wchar_t *wline;
5563 int width;
5564 struct tog_blame_line *blame_line;
5565 struct got_object_id *prev_id = NULL;
5566 char *id_str;
5567 struct tog_color *tc;
5569 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5570 if (err)
5571 return err;
5573 rewind(blame->f);
5574 werase(view->window);
5576 if (asprintf(&line, "commit %s", id_str) == -1) {
5577 err = got_error_from_errno("asprintf");
5578 free(id_str);
5579 return err;
5582 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5583 free(line);
5584 line = NULL;
5585 if (err)
5586 return err;
5587 if (view_needs_focus_indication(view))
5588 wstandout(view->window);
5589 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5590 if (tc)
5591 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5592 waddwstr(view->window, wline);
5593 while (width++ < view->ncols)
5594 waddch(view->window, ' ');
5595 if (tc)
5596 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5597 if (view_needs_focus_indication(view))
5598 wstandend(view->window);
5599 free(wline);
5600 wline = NULL;
5602 if (view->gline > blame->nlines)
5603 view->gline = blame->nlines;
5605 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5606 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5607 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5608 free(id_str);
5609 return got_error_from_errno("asprintf");
5611 free(id_str);
5612 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5613 free(line);
5614 line = NULL;
5615 if (err)
5616 return err;
5617 waddwstr(view->window, wline);
5618 free(wline);
5619 wline = NULL;
5620 if (width < view->ncols - 1)
5621 waddch(view->window, '\n');
5623 s->eof = 0;
5624 view->maxx = 0;
5625 while (nprinted < view->nlines - 2) {
5626 linelen = getline(&line, &linesize, blame->f);
5627 if (linelen == -1) {
5628 if (feof(blame->f)) {
5629 s->eof = 1;
5630 break;
5632 free(line);
5633 return got_ferror(blame->f, GOT_ERR_IO);
5635 if (++lineno < s->first_displayed_line)
5636 continue;
5637 if (view->gline && !gotoline(view, &lineno, &nprinted))
5638 continue;
5640 /* Set view->maxx based on full line length. */
5641 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5642 if (err) {
5643 free(line);
5644 return err;
5646 free(wline);
5647 wline = NULL;
5648 view->maxx = MAX(view->maxx, width);
5650 if (nprinted == s->selected_line - 1)
5651 wstandout(view->window);
5653 if (blame->nlines > 0) {
5654 blame_line = &blame->lines[lineno - 1];
5655 if (blame_line->annotated && prev_id &&
5656 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5657 !(nprinted == s->selected_line - 1)) {
5658 waddstr(view->window, " ");
5659 } else if (blame_line->annotated) {
5660 char *id_str;
5661 err = got_object_id_str(&id_str,
5662 blame_line->id);
5663 if (err) {
5664 free(line);
5665 return err;
5667 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5668 if (tc)
5669 wattr_on(view->window,
5670 COLOR_PAIR(tc->colorpair), NULL);
5671 wprintw(view->window, "%.8s", id_str);
5672 if (tc)
5673 wattr_off(view->window,
5674 COLOR_PAIR(tc->colorpair), NULL);
5675 free(id_str);
5676 prev_id = blame_line->id;
5677 } else {
5678 waddstr(view->window, "........");
5679 prev_id = NULL;
5681 } else {
5682 waddstr(view->window, "........");
5683 prev_id = NULL;
5686 if (nprinted == s->selected_line - 1)
5687 wstandend(view->window);
5688 waddstr(view->window, " ");
5690 if (view->ncols <= 9) {
5691 width = 9;
5692 } else if (s->first_displayed_line + nprinted ==
5693 s->matched_line &&
5694 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5695 err = add_matched_line(&width, line, view->ncols - 9, 9,
5696 view->window, view->x, regmatch);
5697 if (err) {
5698 free(line);
5699 return err;
5701 width += 9;
5702 } else {
5703 int skip;
5704 err = format_line(&wline, &width, &skip, line,
5705 view->x, view->ncols - 9, 9, 1);
5706 if (err) {
5707 free(line);
5708 return err;
5710 waddwstr(view->window, &wline[skip]);
5711 width += 9;
5712 free(wline);
5713 wline = NULL;
5716 if (width <= view->ncols - 1)
5717 waddch(view->window, '\n');
5718 if (++nprinted == 1)
5719 s->first_displayed_line = lineno;
5721 free(line);
5722 s->last_displayed_line = lineno;
5724 view_border(view);
5726 return NULL;
5729 static const struct got_error *
5730 blame_cb(void *arg, int nlines, int lineno,
5731 struct got_commit_object *commit, struct got_object_id *id)
5733 const struct got_error *err = NULL;
5734 struct tog_blame_cb_args *a = arg;
5735 struct tog_blame_line *line;
5736 int errcode;
5738 if (nlines != a->nlines ||
5739 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5740 return got_error(GOT_ERR_RANGE);
5742 errcode = pthread_mutex_lock(&tog_mutex);
5743 if (errcode)
5744 return got_error_set_errno(errcode, "pthread_mutex_lock");
5746 if (*a->quit) { /* user has quit the blame view */
5747 err = got_error(GOT_ERR_ITER_COMPLETED);
5748 goto done;
5751 if (lineno == -1)
5752 goto done; /* no change in this commit */
5754 line = &a->lines[lineno - 1];
5755 if (line->annotated)
5756 goto done;
5758 line->id = got_object_id_dup(id);
5759 if (line->id == NULL) {
5760 err = got_error_from_errno("got_object_id_dup");
5761 goto done;
5763 line->annotated = 1;
5764 done:
5765 errcode = pthread_mutex_unlock(&tog_mutex);
5766 if (errcode)
5767 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5768 return err;
5771 static void *
5772 blame_thread(void *arg)
5774 const struct got_error *err, *close_err;
5775 struct tog_blame_thread_args *ta = arg;
5776 struct tog_blame_cb_args *a = ta->cb_args;
5777 int errcode, fd1 = -1, fd2 = -1;
5778 FILE *f1 = NULL, *f2 = NULL;
5780 fd1 = got_opentempfd();
5781 if (fd1 == -1)
5782 return (void *)got_error_from_errno("got_opentempfd");
5784 fd2 = got_opentempfd();
5785 if (fd2 == -1) {
5786 err = got_error_from_errno("got_opentempfd");
5787 goto done;
5790 f1 = got_opentemp();
5791 if (f1 == NULL) {
5792 err = (void *)got_error_from_errno("got_opentemp");
5793 goto done;
5795 f2 = got_opentemp();
5796 if (f2 == NULL) {
5797 err = (void *)got_error_from_errno("got_opentemp");
5798 goto done;
5801 err = block_signals_used_by_main_thread();
5802 if (err)
5803 goto done;
5805 err = got_blame(ta->path, a->commit_id, ta->repo,
5806 tog_diff_algo, blame_cb, ta->cb_args,
5807 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5808 if (err && err->code == GOT_ERR_CANCELLED)
5809 err = NULL;
5811 errcode = pthread_mutex_lock(&tog_mutex);
5812 if (errcode) {
5813 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5814 goto done;
5817 close_err = got_repo_close(ta->repo);
5818 if (err == NULL)
5819 err = close_err;
5820 ta->repo = NULL;
5821 *ta->complete = 1;
5823 errcode = pthread_mutex_unlock(&tog_mutex);
5824 if (errcode && err == NULL)
5825 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5827 done:
5828 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5829 err = got_error_from_errno("close");
5830 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5831 err = got_error_from_errno("close");
5832 if (f1 && fclose(f1) == EOF && err == NULL)
5833 err = got_error_from_errno("fclose");
5834 if (f2 && fclose(f2) == EOF && err == NULL)
5835 err = got_error_from_errno("fclose");
5837 return (void *)err;
5840 static struct got_object_id *
5841 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5842 int first_displayed_line, int selected_line)
5844 struct tog_blame_line *line;
5846 if (nlines <= 0)
5847 return NULL;
5849 line = &lines[first_displayed_line - 1 + selected_line - 1];
5850 if (!line->annotated)
5851 return NULL;
5853 return line->id;
5856 static struct got_object_id *
5857 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5858 int lineno)
5860 struct tog_blame_line *line;
5862 if (nlines <= 0 || lineno >= nlines)
5863 return NULL;
5865 line = &lines[lineno - 1];
5866 if (!line->annotated)
5867 return NULL;
5869 return line->id;
5872 static const struct got_error *
5873 stop_blame(struct tog_blame *blame)
5875 const struct got_error *err = NULL;
5876 int i;
5878 if (blame->thread) {
5879 int errcode;
5880 errcode = pthread_mutex_unlock(&tog_mutex);
5881 if (errcode)
5882 return got_error_set_errno(errcode,
5883 "pthread_mutex_unlock");
5884 errcode = pthread_join(blame->thread, (void **)&err);
5885 if (errcode)
5886 return got_error_set_errno(errcode, "pthread_join");
5887 errcode = pthread_mutex_lock(&tog_mutex);
5888 if (errcode)
5889 return got_error_set_errno(errcode,
5890 "pthread_mutex_lock");
5891 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5892 err = NULL;
5893 blame->thread = 0; //NULL;
5895 if (blame->thread_args.repo) {
5896 const struct got_error *close_err;
5897 close_err = got_repo_close(blame->thread_args.repo);
5898 if (err == NULL)
5899 err = close_err;
5900 blame->thread_args.repo = NULL;
5902 if (blame->f) {
5903 if (fclose(blame->f) == EOF && err == NULL)
5904 err = got_error_from_errno("fclose");
5905 blame->f = NULL;
5907 if (blame->lines) {
5908 for (i = 0; i < blame->nlines; i++)
5909 free(blame->lines[i].id);
5910 free(blame->lines);
5911 blame->lines = NULL;
5913 free(blame->cb_args.commit_id);
5914 blame->cb_args.commit_id = NULL;
5915 if (blame->pack_fds) {
5916 const struct got_error *pack_err =
5917 got_repo_pack_fds_close(blame->pack_fds);
5918 if (err == NULL)
5919 err = pack_err;
5920 blame->pack_fds = NULL;
5922 return err;
5925 static const struct got_error *
5926 cancel_blame_view(void *arg)
5928 const struct got_error *err = NULL;
5929 int *done = arg;
5930 int errcode;
5932 errcode = pthread_mutex_lock(&tog_mutex);
5933 if (errcode)
5934 return got_error_set_errno(errcode,
5935 "pthread_mutex_unlock");
5937 if (*done)
5938 err = got_error(GOT_ERR_CANCELLED);
5940 errcode = pthread_mutex_unlock(&tog_mutex);
5941 if (errcode)
5942 return got_error_set_errno(errcode,
5943 "pthread_mutex_lock");
5945 return err;
5948 static const struct got_error *
5949 run_blame(struct tog_view *view)
5951 struct tog_blame_view_state *s = &view->state.blame;
5952 struct tog_blame *blame = &s->blame;
5953 const struct got_error *err = NULL;
5954 struct got_commit_object *commit = NULL;
5955 struct got_blob_object *blob = NULL;
5956 struct got_repository *thread_repo = NULL;
5957 struct got_object_id *obj_id = NULL;
5958 int obj_type, fd = -1;
5959 int *pack_fds = NULL;
5961 err = got_object_open_as_commit(&commit, s->repo,
5962 &s->blamed_commit->id);
5963 if (err)
5964 return err;
5966 fd = got_opentempfd();
5967 if (fd == -1) {
5968 err = got_error_from_errno("got_opentempfd");
5969 goto done;
5972 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5973 if (err)
5974 goto done;
5976 err = got_object_get_type(&obj_type, s->repo, obj_id);
5977 if (err)
5978 goto done;
5980 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5981 err = got_error(GOT_ERR_OBJ_TYPE);
5982 goto done;
5985 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5986 if (err)
5987 goto done;
5988 blame->f = got_opentemp();
5989 if (blame->f == NULL) {
5990 err = got_error_from_errno("got_opentemp");
5991 goto done;
5993 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5994 &blame->line_offsets, blame->f, blob);
5995 if (err)
5996 goto done;
5997 if (blame->nlines == 0) {
5998 s->blame_complete = 1;
5999 goto done;
6002 /* Don't include \n at EOF in the blame line count. */
6003 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6004 blame->nlines--;
6006 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6007 if (blame->lines == NULL) {
6008 err = got_error_from_errno("calloc");
6009 goto done;
6012 err = got_repo_pack_fds_open(&pack_fds);
6013 if (err)
6014 goto done;
6015 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6016 pack_fds);
6017 if (err)
6018 goto done;
6020 blame->pack_fds = pack_fds;
6021 blame->cb_args.view = view;
6022 blame->cb_args.lines = blame->lines;
6023 blame->cb_args.nlines = blame->nlines;
6024 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6025 if (blame->cb_args.commit_id == NULL) {
6026 err = got_error_from_errno("got_object_id_dup");
6027 goto done;
6029 blame->cb_args.quit = &s->done;
6031 blame->thread_args.path = s->path;
6032 blame->thread_args.repo = thread_repo;
6033 blame->thread_args.cb_args = &blame->cb_args;
6034 blame->thread_args.complete = &s->blame_complete;
6035 blame->thread_args.cancel_cb = cancel_blame_view;
6036 blame->thread_args.cancel_arg = &s->done;
6037 s->blame_complete = 0;
6039 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6040 s->first_displayed_line = 1;
6041 s->last_displayed_line = view->nlines;
6042 s->selected_line = 1;
6044 s->matched_line = 0;
6046 done:
6047 if (commit)
6048 got_object_commit_close(commit);
6049 if (fd != -1 && close(fd) == -1 && err == NULL)
6050 err = got_error_from_errno("close");
6051 if (blob)
6052 got_object_blob_close(blob);
6053 free(obj_id);
6054 if (err)
6055 stop_blame(blame);
6056 return err;
6059 static const struct got_error *
6060 open_blame_view(struct tog_view *view, char *path,
6061 struct got_object_id *commit_id, struct got_repository *repo)
6063 const struct got_error *err = NULL;
6064 struct tog_blame_view_state *s = &view->state.blame;
6066 STAILQ_INIT(&s->blamed_commits);
6068 s->path = strdup(path);
6069 if (s->path == NULL)
6070 return got_error_from_errno("strdup");
6072 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6073 if (err) {
6074 free(s->path);
6075 return err;
6078 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6079 s->first_displayed_line = 1;
6080 s->last_displayed_line = view->nlines;
6081 s->selected_line = 1;
6082 s->blame_complete = 0;
6083 s->repo = repo;
6084 s->commit_id = commit_id;
6085 memset(&s->blame, 0, sizeof(s->blame));
6087 STAILQ_INIT(&s->colors);
6088 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6089 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6090 get_color_value("TOG_COLOR_COMMIT"));
6091 if (err)
6092 return err;
6095 view->show = show_blame_view;
6096 view->input = input_blame_view;
6097 view->reset = reset_blame_view;
6098 view->close = close_blame_view;
6099 view->search_start = search_start_blame_view;
6100 view->search_setup = search_setup_blame_view;
6101 view->search_next = search_next_view_match;
6103 return run_blame(view);
6106 static const struct got_error *
6107 close_blame_view(struct tog_view *view)
6109 const struct got_error *err = NULL;
6110 struct tog_blame_view_state *s = &view->state.blame;
6112 if (s->blame.thread)
6113 err = stop_blame(&s->blame);
6115 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6116 struct got_object_qid *blamed_commit;
6117 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6118 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6119 got_object_qid_free(blamed_commit);
6122 free(s->path);
6123 free_colors(&s->colors);
6124 return err;
6127 static const struct got_error *
6128 search_start_blame_view(struct tog_view *view)
6130 struct tog_blame_view_state *s = &view->state.blame;
6132 s->matched_line = 0;
6133 return NULL;
6136 static void
6137 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6138 size_t *nlines, int **first, int **last, int **match, int **selected)
6140 struct tog_blame_view_state *s = &view->state.blame;
6142 *f = s->blame.f;
6143 *nlines = s->blame.nlines;
6144 *line_offsets = s->blame.line_offsets;
6145 *match = &s->matched_line;
6146 *first = &s->first_displayed_line;
6147 *last = &s->last_displayed_line;
6148 *selected = &s->selected_line;
6151 static const struct got_error *
6152 show_blame_view(struct tog_view *view)
6154 const struct got_error *err = NULL;
6155 struct tog_blame_view_state *s = &view->state.blame;
6156 int errcode;
6158 if (s->blame.thread == 0 && !s->blame_complete) {
6159 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6160 &s->blame.thread_args);
6161 if (errcode)
6162 return got_error_set_errno(errcode, "pthread_create");
6164 halfdelay(1); /* fast refresh while annotating */
6167 if (s->blame_complete)
6168 halfdelay(10); /* disable fast refresh */
6170 err = draw_blame(view);
6172 view_border(view);
6173 return err;
6176 static const struct got_error *
6177 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6178 struct got_repository *repo, struct got_object_id *id)
6180 struct tog_view *log_view;
6181 const struct got_error *err = NULL;
6183 *new_view = NULL;
6185 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6186 if (log_view == NULL)
6187 return got_error_from_errno("view_open");
6189 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6190 if (err)
6191 view_close(log_view);
6192 else
6193 *new_view = log_view;
6195 return err;
6198 static const struct got_error *
6199 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6201 const struct got_error *err = NULL, *thread_err = NULL;
6202 struct tog_view *diff_view;
6203 struct tog_blame_view_state *s = &view->state.blame;
6204 int eos, nscroll, begin_y = 0, begin_x = 0;
6206 eos = nscroll = view->nlines - 2;
6207 if (view_is_hsplit_top(view))
6208 --eos; /* border */
6210 switch (ch) {
6211 case '0':
6212 view->x = 0;
6213 break;
6214 case '$':
6215 view->x = MAX(view->maxx - view->ncols / 3, 0);
6216 view->count = 0;
6217 break;
6218 case KEY_RIGHT:
6219 case 'l':
6220 if (view->x + view->ncols / 3 < view->maxx)
6221 view->x += 2; /* move two columns right */
6222 else
6223 view->count = 0;
6224 break;
6225 case KEY_LEFT:
6226 case 'h':
6227 view->x -= MIN(view->x, 2); /* move two columns back */
6228 if (view->x <= 0)
6229 view->count = 0;
6230 break;
6231 case 'q':
6232 s->done = 1;
6233 break;
6234 case 'g':
6235 case KEY_HOME:
6236 s->selected_line = 1;
6237 s->first_displayed_line = 1;
6238 view->count = 0;
6239 break;
6240 case 'G':
6241 case KEY_END:
6242 if (s->blame.nlines < eos) {
6243 s->selected_line = s->blame.nlines;
6244 s->first_displayed_line = 1;
6245 } else {
6246 s->selected_line = eos;
6247 s->first_displayed_line = s->blame.nlines - (eos - 1);
6249 view->count = 0;
6250 break;
6251 case 'k':
6252 case KEY_UP:
6253 case CTRL('p'):
6254 if (s->selected_line > 1)
6255 s->selected_line--;
6256 else if (s->selected_line == 1 &&
6257 s->first_displayed_line > 1)
6258 s->first_displayed_line--;
6259 else
6260 view->count = 0;
6261 break;
6262 case CTRL('u'):
6263 case 'u':
6264 nscroll /= 2;
6265 /* FALL THROUGH */
6266 case KEY_PPAGE:
6267 case CTRL('b'):
6268 case 'b':
6269 if (s->first_displayed_line == 1) {
6270 if (view->count > 1)
6271 nscroll += nscroll;
6272 s->selected_line = MAX(1, s->selected_line - nscroll);
6273 view->count = 0;
6274 break;
6276 if (s->first_displayed_line > nscroll)
6277 s->first_displayed_line -= nscroll;
6278 else
6279 s->first_displayed_line = 1;
6280 break;
6281 case 'j':
6282 case KEY_DOWN:
6283 case CTRL('n'):
6284 if (s->selected_line < eos && s->first_displayed_line +
6285 s->selected_line <= s->blame.nlines)
6286 s->selected_line++;
6287 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6288 s->first_displayed_line++;
6289 else
6290 view->count = 0;
6291 break;
6292 case 'c':
6293 case 'p': {
6294 struct got_object_id *id = NULL;
6296 view->count = 0;
6297 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6298 s->first_displayed_line, s->selected_line);
6299 if (id == NULL)
6300 break;
6301 if (ch == 'p') {
6302 struct got_commit_object *commit, *pcommit;
6303 struct got_object_qid *pid;
6304 struct got_object_id *blob_id = NULL;
6305 int obj_type;
6306 err = got_object_open_as_commit(&commit,
6307 s->repo, id);
6308 if (err)
6309 break;
6310 pid = STAILQ_FIRST(
6311 got_object_commit_get_parent_ids(commit));
6312 if (pid == NULL) {
6313 got_object_commit_close(commit);
6314 break;
6316 /* Check if path history ends here. */
6317 err = got_object_open_as_commit(&pcommit,
6318 s->repo, &pid->id);
6319 if (err)
6320 break;
6321 err = got_object_id_by_path(&blob_id, s->repo,
6322 pcommit, s->path);
6323 got_object_commit_close(pcommit);
6324 if (err) {
6325 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6326 err = NULL;
6327 got_object_commit_close(commit);
6328 break;
6330 err = got_object_get_type(&obj_type, s->repo,
6331 blob_id);
6332 free(blob_id);
6333 /* Can't blame non-blob type objects. */
6334 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6335 got_object_commit_close(commit);
6336 break;
6338 err = got_object_qid_alloc(&s->blamed_commit,
6339 &pid->id);
6340 got_object_commit_close(commit);
6341 } else {
6342 if (got_object_id_cmp(id,
6343 &s->blamed_commit->id) == 0)
6344 break;
6345 err = got_object_qid_alloc(&s->blamed_commit,
6346 id);
6348 if (err)
6349 break;
6350 s->done = 1;
6351 thread_err = stop_blame(&s->blame);
6352 s->done = 0;
6353 if (thread_err)
6354 break;
6355 STAILQ_INSERT_HEAD(&s->blamed_commits,
6356 s->blamed_commit, entry);
6357 err = run_blame(view);
6358 if (err)
6359 break;
6360 break;
6362 case 'C': {
6363 struct got_object_qid *first;
6365 view->count = 0;
6366 first = STAILQ_FIRST(&s->blamed_commits);
6367 if (!got_object_id_cmp(&first->id, s->commit_id))
6368 break;
6369 s->done = 1;
6370 thread_err = stop_blame(&s->blame);
6371 s->done = 0;
6372 if (thread_err)
6373 break;
6374 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6375 got_object_qid_free(s->blamed_commit);
6376 s->blamed_commit =
6377 STAILQ_FIRST(&s->blamed_commits);
6378 err = run_blame(view);
6379 if (err)
6380 break;
6381 break;
6383 case 'L':
6384 view->count = 0;
6385 s->id_to_log = get_selected_commit_id(s->blame.lines,
6386 s->blame.nlines, s->first_displayed_line, s->selected_line);
6387 if (s->id_to_log)
6388 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6389 break;
6390 case KEY_ENTER:
6391 case '\r': {
6392 struct got_object_id *id = NULL;
6393 struct got_object_qid *pid;
6394 struct got_commit_object *commit = NULL;
6396 view->count = 0;
6397 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6398 s->first_displayed_line, s->selected_line);
6399 if (id == NULL)
6400 break;
6401 err = got_object_open_as_commit(&commit, s->repo, id);
6402 if (err)
6403 break;
6404 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6405 if (*new_view) {
6406 /* traversed from diff view, release diff resources */
6407 err = close_diff_view(*new_view);
6408 if (err)
6409 break;
6410 diff_view = *new_view;
6411 } else {
6412 if (view_is_parent_view(view))
6413 view_get_split(view, &begin_y, &begin_x);
6415 diff_view = view_open(0, 0, begin_y, begin_x,
6416 TOG_VIEW_DIFF);
6417 if (diff_view == NULL) {
6418 got_object_commit_close(commit);
6419 err = got_error_from_errno("view_open");
6420 break;
6423 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6424 id, NULL, NULL, 3, 0, 0, view, s->repo);
6425 got_object_commit_close(commit);
6426 if (err) {
6427 view_close(diff_view);
6428 break;
6430 s->last_diffed_line = s->first_displayed_line - 1 +
6431 s->selected_line;
6432 if (*new_view)
6433 break; /* still open from active diff view */
6434 if (view_is_parent_view(view) &&
6435 view->mode == TOG_VIEW_SPLIT_HRZN) {
6436 err = view_init_hsplit(view, begin_y);
6437 if (err)
6438 break;
6441 view->focussed = 0;
6442 diff_view->focussed = 1;
6443 diff_view->mode = view->mode;
6444 diff_view->nlines = view->lines - begin_y;
6445 if (view_is_parent_view(view)) {
6446 view_transfer_size(diff_view, view);
6447 err = view_close_child(view);
6448 if (err)
6449 break;
6450 err = view_set_child(view, diff_view);
6451 if (err)
6452 break;
6453 view->focus_child = 1;
6454 } else
6455 *new_view = diff_view;
6456 if (err)
6457 break;
6458 break;
6460 case CTRL('d'):
6461 case 'd':
6462 nscroll /= 2;
6463 /* FALL THROUGH */
6464 case KEY_NPAGE:
6465 case CTRL('f'):
6466 case 'f':
6467 case ' ':
6468 if (s->last_displayed_line >= s->blame.nlines &&
6469 s->selected_line >= MIN(s->blame.nlines,
6470 view->nlines - 2)) {
6471 view->count = 0;
6472 break;
6474 if (s->last_displayed_line >= s->blame.nlines &&
6475 s->selected_line < view->nlines - 2) {
6476 s->selected_line +=
6477 MIN(nscroll, s->last_displayed_line -
6478 s->first_displayed_line - s->selected_line + 1);
6480 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6481 s->first_displayed_line += nscroll;
6482 else
6483 s->first_displayed_line =
6484 s->blame.nlines - (view->nlines - 3);
6485 break;
6486 case KEY_RESIZE:
6487 if (s->selected_line > view->nlines - 2) {
6488 s->selected_line = MIN(s->blame.nlines,
6489 view->nlines - 2);
6491 break;
6492 default:
6493 view->count = 0;
6494 break;
6496 return thread_err ? thread_err : err;
6499 static const struct got_error *
6500 reset_blame_view(struct tog_view *view)
6502 const struct got_error *err;
6503 struct tog_blame_view_state *s = &view->state.blame;
6505 view->count = 0;
6506 s->done = 1;
6507 err = stop_blame(&s->blame);
6508 s->done = 0;
6509 if (err)
6510 return err;
6511 return run_blame(view);
6514 static const struct got_error *
6515 cmd_blame(int argc, char *argv[])
6517 const struct got_error *error;
6518 struct got_repository *repo = NULL;
6519 struct got_worktree *worktree = NULL;
6520 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6521 char *link_target = NULL;
6522 struct got_object_id *commit_id = NULL;
6523 struct got_commit_object *commit = NULL;
6524 char *commit_id_str = NULL;
6525 int ch;
6526 struct tog_view *view;
6527 int *pack_fds = NULL;
6529 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6530 switch (ch) {
6531 case 'c':
6532 commit_id_str = optarg;
6533 break;
6534 case 'r':
6535 repo_path = realpath(optarg, NULL);
6536 if (repo_path == NULL)
6537 return got_error_from_errno2("realpath",
6538 optarg);
6539 break;
6540 default:
6541 usage_blame();
6542 /* NOTREACHED */
6546 argc -= optind;
6547 argv += optind;
6549 if (argc != 1)
6550 usage_blame();
6552 error = got_repo_pack_fds_open(&pack_fds);
6553 if (error != NULL)
6554 goto done;
6556 if (repo_path == NULL) {
6557 cwd = getcwd(NULL, 0);
6558 if (cwd == NULL)
6559 return got_error_from_errno("getcwd");
6560 error = got_worktree_open(&worktree, cwd);
6561 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6562 goto done;
6563 if (worktree)
6564 repo_path =
6565 strdup(got_worktree_get_repo_path(worktree));
6566 else
6567 repo_path = strdup(cwd);
6568 if (repo_path == NULL) {
6569 error = got_error_from_errno("strdup");
6570 goto done;
6574 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6575 if (error != NULL)
6576 goto done;
6578 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6579 worktree);
6580 if (error)
6581 goto done;
6583 init_curses();
6585 error = apply_unveil(got_repo_get_path(repo), NULL);
6586 if (error)
6587 goto done;
6589 error = tog_load_refs(repo, 0);
6590 if (error)
6591 goto done;
6593 if (commit_id_str == NULL) {
6594 struct got_reference *head_ref;
6595 error = got_ref_open(&head_ref, repo, worktree ?
6596 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6597 if (error != NULL)
6598 goto done;
6599 error = got_ref_resolve(&commit_id, repo, head_ref);
6600 got_ref_close(head_ref);
6601 } else {
6602 error = got_repo_match_object_id(&commit_id, NULL,
6603 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6605 if (error != NULL)
6606 goto done;
6608 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6609 if (view == NULL) {
6610 error = got_error_from_errno("view_open");
6611 goto done;
6614 error = got_object_open_as_commit(&commit, repo, commit_id);
6615 if (error)
6616 goto done;
6618 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6619 commit, repo);
6620 if (error)
6621 goto done;
6623 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6624 commit_id, repo);
6625 if (error)
6626 goto done;
6627 if (worktree) {
6628 /* Release work tree lock. */
6629 got_worktree_close(worktree);
6630 worktree = NULL;
6632 error = view_loop(view);
6633 done:
6634 free(repo_path);
6635 free(in_repo_path);
6636 free(link_target);
6637 free(cwd);
6638 free(commit_id);
6639 if (commit)
6640 got_object_commit_close(commit);
6641 if (worktree)
6642 got_worktree_close(worktree);
6643 if (repo) {
6644 const struct got_error *close_err = got_repo_close(repo);
6645 if (error == NULL)
6646 error = close_err;
6648 if (pack_fds) {
6649 const struct got_error *pack_err =
6650 got_repo_pack_fds_close(pack_fds);
6651 if (error == NULL)
6652 error = pack_err;
6654 tog_free_refs();
6655 return error;
6658 static const struct got_error *
6659 draw_tree_entries(struct tog_view *view, const char *parent_path)
6661 struct tog_tree_view_state *s = &view->state.tree;
6662 const struct got_error *err = NULL;
6663 struct got_tree_entry *te;
6664 wchar_t *wline;
6665 char *index = NULL;
6666 struct tog_color *tc;
6667 int width, n, nentries, i = 1;
6668 int limit = view->nlines;
6670 s->ndisplayed = 0;
6671 if (view_is_hsplit_top(view))
6672 --limit; /* border */
6674 werase(view->window);
6676 if (limit == 0)
6677 return NULL;
6679 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6680 0, 0);
6681 if (err)
6682 return err;
6683 if (view_needs_focus_indication(view))
6684 wstandout(view->window);
6685 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6686 if (tc)
6687 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6688 waddwstr(view->window, wline);
6689 free(wline);
6690 wline = NULL;
6691 while (width++ < view->ncols)
6692 waddch(view->window, ' ');
6693 if (tc)
6694 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6695 if (view_needs_focus_indication(view))
6696 wstandend(view->window);
6697 if (--limit <= 0)
6698 return NULL;
6700 i += s->selected;
6701 if (s->first_displayed_entry) {
6702 i += got_tree_entry_get_index(s->first_displayed_entry);
6703 if (s->tree != s->root)
6704 ++i; /* account for ".." entry */
6706 nentries = got_object_tree_get_nentries(s->tree);
6707 if (asprintf(&index, "[%d/%d] %s",
6708 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6709 return got_error_from_errno("asprintf");
6710 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6711 free(index);
6712 if (err)
6713 return err;
6714 waddwstr(view->window, wline);
6715 free(wline);
6716 wline = NULL;
6717 if (width < view->ncols - 1)
6718 waddch(view->window, '\n');
6719 if (--limit <= 0)
6720 return NULL;
6721 waddch(view->window, '\n');
6722 if (--limit <= 0)
6723 return NULL;
6725 if (s->first_displayed_entry == NULL) {
6726 te = got_object_tree_get_first_entry(s->tree);
6727 if (s->selected == 0) {
6728 if (view->focussed)
6729 wstandout(view->window);
6730 s->selected_entry = NULL;
6732 waddstr(view->window, " ..\n"); /* parent directory */
6733 if (s->selected == 0 && view->focussed)
6734 wstandend(view->window);
6735 s->ndisplayed++;
6736 if (--limit <= 0)
6737 return NULL;
6738 n = 1;
6739 } else {
6740 n = 0;
6741 te = s->first_displayed_entry;
6744 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6745 char *line = NULL, *id_str = NULL, *link_target = NULL;
6746 const char *modestr = "";
6747 mode_t mode;
6749 te = got_object_tree_get_entry(s->tree, i);
6750 mode = got_tree_entry_get_mode(te);
6752 if (s->show_ids) {
6753 err = got_object_id_str(&id_str,
6754 got_tree_entry_get_id(te));
6755 if (err)
6756 return got_error_from_errno(
6757 "got_object_id_str");
6759 if (got_object_tree_entry_is_submodule(te))
6760 modestr = "$";
6761 else if (S_ISLNK(mode)) {
6762 int i;
6764 err = got_tree_entry_get_symlink_target(&link_target,
6765 te, s->repo);
6766 if (err) {
6767 free(id_str);
6768 return err;
6770 for (i = 0; i < strlen(link_target); i++) {
6771 if (!isprint((unsigned char)link_target[i]))
6772 link_target[i] = '?';
6774 modestr = "@";
6776 else if (S_ISDIR(mode))
6777 modestr = "/";
6778 else if (mode & S_IXUSR)
6779 modestr = "*";
6780 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6781 got_tree_entry_get_name(te), modestr,
6782 link_target ? " -> ": "",
6783 link_target ? link_target : "") == -1) {
6784 free(id_str);
6785 free(link_target);
6786 return got_error_from_errno("asprintf");
6788 free(id_str);
6789 free(link_target);
6790 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6791 0, 0);
6792 if (err) {
6793 free(line);
6794 break;
6796 if (n == s->selected) {
6797 if (view->focussed)
6798 wstandout(view->window);
6799 s->selected_entry = te;
6801 tc = match_color(&s->colors, line);
6802 if (tc)
6803 wattr_on(view->window,
6804 COLOR_PAIR(tc->colorpair), NULL);
6805 waddwstr(view->window, wline);
6806 if (tc)
6807 wattr_off(view->window,
6808 COLOR_PAIR(tc->colorpair), NULL);
6809 if (width < view->ncols - 1)
6810 waddch(view->window, '\n');
6811 if (n == s->selected && view->focussed)
6812 wstandend(view->window);
6813 free(line);
6814 free(wline);
6815 wline = NULL;
6816 n++;
6817 s->ndisplayed++;
6818 s->last_displayed_entry = te;
6819 if (--limit <= 0)
6820 break;
6823 return err;
6826 static void
6827 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6829 struct got_tree_entry *te;
6830 int isroot = s->tree == s->root;
6831 int i = 0;
6833 if (s->first_displayed_entry == NULL)
6834 return;
6836 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6837 while (i++ < maxscroll) {
6838 if (te == NULL) {
6839 if (!isroot)
6840 s->first_displayed_entry = NULL;
6841 break;
6843 s->first_displayed_entry = te;
6844 te = got_tree_entry_get_prev(s->tree, te);
6848 static const struct got_error *
6849 tree_scroll_down(struct tog_view *view, int maxscroll)
6851 struct tog_tree_view_state *s = &view->state.tree;
6852 struct got_tree_entry *next, *last;
6853 int n = 0;
6855 if (s->first_displayed_entry)
6856 next = got_tree_entry_get_next(s->tree,
6857 s->first_displayed_entry);
6858 else
6859 next = got_object_tree_get_first_entry(s->tree);
6861 last = s->last_displayed_entry;
6862 while (next && n++ < maxscroll) {
6863 if (last) {
6864 s->last_displayed_entry = last;
6865 last = got_tree_entry_get_next(s->tree, last);
6867 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6868 s->first_displayed_entry = next;
6869 next = got_tree_entry_get_next(s->tree, next);
6873 return NULL;
6876 static const struct got_error *
6877 tree_entry_path(char **path, struct tog_parent_trees *parents,
6878 struct got_tree_entry *te)
6880 const struct got_error *err = NULL;
6881 struct tog_parent_tree *pt;
6882 size_t len = 2; /* for leading slash and NUL */
6884 TAILQ_FOREACH(pt, parents, entry)
6885 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6886 + 1 /* slash */;
6887 if (te)
6888 len += strlen(got_tree_entry_get_name(te));
6890 *path = calloc(1, len);
6891 if (path == NULL)
6892 return got_error_from_errno("calloc");
6894 (*path)[0] = '/';
6895 pt = TAILQ_LAST(parents, tog_parent_trees);
6896 while (pt) {
6897 const char *name = got_tree_entry_get_name(pt->selected_entry);
6898 if (strlcat(*path, name, len) >= len) {
6899 err = got_error(GOT_ERR_NO_SPACE);
6900 goto done;
6902 if (strlcat(*path, "/", len) >= len) {
6903 err = got_error(GOT_ERR_NO_SPACE);
6904 goto done;
6906 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6908 if (te) {
6909 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6910 err = got_error(GOT_ERR_NO_SPACE);
6911 goto done;
6914 done:
6915 if (err) {
6916 free(*path);
6917 *path = NULL;
6919 return err;
6922 static const struct got_error *
6923 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6924 struct got_tree_entry *te, struct tog_parent_trees *parents,
6925 struct got_object_id *commit_id, struct got_repository *repo)
6927 const struct got_error *err = NULL;
6928 char *path;
6929 struct tog_view *blame_view;
6931 *new_view = NULL;
6933 err = tree_entry_path(&path, parents, te);
6934 if (err)
6935 return err;
6937 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6938 if (blame_view == NULL) {
6939 err = got_error_from_errno("view_open");
6940 goto done;
6943 err = open_blame_view(blame_view, path, commit_id, repo);
6944 if (err) {
6945 if (err->code == GOT_ERR_CANCELLED)
6946 err = NULL;
6947 view_close(blame_view);
6948 } else
6949 *new_view = blame_view;
6950 done:
6951 free(path);
6952 return err;
6955 static const struct got_error *
6956 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6957 struct tog_tree_view_state *s)
6959 struct tog_view *log_view;
6960 const struct got_error *err = NULL;
6961 char *path;
6963 *new_view = NULL;
6965 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6966 if (log_view == NULL)
6967 return got_error_from_errno("view_open");
6969 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6970 if (err)
6971 return err;
6973 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6974 path, 0);
6975 if (err)
6976 view_close(log_view);
6977 else
6978 *new_view = log_view;
6979 free(path);
6980 return err;
6983 static const struct got_error *
6984 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6985 const char *head_ref_name, struct got_repository *repo)
6987 const struct got_error *err = NULL;
6988 char *commit_id_str = NULL;
6989 struct tog_tree_view_state *s = &view->state.tree;
6990 struct got_commit_object *commit = NULL;
6992 TAILQ_INIT(&s->parents);
6993 STAILQ_INIT(&s->colors);
6995 s->commit_id = got_object_id_dup(commit_id);
6996 if (s->commit_id == NULL)
6997 return got_error_from_errno("got_object_id_dup");
6999 err = got_object_open_as_commit(&commit, repo, commit_id);
7000 if (err)
7001 goto done;
7004 * The root is opened here and will be closed when the view is closed.
7005 * Any visited subtrees and their path-wise parents are opened and
7006 * closed on demand.
7008 err = got_object_open_as_tree(&s->root, repo,
7009 got_object_commit_get_tree_id(commit));
7010 if (err)
7011 goto done;
7012 s->tree = s->root;
7014 err = got_object_id_str(&commit_id_str, commit_id);
7015 if (err != NULL)
7016 goto done;
7018 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7019 err = got_error_from_errno("asprintf");
7020 goto done;
7023 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7024 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7025 if (head_ref_name) {
7026 s->head_ref_name = strdup(head_ref_name);
7027 if (s->head_ref_name == NULL) {
7028 err = got_error_from_errno("strdup");
7029 goto done;
7032 s->repo = repo;
7034 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7035 err = add_color(&s->colors, "\\$$",
7036 TOG_COLOR_TREE_SUBMODULE,
7037 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7038 if (err)
7039 goto done;
7040 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7041 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7042 if (err)
7043 goto done;
7044 err = add_color(&s->colors, "/$",
7045 TOG_COLOR_TREE_DIRECTORY,
7046 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7047 if (err)
7048 goto done;
7050 err = add_color(&s->colors, "\\*$",
7051 TOG_COLOR_TREE_EXECUTABLE,
7052 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7053 if (err)
7054 goto done;
7056 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7057 get_color_value("TOG_COLOR_COMMIT"));
7058 if (err)
7059 goto done;
7062 view->show = show_tree_view;
7063 view->input = input_tree_view;
7064 view->close = close_tree_view;
7065 view->search_start = search_start_tree_view;
7066 view->search_next = search_next_tree_view;
7067 done:
7068 free(commit_id_str);
7069 if (commit)
7070 got_object_commit_close(commit);
7071 if (err)
7072 close_tree_view(view);
7073 return err;
7076 static const struct got_error *
7077 close_tree_view(struct tog_view *view)
7079 struct tog_tree_view_state *s = &view->state.tree;
7081 free_colors(&s->colors);
7082 free(s->tree_label);
7083 s->tree_label = NULL;
7084 free(s->commit_id);
7085 s->commit_id = NULL;
7086 free(s->head_ref_name);
7087 s->head_ref_name = NULL;
7088 while (!TAILQ_EMPTY(&s->parents)) {
7089 struct tog_parent_tree *parent;
7090 parent = TAILQ_FIRST(&s->parents);
7091 TAILQ_REMOVE(&s->parents, parent, entry);
7092 if (parent->tree != s->root)
7093 got_object_tree_close(parent->tree);
7094 free(parent);
7097 if (s->tree != NULL && s->tree != s->root)
7098 got_object_tree_close(s->tree);
7099 if (s->root)
7100 got_object_tree_close(s->root);
7101 return NULL;
7104 static const struct got_error *
7105 search_start_tree_view(struct tog_view *view)
7107 struct tog_tree_view_state *s = &view->state.tree;
7109 s->matched_entry = NULL;
7110 return NULL;
7113 static int
7114 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7116 regmatch_t regmatch;
7118 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7119 0) == 0;
7122 static const struct got_error *
7123 search_next_tree_view(struct tog_view *view)
7125 struct tog_tree_view_state *s = &view->state.tree;
7126 struct got_tree_entry *te = NULL;
7128 if (!view->searching) {
7129 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7130 return NULL;
7133 if (s->matched_entry) {
7134 if (view->searching == TOG_SEARCH_FORWARD) {
7135 if (s->selected_entry)
7136 te = got_tree_entry_get_next(s->tree,
7137 s->selected_entry);
7138 else
7139 te = got_object_tree_get_first_entry(s->tree);
7140 } else {
7141 if (s->selected_entry == NULL)
7142 te = got_object_tree_get_last_entry(s->tree);
7143 else
7144 te = got_tree_entry_get_prev(s->tree,
7145 s->selected_entry);
7147 } else {
7148 if (s->selected_entry)
7149 te = s->selected_entry;
7150 else if (view->searching == TOG_SEARCH_FORWARD)
7151 te = got_object_tree_get_first_entry(s->tree);
7152 else
7153 te = got_object_tree_get_last_entry(s->tree);
7156 while (1) {
7157 if (te == NULL) {
7158 if (s->matched_entry == NULL) {
7159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7160 return NULL;
7162 if (view->searching == TOG_SEARCH_FORWARD)
7163 te = got_object_tree_get_first_entry(s->tree);
7164 else
7165 te = got_object_tree_get_last_entry(s->tree);
7168 if (match_tree_entry(te, &view->regex)) {
7169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7170 s->matched_entry = te;
7171 break;
7174 if (view->searching == TOG_SEARCH_FORWARD)
7175 te = got_tree_entry_get_next(s->tree, te);
7176 else
7177 te = got_tree_entry_get_prev(s->tree, te);
7180 if (s->matched_entry) {
7181 s->first_displayed_entry = s->matched_entry;
7182 s->selected = 0;
7185 return NULL;
7188 static const struct got_error *
7189 show_tree_view(struct tog_view *view)
7191 const struct got_error *err = NULL;
7192 struct tog_tree_view_state *s = &view->state.tree;
7193 char *parent_path;
7195 err = tree_entry_path(&parent_path, &s->parents, NULL);
7196 if (err)
7197 return err;
7199 err = draw_tree_entries(view, parent_path);
7200 free(parent_path);
7202 view_border(view);
7203 return err;
7206 static const struct got_error *
7207 tree_goto_line(struct tog_view *view, int nlines)
7209 const struct got_error *err = NULL;
7210 struct tog_tree_view_state *s = &view->state.tree;
7211 struct got_tree_entry **fte, **lte, **ste;
7212 int g, last, first = 1, i = 1;
7213 int root = s->tree == s->root;
7214 int off = root ? 1 : 2;
7216 g = view->gline;
7217 view->gline = 0;
7219 if (g == 0)
7220 g = 1;
7221 else if (g > got_object_tree_get_nentries(s->tree))
7222 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7224 fte = &s->first_displayed_entry;
7225 lte = &s->last_displayed_entry;
7226 ste = &s->selected_entry;
7228 if (*fte != NULL) {
7229 first = got_tree_entry_get_index(*fte);
7230 first += off; /* account for ".." */
7232 last = got_tree_entry_get_index(*lte);
7233 last += off;
7235 if (g >= first && g <= last && g - first < nlines) {
7236 s->selected = g - first;
7237 return NULL; /* gline is on the current page */
7240 if (*ste != NULL) {
7241 i = got_tree_entry_get_index(*ste);
7242 i += off;
7245 if (i < g) {
7246 err = tree_scroll_down(view, g - i);
7247 if (err)
7248 return err;
7249 if (got_tree_entry_get_index(*lte) >=
7250 got_object_tree_get_nentries(s->tree) - 1 &&
7251 first + s->selected < g &&
7252 s->selected < s->ndisplayed - 1) {
7253 first = got_tree_entry_get_index(*fte);
7254 first += off;
7255 s->selected = g - first;
7257 } else if (i > g)
7258 tree_scroll_up(s, i - g);
7260 if (g < nlines &&
7261 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7262 s->selected = g - 1;
7264 return NULL;
7267 static const struct got_error *
7268 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7270 const struct got_error *err = NULL;
7271 struct tog_tree_view_state *s = &view->state.tree;
7272 struct got_tree_entry *te;
7273 int n, nscroll = view->nlines - 3;
7275 if (view->gline)
7276 return tree_goto_line(view, nscroll);
7278 switch (ch) {
7279 case 'i':
7280 s->show_ids = !s->show_ids;
7281 view->count = 0;
7282 break;
7283 case 'L':
7284 view->count = 0;
7285 if (!s->selected_entry)
7286 break;
7287 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7288 break;
7289 case 'R':
7290 view->count = 0;
7291 err = view_request_new(new_view, view, TOG_VIEW_REF);
7292 break;
7293 case 'g':
7294 case KEY_HOME:
7295 s->selected = 0;
7296 view->count = 0;
7297 if (s->tree == s->root)
7298 s->first_displayed_entry =
7299 got_object_tree_get_first_entry(s->tree);
7300 else
7301 s->first_displayed_entry = NULL;
7302 break;
7303 case 'G':
7304 case KEY_END: {
7305 int eos = view->nlines - 3;
7307 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7308 --eos; /* border */
7309 s->selected = 0;
7310 view->count = 0;
7311 te = got_object_tree_get_last_entry(s->tree);
7312 for (n = 0; n < eos; n++) {
7313 if (te == NULL) {
7314 if (s->tree != s->root) {
7315 s->first_displayed_entry = NULL;
7316 n++;
7318 break;
7320 s->first_displayed_entry = te;
7321 te = got_tree_entry_get_prev(s->tree, te);
7323 if (n > 0)
7324 s->selected = n - 1;
7325 break;
7327 case 'k':
7328 case KEY_UP:
7329 case CTRL('p'):
7330 if (s->selected > 0) {
7331 s->selected--;
7332 break;
7334 tree_scroll_up(s, 1);
7335 if (s->selected_entry == NULL ||
7336 (s->tree == s->root && s->selected_entry ==
7337 got_object_tree_get_first_entry(s->tree)))
7338 view->count = 0;
7339 break;
7340 case CTRL('u'):
7341 case 'u':
7342 nscroll /= 2;
7343 /* FALL THROUGH */
7344 case KEY_PPAGE:
7345 case CTRL('b'):
7346 case 'b':
7347 if (s->tree == s->root) {
7348 if (got_object_tree_get_first_entry(s->tree) ==
7349 s->first_displayed_entry)
7350 s->selected -= MIN(s->selected, nscroll);
7351 } else {
7352 if (s->first_displayed_entry == NULL)
7353 s->selected -= MIN(s->selected, nscroll);
7355 tree_scroll_up(s, MAX(0, nscroll));
7356 if (s->selected_entry == NULL ||
7357 (s->tree == s->root && s->selected_entry ==
7358 got_object_tree_get_first_entry(s->tree)))
7359 view->count = 0;
7360 break;
7361 case 'j':
7362 case KEY_DOWN:
7363 case CTRL('n'):
7364 if (s->selected < s->ndisplayed - 1) {
7365 s->selected++;
7366 break;
7368 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7369 == NULL) {
7370 /* can't scroll any further */
7371 view->count = 0;
7372 break;
7374 tree_scroll_down(view, 1);
7375 break;
7376 case CTRL('d'):
7377 case 'd':
7378 nscroll /= 2;
7379 /* FALL THROUGH */
7380 case KEY_NPAGE:
7381 case CTRL('f'):
7382 case 'f':
7383 case ' ':
7384 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7385 == NULL) {
7386 /* can't scroll any further; move cursor down */
7387 if (s->selected < s->ndisplayed - 1)
7388 s->selected += MIN(nscroll,
7389 s->ndisplayed - s->selected - 1);
7390 else
7391 view->count = 0;
7392 break;
7394 tree_scroll_down(view, nscroll);
7395 break;
7396 case KEY_ENTER:
7397 case '\r':
7398 case KEY_BACKSPACE:
7399 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7400 struct tog_parent_tree *parent;
7401 /* user selected '..' */
7402 if (s->tree == s->root) {
7403 view->count = 0;
7404 break;
7406 parent = TAILQ_FIRST(&s->parents);
7407 TAILQ_REMOVE(&s->parents, parent,
7408 entry);
7409 got_object_tree_close(s->tree);
7410 s->tree = parent->tree;
7411 s->first_displayed_entry =
7412 parent->first_displayed_entry;
7413 s->selected_entry =
7414 parent->selected_entry;
7415 s->selected = parent->selected;
7416 if (s->selected > view->nlines - 3) {
7417 err = offset_selection_down(view);
7418 if (err)
7419 break;
7421 free(parent);
7422 } else if (S_ISDIR(got_tree_entry_get_mode(
7423 s->selected_entry))) {
7424 struct got_tree_object *subtree;
7425 view->count = 0;
7426 err = got_object_open_as_tree(&subtree, s->repo,
7427 got_tree_entry_get_id(s->selected_entry));
7428 if (err)
7429 break;
7430 err = tree_view_visit_subtree(s, subtree);
7431 if (err) {
7432 got_object_tree_close(subtree);
7433 break;
7435 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7436 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7437 break;
7438 case KEY_RESIZE:
7439 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7440 s->selected = view->nlines - 4;
7441 view->count = 0;
7442 break;
7443 default:
7444 view->count = 0;
7445 break;
7448 return err;
7451 __dead static void
7452 usage_tree(void)
7454 endwin();
7455 fprintf(stderr,
7456 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7457 getprogname());
7458 exit(1);
7461 static const struct got_error *
7462 cmd_tree(int argc, char *argv[])
7464 const struct got_error *error;
7465 struct got_repository *repo = NULL;
7466 struct got_worktree *worktree = NULL;
7467 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7468 struct got_object_id *commit_id = NULL;
7469 struct got_commit_object *commit = NULL;
7470 const char *commit_id_arg = NULL;
7471 char *label = NULL;
7472 struct got_reference *ref = NULL;
7473 const char *head_ref_name = NULL;
7474 int ch;
7475 struct tog_view *view;
7476 int *pack_fds = NULL;
7478 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7479 switch (ch) {
7480 case 'c':
7481 commit_id_arg = optarg;
7482 break;
7483 case 'r':
7484 repo_path = realpath(optarg, NULL);
7485 if (repo_path == NULL)
7486 return got_error_from_errno2("realpath",
7487 optarg);
7488 break;
7489 default:
7490 usage_tree();
7491 /* NOTREACHED */
7495 argc -= optind;
7496 argv += optind;
7498 if (argc > 1)
7499 usage_tree();
7501 error = got_repo_pack_fds_open(&pack_fds);
7502 if (error != NULL)
7503 goto done;
7505 if (repo_path == NULL) {
7506 cwd = getcwd(NULL, 0);
7507 if (cwd == NULL)
7508 return got_error_from_errno("getcwd");
7509 error = got_worktree_open(&worktree, cwd);
7510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7511 goto done;
7512 if (worktree)
7513 repo_path =
7514 strdup(got_worktree_get_repo_path(worktree));
7515 else
7516 repo_path = strdup(cwd);
7517 if (repo_path == NULL) {
7518 error = got_error_from_errno("strdup");
7519 goto done;
7523 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7524 if (error != NULL)
7525 goto done;
7527 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7528 repo, worktree);
7529 if (error)
7530 goto done;
7532 init_curses();
7534 error = apply_unveil(got_repo_get_path(repo), NULL);
7535 if (error)
7536 goto done;
7538 error = tog_load_refs(repo, 0);
7539 if (error)
7540 goto done;
7542 if (commit_id_arg == NULL) {
7543 error = got_repo_match_object_id(&commit_id, &label,
7544 worktree ? got_worktree_get_head_ref_name(worktree) :
7545 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7546 if (error)
7547 goto done;
7548 head_ref_name = label;
7549 } else {
7550 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7551 if (error == NULL)
7552 head_ref_name = got_ref_get_name(ref);
7553 else if (error->code != GOT_ERR_NOT_REF)
7554 goto done;
7555 error = got_repo_match_object_id(&commit_id, NULL,
7556 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7557 if (error)
7558 goto done;
7561 error = got_object_open_as_commit(&commit, repo, commit_id);
7562 if (error)
7563 goto done;
7565 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7566 if (view == NULL) {
7567 error = got_error_from_errno("view_open");
7568 goto done;
7570 error = open_tree_view(view, commit_id, head_ref_name, repo);
7571 if (error)
7572 goto done;
7573 if (!got_path_is_root_dir(in_repo_path)) {
7574 error = tree_view_walk_path(&view->state.tree, commit,
7575 in_repo_path);
7576 if (error)
7577 goto done;
7580 if (worktree) {
7581 /* Release work tree lock. */
7582 got_worktree_close(worktree);
7583 worktree = NULL;
7585 error = view_loop(view);
7586 done:
7587 free(repo_path);
7588 free(cwd);
7589 free(commit_id);
7590 free(label);
7591 if (ref)
7592 got_ref_close(ref);
7593 if (repo) {
7594 const struct got_error *close_err = got_repo_close(repo);
7595 if (error == NULL)
7596 error = close_err;
7598 if (pack_fds) {
7599 const struct got_error *pack_err =
7600 got_repo_pack_fds_close(pack_fds);
7601 if (error == NULL)
7602 error = pack_err;
7604 tog_free_refs();
7605 return error;
7608 static const struct got_error *
7609 ref_view_load_refs(struct tog_ref_view_state *s)
7611 struct got_reflist_entry *sre;
7612 struct tog_reflist_entry *re;
7614 s->nrefs = 0;
7615 TAILQ_FOREACH(sre, &tog_refs, entry) {
7616 if (strncmp(got_ref_get_name(sre->ref),
7617 "refs/got/", 9) == 0 &&
7618 strncmp(got_ref_get_name(sre->ref),
7619 "refs/got/backup/", 16) != 0)
7620 continue;
7622 re = malloc(sizeof(*re));
7623 if (re == NULL)
7624 return got_error_from_errno("malloc");
7626 re->ref = got_ref_dup(sre->ref);
7627 if (re->ref == NULL)
7628 return got_error_from_errno("got_ref_dup");
7629 re->idx = s->nrefs++;
7630 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7633 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7634 return NULL;
7637 static void
7638 ref_view_free_refs(struct tog_ref_view_state *s)
7640 struct tog_reflist_entry *re;
7642 while (!TAILQ_EMPTY(&s->refs)) {
7643 re = TAILQ_FIRST(&s->refs);
7644 TAILQ_REMOVE(&s->refs, re, entry);
7645 got_ref_close(re->ref);
7646 free(re);
7650 static const struct got_error *
7651 open_ref_view(struct tog_view *view, struct got_repository *repo)
7653 const struct got_error *err = NULL;
7654 struct tog_ref_view_state *s = &view->state.ref;
7656 s->selected_entry = 0;
7657 s->repo = repo;
7659 TAILQ_INIT(&s->refs);
7660 STAILQ_INIT(&s->colors);
7662 err = ref_view_load_refs(s);
7663 if (err)
7664 return err;
7666 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7667 err = add_color(&s->colors, "^refs/heads/",
7668 TOG_COLOR_REFS_HEADS,
7669 get_color_value("TOG_COLOR_REFS_HEADS"));
7670 if (err)
7671 goto done;
7673 err = add_color(&s->colors, "^refs/tags/",
7674 TOG_COLOR_REFS_TAGS,
7675 get_color_value("TOG_COLOR_REFS_TAGS"));
7676 if (err)
7677 goto done;
7679 err = add_color(&s->colors, "^refs/remotes/",
7680 TOG_COLOR_REFS_REMOTES,
7681 get_color_value("TOG_COLOR_REFS_REMOTES"));
7682 if (err)
7683 goto done;
7685 err = add_color(&s->colors, "^refs/got/backup/",
7686 TOG_COLOR_REFS_BACKUP,
7687 get_color_value("TOG_COLOR_REFS_BACKUP"));
7688 if (err)
7689 goto done;
7692 view->show = show_ref_view;
7693 view->input = input_ref_view;
7694 view->close = close_ref_view;
7695 view->search_start = search_start_ref_view;
7696 view->search_next = search_next_ref_view;
7697 done:
7698 if (err)
7699 free_colors(&s->colors);
7700 return err;
7703 static const struct got_error *
7704 close_ref_view(struct tog_view *view)
7706 struct tog_ref_view_state *s = &view->state.ref;
7708 ref_view_free_refs(s);
7709 free_colors(&s->colors);
7711 return NULL;
7714 static const struct got_error *
7715 resolve_reflist_entry(struct got_object_id **commit_id,
7716 struct tog_reflist_entry *re, struct got_repository *repo)
7718 const struct got_error *err = NULL;
7719 struct got_object_id *obj_id;
7720 struct got_tag_object *tag = NULL;
7721 int obj_type;
7723 *commit_id = NULL;
7725 err = got_ref_resolve(&obj_id, repo, re->ref);
7726 if (err)
7727 return err;
7729 err = got_object_get_type(&obj_type, repo, obj_id);
7730 if (err)
7731 goto done;
7733 switch (obj_type) {
7734 case GOT_OBJ_TYPE_COMMIT:
7735 *commit_id = obj_id;
7736 break;
7737 case GOT_OBJ_TYPE_TAG:
7738 err = got_object_open_as_tag(&tag, repo, obj_id);
7739 if (err)
7740 goto done;
7741 free(obj_id);
7742 err = got_object_get_type(&obj_type, repo,
7743 got_object_tag_get_object_id(tag));
7744 if (err)
7745 goto done;
7746 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7747 err = got_error(GOT_ERR_OBJ_TYPE);
7748 goto done;
7750 *commit_id = got_object_id_dup(
7751 got_object_tag_get_object_id(tag));
7752 if (*commit_id == NULL) {
7753 err = got_error_from_errno("got_object_id_dup");
7754 goto done;
7756 break;
7757 default:
7758 err = got_error(GOT_ERR_OBJ_TYPE);
7759 break;
7762 done:
7763 if (tag)
7764 got_object_tag_close(tag);
7765 if (err) {
7766 free(*commit_id);
7767 *commit_id = NULL;
7769 return err;
7772 static const struct got_error *
7773 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7774 struct tog_reflist_entry *re, struct got_repository *repo)
7776 struct tog_view *log_view;
7777 const struct got_error *err = NULL;
7778 struct got_object_id *commit_id = NULL;
7780 *new_view = NULL;
7782 err = resolve_reflist_entry(&commit_id, re, repo);
7783 if (err) {
7784 if (err->code != GOT_ERR_OBJ_TYPE)
7785 return err;
7786 else
7787 return NULL;
7790 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7791 if (log_view == NULL) {
7792 err = got_error_from_errno("view_open");
7793 goto done;
7796 err = open_log_view(log_view, commit_id, repo,
7797 got_ref_get_name(re->ref), "", 0);
7798 done:
7799 if (err)
7800 view_close(log_view);
7801 else
7802 *new_view = log_view;
7803 free(commit_id);
7804 return err;
7807 static void
7808 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7810 struct tog_reflist_entry *re;
7811 int i = 0;
7813 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7814 return;
7816 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7817 while (i++ < maxscroll) {
7818 if (re == NULL)
7819 break;
7820 s->first_displayed_entry = re;
7821 re = TAILQ_PREV(re, tog_reflist_head, entry);
7825 static const struct got_error *
7826 ref_scroll_down(struct tog_view *view, int maxscroll)
7828 struct tog_ref_view_state *s = &view->state.ref;
7829 struct tog_reflist_entry *next, *last;
7830 int n = 0;
7832 if (s->first_displayed_entry)
7833 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7834 else
7835 next = TAILQ_FIRST(&s->refs);
7837 last = s->last_displayed_entry;
7838 while (next && n++ < maxscroll) {
7839 if (last) {
7840 s->last_displayed_entry = last;
7841 last = TAILQ_NEXT(last, entry);
7843 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7844 s->first_displayed_entry = next;
7845 next = TAILQ_NEXT(next, entry);
7849 return NULL;
7852 static const struct got_error *
7853 search_start_ref_view(struct tog_view *view)
7855 struct tog_ref_view_state *s = &view->state.ref;
7857 s->matched_entry = NULL;
7858 return NULL;
7861 static int
7862 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7864 regmatch_t regmatch;
7866 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7867 0) == 0;
7870 static const struct got_error *
7871 search_next_ref_view(struct tog_view *view)
7873 struct tog_ref_view_state *s = &view->state.ref;
7874 struct tog_reflist_entry *re = NULL;
7876 if (!view->searching) {
7877 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7878 return NULL;
7881 if (s->matched_entry) {
7882 if (view->searching == TOG_SEARCH_FORWARD) {
7883 if (s->selected_entry)
7884 re = TAILQ_NEXT(s->selected_entry, entry);
7885 else
7886 re = TAILQ_PREV(s->selected_entry,
7887 tog_reflist_head, entry);
7888 } else {
7889 if (s->selected_entry == NULL)
7890 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7891 else
7892 re = TAILQ_PREV(s->selected_entry,
7893 tog_reflist_head, entry);
7895 } else {
7896 if (s->selected_entry)
7897 re = s->selected_entry;
7898 else if (view->searching == TOG_SEARCH_FORWARD)
7899 re = TAILQ_FIRST(&s->refs);
7900 else
7901 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7904 while (1) {
7905 if (re == NULL) {
7906 if (s->matched_entry == NULL) {
7907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7908 return NULL;
7910 if (view->searching == TOG_SEARCH_FORWARD)
7911 re = TAILQ_FIRST(&s->refs);
7912 else
7913 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7916 if (match_reflist_entry(re, &view->regex)) {
7917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7918 s->matched_entry = re;
7919 break;
7922 if (view->searching == TOG_SEARCH_FORWARD)
7923 re = TAILQ_NEXT(re, entry);
7924 else
7925 re = TAILQ_PREV(re, tog_reflist_head, entry);
7928 if (s->matched_entry) {
7929 s->first_displayed_entry = s->matched_entry;
7930 s->selected = 0;
7933 return NULL;
7936 static const struct got_error *
7937 show_ref_view(struct tog_view *view)
7939 const struct got_error *err = NULL;
7940 struct tog_ref_view_state *s = &view->state.ref;
7941 struct tog_reflist_entry *re;
7942 char *line = NULL;
7943 wchar_t *wline;
7944 struct tog_color *tc;
7945 int width, n;
7946 int limit = view->nlines;
7948 werase(view->window);
7950 s->ndisplayed = 0;
7951 if (view_is_hsplit_top(view))
7952 --limit; /* border */
7954 if (limit == 0)
7955 return NULL;
7957 re = s->first_displayed_entry;
7959 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7960 s->nrefs) == -1)
7961 return got_error_from_errno("asprintf");
7963 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7964 if (err) {
7965 free(line);
7966 return err;
7968 if (view_needs_focus_indication(view))
7969 wstandout(view->window);
7970 waddwstr(view->window, wline);
7971 while (width++ < view->ncols)
7972 waddch(view->window, ' ');
7973 if (view_needs_focus_indication(view))
7974 wstandend(view->window);
7975 free(wline);
7976 wline = NULL;
7977 free(line);
7978 line = NULL;
7979 if (--limit <= 0)
7980 return NULL;
7982 n = 0;
7983 while (re && limit > 0) {
7984 char *line = NULL;
7985 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7987 if (s->show_date) {
7988 struct got_commit_object *ci;
7989 struct got_tag_object *tag;
7990 struct got_object_id *id;
7991 struct tm tm;
7992 time_t t;
7994 err = got_ref_resolve(&id, s->repo, re->ref);
7995 if (err)
7996 return err;
7997 err = got_object_open_as_tag(&tag, s->repo, id);
7998 if (err) {
7999 if (err->code != GOT_ERR_OBJ_TYPE) {
8000 free(id);
8001 return err;
8003 err = got_object_open_as_commit(&ci, s->repo,
8004 id);
8005 if (err) {
8006 free(id);
8007 return err;
8009 t = got_object_commit_get_committer_time(ci);
8010 got_object_commit_close(ci);
8011 } else {
8012 t = got_object_tag_get_tagger_time(tag);
8013 got_object_tag_close(tag);
8015 free(id);
8016 if (gmtime_r(&t, &tm) == NULL)
8017 return got_error_from_errno("gmtime_r");
8018 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8019 return got_error(GOT_ERR_NO_SPACE);
8021 if (got_ref_is_symbolic(re->ref)) {
8022 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8023 ymd : "", got_ref_get_name(re->ref),
8024 got_ref_get_symref_target(re->ref)) == -1)
8025 return got_error_from_errno("asprintf");
8026 } else if (s->show_ids) {
8027 struct got_object_id *id;
8028 char *id_str;
8029 err = got_ref_resolve(&id, s->repo, re->ref);
8030 if (err)
8031 return err;
8032 err = got_object_id_str(&id_str, id);
8033 if (err) {
8034 free(id);
8035 return err;
8037 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8038 got_ref_get_name(re->ref), id_str) == -1) {
8039 err = got_error_from_errno("asprintf");
8040 free(id);
8041 free(id_str);
8042 return err;
8044 free(id);
8045 free(id_str);
8046 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8047 got_ref_get_name(re->ref)) == -1)
8048 return got_error_from_errno("asprintf");
8050 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8051 0, 0);
8052 if (err) {
8053 free(line);
8054 return err;
8056 if (n == s->selected) {
8057 if (view->focussed)
8058 wstandout(view->window);
8059 s->selected_entry = re;
8061 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8062 if (tc)
8063 wattr_on(view->window,
8064 COLOR_PAIR(tc->colorpair), NULL);
8065 waddwstr(view->window, wline);
8066 if (tc)
8067 wattr_off(view->window,
8068 COLOR_PAIR(tc->colorpair), NULL);
8069 if (width < view->ncols - 1)
8070 waddch(view->window, '\n');
8071 if (n == s->selected && view->focussed)
8072 wstandend(view->window);
8073 free(line);
8074 free(wline);
8075 wline = NULL;
8076 n++;
8077 s->ndisplayed++;
8078 s->last_displayed_entry = re;
8080 limit--;
8081 re = TAILQ_NEXT(re, entry);
8084 view_border(view);
8085 return err;
8088 static const struct got_error *
8089 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8090 struct tog_reflist_entry *re, struct got_repository *repo)
8092 const struct got_error *err = NULL;
8093 struct got_object_id *commit_id = NULL;
8094 struct tog_view *tree_view;
8096 *new_view = NULL;
8098 err = resolve_reflist_entry(&commit_id, re, repo);
8099 if (err) {
8100 if (err->code != GOT_ERR_OBJ_TYPE)
8101 return err;
8102 else
8103 return NULL;
8107 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8108 if (tree_view == NULL) {
8109 err = got_error_from_errno("view_open");
8110 goto done;
8113 err = open_tree_view(tree_view, commit_id,
8114 got_ref_get_name(re->ref), repo);
8115 if (err)
8116 goto done;
8118 *new_view = tree_view;
8119 done:
8120 free(commit_id);
8121 return err;
8124 static const struct got_error *
8125 ref_goto_line(struct tog_view *view, int nlines)
8127 const struct got_error *err = NULL;
8128 struct tog_ref_view_state *s = &view->state.ref;
8129 int g, idx = s->selected_entry->idx;
8131 g = view->gline;
8132 view->gline = 0;
8134 if (g == 0)
8135 g = 1;
8136 else if (g > s->nrefs)
8137 g = s->nrefs;
8139 if (g >= s->first_displayed_entry->idx + 1 &&
8140 g <= s->last_displayed_entry->idx + 1 &&
8141 g - s->first_displayed_entry->idx - 1 < nlines) {
8142 s->selected = g - s->first_displayed_entry->idx - 1;
8143 return NULL;
8146 if (idx + 1 < g) {
8147 err = ref_scroll_down(view, g - idx - 1);
8148 if (err)
8149 return err;
8150 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8151 s->first_displayed_entry->idx + s->selected < g &&
8152 s->selected < s->ndisplayed - 1)
8153 s->selected = g - s->first_displayed_entry->idx - 1;
8154 } else if (idx + 1 > g)
8155 ref_scroll_up(s, idx - g + 1);
8157 if (g < nlines && s->first_displayed_entry->idx == 0)
8158 s->selected = g - 1;
8160 return NULL;
8164 static const struct got_error *
8165 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8167 const struct got_error *err = NULL;
8168 struct tog_ref_view_state *s = &view->state.ref;
8169 struct tog_reflist_entry *re;
8170 int n, nscroll = view->nlines - 1;
8172 if (view->gline)
8173 return ref_goto_line(view, nscroll);
8175 switch (ch) {
8176 case 'i':
8177 s->show_ids = !s->show_ids;
8178 view->count = 0;
8179 break;
8180 case 'm':
8181 s->show_date = !s->show_date;
8182 view->count = 0;
8183 break;
8184 case 'o':
8185 s->sort_by_date = !s->sort_by_date;
8186 view->count = 0;
8187 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8188 got_ref_cmp_by_commit_timestamp_descending :
8189 tog_ref_cmp_by_name, s->repo);
8190 if (err)
8191 break;
8192 got_reflist_object_id_map_free(tog_refs_idmap);
8193 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8194 &tog_refs, s->repo);
8195 if (err)
8196 break;
8197 ref_view_free_refs(s);
8198 err = ref_view_load_refs(s);
8199 break;
8200 case KEY_ENTER:
8201 case '\r':
8202 view->count = 0;
8203 if (!s->selected_entry)
8204 break;
8205 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8206 break;
8207 case 'T':
8208 view->count = 0;
8209 if (!s->selected_entry)
8210 break;
8211 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8212 break;
8213 case 'g':
8214 case KEY_HOME:
8215 s->selected = 0;
8216 view->count = 0;
8217 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8218 break;
8219 case 'G':
8220 case KEY_END: {
8221 int eos = view->nlines - 1;
8223 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8224 --eos; /* border */
8225 s->selected = 0;
8226 view->count = 0;
8227 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8228 for (n = 0; n < eos; n++) {
8229 if (re == NULL)
8230 break;
8231 s->first_displayed_entry = re;
8232 re = TAILQ_PREV(re, tog_reflist_head, entry);
8234 if (n > 0)
8235 s->selected = n - 1;
8236 break;
8238 case 'k':
8239 case KEY_UP:
8240 case CTRL('p'):
8241 if (s->selected > 0) {
8242 s->selected--;
8243 break;
8245 ref_scroll_up(s, 1);
8246 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8247 view->count = 0;
8248 break;
8249 case CTRL('u'):
8250 case 'u':
8251 nscroll /= 2;
8252 /* FALL THROUGH */
8253 case KEY_PPAGE:
8254 case CTRL('b'):
8255 case 'b':
8256 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8257 s->selected -= MIN(nscroll, s->selected);
8258 ref_scroll_up(s, MAX(0, nscroll));
8259 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8260 view->count = 0;
8261 break;
8262 case 'j':
8263 case KEY_DOWN:
8264 case CTRL('n'):
8265 if (s->selected < s->ndisplayed - 1) {
8266 s->selected++;
8267 break;
8269 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8270 /* can't scroll any further */
8271 view->count = 0;
8272 break;
8274 ref_scroll_down(view, 1);
8275 break;
8276 case CTRL('d'):
8277 case 'd':
8278 nscroll /= 2;
8279 /* FALL THROUGH */
8280 case KEY_NPAGE:
8281 case CTRL('f'):
8282 case 'f':
8283 case ' ':
8284 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8285 /* can't scroll any further; move cursor down */
8286 if (s->selected < s->ndisplayed - 1)
8287 s->selected += MIN(nscroll,
8288 s->ndisplayed - s->selected - 1);
8289 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8290 s->selected += s->ndisplayed - s->selected - 1;
8291 view->count = 0;
8292 break;
8294 ref_scroll_down(view, nscroll);
8295 break;
8296 case CTRL('l'):
8297 view->count = 0;
8298 tog_free_refs();
8299 err = tog_load_refs(s->repo, s->sort_by_date);
8300 if (err)
8301 break;
8302 ref_view_free_refs(s);
8303 err = ref_view_load_refs(s);
8304 break;
8305 case KEY_RESIZE:
8306 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8307 s->selected = view->nlines - 2;
8308 break;
8309 default:
8310 view->count = 0;
8311 break;
8314 return err;
8317 __dead static void
8318 usage_ref(void)
8320 endwin();
8321 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8322 getprogname());
8323 exit(1);
8326 static const struct got_error *
8327 cmd_ref(int argc, char *argv[])
8329 const struct got_error *error;
8330 struct got_repository *repo = NULL;
8331 struct got_worktree *worktree = NULL;
8332 char *cwd = NULL, *repo_path = NULL;
8333 int ch;
8334 struct tog_view *view;
8335 int *pack_fds = NULL;
8337 while ((ch = getopt(argc, argv, "r:")) != -1) {
8338 switch (ch) {
8339 case 'r':
8340 repo_path = realpath(optarg, NULL);
8341 if (repo_path == NULL)
8342 return got_error_from_errno2("realpath",
8343 optarg);
8344 break;
8345 default:
8346 usage_ref();
8347 /* NOTREACHED */
8351 argc -= optind;
8352 argv += optind;
8354 if (argc > 1)
8355 usage_ref();
8357 error = got_repo_pack_fds_open(&pack_fds);
8358 if (error != NULL)
8359 goto done;
8361 if (repo_path == NULL) {
8362 cwd = getcwd(NULL, 0);
8363 if (cwd == NULL)
8364 return got_error_from_errno("getcwd");
8365 error = got_worktree_open(&worktree, cwd);
8366 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8367 goto done;
8368 if (worktree)
8369 repo_path =
8370 strdup(got_worktree_get_repo_path(worktree));
8371 else
8372 repo_path = strdup(cwd);
8373 if (repo_path == NULL) {
8374 error = got_error_from_errno("strdup");
8375 goto done;
8379 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8380 if (error != NULL)
8381 goto done;
8383 init_curses();
8385 error = apply_unveil(got_repo_get_path(repo), NULL);
8386 if (error)
8387 goto done;
8389 error = tog_load_refs(repo, 0);
8390 if (error)
8391 goto done;
8393 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8394 if (view == NULL) {
8395 error = got_error_from_errno("view_open");
8396 goto done;
8399 error = open_ref_view(view, repo);
8400 if (error)
8401 goto done;
8403 if (worktree) {
8404 /* Release work tree lock. */
8405 got_worktree_close(worktree);
8406 worktree = NULL;
8408 error = view_loop(view);
8409 done:
8410 free(repo_path);
8411 free(cwd);
8412 if (repo) {
8413 const struct got_error *close_err = got_repo_close(repo);
8414 if (close_err)
8415 error = close_err;
8417 if (pack_fds) {
8418 const struct got_error *pack_err =
8419 got_repo_pack_fds_close(pack_fds);
8420 if (error == NULL)
8421 error = pack_err;
8423 tog_free_refs();
8424 return error;
8427 static const struct got_error*
8428 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8429 const char *str)
8431 size_t len;
8433 if (win == NULL)
8434 win = stdscr;
8436 len = strlen(str);
8437 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8439 if (focus)
8440 wstandout(win);
8441 if (mvwprintw(win, y, x, "%s", str) == ERR)
8442 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8443 if (focus)
8444 wstandend(win);
8446 return NULL;
8449 static const struct got_error *
8450 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8452 off_t *p;
8454 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8455 if (p == NULL) {
8456 free(*line_offsets);
8457 *line_offsets = NULL;
8458 return got_error_from_errno("reallocarray");
8461 *line_offsets = p;
8462 (*line_offsets)[*nlines] = off;
8463 ++(*nlines);
8464 return NULL;
8467 static const struct got_error *
8468 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8470 *ret = 0;
8472 for (;n > 0; --n, ++km) {
8473 char *t0, *t, *k;
8474 size_t len = 1;
8476 if (km->keys == NULL)
8477 continue;
8479 t = t0 = strdup(km->keys);
8480 if (t0 == NULL)
8481 return got_error_from_errno("strdup");
8483 len += strlen(t);
8484 while ((k = strsep(&t, " ")) != NULL)
8485 len += strlen(k) > 1 ? 2 : 0;
8486 free(t0);
8487 *ret = MAX(*ret, len);
8490 return NULL;
8494 * Write keymap section headers, keys, and key info in km to f.
8495 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8496 * wrap control and symbolic keys in guillemets, else use <>.
8498 static const struct got_error *
8499 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8501 int n, len = width;
8503 if (km->keys) {
8504 static const char *u8_glyph[] = {
8505 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8506 "\xe2\x80\xba" /* U+203A (utf8 >) */
8508 char *t0, *t, *k;
8509 int cs, s, first = 1;
8511 cs = got_locale_is_utf8();
8513 t = t0 = strdup(km->keys);
8514 if (t0 == NULL)
8515 return got_error_from_errno("strdup");
8517 len = strlen(km->keys);
8518 while ((k = strsep(&t, " ")) != NULL) {
8519 s = strlen(k) > 1; /* control or symbolic key */
8520 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8521 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8522 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8523 if (n < 0) {
8524 free(t0);
8525 return got_error_from_errno("fprintf");
8527 first = 0;
8528 len += s ? 2 : 0;
8529 *off += n;
8531 free(t0);
8533 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8534 if (n < 0)
8535 return got_error_from_errno("fprintf");
8536 *off += n;
8538 return NULL;
8541 static const struct got_error *
8542 format_help(struct tog_help_view_state *s)
8544 const struct got_error *err = NULL;
8545 off_t off = 0;
8546 int i, max, n, show = s->all;
8547 static const struct tog_key_map km[] = {
8548 #define KEYMAP_(info, type) { NULL, (info), type }
8549 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8550 GENERATE_HELP
8551 #undef KEYMAP_
8552 #undef KEY_
8555 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8556 if (err)
8557 return err;
8559 n = nitems(km);
8560 err = max_key_str(&max, km, n);
8561 if (err)
8562 return err;
8564 for (i = 0; i < n; ++i) {
8565 if (km[i].keys == NULL) {
8566 show = s->all;
8567 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8568 km[i].type == s->type || s->all)
8569 show = 1;
8571 if (show) {
8572 err = format_help_line(&off, s->f, &km[i], max);
8573 if (err)
8574 return err;
8575 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8576 if (err)
8577 return err;
8580 fputc('\n', s->f);
8581 ++off;
8582 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8583 return err;
8586 static const struct got_error *
8587 create_help(struct tog_help_view_state *s)
8589 FILE *f;
8590 const struct got_error *err;
8592 free(s->line_offsets);
8593 s->line_offsets = NULL;
8594 s->nlines = 0;
8596 f = got_opentemp();
8597 if (f == NULL)
8598 return got_error_from_errno("got_opentemp");
8599 s->f = f;
8601 err = format_help(s);
8602 if (err)
8603 return err;
8605 if (s->f && fflush(s->f) != 0)
8606 return got_error_from_errno("fflush");
8608 return NULL;
8611 static const struct got_error *
8612 search_start_help_view(struct tog_view *view)
8614 view->state.help.matched_line = 0;
8615 return NULL;
8618 static void
8619 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8620 size_t *nlines, int **first, int **last, int **match, int **selected)
8622 struct tog_help_view_state *s = &view->state.help;
8624 *f = s->f;
8625 *nlines = s->nlines;
8626 *line_offsets = s->line_offsets;
8627 *match = &s->matched_line;
8628 *first = &s->first_displayed_line;
8629 *last = &s->last_displayed_line;
8630 *selected = &s->selected_line;
8633 static const struct got_error *
8634 show_help_view(struct tog_view *view)
8636 struct tog_help_view_state *s = &view->state.help;
8637 const struct got_error *err;
8638 regmatch_t *regmatch = &view->regmatch;
8639 wchar_t *wline;
8640 char *line;
8641 ssize_t linelen;
8642 size_t linesz = 0;
8643 int width, nprinted = 0, rc = 0;
8644 int eos = view->nlines;
8646 if (view_is_hsplit_top(view))
8647 --eos; /* account for border */
8649 s->lineno = 0;
8650 rewind(s->f);
8651 werase(view->window);
8653 if (view->gline > s->nlines - 1)
8654 view->gline = s->nlines - 1;
8656 err = win_draw_center(view->window, 0, 0, view->ncols,
8657 view_needs_focus_indication(view),
8658 "tog help (press q to return to tog)");
8659 if (err)
8660 return err;
8661 if (eos <= 1)
8662 return NULL;
8663 waddstr(view->window, "\n\n");
8664 eos -= 2;
8666 s->eof = 0;
8667 view->maxx = 0;
8668 line = NULL;
8669 while (eos > 0 && nprinted < eos) {
8670 attr_t attr = 0;
8672 linelen = getline(&line, &linesz, s->f);
8673 if (linelen == -1) {
8674 if (!feof(s->f)) {
8675 free(line);
8676 return got_ferror(s->f, GOT_ERR_IO);
8678 s->eof = 1;
8679 break;
8681 if (++s->lineno < s->first_displayed_line)
8682 continue;
8683 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8684 continue;
8685 if (s->lineno == view->hiline)
8686 attr = A_STANDOUT;
8688 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8689 view->x ? 1 : 0);
8690 if (err) {
8691 free(line);
8692 return err;
8694 view->maxx = MAX(view->maxx, width);
8695 free(wline);
8696 wline = NULL;
8698 if (attr)
8699 wattron(view->window, attr);
8700 if (s->first_displayed_line + nprinted == s->matched_line &&
8701 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8702 err = add_matched_line(&width, line, view->ncols - 1, 0,
8703 view->window, view->x, regmatch);
8704 if (err) {
8705 free(line);
8706 return err;
8708 } else {
8709 int skip;
8711 err = format_line(&wline, &width, &skip, line,
8712 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8713 if (err) {
8714 free(line);
8715 return err;
8717 rc = waddwstr(view->window, &wline[skip]);
8718 free(wline);
8719 wline = NULL;
8720 if (rc == ERR)
8721 return got_error_msg(GOT_ERR_IO, "waddwstr");
8723 if (s->lineno == view->hiline) {
8724 while (width++ < view->ncols)
8725 waddch(view->window, ' ');
8726 } else {
8727 if (width <= view->ncols)
8728 waddch(view->window, '\n');
8730 if (attr)
8731 wattroff(view->window, attr);
8732 if (++nprinted == 1)
8733 s->first_displayed_line = s->lineno;
8735 free(line);
8736 if (nprinted > 0)
8737 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8738 else
8739 s->last_displayed_line = s->first_displayed_line;
8741 view_border(view);
8743 if (s->eof) {
8744 rc = waddnstr(view->window,
8745 "See the tog(1) manual page for full documentation",
8746 view->ncols - 1);
8747 if (rc == ERR)
8748 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8749 } else {
8750 wmove(view->window, view->nlines - 1, 0);
8751 wclrtoeol(view->window);
8752 wstandout(view->window);
8753 rc = waddnstr(view->window, "scroll down for more...",
8754 view->ncols - 1);
8755 if (rc == ERR)
8756 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8757 if (getcurx(view->window) < view->ncols - 6) {
8758 rc = wprintw(view->window, "[%.0f%%]",
8759 100.00 * s->last_displayed_line / s->nlines);
8760 if (rc == ERR)
8761 return got_error_msg(GOT_ERR_IO, "wprintw");
8763 wstandend(view->window);
8766 return NULL;
8769 static const struct got_error *
8770 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8772 struct tog_help_view_state *s = &view->state.help;
8773 const struct got_error *err = NULL;
8774 char *line = NULL;
8775 ssize_t linelen;
8776 size_t linesz = 0;
8777 int eos, nscroll;
8779 eos = nscroll = view->nlines;
8780 if (view_is_hsplit_top(view))
8781 --eos; /* border */
8783 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8785 switch (ch) {
8786 case '0':
8787 view->x = 0;
8788 break;
8789 case '$':
8790 view->x = MAX(view->maxx - view->ncols / 3, 0);
8791 view->count = 0;
8792 break;
8793 case KEY_RIGHT:
8794 case 'l':
8795 if (view->x + view->ncols / 3 < view->maxx)
8796 view->x += 2;
8797 else
8798 view->count = 0;
8799 break;
8800 case KEY_LEFT:
8801 case 'h':
8802 view->x -= MIN(view->x, 2);
8803 if (view->x <= 0)
8804 view->count = 0;
8805 break;
8806 case 'g':
8807 case KEY_HOME:
8808 s->first_displayed_line = 1;
8809 view->count = 0;
8810 break;
8811 case 'G':
8812 case KEY_END:
8813 view->count = 0;
8814 if (s->eof)
8815 break;
8816 s->first_displayed_line = (s->nlines - eos) + 3;
8817 s->eof = 1;
8818 break;
8819 case 'k':
8820 case KEY_UP:
8821 if (s->first_displayed_line > 1)
8822 --s->first_displayed_line;
8823 else
8824 view->count = 0;
8825 break;
8826 case CTRL('u'):
8827 case 'u':
8828 nscroll /= 2;
8829 /* FALL THROUGH */
8830 case KEY_PPAGE:
8831 case CTRL('b'):
8832 case 'b':
8833 if (s->first_displayed_line == 1) {
8834 view->count = 0;
8835 break;
8837 while (--nscroll > 0 && s->first_displayed_line > 1)
8838 s->first_displayed_line--;
8839 break;
8840 case 'j':
8841 case KEY_DOWN:
8842 case CTRL('n'):
8843 if (!s->eof)
8844 ++s->first_displayed_line;
8845 else
8846 view->count = 0;
8847 break;
8848 case CTRL('d'):
8849 case 'd':
8850 nscroll /= 2;
8851 /* FALL THROUGH */
8852 case KEY_NPAGE:
8853 case CTRL('f'):
8854 case 'f':
8855 case ' ':
8856 if (s->eof) {
8857 view->count = 0;
8858 break;
8860 while (!s->eof && --nscroll > 0) {
8861 linelen = getline(&line, &linesz, s->f);
8862 s->first_displayed_line++;
8863 if (linelen == -1) {
8864 if (feof(s->f))
8865 s->eof = 1;
8866 else
8867 err = got_ferror(s->f, GOT_ERR_IO);
8868 break;
8871 free(line);
8872 break;
8873 default:
8874 view->count = 0;
8875 break;
8878 return err;
8881 static const struct got_error *
8882 close_help_view(struct tog_view *view)
8884 struct tog_help_view_state *s = &view->state.help;
8886 free(s->line_offsets);
8887 s->line_offsets = NULL;
8888 if (fclose(s->f) == EOF)
8889 return got_error_from_errno("fclose");
8891 return NULL;
8894 static const struct got_error *
8895 reset_help_view(struct tog_view *view)
8897 struct tog_help_view_state *s = &view->state.help;
8900 if (s->f && fclose(s->f) == EOF)
8901 return got_error_from_errno("fclose");
8903 wclear(view->window);
8904 view->count = 0;
8905 view->x = 0;
8906 s->all = !s->all;
8907 s->first_displayed_line = 1;
8908 s->last_displayed_line = view->nlines;
8909 s->matched_line = 0;
8911 return create_help(s);
8914 static const struct got_error *
8915 open_help_view(struct tog_view *view, struct tog_view *parent)
8917 const struct got_error *err = NULL;
8918 struct tog_help_view_state *s = &view->state.help;
8920 s->type = (enum tog_keymap_type)parent->type;
8921 s->first_displayed_line = 1;
8922 s->last_displayed_line = view->nlines;
8923 s->selected_line = 1;
8925 view->show = show_help_view;
8926 view->input = input_help_view;
8927 view->reset = reset_help_view;
8928 view->close = close_help_view;
8929 view->search_start = search_start_help_view;
8930 view->search_setup = search_setup_help_view;
8931 view->search_next = search_next_view_match;
8933 err = create_help(s);
8934 return err;
8937 static const struct got_error *
8938 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8939 enum tog_view_type request, int y, int x)
8941 const struct got_error *err = NULL;
8943 *new_view = NULL;
8945 switch (request) {
8946 case TOG_VIEW_DIFF:
8947 if (view->type == TOG_VIEW_LOG) {
8948 struct tog_log_view_state *s = &view->state.log;
8950 err = open_diff_view_for_commit(new_view, y, x,
8951 s->selected_entry->commit, s->selected_entry->id,
8952 view, s->repo);
8953 } else
8954 return got_error_msg(GOT_ERR_NOT_IMPL,
8955 "parent/child view pair not supported");
8956 break;
8957 case TOG_VIEW_BLAME:
8958 if (view->type == TOG_VIEW_TREE) {
8959 struct tog_tree_view_state *s = &view->state.tree;
8961 err = blame_tree_entry(new_view, y, x,
8962 s->selected_entry, &s->parents, s->commit_id,
8963 s->repo);
8964 } else
8965 return got_error_msg(GOT_ERR_NOT_IMPL,
8966 "parent/child view pair not supported");
8967 break;
8968 case TOG_VIEW_LOG:
8969 if (view->type == TOG_VIEW_BLAME)
8970 err = log_annotated_line(new_view, y, x,
8971 view->state.blame.repo, view->state.blame.id_to_log);
8972 else if (view->type == TOG_VIEW_TREE)
8973 err = log_selected_tree_entry(new_view, y, x,
8974 &view->state.tree);
8975 else if (view->type == TOG_VIEW_REF)
8976 err = log_ref_entry(new_view, y, x,
8977 view->state.ref.selected_entry,
8978 view->state.ref.repo);
8979 else
8980 return got_error_msg(GOT_ERR_NOT_IMPL,
8981 "parent/child view pair not supported");
8982 break;
8983 case TOG_VIEW_TREE:
8984 if (view->type == TOG_VIEW_LOG)
8985 err = browse_commit_tree(new_view, y, x,
8986 view->state.log.selected_entry,
8987 view->state.log.in_repo_path,
8988 view->state.log.head_ref_name,
8989 view->state.log.repo);
8990 else if (view->type == TOG_VIEW_REF)
8991 err = browse_ref_tree(new_view, y, x,
8992 view->state.ref.selected_entry,
8993 view->state.ref.repo);
8994 else
8995 return got_error_msg(GOT_ERR_NOT_IMPL,
8996 "parent/child view pair not supported");
8997 break;
8998 case TOG_VIEW_REF:
8999 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9000 if (*new_view == NULL)
9001 return got_error_from_errno("view_open");
9002 if (view->type == TOG_VIEW_LOG)
9003 err = open_ref_view(*new_view, view->state.log.repo);
9004 else if (view->type == TOG_VIEW_TREE)
9005 err = open_ref_view(*new_view, view->state.tree.repo);
9006 else
9007 err = got_error_msg(GOT_ERR_NOT_IMPL,
9008 "parent/child view pair not supported");
9009 if (err)
9010 view_close(*new_view);
9011 break;
9012 case TOG_VIEW_HELP:
9013 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9014 if (*new_view == NULL)
9015 return got_error_from_errno("view_open");
9016 err = open_help_view(*new_view, view);
9017 if (err)
9018 view_close(*new_view);
9019 break;
9020 default:
9021 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9024 return err;
9028 * If view was scrolled down to move the selected line into view when opening a
9029 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9031 static void
9032 offset_selection_up(struct tog_view *view)
9034 switch (view->type) {
9035 case TOG_VIEW_BLAME: {
9036 struct tog_blame_view_state *s = &view->state.blame;
9037 if (s->first_displayed_line == 1) {
9038 s->selected_line = MAX(s->selected_line - view->offset,
9039 1);
9040 break;
9042 if (s->first_displayed_line > view->offset)
9043 s->first_displayed_line -= view->offset;
9044 else
9045 s->first_displayed_line = 1;
9046 s->selected_line += view->offset;
9047 break;
9049 case TOG_VIEW_LOG:
9050 log_scroll_up(&view->state.log, view->offset);
9051 view->state.log.selected += view->offset;
9052 break;
9053 case TOG_VIEW_REF:
9054 ref_scroll_up(&view->state.ref, view->offset);
9055 view->state.ref.selected += view->offset;
9056 break;
9057 case TOG_VIEW_TREE:
9058 tree_scroll_up(&view->state.tree, view->offset);
9059 view->state.tree.selected += view->offset;
9060 break;
9061 default:
9062 break;
9065 view->offset = 0;
9069 * If the selected line is in the section of screen covered by the bottom split,
9070 * scroll down offset lines to move it into view and index its new position.
9072 static const struct got_error *
9073 offset_selection_down(struct tog_view *view)
9075 const struct got_error *err = NULL;
9076 const struct got_error *(*scrolld)(struct tog_view *, int);
9077 int *selected = NULL;
9078 int header, offset;
9080 switch (view->type) {
9081 case TOG_VIEW_BLAME: {
9082 struct tog_blame_view_state *s = &view->state.blame;
9083 header = 3;
9084 scrolld = NULL;
9085 if (s->selected_line > view->nlines - header) {
9086 offset = abs(view->nlines - s->selected_line - header);
9087 s->first_displayed_line += offset;
9088 s->selected_line -= offset;
9089 view->offset = offset;
9091 break;
9093 case TOG_VIEW_LOG: {
9094 struct tog_log_view_state *s = &view->state.log;
9095 scrolld = &log_scroll_down;
9096 header = view_is_parent_view(view) ? 3 : 2;
9097 selected = &s->selected;
9098 break;
9100 case TOG_VIEW_REF: {
9101 struct tog_ref_view_state *s = &view->state.ref;
9102 scrolld = &ref_scroll_down;
9103 header = 3;
9104 selected = &s->selected;
9105 break;
9107 case TOG_VIEW_TREE: {
9108 struct tog_tree_view_state *s = &view->state.tree;
9109 scrolld = &tree_scroll_down;
9110 header = 5;
9111 selected = &s->selected;
9112 break;
9114 default:
9115 selected = NULL;
9116 scrolld = NULL;
9117 header = 0;
9118 break;
9121 if (selected && *selected > view->nlines - header) {
9122 offset = abs(view->nlines - *selected - header);
9123 view->offset = offset;
9124 if (scrolld && offset) {
9125 err = scrolld(view, offset);
9126 *selected -= offset;
9130 return err;
9133 static void
9134 list_commands(FILE *fp)
9136 size_t i;
9138 fprintf(fp, "commands:");
9139 for (i = 0; i < nitems(tog_commands); i++) {
9140 const struct tog_cmd *cmd = &tog_commands[i];
9141 fprintf(fp, " %s", cmd->name);
9143 fputc('\n', fp);
9146 __dead static void
9147 usage(int hflag, int status)
9149 FILE *fp = (status == 0) ? stdout : stderr;
9151 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9152 getprogname());
9153 if (hflag) {
9154 fprintf(fp, "lazy usage: %s path\n", getprogname());
9155 list_commands(fp);
9157 exit(status);
9160 static char **
9161 make_argv(int argc, ...)
9163 va_list ap;
9164 char **argv;
9165 int i;
9167 va_start(ap, argc);
9169 argv = calloc(argc, sizeof(char *));
9170 if (argv == NULL)
9171 err(1, "calloc");
9172 for (i = 0; i < argc; i++) {
9173 argv[i] = strdup(va_arg(ap, char *));
9174 if (argv[i] == NULL)
9175 err(1, "strdup");
9178 va_end(ap);
9179 return argv;
9183 * Try to convert 'tog path' into a 'tog log path' command.
9184 * The user could simply have mistyped the command rather than knowingly
9185 * provided a path. So check whether argv[0] can in fact be resolved
9186 * to a path in the HEAD commit and print a special error if not.
9187 * This hack is for mpi@ <3
9189 static const struct got_error *
9190 tog_log_with_path(int argc, char *argv[])
9192 const struct got_error *error = NULL, *close_err;
9193 const struct tog_cmd *cmd = NULL;
9194 struct got_repository *repo = NULL;
9195 struct got_worktree *worktree = NULL;
9196 struct got_object_id *commit_id = NULL, *id = NULL;
9197 struct got_commit_object *commit = NULL;
9198 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9199 char *commit_id_str = NULL, **cmd_argv = NULL;
9200 int *pack_fds = NULL;
9202 cwd = getcwd(NULL, 0);
9203 if (cwd == NULL)
9204 return got_error_from_errno("getcwd");
9206 error = got_repo_pack_fds_open(&pack_fds);
9207 if (error != NULL)
9208 goto done;
9210 error = got_worktree_open(&worktree, cwd);
9211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9212 goto done;
9214 if (worktree)
9215 repo_path = strdup(got_worktree_get_repo_path(worktree));
9216 else
9217 repo_path = strdup(cwd);
9218 if (repo_path == NULL) {
9219 error = got_error_from_errno("strdup");
9220 goto done;
9223 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9224 if (error != NULL)
9225 goto done;
9227 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9228 repo, worktree);
9229 if (error)
9230 goto done;
9232 error = tog_load_refs(repo, 0);
9233 if (error)
9234 goto done;
9235 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9236 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9237 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9238 if (error)
9239 goto done;
9241 if (worktree) {
9242 got_worktree_close(worktree);
9243 worktree = NULL;
9246 error = got_object_open_as_commit(&commit, repo, commit_id);
9247 if (error)
9248 goto done;
9250 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9251 if (error) {
9252 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9253 goto done;
9254 fprintf(stderr, "%s: '%s' is no known command or path\n",
9255 getprogname(), argv[0]);
9256 usage(1, 1);
9257 /* not reached */
9260 error = got_object_id_str(&commit_id_str, commit_id);
9261 if (error)
9262 goto done;
9264 cmd = &tog_commands[0]; /* log */
9265 argc = 4;
9266 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9267 error = cmd->cmd_main(argc, cmd_argv);
9268 done:
9269 if (repo) {
9270 close_err = got_repo_close(repo);
9271 if (error == NULL)
9272 error = close_err;
9274 if (commit)
9275 got_object_commit_close(commit);
9276 if (worktree)
9277 got_worktree_close(worktree);
9278 if (pack_fds) {
9279 const struct got_error *pack_err =
9280 got_repo_pack_fds_close(pack_fds);
9281 if (error == NULL)
9282 error = pack_err;
9284 free(id);
9285 free(commit_id_str);
9286 free(commit_id);
9287 free(cwd);
9288 free(repo_path);
9289 free(in_repo_path);
9290 if (cmd_argv) {
9291 int i;
9292 for (i = 0; i < argc; i++)
9293 free(cmd_argv[i]);
9294 free(cmd_argv);
9296 tog_free_refs();
9297 return error;
9300 int
9301 main(int argc, char *argv[])
9303 const struct got_error *error = NULL;
9304 const struct tog_cmd *cmd = NULL;
9305 int ch, hflag = 0, Vflag = 0;
9306 char **cmd_argv = NULL;
9307 static const struct option longopts[] = {
9308 { "version", no_argument, NULL, 'V' },
9309 { NULL, 0, NULL, 0}
9311 char *diff_algo_str = NULL;
9313 if (!isatty(STDIN_FILENO))
9314 errx(1, "standard input is not a tty");
9316 setlocale(LC_CTYPE, "");
9318 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9319 switch (ch) {
9320 case 'h':
9321 hflag = 1;
9322 break;
9323 case 'V':
9324 Vflag = 1;
9325 break;
9326 default:
9327 usage(hflag, 1);
9328 /* NOTREACHED */
9332 argc -= optind;
9333 argv += optind;
9334 optind = 1;
9335 optreset = 1;
9337 if (Vflag) {
9338 got_version_print_str();
9339 return 0;
9342 #ifndef PROFILE
9343 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9344 NULL) == -1)
9345 err(1, "pledge");
9346 #endif
9348 if (argc == 0) {
9349 if (hflag)
9350 usage(hflag, 0);
9351 /* Build an argument vector which runs a default command. */
9352 cmd = &tog_commands[0];
9353 argc = 1;
9354 cmd_argv = make_argv(argc, cmd->name);
9355 } else {
9356 size_t i;
9358 /* Did the user specify a command? */
9359 for (i = 0; i < nitems(tog_commands); i++) {
9360 if (strncmp(tog_commands[i].name, argv[0],
9361 strlen(argv[0])) == 0) {
9362 cmd = &tog_commands[i];
9363 break;
9368 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9369 if (diff_algo_str) {
9370 if (strcasecmp(diff_algo_str, "patience") == 0)
9371 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9372 if (strcasecmp(diff_algo_str, "myers") == 0)
9373 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9376 if (cmd == NULL) {
9377 if (argc != 1)
9378 usage(0, 1);
9379 /* No command specified; try log with a path */
9380 error = tog_log_with_path(argc, argv);
9381 } else {
9382 if (hflag)
9383 cmd->cmd_usage();
9384 else
9385 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9388 endwin();
9389 putchar('\n');
9390 if (cmd_argv) {
9391 int i;
9392 for (i = 0; i < argc; i++)
9393 free(cmd_argv[i]);
9394 free(cmd_argv);
9397 if (error && error->code != GOT_ERR_CANCELLED &&
9398 error->code != GOT_ERR_EOF &&
9399 error->code != GOT_ERR_PRIVSEP_EXIT &&
9400 error->code != GOT_ERR_PRIVSEP_PIPE &&
9401 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9402 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9403 return 0;