Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 enum tog_view_mode {
109 TOG_VIEW_SPLIT_NONE,
110 TOG_VIEW_SPLIT_VERT,
111 TOG_VIEW_SPLIT_HRZN
112 };
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
122 int idx;
123 };
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
126 int ncommits;
127 struct commit_queue_head head;
128 };
130 struct tog_color {
131 STAILQ_ENTRY(tog_color) entry;
132 regex_t regex;
133 short colorpair;
134 };
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
153 *cmp = -1;
154 return NULL;
155 } else if (isbackup1 && !isbackup2) {
156 *cmp = 1;
157 return NULL;
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 return NULL;
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 repo);
172 if (err)
173 return err;
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 repo);
179 static void
180 tog_free_refs(void)
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
195 int regerr = 0;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 return NULL;
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
203 if (tc == NULL)
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 if (regerr) {
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
211 sizeof(regerr_msg));
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 regerr_msg);
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 free(tc);
216 return err;
218 tc->colorpair = idx;
219 STAILQ_INSERT_HEAD(colors, tc, entry);
220 return NULL;
223 static void
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
231 regfree(&tc->regex);
232 free(tc);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
243 return tc;
246 return NULL;
249 static int
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 return COLOR_CYAN;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 return COLOR_YELLOW;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 return COLOR_GREEN;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 return COLOR_CYAN;
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 return COLOR_GREEN;
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 return COLOR_CYAN;
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 return COLOR_YELLOW;
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 return COLOR_GREEN;
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 return COLOR_YELLOW;
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 return COLOR_CYAN;
283 return -1;
286 static int
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
291 if (val == NULL)
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
295 return COLOR_BLACK;
296 if (strcasecmp(val, "red") == 0)
297 return COLOR_RED;
298 if (strcasecmp(val, "green") == 0)
299 return COLOR_GREEN;
300 if (strcasecmp(val, "yellow") == 0)
301 return COLOR_YELLOW;
302 if (strcasecmp(val, "blue") == 0)
303 return COLOR_BLUE;
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
307 return COLOR_CYAN;
308 if (strcasecmp(val, "white") == 0)
309 return COLOR_WHITE;
310 if (strcasecmp(val, "default") == 0)
311 return -1;
313 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
319 FILE *f, *f1, *f2;
320 int fd1, fd2;
321 int lineno;
322 int first_displayed_line;
323 int last_displayed_line;
324 int eof;
325 int diff_context;
326 int ignore_whitespace;
327 int force_text_diff;
328 struct got_repository *repo;
329 struct got_diff_line *lines;
330 size_t nlines;
331 int matched_line;
332 int selected_line;
334 /* passed from log or blame view; may be NULL */
335 struct tog_view *parent_view;
336 };
338 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 static volatile sig_atomic_t tog_thread_error;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
344 int commits_needed;
345 int load_all;
346 struct got_commit_graph *graph;
347 struct commit_queue *real_commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
351 int *pack_fds;
352 int log_complete;
353 sig_atomic_t *quit;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
356 int *searching;
357 int *search_next_done;
358 regex_t *regex;
359 int *limiting;
360 int limit_match;
361 regex_t *limit_regex;
362 struct commit_queue *limit_commits;
363 };
365 struct tog_log_view_state {
366 struct commit_queue *commits;
367 struct commit_queue_entry *first_displayed_entry;
368 struct commit_queue_entry *last_displayed_entry;
369 struct commit_queue_entry *selected_entry;
370 struct commit_queue real_commits;
371 int selected;
372 char *in_repo_path;
373 char *head_ref_name;
374 int log_branches;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
377 sig_atomic_t quit;
378 pthread_t thread;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
383 int use_committer;
384 int limit_view;
385 regex_t limit_regex;
386 struct commit_queue limit_commits;
387 };
389 #define TOG_COLOR_DIFF_MINUS 1
390 #define TOG_COLOR_DIFF_PLUS 2
391 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
392 #define TOG_COLOR_DIFF_META 4
393 #define TOG_COLOR_TREE_SUBMODULE 5
394 #define TOG_COLOR_TREE_SYMLINK 6
395 #define TOG_COLOR_TREE_DIRECTORY 7
396 #define TOG_COLOR_TREE_EXECUTABLE 8
397 #define TOG_COLOR_COMMIT 9
398 #define TOG_COLOR_AUTHOR 10
399 #define TOG_COLOR_DATE 11
400 #define TOG_COLOR_REFS_HEADS 12
401 #define TOG_COLOR_REFS_TAGS 13
402 #define TOG_COLOR_REFS_REMOTES 14
403 #define TOG_COLOR_REFS_BACKUP 15
405 struct tog_blame_cb_args {
406 struct tog_blame_line *lines; /* one per line */
407 int nlines;
409 struct tog_view *view;
410 struct got_object_id *commit_id;
411 int *quit;
412 };
414 struct tog_blame_thread_args {
415 const char *path;
416 struct got_repository *repo;
417 struct tog_blame_cb_args *cb_args;
418 int *complete;
419 got_cancel_cb cancel_cb;
420 void *cancel_arg;
421 };
423 struct tog_blame {
424 FILE *f;
425 off_t filesize;
426 struct tog_blame_line *lines;
427 int nlines;
428 off_t *line_offsets;
429 pthread_t thread;
430 struct tog_blame_thread_args thread_args;
431 struct tog_blame_cb_args cb_args;
432 const char *path;
433 int *pack_fds;
434 };
436 struct tog_blame_view_state {
437 int first_displayed_line;
438 int last_displayed_line;
439 int selected_line;
440 int last_diffed_line;
441 int blame_complete;
442 int eof;
443 int done;
444 struct got_object_id_queue blamed_commits;
445 struct got_object_qid *blamed_commit;
446 char *path;
447 struct got_repository *repo;
448 struct got_object_id *commit_id;
449 struct got_object_id *id_to_log;
450 struct tog_blame blame;
451 int matched_line;
452 struct tog_colors colors;
453 };
455 struct tog_parent_tree {
456 TAILQ_ENTRY(tog_parent_tree) entry;
457 struct got_tree_object *tree;
458 struct got_tree_entry *first_displayed_entry;
459 struct got_tree_entry *selected_entry;
460 int selected;
461 };
463 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
465 struct tog_tree_view_state {
466 char *tree_label;
467 struct got_object_id *commit_id;/* commit which this tree belongs to */
468 struct got_tree_object *root; /* the commit's root tree entry */
469 struct got_tree_object *tree; /* currently displayed (sub-)tree */
470 struct got_tree_entry *first_displayed_entry;
471 struct got_tree_entry *last_displayed_entry;
472 struct got_tree_entry *selected_entry;
473 int ndisplayed, selected, show_ids;
474 struct tog_parent_trees parents; /* parent trees of current sub-tree */
475 char *head_ref_name;
476 struct got_repository *repo;
477 struct got_tree_entry *matched_entry;
478 struct tog_colors colors;
479 };
481 struct tog_reflist_entry {
482 TAILQ_ENTRY(tog_reflist_entry) entry;
483 struct got_reference *ref;
484 int idx;
485 };
487 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
489 struct tog_ref_view_state {
490 struct tog_reflist_head refs;
491 struct tog_reflist_entry *first_displayed_entry;
492 struct tog_reflist_entry *last_displayed_entry;
493 struct tog_reflist_entry *selected_entry;
494 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
495 struct got_repository *repo;
496 struct tog_reflist_entry *matched_entry;
497 struct tog_colors colors;
498 };
500 /*
501 * We implement two types of views: parent views and child views.
503 * The 'Tab' key switches focus between a parent view and its child view.
504 * Child views are shown side-by-side to their parent view, provided
505 * there is enough screen estate.
507 * When a new view is opened from within a parent view, this new view
508 * becomes a child view of the parent view, replacing any existing child.
510 * When a new view is opened from within a child view, this new view
511 * becomes a parent view which will obscure the views below until the
512 * user quits the new parent view by typing 'q'.
514 * This list of views contains parent views only.
515 * Child views are only pointed to by their parent view.
516 */
517 TAILQ_HEAD(tog_view_list_head, tog_view);
519 struct tog_view {
520 TAILQ_ENTRY(tog_view) entry;
521 WINDOW *window;
522 PANEL *panel;
523 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
524 int resized_y, resized_x; /* begin_y/x based on user resizing */
525 int maxx, x; /* max column and current start column */
526 int lines, cols; /* copies of LINES and COLS */
527 int nscrolled, offset; /* lines scrolled and hsplit line offset */
528 int gline, hiline; /* navigate to and highlight this nG line */
529 int ch, count; /* current keymap and count prefix */
530 int resized; /* set when in a resize event */
531 int focussed; /* Only set on one parent or child view at a time. */
532 int dying;
533 struct tog_view *parent;
534 struct tog_view *child;
536 /*
537 * This flag is initially set on parent views when a new child view
538 * is created. It gets toggled when the 'Tab' key switches focus
539 * between parent and child.
540 * The flag indicates whether focus should be passed on to our child
541 * view if this parent view gets picked for focus after another parent
542 * view was closed. This prevents child views from losing focus in such
543 * situations.
544 */
545 int focus_child;
547 enum tog_view_mode mode;
548 /* type-specific state */
549 enum tog_view_type type;
550 union {
551 struct tog_diff_view_state diff;
552 struct tog_log_view_state log;
553 struct tog_blame_view_state blame;
554 struct tog_tree_view_state tree;
555 struct tog_ref_view_state ref;
556 } state;
558 const struct got_error *(*show)(struct tog_view *);
559 const struct got_error *(*input)(struct tog_view **,
560 struct tog_view *, int);
561 const struct got_error *(*reset)(struct tog_view *);
562 const struct got_error *(*resize)(struct tog_view *, int);
563 const struct got_error *(*close)(struct tog_view *);
565 const struct got_error *(*search_start)(struct tog_view *);
566 const struct got_error *(*search_next)(struct tog_view *);
567 int search_started;
568 int searching;
569 #define TOG_SEARCH_FORWARD 1
570 #define TOG_SEARCH_BACKWARD 2
571 int search_next_done;
572 #define TOG_SEARCH_HAVE_MORE 1
573 #define TOG_SEARCH_NO_MORE 2
574 #define TOG_SEARCH_HAVE_NONE 3
575 regex_t regex;
576 regmatch_t regmatch;
577 };
579 static const struct got_error *open_diff_view(struct tog_view *,
580 struct got_object_id *, struct got_object_id *,
581 const char *, const char *, int, int, int, struct tog_view *,
582 struct got_repository *);
583 static const struct got_error *show_diff_view(struct tog_view *);
584 static const struct got_error *input_diff_view(struct tog_view **,
585 struct tog_view *, int);
586 static const struct got_error *reset_diff_view(struct tog_view *);
587 static const struct got_error* close_diff_view(struct tog_view *);
588 static const struct got_error *search_start_diff_view(struct tog_view *);
589 static const struct got_error *search_next_diff_view(struct tog_view *);
591 static const struct got_error *open_log_view(struct tog_view *,
592 struct got_object_id *, struct got_repository *,
593 const char *, const char *, int);
594 static const struct got_error * show_log_view(struct tog_view *);
595 static const struct got_error *input_log_view(struct tog_view **,
596 struct tog_view *, int);
597 static const struct got_error *resize_log_view(struct tog_view *, int);
598 static const struct got_error *close_log_view(struct tog_view *);
599 static const struct got_error *search_start_log_view(struct tog_view *);
600 static const struct got_error *search_next_log_view(struct tog_view *);
602 static const struct got_error *open_blame_view(struct tog_view *, char *,
603 struct got_object_id *, struct got_repository *);
604 static const struct got_error *show_blame_view(struct tog_view *);
605 static const struct got_error *input_blame_view(struct tog_view **,
606 struct tog_view *, int);
607 static const struct got_error *reset_blame_view(struct tog_view *);
608 static const struct got_error *close_blame_view(struct tog_view *);
609 static const struct got_error *search_start_blame_view(struct tog_view *);
610 static const struct got_error *search_next_blame_view(struct tog_view *);
612 static const struct got_error *open_tree_view(struct tog_view *,
613 struct got_object_id *, const char *, struct got_repository *);
614 static const struct got_error *show_tree_view(struct tog_view *);
615 static const struct got_error *input_tree_view(struct tog_view **,
616 struct tog_view *, int);
617 static const struct got_error *close_tree_view(struct tog_view *);
618 static const struct got_error *search_start_tree_view(struct tog_view *);
619 static const struct got_error *search_next_tree_view(struct tog_view *);
621 static const struct got_error *open_ref_view(struct tog_view *,
622 struct got_repository *);
623 static const struct got_error *show_ref_view(struct tog_view *);
624 static const struct got_error *input_ref_view(struct tog_view **,
625 struct tog_view *, int);
626 static const struct got_error *close_ref_view(struct tog_view *);
627 static const struct got_error *search_start_ref_view(struct tog_view *);
628 static const struct got_error *search_next_ref_view(struct tog_view *);
630 static volatile sig_atomic_t tog_sigwinch_received;
631 static volatile sig_atomic_t tog_sigpipe_received;
632 static volatile sig_atomic_t tog_sigcont_received;
633 static volatile sig_atomic_t tog_sigint_received;
634 static volatile sig_atomic_t tog_sigterm_received;
636 static void
637 tog_sigwinch(int signo)
639 tog_sigwinch_received = 1;
642 static void
643 tog_sigpipe(int signo)
645 tog_sigpipe_received = 1;
648 static void
649 tog_sigcont(int signo)
651 tog_sigcont_received = 1;
654 static void
655 tog_sigint(int signo)
657 tog_sigint_received = 1;
660 static void
661 tog_sigterm(int signo)
663 tog_sigterm_received = 1;
666 static int
667 tog_fatal_signal_received(void)
669 return (tog_sigpipe_received ||
670 tog_sigint_received || tog_sigint_received);
673 static const struct got_error *
674 view_close(struct tog_view *view)
676 const struct got_error *err = NULL, *child_err = NULL;
678 if (view->child) {
679 child_err = view_close(view->child);
680 view->child = NULL;
682 if (view->close)
683 err = view->close(view);
684 if (view->panel)
685 del_panel(view->panel);
686 if (view->window)
687 delwin(view->window);
688 free(view);
689 return err ? err : child_err;
692 static struct tog_view *
693 view_open(int nlines, int ncols, int begin_y, int begin_x,
694 enum tog_view_type type)
696 struct tog_view *view = calloc(1, sizeof(*view));
698 if (view == NULL)
699 return NULL;
701 view->type = type;
702 view->lines = LINES;
703 view->cols = COLS;
704 view->nlines = nlines ? nlines : LINES - begin_y;
705 view->ncols = ncols ? ncols : COLS - begin_x;
706 view->begin_y = begin_y;
707 view->begin_x = begin_x;
708 view->window = newwin(nlines, ncols, begin_y, begin_x);
709 if (view->window == NULL) {
710 view_close(view);
711 return NULL;
713 view->panel = new_panel(view->window);
714 if (view->panel == NULL ||
715 set_panel_userptr(view->panel, view) != OK) {
716 view_close(view);
717 return NULL;
720 keypad(view->window, TRUE);
721 return view;
724 static int
725 view_split_begin_x(int begin_x)
727 if (begin_x > 0 || COLS < 120)
728 return 0;
729 return (COLS - MAX(COLS / 2, 80));
732 /* XXX Stub till we decide what to do. */
733 static int
734 view_split_begin_y(int lines)
736 return lines * HSPLIT_SCALE;
739 static const struct got_error *view_resize(struct tog_view *);
741 static const struct got_error *
742 view_splitscreen(struct tog_view *view)
744 const struct got_error *err = NULL;
746 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
747 if (view->resized_y && view->resized_y < view->lines)
748 view->begin_y = view->resized_y;
749 else
750 view->begin_y = view_split_begin_y(view->nlines);
751 view->begin_x = 0;
752 } else if (!view->resized) {
753 if (view->resized_x && view->resized_x < view->cols - 1 &&
754 view->cols > 119)
755 view->begin_x = view->resized_x;
756 else
757 view->begin_x = view_split_begin_x(0);
758 view->begin_y = 0;
760 view->nlines = LINES - view->begin_y;
761 view->ncols = COLS - view->begin_x;
762 view->lines = LINES;
763 view->cols = COLS;
764 err = view_resize(view);
765 if (err)
766 return err;
768 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
769 view->parent->nlines = view->begin_y;
771 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
772 return got_error_from_errno("mvwin");
774 return NULL;
777 static const struct got_error *
778 view_fullscreen(struct tog_view *view)
780 const struct got_error *err = NULL;
782 view->begin_x = 0;
783 view->begin_y = view->resized ? view->begin_y : 0;
784 view->nlines = view->resized ? view->nlines : LINES;
785 view->ncols = COLS;
786 view->lines = LINES;
787 view->cols = COLS;
788 err = view_resize(view);
789 if (err)
790 return err;
792 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
793 return got_error_from_errno("mvwin");
795 return NULL;
798 static int
799 view_is_parent_view(struct tog_view *view)
801 return view->parent == NULL;
804 static int
805 view_is_splitscreen(struct tog_view *view)
807 return view->begin_x > 0 || view->begin_y > 0;
810 static int
811 view_is_fullscreen(struct tog_view *view)
813 return view->nlines == LINES && view->ncols == COLS;
816 static int
817 view_is_hsplit_top(struct tog_view *view)
819 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
820 view_is_splitscreen(view->child);
823 static void
824 view_border(struct tog_view *view)
826 PANEL *panel;
827 const struct tog_view *view_above;
829 if (view->parent)
830 return view_border(view->parent);
832 panel = panel_above(view->panel);
833 if (panel == NULL)
834 return;
836 view_above = panel_userptr(panel);
837 if (view->mode == TOG_VIEW_SPLIT_HRZN)
838 mvwhline(view->window, view_above->begin_y - 1,
839 view->begin_x, got_locale_is_utf8() ?
840 ACS_HLINE : '-', view->ncols);
841 else
842 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
843 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
846 static const struct got_error *view_init_hsplit(struct tog_view *, int);
847 static const struct got_error *request_log_commits(struct tog_view *);
848 static const struct got_error *offset_selection_down(struct tog_view *);
849 static void offset_selection_up(struct tog_view *);
850 static void view_get_split(struct tog_view *, int *, int *);
852 static const struct got_error *
853 view_resize(struct tog_view *view)
855 const struct got_error *err = NULL;
856 int dif, nlines, ncols;
858 dif = LINES - view->lines; /* line difference */
860 if (view->lines > LINES)
861 nlines = view->nlines - (view->lines - LINES);
862 else
863 nlines = view->nlines + (LINES - view->lines);
864 if (view->cols > COLS)
865 ncols = view->ncols - (view->cols - COLS);
866 else
867 ncols = view->ncols + (COLS - view->cols);
869 if (view->child) {
870 int hs = view->child->begin_y;
872 if (!view_is_fullscreen(view))
873 view->child->begin_x = view_split_begin_x(view->begin_x);
874 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
875 view->child->begin_x == 0) {
876 ncols = COLS;
878 view_fullscreen(view->child);
879 if (view->child->focussed)
880 show_panel(view->child->panel);
881 else
882 show_panel(view->panel);
883 } else {
884 ncols = view->child->begin_x;
886 view_splitscreen(view->child);
887 show_panel(view->child->panel);
889 /*
890 * XXX This is ugly and needs to be moved into the above
891 * logic but "works" for now and my attempts at moving it
892 * break either 'tab' or 'F' key maps in horizontal splits.
893 */
894 if (hs) {
895 err = view_splitscreen(view->child);
896 if (err)
897 return err;
898 if (dif < 0) { /* top split decreased */
899 err = offset_selection_down(view);
900 if (err)
901 return err;
903 view_border(view);
904 update_panels();
905 doupdate();
906 show_panel(view->child->panel);
907 nlines = view->nlines;
909 } else if (view->parent == NULL)
910 ncols = COLS;
912 if (view->resize && dif > 0) {
913 err = view->resize(view, dif);
914 if (err)
915 return err;
918 if (wresize(view->window, nlines, ncols) == ERR)
919 return got_error_from_errno("wresize");
920 if (replace_panel(view->panel, view->window) == ERR)
921 return got_error_from_errno("replace_panel");
922 wclear(view->window);
924 view->nlines = nlines;
925 view->ncols = ncols;
926 view->lines = LINES;
927 view->cols = COLS;
929 return NULL;
932 static const struct got_error *
933 resize_log_view(struct tog_view *view, int increase)
935 struct tog_log_view_state *s = &view->state.log;
936 const struct got_error *err = NULL;
937 int n = 0;
939 if (s->selected_entry)
940 n = s->selected_entry->idx + view->lines - s->selected;
942 /*
943 * Request commits to account for the increased
944 * height so we have enough to populate the view.
945 */
946 if (s->commits->ncommits < n) {
947 view->nscrolled = n - s->commits->ncommits + increase + 1;
948 err = request_log_commits(view);
951 return err;
954 static void
955 view_adjust_offset(struct tog_view *view, int n)
957 if (n == 0)
958 return;
960 if (view->parent && view->parent->offset) {
961 if (view->parent->offset + n >= 0)
962 view->parent->offset += n;
963 else
964 view->parent->offset = 0;
965 } else if (view->offset) {
966 if (view->offset - n >= 0)
967 view->offset -= n;
968 else
969 view->offset = 0;
973 static const struct got_error *
974 view_resize_split(struct tog_view *view, int resize)
976 const struct got_error *err = NULL;
977 struct tog_view *v = NULL;
979 if (view->parent)
980 v = view->parent;
981 else
982 v = view;
984 if (!v->child || !view_is_splitscreen(v->child))
985 return NULL;
987 v->resized = v->child->resized = resize; /* lock for resize event */
989 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
990 if (v->child->resized_y)
991 v->child->begin_y = v->child->resized_y;
992 if (view->parent)
993 v->child->begin_y -= resize;
994 else
995 v->child->begin_y += resize;
996 if (v->child->begin_y < 3) {
997 view->count = 0;
998 v->child->begin_y = 3;
999 } else if (v->child->begin_y > LINES - 1) {
1000 view->count = 0;
1001 v->child->begin_y = LINES - 1;
1003 v->ncols = COLS;
1004 v->child->ncols = COLS;
1005 view_adjust_offset(view, resize);
1006 err = view_init_hsplit(v, v->child->begin_y);
1007 if (err)
1008 return err;
1009 v->child->resized_y = v->child->begin_y;
1010 } else {
1011 if (v->child->resized_x)
1012 v->child->begin_x = v->child->resized_x;
1013 if (view->parent)
1014 v->child->begin_x -= resize;
1015 else
1016 v->child->begin_x += resize;
1017 if (v->child->begin_x < 11) {
1018 view->count = 0;
1019 v->child->begin_x = 11;
1020 } else if (v->child->begin_x > COLS - 1) {
1021 view->count = 0;
1022 v->child->begin_x = COLS - 1;
1024 v->child->resized_x = v->child->begin_x;
1027 v->child->mode = v->mode;
1028 v->child->nlines = v->lines - v->child->begin_y;
1029 v->child->ncols = v->cols - v->child->begin_x;
1030 v->focus_child = 1;
1032 err = view_fullscreen(v);
1033 if (err)
1034 return err;
1035 err = view_splitscreen(v->child);
1036 if (err)
1037 return err;
1039 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1040 err = offset_selection_down(v->child);
1041 if (err)
1042 return err;
1045 if (v->resize)
1046 err = v->resize(v, 0);
1047 else if (v->child->resize)
1048 err = v->child->resize(v->child, 0);
1050 v->resized = v->child->resized = 0;
1052 return err;
1055 static void
1056 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1058 struct tog_view *v = src->child ? src->child : src;
1060 dst->resized_x = v->resized_x;
1061 dst->resized_y = v->resized_y;
1064 static const struct got_error *
1065 view_close_child(struct tog_view *view)
1067 const struct got_error *err = NULL;
1069 if (view->child == NULL)
1070 return NULL;
1072 err = view_close(view->child);
1073 view->child = NULL;
1074 return err;
1077 static const struct got_error *
1078 view_set_child(struct tog_view *view, struct tog_view *child)
1080 const struct got_error *err = NULL;
1082 view->child = child;
1083 child->parent = view;
1085 err = view_resize(view);
1086 if (err)
1087 return err;
1089 if (view->child->resized_x || view->child->resized_y)
1090 err = view_resize_split(view, 0);
1092 return err;
1095 static const struct got_error *view_dispatch_request(struct tog_view **,
1096 struct tog_view *, enum tog_view_type, int, int);
1098 static const struct got_error *
1099 view_request_new(struct tog_view **requested, struct tog_view *view,
1100 enum tog_view_type request)
1102 struct tog_view *new_view = NULL;
1103 const struct got_error *err;
1104 int y = 0, x = 0;
1106 *requested = NULL;
1108 if (view_is_parent_view(view))
1109 view_get_split(view, &y, &x);
1111 err = view_dispatch_request(&new_view, view, request, y, x);
1112 if (err)
1113 return err;
1115 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1116 err = view_init_hsplit(view, y);
1117 if (err)
1118 return err;
1121 view->focussed = 0;
1122 new_view->focussed = 1;
1123 new_view->mode = view->mode;
1124 new_view->nlines = view->lines - y;
1126 if (view_is_parent_view(view)) {
1127 view_transfer_size(new_view, view);
1128 err = view_close_child(view);
1129 if (err)
1130 return err;
1131 err = view_set_child(view, new_view);
1132 if (err)
1133 return err;
1134 view->focus_child = 1;
1135 } else
1136 *requested = new_view;
1138 return NULL;
1141 static void
1142 tog_resizeterm(void)
1144 int cols, lines;
1145 struct winsize size;
1147 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1148 cols = 80; /* Default */
1149 lines = 24;
1150 } else {
1151 cols = size.ws_col;
1152 lines = size.ws_row;
1154 resize_term(lines, cols);
1157 static const struct got_error *
1158 view_search_start(struct tog_view *view)
1160 const struct got_error *err = NULL;
1161 struct tog_view *v = view;
1162 char pattern[1024];
1163 int ret;
1165 if (view->search_started) {
1166 regfree(&view->regex);
1167 view->searching = 0;
1168 memset(&view->regmatch, 0, sizeof(view->regmatch));
1170 view->search_started = 0;
1172 if (view->nlines < 1)
1173 return NULL;
1175 if (view_is_hsplit_top(view))
1176 v = view->child;
1178 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1179 wclrtoeol(v->window);
1181 nodelay(view->window, FALSE); /* block for search term input */
1182 nocbreak();
1183 echo();
1184 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1185 wrefresh(v->window);
1186 cbreak();
1187 noecho();
1188 nodelay(view->window, TRUE);
1189 if (ret == ERR)
1190 return NULL;
1192 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1193 err = view->search_start(view);
1194 if (err) {
1195 regfree(&view->regex);
1196 return err;
1198 view->search_started = 1;
1199 view->searching = TOG_SEARCH_FORWARD;
1200 view->search_next_done = 0;
1201 view->search_next(view);
1204 return NULL;
1207 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1208 static const struct got_error *
1209 switch_split(struct tog_view *view)
1211 const struct got_error *err = NULL;
1212 struct tog_view *v = NULL;
1214 if (view->parent)
1215 v = view->parent;
1216 else
1217 v = view;
1219 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1220 v->mode = TOG_VIEW_SPLIT_VERT;
1221 else
1222 v->mode = TOG_VIEW_SPLIT_HRZN;
1224 if (!v->child)
1225 return NULL;
1226 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1227 v->mode = TOG_VIEW_SPLIT_NONE;
1229 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1230 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1231 v->child->begin_y = v->child->resized_y;
1232 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1233 v->child->begin_x = v->child->resized_x;
1236 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1237 v->ncols = COLS;
1238 v->child->ncols = COLS;
1239 v->child->nscrolled = LINES - v->child->nlines;
1241 err = view_init_hsplit(v, v->child->begin_y);
1242 if (err)
1243 return err;
1245 v->child->mode = v->mode;
1246 v->child->nlines = v->lines - v->child->begin_y;
1247 v->focus_child = 1;
1249 err = view_fullscreen(v);
1250 if (err)
1251 return err;
1252 err = view_splitscreen(v->child);
1253 if (err)
1254 return err;
1256 if (v->mode == TOG_VIEW_SPLIT_NONE)
1257 v->mode = TOG_VIEW_SPLIT_VERT;
1258 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1259 err = offset_selection_down(v);
1260 if (err)
1261 return err;
1262 err = offset_selection_down(v->child);
1263 if (err)
1264 return err;
1265 } else {
1266 offset_selection_up(v);
1267 offset_selection_up(v->child);
1269 if (v->resize)
1270 err = v->resize(v, 0);
1271 else if (v->child->resize)
1272 err = v->child->resize(v->child, 0);
1274 return err;
1278 * Compute view->count from numeric input. Assign total to view->count and
1279 * return first non-numeric key entered.
1281 static int
1282 get_compound_key(struct tog_view *view, int c)
1284 struct tog_view *v = view;
1285 int x, n = 0;
1287 if (view_is_hsplit_top(view))
1288 v = view->child;
1289 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1290 v = view->parent;
1292 view->count = 0;
1293 cbreak(); /* block for input */
1294 nodelay(view->window, FALSE);
1295 wmove(v->window, v->nlines - 1, 0);
1296 wclrtoeol(v->window);
1297 waddch(v->window, ':');
1299 do {
1300 x = getcurx(v->window);
1301 if (x != ERR && x < view->ncols) {
1302 waddch(v->window, c);
1303 wrefresh(v->window);
1307 * Don't overflow. Max valid request should be the greatest
1308 * between the longest and total lines; cap at 10 million.
1310 if (n >= 9999999)
1311 n = 9999999;
1312 else
1313 n = n * 10 + (c - '0');
1314 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1316 if (c == 'G' || c == 'g') { /* nG key map */
1317 view->gline = view->hiline = n;
1318 n = 0;
1319 c = 0;
1322 /* Massage excessive or inapplicable values at the input handler. */
1323 view->count = n;
1325 return c;
1328 static const struct got_error *
1329 view_input(struct tog_view **new, int *done, struct tog_view *view,
1330 struct tog_view_list_head *views)
1332 const struct got_error *err = NULL;
1333 struct tog_view *v;
1334 int ch, errcode;
1336 *new = NULL;
1338 /* Clear "no matches" indicator. */
1339 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1340 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1342 view->count = 0;
1345 if (view->searching && !view->search_next_done) {
1346 errcode = pthread_mutex_unlock(&tog_mutex);
1347 if (errcode)
1348 return got_error_set_errno(errcode,
1349 "pthread_mutex_unlock");
1350 sched_yield();
1351 errcode = pthread_mutex_lock(&tog_mutex);
1352 if (errcode)
1353 return got_error_set_errno(errcode,
1354 "pthread_mutex_lock");
1355 view->search_next(view);
1356 return NULL;
1359 /* Allow threads to make progress while we are waiting for input. */
1360 errcode = pthread_mutex_unlock(&tog_mutex);
1361 if (errcode)
1362 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1363 /* If we have an unfinished count, let C-g or backspace abort. */
1364 if (view->count && --view->count) {
1365 cbreak();
1366 nodelay(view->window, TRUE);
1367 ch = wgetch(view->window);
1368 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1369 view->count = 0;
1370 else
1371 ch = view->ch;
1372 } else {
1373 ch = wgetch(view->window);
1374 if (ch >= '1' && ch <= '9')
1375 view->ch = ch = get_compound_key(view, ch);
1377 if (view->hiline && ch != ERR && ch != 0)
1378 view->hiline = 0; /* key pressed, clear line highlight */
1379 nodelay(view->window, TRUE);
1380 errcode = pthread_mutex_lock(&tog_mutex);
1381 if (errcode)
1382 return got_error_set_errno(errcode, "pthread_mutex_lock");
1384 if (tog_sigwinch_received || tog_sigcont_received) {
1385 tog_resizeterm();
1386 tog_sigwinch_received = 0;
1387 tog_sigcont_received = 0;
1388 TAILQ_FOREACH(v, views, entry) {
1389 err = view_resize(v);
1390 if (err)
1391 return err;
1392 err = v->input(new, v, KEY_RESIZE);
1393 if (err)
1394 return err;
1395 if (v->child) {
1396 err = view_resize(v->child);
1397 if (err)
1398 return err;
1399 err = v->child->input(new, v->child,
1400 KEY_RESIZE);
1401 if (err)
1402 return err;
1403 if (v->child->resized_x || v->child->resized_y) {
1404 err = view_resize_split(v, 0);
1405 if (err)
1406 return err;
1412 switch (ch) {
1413 case '\t':
1414 view->count = 0;
1415 if (view->child) {
1416 view->focussed = 0;
1417 view->child->focussed = 1;
1418 view->focus_child = 1;
1419 } else if (view->parent) {
1420 view->focussed = 0;
1421 view->parent->focussed = 1;
1422 view->parent->focus_child = 0;
1423 if (!view_is_splitscreen(view)) {
1424 if (view->parent->resize) {
1425 err = view->parent->resize(view->parent,
1426 0);
1427 if (err)
1428 return err;
1430 offset_selection_up(view->parent);
1431 err = view_fullscreen(view->parent);
1432 if (err)
1433 return err;
1436 break;
1437 case 'q':
1438 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1439 if (view->parent->resize) {
1440 /* might need more commits to fill fullscreen */
1441 err = view->parent->resize(view->parent, 0);
1442 if (err)
1443 break;
1445 offset_selection_up(view->parent);
1447 err = view->input(new, view, ch);
1448 view->dying = 1;
1449 break;
1450 case 'Q':
1451 *done = 1;
1452 break;
1453 case 'F':
1454 view->count = 0;
1455 if (view_is_parent_view(view)) {
1456 if (view->child == NULL)
1457 break;
1458 if (view_is_splitscreen(view->child)) {
1459 view->focussed = 0;
1460 view->child->focussed = 1;
1461 err = view_fullscreen(view->child);
1462 } else {
1463 err = view_splitscreen(view->child);
1464 if (!err)
1465 err = view_resize_split(view, 0);
1467 if (err)
1468 break;
1469 err = view->child->input(new, view->child,
1470 KEY_RESIZE);
1471 } else {
1472 if (view_is_splitscreen(view)) {
1473 view->parent->focussed = 0;
1474 view->focussed = 1;
1475 err = view_fullscreen(view);
1476 } else {
1477 err = view_splitscreen(view);
1478 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1479 err = view_resize(view->parent);
1480 if (!err)
1481 err = view_resize_split(view, 0);
1483 if (err)
1484 break;
1485 err = view->input(new, view, KEY_RESIZE);
1487 if (err)
1488 break;
1489 if (view->resize) {
1490 err = view->resize(view, 0);
1491 if (err)
1492 break;
1494 if (view->parent)
1495 err = offset_selection_down(view->parent);
1496 if (!err)
1497 err = offset_selection_down(view);
1498 break;
1499 case 'S':
1500 view->count = 0;
1501 err = switch_split(view);
1502 break;
1503 case '-':
1504 err = view_resize_split(view, -1);
1505 break;
1506 case '+':
1507 err = view_resize_split(view, 1);
1508 break;
1509 case KEY_RESIZE:
1510 break;
1511 case '/':
1512 view->count = 0;
1513 if (view->search_start)
1514 view_search_start(view);
1515 else
1516 err = view->input(new, view, ch);
1517 break;
1518 case 'N':
1519 case 'n':
1520 if (view->search_started && view->search_next) {
1521 view->searching = (ch == 'n' ?
1522 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1523 view->search_next_done = 0;
1524 view->search_next(view);
1525 } else
1526 err = view->input(new, view, ch);
1527 break;
1528 case 'A':
1529 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1530 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1531 else
1532 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1533 TAILQ_FOREACH(v, views, entry) {
1534 if (v->reset) {
1535 err = v->reset(v);
1536 if (err)
1537 return err;
1539 if (v->child && v->child->reset) {
1540 err = v->child->reset(v->child);
1541 if (err)
1542 return err;
1545 break;
1546 default:
1547 err = view->input(new, view, ch);
1548 break;
1551 return err;
1554 static int
1555 view_needs_focus_indication(struct tog_view *view)
1557 if (view_is_parent_view(view)) {
1558 if (view->child == NULL || view->child->focussed)
1559 return 0;
1560 if (!view_is_splitscreen(view->child))
1561 return 0;
1562 } else if (!view_is_splitscreen(view))
1563 return 0;
1565 return view->focussed;
1568 static const struct got_error *
1569 view_loop(struct tog_view *view)
1571 const struct got_error *err = NULL;
1572 struct tog_view_list_head views;
1573 struct tog_view *new_view;
1574 char *mode;
1575 int fast_refresh = 10;
1576 int done = 0, errcode;
1578 mode = getenv("TOG_VIEW_SPLIT_MODE");
1579 if (!mode || !(*mode == 'h' || *mode == 'H'))
1580 view->mode = TOG_VIEW_SPLIT_VERT;
1581 else
1582 view->mode = TOG_VIEW_SPLIT_HRZN;
1584 errcode = pthread_mutex_lock(&tog_mutex);
1585 if (errcode)
1586 return got_error_set_errno(errcode, "pthread_mutex_lock");
1588 TAILQ_INIT(&views);
1589 TAILQ_INSERT_HEAD(&views, view, entry);
1591 view->focussed = 1;
1592 err = view->show(view);
1593 if (err)
1594 return err;
1595 update_panels();
1596 doupdate();
1597 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1598 !tog_fatal_signal_received()) {
1599 /* Refresh fast during initialization, then become slower. */
1600 if (fast_refresh && fast_refresh-- == 0)
1601 halfdelay(10); /* switch to once per second */
1603 err = view_input(&new_view, &done, view, &views);
1604 if (err)
1605 break;
1606 if (view->dying) {
1607 struct tog_view *v, *prev = NULL;
1609 if (view_is_parent_view(view))
1610 prev = TAILQ_PREV(view, tog_view_list_head,
1611 entry);
1612 else if (view->parent)
1613 prev = view->parent;
1615 if (view->parent) {
1616 view->parent->child = NULL;
1617 view->parent->focus_child = 0;
1618 /* Restore fullscreen line height. */
1619 view->parent->nlines = view->parent->lines;
1620 err = view_resize(view->parent);
1621 if (err)
1622 break;
1623 /* Make resized splits persist. */
1624 view_transfer_size(view->parent, view);
1625 } else
1626 TAILQ_REMOVE(&views, view, entry);
1628 err = view_close(view);
1629 if (err)
1630 goto done;
1632 view = NULL;
1633 TAILQ_FOREACH(v, &views, entry) {
1634 if (v->focussed)
1635 break;
1637 if (view == NULL && new_view == NULL) {
1638 /* No view has focus. Try to pick one. */
1639 if (prev)
1640 view = prev;
1641 else if (!TAILQ_EMPTY(&views)) {
1642 view = TAILQ_LAST(&views,
1643 tog_view_list_head);
1645 if (view) {
1646 if (view->focus_child) {
1647 view->child->focussed = 1;
1648 view = view->child;
1649 } else
1650 view->focussed = 1;
1654 if (new_view) {
1655 struct tog_view *v, *t;
1656 /* Only allow one parent view per type. */
1657 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1658 if (v->type != new_view->type)
1659 continue;
1660 TAILQ_REMOVE(&views, v, entry);
1661 err = view_close(v);
1662 if (err)
1663 goto done;
1664 break;
1666 TAILQ_INSERT_TAIL(&views, new_view, entry);
1667 view = new_view;
1669 if (view) {
1670 if (view_is_parent_view(view)) {
1671 if (view->child && view->child->focussed)
1672 view = view->child;
1673 } else {
1674 if (view->parent && view->parent->focussed)
1675 view = view->parent;
1677 show_panel(view->panel);
1678 if (view->child && view_is_splitscreen(view->child))
1679 show_panel(view->child->panel);
1680 if (view->parent && view_is_splitscreen(view)) {
1681 err = view->parent->show(view->parent);
1682 if (err)
1683 goto done;
1685 err = view->show(view);
1686 if (err)
1687 goto done;
1688 if (view->child) {
1689 err = view->child->show(view->child);
1690 if (err)
1691 goto done;
1693 update_panels();
1694 doupdate();
1697 done:
1698 while (!TAILQ_EMPTY(&views)) {
1699 const struct got_error *close_err;
1700 view = TAILQ_FIRST(&views);
1701 TAILQ_REMOVE(&views, view, entry);
1702 close_err = view_close(view);
1703 if (close_err && err == NULL)
1704 err = close_err;
1707 errcode = pthread_mutex_unlock(&tog_mutex);
1708 if (errcode && err == NULL)
1709 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1711 return err;
1714 __dead static void
1715 usage_log(void)
1717 endwin();
1718 fprintf(stderr,
1719 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1720 getprogname());
1721 exit(1);
1724 /* Create newly allocated wide-character string equivalent to a byte string. */
1725 static const struct got_error *
1726 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1728 char *vis = NULL;
1729 const struct got_error *err = NULL;
1731 *ws = NULL;
1732 *wlen = mbstowcs(NULL, s, 0);
1733 if (*wlen == (size_t)-1) {
1734 int vislen;
1735 if (errno != EILSEQ)
1736 return got_error_from_errno("mbstowcs");
1738 /* byte string invalid in current encoding; try to "fix" it */
1739 err = got_mbsavis(&vis, &vislen, s);
1740 if (err)
1741 return err;
1742 *wlen = mbstowcs(NULL, vis, 0);
1743 if (*wlen == (size_t)-1) {
1744 err = got_error_from_errno("mbstowcs"); /* give up */
1745 goto done;
1749 *ws = calloc(*wlen + 1, sizeof(**ws));
1750 if (*ws == NULL) {
1751 err = got_error_from_errno("calloc");
1752 goto done;
1755 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1756 err = got_error_from_errno("mbstowcs");
1757 done:
1758 free(vis);
1759 if (err) {
1760 free(*ws);
1761 *ws = NULL;
1762 *wlen = 0;
1764 return err;
1767 static const struct got_error *
1768 expand_tab(char **ptr, const char *src)
1770 char *dst;
1771 size_t len, n, idx = 0, sz = 0;
1773 *ptr = NULL;
1774 n = len = strlen(src);
1775 dst = malloc(n + 1);
1776 if (dst == NULL)
1777 return got_error_from_errno("malloc");
1779 while (idx < len && src[idx]) {
1780 const char c = src[idx];
1782 if (c == '\t') {
1783 size_t nb = TABSIZE - sz % TABSIZE;
1784 char *p;
1786 p = realloc(dst, n + nb);
1787 if (p == NULL) {
1788 free(dst);
1789 return got_error_from_errno("realloc");
1792 dst = p;
1793 n += nb;
1794 memset(dst + sz, ' ', nb);
1795 sz += nb;
1796 } else
1797 dst[sz++] = src[idx];
1798 ++idx;
1801 dst[sz] = '\0';
1802 *ptr = dst;
1803 return NULL;
1807 * Advance at most n columns from wline starting at offset off.
1808 * Return the index to the first character after the span operation.
1809 * Return the combined column width of all spanned wide character in
1810 * *rcol.
1812 static int
1813 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1815 int width, i, cols = 0;
1817 if (n == 0) {
1818 *rcol = cols;
1819 return off;
1822 for (i = off; wline[i] != L'\0'; ++i) {
1823 if (wline[i] == L'\t')
1824 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1825 else
1826 width = wcwidth(wline[i]);
1828 if (width == -1) {
1829 width = 1;
1830 wline[i] = L'.';
1833 if (cols + width > n)
1834 break;
1835 cols += width;
1838 *rcol = cols;
1839 return i;
1843 * Format a line for display, ensuring that it won't overflow a width limit.
1844 * With scrolling, the width returned refers to the scrolled version of the
1845 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1847 static const struct got_error *
1848 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1849 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1851 const struct got_error *err = NULL;
1852 int cols;
1853 wchar_t *wline = NULL;
1854 char *exstr = NULL;
1855 size_t wlen;
1856 int i, scrollx;
1858 *wlinep = NULL;
1859 *widthp = 0;
1861 if (expand) {
1862 err = expand_tab(&exstr, line);
1863 if (err)
1864 return err;
1867 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1868 free(exstr);
1869 if (err)
1870 return err;
1872 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1874 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1875 wline[wlen - 1] = L'\0';
1876 wlen--;
1878 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1879 wline[wlen - 1] = L'\0';
1880 wlen--;
1883 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1884 wline[i] = L'\0';
1886 if (widthp)
1887 *widthp = cols;
1888 if (scrollxp)
1889 *scrollxp = scrollx;
1890 if (err)
1891 free(wline);
1892 else
1893 *wlinep = wline;
1894 return err;
1897 static const struct got_error*
1898 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1899 struct got_object_id *id, struct got_repository *repo)
1901 static const struct got_error *err = NULL;
1902 struct got_reflist_entry *re;
1903 char *s;
1904 const char *name;
1906 *refs_str = NULL;
1908 TAILQ_FOREACH(re, refs, entry) {
1909 struct got_tag_object *tag = NULL;
1910 struct got_object_id *ref_id;
1911 int cmp;
1913 name = got_ref_get_name(re->ref);
1914 if (strcmp(name, GOT_REF_HEAD) == 0)
1915 continue;
1916 if (strncmp(name, "refs/", 5) == 0)
1917 name += 5;
1918 if (strncmp(name, "got/", 4) == 0 &&
1919 strncmp(name, "got/backup/", 11) != 0)
1920 continue;
1921 if (strncmp(name, "heads/", 6) == 0)
1922 name += 6;
1923 if (strncmp(name, "remotes/", 8) == 0) {
1924 name += 8;
1925 s = strstr(name, "/" GOT_REF_HEAD);
1926 if (s != NULL && s[strlen(s)] == '\0')
1927 continue;
1929 err = got_ref_resolve(&ref_id, repo, re->ref);
1930 if (err)
1931 break;
1932 if (strncmp(name, "tags/", 5) == 0) {
1933 err = got_object_open_as_tag(&tag, repo, ref_id);
1934 if (err) {
1935 if (err->code != GOT_ERR_OBJ_TYPE) {
1936 free(ref_id);
1937 break;
1939 /* Ref points at something other than a tag. */
1940 err = NULL;
1941 tag = NULL;
1944 cmp = got_object_id_cmp(tag ?
1945 got_object_tag_get_object_id(tag) : ref_id, id);
1946 free(ref_id);
1947 if (tag)
1948 got_object_tag_close(tag);
1949 if (cmp != 0)
1950 continue;
1951 s = *refs_str;
1952 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1953 s ? ", " : "", name) == -1) {
1954 err = got_error_from_errno("asprintf");
1955 free(s);
1956 *refs_str = NULL;
1957 break;
1959 free(s);
1962 return err;
1965 static const struct got_error *
1966 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1967 int col_tab_align)
1969 char *smallerthan;
1971 smallerthan = strchr(author, '<');
1972 if (smallerthan && smallerthan[1] != '\0')
1973 author = smallerthan + 1;
1974 author[strcspn(author, "@>")] = '\0';
1975 return format_line(wauthor, author_width, NULL, author, 0, limit,
1976 col_tab_align, 0);
1979 static const struct got_error *
1980 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1981 struct got_object_id *id, const size_t date_display_cols,
1982 int author_display_cols)
1984 struct tog_log_view_state *s = &view->state.log;
1985 const struct got_error *err = NULL;
1986 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1987 char *logmsg0 = NULL, *logmsg = NULL;
1988 char *author = NULL;
1989 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1990 int author_width, logmsg_width;
1991 char *newline, *line = NULL;
1992 int col, limit, scrollx;
1993 const int avail = view->ncols;
1994 struct tm tm;
1995 time_t committer_time;
1996 struct tog_color *tc;
1998 committer_time = got_object_commit_get_committer_time(commit);
1999 if (gmtime_r(&committer_time, &tm) == NULL)
2000 return got_error_from_errno("gmtime_r");
2001 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2002 return got_error(GOT_ERR_NO_SPACE);
2004 if (avail <= date_display_cols)
2005 limit = MIN(sizeof(datebuf) - 1, avail);
2006 else
2007 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2008 tc = get_color(&s->colors, TOG_COLOR_DATE);
2009 if (tc)
2010 wattr_on(view->window,
2011 COLOR_PAIR(tc->colorpair), NULL);
2012 waddnstr(view->window, datebuf, limit);
2013 if (tc)
2014 wattr_off(view->window,
2015 COLOR_PAIR(tc->colorpair), NULL);
2016 col = limit;
2017 if (col > avail)
2018 goto done;
2020 if (avail >= 120) {
2021 char *id_str;
2022 err = got_object_id_str(&id_str, id);
2023 if (err)
2024 goto done;
2025 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2026 if (tc)
2027 wattr_on(view->window,
2028 COLOR_PAIR(tc->colorpair), NULL);
2029 wprintw(view->window, "%.8s ", id_str);
2030 if (tc)
2031 wattr_off(view->window,
2032 COLOR_PAIR(tc->colorpair), NULL);
2033 free(id_str);
2034 col += 9;
2035 if (col > avail)
2036 goto done;
2039 if (s->use_committer)
2040 author = strdup(got_object_commit_get_committer(commit));
2041 else
2042 author = strdup(got_object_commit_get_author(commit));
2043 if (author == NULL) {
2044 err = got_error_from_errno("strdup");
2045 goto done;
2047 err = format_author(&wauthor, &author_width, author, avail - col, col);
2048 if (err)
2049 goto done;
2050 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2051 if (tc)
2052 wattr_on(view->window,
2053 COLOR_PAIR(tc->colorpair), NULL);
2054 waddwstr(view->window, wauthor);
2055 col += author_width;
2056 while (col < avail && author_width < author_display_cols + 2) {
2057 waddch(view->window, ' ');
2058 col++;
2059 author_width++;
2061 if (tc)
2062 wattr_off(view->window,
2063 COLOR_PAIR(tc->colorpair), NULL);
2064 if (col > avail)
2065 goto done;
2067 err = got_object_commit_get_logmsg(&logmsg0, commit);
2068 if (err)
2069 goto done;
2070 logmsg = logmsg0;
2071 while (*logmsg == '\n')
2072 logmsg++;
2073 newline = strchr(logmsg, '\n');
2074 if (newline)
2075 *newline = '\0';
2076 limit = avail - col;
2077 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2078 limit--; /* for the border */
2079 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2080 limit, col, 1);
2081 if (err)
2082 goto done;
2083 waddwstr(view->window, &wlogmsg[scrollx]);
2084 col += MAX(logmsg_width, 0);
2085 while (col < avail) {
2086 waddch(view->window, ' ');
2087 col++;
2089 done:
2090 free(logmsg0);
2091 free(wlogmsg);
2092 free(author);
2093 free(wauthor);
2094 free(line);
2095 return err;
2098 static struct commit_queue_entry *
2099 alloc_commit_queue_entry(struct got_commit_object *commit,
2100 struct got_object_id *id)
2102 struct commit_queue_entry *entry;
2103 struct got_object_id *dup;
2105 entry = calloc(1, sizeof(*entry));
2106 if (entry == NULL)
2107 return NULL;
2109 dup = got_object_id_dup(id);
2110 if (dup == NULL) {
2111 free(entry);
2112 return NULL;
2115 entry->id = dup;
2116 entry->commit = commit;
2117 return entry;
2120 static void
2121 pop_commit(struct commit_queue *commits)
2123 struct commit_queue_entry *entry;
2125 entry = TAILQ_FIRST(&commits->head);
2126 TAILQ_REMOVE(&commits->head, entry, entry);
2127 got_object_commit_close(entry->commit);
2128 commits->ncommits--;
2129 free(entry->id);
2130 free(entry);
2133 static void
2134 free_commits(struct commit_queue *commits)
2136 while (!TAILQ_EMPTY(&commits->head))
2137 pop_commit(commits);
2140 static const struct got_error *
2141 match_commit(int *have_match, struct got_object_id *id,
2142 struct got_commit_object *commit, regex_t *regex)
2144 const struct got_error *err = NULL;
2145 regmatch_t regmatch;
2146 char *id_str = NULL, *logmsg = NULL;
2148 *have_match = 0;
2150 err = got_object_id_str(&id_str, id);
2151 if (err)
2152 return err;
2154 err = got_object_commit_get_logmsg(&logmsg, commit);
2155 if (err)
2156 goto done;
2158 if (regexec(regex, got_object_commit_get_author(commit), 1,
2159 &regmatch, 0) == 0 ||
2160 regexec(regex, got_object_commit_get_committer(commit), 1,
2161 &regmatch, 0) == 0 ||
2162 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2163 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2164 *have_match = 1;
2165 done:
2166 free(id_str);
2167 free(logmsg);
2168 return err;
2171 static const struct got_error *
2172 queue_commits(struct tog_log_thread_args *a)
2174 const struct got_error *err = NULL;
2177 * We keep all commits open throughout the lifetime of the log
2178 * view in order to avoid having to re-fetch commits from disk
2179 * while updating the display.
2181 do {
2182 struct got_object_id id;
2183 struct got_commit_object *commit;
2184 struct commit_queue_entry *entry;
2185 int limit_match = 0;
2186 int errcode;
2188 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2189 NULL, NULL);
2190 if (err)
2191 break;
2193 err = got_object_open_as_commit(&commit, a->repo, &id);
2194 if (err)
2195 break;
2196 entry = alloc_commit_queue_entry(commit, &id);
2197 if (entry == NULL) {
2198 err = got_error_from_errno("alloc_commit_queue_entry");
2199 break;
2202 errcode = pthread_mutex_lock(&tog_mutex);
2203 if (errcode) {
2204 err = got_error_set_errno(errcode,
2205 "pthread_mutex_lock");
2206 break;
2209 entry->idx = a->real_commits->ncommits;
2210 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2211 a->real_commits->ncommits++;
2213 if (*a->limiting) {
2214 err = match_commit(&limit_match, &id, commit,
2215 a->limit_regex);
2216 if (err)
2217 break;
2219 if (limit_match) {
2220 struct commit_queue_entry *matched;
2222 matched = alloc_commit_queue_entry(
2223 entry->commit, entry->id);
2224 if (matched == NULL) {
2225 err = got_error_from_errno(
2226 "alloc_commit_queue_entry");
2227 break;
2230 err = got_object_commit_dup(&matched->commit,
2231 entry->commit);
2232 if (err)
2233 break;
2235 matched->idx = a->limit_commits->ncommits;
2236 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2237 matched, entry);
2238 a->limit_commits->ncommits++;
2242 * This is how we signal log_thread() that we
2243 * have found a match, and that it should be
2244 * counted as a new entry for the view.
2246 a->limit_match = limit_match;
2249 if (*a->searching == TOG_SEARCH_FORWARD &&
2250 !*a->search_next_done) {
2251 int have_match;
2252 err = match_commit(&have_match, &id, commit, a->regex);
2253 if (err)
2254 break;
2256 if (*a->limiting) {
2257 if (limit_match && have_match)
2258 *a->search_next_done =
2259 TOG_SEARCH_HAVE_MORE;
2260 } else if (have_match)
2261 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2264 errcode = pthread_mutex_unlock(&tog_mutex);
2265 if (errcode && err == NULL)
2266 err = got_error_set_errno(errcode,
2267 "pthread_mutex_unlock");
2268 if (err)
2269 break;
2270 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2272 return err;
2275 static void
2276 select_commit(struct tog_log_view_state *s)
2278 struct commit_queue_entry *entry;
2279 int ncommits = 0;
2281 entry = s->first_displayed_entry;
2282 while (entry) {
2283 if (ncommits == s->selected) {
2284 s->selected_entry = entry;
2285 break;
2287 entry = TAILQ_NEXT(entry, entry);
2288 ncommits++;
2292 static const struct got_error *
2293 draw_commits(struct tog_view *view)
2295 const struct got_error *err = NULL;
2296 struct tog_log_view_state *s = &view->state.log;
2297 struct commit_queue_entry *entry = s->selected_entry;
2298 int limit = view->nlines;
2299 int width;
2300 int ncommits, author_cols = 4;
2301 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2302 char *refs_str = NULL;
2303 wchar_t *wline;
2304 struct tog_color *tc;
2305 static const size_t date_display_cols = 12;
2307 if (view_is_hsplit_top(view))
2308 --limit; /* account for border */
2310 if (s->selected_entry &&
2311 !(view->searching && view->search_next_done == 0)) {
2312 struct got_reflist_head *refs;
2313 err = got_object_id_str(&id_str, s->selected_entry->id);
2314 if (err)
2315 return err;
2316 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2317 s->selected_entry->id);
2318 if (refs) {
2319 err = build_refs_str(&refs_str, refs,
2320 s->selected_entry->id, s->repo);
2321 if (err)
2322 goto done;
2326 if (s->thread_args.commits_needed == 0)
2327 halfdelay(10); /* disable fast refresh */
2329 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2330 if (asprintf(&ncommits_str, " [%d/%d] %s",
2331 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2332 (view->searching && !view->search_next_done) ?
2333 "searching..." : "loading...") == -1) {
2334 err = got_error_from_errno("asprintf");
2335 goto done;
2337 } else {
2338 const char *search_str = NULL;
2339 const char *limit_str = NULL;
2341 if (view->searching) {
2342 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2343 search_str = "no more matches";
2344 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2345 search_str = "no matches found";
2346 else if (!view->search_next_done)
2347 search_str = "searching...";
2350 if (s->limit_view && s->commits->ncommits == 0)
2351 limit_str = "no matches found";
2353 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2354 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2355 search_str ? search_str : (refs_str ? refs_str : ""),
2356 limit_str ? limit_str : "") == -1) {
2357 err = got_error_from_errno("asprintf");
2358 goto done;
2362 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2363 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2364 "........................................",
2365 s->in_repo_path, ncommits_str) == -1) {
2366 err = got_error_from_errno("asprintf");
2367 header = NULL;
2368 goto done;
2370 } else if (asprintf(&header, "commit %s%s",
2371 id_str ? id_str : "........................................",
2372 ncommits_str) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 header = NULL;
2375 goto done;
2377 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2378 if (err)
2379 goto done;
2381 werase(view->window);
2383 if (view_needs_focus_indication(view))
2384 wstandout(view->window);
2385 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2386 if (tc)
2387 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2388 waddwstr(view->window, wline);
2389 while (width < view->ncols) {
2390 waddch(view->window, ' ');
2391 width++;
2393 if (tc)
2394 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2395 if (view_needs_focus_indication(view))
2396 wstandend(view->window);
2397 free(wline);
2398 if (limit <= 1)
2399 goto done;
2401 /* Grow author column size if necessary, and set view->maxx. */
2402 entry = s->first_displayed_entry;
2403 ncommits = 0;
2404 view->maxx = 0;
2405 while (entry) {
2406 struct got_commit_object *c = entry->commit;
2407 char *author, *eol, *msg, *msg0;
2408 wchar_t *wauthor, *wmsg;
2409 int width;
2410 if (ncommits >= limit - 1)
2411 break;
2412 if (s->use_committer)
2413 author = strdup(got_object_commit_get_committer(c));
2414 else
2415 author = strdup(got_object_commit_get_author(c));
2416 if (author == NULL) {
2417 err = got_error_from_errno("strdup");
2418 goto done;
2420 err = format_author(&wauthor, &width, author, COLS,
2421 date_display_cols);
2422 if (author_cols < width)
2423 author_cols = width;
2424 free(wauthor);
2425 free(author);
2426 if (err)
2427 goto done;
2428 err = got_object_commit_get_logmsg(&msg0, c);
2429 if (err)
2430 goto done;
2431 msg = msg0;
2432 while (*msg == '\n')
2433 ++msg;
2434 if ((eol = strchr(msg, '\n')))
2435 *eol = '\0';
2436 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2437 date_display_cols + author_cols, 0);
2438 if (err)
2439 goto done;
2440 view->maxx = MAX(view->maxx, width);
2441 free(msg0);
2442 free(wmsg);
2443 ncommits++;
2444 entry = TAILQ_NEXT(entry, entry);
2447 entry = s->first_displayed_entry;
2448 s->last_displayed_entry = s->first_displayed_entry;
2449 ncommits = 0;
2450 while (entry) {
2451 if (ncommits >= limit - 1)
2452 break;
2453 if (ncommits == s->selected)
2454 wstandout(view->window);
2455 err = draw_commit(view, entry->commit, entry->id,
2456 date_display_cols, author_cols);
2457 if (ncommits == s->selected)
2458 wstandend(view->window);
2459 if (err)
2460 goto done;
2461 ncommits++;
2462 s->last_displayed_entry = entry;
2463 entry = TAILQ_NEXT(entry, entry);
2466 view_border(view);
2467 done:
2468 free(id_str);
2469 free(refs_str);
2470 free(ncommits_str);
2471 free(header);
2472 return err;
2475 static void
2476 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2478 struct commit_queue_entry *entry;
2479 int nscrolled = 0;
2481 entry = TAILQ_FIRST(&s->commits->head);
2482 if (s->first_displayed_entry == entry)
2483 return;
2485 entry = s->first_displayed_entry;
2486 while (entry && nscrolled < maxscroll) {
2487 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2488 if (entry) {
2489 s->first_displayed_entry = entry;
2490 nscrolled++;
2495 static const struct got_error *
2496 trigger_log_thread(struct tog_view *view, int wait)
2498 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2499 int errcode;
2501 halfdelay(1); /* fast refresh while loading commits */
2503 while (!ta->log_complete && !tog_thread_error &&
2504 (ta->commits_needed > 0 || ta->load_all)) {
2505 /* Wake the log thread. */
2506 errcode = pthread_cond_signal(&ta->need_commits);
2507 if (errcode)
2508 return got_error_set_errno(errcode,
2509 "pthread_cond_signal");
2512 * The mutex will be released while the view loop waits
2513 * in wgetch(), at which time the log thread will run.
2515 if (!wait)
2516 break;
2518 /* Display progress update in log view. */
2519 show_log_view(view);
2520 update_panels();
2521 doupdate();
2523 /* Wait right here while next commit is being loaded. */
2524 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2525 if (errcode)
2526 return got_error_set_errno(errcode,
2527 "pthread_cond_wait");
2529 /* Display progress update in log view. */
2530 show_log_view(view);
2531 update_panels();
2532 doupdate();
2535 return NULL;
2538 static const struct got_error *
2539 request_log_commits(struct tog_view *view)
2541 struct tog_log_view_state *state = &view->state.log;
2542 const struct got_error *err = NULL;
2544 if (state->thread_args.log_complete)
2545 return NULL;
2547 state->thread_args.commits_needed += view->nscrolled;
2548 err = trigger_log_thread(view, 1);
2549 view->nscrolled = 0;
2551 return err;
2554 static const struct got_error *
2555 log_scroll_down(struct tog_view *view, int maxscroll)
2557 struct tog_log_view_state *s = &view->state.log;
2558 const struct got_error *err = NULL;
2559 struct commit_queue_entry *pentry;
2560 int nscrolled = 0, ncommits_needed;
2562 if (s->last_displayed_entry == NULL)
2563 return NULL;
2565 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2566 if (s->commits->ncommits < ncommits_needed &&
2567 !s->thread_args.log_complete) {
2569 * Ask the log thread for required amount of commits.
2571 s->thread_args.commits_needed +=
2572 ncommits_needed - s->commits->ncommits;
2573 err = trigger_log_thread(view, 1);
2574 if (err)
2575 return err;
2578 do {
2579 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2580 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2581 break;
2583 s->last_displayed_entry = pentry ?
2584 pentry : s->last_displayed_entry;;
2586 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2587 if (pentry == NULL)
2588 break;
2589 s->first_displayed_entry = pentry;
2590 } while (++nscrolled < maxscroll);
2592 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2593 view->nscrolled += nscrolled;
2594 else
2595 view->nscrolled = 0;
2597 return err;
2600 static const struct got_error *
2601 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2602 struct got_commit_object *commit, struct got_object_id *commit_id,
2603 struct tog_view *log_view, struct got_repository *repo)
2605 const struct got_error *err;
2606 struct got_object_qid *parent_id;
2607 struct tog_view *diff_view;
2609 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2610 if (diff_view == NULL)
2611 return got_error_from_errno("view_open");
2613 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2614 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2615 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2616 if (err == NULL)
2617 *new_view = diff_view;
2618 return err;
2621 static const struct got_error *
2622 tree_view_visit_subtree(struct tog_tree_view_state *s,
2623 struct got_tree_object *subtree)
2625 struct tog_parent_tree *parent;
2627 parent = calloc(1, sizeof(*parent));
2628 if (parent == NULL)
2629 return got_error_from_errno("calloc");
2631 parent->tree = s->tree;
2632 parent->first_displayed_entry = s->first_displayed_entry;
2633 parent->selected_entry = s->selected_entry;
2634 parent->selected = s->selected;
2635 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2636 s->tree = subtree;
2637 s->selected = 0;
2638 s->first_displayed_entry = NULL;
2639 return NULL;
2642 static const struct got_error *
2643 tree_view_walk_path(struct tog_tree_view_state *s,
2644 struct got_commit_object *commit, const char *path)
2646 const struct got_error *err = NULL;
2647 struct got_tree_object *tree = NULL;
2648 const char *p;
2649 char *slash, *subpath = NULL;
2651 /* Walk the path and open corresponding tree objects. */
2652 p = path;
2653 while (*p) {
2654 struct got_tree_entry *te;
2655 struct got_object_id *tree_id;
2656 char *te_name;
2658 while (p[0] == '/')
2659 p++;
2661 /* Ensure the correct subtree entry is selected. */
2662 slash = strchr(p, '/');
2663 if (slash == NULL)
2664 te_name = strdup(p);
2665 else
2666 te_name = strndup(p, slash - p);
2667 if (te_name == NULL) {
2668 err = got_error_from_errno("strndup");
2669 break;
2671 te = got_object_tree_find_entry(s->tree, te_name);
2672 if (te == NULL) {
2673 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2674 free(te_name);
2675 break;
2677 free(te_name);
2678 s->first_displayed_entry = s->selected_entry = te;
2680 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2681 break; /* jump to this file's entry */
2683 slash = strchr(p, '/');
2684 if (slash)
2685 subpath = strndup(path, slash - path);
2686 else
2687 subpath = strdup(path);
2688 if (subpath == NULL) {
2689 err = got_error_from_errno("strdup");
2690 break;
2693 err = got_object_id_by_path(&tree_id, s->repo, commit,
2694 subpath);
2695 if (err)
2696 break;
2698 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2699 free(tree_id);
2700 if (err)
2701 break;
2703 err = tree_view_visit_subtree(s, tree);
2704 if (err) {
2705 got_object_tree_close(tree);
2706 break;
2708 if (slash == NULL)
2709 break;
2710 free(subpath);
2711 subpath = NULL;
2712 p = slash;
2715 free(subpath);
2716 return err;
2719 static const struct got_error *
2720 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2721 struct commit_queue_entry *entry, const char *path,
2722 const char *head_ref_name, struct got_repository *repo)
2724 const struct got_error *err = NULL;
2725 struct tog_tree_view_state *s;
2726 struct tog_view *tree_view;
2728 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2729 if (tree_view == NULL)
2730 return got_error_from_errno("view_open");
2732 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2733 if (err)
2734 return err;
2735 s = &tree_view->state.tree;
2737 *new_view = tree_view;
2739 if (got_path_is_root_dir(path))
2740 return NULL;
2742 return tree_view_walk_path(s, entry->commit, path);
2745 static const struct got_error *
2746 block_signals_used_by_main_thread(void)
2748 sigset_t sigset;
2749 int errcode;
2751 if (sigemptyset(&sigset) == -1)
2752 return got_error_from_errno("sigemptyset");
2754 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2755 if (sigaddset(&sigset, SIGWINCH) == -1)
2756 return got_error_from_errno("sigaddset");
2757 if (sigaddset(&sigset, SIGCONT) == -1)
2758 return got_error_from_errno("sigaddset");
2759 if (sigaddset(&sigset, SIGINT) == -1)
2760 return got_error_from_errno("sigaddset");
2761 if (sigaddset(&sigset, SIGTERM) == -1)
2762 return got_error_from_errno("sigaddset");
2764 /* ncurses handles SIGTSTP */
2765 if (sigaddset(&sigset, SIGTSTP) == -1)
2766 return got_error_from_errno("sigaddset");
2768 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2769 if (errcode)
2770 return got_error_set_errno(errcode, "pthread_sigmask");
2772 return NULL;
2775 static void *
2776 log_thread(void *arg)
2778 const struct got_error *err = NULL;
2779 int errcode = 0;
2780 struct tog_log_thread_args *a = arg;
2781 int done = 0;
2784 * Sync startup with main thread such that we begin our
2785 * work once view_input() has released the mutex.
2787 errcode = pthread_mutex_lock(&tog_mutex);
2788 if (errcode) {
2789 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2790 return (void *)err;
2793 err = block_signals_used_by_main_thread();
2794 if (err) {
2795 pthread_mutex_unlock(&tog_mutex);
2796 goto done;
2799 while (!done && !err && !tog_fatal_signal_received()) {
2800 errcode = pthread_mutex_unlock(&tog_mutex);
2801 if (errcode) {
2802 err = got_error_set_errno(errcode,
2803 "pthread_mutex_unlock");
2804 goto done;
2806 err = queue_commits(a);
2807 if (err) {
2808 if (err->code != GOT_ERR_ITER_COMPLETED)
2809 goto done;
2810 err = NULL;
2811 done = 1;
2812 } else if (a->commits_needed > 0 && !a->load_all) {
2813 if (*a->limiting) {
2814 if (a->limit_match)
2815 a->commits_needed--;
2816 } else
2817 a->commits_needed--;
2820 errcode = pthread_mutex_lock(&tog_mutex);
2821 if (errcode) {
2822 err = got_error_set_errno(errcode,
2823 "pthread_mutex_lock");
2824 goto done;
2825 } else if (*a->quit)
2826 done = 1;
2827 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2828 *a->first_displayed_entry =
2829 TAILQ_FIRST(&a->limit_commits->head);
2830 *a->selected_entry = *a->first_displayed_entry;
2831 } else if (*a->first_displayed_entry == NULL) {
2832 *a->first_displayed_entry =
2833 TAILQ_FIRST(&a->real_commits->head);
2834 *a->selected_entry = *a->first_displayed_entry;
2837 errcode = pthread_cond_signal(&a->commit_loaded);
2838 if (errcode) {
2839 err = got_error_set_errno(errcode,
2840 "pthread_cond_signal");
2841 pthread_mutex_unlock(&tog_mutex);
2842 goto done;
2845 if (done)
2846 a->commits_needed = 0;
2847 else {
2848 if (a->commits_needed == 0 && !a->load_all) {
2849 errcode = pthread_cond_wait(&a->need_commits,
2850 &tog_mutex);
2851 if (errcode) {
2852 err = got_error_set_errno(errcode,
2853 "pthread_cond_wait");
2854 pthread_mutex_unlock(&tog_mutex);
2855 goto done;
2857 if (*a->quit)
2858 done = 1;
2862 a->log_complete = 1;
2863 errcode = pthread_mutex_unlock(&tog_mutex);
2864 if (errcode)
2865 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2866 done:
2867 if (err) {
2868 tog_thread_error = 1;
2869 pthread_cond_signal(&a->commit_loaded);
2871 return (void *)err;
2874 static const struct got_error *
2875 stop_log_thread(struct tog_log_view_state *s)
2877 const struct got_error *err = NULL, *thread_err = NULL;
2878 int errcode;
2880 if (s->thread) {
2881 s->quit = 1;
2882 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2883 if (errcode)
2884 return got_error_set_errno(errcode,
2885 "pthread_cond_signal");
2886 errcode = pthread_mutex_unlock(&tog_mutex);
2887 if (errcode)
2888 return got_error_set_errno(errcode,
2889 "pthread_mutex_unlock");
2890 errcode = pthread_join(s->thread, (void **)&thread_err);
2891 if (errcode)
2892 return got_error_set_errno(errcode, "pthread_join");
2893 errcode = pthread_mutex_lock(&tog_mutex);
2894 if (errcode)
2895 return got_error_set_errno(errcode,
2896 "pthread_mutex_lock");
2897 s->thread = NULL;
2900 if (s->thread_args.repo) {
2901 err = got_repo_close(s->thread_args.repo);
2902 s->thread_args.repo = NULL;
2905 if (s->thread_args.pack_fds) {
2906 const struct got_error *pack_err =
2907 got_repo_pack_fds_close(s->thread_args.pack_fds);
2908 if (err == NULL)
2909 err = pack_err;
2910 s->thread_args.pack_fds = NULL;
2913 if (s->thread_args.graph) {
2914 got_commit_graph_close(s->thread_args.graph);
2915 s->thread_args.graph = NULL;
2918 return err ? err : thread_err;
2921 static const struct got_error *
2922 close_log_view(struct tog_view *view)
2924 const struct got_error *err = NULL;
2925 struct tog_log_view_state *s = &view->state.log;
2926 int errcode;
2928 err = stop_log_thread(s);
2930 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2931 if (errcode && err == NULL)
2932 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2934 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2935 if (errcode && err == NULL)
2936 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2938 free_commits(&s->limit_commits);
2939 free_commits(&s->real_commits);
2940 free(s->in_repo_path);
2941 s->in_repo_path = NULL;
2942 free(s->start_id);
2943 s->start_id = NULL;
2944 free(s->head_ref_name);
2945 s->head_ref_name = NULL;
2946 return err;
2950 * We use two queues to implement the limit feature: first consists of
2951 * commits matching the current limit_regex; second is the real queue
2952 * of all known commits (real_commits). When the user starts limiting,
2953 * we swap queues such that all movement and displaying functionality
2954 * works with very slight change.
2956 static const struct got_error *
2957 limit_log_view(struct tog_view *view)
2959 struct tog_log_view_state *s = &view->state.log;
2960 struct commit_queue_entry *entry;
2961 struct tog_view *v = view;
2962 const struct got_error *err = NULL;
2963 char pattern[1024];
2964 int ret;
2966 if (view_is_hsplit_top(view))
2967 v = view->child;
2968 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2969 v = view->parent;
2971 /* Get the pattern */
2972 wmove(v->window, v->nlines - 1, 0);
2973 wclrtoeol(v->window);
2974 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2975 nodelay(v->window, FALSE);
2976 nocbreak();
2977 echo();
2978 ret = wgetnstr(v->window, pattern, sizeof(pattern));
2979 cbreak();
2980 noecho();
2981 nodelay(v->window, TRUE);
2982 if (ret == ERR)
2983 return NULL;
2985 if (*pattern == '\0') {
2987 * Safety measure for the situation where the user
2988 * resets limit without previously limiting anything.
2990 if (!s->limit_view)
2991 return NULL;
2994 * User could have pressed Ctrl+L, which refreshed the
2995 * commit queues, it means we can't save previously
2996 * (before limit took place) displayed entries,
2997 * because they would point to already free'ed memory,
2998 * so we are forced to always select first entry of
2999 * the queue.
3001 s->commits = &s->real_commits;
3002 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3003 s->selected_entry = s->first_displayed_entry;
3004 s->selected = 0;
3005 s->limit_view = 0;
3007 return NULL;
3010 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3011 return NULL;
3013 s->limit_view = 1;
3015 /* Clear the screen while loading limit view */
3016 s->first_displayed_entry = NULL;
3017 s->last_displayed_entry = NULL;
3018 s->selected_entry = NULL;
3019 s->commits = &s->limit_commits;
3021 /* Prepare limit queue for new search */
3022 free_commits(&s->limit_commits);
3023 s->limit_commits.ncommits = 0;
3025 /* First process commits, which are in queue already */
3026 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3027 int have_match = 0;
3029 err = match_commit(&have_match, entry->id,
3030 entry->commit, &s->limit_regex);
3031 if (err)
3032 return err;
3034 if (have_match) {
3035 struct commit_queue_entry *matched;
3037 matched = alloc_commit_queue_entry(entry->commit,
3038 entry->id);
3039 if (matched == NULL) {
3040 err = got_error_from_errno(
3041 "alloc_commit_queue_entry");
3042 break;
3045 err = got_object_commit_dup(&matched->commit,
3046 entry->commit);
3047 if (err) {
3048 free(matched);
3049 return err;
3052 matched->idx = s->limit_commits.ncommits;
3053 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3054 matched, entry);
3055 s->limit_commits.ncommits++;
3059 /* Second process all the commits, until we fill the screen */
3060 if (s->limit_commits.ncommits < view->nlines - 1 &&
3061 !s->thread_args.log_complete) {
3062 s->thread_args.commits_needed +=
3063 view->nlines - s->limit_commits.ncommits - 1;
3064 err = trigger_log_thread(view, 1);
3065 if (err)
3066 return err;
3069 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3070 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3071 s->selected = 0;
3073 return NULL;
3076 static const struct got_error *
3077 search_start_log_view(struct tog_view *view)
3079 struct tog_log_view_state *s = &view->state.log;
3081 s->matched_entry = NULL;
3082 s->search_entry = NULL;
3083 return NULL;
3086 static const struct got_error *
3087 search_next_log_view(struct tog_view *view)
3089 const struct got_error *err = NULL;
3090 struct tog_log_view_state *s = &view->state.log;
3091 struct commit_queue_entry *entry;
3093 /* Display progress update in log view. */
3094 show_log_view(view);
3095 update_panels();
3096 doupdate();
3098 if (s->search_entry) {
3099 int errcode, ch;
3100 errcode = pthread_mutex_unlock(&tog_mutex);
3101 if (errcode)
3102 return got_error_set_errno(errcode,
3103 "pthread_mutex_unlock");
3104 ch = wgetch(view->window);
3105 errcode = pthread_mutex_lock(&tog_mutex);
3106 if (errcode)
3107 return got_error_set_errno(errcode,
3108 "pthread_mutex_lock");
3109 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3110 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3111 return NULL;
3113 if (view->searching == TOG_SEARCH_FORWARD)
3114 entry = TAILQ_NEXT(s->search_entry, entry);
3115 else
3116 entry = TAILQ_PREV(s->search_entry,
3117 commit_queue_head, entry);
3118 } else if (s->matched_entry) {
3120 * If the user has moved the cursor after we hit a match,
3121 * the position from where we should continue searching
3122 * might have changed.
3124 if (view->searching == TOG_SEARCH_FORWARD)
3125 entry = TAILQ_NEXT(s->selected_entry, entry);
3126 else
3127 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3128 entry);
3129 } else {
3130 entry = s->selected_entry;
3133 while (1) {
3134 int have_match = 0;
3136 if (entry == NULL) {
3137 if (s->thread_args.log_complete ||
3138 view->searching == TOG_SEARCH_BACKWARD) {
3139 view->search_next_done =
3140 (s->matched_entry == NULL ?
3141 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3142 s->search_entry = NULL;
3143 return NULL;
3146 * Poke the log thread for more commits and return,
3147 * allowing the main loop to make progress. Search
3148 * will resume at s->search_entry once we come back.
3150 s->thread_args.commits_needed++;
3151 return trigger_log_thread(view, 0);
3154 err = match_commit(&have_match, entry->id, entry->commit,
3155 &view->regex);
3156 if (err)
3157 break;
3158 if (have_match) {
3159 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3160 s->matched_entry = entry;
3161 break;
3164 s->search_entry = entry;
3165 if (view->searching == TOG_SEARCH_FORWARD)
3166 entry = TAILQ_NEXT(entry, entry);
3167 else
3168 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3171 if (s->matched_entry) {
3172 int cur = s->selected_entry->idx;
3173 while (cur < s->matched_entry->idx) {
3174 err = input_log_view(NULL, view, KEY_DOWN);
3175 if (err)
3176 return err;
3177 cur++;
3179 while (cur > s->matched_entry->idx) {
3180 err = input_log_view(NULL, view, KEY_UP);
3181 if (err)
3182 return err;
3183 cur--;
3187 s->search_entry = NULL;
3189 return NULL;
3192 static const struct got_error *
3193 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3194 struct got_repository *repo, const char *head_ref_name,
3195 const char *in_repo_path, int log_branches)
3197 const struct got_error *err = NULL;
3198 struct tog_log_view_state *s = &view->state.log;
3199 struct got_repository *thread_repo = NULL;
3200 struct got_commit_graph *thread_graph = NULL;
3201 int errcode;
3203 if (in_repo_path != s->in_repo_path) {
3204 free(s->in_repo_path);
3205 s->in_repo_path = strdup(in_repo_path);
3206 if (s->in_repo_path == NULL)
3207 return got_error_from_errno("strdup");
3210 /* The commit queue only contains commits being displayed. */
3211 TAILQ_INIT(&s->real_commits.head);
3212 s->real_commits.ncommits = 0;
3213 s->commits = &s->real_commits;
3215 TAILQ_INIT(&s->limit_commits.head);
3216 s->limit_view = 0;
3217 s->limit_commits.ncommits = 0;
3219 s->repo = repo;
3220 if (head_ref_name) {
3221 s->head_ref_name = strdup(head_ref_name);
3222 if (s->head_ref_name == NULL) {
3223 err = got_error_from_errno("strdup");
3224 goto done;
3227 s->start_id = got_object_id_dup(start_id);
3228 if (s->start_id == NULL) {
3229 err = got_error_from_errno("got_object_id_dup");
3230 goto done;
3232 s->log_branches = log_branches;
3234 STAILQ_INIT(&s->colors);
3235 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3236 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3237 get_color_value("TOG_COLOR_COMMIT"));
3238 if (err)
3239 goto done;
3240 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3241 get_color_value("TOG_COLOR_AUTHOR"));
3242 if (err) {
3243 free_colors(&s->colors);
3244 goto done;
3246 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3247 get_color_value("TOG_COLOR_DATE"));
3248 if (err) {
3249 free_colors(&s->colors);
3250 goto done;
3254 view->show = show_log_view;
3255 view->input = input_log_view;
3256 view->resize = resize_log_view;
3257 view->close = close_log_view;
3258 view->search_start = search_start_log_view;
3259 view->search_next = search_next_log_view;
3261 if (s->thread_args.pack_fds == NULL) {
3262 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3263 if (err)
3264 goto done;
3266 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3267 s->thread_args.pack_fds);
3268 if (err)
3269 goto done;
3270 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3271 !s->log_branches);
3272 if (err)
3273 goto done;
3274 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3275 s->repo, NULL, NULL);
3276 if (err)
3277 goto done;
3279 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3280 if (errcode) {
3281 err = got_error_set_errno(errcode, "pthread_cond_init");
3282 goto done;
3284 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3285 if (errcode) {
3286 err = got_error_set_errno(errcode, "pthread_cond_init");
3287 goto done;
3290 s->thread_args.commits_needed = view->nlines;
3291 s->thread_args.graph = thread_graph;
3292 s->thread_args.real_commits = &s->real_commits;
3293 s->thread_args.limit_commits = &s->limit_commits;
3294 s->thread_args.in_repo_path = s->in_repo_path;
3295 s->thread_args.start_id = s->start_id;
3296 s->thread_args.repo = thread_repo;
3297 s->thread_args.log_complete = 0;
3298 s->thread_args.quit = &s->quit;
3299 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3300 s->thread_args.selected_entry = &s->selected_entry;
3301 s->thread_args.searching = &view->searching;
3302 s->thread_args.search_next_done = &view->search_next_done;
3303 s->thread_args.regex = &view->regex;
3304 s->thread_args.limiting = &s->limit_view;
3305 s->thread_args.limit_regex = &s->limit_regex;
3306 s->thread_args.limit_commits = &s->limit_commits;
3307 done:
3308 if (err)
3309 close_log_view(view);
3310 return err;
3313 static const struct got_error *
3314 show_log_view(struct tog_view *view)
3316 const struct got_error *err;
3317 struct tog_log_view_state *s = &view->state.log;
3319 if (s->thread == NULL) {
3320 int errcode = pthread_create(&s->thread, NULL, log_thread,
3321 &s->thread_args);
3322 if (errcode)
3323 return got_error_set_errno(errcode, "pthread_create");
3324 if (s->thread_args.commits_needed > 0) {
3325 err = trigger_log_thread(view, 1);
3326 if (err)
3327 return err;
3331 return draw_commits(view);
3334 static void
3335 log_move_cursor_up(struct tog_view *view, int page, int home)
3337 struct tog_log_view_state *s = &view->state.log;
3339 if (s->first_displayed_entry == NULL)
3340 return;
3341 if (s->selected_entry->idx == 0)
3342 view->count = 0;
3344 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3345 || home)
3346 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3348 if (!page && !home && s->selected > 0)
3349 --s->selected;
3350 else
3351 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3353 select_commit(s);
3354 return;
3357 static const struct got_error *
3358 log_move_cursor_down(struct tog_view *view, int page)
3360 struct tog_log_view_state *s = &view->state.log;
3361 const struct got_error *err = NULL;
3362 int eos = view->nlines - 2;
3364 if (s->first_displayed_entry == NULL)
3365 return NULL;
3367 if (s->thread_args.log_complete &&
3368 s->selected_entry->idx >= s->commits->ncommits - 1)
3369 return NULL;
3371 if (view_is_hsplit_top(view))
3372 --eos; /* border consumes the last line */
3374 if (!page) {
3375 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3376 ++s->selected;
3377 else
3378 err = log_scroll_down(view, 1);
3379 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3380 struct commit_queue_entry *entry;
3381 int n;
3383 s->selected = 0;
3384 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3385 s->last_displayed_entry = entry;
3386 for (n = 0; n <= eos; n++) {
3387 if (entry == NULL)
3388 break;
3389 s->first_displayed_entry = entry;
3390 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3392 if (n > 0)
3393 s->selected = n - 1;
3394 } else {
3395 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3396 s->thread_args.log_complete)
3397 s->selected += MIN(page,
3398 s->commits->ncommits - s->selected_entry->idx - 1);
3399 else
3400 err = log_scroll_down(view, page);
3402 if (err)
3403 return err;
3406 * We might necessarily overshoot in horizontal
3407 * splits; if so, select the last displayed commit.
3409 if (s->first_displayed_entry && s->last_displayed_entry) {
3410 s->selected = MIN(s->selected,
3411 s->last_displayed_entry->idx -
3412 s->first_displayed_entry->idx);
3415 select_commit(s);
3417 if (s->thread_args.log_complete &&
3418 s->selected_entry->idx == s->commits->ncommits - 1)
3419 view->count = 0;
3421 return NULL;
3424 static void
3425 view_get_split(struct tog_view *view, int *y, int *x)
3427 *x = 0;
3428 *y = 0;
3430 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3431 if (view->child && view->child->resized_y)
3432 *y = view->child->resized_y;
3433 else if (view->resized_y)
3434 *y = view->resized_y;
3435 else
3436 *y = view_split_begin_y(view->lines);
3437 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3438 if (view->child && view->child->resized_x)
3439 *x = view->child->resized_x;
3440 else if (view->resized_x)
3441 *x = view->resized_x;
3442 else
3443 *x = view_split_begin_x(view->begin_x);
3447 /* Split view horizontally at y and offset view->state->selected line. */
3448 static const struct got_error *
3449 view_init_hsplit(struct tog_view *view, int y)
3451 const struct got_error *err = NULL;
3453 view->nlines = y;
3454 view->ncols = COLS;
3455 err = view_resize(view);
3456 if (err)
3457 return err;
3459 err = offset_selection_down(view);
3461 return err;
3464 static const struct got_error *
3465 log_goto_line(struct tog_view *view, int nlines)
3467 const struct got_error *err = NULL;
3468 struct tog_log_view_state *s = &view->state.log;
3469 int g, idx = s->selected_entry->idx;
3471 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3472 return NULL;
3474 g = view->gline;
3475 view->gline = 0;
3477 if (g >= s->first_displayed_entry->idx + 1 &&
3478 g <= s->last_displayed_entry->idx + 1 &&
3479 g - s->first_displayed_entry->idx - 1 < nlines) {
3480 s->selected = g - s->first_displayed_entry->idx - 1;
3481 select_commit(s);
3482 return NULL;
3485 if (idx + 1 < g) {
3486 err = log_move_cursor_down(view, g - idx - 1);
3487 if (!err && g > s->selected_entry->idx + 1)
3488 err = log_move_cursor_down(view,
3489 g - s->first_displayed_entry->idx - 1);
3490 if (err)
3491 return err;
3492 } else if (idx + 1 > g)
3493 log_move_cursor_up(view, idx - g + 1, 0);
3495 if (g < nlines && s->first_displayed_entry->idx == 0)
3496 s->selected = g - 1;
3498 select_commit(s);
3499 return NULL;
3503 static const struct got_error *
3504 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3506 const struct got_error *err = NULL;
3507 struct tog_log_view_state *s = &view->state.log;
3508 int eos, nscroll;
3510 if (s->thread_args.load_all) {
3511 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3512 s->thread_args.load_all = 0;
3513 else if (s->thread_args.log_complete) {
3514 err = log_move_cursor_down(view, s->commits->ncommits);
3515 s->thread_args.load_all = 0;
3517 if (err)
3518 return err;
3521 eos = nscroll = view->nlines - 1;
3522 if (view_is_hsplit_top(view))
3523 --eos; /* border */
3525 if (view->gline)
3526 return log_goto_line(view, eos);
3528 switch (ch) {
3529 case '&':
3530 err = limit_log_view(view);
3531 break;
3532 case 'q':
3533 s->quit = 1;
3534 break;
3535 case '0':
3536 view->x = 0;
3537 break;
3538 case '$':
3539 view->x = MAX(view->maxx - view->ncols / 2, 0);
3540 view->count = 0;
3541 break;
3542 case KEY_RIGHT:
3543 case 'l':
3544 if (view->x + view->ncols / 2 < view->maxx)
3545 view->x += 2; /* move two columns right */
3546 else
3547 view->count = 0;
3548 break;
3549 case KEY_LEFT:
3550 case 'h':
3551 view->x -= MIN(view->x, 2); /* move two columns back */
3552 if (view->x <= 0)
3553 view->count = 0;
3554 break;
3555 case 'k':
3556 case KEY_UP:
3557 case '<':
3558 case ',':
3559 case CTRL('p'):
3560 log_move_cursor_up(view, 0, 0);
3561 break;
3562 case 'g':
3563 case KEY_HOME:
3564 log_move_cursor_up(view, 0, 1);
3565 view->count = 0;
3566 break;
3567 case CTRL('u'):
3568 case 'u':
3569 nscroll /= 2;
3570 /* FALL THROUGH */
3571 case KEY_PPAGE:
3572 case CTRL('b'):
3573 case 'b':
3574 log_move_cursor_up(view, nscroll, 0);
3575 break;
3576 case 'j':
3577 case KEY_DOWN:
3578 case '>':
3579 case '.':
3580 case CTRL('n'):
3581 err = log_move_cursor_down(view, 0);
3582 break;
3583 case '@':
3584 s->use_committer = !s->use_committer;
3585 break;
3586 case 'G':
3587 case KEY_END: {
3588 /* We don't know yet how many commits, so we're forced to
3589 * traverse them all. */
3590 view->count = 0;
3591 s->thread_args.load_all = 1;
3592 if (!s->thread_args.log_complete)
3593 return trigger_log_thread(view, 0);
3594 err = log_move_cursor_down(view, s->commits->ncommits);
3595 s->thread_args.load_all = 0;
3596 break;
3598 case CTRL('d'):
3599 case 'd':
3600 nscroll /= 2;
3601 /* FALL THROUGH */
3602 case KEY_NPAGE:
3603 case CTRL('f'):
3604 case 'f':
3605 case ' ':
3606 err = log_move_cursor_down(view, nscroll);
3607 break;
3608 case KEY_RESIZE:
3609 if (s->selected > view->nlines - 2)
3610 s->selected = view->nlines - 2;
3611 if (s->selected > s->commits->ncommits - 1)
3612 s->selected = s->commits->ncommits - 1;
3613 select_commit(s);
3614 if (s->commits->ncommits < view->nlines - 1 &&
3615 !s->thread_args.log_complete) {
3616 s->thread_args.commits_needed += (view->nlines - 1) -
3617 s->commits->ncommits;
3618 err = trigger_log_thread(view, 1);
3620 break;
3621 case KEY_ENTER:
3622 case '\r':
3623 view->count = 0;
3624 if (s->selected_entry == NULL)
3625 break;
3626 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3627 break;
3628 case 'T':
3629 view->count = 0;
3630 if (s->selected_entry == NULL)
3631 break;
3632 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3633 break;
3634 case KEY_BACKSPACE:
3635 case CTRL('l'):
3636 case 'B':
3637 view->count = 0;
3638 if (ch == KEY_BACKSPACE &&
3639 got_path_is_root_dir(s->in_repo_path))
3640 break;
3641 err = stop_log_thread(s);
3642 if (err)
3643 return err;
3644 if (ch == KEY_BACKSPACE) {
3645 char *parent_path;
3646 err = got_path_dirname(&parent_path, s->in_repo_path);
3647 if (err)
3648 return err;
3649 free(s->in_repo_path);
3650 s->in_repo_path = parent_path;
3651 s->thread_args.in_repo_path = s->in_repo_path;
3652 } else if (ch == CTRL('l')) {
3653 struct got_object_id *start_id;
3654 err = got_repo_match_object_id(&start_id, NULL,
3655 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3656 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3657 if (err)
3658 return err;
3659 free(s->start_id);
3660 s->start_id = start_id;
3661 s->thread_args.start_id = s->start_id;
3662 } else /* 'B' */
3663 s->log_branches = !s->log_branches;
3665 if (s->thread_args.pack_fds == NULL) {
3666 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3667 if (err)
3668 return err;
3670 err = got_repo_open(&s->thread_args.repo,
3671 got_repo_get_path(s->repo), NULL,
3672 s->thread_args.pack_fds);
3673 if (err)
3674 return err;
3675 tog_free_refs();
3676 err = tog_load_refs(s->repo, 0);
3677 if (err)
3678 return err;
3679 err = got_commit_graph_open(&s->thread_args.graph,
3680 s->in_repo_path, !s->log_branches);
3681 if (err)
3682 return err;
3683 err = got_commit_graph_iter_start(s->thread_args.graph,
3684 s->start_id, s->repo, NULL, NULL);
3685 if (err)
3686 return err;
3687 free_commits(&s->real_commits);
3688 free_commits(&s->limit_commits);
3689 s->first_displayed_entry = NULL;
3690 s->last_displayed_entry = NULL;
3691 s->selected_entry = NULL;
3692 s->selected = 0;
3693 s->thread_args.log_complete = 0;
3694 s->quit = 0;
3695 s->thread_args.commits_needed = view->lines;
3696 s->matched_entry = NULL;
3697 s->search_entry = NULL;
3698 view->offset = 0;
3699 break;
3700 case 'R':
3701 view->count = 0;
3702 err = view_request_new(new_view, view, TOG_VIEW_REF);
3703 break;
3704 default:
3705 view->count = 0;
3706 break;
3709 return err;
3712 static const struct got_error *
3713 apply_unveil(const char *repo_path, const char *worktree_path)
3715 const struct got_error *error;
3717 #ifdef PROFILE
3718 if (unveil("gmon.out", "rwc") != 0)
3719 return got_error_from_errno2("unveil", "gmon.out");
3720 #endif
3721 if (repo_path && unveil(repo_path, "r") != 0)
3722 return got_error_from_errno2("unveil", repo_path);
3724 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3725 return got_error_from_errno2("unveil", worktree_path);
3727 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3728 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3730 error = got_privsep_unveil_exec_helpers();
3731 if (error != NULL)
3732 return error;
3734 if (unveil(NULL, NULL) != 0)
3735 return got_error_from_errno("unveil");
3737 return NULL;
3740 static void
3741 init_curses(void)
3744 * Override default signal handlers before starting ncurses.
3745 * This should prevent ncurses from installing its own
3746 * broken cleanup() signal handler.
3748 signal(SIGWINCH, tog_sigwinch);
3749 signal(SIGPIPE, tog_sigpipe);
3750 signal(SIGCONT, tog_sigcont);
3751 signal(SIGINT, tog_sigint);
3752 signal(SIGTERM, tog_sigterm);
3754 initscr();
3755 cbreak();
3756 halfdelay(1); /* Do fast refresh while initial view is loading. */
3757 noecho();
3758 nonl();
3759 intrflush(stdscr, FALSE);
3760 keypad(stdscr, TRUE);
3761 curs_set(0);
3762 if (getenv("TOG_COLORS") != NULL) {
3763 start_color();
3764 use_default_colors();
3768 static const struct got_error *
3769 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3770 struct got_repository *repo, struct got_worktree *worktree)
3772 const struct got_error *err = NULL;
3774 if (argc == 0) {
3775 *in_repo_path = strdup("/");
3776 if (*in_repo_path == NULL)
3777 return got_error_from_errno("strdup");
3778 return NULL;
3781 if (worktree) {
3782 const char *prefix = got_worktree_get_path_prefix(worktree);
3783 char *p;
3785 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3786 if (err)
3787 return err;
3788 if (asprintf(in_repo_path, "%s%s%s", prefix,
3789 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3790 p) == -1) {
3791 err = got_error_from_errno("asprintf");
3792 *in_repo_path = NULL;
3794 free(p);
3795 } else
3796 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3798 return err;
3801 static const struct got_error *
3802 cmd_log(int argc, char *argv[])
3804 const struct got_error *error;
3805 struct got_repository *repo = NULL;
3806 struct got_worktree *worktree = NULL;
3807 struct got_object_id *start_id = NULL;
3808 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3809 char *start_commit = NULL, *label = NULL;
3810 struct got_reference *ref = NULL;
3811 const char *head_ref_name = NULL;
3812 int ch, log_branches = 0;
3813 struct tog_view *view;
3814 int *pack_fds = NULL;
3816 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3817 switch (ch) {
3818 case 'b':
3819 log_branches = 1;
3820 break;
3821 case 'c':
3822 start_commit = optarg;
3823 break;
3824 case 'r':
3825 repo_path = realpath(optarg, NULL);
3826 if (repo_path == NULL)
3827 return got_error_from_errno2("realpath",
3828 optarg);
3829 break;
3830 default:
3831 usage_log();
3832 /* NOTREACHED */
3836 argc -= optind;
3837 argv += optind;
3839 if (argc > 1)
3840 usage_log();
3842 error = got_repo_pack_fds_open(&pack_fds);
3843 if (error != NULL)
3844 goto done;
3846 if (repo_path == NULL) {
3847 cwd = getcwd(NULL, 0);
3848 if (cwd == NULL)
3849 return got_error_from_errno("getcwd");
3850 error = got_worktree_open(&worktree, cwd);
3851 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3852 goto done;
3853 if (worktree)
3854 repo_path =
3855 strdup(got_worktree_get_repo_path(worktree));
3856 else
3857 repo_path = strdup(cwd);
3858 if (repo_path == NULL) {
3859 error = got_error_from_errno("strdup");
3860 goto done;
3864 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3865 if (error != NULL)
3866 goto done;
3868 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3869 repo, worktree);
3870 if (error)
3871 goto done;
3873 init_curses();
3875 error = apply_unveil(got_repo_get_path(repo),
3876 worktree ? got_worktree_get_root_path(worktree) : NULL);
3877 if (error)
3878 goto done;
3880 /* already loaded by tog_log_with_path()? */
3881 if (TAILQ_EMPTY(&tog_refs)) {
3882 error = tog_load_refs(repo, 0);
3883 if (error)
3884 goto done;
3887 if (start_commit == NULL) {
3888 error = got_repo_match_object_id(&start_id, &label,
3889 worktree ? got_worktree_get_head_ref_name(worktree) :
3890 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3891 if (error)
3892 goto done;
3893 head_ref_name = label;
3894 } else {
3895 error = got_ref_open(&ref, repo, start_commit, 0);
3896 if (error == NULL)
3897 head_ref_name = got_ref_get_name(ref);
3898 else if (error->code != GOT_ERR_NOT_REF)
3899 goto done;
3900 error = got_repo_match_object_id(&start_id, NULL,
3901 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3902 if (error)
3903 goto done;
3906 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3907 if (view == NULL) {
3908 error = got_error_from_errno("view_open");
3909 goto done;
3911 error = open_log_view(view, start_id, repo, head_ref_name,
3912 in_repo_path, log_branches);
3913 if (error)
3914 goto done;
3915 if (worktree) {
3916 /* Release work tree lock. */
3917 got_worktree_close(worktree);
3918 worktree = NULL;
3920 error = view_loop(view);
3921 done:
3922 free(in_repo_path);
3923 free(repo_path);
3924 free(cwd);
3925 free(start_id);
3926 free(label);
3927 if (ref)
3928 got_ref_close(ref);
3929 if (repo) {
3930 const struct got_error *close_err = got_repo_close(repo);
3931 if (error == NULL)
3932 error = close_err;
3934 if (worktree)
3935 got_worktree_close(worktree);
3936 if (pack_fds) {
3937 const struct got_error *pack_err =
3938 got_repo_pack_fds_close(pack_fds);
3939 if (error == NULL)
3940 error = pack_err;
3942 tog_free_refs();
3943 return error;
3946 __dead static void
3947 usage_diff(void)
3949 endwin();
3950 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3951 "object1 object2\n", getprogname());
3952 exit(1);
3955 static int
3956 match_line(const char *line, regex_t *regex, size_t nmatch,
3957 regmatch_t *regmatch)
3959 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3962 static struct tog_color *
3963 match_color(struct tog_colors *colors, const char *line)
3965 struct tog_color *tc = NULL;
3967 STAILQ_FOREACH(tc, colors, entry) {
3968 if (match_line(line, &tc->regex, 0, NULL))
3969 return tc;
3972 return NULL;
3975 static const struct got_error *
3976 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3977 WINDOW *window, int skipcol, regmatch_t *regmatch)
3979 const struct got_error *err = NULL;
3980 char *exstr = NULL;
3981 wchar_t *wline = NULL;
3982 int rme, rms, n, width, scrollx;
3983 int width0 = 0, width1 = 0, width2 = 0;
3984 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3986 *wtotal = 0;
3988 rms = regmatch->rm_so;
3989 rme = regmatch->rm_eo;
3991 err = expand_tab(&exstr, line);
3992 if (err)
3993 return err;
3995 /* Split the line into 3 segments, according to match offsets. */
3996 seg0 = strndup(exstr, rms);
3997 if (seg0 == NULL) {
3998 err = got_error_from_errno("strndup");
3999 goto done;
4001 seg1 = strndup(exstr + rms, rme - rms);
4002 if (seg1 == NULL) {
4003 err = got_error_from_errno("strndup");
4004 goto done;
4006 seg2 = strdup(exstr + rme);
4007 if (seg2 == NULL) {
4008 err = got_error_from_errno("strndup");
4009 goto done;
4012 /* draw up to matched token if we haven't scrolled past it */
4013 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4014 col_tab_align, 1);
4015 if (err)
4016 goto done;
4017 n = MAX(width0 - skipcol, 0);
4018 if (n) {
4019 free(wline);
4020 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4021 wlimit, col_tab_align, 1);
4022 if (err)
4023 goto done;
4024 waddwstr(window, &wline[scrollx]);
4025 wlimit -= width;
4026 *wtotal += width;
4029 if (wlimit > 0) {
4030 int i = 0, w = 0;
4031 size_t wlen;
4033 free(wline);
4034 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4035 col_tab_align, 1);
4036 if (err)
4037 goto done;
4038 wlen = wcslen(wline);
4039 while (i < wlen) {
4040 width = wcwidth(wline[i]);
4041 if (width == -1) {
4042 /* should not happen, tabs are expanded */
4043 err = got_error(GOT_ERR_RANGE);
4044 goto done;
4046 if (width0 + w + width > skipcol)
4047 break;
4048 w += width;
4049 i++;
4051 /* draw (visible part of) matched token (if scrolled into it) */
4052 if (width1 - w > 0) {
4053 wattron(window, A_STANDOUT);
4054 waddwstr(window, &wline[i]);
4055 wattroff(window, A_STANDOUT);
4056 wlimit -= (width1 - w);
4057 *wtotal += (width1 - w);
4061 if (wlimit > 0) { /* draw rest of line */
4062 free(wline);
4063 if (skipcol > width0 + width1) {
4064 err = format_line(&wline, &width2, &scrollx, seg2,
4065 skipcol - (width0 + width1), wlimit,
4066 col_tab_align, 1);
4067 if (err)
4068 goto done;
4069 waddwstr(window, &wline[scrollx]);
4070 } else {
4071 err = format_line(&wline, &width2, NULL, seg2, 0,
4072 wlimit, col_tab_align, 1);
4073 if (err)
4074 goto done;
4075 waddwstr(window, wline);
4077 *wtotal += width2;
4079 done:
4080 free(wline);
4081 free(exstr);
4082 free(seg0);
4083 free(seg1);
4084 free(seg2);
4085 return err;
4088 static int
4089 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4091 FILE *f = NULL;
4092 int *eof, *first, *selected;
4094 if (view->type == TOG_VIEW_DIFF) {
4095 struct tog_diff_view_state *s = &view->state.diff;
4097 first = &s->first_displayed_line;
4098 selected = first;
4099 eof = &s->eof;
4100 f = s->f;
4101 } else if (view->type == TOG_VIEW_BLAME) {
4102 struct tog_blame_view_state *s = &view->state.blame;
4104 first = &s->first_displayed_line;
4105 selected = &s->selected_line;
4106 eof = &s->eof;
4107 f = s->blame.f;
4108 } else
4109 return 0;
4111 /* Center gline in the middle of the page like vi(1). */
4112 if (*lineno < view->gline - (view->nlines - 3) / 2)
4113 return 0;
4114 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4115 rewind(f);
4116 *eof = 0;
4117 *first = 1;
4118 *lineno = 0;
4119 *nprinted = 0;
4120 return 0;
4123 *selected = view->gline <= (view->nlines - 3) / 2 ?
4124 view->gline : (view->nlines - 3) / 2 + 1;
4125 view->gline = 0;
4127 return 1;
4130 static const struct got_error *
4131 draw_file(struct tog_view *view, const char *header)
4133 struct tog_diff_view_state *s = &view->state.diff;
4134 regmatch_t *regmatch = &view->regmatch;
4135 const struct got_error *err;
4136 int nprinted = 0;
4137 char *line;
4138 size_t linesize = 0;
4139 ssize_t linelen;
4140 wchar_t *wline;
4141 int width;
4142 int max_lines = view->nlines;
4143 int nlines = s->nlines;
4144 off_t line_offset;
4146 s->lineno = s->first_displayed_line - 1;
4147 line_offset = s->lines[s->first_displayed_line - 1].offset;
4148 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4149 return got_error_from_errno("fseek");
4151 werase(view->window);
4153 if (view->gline > s->nlines - 1)
4154 view->gline = s->nlines - 1;
4156 if (header) {
4157 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4158 1 : view->gline - (view->nlines - 3) / 2 :
4159 s->lineno + s->selected_line;
4161 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4162 return got_error_from_errno("asprintf");
4163 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4164 0, 0);
4165 free(line);
4166 if (err)
4167 return err;
4169 if (view_needs_focus_indication(view))
4170 wstandout(view->window);
4171 waddwstr(view->window, wline);
4172 free(wline);
4173 wline = NULL;
4174 while (width++ < view->ncols)
4175 waddch(view->window, ' ');
4176 if (view_needs_focus_indication(view))
4177 wstandend(view->window);
4179 if (max_lines <= 1)
4180 return NULL;
4181 max_lines--;
4184 s->eof = 0;
4185 view->maxx = 0;
4186 line = NULL;
4187 while (max_lines > 0 && nprinted < max_lines) {
4188 enum got_diff_line_type linetype;
4189 attr_t attr = 0;
4191 linelen = getline(&line, &linesize, s->f);
4192 if (linelen == -1) {
4193 if (feof(s->f)) {
4194 s->eof = 1;
4195 break;
4197 free(line);
4198 return got_ferror(s->f, GOT_ERR_IO);
4201 if (++s->lineno < s->first_displayed_line)
4202 continue;
4203 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4204 continue;
4205 if (s->lineno == view->hiline)
4206 attr = A_STANDOUT;
4208 /* Set view->maxx based on full line length. */
4209 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4210 view->x ? 1 : 0);
4211 if (err) {
4212 free(line);
4213 return err;
4215 view->maxx = MAX(view->maxx, width);
4216 free(wline);
4217 wline = NULL;
4219 linetype = s->lines[s->lineno].type;
4220 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4221 linetype < GOT_DIFF_LINE_CONTEXT)
4222 attr |= COLOR_PAIR(linetype);
4223 if (attr)
4224 wattron(view->window, attr);
4225 if (s->first_displayed_line + nprinted == s->matched_line &&
4226 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4227 err = add_matched_line(&width, line, view->ncols, 0,
4228 view->window, view->x, regmatch);
4229 if (err) {
4230 free(line);
4231 return err;
4233 } else {
4234 int skip;
4235 err = format_line(&wline, &width, &skip, line,
4236 view->x, view->ncols, 0, view->x ? 1 : 0);
4237 if (err) {
4238 free(line);
4239 return err;
4241 waddwstr(view->window, &wline[skip]);
4242 free(wline);
4243 wline = NULL;
4245 if (s->lineno == view->hiline) {
4246 /* highlight full gline length */
4247 while (width++ < view->ncols)
4248 waddch(view->window, ' ');
4249 } else {
4250 if (width <= view->ncols - 1)
4251 waddch(view->window, '\n');
4253 if (attr)
4254 wattroff(view->window, attr);
4255 if (++nprinted == 1)
4256 s->first_displayed_line = s->lineno;
4258 free(line);
4259 if (nprinted >= 1)
4260 s->last_displayed_line = s->first_displayed_line +
4261 (nprinted - 1);
4262 else
4263 s->last_displayed_line = s->first_displayed_line;
4265 view_border(view);
4267 if (s->eof) {
4268 while (nprinted < view->nlines) {
4269 waddch(view->window, '\n');
4270 nprinted++;
4273 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4274 view->ncols, 0, 0);
4275 if (err) {
4276 return err;
4279 wstandout(view->window);
4280 waddwstr(view->window, wline);
4281 free(wline);
4282 wline = NULL;
4283 wstandend(view->window);
4286 return NULL;
4289 static char *
4290 get_datestr(time_t *time, char *datebuf)
4292 struct tm mytm, *tm;
4293 char *p, *s;
4295 tm = gmtime_r(time, &mytm);
4296 if (tm == NULL)
4297 return NULL;
4298 s = asctime_r(tm, datebuf);
4299 if (s == NULL)
4300 return NULL;
4301 p = strchr(s, '\n');
4302 if (p)
4303 *p = '\0';
4304 return s;
4307 static const struct got_error *
4308 get_changed_paths(struct got_pathlist_head *paths,
4309 struct got_commit_object *commit, struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4313 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4314 struct got_object_qid *qid;
4316 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4317 if (qid != NULL) {
4318 struct got_commit_object *pcommit;
4319 err = got_object_open_as_commit(&pcommit, repo,
4320 &qid->id);
4321 if (err)
4322 return err;
4324 tree_id1 = got_object_id_dup(
4325 got_object_commit_get_tree_id(pcommit));
4326 if (tree_id1 == NULL) {
4327 got_object_commit_close(pcommit);
4328 return got_error_from_errno("got_object_id_dup");
4330 got_object_commit_close(pcommit);
4334 if (tree_id1) {
4335 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4336 if (err)
4337 goto done;
4340 tree_id2 = got_object_commit_get_tree_id(commit);
4341 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4342 if (err)
4343 goto done;
4345 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4346 got_diff_tree_collect_changed_paths, paths, 0);
4347 done:
4348 if (tree1)
4349 got_object_tree_close(tree1);
4350 if (tree2)
4351 got_object_tree_close(tree2);
4352 free(tree_id1);
4353 return err;
4356 static const struct got_error *
4357 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4358 off_t off, uint8_t type)
4360 struct got_diff_line *p;
4362 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4363 if (p == NULL)
4364 return got_error_from_errno("reallocarray");
4365 *lines = p;
4366 (*lines)[*nlines].offset = off;
4367 (*lines)[*nlines].type = type;
4368 (*nlines)++;
4370 return NULL;
4373 static const struct got_error *
4374 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4375 struct got_object_id *commit_id, struct got_reflist_head *refs,
4376 struct got_repository *repo, FILE *outfile)
4378 const struct got_error *err = NULL;
4379 char datebuf[26], *datestr;
4380 struct got_commit_object *commit;
4381 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4382 time_t committer_time;
4383 const char *author, *committer;
4384 char *refs_str = NULL;
4385 struct got_pathlist_head changed_paths;
4386 struct got_pathlist_entry *pe;
4387 off_t outoff = 0;
4388 int n;
4390 TAILQ_INIT(&changed_paths);
4392 if (refs) {
4393 err = build_refs_str(&refs_str, refs, commit_id, repo);
4394 if (err)
4395 return err;
4398 err = got_object_open_as_commit(&commit, repo, commit_id);
4399 if (err)
4400 return err;
4402 err = got_object_id_str(&id_str, commit_id);
4403 if (err) {
4404 err = got_error_from_errno("got_object_id_str");
4405 goto done;
4408 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4409 if (err)
4410 goto done;
4412 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4413 refs_str ? refs_str : "", refs_str ? ")" : "");
4414 if (n < 0) {
4415 err = got_error_from_errno("fprintf");
4416 goto done;
4418 outoff += n;
4419 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4420 if (err)
4421 goto done;
4423 n = fprintf(outfile, "from: %s\n",
4424 got_object_commit_get_author(commit));
4425 if (n < 0) {
4426 err = got_error_from_errno("fprintf");
4427 goto done;
4429 outoff += n;
4430 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4431 if (err)
4432 goto done;
4434 committer_time = got_object_commit_get_committer_time(commit);
4435 datestr = get_datestr(&committer_time, datebuf);
4436 if (datestr) {
4437 n = fprintf(outfile, "date: %s UTC\n", datestr);
4438 if (n < 0) {
4439 err = got_error_from_errno("fprintf");
4440 goto done;
4442 outoff += n;
4443 err = add_line_metadata(lines, nlines, outoff,
4444 GOT_DIFF_LINE_DATE);
4445 if (err)
4446 goto done;
4448 author = got_object_commit_get_author(commit);
4449 committer = got_object_commit_get_committer(commit);
4450 if (strcmp(author, committer) != 0) {
4451 n = fprintf(outfile, "via: %s\n", committer);
4452 if (n < 0) {
4453 err = got_error_from_errno("fprintf");
4454 goto done;
4456 outoff += n;
4457 err = add_line_metadata(lines, nlines, outoff,
4458 GOT_DIFF_LINE_AUTHOR);
4459 if (err)
4460 goto done;
4462 if (got_object_commit_get_nparents(commit) > 1) {
4463 const struct got_object_id_queue *parent_ids;
4464 struct got_object_qid *qid;
4465 int pn = 1;
4466 parent_ids = got_object_commit_get_parent_ids(commit);
4467 STAILQ_FOREACH(qid, parent_ids, entry) {
4468 err = got_object_id_str(&id_str, &qid->id);
4469 if (err)
4470 goto done;
4471 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4472 if (n < 0) {
4473 err = got_error_from_errno("fprintf");
4474 goto done;
4476 outoff += n;
4477 err = add_line_metadata(lines, nlines, outoff,
4478 GOT_DIFF_LINE_META);
4479 if (err)
4480 goto done;
4481 free(id_str);
4482 id_str = NULL;
4486 err = got_object_commit_get_logmsg(&logmsg, commit);
4487 if (err)
4488 goto done;
4489 s = logmsg;
4490 while ((line = strsep(&s, "\n")) != NULL) {
4491 n = fprintf(outfile, "%s\n", line);
4492 if (n < 0) {
4493 err = got_error_from_errno("fprintf");
4494 goto done;
4496 outoff += n;
4497 err = add_line_metadata(lines, nlines, outoff,
4498 GOT_DIFF_LINE_LOGMSG);
4499 if (err)
4500 goto done;
4503 err = get_changed_paths(&changed_paths, commit, repo);
4504 if (err)
4505 goto done;
4506 TAILQ_FOREACH(pe, &changed_paths, entry) {
4507 struct got_diff_changed_path *cp = pe->data;
4508 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4509 if (n < 0) {
4510 err = got_error_from_errno("fprintf");
4511 goto done;
4513 outoff += n;
4514 err = add_line_metadata(lines, nlines, outoff,
4515 GOT_DIFF_LINE_CHANGES);
4516 if (err)
4517 goto done;
4518 free((char *)pe->path);
4519 free(pe->data);
4522 fputc('\n', outfile);
4523 outoff++;
4524 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4525 done:
4526 got_pathlist_free(&changed_paths);
4527 free(id_str);
4528 free(logmsg);
4529 free(refs_str);
4530 got_object_commit_close(commit);
4531 if (err) {
4532 free(*lines);
4533 *lines = NULL;
4534 *nlines = 0;
4536 return err;
4539 static const struct got_error *
4540 create_diff(struct tog_diff_view_state *s)
4542 const struct got_error *err = NULL;
4543 FILE *f = NULL;
4544 int obj_type;
4546 free(s->lines);
4547 s->lines = malloc(sizeof(*s->lines));
4548 if (s->lines == NULL)
4549 return got_error_from_errno("malloc");
4550 s->nlines = 0;
4552 f = got_opentemp();
4553 if (f == NULL) {
4554 err = got_error_from_errno("got_opentemp");
4555 goto done;
4557 if (s->f && fclose(s->f) == EOF) {
4558 err = got_error_from_errno("fclose");
4559 goto done;
4561 s->f = f;
4563 if (s->id1)
4564 err = got_object_get_type(&obj_type, s->repo, s->id1);
4565 else
4566 err = got_object_get_type(&obj_type, s->repo, s->id2);
4567 if (err)
4568 goto done;
4570 switch (obj_type) {
4571 case GOT_OBJ_TYPE_BLOB:
4572 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4573 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4574 s->label1, s->label2, tog_diff_algo, s->diff_context,
4575 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4576 break;
4577 case GOT_OBJ_TYPE_TREE:
4578 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4579 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4580 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4581 s->force_text_diff, s->repo, s->f);
4582 break;
4583 case GOT_OBJ_TYPE_COMMIT: {
4584 const struct got_object_id_queue *parent_ids;
4585 struct got_object_qid *pid;
4586 struct got_commit_object *commit2;
4587 struct got_reflist_head *refs;
4589 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4590 if (err)
4591 goto done;
4592 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4593 /* Show commit info if we're diffing to a parent/root commit. */
4594 if (s->id1 == NULL) {
4595 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4596 refs, s->repo, s->f);
4597 if (err)
4598 goto done;
4599 } else {
4600 parent_ids = got_object_commit_get_parent_ids(commit2);
4601 STAILQ_FOREACH(pid, parent_ids, entry) {
4602 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4603 err = write_commit_info(&s->lines,
4604 &s->nlines, s->id2, refs, s->repo,
4605 s->f);
4606 if (err)
4607 goto done;
4608 break;
4612 got_object_commit_close(commit2);
4614 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4615 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4616 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4617 s->force_text_diff, s->repo, s->f);
4618 break;
4620 default:
4621 err = got_error(GOT_ERR_OBJ_TYPE);
4622 break;
4624 done:
4625 if (s->f && fflush(s->f) != 0 && err == NULL)
4626 err = got_error_from_errno("fflush");
4627 return err;
4630 static void
4631 diff_view_indicate_progress(struct tog_view *view)
4633 mvwaddstr(view->window, 0, 0, "diffing...");
4634 update_panels();
4635 doupdate();
4638 static const struct got_error *
4639 search_start_diff_view(struct tog_view *view)
4641 struct tog_diff_view_state *s = &view->state.diff;
4643 s->matched_line = 0;
4644 return NULL;
4647 static const struct got_error *
4648 search_next_diff_view(struct tog_view *view)
4650 struct tog_diff_view_state *s = &view->state.diff;
4651 const struct got_error *err = NULL;
4652 int lineno;
4653 char *line = NULL;
4654 size_t linesize = 0;
4655 ssize_t linelen;
4657 if (!view->searching) {
4658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4659 return NULL;
4662 if (s->matched_line) {
4663 if (view->searching == TOG_SEARCH_FORWARD)
4664 lineno = s->matched_line + 1;
4665 else
4666 lineno = s->matched_line - 1;
4667 } else
4668 lineno = s->first_displayed_line;
4670 while (1) {
4671 off_t offset;
4673 if (lineno <= 0 || lineno > s->nlines) {
4674 if (s->matched_line == 0) {
4675 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4676 break;
4679 if (view->searching == TOG_SEARCH_FORWARD)
4680 lineno = 1;
4681 else
4682 lineno = s->nlines;
4685 offset = s->lines[lineno - 1].offset;
4686 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4687 free(line);
4688 return got_error_from_errno("fseeko");
4690 linelen = getline(&line, &linesize, s->f);
4691 if (linelen != -1) {
4692 char *exstr;
4693 err = expand_tab(&exstr, line);
4694 if (err)
4695 break;
4696 if (match_line(exstr, &view->regex, 1,
4697 &view->regmatch)) {
4698 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4699 s->matched_line = lineno;
4700 free(exstr);
4701 break;
4703 free(exstr);
4705 if (view->searching == TOG_SEARCH_FORWARD)
4706 lineno++;
4707 else
4708 lineno--;
4710 free(line);
4712 if (s->matched_line) {
4713 s->first_displayed_line = s->matched_line;
4714 s->selected_line = 1;
4717 return err;
4720 static const struct got_error *
4721 close_diff_view(struct tog_view *view)
4723 const struct got_error *err = NULL;
4724 struct tog_diff_view_state *s = &view->state.diff;
4726 free(s->id1);
4727 s->id1 = NULL;
4728 free(s->id2);
4729 s->id2 = NULL;
4730 if (s->f && fclose(s->f) == EOF)
4731 err = got_error_from_errno("fclose");
4732 s->f = NULL;
4733 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4734 err = got_error_from_errno("fclose");
4735 s->f1 = NULL;
4736 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4737 err = got_error_from_errno("fclose");
4738 s->f2 = NULL;
4739 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4740 err = got_error_from_errno("close");
4741 s->fd1 = -1;
4742 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4743 err = got_error_from_errno("close");
4744 s->fd2 = -1;
4745 free(s->lines);
4746 s->lines = NULL;
4747 s->nlines = 0;
4748 return err;
4751 static const struct got_error *
4752 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4753 struct got_object_id *id2, const char *label1, const char *label2,
4754 int diff_context, int ignore_whitespace, int force_text_diff,
4755 struct tog_view *parent_view, struct got_repository *repo)
4757 const struct got_error *err;
4758 struct tog_diff_view_state *s = &view->state.diff;
4760 memset(s, 0, sizeof(*s));
4761 s->fd1 = -1;
4762 s->fd2 = -1;
4764 if (id1 != NULL && id2 != NULL) {
4765 int type1, type2;
4766 err = got_object_get_type(&type1, repo, id1);
4767 if (err)
4768 return err;
4769 err = got_object_get_type(&type2, repo, id2);
4770 if (err)
4771 return err;
4773 if (type1 != type2)
4774 return got_error(GOT_ERR_OBJ_TYPE);
4776 s->first_displayed_line = 1;
4777 s->last_displayed_line = view->nlines;
4778 s->selected_line = 1;
4779 s->repo = repo;
4780 s->id1 = id1;
4781 s->id2 = id2;
4782 s->label1 = label1;
4783 s->label2 = label2;
4785 if (id1) {
4786 s->id1 = got_object_id_dup(id1);
4787 if (s->id1 == NULL)
4788 return got_error_from_errno("got_object_id_dup");
4789 } else
4790 s->id1 = NULL;
4792 s->id2 = got_object_id_dup(id2);
4793 if (s->id2 == NULL) {
4794 err = got_error_from_errno("got_object_id_dup");
4795 goto done;
4798 s->f1 = got_opentemp();
4799 if (s->f1 == NULL) {
4800 err = got_error_from_errno("got_opentemp");
4801 goto done;
4804 s->f2 = got_opentemp();
4805 if (s->f2 == NULL) {
4806 err = got_error_from_errno("got_opentemp");
4807 goto done;
4810 s->fd1 = got_opentempfd();
4811 if (s->fd1 == -1) {
4812 err = got_error_from_errno("got_opentempfd");
4813 goto done;
4816 s->fd2 = got_opentempfd();
4817 if (s->fd2 == -1) {
4818 err = got_error_from_errno("got_opentempfd");
4819 goto done;
4822 s->first_displayed_line = 1;
4823 s->last_displayed_line = view->nlines;
4824 s->diff_context = diff_context;
4825 s->ignore_whitespace = ignore_whitespace;
4826 s->force_text_diff = force_text_diff;
4827 s->parent_view = parent_view;
4828 s->repo = repo;
4830 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4831 int rc;
4833 rc = init_pair(GOT_DIFF_LINE_MINUS,
4834 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4835 if (rc != ERR)
4836 rc = init_pair(GOT_DIFF_LINE_PLUS,
4837 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4838 if (rc != ERR)
4839 rc = init_pair(GOT_DIFF_LINE_HUNK,
4840 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4841 if (rc != ERR)
4842 rc = init_pair(GOT_DIFF_LINE_META,
4843 get_color_value("TOG_COLOR_DIFF_META"), -1);
4844 if (rc != ERR)
4845 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4846 get_color_value("TOG_COLOR_DIFF_META"), -1);
4847 if (rc != ERR)
4848 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4849 get_color_value("TOG_COLOR_DIFF_META"), -1);
4850 if (rc != ERR)
4851 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4852 get_color_value("TOG_COLOR_DIFF_META"), -1);
4853 if (rc != ERR)
4854 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4855 get_color_value("TOG_COLOR_AUTHOR"), -1);
4856 if (rc != ERR)
4857 rc = init_pair(GOT_DIFF_LINE_DATE,
4858 get_color_value("TOG_COLOR_DATE"), -1);
4859 if (rc == ERR) {
4860 err = got_error(GOT_ERR_RANGE);
4861 goto done;
4865 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4866 view_is_splitscreen(view))
4867 show_log_view(parent_view); /* draw border */
4868 diff_view_indicate_progress(view);
4870 err = create_diff(s);
4872 view->show = show_diff_view;
4873 view->input = input_diff_view;
4874 view->reset = reset_diff_view;
4875 view->close = close_diff_view;
4876 view->search_start = search_start_diff_view;
4877 view->search_next = search_next_diff_view;
4878 done:
4879 if (err)
4880 close_diff_view(view);
4881 return err;
4884 static const struct got_error *
4885 show_diff_view(struct tog_view *view)
4887 const struct got_error *err;
4888 struct tog_diff_view_state *s = &view->state.diff;
4889 char *id_str1 = NULL, *id_str2, *header;
4890 const char *label1, *label2;
4892 if (s->id1) {
4893 err = got_object_id_str(&id_str1, s->id1);
4894 if (err)
4895 return err;
4896 label1 = s->label1 ? s->label1 : id_str1;
4897 } else
4898 label1 = "/dev/null";
4900 err = got_object_id_str(&id_str2, s->id2);
4901 if (err)
4902 return err;
4903 label2 = s->label2 ? s->label2 : id_str2;
4905 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4906 err = got_error_from_errno("asprintf");
4907 free(id_str1);
4908 free(id_str2);
4909 return err;
4911 free(id_str1);
4912 free(id_str2);
4914 err = draw_file(view, header);
4915 free(header);
4916 return err;
4919 static const struct got_error *
4920 set_selected_commit(struct tog_diff_view_state *s,
4921 struct commit_queue_entry *entry)
4923 const struct got_error *err;
4924 const struct got_object_id_queue *parent_ids;
4925 struct got_commit_object *selected_commit;
4926 struct got_object_qid *pid;
4928 free(s->id2);
4929 s->id2 = got_object_id_dup(entry->id);
4930 if (s->id2 == NULL)
4931 return got_error_from_errno("got_object_id_dup");
4933 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4934 if (err)
4935 return err;
4936 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4937 free(s->id1);
4938 pid = STAILQ_FIRST(parent_ids);
4939 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4940 got_object_commit_close(selected_commit);
4941 return NULL;
4944 static const struct got_error *
4945 reset_diff_view(struct tog_view *view)
4947 struct tog_diff_view_state *s = &view->state.diff;
4949 view->count = 0;
4950 wclear(view->window);
4951 s->first_displayed_line = 1;
4952 s->last_displayed_line = view->nlines;
4953 s->matched_line = 0;
4954 diff_view_indicate_progress(view);
4955 return create_diff(s);
4958 static void
4959 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4961 int start, i;
4963 i = start = s->first_displayed_line - 1;
4965 while (s->lines[i].type != type) {
4966 if (i == 0)
4967 i = s->nlines - 1;
4968 if (--i == start)
4969 return; /* do nothing, requested type not in file */
4972 s->selected_line = 1;
4973 s->first_displayed_line = i;
4976 static void
4977 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4979 int start, i;
4981 i = start = s->first_displayed_line + 1;
4983 while (s->lines[i].type != type) {
4984 if (i == s->nlines - 1)
4985 i = 0;
4986 if (++i == start)
4987 return; /* do nothing, requested type not in file */
4990 s->selected_line = 1;
4991 s->first_displayed_line = i;
4994 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4995 int, int, int);
4996 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4997 int, int);
4999 static const struct got_error *
5000 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5002 const struct got_error *err = NULL;
5003 struct tog_diff_view_state *s = &view->state.diff;
5004 struct tog_log_view_state *ls;
5005 struct commit_queue_entry *old_selected_entry;
5006 char *line = NULL;
5007 size_t linesize = 0;
5008 ssize_t linelen;
5009 int i, nscroll = view->nlines - 1, up = 0;
5011 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5013 switch (ch) {
5014 case '0':
5015 view->x = 0;
5016 break;
5017 case '$':
5018 view->x = MAX(view->maxx - view->ncols / 3, 0);
5019 view->count = 0;
5020 break;
5021 case KEY_RIGHT:
5022 case 'l':
5023 if (view->x + view->ncols / 3 < view->maxx)
5024 view->x += 2; /* move two columns right */
5025 else
5026 view->count = 0;
5027 break;
5028 case KEY_LEFT:
5029 case 'h':
5030 view->x -= MIN(view->x, 2); /* move two columns back */
5031 if (view->x <= 0)
5032 view->count = 0;
5033 break;
5034 case 'a':
5035 case 'w':
5036 if (ch == 'a')
5037 s->force_text_diff = !s->force_text_diff;
5038 if (ch == 'w')
5039 s->ignore_whitespace = !s->ignore_whitespace;
5040 err = reset_diff_view(view);
5041 break;
5042 case 'g':
5043 case KEY_HOME:
5044 s->first_displayed_line = 1;
5045 view->count = 0;
5046 break;
5047 case 'G':
5048 case KEY_END:
5049 view->count = 0;
5050 if (s->eof)
5051 break;
5053 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5054 s->eof = 1;
5055 break;
5056 case 'k':
5057 case KEY_UP:
5058 case CTRL('p'):
5059 if (s->first_displayed_line > 1)
5060 s->first_displayed_line--;
5061 else
5062 view->count = 0;
5063 break;
5064 case CTRL('u'):
5065 case 'u':
5066 nscroll /= 2;
5067 /* FALL THROUGH */
5068 case KEY_PPAGE:
5069 case CTRL('b'):
5070 case 'b':
5071 if (s->first_displayed_line == 1) {
5072 view->count = 0;
5073 break;
5075 i = 0;
5076 while (i++ < nscroll && s->first_displayed_line > 1)
5077 s->first_displayed_line--;
5078 break;
5079 case 'j':
5080 case KEY_DOWN:
5081 case CTRL('n'):
5082 if (!s->eof)
5083 s->first_displayed_line++;
5084 else
5085 view->count = 0;
5086 break;
5087 case CTRL('d'):
5088 case 'd':
5089 nscroll /= 2;
5090 /* FALL THROUGH */
5091 case KEY_NPAGE:
5092 case CTRL('f'):
5093 case 'f':
5094 case ' ':
5095 if (s->eof) {
5096 view->count = 0;
5097 break;
5099 i = 0;
5100 while (!s->eof && i++ < nscroll) {
5101 linelen = getline(&line, &linesize, s->f);
5102 s->first_displayed_line++;
5103 if (linelen == -1) {
5104 if (feof(s->f)) {
5105 s->eof = 1;
5106 } else
5107 err = got_ferror(s->f, GOT_ERR_IO);
5108 break;
5111 free(line);
5112 break;
5113 case '(':
5114 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5115 break;
5116 case ')':
5117 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5118 break;
5119 case '{':
5120 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5121 break;
5122 case '}':
5123 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5124 break;
5125 case '[':
5126 if (s->diff_context > 0) {
5127 s->diff_context--;
5128 s->matched_line = 0;
5129 diff_view_indicate_progress(view);
5130 err = create_diff(s);
5131 if (s->first_displayed_line + view->nlines - 1 >
5132 s->nlines) {
5133 s->first_displayed_line = 1;
5134 s->last_displayed_line = view->nlines;
5136 } else
5137 view->count = 0;
5138 break;
5139 case ']':
5140 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5141 s->diff_context++;
5142 s->matched_line = 0;
5143 diff_view_indicate_progress(view);
5144 err = create_diff(s);
5145 } else
5146 view->count = 0;
5147 break;
5148 case '<':
5149 case ',':
5150 case 'K':
5151 up = 1;
5152 /* FALL THROUGH */
5153 case '>':
5154 case '.':
5155 case 'J':
5156 if (s->parent_view == NULL) {
5157 view->count = 0;
5158 break;
5160 s->parent_view->count = view->count;
5162 if (s->parent_view->type == TOG_VIEW_LOG) {
5163 ls = &s->parent_view->state.log;
5164 old_selected_entry = ls->selected_entry;
5166 err = input_log_view(NULL, s->parent_view,
5167 up ? KEY_UP : KEY_DOWN);
5168 if (err)
5169 break;
5170 view->count = s->parent_view->count;
5172 if (old_selected_entry == ls->selected_entry)
5173 break;
5175 err = set_selected_commit(s, ls->selected_entry);
5176 if (err)
5177 break;
5178 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5179 struct tog_blame_view_state *bs;
5180 struct got_object_id *id, *prev_id;
5182 bs = &s->parent_view->state.blame;
5183 prev_id = get_annotation_for_line(bs->blame.lines,
5184 bs->blame.nlines, bs->last_diffed_line);
5186 err = input_blame_view(&view, s->parent_view,
5187 up ? KEY_UP : KEY_DOWN);
5188 if (err)
5189 break;
5190 view->count = s->parent_view->count;
5192 if (prev_id == NULL)
5193 break;
5194 id = get_selected_commit_id(bs->blame.lines,
5195 bs->blame.nlines, bs->first_displayed_line,
5196 bs->selected_line);
5197 if (id == NULL)
5198 break;
5200 if (!got_object_id_cmp(prev_id, id))
5201 break;
5203 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5204 if (err)
5205 break;
5207 s->first_displayed_line = 1;
5208 s->last_displayed_line = view->nlines;
5209 s->matched_line = 0;
5210 view->x = 0;
5212 diff_view_indicate_progress(view);
5213 err = create_diff(s);
5214 break;
5215 default:
5216 view->count = 0;
5217 break;
5220 return err;
5223 static const struct got_error *
5224 cmd_diff(int argc, char *argv[])
5226 const struct got_error *error = NULL;
5227 struct got_repository *repo = NULL;
5228 struct got_worktree *worktree = NULL;
5229 struct got_object_id *id1 = NULL, *id2 = NULL;
5230 char *repo_path = NULL, *cwd = NULL;
5231 char *id_str1 = NULL, *id_str2 = NULL;
5232 char *label1 = NULL, *label2 = NULL;
5233 int diff_context = 3, ignore_whitespace = 0;
5234 int ch, force_text_diff = 0;
5235 const char *errstr;
5236 struct tog_view *view;
5237 int *pack_fds = NULL;
5239 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5240 switch (ch) {
5241 case 'a':
5242 force_text_diff = 1;
5243 break;
5244 case 'C':
5245 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5246 &errstr);
5247 if (errstr != NULL)
5248 errx(1, "number of context lines is %s: %s",
5249 errstr, errstr);
5250 break;
5251 case 'r':
5252 repo_path = realpath(optarg, NULL);
5253 if (repo_path == NULL)
5254 return got_error_from_errno2("realpath",
5255 optarg);
5256 got_path_strip_trailing_slashes(repo_path);
5257 break;
5258 case 'w':
5259 ignore_whitespace = 1;
5260 break;
5261 default:
5262 usage_diff();
5263 /* NOTREACHED */
5267 argc -= optind;
5268 argv += optind;
5270 if (argc == 0) {
5271 usage_diff(); /* TODO show local worktree changes */
5272 } else if (argc == 2) {
5273 id_str1 = argv[0];
5274 id_str2 = argv[1];
5275 } else
5276 usage_diff();
5278 error = got_repo_pack_fds_open(&pack_fds);
5279 if (error)
5280 goto done;
5282 if (repo_path == NULL) {
5283 cwd = getcwd(NULL, 0);
5284 if (cwd == NULL)
5285 return got_error_from_errno("getcwd");
5286 error = got_worktree_open(&worktree, cwd);
5287 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5288 goto done;
5289 if (worktree)
5290 repo_path =
5291 strdup(got_worktree_get_repo_path(worktree));
5292 else
5293 repo_path = strdup(cwd);
5294 if (repo_path == NULL) {
5295 error = got_error_from_errno("strdup");
5296 goto done;
5300 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5301 if (error)
5302 goto done;
5304 init_curses();
5306 error = apply_unveil(got_repo_get_path(repo), NULL);
5307 if (error)
5308 goto done;
5310 error = tog_load_refs(repo, 0);
5311 if (error)
5312 goto done;
5314 error = got_repo_match_object_id(&id1, &label1, id_str1,
5315 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5316 if (error)
5317 goto done;
5319 error = got_repo_match_object_id(&id2, &label2, id_str2,
5320 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5321 if (error)
5322 goto done;
5324 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5325 if (view == NULL) {
5326 error = got_error_from_errno("view_open");
5327 goto done;
5329 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5330 ignore_whitespace, force_text_diff, NULL, repo);
5331 if (error)
5332 goto done;
5333 error = view_loop(view);
5334 done:
5335 free(label1);
5336 free(label2);
5337 free(repo_path);
5338 free(cwd);
5339 if (repo) {
5340 const struct got_error *close_err = got_repo_close(repo);
5341 if (error == NULL)
5342 error = close_err;
5344 if (worktree)
5345 got_worktree_close(worktree);
5346 if (pack_fds) {
5347 const struct got_error *pack_err =
5348 got_repo_pack_fds_close(pack_fds);
5349 if (error == NULL)
5350 error = pack_err;
5352 tog_free_refs();
5353 return error;
5356 __dead static void
5357 usage_blame(void)
5359 endwin();
5360 fprintf(stderr,
5361 "usage: %s blame [-c commit] [-r repository-path] path\n",
5362 getprogname());
5363 exit(1);
5366 struct tog_blame_line {
5367 int annotated;
5368 struct got_object_id *id;
5371 static const struct got_error *
5372 draw_blame(struct tog_view *view)
5374 struct tog_blame_view_state *s = &view->state.blame;
5375 struct tog_blame *blame = &s->blame;
5376 regmatch_t *regmatch = &view->regmatch;
5377 const struct got_error *err;
5378 int lineno = 0, nprinted = 0;
5379 char *line = NULL;
5380 size_t linesize = 0;
5381 ssize_t linelen;
5382 wchar_t *wline;
5383 int width;
5384 struct tog_blame_line *blame_line;
5385 struct got_object_id *prev_id = NULL;
5386 char *id_str;
5387 struct tog_color *tc;
5389 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5390 if (err)
5391 return err;
5393 rewind(blame->f);
5394 werase(view->window);
5396 if (asprintf(&line, "commit %s", id_str) == -1) {
5397 err = got_error_from_errno("asprintf");
5398 free(id_str);
5399 return err;
5402 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5403 free(line);
5404 line = NULL;
5405 if (err)
5406 return err;
5407 if (view_needs_focus_indication(view))
5408 wstandout(view->window);
5409 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5410 if (tc)
5411 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5412 waddwstr(view->window, wline);
5413 while (width++ < view->ncols)
5414 waddch(view->window, ' ');
5415 if (tc)
5416 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5417 if (view_needs_focus_indication(view))
5418 wstandend(view->window);
5419 free(wline);
5420 wline = NULL;
5422 if (view->gline > blame->nlines)
5423 view->gline = blame->nlines;
5425 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5426 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5427 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5428 free(id_str);
5429 return got_error_from_errno("asprintf");
5431 free(id_str);
5432 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5433 free(line);
5434 line = NULL;
5435 if (err)
5436 return err;
5437 waddwstr(view->window, wline);
5438 free(wline);
5439 wline = NULL;
5440 if (width < view->ncols - 1)
5441 waddch(view->window, '\n');
5443 s->eof = 0;
5444 view->maxx = 0;
5445 while (nprinted < view->nlines - 2) {
5446 linelen = getline(&line, &linesize, blame->f);
5447 if (linelen == -1) {
5448 if (feof(blame->f)) {
5449 s->eof = 1;
5450 break;
5452 free(line);
5453 return got_ferror(blame->f, GOT_ERR_IO);
5455 if (++lineno < s->first_displayed_line)
5456 continue;
5457 if (view->gline && !gotoline(view, &lineno, &nprinted))
5458 continue;
5460 /* Set view->maxx based on full line length. */
5461 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5462 if (err) {
5463 free(line);
5464 return err;
5466 free(wline);
5467 wline = NULL;
5468 view->maxx = MAX(view->maxx, width);
5470 if (nprinted == s->selected_line - 1)
5471 wstandout(view->window);
5473 if (blame->nlines > 0) {
5474 blame_line = &blame->lines[lineno - 1];
5475 if (blame_line->annotated && prev_id &&
5476 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5477 !(nprinted == s->selected_line - 1)) {
5478 waddstr(view->window, " ");
5479 } else if (blame_line->annotated) {
5480 char *id_str;
5481 err = got_object_id_str(&id_str,
5482 blame_line->id);
5483 if (err) {
5484 free(line);
5485 return err;
5487 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5488 if (tc)
5489 wattr_on(view->window,
5490 COLOR_PAIR(tc->colorpair), NULL);
5491 wprintw(view->window, "%.8s", id_str);
5492 if (tc)
5493 wattr_off(view->window,
5494 COLOR_PAIR(tc->colorpair), NULL);
5495 free(id_str);
5496 prev_id = blame_line->id;
5497 } else {
5498 waddstr(view->window, "........");
5499 prev_id = NULL;
5501 } else {
5502 waddstr(view->window, "........");
5503 prev_id = NULL;
5506 if (nprinted == s->selected_line - 1)
5507 wstandend(view->window);
5508 waddstr(view->window, " ");
5510 if (view->ncols <= 9) {
5511 width = 9;
5512 } else if (s->first_displayed_line + nprinted ==
5513 s->matched_line &&
5514 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5515 err = add_matched_line(&width, line, view->ncols - 9, 9,
5516 view->window, view->x, regmatch);
5517 if (err) {
5518 free(line);
5519 return err;
5521 width += 9;
5522 } else {
5523 int skip;
5524 err = format_line(&wline, &width, &skip, line,
5525 view->x, view->ncols - 9, 9, 1);
5526 if (err) {
5527 free(line);
5528 return err;
5530 waddwstr(view->window, &wline[skip]);
5531 width += 9;
5532 free(wline);
5533 wline = NULL;
5536 if (width <= view->ncols - 1)
5537 waddch(view->window, '\n');
5538 if (++nprinted == 1)
5539 s->first_displayed_line = lineno;
5541 free(line);
5542 s->last_displayed_line = lineno;
5544 view_border(view);
5546 return NULL;
5549 static const struct got_error *
5550 blame_cb(void *arg, int nlines, int lineno,
5551 struct got_commit_object *commit, struct got_object_id *id)
5553 const struct got_error *err = NULL;
5554 struct tog_blame_cb_args *a = arg;
5555 struct tog_blame_line *line;
5556 int errcode;
5558 if (nlines != a->nlines ||
5559 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5560 return got_error(GOT_ERR_RANGE);
5562 errcode = pthread_mutex_lock(&tog_mutex);
5563 if (errcode)
5564 return got_error_set_errno(errcode, "pthread_mutex_lock");
5566 if (*a->quit) { /* user has quit the blame view */
5567 err = got_error(GOT_ERR_ITER_COMPLETED);
5568 goto done;
5571 if (lineno == -1)
5572 goto done; /* no change in this commit */
5574 line = &a->lines[lineno - 1];
5575 if (line->annotated)
5576 goto done;
5578 line->id = got_object_id_dup(id);
5579 if (line->id == NULL) {
5580 err = got_error_from_errno("got_object_id_dup");
5581 goto done;
5583 line->annotated = 1;
5584 done:
5585 errcode = pthread_mutex_unlock(&tog_mutex);
5586 if (errcode)
5587 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5588 return err;
5591 static void *
5592 blame_thread(void *arg)
5594 const struct got_error *err, *close_err;
5595 struct tog_blame_thread_args *ta = arg;
5596 struct tog_blame_cb_args *a = ta->cb_args;
5597 int errcode, fd1 = -1, fd2 = -1;
5598 FILE *f1 = NULL, *f2 = NULL;
5600 fd1 = got_opentempfd();
5601 if (fd1 == -1)
5602 return (void *)got_error_from_errno("got_opentempfd");
5604 fd2 = got_opentempfd();
5605 if (fd2 == -1) {
5606 err = got_error_from_errno("got_opentempfd");
5607 goto done;
5610 f1 = got_opentemp();
5611 if (f1 == NULL) {
5612 err = (void *)got_error_from_errno("got_opentemp");
5613 goto done;
5615 f2 = got_opentemp();
5616 if (f2 == NULL) {
5617 err = (void *)got_error_from_errno("got_opentemp");
5618 goto done;
5621 err = block_signals_used_by_main_thread();
5622 if (err)
5623 goto done;
5625 err = got_blame(ta->path, a->commit_id, ta->repo,
5626 tog_diff_algo, blame_cb, ta->cb_args,
5627 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5628 if (err && err->code == GOT_ERR_CANCELLED)
5629 err = NULL;
5631 errcode = pthread_mutex_lock(&tog_mutex);
5632 if (errcode) {
5633 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5634 goto done;
5637 close_err = got_repo_close(ta->repo);
5638 if (err == NULL)
5639 err = close_err;
5640 ta->repo = NULL;
5641 *ta->complete = 1;
5643 errcode = pthread_mutex_unlock(&tog_mutex);
5644 if (errcode && err == NULL)
5645 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5647 done:
5648 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5649 err = got_error_from_errno("close");
5650 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5651 err = got_error_from_errno("close");
5652 if (f1 && fclose(f1) == EOF && err == NULL)
5653 err = got_error_from_errno("fclose");
5654 if (f2 && fclose(f2) == EOF && err == NULL)
5655 err = got_error_from_errno("fclose");
5657 return (void *)err;
5660 static struct got_object_id *
5661 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5662 int first_displayed_line, int selected_line)
5664 struct tog_blame_line *line;
5666 if (nlines <= 0)
5667 return NULL;
5669 line = &lines[first_displayed_line - 1 + selected_line - 1];
5670 if (!line->annotated)
5671 return NULL;
5673 return line->id;
5676 static struct got_object_id *
5677 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5678 int lineno)
5680 struct tog_blame_line *line;
5682 if (nlines <= 0 || lineno >= nlines)
5683 return NULL;
5685 line = &lines[lineno - 1];
5686 if (!line->annotated)
5687 return NULL;
5689 return line->id;
5692 static const struct got_error *
5693 stop_blame(struct tog_blame *blame)
5695 const struct got_error *err = NULL;
5696 int i;
5698 if (blame->thread) {
5699 int errcode;
5700 errcode = pthread_mutex_unlock(&tog_mutex);
5701 if (errcode)
5702 return got_error_set_errno(errcode,
5703 "pthread_mutex_unlock");
5704 errcode = pthread_join(blame->thread, (void **)&err);
5705 if (errcode)
5706 return got_error_set_errno(errcode, "pthread_join");
5707 errcode = pthread_mutex_lock(&tog_mutex);
5708 if (errcode)
5709 return got_error_set_errno(errcode,
5710 "pthread_mutex_lock");
5711 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5712 err = NULL;
5713 blame->thread = NULL;
5715 if (blame->thread_args.repo) {
5716 const struct got_error *close_err;
5717 close_err = got_repo_close(blame->thread_args.repo);
5718 if (err == NULL)
5719 err = close_err;
5720 blame->thread_args.repo = NULL;
5722 if (blame->f) {
5723 if (fclose(blame->f) == EOF && err == NULL)
5724 err = got_error_from_errno("fclose");
5725 blame->f = NULL;
5727 if (blame->lines) {
5728 for (i = 0; i < blame->nlines; i++)
5729 free(blame->lines[i].id);
5730 free(blame->lines);
5731 blame->lines = NULL;
5733 free(blame->cb_args.commit_id);
5734 blame->cb_args.commit_id = NULL;
5735 if (blame->pack_fds) {
5736 const struct got_error *pack_err =
5737 got_repo_pack_fds_close(blame->pack_fds);
5738 if (err == NULL)
5739 err = pack_err;
5740 blame->pack_fds = NULL;
5742 return err;
5745 static const struct got_error *
5746 cancel_blame_view(void *arg)
5748 const struct got_error *err = NULL;
5749 int *done = arg;
5750 int errcode;
5752 errcode = pthread_mutex_lock(&tog_mutex);
5753 if (errcode)
5754 return got_error_set_errno(errcode,
5755 "pthread_mutex_unlock");
5757 if (*done)
5758 err = got_error(GOT_ERR_CANCELLED);
5760 errcode = pthread_mutex_unlock(&tog_mutex);
5761 if (errcode)
5762 return got_error_set_errno(errcode,
5763 "pthread_mutex_lock");
5765 return err;
5768 static const struct got_error *
5769 run_blame(struct tog_view *view)
5771 struct tog_blame_view_state *s = &view->state.blame;
5772 struct tog_blame *blame = &s->blame;
5773 const struct got_error *err = NULL;
5774 struct got_commit_object *commit = NULL;
5775 struct got_blob_object *blob = NULL;
5776 struct got_repository *thread_repo = NULL;
5777 struct got_object_id *obj_id = NULL;
5778 int obj_type, fd = -1;
5779 int *pack_fds = NULL;
5781 err = got_object_open_as_commit(&commit, s->repo,
5782 &s->blamed_commit->id);
5783 if (err)
5784 return err;
5786 fd = got_opentempfd();
5787 if (fd == -1) {
5788 err = got_error_from_errno("got_opentempfd");
5789 goto done;
5792 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5793 if (err)
5794 goto done;
5796 err = got_object_get_type(&obj_type, s->repo, obj_id);
5797 if (err)
5798 goto done;
5800 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5801 err = got_error(GOT_ERR_OBJ_TYPE);
5802 goto done;
5805 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5806 if (err)
5807 goto done;
5808 blame->f = got_opentemp();
5809 if (blame->f == NULL) {
5810 err = got_error_from_errno("got_opentemp");
5811 goto done;
5813 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5814 &blame->line_offsets, blame->f, blob);
5815 if (err)
5816 goto done;
5817 if (blame->nlines == 0) {
5818 s->blame_complete = 1;
5819 goto done;
5822 /* Don't include \n at EOF in the blame line count. */
5823 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5824 blame->nlines--;
5826 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5827 if (blame->lines == NULL) {
5828 err = got_error_from_errno("calloc");
5829 goto done;
5832 err = got_repo_pack_fds_open(&pack_fds);
5833 if (err)
5834 goto done;
5835 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5836 pack_fds);
5837 if (err)
5838 goto done;
5840 blame->pack_fds = pack_fds;
5841 blame->cb_args.view = view;
5842 blame->cb_args.lines = blame->lines;
5843 blame->cb_args.nlines = blame->nlines;
5844 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5845 if (blame->cb_args.commit_id == NULL) {
5846 err = got_error_from_errno("got_object_id_dup");
5847 goto done;
5849 blame->cb_args.quit = &s->done;
5851 blame->thread_args.path = s->path;
5852 blame->thread_args.repo = thread_repo;
5853 blame->thread_args.cb_args = &blame->cb_args;
5854 blame->thread_args.complete = &s->blame_complete;
5855 blame->thread_args.cancel_cb = cancel_blame_view;
5856 blame->thread_args.cancel_arg = &s->done;
5857 s->blame_complete = 0;
5859 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5860 s->first_displayed_line = 1;
5861 s->last_displayed_line = view->nlines;
5862 s->selected_line = 1;
5864 s->matched_line = 0;
5866 done:
5867 if (commit)
5868 got_object_commit_close(commit);
5869 if (fd != -1 && close(fd) == -1 && err == NULL)
5870 err = got_error_from_errno("close");
5871 if (blob)
5872 got_object_blob_close(blob);
5873 free(obj_id);
5874 if (err)
5875 stop_blame(blame);
5876 return err;
5879 static const struct got_error *
5880 open_blame_view(struct tog_view *view, char *path,
5881 struct got_object_id *commit_id, struct got_repository *repo)
5883 const struct got_error *err = NULL;
5884 struct tog_blame_view_state *s = &view->state.blame;
5886 STAILQ_INIT(&s->blamed_commits);
5888 s->path = strdup(path);
5889 if (s->path == NULL)
5890 return got_error_from_errno("strdup");
5892 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5893 if (err) {
5894 free(s->path);
5895 return err;
5898 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5899 s->first_displayed_line = 1;
5900 s->last_displayed_line = view->nlines;
5901 s->selected_line = 1;
5902 s->blame_complete = 0;
5903 s->repo = repo;
5904 s->commit_id = commit_id;
5905 memset(&s->blame, 0, sizeof(s->blame));
5907 STAILQ_INIT(&s->colors);
5908 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5909 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5910 get_color_value("TOG_COLOR_COMMIT"));
5911 if (err)
5912 return err;
5915 view->show = show_blame_view;
5916 view->input = input_blame_view;
5917 view->reset = reset_blame_view;
5918 view->close = close_blame_view;
5919 view->search_start = search_start_blame_view;
5920 view->search_next = search_next_blame_view;
5922 return run_blame(view);
5925 static const struct got_error *
5926 close_blame_view(struct tog_view *view)
5928 const struct got_error *err = NULL;
5929 struct tog_blame_view_state *s = &view->state.blame;
5931 if (s->blame.thread)
5932 err = stop_blame(&s->blame);
5934 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5935 struct got_object_qid *blamed_commit;
5936 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5937 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5938 got_object_qid_free(blamed_commit);
5941 free(s->path);
5942 free_colors(&s->colors);
5943 return err;
5946 static const struct got_error *
5947 search_start_blame_view(struct tog_view *view)
5949 struct tog_blame_view_state *s = &view->state.blame;
5951 s->matched_line = 0;
5952 return NULL;
5955 static const struct got_error *
5956 search_next_blame_view(struct tog_view *view)
5958 struct tog_blame_view_state *s = &view->state.blame;
5959 const struct got_error *err = NULL;
5960 int lineno;
5961 char *line = NULL;
5962 size_t linesize = 0;
5963 ssize_t linelen;
5965 if (!view->searching) {
5966 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5967 return NULL;
5970 if (s->matched_line) {
5971 if (view->searching == TOG_SEARCH_FORWARD)
5972 lineno = s->matched_line + 1;
5973 else
5974 lineno = s->matched_line - 1;
5975 } else
5976 lineno = s->first_displayed_line - 1 + s->selected_line;
5978 while (1) {
5979 off_t offset;
5981 if (lineno <= 0 || lineno > s->blame.nlines) {
5982 if (s->matched_line == 0) {
5983 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5984 break;
5987 if (view->searching == TOG_SEARCH_FORWARD)
5988 lineno = 1;
5989 else
5990 lineno = s->blame.nlines;
5993 offset = s->blame.line_offsets[lineno - 1];
5994 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5995 free(line);
5996 return got_error_from_errno("fseeko");
5998 linelen = getline(&line, &linesize, s->blame.f);
5999 if (linelen != -1) {
6000 char *exstr;
6001 err = expand_tab(&exstr, line);
6002 if (err)
6003 break;
6004 if (match_line(exstr, &view->regex, 1,
6005 &view->regmatch)) {
6006 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6007 s->matched_line = lineno;
6008 free(exstr);
6009 break;
6011 free(exstr);
6013 if (view->searching == TOG_SEARCH_FORWARD)
6014 lineno++;
6015 else
6016 lineno--;
6018 free(line);
6020 if (s->matched_line) {
6021 s->first_displayed_line = s->matched_line;
6022 s->selected_line = 1;
6025 return err;
6028 static const struct got_error *
6029 show_blame_view(struct tog_view *view)
6031 const struct got_error *err = NULL;
6032 struct tog_blame_view_state *s = &view->state.blame;
6033 int errcode;
6035 if (s->blame.thread == NULL && !s->blame_complete) {
6036 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6037 &s->blame.thread_args);
6038 if (errcode)
6039 return got_error_set_errno(errcode, "pthread_create");
6041 halfdelay(1); /* fast refresh while annotating */
6044 if (s->blame_complete)
6045 halfdelay(10); /* disable fast refresh */
6047 err = draw_blame(view);
6049 view_border(view);
6050 return err;
6053 static const struct got_error *
6054 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6055 struct got_repository *repo, struct got_object_id *id)
6057 struct tog_view *log_view;
6058 const struct got_error *err = NULL;
6060 *new_view = NULL;
6062 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6063 if (log_view == NULL)
6064 return got_error_from_errno("view_open");
6066 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6067 if (err)
6068 view_close(log_view);
6069 else
6070 *new_view = log_view;
6072 return err;
6075 static const struct got_error *
6076 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6078 const struct got_error *err = NULL, *thread_err = NULL;
6079 struct tog_view *diff_view;
6080 struct tog_blame_view_state *s = &view->state.blame;
6081 int eos, nscroll, begin_y = 0, begin_x = 0;
6083 eos = nscroll = view->nlines - 2;
6084 if (view_is_hsplit_top(view))
6085 --eos; /* border */
6087 switch (ch) {
6088 case '0':
6089 view->x = 0;
6090 break;
6091 case '$':
6092 view->x = MAX(view->maxx - view->ncols / 3, 0);
6093 view->count = 0;
6094 break;
6095 case KEY_RIGHT:
6096 case 'l':
6097 if (view->x + view->ncols / 3 < view->maxx)
6098 view->x += 2; /* move two columns right */
6099 else
6100 view->count = 0;
6101 break;
6102 case KEY_LEFT:
6103 case 'h':
6104 view->x -= MIN(view->x, 2); /* move two columns back */
6105 if (view->x <= 0)
6106 view->count = 0;
6107 break;
6108 case 'q':
6109 s->done = 1;
6110 break;
6111 case 'g':
6112 case KEY_HOME:
6113 s->selected_line = 1;
6114 s->first_displayed_line = 1;
6115 view->count = 0;
6116 break;
6117 case 'G':
6118 case KEY_END:
6119 if (s->blame.nlines < eos) {
6120 s->selected_line = s->blame.nlines;
6121 s->first_displayed_line = 1;
6122 } else {
6123 s->selected_line = eos;
6124 s->first_displayed_line = s->blame.nlines - (eos - 1);
6126 view->count = 0;
6127 break;
6128 case 'k':
6129 case KEY_UP:
6130 case CTRL('p'):
6131 if (s->selected_line > 1)
6132 s->selected_line--;
6133 else if (s->selected_line == 1 &&
6134 s->first_displayed_line > 1)
6135 s->first_displayed_line--;
6136 else
6137 view->count = 0;
6138 break;
6139 case CTRL('u'):
6140 case 'u':
6141 nscroll /= 2;
6142 /* FALL THROUGH */
6143 case KEY_PPAGE:
6144 case CTRL('b'):
6145 case 'b':
6146 if (s->first_displayed_line == 1) {
6147 if (view->count > 1)
6148 nscroll += nscroll;
6149 s->selected_line = MAX(1, s->selected_line - nscroll);
6150 view->count = 0;
6151 break;
6153 if (s->first_displayed_line > nscroll)
6154 s->first_displayed_line -= nscroll;
6155 else
6156 s->first_displayed_line = 1;
6157 break;
6158 case 'j':
6159 case KEY_DOWN:
6160 case CTRL('n'):
6161 if (s->selected_line < eos && s->first_displayed_line +
6162 s->selected_line <= s->blame.nlines)
6163 s->selected_line++;
6164 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6165 s->first_displayed_line++;
6166 else
6167 view->count = 0;
6168 break;
6169 case 'c':
6170 case 'p': {
6171 struct got_object_id *id = NULL;
6173 view->count = 0;
6174 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6175 s->first_displayed_line, s->selected_line);
6176 if (id == NULL)
6177 break;
6178 if (ch == 'p') {
6179 struct got_commit_object *commit, *pcommit;
6180 struct got_object_qid *pid;
6181 struct got_object_id *blob_id = NULL;
6182 int obj_type;
6183 err = got_object_open_as_commit(&commit,
6184 s->repo, id);
6185 if (err)
6186 break;
6187 pid = STAILQ_FIRST(
6188 got_object_commit_get_parent_ids(commit));
6189 if (pid == NULL) {
6190 got_object_commit_close(commit);
6191 break;
6193 /* Check if path history ends here. */
6194 err = got_object_open_as_commit(&pcommit,
6195 s->repo, &pid->id);
6196 if (err)
6197 break;
6198 err = got_object_id_by_path(&blob_id, s->repo,
6199 pcommit, s->path);
6200 got_object_commit_close(pcommit);
6201 if (err) {
6202 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6203 err = NULL;
6204 got_object_commit_close(commit);
6205 break;
6207 err = got_object_get_type(&obj_type, s->repo,
6208 blob_id);
6209 free(blob_id);
6210 /* Can't blame non-blob type objects. */
6211 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6212 got_object_commit_close(commit);
6213 break;
6215 err = got_object_qid_alloc(&s->blamed_commit,
6216 &pid->id);
6217 got_object_commit_close(commit);
6218 } else {
6219 if (got_object_id_cmp(id,
6220 &s->blamed_commit->id) == 0)
6221 break;
6222 err = got_object_qid_alloc(&s->blamed_commit,
6223 id);
6225 if (err)
6226 break;
6227 s->done = 1;
6228 thread_err = stop_blame(&s->blame);
6229 s->done = 0;
6230 if (thread_err)
6231 break;
6232 STAILQ_INSERT_HEAD(&s->blamed_commits,
6233 s->blamed_commit, entry);
6234 err = run_blame(view);
6235 if (err)
6236 break;
6237 break;
6239 case 'C': {
6240 struct got_object_qid *first;
6242 view->count = 0;
6243 first = STAILQ_FIRST(&s->blamed_commits);
6244 if (!got_object_id_cmp(&first->id, s->commit_id))
6245 break;
6246 s->done = 1;
6247 thread_err = stop_blame(&s->blame);
6248 s->done = 0;
6249 if (thread_err)
6250 break;
6251 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6252 got_object_qid_free(s->blamed_commit);
6253 s->blamed_commit =
6254 STAILQ_FIRST(&s->blamed_commits);
6255 err = run_blame(view);
6256 if (err)
6257 break;
6258 break;
6260 case 'L':
6261 view->count = 0;
6262 s->id_to_log = get_selected_commit_id(s->blame.lines,
6263 s->blame.nlines, s->first_displayed_line, s->selected_line);
6264 if (s->id_to_log)
6265 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6266 break;
6267 case KEY_ENTER:
6268 case '\r': {
6269 struct got_object_id *id = NULL;
6270 struct got_object_qid *pid;
6271 struct got_commit_object *commit = NULL;
6273 view->count = 0;
6274 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6275 s->first_displayed_line, s->selected_line);
6276 if (id == NULL)
6277 break;
6278 err = got_object_open_as_commit(&commit, s->repo, id);
6279 if (err)
6280 break;
6281 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6282 if (*new_view) {
6283 /* traversed from diff view, release diff resources */
6284 err = close_diff_view(*new_view);
6285 if (err)
6286 break;
6287 diff_view = *new_view;
6288 } else {
6289 if (view_is_parent_view(view))
6290 view_get_split(view, &begin_y, &begin_x);
6292 diff_view = view_open(0, 0, begin_y, begin_x,
6293 TOG_VIEW_DIFF);
6294 if (diff_view == NULL) {
6295 got_object_commit_close(commit);
6296 err = got_error_from_errno("view_open");
6297 break;
6300 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6301 id, NULL, NULL, 3, 0, 0, view, s->repo);
6302 got_object_commit_close(commit);
6303 if (err) {
6304 view_close(diff_view);
6305 break;
6307 s->last_diffed_line = s->first_displayed_line - 1 +
6308 s->selected_line;
6309 if (*new_view)
6310 break; /* still open from active diff view */
6311 if (view_is_parent_view(view) &&
6312 view->mode == TOG_VIEW_SPLIT_HRZN) {
6313 err = view_init_hsplit(view, begin_y);
6314 if (err)
6315 break;
6318 view->focussed = 0;
6319 diff_view->focussed = 1;
6320 diff_view->mode = view->mode;
6321 diff_view->nlines = view->lines - begin_y;
6322 if (view_is_parent_view(view)) {
6323 view_transfer_size(diff_view, view);
6324 err = view_close_child(view);
6325 if (err)
6326 break;
6327 err = view_set_child(view, diff_view);
6328 if (err)
6329 break;
6330 view->focus_child = 1;
6331 } else
6332 *new_view = diff_view;
6333 if (err)
6334 break;
6335 break;
6337 case CTRL('d'):
6338 case 'd':
6339 nscroll /= 2;
6340 /* FALL THROUGH */
6341 case KEY_NPAGE:
6342 case CTRL('f'):
6343 case 'f':
6344 case ' ':
6345 if (s->last_displayed_line >= s->blame.nlines &&
6346 s->selected_line >= MIN(s->blame.nlines,
6347 view->nlines - 2)) {
6348 view->count = 0;
6349 break;
6351 if (s->last_displayed_line >= s->blame.nlines &&
6352 s->selected_line < view->nlines - 2) {
6353 s->selected_line +=
6354 MIN(nscroll, s->last_displayed_line -
6355 s->first_displayed_line - s->selected_line + 1);
6357 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6358 s->first_displayed_line += nscroll;
6359 else
6360 s->first_displayed_line =
6361 s->blame.nlines - (view->nlines - 3);
6362 break;
6363 case KEY_RESIZE:
6364 if (s->selected_line > view->nlines - 2) {
6365 s->selected_line = MIN(s->blame.nlines,
6366 view->nlines - 2);
6368 break;
6369 default:
6370 view->count = 0;
6371 break;
6373 return thread_err ? thread_err : err;
6376 static const struct got_error *
6377 reset_blame_view(struct tog_view *view)
6379 const struct got_error *err;
6380 struct tog_blame_view_state *s = &view->state.blame;
6382 view->count = 0;
6383 s->done = 1;
6384 err = stop_blame(&s->blame);
6385 s->done = 0;
6386 if (err)
6387 return err;
6388 return run_blame(view);
6391 static const struct got_error *
6392 cmd_blame(int argc, char *argv[])
6394 const struct got_error *error;
6395 struct got_repository *repo = NULL;
6396 struct got_worktree *worktree = NULL;
6397 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6398 char *link_target = NULL;
6399 struct got_object_id *commit_id = NULL;
6400 struct got_commit_object *commit = NULL;
6401 char *commit_id_str = NULL;
6402 int ch;
6403 struct tog_view *view;
6404 int *pack_fds = NULL;
6406 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6407 switch (ch) {
6408 case 'c':
6409 commit_id_str = optarg;
6410 break;
6411 case 'r':
6412 repo_path = realpath(optarg, NULL);
6413 if (repo_path == NULL)
6414 return got_error_from_errno2("realpath",
6415 optarg);
6416 break;
6417 default:
6418 usage_blame();
6419 /* NOTREACHED */
6423 argc -= optind;
6424 argv += optind;
6426 if (argc != 1)
6427 usage_blame();
6429 error = got_repo_pack_fds_open(&pack_fds);
6430 if (error != NULL)
6431 goto done;
6433 if (repo_path == NULL) {
6434 cwd = getcwd(NULL, 0);
6435 if (cwd == NULL)
6436 return got_error_from_errno("getcwd");
6437 error = got_worktree_open(&worktree, cwd);
6438 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6439 goto done;
6440 if (worktree)
6441 repo_path =
6442 strdup(got_worktree_get_repo_path(worktree));
6443 else
6444 repo_path = strdup(cwd);
6445 if (repo_path == NULL) {
6446 error = got_error_from_errno("strdup");
6447 goto done;
6451 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6452 if (error != NULL)
6453 goto done;
6455 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6456 worktree);
6457 if (error)
6458 goto done;
6460 init_curses();
6462 error = apply_unveil(got_repo_get_path(repo), NULL);
6463 if (error)
6464 goto done;
6466 error = tog_load_refs(repo, 0);
6467 if (error)
6468 goto done;
6470 if (commit_id_str == NULL) {
6471 struct got_reference *head_ref;
6472 error = got_ref_open(&head_ref, repo, worktree ?
6473 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6474 if (error != NULL)
6475 goto done;
6476 error = got_ref_resolve(&commit_id, repo, head_ref);
6477 got_ref_close(head_ref);
6478 } else {
6479 error = got_repo_match_object_id(&commit_id, NULL,
6480 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6482 if (error != NULL)
6483 goto done;
6485 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6486 if (view == NULL) {
6487 error = got_error_from_errno("view_open");
6488 goto done;
6491 error = got_object_open_as_commit(&commit, repo, commit_id);
6492 if (error)
6493 goto done;
6495 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6496 commit, repo);
6497 if (error)
6498 goto done;
6500 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6501 commit_id, repo);
6502 if (error)
6503 goto done;
6504 if (worktree) {
6505 /* Release work tree lock. */
6506 got_worktree_close(worktree);
6507 worktree = NULL;
6509 error = view_loop(view);
6510 done:
6511 free(repo_path);
6512 free(in_repo_path);
6513 free(link_target);
6514 free(cwd);
6515 free(commit_id);
6516 if (commit)
6517 got_object_commit_close(commit);
6518 if (worktree)
6519 got_worktree_close(worktree);
6520 if (repo) {
6521 const struct got_error *close_err = got_repo_close(repo);
6522 if (error == NULL)
6523 error = close_err;
6525 if (pack_fds) {
6526 const struct got_error *pack_err =
6527 got_repo_pack_fds_close(pack_fds);
6528 if (error == NULL)
6529 error = pack_err;
6531 tog_free_refs();
6532 return error;
6535 static const struct got_error *
6536 draw_tree_entries(struct tog_view *view, const char *parent_path)
6538 struct tog_tree_view_state *s = &view->state.tree;
6539 const struct got_error *err = NULL;
6540 struct got_tree_entry *te;
6541 wchar_t *wline;
6542 char *index = NULL;
6543 struct tog_color *tc;
6544 int width, n, nentries, i = 1;
6545 int limit = view->nlines;
6547 s->ndisplayed = 0;
6548 if (view_is_hsplit_top(view))
6549 --limit; /* border */
6551 werase(view->window);
6553 if (limit == 0)
6554 return NULL;
6556 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6557 0, 0);
6558 if (err)
6559 return err;
6560 if (view_needs_focus_indication(view))
6561 wstandout(view->window);
6562 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6563 if (tc)
6564 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6565 waddwstr(view->window, wline);
6566 free(wline);
6567 wline = NULL;
6568 while (width++ < view->ncols)
6569 waddch(view->window, ' ');
6570 if (tc)
6571 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6572 if (view_needs_focus_indication(view))
6573 wstandend(view->window);
6574 if (--limit <= 0)
6575 return NULL;
6577 i += s->selected;
6578 if (s->first_displayed_entry) {
6579 i += got_tree_entry_get_index(s->first_displayed_entry);
6580 if (s->tree != s->root)
6581 ++i; /* account for ".." entry */
6583 nentries = got_object_tree_get_nentries(s->tree);
6584 if (asprintf(&index, "[%d/%d] %s",
6585 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6586 return got_error_from_errno("asprintf");
6587 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6588 free(index);
6589 if (err)
6590 return err;
6591 waddwstr(view->window, wline);
6592 free(wline);
6593 wline = NULL;
6594 if (width < view->ncols - 1)
6595 waddch(view->window, '\n');
6596 if (--limit <= 0)
6597 return NULL;
6598 waddch(view->window, '\n');
6599 if (--limit <= 0)
6600 return NULL;
6602 if (s->first_displayed_entry == NULL) {
6603 te = got_object_tree_get_first_entry(s->tree);
6604 if (s->selected == 0) {
6605 if (view->focussed)
6606 wstandout(view->window);
6607 s->selected_entry = NULL;
6609 waddstr(view->window, " ..\n"); /* parent directory */
6610 if (s->selected == 0 && view->focussed)
6611 wstandend(view->window);
6612 s->ndisplayed++;
6613 if (--limit <= 0)
6614 return NULL;
6615 n = 1;
6616 } else {
6617 n = 0;
6618 te = s->first_displayed_entry;
6621 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6622 char *line = NULL, *id_str = NULL, *link_target = NULL;
6623 const char *modestr = "";
6624 mode_t mode;
6626 te = got_object_tree_get_entry(s->tree, i);
6627 mode = got_tree_entry_get_mode(te);
6629 if (s->show_ids) {
6630 err = got_object_id_str(&id_str,
6631 got_tree_entry_get_id(te));
6632 if (err)
6633 return got_error_from_errno(
6634 "got_object_id_str");
6636 if (got_object_tree_entry_is_submodule(te))
6637 modestr = "$";
6638 else if (S_ISLNK(mode)) {
6639 int i;
6641 err = got_tree_entry_get_symlink_target(&link_target,
6642 te, s->repo);
6643 if (err) {
6644 free(id_str);
6645 return err;
6647 for (i = 0; i < strlen(link_target); i++) {
6648 if (!isprint((unsigned char)link_target[i]))
6649 link_target[i] = '?';
6651 modestr = "@";
6653 else if (S_ISDIR(mode))
6654 modestr = "/";
6655 else if (mode & S_IXUSR)
6656 modestr = "*";
6657 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6658 got_tree_entry_get_name(te), modestr,
6659 link_target ? " -> ": "",
6660 link_target ? link_target : "") == -1) {
6661 free(id_str);
6662 free(link_target);
6663 return got_error_from_errno("asprintf");
6665 free(id_str);
6666 free(link_target);
6667 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6668 0, 0);
6669 if (err) {
6670 free(line);
6671 break;
6673 if (n == s->selected) {
6674 if (view->focussed)
6675 wstandout(view->window);
6676 s->selected_entry = te;
6678 tc = match_color(&s->colors, line);
6679 if (tc)
6680 wattr_on(view->window,
6681 COLOR_PAIR(tc->colorpair), NULL);
6682 waddwstr(view->window, wline);
6683 if (tc)
6684 wattr_off(view->window,
6685 COLOR_PAIR(tc->colorpair), NULL);
6686 if (width < view->ncols - 1)
6687 waddch(view->window, '\n');
6688 if (n == s->selected && view->focussed)
6689 wstandend(view->window);
6690 free(line);
6691 free(wline);
6692 wline = NULL;
6693 n++;
6694 s->ndisplayed++;
6695 s->last_displayed_entry = te;
6696 if (--limit <= 0)
6697 break;
6700 return err;
6703 static void
6704 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6706 struct got_tree_entry *te;
6707 int isroot = s->tree == s->root;
6708 int i = 0;
6710 if (s->first_displayed_entry == NULL)
6711 return;
6713 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6714 while (i++ < maxscroll) {
6715 if (te == NULL) {
6716 if (!isroot)
6717 s->first_displayed_entry = NULL;
6718 break;
6720 s->first_displayed_entry = te;
6721 te = got_tree_entry_get_prev(s->tree, te);
6725 static const struct got_error *
6726 tree_scroll_down(struct tog_view *view, int maxscroll)
6728 struct tog_tree_view_state *s = &view->state.tree;
6729 struct got_tree_entry *next, *last;
6730 int n = 0;
6732 if (s->first_displayed_entry)
6733 next = got_tree_entry_get_next(s->tree,
6734 s->first_displayed_entry);
6735 else
6736 next = got_object_tree_get_first_entry(s->tree);
6738 last = s->last_displayed_entry;
6739 while (next && n++ < maxscroll) {
6740 if (last) {
6741 s->last_displayed_entry = last;
6742 last = got_tree_entry_get_next(s->tree, last);
6744 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6745 s->first_displayed_entry = next;
6746 next = got_tree_entry_get_next(s->tree, next);
6750 return NULL;
6753 static const struct got_error *
6754 tree_entry_path(char **path, struct tog_parent_trees *parents,
6755 struct got_tree_entry *te)
6757 const struct got_error *err = NULL;
6758 struct tog_parent_tree *pt;
6759 size_t len = 2; /* for leading slash and NUL */
6761 TAILQ_FOREACH(pt, parents, entry)
6762 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6763 + 1 /* slash */;
6764 if (te)
6765 len += strlen(got_tree_entry_get_name(te));
6767 *path = calloc(1, len);
6768 if (path == NULL)
6769 return got_error_from_errno("calloc");
6771 (*path)[0] = '/';
6772 pt = TAILQ_LAST(parents, tog_parent_trees);
6773 while (pt) {
6774 const char *name = got_tree_entry_get_name(pt->selected_entry);
6775 if (strlcat(*path, name, len) >= len) {
6776 err = got_error(GOT_ERR_NO_SPACE);
6777 goto done;
6779 if (strlcat(*path, "/", len) >= len) {
6780 err = got_error(GOT_ERR_NO_SPACE);
6781 goto done;
6783 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6785 if (te) {
6786 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6787 err = got_error(GOT_ERR_NO_SPACE);
6788 goto done;
6791 done:
6792 if (err) {
6793 free(*path);
6794 *path = NULL;
6796 return err;
6799 static const struct got_error *
6800 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6801 struct got_tree_entry *te, struct tog_parent_trees *parents,
6802 struct got_object_id *commit_id, struct got_repository *repo)
6804 const struct got_error *err = NULL;
6805 char *path;
6806 struct tog_view *blame_view;
6808 *new_view = NULL;
6810 err = tree_entry_path(&path, parents, te);
6811 if (err)
6812 return err;
6814 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6815 if (blame_view == NULL) {
6816 err = got_error_from_errno("view_open");
6817 goto done;
6820 err = open_blame_view(blame_view, path, commit_id, repo);
6821 if (err) {
6822 if (err->code == GOT_ERR_CANCELLED)
6823 err = NULL;
6824 view_close(blame_view);
6825 } else
6826 *new_view = blame_view;
6827 done:
6828 free(path);
6829 return err;
6832 static const struct got_error *
6833 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6834 struct tog_tree_view_state *s)
6836 struct tog_view *log_view;
6837 const struct got_error *err = NULL;
6838 char *path;
6840 *new_view = NULL;
6842 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6843 if (log_view == NULL)
6844 return got_error_from_errno("view_open");
6846 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6847 if (err)
6848 return err;
6850 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6851 path, 0);
6852 if (err)
6853 view_close(log_view);
6854 else
6855 *new_view = log_view;
6856 free(path);
6857 return err;
6860 static const struct got_error *
6861 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6862 const char *head_ref_name, struct got_repository *repo)
6864 const struct got_error *err = NULL;
6865 char *commit_id_str = NULL;
6866 struct tog_tree_view_state *s = &view->state.tree;
6867 struct got_commit_object *commit = NULL;
6869 TAILQ_INIT(&s->parents);
6870 STAILQ_INIT(&s->colors);
6872 s->commit_id = got_object_id_dup(commit_id);
6873 if (s->commit_id == NULL)
6874 return got_error_from_errno("got_object_id_dup");
6876 err = got_object_open_as_commit(&commit, repo, commit_id);
6877 if (err)
6878 goto done;
6881 * The root is opened here and will be closed when the view is closed.
6882 * Any visited subtrees and their path-wise parents are opened and
6883 * closed on demand.
6885 err = got_object_open_as_tree(&s->root, repo,
6886 got_object_commit_get_tree_id(commit));
6887 if (err)
6888 goto done;
6889 s->tree = s->root;
6891 err = got_object_id_str(&commit_id_str, commit_id);
6892 if (err != NULL)
6893 goto done;
6895 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6896 err = got_error_from_errno("asprintf");
6897 goto done;
6900 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6901 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6902 if (head_ref_name) {
6903 s->head_ref_name = strdup(head_ref_name);
6904 if (s->head_ref_name == NULL) {
6905 err = got_error_from_errno("strdup");
6906 goto done;
6909 s->repo = repo;
6911 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6912 err = add_color(&s->colors, "\\$$",
6913 TOG_COLOR_TREE_SUBMODULE,
6914 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6915 if (err)
6916 goto done;
6917 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6918 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6919 if (err)
6920 goto done;
6921 err = add_color(&s->colors, "/$",
6922 TOG_COLOR_TREE_DIRECTORY,
6923 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6924 if (err)
6925 goto done;
6927 err = add_color(&s->colors, "\\*$",
6928 TOG_COLOR_TREE_EXECUTABLE,
6929 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6930 if (err)
6931 goto done;
6933 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6934 get_color_value("TOG_COLOR_COMMIT"));
6935 if (err)
6936 goto done;
6939 view->show = show_tree_view;
6940 view->input = input_tree_view;
6941 view->close = close_tree_view;
6942 view->search_start = search_start_tree_view;
6943 view->search_next = search_next_tree_view;
6944 done:
6945 free(commit_id_str);
6946 if (commit)
6947 got_object_commit_close(commit);
6948 if (err)
6949 close_tree_view(view);
6950 return err;
6953 static const struct got_error *
6954 close_tree_view(struct tog_view *view)
6956 struct tog_tree_view_state *s = &view->state.tree;
6958 free_colors(&s->colors);
6959 free(s->tree_label);
6960 s->tree_label = NULL;
6961 free(s->commit_id);
6962 s->commit_id = NULL;
6963 free(s->head_ref_name);
6964 s->head_ref_name = NULL;
6965 while (!TAILQ_EMPTY(&s->parents)) {
6966 struct tog_parent_tree *parent;
6967 parent = TAILQ_FIRST(&s->parents);
6968 TAILQ_REMOVE(&s->parents, parent, entry);
6969 if (parent->tree != s->root)
6970 got_object_tree_close(parent->tree);
6971 free(parent);
6974 if (s->tree != NULL && s->tree != s->root)
6975 got_object_tree_close(s->tree);
6976 if (s->root)
6977 got_object_tree_close(s->root);
6978 return NULL;
6981 static const struct got_error *
6982 search_start_tree_view(struct tog_view *view)
6984 struct tog_tree_view_state *s = &view->state.tree;
6986 s->matched_entry = NULL;
6987 return NULL;
6990 static int
6991 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6993 regmatch_t regmatch;
6995 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6996 0) == 0;
6999 static const struct got_error *
7000 search_next_tree_view(struct tog_view *view)
7002 struct tog_tree_view_state *s = &view->state.tree;
7003 struct got_tree_entry *te = NULL;
7005 if (!view->searching) {
7006 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7007 return NULL;
7010 if (s->matched_entry) {
7011 if (view->searching == TOG_SEARCH_FORWARD) {
7012 if (s->selected_entry)
7013 te = got_tree_entry_get_next(s->tree,
7014 s->selected_entry);
7015 else
7016 te = got_object_tree_get_first_entry(s->tree);
7017 } else {
7018 if (s->selected_entry == NULL)
7019 te = got_object_tree_get_last_entry(s->tree);
7020 else
7021 te = got_tree_entry_get_prev(s->tree,
7022 s->selected_entry);
7024 } else {
7025 if (s->selected_entry)
7026 te = s->selected_entry;
7027 else if (view->searching == TOG_SEARCH_FORWARD)
7028 te = got_object_tree_get_first_entry(s->tree);
7029 else
7030 te = got_object_tree_get_last_entry(s->tree);
7033 while (1) {
7034 if (te == NULL) {
7035 if (s->matched_entry == NULL) {
7036 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7037 return NULL;
7039 if (view->searching == TOG_SEARCH_FORWARD)
7040 te = got_object_tree_get_first_entry(s->tree);
7041 else
7042 te = got_object_tree_get_last_entry(s->tree);
7045 if (match_tree_entry(te, &view->regex)) {
7046 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7047 s->matched_entry = te;
7048 break;
7051 if (view->searching == TOG_SEARCH_FORWARD)
7052 te = got_tree_entry_get_next(s->tree, te);
7053 else
7054 te = got_tree_entry_get_prev(s->tree, te);
7057 if (s->matched_entry) {
7058 s->first_displayed_entry = s->matched_entry;
7059 s->selected = 0;
7062 return NULL;
7065 static const struct got_error *
7066 show_tree_view(struct tog_view *view)
7068 const struct got_error *err = NULL;
7069 struct tog_tree_view_state *s = &view->state.tree;
7070 char *parent_path;
7072 err = tree_entry_path(&parent_path, &s->parents, NULL);
7073 if (err)
7074 return err;
7076 err = draw_tree_entries(view, parent_path);
7077 free(parent_path);
7079 view_border(view);
7080 return err;
7083 static const struct got_error *
7084 tree_goto_line(struct tog_view *view, int nlines)
7086 const struct got_error *err = NULL;
7087 struct tog_tree_view_state *s = &view->state.tree;
7088 struct got_tree_entry **fte, **lte, **ste;
7089 int g, last, first = 1, i = 1;
7090 int root = s->tree == s->root;
7091 int off = root ? 1 : 2;
7093 g = view->gline;
7094 view->gline = 0;
7096 if (g == 0)
7097 g = 1;
7098 else if (g > got_object_tree_get_nentries(s->tree))
7099 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7101 fte = &s->first_displayed_entry;
7102 lte = &s->last_displayed_entry;
7103 ste = &s->selected_entry;
7105 if (*fte != NULL) {
7106 first = got_tree_entry_get_index(*fte);
7107 first += off; /* account for ".." */
7109 last = got_tree_entry_get_index(*lte);
7110 last += off;
7112 if (g >= first && g <= last && g - first < nlines) {
7113 s->selected = g - first;
7114 return NULL; /* gline is on the current page */
7117 if (*ste != NULL) {
7118 i = got_tree_entry_get_index(*ste);
7119 i += off;
7122 if (i < g) {
7123 err = tree_scroll_down(view, g - i);
7124 if (err)
7125 return err;
7126 if (got_tree_entry_get_index(*lte) >=
7127 got_object_tree_get_nentries(s->tree) - 1 &&
7128 first + s->selected < g &&
7129 s->selected < s->ndisplayed - 1) {
7130 first = got_tree_entry_get_index(*fte);
7131 first += off;
7132 s->selected = g - first;
7134 } else if (i > g)
7135 tree_scroll_up(s, i - g);
7137 if (g < nlines &&
7138 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7139 s->selected = g - 1;
7141 return NULL;
7144 static const struct got_error *
7145 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7147 const struct got_error *err = NULL;
7148 struct tog_tree_view_state *s = &view->state.tree;
7149 struct got_tree_entry *te;
7150 int n, nscroll = view->nlines - 3;
7152 if (view->gline)
7153 return tree_goto_line(view, nscroll);
7155 switch (ch) {
7156 case 'i':
7157 s->show_ids = !s->show_ids;
7158 view->count = 0;
7159 break;
7160 case 'L':
7161 view->count = 0;
7162 if (!s->selected_entry)
7163 break;
7164 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7165 break;
7166 case 'R':
7167 view->count = 0;
7168 err = view_request_new(new_view, view, TOG_VIEW_REF);
7169 break;
7170 case 'g':
7171 case KEY_HOME:
7172 s->selected = 0;
7173 view->count = 0;
7174 if (s->tree == s->root)
7175 s->first_displayed_entry =
7176 got_object_tree_get_first_entry(s->tree);
7177 else
7178 s->first_displayed_entry = NULL;
7179 break;
7180 case 'G':
7181 case KEY_END: {
7182 int eos = view->nlines - 3;
7184 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7185 --eos; /* border */
7186 s->selected = 0;
7187 view->count = 0;
7188 te = got_object_tree_get_last_entry(s->tree);
7189 for (n = 0; n < eos; n++) {
7190 if (te == NULL) {
7191 if (s->tree != s->root) {
7192 s->first_displayed_entry = NULL;
7193 n++;
7195 break;
7197 s->first_displayed_entry = te;
7198 te = got_tree_entry_get_prev(s->tree, te);
7200 if (n > 0)
7201 s->selected = n - 1;
7202 break;
7204 case 'k':
7205 case KEY_UP:
7206 case CTRL('p'):
7207 if (s->selected > 0) {
7208 s->selected--;
7209 break;
7211 tree_scroll_up(s, 1);
7212 if (s->selected_entry == NULL ||
7213 (s->tree == s->root && s->selected_entry ==
7214 got_object_tree_get_first_entry(s->tree)))
7215 view->count = 0;
7216 break;
7217 case CTRL('u'):
7218 case 'u':
7219 nscroll /= 2;
7220 /* FALL THROUGH */
7221 case KEY_PPAGE:
7222 case CTRL('b'):
7223 case 'b':
7224 if (s->tree == s->root) {
7225 if (got_object_tree_get_first_entry(s->tree) ==
7226 s->first_displayed_entry)
7227 s->selected -= MIN(s->selected, nscroll);
7228 } else {
7229 if (s->first_displayed_entry == NULL)
7230 s->selected -= MIN(s->selected, nscroll);
7232 tree_scroll_up(s, MAX(0, nscroll));
7233 if (s->selected_entry == NULL ||
7234 (s->tree == s->root && s->selected_entry ==
7235 got_object_tree_get_first_entry(s->tree)))
7236 view->count = 0;
7237 break;
7238 case 'j':
7239 case KEY_DOWN:
7240 case CTRL('n'):
7241 if (s->selected < s->ndisplayed - 1) {
7242 s->selected++;
7243 break;
7245 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7246 == NULL) {
7247 /* can't scroll any further */
7248 view->count = 0;
7249 break;
7251 tree_scroll_down(view, 1);
7252 break;
7253 case CTRL('d'):
7254 case 'd':
7255 nscroll /= 2;
7256 /* FALL THROUGH */
7257 case KEY_NPAGE:
7258 case CTRL('f'):
7259 case 'f':
7260 case ' ':
7261 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7262 == NULL) {
7263 /* can't scroll any further; move cursor down */
7264 if (s->selected < s->ndisplayed - 1)
7265 s->selected += MIN(nscroll,
7266 s->ndisplayed - s->selected - 1);
7267 else
7268 view->count = 0;
7269 break;
7271 tree_scroll_down(view, nscroll);
7272 break;
7273 case KEY_ENTER:
7274 case '\r':
7275 case KEY_BACKSPACE:
7276 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7277 struct tog_parent_tree *parent;
7278 /* user selected '..' */
7279 if (s->tree == s->root) {
7280 view->count = 0;
7281 break;
7283 parent = TAILQ_FIRST(&s->parents);
7284 TAILQ_REMOVE(&s->parents, parent,
7285 entry);
7286 got_object_tree_close(s->tree);
7287 s->tree = parent->tree;
7288 s->first_displayed_entry =
7289 parent->first_displayed_entry;
7290 s->selected_entry =
7291 parent->selected_entry;
7292 s->selected = parent->selected;
7293 if (s->selected > view->nlines - 3) {
7294 err = offset_selection_down(view);
7295 if (err)
7296 break;
7298 free(parent);
7299 } else if (S_ISDIR(got_tree_entry_get_mode(
7300 s->selected_entry))) {
7301 struct got_tree_object *subtree;
7302 view->count = 0;
7303 err = got_object_open_as_tree(&subtree, s->repo,
7304 got_tree_entry_get_id(s->selected_entry));
7305 if (err)
7306 break;
7307 err = tree_view_visit_subtree(s, subtree);
7308 if (err) {
7309 got_object_tree_close(subtree);
7310 break;
7312 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7313 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7314 break;
7315 case KEY_RESIZE:
7316 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7317 s->selected = view->nlines - 4;
7318 view->count = 0;
7319 break;
7320 default:
7321 view->count = 0;
7322 break;
7325 return err;
7328 __dead static void
7329 usage_tree(void)
7331 endwin();
7332 fprintf(stderr,
7333 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7334 getprogname());
7335 exit(1);
7338 static const struct got_error *
7339 cmd_tree(int argc, char *argv[])
7341 const struct got_error *error;
7342 struct got_repository *repo = NULL;
7343 struct got_worktree *worktree = NULL;
7344 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7345 struct got_object_id *commit_id = NULL;
7346 struct got_commit_object *commit = NULL;
7347 const char *commit_id_arg = NULL;
7348 char *label = NULL;
7349 struct got_reference *ref = NULL;
7350 const char *head_ref_name = NULL;
7351 int ch;
7352 struct tog_view *view;
7353 int *pack_fds = NULL;
7355 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7356 switch (ch) {
7357 case 'c':
7358 commit_id_arg = optarg;
7359 break;
7360 case 'r':
7361 repo_path = realpath(optarg, NULL);
7362 if (repo_path == NULL)
7363 return got_error_from_errno2("realpath",
7364 optarg);
7365 break;
7366 default:
7367 usage_tree();
7368 /* NOTREACHED */
7372 argc -= optind;
7373 argv += optind;
7375 if (argc > 1)
7376 usage_tree();
7378 error = got_repo_pack_fds_open(&pack_fds);
7379 if (error != NULL)
7380 goto done;
7382 if (repo_path == NULL) {
7383 cwd = getcwd(NULL, 0);
7384 if (cwd == NULL)
7385 return got_error_from_errno("getcwd");
7386 error = got_worktree_open(&worktree, cwd);
7387 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7388 goto done;
7389 if (worktree)
7390 repo_path =
7391 strdup(got_worktree_get_repo_path(worktree));
7392 else
7393 repo_path = strdup(cwd);
7394 if (repo_path == NULL) {
7395 error = got_error_from_errno("strdup");
7396 goto done;
7400 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7401 if (error != NULL)
7402 goto done;
7404 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7405 repo, worktree);
7406 if (error)
7407 goto done;
7409 init_curses();
7411 error = apply_unveil(got_repo_get_path(repo), NULL);
7412 if (error)
7413 goto done;
7415 error = tog_load_refs(repo, 0);
7416 if (error)
7417 goto done;
7419 if (commit_id_arg == NULL) {
7420 error = got_repo_match_object_id(&commit_id, &label,
7421 worktree ? got_worktree_get_head_ref_name(worktree) :
7422 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7423 if (error)
7424 goto done;
7425 head_ref_name = label;
7426 } else {
7427 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7428 if (error == NULL)
7429 head_ref_name = got_ref_get_name(ref);
7430 else if (error->code != GOT_ERR_NOT_REF)
7431 goto done;
7432 error = got_repo_match_object_id(&commit_id, NULL,
7433 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7434 if (error)
7435 goto done;
7438 error = got_object_open_as_commit(&commit, repo, commit_id);
7439 if (error)
7440 goto done;
7442 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7443 if (view == NULL) {
7444 error = got_error_from_errno("view_open");
7445 goto done;
7447 error = open_tree_view(view, commit_id, head_ref_name, repo);
7448 if (error)
7449 goto done;
7450 if (!got_path_is_root_dir(in_repo_path)) {
7451 error = tree_view_walk_path(&view->state.tree, commit,
7452 in_repo_path);
7453 if (error)
7454 goto done;
7457 if (worktree) {
7458 /* Release work tree lock. */
7459 got_worktree_close(worktree);
7460 worktree = NULL;
7462 error = view_loop(view);
7463 done:
7464 free(repo_path);
7465 free(cwd);
7466 free(commit_id);
7467 free(label);
7468 if (ref)
7469 got_ref_close(ref);
7470 if (repo) {
7471 const struct got_error *close_err = got_repo_close(repo);
7472 if (error == NULL)
7473 error = close_err;
7475 if (pack_fds) {
7476 const struct got_error *pack_err =
7477 got_repo_pack_fds_close(pack_fds);
7478 if (error == NULL)
7479 error = pack_err;
7481 tog_free_refs();
7482 return error;
7485 static const struct got_error *
7486 ref_view_load_refs(struct tog_ref_view_state *s)
7488 struct got_reflist_entry *sre;
7489 struct tog_reflist_entry *re;
7491 s->nrefs = 0;
7492 TAILQ_FOREACH(sre, &tog_refs, entry) {
7493 if (strncmp(got_ref_get_name(sre->ref),
7494 "refs/got/", 9) == 0 &&
7495 strncmp(got_ref_get_name(sre->ref),
7496 "refs/got/backup/", 16) != 0)
7497 continue;
7499 re = malloc(sizeof(*re));
7500 if (re == NULL)
7501 return got_error_from_errno("malloc");
7503 re->ref = got_ref_dup(sre->ref);
7504 if (re->ref == NULL)
7505 return got_error_from_errno("got_ref_dup");
7506 re->idx = s->nrefs++;
7507 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7510 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7511 return NULL;
7514 static void
7515 ref_view_free_refs(struct tog_ref_view_state *s)
7517 struct tog_reflist_entry *re;
7519 while (!TAILQ_EMPTY(&s->refs)) {
7520 re = TAILQ_FIRST(&s->refs);
7521 TAILQ_REMOVE(&s->refs, re, entry);
7522 got_ref_close(re->ref);
7523 free(re);
7527 static const struct got_error *
7528 open_ref_view(struct tog_view *view, struct got_repository *repo)
7530 const struct got_error *err = NULL;
7531 struct tog_ref_view_state *s = &view->state.ref;
7533 s->selected_entry = 0;
7534 s->repo = repo;
7536 TAILQ_INIT(&s->refs);
7537 STAILQ_INIT(&s->colors);
7539 err = ref_view_load_refs(s);
7540 if (err)
7541 return err;
7543 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7544 err = add_color(&s->colors, "^refs/heads/",
7545 TOG_COLOR_REFS_HEADS,
7546 get_color_value("TOG_COLOR_REFS_HEADS"));
7547 if (err)
7548 goto done;
7550 err = add_color(&s->colors, "^refs/tags/",
7551 TOG_COLOR_REFS_TAGS,
7552 get_color_value("TOG_COLOR_REFS_TAGS"));
7553 if (err)
7554 goto done;
7556 err = add_color(&s->colors, "^refs/remotes/",
7557 TOG_COLOR_REFS_REMOTES,
7558 get_color_value("TOG_COLOR_REFS_REMOTES"));
7559 if (err)
7560 goto done;
7562 err = add_color(&s->colors, "^refs/got/backup/",
7563 TOG_COLOR_REFS_BACKUP,
7564 get_color_value("TOG_COLOR_REFS_BACKUP"));
7565 if (err)
7566 goto done;
7569 view->show = show_ref_view;
7570 view->input = input_ref_view;
7571 view->close = close_ref_view;
7572 view->search_start = search_start_ref_view;
7573 view->search_next = search_next_ref_view;
7574 done:
7575 if (err)
7576 free_colors(&s->colors);
7577 return err;
7580 static const struct got_error *
7581 close_ref_view(struct tog_view *view)
7583 struct tog_ref_view_state *s = &view->state.ref;
7585 ref_view_free_refs(s);
7586 free_colors(&s->colors);
7588 return NULL;
7591 static const struct got_error *
7592 resolve_reflist_entry(struct got_object_id **commit_id,
7593 struct tog_reflist_entry *re, struct got_repository *repo)
7595 const struct got_error *err = NULL;
7596 struct got_object_id *obj_id;
7597 struct got_tag_object *tag = NULL;
7598 int obj_type;
7600 *commit_id = NULL;
7602 err = got_ref_resolve(&obj_id, repo, re->ref);
7603 if (err)
7604 return err;
7606 err = got_object_get_type(&obj_type, repo, obj_id);
7607 if (err)
7608 goto done;
7610 switch (obj_type) {
7611 case GOT_OBJ_TYPE_COMMIT:
7612 *commit_id = obj_id;
7613 break;
7614 case GOT_OBJ_TYPE_TAG:
7615 err = got_object_open_as_tag(&tag, repo, obj_id);
7616 if (err)
7617 goto done;
7618 free(obj_id);
7619 err = got_object_get_type(&obj_type, repo,
7620 got_object_tag_get_object_id(tag));
7621 if (err)
7622 goto done;
7623 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7624 err = got_error(GOT_ERR_OBJ_TYPE);
7625 goto done;
7627 *commit_id = got_object_id_dup(
7628 got_object_tag_get_object_id(tag));
7629 if (*commit_id == NULL) {
7630 err = got_error_from_errno("got_object_id_dup");
7631 goto done;
7633 break;
7634 default:
7635 err = got_error(GOT_ERR_OBJ_TYPE);
7636 break;
7639 done:
7640 if (tag)
7641 got_object_tag_close(tag);
7642 if (err) {
7643 free(*commit_id);
7644 *commit_id = NULL;
7646 return err;
7649 static const struct got_error *
7650 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7651 struct tog_reflist_entry *re, struct got_repository *repo)
7653 struct tog_view *log_view;
7654 const struct got_error *err = NULL;
7655 struct got_object_id *commit_id = NULL;
7657 *new_view = NULL;
7659 err = resolve_reflist_entry(&commit_id, re, repo);
7660 if (err) {
7661 if (err->code != GOT_ERR_OBJ_TYPE)
7662 return err;
7663 else
7664 return NULL;
7667 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7668 if (log_view == NULL) {
7669 err = got_error_from_errno("view_open");
7670 goto done;
7673 err = open_log_view(log_view, commit_id, repo,
7674 got_ref_get_name(re->ref), "", 0);
7675 done:
7676 if (err)
7677 view_close(log_view);
7678 else
7679 *new_view = log_view;
7680 free(commit_id);
7681 return err;
7684 static void
7685 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7687 struct tog_reflist_entry *re;
7688 int i = 0;
7690 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7691 return;
7693 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7694 while (i++ < maxscroll) {
7695 if (re == NULL)
7696 break;
7697 s->first_displayed_entry = re;
7698 re = TAILQ_PREV(re, tog_reflist_head, entry);
7702 static const struct got_error *
7703 ref_scroll_down(struct tog_view *view, int maxscroll)
7705 struct tog_ref_view_state *s = &view->state.ref;
7706 struct tog_reflist_entry *next, *last;
7707 int n = 0;
7709 if (s->first_displayed_entry)
7710 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7711 else
7712 next = TAILQ_FIRST(&s->refs);
7714 last = s->last_displayed_entry;
7715 while (next && n++ < maxscroll) {
7716 if (last) {
7717 s->last_displayed_entry = last;
7718 last = TAILQ_NEXT(last, entry);
7720 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7721 s->first_displayed_entry = next;
7722 next = TAILQ_NEXT(next, entry);
7726 return NULL;
7729 static const struct got_error *
7730 search_start_ref_view(struct tog_view *view)
7732 struct tog_ref_view_state *s = &view->state.ref;
7734 s->matched_entry = NULL;
7735 return NULL;
7738 static int
7739 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7741 regmatch_t regmatch;
7743 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7744 0) == 0;
7747 static const struct got_error *
7748 search_next_ref_view(struct tog_view *view)
7750 struct tog_ref_view_state *s = &view->state.ref;
7751 struct tog_reflist_entry *re = NULL;
7753 if (!view->searching) {
7754 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7755 return NULL;
7758 if (s->matched_entry) {
7759 if (view->searching == TOG_SEARCH_FORWARD) {
7760 if (s->selected_entry)
7761 re = TAILQ_NEXT(s->selected_entry, entry);
7762 else
7763 re = TAILQ_PREV(s->selected_entry,
7764 tog_reflist_head, entry);
7765 } else {
7766 if (s->selected_entry == NULL)
7767 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7768 else
7769 re = TAILQ_PREV(s->selected_entry,
7770 tog_reflist_head, entry);
7772 } else {
7773 if (s->selected_entry)
7774 re = s->selected_entry;
7775 else if (view->searching == TOG_SEARCH_FORWARD)
7776 re = TAILQ_FIRST(&s->refs);
7777 else
7778 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7781 while (1) {
7782 if (re == NULL) {
7783 if (s->matched_entry == NULL) {
7784 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7785 return NULL;
7787 if (view->searching == TOG_SEARCH_FORWARD)
7788 re = TAILQ_FIRST(&s->refs);
7789 else
7790 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7793 if (match_reflist_entry(re, &view->regex)) {
7794 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7795 s->matched_entry = re;
7796 break;
7799 if (view->searching == TOG_SEARCH_FORWARD)
7800 re = TAILQ_NEXT(re, entry);
7801 else
7802 re = TAILQ_PREV(re, tog_reflist_head, entry);
7805 if (s->matched_entry) {
7806 s->first_displayed_entry = s->matched_entry;
7807 s->selected = 0;
7810 return NULL;
7813 static const struct got_error *
7814 show_ref_view(struct tog_view *view)
7816 const struct got_error *err = NULL;
7817 struct tog_ref_view_state *s = &view->state.ref;
7818 struct tog_reflist_entry *re;
7819 char *line = NULL;
7820 wchar_t *wline;
7821 struct tog_color *tc;
7822 int width, n;
7823 int limit = view->nlines;
7825 werase(view->window);
7827 s->ndisplayed = 0;
7828 if (view_is_hsplit_top(view))
7829 --limit; /* border */
7831 if (limit == 0)
7832 return NULL;
7834 re = s->first_displayed_entry;
7836 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7837 s->nrefs) == -1)
7838 return got_error_from_errno("asprintf");
7840 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7841 if (err) {
7842 free(line);
7843 return err;
7845 if (view_needs_focus_indication(view))
7846 wstandout(view->window);
7847 waddwstr(view->window, wline);
7848 while (width++ < view->ncols)
7849 waddch(view->window, ' ');
7850 if (view_needs_focus_indication(view))
7851 wstandend(view->window);
7852 free(wline);
7853 wline = NULL;
7854 free(line);
7855 line = NULL;
7856 if (--limit <= 0)
7857 return NULL;
7859 n = 0;
7860 while (re && limit > 0) {
7861 char *line = NULL;
7862 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7864 if (s->show_date) {
7865 struct got_commit_object *ci;
7866 struct got_tag_object *tag;
7867 struct got_object_id *id;
7868 struct tm tm;
7869 time_t t;
7871 err = got_ref_resolve(&id, s->repo, re->ref);
7872 if (err)
7873 return err;
7874 err = got_object_open_as_tag(&tag, s->repo, id);
7875 if (err) {
7876 if (err->code != GOT_ERR_OBJ_TYPE) {
7877 free(id);
7878 return err;
7880 err = got_object_open_as_commit(&ci, s->repo,
7881 id);
7882 if (err) {
7883 free(id);
7884 return err;
7886 t = got_object_commit_get_committer_time(ci);
7887 got_object_commit_close(ci);
7888 } else {
7889 t = got_object_tag_get_tagger_time(tag);
7890 got_object_tag_close(tag);
7892 free(id);
7893 if (gmtime_r(&t, &tm) == NULL)
7894 return got_error_from_errno("gmtime_r");
7895 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7896 return got_error(GOT_ERR_NO_SPACE);
7898 if (got_ref_is_symbolic(re->ref)) {
7899 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7900 ymd : "", got_ref_get_name(re->ref),
7901 got_ref_get_symref_target(re->ref)) == -1)
7902 return got_error_from_errno("asprintf");
7903 } else if (s->show_ids) {
7904 struct got_object_id *id;
7905 char *id_str;
7906 err = got_ref_resolve(&id, s->repo, re->ref);
7907 if (err)
7908 return err;
7909 err = got_object_id_str(&id_str, id);
7910 if (err) {
7911 free(id);
7912 return err;
7914 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7915 got_ref_get_name(re->ref), id_str) == -1) {
7916 err = got_error_from_errno("asprintf");
7917 free(id);
7918 free(id_str);
7919 return err;
7921 free(id);
7922 free(id_str);
7923 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7924 got_ref_get_name(re->ref)) == -1)
7925 return got_error_from_errno("asprintf");
7927 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7928 0, 0);
7929 if (err) {
7930 free(line);
7931 return err;
7933 if (n == s->selected) {
7934 if (view->focussed)
7935 wstandout(view->window);
7936 s->selected_entry = re;
7938 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7939 if (tc)
7940 wattr_on(view->window,
7941 COLOR_PAIR(tc->colorpair), NULL);
7942 waddwstr(view->window, wline);
7943 if (tc)
7944 wattr_off(view->window,
7945 COLOR_PAIR(tc->colorpair), NULL);
7946 if (width < view->ncols - 1)
7947 waddch(view->window, '\n');
7948 if (n == s->selected && view->focussed)
7949 wstandend(view->window);
7950 free(line);
7951 free(wline);
7952 wline = NULL;
7953 n++;
7954 s->ndisplayed++;
7955 s->last_displayed_entry = re;
7957 limit--;
7958 re = TAILQ_NEXT(re, entry);
7961 view_border(view);
7962 return err;
7965 static const struct got_error *
7966 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7967 struct tog_reflist_entry *re, struct got_repository *repo)
7969 const struct got_error *err = NULL;
7970 struct got_object_id *commit_id = NULL;
7971 struct tog_view *tree_view;
7973 *new_view = NULL;
7975 err = resolve_reflist_entry(&commit_id, re, repo);
7976 if (err) {
7977 if (err->code != GOT_ERR_OBJ_TYPE)
7978 return err;
7979 else
7980 return NULL;
7984 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7985 if (tree_view == NULL) {
7986 err = got_error_from_errno("view_open");
7987 goto done;
7990 err = open_tree_view(tree_view, commit_id,
7991 got_ref_get_name(re->ref), repo);
7992 if (err)
7993 goto done;
7995 *new_view = tree_view;
7996 done:
7997 free(commit_id);
7998 return err;
8001 static const struct got_error *
8002 ref_goto_line(struct tog_view *view, int nlines)
8004 const struct got_error *err = NULL;
8005 struct tog_ref_view_state *s = &view->state.ref;
8006 int g, idx = s->selected_entry->idx;
8008 g = view->gline;
8009 view->gline = 0;
8011 if (g == 0)
8012 g = 1;
8013 else if (g > s->nrefs)
8014 g = s->nrefs;
8016 if (g >= s->first_displayed_entry->idx + 1 &&
8017 g <= s->last_displayed_entry->idx + 1 &&
8018 g - s->first_displayed_entry->idx - 1 < nlines) {
8019 s->selected = g - s->first_displayed_entry->idx - 1;
8020 return NULL;
8023 if (idx + 1 < g) {
8024 err = ref_scroll_down(view, g - idx - 1);
8025 if (err)
8026 return err;
8027 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8028 s->first_displayed_entry->idx + s->selected < g &&
8029 s->selected < s->ndisplayed - 1)
8030 s->selected = g - s->first_displayed_entry->idx - 1;
8031 } else if (idx + 1 > g)
8032 ref_scroll_up(s, idx - g + 1);
8034 if (g < nlines && s->first_displayed_entry->idx == 0)
8035 s->selected = g - 1;
8037 return NULL;
8041 static const struct got_error *
8042 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8044 const struct got_error *err = NULL;
8045 struct tog_ref_view_state *s = &view->state.ref;
8046 struct tog_reflist_entry *re;
8047 int n, nscroll = view->nlines - 1;
8049 if (view->gline)
8050 return ref_goto_line(view, nscroll);
8052 switch (ch) {
8053 case 'i':
8054 s->show_ids = !s->show_ids;
8055 view->count = 0;
8056 break;
8057 case 'm':
8058 s->show_date = !s->show_date;
8059 view->count = 0;
8060 break;
8061 case 'o':
8062 s->sort_by_date = !s->sort_by_date;
8063 view->count = 0;
8064 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8065 got_ref_cmp_by_commit_timestamp_descending :
8066 tog_ref_cmp_by_name, s->repo);
8067 if (err)
8068 break;
8069 got_reflist_object_id_map_free(tog_refs_idmap);
8070 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8071 &tog_refs, s->repo);
8072 if (err)
8073 break;
8074 ref_view_free_refs(s);
8075 err = ref_view_load_refs(s);
8076 break;
8077 case KEY_ENTER:
8078 case '\r':
8079 view->count = 0;
8080 if (!s->selected_entry)
8081 break;
8082 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8083 break;
8084 case 'T':
8085 view->count = 0;
8086 if (!s->selected_entry)
8087 break;
8088 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8089 break;
8090 case 'g':
8091 case KEY_HOME:
8092 s->selected = 0;
8093 view->count = 0;
8094 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8095 break;
8096 case 'G':
8097 case KEY_END: {
8098 int eos = view->nlines - 1;
8100 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8101 --eos; /* border */
8102 s->selected = 0;
8103 view->count = 0;
8104 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8105 for (n = 0; n < eos; n++) {
8106 if (re == NULL)
8107 break;
8108 s->first_displayed_entry = re;
8109 re = TAILQ_PREV(re, tog_reflist_head, entry);
8111 if (n > 0)
8112 s->selected = n - 1;
8113 break;
8115 case 'k':
8116 case KEY_UP:
8117 case CTRL('p'):
8118 if (s->selected > 0) {
8119 s->selected--;
8120 break;
8122 ref_scroll_up(s, 1);
8123 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8124 view->count = 0;
8125 break;
8126 case CTRL('u'):
8127 case 'u':
8128 nscroll /= 2;
8129 /* FALL THROUGH */
8130 case KEY_PPAGE:
8131 case CTRL('b'):
8132 case 'b':
8133 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8134 s->selected -= MIN(nscroll, s->selected);
8135 ref_scroll_up(s, MAX(0, nscroll));
8136 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8137 view->count = 0;
8138 break;
8139 case 'j':
8140 case KEY_DOWN:
8141 case CTRL('n'):
8142 if (s->selected < s->ndisplayed - 1) {
8143 s->selected++;
8144 break;
8146 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8147 /* can't scroll any further */
8148 view->count = 0;
8149 break;
8151 ref_scroll_down(view, 1);
8152 break;
8153 case CTRL('d'):
8154 case 'd':
8155 nscroll /= 2;
8156 /* FALL THROUGH */
8157 case KEY_NPAGE:
8158 case CTRL('f'):
8159 case 'f':
8160 case ' ':
8161 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8162 /* can't scroll any further; move cursor down */
8163 if (s->selected < s->ndisplayed - 1)
8164 s->selected += MIN(nscroll,
8165 s->ndisplayed - s->selected - 1);
8166 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8167 s->selected += s->ndisplayed - s->selected - 1;
8168 view->count = 0;
8169 break;
8171 ref_scroll_down(view, nscroll);
8172 break;
8173 case CTRL('l'):
8174 view->count = 0;
8175 tog_free_refs();
8176 err = tog_load_refs(s->repo, s->sort_by_date);
8177 if (err)
8178 break;
8179 ref_view_free_refs(s);
8180 err = ref_view_load_refs(s);
8181 break;
8182 case KEY_RESIZE:
8183 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8184 s->selected = view->nlines - 2;
8185 break;
8186 default:
8187 view->count = 0;
8188 break;
8191 return err;
8194 __dead static void
8195 usage_ref(void)
8197 endwin();
8198 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8199 getprogname());
8200 exit(1);
8203 static const struct got_error *
8204 cmd_ref(int argc, char *argv[])
8206 const struct got_error *error;
8207 struct got_repository *repo = NULL;
8208 struct got_worktree *worktree = NULL;
8209 char *cwd = NULL, *repo_path = NULL;
8210 int ch;
8211 struct tog_view *view;
8212 int *pack_fds = NULL;
8214 while ((ch = getopt(argc, argv, "r:")) != -1) {
8215 switch (ch) {
8216 case 'r':
8217 repo_path = realpath(optarg, NULL);
8218 if (repo_path == NULL)
8219 return got_error_from_errno2("realpath",
8220 optarg);
8221 break;
8222 default:
8223 usage_ref();
8224 /* NOTREACHED */
8228 argc -= optind;
8229 argv += optind;
8231 if (argc > 1)
8232 usage_ref();
8234 error = got_repo_pack_fds_open(&pack_fds);
8235 if (error != NULL)
8236 goto done;
8238 if (repo_path == NULL) {
8239 cwd = getcwd(NULL, 0);
8240 if (cwd == NULL)
8241 return got_error_from_errno("getcwd");
8242 error = got_worktree_open(&worktree, cwd);
8243 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8244 goto done;
8245 if (worktree)
8246 repo_path =
8247 strdup(got_worktree_get_repo_path(worktree));
8248 else
8249 repo_path = strdup(cwd);
8250 if (repo_path == NULL) {
8251 error = got_error_from_errno("strdup");
8252 goto done;
8256 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8257 if (error != NULL)
8258 goto done;
8260 init_curses();
8262 error = apply_unveil(got_repo_get_path(repo), NULL);
8263 if (error)
8264 goto done;
8266 error = tog_load_refs(repo, 0);
8267 if (error)
8268 goto done;
8270 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8271 if (view == NULL) {
8272 error = got_error_from_errno("view_open");
8273 goto done;
8276 error = open_ref_view(view, repo);
8277 if (error)
8278 goto done;
8280 if (worktree) {
8281 /* Release work tree lock. */
8282 got_worktree_close(worktree);
8283 worktree = NULL;
8285 error = view_loop(view);
8286 done:
8287 free(repo_path);
8288 free(cwd);
8289 if (repo) {
8290 const struct got_error *close_err = got_repo_close(repo);
8291 if (close_err)
8292 error = close_err;
8294 if (pack_fds) {
8295 const struct got_error *pack_err =
8296 got_repo_pack_fds_close(pack_fds);
8297 if (error == NULL)
8298 error = pack_err;
8300 tog_free_refs();
8301 return error;
8304 static const struct got_error *
8305 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8306 enum tog_view_type request, int y, int x)
8308 const struct got_error *err = NULL;
8310 *new_view = NULL;
8312 switch (request) {
8313 case TOG_VIEW_DIFF:
8314 if (view->type == TOG_VIEW_LOG) {
8315 struct tog_log_view_state *s = &view->state.log;
8317 err = open_diff_view_for_commit(new_view, y, x,
8318 s->selected_entry->commit, s->selected_entry->id,
8319 view, s->repo);
8320 } else
8321 return got_error_msg(GOT_ERR_NOT_IMPL,
8322 "parent/child view pair not supported");
8323 break;
8324 case TOG_VIEW_BLAME:
8325 if (view->type == TOG_VIEW_TREE) {
8326 struct tog_tree_view_state *s = &view->state.tree;
8328 err = blame_tree_entry(new_view, y, x,
8329 s->selected_entry, &s->parents, s->commit_id,
8330 s->repo);
8331 } else
8332 return got_error_msg(GOT_ERR_NOT_IMPL,
8333 "parent/child view pair not supported");
8334 break;
8335 case TOG_VIEW_LOG:
8336 if (view->type == TOG_VIEW_BLAME)
8337 err = log_annotated_line(new_view, y, x,
8338 view->state.blame.repo, view->state.blame.id_to_log);
8339 else if (view->type == TOG_VIEW_TREE)
8340 err = log_selected_tree_entry(new_view, y, x,
8341 &view->state.tree);
8342 else if (view->type == TOG_VIEW_REF)
8343 err = log_ref_entry(new_view, y, x,
8344 view->state.ref.selected_entry,
8345 view->state.ref.repo);
8346 else
8347 return got_error_msg(GOT_ERR_NOT_IMPL,
8348 "parent/child view pair not supported");
8349 break;
8350 case TOG_VIEW_TREE:
8351 if (view->type == TOG_VIEW_LOG)
8352 err = browse_commit_tree(new_view, y, x,
8353 view->state.log.selected_entry,
8354 view->state.log.in_repo_path,
8355 view->state.log.head_ref_name,
8356 view->state.log.repo);
8357 else if (view->type == TOG_VIEW_REF)
8358 err = browse_ref_tree(new_view, y, x,
8359 view->state.ref.selected_entry,
8360 view->state.ref.repo);
8361 else
8362 return got_error_msg(GOT_ERR_NOT_IMPL,
8363 "parent/child view pair not supported");
8364 break;
8365 case TOG_VIEW_REF:
8366 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8367 if (*new_view == NULL)
8368 return got_error_from_errno("view_open");
8369 if (view->type == TOG_VIEW_LOG)
8370 err = open_ref_view(*new_view, view->state.log.repo);
8371 else if (view->type == TOG_VIEW_TREE)
8372 err = open_ref_view(*new_view, view->state.tree.repo);
8373 else
8374 err = got_error_msg(GOT_ERR_NOT_IMPL,
8375 "parent/child view pair not supported");
8376 if (err)
8377 view_close(*new_view);
8378 break;
8379 default:
8380 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8383 return err;
8387 * If view was scrolled down to move the selected line into view when opening a
8388 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8390 static void
8391 offset_selection_up(struct tog_view *view)
8393 switch (view->type) {
8394 case TOG_VIEW_BLAME: {
8395 struct tog_blame_view_state *s = &view->state.blame;
8396 if (s->first_displayed_line == 1) {
8397 s->selected_line = MAX(s->selected_line - view->offset,
8398 1);
8399 break;
8401 if (s->first_displayed_line > view->offset)
8402 s->first_displayed_line -= view->offset;
8403 else
8404 s->first_displayed_line = 1;
8405 s->selected_line += view->offset;
8406 break;
8408 case TOG_VIEW_LOG:
8409 log_scroll_up(&view->state.log, view->offset);
8410 view->state.log.selected += view->offset;
8411 break;
8412 case TOG_VIEW_REF:
8413 ref_scroll_up(&view->state.ref, view->offset);
8414 view->state.ref.selected += view->offset;
8415 break;
8416 case TOG_VIEW_TREE:
8417 tree_scroll_up(&view->state.tree, view->offset);
8418 view->state.tree.selected += view->offset;
8419 break;
8420 default:
8421 break;
8424 view->offset = 0;
8428 * If the selected line is in the section of screen covered by the bottom split,
8429 * scroll down offset lines to move it into view and index its new position.
8431 static const struct got_error *
8432 offset_selection_down(struct tog_view *view)
8434 const struct got_error *err = NULL;
8435 const struct got_error *(*scrolld)(struct tog_view *, int);
8436 int *selected = NULL;
8437 int header, offset;
8439 switch (view->type) {
8440 case TOG_VIEW_BLAME: {
8441 struct tog_blame_view_state *s = &view->state.blame;
8442 header = 3;
8443 scrolld = NULL;
8444 if (s->selected_line > view->nlines - header) {
8445 offset = abs(view->nlines - s->selected_line - header);
8446 s->first_displayed_line += offset;
8447 s->selected_line -= offset;
8448 view->offset = offset;
8450 break;
8452 case TOG_VIEW_LOG: {
8453 struct tog_log_view_state *s = &view->state.log;
8454 scrolld = &log_scroll_down;
8455 header = view_is_parent_view(view) ? 3 : 2;
8456 selected = &s->selected;
8457 break;
8459 case TOG_VIEW_REF: {
8460 struct tog_ref_view_state *s = &view->state.ref;
8461 scrolld = &ref_scroll_down;
8462 header = 3;
8463 selected = &s->selected;
8464 break;
8466 case TOG_VIEW_TREE: {
8467 struct tog_tree_view_state *s = &view->state.tree;
8468 scrolld = &tree_scroll_down;
8469 header = 5;
8470 selected = &s->selected;
8471 break;
8473 default:
8474 selected = NULL;
8475 scrolld = NULL;
8476 header = 0;
8477 break;
8480 if (selected && *selected > view->nlines - header) {
8481 offset = abs(view->nlines - *selected - header);
8482 view->offset = offset;
8483 if (scrolld && offset) {
8484 err = scrolld(view, offset);
8485 *selected -= offset;
8489 return err;
8492 static void
8493 list_commands(FILE *fp)
8495 size_t i;
8497 fprintf(fp, "commands:");
8498 for (i = 0; i < nitems(tog_commands); i++) {
8499 const struct tog_cmd *cmd = &tog_commands[i];
8500 fprintf(fp, " %s", cmd->name);
8502 fputc('\n', fp);
8505 __dead static void
8506 usage(int hflag, int status)
8508 FILE *fp = (status == 0) ? stdout : stderr;
8510 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8511 getprogname());
8512 if (hflag) {
8513 fprintf(fp, "lazy usage: %s path\n", getprogname());
8514 list_commands(fp);
8516 exit(status);
8519 static char **
8520 make_argv(int argc, ...)
8522 va_list ap;
8523 char **argv;
8524 int i;
8526 va_start(ap, argc);
8528 argv = calloc(argc, sizeof(char *));
8529 if (argv == NULL)
8530 err(1, "calloc");
8531 for (i = 0; i < argc; i++) {
8532 argv[i] = strdup(va_arg(ap, char *));
8533 if (argv[i] == NULL)
8534 err(1, "strdup");
8537 va_end(ap);
8538 return argv;
8542 * Try to convert 'tog path' into a 'tog log path' command.
8543 * The user could simply have mistyped the command rather than knowingly
8544 * provided a path. So check whether argv[0] can in fact be resolved
8545 * to a path in the HEAD commit and print a special error if not.
8546 * This hack is for mpi@ <3
8548 static const struct got_error *
8549 tog_log_with_path(int argc, char *argv[])
8551 const struct got_error *error = NULL, *close_err;
8552 const struct tog_cmd *cmd = NULL;
8553 struct got_repository *repo = NULL;
8554 struct got_worktree *worktree = NULL;
8555 struct got_object_id *commit_id = NULL, *id = NULL;
8556 struct got_commit_object *commit = NULL;
8557 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8558 char *commit_id_str = NULL, **cmd_argv = NULL;
8559 int *pack_fds = NULL;
8561 cwd = getcwd(NULL, 0);
8562 if (cwd == NULL)
8563 return got_error_from_errno("getcwd");
8565 error = got_repo_pack_fds_open(&pack_fds);
8566 if (error != NULL)
8567 goto done;
8569 error = got_worktree_open(&worktree, cwd);
8570 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8571 goto done;
8573 if (worktree)
8574 repo_path = strdup(got_worktree_get_repo_path(worktree));
8575 else
8576 repo_path = strdup(cwd);
8577 if (repo_path == NULL) {
8578 error = got_error_from_errno("strdup");
8579 goto done;
8582 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8583 if (error != NULL)
8584 goto done;
8586 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8587 repo, worktree);
8588 if (error)
8589 goto done;
8591 error = tog_load_refs(repo, 0);
8592 if (error)
8593 goto done;
8594 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8595 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8596 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8597 if (error)
8598 goto done;
8600 if (worktree) {
8601 got_worktree_close(worktree);
8602 worktree = NULL;
8605 error = got_object_open_as_commit(&commit, repo, commit_id);
8606 if (error)
8607 goto done;
8609 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8610 if (error) {
8611 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8612 goto done;
8613 fprintf(stderr, "%s: '%s' is no known command or path\n",
8614 getprogname(), argv[0]);
8615 usage(1, 1);
8616 /* not reached */
8619 error = got_object_id_str(&commit_id_str, commit_id);
8620 if (error)
8621 goto done;
8623 cmd = &tog_commands[0]; /* log */
8624 argc = 4;
8625 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8626 error = cmd->cmd_main(argc, cmd_argv);
8627 done:
8628 if (repo) {
8629 close_err = got_repo_close(repo);
8630 if (error == NULL)
8631 error = close_err;
8633 if (commit)
8634 got_object_commit_close(commit);
8635 if (worktree)
8636 got_worktree_close(worktree);
8637 if (pack_fds) {
8638 const struct got_error *pack_err =
8639 got_repo_pack_fds_close(pack_fds);
8640 if (error == NULL)
8641 error = pack_err;
8643 free(id);
8644 free(commit_id_str);
8645 free(commit_id);
8646 free(cwd);
8647 free(repo_path);
8648 free(in_repo_path);
8649 if (cmd_argv) {
8650 int i;
8651 for (i = 0; i < argc; i++)
8652 free(cmd_argv[i]);
8653 free(cmd_argv);
8655 tog_free_refs();
8656 return error;
8659 int
8660 main(int argc, char *argv[])
8662 const struct got_error *error = NULL;
8663 const struct tog_cmd *cmd = NULL;
8664 int ch, hflag = 0, Vflag = 0;
8665 char **cmd_argv = NULL;
8666 static const struct option longopts[] = {
8667 { "version", no_argument, NULL, 'V' },
8668 { NULL, 0, NULL, 0}
8670 char *diff_algo_str = NULL;
8672 if (!isatty(STDIN_FILENO))
8673 errx(1, "standard input is not a tty");
8675 setlocale(LC_CTYPE, "");
8677 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8678 switch (ch) {
8679 case 'h':
8680 hflag = 1;
8681 break;
8682 case 'V':
8683 Vflag = 1;
8684 break;
8685 default:
8686 usage(hflag, 1);
8687 /* NOTREACHED */
8691 argc -= optind;
8692 argv += optind;
8693 optind = 1;
8694 optreset = 1;
8696 if (Vflag) {
8697 got_version_print_str();
8698 return 0;
8701 #ifndef PROFILE
8702 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8703 NULL) == -1)
8704 err(1, "pledge");
8705 #endif
8707 if (argc == 0) {
8708 if (hflag)
8709 usage(hflag, 0);
8710 /* Build an argument vector which runs a default command. */
8711 cmd = &tog_commands[0];
8712 argc = 1;
8713 cmd_argv = make_argv(argc, cmd->name);
8714 } else {
8715 size_t i;
8717 /* Did the user specify a command? */
8718 for (i = 0; i < nitems(tog_commands); i++) {
8719 if (strncmp(tog_commands[i].name, argv[0],
8720 strlen(argv[0])) == 0) {
8721 cmd = &tog_commands[i];
8722 break;
8727 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8728 if (diff_algo_str) {
8729 if (strcasecmp(diff_algo_str, "patience") == 0)
8730 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8731 if (strcasecmp(diff_algo_str, "myers") == 0)
8732 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8735 if (cmd == NULL) {
8736 if (argc != 1)
8737 usage(0, 1);
8738 /* No command specified; try log with a path */
8739 error = tog_log_with_path(argc, argv);
8740 } else {
8741 if (hflag)
8742 cmd->cmd_usage();
8743 else
8744 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8747 endwin();
8748 putchar('\n');
8749 if (cmd_argv) {
8750 int i;
8751 for (i = 0; i < argc; i++)
8752 free(cmd_argv[i]);
8753 free(cmd_argv);
8756 if (error && error->code != GOT_ERR_CANCELLED)
8757 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8758 return 0;