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;
1312 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1313 v = view->parent;
1315 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1316 wclrtoeol(v->window);
1318 nodelay(v->window, FALSE); /* block for search term input */
1319 nocbreak();
1320 echo();
1321 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1322 wrefresh(v->window);
1323 cbreak();
1324 noecho();
1325 nodelay(v->window, TRUE);
1326 if (ret == ERR)
1327 return NULL;
1329 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1330 err = view->search_start(view);
1331 if (err) {
1332 regfree(&view->regex);
1333 return err;
1335 view->search_started = 1;
1336 view->searching = TOG_SEARCH_FORWARD;
1337 view->search_next_done = 0;
1338 view->search_next(view);
1341 return NULL;
1344 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1345 static const struct got_error *
1346 switch_split(struct tog_view *view)
1348 const struct got_error *err = NULL;
1349 struct tog_view *v = NULL;
1351 if (view->parent)
1352 v = view->parent;
1353 else
1354 v = view;
1356 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1357 v->mode = TOG_VIEW_SPLIT_VERT;
1358 else
1359 v->mode = TOG_VIEW_SPLIT_HRZN;
1361 if (!v->child)
1362 return NULL;
1363 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1364 v->mode = TOG_VIEW_SPLIT_NONE;
1366 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1367 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1368 v->child->begin_y = v->child->resized_y;
1369 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1370 v->child->begin_x = v->child->resized_x;
1373 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1374 v->ncols = COLS;
1375 v->child->ncols = COLS;
1376 v->child->nscrolled = LINES - v->child->nlines;
1378 err = view_init_hsplit(v, v->child->begin_y);
1379 if (err)
1380 return err;
1382 v->child->mode = v->mode;
1383 v->child->nlines = v->lines - v->child->begin_y;
1384 v->focus_child = 1;
1386 err = view_fullscreen(v);
1387 if (err)
1388 return err;
1389 err = view_splitscreen(v->child);
1390 if (err)
1391 return err;
1393 if (v->mode == TOG_VIEW_SPLIT_NONE)
1394 v->mode = TOG_VIEW_SPLIT_VERT;
1395 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1396 err = offset_selection_down(v);
1397 if (err)
1398 return err;
1399 err = offset_selection_down(v->child);
1400 if (err)
1401 return err;
1402 } else {
1403 offset_selection_up(v);
1404 offset_selection_up(v->child);
1406 if (v->resize)
1407 err = v->resize(v, 0);
1408 else if (v->child->resize)
1409 err = v->child->resize(v->child, 0);
1411 return err;
1415 * Compute view->count from numeric input. Assign total to view->count and
1416 * return first non-numeric key entered.
1418 static int
1419 get_compound_key(struct tog_view *view, int c)
1421 struct tog_view *v = view;
1422 int x, n = 0;
1424 if (view_is_hsplit_top(view))
1425 v = view->child;
1426 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1427 v = view->parent;
1429 view->count = 0;
1430 cbreak(); /* block for input */
1431 nodelay(view->window, FALSE);
1432 wmove(v->window, v->nlines - 1, 0);
1433 wclrtoeol(v->window);
1434 waddch(v->window, ':');
1436 do {
1437 x = getcurx(v->window);
1438 if (x != ERR && x < view->ncols) {
1439 waddch(v->window, c);
1440 wrefresh(v->window);
1444 * Don't overflow. Max valid request should be the greatest
1445 * between the longest and total lines; cap at 10 million.
1447 if (n >= 9999999)
1448 n = 9999999;
1449 else
1450 n = n * 10 + (c - '0');
1451 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1453 if (c == 'G' || c == 'g') { /* nG key map */
1454 view->gline = view->hiline = n;
1455 n = 0;
1456 c = 0;
1459 /* Massage excessive or inapplicable values at the input handler. */
1460 view->count = n;
1462 return c;
1465 static const struct got_error *
1466 view_input(struct tog_view **new, int *done, struct tog_view *view,
1467 struct tog_view_list_head *views)
1469 const struct got_error *err = NULL;
1470 struct tog_view *v;
1471 int ch, errcode;
1473 *new = NULL;
1475 /* Clear "no matches" indicator. */
1476 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1477 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1478 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1479 view->count = 0;
1482 if (view->searching && !view->search_next_done) {
1483 errcode = pthread_mutex_unlock(&tog_mutex);
1484 if (errcode)
1485 return got_error_set_errno(errcode,
1486 "pthread_mutex_unlock");
1487 sched_yield();
1488 errcode = pthread_mutex_lock(&tog_mutex);
1489 if (errcode)
1490 return got_error_set_errno(errcode,
1491 "pthread_mutex_lock");
1492 view->search_next(view);
1493 return NULL;
1496 /* Allow threads to make progress while we are waiting for input. */
1497 errcode = pthread_mutex_unlock(&tog_mutex);
1498 if (errcode)
1499 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1500 /* If we have an unfinished count, let C-g or backspace abort. */
1501 if (view->count && --view->count) {
1502 cbreak();
1503 nodelay(view->window, TRUE);
1504 ch = wgetch(view->window);
1505 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1506 view->count = 0;
1507 else
1508 ch = view->ch;
1509 } else {
1510 ch = wgetch(view->window);
1511 if (ch >= '1' && ch <= '9')
1512 view->ch = ch = get_compound_key(view, ch);
1514 if (view->hiline && ch != ERR && ch != 0)
1515 view->hiline = 0; /* key pressed, clear line highlight */
1516 nodelay(view->window, TRUE);
1517 errcode = pthread_mutex_lock(&tog_mutex);
1518 if (errcode)
1519 return got_error_set_errno(errcode, "pthread_mutex_lock");
1521 if (tog_sigwinch_received || tog_sigcont_received) {
1522 tog_resizeterm();
1523 tog_sigwinch_received = 0;
1524 tog_sigcont_received = 0;
1525 TAILQ_FOREACH(v, views, entry) {
1526 err = view_resize(v);
1527 if (err)
1528 return err;
1529 err = v->input(new, v, KEY_RESIZE);
1530 if (err)
1531 return err;
1532 if (v->child) {
1533 err = view_resize(v->child);
1534 if (err)
1535 return err;
1536 err = v->child->input(new, v->child,
1537 KEY_RESIZE);
1538 if (err)
1539 return err;
1540 if (v->child->resized_x || v->child->resized_y) {
1541 err = view_resize_split(v, 0);
1542 if (err)
1543 return err;
1549 switch (ch) {
1550 case '?':
1551 case 'H':
1552 case KEY_F(1):
1553 if (view->type == TOG_VIEW_HELP)
1554 err = view->reset(view);
1555 else
1556 err = view_request_new(new, view, TOG_VIEW_HELP);
1557 break;
1558 case '\t':
1559 view->count = 0;
1560 if (view->child) {
1561 view->focussed = 0;
1562 view->child->focussed = 1;
1563 view->focus_child = 1;
1564 } else if (view->parent) {
1565 view->focussed = 0;
1566 view->parent->focussed = 1;
1567 view->parent->focus_child = 0;
1568 if (!view_is_splitscreen(view)) {
1569 if (view->parent->resize) {
1570 err = view->parent->resize(view->parent,
1571 0);
1572 if (err)
1573 return err;
1575 offset_selection_up(view->parent);
1576 err = view_fullscreen(view->parent);
1577 if (err)
1578 return err;
1581 break;
1582 case 'q':
1583 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1584 if (view->parent->resize) {
1585 /* might need more commits to fill fullscreen */
1586 err = view->parent->resize(view->parent, 0);
1587 if (err)
1588 break;
1590 offset_selection_up(view->parent);
1592 err = view->input(new, view, ch);
1593 view->dying = 1;
1594 break;
1595 case 'Q':
1596 *done = 1;
1597 break;
1598 case 'F':
1599 view->count = 0;
1600 if (view_is_parent_view(view)) {
1601 if (view->child == NULL)
1602 break;
1603 if (view_is_splitscreen(view->child)) {
1604 view->focussed = 0;
1605 view->child->focussed = 1;
1606 err = view_fullscreen(view->child);
1607 } else {
1608 err = view_splitscreen(view->child);
1609 if (!err)
1610 err = view_resize_split(view, 0);
1612 if (err)
1613 break;
1614 err = view->child->input(new, view->child,
1615 KEY_RESIZE);
1616 } else {
1617 if (view_is_splitscreen(view)) {
1618 view->parent->focussed = 0;
1619 view->focussed = 1;
1620 err = view_fullscreen(view);
1621 } else {
1622 err = view_splitscreen(view);
1623 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1624 err = view_resize(view->parent);
1625 if (!err)
1626 err = view_resize_split(view, 0);
1628 if (err)
1629 break;
1630 err = view->input(new, view, KEY_RESIZE);
1632 if (err)
1633 break;
1634 if (view->resize) {
1635 err = view->resize(view, 0);
1636 if (err)
1637 break;
1639 if (view->parent)
1640 err = offset_selection_down(view->parent);
1641 if (!err)
1642 err = offset_selection_down(view);
1643 break;
1644 case 'S':
1645 view->count = 0;
1646 err = switch_split(view);
1647 break;
1648 case '-':
1649 err = view_resize_split(view, -1);
1650 break;
1651 case '+':
1652 err = view_resize_split(view, 1);
1653 break;
1654 case KEY_RESIZE:
1655 break;
1656 case '/':
1657 view->count = 0;
1658 if (view->search_start)
1659 view_search_start(view);
1660 else
1661 err = view->input(new, view, ch);
1662 break;
1663 case 'N':
1664 case 'n':
1665 if (view->search_started && view->search_next) {
1666 view->searching = (ch == 'n' ?
1667 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1668 view->search_next_done = 0;
1669 view->search_next(view);
1670 } else
1671 err = view->input(new, view, ch);
1672 break;
1673 case 'A':
1674 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1675 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1676 else
1677 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1678 TAILQ_FOREACH(v, views, entry) {
1679 if (v->reset) {
1680 err = v->reset(v);
1681 if (err)
1682 return err;
1684 if (v->child && v->child->reset) {
1685 err = v->child->reset(v->child);
1686 if (err)
1687 return err;
1690 break;
1691 default:
1692 err = view->input(new, view, ch);
1693 break;
1696 return err;
1699 static int
1700 view_needs_focus_indication(struct tog_view *view)
1702 if (view_is_parent_view(view)) {
1703 if (view->child == NULL || view->child->focussed)
1704 return 0;
1705 if (!view_is_splitscreen(view->child))
1706 return 0;
1707 } else if (!view_is_splitscreen(view))
1708 return 0;
1710 return view->focussed;
1713 static const struct got_error *
1714 view_loop(struct tog_view *view)
1716 const struct got_error *err = NULL;
1717 struct tog_view_list_head views;
1718 struct tog_view *new_view;
1719 char *mode;
1720 int fast_refresh = 10;
1721 int done = 0, errcode;
1723 mode = getenv("TOG_VIEW_SPLIT_MODE");
1724 if (!mode || !(*mode == 'h' || *mode == 'H'))
1725 view->mode = TOG_VIEW_SPLIT_VERT;
1726 else
1727 view->mode = TOG_VIEW_SPLIT_HRZN;
1729 errcode = pthread_mutex_lock(&tog_mutex);
1730 if (errcode)
1731 return got_error_set_errno(errcode, "pthread_mutex_lock");
1733 TAILQ_INIT(&views);
1734 TAILQ_INSERT_HEAD(&views, view, entry);
1736 view->focussed = 1;
1737 err = view->show(view);
1738 if (err)
1739 return err;
1740 update_panels();
1741 doupdate();
1742 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1743 !tog_fatal_signal_received()) {
1744 /* Refresh fast during initialization, then become slower. */
1745 if (fast_refresh && fast_refresh-- == 0)
1746 halfdelay(10); /* switch to once per second */
1748 err = view_input(&new_view, &done, view, &views);
1749 if (err)
1750 break;
1751 if (view->dying) {
1752 struct tog_view *v, *prev = NULL;
1754 if (view_is_parent_view(view))
1755 prev = TAILQ_PREV(view, tog_view_list_head,
1756 entry);
1757 else if (view->parent)
1758 prev = view->parent;
1760 if (view->parent) {
1761 view->parent->child = NULL;
1762 view->parent->focus_child = 0;
1763 /* Restore fullscreen line height. */
1764 view->parent->nlines = view->parent->lines;
1765 err = view_resize(view->parent);
1766 if (err)
1767 break;
1768 /* Make resized splits persist. */
1769 view_transfer_size(view->parent, view);
1770 } else
1771 TAILQ_REMOVE(&views, view, entry);
1773 err = view_close(view);
1774 if (err)
1775 goto done;
1777 view = NULL;
1778 TAILQ_FOREACH(v, &views, entry) {
1779 if (v->focussed)
1780 break;
1782 if (view == NULL && new_view == NULL) {
1783 /* No view has focus. Try to pick one. */
1784 if (prev)
1785 view = prev;
1786 else if (!TAILQ_EMPTY(&views)) {
1787 view = TAILQ_LAST(&views,
1788 tog_view_list_head);
1790 if (view) {
1791 if (view->focus_child) {
1792 view->child->focussed = 1;
1793 view = view->child;
1794 } else
1795 view->focussed = 1;
1799 if (new_view) {
1800 struct tog_view *v, *t;
1801 /* Only allow one parent view per type. */
1802 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1803 if (v->type != new_view->type)
1804 continue;
1805 TAILQ_REMOVE(&views, v, entry);
1806 err = view_close(v);
1807 if (err)
1808 goto done;
1809 break;
1811 TAILQ_INSERT_TAIL(&views, new_view, entry);
1812 view = new_view;
1814 if (view) {
1815 if (view_is_parent_view(view)) {
1816 if (view->child && view->child->focussed)
1817 view = view->child;
1818 } else {
1819 if (view->parent && view->parent->focussed)
1820 view = view->parent;
1822 show_panel(view->panel);
1823 if (view->child && view_is_splitscreen(view->child))
1824 show_panel(view->child->panel);
1825 if (view->parent && view_is_splitscreen(view)) {
1826 err = view->parent->show(view->parent);
1827 if (err)
1828 goto done;
1830 err = view->show(view);
1831 if (err)
1832 goto done;
1833 if (view->child) {
1834 err = view->child->show(view->child);
1835 if (err)
1836 goto done;
1838 update_panels();
1839 doupdate();
1842 done:
1843 while (!TAILQ_EMPTY(&views)) {
1844 const struct got_error *close_err;
1845 view = TAILQ_FIRST(&views);
1846 TAILQ_REMOVE(&views, view, entry);
1847 close_err = view_close(view);
1848 if (close_err && err == NULL)
1849 err = close_err;
1852 errcode = pthread_mutex_unlock(&tog_mutex);
1853 if (errcode && err == NULL)
1854 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1856 return err;
1859 __dead static void
1860 usage_log(void)
1862 endwin();
1863 fprintf(stderr,
1864 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1865 getprogname());
1866 exit(1);
1869 /* Create newly allocated wide-character string equivalent to a byte string. */
1870 static const struct got_error *
1871 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1873 char *vis = NULL;
1874 const struct got_error *err = NULL;
1876 *ws = NULL;
1877 *wlen = mbstowcs(NULL, s, 0);
1878 if (*wlen == (size_t)-1) {
1879 int vislen;
1880 if (errno != EILSEQ)
1881 return got_error_from_errno("mbstowcs");
1883 /* byte string invalid in current encoding; try to "fix" it */
1884 err = got_mbsavis(&vis, &vislen, s);
1885 if (err)
1886 return err;
1887 *wlen = mbstowcs(NULL, vis, 0);
1888 if (*wlen == (size_t)-1) {
1889 err = got_error_from_errno("mbstowcs"); /* give up */
1890 goto done;
1894 *ws = calloc(*wlen + 1, sizeof(**ws));
1895 if (*ws == NULL) {
1896 err = got_error_from_errno("calloc");
1897 goto done;
1900 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1901 err = got_error_from_errno("mbstowcs");
1902 done:
1903 free(vis);
1904 if (err) {
1905 free(*ws);
1906 *ws = NULL;
1907 *wlen = 0;
1909 return err;
1912 static const struct got_error *
1913 expand_tab(char **ptr, const char *src)
1915 char *dst;
1916 size_t len, n, idx = 0, sz = 0;
1918 *ptr = NULL;
1919 n = len = strlen(src);
1920 dst = malloc(n + 1);
1921 if (dst == NULL)
1922 return got_error_from_errno("malloc");
1924 while (idx < len && src[idx]) {
1925 const char c = src[idx];
1927 if (c == '\t') {
1928 size_t nb = TABSIZE - sz % TABSIZE;
1929 char *p;
1931 p = realloc(dst, n + nb);
1932 if (p == NULL) {
1933 free(dst);
1934 return got_error_from_errno("realloc");
1937 dst = p;
1938 n += nb;
1939 memset(dst + sz, ' ', nb);
1940 sz += nb;
1941 } else
1942 dst[sz++] = src[idx];
1943 ++idx;
1946 dst[sz] = '\0';
1947 *ptr = dst;
1948 return NULL;
1952 * Advance at most n columns from wline starting at offset off.
1953 * Return the index to the first character after the span operation.
1954 * Return the combined column width of all spanned wide character in
1955 * *rcol.
1957 static int
1958 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1960 int width, i, cols = 0;
1962 if (n == 0) {
1963 *rcol = cols;
1964 return off;
1967 for (i = off; wline[i] != L'\0'; ++i) {
1968 if (wline[i] == L'\t')
1969 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1970 else
1971 width = wcwidth(wline[i]);
1973 if (width == -1) {
1974 width = 1;
1975 wline[i] = L'.';
1978 if (cols + width > n)
1979 break;
1980 cols += width;
1983 *rcol = cols;
1984 return i;
1988 * Format a line for display, ensuring that it won't overflow a width limit.
1989 * With scrolling, the width returned refers to the scrolled version of the
1990 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1992 static const struct got_error *
1993 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1994 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1996 const struct got_error *err = NULL;
1997 int cols;
1998 wchar_t *wline = NULL;
1999 char *exstr = NULL;
2000 size_t wlen;
2001 int i, scrollx;
2003 *wlinep = NULL;
2004 *widthp = 0;
2006 if (expand) {
2007 err = expand_tab(&exstr, line);
2008 if (err)
2009 return err;
2012 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
2013 free(exstr);
2014 if (err)
2015 return err;
2017 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
2019 if (wlen > 0 && wline[wlen - 1] == L'\n') {
2020 wline[wlen - 1] = L'\0';
2021 wlen--;
2023 if (wlen > 0 && wline[wlen - 1] == L'\r') {
2024 wline[wlen - 1] = L'\0';
2025 wlen--;
2028 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
2029 wline[i] = L'\0';
2031 if (widthp)
2032 *widthp = cols;
2033 if (scrollxp)
2034 *scrollxp = scrollx;
2035 if (err)
2036 free(wline);
2037 else
2038 *wlinep = wline;
2039 return err;
2042 static const struct got_error*
2043 build_refs_str(char **refs_str, struct got_reflist_head *refs,
2044 struct got_object_id *id, struct got_repository *repo)
2046 static const struct got_error *err = NULL;
2047 struct got_reflist_entry *re;
2048 char *s;
2049 const char *name;
2051 *refs_str = NULL;
2053 TAILQ_FOREACH(re, refs, entry) {
2054 struct got_tag_object *tag = NULL;
2055 struct got_object_id *ref_id;
2056 int cmp;
2058 name = got_ref_get_name(re->ref);
2059 if (strcmp(name, GOT_REF_HEAD) == 0)
2060 continue;
2061 if (strncmp(name, "refs/", 5) == 0)
2062 name += 5;
2063 if (strncmp(name, "got/", 4) == 0 &&
2064 strncmp(name, "got/backup/", 11) != 0)
2065 continue;
2066 if (strncmp(name, "heads/", 6) == 0)
2067 name += 6;
2068 if (strncmp(name, "remotes/", 8) == 0) {
2069 name += 8;
2070 s = strstr(name, "/" GOT_REF_HEAD);
2071 if (s != NULL && s[strlen(s)] == '\0')
2072 continue;
2074 err = got_ref_resolve(&ref_id, repo, re->ref);
2075 if (err)
2076 break;
2077 if (strncmp(name, "tags/", 5) == 0) {
2078 err = got_object_open_as_tag(&tag, repo, ref_id);
2079 if (err) {
2080 if (err->code != GOT_ERR_OBJ_TYPE) {
2081 free(ref_id);
2082 break;
2084 /* Ref points at something other than a tag. */
2085 err = NULL;
2086 tag = NULL;
2089 cmp = got_object_id_cmp(tag ?
2090 got_object_tag_get_object_id(tag) : ref_id, id);
2091 free(ref_id);
2092 if (tag)
2093 got_object_tag_close(tag);
2094 if (cmp != 0)
2095 continue;
2096 s = *refs_str;
2097 if (asprintf(refs_str, "%s%s%s", s ? s : "",
2098 s ? ", " : "", name) == -1) {
2099 err = got_error_from_errno("asprintf");
2100 free(s);
2101 *refs_str = NULL;
2102 break;
2104 free(s);
2107 return err;
2110 static const struct got_error *
2111 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
2112 int col_tab_align)
2114 char *smallerthan;
2116 smallerthan = strchr(author, '<');
2117 if (smallerthan && smallerthan[1] != '\0')
2118 author = smallerthan + 1;
2119 author[strcspn(author, "@>")] = '\0';
2120 return format_line(wauthor, author_width, NULL, author, 0, limit,
2121 col_tab_align, 0);
2124 static const struct got_error *
2125 draw_commit(struct tog_view *view, struct got_commit_object *commit,
2126 struct got_object_id *id, const size_t date_display_cols,
2127 int author_display_cols)
2129 struct tog_log_view_state *s = &view->state.log;
2130 const struct got_error *err = NULL;
2131 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
2132 char *logmsg0 = NULL, *logmsg = NULL;
2133 char *author = NULL;
2134 wchar_t *wlogmsg = NULL, *wauthor = NULL;
2135 int author_width, logmsg_width;
2136 char *newline, *line = NULL;
2137 int col, limit, scrollx;
2138 const int avail = view->ncols;
2139 struct tm tm;
2140 time_t committer_time;
2141 struct tog_color *tc;
2143 committer_time = got_object_commit_get_committer_time(commit);
2144 if (gmtime_r(&committer_time, &tm) == NULL)
2145 return got_error_from_errno("gmtime_r");
2146 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2147 return got_error(GOT_ERR_NO_SPACE);
2149 if (avail <= date_display_cols)
2150 limit = MIN(sizeof(datebuf) - 1, avail);
2151 else
2152 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2153 tc = get_color(&s->colors, TOG_COLOR_DATE);
2154 if (tc)
2155 wattr_on(view->window,
2156 COLOR_PAIR(tc->colorpair), NULL);
2157 waddnstr(view->window, datebuf, limit);
2158 if (tc)
2159 wattr_off(view->window,
2160 COLOR_PAIR(tc->colorpair), NULL);
2161 col = limit;
2162 if (col > avail)
2163 goto done;
2165 if (avail >= 120) {
2166 char *id_str;
2167 err = got_object_id_str(&id_str, id);
2168 if (err)
2169 goto done;
2170 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2171 if (tc)
2172 wattr_on(view->window,
2173 COLOR_PAIR(tc->colorpair), NULL);
2174 wprintw(view->window, "%.8s ", id_str);
2175 if (tc)
2176 wattr_off(view->window,
2177 COLOR_PAIR(tc->colorpair), NULL);
2178 free(id_str);
2179 col += 9;
2180 if (col > avail)
2181 goto done;
2184 if (s->use_committer)
2185 author = strdup(got_object_commit_get_committer(commit));
2186 else
2187 author = strdup(got_object_commit_get_author(commit));
2188 if (author == NULL) {
2189 err = got_error_from_errno("strdup");
2190 goto done;
2192 err = format_author(&wauthor, &author_width, author, avail - col, col);
2193 if (err)
2194 goto done;
2195 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2196 if (tc)
2197 wattr_on(view->window,
2198 COLOR_PAIR(tc->colorpair), NULL);
2199 waddwstr(view->window, wauthor);
2200 col += author_width;
2201 while (col < avail && author_width < author_display_cols + 2) {
2202 waddch(view->window, ' ');
2203 col++;
2204 author_width++;
2206 if (tc)
2207 wattr_off(view->window,
2208 COLOR_PAIR(tc->colorpair), NULL);
2209 if (col > avail)
2210 goto done;
2212 err = got_object_commit_get_logmsg(&logmsg0, commit);
2213 if (err)
2214 goto done;
2215 logmsg = logmsg0;
2216 while (*logmsg == '\n')
2217 logmsg++;
2218 newline = strchr(logmsg, '\n');
2219 if (newline)
2220 *newline = '\0';
2221 limit = avail - col;
2222 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2223 limit--; /* for the border */
2224 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2225 limit, col, 1);
2226 if (err)
2227 goto done;
2228 waddwstr(view->window, &wlogmsg[scrollx]);
2229 col += MAX(logmsg_width, 0);
2230 while (col < avail) {
2231 waddch(view->window, ' ');
2232 col++;
2234 done:
2235 free(logmsg0);
2236 free(wlogmsg);
2237 free(author);
2238 free(wauthor);
2239 free(line);
2240 return err;
2243 static struct commit_queue_entry *
2244 alloc_commit_queue_entry(struct got_commit_object *commit,
2245 struct got_object_id *id)
2247 struct commit_queue_entry *entry;
2248 struct got_object_id *dup;
2250 entry = calloc(1, sizeof(*entry));
2251 if (entry == NULL)
2252 return NULL;
2254 dup = got_object_id_dup(id);
2255 if (dup == NULL) {
2256 free(entry);
2257 return NULL;
2260 entry->id = dup;
2261 entry->commit = commit;
2262 return entry;
2265 static void
2266 pop_commit(struct commit_queue *commits)
2268 struct commit_queue_entry *entry;
2270 entry = TAILQ_FIRST(&commits->head);
2271 TAILQ_REMOVE(&commits->head, entry, entry);
2272 got_object_commit_close(entry->commit);
2273 commits->ncommits--;
2274 free(entry->id);
2275 free(entry);
2278 static void
2279 free_commits(struct commit_queue *commits)
2281 while (!TAILQ_EMPTY(&commits->head))
2282 pop_commit(commits);
2285 static const struct got_error *
2286 match_commit(int *have_match, struct got_object_id *id,
2287 struct got_commit_object *commit, regex_t *regex)
2289 const struct got_error *err = NULL;
2290 regmatch_t regmatch;
2291 char *id_str = NULL, *logmsg = NULL;
2293 *have_match = 0;
2295 err = got_object_id_str(&id_str, id);
2296 if (err)
2297 return err;
2299 err = got_object_commit_get_logmsg(&logmsg, commit);
2300 if (err)
2301 goto done;
2303 if (regexec(regex, got_object_commit_get_author(commit), 1,
2304 &regmatch, 0) == 0 ||
2305 regexec(regex, got_object_commit_get_committer(commit), 1,
2306 &regmatch, 0) == 0 ||
2307 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2308 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2309 *have_match = 1;
2310 done:
2311 free(id_str);
2312 free(logmsg);
2313 return err;
2316 static const struct got_error *
2317 queue_commits(struct tog_log_thread_args *a)
2319 const struct got_error *err = NULL;
2322 * We keep all commits open throughout the lifetime of the log
2323 * view in order to avoid having to re-fetch commits from disk
2324 * while updating the display.
2326 do {
2327 struct got_object_id id;
2328 struct got_commit_object *commit;
2329 struct commit_queue_entry *entry;
2330 int limit_match = 0;
2331 int errcode;
2333 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2334 NULL, NULL);
2335 if (err)
2336 break;
2338 err = got_object_open_as_commit(&commit, a->repo, &id);
2339 if (err)
2340 break;
2341 entry = alloc_commit_queue_entry(commit, &id);
2342 if (entry == NULL) {
2343 err = got_error_from_errno("alloc_commit_queue_entry");
2344 break;
2347 errcode = pthread_mutex_lock(&tog_mutex);
2348 if (errcode) {
2349 err = got_error_set_errno(errcode,
2350 "pthread_mutex_lock");
2351 break;
2354 entry->idx = a->real_commits->ncommits;
2355 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2356 a->real_commits->ncommits++;
2358 if (*a->limiting) {
2359 err = match_commit(&limit_match, &id, commit,
2360 a->limit_regex);
2361 if (err)
2362 break;
2364 if (limit_match) {
2365 struct commit_queue_entry *matched;
2367 matched = alloc_commit_queue_entry(
2368 entry->commit, entry->id);
2369 if (matched == NULL) {
2370 err = got_error_from_errno(
2371 "alloc_commit_queue_entry");
2372 break;
2374 matched->commit = entry->commit;
2375 got_object_commit_retain(entry->commit);
2377 matched->idx = a->limit_commits->ncommits;
2378 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2379 matched, entry);
2380 a->limit_commits->ncommits++;
2384 * This is how we signal log_thread() that we
2385 * have found a match, and that it should be
2386 * counted as a new entry for the view.
2388 a->limit_match = limit_match;
2391 if (*a->searching == TOG_SEARCH_FORWARD &&
2392 !*a->search_next_done) {
2393 int have_match;
2394 err = match_commit(&have_match, &id, commit, a->regex);
2395 if (err)
2396 break;
2398 if (*a->limiting) {
2399 if (limit_match && have_match)
2400 *a->search_next_done =
2401 TOG_SEARCH_HAVE_MORE;
2402 } else if (have_match)
2403 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2406 errcode = pthread_mutex_unlock(&tog_mutex);
2407 if (errcode && err == NULL)
2408 err = got_error_set_errno(errcode,
2409 "pthread_mutex_unlock");
2410 if (err)
2411 break;
2412 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2414 return err;
2417 static void
2418 select_commit(struct tog_log_view_state *s)
2420 struct commit_queue_entry *entry;
2421 int ncommits = 0;
2423 entry = s->first_displayed_entry;
2424 while (entry) {
2425 if (ncommits == s->selected) {
2426 s->selected_entry = entry;
2427 break;
2429 entry = TAILQ_NEXT(entry, entry);
2430 ncommits++;
2434 static const struct got_error *
2435 draw_commits(struct tog_view *view)
2437 const struct got_error *err = NULL;
2438 struct tog_log_view_state *s = &view->state.log;
2439 struct commit_queue_entry *entry = s->selected_entry;
2440 int limit = view->nlines;
2441 int width;
2442 int ncommits, author_cols = 4;
2443 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2444 char *refs_str = NULL;
2445 wchar_t *wline;
2446 struct tog_color *tc;
2447 static const size_t date_display_cols = 12;
2449 if (view_is_hsplit_top(view))
2450 --limit; /* account for border */
2452 if (s->selected_entry &&
2453 !(view->searching && view->search_next_done == 0)) {
2454 struct got_reflist_head *refs;
2455 err = got_object_id_str(&id_str, s->selected_entry->id);
2456 if (err)
2457 return err;
2458 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2459 s->selected_entry->id);
2460 if (refs) {
2461 err = build_refs_str(&refs_str, refs,
2462 s->selected_entry->id, s->repo);
2463 if (err)
2464 goto done;
2468 if (s->thread_args.commits_needed == 0)
2469 halfdelay(10); /* disable fast refresh */
2471 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2472 if (asprintf(&ncommits_str, " [%d/%d] %s",
2473 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2474 (view->searching && !view->search_next_done) ?
2475 "searching..." : "loading...") == -1) {
2476 err = got_error_from_errno("asprintf");
2477 goto done;
2479 } else {
2480 const char *search_str = NULL;
2481 const char *limit_str = NULL;
2483 if (view->searching) {
2484 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2485 search_str = "no more matches";
2486 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2487 search_str = "no matches found";
2488 else if (!view->search_next_done)
2489 search_str = "searching...";
2492 if (s->limit_view && s->commits->ncommits == 0)
2493 limit_str = "no matches found";
2495 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2496 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2497 search_str ? search_str : (refs_str ? refs_str : ""),
2498 limit_str ? limit_str : "") == -1) {
2499 err = got_error_from_errno("asprintf");
2500 goto done;
2504 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2505 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2506 "........................................",
2507 s->in_repo_path, ncommits_str) == -1) {
2508 err = got_error_from_errno("asprintf");
2509 header = NULL;
2510 goto done;
2512 } else if (asprintf(&header, "commit %s%s",
2513 id_str ? id_str : "........................................",
2514 ncommits_str) == -1) {
2515 err = got_error_from_errno("asprintf");
2516 header = NULL;
2517 goto done;
2519 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2520 if (err)
2521 goto done;
2523 werase(view->window);
2525 if (view_needs_focus_indication(view))
2526 wstandout(view->window);
2527 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2528 if (tc)
2529 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2530 waddwstr(view->window, wline);
2531 while (width < view->ncols) {
2532 waddch(view->window, ' ');
2533 width++;
2535 if (tc)
2536 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2537 if (view_needs_focus_indication(view))
2538 wstandend(view->window);
2539 free(wline);
2540 if (limit <= 1)
2541 goto done;
2543 /* Grow author column size if necessary, and set view->maxx. */
2544 entry = s->first_displayed_entry;
2545 ncommits = 0;
2546 view->maxx = 0;
2547 while (entry) {
2548 struct got_commit_object *c = entry->commit;
2549 char *author, *eol, *msg, *msg0;
2550 wchar_t *wauthor, *wmsg;
2551 int width;
2552 if (ncommits >= limit - 1)
2553 break;
2554 if (s->use_committer)
2555 author = strdup(got_object_commit_get_committer(c));
2556 else
2557 author = strdup(got_object_commit_get_author(c));
2558 if (author == NULL) {
2559 err = got_error_from_errno("strdup");
2560 goto done;
2562 err = format_author(&wauthor, &width, author, COLS,
2563 date_display_cols);
2564 if (author_cols < width)
2565 author_cols = width;
2566 free(wauthor);
2567 free(author);
2568 if (err)
2569 goto done;
2570 err = got_object_commit_get_logmsg(&msg0, c);
2571 if (err)
2572 goto done;
2573 msg = msg0;
2574 while (*msg == '\n')
2575 ++msg;
2576 if ((eol = strchr(msg, '\n')))
2577 *eol = '\0';
2578 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2579 date_display_cols + author_cols, 0);
2580 if (err)
2581 goto done;
2582 view->maxx = MAX(view->maxx, width);
2583 free(msg0);
2584 free(wmsg);
2585 ncommits++;
2586 entry = TAILQ_NEXT(entry, entry);
2589 entry = s->first_displayed_entry;
2590 s->last_displayed_entry = s->first_displayed_entry;
2591 ncommits = 0;
2592 while (entry) {
2593 if (ncommits >= limit - 1)
2594 break;
2595 if (ncommits == s->selected)
2596 wstandout(view->window);
2597 err = draw_commit(view, entry->commit, entry->id,
2598 date_display_cols, author_cols);
2599 if (ncommits == s->selected)
2600 wstandend(view->window);
2601 if (err)
2602 goto done;
2603 ncommits++;
2604 s->last_displayed_entry = entry;
2605 entry = TAILQ_NEXT(entry, entry);
2608 view_border(view);
2609 done:
2610 free(id_str);
2611 free(refs_str);
2612 free(ncommits_str);
2613 free(header);
2614 return err;
2617 static void
2618 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2620 struct commit_queue_entry *entry;
2621 int nscrolled = 0;
2623 entry = TAILQ_FIRST(&s->commits->head);
2624 if (s->first_displayed_entry == entry)
2625 return;
2627 entry = s->first_displayed_entry;
2628 while (entry && nscrolled < maxscroll) {
2629 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2630 if (entry) {
2631 s->first_displayed_entry = entry;
2632 nscrolled++;
2637 static const struct got_error *
2638 trigger_log_thread(struct tog_view *view, int wait)
2640 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2641 int errcode;
2643 halfdelay(1); /* fast refresh while loading commits */
2645 while (!ta->log_complete && !tog_thread_error &&
2646 (ta->commits_needed > 0 || ta->load_all)) {
2647 /* Wake the log thread. */
2648 errcode = pthread_cond_signal(&ta->need_commits);
2649 if (errcode)
2650 return got_error_set_errno(errcode,
2651 "pthread_cond_signal");
2654 * The mutex will be released while the view loop waits
2655 * in wgetch(), at which time the log thread will run.
2657 if (!wait)
2658 break;
2660 /* Display progress update in log view. */
2661 show_log_view(view);
2662 update_panels();
2663 doupdate();
2665 /* Wait right here while next commit is being loaded. */
2666 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2667 if (errcode)
2668 return got_error_set_errno(errcode,
2669 "pthread_cond_wait");
2671 /* Display progress update in log view. */
2672 show_log_view(view);
2673 update_panels();
2674 doupdate();
2677 return NULL;
2680 static const struct got_error *
2681 request_log_commits(struct tog_view *view)
2683 struct tog_log_view_state *state = &view->state.log;
2684 const struct got_error *err = NULL;
2686 if (state->thread_args.log_complete)
2687 return NULL;
2689 state->thread_args.commits_needed += view->nscrolled;
2690 err = trigger_log_thread(view, 1);
2691 view->nscrolled = 0;
2693 return err;
2696 static const struct got_error *
2697 log_scroll_down(struct tog_view *view, int maxscroll)
2699 struct tog_log_view_state *s = &view->state.log;
2700 const struct got_error *err = NULL;
2701 struct commit_queue_entry *pentry;
2702 int nscrolled = 0, ncommits_needed;
2704 if (s->last_displayed_entry == NULL)
2705 return NULL;
2707 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2708 if (s->commits->ncommits < ncommits_needed &&
2709 !s->thread_args.log_complete) {
2711 * Ask the log thread for required amount of commits.
2713 s->thread_args.commits_needed +=
2714 ncommits_needed - s->commits->ncommits;
2715 err = trigger_log_thread(view, 1);
2716 if (err)
2717 return err;
2720 do {
2721 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2722 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2723 break;
2725 s->last_displayed_entry = pentry ?
2726 pentry : s->last_displayed_entry;;
2728 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2729 if (pentry == NULL)
2730 break;
2731 s->first_displayed_entry = pentry;
2732 } while (++nscrolled < maxscroll);
2734 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2735 view->nscrolled += nscrolled;
2736 else
2737 view->nscrolled = 0;
2739 return err;
2742 static const struct got_error *
2743 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2744 struct got_commit_object *commit, struct got_object_id *commit_id,
2745 struct tog_view *log_view, struct got_repository *repo)
2747 const struct got_error *err;
2748 struct got_object_qid *parent_id;
2749 struct tog_view *diff_view;
2751 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2752 if (diff_view == NULL)
2753 return got_error_from_errno("view_open");
2755 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2756 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2757 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2758 if (err == NULL)
2759 *new_view = diff_view;
2760 return err;
2763 static const struct got_error *
2764 tree_view_visit_subtree(struct tog_tree_view_state *s,
2765 struct got_tree_object *subtree)
2767 struct tog_parent_tree *parent;
2769 parent = calloc(1, sizeof(*parent));
2770 if (parent == NULL)
2771 return got_error_from_errno("calloc");
2773 parent->tree = s->tree;
2774 parent->first_displayed_entry = s->first_displayed_entry;
2775 parent->selected_entry = s->selected_entry;
2776 parent->selected = s->selected;
2777 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2778 s->tree = subtree;
2779 s->selected = 0;
2780 s->first_displayed_entry = NULL;
2781 return NULL;
2784 static const struct got_error *
2785 tree_view_walk_path(struct tog_tree_view_state *s,
2786 struct got_commit_object *commit, const char *path)
2788 const struct got_error *err = NULL;
2789 struct got_tree_object *tree = NULL;
2790 const char *p;
2791 char *slash, *subpath = NULL;
2793 /* Walk the path and open corresponding tree objects. */
2794 p = path;
2795 while (*p) {
2796 struct got_tree_entry *te;
2797 struct got_object_id *tree_id;
2798 char *te_name;
2800 while (p[0] == '/')
2801 p++;
2803 /* Ensure the correct subtree entry is selected. */
2804 slash = strchr(p, '/');
2805 if (slash == NULL)
2806 te_name = strdup(p);
2807 else
2808 te_name = strndup(p, slash - p);
2809 if (te_name == NULL) {
2810 err = got_error_from_errno("strndup");
2811 break;
2813 te = got_object_tree_find_entry(s->tree, te_name);
2814 if (te == NULL) {
2815 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2816 free(te_name);
2817 break;
2819 free(te_name);
2820 s->first_displayed_entry = s->selected_entry = te;
2822 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2823 break; /* jump to this file's entry */
2825 slash = strchr(p, '/');
2826 if (slash)
2827 subpath = strndup(path, slash - path);
2828 else
2829 subpath = strdup(path);
2830 if (subpath == NULL) {
2831 err = got_error_from_errno("strdup");
2832 break;
2835 err = got_object_id_by_path(&tree_id, s->repo, commit,
2836 subpath);
2837 if (err)
2838 break;
2840 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2841 free(tree_id);
2842 if (err)
2843 break;
2845 err = tree_view_visit_subtree(s, tree);
2846 if (err) {
2847 got_object_tree_close(tree);
2848 break;
2850 if (slash == NULL)
2851 break;
2852 free(subpath);
2853 subpath = NULL;
2854 p = slash;
2857 free(subpath);
2858 return err;
2861 static const struct got_error *
2862 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2863 struct commit_queue_entry *entry, const char *path,
2864 const char *head_ref_name, struct got_repository *repo)
2866 const struct got_error *err = NULL;
2867 struct tog_tree_view_state *s;
2868 struct tog_view *tree_view;
2870 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2871 if (tree_view == NULL)
2872 return got_error_from_errno("view_open");
2874 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2875 if (err)
2876 return err;
2877 s = &tree_view->state.tree;
2879 *new_view = tree_view;
2881 if (got_path_is_root_dir(path))
2882 return NULL;
2884 return tree_view_walk_path(s, entry->commit, path);
2887 static const struct got_error *
2888 block_signals_used_by_main_thread(void)
2890 sigset_t sigset;
2891 int errcode;
2893 if (sigemptyset(&sigset) == -1)
2894 return got_error_from_errno("sigemptyset");
2896 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2897 if (sigaddset(&sigset, SIGWINCH) == -1)
2898 return got_error_from_errno("sigaddset");
2899 if (sigaddset(&sigset, SIGCONT) == -1)
2900 return got_error_from_errno("sigaddset");
2901 if (sigaddset(&sigset, SIGINT) == -1)
2902 return got_error_from_errno("sigaddset");
2903 if (sigaddset(&sigset, SIGTERM) == -1)
2904 return got_error_from_errno("sigaddset");
2906 /* ncurses handles SIGTSTP */
2907 if (sigaddset(&sigset, SIGTSTP) == -1)
2908 return got_error_from_errno("sigaddset");
2910 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2911 if (errcode)
2912 return got_error_set_errno(errcode, "pthread_sigmask");
2914 return NULL;
2917 static void *
2918 log_thread(void *arg)
2920 const struct got_error *err = NULL;
2921 int errcode = 0;
2922 struct tog_log_thread_args *a = arg;
2923 int done = 0;
2926 * Sync startup with main thread such that we begin our
2927 * work once view_input() has released the mutex.
2929 errcode = pthread_mutex_lock(&tog_mutex);
2930 if (errcode) {
2931 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2932 return (void *)err;
2935 err = block_signals_used_by_main_thread();
2936 if (err) {
2937 pthread_mutex_unlock(&tog_mutex);
2938 goto done;
2941 while (!done && !err && !tog_fatal_signal_received()) {
2942 errcode = pthread_mutex_unlock(&tog_mutex);
2943 if (errcode) {
2944 err = got_error_set_errno(errcode,
2945 "pthread_mutex_unlock");
2946 goto done;
2948 err = queue_commits(a);
2949 if (err) {
2950 if (err->code != GOT_ERR_ITER_COMPLETED)
2951 goto done;
2952 err = NULL;
2953 done = 1;
2954 } else if (a->commits_needed > 0 && !a->load_all) {
2955 if (*a->limiting) {
2956 if (a->limit_match)
2957 a->commits_needed--;
2958 } else
2959 a->commits_needed--;
2962 errcode = pthread_mutex_lock(&tog_mutex);
2963 if (errcode) {
2964 err = got_error_set_errno(errcode,
2965 "pthread_mutex_lock");
2966 goto done;
2967 } else if (*a->quit)
2968 done = 1;
2969 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2970 *a->first_displayed_entry =
2971 TAILQ_FIRST(&a->limit_commits->head);
2972 *a->selected_entry = *a->first_displayed_entry;
2973 } else if (*a->first_displayed_entry == NULL) {
2974 *a->first_displayed_entry =
2975 TAILQ_FIRST(&a->real_commits->head);
2976 *a->selected_entry = *a->first_displayed_entry;
2979 errcode = pthread_cond_signal(&a->commit_loaded);
2980 if (errcode) {
2981 err = got_error_set_errno(errcode,
2982 "pthread_cond_signal");
2983 pthread_mutex_unlock(&tog_mutex);
2984 goto done;
2987 if (done)
2988 a->commits_needed = 0;
2989 else {
2990 if (a->commits_needed == 0 && !a->load_all) {
2991 errcode = pthread_cond_wait(&a->need_commits,
2992 &tog_mutex);
2993 if (errcode) {
2994 err = got_error_set_errno(errcode,
2995 "pthread_cond_wait");
2996 pthread_mutex_unlock(&tog_mutex);
2997 goto done;
2999 if (*a->quit)
3000 done = 1;
3004 a->log_complete = 1;
3005 errcode = pthread_mutex_unlock(&tog_mutex);
3006 if (errcode)
3007 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3008 done:
3009 if (err) {
3010 tog_thread_error = 1;
3011 pthread_cond_signal(&a->commit_loaded);
3013 return (void *)err;
3016 static const struct got_error *
3017 stop_log_thread(struct tog_log_view_state *s)
3019 const struct got_error *err = NULL, *thread_err = NULL;
3020 int errcode;
3022 if (s->thread) {
3023 s->quit = 1;
3024 errcode = pthread_cond_signal(&s->thread_args.need_commits);
3025 if (errcode)
3026 return got_error_set_errno(errcode,
3027 "pthread_cond_signal");
3028 errcode = pthread_mutex_unlock(&tog_mutex);
3029 if (errcode)
3030 return got_error_set_errno(errcode,
3031 "pthread_mutex_unlock");
3032 errcode = pthread_join(s->thread, (void **)&thread_err);
3033 if (errcode)
3034 return got_error_set_errno(errcode, "pthread_join");
3035 errcode = pthread_mutex_lock(&tog_mutex);
3036 if (errcode)
3037 return got_error_set_errno(errcode,
3038 "pthread_mutex_lock");
3039 s->thread = 0; //NULL;
3042 if (s->thread_args.repo) {
3043 err = got_repo_close(s->thread_args.repo);
3044 s->thread_args.repo = NULL;
3047 if (s->thread_args.pack_fds) {
3048 const struct got_error *pack_err =
3049 got_repo_pack_fds_close(s->thread_args.pack_fds);
3050 if (err == NULL)
3051 err = pack_err;
3052 s->thread_args.pack_fds = NULL;
3055 if (s->thread_args.graph) {
3056 got_commit_graph_close(s->thread_args.graph);
3057 s->thread_args.graph = NULL;
3060 return err ? err : thread_err;
3063 static const struct got_error *
3064 close_log_view(struct tog_view *view)
3066 const struct got_error *err = NULL;
3067 struct tog_log_view_state *s = &view->state.log;
3068 int errcode;
3070 err = stop_log_thread(s);
3072 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
3073 if (errcode && err == NULL)
3074 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3076 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
3077 if (errcode && err == NULL)
3078 err = got_error_set_errno(errcode, "pthread_cond_destroy");
3080 free_commits(&s->limit_commits);
3081 free_commits(&s->real_commits);
3082 free(s->in_repo_path);
3083 s->in_repo_path = NULL;
3084 free(s->start_id);
3085 s->start_id = NULL;
3086 free(s->head_ref_name);
3087 s->head_ref_name = NULL;
3088 return err;
3092 * We use two queues to implement the limit feature: first consists of
3093 * commits matching the current limit_regex; second is the real queue
3094 * of all known commits (real_commits). When the user starts limiting,
3095 * we swap queues such that all movement and displaying functionality
3096 * works with very slight change.
3098 static const struct got_error *
3099 limit_log_view(struct tog_view *view)
3101 struct tog_log_view_state *s = &view->state.log;
3102 struct commit_queue_entry *entry;
3103 struct tog_view *v = view;
3104 const struct got_error *err = NULL;
3105 char pattern[1024];
3106 int ret;
3108 if (view_is_hsplit_top(view))
3109 v = view->child;
3110 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
3111 v = view->parent;
3113 /* Get the pattern */
3114 wmove(v->window, v->nlines - 1, 0);
3115 wclrtoeol(v->window);
3116 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
3117 nodelay(v->window, FALSE);
3118 nocbreak();
3119 echo();
3120 ret = wgetnstr(v->window, pattern, sizeof(pattern));
3121 cbreak();
3122 noecho();
3123 nodelay(v->window, TRUE);
3124 if (ret == ERR)
3125 return NULL;
3127 if (*pattern == '\0') {
3129 * Safety measure for the situation where the user
3130 * resets limit without previously limiting anything.
3132 if (!s->limit_view)
3133 return NULL;
3136 * User could have pressed Ctrl+L, which refreshed the
3137 * commit queues, it means we can't save previously
3138 * (before limit took place) displayed entries,
3139 * because they would point to already free'ed memory,
3140 * so we are forced to always select first entry of
3141 * the queue.
3143 s->commits = &s->real_commits;
3144 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3145 s->selected_entry = s->first_displayed_entry;
3146 s->selected = 0;
3147 s->limit_view = 0;
3149 return NULL;
3152 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3153 return NULL;
3155 s->limit_view = 1;
3157 /* Clear the screen while loading limit view */
3158 s->first_displayed_entry = NULL;
3159 s->last_displayed_entry = NULL;
3160 s->selected_entry = NULL;
3161 s->commits = &s->limit_commits;
3163 /* Prepare limit queue for new search */
3164 free_commits(&s->limit_commits);
3165 s->limit_commits.ncommits = 0;
3167 /* First process commits, which are in queue already */
3168 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3169 int have_match = 0;
3171 err = match_commit(&have_match, entry->id,
3172 entry->commit, &s->limit_regex);
3173 if (err)
3174 return err;
3176 if (have_match) {
3177 struct commit_queue_entry *matched;
3179 matched = alloc_commit_queue_entry(entry->commit,
3180 entry->id);
3181 if (matched == NULL) {
3182 err = got_error_from_errno(
3183 "alloc_commit_queue_entry");
3184 break;
3186 matched->commit = entry->commit;
3187 got_object_commit_retain(entry->commit);
3189 matched->idx = s->limit_commits.ncommits;
3190 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3191 matched, entry);
3192 s->limit_commits.ncommits++;
3196 /* Second process all the commits, until we fill the screen */
3197 if (s->limit_commits.ncommits < view->nlines - 1 &&
3198 !s->thread_args.log_complete) {
3199 s->thread_args.commits_needed +=
3200 view->nlines - s->limit_commits.ncommits - 1;
3201 err = trigger_log_thread(view, 1);
3202 if (err)
3203 return err;
3206 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3207 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3208 s->selected = 0;
3210 return NULL;
3213 static const struct got_error *
3214 search_start_log_view(struct tog_view *view)
3216 struct tog_log_view_state *s = &view->state.log;
3218 s->matched_entry = NULL;
3219 s->search_entry = NULL;
3220 return NULL;
3223 static const struct got_error *
3224 search_next_log_view(struct tog_view *view)
3226 const struct got_error *err = NULL;
3227 struct tog_log_view_state *s = &view->state.log;
3228 struct commit_queue_entry *entry;
3230 /* Display progress update in log view. */
3231 show_log_view(view);
3232 update_panels();
3233 doupdate();
3235 if (s->search_entry) {
3236 int errcode, ch;
3237 errcode = pthread_mutex_unlock(&tog_mutex);
3238 if (errcode)
3239 return got_error_set_errno(errcode,
3240 "pthread_mutex_unlock");
3241 ch = wgetch(view->window);
3242 errcode = pthread_mutex_lock(&tog_mutex);
3243 if (errcode)
3244 return got_error_set_errno(errcode,
3245 "pthread_mutex_lock");
3246 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3247 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3248 return NULL;
3250 if (view->searching == TOG_SEARCH_FORWARD)
3251 entry = TAILQ_NEXT(s->search_entry, entry);
3252 else
3253 entry = TAILQ_PREV(s->search_entry,
3254 commit_queue_head, entry);
3255 } else if (s->matched_entry) {
3257 * If the user has moved the cursor after we hit a match,
3258 * the position from where we should continue searching
3259 * might have changed.
3261 if (view->searching == TOG_SEARCH_FORWARD)
3262 entry = TAILQ_NEXT(s->selected_entry, entry);
3263 else
3264 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3265 entry);
3266 } else {
3267 entry = s->selected_entry;
3270 while (1) {
3271 int have_match = 0;
3273 if (entry == NULL) {
3274 if (s->thread_args.log_complete ||
3275 view->searching == TOG_SEARCH_BACKWARD) {
3276 view->search_next_done =
3277 (s->matched_entry == NULL ?
3278 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3279 s->search_entry = NULL;
3280 return NULL;
3283 * Poke the log thread for more commits and return,
3284 * allowing the main loop to make progress. Search
3285 * will resume at s->search_entry once we come back.
3287 s->thread_args.commits_needed++;
3288 return trigger_log_thread(view, 0);
3291 err = match_commit(&have_match, entry->id, entry->commit,
3292 &view->regex);
3293 if (err)
3294 break;
3295 if (have_match) {
3296 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3297 s->matched_entry = entry;
3298 break;
3301 s->search_entry = entry;
3302 if (view->searching == TOG_SEARCH_FORWARD)
3303 entry = TAILQ_NEXT(entry, entry);
3304 else
3305 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3308 if (s->matched_entry) {
3309 int cur = s->selected_entry->idx;
3310 while (cur < s->matched_entry->idx) {
3311 err = input_log_view(NULL, view, KEY_DOWN);
3312 if (err)
3313 return err;
3314 cur++;
3316 while (cur > s->matched_entry->idx) {
3317 err = input_log_view(NULL, view, KEY_UP);
3318 if (err)
3319 return err;
3320 cur--;
3324 s->search_entry = NULL;
3326 return NULL;
3329 static const struct got_error *
3330 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3331 struct got_repository *repo, const char *head_ref_name,
3332 const char *in_repo_path, int log_branches)
3334 const struct got_error *err = NULL;
3335 struct tog_log_view_state *s = &view->state.log;
3336 struct got_repository *thread_repo = NULL;
3337 struct got_commit_graph *thread_graph = NULL;
3338 int errcode;
3340 if (in_repo_path != s->in_repo_path) {
3341 free(s->in_repo_path);
3342 s->in_repo_path = strdup(in_repo_path);
3343 if (s->in_repo_path == NULL)
3344 return got_error_from_errno("strdup");
3347 /* The commit queue only contains commits being displayed. */
3348 TAILQ_INIT(&s->real_commits.head);
3349 s->real_commits.ncommits = 0;
3350 s->commits = &s->real_commits;
3352 TAILQ_INIT(&s->limit_commits.head);
3353 s->limit_view = 0;
3354 s->limit_commits.ncommits = 0;
3356 s->repo = repo;
3357 if (head_ref_name) {
3358 s->head_ref_name = strdup(head_ref_name);
3359 if (s->head_ref_name == NULL) {
3360 err = got_error_from_errno("strdup");
3361 goto done;
3364 s->start_id = got_object_id_dup(start_id);
3365 if (s->start_id == NULL) {
3366 err = got_error_from_errno("got_object_id_dup");
3367 goto done;
3369 s->log_branches = log_branches;
3371 STAILQ_INIT(&s->colors);
3372 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3373 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3374 get_color_value("TOG_COLOR_COMMIT"));
3375 if (err)
3376 goto done;
3377 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3378 get_color_value("TOG_COLOR_AUTHOR"));
3379 if (err) {
3380 free_colors(&s->colors);
3381 goto done;
3383 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3384 get_color_value("TOG_COLOR_DATE"));
3385 if (err) {
3386 free_colors(&s->colors);
3387 goto done;
3391 view->show = show_log_view;
3392 view->input = input_log_view;
3393 view->resize = resize_log_view;
3394 view->close = close_log_view;
3395 view->search_start = search_start_log_view;
3396 view->search_next = search_next_log_view;
3398 if (s->thread_args.pack_fds == NULL) {
3399 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3400 if (err)
3401 goto done;
3403 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3404 s->thread_args.pack_fds);
3405 if (err)
3406 goto done;
3407 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3408 !s->log_branches);
3409 if (err)
3410 goto done;
3411 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3412 s->repo, NULL, NULL);
3413 if (err)
3414 goto done;
3416 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3417 if (errcode) {
3418 err = got_error_set_errno(errcode, "pthread_cond_init");
3419 goto done;
3421 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3422 if (errcode) {
3423 err = got_error_set_errno(errcode, "pthread_cond_init");
3424 goto done;
3427 s->thread_args.commits_needed = view->nlines;
3428 s->thread_args.graph = thread_graph;
3429 s->thread_args.real_commits = &s->real_commits;
3430 s->thread_args.limit_commits = &s->limit_commits;
3431 s->thread_args.in_repo_path = s->in_repo_path;
3432 s->thread_args.start_id = s->start_id;
3433 s->thread_args.repo = thread_repo;
3434 s->thread_args.log_complete = 0;
3435 s->thread_args.quit = &s->quit;
3436 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3437 s->thread_args.selected_entry = &s->selected_entry;
3438 s->thread_args.searching = &view->searching;
3439 s->thread_args.search_next_done = &view->search_next_done;
3440 s->thread_args.regex = &view->regex;
3441 s->thread_args.limiting = &s->limit_view;
3442 s->thread_args.limit_regex = &s->limit_regex;
3443 s->thread_args.limit_commits = &s->limit_commits;
3444 done:
3445 if (err)
3446 close_log_view(view);
3447 return err;
3450 static const struct got_error *
3451 show_log_view(struct tog_view *view)
3453 const struct got_error *err;
3454 struct tog_log_view_state *s = &view->state.log;
3456 if (s->thread == 0) { //NULL) {
3457 int errcode = pthread_create(&s->thread, NULL, log_thread,
3458 &s->thread_args);
3459 if (errcode)
3460 return got_error_set_errno(errcode, "pthread_create");
3461 if (s->thread_args.commits_needed > 0) {
3462 err = trigger_log_thread(view, 1);
3463 if (err)
3464 return err;
3468 return draw_commits(view);
3471 static void
3472 log_move_cursor_up(struct tog_view *view, int page, int home)
3474 struct tog_log_view_state *s = &view->state.log;
3476 if (s->first_displayed_entry == NULL)
3477 return;
3478 if (s->selected_entry->idx == 0)
3479 view->count = 0;
3481 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3482 || home)
3483 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3485 if (!page && !home && s->selected > 0)
3486 --s->selected;
3487 else
3488 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3490 select_commit(s);
3491 return;
3494 static const struct got_error *
3495 log_move_cursor_down(struct tog_view *view, int page)
3497 struct tog_log_view_state *s = &view->state.log;
3498 const struct got_error *err = NULL;
3499 int eos = view->nlines - 2;
3501 if (s->first_displayed_entry == NULL)
3502 return NULL;
3504 if (s->thread_args.log_complete &&
3505 s->selected_entry->idx >= s->commits->ncommits - 1)
3506 return NULL;
3508 if (view_is_hsplit_top(view))
3509 --eos; /* border consumes the last line */
3511 if (!page) {
3512 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3513 ++s->selected;
3514 else
3515 err = log_scroll_down(view, 1);
3516 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3517 struct commit_queue_entry *entry;
3518 int n;
3520 s->selected = 0;
3521 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3522 s->last_displayed_entry = entry;
3523 for (n = 0; n <= eos; n++) {
3524 if (entry == NULL)
3525 break;
3526 s->first_displayed_entry = entry;
3527 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3529 if (n > 0)
3530 s->selected = n - 1;
3531 } else {
3532 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3533 s->thread_args.log_complete)
3534 s->selected += MIN(page,
3535 s->commits->ncommits - s->selected_entry->idx - 1);
3536 else
3537 err = log_scroll_down(view, page);
3539 if (err)
3540 return err;
3543 * We might necessarily overshoot in horizontal
3544 * splits; if so, select the last displayed commit.
3546 if (s->first_displayed_entry && s->last_displayed_entry) {
3547 s->selected = MIN(s->selected,
3548 s->last_displayed_entry->idx -
3549 s->first_displayed_entry->idx);
3552 select_commit(s);
3554 if (s->thread_args.log_complete &&
3555 s->selected_entry->idx == s->commits->ncommits - 1)
3556 view->count = 0;
3558 return NULL;
3561 static void
3562 view_get_split(struct tog_view *view, int *y, int *x)
3564 *x = 0;
3565 *y = 0;
3567 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3568 if (view->child && view->child->resized_y)
3569 *y = view->child->resized_y;
3570 else if (view->resized_y)
3571 *y = view->resized_y;
3572 else
3573 *y = view_split_begin_y(view->lines);
3574 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3575 if (view->child && view->child->resized_x)
3576 *x = view->child->resized_x;
3577 else if (view->resized_x)
3578 *x = view->resized_x;
3579 else
3580 *x = view_split_begin_x(view->begin_x);
3584 /* Split view horizontally at y and offset view->state->selected line. */
3585 static const struct got_error *
3586 view_init_hsplit(struct tog_view *view, int y)
3588 const struct got_error *err = NULL;
3590 view->nlines = y;
3591 view->ncols = COLS;
3592 err = view_resize(view);
3593 if (err)
3594 return err;
3596 err = offset_selection_down(view);
3598 return err;
3601 static const struct got_error *
3602 log_goto_line(struct tog_view *view, int nlines)
3604 const struct got_error *err = NULL;
3605 struct tog_log_view_state *s = &view->state.log;
3606 int g, idx = s->selected_entry->idx;
3608 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3609 return NULL;
3611 g = view->gline;
3612 view->gline = 0;
3614 if (g >= s->first_displayed_entry->idx + 1 &&
3615 g <= s->last_displayed_entry->idx + 1 &&
3616 g - s->first_displayed_entry->idx - 1 < nlines) {
3617 s->selected = g - s->first_displayed_entry->idx - 1;
3618 select_commit(s);
3619 return NULL;
3622 if (idx + 1 < g) {
3623 err = log_move_cursor_down(view, g - idx - 1);
3624 if (!err && g > s->selected_entry->idx + 1)
3625 err = log_move_cursor_down(view,
3626 g - s->first_displayed_entry->idx - 1);
3627 if (err)
3628 return err;
3629 } else if (idx + 1 > g)
3630 log_move_cursor_up(view, idx - g + 1, 0);
3632 if (g < nlines && s->first_displayed_entry->idx == 0)
3633 s->selected = g - 1;
3635 select_commit(s);
3636 return NULL;
3640 static const struct got_error *
3641 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3643 const struct got_error *err = NULL;
3644 struct tog_log_view_state *s = &view->state.log;
3645 int eos, nscroll;
3647 if (s->thread_args.load_all) {
3648 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3649 s->thread_args.load_all = 0;
3650 else if (s->thread_args.log_complete) {
3651 err = log_move_cursor_down(view, s->commits->ncommits);
3652 s->thread_args.load_all = 0;
3654 if (err)
3655 return err;
3658 eos = nscroll = view->nlines - 1;
3659 if (view_is_hsplit_top(view))
3660 --eos; /* border */
3662 if (view->gline)
3663 return log_goto_line(view, eos);
3665 switch (ch) {
3666 case '&':
3667 err = limit_log_view(view);
3668 break;
3669 case 'q':
3670 s->quit = 1;
3671 break;
3672 case '0':
3673 view->x = 0;
3674 break;
3675 case '$':
3676 view->x = MAX(view->maxx - view->ncols / 2, 0);
3677 view->count = 0;
3678 break;
3679 case KEY_RIGHT:
3680 case 'l':
3681 if (view->x + view->ncols / 2 < view->maxx)
3682 view->x += 2; /* move two columns right */
3683 else
3684 view->count = 0;
3685 break;
3686 case KEY_LEFT:
3687 case 'h':
3688 view->x -= MIN(view->x, 2); /* move two columns back */
3689 if (view->x <= 0)
3690 view->count = 0;
3691 break;
3692 case 'k':
3693 case KEY_UP:
3694 case '<':
3695 case ',':
3696 case CTRL('p'):
3697 log_move_cursor_up(view, 0, 0);
3698 break;
3699 case 'g':
3700 case KEY_HOME:
3701 log_move_cursor_up(view, 0, 1);
3702 view->count = 0;
3703 break;
3704 case CTRL('u'):
3705 case 'u':
3706 nscroll /= 2;
3707 /* FALL THROUGH */
3708 case KEY_PPAGE:
3709 case CTRL('b'):
3710 case 'b':
3711 log_move_cursor_up(view, nscroll, 0);
3712 break;
3713 case 'j':
3714 case KEY_DOWN:
3715 case '>':
3716 case '.':
3717 case CTRL('n'):
3718 err = log_move_cursor_down(view, 0);
3719 break;
3720 case '@':
3721 s->use_committer = !s->use_committer;
3722 break;
3723 case 'G':
3724 case KEY_END: {
3725 /* We don't know yet how many commits, so we're forced to
3726 * traverse them all. */
3727 view->count = 0;
3728 s->thread_args.load_all = 1;
3729 if (!s->thread_args.log_complete)
3730 return trigger_log_thread(view, 0);
3731 err = log_move_cursor_down(view, s->commits->ncommits);
3732 s->thread_args.load_all = 0;
3733 break;
3735 case CTRL('d'):
3736 case 'd':
3737 nscroll /= 2;
3738 /* FALL THROUGH */
3739 case KEY_NPAGE:
3740 case CTRL('f'):
3741 case 'f':
3742 case ' ':
3743 err = log_move_cursor_down(view, nscroll);
3744 break;
3745 case KEY_RESIZE:
3746 if (s->selected > view->nlines - 2)
3747 s->selected = view->nlines - 2;
3748 if (s->selected > s->commits->ncommits - 1)
3749 s->selected = s->commits->ncommits - 1;
3750 select_commit(s);
3751 if (s->commits->ncommits < view->nlines - 1 &&
3752 !s->thread_args.log_complete) {
3753 s->thread_args.commits_needed += (view->nlines - 1) -
3754 s->commits->ncommits;
3755 err = trigger_log_thread(view, 1);
3757 break;
3758 case KEY_ENTER:
3759 case '\r':
3760 view->count = 0;
3761 if (s->selected_entry == NULL)
3762 break;
3763 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3764 break;
3765 case 'T':
3766 view->count = 0;
3767 if (s->selected_entry == NULL)
3768 break;
3769 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3770 break;
3771 case KEY_BACKSPACE:
3772 case CTRL('l'):
3773 case 'B':
3774 view->count = 0;
3775 if (ch == KEY_BACKSPACE &&
3776 got_path_is_root_dir(s->in_repo_path))
3777 break;
3778 err = stop_log_thread(s);
3779 if (err)
3780 return err;
3781 if (ch == KEY_BACKSPACE) {
3782 char *parent_path;
3783 err = got_path_dirname(&parent_path, s->in_repo_path);
3784 if (err)
3785 return err;
3786 free(s->in_repo_path);
3787 s->in_repo_path = parent_path;
3788 s->thread_args.in_repo_path = s->in_repo_path;
3789 } else if (ch == CTRL('l')) {
3790 struct got_object_id *start_id;
3791 err = got_repo_match_object_id(&start_id, NULL,
3792 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3793 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3794 if (err) {
3795 if (s->head_ref_name == NULL ||
3796 err->code != GOT_ERR_NOT_REF)
3797 return err;
3798 /* Try to cope with deleted references. */
3799 free(s->head_ref_name);
3800 s->head_ref_name = NULL;
3801 err = got_repo_match_object_id(&start_id,
3802 NULL, GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT,
3803 &tog_refs, s->repo);
3804 if (err)
3805 return err;
3807 free(s->start_id);
3808 s->start_id = start_id;
3809 s->thread_args.start_id = s->start_id;
3810 } else /* 'B' */
3811 s->log_branches = !s->log_branches;
3813 if (s->thread_args.pack_fds == NULL) {
3814 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3815 if (err)
3816 return err;
3818 err = got_repo_open(&s->thread_args.repo,
3819 got_repo_get_path(s->repo), NULL,
3820 s->thread_args.pack_fds);
3821 if (err)
3822 return err;
3823 tog_free_refs();
3824 err = tog_load_refs(s->repo, 0);
3825 if (err)
3826 return err;
3827 err = got_commit_graph_open(&s->thread_args.graph,
3828 s->in_repo_path, !s->log_branches);
3829 if (err)
3830 return err;
3831 err = got_commit_graph_iter_start(s->thread_args.graph,
3832 s->start_id, s->repo, NULL, NULL);
3833 if (err)
3834 return err;
3835 free_commits(&s->real_commits);
3836 free_commits(&s->limit_commits);
3837 s->first_displayed_entry = NULL;
3838 s->last_displayed_entry = NULL;
3839 s->selected_entry = NULL;
3840 s->selected = 0;
3841 s->thread_args.log_complete = 0;
3842 s->quit = 0;
3843 s->thread_args.commits_needed = view->lines;
3844 s->matched_entry = NULL;
3845 s->search_entry = NULL;
3846 view->offset = 0;
3847 break;
3848 case 'R':
3849 view->count = 0;
3850 err = view_request_new(new_view, view, TOG_VIEW_REF);
3851 break;
3852 default:
3853 view->count = 0;
3854 break;
3857 return err;
3860 static const struct got_error *
3861 apply_unveil(const char *repo_path, const char *worktree_path)
3863 const struct got_error *error;
3865 #ifdef PROFILE
3866 if (unveil("gmon.out", "rwc") != 0)
3867 return got_error_from_errno2("unveil", "gmon.out");
3868 #endif
3869 if (repo_path && unveil(repo_path, "r") != 0)
3870 return got_error_from_errno2("unveil", repo_path);
3872 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3873 return got_error_from_errno2("unveil", worktree_path);
3875 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3876 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3878 error = got_privsep_unveil_exec_helpers();
3879 if (error != NULL)
3880 return error;
3882 if (unveil(NULL, NULL) != 0)
3883 return got_error_from_errno("unveil");
3885 return NULL;
3888 static void
3889 init_curses(void)
3892 * Override default signal handlers before starting ncurses.
3893 * This should prevent ncurses from installing its own
3894 * broken cleanup() signal handler.
3896 signal(SIGWINCH, tog_sigwinch);
3897 signal(SIGPIPE, tog_sigpipe);
3898 signal(SIGCONT, tog_sigcont);
3899 signal(SIGINT, tog_sigint);
3900 signal(SIGTERM, tog_sigterm);
3902 initscr();
3903 cbreak();
3904 halfdelay(1); /* Do fast refresh while initial view is loading. */
3905 noecho();
3906 nonl();
3907 intrflush(stdscr, FALSE);
3908 keypad(stdscr, TRUE);
3909 curs_set(0);
3910 if (getenv("TOG_COLORS") != NULL) {
3911 start_color();
3912 use_default_colors();
3916 static const struct got_error *
3917 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3918 struct got_repository *repo, struct got_worktree *worktree)
3920 const struct got_error *err = NULL;
3922 if (argc == 0) {
3923 *in_repo_path = strdup("/");
3924 if (*in_repo_path == NULL)
3925 return got_error_from_errno("strdup");
3926 return NULL;
3929 if (worktree) {
3930 const char *prefix = got_worktree_get_path_prefix(worktree);
3931 char *p;
3933 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3934 if (err)
3935 return err;
3936 if (asprintf(in_repo_path, "%s%s%s", prefix,
3937 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3938 p) == -1) {
3939 err = got_error_from_errno("asprintf");
3940 *in_repo_path = NULL;
3942 free(p);
3943 } else
3944 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3946 return err;
3949 static const struct got_error *
3950 cmd_log(int argc, char *argv[])
3952 const struct got_error *error;
3953 struct got_repository *repo = NULL;
3954 struct got_worktree *worktree = NULL;
3955 struct got_object_id *start_id = NULL;
3956 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3957 char *start_commit = NULL, *label = NULL;
3958 struct got_reference *ref = NULL;
3959 const char *head_ref_name = NULL;
3960 int ch, log_branches = 0;
3961 struct tog_view *view;
3962 int *pack_fds = NULL;
3964 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3965 switch (ch) {
3966 case 'b':
3967 log_branches = 1;
3968 break;
3969 case 'c':
3970 start_commit = optarg;
3971 break;
3972 case 'r':
3973 repo_path = realpath(optarg, NULL);
3974 if (repo_path == NULL)
3975 return got_error_from_errno2("realpath",
3976 optarg);
3977 break;
3978 default:
3979 usage_log();
3980 /* NOTREACHED */
3984 argc -= optind;
3985 argv += optind;
3987 if (argc > 1)
3988 usage_log();
3990 error = got_repo_pack_fds_open(&pack_fds);
3991 if (error != NULL)
3992 goto done;
3994 if (repo_path == NULL) {
3995 cwd = getcwd(NULL, 0);
3996 if (cwd == NULL)
3997 return got_error_from_errno("getcwd");
3998 error = got_worktree_open(&worktree, cwd);
3999 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4000 goto done;
4001 if (worktree)
4002 repo_path =
4003 strdup(got_worktree_get_repo_path(worktree));
4004 else
4005 repo_path = strdup(cwd);
4006 if (repo_path == NULL) {
4007 error = got_error_from_errno("strdup");
4008 goto done;
4012 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4013 if (error != NULL)
4014 goto done;
4016 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
4017 repo, worktree);
4018 if (error)
4019 goto done;
4021 init_curses();
4023 error = apply_unveil(got_repo_get_path(repo),
4024 worktree ? got_worktree_get_root_path(worktree) : NULL);
4025 if (error)
4026 goto done;
4028 /* already loaded by tog_log_with_path()? */
4029 if (TAILQ_EMPTY(&tog_refs)) {
4030 error = tog_load_refs(repo, 0);
4031 if (error)
4032 goto done;
4035 if (start_commit == NULL) {
4036 error = got_repo_match_object_id(&start_id, &label,
4037 worktree ? got_worktree_get_head_ref_name(worktree) :
4038 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4039 if (error)
4040 goto done;
4041 head_ref_name = label;
4042 } else {
4043 error = got_ref_open(&ref, repo, start_commit, 0);
4044 if (error == NULL)
4045 head_ref_name = got_ref_get_name(ref);
4046 else if (error->code != GOT_ERR_NOT_REF)
4047 goto done;
4048 error = got_repo_match_object_id(&start_id, NULL,
4049 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4050 if (error)
4051 goto done;
4054 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
4055 if (view == NULL) {
4056 error = got_error_from_errno("view_open");
4057 goto done;
4059 error = open_log_view(view, start_id, repo, head_ref_name,
4060 in_repo_path, log_branches);
4061 if (error)
4062 goto done;
4063 if (worktree) {
4064 /* Release work tree lock. */
4065 got_worktree_close(worktree);
4066 worktree = NULL;
4068 error = view_loop(view);
4069 done:
4070 free(in_repo_path);
4071 free(repo_path);
4072 free(cwd);
4073 free(start_id);
4074 free(label);
4075 if (ref)
4076 got_ref_close(ref);
4077 if (repo) {
4078 const struct got_error *close_err = got_repo_close(repo);
4079 if (error == NULL)
4080 error = close_err;
4082 if (worktree)
4083 got_worktree_close(worktree);
4084 if (pack_fds) {
4085 const struct got_error *pack_err =
4086 got_repo_pack_fds_close(pack_fds);
4087 if (error == NULL)
4088 error = pack_err;
4090 tog_free_refs();
4091 return error;
4094 __dead static void
4095 usage_diff(void)
4097 endwin();
4098 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
4099 "object1 object2\n", getprogname());
4100 exit(1);
4103 static int
4104 match_line(const char *line, regex_t *regex, size_t nmatch,
4105 regmatch_t *regmatch)
4107 return regexec(regex, line, nmatch, regmatch, 0) == 0;
4110 static struct tog_color *
4111 match_color(struct tog_colors *colors, const char *line)
4113 struct tog_color *tc = NULL;
4115 STAILQ_FOREACH(tc, colors, entry) {
4116 if (match_line(line, &tc->regex, 0, NULL))
4117 return tc;
4120 return NULL;
4123 static const struct got_error *
4124 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
4125 WINDOW *window, int skipcol, regmatch_t *regmatch)
4127 const struct got_error *err = NULL;
4128 char *exstr = NULL;
4129 wchar_t *wline = NULL;
4130 int rme, rms, n, width, scrollx;
4131 int width0 = 0, width1 = 0, width2 = 0;
4132 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4134 *wtotal = 0;
4136 rms = regmatch->rm_so;
4137 rme = regmatch->rm_eo;
4139 err = expand_tab(&exstr, line);
4140 if (err)
4141 return err;
4143 /* Split the line into 3 segments, according to match offsets. */
4144 seg0 = strndup(exstr, rms);
4145 if (seg0 == NULL) {
4146 err = got_error_from_errno("strndup");
4147 goto done;
4149 seg1 = strndup(exstr + rms, rme - rms);
4150 if (seg1 == NULL) {
4151 err = got_error_from_errno("strndup");
4152 goto done;
4154 seg2 = strdup(exstr + rme);
4155 if (seg2 == NULL) {
4156 err = got_error_from_errno("strndup");
4157 goto done;
4160 /* draw up to matched token if we haven't scrolled past it */
4161 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4162 col_tab_align, 1);
4163 if (err)
4164 goto done;
4165 n = MAX(width0 - skipcol, 0);
4166 if (n) {
4167 free(wline);
4168 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4169 wlimit, col_tab_align, 1);
4170 if (err)
4171 goto done;
4172 waddwstr(window, &wline[scrollx]);
4173 wlimit -= width;
4174 *wtotal += width;
4177 if (wlimit > 0) {
4178 int i = 0, w = 0;
4179 size_t wlen;
4181 free(wline);
4182 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4183 col_tab_align, 1);
4184 if (err)
4185 goto done;
4186 wlen = wcslen(wline);
4187 while (i < wlen) {
4188 width = wcwidth(wline[i]);
4189 if (width == -1) {
4190 /* should not happen, tabs are expanded */
4191 err = got_error(GOT_ERR_RANGE);
4192 goto done;
4194 if (width0 + w + width > skipcol)
4195 break;
4196 w += width;
4197 i++;
4199 /* draw (visible part of) matched token (if scrolled into it) */
4200 if (width1 - w > 0) {
4201 wattron(window, A_STANDOUT);
4202 waddwstr(window, &wline[i]);
4203 wattroff(window, A_STANDOUT);
4204 wlimit -= (width1 - w);
4205 *wtotal += (width1 - w);
4209 if (wlimit > 0) { /* draw rest of line */
4210 free(wline);
4211 if (skipcol > width0 + width1) {
4212 err = format_line(&wline, &width2, &scrollx, seg2,
4213 skipcol - (width0 + width1), wlimit,
4214 col_tab_align, 1);
4215 if (err)
4216 goto done;
4217 waddwstr(window, &wline[scrollx]);
4218 } else {
4219 err = format_line(&wline, &width2, NULL, seg2, 0,
4220 wlimit, col_tab_align, 1);
4221 if (err)
4222 goto done;
4223 waddwstr(window, wline);
4225 *wtotal += width2;
4227 done:
4228 free(wline);
4229 free(exstr);
4230 free(seg0);
4231 free(seg1);
4232 free(seg2);
4233 return err;
4236 static int
4237 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4239 FILE *f = NULL;
4240 int *eof, *first, *selected;
4242 if (view->type == TOG_VIEW_DIFF) {
4243 struct tog_diff_view_state *s = &view->state.diff;
4245 first = &s->first_displayed_line;
4246 selected = first;
4247 eof = &s->eof;
4248 f = s->f;
4249 } else if (view->type == TOG_VIEW_HELP) {
4250 struct tog_help_view_state *s = &view->state.help;
4252 first = &s->first_displayed_line;
4253 selected = first;
4254 eof = &s->eof;
4255 f = s->f;
4256 } else if (view->type == TOG_VIEW_BLAME) {
4257 struct tog_blame_view_state *s = &view->state.blame;
4259 first = &s->first_displayed_line;
4260 selected = &s->selected_line;
4261 eof = &s->eof;
4262 f = s->blame.f;
4263 } else
4264 return 0;
4266 /* Center gline in the middle of the page like vi(1). */
4267 if (*lineno < view->gline - (view->nlines - 3) / 2)
4268 return 0;
4269 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4270 rewind(f);
4271 *eof = 0;
4272 *first = 1;
4273 *lineno = 0;
4274 *nprinted = 0;
4275 return 0;
4278 *selected = view->gline <= (view->nlines - 3) / 2 ?
4279 view->gline : (view->nlines - 3) / 2 + 1;
4280 view->gline = 0;
4282 return 1;
4285 static const struct got_error *
4286 draw_file(struct tog_view *view, const char *header)
4288 struct tog_diff_view_state *s = &view->state.diff;
4289 regmatch_t *regmatch = &view->regmatch;
4290 const struct got_error *err;
4291 int nprinted = 0;
4292 char *line;
4293 size_t linesize = 0;
4294 ssize_t linelen;
4295 wchar_t *wline;
4296 int width;
4297 int max_lines = view->nlines;
4298 int nlines = s->nlines;
4299 off_t line_offset;
4301 s->lineno = s->first_displayed_line - 1;
4302 line_offset = s->lines[s->first_displayed_line - 1].offset;
4303 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4304 return got_error_from_errno("fseek");
4306 werase(view->window);
4308 if (view->gline > s->nlines - 1)
4309 view->gline = s->nlines - 1;
4311 if (header) {
4312 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4313 1 : view->gline - (view->nlines - 3) / 2 :
4314 s->lineno + s->selected_line;
4316 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4317 return got_error_from_errno("asprintf");
4318 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4319 0, 0);
4320 free(line);
4321 if (err)
4322 return err;
4324 if (view_needs_focus_indication(view))
4325 wstandout(view->window);
4326 waddwstr(view->window, wline);
4327 free(wline);
4328 wline = NULL;
4329 while (width++ < view->ncols)
4330 waddch(view->window, ' ');
4331 if (view_needs_focus_indication(view))
4332 wstandend(view->window);
4334 if (max_lines <= 1)
4335 return NULL;
4336 max_lines--;
4339 s->eof = 0;
4340 view->maxx = 0;
4341 line = NULL;
4342 while (max_lines > 0 && nprinted < max_lines) {
4343 enum got_diff_line_type linetype;
4344 attr_t attr = 0;
4346 linelen = getline(&line, &linesize, s->f);
4347 if (linelen == -1) {
4348 if (feof(s->f)) {
4349 s->eof = 1;
4350 break;
4352 free(line);
4353 return got_ferror(s->f, GOT_ERR_IO);
4356 if (++s->lineno < s->first_displayed_line)
4357 continue;
4358 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4359 continue;
4360 if (s->lineno == view->hiline)
4361 attr = A_STANDOUT;
4363 /* Set view->maxx based on full line length. */
4364 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4365 view->x ? 1 : 0);
4366 if (err) {
4367 free(line);
4368 return err;
4370 view->maxx = MAX(view->maxx, width);
4371 free(wline);
4372 wline = NULL;
4374 linetype = s->lines[s->lineno].type;
4375 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4376 linetype < GOT_DIFF_LINE_CONTEXT)
4377 attr |= COLOR_PAIR(linetype);
4378 if (attr)
4379 wattron(view->window, attr);
4380 if (s->first_displayed_line + nprinted == s->matched_line &&
4381 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4382 err = add_matched_line(&width, line, view->ncols, 0,
4383 view->window, view->x, regmatch);
4384 if (err) {
4385 free(line);
4386 return err;
4388 } else {
4389 int skip;
4390 err = format_line(&wline, &width, &skip, line,
4391 view->x, view->ncols, 0, view->x ? 1 : 0);
4392 if (err) {
4393 free(line);
4394 return err;
4396 waddwstr(view->window, &wline[skip]);
4397 free(wline);
4398 wline = NULL;
4400 if (s->lineno == view->hiline) {
4401 /* highlight full gline length */
4402 while (width++ < view->ncols)
4403 waddch(view->window, ' ');
4404 } else {
4405 if (width <= view->ncols - 1)
4406 waddch(view->window, '\n');
4408 if (attr)
4409 wattroff(view->window, attr);
4410 if (++nprinted == 1)
4411 s->first_displayed_line = s->lineno;
4413 free(line);
4414 if (nprinted >= 1)
4415 s->last_displayed_line = s->first_displayed_line +
4416 (nprinted - 1);
4417 else
4418 s->last_displayed_line = s->first_displayed_line;
4420 view_border(view);
4422 if (s->eof) {
4423 while (nprinted < view->nlines) {
4424 waddch(view->window, '\n');
4425 nprinted++;
4428 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4429 view->ncols, 0, 0);
4430 if (err) {
4431 return err;
4434 wstandout(view->window);
4435 waddwstr(view->window, wline);
4436 free(wline);
4437 wline = NULL;
4438 wstandend(view->window);
4441 return NULL;
4444 static char *
4445 get_datestr(time_t *time, char *datebuf)
4447 struct tm mytm, *tm;
4448 char *p, *s;
4450 tm = gmtime_r(time, &mytm);
4451 if (tm == NULL)
4452 return NULL;
4453 s = asctime_r(tm, datebuf);
4454 if (s == NULL)
4455 return NULL;
4456 p = strchr(s, '\n');
4457 if (p)
4458 *p = '\0';
4459 return s;
4462 static const struct got_error *
4463 get_changed_paths(struct got_pathlist_head *paths,
4464 struct got_commit_object *commit, struct got_repository *repo)
4466 const struct got_error *err = NULL;
4467 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4468 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4469 struct got_object_qid *qid;
4471 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4472 if (qid != NULL) {
4473 struct got_commit_object *pcommit;
4474 err = got_object_open_as_commit(&pcommit, repo,
4475 &qid->id);
4476 if (err)
4477 return err;
4479 tree_id1 = got_object_id_dup(
4480 got_object_commit_get_tree_id(pcommit));
4481 if (tree_id1 == NULL) {
4482 got_object_commit_close(pcommit);
4483 return got_error_from_errno("got_object_id_dup");
4485 got_object_commit_close(pcommit);
4489 if (tree_id1) {
4490 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4491 if (err)
4492 goto done;
4495 tree_id2 = got_object_commit_get_tree_id(commit);
4496 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4497 if (err)
4498 goto done;
4500 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4501 got_diff_tree_collect_changed_paths, paths, 0);
4502 done:
4503 if (tree1)
4504 got_object_tree_close(tree1);
4505 if (tree2)
4506 got_object_tree_close(tree2);
4507 free(tree_id1);
4508 return err;
4511 static const struct got_error *
4512 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4513 off_t off, uint8_t type)
4515 struct got_diff_line *p;
4517 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4518 if (p == NULL)
4519 return got_error_from_errno("reallocarray");
4520 *lines = p;
4521 (*lines)[*nlines].offset = off;
4522 (*lines)[*nlines].type = type;
4523 (*nlines)++;
4525 return NULL;
4528 static const struct got_error *
4529 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4530 struct got_object_id *commit_id, struct got_reflist_head *refs,
4531 struct got_repository *repo, FILE *outfile)
4533 const struct got_error *err = NULL;
4534 char datebuf[26], *datestr;
4535 struct got_commit_object *commit;
4536 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4537 time_t committer_time;
4538 const char *author, *committer;
4539 char *refs_str = NULL;
4540 struct got_pathlist_head changed_paths;
4541 struct got_pathlist_entry *pe;
4542 off_t outoff = 0;
4543 int n;
4545 TAILQ_INIT(&changed_paths);
4547 if (refs) {
4548 err = build_refs_str(&refs_str, refs, commit_id, repo);
4549 if (err)
4550 return err;
4553 err = got_object_open_as_commit(&commit, repo, commit_id);
4554 if (err)
4555 return err;
4557 err = got_object_id_str(&id_str, commit_id);
4558 if (err) {
4559 err = got_error_from_errno("got_object_id_str");
4560 goto done;
4563 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4564 if (err)
4565 goto done;
4567 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4568 refs_str ? refs_str : "", refs_str ? ")" : "");
4569 if (n < 0) {
4570 err = got_error_from_errno("fprintf");
4571 goto done;
4573 outoff += n;
4574 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4575 if (err)
4576 goto done;
4578 n = fprintf(outfile, "from: %s\n",
4579 got_object_commit_get_author(commit));
4580 if (n < 0) {
4581 err = got_error_from_errno("fprintf");
4582 goto done;
4584 outoff += n;
4585 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4586 if (err)
4587 goto done;
4589 committer_time = got_object_commit_get_committer_time(commit);
4590 datestr = get_datestr(&committer_time, datebuf);
4591 if (datestr) {
4592 n = fprintf(outfile, "date: %s UTC\n", datestr);
4593 if (n < 0) {
4594 err = got_error_from_errno("fprintf");
4595 goto done;
4597 outoff += n;
4598 err = add_line_metadata(lines, nlines, outoff,
4599 GOT_DIFF_LINE_DATE);
4600 if (err)
4601 goto done;
4603 author = got_object_commit_get_author(commit);
4604 committer = got_object_commit_get_committer(commit);
4605 if (strcmp(author, committer) != 0) {
4606 n = fprintf(outfile, "via: %s\n", committer);
4607 if (n < 0) {
4608 err = got_error_from_errno("fprintf");
4609 goto done;
4611 outoff += n;
4612 err = add_line_metadata(lines, nlines, outoff,
4613 GOT_DIFF_LINE_AUTHOR);
4614 if (err)
4615 goto done;
4617 if (got_object_commit_get_nparents(commit) > 1) {
4618 const struct got_object_id_queue *parent_ids;
4619 struct got_object_qid *qid;
4620 int pn = 1;
4621 parent_ids = got_object_commit_get_parent_ids(commit);
4622 STAILQ_FOREACH(qid, parent_ids, entry) {
4623 err = got_object_id_str(&id_str, &qid->id);
4624 if (err)
4625 goto done;
4626 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4627 if (n < 0) {
4628 err = got_error_from_errno("fprintf");
4629 goto done;
4631 outoff += n;
4632 err = add_line_metadata(lines, nlines, outoff,
4633 GOT_DIFF_LINE_META);
4634 if (err)
4635 goto done;
4636 free(id_str);
4637 id_str = NULL;
4641 err = got_object_commit_get_logmsg(&logmsg, commit);
4642 if (err)
4643 goto done;
4644 s = logmsg;
4645 while ((line = strsep(&s, "\n")) != NULL) {
4646 n = fprintf(outfile, "%s\n", line);
4647 if (n < 0) {
4648 err = got_error_from_errno("fprintf");
4649 goto done;
4651 outoff += n;
4652 err = add_line_metadata(lines, nlines, outoff,
4653 GOT_DIFF_LINE_LOGMSG);
4654 if (err)
4655 goto done;
4658 err = get_changed_paths(&changed_paths, commit, repo);
4659 if (err)
4660 goto done;
4661 TAILQ_FOREACH(pe, &changed_paths, entry) {
4662 struct got_diff_changed_path *cp = pe->data;
4663 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4664 if (n < 0) {
4665 err = got_error_from_errno("fprintf");
4666 goto done;
4668 outoff += n;
4669 err = add_line_metadata(lines, nlines, outoff,
4670 GOT_DIFF_LINE_CHANGES);
4671 if (err)
4672 goto done;
4673 free((char *)pe->path);
4674 free(pe->data);
4677 fputc('\n', outfile);
4678 outoff++;
4679 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4680 done:
4681 got_pathlist_free(&changed_paths);
4682 free(id_str);
4683 free(logmsg);
4684 free(refs_str);
4685 got_object_commit_close(commit);
4686 if (err) {
4687 free(*lines);
4688 *lines = NULL;
4689 *nlines = 0;
4691 return err;
4694 static const struct got_error *
4695 create_diff(struct tog_diff_view_state *s)
4697 const struct got_error *err = NULL;
4698 FILE *f = NULL;
4699 int obj_type;
4701 free(s->lines);
4702 s->lines = malloc(sizeof(*s->lines));
4703 if (s->lines == NULL)
4704 return got_error_from_errno("malloc");
4705 s->nlines = 0;
4707 f = got_opentemp();
4708 if (f == NULL) {
4709 err = got_error_from_errno("got_opentemp");
4710 goto done;
4712 if (s->f && fclose(s->f) == EOF) {
4713 err = got_error_from_errno("fclose");
4714 goto done;
4716 s->f = f;
4718 if (s->id1)
4719 err = got_object_get_type(&obj_type, s->repo, s->id1);
4720 else
4721 err = got_object_get_type(&obj_type, s->repo, s->id2);
4722 if (err)
4723 goto done;
4725 switch (obj_type) {
4726 case GOT_OBJ_TYPE_BLOB:
4727 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4728 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4729 s->label1, s->label2, tog_diff_algo, s->diff_context,
4730 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4731 break;
4732 case GOT_OBJ_TYPE_TREE:
4733 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4734 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4735 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4736 s->force_text_diff, s->repo, s->f);
4737 break;
4738 case GOT_OBJ_TYPE_COMMIT: {
4739 const struct got_object_id_queue *parent_ids;
4740 struct got_object_qid *pid;
4741 struct got_commit_object *commit2;
4742 struct got_reflist_head *refs;
4744 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4745 if (err)
4746 goto done;
4747 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4748 /* Show commit info if we're diffing to a parent/root commit. */
4749 if (s->id1 == NULL) {
4750 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4751 refs, s->repo, s->f);
4752 if (err)
4753 goto done;
4754 } else {
4755 parent_ids = got_object_commit_get_parent_ids(commit2);
4756 STAILQ_FOREACH(pid, parent_ids, entry) {
4757 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4758 err = write_commit_info(&s->lines,
4759 &s->nlines, s->id2, refs, s->repo,
4760 s->f);
4761 if (err)
4762 goto done;
4763 break;
4767 got_object_commit_close(commit2);
4769 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4770 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4771 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4772 s->force_text_diff, s->repo, s->f);
4773 break;
4775 default:
4776 err = got_error(GOT_ERR_OBJ_TYPE);
4777 break;
4779 done:
4780 if (s->f && fflush(s->f) != 0 && err == NULL)
4781 err = got_error_from_errno("fflush");
4782 return err;
4785 static void
4786 diff_view_indicate_progress(struct tog_view *view)
4788 mvwaddstr(view->window, 0, 0, "diffing...");
4789 update_panels();
4790 doupdate();
4793 static const struct got_error *
4794 search_start_diff_view(struct tog_view *view)
4796 struct tog_diff_view_state *s = &view->state.diff;
4798 s->matched_line = 0;
4799 return NULL;
4802 static void
4803 search_setup_diff_view(struct tog_view *view, FILE **f, off_t **line_offsets,
4804 size_t *nlines, int **first, int **last, int **match, int **selected)
4806 struct tog_diff_view_state *s = &view->state.diff;
4808 *f = s->f;
4809 *nlines = s->nlines;
4810 *line_offsets = NULL;
4811 *match = &s->matched_line;
4812 *first = &s->first_displayed_line;
4813 *last = &s->last_displayed_line;
4814 *selected = &s->selected_line;
4817 static const struct got_error *
4818 search_next_view_match(struct tog_view *view)
4820 const struct got_error *err = NULL;
4821 FILE *f;
4822 int lineno;
4823 char *line = NULL;
4824 size_t linesize = 0;
4825 ssize_t linelen;
4826 off_t *line_offsets;
4827 size_t nlines = 0;
4828 int *first, *last, *match, *selected;
4830 if (!view->search_setup)
4831 return got_error_msg(GOT_ERR_NOT_IMPL,
4832 "view search not supported");
4833 view->search_setup(view, &f, &line_offsets, &nlines, &first, &last,
4834 &match, &selected);
4836 if (!view->searching) {
4837 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4838 return NULL;
4841 if (*match) {
4842 if (view->searching == TOG_SEARCH_FORWARD)
4843 lineno = *match + 1;
4844 else
4845 lineno = *match - 1;
4846 } else
4847 lineno = *first - 1 + *selected;
4849 while (1) {
4850 off_t offset;
4852 if (lineno <= 0 || lineno > nlines) {
4853 if (*match == 0) {
4854 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4855 break;
4858 if (view->searching == TOG_SEARCH_FORWARD)
4859 lineno = 1;
4860 else
4861 lineno = nlines;
4864 offset = view->type == TOG_VIEW_DIFF ?
4865 view->state.diff.lines[lineno - 1].offset :
4866 line_offsets[lineno - 1];
4867 if (fseeko(f, offset, SEEK_SET) != 0) {
4868 free(line);
4869 return got_error_from_errno("fseeko");
4871 linelen = getline(&line, &linesize, f);
4872 if (linelen != -1) {
4873 char *exstr;
4874 err = expand_tab(&exstr, line);
4875 if (err)
4876 break;
4877 if (match_line(exstr, &view->regex, 1,
4878 &view->regmatch)) {
4879 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4880 *match = lineno;
4881 free(exstr);
4882 break;
4884 free(exstr);
4886 if (view->searching == TOG_SEARCH_FORWARD)
4887 lineno++;
4888 else
4889 lineno--;
4891 free(line);
4893 if (*match) {
4894 *first = *match;
4895 *selected = 1;
4898 return err;
4901 static const struct got_error *
4902 close_diff_view(struct tog_view *view)
4904 const struct got_error *err = NULL;
4905 struct tog_diff_view_state *s = &view->state.diff;
4907 free(s->id1);
4908 s->id1 = NULL;
4909 free(s->id2);
4910 s->id2 = NULL;
4911 if (s->f && fclose(s->f) == EOF)
4912 err = got_error_from_errno("fclose");
4913 s->f = NULL;
4914 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4915 err = got_error_from_errno("fclose");
4916 s->f1 = NULL;
4917 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4918 err = got_error_from_errno("fclose");
4919 s->f2 = NULL;
4920 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4921 err = got_error_from_errno("close");
4922 s->fd1 = -1;
4923 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4924 err = got_error_from_errno("close");
4925 s->fd2 = -1;
4926 free(s->lines);
4927 s->lines = NULL;
4928 s->nlines = 0;
4929 return err;
4932 static const struct got_error *
4933 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4934 struct got_object_id *id2, const char *label1, const char *label2,
4935 int diff_context, int ignore_whitespace, int force_text_diff,
4936 struct tog_view *parent_view, struct got_repository *repo)
4938 const struct got_error *err;
4939 struct tog_diff_view_state *s = &view->state.diff;
4941 memset(s, 0, sizeof(*s));
4942 s->fd1 = -1;
4943 s->fd2 = -1;
4945 if (id1 != NULL && id2 != NULL) {
4946 int type1, type2;
4947 err = got_object_get_type(&type1, repo, id1);
4948 if (err)
4949 return err;
4950 err = got_object_get_type(&type2, repo, id2);
4951 if (err)
4952 return err;
4954 if (type1 != type2)
4955 return got_error(GOT_ERR_OBJ_TYPE);
4957 s->first_displayed_line = 1;
4958 s->last_displayed_line = view->nlines;
4959 s->selected_line = 1;
4960 s->repo = repo;
4961 s->id1 = id1;
4962 s->id2 = id2;
4963 s->label1 = label1;
4964 s->label2 = label2;
4966 if (id1) {
4967 s->id1 = got_object_id_dup(id1);
4968 if (s->id1 == NULL)
4969 return got_error_from_errno("got_object_id_dup");
4970 } else
4971 s->id1 = NULL;
4973 s->id2 = got_object_id_dup(id2);
4974 if (s->id2 == NULL) {
4975 err = got_error_from_errno("got_object_id_dup");
4976 goto done;
4979 s->f1 = got_opentemp();
4980 if (s->f1 == NULL) {
4981 err = got_error_from_errno("got_opentemp");
4982 goto done;
4985 s->f2 = got_opentemp();
4986 if (s->f2 == NULL) {
4987 err = got_error_from_errno("got_opentemp");
4988 goto done;
4991 s->fd1 = got_opentempfd();
4992 if (s->fd1 == -1) {
4993 err = got_error_from_errno("got_opentempfd");
4994 goto done;
4997 s->fd2 = got_opentempfd();
4998 if (s->fd2 == -1) {
4999 err = got_error_from_errno("got_opentempfd");
5000 goto done;
5003 s->first_displayed_line = 1;
5004 s->last_displayed_line = view->nlines;
5005 s->diff_context = diff_context;
5006 s->ignore_whitespace = ignore_whitespace;
5007 s->force_text_diff = force_text_diff;
5008 s->parent_view = parent_view;
5009 s->repo = repo;
5011 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5012 int rc;
5014 rc = init_pair(GOT_DIFF_LINE_MINUS,
5015 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
5016 if (rc != ERR)
5017 rc = init_pair(GOT_DIFF_LINE_PLUS,
5018 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
5019 if (rc != ERR)
5020 rc = init_pair(GOT_DIFF_LINE_HUNK,
5021 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
5022 if (rc != ERR)
5023 rc = init_pair(GOT_DIFF_LINE_META,
5024 get_color_value("TOG_COLOR_DIFF_META"), -1);
5025 if (rc != ERR)
5026 rc = init_pair(GOT_DIFF_LINE_CHANGES,
5027 get_color_value("TOG_COLOR_DIFF_META"), -1);
5028 if (rc != ERR)
5029 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
5030 get_color_value("TOG_COLOR_DIFF_META"), -1);
5031 if (rc != ERR)
5032 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
5033 get_color_value("TOG_COLOR_DIFF_META"), -1);
5034 if (rc != ERR)
5035 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
5036 get_color_value("TOG_COLOR_AUTHOR"), -1);
5037 if (rc != ERR)
5038 rc = init_pair(GOT_DIFF_LINE_DATE,
5039 get_color_value("TOG_COLOR_DATE"), -1);
5040 if (rc == ERR) {
5041 err = got_error(GOT_ERR_RANGE);
5042 goto done;
5046 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
5047 view_is_splitscreen(view))
5048 show_log_view(parent_view); /* draw border */
5049 diff_view_indicate_progress(view);
5051 err = create_diff(s);
5053 view->show = show_diff_view;
5054 view->input = input_diff_view;
5055 view->reset = reset_diff_view;
5056 view->close = close_diff_view;
5057 view->search_start = search_start_diff_view;
5058 view->search_setup = search_setup_diff_view;
5059 view->search_next = search_next_view_match;
5060 done:
5061 if (err)
5062 close_diff_view(view);
5063 return err;
5066 static const struct got_error *
5067 show_diff_view(struct tog_view *view)
5069 const struct got_error *err;
5070 struct tog_diff_view_state *s = &view->state.diff;
5071 char *id_str1 = NULL, *id_str2, *header;
5072 const char *label1, *label2;
5074 if (s->id1) {
5075 err = got_object_id_str(&id_str1, s->id1);
5076 if (err)
5077 return err;
5078 label1 = s->label1 ? s->label1 : id_str1;
5079 } else
5080 label1 = "/dev/null";
5082 err = got_object_id_str(&id_str2, s->id2);
5083 if (err)
5084 return err;
5085 label2 = s->label2 ? s->label2 : id_str2;
5087 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
5088 err = got_error_from_errno("asprintf");
5089 free(id_str1);
5090 free(id_str2);
5091 return err;
5093 free(id_str1);
5094 free(id_str2);
5096 err = draw_file(view, header);
5097 free(header);
5098 return err;
5101 static const struct got_error *
5102 set_selected_commit(struct tog_diff_view_state *s,
5103 struct commit_queue_entry *entry)
5105 const struct got_error *err;
5106 const struct got_object_id_queue *parent_ids;
5107 struct got_commit_object *selected_commit;
5108 struct got_object_qid *pid;
5110 free(s->id2);
5111 s->id2 = got_object_id_dup(entry->id);
5112 if (s->id2 == NULL)
5113 return got_error_from_errno("got_object_id_dup");
5115 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
5116 if (err)
5117 return err;
5118 parent_ids = got_object_commit_get_parent_ids(selected_commit);
5119 free(s->id1);
5120 pid = STAILQ_FIRST(parent_ids);
5121 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
5122 got_object_commit_close(selected_commit);
5123 return NULL;
5126 static const struct got_error *
5127 reset_diff_view(struct tog_view *view)
5129 struct tog_diff_view_state *s = &view->state.diff;
5131 view->count = 0;
5132 wclear(view->window);
5133 s->first_displayed_line = 1;
5134 s->last_displayed_line = view->nlines;
5135 s->matched_line = 0;
5136 diff_view_indicate_progress(view);
5137 return create_diff(s);
5140 static void
5141 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5143 int start, i;
5145 i = start = s->first_displayed_line - 1;
5147 while (s->lines[i].type != type) {
5148 if (i == 0)
5149 i = s->nlines - 1;
5150 if (--i == start)
5151 return; /* do nothing, requested type not in file */
5154 s->selected_line = 1;
5155 s->first_displayed_line = i;
5158 static void
5159 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
5161 int start, i;
5163 i = start = s->first_displayed_line + 1;
5165 while (s->lines[i].type != type) {
5166 if (i == s->nlines - 1)
5167 i = 0;
5168 if (++i == start)
5169 return; /* do nothing, requested type not in file */
5172 s->selected_line = 1;
5173 s->first_displayed_line = i;
5176 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5177 int, int, int);
5178 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5179 int, int);
5181 static const struct got_error *
5182 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5184 const struct got_error *err = NULL;
5185 struct tog_diff_view_state *s = &view->state.diff;
5186 struct tog_log_view_state *ls;
5187 struct commit_queue_entry *old_selected_entry;
5188 char *line = NULL;
5189 size_t linesize = 0;
5190 ssize_t linelen;
5191 int i, nscroll = view->nlines - 1, up = 0;
5193 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5195 switch (ch) {
5196 case '0':
5197 view->x = 0;
5198 break;
5199 case '$':
5200 view->x = MAX(view->maxx - view->ncols / 3, 0);
5201 view->count = 0;
5202 break;
5203 case KEY_RIGHT:
5204 case 'l':
5205 if (view->x + view->ncols / 3 < view->maxx)
5206 view->x += 2; /* move two columns right */
5207 else
5208 view->count = 0;
5209 break;
5210 case KEY_LEFT:
5211 case 'h':
5212 view->x -= MIN(view->x, 2); /* move two columns back */
5213 if (view->x <= 0)
5214 view->count = 0;
5215 break;
5216 case 'a':
5217 case 'w':
5218 if (ch == 'a')
5219 s->force_text_diff = !s->force_text_diff;
5220 if (ch == 'w')
5221 s->ignore_whitespace = !s->ignore_whitespace;
5222 err = reset_diff_view(view);
5223 break;
5224 case 'g':
5225 case KEY_HOME:
5226 s->first_displayed_line = 1;
5227 view->count = 0;
5228 break;
5229 case 'G':
5230 case KEY_END:
5231 view->count = 0;
5232 if (s->eof)
5233 break;
5235 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5236 s->eof = 1;
5237 break;
5238 case 'k':
5239 case KEY_UP:
5240 case CTRL('p'):
5241 if (s->first_displayed_line > 1)
5242 s->first_displayed_line--;
5243 else
5244 view->count = 0;
5245 break;
5246 case CTRL('u'):
5247 case 'u':
5248 nscroll /= 2;
5249 /* FALL THROUGH */
5250 case KEY_PPAGE:
5251 case CTRL('b'):
5252 case 'b':
5253 if (s->first_displayed_line == 1) {
5254 view->count = 0;
5255 break;
5257 i = 0;
5258 while (i++ < nscroll && s->first_displayed_line > 1)
5259 s->first_displayed_line--;
5260 break;
5261 case 'j':
5262 case KEY_DOWN:
5263 case CTRL('n'):
5264 if (!s->eof)
5265 s->first_displayed_line++;
5266 else
5267 view->count = 0;
5268 break;
5269 case CTRL('d'):
5270 case 'd':
5271 nscroll /= 2;
5272 /* FALL THROUGH */
5273 case KEY_NPAGE:
5274 case CTRL('f'):
5275 case 'f':
5276 case ' ':
5277 if (s->eof) {
5278 view->count = 0;
5279 break;
5281 i = 0;
5282 while (!s->eof && i++ < nscroll) {
5283 linelen = getline(&line, &linesize, s->f);
5284 s->first_displayed_line++;
5285 if (linelen == -1) {
5286 if (feof(s->f)) {
5287 s->eof = 1;
5288 } else
5289 err = got_ferror(s->f, GOT_ERR_IO);
5290 break;
5293 free(line);
5294 break;
5295 case '(':
5296 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5297 break;
5298 case ')':
5299 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5300 break;
5301 case '{':
5302 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5303 break;
5304 case '}':
5305 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5306 break;
5307 case '[':
5308 if (s->diff_context > 0) {
5309 s->diff_context--;
5310 s->matched_line = 0;
5311 diff_view_indicate_progress(view);
5312 err = create_diff(s);
5313 if (s->first_displayed_line + view->nlines - 1 >
5314 s->nlines) {
5315 s->first_displayed_line = 1;
5316 s->last_displayed_line = view->nlines;
5318 } else
5319 view->count = 0;
5320 break;
5321 case ']':
5322 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5323 s->diff_context++;
5324 s->matched_line = 0;
5325 diff_view_indicate_progress(view);
5326 err = create_diff(s);
5327 } else
5328 view->count = 0;
5329 break;
5330 case '<':
5331 case ',':
5332 case 'K':
5333 up = 1;
5334 /* FALL THROUGH */
5335 case '>':
5336 case '.':
5337 case 'J':
5338 if (s->parent_view == NULL) {
5339 view->count = 0;
5340 break;
5342 s->parent_view->count = view->count;
5344 if (s->parent_view->type == TOG_VIEW_LOG) {
5345 ls = &s->parent_view->state.log;
5346 old_selected_entry = ls->selected_entry;
5348 err = input_log_view(NULL, s->parent_view,
5349 up ? KEY_UP : KEY_DOWN);
5350 if (err)
5351 break;
5352 view->count = s->parent_view->count;
5354 if (old_selected_entry == ls->selected_entry)
5355 break;
5357 err = set_selected_commit(s, ls->selected_entry);
5358 if (err)
5359 break;
5360 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5361 struct tog_blame_view_state *bs;
5362 struct got_object_id *id, *prev_id;
5364 bs = &s->parent_view->state.blame;
5365 prev_id = get_annotation_for_line(bs->blame.lines,
5366 bs->blame.nlines, bs->last_diffed_line);
5368 err = input_blame_view(&view, s->parent_view,
5369 up ? KEY_UP : KEY_DOWN);
5370 if (err)
5371 break;
5372 view->count = s->parent_view->count;
5374 if (prev_id == NULL)
5375 break;
5376 id = get_selected_commit_id(bs->blame.lines,
5377 bs->blame.nlines, bs->first_displayed_line,
5378 bs->selected_line);
5379 if (id == NULL)
5380 break;
5382 if (!got_object_id_cmp(prev_id, id))
5383 break;
5385 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5386 if (err)
5387 break;
5389 s->first_displayed_line = 1;
5390 s->last_displayed_line = view->nlines;
5391 s->matched_line = 0;
5392 view->x = 0;
5394 diff_view_indicate_progress(view);
5395 err = create_diff(s);
5396 break;
5397 default:
5398 view->count = 0;
5399 break;
5402 return err;
5405 static const struct got_error *
5406 cmd_diff(int argc, char *argv[])
5408 const struct got_error *error = NULL;
5409 struct got_repository *repo = NULL;
5410 struct got_worktree *worktree = NULL;
5411 struct got_object_id *id1 = NULL, *id2 = NULL;
5412 char *repo_path = NULL, *cwd = NULL;
5413 char *id_str1 = NULL, *id_str2 = NULL;
5414 char *label1 = NULL, *label2 = NULL;
5415 int diff_context = 3, ignore_whitespace = 0;
5416 int ch, force_text_diff = 0;
5417 const char *errstr;
5418 struct tog_view *view;
5419 int *pack_fds = NULL;
5421 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5422 switch (ch) {
5423 case 'a':
5424 force_text_diff = 1;
5425 break;
5426 case 'C':
5427 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5428 &errstr);
5429 if (errstr != NULL)
5430 errx(1, "number of context lines is %s: %s",
5431 errstr, errstr);
5432 break;
5433 case 'r':
5434 repo_path = realpath(optarg, NULL);
5435 if (repo_path == NULL)
5436 return got_error_from_errno2("realpath",
5437 optarg);
5438 got_path_strip_trailing_slashes(repo_path);
5439 break;
5440 case 'w':
5441 ignore_whitespace = 1;
5442 break;
5443 default:
5444 usage_diff();
5445 /* NOTREACHED */
5449 argc -= optind;
5450 argv += optind;
5452 if (argc == 0) {
5453 usage_diff(); /* TODO show local worktree changes */
5454 } else if (argc == 2) {
5455 id_str1 = argv[0];
5456 id_str2 = argv[1];
5457 } else
5458 usage_diff();
5460 error = got_repo_pack_fds_open(&pack_fds);
5461 if (error)
5462 goto done;
5464 if (repo_path == NULL) {
5465 cwd = getcwd(NULL, 0);
5466 if (cwd == NULL)
5467 return got_error_from_errno("getcwd");
5468 error = got_worktree_open(&worktree, cwd);
5469 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5470 goto done;
5471 if (worktree)
5472 repo_path =
5473 strdup(got_worktree_get_repo_path(worktree));
5474 else
5475 repo_path = strdup(cwd);
5476 if (repo_path == NULL) {
5477 error = got_error_from_errno("strdup");
5478 goto done;
5482 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5483 if (error)
5484 goto done;
5486 init_curses();
5488 error = apply_unveil(got_repo_get_path(repo), NULL);
5489 if (error)
5490 goto done;
5492 error = tog_load_refs(repo, 0);
5493 if (error)
5494 goto done;
5496 error = got_repo_match_object_id(&id1, &label1, id_str1,
5497 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5498 if (error)
5499 goto done;
5501 error = got_repo_match_object_id(&id2, &label2, id_str2,
5502 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5503 if (error)
5504 goto done;
5506 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5507 if (view == NULL) {
5508 error = got_error_from_errno("view_open");
5509 goto done;
5511 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5512 ignore_whitespace, force_text_diff, NULL, repo);
5513 if (error)
5514 goto done;
5515 error = view_loop(view);
5516 done:
5517 free(label1);
5518 free(label2);
5519 free(repo_path);
5520 free(cwd);
5521 if (repo) {
5522 const struct got_error *close_err = got_repo_close(repo);
5523 if (error == NULL)
5524 error = close_err;
5526 if (worktree)
5527 got_worktree_close(worktree);
5528 if (pack_fds) {
5529 const struct got_error *pack_err =
5530 got_repo_pack_fds_close(pack_fds);
5531 if (error == NULL)
5532 error = pack_err;
5534 tog_free_refs();
5535 return error;
5538 __dead static void
5539 usage_blame(void)
5541 endwin();
5542 fprintf(stderr,
5543 "usage: %s blame [-c commit] [-r repository-path] path\n",
5544 getprogname());
5545 exit(1);
5548 struct tog_blame_line {
5549 int annotated;
5550 struct got_object_id *id;
5553 static const struct got_error *
5554 draw_blame(struct tog_view *view)
5556 struct tog_blame_view_state *s = &view->state.blame;
5557 struct tog_blame *blame = &s->blame;
5558 regmatch_t *regmatch = &view->regmatch;
5559 const struct got_error *err;
5560 int lineno = 0, nprinted = 0;
5561 char *line = NULL;
5562 size_t linesize = 0;
5563 ssize_t linelen;
5564 wchar_t *wline;
5565 int width;
5566 struct tog_blame_line *blame_line;
5567 struct got_object_id *prev_id = NULL;
5568 char *id_str;
5569 struct tog_color *tc;
5571 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5572 if (err)
5573 return err;
5575 rewind(blame->f);
5576 werase(view->window);
5578 if (asprintf(&line, "commit %s", id_str) == -1) {
5579 err = got_error_from_errno("asprintf");
5580 free(id_str);
5581 return err;
5584 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5585 free(line);
5586 line = NULL;
5587 if (err)
5588 return err;
5589 if (view_needs_focus_indication(view))
5590 wstandout(view->window);
5591 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5592 if (tc)
5593 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5594 waddwstr(view->window, wline);
5595 while (width++ < view->ncols)
5596 waddch(view->window, ' ');
5597 if (tc)
5598 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5599 if (view_needs_focus_indication(view))
5600 wstandend(view->window);
5601 free(wline);
5602 wline = NULL;
5604 if (view->gline > blame->nlines)
5605 view->gline = blame->nlines;
5607 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5608 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5609 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5610 free(id_str);
5611 return got_error_from_errno("asprintf");
5613 free(id_str);
5614 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5615 free(line);
5616 line = NULL;
5617 if (err)
5618 return err;
5619 waddwstr(view->window, wline);
5620 free(wline);
5621 wline = NULL;
5622 if (width < view->ncols - 1)
5623 waddch(view->window, '\n');
5625 s->eof = 0;
5626 view->maxx = 0;
5627 while (nprinted < view->nlines - 2) {
5628 linelen = getline(&line, &linesize, blame->f);
5629 if (linelen == -1) {
5630 if (feof(blame->f)) {
5631 s->eof = 1;
5632 break;
5634 free(line);
5635 return got_ferror(blame->f, GOT_ERR_IO);
5637 if (++lineno < s->first_displayed_line)
5638 continue;
5639 if (view->gline && !gotoline(view, &lineno, &nprinted))
5640 continue;
5642 /* Set view->maxx based on full line length. */
5643 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5644 if (err) {
5645 free(line);
5646 return err;
5648 free(wline);
5649 wline = NULL;
5650 view->maxx = MAX(view->maxx, width);
5652 if (nprinted == s->selected_line - 1)
5653 wstandout(view->window);
5655 if (blame->nlines > 0) {
5656 blame_line = &blame->lines[lineno - 1];
5657 if (blame_line->annotated && prev_id &&
5658 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5659 !(nprinted == s->selected_line - 1)) {
5660 waddstr(view->window, " ");
5661 } else if (blame_line->annotated) {
5662 char *id_str;
5663 err = got_object_id_str(&id_str,
5664 blame_line->id);
5665 if (err) {
5666 free(line);
5667 return err;
5669 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5670 if (tc)
5671 wattr_on(view->window,
5672 COLOR_PAIR(tc->colorpair), NULL);
5673 wprintw(view->window, "%.8s", id_str);
5674 if (tc)
5675 wattr_off(view->window,
5676 COLOR_PAIR(tc->colorpair), NULL);
5677 free(id_str);
5678 prev_id = blame_line->id;
5679 } else {
5680 waddstr(view->window, "........");
5681 prev_id = NULL;
5683 } else {
5684 waddstr(view->window, "........");
5685 prev_id = NULL;
5688 if (nprinted == s->selected_line - 1)
5689 wstandend(view->window);
5690 waddstr(view->window, " ");
5692 if (view->ncols <= 9) {
5693 width = 9;
5694 } else if (s->first_displayed_line + nprinted ==
5695 s->matched_line &&
5696 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5697 err = add_matched_line(&width, line, view->ncols - 9, 9,
5698 view->window, view->x, regmatch);
5699 if (err) {
5700 free(line);
5701 return err;
5703 width += 9;
5704 } else {
5705 int skip;
5706 err = format_line(&wline, &width, &skip, line,
5707 view->x, view->ncols - 9, 9, 1);
5708 if (err) {
5709 free(line);
5710 return err;
5712 waddwstr(view->window, &wline[skip]);
5713 width += 9;
5714 free(wline);
5715 wline = NULL;
5718 if (width <= view->ncols - 1)
5719 waddch(view->window, '\n');
5720 if (++nprinted == 1)
5721 s->first_displayed_line = lineno;
5723 free(line);
5724 s->last_displayed_line = lineno;
5726 view_border(view);
5728 return NULL;
5731 static const struct got_error *
5732 blame_cb(void *arg, int nlines, int lineno,
5733 struct got_commit_object *commit, struct got_object_id *id)
5735 const struct got_error *err = NULL;
5736 struct tog_blame_cb_args *a = arg;
5737 struct tog_blame_line *line;
5738 int errcode;
5740 if (nlines != a->nlines ||
5741 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5742 return got_error(GOT_ERR_RANGE);
5744 errcode = pthread_mutex_lock(&tog_mutex);
5745 if (errcode)
5746 return got_error_set_errno(errcode, "pthread_mutex_lock");
5748 if (*a->quit) { /* user has quit the blame view */
5749 err = got_error(GOT_ERR_ITER_COMPLETED);
5750 goto done;
5753 if (lineno == -1)
5754 goto done; /* no change in this commit */
5756 line = &a->lines[lineno - 1];
5757 if (line->annotated)
5758 goto done;
5760 line->id = got_object_id_dup(id);
5761 if (line->id == NULL) {
5762 err = got_error_from_errno("got_object_id_dup");
5763 goto done;
5765 line->annotated = 1;
5766 done:
5767 errcode = pthread_mutex_unlock(&tog_mutex);
5768 if (errcode)
5769 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5770 return err;
5773 static void *
5774 blame_thread(void *arg)
5776 const struct got_error *err, *close_err;
5777 struct tog_blame_thread_args *ta = arg;
5778 struct tog_blame_cb_args *a = ta->cb_args;
5779 int errcode, fd1 = -1, fd2 = -1;
5780 FILE *f1 = NULL, *f2 = NULL;
5782 fd1 = got_opentempfd();
5783 if (fd1 == -1)
5784 return (void *)got_error_from_errno("got_opentempfd");
5786 fd2 = got_opentempfd();
5787 if (fd2 == -1) {
5788 err = got_error_from_errno("got_opentempfd");
5789 goto done;
5792 f1 = got_opentemp();
5793 if (f1 == NULL) {
5794 err = (void *)got_error_from_errno("got_opentemp");
5795 goto done;
5797 f2 = got_opentemp();
5798 if (f2 == NULL) {
5799 err = (void *)got_error_from_errno("got_opentemp");
5800 goto done;
5803 err = block_signals_used_by_main_thread();
5804 if (err)
5805 goto done;
5807 err = got_blame(ta->path, a->commit_id, ta->repo,
5808 tog_diff_algo, blame_cb, ta->cb_args,
5809 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5810 if (err && err->code == GOT_ERR_CANCELLED)
5811 err = NULL;
5813 errcode = pthread_mutex_lock(&tog_mutex);
5814 if (errcode) {
5815 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5816 goto done;
5819 close_err = got_repo_close(ta->repo);
5820 if (err == NULL)
5821 err = close_err;
5822 ta->repo = NULL;
5823 *ta->complete = 1;
5825 errcode = pthread_mutex_unlock(&tog_mutex);
5826 if (errcode && err == NULL)
5827 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5829 done:
5830 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5831 err = got_error_from_errno("close");
5832 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5833 err = got_error_from_errno("close");
5834 if (f1 && fclose(f1) == EOF && err == NULL)
5835 err = got_error_from_errno("fclose");
5836 if (f2 && fclose(f2) == EOF && err == NULL)
5837 err = got_error_from_errno("fclose");
5839 return (void *)err;
5842 static struct got_object_id *
5843 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5844 int first_displayed_line, int selected_line)
5846 struct tog_blame_line *line;
5848 if (nlines <= 0)
5849 return NULL;
5851 line = &lines[first_displayed_line - 1 + selected_line - 1];
5852 if (!line->annotated)
5853 return NULL;
5855 return line->id;
5858 static struct got_object_id *
5859 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5860 int lineno)
5862 struct tog_blame_line *line;
5864 if (nlines <= 0 || lineno >= nlines)
5865 return NULL;
5867 line = &lines[lineno - 1];
5868 if (!line->annotated)
5869 return NULL;
5871 return line->id;
5874 static const struct got_error *
5875 stop_blame(struct tog_blame *blame)
5877 const struct got_error *err = NULL;
5878 int i;
5880 if (blame->thread) {
5881 int errcode;
5882 errcode = pthread_mutex_unlock(&tog_mutex);
5883 if (errcode)
5884 return got_error_set_errno(errcode,
5885 "pthread_mutex_unlock");
5886 errcode = pthread_join(blame->thread, (void **)&err);
5887 if (errcode)
5888 return got_error_set_errno(errcode, "pthread_join");
5889 errcode = pthread_mutex_lock(&tog_mutex);
5890 if (errcode)
5891 return got_error_set_errno(errcode,
5892 "pthread_mutex_lock");
5893 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5894 err = NULL;
5895 blame->thread = 0; //NULL;
5897 if (blame->thread_args.repo) {
5898 const struct got_error *close_err;
5899 close_err = got_repo_close(blame->thread_args.repo);
5900 if (err == NULL)
5901 err = close_err;
5902 blame->thread_args.repo = NULL;
5904 if (blame->f) {
5905 if (fclose(blame->f) == EOF && err == NULL)
5906 err = got_error_from_errno("fclose");
5907 blame->f = NULL;
5909 if (blame->lines) {
5910 for (i = 0; i < blame->nlines; i++)
5911 free(blame->lines[i].id);
5912 free(blame->lines);
5913 blame->lines = NULL;
5915 free(blame->cb_args.commit_id);
5916 blame->cb_args.commit_id = NULL;
5917 if (blame->pack_fds) {
5918 const struct got_error *pack_err =
5919 got_repo_pack_fds_close(blame->pack_fds);
5920 if (err == NULL)
5921 err = pack_err;
5922 blame->pack_fds = NULL;
5924 return err;
5927 static const struct got_error *
5928 cancel_blame_view(void *arg)
5930 const struct got_error *err = NULL;
5931 int *done = arg;
5932 int errcode;
5934 errcode = pthread_mutex_lock(&tog_mutex);
5935 if (errcode)
5936 return got_error_set_errno(errcode,
5937 "pthread_mutex_unlock");
5939 if (*done)
5940 err = got_error(GOT_ERR_CANCELLED);
5942 errcode = pthread_mutex_unlock(&tog_mutex);
5943 if (errcode)
5944 return got_error_set_errno(errcode,
5945 "pthread_mutex_lock");
5947 return err;
5950 static const struct got_error *
5951 run_blame(struct tog_view *view)
5953 struct tog_blame_view_state *s = &view->state.blame;
5954 struct tog_blame *blame = &s->blame;
5955 const struct got_error *err = NULL;
5956 struct got_commit_object *commit = NULL;
5957 struct got_blob_object *blob = NULL;
5958 struct got_repository *thread_repo = NULL;
5959 struct got_object_id *obj_id = NULL;
5960 int obj_type, fd = -1;
5961 int *pack_fds = NULL;
5963 err = got_object_open_as_commit(&commit, s->repo,
5964 &s->blamed_commit->id);
5965 if (err)
5966 return err;
5968 fd = got_opentempfd();
5969 if (fd == -1) {
5970 err = got_error_from_errno("got_opentempfd");
5971 goto done;
5974 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5975 if (err)
5976 goto done;
5978 err = got_object_get_type(&obj_type, s->repo, obj_id);
5979 if (err)
5980 goto done;
5982 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5983 err = got_error(GOT_ERR_OBJ_TYPE);
5984 goto done;
5987 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5988 if (err)
5989 goto done;
5990 blame->f = got_opentemp();
5991 if (blame->f == NULL) {
5992 err = got_error_from_errno("got_opentemp");
5993 goto done;
5995 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5996 &blame->line_offsets, blame->f, blob);
5997 if (err)
5998 goto done;
5999 if (blame->nlines == 0) {
6000 s->blame_complete = 1;
6001 goto done;
6004 /* Don't include \n at EOF in the blame line count. */
6005 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
6006 blame->nlines--;
6008 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
6009 if (blame->lines == NULL) {
6010 err = got_error_from_errno("calloc");
6011 goto done;
6014 err = got_repo_pack_fds_open(&pack_fds);
6015 if (err)
6016 goto done;
6017 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
6018 pack_fds);
6019 if (err)
6020 goto done;
6022 blame->pack_fds = pack_fds;
6023 blame->cb_args.view = view;
6024 blame->cb_args.lines = blame->lines;
6025 blame->cb_args.nlines = blame->nlines;
6026 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
6027 if (blame->cb_args.commit_id == NULL) {
6028 err = got_error_from_errno("got_object_id_dup");
6029 goto done;
6031 blame->cb_args.quit = &s->done;
6033 blame->thread_args.path = s->path;
6034 blame->thread_args.repo = thread_repo;
6035 blame->thread_args.cb_args = &blame->cb_args;
6036 blame->thread_args.complete = &s->blame_complete;
6037 blame->thread_args.cancel_cb = cancel_blame_view;
6038 blame->thread_args.cancel_arg = &s->done;
6039 s->blame_complete = 0;
6041 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
6042 s->first_displayed_line = 1;
6043 s->last_displayed_line = view->nlines;
6044 s->selected_line = 1;
6046 s->matched_line = 0;
6048 done:
6049 if (commit)
6050 got_object_commit_close(commit);
6051 if (fd != -1 && close(fd) == -1 && err == NULL)
6052 err = got_error_from_errno("close");
6053 if (blob)
6054 got_object_blob_close(blob);
6055 free(obj_id);
6056 if (err)
6057 stop_blame(blame);
6058 return err;
6061 static const struct got_error *
6062 open_blame_view(struct tog_view *view, char *path,
6063 struct got_object_id *commit_id, struct got_repository *repo)
6065 const struct got_error *err = NULL;
6066 struct tog_blame_view_state *s = &view->state.blame;
6068 STAILQ_INIT(&s->blamed_commits);
6070 s->path = strdup(path);
6071 if (s->path == NULL)
6072 return got_error_from_errno("strdup");
6074 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
6075 if (err) {
6076 free(s->path);
6077 return err;
6080 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
6081 s->first_displayed_line = 1;
6082 s->last_displayed_line = view->nlines;
6083 s->selected_line = 1;
6084 s->blame_complete = 0;
6085 s->repo = repo;
6086 s->commit_id = commit_id;
6087 memset(&s->blame, 0, sizeof(s->blame));
6089 STAILQ_INIT(&s->colors);
6090 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6091 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
6092 get_color_value("TOG_COLOR_COMMIT"));
6093 if (err)
6094 return err;
6097 view->show = show_blame_view;
6098 view->input = input_blame_view;
6099 view->reset = reset_blame_view;
6100 view->close = close_blame_view;
6101 view->search_start = search_start_blame_view;
6102 view->search_setup = search_setup_blame_view;
6103 view->search_next = search_next_view_match;
6105 return run_blame(view);
6108 static const struct got_error *
6109 close_blame_view(struct tog_view *view)
6111 const struct got_error *err = NULL;
6112 struct tog_blame_view_state *s = &view->state.blame;
6114 if (s->blame.thread)
6115 err = stop_blame(&s->blame);
6117 while (!STAILQ_EMPTY(&s->blamed_commits)) {
6118 struct got_object_qid *blamed_commit;
6119 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
6120 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6121 got_object_qid_free(blamed_commit);
6124 free(s->path);
6125 free_colors(&s->colors);
6126 return err;
6129 static const struct got_error *
6130 search_start_blame_view(struct tog_view *view)
6132 struct tog_blame_view_state *s = &view->state.blame;
6134 s->matched_line = 0;
6135 return NULL;
6138 static void
6139 search_setup_blame_view(struct tog_view *view, FILE **f, off_t **line_offsets,
6140 size_t *nlines, int **first, int **last, int **match, int **selected)
6142 struct tog_blame_view_state *s = &view->state.blame;
6144 *f = s->blame.f;
6145 *nlines = s->blame.nlines;
6146 *line_offsets = s->blame.line_offsets;
6147 *match = &s->matched_line;
6148 *first = &s->first_displayed_line;
6149 *last = &s->last_displayed_line;
6150 *selected = &s->selected_line;
6153 static const struct got_error *
6154 show_blame_view(struct tog_view *view)
6156 const struct got_error *err = NULL;
6157 struct tog_blame_view_state *s = &view->state.blame;
6158 int errcode;
6160 if (s->blame.thread == 0 && !s->blame_complete) {
6161 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6162 &s->blame.thread_args);
6163 if (errcode)
6164 return got_error_set_errno(errcode, "pthread_create");
6166 halfdelay(1); /* fast refresh while annotating */
6169 if (s->blame_complete)
6170 halfdelay(10); /* disable fast refresh */
6172 err = draw_blame(view);
6174 view_border(view);
6175 return err;
6178 static const struct got_error *
6179 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6180 struct got_repository *repo, struct got_object_id *id)
6182 struct tog_view *log_view;
6183 const struct got_error *err = NULL;
6185 *new_view = NULL;
6187 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6188 if (log_view == NULL)
6189 return got_error_from_errno("view_open");
6191 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6192 if (err)
6193 view_close(log_view);
6194 else
6195 *new_view = log_view;
6197 return err;
6200 static const struct got_error *
6201 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6203 const struct got_error *err = NULL, *thread_err = NULL;
6204 struct tog_view *diff_view;
6205 struct tog_blame_view_state *s = &view->state.blame;
6206 int eos, nscroll, begin_y = 0, begin_x = 0;
6208 eos = nscroll = view->nlines - 2;
6209 if (view_is_hsplit_top(view))
6210 --eos; /* border */
6212 switch (ch) {
6213 case '0':
6214 view->x = 0;
6215 break;
6216 case '$':
6217 view->x = MAX(view->maxx - view->ncols / 3, 0);
6218 view->count = 0;
6219 break;
6220 case KEY_RIGHT:
6221 case 'l':
6222 if (view->x + view->ncols / 3 < view->maxx)
6223 view->x += 2; /* move two columns right */
6224 else
6225 view->count = 0;
6226 break;
6227 case KEY_LEFT:
6228 case 'h':
6229 view->x -= MIN(view->x, 2); /* move two columns back */
6230 if (view->x <= 0)
6231 view->count = 0;
6232 break;
6233 case 'q':
6234 s->done = 1;
6235 break;
6236 case 'g':
6237 case KEY_HOME:
6238 s->selected_line = 1;
6239 s->first_displayed_line = 1;
6240 view->count = 0;
6241 break;
6242 case 'G':
6243 case KEY_END:
6244 if (s->blame.nlines < eos) {
6245 s->selected_line = s->blame.nlines;
6246 s->first_displayed_line = 1;
6247 } else {
6248 s->selected_line = eos;
6249 s->first_displayed_line = s->blame.nlines - (eos - 1);
6251 view->count = 0;
6252 break;
6253 case 'k':
6254 case KEY_UP:
6255 case CTRL('p'):
6256 if (s->selected_line > 1)
6257 s->selected_line--;
6258 else if (s->selected_line == 1 &&
6259 s->first_displayed_line > 1)
6260 s->first_displayed_line--;
6261 else
6262 view->count = 0;
6263 break;
6264 case CTRL('u'):
6265 case 'u':
6266 nscroll /= 2;
6267 /* FALL THROUGH */
6268 case KEY_PPAGE:
6269 case CTRL('b'):
6270 case 'b':
6271 if (s->first_displayed_line == 1) {
6272 if (view->count > 1)
6273 nscroll += nscroll;
6274 s->selected_line = MAX(1, s->selected_line - nscroll);
6275 view->count = 0;
6276 break;
6278 if (s->first_displayed_line > nscroll)
6279 s->first_displayed_line -= nscroll;
6280 else
6281 s->first_displayed_line = 1;
6282 break;
6283 case 'j':
6284 case KEY_DOWN:
6285 case CTRL('n'):
6286 if (s->selected_line < eos && s->first_displayed_line +
6287 s->selected_line <= s->blame.nlines)
6288 s->selected_line++;
6289 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6290 s->first_displayed_line++;
6291 else
6292 view->count = 0;
6293 break;
6294 case 'c':
6295 case 'p': {
6296 struct got_object_id *id = NULL;
6298 view->count = 0;
6299 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6300 s->first_displayed_line, s->selected_line);
6301 if (id == NULL)
6302 break;
6303 if (ch == 'p') {
6304 struct got_commit_object *commit, *pcommit;
6305 struct got_object_qid *pid;
6306 struct got_object_id *blob_id = NULL;
6307 int obj_type;
6308 err = got_object_open_as_commit(&commit,
6309 s->repo, id);
6310 if (err)
6311 break;
6312 pid = STAILQ_FIRST(
6313 got_object_commit_get_parent_ids(commit));
6314 if (pid == NULL) {
6315 got_object_commit_close(commit);
6316 break;
6318 /* Check if path history ends here. */
6319 err = got_object_open_as_commit(&pcommit,
6320 s->repo, &pid->id);
6321 if (err)
6322 break;
6323 err = got_object_id_by_path(&blob_id, s->repo,
6324 pcommit, s->path);
6325 got_object_commit_close(pcommit);
6326 if (err) {
6327 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6328 err = NULL;
6329 got_object_commit_close(commit);
6330 break;
6332 err = got_object_get_type(&obj_type, s->repo,
6333 blob_id);
6334 free(blob_id);
6335 /* Can't blame non-blob type objects. */
6336 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6337 got_object_commit_close(commit);
6338 break;
6340 err = got_object_qid_alloc(&s->blamed_commit,
6341 &pid->id);
6342 got_object_commit_close(commit);
6343 } else {
6344 if (got_object_id_cmp(id,
6345 &s->blamed_commit->id) == 0)
6346 break;
6347 err = got_object_qid_alloc(&s->blamed_commit,
6348 id);
6350 if (err)
6351 break;
6352 s->done = 1;
6353 thread_err = stop_blame(&s->blame);
6354 s->done = 0;
6355 if (thread_err)
6356 break;
6357 STAILQ_INSERT_HEAD(&s->blamed_commits,
6358 s->blamed_commit, entry);
6359 err = run_blame(view);
6360 if (err)
6361 break;
6362 break;
6364 case 'C': {
6365 struct got_object_qid *first;
6367 view->count = 0;
6368 first = STAILQ_FIRST(&s->blamed_commits);
6369 if (!got_object_id_cmp(&first->id, s->commit_id))
6370 break;
6371 s->done = 1;
6372 thread_err = stop_blame(&s->blame);
6373 s->done = 0;
6374 if (thread_err)
6375 break;
6376 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6377 got_object_qid_free(s->blamed_commit);
6378 s->blamed_commit =
6379 STAILQ_FIRST(&s->blamed_commits);
6380 err = run_blame(view);
6381 if (err)
6382 break;
6383 break;
6385 case 'L':
6386 view->count = 0;
6387 s->id_to_log = get_selected_commit_id(s->blame.lines,
6388 s->blame.nlines, s->first_displayed_line, s->selected_line);
6389 if (s->id_to_log)
6390 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6391 break;
6392 case KEY_ENTER:
6393 case '\r': {
6394 struct got_object_id *id = NULL;
6395 struct got_object_qid *pid;
6396 struct got_commit_object *commit = NULL;
6398 view->count = 0;
6399 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6400 s->first_displayed_line, s->selected_line);
6401 if (id == NULL)
6402 break;
6403 err = got_object_open_as_commit(&commit, s->repo, id);
6404 if (err)
6405 break;
6406 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6407 if (*new_view) {
6408 /* traversed from diff view, release diff resources */
6409 err = close_diff_view(*new_view);
6410 if (err)
6411 break;
6412 diff_view = *new_view;
6413 } else {
6414 if (view_is_parent_view(view))
6415 view_get_split(view, &begin_y, &begin_x);
6417 diff_view = view_open(0, 0, begin_y, begin_x,
6418 TOG_VIEW_DIFF);
6419 if (diff_view == NULL) {
6420 got_object_commit_close(commit);
6421 err = got_error_from_errno("view_open");
6422 break;
6425 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6426 id, NULL, NULL, 3, 0, 0, view, s->repo);
6427 got_object_commit_close(commit);
6428 if (err) {
6429 view_close(diff_view);
6430 break;
6432 s->last_diffed_line = s->first_displayed_line - 1 +
6433 s->selected_line;
6434 if (*new_view)
6435 break; /* still open from active diff view */
6436 if (view_is_parent_view(view) &&
6437 view->mode == TOG_VIEW_SPLIT_HRZN) {
6438 err = view_init_hsplit(view, begin_y);
6439 if (err)
6440 break;
6443 view->focussed = 0;
6444 diff_view->focussed = 1;
6445 diff_view->mode = view->mode;
6446 diff_view->nlines = view->lines - begin_y;
6447 if (view_is_parent_view(view)) {
6448 view_transfer_size(diff_view, view);
6449 err = view_close_child(view);
6450 if (err)
6451 break;
6452 err = view_set_child(view, diff_view);
6453 if (err)
6454 break;
6455 view->focus_child = 1;
6456 } else
6457 *new_view = diff_view;
6458 if (err)
6459 break;
6460 break;
6462 case CTRL('d'):
6463 case 'd':
6464 nscroll /= 2;
6465 /* FALL THROUGH */
6466 case KEY_NPAGE:
6467 case CTRL('f'):
6468 case 'f':
6469 case ' ':
6470 if (s->last_displayed_line >= s->blame.nlines &&
6471 s->selected_line >= MIN(s->blame.nlines,
6472 view->nlines - 2)) {
6473 view->count = 0;
6474 break;
6476 if (s->last_displayed_line >= s->blame.nlines &&
6477 s->selected_line < view->nlines - 2) {
6478 s->selected_line +=
6479 MIN(nscroll, s->last_displayed_line -
6480 s->first_displayed_line - s->selected_line + 1);
6482 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6483 s->first_displayed_line += nscroll;
6484 else
6485 s->first_displayed_line =
6486 s->blame.nlines - (view->nlines - 3);
6487 break;
6488 case KEY_RESIZE:
6489 if (s->selected_line > view->nlines - 2) {
6490 s->selected_line = MIN(s->blame.nlines,
6491 view->nlines - 2);
6493 break;
6494 default:
6495 view->count = 0;
6496 break;
6498 return thread_err ? thread_err : err;
6501 static const struct got_error *
6502 reset_blame_view(struct tog_view *view)
6504 const struct got_error *err;
6505 struct tog_blame_view_state *s = &view->state.blame;
6507 view->count = 0;
6508 s->done = 1;
6509 err = stop_blame(&s->blame);
6510 s->done = 0;
6511 if (err)
6512 return err;
6513 return run_blame(view);
6516 static const struct got_error *
6517 cmd_blame(int argc, char *argv[])
6519 const struct got_error *error;
6520 struct got_repository *repo = NULL;
6521 struct got_worktree *worktree = NULL;
6522 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6523 char *link_target = NULL;
6524 struct got_object_id *commit_id = NULL;
6525 struct got_commit_object *commit = NULL;
6526 char *commit_id_str = NULL;
6527 int ch;
6528 struct tog_view *view;
6529 int *pack_fds = NULL;
6531 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6532 switch (ch) {
6533 case 'c':
6534 commit_id_str = optarg;
6535 break;
6536 case 'r':
6537 repo_path = realpath(optarg, NULL);
6538 if (repo_path == NULL)
6539 return got_error_from_errno2("realpath",
6540 optarg);
6541 break;
6542 default:
6543 usage_blame();
6544 /* NOTREACHED */
6548 argc -= optind;
6549 argv += optind;
6551 if (argc != 1)
6552 usage_blame();
6554 error = got_repo_pack_fds_open(&pack_fds);
6555 if (error != NULL)
6556 goto done;
6558 if (repo_path == NULL) {
6559 cwd = getcwd(NULL, 0);
6560 if (cwd == NULL)
6561 return got_error_from_errno("getcwd");
6562 error = got_worktree_open(&worktree, cwd);
6563 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6564 goto done;
6565 if (worktree)
6566 repo_path =
6567 strdup(got_worktree_get_repo_path(worktree));
6568 else
6569 repo_path = strdup(cwd);
6570 if (repo_path == NULL) {
6571 error = got_error_from_errno("strdup");
6572 goto done;
6576 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6577 if (error != NULL)
6578 goto done;
6580 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6581 worktree);
6582 if (error)
6583 goto done;
6585 init_curses();
6587 error = apply_unveil(got_repo_get_path(repo), NULL);
6588 if (error)
6589 goto done;
6591 error = tog_load_refs(repo, 0);
6592 if (error)
6593 goto done;
6595 if (commit_id_str == NULL) {
6596 struct got_reference *head_ref;
6597 error = got_ref_open(&head_ref, repo, worktree ?
6598 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6599 if (error != NULL)
6600 goto done;
6601 error = got_ref_resolve(&commit_id, repo, head_ref);
6602 got_ref_close(head_ref);
6603 } else {
6604 error = got_repo_match_object_id(&commit_id, NULL,
6605 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6607 if (error != NULL)
6608 goto done;
6610 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6611 if (view == NULL) {
6612 error = got_error_from_errno("view_open");
6613 goto done;
6616 error = got_object_open_as_commit(&commit, repo, commit_id);
6617 if (error)
6618 goto done;
6620 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6621 commit, repo);
6622 if (error)
6623 goto done;
6625 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6626 commit_id, repo);
6627 if (error)
6628 goto done;
6629 if (worktree) {
6630 /* Release work tree lock. */
6631 got_worktree_close(worktree);
6632 worktree = NULL;
6634 error = view_loop(view);
6635 done:
6636 free(repo_path);
6637 free(in_repo_path);
6638 free(link_target);
6639 free(cwd);
6640 free(commit_id);
6641 if (commit)
6642 got_object_commit_close(commit);
6643 if (worktree)
6644 got_worktree_close(worktree);
6645 if (repo) {
6646 const struct got_error *close_err = got_repo_close(repo);
6647 if (error == NULL)
6648 error = close_err;
6650 if (pack_fds) {
6651 const struct got_error *pack_err =
6652 got_repo_pack_fds_close(pack_fds);
6653 if (error == NULL)
6654 error = pack_err;
6656 tog_free_refs();
6657 return error;
6660 static const struct got_error *
6661 draw_tree_entries(struct tog_view *view, const char *parent_path)
6663 struct tog_tree_view_state *s = &view->state.tree;
6664 const struct got_error *err = NULL;
6665 struct got_tree_entry *te;
6666 wchar_t *wline;
6667 char *index = NULL;
6668 struct tog_color *tc;
6669 int width, n, nentries, i = 1;
6670 int limit = view->nlines;
6672 s->ndisplayed = 0;
6673 if (view_is_hsplit_top(view))
6674 --limit; /* border */
6676 werase(view->window);
6678 if (limit == 0)
6679 return NULL;
6681 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6682 0, 0);
6683 if (err)
6684 return err;
6685 if (view_needs_focus_indication(view))
6686 wstandout(view->window);
6687 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6688 if (tc)
6689 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6690 waddwstr(view->window, wline);
6691 free(wline);
6692 wline = NULL;
6693 while (width++ < view->ncols)
6694 waddch(view->window, ' ');
6695 if (tc)
6696 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6697 if (view_needs_focus_indication(view))
6698 wstandend(view->window);
6699 if (--limit <= 0)
6700 return NULL;
6702 i += s->selected;
6703 if (s->first_displayed_entry) {
6704 i += got_tree_entry_get_index(s->first_displayed_entry);
6705 if (s->tree != s->root)
6706 ++i; /* account for ".." entry */
6708 nentries = got_object_tree_get_nentries(s->tree);
6709 if (asprintf(&index, "[%d/%d] %s",
6710 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6711 return got_error_from_errno("asprintf");
6712 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6713 free(index);
6714 if (err)
6715 return err;
6716 waddwstr(view->window, wline);
6717 free(wline);
6718 wline = NULL;
6719 if (width < view->ncols - 1)
6720 waddch(view->window, '\n');
6721 if (--limit <= 0)
6722 return NULL;
6723 waddch(view->window, '\n');
6724 if (--limit <= 0)
6725 return NULL;
6727 if (s->first_displayed_entry == NULL) {
6728 te = got_object_tree_get_first_entry(s->tree);
6729 if (s->selected == 0) {
6730 if (view->focussed)
6731 wstandout(view->window);
6732 s->selected_entry = NULL;
6734 waddstr(view->window, " ..\n"); /* parent directory */
6735 if (s->selected == 0 && view->focussed)
6736 wstandend(view->window);
6737 s->ndisplayed++;
6738 if (--limit <= 0)
6739 return NULL;
6740 n = 1;
6741 } else {
6742 n = 0;
6743 te = s->first_displayed_entry;
6746 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6747 char *line = NULL, *id_str = NULL, *link_target = NULL;
6748 const char *modestr = "";
6749 mode_t mode;
6751 te = got_object_tree_get_entry(s->tree, i);
6752 mode = got_tree_entry_get_mode(te);
6754 if (s->show_ids) {
6755 err = got_object_id_str(&id_str,
6756 got_tree_entry_get_id(te));
6757 if (err)
6758 return got_error_from_errno(
6759 "got_object_id_str");
6761 if (got_object_tree_entry_is_submodule(te))
6762 modestr = "$";
6763 else if (S_ISLNK(mode)) {
6764 int i;
6766 err = got_tree_entry_get_symlink_target(&link_target,
6767 te, s->repo);
6768 if (err) {
6769 free(id_str);
6770 return err;
6772 for (i = 0; i < strlen(link_target); i++) {
6773 if (!isprint((unsigned char)link_target[i]))
6774 link_target[i] = '?';
6776 modestr = "@";
6778 else if (S_ISDIR(mode))
6779 modestr = "/";
6780 else if (mode & S_IXUSR)
6781 modestr = "*";
6782 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6783 got_tree_entry_get_name(te), modestr,
6784 link_target ? " -> ": "",
6785 link_target ? link_target : "") == -1) {
6786 free(id_str);
6787 free(link_target);
6788 return got_error_from_errno("asprintf");
6790 free(id_str);
6791 free(link_target);
6792 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6793 0, 0);
6794 if (err) {
6795 free(line);
6796 break;
6798 if (n == s->selected) {
6799 if (view->focussed)
6800 wstandout(view->window);
6801 s->selected_entry = te;
6803 tc = match_color(&s->colors, line);
6804 if (tc)
6805 wattr_on(view->window,
6806 COLOR_PAIR(tc->colorpair), NULL);
6807 waddwstr(view->window, wline);
6808 if (tc)
6809 wattr_off(view->window,
6810 COLOR_PAIR(tc->colorpair), NULL);
6811 if (width < view->ncols - 1)
6812 waddch(view->window, '\n');
6813 if (n == s->selected && view->focussed)
6814 wstandend(view->window);
6815 free(line);
6816 free(wline);
6817 wline = NULL;
6818 n++;
6819 s->ndisplayed++;
6820 s->last_displayed_entry = te;
6821 if (--limit <= 0)
6822 break;
6825 return err;
6828 static void
6829 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6831 struct got_tree_entry *te;
6832 int isroot = s->tree == s->root;
6833 int i = 0;
6835 if (s->first_displayed_entry == NULL)
6836 return;
6838 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6839 while (i++ < maxscroll) {
6840 if (te == NULL) {
6841 if (!isroot)
6842 s->first_displayed_entry = NULL;
6843 break;
6845 s->first_displayed_entry = te;
6846 te = got_tree_entry_get_prev(s->tree, te);
6850 static const struct got_error *
6851 tree_scroll_down(struct tog_view *view, int maxscroll)
6853 struct tog_tree_view_state *s = &view->state.tree;
6854 struct got_tree_entry *next, *last;
6855 int n = 0;
6857 if (s->first_displayed_entry)
6858 next = got_tree_entry_get_next(s->tree,
6859 s->first_displayed_entry);
6860 else
6861 next = got_object_tree_get_first_entry(s->tree);
6863 last = s->last_displayed_entry;
6864 while (next && n++ < maxscroll) {
6865 if (last) {
6866 s->last_displayed_entry = last;
6867 last = got_tree_entry_get_next(s->tree, last);
6869 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6870 s->first_displayed_entry = next;
6871 next = got_tree_entry_get_next(s->tree, next);
6875 return NULL;
6878 static const struct got_error *
6879 tree_entry_path(char **path, struct tog_parent_trees *parents,
6880 struct got_tree_entry *te)
6882 const struct got_error *err = NULL;
6883 struct tog_parent_tree *pt;
6884 size_t len = 2; /* for leading slash and NUL */
6886 TAILQ_FOREACH(pt, parents, entry)
6887 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6888 + 1 /* slash */;
6889 if (te)
6890 len += strlen(got_tree_entry_get_name(te));
6892 *path = calloc(1, len);
6893 if (path == NULL)
6894 return got_error_from_errno("calloc");
6896 (*path)[0] = '/';
6897 pt = TAILQ_LAST(parents, tog_parent_trees);
6898 while (pt) {
6899 const char *name = got_tree_entry_get_name(pt->selected_entry);
6900 if (strlcat(*path, name, len) >= len) {
6901 err = got_error(GOT_ERR_NO_SPACE);
6902 goto done;
6904 if (strlcat(*path, "/", len) >= len) {
6905 err = got_error(GOT_ERR_NO_SPACE);
6906 goto done;
6908 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6910 if (te) {
6911 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6912 err = got_error(GOT_ERR_NO_SPACE);
6913 goto done;
6916 done:
6917 if (err) {
6918 free(*path);
6919 *path = NULL;
6921 return err;
6924 static const struct got_error *
6925 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6926 struct got_tree_entry *te, struct tog_parent_trees *parents,
6927 struct got_object_id *commit_id, struct got_repository *repo)
6929 const struct got_error *err = NULL;
6930 char *path;
6931 struct tog_view *blame_view;
6933 *new_view = NULL;
6935 err = tree_entry_path(&path, parents, te);
6936 if (err)
6937 return err;
6939 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6940 if (blame_view == NULL) {
6941 err = got_error_from_errno("view_open");
6942 goto done;
6945 err = open_blame_view(blame_view, path, commit_id, repo);
6946 if (err) {
6947 if (err->code == GOT_ERR_CANCELLED)
6948 err = NULL;
6949 view_close(blame_view);
6950 } else
6951 *new_view = blame_view;
6952 done:
6953 free(path);
6954 return err;
6957 static const struct got_error *
6958 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6959 struct tog_tree_view_state *s)
6961 struct tog_view *log_view;
6962 const struct got_error *err = NULL;
6963 char *path;
6965 *new_view = NULL;
6967 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6968 if (log_view == NULL)
6969 return got_error_from_errno("view_open");
6971 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6972 if (err)
6973 return err;
6975 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6976 path, 0);
6977 if (err)
6978 view_close(log_view);
6979 else
6980 *new_view = log_view;
6981 free(path);
6982 return err;
6985 static const struct got_error *
6986 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6987 const char *head_ref_name, struct got_repository *repo)
6989 const struct got_error *err = NULL;
6990 char *commit_id_str = NULL;
6991 struct tog_tree_view_state *s = &view->state.tree;
6992 struct got_commit_object *commit = NULL;
6994 TAILQ_INIT(&s->parents);
6995 STAILQ_INIT(&s->colors);
6997 s->commit_id = got_object_id_dup(commit_id);
6998 if (s->commit_id == NULL)
6999 return got_error_from_errno("got_object_id_dup");
7001 err = got_object_open_as_commit(&commit, repo, commit_id);
7002 if (err)
7003 goto done;
7006 * The root is opened here and will be closed when the view is closed.
7007 * Any visited subtrees and their path-wise parents are opened and
7008 * closed on demand.
7010 err = got_object_open_as_tree(&s->root, repo,
7011 got_object_commit_get_tree_id(commit));
7012 if (err)
7013 goto done;
7014 s->tree = s->root;
7016 err = got_object_id_str(&commit_id_str, commit_id);
7017 if (err != NULL)
7018 goto done;
7020 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
7021 err = got_error_from_errno("asprintf");
7022 goto done;
7025 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
7026 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
7027 if (head_ref_name) {
7028 s->head_ref_name = strdup(head_ref_name);
7029 if (s->head_ref_name == NULL) {
7030 err = got_error_from_errno("strdup");
7031 goto done;
7034 s->repo = repo;
7036 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7037 err = add_color(&s->colors, "\\$$",
7038 TOG_COLOR_TREE_SUBMODULE,
7039 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
7040 if (err)
7041 goto done;
7042 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
7043 get_color_value("TOG_COLOR_TREE_SYMLINK"));
7044 if (err)
7045 goto done;
7046 err = add_color(&s->colors, "/$",
7047 TOG_COLOR_TREE_DIRECTORY,
7048 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
7049 if (err)
7050 goto done;
7052 err = add_color(&s->colors, "\\*$",
7053 TOG_COLOR_TREE_EXECUTABLE,
7054 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
7055 if (err)
7056 goto done;
7058 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
7059 get_color_value("TOG_COLOR_COMMIT"));
7060 if (err)
7061 goto done;
7064 view->show = show_tree_view;
7065 view->input = input_tree_view;
7066 view->close = close_tree_view;
7067 view->search_start = search_start_tree_view;
7068 view->search_next = search_next_tree_view;
7069 done:
7070 free(commit_id_str);
7071 if (commit)
7072 got_object_commit_close(commit);
7073 if (err)
7074 close_tree_view(view);
7075 return err;
7078 static const struct got_error *
7079 close_tree_view(struct tog_view *view)
7081 struct tog_tree_view_state *s = &view->state.tree;
7083 free_colors(&s->colors);
7084 free(s->tree_label);
7085 s->tree_label = NULL;
7086 free(s->commit_id);
7087 s->commit_id = NULL;
7088 free(s->head_ref_name);
7089 s->head_ref_name = NULL;
7090 while (!TAILQ_EMPTY(&s->parents)) {
7091 struct tog_parent_tree *parent;
7092 parent = TAILQ_FIRST(&s->parents);
7093 TAILQ_REMOVE(&s->parents, parent, entry);
7094 if (parent->tree != s->root)
7095 got_object_tree_close(parent->tree);
7096 free(parent);
7099 if (s->tree != NULL && s->tree != s->root)
7100 got_object_tree_close(s->tree);
7101 if (s->root)
7102 got_object_tree_close(s->root);
7103 return NULL;
7106 static const struct got_error *
7107 search_start_tree_view(struct tog_view *view)
7109 struct tog_tree_view_state *s = &view->state.tree;
7111 s->matched_entry = NULL;
7112 return NULL;
7115 static int
7116 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7118 regmatch_t regmatch;
7120 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7121 0) == 0;
7124 static const struct got_error *
7125 search_next_tree_view(struct tog_view *view)
7127 struct tog_tree_view_state *s = &view->state.tree;
7128 struct got_tree_entry *te = NULL;
7130 if (!view->searching) {
7131 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7132 return NULL;
7135 if (s->matched_entry) {
7136 if (view->searching == TOG_SEARCH_FORWARD) {
7137 if (s->selected_entry)
7138 te = got_tree_entry_get_next(s->tree,
7139 s->selected_entry);
7140 else
7141 te = got_object_tree_get_first_entry(s->tree);
7142 } else {
7143 if (s->selected_entry == NULL)
7144 te = got_object_tree_get_last_entry(s->tree);
7145 else
7146 te = got_tree_entry_get_prev(s->tree,
7147 s->selected_entry);
7149 } else {
7150 if (s->selected_entry)
7151 te = s->selected_entry;
7152 else if (view->searching == TOG_SEARCH_FORWARD)
7153 te = got_object_tree_get_first_entry(s->tree);
7154 else
7155 te = got_object_tree_get_last_entry(s->tree);
7158 while (1) {
7159 if (te == NULL) {
7160 if (s->matched_entry == NULL) {
7161 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7162 return NULL;
7164 if (view->searching == TOG_SEARCH_FORWARD)
7165 te = got_object_tree_get_first_entry(s->tree);
7166 else
7167 te = got_object_tree_get_last_entry(s->tree);
7170 if (match_tree_entry(te, &view->regex)) {
7171 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7172 s->matched_entry = te;
7173 break;
7176 if (view->searching == TOG_SEARCH_FORWARD)
7177 te = got_tree_entry_get_next(s->tree, te);
7178 else
7179 te = got_tree_entry_get_prev(s->tree, te);
7182 if (s->matched_entry) {
7183 s->first_displayed_entry = s->matched_entry;
7184 s->selected = 0;
7187 return NULL;
7190 static const struct got_error *
7191 show_tree_view(struct tog_view *view)
7193 const struct got_error *err = NULL;
7194 struct tog_tree_view_state *s = &view->state.tree;
7195 char *parent_path;
7197 err = tree_entry_path(&parent_path, &s->parents, NULL);
7198 if (err)
7199 return err;
7201 err = draw_tree_entries(view, parent_path);
7202 free(parent_path);
7204 view_border(view);
7205 return err;
7208 static const struct got_error *
7209 tree_goto_line(struct tog_view *view, int nlines)
7211 const struct got_error *err = NULL;
7212 struct tog_tree_view_state *s = &view->state.tree;
7213 struct got_tree_entry **fte, **lte, **ste;
7214 int g, last, first = 1, i = 1;
7215 int root = s->tree == s->root;
7216 int off = root ? 1 : 2;
7218 g = view->gline;
7219 view->gline = 0;
7221 if (g == 0)
7222 g = 1;
7223 else if (g > got_object_tree_get_nentries(s->tree))
7224 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7226 fte = &s->first_displayed_entry;
7227 lte = &s->last_displayed_entry;
7228 ste = &s->selected_entry;
7230 if (*fte != NULL) {
7231 first = got_tree_entry_get_index(*fte);
7232 first += off; /* account for ".." */
7234 last = got_tree_entry_get_index(*lte);
7235 last += off;
7237 if (g >= first && g <= last && g - first < nlines) {
7238 s->selected = g - first;
7239 return NULL; /* gline is on the current page */
7242 if (*ste != NULL) {
7243 i = got_tree_entry_get_index(*ste);
7244 i += off;
7247 if (i < g) {
7248 err = tree_scroll_down(view, g - i);
7249 if (err)
7250 return err;
7251 if (got_tree_entry_get_index(*lte) >=
7252 got_object_tree_get_nentries(s->tree) - 1 &&
7253 first + s->selected < g &&
7254 s->selected < s->ndisplayed - 1) {
7255 first = got_tree_entry_get_index(*fte);
7256 first += off;
7257 s->selected = g - first;
7259 } else if (i > g)
7260 tree_scroll_up(s, i - g);
7262 if (g < nlines &&
7263 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7264 s->selected = g - 1;
7266 return NULL;
7269 static const struct got_error *
7270 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7272 const struct got_error *err = NULL;
7273 struct tog_tree_view_state *s = &view->state.tree;
7274 struct got_tree_entry *te;
7275 int n, nscroll = view->nlines - 3;
7277 if (view->gline)
7278 return tree_goto_line(view, nscroll);
7280 switch (ch) {
7281 case 'i':
7282 s->show_ids = !s->show_ids;
7283 view->count = 0;
7284 break;
7285 case 'L':
7286 view->count = 0;
7287 if (!s->selected_entry)
7288 break;
7289 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7290 break;
7291 case 'R':
7292 view->count = 0;
7293 err = view_request_new(new_view, view, TOG_VIEW_REF);
7294 break;
7295 case 'g':
7296 case KEY_HOME:
7297 s->selected = 0;
7298 view->count = 0;
7299 if (s->tree == s->root)
7300 s->first_displayed_entry =
7301 got_object_tree_get_first_entry(s->tree);
7302 else
7303 s->first_displayed_entry = NULL;
7304 break;
7305 case 'G':
7306 case KEY_END: {
7307 int eos = view->nlines - 3;
7309 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7310 --eos; /* border */
7311 s->selected = 0;
7312 view->count = 0;
7313 te = got_object_tree_get_last_entry(s->tree);
7314 for (n = 0; n < eos; n++) {
7315 if (te == NULL) {
7316 if (s->tree != s->root) {
7317 s->first_displayed_entry = NULL;
7318 n++;
7320 break;
7322 s->first_displayed_entry = te;
7323 te = got_tree_entry_get_prev(s->tree, te);
7325 if (n > 0)
7326 s->selected = n - 1;
7327 break;
7329 case 'k':
7330 case KEY_UP:
7331 case CTRL('p'):
7332 if (s->selected > 0) {
7333 s->selected--;
7334 break;
7336 tree_scroll_up(s, 1);
7337 if (s->selected_entry == NULL ||
7338 (s->tree == s->root && s->selected_entry ==
7339 got_object_tree_get_first_entry(s->tree)))
7340 view->count = 0;
7341 break;
7342 case CTRL('u'):
7343 case 'u':
7344 nscroll /= 2;
7345 /* FALL THROUGH */
7346 case KEY_PPAGE:
7347 case CTRL('b'):
7348 case 'b':
7349 if (s->tree == s->root) {
7350 if (got_object_tree_get_first_entry(s->tree) ==
7351 s->first_displayed_entry)
7352 s->selected -= MIN(s->selected, nscroll);
7353 } else {
7354 if (s->first_displayed_entry == NULL)
7355 s->selected -= MIN(s->selected, nscroll);
7357 tree_scroll_up(s, MAX(0, nscroll));
7358 if (s->selected_entry == NULL ||
7359 (s->tree == s->root && s->selected_entry ==
7360 got_object_tree_get_first_entry(s->tree)))
7361 view->count = 0;
7362 break;
7363 case 'j':
7364 case KEY_DOWN:
7365 case CTRL('n'):
7366 if (s->selected < s->ndisplayed - 1) {
7367 s->selected++;
7368 break;
7370 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7371 == NULL) {
7372 /* can't scroll any further */
7373 view->count = 0;
7374 break;
7376 tree_scroll_down(view, 1);
7377 break;
7378 case CTRL('d'):
7379 case 'd':
7380 nscroll /= 2;
7381 /* FALL THROUGH */
7382 case KEY_NPAGE:
7383 case CTRL('f'):
7384 case 'f':
7385 case ' ':
7386 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7387 == NULL) {
7388 /* can't scroll any further; move cursor down */
7389 if (s->selected < s->ndisplayed - 1)
7390 s->selected += MIN(nscroll,
7391 s->ndisplayed - s->selected - 1);
7392 else
7393 view->count = 0;
7394 break;
7396 tree_scroll_down(view, nscroll);
7397 break;
7398 case KEY_ENTER:
7399 case '\r':
7400 case KEY_BACKSPACE:
7401 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7402 struct tog_parent_tree *parent;
7403 /* user selected '..' */
7404 if (s->tree == s->root) {
7405 view->count = 0;
7406 break;
7408 parent = TAILQ_FIRST(&s->parents);
7409 TAILQ_REMOVE(&s->parents, parent,
7410 entry);
7411 got_object_tree_close(s->tree);
7412 s->tree = parent->tree;
7413 s->first_displayed_entry =
7414 parent->first_displayed_entry;
7415 s->selected_entry =
7416 parent->selected_entry;
7417 s->selected = parent->selected;
7418 if (s->selected > view->nlines - 3) {
7419 err = offset_selection_down(view);
7420 if (err)
7421 break;
7423 free(parent);
7424 } else if (S_ISDIR(got_tree_entry_get_mode(
7425 s->selected_entry))) {
7426 struct got_tree_object *subtree;
7427 view->count = 0;
7428 err = got_object_open_as_tree(&subtree, s->repo,
7429 got_tree_entry_get_id(s->selected_entry));
7430 if (err)
7431 break;
7432 err = tree_view_visit_subtree(s, subtree);
7433 if (err) {
7434 got_object_tree_close(subtree);
7435 break;
7437 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7438 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7439 break;
7440 case KEY_RESIZE:
7441 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7442 s->selected = view->nlines - 4;
7443 view->count = 0;
7444 break;
7445 default:
7446 view->count = 0;
7447 break;
7450 return err;
7453 __dead static void
7454 usage_tree(void)
7456 endwin();
7457 fprintf(stderr,
7458 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7459 getprogname());
7460 exit(1);
7463 static const struct got_error *
7464 cmd_tree(int argc, char *argv[])
7466 const struct got_error *error;
7467 struct got_repository *repo = NULL;
7468 struct got_worktree *worktree = NULL;
7469 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7470 struct got_object_id *commit_id = NULL;
7471 struct got_commit_object *commit = NULL;
7472 const char *commit_id_arg = NULL;
7473 char *label = NULL;
7474 struct got_reference *ref = NULL;
7475 const char *head_ref_name = NULL;
7476 int ch;
7477 struct tog_view *view;
7478 int *pack_fds = NULL;
7480 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7481 switch (ch) {
7482 case 'c':
7483 commit_id_arg = optarg;
7484 break;
7485 case 'r':
7486 repo_path = realpath(optarg, NULL);
7487 if (repo_path == NULL)
7488 return got_error_from_errno2("realpath",
7489 optarg);
7490 break;
7491 default:
7492 usage_tree();
7493 /* NOTREACHED */
7497 argc -= optind;
7498 argv += optind;
7500 if (argc > 1)
7501 usage_tree();
7503 error = got_repo_pack_fds_open(&pack_fds);
7504 if (error != NULL)
7505 goto done;
7507 if (repo_path == NULL) {
7508 cwd = getcwd(NULL, 0);
7509 if (cwd == NULL)
7510 return got_error_from_errno("getcwd");
7511 error = got_worktree_open(&worktree, cwd);
7512 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7513 goto done;
7514 if (worktree)
7515 repo_path =
7516 strdup(got_worktree_get_repo_path(worktree));
7517 else
7518 repo_path = strdup(cwd);
7519 if (repo_path == NULL) {
7520 error = got_error_from_errno("strdup");
7521 goto done;
7525 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7526 if (error != NULL)
7527 goto done;
7529 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7530 repo, worktree);
7531 if (error)
7532 goto done;
7534 init_curses();
7536 error = apply_unveil(got_repo_get_path(repo), NULL);
7537 if (error)
7538 goto done;
7540 error = tog_load_refs(repo, 0);
7541 if (error)
7542 goto done;
7544 if (commit_id_arg == NULL) {
7545 error = got_repo_match_object_id(&commit_id, &label,
7546 worktree ? got_worktree_get_head_ref_name(worktree) :
7547 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7548 if (error)
7549 goto done;
7550 head_ref_name = label;
7551 } else {
7552 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7553 if (error == NULL)
7554 head_ref_name = got_ref_get_name(ref);
7555 else if (error->code != GOT_ERR_NOT_REF)
7556 goto done;
7557 error = got_repo_match_object_id(&commit_id, NULL,
7558 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7559 if (error)
7560 goto done;
7563 error = got_object_open_as_commit(&commit, repo, commit_id);
7564 if (error)
7565 goto done;
7567 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7568 if (view == NULL) {
7569 error = got_error_from_errno("view_open");
7570 goto done;
7572 error = open_tree_view(view, commit_id, head_ref_name, repo);
7573 if (error)
7574 goto done;
7575 if (!got_path_is_root_dir(in_repo_path)) {
7576 error = tree_view_walk_path(&view->state.tree, commit,
7577 in_repo_path);
7578 if (error)
7579 goto done;
7582 if (worktree) {
7583 /* Release work tree lock. */
7584 got_worktree_close(worktree);
7585 worktree = NULL;
7587 error = view_loop(view);
7588 done:
7589 free(repo_path);
7590 free(cwd);
7591 free(commit_id);
7592 free(label);
7593 if (ref)
7594 got_ref_close(ref);
7595 if (repo) {
7596 const struct got_error *close_err = got_repo_close(repo);
7597 if (error == NULL)
7598 error = close_err;
7600 if (pack_fds) {
7601 const struct got_error *pack_err =
7602 got_repo_pack_fds_close(pack_fds);
7603 if (error == NULL)
7604 error = pack_err;
7606 tog_free_refs();
7607 return error;
7610 static const struct got_error *
7611 ref_view_load_refs(struct tog_ref_view_state *s)
7613 struct got_reflist_entry *sre;
7614 struct tog_reflist_entry *re;
7616 s->nrefs = 0;
7617 TAILQ_FOREACH(sre, &tog_refs, entry) {
7618 if (strncmp(got_ref_get_name(sre->ref),
7619 "refs/got/", 9) == 0 &&
7620 strncmp(got_ref_get_name(sre->ref),
7621 "refs/got/backup/", 16) != 0)
7622 continue;
7624 re = malloc(sizeof(*re));
7625 if (re == NULL)
7626 return got_error_from_errno("malloc");
7628 re->ref = got_ref_dup(sre->ref);
7629 if (re->ref == NULL)
7630 return got_error_from_errno("got_ref_dup");
7631 re->idx = s->nrefs++;
7632 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7635 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7636 return NULL;
7639 static void
7640 ref_view_free_refs(struct tog_ref_view_state *s)
7642 struct tog_reflist_entry *re;
7644 while (!TAILQ_EMPTY(&s->refs)) {
7645 re = TAILQ_FIRST(&s->refs);
7646 TAILQ_REMOVE(&s->refs, re, entry);
7647 got_ref_close(re->ref);
7648 free(re);
7652 static const struct got_error *
7653 open_ref_view(struct tog_view *view, struct got_repository *repo)
7655 const struct got_error *err = NULL;
7656 struct tog_ref_view_state *s = &view->state.ref;
7658 s->selected_entry = 0;
7659 s->repo = repo;
7661 TAILQ_INIT(&s->refs);
7662 STAILQ_INIT(&s->colors);
7664 err = ref_view_load_refs(s);
7665 if (err)
7666 return err;
7668 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7669 err = add_color(&s->colors, "^refs/heads/",
7670 TOG_COLOR_REFS_HEADS,
7671 get_color_value("TOG_COLOR_REFS_HEADS"));
7672 if (err)
7673 goto done;
7675 err = add_color(&s->colors, "^refs/tags/",
7676 TOG_COLOR_REFS_TAGS,
7677 get_color_value("TOG_COLOR_REFS_TAGS"));
7678 if (err)
7679 goto done;
7681 err = add_color(&s->colors, "^refs/remotes/",
7682 TOG_COLOR_REFS_REMOTES,
7683 get_color_value("TOG_COLOR_REFS_REMOTES"));
7684 if (err)
7685 goto done;
7687 err = add_color(&s->colors, "^refs/got/backup/",
7688 TOG_COLOR_REFS_BACKUP,
7689 get_color_value("TOG_COLOR_REFS_BACKUP"));
7690 if (err)
7691 goto done;
7694 view->show = show_ref_view;
7695 view->input = input_ref_view;
7696 view->close = close_ref_view;
7697 view->search_start = search_start_ref_view;
7698 view->search_next = search_next_ref_view;
7699 done:
7700 if (err)
7701 free_colors(&s->colors);
7702 return err;
7705 static const struct got_error *
7706 close_ref_view(struct tog_view *view)
7708 struct tog_ref_view_state *s = &view->state.ref;
7710 ref_view_free_refs(s);
7711 free_colors(&s->colors);
7713 return NULL;
7716 static const struct got_error *
7717 resolve_reflist_entry(struct got_object_id **commit_id,
7718 struct tog_reflist_entry *re, struct got_repository *repo)
7720 const struct got_error *err = NULL;
7721 struct got_object_id *obj_id;
7722 struct got_tag_object *tag = NULL;
7723 int obj_type;
7725 *commit_id = NULL;
7727 err = got_ref_resolve(&obj_id, repo, re->ref);
7728 if (err)
7729 return err;
7731 err = got_object_get_type(&obj_type, repo, obj_id);
7732 if (err)
7733 goto done;
7735 switch (obj_type) {
7736 case GOT_OBJ_TYPE_COMMIT:
7737 *commit_id = obj_id;
7738 break;
7739 case GOT_OBJ_TYPE_TAG:
7740 err = got_object_open_as_tag(&tag, repo, obj_id);
7741 if (err)
7742 goto done;
7743 free(obj_id);
7744 err = got_object_get_type(&obj_type, repo,
7745 got_object_tag_get_object_id(tag));
7746 if (err)
7747 goto done;
7748 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7749 err = got_error(GOT_ERR_OBJ_TYPE);
7750 goto done;
7752 *commit_id = got_object_id_dup(
7753 got_object_tag_get_object_id(tag));
7754 if (*commit_id == NULL) {
7755 err = got_error_from_errno("got_object_id_dup");
7756 goto done;
7758 break;
7759 default:
7760 err = got_error(GOT_ERR_OBJ_TYPE);
7761 break;
7764 done:
7765 if (tag)
7766 got_object_tag_close(tag);
7767 if (err) {
7768 free(*commit_id);
7769 *commit_id = NULL;
7771 return err;
7774 static const struct got_error *
7775 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7776 struct tog_reflist_entry *re, struct got_repository *repo)
7778 struct tog_view *log_view;
7779 const struct got_error *err = NULL;
7780 struct got_object_id *commit_id = NULL;
7782 *new_view = NULL;
7784 err = resolve_reflist_entry(&commit_id, re, repo);
7785 if (err) {
7786 if (err->code != GOT_ERR_OBJ_TYPE)
7787 return err;
7788 else
7789 return NULL;
7792 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7793 if (log_view == NULL) {
7794 err = got_error_from_errno("view_open");
7795 goto done;
7798 err = open_log_view(log_view, commit_id, repo,
7799 got_ref_get_name(re->ref), "", 0);
7800 done:
7801 if (err)
7802 view_close(log_view);
7803 else
7804 *new_view = log_view;
7805 free(commit_id);
7806 return err;
7809 static void
7810 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7812 struct tog_reflist_entry *re;
7813 int i = 0;
7815 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7816 return;
7818 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7819 while (i++ < maxscroll) {
7820 if (re == NULL)
7821 break;
7822 s->first_displayed_entry = re;
7823 re = TAILQ_PREV(re, tog_reflist_head, entry);
7827 static const struct got_error *
7828 ref_scroll_down(struct tog_view *view, int maxscroll)
7830 struct tog_ref_view_state *s = &view->state.ref;
7831 struct tog_reflist_entry *next, *last;
7832 int n = 0;
7834 if (s->first_displayed_entry)
7835 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7836 else
7837 next = TAILQ_FIRST(&s->refs);
7839 last = s->last_displayed_entry;
7840 while (next && n++ < maxscroll) {
7841 if (last) {
7842 s->last_displayed_entry = last;
7843 last = TAILQ_NEXT(last, entry);
7845 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7846 s->first_displayed_entry = next;
7847 next = TAILQ_NEXT(next, entry);
7851 return NULL;
7854 static const struct got_error *
7855 search_start_ref_view(struct tog_view *view)
7857 struct tog_ref_view_state *s = &view->state.ref;
7859 s->matched_entry = NULL;
7860 return NULL;
7863 static int
7864 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7866 regmatch_t regmatch;
7868 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7869 0) == 0;
7872 static const struct got_error *
7873 search_next_ref_view(struct tog_view *view)
7875 struct tog_ref_view_state *s = &view->state.ref;
7876 struct tog_reflist_entry *re = NULL;
7878 if (!view->searching) {
7879 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7880 return NULL;
7883 if (s->matched_entry) {
7884 if (view->searching == TOG_SEARCH_FORWARD) {
7885 if (s->selected_entry)
7886 re = TAILQ_NEXT(s->selected_entry, entry);
7887 else
7888 re = TAILQ_PREV(s->selected_entry,
7889 tog_reflist_head, entry);
7890 } else {
7891 if (s->selected_entry == NULL)
7892 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7893 else
7894 re = TAILQ_PREV(s->selected_entry,
7895 tog_reflist_head, entry);
7897 } else {
7898 if (s->selected_entry)
7899 re = s->selected_entry;
7900 else if (view->searching == TOG_SEARCH_FORWARD)
7901 re = TAILQ_FIRST(&s->refs);
7902 else
7903 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7906 while (1) {
7907 if (re == NULL) {
7908 if (s->matched_entry == NULL) {
7909 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7910 return NULL;
7912 if (view->searching == TOG_SEARCH_FORWARD)
7913 re = TAILQ_FIRST(&s->refs);
7914 else
7915 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7918 if (match_reflist_entry(re, &view->regex)) {
7919 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7920 s->matched_entry = re;
7921 break;
7924 if (view->searching == TOG_SEARCH_FORWARD)
7925 re = TAILQ_NEXT(re, entry);
7926 else
7927 re = TAILQ_PREV(re, tog_reflist_head, entry);
7930 if (s->matched_entry) {
7931 s->first_displayed_entry = s->matched_entry;
7932 s->selected = 0;
7935 return NULL;
7938 static const struct got_error *
7939 show_ref_view(struct tog_view *view)
7941 const struct got_error *err = NULL;
7942 struct tog_ref_view_state *s = &view->state.ref;
7943 struct tog_reflist_entry *re;
7944 char *line = NULL;
7945 wchar_t *wline;
7946 struct tog_color *tc;
7947 int width, n;
7948 int limit = view->nlines;
7950 werase(view->window);
7952 s->ndisplayed = 0;
7953 if (view_is_hsplit_top(view))
7954 --limit; /* border */
7956 if (limit == 0)
7957 return NULL;
7959 re = s->first_displayed_entry;
7961 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7962 s->nrefs) == -1)
7963 return got_error_from_errno("asprintf");
7965 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7966 if (err) {
7967 free(line);
7968 return err;
7970 if (view_needs_focus_indication(view))
7971 wstandout(view->window);
7972 waddwstr(view->window, wline);
7973 while (width++ < view->ncols)
7974 waddch(view->window, ' ');
7975 if (view_needs_focus_indication(view))
7976 wstandend(view->window);
7977 free(wline);
7978 wline = NULL;
7979 free(line);
7980 line = NULL;
7981 if (--limit <= 0)
7982 return NULL;
7984 n = 0;
7985 while (re && limit > 0) {
7986 char *line = NULL;
7987 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7989 if (s->show_date) {
7990 struct got_commit_object *ci;
7991 struct got_tag_object *tag;
7992 struct got_object_id *id;
7993 struct tm tm;
7994 time_t t;
7996 err = got_ref_resolve(&id, s->repo, re->ref);
7997 if (err)
7998 return err;
7999 err = got_object_open_as_tag(&tag, s->repo, id);
8000 if (err) {
8001 if (err->code != GOT_ERR_OBJ_TYPE) {
8002 free(id);
8003 return err;
8005 err = got_object_open_as_commit(&ci, s->repo,
8006 id);
8007 if (err) {
8008 free(id);
8009 return err;
8011 t = got_object_commit_get_committer_time(ci);
8012 got_object_commit_close(ci);
8013 } else {
8014 t = got_object_tag_get_tagger_time(tag);
8015 got_object_tag_close(tag);
8017 free(id);
8018 if (gmtime_r(&t, &tm) == NULL)
8019 return got_error_from_errno("gmtime_r");
8020 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
8021 return got_error(GOT_ERR_NO_SPACE);
8023 if (got_ref_is_symbolic(re->ref)) {
8024 if (asprintf(&line, "%s%s -> %s", s->show_date ?
8025 ymd : "", got_ref_get_name(re->ref),
8026 got_ref_get_symref_target(re->ref)) == -1)
8027 return got_error_from_errno("asprintf");
8028 } else if (s->show_ids) {
8029 struct got_object_id *id;
8030 char *id_str;
8031 err = got_ref_resolve(&id, s->repo, re->ref);
8032 if (err)
8033 return err;
8034 err = got_object_id_str(&id_str, id);
8035 if (err) {
8036 free(id);
8037 return err;
8039 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
8040 got_ref_get_name(re->ref), id_str) == -1) {
8041 err = got_error_from_errno("asprintf");
8042 free(id);
8043 free(id_str);
8044 return err;
8046 free(id);
8047 free(id_str);
8048 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
8049 got_ref_get_name(re->ref)) == -1)
8050 return got_error_from_errno("asprintf");
8052 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
8053 0, 0);
8054 if (err) {
8055 free(line);
8056 return err;
8058 if (n == s->selected) {
8059 if (view->focussed)
8060 wstandout(view->window);
8061 s->selected_entry = re;
8063 tc = match_color(&s->colors, got_ref_get_name(re->ref));
8064 if (tc)
8065 wattr_on(view->window,
8066 COLOR_PAIR(tc->colorpair), NULL);
8067 waddwstr(view->window, wline);
8068 if (tc)
8069 wattr_off(view->window,
8070 COLOR_PAIR(tc->colorpair), NULL);
8071 if (width < view->ncols - 1)
8072 waddch(view->window, '\n');
8073 if (n == s->selected && view->focussed)
8074 wstandend(view->window);
8075 free(line);
8076 free(wline);
8077 wline = NULL;
8078 n++;
8079 s->ndisplayed++;
8080 s->last_displayed_entry = re;
8082 limit--;
8083 re = TAILQ_NEXT(re, entry);
8086 view_border(view);
8087 return err;
8090 static const struct got_error *
8091 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
8092 struct tog_reflist_entry *re, struct got_repository *repo)
8094 const struct got_error *err = NULL;
8095 struct got_object_id *commit_id = NULL;
8096 struct tog_view *tree_view;
8098 *new_view = NULL;
8100 err = resolve_reflist_entry(&commit_id, re, repo);
8101 if (err) {
8102 if (err->code != GOT_ERR_OBJ_TYPE)
8103 return err;
8104 else
8105 return NULL;
8109 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8110 if (tree_view == NULL) {
8111 err = got_error_from_errno("view_open");
8112 goto done;
8115 err = open_tree_view(tree_view, commit_id,
8116 got_ref_get_name(re->ref), repo);
8117 if (err)
8118 goto done;
8120 *new_view = tree_view;
8121 done:
8122 free(commit_id);
8123 return err;
8126 static const struct got_error *
8127 ref_goto_line(struct tog_view *view, int nlines)
8129 const struct got_error *err = NULL;
8130 struct tog_ref_view_state *s = &view->state.ref;
8131 int g, idx = s->selected_entry->idx;
8133 g = view->gline;
8134 view->gline = 0;
8136 if (g == 0)
8137 g = 1;
8138 else if (g > s->nrefs)
8139 g = s->nrefs;
8141 if (g >= s->first_displayed_entry->idx + 1 &&
8142 g <= s->last_displayed_entry->idx + 1 &&
8143 g - s->first_displayed_entry->idx - 1 < nlines) {
8144 s->selected = g - s->first_displayed_entry->idx - 1;
8145 return NULL;
8148 if (idx + 1 < g) {
8149 err = ref_scroll_down(view, g - idx - 1);
8150 if (err)
8151 return err;
8152 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8153 s->first_displayed_entry->idx + s->selected < g &&
8154 s->selected < s->ndisplayed - 1)
8155 s->selected = g - s->first_displayed_entry->idx - 1;
8156 } else if (idx + 1 > g)
8157 ref_scroll_up(s, idx - g + 1);
8159 if (g < nlines && s->first_displayed_entry->idx == 0)
8160 s->selected = g - 1;
8162 return NULL;
8166 static const struct got_error *
8167 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8169 const struct got_error *err = NULL;
8170 struct tog_ref_view_state *s = &view->state.ref;
8171 struct tog_reflist_entry *re;
8172 int n, nscroll = view->nlines - 1;
8174 if (view->gline)
8175 return ref_goto_line(view, nscroll);
8177 switch (ch) {
8178 case 'i':
8179 s->show_ids = !s->show_ids;
8180 view->count = 0;
8181 break;
8182 case 'm':
8183 s->show_date = !s->show_date;
8184 view->count = 0;
8185 break;
8186 case 'o':
8187 s->sort_by_date = !s->sort_by_date;
8188 view->count = 0;
8189 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8190 got_ref_cmp_by_commit_timestamp_descending :
8191 tog_ref_cmp_by_name, s->repo);
8192 if (err)
8193 break;
8194 got_reflist_object_id_map_free(tog_refs_idmap);
8195 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8196 &tog_refs, s->repo);
8197 if (err)
8198 break;
8199 ref_view_free_refs(s);
8200 err = ref_view_load_refs(s);
8201 break;
8202 case KEY_ENTER:
8203 case '\r':
8204 view->count = 0;
8205 if (!s->selected_entry)
8206 break;
8207 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8208 break;
8209 case 'T':
8210 view->count = 0;
8211 if (!s->selected_entry)
8212 break;
8213 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8214 break;
8215 case 'g':
8216 case KEY_HOME:
8217 s->selected = 0;
8218 view->count = 0;
8219 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8220 break;
8221 case 'G':
8222 case KEY_END: {
8223 int eos = view->nlines - 1;
8225 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8226 --eos; /* border */
8227 s->selected = 0;
8228 view->count = 0;
8229 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8230 for (n = 0; n < eos; n++) {
8231 if (re == NULL)
8232 break;
8233 s->first_displayed_entry = re;
8234 re = TAILQ_PREV(re, tog_reflist_head, entry);
8236 if (n > 0)
8237 s->selected = n - 1;
8238 break;
8240 case 'k':
8241 case KEY_UP:
8242 case CTRL('p'):
8243 if (s->selected > 0) {
8244 s->selected--;
8245 break;
8247 ref_scroll_up(s, 1);
8248 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8249 view->count = 0;
8250 break;
8251 case CTRL('u'):
8252 case 'u':
8253 nscroll /= 2;
8254 /* FALL THROUGH */
8255 case KEY_PPAGE:
8256 case CTRL('b'):
8257 case 'b':
8258 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8259 s->selected -= MIN(nscroll, s->selected);
8260 ref_scroll_up(s, MAX(0, nscroll));
8261 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8262 view->count = 0;
8263 break;
8264 case 'j':
8265 case KEY_DOWN:
8266 case CTRL('n'):
8267 if (s->selected < s->ndisplayed - 1) {
8268 s->selected++;
8269 break;
8271 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8272 /* can't scroll any further */
8273 view->count = 0;
8274 break;
8276 ref_scroll_down(view, 1);
8277 break;
8278 case CTRL('d'):
8279 case 'd':
8280 nscroll /= 2;
8281 /* FALL THROUGH */
8282 case KEY_NPAGE:
8283 case CTRL('f'):
8284 case 'f':
8285 case ' ':
8286 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8287 /* can't scroll any further; move cursor down */
8288 if (s->selected < s->ndisplayed - 1)
8289 s->selected += MIN(nscroll,
8290 s->ndisplayed - s->selected - 1);
8291 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8292 s->selected += s->ndisplayed - s->selected - 1;
8293 view->count = 0;
8294 break;
8296 ref_scroll_down(view, nscroll);
8297 break;
8298 case CTRL('l'):
8299 view->count = 0;
8300 tog_free_refs();
8301 err = tog_load_refs(s->repo, s->sort_by_date);
8302 if (err)
8303 break;
8304 ref_view_free_refs(s);
8305 err = ref_view_load_refs(s);
8306 break;
8307 case KEY_RESIZE:
8308 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8309 s->selected = view->nlines - 2;
8310 break;
8311 default:
8312 view->count = 0;
8313 break;
8316 return err;
8319 __dead static void
8320 usage_ref(void)
8322 endwin();
8323 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8324 getprogname());
8325 exit(1);
8328 static const struct got_error *
8329 cmd_ref(int argc, char *argv[])
8331 const struct got_error *error;
8332 struct got_repository *repo = NULL;
8333 struct got_worktree *worktree = NULL;
8334 char *cwd = NULL, *repo_path = NULL;
8335 int ch;
8336 struct tog_view *view;
8337 int *pack_fds = NULL;
8339 while ((ch = getopt(argc, argv, "r:")) != -1) {
8340 switch (ch) {
8341 case 'r':
8342 repo_path = realpath(optarg, NULL);
8343 if (repo_path == NULL)
8344 return got_error_from_errno2("realpath",
8345 optarg);
8346 break;
8347 default:
8348 usage_ref();
8349 /* NOTREACHED */
8353 argc -= optind;
8354 argv += optind;
8356 if (argc > 1)
8357 usage_ref();
8359 error = got_repo_pack_fds_open(&pack_fds);
8360 if (error != NULL)
8361 goto done;
8363 if (repo_path == NULL) {
8364 cwd = getcwd(NULL, 0);
8365 if (cwd == NULL)
8366 return got_error_from_errno("getcwd");
8367 error = got_worktree_open(&worktree, cwd);
8368 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8369 goto done;
8370 if (worktree)
8371 repo_path =
8372 strdup(got_worktree_get_repo_path(worktree));
8373 else
8374 repo_path = strdup(cwd);
8375 if (repo_path == NULL) {
8376 error = got_error_from_errno("strdup");
8377 goto done;
8381 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8382 if (error != NULL)
8383 goto done;
8385 init_curses();
8387 error = apply_unveil(got_repo_get_path(repo), NULL);
8388 if (error)
8389 goto done;
8391 error = tog_load_refs(repo, 0);
8392 if (error)
8393 goto done;
8395 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8396 if (view == NULL) {
8397 error = got_error_from_errno("view_open");
8398 goto done;
8401 error = open_ref_view(view, repo);
8402 if (error)
8403 goto done;
8405 if (worktree) {
8406 /* Release work tree lock. */
8407 got_worktree_close(worktree);
8408 worktree = NULL;
8410 error = view_loop(view);
8411 done:
8412 free(repo_path);
8413 free(cwd);
8414 if (repo) {
8415 const struct got_error *close_err = got_repo_close(repo);
8416 if (close_err)
8417 error = close_err;
8419 if (pack_fds) {
8420 const struct got_error *pack_err =
8421 got_repo_pack_fds_close(pack_fds);
8422 if (error == NULL)
8423 error = pack_err;
8425 tog_free_refs();
8426 return error;
8429 static const struct got_error*
8430 win_draw_center(WINDOW *win, size_t y, size_t x, size_t maxx, int focus,
8431 const char *str)
8433 size_t len;
8435 if (win == NULL)
8436 win = stdscr;
8438 len = strlen(str);
8439 x = x ? x : maxx > len ? (maxx - len) / 2 : 0;
8441 if (focus)
8442 wstandout(win);
8443 if (mvwprintw(win, y, x, "%s", str) == ERR)
8444 return got_error_msg(GOT_ERR_RANGE, "mvwprintw");
8445 if (focus)
8446 wstandend(win);
8448 return NULL;
8451 static const struct got_error *
8452 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
8454 off_t *p;
8456 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
8457 if (p == NULL) {
8458 free(*line_offsets);
8459 *line_offsets = NULL;
8460 return got_error_from_errno("reallocarray");
8463 *line_offsets = p;
8464 (*line_offsets)[*nlines] = off;
8465 ++(*nlines);
8466 return NULL;
8469 static const struct got_error *
8470 max_key_str(int *ret, const struct tog_key_map *km, size_t n)
8472 *ret = 0;
8474 for (;n > 0; --n, ++km) {
8475 char *t0, *t, *k;
8476 size_t len = 1;
8478 if (km->keys == NULL)
8479 continue;
8481 t = t0 = strdup(km->keys);
8482 if (t0 == NULL)
8483 return got_error_from_errno("strdup");
8485 len += strlen(t);
8486 while ((k = strsep(&t, " ")) != NULL)
8487 len += strlen(k) > 1 ? 2 : 0;
8488 free(t0);
8489 *ret = MAX(*ret, len);
8492 return NULL;
8496 * Write keymap section headers, keys, and key info in km to f.
8497 * Save line offset to *off. If terminal has UTF8 encoding enabled,
8498 * wrap control and symbolic keys in guillemets, else use <>.
8500 static const struct got_error *
8501 format_help_line(off_t *off, FILE *f, const struct tog_key_map *km, int width)
8503 int n, len = width;
8505 if (km->keys) {
8506 static const char *u8_glyph[] = {
8507 "\xe2\x80\xb9", /* U+2039 (utf8 <) */
8508 "\xe2\x80\xba" /* U+203A (utf8 >) */
8510 char *t0, *t, *k;
8511 int cs, s, first = 1;
8513 cs = got_locale_is_utf8();
8515 t = t0 = strdup(km->keys);
8516 if (t0 == NULL)
8517 return got_error_from_errno("strdup");
8519 len = strlen(km->keys);
8520 while ((k = strsep(&t, " ")) != NULL) {
8521 s = strlen(k) > 1; /* control or symbolic key */
8522 n = fprintf(f, "%s%s%s%s%s", first ? " " : "",
8523 cs && s ? u8_glyph[0] : s ? "<" : "", k,
8524 cs && s ? u8_glyph[1] : s ? ">" : "", t ? " " : "");
8525 if (n < 0) {
8526 free(t0);
8527 return got_error_from_errno("fprintf");
8529 first = 0;
8530 len += s ? 2 : 0;
8531 *off += n;
8533 free(t0);
8535 n = fprintf(f, "%*s%s\n", width - len, width - len ? " " : "", km->info);
8536 if (n < 0)
8537 return got_error_from_errno("fprintf");
8538 *off += n;
8540 return NULL;
8543 static const struct got_error *
8544 format_help(struct tog_help_view_state *s)
8546 const struct got_error *err = NULL;
8547 off_t off = 0;
8548 int i, max, n, show = s->all;
8549 static const struct tog_key_map km[] = {
8550 #define KEYMAP_(info, type) { NULL, (info), type }
8551 #define KEY_(keys, info) { (keys), (info), TOG_KEYMAP_KEYS }
8552 GENERATE_HELP
8553 #undef KEYMAP_
8554 #undef KEY_
8557 err = add_line_offset(&s->line_offsets, &s->nlines, 0);
8558 if (err)
8559 return err;
8561 n = nitems(km);
8562 err = max_key_str(&max, km, n);
8563 if (err)
8564 return err;
8566 for (i = 0; i < n; ++i) {
8567 if (km[i].keys == NULL) {
8568 show = s->all;
8569 if (km[i].type == TOG_KEYMAP_GLOBAL ||
8570 km[i].type == s->type || s->all)
8571 show = 1;
8573 if (show) {
8574 err = format_help_line(&off, s->f, &km[i], max);
8575 if (err)
8576 return err;
8577 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8578 if (err)
8579 return err;
8582 fputc('\n', s->f);
8583 ++off;
8584 err = add_line_offset(&s->line_offsets, &s->nlines, off);
8585 return err;
8588 static const struct got_error *
8589 create_help(struct tog_help_view_state *s)
8591 FILE *f;
8592 const struct got_error *err;
8594 free(s->line_offsets);
8595 s->line_offsets = NULL;
8596 s->nlines = 0;
8598 f = got_opentemp();
8599 if (f == NULL)
8600 return got_error_from_errno("got_opentemp");
8601 s->f = f;
8603 err = format_help(s);
8604 if (err)
8605 return err;
8607 if (s->f && fflush(s->f) != 0)
8608 return got_error_from_errno("fflush");
8610 return NULL;
8613 static const struct got_error *
8614 search_start_help_view(struct tog_view *view)
8616 view->state.help.matched_line = 0;
8617 return NULL;
8620 static void
8621 search_setup_help_view(struct tog_view *view, FILE **f, off_t **line_offsets,
8622 size_t *nlines, int **first, int **last, int **match, int **selected)
8624 struct tog_help_view_state *s = &view->state.help;
8626 *f = s->f;
8627 *nlines = s->nlines;
8628 *line_offsets = s->line_offsets;
8629 *match = &s->matched_line;
8630 *first = &s->first_displayed_line;
8631 *last = &s->last_displayed_line;
8632 *selected = &s->selected_line;
8635 static const struct got_error *
8636 show_help_view(struct tog_view *view)
8638 struct tog_help_view_state *s = &view->state.help;
8639 const struct got_error *err;
8640 regmatch_t *regmatch = &view->regmatch;
8641 wchar_t *wline;
8642 char *line;
8643 ssize_t linelen;
8644 size_t linesz = 0;
8645 int width, nprinted = 0, rc = 0;
8646 int eos = view->nlines;
8648 if (view_is_hsplit_top(view))
8649 --eos; /* account for border */
8651 s->lineno = 0;
8652 rewind(s->f);
8653 werase(view->window);
8655 if (view->gline > s->nlines - 1)
8656 view->gline = s->nlines - 1;
8658 err = win_draw_center(view->window, 0, 0, view->ncols,
8659 view_needs_focus_indication(view),
8660 "tog help (press q to return to tog)");
8661 if (err)
8662 return err;
8663 if (eos <= 1)
8664 return NULL;
8665 waddstr(view->window, "\n\n");
8666 eos -= 2;
8668 s->eof = 0;
8669 view->maxx = 0;
8670 line = NULL;
8671 while (eos > 0 && nprinted < eos) {
8672 attr_t attr = 0;
8674 linelen = getline(&line, &linesz, s->f);
8675 if (linelen == -1) {
8676 if (!feof(s->f)) {
8677 free(line);
8678 return got_ferror(s->f, GOT_ERR_IO);
8680 s->eof = 1;
8681 break;
8683 if (++s->lineno < s->first_displayed_line)
8684 continue;
8685 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
8686 continue;
8687 if (s->lineno == view->hiline)
8688 attr = A_STANDOUT;
8690 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
8691 view->x ? 1 : 0);
8692 if (err) {
8693 free(line);
8694 return err;
8696 view->maxx = MAX(view->maxx, width);
8697 free(wline);
8698 wline = NULL;
8700 if (attr)
8701 wattron(view->window, attr);
8702 if (s->first_displayed_line + nprinted == s->matched_line &&
8703 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
8704 err = add_matched_line(&width, line, view->ncols - 1, 0,
8705 view->window, view->x, regmatch);
8706 if (err) {
8707 free(line);
8708 return err;
8710 } else {
8711 int skip;
8713 err = format_line(&wline, &width, &skip, line,
8714 view->x, view->ncols - 1, 0, view->x ? 1 : 0);
8715 if (err) {
8716 free(line);
8717 return err;
8719 rc = waddwstr(view->window, &wline[skip]);
8720 free(wline);
8721 wline = NULL;
8722 if (rc == ERR)
8723 return got_error_msg(GOT_ERR_IO, "waddwstr");
8725 if (s->lineno == view->hiline) {
8726 while (width++ < view->ncols)
8727 waddch(view->window, ' ');
8728 } else {
8729 if (width <= view->ncols)
8730 waddch(view->window, '\n');
8732 if (attr)
8733 wattroff(view->window, attr);
8734 if (++nprinted == 1)
8735 s->first_displayed_line = s->lineno;
8737 free(line);
8738 if (nprinted > 0)
8739 s->last_displayed_line = s->first_displayed_line + nprinted - 1;
8740 else
8741 s->last_displayed_line = s->first_displayed_line;
8743 view_border(view);
8745 if (s->eof) {
8746 rc = waddnstr(view->window,
8747 "See the tog(1) manual page for full documentation",
8748 view->ncols - 1);
8749 if (rc == ERR)
8750 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8751 } else {
8752 wmove(view->window, view->nlines - 1, 0);
8753 wclrtoeol(view->window);
8754 wstandout(view->window);
8755 rc = waddnstr(view->window, "scroll down for more...",
8756 view->ncols - 1);
8757 if (rc == ERR)
8758 return got_error_msg(GOT_ERR_RANGE, "waddnstr");
8759 if (getcurx(view->window) < view->ncols - 6) {
8760 rc = wprintw(view->window, "[%.0f%%]",
8761 100.00 * s->last_displayed_line / s->nlines);
8762 if (rc == ERR)
8763 return got_error_msg(GOT_ERR_IO, "wprintw");
8765 wstandend(view->window);
8768 return NULL;
8771 static const struct got_error *
8772 input_help_view(struct tog_view **new_view, struct tog_view *view, int ch)
8774 struct tog_help_view_state *s = &view->state.help;
8775 const struct got_error *err = NULL;
8776 char *line = NULL;
8777 ssize_t linelen;
8778 size_t linesz = 0;
8779 int eos, nscroll;
8781 eos = nscroll = view->nlines;
8782 if (view_is_hsplit_top(view))
8783 --eos; /* border */
8785 s->lineno = s->first_displayed_line - 1 + s->selected_line;
8787 switch (ch) {
8788 case '0':
8789 view->x = 0;
8790 break;
8791 case '$':
8792 view->x = MAX(view->maxx - view->ncols / 3, 0);
8793 view->count = 0;
8794 break;
8795 case KEY_RIGHT:
8796 case 'l':
8797 if (view->x + view->ncols / 3 < view->maxx)
8798 view->x += 2;
8799 else
8800 view->count = 0;
8801 break;
8802 case KEY_LEFT:
8803 case 'h':
8804 view->x -= MIN(view->x, 2);
8805 if (view->x <= 0)
8806 view->count = 0;
8807 break;
8808 case 'g':
8809 case KEY_HOME:
8810 s->first_displayed_line = 1;
8811 view->count = 0;
8812 break;
8813 case 'G':
8814 case KEY_END:
8815 view->count = 0;
8816 if (s->eof)
8817 break;
8818 s->first_displayed_line = (s->nlines - eos) + 3;
8819 s->eof = 1;
8820 break;
8821 case 'k':
8822 case KEY_UP:
8823 if (s->first_displayed_line > 1)
8824 --s->first_displayed_line;
8825 else
8826 view->count = 0;
8827 break;
8828 case CTRL('u'):
8829 case 'u':
8830 nscroll /= 2;
8831 /* FALL THROUGH */
8832 case KEY_PPAGE:
8833 case CTRL('b'):
8834 case 'b':
8835 if (s->first_displayed_line == 1) {
8836 view->count = 0;
8837 break;
8839 while (--nscroll > 0 && s->first_displayed_line > 1)
8840 s->first_displayed_line--;
8841 break;
8842 case 'j':
8843 case KEY_DOWN:
8844 case CTRL('n'):
8845 if (!s->eof)
8846 ++s->first_displayed_line;
8847 else
8848 view->count = 0;
8849 break;
8850 case CTRL('d'):
8851 case 'd':
8852 nscroll /= 2;
8853 /* FALL THROUGH */
8854 case KEY_NPAGE:
8855 case CTRL('f'):
8856 case 'f':
8857 case ' ':
8858 if (s->eof) {
8859 view->count = 0;
8860 break;
8862 while (!s->eof && --nscroll > 0) {
8863 linelen = getline(&line, &linesz, s->f);
8864 s->first_displayed_line++;
8865 if (linelen == -1) {
8866 if (feof(s->f))
8867 s->eof = 1;
8868 else
8869 err = got_ferror(s->f, GOT_ERR_IO);
8870 break;
8873 free(line);
8874 break;
8875 default:
8876 view->count = 0;
8877 break;
8880 return err;
8883 static const struct got_error *
8884 close_help_view(struct tog_view *view)
8886 struct tog_help_view_state *s = &view->state.help;
8888 free(s->line_offsets);
8889 s->line_offsets = NULL;
8890 if (fclose(s->f) == EOF)
8891 return got_error_from_errno("fclose");
8893 return NULL;
8896 static const struct got_error *
8897 reset_help_view(struct tog_view *view)
8899 struct tog_help_view_state *s = &view->state.help;
8902 if (s->f && fclose(s->f) == EOF)
8903 return got_error_from_errno("fclose");
8905 wclear(view->window);
8906 view->count = 0;
8907 view->x = 0;
8908 s->all = !s->all;
8909 s->first_displayed_line = 1;
8910 s->last_displayed_line = view->nlines;
8911 s->matched_line = 0;
8913 return create_help(s);
8916 static const struct got_error *
8917 open_help_view(struct tog_view *view, struct tog_view *parent)
8919 const struct got_error *err = NULL;
8920 struct tog_help_view_state *s = &view->state.help;
8922 s->type = (enum tog_keymap_type)parent->type;
8923 s->first_displayed_line = 1;
8924 s->last_displayed_line = view->nlines;
8925 s->selected_line = 1;
8927 view->show = show_help_view;
8928 view->input = input_help_view;
8929 view->reset = reset_help_view;
8930 view->close = close_help_view;
8931 view->search_start = search_start_help_view;
8932 view->search_setup = search_setup_help_view;
8933 view->search_next = search_next_view_match;
8935 err = create_help(s);
8936 return err;
8939 static const struct got_error *
8940 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8941 enum tog_view_type request, int y, int x)
8943 const struct got_error *err = NULL;
8945 *new_view = NULL;
8947 switch (request) {
8948 case TOG_VIEW_DIFF:
8949 if (view->type == TOG_VIEW_LOG) {
8950 struct tog_log_view_state *s = &view->state.log;
8952 err = open_diff_view_for_commit(new_view, y, x,
8953 s->selected_entry->commit, s->selected_entry->id,
8954 view, s->repo);
8955 } else
8956 return got_error_msg(GOT_ERR_NOT_IMPL,
8957 "parent/child view pair not supported");
8958 break;
8959 case TOG_VIEW_BLAME:
8960 if (view->type == TOG_VIEW_TREE) {
8961 struct tog_tree_view_state *s = &view->state.tree;
8963 err = blame_tree_entry(new_view, y, x,
8964 s->selected_entry, &s->parents, s->commit_id,
8965 s->repo);
8966 } else
8967 return got_error_msg(GOT_ERR_NOT_IMPL,
8968 "parent/child view pair not supported");
8969 break;
8970 case TOG_VIEW_LOG:
8971 if (view->type == TOG_VIEW_BLAME)
8972 err = log_annotated_line(new_view, y, x,
8973 view->state.blame.repo, view->state.blame.id_to_log);
8974 else if (view->type == TOG_VIEW_TREE)
8975 err = log_selected_tree_entry(new_view, y, x,
8976 &view->state.tree);
8977 else if (view->type == TOG_VIEW_REF)
8978 err = log_ref_entry(new_view, y, x,
8979 view->state.ref.selected_entry,
8980 view->state.ref.repo);
8981 else
8982 return got_error_msg(GOT_ERR_NOT_IMPL,
8983 "parent/child view pair not supported");
8984 break;
8985 case TOG_VIEW_TREE:
8986 if (view->type == TOG_VIEW_LOG)
8987 err = browse_commit_tree(new_view, y, x,
8988 view->state.log.selected_entry,
8989 view->state.log.in_repo_path,
8990 view->state.log.head_ref_name,
8991 view->state.log.repo);
8992 else if (view->type == TOG_VIEW_REF)
8993 err = browse_ref_tree(new_view, y, x,
8994 view->state.ref.selected_entry,
8995 view->state.ref.repo);
8996 else
8997 return got_error_msg(GOT_ERR_NOT_IMPL,
8998 "parent/child view pair not supported");
8999 break;
9000 case TOG_VIEW_REF:
9001 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
9002 if (*new_view == NULL)
9003 return got_error_from_errno("view_open");
9004 if (view->type == TOG_VIEW_LOG)
9005 err = open_ref_view(*new_view, view->state.log.repo);
9006 else if (view->type == TOG_VIEW_TREE)
9007 err = open_ref_view(*new_view, view->state.tree.repo);
9008 else
9009 err = got_error_msg(GOT_ERR_NOT_IMPL,
9010 "parent/child view pair not supported");
9011 if (err)
9012 view_close(*new_view);
9013 break;
9014 case TOG_VIEW_HELP:
9015 *new_view = view_open(0, 0, 0, 0, TOG_VIEW_HELP);
9016 if (*new_view == NULL)
9017 return got_error_from_errno("view_open");
9018 err = open_help_view(*new_view, view);
9019 if (err)
9020 view_close(*new_view);
9021 break;
9022 default:
9023 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
9026 return err;
9030 * If view was scrolled down to move the selected line into view when opening a
9031 * horizontal split, scroll back up when closing the split/toggling fullscreen.
9033 static void
9034 offset_selection_up(struct tog_view *view)
9036 switch (view->type) {
9037 case TOG_VIEW_BLAME: {
9038 struct tog_blame_view_state *s = &view->state.blame;
9039 if (s->first_displayed_line == 1) {
9040 s->selected_line = MAX(s->selected_line - view->offset,
9041 1);
9042 break;
9044 if (s->first_displayed_line > view->offset)
9045 s->first_displayed_line -= view->offset;
9046 else
9047 s->first_displayed_line = 1;
9048 s->selected_line += view->offset;
9049 break;
9051 case TOG_VIEW_LOG:
9052 log_scroll_up(&view->state.log, view->offset);
9053 view->state.log.selected += view->offset;
9054 break;
9055 case TOG_VIEW_REF:
9056 ref_scroll_up(&view->state.ref, view->offset);
9057 view->state.ref.selected += view->offset;
9058 break;
9059 case TOG_VIEW_TREE:
9060 tree_scroll_up(&view->state.tree, view->offset);
9061 view->state.tree.selected += view->offset;
9062 break;
9063 default:
9064 break;
9067 view->offset = 0;
9071 * If the selected line is in the section of screen covered by the bottom split,
9072 * scroll down offset lines to move it into view and index its new position.
9074 static const struct got_error *
9075 offset_selection_down(struct tog_view *view)
9077 const struct got_error *err = NULL;
9078 const struct got_error *(*scrolld)(struct tog_view *, int);
9079 int *selected = NULL;
9080 int header, offset;
9082 switch (view->type) {
9083 case TOG_VIEW_BLAME: {
9084 struct tog_blame_view_state *s = &view->state.blame;
9085 header = 3;
9086 scrolld = NULL;
9087 if (s->selected_line > view->nlines - header) {
9088 offset = abs(view->nlines - s->selected_line - header);
9089 s->first_displayed_line += offset;
9090 s->selected_line -= offset;
9091 view->offset = offset;
9093 break;
9095 case TOG_VIEW_LOG: {
9096 struct tog_log_view_state *s = &view->state.log;
9097 scrolld = &log_scroll_down;
9098 header = view_is_parent_view(view) ? 3 : 2;
9099 selected = &s->selected;
9100 break;
9102 case TOG_VIEW_REF: {
9103 struct tog_ref_view_state *s = &view->state.ref;
9104 scrolld = &ref_scroll_down;
9105 header = 3;
9106 selected = &s->selected;
9107 break;
9109 case TOG_VIEW_TREE: {
9110 struct tog_tree_view_state *s = &view->state.tree;
9111 scrolld = &tree_scroll_down;
9112 header = 5;
9113 selected = &s->selected;
9114 break;
9116 default:
9117 selected = NULL;
9118 scrolld = NULL;
9119 header = 0;
9120 break;
9123 if (selected && *selected > view->nlines - header) {
9124 offset = abs(view->nlines - *selected - header);
9125 view->offset = offset;
9126 if (scrolld && offset) {
9127 err = scrolld(view, offset);
9128 *selected -= offset;
9132 return err;
9135 static void
9136 list_commands(FILE *fp)
9138 size_t i;
9140 fprintf(fp, "commands:");
9141 for (i = 0; i < nitems(tog_commands); i++) {
9142 const struct tog_cmd *cmd = &tog_commands[i];
9143 fprintf(fp, " %s", cmd->name);
9145 fputc('\n', fp);
9148 __dead static void
9149 usage(int hflag, int status)
9151 FILE *fp = (status == 0) ? stdout : stderr;
9153 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
9154 getprogname());
9155 if (hflag) {
9156 fprintf(fp, "lazy usage: %s path\n", getprogname());
9157 list_commands(fp);
9159 exit(status);
9162 static char **
9163 make_argv(int argc, ...)
9165 va_list ap;
9166 char **argv;
9167 int i;
9169 va_start(ap, argc);
9171 argv = calloc(argc, sizeof(char *));
9172 if (argv == NULL)
9173 err(1, "calloc");
9174 for (i = 0; i < argc; i++) {
9175 argv[i] = strdup(va_arg(ap, char *));
9176 if (argv[i] == NULL)
9177 err(1, "strdup");
9180 va_end(ap);
9181 return argv;
9185 * Try to convert 'tog path' into a 'tog log path' command.
9186 * The user could simply have mistyped the command rather than knowingly
9187 * provided a path. So check whether argv[0] can in fact be resolved
9188 * to a path in the HEAD commit and print a special error if not.
9189 * This hack is for mpi@ <3
9191 static const struct got_error *
9192 tog_log_with_path(int argc, char *argv[])
9194 const struct got_error *error = NULL, *close_err;
9195 const struct tog_cmd *cmd = NULL;
9196 struct got_repository *repo = NULL;
9197 struct got_worktree *worktree = NULL;
9198 struct got_object_id *commit_id = NULL, *id = NULL;
9199 struct got_commit_object *commit = NULL;
9200 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
9201 char *commit_id_str = NULL, **cmd_argv = NULL;
9202 int *pack_fds = NULL;
9204 cwd = getcwd(NULL, 0);
9205 if (cwd == NULL)
9206 return got_error_from_errno("getcwd");
9208 error = got_repo_pack_fds_open(&pack_fds);
9209 if (error != NULL)
9210 goto done;
9212 error = got_worktree_open(&worktree, cwd);
9213 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9214 goto done;
9216 if (worktree)
9217 repo_path = strdup(got_worktree_get_repo_path(worktree));
9218 else
9219 repo_path = strdup(cwd);
9220 if (repo_path == NULL) {
9221 error = got_error_from_errno("strdup");
9222 goto done;
9225 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9226 if (error != NULL)
9227 goto done;
9229 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
9230 repo, worktree);
9231 if (error)
9232 goto done;
9234 error = tog_load_refs(repo, 0);
9235 if (error)
9236 goto done;
9237 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
9238 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
9239 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
9240 if (error)
9241 goto done;
9243 if (worktree) {
9244 got_worktree_close(worktree);
9245 worktree = NULL;
9248 error = got_object_open_as_commit(&commit, repo, commit_id);
9249 if (error)
9250 goto done;
9252 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
9253 if (error) {
9254 if (error->code != GOT_ERR_NO_TREE_ENTRY)
9255 goto done;
9256 fprintf(stderr, "%s: '%s' is no known command or path\n",
9257 getprogname(), argv[0]);
9258 usage(1, 1);
9259 /* not reached */
9262 error = got_object_id_str(&commit_id_str, commit_id);
9263 if (error)
9264 goto done;
9266 cmd = &tog_commands[0]; /* log */
9267 argc = 4;
9268 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
9269 error = cmd->cmd_main(argc, cmd_argv);
9270 done:
9271 if (repo) {
9272 close_err = got_repo_close(repo);
9273 if (error == NULL)
9274 error = close_err;
9276 if (commit)
9277 got_object_commit_close(commit);
9278 if (worktree)
9279 got_worktree_close(worktree);
9280 if (pack_fds) {
9281 const struct got_error *pack_err =
9282 got_repo_pack_fds_close(pack_fds);
9283 if (error == NULL)
9284 error = pack_err;
9286 free(id);
9287 free(commit_id_str);
9288 free(commit_id);
9289 free(cwd);
9290 free(repo_path);
9291 free(in_repo_path);
9292 if (cmd_argv) {
9293 int i;
9294 for (i = 0; i < argc; i++)
9295 free(cmd_argv[i]);
9296 free(cmd_argv);
9298 tog_free_refs();
9299 return error;
9302 int
9303 main(int argc, char *argv[])
9305 const struct got_error *error = NULL;
9306 const struct tog_cmd *cmd = NULL;
9307 int ch, hflag = 0, Vflag = 0;
9308 char **cmd_argv = NULL;
9309 static const struct option longopts[] = {
9310 { "version", no_argument, NULL, 'V' },
9311 { NULL, 0, NULL, 0}
9313 char *diff_algo_str = NULL;
9315 if (!isatty(STDIN_FILENO))
9316 errx(1, "standard input is not a tty");
9318 setlocale(LC_CTYPE, "");
9320 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
9321 switch (ch) {
9322 case 'h':
9323 hflag = 1;
9324 break;
9325 case 'V':
9326 Vflag = 1;
9327 break;
9328 default:
9329 usage(hflag, 1);
9330 /* NOTREACHED */
9334 argc -= optind;
9335 argv += optind;
9336 optind = 1;
9337 optreset = 1;
9339 if (Vflag) {
9340 got_version_print_str();
9341 return 0;
9344 #ifndef PROFILE
9345 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
9346 NULL) == -1)
9347 err(1, "pledge");
9348 #endif
9350 if (argc == 0) {
9351 if (hflag)
9352 usage(hflag, 0);
9353 /* Build an argument vector which runs a default command. */
9354 cmd = &tog_commands[0];
9355 argc = 1;
9356 cmd_argv = make_argv(argc, cmd->name);
9357 } else {
9358 size_t i;
9360 /* Did the user specify a command? */
9361 for (i = 0; i < nitems(tog_commands); i++) {
9362 if (strncmp(tog_commands[i].name, argv[0],
9363 strlen(argv[0])) == 0) {
9364 cmd = &tog_commands[i];
9365 break;
9370 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
9371 if (diff_algo_str) {
9372 if (strcasecmp(diff_algo_str, "patience") == 0)
9373 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
9374 if (strcasecmp(diff_algo_str, "myers") == 0)
9375 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
9378 if (cmd == NULL) {
9379 if (argc != 1)
9380 usage(0, 1);
9381 /* No command specified; try log with a path */
9382 error = tog_log_with_path(argc, argv);
9383 } else {
9384 if (hflag)
9385 cmd->cmd_usage();
9386 else
9387 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
9390 endwin();
9391 putchar('\n');
9392 if (cmd_argv) {
9393 int i;
9394 for (i = 0; i < argc; i++)
9395 free(cmd_argv[i]);
9396 free(cmd_argv);
9399 if (error && error->code != GOT_ERR_CANCELLED &&
9400 error->code != GOT_ERR_EOF &&
9401 error->code != GOT_ERR_PRIVSEP_EXIT &&
9402 error->code != GOT_ERR_PRIVSEP_PIPE &&
9403 !(error->code == GOT_ERR_ERRNO && errno == EINTR))
9404 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
9405 return 0;