Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 #endif
26 #include <curses.h>
27 #include <panel.h>
28 #include <locale.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #include <string.h>
35 #include <err.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
43 #include <sched.h>
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
52 #include "got_diff.h"
53 #include "got_opentemp.h"
54 #include "got_utf8.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_path.h"
60 #include "got_worktree.h"
62 #ifndef MIN
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #endif
66 #ifndef MAX
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #endif
70 #ifndef CTRL
71 #define CTRL(x) ((x) & 0x1f)
72 #endif
74 #ifndef nitems
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 #endif
78 struct tog_cmd {
79 const char *name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 };
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
103 };
105 enum tog_view_type {
106 TOG_VIEW_DIFF,
107 TOG_VIEW_LOG,
108 TOG_VIEW_BLAME,
109 TOG_VIEW_TREE,
110 TOG_VIEW_REF,
111 };
113 enum tog_view_mode {
114 TOG_VIEW_SPLIT_NONE,
115 TOG_VIEW_SPLIT_VERT,
116 TOG_VIEW_SPLIT_HRZN
117 };
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
127 int idx;
128 };
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
131 int ncommits;
132 struct commit_queue_head head;
133 };
135 struct tog_color {
136 STAILQ_ENTRY(tog_color) entry;
137 regex_t regex;
138 short colorpair;
139 };
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
158 *cmp = -1;
159 return NULL;
160 } else if (isbackup1 && !isbackup2) {
161 *cmp = 1;
162 return NULL;
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 return NULL;
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 repo);
177 if (err)
178 return err;
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 repo);
184 static void
185 tog_free_refs(void)
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
200 int regerr = 0;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
203 return NULL;
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
208 if (tc == NULL)
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 if (regerr) {
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
216 sizeof(regerr_msg));
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 regerr_msg);
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 free(tc);
221 return err;
223 tc->colorpair = idx;
224 STAILQ_INSERT_HEAD(colors, tc, entry);
225 return NULL;
228 static void
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
236 regfree(&tc->regex);
237 free(tc);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
248 return tc;
251 return NULL;
254 static int
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 return COLOR_CYAN;
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 return COLOR_YELLOW;
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 return COLOR_GREEN;
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 return COLOR_CYAN;
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 return COLOR_GREEN;
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 return COLOR_GREEN;
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 return COLOR_CYAN;
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 return COLOR_YELLOW;
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 return COLOR_GREEN;
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 return COLOR_YELLOW;
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 return COLOR_CYAN;
288 return -1;
291 static int
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
296 if (val == NULL)
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
300 return COLOR_BLACK;
301 if (strcasecmp(val, "red") == 0)
302 return COLOR_RED;
303 if (strcasecmp(val, "green") == 0)
304 return COLOR_GREEN;
305 if (strcasecmp(val, "yellow") == 0)
306 return COLOR_YELLOW;
307 if (strcasecmp(val, "blue") == 0)
308 return COLOR_BLUE;
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
312 return COLOR_CYAN;
313 if (strcasecmp(val, "white") == 0)
314 return COLOR_WHITE;
315 if (strcasecmp(val, "default") == 0)
316 return -1;
318 return default_color_value(envvar);
322 struct tog_diff_view_state {
323 struct got_object_id *id1, *id2;
324 const char *label1, *label2;
325 FILE *f, *f1, *f2;
326 int fd1, fd2;
327 int first_displayed_line;
328 int last_displayed_line;
329 int eof;
330 int diff_context;
331 int ignore_whitespace;
332 int force_text_diff;
333 struct got_repository *repo;
334 struct tog_colors colors;
335 size_t nlines;
336 off_t *line_offsets;
337 int matched_line;
338 int selected_line;
340 /* passed from log or blame view; may be NULL */
341 struct tog_view *parent_view;
342 };
344 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 static volatile sig_atomic_t tog_thread_error;
347 struct tog_log_thread_args {
348 pthread_cond_t need_commits;
349 pthread_cond_t commit_loaded;
350 int commits_needed;
351 int load_all;
352 struct got_commit_graph *graph;
353 struct commit_queue *commits;
354 const char *in_repo_path;
355 struct got_object_id *start_id;
356 struct got_repository *repo;
357 int *pack_fds;
358 int log_complete;
359 sig_atomic_t *quit;
360 struct commit_queue_entry **first_displayed_entry;
361 struct commit_queue_entry **selected_entry;
362 int *searching;
363 int *search_next_done;
364 regex_t *regex;
365 };
367 struct tog_log_view_state {
368 struct commit_queue commits;
369 struct commit_queue_entry *first_displayed_entry;
370 struct commit_queue_entry *last_displayed_entry;
371 struct commit_queue_entry *selected_entry;
372 int selected;
373 char *in_repo_path;
374 char *head_ref_name;
375 int log_branches;
376 struct got_repository *repo;
377 struct got_object_id *start_id;
378 sig_atomic_t quit;
379 pthread_t thread;
380 struct tog_log_thread_args thread_args;
381 struct commit_queue_entry *matched_entry;
382 struct commit_queue_entry *search_entry;
383 struct tog_colors colors;
384 int use_committer;
385 };
387 #define TOG_COLOR_DIFF_MINUS 1
388 #define TOG_COLOR_DIFF_PLUS 2
389 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 #define TOG_COLOR_DIFF_META 4
391 #define TOG_COLOR_TREE_SUBMODULE 5
392 #define TOG_COLOR_TREE_SYMLINK 6
393 #define TOG_COLOR_TREE_DIRECTORY 7
394 #define TOG_COLOR_TREE_EXECUTABLE 8
395 #define TOG_COLOR_COMMIT 9
396 #define TOG_COLOR_AUTHOR 10
397 #define TOG_COLOR_DATE 11
398 #define TOG_COLOR_REFS_HEADS 12
399 #define TOG_COLOR_REFS_TAGS 13
400 #define TOG_COLOR_REFS_REMOTES 14
401 #define TOG_COLOR_REFS_BACKUP 15
403 struct tog_blame_cb_args {
404 struct tog_blame_line *lines; /* one per line */
405 int nlines;
407 struct tog_view *view;
408 struct got_object_id *commit_id;
409 int *quit;
410 };
412 struct tog_blame_thread_args {
413 const char *path;
414 struct got_repository *repo;
415 struct tog_blame_cb_args *cb_args;
416 int *complete;
417 got_cancel_cb cancel_cb;
418 void *cancel_arg;
419 };
421 struct tog_blame {
422 FILE *f;
423 off_t filesize;
424 struct tog_blame_line *lines;
425 int nlines;
426 off_t *line_offsets;
427 pthread_t thread;
428 struct tog_blame_thread_args thread_args;
429 struct tog_blame_cb_args cb_args;
430 const char *path;
431 int *pack_fds;
432 };
434 struct tog_blame_view_state {
435 int first_displayed_line;
436 int last_displayed_line;
437 int selected_line;
438 int last_diffed_line;
439 int blame_complete;
440 int eof;
441 int done;
442 struct got_object_id_queue blamed_commits;
443 struct got_object_qid *blamed_commit;
444 char *path;
445 struct got_repository *repo;
446 struct got_object_id *commit_id;
447 struct tog_blame blame;
448 int matched_line;
449 struct tog_colors colors;
450 };
452 struct tog_parent_tree {
453 TAILQ_ENTRY(tog_parent_tree) entry;
454 struct got_tree_object *tree;
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *selected_entry;
457 int selected;
458 };
460 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 struct tog_tree_view_state {
463 char *tree_label;
464 struct got_object_id *commit_id;/* commit which this tree belongs to */
465 struct got_tree_object *root; /* the commit's root tree entry */
466 struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 struct got_tree_entry *first_displayed_entry;
468 struct got_tree_entry *last_displayed_entry;
469 struct got_tree_entry *selected_entry;
470 int ndisplayed, selected, show_ids;
471 struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 char *head_ref_name;
473 struct got_repository *repo;
474 struct got_tree_entry *matched_entry;
475 struct tog_colors colors;
476 };
478 struct tog_reflist_entry {
479 TAILQ_ENTRY(tog_reflist_entry) entry;
480 struct got_reference *ref;
481 int idx;
482 };
484 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 struct tog_ref_view_state {
487 struct tog_reflist_head refs;
488 struct tog_reflist_entry *first_displayed_entry;
489 struct tog_reflist_entry *last_displayed_entry;
490 struct tog_reflist_entry *selected_entry;
491 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 struct got_repository *repo;
493 struct tog_reflist_entry *matched_entry;
494 struct tog_colors colors;
495 };
497 /*
498 * We implement two types of views: parent views and child views.
500 * The 'Tab' key switches focus between a parent view and its child view.
501 * Child views are shown side-by-side to their parent view, provided
502 * there is enough screen estate.
504 * When a new view is opened from within a parent view, this new view
505 * becomes a child view of the parent view, replacing any existing child.
507 * When a new view is opened from within a child view, this new view
508 * becomes a parent view which will obscure the views below until the
509 * user quits the new parent view by typing 'q'.
511 * This list of views contains parent views only.
512 * Child views are only pointed to by their parent view.
513 */
514 TAILQ_HEAD(tog_view_list_head, tog_view);
516 struct tog_view {
517 TAILQ_ENTRY(tog_view) entry;
518 WINDOW *window;
519 PANEL *panel;
520 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 int resized_y, resized_x; /* begin_y/x based on user resizing */
522 int maxx, x; /* max column and current start column */
523 int lines, cols; /* copies of LINES and COLS */
524 int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 int ch, count; /* current keymap and count prefix */
526 int resized; /* set when in a resize event */
527 int focussed; /* Only set on one parent or child view at a time. */
528 int dying;
529 struct tog_view *parent;
530 struct tog_view *child;
532 /*
533 * This flag is initially set on parent views when a new child view
534 * is created. It gets toggled when the 'Tab' key switches focus
535 * between parent and child.
536 * The flag indicates whether focus should be passed on to our child
537 * view if this parent view gets picked for focus after another parent
538 * view was closed. This prevents child views from losing focus in such
539 * situations.
540 */
541 int focus_child;
543 enum tog_view_mode mode;
544 /* type-specific state */
545 enum tog_view_type type;
546 union {
547 struct tog_diff_view_state diff;
548 struct tog_log_view_state log;
549 struct tog_blame_view_state blame;
550 struct tog_tree_view_state tree;
551 struct tog_ref_view_state ref;
552 } state;
554 const struct got_error *(*show)(struct tog_view *);
555 const struct got_error *(*input)(struct tog_view **,
556 struct tog_view *, int);
557 const struct got_error *(*reset)(struct tog_view *);
558 const struct got_error *(*resize)(struct tog_view *, int);
559 const struct got_error *(*close)(struct tog_view *);
561 const struct got_error *(*search_start)(struct tog_view *);
562 const struct got_error *(*search_next)(struct tog_view *);
563 int search_started;
564 int searching;
565 #define TOG_SEARCH_FORWARD 1
566 #define TOG_SEARCH_BACKWARD 2
567 int search_next_done;
568 #define TOG_SEARCH_HAVE_MORE 1
569 #define TOG_SEARCH_NO_MORE 2
570 #define TOG_SEARCH_HAVE_NONE 3
571 regex_t regex;
572 regmatch_t regmatch;
573 };
575 static const struct got_error *open_diff_view(struct tog_view *,
576 struct got_object_id *, struct got_object_id *,
577 const char *, const char *, int, int, int, struct tog_view *,
578 struct got_repository *);
579 static const struct got_error *show_diff_view(struct tog_view *);
580 static const struct got_error *input_diff_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *reset_diff_view(struct tog_view *);
583 static const struct got_error* close_diff_view(struct tog_view *);
584 static const struct got_error *search_start_diff_view(struct tog_view *);
585 static const struct got_error *search_next_diff_view(struct tog_view *);
587 static const struct got_error *open_log_view(struct tog_view *,
588 struct got_object_id *, struct got_repository *,
589 const char *, const char *, int);
590 static const struct got_error * show_log_view(struct tog_view *);
591 static const struct got_error *input_log_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *resize_log_view(struct tog_view *, int);
594 static const struct got_error *close_log_view(struct tog_view *);
595 static const struct got_error *search_start_log_view(struct tog_view *);
596 static const struct got_error *search_next_log_view(struct tog_view *);
598 static const struct got_error *open_blame_view(struct tog_view *, char *,
599 struct got_object_id *, struct got_repository *);
600 static const struct got_error *show_blame_view(struct tog_view *);
601 static const struct got_error *input_blame_view(struct tog_view **,
602 struct tog_view *, int);
603 static const struct got_error *reset_blame_view(struct tog_view *);
604 static const struct got_error *close_blame_view(struct tog_view *);
605 static const struct got_error *search_start_blame_view(struct tog_view *);
606 static const struct got_error *search_next_blame_view(struct tog_view *);
608 static const struct got_error *open_tree_view(struct tog_view *,
609 struct got_object_id *, const char *, struct got_repository *);
610 static const struct got_error *show_tree_view(struct tog_view *);
611 static const struct got_error *input_tree_view(struct tog_view **,
612 struct tog_view *, int);
613 static const struct got_error *close_tree_view(struct tog_view *);
614 static const struct got_error *search_start_tree_view(struct tog_view *);
615 static const struct got_error *search_next_tree_view(struct tog_view *);
617 static const struct got_error *open_ref_view(struct tog_view *,
618 struct got_repository *);
619 static const struct got_error *show_ref_view(struct tog_view *);
620 static const struct got_error *input_ref_view(struct tog_view **,
621 struct tog_view *, int);
622 static const struct got_error *close_ref_view(struct tog_view *);
623 static const struct got_error *search_start_ref_view(struct tog_view *);
624 static const struct got_error *search_next_ref_view(struct tog_view *);
626 static volatile sig_atomic_t tog_sigwinch_received;
627 static volatile sig_atomic_t tog_sigpipe_received;
628 static volatile sig_atomic_t tog_sigcont_received;
629 static volatile sig_atomic_t tog_sigint_received;
630 static volatile sig_atomic_t tog_sigterm_received;
632 static void
633 tog_sigwinch(int signo)
635 tog_sigwinch_received = 1;
638 static void
639 tog_sigpipe(int signo)
641 tog_sigpipe_received = 1;
644 static void
645 tog_sigcont(int signo)
647 tog_sigcont_received = 1;
650 static void
651 tog_sigint(int signo)
653 tog_sigint_received = 1;
656 static void
657 tog_sigterm(int signo)
659 tog_sigterm_received = 1;
662 static int
663 tog_fatal_signal_received(void)
665 return (tog_sigpipe_received ||
666 tog_sigint_received || tog_sigint_received);
669 static const struct got_error *
670 view_close(struct tog_view *view)
672 const struct got_error *err = NULL, *child_err = NULL;
674 if (view->child) {
675 child_err = view_close(view->child);
676 view->child = NULL;
678 if (view->close)
679 err = view->close(view);
680 if (view->panel)
681 del_panel(view->panel);
682 if (view->window)
683 delwin(view->window);
684 free(view);
685 return err ? err : child_err;
688 static struct tog_view *
689 view_open(int nlines, int ncols, int begin_y, int begin_x,
690 enum tog_view_type type)
692 struct tog_view *view = calloc(1, sizeof(*view));
694 if (view == NULL)
695 return NULL;
697 view->type = type;
698 view->lines = LINES;
699 view->cols = COLS;
700 view->nlines = nlines ? nlines : LINES - begin_y;
701 view->ncols = ncols ? ncols : COLS - begin_x;
702 view->begin_y = begin_y;
703 view->begin_x = begin_x;
704 view->window = newwin(nlines, ncols, begin_y, begin_x);
705 if (view->window == NULL) {
706 view_close(view);
707 return NULL;
709 view->panel = new_panel(view->window);
710 if (view->panel == NULL ||
711 set_panel_userptr(view->panel, view) != OK) {
712 view_close(view);
713 return NULL;
716 keypad(view->window, TRUE);
717 return view;
720 static int
721 view_split_begin_x(int begin_x)
723 if (begin_x > 0 || COLS < 120)
724 return 0;
725 return (COLS - MAX(COLS / 2, 80));
728 /* XXX Stub till we decide what to do. */
729 static int
730 view_split_begin_y(int lines)
732 return lines * HSPLIT_SCALE;
735 static const struct got_error *view_resize(struct tog_view *);
737 static const struct got_error *
738 view_splitscreen(struct tog_view *view)
740 const struct got_error *err = NULL;
742 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 if (view->resized_y && view->resized_y < view->lines)
744 view->begin_y = view->resized_y;
745 else
746 view->begin_y = view_split_begin_y(view->nlines);
747 view->begin_x = 0;
748 } else if (!view->resized) {
749 if (view->resized_x && view->resized_x < view->cols - 1 &&
750 view->cols > 119)
751 view->begin_x = view->resized_x;
752 else
753 view->begin_x = view_split_begin_x(0);
754 view->begin_y = 0;
756 view->nlines = LINES - view->begin_y;
757 view->ncols = COLS - view->begin_x;
758 view->lines = LINES;
759 view->cols = COLS;
760 err = view_resize(view);
761 if (err)
762 return err;
764 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 view->parent->nlines = view->begin_y;
767 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 return got_error_from_errno("mvwin");
770 return NULL;
773 static const struct got_error *
774 view_fullscreen(struct tog_view *view)
776 const struct got_error *err = NULL;
778 view->begin_x = 0;
779 view->begin_y = view->resized ? view->begin_y : 0;
780 view->nlines = view->resized ? view->nlines : LINES;
781 view->ncols = COLS;
782 view->lines = LINES;
783 view->cols = COLS;
784 err = view_resize(view);
785 if (err)
786 return err;
788 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 return got_error_from_errno("mvwin");
791 return NULL;
794 static int
795 view_is_parent_view(struct tog_view *view)
797 return view->parent == NULL;
800 static int
801 view_is_splitscreen(struct tog_view *view)
803 return view->begin_x > 0 || view->begin_y > 0;
806 static int
807 view_is_fullscreen(struct tog_view *view)
809 return view->nlines == LINES && view->ncols == COLS;
812 static int
813 view_is_hsplit_top(struct tog_view *view)
815 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 view_is_splitscreen(view->child);
819 static void
820 view_border(struct tog_view *view)
822 PANEL *panel;
823 const struct tog_view *view_above;
825 if (view->parent)
826 return view_border(view->parent);
828 panel = panel_above(view->panel);
829 if (panel == NULL)
830 return;
832 view_above = panel_userptr(panel);
833 if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 mvwhline(view->window, view_above->begin_y - 1,
835 view->begin_x, got_locale_is_utf8() ?
836 ACS_HLINE : '-', view->ncols);
837 else
838 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
842 static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 static const struct got_error *request_log_commits(struct tog_view *);
844 static const struct got_error *offset_selection_down(struct tog_view *);
845 static void offset_selection_up(struct tog_view *);
846 static void view_get_split(struct tog_view *, int *, int *);
848 static const struct got_error *
849 view_resize(struct tog_view *view)
851 const struct got_error *err = NULL;
852 int dif, nlines, ncols;
854 dif = LINES - view->lines; /* line difference */
856 if (view->lines > LINES)
857 nlines = view->nlines - (view->lines - LINES);
858 else
859 nlines = view->nlines + (LINES - view->lines);
860 if (view->cols > COLS)
861 ncols = view->ncols - (view->cols - COLS);
862 else
863 ncols = view->ncols + (COLS - view->cols);
865 if (view->child) {
866 int hs = view->child->begin_y;
868 if (!view_is_fullscreen(view))
869 view->child->begin_x = view_split_begin_x(view->begin_x);
870 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 view->child->begin_x == 0) {
872 ncols = COLS;
874 view_fullscreen(view->child);
875 if (view->child->focussed)
876 show_panel(view->child->panel);
877 else
878 show_panel(view->panel);
879 } else {
880 ncols = view->child->begin_x;
882 view_splitscreen(view->child);
883 show_panel(view->child->panel);
885 /*
886 * XXX This is ugly and needs to be moved into the above
887 * logic but "works" for now and my attempts at moving it
888 * break either 'tab' or 'F' key maps in horizontal splits.
889 */
890 if (hs) {
891 err = view_splitscreen(view->child);
892 if (err)
893 return err;
894 if (dif < 0) { /* top split decreased */
895 err = offset_selection_down(view);
896 if (err)
897 return err;
899 view_border(view);
900 update_panels();
901 doupdate();
902 show_panel(view->child->panel);
903 nlines = view->nlines;
905 } else if (view->parent == NULL)
906 ncols = COLS;
908 if (view->resize && dif > 0) {
909 err = view->resize(view, dif);
910 if (err)
911 return err;
914 if (wresize(view->window, nlines, ncols) == ERR)
915 return got_error_from_errno("wresize");
916 if (replace_panel(view->panel, view->window) == ERR)
917 return got_error_from_errno("replace_panel");
918 wclear(view->window);
920 view->nlines = nlines;
921 view->ncols = ncols;
922 view->lines = LINES;
923 view->cols = COLS;
925 return NULL;
928 static const struct got_error *
929 resize_log_view(struct tog_view *view, int increase)
931 struct tog_log_view_state *s = &view->state.log;
932 const struct got_error *err = NULL;
933 int n = s->selected_entry->idx + view->lines - s->selected;
935 /*
936 * Request commits to account for the increased
937 * height so we have enough to populate the view.
938 */
939 if (s->commits.ncommits < n) {
940 view->nscrolled = n - s->commits.ncommits + increase + 1;
941 err = request_log_commits(view);
944 return err;
947 static void
948 view_adjust_offset(struct tog_view *view, int n)
950 if (n == 0)
951 return;
953 if (view->parent && view->parent->offset) {
954 if (view->parent->offset + n >= 0)
955 view->parent->offset += n;
956 else
957 view->parent->offset = 0;
958 } else if (view->offset) {
959 if (view->offset - n >= 0)
960 view->offset -= n;
961 else
962 view->offset = 0;
966 static const struct got_error *
967 view_resize_split(struct tog_view *view, int resize)
969 const struct got_error *err = NULL;
970 struct tog_view *v = NULL;
972 if (view->parent)
973 v = view->parent;
974 else
975 v = view;
977 if (!v->child || !view_is_splitscreen(v->child))
978 return NULL;
980 v->resized = v->child->resized = resize; /* lock for resize event */
982 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 int y = v->child->begin_y;
985 if (v->child->resized_y)
986 v->child->begin_y = v->child->resized_y;
987 if (view->parent)
988 v->child->begin_y -= resize;
989 else
990 v->child->begin_y += resize;
991 if (v->child->begin_y < 3) {
992 view->count = 0;
993 v->child->begin_y = 3;
994 } else if (v->child->begin_y > LINES - 1) {
995 view->count = 0;
996 v->child->begin_y = LINES - 1;
998 v->ncols = COLS;
999 v->child->ncols = COLS;
1000 view_adjust_offset(view, resize);
1001 err = view_init_hsplit(v, v->child->begin_y);
1002 if (err)
1003 return err;
1004 v->child->resized_y = v->child->begin_y;
1005 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 v->child->nscrolled = y - v->child->begin_y;
1007 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 v->nscrolled = v->child->begin_y - y;
1009 } else {
1010 if (v->child->resized_x)
1011 v->child->begin_x = v->child->resized_x;
1012 if (view->parent)
1013 v->child->begin_x -= resize;
1014 else
1015 v->child->begin_x += resize;
1016 if (v->child->begin_x < 11) {
1017 view->count = 0;
1018 v->child->begin_x = 11;
1019 } else if (v->child->begin_x > COLS - 1) {
1020 view->count = 0;
1021 v->child->begin_x = COLS - 1;
1023 v->child->resized_x = v->child->begin_x;
1026 v->child->mode = v->mode;
1027 v->child->nlines = v->lines - v->child->begin_y;
1028 v->child->ncols = v->cols - v->child->begin_x;
1029 v->focus_child = 1;
1031 err = view_fullscreen(v);
1032 if (err)
1033 return err;
1034 err = view_splitscreen(v->child);
1035 if (err)
1036 return err;
1038 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 err = offset_selection_down(v->child);
1040 if (err)
1041 return err;
1044 if (v->nscrolled)
1045 err = request_log_commits(v);
1046 else if (v->child->nscrolled)
1047 err = request_log_commits(v->child);
1049 v->resized = v->child->resized = 0;
1051 return err;
1054 static void
1055 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 struct tog_view *v = src->child ? src->child : src;
1059 dst->resized_x = v->resized_x;
1060 dst->resized_y = v->resized_y;
1063 static const struct got_error *
1064 view_close_child(struct tog_view *view)
1066 const struct got_error *err = NULL;
1068 if (view->child == NULL)
1069 return NULL;
1071 err = view_close(view->child);
1072 view->child = NULL;
1073 return err;
1076 static const struct got_error *
1077 view_set_child(struct tog_view *view, struct tog_view *child)
1079 const struct got_error *err = NULL;
1081 view->child = child;
1082 child->parent = view;
1084 err = view_resize(view);
1085 if (err)
1086 return err;
1088 if (view->child->resized_x || view->child->resized_y)
1089 err = view_resize_split(view, 0);
1091 return err;
1094 static void
1095 tog_resizeterm(void)
1097 int cols, lines;
1098 struct winsize size;
1100 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 cols = 80; /* Default */
1102 lines = 24;
1103 } else {
1104 cols = size.ws_col;
1105 lines = size.ws_row;
1107 resize_term(lines, cols);
1110 static const struct got_error *
1111 view_search_start(struct tog_view *view)
1113 const struct got_error *err = NULL;
1114 struct tog_view *v = view;
1115 char pattern[1024];
1116 int ret;
1118 if (view->search_started) {
1119 regfree(&view->regex);
1120 view->searching = 0;
1121 memset(&view->regmatch, 0, sizeof(view->regmatch));
1123 view->search_started = 0;
1125 if (view->nlines < 1)
1126 return NULL;
1128 if (view_is_hsplit_top(view))
1129 v = view->child;
1131 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 wclrtoeol(v->window);
1134 nodelay(view->window, FALSE); /* block for search term input */
1135 nocbreak();
1136 echo();
1137 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 wrefresh(v->window);
1139 cbreak();
1140 noecho();
1141 nodelay(view->window, TRUE);
1142 if (ret == ERR)
1143 return NULL;
1145 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 err = view->search_start(view);
1147 if (err) {
1148 regfree(&view->regex);
1149 return err;
1151 view->search_started = 1;
1152 view->searching = TOG_SEARCH_FORWARD;
1153 view->search_next_done = 0;
1154 view->search_next(view);
1157 return NULL;
1160 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 static const struct got_error *
1162 switch_split(struct tog_view *view)
1164 const struct got_error *err = NULL;
1165 struct tog_view *v = NULL;
1167 if (view->parent)
1168 v = view->parent;
1169 else
1170 v = view;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 v->mode = TOG_VIEW_SPLIT_VERT;
1174 else
1175 v->mode = TOG_VIEW_SPLIT_HRZN;
1177 if (!v->child)
1178 return NULL;
1179 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 v->mode = TOG_VIEW_SPLIT_NONE;
1182 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 v->child->begin_y = v->child->resized_y;
1185 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 v->child->begin_x = v->child->resized_x;
1189 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 v->ncols = COLS;
1191 v->child->ncols = COLS;
1192 v->child->nscrolled = LINES - v->child->nlines;
1194 err = view_init_hsplit(v, v->child->begin_y);
1195 if (err)
1196 return err;
1198 v->child->mode = v->mode;
1199 v->child->nlines = v->lines - v->child->begin_y;
1200 v->focus_child = 1;
1202 err = view_fullscreen(v);
1203 if (err)
1204 return err;
1205 err = view_splitscreen(v->child);
1206 if (err)
1207 return err;
1209 if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 v->mode = TOG_VIEW_SPLIT_VERT;
1211 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 err = offset_selection_down(v);
1213 err = offset_selection_down(v->child);
1214 } else {
1215 offset_selection_up(v);
1216 offset_selection_up(v->child);
1218 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 err = request_log_commits(v);
1220 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 err = request_log_commits(v->child);
1223 return err;
1227 * Compute view->count from numeric input. Assign total to view->count and
1228 * return first non-numeric key entered.
1230 static int
1231 get_compound_key(struct tog_view *view, int c)
1233 struct tog_view *v = view;
1234 int x, n = 0;
1236 if (view_is_hsplit_top(view))
1237 v = view->child;
1238 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1239 v = view->parent;
1241 view->count = 0;
1242 cbreak(); /* block for input */
1243 wmove(v->window, v->nlines - 1, 0);
1244 wclrtoeol(v->window);
1245 waddch(v->window, ':');
1247 do {
1248 x = getcurx(v->window);
1249 if (x != ERR && x < view->ncols) {
1250 waddch(v->window, c);
1251 wrefresh(v->window);
1255 * Don't overflow. Max valid request should be the greatest
1256 * between the longest and total lines; cap at 10 million.
1258 if (n >= 9999999)
1259 n = 9999999;
1260 else
1261 n = n * 10 + (c - '0');
1262 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1264 /* Massage excessive or inapplicable values at the input handler. */
1265 view->count = n;
1267 return c;
1270 static const struct got_error *
1271 view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 struct tog_view_list_head *views)
1274 const struct got_error *err = NULL;
1275 struct tog_view *v;
1276 int ch, errcode;
1278 *new = NULL;
1280 /* Clear "no matches" indicator. */
1281 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1284 view->count = 0;
1287 if (view->searching && !view->search_next_done) {
1288 errcode = pthread_mutex_unlock(&tog_mutex);
1289 if (errcode)
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_unlock");
1292 sched_yield();
1293 errcode = pthread_mutex_lock(&tog_mutex);
1294 if (errcode)
1295 return got_error_set_errno(errcode,
1296 "pthread_mutex_lock");
1297 view->search_next(view);
1298 return NULL;
1301 nodelay(view->window, FALSE);
1302 /* Allow threads to make progress while we are waiting for input. */
1303 errcode = pthread_mutex_unlock(&tog_mutex);
1304 if (errcode)
1305 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 /* If we have an unfinished count, let C-g or backspace abort. */
1307 if (view->count && --view->count) {
1308 cbreak();
1309 nodelay(view->window, TRUE);
1310 ch = wgetch(view->window);
1311 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1312 view->count = 0;
1313 else
1314 ch = view->ch;
1315 } else {
1316 ch = wgetch(view->window);
1317 if (ch >= '1' && ch <= '9')
1318 view->ch = ch = get_compound_key(view, ch);
1320 errcode = pthread_mutex_lock(&tog_mutex);
1321 if (errcode)
1322 return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 nodelay(view->window, TRUE);
1325 if (tog_sigwinch_received || tog_sigcont_received) {
1326 tog_resizeterm();
1327 tog_sigwinch_received = 0;
1328 tog_sigcont_received = 0;
1329 TAILQ_FOREACH(v, views, entry) {
1330 err = view_resize(v);
1331 if (err)
1332 return err;
1333 err = v->input(new, v, KEY_RESIZE);
1334 if (err)
1335 return err;
1336 if (v->child) {
1337 err = view_resize(v->child);
1338 if (err)
1339 return err;
1340 err = v->child->input(new, v->child,
1341 KEY_RESIZE);
1342 if (err)
1343 return err;
1344 if (v->child->resized_x || v->child->resized_y) {
1345 err = view_resize_split(v, 0);
1346 if (err)
1347 return err;
1353 switch (ch) {
1354 case '\t':
1355 view->count = 0;
1356 if (view->child) {
1357 view->focussed = 0;
1358 view->child->focussed = 1;
1359 view->focus_child = 1;
1360 } else if (view->parent) {
1361 view->focussed = 0;
1362 view->parent->focussed = 1;
1363 view->parent->focus_child = 0;
1364 if (!view_is_splitscreen(view)) {
1365 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 view->parent->type == TOG_VIEW_LOG) {
1367 err = request_log_commits(view->parent);
1368 if (err)
1369 return err;
1371 offset_selection_up(view->parent);
1372 err = view_fullscreen(view->parent);
1373 if (err)
1374 return err;
1377 break;
1378 case 'q':
1379 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 if (view->parent->type == TOG_VIEW_LOG) {
1381 /* might need more commits to fill fullscreen */
1382 err = request_log_commits(view->parent);
1383 if (err)
1384 break;
1386 offset_selection_up(view->parent);
1388 err = view->input(new, view, ch);
1389 view->dying = 1;
1390 break;
1391 case 'Q':
1392 *done = 1;
1393 break;
1394 case 'F':
1395 view->count = 0;
1396 if (view_is_parent_view(view)) {
1397 if (view->child == NULL)
1398 break;
1399 if (view_is_splitscreen(view->child)) {
1400 view->focussed = 0;
1401 view->child->focussed = 1;
1402 err = view_fullscreen(view->child);
1403 } else {
1404 err = view_splitscreen(view->child);
1405 if (!err)
1406 err = view_resize_split(view, 0);
1408 if (err)
1409 break;
1410 err = view->child->input(new, view->child,
1411 KEY_RESIZE);
1412 } else {
1413 if (view_is_splitscreen(view)) {
1414 view->parent->focussed = 0;
1415 view->focussed = 1;
1416 err = view_fullscreen(view);
1417 } else {
1418 err = view_splitscreen(view);
1419 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 err = view_resize(view->parent);
1421 if (!err)
1422 err = view_resize_split(view, 0);
1424 if (err)
1425 break;
1426 err = view->input(new, view, KEY_RESIZE);
1428 if (err)
1429 break;
1430 if (view->type == TOG_VIEW_LOG) {
1431 err = request_log_commits(view);
1432 if (err)
1433 break;
1435 if (view->parent)
1436 err = offset_selection_down(view->parent);
1437 if (!err)
1438 err = offset_selection_down(view);
1439 break;
1440 case 'S':
1441 view->count = 0;
1442 err = switch_split(view);
1443 break;
1444 case '-':
1445 err = view_resize_split(view, -1);
1446 break;
1447 case '+':
1448 err = view_resize_split(view, 1);
1449 break;
1450 case KEY_RESIZE:
1451 break;
1452 case '/':
1453 view->count = 0;
1454 if (view->search_start)
1455 view_search_start(view);
1456 else
1457 err = view->input(new, view, ch);
1458 break;
1459 case 'N':
1460 case 'n':
1461 if (view->search_started && view->search_next) {
1462 view->searching = (ch == 'n' ?
1463 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 view->search_next_done = 0;
1465 view->search_next(view);
1466 } else
1467 err = view->input(new, view, ch);
1468 break;
1469 case 'A':
1470 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1472 else
1473 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 TAILQ_FOREACH(v, views, entry) {
1475 if (v->reset) {
1476 err = v->reset(v);
1477 if (err)
1478 return err;
1480 if (v->child && v->child->reset) {
1481 err = v->child->reset(v->child);
1482 if (err)
1483 return err;
1486 break;
1487 default:
1488 err = view->input(new, view, ch);
1489 break;
1492 return err;
1495 static int
1496 view_needs_focus_indication(struct tog_view *view)
1498 if (view_is_parent_view(view)) {
1499 if (view->child == NULL || view->child->focussed)
1500 return 0;
1501 if (!view_is_splitscreen(view->child))
1502 return 0;
1503 } else if (!view_is_splitscreen(view))
1504 return 0;
1506 return view->focussed;
1509 static const struct got_error *
1510 view_loop(struct tog_view *view)
1512 const struct got_error *err = NULL;
1513 struct tog_view_list_head views;
1514 struct tog_view *new_view;
1515 char *mode;
1516 int fast_refresh = 10;
1517 int done = 0, errcode;
1519 mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 view->mode = TOG_VIEW_SPLIT_VERT;
1522 else
1523 view->mode = TOG_VIEW_SPLIT_HRZN;
1525 errcode = pthread_mutex_lock(&tog_mutex);
1526 if (errcode)
1527 return got_error_set_errno(errcode, "pthread_mutex_lock");
1529 TAILQ_INIT(&views);
1530 TAILQ_INSERT_HEAD(&views, view, entry);
1532 view->focussed = 1;
1533 err = view->show(view);
1534 if (err)
1535 return err;
1536 update_panels();
1537 doupdate();
1538 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 !tog_fatal_signal_received()) {
1540 /* Refresh fast during initialization, then become slower. */
1541 if (fast_refresh && fast_refresh-- == 0)
1542 halfdelay(10); /* switch to once per second */
1544 err = view_input(&new_view, &done, view, &views);
1545 if (err)
1546 break;
1547 if (view->dying) {
1548 struct tog_view *v, *prev = NULL;
1550 if (view_is_parent_view(view))
1551 prev = TAILQ_PREV(view, tog_view_list_head,
1552 entry);
1553 else if (view->parent)
1554 prev = view->parent;
1556 if (view->parent) {
1557 view->parent->child = NULL;
1558 view->parent->focus_child = 0;
1559 /* Restore fullscreen line height. */
1560 view->parent->nlines = view->parent->lines;
1561 err = view_resize(view->parent);
1562 if (err)
1563 break;
1564 /* Make resized splits persist. */
1565 view_transfer_size(view->parent, view);
1566 } else
1567 TAILQ_REMOVE(&views, view, entry);
1569 err = view_close(view);
1570 if (err)
1571 goto done;
1573 view = NULL;
1574 TAILQ_FOREACH(v, &views, entry) {
1575 if (v->focussed)
1576 break;
1578 if (view == NULL && new_view == NULL) {
1579 /* No view has focus. Try to pick one. */
1580 if (prev)
1581 view = prev;
1582 else if (!TAILQ_EMPTY(&views)) {
1583 view = TAILQ_LAST(&views,
1584 tog_view_list_head);
1586 if (view) {
1587 if (view->focus_child) {
1588 view->child->focussed = 1;
1589 view = view->child;
1590 } else
1591 view->focussed = 1;
1595 if (new_view) {
1596 struct tog_view *v, *t;
1597 /* Only allow one parent view per type. */
1598 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 if (v->type != new_view->type)
1600 continue;
1601 TAILQ_REMOVE(&views, v, entry);
1602 err = view_close(v);
1603 if (err)
1604 goto done;
1605 break;
1607 TAILQ_INSERT_TAIL(&views, new_view, entry);
1608 view = new_view;
1610 if (view) {
1611 if (view_is_parent_view(view)) {
1612 if (view->child && view->child->focussed)
1613 view = view->child;
1614 } else {
1615 if (view->parent && view->parent->focussed)
1616 view = view->parent;
1618 show_panel(view->panel);
1619 if (view->child && view_is_splitscreen(view->child))
1620 show_panel(view->child->panel);
1621 if (view->parent && view_is_splitscreen(view)) {
1622 err = view->parent->show(view->parent);
1623 if (err)
1624 goto done;
1626 err = view->show(view);
1627 if (err)
1628 goto done;
1629 if (view->child) {
1630 err = view->child->show(view->child);
1631 if (err)
1632 goto done;
1634 update_panels();
1635 doupdate();
1638 done:
1639 while (!TAILQ_EMPTY(&views)) {
1640 const struct got_error *close_err;
1641 view = TAILQ_FIRST(&views);
1642 TAILQ_REMOVE(&views, view, entry);
1643 close_err = view_close(view);
1644 if (close_err && err == NULL)
1645 err = close_err;
1648 errcode = pthread_mutex_unlock(&tog_mutex);
1649 if (errcode && err == NULL)
1650 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1652 return err;
1655 __dead static void
1656 usage_log(void)
1658 endwin();
1659 fprintf(stderr,
1660 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1661 getprogname());
1662 exit(1);
1665 /* Create newly allocated wide-character string equivalent to a byte string. */
1666 static const struct got_error *
1667 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1669 char *vis = NULL;
1670 const struct got_error *err = NULL;
1672 *ws = NULL;
1673 *wlen = mbstowcs(NULL, s, 0);
1674 if (*wlen == (size_t)-1) {
1675 int vislen;
1676 if (errno != EILSEQ)
1677 return got_error_from_errno("mbstowcs");
1679 /* byte string invalid in current encoding; try to "fix" it */
1680 err = got_mbsavis(&vis, &vislen, s);
1681 if (err)
1682 return err;
1683 *wlen = mbstowcs(NULL, vis, 0);
1684 if (*wlen == (size_t)-1) {
1685 err = got_error_from_errno("mbstowcs"); /* give up */
1686 goto done;
1690 *ws = calloc(*wlen + 1, sizeof(**ws));
1691 if (*ws == NULL) {
1692 err = got_error_from_errno("calloc");
1693 goto done;
1696 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 err = got_error_from_errno("mbstowcs");
1698 done:
1699 free(vis);
1700 if (err) {
1701 free(*ws);
1702 *ws = NULL;
1703 *wlen = 0;
1705 return err;
1708 static const struct got_error *
1709 expand_tab(char **ptr, const char *src)
1711 char *dst;
1712 size_t len, n, idx = 0, sz = 0;
1714 *ptr = NULL;
1715 n = len = strlen(src);
1716 dst = malloc(n + 1);
1717 if (dst == NULL)
1718 return got_error_from_errno("malloc");
1720 while (idx < len && src[idx]) {
1721 const char c = src[idx];
1723 if (c == '\t') {
1724 size_t nb = TABSIZE - sz % TABSIZE;
1725 char *p;
1727 p = realloc(dst, n + nb);
1728 if (p == NULL) {
1729 free(dst);
1730 return got_error_from_errno("realloc");
1733 dst = p;
1734 n += nb;
1735 memset(dst + sz, ' ', nb);
1736 sz += nb;
1737 } else
1738 dst[sz++] = src[idx];
1739 ++idx;
1742 dst[sz] = '\0';
1743 *ptr = dst;
1744 return NULL;
1748 * Advance at most n columns from wline starting at offset off.
1749 * Return the index to the first character after the span operation.
1750 * Return the combined column width of all spanned wide character in
1751 * *rcol.
1753 static int
1754 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1756 int width, i, cols = 0;
1758 if (n == 0) {
1759 *rcol = cols;
1760 return off;
1763 for (i = off; wline[i] != L'\0'; ++i) {
1764 if (wline[i] == L'\t')
1765 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1766 else
1767 width = wcwidth(wline[i]);
1769 if (width == -1) {
1770 width = 1;
1771 wline[i] = L'.';
1774 if (cols + width > n)
1775 break;
1776 cols += width;
1779 *rcol = cols;
1780 return i;
1784 * Format a line for display, ensuring that it won't overflow a width limit.
1785 * With scrolling, the width returned refers to the scrolled version of the
1786 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1788 static const struct got_error *
1789 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1792 const struct got_error *err = NULL;
1793 int cols;
1794 wchar_t *wline = NULL;
1795 char *exstr = NULL;
1796 size_t wlen;
1797 int i, scrollx;
1799 *wlinep = NULL;
1800 *widthp = 0;
1802 if (expand) {
1803 err = expand_tab(&exstr, line);
1804 if (err)
1805 return err;
1808 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1809 free(exstr);
1810 if (err)
1811 return err;
1813 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1815 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 wline[wlen - 1] = L'\0';
1817 wlen--;
1819 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 wline[wlen - 1] = L'\0';
1821 wlen--;
1824 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 wline[i] = L'\0';
1827 if (widthp)
1828 *widthp = cols;
1829 if (scrollxp)
1830 *scrollxp = scrollx;
1831 if (err)
1832 free(wline);
1833 else
1834 *wlinep = wline;
1835 return err;
1838 static const struct got_error*
1839 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 struct got_object_id *id, struct got_repository *repo)
1842 static const struct got_error *err = NULL;
1843 struct got_reflist_entry *re;
1844 char *s;
1845 const char *name;
1847 *refs_str = NULL;
1849 TAILQ_FOREACH(re, refs, entry) {
1850 struct got_tag_object *tag = NULL;
1851 struct got_object_id *ref_id;
1852 int cmp;
1854 name = got_ref_get_name(re->ref);
1855 if (strcmp(name, GOT_REF_HEAD) == 0)
1856 continue;
1857 if (strncmp(name, "refs/", 5) == 0)
1858 name += 5;
1859 if (strncmp(name, "got/", 4) == 0 &&
1860 strncmp(name, "got/backup/", 11) != 0)
1861 continue;
1862 if (strncmp(name, "heads/", 6) == 0)
1863 name += 6;
1864 if (strncmp(name, "remotes/", 8) == 0) {
1865 name += 8;
1866 s = strstr(name, "/" GOT_REF_HEAD);
1867 if (s != NULL && s[strlen(s)] == '\0')
1868 continue;
1870 err = got_ref_resolve(&ref_id, repo, re->ref);
1871 if (err)
1872 break;
1873 if (strncmp(name, "tags/", 5) == 0) {
1874 err = got_object_open_as_tag(&tag, repo, ref_id);
1875 if (err) {
1876 if (err->code != GOT_ERR_OBJ_TYPE) {
1877 free(ref_id);
1878 break;
1880 /* Ref points at something other than a tag. */
1881 err = NULL;
1882 tag = NULL;
1885 cmp = got_object_id_cmp(tag ?
1886 got_object_tag_get_object_id(tag) : ref_id, id);
1887 free(ref_id);
1888 if (tag)
1889 got_object_tag_close(tag);
1890 if (cmp != 0)
1891 continue;
1892 s = *refs_str;
1893 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 s ? ", " : "", name) == -1) {
1895 err = got_error_from_errno("asprintf");
1896 free(s);
1897 *refs_str = NULL;
1898 break;
1900 free(s);
1903 return err;
1906 static const struct got_error *
1907 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1908 int col_tab_align)
1910 char *smallerthan;
1912 smallerthan = strchr(author, '<');
1913 if (smallerthan && smallerthan[1] != '\0')
1914 author = smallerthan + 1;
1915 author[strcspn(author, "@>")] = '\0';
1916 return format_line(wauthor, author_width, NULL, author, 0, limit,
1917 col_tab_align, 0);
1920 static const struct got_error *
1921 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 struct got_object_id *id, const size_t date_display_cols,
1923 int author_display_cols)
1925 struct tog_log_view_state *s = &view->state.log;
1926 const struct got_error *err = NULL;
1927 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 char *logmsg0 = NULL, *logmsg = NULL;
1929 char *author = NULL;
1930 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 int author_width, logmsg_width;
1932 char *newline, *line = NULL;
1933 int col, limit, scrollx;
1934 const int avail = view->ncols;
1935 struct tm tm;
1936 time_t committer_time;
1937 struct tog_color *tc;
1939 committer_time = got_object_commit_get_committer_time(commit);
1940 if (gmtime_r(&committer_time, &tm) == NULL)
1941 return got_error_from_errno("gmtime_r");
1942 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 return got_error(GOT_ERR_NO_SPACE);
1945 if (avail <= date_display_cols)
1946 limit = MIN(sizeof(datebuf) - 1, avail);
1947 else
1948 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 tc = get_color(&s->colors, TOG_COLOR_DATE);
1950 if (tc)
1951 wattr_on(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 waddnstr(view->window, datebuf, limit);
1954 if (tc)
1955 wattr_off(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1957 col = limit;
1958 if (col > avail)
1959 goto done;
1961 if (avail >= 120) {
1962 char *id_str;
1963 err = got_object_id_str(&id_str, id);
1964 if (err)
1965 goto done;
1966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1967 if (tc)
1968 wattr_on(view->window,
1969 COLOR_PAIR(tc->colorpair), NULL);
1970 wprintw(view->window, "%.8s ", id_str);
1971 if (tc)
1972 wattr_off(view->window,
1973 COLOR_PAIR(tc->colorpair), NULL);
1974 free(id_str);
1975 col += 9;
1976 if (col > avail)
1977 goto done;
1980 if (s->use_committer)
1981 author = strdup(got_object_commit_get_committer(commit));
1982 else
1983 author = strdup(got_object_commit_get_author(commit));
1984 if (author == NULL) {
1985 err = got_error_from_errno("strdup");
1986 goto done;
1988 err = format_author(&wauthor, &author_width, author, avail - col, col);
1989 if (err)
1990 goto done;
1991 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1992 if (tc)
1993 wattr_on(view->window,
1994 COLOR_PAIR(tc->colorpair), NULL);
1995 waddwstr(view->window, wauthor);
1996 if (tc)
1997 wattr_off(view->window,
1998 COLOR_PAIR(tc->colorpair), NULL);
1999 col += author_width;
2000 while (col < avail && author_width < author_display_cols + 2) {
2001 waddch(view->window, ' ');
2002 col++;
2003 author_width++;
2005 if (col > avail)
2006 goto done;
2008 err = got_object_commit_get_logmsg(&logmsg0, commit);
2009 if (err)
2010 goto done;
2011 logmsg = logmsg0;
2012 while (*logmsg == '\n')
2013 logmsg++;
2014 newline = strchr(logmsg, '\n');
2015 if (newline)
2016 *newline = '\0';
2017 limit = avail - col;
2018 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 limit--; /* for the border */
2020 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2021 limit, col, 1);
2022 if (err)
2023 goto done;
2024 waddwstr(view->window, &wlogmsg[scrollx]);
2025 col += MAX(logmsg_width, 0);
2026 while (col < avail) {
2027 waddch(view->window, ' ');
2028 col++;
2030 done:
2031 free(logmsg0);
2032 free(wlogmsg);
2033 free(author);
2034 free(wauthor);
2035 free(line);
2036 return err;
2039 static struct commit_queue_entry *
2040 alloc_commit_queue_entry(struct got_commit_object *commit,
2041 struct got_object_id *id)
2043 struct commit_queue_entry *entry;
2045 entry = calloc(1, sizeof(*entry));
2046 if (entry == NULL)
2047 return NULL;
2049 entry->id = id;
2050 entry->commit = commit;
2051 return entry;
2054 static void
2055 pop_commit(struct commit_queue *commits)
2057 struct commit_queue_entry *entry;
2059 entry = TAILQ_FIRST(&commits->head);
2060 TAILQ_REMOVE(&commits->head, entry, entry);
2061 got_object_commit_close(entry->commit);
2062 commits->ncommits--;
2063 /* Don't free entry->id! It is owned by the commit graph. */
2064 free(entry);
2067 static void
2068 free_commits(struct commit_queue *commits)
2070 while (!TAILQ_EMPTY(&commits->head))
2071 pop_commit(commits);
2074 static const struct got_error *
2075 match_commit(int *have_match, struct got_object_id *id,
2076 struct got_commit_object *commit, regex_t *regex)
2078 const struct got_error *err = NULL;
2079 regmatch_t regmatch;
2080 char *id_str = NULL, *logmsg = NULL;
2082 *have_match = 0;
2084 err = got_object_id_str(&id_str, id);
2085 if (err)
2086 return err;
2088 err = got_object_commit_get_logmsg(&logmsg, commit);
2089 if (err)
2090 goto done;
2092 if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 &regmatch, 0) == 0 ||
2094 regexec(regex, got_object_commit_get_committer(commit), 1,
2095 &regmatch, 0) == 0 ||
2096 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2097 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2098 *have_match = 1;
2099 done:
2100 free(id_str);
2101 free(logmsg);
2102 return err;
2105 static const struct got_error *
2106 queue_commits(struct tog_log_thread_args *a)
2108 const struct got_error *err = NULL;
2111 * We keep all commits open throughout the lifetime of the log
2112 * view in order to avoid having to re-fetch commits from disk
2113 * while updating the display.
2115 do {
2116 struct got_object_id *id;
2117 struct got_commit_object *commit;
2118 struct commit_queue_entry *entry;
2119 int errcode;
2121 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2122 NULL, NULL);
2123 if (err || id == NULL)
2124 break;
2126 err = got_object_open_as_commit(&commit, a->repo, id);
2127 if (err)
2128 break;
2129 entry = alloc_commit_queue_entry(commit, id);
2130 if (entry == NULL) {
2131 err = got_error_from_errno("alloc_commit_queue_entry");
2132 break;
2135 errcode = pthread_mutex_lock(&tog_mutex);
2136 if (errcode) {
2137 err = got_error_set_errno(errcode,
2138 "pthread_mutex_lock");
2139 break;
2142 entry->idx = a->commits->ncommits;
2143 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 a->commits->ncommits++;
2146 if (*a->searching == TOG_SEARCH_FORWARD &&
2147 !*a->search_next_done) {
2148 int have_match;
2149 err = match_commit(&have_match, id, commit, a->regex);
2150 if (err)
2151 break;
2152 if (have_match)
2153 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2156 errcode = pthread_mutex_unlock(&tog_mutex);
2157 if (errcode && err == NULL)
2158 err = got_error_set_errno(errcode,
2159 "pthread_mutex_unlock");
2160 if (err)
2161 break;
2162 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2164 return err;
2167 static void
2168 select_commit(struct tog_log_view_state *s)
2170 struct commit_queue_entry *entry;
2171 int ncommits = 0;
2173 entry = s->first_displayed_entry;
2174 while (entry) {
2175 if (ncommits == s->selected) {
2176 s->selected_entry = entry;
2177 break;
2179 entry = TAILQ_NEXT(entry, entry);
2180 ncommits++;
2184 static const struct got_error *
2185 draw_commits(struct tog_view *view)
2187 const struct got_error *err = NULL;
2188 struct tog_log_view_state *s = &view->state.log;
2189 struct commit_queue_entry *entry = s->selected_entry;
2190 const int limit = view->nlines;
2191 int width;
2192 int ncommits, author_cols = 4;
2193 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 char *refs_str = NULL;
2195 wchar_t *wline;
2196 struct tog_color *tc;
2197 static const size_t date_display_cols = 12;
2199 if (s->selected_entry &&
2200 !(view->searching && view->search_next_done == 0)) {
2201 struct got_reflist_head *refs;
2202 err = got_object_id_str(&id_str, s->selected_entry->id);
2203 if (err)
2204 return err;
2205 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 s->selected_entry->id);
2207 if (refs) {
2208 err = build_refs_str(&refs_str, refs,
2209 s->selected_entry->id, s->repo);
2210 if (err)
2211 goto done;
2215 if (s->thread_args.commits_needed == 0)
2216 halfdelay(10); /* disable fast refresh */
2218 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 (view->searching && !view->search_next_done) ?
2222 "searching..." : "loading...") == -1) {
2223 err = got_error_from_errno("asprintf");
2224 goto done;
2226 } else {
2227 const char *search_str = NULL;
2229 if (view->searching) {
2230 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 search_str = "no more matches";
2232 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 search_str = "no matches found";
2234 else if (!view->search_next_done)
2235 search_str = "searching...";
2238 if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 search_str ? search_str :
2241 (refs_str ? refs_str : "")) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 goto done;
2247 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 "........................................",
2250 s->in_repo_path, ncommits_str) == -1) {
2251 err = got_error_from_errno("asprintf");
2252 header = NULL;
2253 goto done;
2255 } else if (asprintf(&header, "commit %s%s",
2256 id_str ? id_str : "........................................",
2257 ncommits_str) == -1) {
2258 err = got_error_from_errno("asprintf");
2259 header = NULL;
2260 goto done;
2262 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2263 if (err)
2264 goto done;
2266 werase(view->window);
2268 if (view_needs_focus_indication(view))
2269 wstandout(view->window);
2270 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2271 if (tc)
2272 wattr_on(view->window,
2273 COLOR_PAIR(tc->colorpair), NULL);
2274 waddwstr(view->window, wline);
2275 if (tc)
2276 wattr_off(view->window,
2277 COLOR_PAIR(tc->colorpair), NULL);
2278 while (width < view->ncols) {
2279 waddch(view->window, ' ');
2280 width++;
2282 if (view_needs_focus_indication(view))
2283 wstandend(view->window);
2284 free(wline);
2285 if (limit <= 1)
2286 goto done;
2288 /* Grow author column size if necessary, and set view->maxx. */
2289 entry = s->first_displayed_entry;
2290 ncommits = 0;
2291 view->maxx = 0;
2292 while (entry) {
2293 struct got_commit_object *c = entry->commit;
2294 char *author, *eol, *msg, *msg0;
2295 wchar_t *wauthor, *wmsg;
2296 int width;
2297 if (ncommits >= limit - 1)
2298 break;
2299 if (s->use_committer)
2300 author = strdup(got_object_commit_get_committer(c));
2301 else
2302 author = strdup(got_object_commit_get_author(c));
2303 if (author == NULL) {
2304 err = got_error_from_errno("strdup");
2305 goto done;
2307 err = format_author(&wauthor, &width, author, COLS,
2308 date_display_cols);
2309 if (author_cols < width)
2310 author_cols = width;
2311 free(wauthor);
2312 free(author);
2313 err = got_object_commit_get_logmsg(&msg0, c);
2314 if (err)
2315 goto done;
2316 msg = msg0;
2317 while (*msg == '\n')
2318 ++msg;
2319 if ((eol = strchr(msg, '\n')))
2320 *eol = '\0';
2321 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2322 date_display_cols + author_cols, 0);
2323 if (err)
2324 goto done;
2325 view->maxx = MAX(view->maxx, width);
2326 free(msg0);
2327 free(wmsg);
2328 ncommits++;
2329 entry = TAILQ_NEXT(entry, entry);
2332 entry = s->first_displayed_entry;
2333 s->last_displayed_entry = s->first_displayed_entry;
2334 ncommits = 0;
2335 while (entry) {
2336 if (ncommits >= limit - 1)
2337 break;
2338 if (ncommits == s->selected)
2339 wstandout(view->window);
2340 err = draw_commit(view, entry->commit, entry->id,
2341 date_display_cols, author_cols);
2342 if (ncommits == s->selected)
2343 wstandend(view->window);
2344 if (err)
2345 goto done;
2346 ncommits++;
2347 s->last_displayed_entry = entry;
2348 entry = TAILQ_NEXT(entry, entry);
2351 view_border(view);
2352 done:
2353 free(id_str);
2354 free(refs_str);
2355 free(ncommits_str);
2356 free(header);
2357 return err;
2360 static void
2361 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2363 struct commit_queue_entry *entry;
2364 int nscrolled = 0;
2366 entry = TAILQ_FIRST(&s->commits.head);
2367 if (s->first_displayed_entry == entry)
2368 return;
2370 entry = s->first_displayed_entry;
2371 while (entry && nscrolled < maxscroll) {
2372 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2373 if (entry) {
2374 s->first_displayed_entry = entry;
2375 nscrolled++;
2380 static const struct got_error *
2381 trigger_log_thread(struct tog_view *view, int wait)
2383 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2384 int errcode;
2386 halfdelay(1); /* fast refresh while loading commits */
2388 while (!ta->log_complete && !tog_thread_error &&
2389 (ta->commits_needed > 0 || ta->load_all)) {
2390 /* Wake the log thread. */
2391 errcode = pthread_cond_signal(&ta->need_commits);
2392 if (errcode)
2393 return got_error_set_errno(errcode,
2394 "pthread_cond_signal");
2397 * The mutex will be released while the view loop waits
2398 * in wgetch(), at which time the log thread will run.
2400 if (!wait)
2401 break;
2403 /* Display progress update in log view. */
2404 show_log_view(view);
2405 update_panels();
2406 doupdate();
2408 /* Wait right here while next commit is being loaded. */
2409 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2410 if (errcode)
2411 return got_error_set_errno(errcode,
2412 "pthread_cond_wait");
2414 /* Display progress update in log view. */
2415 show_log_view(view);
2416 update_panels();
2417 doupdate();
2420 return NULL;
2423 static const struct got_error *
2424 request_log_commits(struct tog_view *view)
2426 struct tog_log_view_state *state = &view->state.log;
2427 const struct got_error *err = NULL;
2429 if (state->thread_args.log_complete)
2430 return NULL;
2432 state->thread_args.commits_needed += view->nscrolled;
2433 err = trigger_log_thread(view, 1);
2434 view->nscrolled = 0;
2436 return err;
2439 static const struct got_error *
2440 log_scroll_down(struct tog_view *view, int maxscroll)
2442 struct tog_log_view_state *s = &view->state.log;
2443 const struct got_error *err = NULL;
2444 struct commit_queue_entry *pentry;
2445 int nscrolled = 0, ncommits_needed;
2447 if (s->last_displayed_entry == NULL)
2448 return NULL;
2450 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2451 if (s->commits.ncommits < ncommits_needed &&
2452 !s->thread_args.log_complete) {
2454 * Ask the log thread for required amount of commits.
2456 s->thread_args.commits_needed += maxscroll;
2457 err = trigger_log_thread(view, 1);
2458 if (err)
2459 return err;
2462 do {
2463 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2464 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2465 break;
2467 s->last_displayed_entry = pentry ?
2468 pentry : s->last_displayed_entry;;
2470 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2471 if (pentry == NULL)
2472 break;
2473 s->first_displayed_entry = pentry;
2474 } while (++nscrolled < maxscroll);
2476 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2477 view->nscrolled += nscrolled;
2478 else
2479 view->nscrolled = 0;
2481 return err;
2484 static const struct got_error *
2485 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2486 struct got_commit_object *commit, struct got_object_id *commit_id,
2487 struct tog_view *log_view, struct got_repository *repo)
2489 const struct got_error *err;
2490 struct got_object_qid *parent_id;
2491 struct tog_view *diff_view;
2493 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2494 if (diff_view == NULL)
2495 return got_error_from_errno("view_open");
2497 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2498 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2499 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2500 if (err == NULL)
2501 *new_view = diff_view;
2502 return err;
2505 static const struct got_error *
2506 tree_view_visit_subtree(struct tog_tree_view_state *s,
2507 struct got_tree_object *subtree)
2509 struct tog_parent_tree *parent;
2511 parent = calloc(1, sizeof(*parent));
2512 if (parent == NULL)
2513 return got_error_from_errno("calloc");
2515 parent->tree = s->tree;
2516 parent->first_displayed_entry = s->first_displayed_entry;
2517 parent->selected_entry = s->selected_entry;
2518 parent->selected = s->selected;
2519 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2520 s->tree = subtree;
2521 s->selected = 0;
2522 s->first_displayed_entry = NULL;
2523 return NULL;
2526 static const struct got_error *
2527 tree_view_walk_path(struct tog_tree_view_state *s,
2528 struct got_commit_object *commit, const char *path)
2530 const struct got_error *err = NULL;
2531 struct got_tree_object *tree = NULL;
2532 const char *p;
2533 char *slash, *subpath = NULL;
2535 /* Walk the path and open corresponding tree objects. */
2536 p = path;
2537 while (*p) {
2538 struct got_tree_entry *te;
2539 struct got_object_id *tree_id;
2540 char *te_name;
2542 while (p[0] == '/')
2543 p++;
2545 /* Ensure the correct subtree entry is selected. */
2546 slash = strchr(p, '/');
2547 if (slash == NULL)
2548 te_name = strdup(p);
2549 else
2550 te_name = strndup(p, slash - p);
2551 if (te_name == NULL) {
2552 err = got_error_from_errno("strndup");
2553 break;
2555 te = got_object_tree_find_entry(s->tree, te_name);
2556 if (te == NULL) {
2557 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2558 free(te_name);
2559 break;
2561 free(te_name);
2562 s->first_displayed_entry = s->selected_entry = te;
2564 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2565 break; /* jump to this file's entry */
2567 slash = strchr(p, '/');
2568 if (slash)
2569 subpath = strndup(path, slash - path);
2570 else
2571 subpath = strdup(path);
2572 if (subpath == NULL) {
2573 err = got_error_from_errno("strdup");
2574 break;
2577 err = got_object_id_by_path(&tree_id, s->repo, commit,
2578 subpath);
2579 if (err)
2580 break;
2582 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2583 free(tree_id);
2584 if (err)
2585 break;
2587 err = tree_view_visit_subtree(s, tree);
2588 if (err) {
2589 got_object_tree_close(tree);
2590 break;
2592 if (slash == NULL)
2593 break;
2594 free(subpath);
2595 subpath = NULL;
2596 p = slash;
2599 free(subpath);
2600 return err;
2603 static const struct got_error *
2604 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2605 struct commit_queue_entry *entry, const char *path,
2606 const char *head_ref_name, struct got_repository *repo)
2608 const struct got_error *err = NULL;
2609 struct tog_tree_view_state *s;
2610 struct tog_view *tree_view;
2612 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2613 if (tree_view == NULL)
2614 return got_error_from_errno("view_open");
2616 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2617 if (err)
2618 return err;
2619 s = &tree_view->state.tree;
2621 *new_view = tree_view;
2623 if (got_path_is_root_dir(path))
2624 return NULL;
2626 return tree_view_walk_path(s, entry->commit, path);
2629 static const struct got_error *
2630 block_signals_used_by_main_thread(void)
2632 sigset_t sigset;
2633 int errcode;
2635 if (sigemptyset(&sigset) == -1)
2636 return got_error_from_errno("sigemptyset");
2638 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2639 if (sigaddset(&sigset, SIGWINCH) == -1)
2640 return got_error_from_errno("sigaddset");
2641 if (sigaddset(&sigset, SIGCONT) == -1)
2642 return got_error_from_errno("sigaddset");
2643 if (sigaddset(&sigset, SIGINT) == -1)
2644 return got_error_from_errno("sigaddset");
2645 if (sigaddset(&sigset, SIGTERM) == -1)
2646 return got_error_from_errno("sigaddset");
2648 /* ncurses handles SIGTSTP */
2649 if (sigaddset(&sigset, SIGTSTP) == -1)
2650 return got_error_from_errno("sigaddset");
2652 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2653 if (errcode)
2654 return got_error_set_errno(errcode, "pthread_sigmask");
2656 return NULL;
2659 static void *
2660 log_thread(void *arg)
2662 const struct got_error *err = NULL;
2663 int errcode = 0;
2664 struct tog_log_thread_args *a = arg;
2665 int done = 0;
2668 * Sync startup with main thread such that we begin our
2669 * work once view_input() has released the mutex.
2671 errcode = pthread_mutex_lock(&tog_mutex);
2672 if (errcode) {
2673 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2674 return (void *)err;
2677 err = block_signals_used_by_main_thread();
2678 if (err) {
2679 pthread_mutex_unlock(&tog_mutex);
2680 goto done;
2683 while (!done && !err && !tog_fatal_signal_received()) {
2684 errcode = pthread_mutex_unlock(&tog_mutex);
2685 if (errcode) {
2686 err = got_error_set_errno(errcode,
2687 "pthread_mutex_unlock");
2688 goto done;
2690 err = queue_commits(a);
2691 if (err) {
2692 if (err->code != GOT_ERR_ITER_COMPLETED)
2693 goto done;
2694 err = NULL;
2695 done = 1;
2696 } else if (a->commits_needed > 0 && !a->load_all)
2697 a->commits_needed--;
2699 errcode = pthread_mutex_lock(&tog_mutex);
2700 if (errcode) {
2701 err = got_error_set_errno(errcode,
2702 "pthread_mutex_lock");
2703 goto done;
2704 } else if (*a->quit)
2705 done = 1;
2706 else if (*a->first_displayed_entry == NULL) {
2707 *a->first_displayed_entry =
2708 TAILQ_FIRST(&a->commits->head);
2709 *a->selected_entry = *a->first_displayed_entry;
2712 errcode = pthread_cond_signal(&a->commit_loaded);
2713 if (errcode) {
2714 err = got_error_set_errno(errcode,
2715 "pthread_cond_signal");
2716 pthread_mutex_unlock(&tog_mutex);
2717 goto done;
2720 if (done)
2721 a->commits_needed = 0;
2722 else {
2723 if (a->commits_needed == 0 && !a->load_all) {
2724 errcode = pthread_cond_wait(&a->need_commits,
2725 &tog_mutex);
2726 if (errcode) {
2727 err = got_error_set_errno(errcode,
2728 "pthread_cond_wait");
2729 pthread_mutex_unlock(&tog_mutex);
2730 goto done;
2732 if (*a->quit)
2733 done = 1;
2737 a->log_complete = 1;
2738 errcode = pthread_mutex_unlock(&tog_mutex);
2739 if (errcode)
2740 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2741 done:
2742 if (err) {
2743 tog_thread_error = 1;
2744 pthread_cond_signal(&a->commit_loaded);
2746 return (void *)err;
2749 static const struct got_error *
2750 stop_log_thread(struct tog_log_view_state *s)
2752 const struct got_error *err = NULL, *thread_err = NULL;
2753 int errcode;
2755 if (s->thread) {
2756 s->quit = 1;
2757 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2758 if (errcode)
2759 return got_error_set_errno(errcode,
2760 "pthread_cond_signal");
2761 errcode = pthread_mutex_unlock(&tog_mutex);
2762 if (errcode)
2763 return got_error_set_errno(errcode,
2764 "pthread_mutex_unlock");
2765 errcode = pthread_join(s->thread, (void **)&thread_err);
2766 if (errcode)
2767 return got_error_set_errno(errcode, "pthread_join");
2768 errcode = pthread_mutex_lock(&tog_mutex);
2769 if (errcode)
2770 return got_error_set_errno(errcode,
2771 "pthread_mutex_lock");
2772 s->thread = 0; //NULL;
2775 if (s->thread_args.repo) {
2776 err = got_repo_close(s->thread_args.repo);
2777 s->thread_args.repo = NULL;
2780 if (s->thread_args.pack_fds) {
2781 const struct got_error *pack_err =
2782 got_repo_pack_fds_close(s->thread_args.pack_fds);
2783 if (err == NULL)
2784 err = pack_err;
2785 s->thread_args.pack_fds = NULL;
2788 if (s->thread_args.graph) {
2789 got_commit_graph_close(s->thread_args.graph);
2790 s->thread_args.graph = NULL;
2793 return err ? err : thread_err;
2796 static const struct got_error *
2797 close_log_view(struct tog_view *view)
2799 const struct got_error *err = NULL;
2800 struct tog_log_view_state *s = &view->state.log;
2801 int errcode;
2803 err = stop_log_thread(s);
2805 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2806 if (errcode && err == NULL)
2807 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2809 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2810 if (errcode && err == NULL)
2811 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2813 free_commits(&s->commits);
2814 free(s->in_repo_path);
2815 s->in_repo_path = NULL;
2816 free(s->start_id);
2817 s->start_id = NULL;
2818 free(s->head_ref_name);
2819 s->head_ref_name = NULL;
2820 return err;
2823 static const struct got_error *
2824 search_start_log_view(struct tog_view *view)
2826 struct tog_log_view_state *s = &view->state.log;
2828 s->matched_entry = NULL;
2829 s->search_entry = NULL;
2830 return NULL;
2833 static const struct got_error *
2834 search_next_log_view(struct tog_view *view)
2836 const struct got_error *err = NULL;
2837 struct tog_log_view_state *s = &view->state.log;
2838 struct commit_queue_entry *entry;
2840 /* Display progress update in log view. */
2841 show_log_view(view);
2842 update_panels();
2843 doupdate();
2845 if (s->search_entry) {
2846 int errcode, ch;
2847 errcode = pthread_mutex_unlock(&tog_mutex);
2848 if (errcode)
2849 return got_error_set_errno(errcode,
2850 "pthread_mutex_unlock");
2851 ch = wgetch(view->window);
2852 errcode = pthread_mutex_lock(&tog_mutex);
2853 if (errcode)
2854 return got_error_set_errno(errcode,
2855 "pthread_mutex_lock");
2856 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2857 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2858 return NULL;
2860 if (view->searching == TOG_SEARCH_FORWARD)
2861 entry = TAILQ_NEXT(s->search_entry, entry);
2862 else
2863 entry = TAILQ_PREV(s->search_entry,
2864 commit_queue_head, entry);
2865 } else if (s->matched_entry) {
2866 int matched_idx = s->matched_entry->idx;
2867 int selected_idx = s->selected_entry->idx;
2870 * If the user has moved the cursor after we hit a match,
2871 * the position from where we should continue searching
2872 * might have changed.
2874 if (view->searching == TOG_SEARCH_FORWARD) {
2875 if (matched_idx > selected_idx)
2876 entry = TAILQ_NEXT(s->selected_entry, entry);
2877 else
2878 entry = TAILQ_NEXT(s->matched_entry, entry);
2879 } else {
2880 if (matched_idx < selected_idx)
2881 entry = TAILQ_PREV(s->selected_entry,
2882 commit_queue_head, entry);
2883 else
2884 entry = TAILQ_PREV(s->matched_entry,
2885 commit_queue_head, entry);
2887 } else {
2888 entry = s->selected_entry;
2891 while (1) {
2892 int have_match = 0;
2894 if (entry == NULL) {
2895 if (s->thread_args.log_complete ||
2896 view->searching == TOG_SEARCH_BACKWARD) {
2897 view->search_next_done =
2898 (s->matched_entry == NULL ?
2899 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2900 s->search_entry = NULL;
2901 return NULL;
2904 * Poke the log thread for more commits and return,
2905 * allowing the main loop to make progress. Search
2906 * will resume at s->search_entry once we come back.
2908 s->thread_args.commits_needed++;
2909 return trigger_log_thread(view, 0);
2912 err = match_commit(&have_match, entry->id, entry->commit,
2913 &view->regex);
2914 if (err)
2915 break;
2916 if (have_match) {
2917 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2918 s->matched_entry = entry;
2919 break;
2922 s->search_entry = entry;
2923 if (view->searching == TOG_SEARCH_FORWARD)
2924 entry = TAILQ_NEXT(entry, entry);
2925 else
2926 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2929 if (s->matched_entry) {
2930 int cur = s->selected_entry->idx;
2931 while (cur < s->matched_entry->idx) {
2932 err = input_log_view(NULL, view, KEY_DOWN);
2933 if (err)
2934 return err;
2935 cur++;
2937 while (cur > s->matched_entry->idx) {
2938 err = input_log_view(NULL, view, KEY_UP);
2939 if (err)
2940 return err;
2941 cur--;
2945 s->search_entry = NULL;
2947 return NULL;
2950 static const struct got_error *
2951 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2952 struct got_repository *repo, const char *head_ref_name,
2953 const char *in_repo_path, int log_branches)
2955 const struct got_error *err = NULL;
2956 struct tog_log_view_state *s = &view->state.log;
2957 struct got_repository *thread_repo = NULL;
2958 struct got_commit_graph *thread_graph = NULL;
2959 int errcode;
2961 if (in_repo_path != s->in_repo_path) {
2962 free(s->in_repo_path);
2963 s->in_repo_path = strdup(in_repo_path);
2964 if (s->in_repo_path == NULL)
2965 return got_error_from_errno("strdup");
2968 /* The commit queue only contains commits being displayed. */
2969 TAILQ_INIT(&s->commits.head);
2970 s->commits.ncommits = 0;
2972 s->repo = repo;
2973 if (head_ref_name) {
2974 s->head_ref_name = strdup(head_ref_name);
2975 if (s->head_ref_name == NULL) {
2976 err = got_error_from_errno("strdup");
2977 goto done;
2980 s->start_id = got_object_id_dup(start_id);
2981 if (s->start_id == NULL) {
2982 err = got_error_from_errno("got_object_id_dup");
2983 goto done;
2985 s->log_branches = log_branches;
2987 STAILQ_INIT(&s->colors);
2988 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2989 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2990 get_color_value("TOG_COLOR_COMMIT"));
2991 if (err)
2992 goto done;
2993 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2994 get_color_value("TOG_COLOR_AUTHOR"));
2995 if (err) {
2996 free_colors(&s->colors);
2997 goto done;
2999 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3000 get_color_value("TOG_COLOR_DATE"));
3001 if (err) {
3002 free_colors(&s->colors);
3003 goto done;
3007 view->show = show_log_view;
3008 view->input = input_log_view;
3009 view->resize = resize_log_view;
3010 view->close = close_log_view;
3011 view->search_start = search_start_log_view;
3012 view->search_next = search_next_log_view;
3014 if (s->thread_args.pack_fds == NULL) {
3015 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3016 if (err)
3017 goto done;
3019 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3020 s->thread_args.pack_fds);
3021 if (err)
3022 goto done;
3023 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3024 !s->log_branches);
3025 if (err)
3026 goto done;
3027 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3028 s->repo, NULL, NULL);
3029 if (err)
3030 goto done;
3032 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3033 if (errcode) {
3034 err = got_error_set_errno(errcode, "pthread_cond_init");
3035 goto done;
3037 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3038 if (errcode) {
3039 err = got_error_set_errno(errcode, "pthread_cond_init");
3040 goto done;
3043 s->thread_args.commits_needed = view->nlines;
3044 s->thread_args.graph = thread_graph;
3045 s->thread_args.commits = &s->commits;
3046 s->thread_args.in_repo_path = s->in_repo_path;
3047 s->thread_args.start_id = s->start_id;
3048 s->thread_args.repo = thread_repo;
3049 s->thread_args.log_complete = 0;
3050 s->thread_args.quit = &s->quit;
3051 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3052 s->thread_args.selected_entry = &s->selected_entry;
3053 s->thread_args.searching = &view->searching;
3054 s->thread_args.search_next_done = &view->search_next_done;
3055 s->thread_args.regex = &view->regex;
3056 done:
3057 if (err)
3058 close_log_view(view);
3059 return err;
3062 static const struct got_error *
3063 show_log_view(struct tog_view *view)
3065 const struct got_error *err;
3066 struct tog_log_view_state *s = &view->state.log;
3068 if (s->thread == 0) { //NULL) {
3069 int errcode = pthread_create(&s->thread, NULL, log_thread,
3070 &s->thread_args);
3071 if (errcode)
3072 return got_error_set_errno(errcode, "pthread_create");
3073 if (s->thread_args.commits_needed > 0) {
3074 err = trigger_log_thread(view, 1);
3075 if (err)
3076 return err;
3080 return draw_commits(view);
3083 static void
3084 log_move_cursor_up(struct tog_view *view, int page, int home)
3086 struct tog_log_view_state *s = &view->state.log;
3088 if (s->selected_entry->idx == 0)
3089 view->count = 0;
3090 if (s->first_displayed_entry == NULL)
3091 return;
3093 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3094 || home)
3095 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3097 if (!page && !home && s->selected > 0)
3098 --s->selected;
3099 else
3100 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3102 select_commit(s);
3103 return;
3106 static const struct got_error *
3107 log_move_cursor_down(struct tog_view *view, int page)
3109 struct tog_log_view_state *s = &view->state.log;
3110 struct commit_queue_entry *first;
3111 const struct got_error *err = NULL;
3113 first = s->first_displayed_entry;
3114 if (first == NULL) {
3115 view->count = 0;
3116 return NULL;
3119 if (s->thread_args.log_complete &&
3120 s->selected_entry->idx >= s->commits.ncommits - 1)
3121 return NULL;
3123 if (!page) {
3124 int eos = view->nlines - 2;
3126 if (view_is_hsplit_top(view))
3127 --eos; /* border consumes the last line */
3128 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3129 ++s->selected;
3130 else
3131 err = log_scroll_down(view, 1);
3132 } else if (s->thread_args.load_all) {
3133 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3134 s->selected += MIN(s->last_displayed_entry->idx -
3135 s->selected_entry->idx, page + 1);
3136 else
3137 err = log_scroll_down(view, MIN(page,
3138 s->commits.ncommits - s->selected_entry->idx - 1));
3139 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3140 } else {
3141 err = log_scroll_down(view, page);
3142 if (err)
3143 return err;
3144 if (first == s->first_displayed_entry && s->selected <
3145 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3146 s->selected = MIN(s->commits.ncommits - 1, page);
3149 if (err)
3150 return err;
3153 * We might necessarily overshoot in horizontal
3154 * splits; if so, select the last displayed commit.
3156 s->selected = MIN(s->selected,
3157 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3159 select_commit(s);
3161 if (s->thread_args.log_complete &&
3162 s->selected_entry->idx == s->commits.ncommits - 1)
3163 view->count = 0;
3165 return NULL;
3168 static void
3169 view_get_split(struct tog_view *view, int *y, int *x)
3171 *x = 0;
3172 *y = 0;
3174 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3175 if (view->child && view->child->resized_y)
3176 *y = view->child->resized_y;
3177 else if (view->resized_y)
3178 *y = view->resized_y;
3179 else
3180 *y = view_split_begin_y(view->lines);
3181 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3182 if (view->child && view->child->resized_x)
3183 *x = view->child->resized_x;
3184 else if (view->resized_x)
3185 *x = view->resized_x;
3186 else
3187 *x = view_split_begin_x(view->begin_x);
3191 /* Split view horizontally at y and offset view->state->selected line. */
3192 static const struct got_error *
3193 view_init_hsplit(struct tog_view *view, int y)
3195 const struct got_error *err = NULL;
3197 view->nlines = y;
3198 view->ncols = COLS;
3199 err = view_resize(view);
3200 if (err)
3201 return err;
3203 err = offset_selection_down(view);
3205 return err;
3208 static const struct got_error *
3209 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3211 const struct got_error *err = NULL;
3212 struct tog_log_view_state *s = &view->state.log;
3213 struct tog_view *diff_view = NULL, *tree_view = NULL;
3214 struct tog_view *ref_view = NULL;
3215 struct commit_queue_entry *entry;
3216 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3218 if (s->thread_args.load_all) {
3219 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3220 s->thread_args.load_all = 0;
3221 else if (s->thread_args.log_complete) {
3222 err = log_move_cursor_down(view, s->commits.ncommits);
3223 s->thread_args.load_all = 0;
3225 return err;
3228 eos = nscroll = view->nlines - 1;
3229 if (view_is_hsplit_top(view))
3230 --eos; /* border */
3232 switch (ch) {
3233 case 'q':
3234 s->quit = 1;
3235 break;
3236 case '0':
3237 view->x = 0;
3238 break;
3239 case '$':
3240 view->x = MAX(view->maxx - view->ncols / 2, 0);
3241 view->count = 0;
3242 break;
3243 case KEY_RIGHT:
3244 case 'l':
3245 if (view->x + view->ncols / 2 < view->maxx)
3246 view->x += 2; /* move two columns right */
3247 else
3248 view->count = 0;
3249 break;
3250 case KEY_LEFT:
3251 case 'h':
3252 view->x -= MIN(view->x, 2); /* move two columns back */
3253 if (view->x <= 0)
3254 view->count = 0;
3255 break;
3256 case 'k':
3257 case KEY_UP:
3258 case '<':
3259 case ',':
3260 case CTRL('p'):
3261 log_move_cursor_up(view, 0, 0);
3262 break;
3263 case 'g':
3264 case KEY_HOME:
3265 log_move_cursor_up(view, 0, 1);
3266 view->count = 0;
3267 break;
3268 case CTRL('u'):
3269 case 'u':
3270 nscroll /= 2;
3271 /* FALL THROUGH */
3272 case KEY_PPAGE:
3273 case CTRL('b'):
3274 case 'b':
3275 log_move_cursor_up(view, nscroll, 0);
3276 break;
3277 case 'j':
3278 case KEY_DOWN:
3279 case '>':
3280 case '.':
3281 case CTRL('n'):
3282 err = log_move_cursor_down(view, 0);
3283 break;
3284 case '@':
3285 s->use_committer = !s->use_committer;
3286 break;
3287 case 'G':
3288 case KEY_END: {
3289 /* We don't know yet how many commits, so we're forced to
3290 * traverse them all. */
3291 view->count = 0;
3292 if (!s->thread_args.log_complete) {
3293 s->thread_args.load_all = 1;
3294 return trigger_log_thread(view, 0);
3297 s->selected = 0;
3298 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3299 for (n = 0; n < eos; n++) {
3300 if (entry == NULL)
3301 break;
3302 s->first_displayed_entry = entry;
3303 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3305 if (n > 0)
3306 s->selected = n - 1;
3307 select_commit(s);
3308 break;
3310 case CTRL('d'):
3311 case 'd':
3312 nscroll /= 2;
3313 /* FALL THROUGH */
3314 case KEY_NPAGE:
3315 case CTRL('f'):
3316 case 'f':
3317 case ' ':
3318 err = log_move_cursor_down(view, nscroll);
3319 break;
3320 case KEY_RESIZE:
3321 if (s->selected > view->nlines - 2)
3322 s->selected = view->nlines - 2;
3323 if (s->selected > s->commits.ncommits - 1)
3324 s->selected = s->commits.ncommits - 1;
3325 select_commit(s);
3326 if (s->commits.ncommits < view->nlines - 1 &&
3327 !s->thread_args.log_complete) {
3328 s->thread_args.commits_needed += (view->nlines - 1) -
3329 s->commits.ncommits;
3330 err = trigger_log_thread(view, 1);
3332 break;
3333 case KEY_ENTER:
3334 case '\r':
3335 view->count = 0;
3336 if (s->selected_entry == NULL)
3337 break;
3339 /* get dimensions--don't split till initialisation succeeds */
3340 if (view_is_parent_view(view))
3341 view_get_split(view, &begin_y, &begin_x);
3343 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3344 s->selected_entry->commit, s->selected_entry->id,
3345 view, s->repo);
3346 if (err)
3347 break;
3349 if (view_is_parent_view(view) &&
3350 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3351 err = view_init_hsplit(view, begin_y);
3352 if (err)
3353 break;
3356 view->focussed = 0;
3357 diff_view->focussed = 1;
3358 diff_view->mode = view->mode;
3359 diff_view->nlines = view->lines - begin_y;
3361 if (view_is_parent_view(view)) {
3362 view_transfer_size(diff_view, view);
3363 err = view_close_child(view);
3364 if (err)
3365 return err;
3366 err = view_set_child(view, diff_view);
3367 if (err)
3368 return err;
3369 view->focus_child = 1;
3370 } else
3371 *new_view = diff_view;
3372 break;
3373 case 't':
3374 view->count = 0;
3375 if (s->selected_entry == NULL)
3376 break;
3377 if (view_is_parent_view(view))
3378 view_get_split(view, &begin_y, &begin_x);
3379 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3380 s->selected_entry, s->in_repo_path, s->head_ref_name,
3381 s->repo);
3382 if (err)
3383 break;
3384 if (view_is_parent_view(view) &&
3385 view->mode == TOG_VIEW_SPLIT_HRZN) {
3386 err = view_init_hsplit(view, begin_y);
3387 if (err)
3388 break;
3390 view->focussed = 0;
3391 tree_view->focussed = 1;
3392 tree_view->mode = view->mode;
3393 tree_view->nlines = view->lines - begin_y;
3394 if (view_is_parent_view(view)) {
3395 view_transfer_size(tree_view, view);
3396 err = view_close_child(view);
3397 if (err)
3398 return err;
3399 err = view_set_child(view, tree_view);
3400 if (err)
3401 return err;
3402 view->focus_child = 1;
3403 } else
3404 *new_view = tree_view;
3405 break;
3406 case KEY_BACKSPACE:
3407 case CTRL('l'):
3408 case 'B':
3409 view->count = 0;
3410 if (ch == KEY_BACKSPACE &&
3411 got_path_is_root_dir(s->in_repo_path))
3412 break;
3413 err = stop_log_thread(s);
3414 if (err)
3415 return err;
3416 if (ch == KEY_BACKSPACE) {
3417 char *parent_path;
3418 err = got_path_dirname(&parent_path, s->in_repo_path);
3419 if (err)
3420 return err;
3421 free(s->in_repo_path);
3422 s->in_repo_path = parent_path;
3423 s->thread_args.in_repo_path = s->in_repo_path;
3424 } else if (ch == CTRL('l')) {
3425 struct got_object_id *start_id;
3426 err = got_repo_match_object_id(&start_id, NULL,
3427 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3428 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3429 if (err)
3430 return err;
3431 free(s->start_id);
3432 s->start_id = start_id;
3433 s->thread_args.start_id = s->start_id;
3434 } else /* 'B' */
3435 s->log_branches = !s->log_branches;
3437 if (s->thread_args.pack_fds == NULL) {
3438 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3439 if (err)
3440 return err;
3442 err = got_repo_open(&s->thread_args.repo,
3443 got_repo_get_path(s->repo), NULL,
3444 s->thread_args.pack_fds);
3445 if (err)
3446 return err;
3447 tog_free_refs();
3448 err = tog_load_refs(s->repo, 0);
3449 if (err)
3450 return err;
3451 err = got_commit_graph_open(&s->thread_args.graph,
3452 s->in_repo_path, !s->log_branches);
3453 if (err)
3454 return err;
3455 err = got_commit_graph_iter_start(s->thread_args.graph,
3456 s->start_id, s->repo, NULL, NULL);
3457 if (err)
3458 return err;
3459 free_commits(&s->commits);
3460 s->first_displayed_entry = NULL;
3461 s->last_displayed_entry = NULL;
3462 s->selected_entry = NULL;
3463 s->selected = 0;
3464 s->thread_args.log_complete = 0;
3465 s->quit = 0;
3466 s->thread_args.commits_needed = view->lines;
3467 s->matched_entry = NULL;
3468 s->search_entry = NULL;
3469 break;
3470 case 'r':
3471 view->count = 0;
3472 if (view_is_parent_view(view))
3473 view_get_split(view, &begin_y, &begin_x);
3474 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3475 if (ref_view == NULL)
3476 return got_error_from_errno("view_open");
3477 err = open_ref_view(ref_view, s->repo);
3478 if (err) {
3479 view_close(ref_view);
3480 return err;
3482 if (view_is_parent_view(view) &&
3483 view->mode == TOG_VIEW_SPLIT_HRZN) {
3484 err = view_init_hsplit(view, begin_y);
3485 if (err)
3486 break;
3488 view->focussed = 0;
3489 ref_view->focussed = 1;
3490 ref_view->mode = view->mode;
3491 ref_view->nlines = view->lines - begin_y;
3492 if (view_is_parent_view(view)) {
3493 view_transfer_size(ref_view, view);
3494 err = view_close_child(view);
3495 if (err)
3496 return err;
3497 err = view_set_child(view, ref_view);
3498 if (err)
3499 return err;
3500 view->focus_child = 1;
3501 } else
3502 *new_view = ref_view;
3503 break;
3504 default:
3505 view->count = 0;
3506 break;
3509 return err;
3512 static const struct got_error *
3513 apply_unveil(const char *repo_path, const char *worktree_path)
3515 const struct got_error *error;
3517 #ifdef PROFILE
3518 if (unveil("gmon.out", "rwc") != 0)
3519 return got_error_from_errno2("unveil", "gmon.out");
3520 #endif
3521 if (repo_path && unveil(repo_path, "r") != 0)
3522 return got_error_from_errno2("unveil", repo_path);
3524 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3525 return got_error_from_errno2("unveil", worktree_path);
3527 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3528 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3530 error = got_privsep_unveil_exec_helpers();
3531 if (error != NULL)
3532 return error;
3534 if (unveil(NULL, NULL) != 0)
3535 return got_error_from_errno("unveil");
3537 return NULL;
3540 static void
3541 init_curses(void)
3544 * Override default signal handlers before starting ncurses.
3545 * This should prevent ncurses from installing its own
3546 * broken cleanup() signal handler.
3548 signal(SIGWINCH, tog_sigwinch);
3549 signal(SIGPIPE, tog_sigpipe);
3550 signal(SIGCONT, tog_sigcont);
3551 signal(SIGINT, tog_sigint);
3552 signal(SIGTERM, tog_sigterm);
3554 initscr();
3555 cbreak();
3556 halfdelay(1); /* Do fast refresh while initial view is loading. */
3557 noecho();
3558 nonl();
3559 intrflush(stdscr, FALSE);
3560 keypad(stdscr, TRUE);
3561 curs_set(0);
3562 if (getenv("TOG_COLORS") != NULL) {
3563 start_color();
3564 use_default_colors();
3568 static const struct got_error *
3569 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3570 struct got_repository *repo, struct got_worktree *worktree)
3572 const struct got_error *err = NULL;
3574 if (argc == 0) {
3575 *in_repo_path = strdup("/");
3576 if (*in_repo_path == NULL)
3577 return got_error_from_errno("strdup");
3578 return NULL;
3581 if (worktree) {
3582 const char *prefix = got_worktree_get_path_prefix(worktree);
3583 char *p;
3585 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3586 if (err)
3587 return err;
3588 if (asprintf(in_repo_path, "%s%s%s", prefix,
3589 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3590 p) == -1) {
3591 err = got_error_from_errno("asprintf");
3592 *in_repo_path = NULL;
3594 free(p);
3595 } else
3596 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3598 return err;
3601 static const struct got_error *
3602 cmd_log(int argc, char *argv[])
3604 const struct got_error *error;
3605 struct got_repository *repo = NULL;
3606 struct got_worktree *worktree = NULL;
3607 struct got_object_id *start_id = NULL;
3608 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3609 char *start_commit = NULL, *label = NULL;
3610 struct got_reference *ref = NULL;
3611 const char *head_ref_name = NULL;
3612 int ch, log_branches = 0;
3613 struct tog_view *view;
3614 int *pack_fds = NULL;
3616 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3617 switch (ch) {
3618 case 'b':
3619 log_branches = 1;
3620 break;
3621 case 'c':
3622 start_commit = optarg;
3623 break;
3624 case 'r':
3625 repo_path = realpath(optarg, NULL);
3626 if (repo_path == NULL)
3627 return got_error_from_errno2("realpath",
3628 optarg);
3629 break;
3630 default:
3631 usage_log();
3632 /* NOTREACHED */
3636 argc -= optind;
3637 argv += optind;
3639 if (argc > 1)
3640 usage_log();
3642 error = got_repo_pack_fds_open(&pack_fds);
3643 if (error != NULL)
3644 goto done;
3646 if (repo_path == NULL) {
3647 cwd = getcwd(NULL, 0);
3648 if (cwd == NULL)
3649 return got_error_from_errno("getcwd");
3650 error = got_worktree_open(&worktree, cwd);
3651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3652 goto done;
3653 if (worktree)
3654 repo_path =
3655 strdup(got_worktree_get_repo_path(worktree));
3656 else
3657 repo_path = strdup(cwd);
3658 if (repo_path == NULL) {
3659 error = got_error_from_errno("strdup");
3660 goto done;
3664 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3665 if (error != NULL)
3666 goto done;
3668 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3669 repo, worktree);
3670 if (error)
3671 goto done;
3673 init_curses();
3675 error = apply_unveil(got_repo_get_path(repo),
3676 worktree ? got_worktree_get_root_path(worktree) : NULL);
3677 if (error)
3678 goto done;
3680 /* already loaded by tog_log_with_path()? */
3681 if (TAILQ_EMPTY(&tog_refs)) {
3682 error = tog_load_refs(repo, 0);
3683 if (error)
3684 goto done;
3687 if (start_commit == NULL) {
3688 error = got_repo_match_object_id(&start_id, &label,
3689 worktree ? got_worktree_get_head_ref_name(worktree) :
3690 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3691 if (error)
3692 goto done;
3693 head_ref_name = label;
3694 } else {
3695 error = got_ref_open(&ref, repo, start_commit, 0);
3696 if (error == NULL)
3697 head_ref_name = got_ref_get_name(ref);
3698 else if (error->code != GOT_ERR_NOT_REF)
3699 goto done;
3700 error = got_repo_match_object_id(&start_id, NULL,
3701 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3702 if (error)
3703 goto done;
3706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3707 if (view == NULL) {
3708 error = got_error_from_errno("view_open");
3709 goto done;
3711 error = open_log_view(view, start_id, repo, head_ref_name,
3712 in_repo_path, log_branches);
3713 if (error)
3714 goto done;
3715 if (worktree) {
3716 /* Release work tree lock. */
3717 got_worktree_close(worktree);
3718 worktree = NULL;
3720 error = view_loop(view);
3721 done:
3722 free(in_repo_path);
3723 free(repo_path);
3724 free(cwd);
3725 free(start_id);
3726 free(label);
3727 if (ref)
3728 got_ref_close(ref);
3729 if (repo) {
3730 const struct got_error *close_err = got_repo_close(repo);
3731 if (error == NULL)
3732 error = close_err;
3734 if (worktree)
3735 got_worktree_close(worktree);
3736 if (pack_fds) {
3737 const struct got_error *pack_err =
3738 got_repo_pack_fds_close(pack_fds);
3739 if (error == NULL)
3740 error = pack_err;
3742 tog_free_refs();
3743 return error;
3746 __dead static void
3747 usage_diff(void)
3749 endwin();
3750 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3751 "[-w] object1 object2\n", getprogname());
3752 exit(1);
3755 static int
3756 match_line(const char *line, regex_t *regex, size_t nmatch,
3757 regmatch_t *regmatch)
3759 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3762 static struct tog_color *
3763 match_color(struct tog_colors *colors, const char *line)
3765 struct tog_color *tc = NULL;
3767 STAILQ_FOREACH(tc, colors, entry) {
3768 if (match_line(line, &tc->regex, 0, NULL))
3769 return tc;
3772 return NULL;
3775 static const struct got_error *
3776 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3777 WINDOW *window, int skipcol, regmatch_t *regmatch)
3779 const struct got_error *err = NULL;
3780 char *exstr = NULL;
3781 wchar_t *wline = NULL;
3782 int rme, rms, n, width, scrollx;
3783 int width0 = 0, width1 = 0, width2 = 0;
3784 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3786 *wtotal = 0;
3788 rms = regmatch->rm_so;
3789 rme = regmatch->rm_eo;
3791 err = expand_tab(&exstr, line);
3792 if (err)
3793 return err;
3795 /* Split the line into 3 segments, according to match offsets. */
3796 seg0 = strndup(exstr, rms);
3797 if (seg0 == NULL) {
3798 err = got_error_from_errno("strndup");
3799 goto done;
3801 seg1 = strndup(exstr + rms, rme - rms);
3802 if (seg1 == NULL) {
3803 err = got_error_from_errno("strndup");
3804 goto done;
3806 seg2 = strdup(exstr + rme);
3807 if (seg2 == NULL) {
3808 err = got_error_from_errno("strndup");
3809 goto done;
3812 /* draw up to matched token if we haven't scrolled past it */
3813 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3814 col_tab_align, 1);
3815 if (err)
3816 goto done;
3817 n = MAX(width0 - skipcol, 0);
3818 if (n) {
3819 free(wline);
3820 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3821 wlimit, col_tab_align, 1);
3822 if (err)
3823 goto done;
3824 waddwstr(window, &wline[scrollx]);
3825 wlimit -= width;
3826 *wtotal += width;
3829 if (wlimit > 0) {
3830 int i = 0, w = 0;
3831 size_t wlen;
3833 free(wline);
3834 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3835 col_tab_align, 1);
3836 if (err)
3837 goto done;
3838 wlen = wcslen(wline);
3839 while (i < wlen) {
3840 width = wcwidth(wline[i]);
3841 if (width == -1) {
3842 /* should not happen, tabs are expanded */
3843 err = got_error(GOT_ERR_RANGE);
3844 goto done;
3846 if (width0 + w + width > skipcol)
3847 break;
3848 w += width;
3849 i++;
3851 /* draw (visible part of) matched token (if scrolled into it) */
3852 if (width1 - w > 0) {
3853 wattron(window, A_STANDOUT);
3854 waddwstr(window, &wline[i]);
3855 wattroff(window, A_STANDOUT);
3856 wlimit -= (width1 - w);
3857 *wtotal += (width1 - w);
3861 if (wlimit > 0) { /* draw rest of line */
3862 free(wline);
3863 if (skipcol > width0 + width1) {
3864 err = format_line(&wline, &width2, &scrollx, seg2,
3865 skipcol - (width0 + width1), wlimit,
3866 col_tab_align, 1);
3867 if (err)
3868 goto done;
3869 waddwstr(window, &wline[scrollx]);
3870 } else {
3871 err = format_line(&wline, &width2, NULL, seg2, 0,
3872 wlimit, col_tab_align, 1);
3873 if (err)
3874 goto done;
3875 waddwstr(window, wline);
3877 *wtotal += width2;
3879 done:
3880 free(wline);
3881 free(exstr);
3882 free(seg0);
3883 free(seg1);
3884 free(seg2);
3885 return err;
3888 static const struct got_error *
3889 draw_file(struct tog_view *view, const char *header)
3891 struct tog_diff_view_state *s = &view->state.diff;
3892 regmatch_t *regmatch = &view->regmatch;
3893 const struct got_error *err;
3894 int nprinted = 0;
3895 char *line;
3896 size_t linesize = 0;
3897 ssize_t linelen;
3898 struct tog_color *tc;
3899 wchar_t *wline;
3900 int width;
3901 int max_lines = view->nlines;
3902 int nlines = s->nlines;
3903 off_t line_offset;
3905 line_offset = s->line_offsets[s->first_displayed_line - 1];
3906 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3907 return got_error_from_errno("fseek");
3909 werase(view->window);
3911 if (header) {
3912 if (asprintf(&line, "[%d/%d] %s",
3913 s->first_displayed_line - 1 + s->selected_line, nlines,
3914 header) == -1)
3915 return got_error_from_errno("asprintf");
3916 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3917 0, 0);
3918 free(line);
3919 if (err)
3920 return err;
3922 if (view_needs_focus_indication(view))
3923 wstandout(view->window);
3924 waddwstr(view->window, wline);
3925 free(wline);
3926 wline = NULL;
3927 if (view_needs_focus_indication(view))
3928 wstandend(view->window);
3929 if (width <= view->ncols - 1)
3930 waddch(view->window, '\n');
3932 if (max_lines <= 1)
3933 return NULL;
3934 max_lines--;
3937 s->eof = 0;
3938 view->maxx = 0;
3939 line = NULL;
3940 while (max_lines > 0 && nprinted < max_lines) {
3941 linelen = getline(&line, &linesize, s->f);
3942 if (linelen == -1) {
3943 if (feof(s->f)) {
3944 s->eof = 1;
3945 break;
3947 free(line);
3948 return got_ferror(s->f, GOT_ERR_IO);
3951 /* Set view->maxx based on full line length. */
3952 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3953 view->x ? 1 : 0);
3954 if (err) {
3955 free(line);
3956 return err;
3958 view->maxx = MAX(view->maxx, width);
3959 free(wline);
3960 wline = NULL;
3962 tc = match_color(&s->colors, line);
3963 if (tc)
3964 wattr_on(view->window,
3965 COLOR_PAIR(tc->colorpair), NULL);
3966 if (s->first_displayed_line + nprinted == s->matched_line &&
3967 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3968 err = add_matched_line(&width, line, view->ncols, 0,
3969 view->window, view->x, regmatch);
3970 if (err) {
3971 free(line);
3972 return err;
3974 } else {
3975 int skip;
3976 err = format_line(&wline, &width, &skip, line,
3977 view->x, view->ncols, 0, view->x ? 1 : 0);
3978 if (err) {
3979 free(line);
3980 return err;
3982 waddwstr(view->window, &wline[skip]);
3983 free(wline);
3984 wline = NULL;
3986 if (tc)
3987 wattr_off(view->window,
3988 COLOR_PAIR(tc->colorpair), NULL);
3989 if (width <= view->ncols - 1)
3990 waddch(view->window, '\n');
3991 nprinted++;
3993 free(line);
3994 if (nprinted >= 1)
3995 s->last_displayed_line = s->first_displayed_line +
3996 (nprinted - 1);
3997 else
3998 s->last_displayed_line = s->first_displayed_line;
4000 view_border(view);
4002 if (s->eof) {
4003 while (nprinted < view->nlines) {
4004 waddch(view->window, '\n');
4005 nprinted++;
4008 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4009 view->ncols, 0, 0);
4010 if (err) {
4011 return err;
4014 wstandout(view->window);
4015 waddwstr(view->window, wline);
4016 free(wline);
4017 wline = NULL;
4018 wstandend(view->window);
4021 return NULL;
4024 static char *
4025 get_datestr(time_t *time, char *datebuf)
4027 struct tm mytm, *tm;
4028 char *p, *s;
4030 tm = gmtime_r(time, &mytm);
4031 if (tm == NULL)
4032 return NULL;
4033 s = asctime_r(tm, datebuf);
4034 if (s == NULL)
4035 return NULL;
4036 p = strchr(s, '\n');
4037 if (p)
4038 *p = '\0';
4039 return s;
4042 static const struct got_error *
4043 get_changed_paths(struct got_pathlist_head *paths,
4044 struct got_commit_object *commit, struct got_repository *repo)
4046 const struct got_error *err = NULL;
4047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4049 struct got_object_qid *qid;
4051 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4052 if (qid != NULL) {
4053 struct got_commit_object *pcommit;
4054 err = got_object_open_as_commit(&pcommit, repo,
4055 &qid->id);
4056 if (err)
4057 return err;
4059 tree_id1 = got_object_id_dup(
4060 got_object_commit_get_tree_id(pcommit));
4061 if (tree_id1 == NULL) {
4062 got_object_commit_close(pcommit);
4063 return got_error_from_errno("got_object_id_dup");
4065 got_object_commit_close(pcommit);
4069 if (tree_id1) {
4070 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4071 if (err)
4072 goto done;
4075 tree_id2 = got_object_commit_get_tree_id(commit);
4076 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4077 if (err)
4078 goto done;
4080 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4081 got_diff_tree_collect_changed_paths, paths, 0);
4082 done:
4083 if (tree1)
4084 got_object_tree_close(tree1);
4085 if (tree2)
4086 got_object_tree_close(tree2);
4087 free(tree_id1);
4088 return err;
4091 static const struct got_error *
4092 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4094 off_t *p;
4096 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4097 if (p == NULL)
4098 return got_error_from_errno("reallocarray");
4099 *line_offsets = p;
4100 (*line_offsets)[*nlines] = off;
4101 (*nlines)++;
4102 return NULL;
4105 static const struct got_error *
4106 write_commit_info(off_t **line_offsets, size_t *nlines,
4107 struct got_object_id *commit_id, struct got_reflist_head *refs,
4108 struct got_repository *repo, FILE *outfile)
4110 const struct got_error *err = NULL;
4111 char datebuf[26], *datestr;
4112 struct got_commit_object *commit;
4113 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4114 time_t committer_time;
4115 const char *author, *committer;
4116 char *refs_str = NULL;
4117 struct got_pathlist_head changed_paths;
4118 struct got_pathlist_entry *pe;
4119 off_t outoff = 0;
4120 int n;
4122 TAILQ_INIT(&changed_paths);
4124 if (refs) {
4125 err = build_refs_str(&refs_str, refs, commit_id, repo);
4126 if (err)
4127 return err;
4130 err = got_object_open_as_commit(&commit, repo, commit_id);
4131 if (err)
4132 return err;
4134 err = got_object_id_str(&id_str, commit_id);
4135 if (err) {
4136 err = got_error_from_errno("got_object_id_str");
4137 goto done;
4140 err = add_line_offset(line_offsets, nlines, 0);
4141 if (err)
4142 goto done;
4144 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4145 refs_str ? refs_str : "", refs_str ? ")" : "");
4146 if (n < 0) {
4147 err = got_error_from_errno("fprintf");
4148 goto done;
4150 outoff += n;
4151 err = add_line_offset(line_offsets, nlines, outoff);
4152 if (err)
4153 goto done;
4155 n = fprintf(outfile, "from: %s\n",
4156 got_object_commit_get_author(commit));
4157 if (n < 0) {
4158 err = got_error_from_errno("fprintf");
4159 goto done;
4161 outoff += n;
4162 err = add_line_offset(line_offsets, nlines, outoff);
4163 if (err)
4164 goto done;
4166 committer_time = got_object_commit_get_committer_time(commit);
4167 datestr = get_datestr(&committer_time, datebuf);
4168 if (datestr) {
4169 n = fprintf(outfile, "date: %s UTC\n", datestr);
4170 if (n < 0) {
4171 err = got_error_from_errno("fprintf");
4172 goto done;
4174 outoff += n;
4175 err = add_line_offset(line_offsets, nlines, outoff);
4176 if (err)
4177 goto done;
4179 author = got_object_commit_get_author(commit);
4180 committer = got_object_commit_get_committer(commit);
4181 if (strcmp(author, committer) != 0) {
4182 n = fprintf(outfile, "via: %s\n", committer);
4183 if (n < 0) {
4184 err = got_error_from_errno("fprintf");
4185 goto done;
4187 outoff += n;
4188 err = add_line_offset(line_offsets, nlines, outoff);
4189 if (err)
4190 goto done;
4192 if (got_object_commit_get_nparents(commit) > 1) {
4193 const struct got_object_id_queue *parent_ids;
4194 struct got_object_qid *qid;
4195 int pn = 1;
4196 parent_ids = got_object_commit_get_parent_ids(commit);
4197 STAILQ_FOREACH(qid, parent_ids, entry) {
4198 err = got_object_id_str(&id_str, &qid->id);
4199 if (err)
4200 goto done;
4201 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4202 if (n < 0) {
4203 err = got_error_from_errno("fprintf");
4204 goto done;
4206 outoff += n;
4207 err = add_line_offset(line_offsets, nlines, outoff);
4208 if (err)
4209 goto done;
4210 free(id_str);
4211 id_str = NULL;
4215 err = got_object_commit_get_logmsg(&logmsg, commit);
4216 if (err)
4217 goto done;
4218 s = logmsg;
4219 while ((line = strsep(&s, "\n")) != NULL) {
4220 n = fprintf(outfile, "%s\n", line);
4221 if (n < 0) {
4222 err = got_error_from_errno("fprintf");
4223 goto done;
4225 outoff += n;
4226 err = add_line_offset(line_offsets, nlines, outoff);
4227 if (err)
4228 goto done;
4231 err = get_changed_paths(&changed_paths, commit, repo);
4232 if (err)
4233 goto done;
4234 TAILQ_FOREACH(pe, &changed_paths, entry) {
4235 struct got_diff_changed_path *cp = pe->data;
4236 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4237 if (n < 0) {
4238 err = got_error_from_errno("fprintf");
4239 goto done;
4241 outoff += n;
4242 err = add_line_offset(line_offsets, nlines, outoff);
4243 if (err)
4244 goto done;
4245 free((char *)pe->path);
4246 free(pe->data);
4249 fputc('\n', outfile);
4250 outoff++;
4251 err = add_line_offset(line_offsets, nlines, outoff);
4252 done:
4253 got_pathlist_free(&changed_paths);
4254 free(id_str);
4255 free(logmsg);
4256 free(refs_str);
4257 got_object_commit_close(commit);
4258 if (err) {
4259 free(*line_offsets);
4260 *line_offsets = NULL;
4261 *nlines = 0;
4263 return err;
4266 static const struct got_error *
4267 create_diff(struct tog_diff_view_state *s)
4269 const struct got_error *err = NULL;
4270 FILE *f = NULL;
4271 int obj_type;
4273 free(s->line_offsets);
4274 s->line_offsets = malloc(sizeof(off_t));
4275 if (s->line_offsets == NULL)
4276 return got_error_from_errno("malloc");
4277 s->nlines = 0;
4279 f = got_opentemp();
4280 if (f == NULL) {
4281 err = got_error_from_errno("got_opentemp");
4282 goto done;
4284 if (s->f && fclose(s->f) == EOF) {
4285 err = got_error_from_errno("fclose");
4286 goto done;
4288 s->f = f;
4290 if (s->id1)
4291 err = got_object_get_type(&obj_type, s->repo, s->id1);
4292 else
4293 err = got_object_get_type(&obj_type, s->repo, s->id2);
4294 if (err)
4295 goto done;
4297 switch (obj_type) {
4298 case GOT_OBJ_TYPE_BLOB:
4299 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4300 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4301 s->label1, s->label2, tog_diff_algo, s->diff_context,
4302 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4303 break;
4304 case GOT_OBJ_TYPE_TREE:
4305 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4306 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4307 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4308 s->force_text_diff, s->repo, s->f);
4309 break;
4310 case GOT_OBJ_TYPE_COMMIT: {
4311 const struct got_object_id_queue *parent_ids;
4312 struct got_object_qid *pid;
4313 struct got_commit_object *commit2;
4314 struct got_reflist_head *refs;
4316 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4317 if (err)
4318 goto done;
4319 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4320 /* Show commit info if we're diffing to a parent/root commit. */
4321 if (s->id1 == NULL) {
4322 err = write_commit_info(&s->line_offsets, &s->nlines,
4323 s->id2, refs, s->repo, s->f);
4324 if (err)
4325 goto done;
4326 } else {
4327 parent_ids = got_object_commit_get_parent_ids(commit2);
4328 STAILQ_FOREACH(pid, parent_ids, entry) {
4329 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4330 err = write_commit_info(
4331 &s->line_offsets, &s->nlines,
4332 s->id2, refs, s->repo, s->f);
4333 if (err)
4334 goto done;
4335 break;
4339 got_object_commit_close(commit2);
4341 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4342 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4343 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4344 s->force_text_diff, s->repo, s->f);
4345 break;
4347 default:
4348 err = got_error(GOT_ERR_OBJ_TYPE);
4349 break;
4351 if (err)
4352 goto done;
4353 done:
4354 if (s->f && fflush(s->f) != 0 && err == NULL)
4355 err = got_error_from_errno("fflush");
4356 return err;
4359 static void
4360 diff_view_indicate_progress(struct tog_view *view)
4362 mvwaddstr(view->window, 0, 0, "diffing...");
4363 update_panels();
4364 doupdate();
4367 static const struct got_error *
4368 search_start_diff_view(struct tog_view *view)
4370 struct tog_diff_view_state *s = &view->state.diff;
4372 s->matched_line = 0;
4373 return NULL;
4376 static const struct got_error *
4377 search_next_diff_view(struct tog_view *view)
4379 struct tog_diff_view_state *s = &view->state.diff;
4380 const struct got_error *err = NULL;
4381 int lineno;
4382 char *line = NULL;
4383 size_t linesize = 0;
4384 ssize_t linelen;
4386 if (!view->searching) {
4387 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4388 return NULL;
4391 if (s->matched_line) {
4392 if (view->searching == TOG_SEARCH_FORWARD)
4393 lineno = s->matched_line + 1;
4394 else
4395 lineno = s->matched_line - 1;
4396 } else
4397 lineno = s->first_displayed_line;
4399 while (1) {
4400 off_t offset;
4402 if (lineno <= 0 || lineno > s->nlines) {
4403 if (s->matched_line == 0) {
4404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4405 break;
4408 if (view->searching == TOG_SEARCH_FORWARD)
4409 lineno = 1;
4410 else
4411 lineno = s->nlines;
4414 offset = s->line_offsets[lineno - 1];
4415 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4416 free(line);
4417 return got_error_from_errno("fseeko");
4419 linelen = getline(&line, &linesize, s->f);
4420 if (linelen != -1) {
4421 char *exstr;
4422 err = expand_tab(&exstr, line);
4423 if (err)
4424 break;
4425 if (match_line(exstr, &view->regex, 1,
4426 &view->regmatch)) {
4427 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4428 s->matched_line = lineno;
4429 free(exstr);
4430 break;
4432 free(exstr);
4434 if (view->searching == TOG_SEARCH_FORWARD)
4435 lineno++;
4436 else
4437 lineno--;
4439 free(line);
4441 if (s->matched_line) {
4442 s->first_displayed_line = s->matched_line;
4443 s->selected_line = 1;
4446 return err;
4449 static const struct got_error *
4450 close_diff_view(struct tog_view *view)
4452 const struct got_error *err = NULL;
4453 struct tog_diff_view_state *s = &view->state.diff;
4455 free(s->id1);
4456 s->id1 = NULL;
4457 free(s->id2);
4458 s->id2 = NULL;
4459 if (s->f && fclose(s->f) == EOF)
4460 err = got_error_from_errno("fclose");
4461 s->f = NULL;
4462 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4463 err = got_error_from_errno("fclose");
4464 s->f1 = NULL;
4465 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4466 err = got_error_from_errno("fclose");
4467 s->f2 = NULL;
4468 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4469 err = got_error_from_errno("close");
4470 s->fd1 = -1;
4471 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4472 err = got_error_from_errno("close");
4473 s->fd2 = -1;
4474 free_colors(&s->colors);
4475 free(s->line_offsets);
4476 s->line_offsets = NULL;
4477 s->nlines = 0;
4478 return err;
4481 static const struct got_error *
4482 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4483 struct got_object_id *id2, const char *label1, const char *label2,
4484 int diff_context, int ignore_whitespace, int force_text_diff,
4485 struct tog_view *parent_view, struct got_repository *repo)
4487 const struct got_error *err;
4488 struct tog_diff_view_state *s = &view->state.diff;
4490 memset(s, 0, sizeof(*s));
4491 s->fd1 = -1;
4492 s->fd2 = -1;
4494 if (id1 != NULL && id2 != NULL) {
4495 int type1, type2;
4496 err = got_object_get_type(&type1, repo, id1);
4497 if (err)
4498 return err;
4499 err = got_object_get_type(&type2, repo, id2);
4500 if (err)
4501 return err;
4503 if (type1 != type2)
4504 return got_error(GOT_ERR_OBJ_TYPE);
4506 s->first_displayed_line = 1;
4507 s->last_displayed_line = view->nlines;
4508 s->selected_line = 1;
4509 s->repo = repo;
4510 s->id1 = id1;
4511 s->id2 = id2;
4512 s->label1 = label1;
4513 s->label2 = label2;
4515 if (id1) {
4516 s->id1 = got_object_id_dup(id1);
4517 if (s->id1 == NULL)
4518 return got_error_from_errno("got_object_id_dup");
4519 } else
4520 s->id1 = NULL;
4522 s->id2 = got_object_id_dup(id2);
4523 if (s->id2 == NULL) {
4524 err = got_error_from_errno("got_object_id_dup");
4525 goto done;
4528 s->f1 = got_opentemp();
4529 if (s->f1 == NULL) {
4530 err = got_error_from_errno("got_opentemp");
4531 goto done;
4534 s->f2 = got_opentemp();
4535 if (s->f2 == NULL) {
4536 err = got_error_from_errno("got_opentemp");
4537 goto done;
4540 s->fd1 = got_opentempfd();
4541 if (s->fd1 == -1) {
4542 err = got_error_from_errno("got_opentempfd");
4543 goto done;
4546 s->fd2 = got_opentempfd();
4547 if (s->fd2 == -1) {
4548 err = got_error_from_errno("got_opentempfd");
4549 goto done;
4552 s->first_displayed_line = 1;
4553 s->last_displayed_line = view->nlines;
4554 s->diff_context = diff_context;
4555 s->ignore_whitespace = ignore_whitespace;
4556 s->force_text_diff = force_text_diff;
4557 s->parent_view = parent_view;
4558 s->repo = repo;
4560 STAILQ_INIT(&s->colors);
4561 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4562 err = add_color(&s->colors,
4563 "^-", TOG_COLOR_DIFF_MINUS,
4564 get_color_value("TOG_COLOR_DIFF_MINUS"));
4565 if (err)
4566 goto done;
4567 err = add_color(&s->colors, "^\\+",
4568 TOG_COLOR_DIFF_PLUS,
4569 get_color_value("TOG_COLOR_DIFF_PLUS"));
4570 if (err)
4571 goto done;
4572 err = add_color(&s->colors,
4573 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4574 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4575 if (err)
4576 goto done;
4578 err = add_color(&s->colors,
4579 "^(commit [0-9a-f]|parent [0-9]|"
4580 "(blob|file|tree|commit) [-+] |"
4581 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4582 get_color_value("TOG_COLOR_DIFF_META"));
4583 if (err)
4584 goto done;
4586 err = add_color(&s->colors,
4587 "^(from|via): ", TOG_COLOR_AUTHOR,
4588 get_color_value("TOG_COLOR_AUTHOR"));
4589 if (err)
4590 goto done;
4592 err = add_color(&s->colors,
4593 "^date: ", TOG_COLOR_DATE,
4594 get_color_value("TOG_COLOR_DATE"));
4595 if (err)
4596 goto done;
4599 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4600 view_is_splitscreen(view))
4601 show_log_view(parent_view); /* draw border */
4602 diff_view_indicate_progress(view);
4604 err = create_diff(s);
4606 view->show = show_diff_view;
4607 view->input = input_diff_view;
4608 view->reset = reset_diff_view;
4609 view->close = close_diff_view;
4610 view->search_start = search_start_diff_view;
4611 view->search_next = search_next_diff_view;
4612 done:
4613 if (err)
4614 close_diff_view(view);
4615 return err;
4618 static const struct got_error *
4619 show_diff_view(struct tog_view *view)
4621 const struct got_error *err;
4622 struct tog_diff_view_state *s = &view->state.diff;
4623 char *id_str1 = NULL, *id_str2, *header;
4624 const char *label1, *label2;
4626 if (s->id1) {
4627 err = got_object_id_str(&id_str1, s->id1);
4628 if (err)
4629 return err;
4630 label1 = s->label1 ? : id_str1;
4631 } else
4632 label1 = "/dev/null";
4634 err = got_object_id_str(&id_str2, s->id2);
4635 if (err)
4636 return err;
4637 label2 = s->label2 ? : id_str2;
4639 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4640 err = got_error_from_errno("asprintf");
4641 free(id_str1);
4642 free(id_str2);
4643 return err;
4645 free(id_str1);
4646 free(id_str2);
4648 err = draw_file(view, header);
4649 free(header);
4650 return err;
4653 static const struct got_error *
4654 set_selected_commit(struct tog_diff_view_state *s,
4655 struct commit_queue_entry *entry)
4657 const struct got_error *err;
4658 const struct got_object_id_queue *parent_ids;
4659 struct got_commit_object *selected_commit;
4660 struct got_object_qid *pid;
4662 free(s->id2);
4663 s->id2 = got_object_id_dup(entry->id);
4664 if (s->id2 == NULL)
4665 return got_error_from_errno("got_object_id_dup");
4667 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4668 if (err)
4669 return err;
4670 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4671 free(s->id1);
4672 pid = STAILQ_FIRST(parent_ids);
4673 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4674 got_object_commit_close(selected_commit);
4675 return NULL;
4678 static const struct got_error *
4679 reset_diff_view(struct tog_view *view)
4681 struct tog_diff_view_state *s = &view->state.diff;
4683 view->count = 0;
4684 wclear(view->window);
4685 s->first_displayed_line = 1;
4686 s->last_displayed_line = view->nlines;
4687 s->matched_line = 0;
4688 diff_view_indicate_progress(view);
4689 return create_diff(s);
4692 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4693 int, int, int);
4694 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4695 int, int);
4697 static const struct got_error *
4698 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4700 const struct got_error *err = NULL;
4701 struct tog_diff_view_state *s = &view->state.diff;
4702 struct tog_log_view_state *ls;
4703 struct commit_queue_entry *old_selected_entry;
4704 char *line = NULL;
4705 size_t linesize = 0;
4706 ssize_t linelen;
4707 int i, nscroll = view->nlines - 1, up = 0;
4709 switch (ch) {
4710 case '0':
4711 view->x = 0;
4712 break;
4713 case '$':
4714 view->x = MAX(view->maxx - view->ncols / 3, 0);
4715 view->count = 0;
4716 break;
4717 case KEY_RIGHT:
4718 case 'l':
4719 if (view->x + view->ncols / 3 < view->maxx)
4720 view->x += 2; /* move two columns right */
4721 else
4722 view->count = 0;
4723 break;
4724 case KEY_LEFT:
4725 case 'h':
4726 view->x -= MIN(view->x, 2); /* move two columns back */
4727 if (view->x <= 0)
4728 view->count = 0;
4729 break;
4730 case 'a':
4731 case 'w':
4732 if (ch == 'a')
4733 s->force_text_diff = !s->force_text_diff;
4734 if (ch == 'w')
4735 s->ignore_whitespace = !s->ignore_whitespace;
4736 err = reset_diff_view(view);
4737 break;
4738 case 'g':
4739 case KEY_HOME:
4740 s->first_displayed_line = 1;
4741 view->count = 0;
4742 break;
4743 case 'G':
4744 case KEY_END:
4745 view->count = 0;
4746 if (s->eof)
4747 break;
4749 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4750 s->eof = 1;
4751 break;
4752 case 'k':
4753 case KEY_UP:
4754 case CTRL('p'):
4755 if (s->first_displayed_line > 1)
4756 s->first_displayed_line--;
4757 else
4758 view->count = 0;
4759 break;
4760 case CTRL('u'):
4761 case 'u':
4762 nscroll /= 2;
4763 /* FALL THROUGH */
4764 case KEY_PPAGE:
4765 case CTRL('b'):
4766 case 'b':
4767 if (s->first_displayed_line == 1) {
4768 view->count = 0;
4769 break;
4771 i = 0;
4772 while (i++ < nscroll && s->first_displayed_line > 1)
4773 s->first_displayed_line--;
4774 break;
4775 case 'j':
4776 case KEY_DOWN:
4777 case CTRL('n'):
4778 if (!s->eof)
4779 s->first_displayed_line++;
4780 else
4781 view->count = 0;
4782 break;
4783 case CTRL('d'):
4784 case 'd':
4785 nscroll /= 2;
4786 /* FALL THROUGH */
4787 case KEY_NPAGE:
4788 case CTRL('f'):
4789 case 'f':
4790 case ' ':
4791 if (s->eof) {
4792 view->count = 0;
4793 break;
4795 i = 0;
4796 while (!s->eof && i++ < nscroll) {
4797 linelen = getline(&line, &linesize, s->f);
4798 s->first_displayed_line++;
4799 if (linelen == -1) {
4800 if (feof(s->f)) {
4801 s->eof = 1;
4802 } else
4803 err = got_ferror(s->f, GOT_ERR_IO);
4804 break;
4807 free(line);
4808 break;
4809 case '[':
4810 if (s->diff_context > 0) {
4811 s->diff_context--;
4812 s->matched_line = 0;
4813 diff_view_indicate_progress(view);
4814 err = create_diff(s);
4815 if (s->first_displayed_line + view->nlines - 1 >
4816 s->nlines) {
4817 s->first_displayed_line = 1;
4818 s->last_displayed_line = view->nlines;
4820 } else
4821 view->count = 0;
4822 break;
4823 case ']':
4824 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4825 s->diff_context++;
4826 s->matched_line = 0;
4827 diff_view_indicate_progress(view);
4828 err = create_diff(s);
4829 } else
4830 view->count = 0;
4831 break;
4832 case '<':
4833 case ',':
4834 case 'K':
4835 up = 1;
4836 /* FALL THROUGH */
4837 case '>':
4838 case '.':
4839 case 'J':
4840 if (s->parent_view == NULL) {
4841 view->count = 0;
4842 break;
4844 s->parent_view->count = view->count;
4846 if (s->parent_view->type == TOG_VIEW_LOG) {
4847 ls = &s->parent_view->state.log;
4848 old_selected_entry = ls->selected_entry;
4850 err = input_log_view(NULL, s->parent_view,
4851 up ? KEY_UP : KEY_DOWN);
4852 if (err)
4853 break;
4854 view->count = s->parent_view->count;
4856 if (old_selected_entry == ls->selected_entry)
4857 break;
4859 err = set_selected_commit(s, ls->selected_entry);
4860 if (err)
4861 break;
4862 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4863 struct tog_blame_view_state *bs;
4864 struct got_object_id *id, *prev_id;
4866 bs = &s->parent_view->state.blame;
4867 prev_id = get_annotation_for_line(bs->blame.lines,
4868 bs->blame.nlines, bs->last_diffed_line);
4870 err = input_blame_view(&view, s->parent_view,
4871 up ? KEY_UP : KEY_DOWN);
4872 if (err)
4873 break;
4874 view->count = s->parent_view->count;
4876 if (prev_id == NULL)
4877 break;
4878 id = get_selected_commit_id(bs->blame.lines,
4879 bs->blame.nlines, bs->first_displayed_line,
4880 bs->selected_line);
4881 if (id == NULL)
4882 break;
4884 if (!got_object_id_cmp(prev_id, id))
4885 break;
4887 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4888 if (err)
4889 break;
4891 s->first_displayed_line = 1;
4892 s->last_displayed_line = view->nlines;
4893 s->matched_line = 0;
4894 view->x = 0;
4896 diff_view_indicate_progress(view);
4897 err = create_diff(s);
4898 break;
4899 default:
4900 view->count = 0;
4901 break;
4904 return err;
4907 static const struct got_error *
4908 cmd_diff(int argc, char *argv[])
4910 const struct got_error *error = NULL;
4911 struct got_repository *repo = NULL;
4912 struct got_worktree *worktree = NULL;
4913 struct got_object_id *id1 = NULL, *id2 = NULL;
4914 char *repo_path = NULL, *cwd = NULL;
4915 char *id_str1 = NULL, *id_str2 = NULL;
4916 char *label1 = NULL, *label2 = NULL;
4917 int diff_context = 3, ignore_whitespace = 0;
4918 int ch, force_text_diff = 0;
4919 const char *errstr;
4920 struct tog_view *view;
4921 int *pack_fds = NULL;
4923 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4924 switch (ch) {
4925 case 'a':
4926 force_text_diff = 1;
4927 break;
4928 case 'C':
4929 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4930 &errstr);
4931 if (errstr != NULL)
4932 errx(1, "number of context lines is %s: %s",
4933 errstr, errstr);
4934 break;
4935 case 'r':
4936 repo_path = realpath(optarg, NULL);
4937 if (repo_path == NULL)
4938 return got_error_from_errno2("realpath",
4939 optarg);
4940 got_path_strip_trailing_slashes(repo_path);
4941 break;
4942 case 'w':
4943 ignore_whitespace = 1;
4944 break;
4945 default:
4946 usage_diff();
4947 /* NOTREACHED */
4951 argc -= optind;
4952 argv += optind;
4954 if (argc == 0) {
4955 usage_diff(); /* TODO show local worktree changes */
4956 } else if (argc == 2) {
4957 id_str1 = argv[0];
4958 id_str2 = argv[1];
4959 } else
4960 usage_diff();
4962 error = got_repo_pack_fds_open(&pack_fds);
4963 if (error)
4964 goto done;
4966 if (repo_path == NULL) {
4967 cwd = getcwd(NULL, 0);
4968 if (cwd == NULL)
4969 return got_error_from_errno("getcwd");
4970 error = got_worktree_open(&worktree, cwd);
4971 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4972 goto done;
4973 if (worktree)
4974 repo_path =
4975 strdup(got_worktree_get_repo_path(worktree));
4976 else
4977 repo_path = strdup(cwd);
4978 if (repo_path == NULL) {
4979 error = got_error_from_errno("strdup");
4980 goto done;
4984 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4985 if (error)
4986 goto done;
4988 init_curses();
4990 error = apply_unveil(got_repo_get_path(repo), NULL);
4991 if (error)
4992 goto done;
4994 error = tog_load_refs(repo, 0);
4995 if (error)
4996 goto done;
4998 error = got_repo_match_object_id(&id1, &label1, id_str1,
4999 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 if (error)
5001 goto done;
5003 error = got_repo_match_object_id(&id2, &label2, id_str2,
5004 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5005 if (error)
5006 goto done;
5008 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5009 if (view == NULL) {
5010 error = got_error_from_errno("view_open");
5011 goto done;
5013 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5014 ignore_whitespace, force_text_diff, NULL, repo);
5015 if (error)
5016 goto done;
5017 error = view_loop(view);
5018 done:
5019 free(label1);
5020 free(label2);
5021 free(repo_path);
5022 free(cwd);
5023 if (repo) {
5024 const struct got_error *close_err = got_repo_close(repo);
5025 if (error == NULL)
5026 error = close_err;
5028 if (worktree)
5029 got_worktree_close(worktree);
5030 if (pack_fds) {
5031 const struct got_error *pack_err =
5032 got_repo_pack_fds_close(pack_fds);
5033 if (error == NULL)
5034 error = pack_err;
5036 tog_free_refs();
5037 return error;
5040 __dead static void
5041 usage_blame(void)
5043 endwin();
5044 fprintf(stderr,
5045 "usage: %s blame [-c commit] [-r repository-path] path\n",
5046 getprogname());
5047 exit(1);
5050 struct tog_blame_line {
5051 int annotated;
5052 struct got_object_id *id;
5055 static const struct got_error *
5056 draw_blame(struct tog_view *view)
5058 struct tog_blame_view_state *s = &view->state.blame;
5059 struct tog_blame *blame = &s->blame;
5060 regmatch_t *regmatch = &view->regmatch;
5061 const struct got_error *err;
5062 int lineno = 0, nprinted = 0;
5063 char *line = NULL;
5064 size_t linesize = 0;
5065 ssize_t linelen;
5066 wchar_t *wline;
5067 int width;
5068 struct tog_blame_line *blame_line;
5069 struct got_object_id *prev_id = NULL;
5070 char *id_str;
5071 struct tog_color *tc;
5073 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5074 if (err)
5075 return err;
5077 rewind(blame->f);
5078 werase(view->window);
5080 if (asprintf(&line, "commit %s", id_str) == -1) {
5081 err = got_error_from_errno("asprintf");
5082 free(id_str);
5083 return err;
5086 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 free(line);
5088 line = NULL;
5089 if (err)
5090 return err;
5091 if (view_needs_focus_indication(view))
5092 wstandout(view->window);
5093 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5094 if (tc)
5095 wattr_on(view->window,
5096 COLOR_PAIR(tc->colorpair), NULL);
5097 waddwstr(view->window, wline);
5098 if (tc)
5099 wattr_off(view->window,
5100 COLOR_PAIR(tc->colorpair), NULL);
5101 if (view_needs_focus_indication(view))
5102 wstandend(view->window);
5103 free(wline);
5104 wline = NULL;
5105 if (width < view->ncols - 1)
5106 waddch(view->window, '\n');
5108 if (asprintf(&line, "[%d/%d] %s%s",
5109 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5110 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5111 free(id_str);
5112 return got_error_from_errno("asprintf");
5114 free(id_str);
5115 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5116 free(line);
5117 line = NULL;
5118 if (err)
5119 return err;
5120 waddwstr(view->window, wline);
5121 free(wline);
5122 wline = NULL;
5123 if (width < view->ncols - 1)
5124 waddch(view->window, '\n');
5126 s->eof = 0;
5127 view->maxx = 0;
5128 while (nprinted < view->nlines - 2) {
5129 linelen = getline(&line, &linesize, blame->f);
5130 if (linelen == -1) {
5131 if (feof(blame->f)) {
5132 s->eof = 1;
5133 break;
5135 free(line);
5136 return got_ferror(blame->f, GOT_ERR_IO);
5138 if (++lineno < s->first_displayed_line)
5139 continue;
5141 /* Set view->maxx based on full line length. */
5142 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5143 if (err) {
5144 free(line);
5145 return err;
5147 free(wline);
5148 wline = NULL;
5149 view->maxx = MAX(view->maxx, width);
5151 if (nprinted == s->selected_line - 1)
5152 wstandout(view->window);
5154 if (blame->nlines > 0) {
5155 blame_line = &blame->lines[lineno - 1];
5156 if (blame_line->annotated && prev_id &&
5157 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5158 !(nprinted == s->selected_line - 1)) {
5159 waddstr(view->window, " ");
5160 } else if (blame_line->annotated) {
5161 char *id_str;
5162 err = got_object_id_str(&id_str,
5163 blame_line->id);
5164 if (err) {
5165 free(line);
5166 return err;
5168 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5169 if (tc)
5170 wattr_on(view->window,
5171 COLOR_PAIR(tc->colorpair), NULL);
5172 wprintw(view->window, "%.8s", id_str);
5173 if (tc)
5174 wattr_off(view->window,
5175 COLOR_PAIR(tc->colorpair), NULL);
5176 free(id_str);
5177 prev_id = blame_line->id;
5178 } else {
5179 waddstr(view->window, "........");
5180 prev_id = NULL;
5182 } else {
5183 waddstr(view->window, "........");
5184 prev_id = NULL;
5187 if (nprinted == s->selected_line - 1)
5188 wstandend(view->window);
5189 waddstr(view->window, " ");
5191 if (view->ncols <= 9) {
5192 width = 9;
5193 } else if (s->first_displayed_line + nprinted ==
5194 s->matched_line &&
5195 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5196 err = add_matched_line(&width, line, view->ncols - 9, 9,
5197 view->window, view->x, regmatch);
5198 if (err) {
5199 free(line);
5200 return err;
5202 width += 9;
5203 } else {
5204 int skip;
5205 err = format_line(&wline, &width, &skip, line,
5206 view->x, view->ncols - 9, 9, 1);
5207 if (err) {
5208 free(line);
5209 return err;
5211 waddwstr(view->window, &wline[skip]);
5212 width += 9;
5213 free(wline);
5214 wline = NULL;
5217 if (width <= view->ncols - 1)
5218 waddch(view->window, '\n');
5219 if (++nprinted == 1)
5220 s->first_displayed_line = lineno;
5222 free(line);
5223 s->last_displayed_line = lineno;
5225 view_border(view);
5227 return NULL;
5230 static const struct got_error *
5231 blame_cb(void *arg, int nlines, int lineno,
5232 struct got_commit_object *commit, struct got_object_id *id)
5234 const struct got_error *err = NULL;
5235 struct tog_blame_cb_args *a = arg;
5236 struct tog_blame_line *line;
5237 int errcode;
5239 if (nlines != a->nlines ||
5240 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5241 return got_error(GOT_ERR_RANGE);
5243 errcode = pthread_mutex_lock(&tog_mutex);
5244 if (errcode)
5245 return got_error_set_errno(errcode, "pthread_mutex_lock");
5247 if (*a->quit) { /* user has quit the blame view */
5248 err = got_error(GOT_ERR_ITER_COMPLETED);
5249 goto done;
5252 if (lineno == -1)
5253 goto done; /* no change in this commit */
5255 line = &a->lines[lineno - 1];
5256 if (line->annotated)
5257 goto done;
5259 line->id = got_object_id_dup(id);
5260 if (line->id == NULL) {
5261 err = got_error_from_errno("got_object_id_dup");
5262 goto done;
5264 line->annotated = 1;
5265 done:
5266 errcode = pthread_mutex_unlock(&tog_mutex);
5267 if (errcode)
5268 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5269 return err;
5272 static void *
5273 blame_thread(void *arg)
5275 const struct got_error *err, *close_err;
5276 struct tog_blame_thread_args *ta = arg;
5277 struct tog_blame_cb_args *a = ta->cb_args;
5278 int errcode, fd1 = -1, fd2 = -1;
5279 FILE *f1 = NULL, *f2 = NULL;
5281 fd1 = got_opentempfd();
5282 if (fd1 == -1)
5283 return (void *)got_error_from_errno("got_opentempfd");
5285 fd2 = got_opentempfd();
5286 if (fd2 == -1) {
5287 err = got_error_from_errno("got_opentempfd");
5288 goto done;
5291 f1 = got_opentemp();
5292 if (f1 == NULL) {
5293 err = (void *)got_error_from_errno("got_opentemp");
5294 goto done;
5296 f2 = got_opentemp();
5297 if (f2 == NULL) {
5298 err = (void *)got_error_from_errno("got_opentemp");
5299 goto done;
5302 err = block_signals_used_by_main_thread();
5303 if (err)
5304 goto done;
5306 err = got_blame(ta->path, a->commit_id, ta->repo,
5307 tog_diff_algo, blame_cb, ta->cb_args,
5308 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5309 if (err && err->code == GOT_ERR_CANCELLED)
5310 err = NULL;
5312 errcode = pthread_mutex_lock(&tog_mutex);
5313 if (errcode) {
5314 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5315 goto done;
5318 close_err = got_repo_close(ta->repo);
5319 if (err == NULL)
5320 err = close_err;
5321 ta->repo = NULL;
5322 *ta->complete = 1;
5324 errcode = pthread_mutex_unlock(&tog_mutex);
5325 if (errcode && err == NULL)
5326 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5328 done:
5329 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5330 err = got_error_from_errno("close");
5331 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5332 err = got_error_from_errno("close");
5333 if (f1 && fclose(f1) == EOF && err == NULL)
5334 err = got_error_from_errno("fclose");
5335 if (f2 && fclose(f2) == EOF && err == NULL)
5336 err = got_error_from_errno("fclose");
5338 return (void *)err;
5341 static struct got_object_id *
5342 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5343 int first_displayed_line, int selected_line)
5345 struct tog_blame_line *line;
5347 if (nlines <= 0)
5348 return NULL;
5350 line = &lines[first_displayed_line - 1 + selected_line - 1];
5351 if (!line->annotated)
5352 return NULL;
5354 return line->id;
5357 static struct got_object_id *
5358 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5359 int lineno)
5361 struct tog_blame_line *line;
5363 if (nlines <= 0 || lineno >= nlines)
5364 return NULL;
5366 line = &lines[lineno - 1];
5367 if (!line->annotated)
5368 return NULL;
5370 return line->id;
5373 static const struct got_error *
5374 stop_blame(struct tog_blame *blame)
5376 const struct got_error *err = NULL;
5377 int i;
5379 if (blame->thread) {
5380 int errcode;
5381 errcode = pthread_mutex_unlock(&tog_mutex);
5382 if (errcode)
5383 return got_error_set_errno(errcode,
5384 "pthread_mutex_unlock");
5385 errcode = pthread_join(blame->thread, (void **)&err);
5386 if (errcode)
5387 return got_error_set_errno(errcode, "pthread_join");
5388 errcode = pthread_mutex_lock(&tog_mutex);
5389 if (errcode)
5390 return got_error_set_errno(errcode,
5391 "pthread_mutex_lock");
5392 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5393 err = NULL;
5394 blame->thread = 0; //NULL;
5396 if (blame->thread_args.repo) {
5397 const struct got_error *close_err;
5398 close_err = got_repo_close(blame->thread_args.repo);
5399 if (err == NULL)
5400 err = close_err;
5401 blame->thread_args.repo = NULL;
5403 if (blame->f) {
5404 if (fclose(blame->f) == EOF && err == NULL)
5405 err = got_error_from_errno("fclose");
5406 blame->f = NULL;
5408 if (blame->lines) {
5409 for (i = 0; i < blame->nlines; i++)
5410 free(blame->lines[i].id);
5411 free(blame->lines);
5412 blame->lines = NULL;
5414 free(blame->cb_args.commit_id);
5415 blame->cb_args.commit_id = NULL;
5416 if (blame->pack_fds) {
5417 const struct got_error *pack_err =
5418 got_repo_pack_fds_close(blame->pack_fds);
5419 if (err == NULL)
5420 err = pack_err;
5421 blame->pack_fds = NULL;
5423 return err;
5426 static const struct got_error *
5427 cancel_blame_view(void *arg)
5429 const struct got_error *err = NULL;
5430 int *done = arg;
5431 int errcode;
5433 errcode = pthread_mutex_lock(&tog_mutex);
5434 if (errcode)
5435 return got_error_set_errno(errcode,
5436 "pthread_mutex_unlock");
5438 if (*done)
5439 err = got_error(GOT_ERR_CANCELLED);
5441 errcode = pthread_mutex_unlock(&tog_mutex);
5442 if (errcode)
5443 return got_error_set_errno(errcode,
5444 "pthread_mutex_lock");
5446 return err;
5449 static const struct got_error *
5450 run_blame(struct tog_view *view)
5452 struct tog_blame_view_state *s = &view->state.blame;
5453 struct tog_blame *blame = &s->blame;
5454 const struct got_error *err = NULL;
5455 struct got_commit_object *commit = NULL;
5456 struct got_blob_object *blob = NULL;
5457 struct got_repository *thread_repo = NULL;
5458 struct got_object_id *obj_id = NULL;
5459 int obj_type, fd = -1;
5460 int *pack_fds = NULL;
5462 err = got_object_open_as_commit(&commit, s->repo,
5463 &s->blamed_commit->id);
5464 if (err)
5465 return err;
5467 fd = got_opentempfd();
5468 if (fd == -1) {
5469 err = got_error_from_errno("got_opentempfd");
5470 goto done;
5473 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5474 if (err)
5475 goto done;
5477 err = got_object_get_type(&obj_type, s->repo, obj_id);
5478 if (err)
5479 goto done;
5481 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5482 err = got_error(GOT_ERR_OBJ_TYPE);
5483 goto done;
5486 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5487 if (err)
5488 goto done;
5489 blame->f = got_opentemp();
5490 if (blame->f == NULL) {
5491 err = got_error_from_errno("got_opentemp");
5492 goto done;
5494 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5495 &blame->line_offsets, blame->f, blob);
5496 if (err)
5497 goto done;
5498 if (blame->nlines == 0) {
5499 s->blame_complete = 1;
5500 goto done;
5503 /* Don't include \n at EOF in the blame line count. */
5504 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5505 blame->nlines--;
5507 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5508 if (blame->lines == NULL) {
5509 err = got_error_from_errno("calloc");
5510 goto done;
5513 err = got_repo_pack_fds_open(&pack_fds);
5514 if (err)
5515 goto done;
5516 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5517 pack_fds);
5518 if (err)
5519 goto done;
5521 blame->pack_fds = pack_fds;
5522 blame->cb_args.view = view;
5523 blame->cb_args.lines = blame->lines;
5524 blame->cb_args.nlines = blame->nlines;
5525 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5526 if (blame->cb_args.commit_id == NULL) {
5527 err = got_error_from_errno("got_object_id_dup");
5528 goto done;
5530 blame->cb_args.quit = &s->done;
5532 blame->thread_args.path = s->path;
5533 blame->thread_args.repo = thread_repo;
5534 blame->thread_args.cb_args = &blame->cb_args;
5535 blame->thread_args.complete = &s->blame_complete;
5536 blame->thread_args.cancel_cb = cancel_blame_view;
5537 blame->thread_args.cancel_arg = &s->done;
5538 s->blame_complete = 0;
5540 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5541 s->first_displayed_line = 1;
5542 s->last_displayed_line = view->nlines;
5543 s->selected_line = 1;
5545 s->matched_line = 0;
5547 done:
5548 if (commit)
5549 got_object_commit_close(commit);
5550 if (fd != -1 && close(fd) == -1 && err == NULL)
5551 err = got_error_from_errno("close");
5552 if (blob)
5553 got_object_blob_close(blob);
5554 free(obj_id);
5555 if (err)
5556 stop_blame(blame);
5557 return err;
5560 static const struct got_error *
5561 open_blame_view(struct tog_view *view, char *path,
5562 struct got_object_id *commit_id, struct got_repository *repo)
5564 const struct got_error *err = NULL;
5565 struct tog_blame_view_state *s = &view->state.blame;
5567 STAILQ_INIT(&s->blamed_commits);
5569 s->path = strdup(path);
5570 if (s->path == NULL)
5571 return got_error_from_errno("strdup");
5573 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5574 if (err) {
5575 free(s->path);
5576 return err;
5579 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5580 s->first_displayed_line = 1;
5581 s->last_displayed_line = view->nlines;
5582 s->selected_line = 1;
5583 s->blame_complete = 0;
5584 s->repo = repo;
5585 s->commit_id = commit_id;
5586 memset(&s->blame, 0, sizeof(s->blame));
5588 STAILQ_INIT(&s->colors);
5589 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5590 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5591 get_color_value("TOG_COLOR_COMMIT"));
5592 if (err)
5593 return err;
5596 view->show = show_blame_view;
5597 view->input = input_blame_view;
5598 view->reset = reset_blame_view;
5599 view->close = close_blame_view;
5600 view->search_start = search_start_blame_view;
5601 view->search_next = search_next_blame_view;
5603 return run_blame(view);
5606 static const struct got_error *
5607 close_blame_view(struct tog_view *view)
5609 const struct got_error *err = NULL;
5610 struct tog_blame_view_state *s = &view->state.blame;
5612 if (s->blame.thread)
5613 err = stop_blame(&s->blame);
5615 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5616 struct got_object_qid *blamed_commit;
5617 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5618 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5619 got_object_qid_free(blamed_commit);
5622 free(s->path);
5623 free_colors(&s->colors);
5624 return err;
5627 static const struct got_error *
5628 search_start_blame_view(struct tog_view *view)
5630 struct tog_blame_view_state *s = &view->state.blame;
5632 s->matched_line = 0;
5633 return NULL;
5636 static const struct got_error *
5637 search_next_blame_view(struct tog_view *view)
5639 struct tog_blame_view_state *s = &view->state.blame;
5640 const struct got_error *err = NULL;
5641 int lineno;
5642 char *line = NULL;
5643 size_t linesize = 0;
5644 ssize_t linelen;
5646 if (!view->searching) {
5647 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5648 return NULL;
5651 if (s->matched_line) {
5652 if (view->searching == TOG_SEARCH_FORWARD)
5653 lineno = s->matched_line + 1;
5654 else
5655 lineno = s->matched_line - 1;
5656 } else
5657 lineno = s->first_displayed_line - 1 + s->selected_line;
5659 while (1) {
5660 off_t offset;
5662 if (lineno <= 0 || lineno > s->blame.nlines) {
5663 if (s->matched_line == 0) {
5664 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5665 break;
5668 if (view->searching == TOG_SEARCH_FORWARD)
5669 lineno = 1;
5670 else
5671 lineno = s->blame.nlines;
5674 offset = s->blame.line_offsets[lineno - 1];
5675 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5676 free(line);
5677 return got_error_from_errno("fseeko");
5679 linelen = getline(&line, &linesize, s->blame.f);
5680 if (linelen != -1) {
5681 char *exstr;
5682 err = expand_tab(&exstr, line);
5683 if (err)
5684 break;
5685 if (match_line(exstr, &view->regex, 1,
5686 &view->regmatch)) {
5687 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5688 s->matched_line = lineno;
5689 free(exstr);
5690 break;
5692 free(exstr);
5694 if (view->searching == TOG_SEARCH_FORWARD)
5695 lineno++;
5696 else
5697 lineno--;
5699 free(line);
5701 if (s->matched_line) {
5702 s->first_displayed_line = s->matched_line;
5703 s->selected_line = 1;
5706 return err;
5709 static const struct got_error *
5710 show_blame_view(struct tog_view *view)
5712 const struct got_error *err = NULL;
5713 struct tog_blame_view_state *s = &view->state.blame;
5714 int errcode;
5716 if (s->blame.thread == 0 && !s->blame_complete) {
5717 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5718 &s->blame.thread_args);
5719 if (errcode)
5720 return got_error_set_errno(errcode, "pthread_create");
5722 halfdelay(1); /* fast refresh while annotating */
5725 if (s->blame_complete)
5726 halfdelay(10); /* disable fast refresh */
5728 err = draw_blame(view);
5730 view_border(view);
5731 return err;
5734 static const struct got_error *
5735 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5736 struct got_repository *repo, struct got_object_id *id)
5738 struct tog_view *log_view;
5739 const struct got_error *err = NULL;
5741 *new_view = NULL;
5743 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5744 if (log_view == NULL)
5745 return got_error_from_errno("view_open");
5747 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5748 if (err)
5749 view_close(log_view);
5750 else
5751 *new_view = log_view;
5753 return err;
5756 static const struct got_error *
5757 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5759 const struct got_error *err = NULL, *thread_err = NULL;
5760 struct tog_view *diff_view, *log_view;
5761 struct tog_blame_view_state *s = &view->state.blame;
5762 int eos, nscroll, begin_y = 0, begin_x = 0;
5764 eos = nscroll = view->nlines - 2;
5765 if (view_is_hsplit_top(view))
5766 --eos; /* border */
5768 switch (ch) {
5769 case '0':
5770 view->x = 0;
5771 break;
5772 case '$':
5773 view->x = MAX(view->maxx - view->ncols / 3, 0);
5774 view->count = 0;
5775 break;
5776 case KEY_RIGHT:
5777 case 'l':
5778 if (view->x + view->ncols / 3 < view->maxx)
5779 view->x += 2; /* move two columns right */
5780 else
5781 view->count = 0;
5782 break;
5783 case KEY_LEFT:
5784 case 'h':
5785 view->x -= MIN(view->x, 2); /* move two columns back */
5786 if (view->x <= 0)
5787 view->count = 0;
5788 break;
5789 case 'q':
5790 s->done = 1;
5791 break;
5792 case 'g':
5793 case KEY_HOME:
5794 s->selected_line = 1;
5795 s->first_displayed_line = 1;
5796 view->count = 0;
5797 break;
5798 case 'G':
5799 case KEY_END:
5800 if (s->blame.nlines < eos) {
5801 s->selected_line = s->blame.nlines;
5802 s->first_displayed_line = 1;
5803 } else {
5804 s->selected_line = eos;
5805 s->first_displayed_line = s->blame.nlines - (eos - 1);
5807 view->count = 0;
5808 break;
5809 case 'k':
5810 case KEY_UP:
5811 case CTRL('p'):
5812 if (s->selected_line > 1)
5813 s->selected_line--;
5814 else if (s->selected_line == 1 &&
5815 s->first_displayed_line > 1)
5816 s->first_displayed_line--;
5817 else
5818 view->count = 0;
5819 break;
5820 case CTRL('u'):
5821 case 'u':
5822 nscroll /= 2;
5823 /* FALL THROUGH */
5824 case KEY_PPAGE:
5825 case CTRL('b'):
5826 case 'b':
5827 if (s->first_displayed_line == 1) {
5828 if (view->count > 1)
5829 nscroll += nscroll;
5830 s->selected_line = MAX(1, s->selected_line - nscroll);
5831 view->count = 0;
5832 break;
5834 if (s->first_displayed_line > nscroll)
5835 s->first_displayed_line -= nscroll;
5836 else
5837 s->first_displayed_line = 1;
5838 break;
5839 case 'j':
5840 case KEY_DOWN:
5841 case CTRL('n'):
5842 if (s->selected_line < eos && s->first_displayed_line +
5843 s->selected_line <= s->blame.nlines)
5844 s->selected_line++;
5845 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5846 s->first_displayed_line++;
5847 else
5848 view->count = 0;
5849 break;
5850 case 'c':
5851 case 'p': {
5852 struct got_object_id *id = NULL;
5854 view->count = 0;
5855 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5856 s->first_displayed_line, s->selected_line);
5857 if (id == NULL)
5858 break;
5859 if (ch == 'p') {
5860 struct got_commit_object *commit, *pcommit;
5861 struct got_object_qid *pid;
5862 struct got_object_id *blob_id = NULL;
5863 int obj_type;
5864 err = got_object_open_as_commit(&commit,
5865 s->repo, id);
5866 if (err)
5867 break;
5868 pid = STAILQ_FIRST(
5869 got_object_commit_get_parent_ids(commit));
5870 if (pid == NULL) {
5871 got_object_commit_close(commit);
5872 break;
5874 /* Check if path history ends here. */
5875 err = got_object_open_as_commit(&pcommit,
5876 s->repo, &pid->id);
5877 if (err)
5878 break;
5879 err = got_object_id_by_path(&blob_id, s->repo,
5880 pcommit, s->path);
5881 got_object_commit_close(pcommit);
5882 if (err) {
5883 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5884 err = NULL;
5885 got_object_commit_close(commit);
5886 break;
5888 err = got_object_get_type(&obj_type, s->repo,
5889 blob_id);
5890 free(blob_id);
5891 /* Can't blame non-blob type objects. */
5892 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5893 got_object_commit_close(commit);
5894 break;
5896 err = got_object_qid_alloc(&s->blamed_commit,
5897 &pid->id);
5898 got_object_commit_close(commit);
5899 } else {
5900 if (got_object_id_cmp(id,
5901 &s->blamed_commit->id) == 0)
5902 break;
5903 err = got_object_qid_alloc(&s->blamed_commit,
5904 id);
5906 if (err)
5907 break;
5908 s->done = 1;
5909 thread_err = stop_blame(&s->blame);
5910 s->done = 0;
5911 if (thread_err)
5912 break;
5913 STAILQ_INSERT_HEAD(&s->blamed_commits,
5914 s->blamed_commit, entry);
5915 err = run_blame(view);
5916 if (err)
5917 break;
5918 break;
5920 case 'C': {
5921 struct got_object_qid *first;
5923 view->count = 0;
5924 first = STAILQ_FIRST(&s->blamed_commits);
5925 if (!got_object_id_cmp(&first->id, s->commit_id))
5926 break;
5927 s->done = 1;
5928 thread_err = stop_blame(&s->blame);
5929 s->done = 0;
5930 if (thread_err)
5931 break;
5932 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5933 got_object_qid_free(s->blamed_commit);
5934 s->blamed_commit =
5935 STAILQ_FIRST(&s->blamed_commits);
5936 err = run_blame(view);
5937 if (err)
5938 break;
5939 break;
5941 case 'L': {
5942 struct got_object_id *id = NULL;
5944 view->count = 0;
5945 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5946 s->first_displayed_line, s->selected_line);
5947 if (id == NULL)
5948 break;
5950 if (view_is_parent_view(view))
5951 view_get_split(view, &begin_y, &begin_x);
5952 err = log_annotated_line(&log_view, begin_y, begin_x,
5953 s->repo, id);
5954 if (err)
5955 break;
5956 if (view_is_parent_view(view) &&
5957 view->mode == TOG_VIEW_SPLIT_HRZN) {
5958 err = view_init_hsplit(view, begin_y);
5959 if (err)
5960 break;
5963 view->focussed = 0;
5964 log_view->focussed = 1;
5965 log_view->mode = view->mode;
5966 log_view->nlines = view->lines - begin_y;
5967 if (view_is_parent_view(view)) {
5968 view_transfer_size(log_view, view);
5969 err = view_close_child(view);
5970 if (err)
5971 return err;
5972 err = view_set_child(view, log_view);
5973 if (err)
5974 return err;
5975 view->focus_child = 1;
5976 } else
5977 *new_view = log_view;
5978 break;
5980 case KEY_ENTER:
5981 case '\r': {
5982 struct got_object_id *id = NULL;
5983 struct got_object_qid *pid;
5984 struct got_commit_object *commit = NULL;
5986 view->count = 0;
5987 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5988 s->first_displayed_line, s->selected_line);
5989 if (id == NULL)
5990 break;
5991 err = got_object_open_as_commit(&commit, s->repo, id);
5992 if (err)
5993 break;
5994 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5995 if (*new_view) {
5996 /* traversed from diff view, release diff resources */
5997 err = close_diff_view(*new_view);
5998 if (err)
5999 break;
6000 diff_view = *new_view;
6001 } else {
6002 if (view_is_parent_view(view))
6003 view_get_split(view, &begin_y, &begin_x);
6005 diff_view = view_open(0, 0, begin_y, begin_x,
6006 TOG_VIEW_DIFF);
6007 if (diff_view == NULL) {
6008 got_object_commit_close(commit);
6009 err = got_error_from_errno("view_open");
6010 break;
6013 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6014 id, NULL, NULL, 3, 0, 0, view, s->repo);
6015 got_object_commit_close(commit);
6016 if (err) {
6017 view_close(diff_view);
6018 break;
6020 s->last_diffed_line = s->first_displayed_line - 1 +
6021 s->selected_line;
6022 if (*new_view)
6023 break; /* still open from active diff view */
6024 if (view_is_parent_view(view) &&
6025 view->mode == TOG_VIEW_SPLIT_HRZN) {
6026 err = view_init_hsplit(view, begin_y);
6027 if (err)
6028 break;
6031 view->focussed = 0;
6032 diff_view->focussed = 1;
6033 diff_view->mode = view->mode;
6034 diff_view->nlines = view->lines - begin_y;
6035 if (view_is_parent_view(view)) {
6036 view_transfer_size(diff_view, view);
6037 err = view_close_child(view);
6038 if (err)
6039 break;
6040 err = view_set_child(view, diff_view);
6041 if (err)
6042 break;
6043 view->focus_child = 1;
6044 } else
6045 *new_view = diff_view;
6046 if (err)
6047 break;
6048 break;
6050 case CTRL('d'):
6051 case 'd':
6052 nscroll /= 2;
6053 /* FALL THROUGH */
6054 case KEY_NPAGE:
6055 case CTRL('f'):
6056 case 'f':
6057 case ' ':
6058 if (s->last_displayed_line >= s->blame.nlines &&
6059 s->selected_line >= MIN(s->blame.nlines,
6060 view->nlines - 2)) {
6061 view->count = 0;
6062 break;
6064 if (s->last_displayed_line >= s->blame.nlines &&
6065 s->selected_line < view->nlines - 2) {
6066 s->selected_line +=
6067 MIN(nscroll, s->last_displayed_line -
6068 s->first_displayed_line - s->selected_line + 1);
6070 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6071 s->first_displayed_line += nscroll;
6072 else
6073 s->first_displayed_line =
6074 s->blame.nlines - (view->nlines - 3);
6075 break;
6076 case KEY_RESIZE:
6077 if (s->selected_line > view->nlines - 2) {
6078 s->selected_line = MIN(s->blame.nlines,
6079 view->nlines - 2);
6081 break;
6082 default:
6083 view->count = 0;
6084 break;
6086 return thread_err ? thread_err : err;
6089 static const struct got_error *
6090 reset_blame_view(struct tog_view *view)
6092 const struct got_error *err;
6093 struct tog_blame_view_state *s = &view->state.blame;
6095 view->count = 0;
6096 s->done = 1;
6097 err = stop_blame(&s->blame);
6098 s->done = 0;
6099 if (err)
6100 return err;
6101 return run_blame(view);
6104 static const struct got_error *
6105 cmd_blame(int argc, char *argv[])
6107 const struct got_error *error;
6108 struct got_repository *repo = NULL;
6109 struct got_worktree *worktree = NULL;
6110 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6111 char *link_target = NULL;
6112 struct got_object_id *commit_id = NULL;
6113 struct got_commit_object *commit = NULL;
6114 char *commit_id_str = NULL;
6115 int ch;
6116 struct tog_view *view;
6117 int *pack_fds = NULL;
6119 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6120 switch (ch) {
6121 case 'c':
6122 commit_id_str = optarg;
6123 break;
6124 case 'r':
6125 repo_path = realpath(optarg, NULL);
6126 if (repo_path == NULL)
6127 return got_error_from_errno2("realpath",
6128 optarg);
6129 break;
6130 default:
6131 usage_blame();
6132 /* NOTREACHED */
6136 argc -= optind;
6137 argv += optind;
6139 if (argc != 1)
6140 usage_blame();
6142 error = got_repo_pack_fds_open(&pack_fds);
6143 if (error != NULL)
6144 goto done;
6146 if (repo_path == NULL) {
6147 cwd = getcwd(NULL, 0);
6148 if (cwd == NULL)
6149 return got_error_from_errno("getcwd");
6150 error = got_worktree_open(&worktree, cwd);
6151 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6152 goto done;
6153 if (worktree)
6154 repo_path =
6155 strdup(got_worktree_get_repo_path(worktree));
6156 else
6157 repo_path = strdup(cwd);
6158 if (repo_path == NULL) {
6159 error = got_error_from_errno("strdup");
6160 goto done;
6164 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6165 if (error != NULL)
6166 goto done;
6168 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6169 worktree);
6170 if (error)
6171 goto done;
6173 init_curses();
6175 error = apply_unveil(got_repo_get_path(repo), NULL);
6176 if (error)
6177 goto done;
6179 error = tog_load_refs(repo, 0);
6180 if (error)
6181 goto done;
6183 if (commit_id_str == NULL) {
6184 struct got_reference *head_ref;
6185 error = got_ref_open(&head_ref, repo, worktree ?
6186 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6187 if (error != NULL)
6188 goto done;
6189 error = got_ref_resolve(&commit_id, repo, head_ref);
6190 got_ref_close(head_ref);
6191 } else {
6192 error = got_repo_match_object_id(&commit_id, NULL,
6193 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6195 if (error != NULL)
6196 goto done;
6198 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6199 if (view == NULL) {
6200 error = got_error_from_errno("view_open");
6201 goto done;
6204 error = got_object_open_as_commit(&commit, repo, commit_id);
6205 if (error)
6206 goto done;
6208 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6209 commit, repo);
6210 if (error)
6211 goto done;
6213 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6214 commit_id, repo);
6215 if (error)
6216 goto done;
6217 if (worktree) {
6218 /* Release work tree lock. */
6219 got_worktree_close(worktree);
6220 worktree = NULL;
6222 error = view_loop(view);
6223 done:
6224 free(repo_path);
6225 free(in_repo_path);
6226 free(link_target);
6227 free(cwd);
6228 free(commit_id);
6229 if (commit)
6230 got_object_commit_close(commit);
6231 if (worktree)
6232 got_worktree_close(worktree);
6233 if (repo) {
6234 const struct got_error *close_err = got_repo_close(repo);
6235 if (error == NULL)
6236 error = close_err;
6238 if (pack_fds) {
6239 const struct got_error *pack_err =
6240 got_repo_pack_fds_close(pack_fds);
6241 if (error == NULL)
6242 error = pack_err;
6244 tog_free_refs();
6245 return error;
6248 static const struct got_error *
6249 draw_tree_entries(struct tog_view *view, const char *parent_path)
6251 struct tog_tree_view_state *s = &view->state.tree;
6252 const struct got_error *err = NULL;
6253 struct got_tree_entry *te;
6254 wchar_t *wline;
6255 struct tog_color *tc;
6256 int width, n, i, nentries;
6257 int limit = view->nlines;
6259 s->ndisplayed = 0;
6260 if (view_is_hsplit_top(view))
6261 --limit; /* border */
6263 werase(view->window);
6265 if (limit == 0)
6266 return NULL;
6268 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6269 0, 0);
6270 if (err)
6271 return err;
6272 if (view_needs_focus_indication(view))
6273 wstandout(view->window);
6274 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6275 if (tc)
6276 wattr_on(view->window,
6277 COLOR_PAIR(tc->colorpair), NULL);
6278 waddwstr(view->window, wline);
6279 if (tc)
6280 wattr_off(view->window,
6281 COLOR_PAIR(tc->colorpair), NULL);
6282 if (view_needs_focus_indication(view))
6283 wstandend(view->window);
6284 free(wline);
6285 wline = NULL;
6286 if (width < view->ncols - 1)
6287 waddch(view->window, '\n');
6288 if (--limit <= 0)
6289 return NULL;
6290 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6291 0, 0);
6292 if (err)
6293 return err;
6294 waddwstr(view->window, wline);
6295 free(wline);
6296 wline = NULL;
6297 if (width < view->ncols - 1)
6298 waddch(view->window, '\n');
6299 if (--limit <= 0)
6300 return NULL;
6301 waddch(view->window, '\n');
6302 if (--limit <= 0)
6303 return NULL;
6305 if (s->first_displayed_entry == NULL) {
6306 te = got_object_tree_get_first_entry(s->tree);
6307 if (s->selected == 0) {
6308 if (view->focussed)
6309 wstandout(view->window);
6310 s->selected_entry = NULL;
6312 waddstr(view->window, " ..\n"); /* parent directory */
6313 if (s->selected == 0 && view->focussed)
6314 wstandend(view->window);
6315 s->ndisplayed++;
6316 if (--limit <= 0)
6317 return NULL;
6318 n = 1;
6319 } else {
6320 n = 0;
6321 te = s->first_displayed_entry;
6324 nentries = got_object_tree_get_nentries(s->tree);
6325 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6326 char *line = NULL, *id_str = NULL, *link_target = NULL;
6327 const char *modestr = "";
6328 mode_t mode;
6330 te = got_object_tree_get_entry(s->tree, i);
6331 mode = got_tree_entry_get_mode(te);
6333 if (s->show_ids) {
6334 err = got_object_id_str(&id_str,
6335 got_tree_entry_get_id(te));
6336 if (err)
6337 return got_error_from_errno(
6338 "got_object_id_str");
6340 if (got_object_tree_entry_is_submodule(te))
6341 modestr = "$";
6342 else if (S_ISLNK(mode)) {
6343 int i;
6345 err = got_tree_entry_get_symlink_target(&link_target,
6346 te, s->repo);
6347 if (err) {
6348 free(id_str);
6349 return err;
6351 for (i = 0; i < strlen(link_target); i++) {
6352 if (!isprint((unsigned char)link_target[i]))
6353 link_target[i] = '?';
6355 modestr = "@";
6357 else if (S_ISDIR(mode))
6358 modestr = "/";
6359 else if (mode & S_IXUSR)
6360 modestr = "*";
6361 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6362 got_tree_entry_get_name(te), modestr,
6363 link_target ? " -> ": "",
6364 link_target ? link_target : "") == -1) {
6365 free(id_str);
6366 free(link_target);
6367 return got_error_from_errno("asprintf");
6369 free(id_str);
6370 free(link_target);
6371 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6372 0, 0);
6373 if (err) {
6374 free(line);
6375 break;
6377 if (n == s->selected) {
6378 if (view->focussed)
6379 wstandout(view->window);
6380 s->selected_entry = te;
6382 tc = match_color(&s->colors, line);
6383 if (tc)
6384 wattr_on(view->window,
6385 COLOR_PAIR(tc->colorpair), NULL);
6386 waddwstr(view->window, wline);
6387 if (tc)
6388 wattr_off(view->window,
6389 COLOR_PAIR(tc->colorpair), NULL);
6390 if (width < view->ncols - 1)
6391 waddch(view->window, '\n');
6392 if (n == s->selected && view->focussed)
6393 wstandend(view->window);
6394 free(line);
6395 free(wline);
6396 wline = NULL;
6397 n++;
6398 s->ndisplayed++;
6399 s->last_displayed_entry = te;
6400 if (--limit <= 0)
6401 break;
6404 return err;
6407 static void
6408 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6410 struct got_tree_entry *te;
6411 int isroot = s->tree == s->root;
6412 int i = 0;
6414 if (s->first_displayed_entry == NULL)
6415 return;
6417 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6418 while (i++ < maxscroll) {
6419 if (te == NULL) {
6420 if (!isroot)
6421 s->first_displayed_entry = NULL;
6422 break;
6424 s->first_displayed_entry = te;
6425 te = got_tree_entry_get_prev(s->tree, te);
6429 static const struct got_error *
6430 tree_scroll_down(struct tog_view *view, int maxscroll)
6432 struct tog_tree_view_state *s = &view->state.tree;
6433 struct got_tree_entry *next, *last;
6434 int n = 0;
6436 if (s->first_displayed_entry)
6437 next = got_tree_entry_get_next(s->tree,
6438 s->first_displayed_entry);
6439 else
6440 next = got_object_tree_get_first_entry(s->tree);
6442 last = s->last_displayed_entry;
6443 while (next && n++ < maxscroll) {
6444 if (last)
6445 last = got_tree_entry_get_next(s->tree, last);
6446 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6447 s->first_displayed_entry = next;
6448 next = got_tree_entry_get_next(s->tree, next);
6452 return NULL;
6455 static const struct got_error *
6456 tree_entry_path(char **path, struct tog_parent_trees *parents,
6457 struct got_tree_entry *te)
6459 const struct got_error *err = NULL;
6460 struct tog_parent_tree *pt;
6461 size_t len = 2; /* for leading slash and NUL */
6463 TAILQ_FOREACH(pt, parents, entry)
6464 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6465 + 1 /* slash */;
6466 if (te)
6467 len += strlen(got_tree_entry_get_name(te));
6469 *path = calloc(1, len);
6470 if (path == NULL)
6471 return got_error_from_errno("calloc");
6473 (*path)[0] = '/';
6474 pt = TAILQ_LAST(parents, tog_parent_trees);
6475 while (pt) {
6476 const char *name = got_tree_entry_get_name(pt->selected_entry);
6477 if (strlcat(*path, name, len) >= len) {
6478 err = got_error(GOT_ERR_NO_SPACE);
6479 goto done;
6481 if (strlcat(*path, "/", len) >= len) {
6482 err = got_error(GOT_ERR_NO_SPACE);
6483 goto done;
6485 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6487 if (te) {
6488 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6489 err = got_error(GOT_ERR_NO_SPACE);
6490 goto done;
6493 done:
6494 if (err) {
6495 free(*path);
6496 *path = NULL;
6498 return err;
6501 static const struct got_error *
6502 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6503 struct got_tree_entry *te, struct tog_parent_trees *parents,
6504 struct got_object_id *commit_id, struct got_repository *repo)
6506 const struct got_error *err = NULL;
6507 char *path;
6508 struct tog_view *blame_view;
6510 *new_view = NULL;
6512 err = tree_entry_path(&path, parents, te);
6513 if (err)
6514 return err;
6516 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6517 if (blame_view == NULL) {
6518 err = got_error_from_errno("view_open");
6519 goto done;
6522 err = open_blame_view(blame_view, path, commit_id, repo);
6523 if (err) {
6524 if (err->code == GOT_ERR_CANCELLED)
6525 err = NULL;
6526 view_close(blame_view);
6527 } else
6528 *new_view = blame_view;
6529 done:
6530 free(path);
6531 return err;
6534 static const struct got_error *
6535 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6536 struct tog_tree_view_state *s)
6538 struct tog_view *log_view;
6539 const struct got_error *err = NULL;
6540 char *path;
6542 *new_view = NULL;
6544 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6545 if (log_view == NULL)
6546 return got_error_from_errno("view_open");
6548 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6549 if (err)
6550 return err;
6552 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6553 path, 0);
6554 if (err)
6555 view_close(log_view);
6556 else
6557 *new_view = log_view;
6558 free(path);
6559 return err;
6562 static const struct got_error *
6563 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6564 const char *head_ref_name, struct got_repository *repo)
6566 const struct got_error *err = NULL;
6567 char *commit_id_str = NULL;
6568 struct tog_tree_view_state *s = &view->state.tree;
6569 struct got_commit_object *commit = NULL;
6571 TAILQ_INIT(&s->parents);
6572 STAILQ_INIT(&s->colors);
6574 s->commit_id = got_object_id_dup(commit_id);
6575 if (s->commit_id == NULL)
6576 return got_error_from_errno("got_object_id_dup");
6578 err = got_object_open_as_commit(&commit, repo, commit_id);
6579 if (err)
6580 goto done;
6583 * The root is opened here and will be closed when the view is closed.
6584 * Any visited subtrees and their path-wise parents are opened and
6585 * closed on demand.
6587 err = got_object_open_as_tree(&s->root, repo,
6588 got_object_commit_get_tree_id(commit));
6589 if (err)
6590 goto done;
6591 s->tree = s->root;
6593 err = got_object_id_str(&commit_id_str, commit_id);
6594 if (err != NULL)
6595 goto done;
6597 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6598 err = got_error_from_errno("asprintf");
6599 goto done;
6602 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6603 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6604 if (head_ref_name) {
6605 s->head_ref_name = strdup(head_ref_name);
6606 if (s->head_ref_name == NULL) {
6607 err = got_error_from_errno("strdup");
6608 goto done;
6611 s->repo = repo;
6613 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6614 err = add_color(&s->colors, "\\$$",
6615 TOG_COLOR_TREE_SUBMODULE,
6616 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6617 if (err)
6618 goto done;
6619 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6620 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6621 if (err)
6622 goto done;
6623 err = add_color(&s->colors, "/$",
6624 TOG_COLOR_TREE_DIRECTORY,
6625 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6626 if (err)
6627 goto done;
6629 err = add_color(&s->colors, "\\*$",
6630 TOG_COLOR_TREE_EXECUTABLE,
6631 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6632 if (err)
6633 goto done;
6635 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6636 get_color_value("TOG_COLOR_COMMIT"));
6637 if (err)
6638 goto done;
6641 view->show = show_tree_view;
6642 view->input = input_tree_view;
6643 view->close = close_tree_view;
6644 view->search_start = search_start_tree_view;
6645 view->search_next = search_next_tree_view;
6646 done:
6647 free(commit_id_str);
6648 if (commit)
6649 got_object_commit_close(commit);
6650 if (err)
6651 close_tree_view(view);
6652 return err;
6655 static const struct got_error *
6656 close_tree_view(struct tog_view *view)
6658 struct tog_tree_view_state *s = &view->state.tree;
6660 free_colors(&s->colors);
6661 free(s->tree_label);
6662 s->tree_label = NULL;
6663 free(s->commit_id);
6664 s->commit_id = NULL;
6665 free(s->head_ref_name);
6666 s->head_ref_name = NULL;
6667 while (!TAILQ_EMPTY(&s->parents)) {
6668 struct tog_parent_tree *parent;
6669 parent = TAILQ_FIRST(&s->parents);
6670 TAILQ_REMOVE(&s->parents, parent, entry);
6671 if (parent->tree != s->root)
6672 got_object_tree_close(parent->tree);
6673 free(parent);
6676 if (s->tree != NULL && s->tree != s->root)
6677 got_object_tree_close(s->tree);
6678 if (s->root)
6679 got_object_tree_close(s->root);
6680 return NULL;
6683 static const struct got_error *
6684 search_start_tree_view(struct tog_view *view)
6686 struct tog_tree_view_state *s = &view->state.tree;
6688 s->matched_entry = NULL;
6689 return NULL;
6692 static int
6693 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6695 regmatch_t regmatch;
6697 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6698 0) == 0;
6701 static const struct got_error *
6702 search_next_tree_view(struct tog_view *view)
6704 struct tog_tree_view_state *s = &view->state.tree;
6705 struct got_tree_entry *te = NULL;
6707 if (!view->searching) {
6708 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6709 return NULL;
6712 if (s->matched_entry) {
6713 if (view->searching == TOG_SEARCH_FORWARD) {
6714 if (s->selected_entry)
6715 te = got_tree_entry_get_next(s->tree,
6716 s->selected_entry);
6717 else
6718 te = got_object_tree_get_first_entry(s->tree);
6719 } else {
6720 if (s->selected_entry == NULL)
6721 te = got_object_tree_get_last_entry(s->tree);
6722 else
6723 te = got_tree_entry_get_prev(s->tree,
6724 s->selected_entry);
6726 } else {
6727 if (s->selected_entry)
6728 te = s->selected_entry;
6729 else if (view->searching == TOG_SEARCH_FORWARD)
6730 te = got_object_tree_get_first_entry(s->tree);
6731 else
6732 te = got_object_tree_get_last_entry(s->tree);
6735 while (1) {
6736 if (te == NULL) {
6737 if (s->matched_entry == NULL) {
6738 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6739 return NULL;
6741 if (view->searching == TOG_SEARCH_FORWARD)
6742 te = got_object_tree_get_first_entry(s->tree);
6743 else
6744 te = got_object_tree_get_last_entry(s->tree);
6747 if (match_tree_entry(te, &view->regex)) {
6748 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6749 s->matched_entry = te;
6750 break;
6753 if (view->searching == TOG_SEARCH_FORWARD)
6754 te = got_tree_entry_get_next(s->tree, te);
6755 else
6756 te = got_tree_entry_get_prev(s->tree, te);
6759 if (s->matched_entry) {
6760 s->first_displayed_entry = s->matched_entry;
6761 s->selected = 0;
6764 return NULL;
6767 static const struct got_error *
6768 show_tree_view(struct tog_view *view)
6770 const struct got_error *err = NULL;
6771 struct tog_tree_view_state *s = &view->state.tree;
6772 char *parent_path;
6774 err = tree_entry_path(&parent_path, &s->parents, NULL);
6775 if (err)
6776 return err;
6778 err = draw_tree_entries(view, parent_path);
6779 free(parent_path);
6781 view_border(view);
6782 return err;
6785 static const struct got_error *
6786 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6788 const struct got_error *err = NULL;
6789 struct tog_tree_view_state *s = &view->state.tree;
6790 struct tog_view *log_view, *ref_view;
6791 struct got_tree_entry *te;
6792 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6794 switch (ch) {
6795 case 'i':
6796 s->show_ids = !s->show_ids;
6797 view->count = 0;
6798 break;
6799 case 'l':
6800 view->count = 0;
6801 if (!s->selected_entry)
6802 break;
6803 if (view_is_parent_view(view))
6804 view_get_split(view, &begin_y, &begin_x);
6805 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6806 if (view_is_parent_view(view) &&
6807 view->mode == TOG_VIEW_SPLIT_HRZN) {
6808 err = view_init_hsplit(view, begin_y);
6809 if (err)
6810 break;
6812 view->focussed = 0;
6813 log_view->focussed = 1;
6814 log_view->mode = view->mode;
6815 log_view->nlines = view->lines - begin_y;
6816 if (view_is_parent_view(view)) {
6817 view_transfer_size(log_view, view);
6818 err = view_close_child(view);
6819 if (err)
6820 return err;
6821 err = view_set_child(view, log_view);
6822 if (err)
6823 return err;
6824 view->focus_child = 1;
6825 } else
6826 *new_view = log_view;
6827 break;
6828 case 'r':
6829 view->count = 0;
6830 if (view_is_parent_view(view))
6831 view_get_split(view, &begin_y, &begin_x);
6832 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6833 if (ref_view == NULL)
6834 return got_error_from_errno("view_open");
6835 err = open_ref_view(ref_view, s->repo);
6836 if (err) {
6837 view_close(ref_view);
6838 return err;
6840 if (view_is_parent_view(view) &&
6841 view->mode == TOG_VIEW_SPLIT_HRZN) {
6842 err = view_init_hsplit(view, begin_y);
6843 if (err)
6844 break;
6846 view->focussed = 0;
6847 ref_view->focussed = 1;
6848 ref_view->mode = view->mode;
6849 ref_view->nlines = view->lines - begin_y;
6850 if (view_is_parent_view(view)) {
6851 view_transfer_size(ref_view, view);
6852 err = view_close_child(view);
6853 if (err)
6854 return err;
6855 err = view_set_child(view, ref_view);
6856 if (err)
6857 return err;
6858 view->focus_child = 1;
6859 } else
6860 *new_view = ref_view;
6861 break;
6862 case 'g':
6863 case KEY_HOME:
6864 s->selected = 0;
6865 view->count = 0;
6866 if (s->tree == s->root)
6867 s->first_displayed_entry =
6868 got_object_tree_get_first_entry(s->tree);
6869 else
6870 s->first_displayed_entry = NULL;
6871 break;
6872 case 'G':
6873 case KEY_END: {
6874 int eos = view->nlines - 3;
6876 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6877 --eos; /* border */
6878 s->selected = 0;
6879 view->count = 0;
6880 te = got_object_tree_get_last_entry(s->tree);
6881 for (n = 0; n < eos; n++) {
6882 if (te == NULL) {
6883 if (s->tree != s->root) {
6884 s->first_displayed_entry = NULL;
6885 n++;
6887 break;
6889 s->first_displayed_entry = te;
6890 te = got_tree_entry_get_prev(s->tree, te);
6892 if (n > 0)
6893 s->selected = n - 1;
6894 break;
6896 case 'k':
6897 case KEY_UP:
6898 case CTRL('p'):
6899 if (s->selected > 0) {
6900 s->selected--;
6901 break;
6903 tree_scroll_up(s, 1);
6904 if (s->selected_entry == NULL ||
6905 (s->tree == s->root && s->selected_entry ==
6906 got_object_tree_get_first_entry(s->tree)))
6907 view->count = 0;
6908 break;
6909 case CTRL('u'):
6910 case 'u':
6911 nscroll /= 2;
6912 /* FALL THROUGH */
6913 case KEY_PPAGE:
6914 case CTRL('b'):
6915 case 'b':
6916 if (s->tree == s->root) {
6917 if (got_object_tree_get_first_entry(s->tree) ==
6918 s->first_displayed_entry)
6919 s->selected -= MIN(s->selected, nscroll);
6920 } else {
6921 if (s->first_displayed_entry == NULL)
6922 s->selected -= MIN(s->selected, nscroll);
6924 tree_scroll_up(s, MAX(0, nscroll));
6925 if (s->selected_entry == NULL ||
6926 (s->tree == s->root && s->selected_entry ==
6927 got_object_tree_get_first_entry(s->tree)))
6928 view->count = 0;
6929 break;
6930 case 'j':
6931 case KEY_DOWN:
6932 case CTRL('n'):
6933 if (s->selected < s->ndisplayed - 1) {
6934 s->selected++;
6935 break;
6937 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6938 == NULL) {
6939 /* can't scroll any further */
6940 view->count = 0;
6941 break;
6943 tree_scroll_down(view, 1);
6944 break;
6945 case CTRL('d'):
6946 case 'd':
6947 nscroll /= 2;
6948 /* FALL THROUGH */
6949 case KEY_NPAGE:
6950 case CTRL('f'):
6951 case 'f':
6952 case ' ':
6953 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6954 == NULL) {
6955 /* can't scroll any further; move cursor down */
6956 if (s->selected < s->ndisplayed - 1)
6957 s->selected += MIN(nscroll,
6958 s->ndisplayed - s->selected - 1);
6959 else
6960 view->count = 0;
6961 break;
6963 tree_scroll_down(view, nscroll);
6964 break;
6965 case KEY_ENTER:
6966 case '\r':
6967 case KEY_BACKSPACE:
6968 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6969 struct tog_parent_tree *parent;
6970 /* user selected '..' */
6971 if (s->tree == s->root) {
6972 view->count = 0;
6973 break;
6975 parent = TAILQ_FIRST(&s->parents);
6976 TAILQ_REMOVE(&s->parents, parent,
6977 entry);
6978 got_object_tree_close(s->tree);
6979 s->tree = parent->tree;
6980 s->first_displayed_entry =
6981 parent->first_displayed_entry;
6982 s->selected_entry =
6983 parent->selected_entry;
6984 s->selected = parent->selected;
6985 if (s->selected > view->nlines - 3) {
6986 err = offset_selection_down(view);
6987 if (err)
6988 break;
6990 free(parent);
6991 } else if (S_ISDIR(got_tree_entry_get_mode(
6992 s->selected_entry))) {
6993 struct got_tree_object *subtree;
6994 view->count = 0;
6995 err = got_object_open_as_tree(&subtree, s->repo,
6996 got_tree_entry_get_id(s->selected_entry));
6997 if (err)
6998 break;
6999 err = tree_view_visit_subtree(s, subtree);
7000 if (err) {
7001 got_object_tree_close(subtree);
7002 break;
7004 } else if (S_ISREG(got_tree_entry_get_mode(
7005 s->selected_entry))) {
7006 struct tog_view *blame_view;
7007 int begin_x = 0, begin_y = 0;
7009 if (view_is_parent_view(view))
7010 view_get_split(view, &begin_y, &begin_x);
7012 err = blame_tree_entry(&blame_view, begin_y, begin_x,
7013 s->selected_entry, &s->parents,
7014 s->commit_id, s->repo);
7015 if (err)
7016 break;
7018 if (view_is_parent_view(view) &&
7019 view->mode == TOG_VIEW_SPLIT_HRZN) {
7020 err = view_init_hsplit(view, begin_y);
7021 if (err)
7022 break;
7025 view->count = 0;
7026 view->focussed = 0;
7027 blame_view->focussed = 1;
7028 blame_view->mode = view->mode;
7029 blame_view->nlines = view->lines - begin_y;
7030 if (view_is_parent_view(view)) {
7031 view_transfer_size(blame_view, view);
7032 err = view_close_child(view);
7033 if (err)
7034 return err;
7035 err = view_set_child(view, blame_view);
7036 if (err)
7037 return err;
7038 view->focus_child = 1;
7039 } else
7040 *new_view = blame_view;
7042 break;
7043 case KEY_RESIZE:
7044 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7045 s->selected = view->nlines - 4;
7046 view->count = 0;
7047 break;
7048 default:
7049 view->count = 0;
7050 break;
7053 return err;
7056 __dead static void
7057 usage_tree(void)
7059 endwin();
7060 fprintf(stderr,
7061 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7062 getprogname());
7063 exit(1);
7066 static const struct got_error *
7067 cmd_tree(int argc, char *argv[])
7069 const struct got_error *error;
7070 struct got_repository *repo = NULL;
7071 struct got_worktree *worktree = NULL;
7072 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7073 struct got_object_id *commit_id = NULL;
7074 struct got_commit_object *commit = NULL;
7075 const char *commit_id_arg = NULL;
7076 char *label = NULL;
7077 struct got_reference *ref = NULL;
7078 const char *head_ref_name = NULL;
7079 int ch;
7080 struct tog_view *view;
7081 int *pack_fds = NULL;
7083 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7084 switch (ch) {
7085 case 'c':
7086 commit_id_arg = optarg;
7087 break;
7088 case 'r':
7089 repo_path = realpath(optarg, NULL);
7090 if (repo_path == NULL)
7091 return got_error_from_errno2("realpath",
7092 optarg);
7093 break;
7094 default:
7095 usage_tree();
7096 /* NOTREACHED */
7100 argc -= optind;
7101 argv += optind;
7103 if (argc > 1)
7104 usage_tree();
7106 error = got_repo_pack_fds_open(&pack_fds);
7107 if (error != NULL)
7108 goto done;
7110 if (repo_path == NULL) {
7111 cwd = getcwd(NULL, 0);
7112 if (cwd == NULL)
7113 return got_error_from_errno("getcwd");
7114 error = got_worktree_open(&worktree, cwd);
7115 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7116 goto done;
7117 if (worktree)
7118 repo_path =
7119 strdup(got_worktree_get_repo_path(worktree));
7120 else
7121 repo_path = strdup(cwd);
7122 if (repo_path == NULL) {
7123 error = got_error_from_errno("strdup");
7124 goto done;
7128 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7129 if (error != NULL)
7130 goto done;
7132 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7133 repo, worktree);
7134 if (error)
7135 goto done;
7137 init_curses();
7139 error = apply_unveil(got_repo_get_path(repo), NULL);
7140 if (error)
7141 goto done;
7143 error = tog_load_refs(repo, 0);
7144 if (error)
7145 goto done;
7147 if (commit_id_arg == NULL) {
7148 error = got_repo_match_object_id(&commit_id, &label,
7149 worktree ? got_worktree_get_head_ref_name(worktree) :
7150 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7151 if (error)
7152 goto done;
7153 head_ref_name = label;
7154 } else {
7155 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7156 if (error == NULL)
7157 head_ref_name = got_ref_get_name(ref);
7158 else if (error->code != GOT_ERR_NOT_REF)
7159 goto done;
7160 error = got_repo_match_object_id(&commit_id, NULL,
7161 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7162 if (error)
7163 goto done;
7166 error = got_object_open_as_commit(&commit, repo, commit_id);
7167 if (error)
7168 goto done;
7170 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7171 if (view == NULL) {
7172 error = got_error_from_errno("view_open");
7173 goto done;
7175 error = open_tree_view(view, commit_id, head_ref_name, repo);
7176 if (error)
7177 goto done;
7178 if (!got_path_is_root_dir(in_repo_path)) {
7179 error = tree_view_walk_path(&view->state.tree, commit,
7180 in_repo_path);
7181 if (error)
7182 goto done;
7185 if (worktree) {
7186 /* Release work tree lock. */
7187 got_worktree_close(worktree);
7188 worktree = NULL;
7190 error = view_loop(view);
7191 done:
7192 free(repo_path);
7193 free(cwd);
7194 free(commit_id);
7195 free(label);
7196 if (ref)
7197 got_ref_close(ref);
7198 if (repo) {
7199 const struct got_error *close_err = got_repo_close(repo);
7200 if (error == NULL)
7201 error = close_err;
7203 if (pack_fds) {
7204 const struct got_error *pack_err =
7205 got_repo_pack_fds_close(pack_fds);
7206 if (error == NULL)
7207 error = pack_err;
7209 tog_free_refs();
7210 return error;
7213 static const struct got_error *
7214 ref_view_load_refs(struct tog_ref_view_state *s)
7216 struct got_reflist_entry *sre;
7217 struct tog_reflist_entry *re;
7219 s->nrefs = 0;
7220 TAILQ_FOREACH(sre, &tog_refs, entry) {
7221 if (strncmp(got_ref_get_name(sre->ref),
7222 "refs/got/", 9) == 0 &&
7223 strncmp(got_ref_get_name(sre->ref),
7224 "refs/got/backup/", 16) != 0)
7225 continue;
7227 re = malloc(sizeof(*re));
7228 if (re == NULL)
7229 return got_error_from_errno("malloc");
7231 re->ref = got_ref_dup(sre->ref);
7232 if (re->ref == NULL)
7233 return got_error_from_errno("got_ref_dup");
7234 re->idx = s->nrefs++;
7235 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7238 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7239 return NULL;
7242 static void
7243 ref_view_free_refs(struct tog_ref_view_state *s)
7245 struct tog_reflist_entry *re;
7247 while (!TAILQ_EMPTY(&s->refs)) {
7248 re = TAILQ_FIRST(&s->refs);
7249 TAILQ_REMOVE(&s->refs, re, entry);
7250 got_ref_close(re->ref);
7251 free(re);
7255 static const struct got_error *
7256 open_ref_view(struct tog_view *view, struct got_repository *repo)
7258 const struct got_error *err = NULL;
7259 struct tog_ref_view_state *s = &view->state.ref;
7261 s->selected_entry = 0;
7262 s->repo = repo;
7264 TAILQ_INIT(&s->refs);
7265 STAILQ_INIT(&s->colors);
7267 err = ref_view_load_refs(s);
7268 if (err)
7269 return err;
7271 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7272 err = add_color(&s->colors, "^refs/heads/",
7273 TOG_COLOR_REFS_HEADS,
7274 get_color_value("TOG_COLOR_REFS_HEADS"));
7275 if (err)
7276 goto done;
7278 err = add_color(&s->colors, "^refs/tags/",
7279 TOG_COLOR_REFS_TAGS,
7280 get_color_value("TOG_COLOR_REFS_TAGS"));
7281 if (err)
7282 goto done;
7284 err = add_color(&s->colors, "^refs/remotes/",
7285 TOG_COLOR_REFS_REMOTES,
7286 get_color_value("TOG_COLOR_REFS_REMOTES"));
7287 if (err)
7288 goto done;
7290 err = add_color(&s->colors, "^refs/got/backup/",
7291 TOG_COLOR_REFS_BACKUP,
7292 get_color_value("TOG_COLOR_REFS_BACKUP"));
7293 if (err)
7294 goto done;
7297 view->show = show_ref_view;
7298 view->input = input_ref_view;
7299 view->close = close_ref_view;
7300 view->search_start = search_start_ref_view;
7301 view->search_next = search_next_ref_view;
7302 done:
7303 if (err)
7304 free_colors(&s->colors);
7305 return err;
7308 static const struct got_error *
7309 close_ref_view(struct tog_view *view)
7311 struct tog_ref_view_state *s = &view->state.ref;
7313 ref_view_free_refs(s);
7314 free_colors(&s->colors);
7316 return NULL;
7319 static const struct got_error *
7320 resolve_reflist_entry(struct got_object_id **commit_id,
7321 struct tog_reflist_entry *re, struct got_repository *repo)
7323 const struct got_error *err = NULL;
7324 struct got_object_id *obj_id;
7325 struct got_tag_object *tag = NULL;
7326 int obj_type;
7328 *commit_id = NULL;
7330 err = got_ref_resolve(&obj_id, repo, re->ref);
7331 if (err)
7332 return err;
7334 err = got_object_get_type(&obj_type, repo, obj_id);
7335 if (err)
7336 goto done;
7338 switch (obj_type) {
7339 case GOT_OBJ_TYPE_COMMIT:
7340 *commit_id = obj_id;
7341 break;
7342 case GOT_OBJ_TYPE_TAG:
7343 err = got_object_open_as_tag(&tag, repo, obj_id);
7344 if (err)
7345 goto done;
7346 free(obj_id);
7347 err = got_object_get_type(&obj_type, repo,
7348 got_object_tag_get_object_id(tag));
7349 if (err)
7350 goto done;
7351 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7352 err = got_error(GOT_ERR_OBJ_TYPE);
7353 goto done;
7355 *commit_id = got_object_id_dup(
7356 got_object_tag_get_object_id(tag));
7357 if (*commit_id == NULL) {
7358 err = got_error_from_errno("got_object_id_dup");
7359 goto done;
7361 break;
7362 default:
7363 err = got_error(GOT_ERR_OBJ_TYPE);
7364 break;
7367 done:
7368 if (tag)
7369 got_object_tag_close(tag);
7370 if (err) {
7371 free(*commit_id);
7372 *commit_id = NULL;
7374 return err;
7377 static const struct got_error *
7378 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7379 struct tog_reflist_entry *re, struct got_repository *repo)
7381 struct tog_view *log_view;
7382 const struct got_error *err = NULL;
7383 struct got_object_id *commit_id = NULL;
7385 *new_view = NULL;
7387 err = resolve_reflist_entry(&commit_id, re, repo);
7388 if (err) {
7389 if (err->code != GOT_ERR_OBJ_TYPE)
7390 return err;
7391 else
7392 return NULL;
7395 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7396 if (log_view == NULL) {
7397 err = got_error_from_errno("view_open");
7398 goto done;
7401 err = open_log_view(log_view, commit_id, repo,
7402 got_ref_get_name(re->ref), "", 0);
7403 done:
7404 if (err)
7405 view_close(log_view);
7406 else
7407 *new_view = log_view;
7408 free(commit_id);
7409 return err;
7412 static void
7413 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7415 struct tog_reflist_entry *re;
7416 int i = 0;
7418 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7419 return;
7421 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7422 while (i++ < maxscroll) {
7423 if (re == NULL)
7424 break;
7425 s->first_displayed_entry = re;
7426 re = TAILQ_PREV(re, tog_reflist_head, entry);
7430 static const struct got_error *
7431 ref_scroll_down(struct tog_view *view, int maxscroll)
7433 struct tog_ref_view_state *s = &view->state.ref;
7434 struct tog_reflist_entry *next, *last;
7435 int n = 0;
7437 if (s->first_displayed_entry)
7438 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7439 else
7440 next = TAILQ_FIRST(&s->refs);
7442 last = s->last_displayed_entry;
7443 while (next && n++ < maxscroll) {
7444 if (last)
7445 last = TAILQ_NEXT(last, entry);
7446 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7447 s->first_displayed_entry = next;
7448 next = TAILQ_NEXT(next, entry);
7452 return NULL;
7455 static const struct got_error *
7456 search_start_ref_view(struct tog_view *view)
7458 struct tog_ref_view_state *s = &view->state.ref;
7460 s->matched_entry = NULL;
7461 return NULL;
7464 static int
7465 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7467 regmatch_t regmatch;
7469 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7470 0) == 0;
7473 static const struct got_error *
7474 search_next_ref_view(struct tog_view *view)
7476 struct tog_ref_view_state *s = &view->state.ref;
7477 struct tog_reflist_entry *re = NULL;
7479 if (!view->searching) {
7480 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7481 return NULL;
7484 if (s->matched_entry) {
7485 if (view->searching == TOG_SEARCH_FORWARD) {
7486 if (s->selected_entry)
7487 re = TAILQ_NEXT(s->selected_entry, entry);
7488 else
7489 re = TAILQ_PREV(s->selected_entry,
7490 tog_reflist_head, entry);
7491 } else {
7492 if (s->selected_entry == NULL)
7493 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7494 else
7495 re = TAILQ_PREV(s->selected_entry,
7496 tog_reflist_head, entry);
7498 } else {
7499 if (s->selected_entry)
7500 re = s->selected_entry;
7501 else if (view->searching == TOG_SEARCH_FORWARD)
7502 re = TAILQ_FIRST(&s->refs);
7503 else
7504 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7507 while (1) {
7508 if (re == NULL) {
7509 if (s->matched_entry == NULL) {
7510 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7511 return NULL;
7513 if (view->searching == TOG_SEARCH_FORWARD)
7514 re = TAILQ_FIRST(&s->refs);
7515 else
7516 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7519 if (match_reflist_entry(re, &view->regex)) {
7520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7521 s->matched_entry = re;
7522 break;
7525 if (view->searching == TOG_SEARCH_FORWARD)
7526 re = TAILQ_NEXT(re, entry);
7527 else
7528 re = TAILQ_PREV(re, tog_reflist_head, entry);
7531 if (s->matched_entry) {
7532 s->first_displayed_entry = s->matched_entry;
7533 s->selected = 0;
7536 return NULL;
7539 static const struct got_error *
7540 show_ref_view(struct tog_view *view)
7542 const struct got_error *err = NULL;
7543 struct tog_ref_view_state *s = &view->state.ref;
7544 struct tog_reflist_entry *re;
7545 char *line = NULL;
7546 wchar_t *wline;
7547 struct tog_color *tc;
7548 int width, n;
7549 int limit = view->nlines;
7551 werase(view->window);
7553 s->ndisplayed = 0;
7554 if (view_is_hsplit_top(view))
7555 --limit; /* border */
7557 if (limit == 0)
7558 return NULL;
7560 re = s->first_displayed_entry;
7562 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7563 s->nrefs) == -1)
7564 return got_error_from_errno("asprintf");
7566 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7567 if (err) {
7568 free(line);
7569 return err;
7571 if (view_needs_focus_indication(view))
7572 wstandout(view->window);
7573 waddwstr(view->window, wline);
7574 if (view_needs_focus_indication(view))
7575 wstandend(view->window);
7576 free(wline);
7577 wline = NULL;
7578 free(line);
7579 line = NULL;
7580 if (width < view->ncols - 1)
7581 waddch(view->window, '\n');
7582 if (--limit <= 0)
7583 return NULL;
7585 n = 0;
7586 while (re && limit > 0) {
7587 char *line = NULL;
7588 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7590 if (s->show_date) {
7591 struct got_commit_object *ci;
7592 struct got_tag_object *tag;
7593 struct got_object_id *id;
7594 struct tm tm;
7595 time_t t;
7597 err = got_ref_resolve(&id, s->repo, re->ref);
7598 if (err)
7599 return err;
7600 err = got_object_open_as_tag(&tag, s->repo, id);
7601 if (err) {
7602 if (err->code != GOT_ERR_OBJ_TYPE) {
7603 free(id);
7604 return err;
7606 err = got_object_open_as_commit(&ci, s->repo,
7607 id);
7608 if (err) {
7609 free(id);
7610 return err;
7612 t = got_object_commit_get_committer_time(ci);
7613 got_object_commit_close(ci);
7614 } else {
7615 t = got_object_tag_get_tagger_time(tag);
7616 got_object_tag_close(tag);
7618 free(id);
7619 if (gmtime_r(&t, &tm) == NULL)
7620 return got_error_from_errno("gmtime_r");
7621 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7622 return got_error(GOT_ERR_NO_SPACE);
7624 if (got_ref_is_symbolic(re->ref)) {
7625 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7626 ymd : "", got_ref_get_name(re->ref),
7627 got_ref_get_symref_target(re->ref)) == -1)
7628 return got_error_from_errno("asprintf");
7629 } else if (s->show_ids) {
7630 struct got_object_id *id;
7631 char *id_str;
7632 err = got_ref_resolve(&id, s->repo, re->ref);
7633 if (err)
7634 return err;
7635 err = got_object_id_str(&id_str, id);
7636 if (err) {
7637 free(id);
7638 return err;
7640 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7641 got_ref_get_name(re->ref), id_str) == -1) {
7642 err = got_error_from_errno("asprintf");
7643 free(id);
7644 free(id_str);
7645 return err;
7647 free(id);
7648 free(id_str);
7649 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7650 got_ref_get_name(re->ref)) == -1)
7651 return got_error_from_errno("asprintf");
7653 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7654 0, 0);
7655 if (err) {
7656 free(line);
7657 return err;
7659 if (n == s->selected) {
7660 if (view->focussed)
7661 wstandout(view->window);
7662 s->selected_entry = re;
7664 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7665 if (tc)
7666 wattr_on(view->window,
7667 COLOR_PAIR(tc->colorpair), NULL);
7668 waddwstr(view->window, wline);
7669 if (tc)
7670 wattr_off(view->window,
7671 COLOR_PAIR(tc->colorpair), NULL);
7672 if (width < view->ncols - 1)
7673 waddch(view->window, '\n');
7674 if (n == s->selected && view->focussed)
7675 wstandend(view->window);
7676 free(line);
7677 free(wline);
7678 wline = NULL;
7679 n++;
7680 s->ndisplayed++;
7681 s->last_displayed_entry = re;
7683 limit--;
7684 re = TAILQ_NEXT(re, entry);
7687 view_border(view);
7688 return err;
7691 static const struct got_error *
7692 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7693 struct tog_reflist_entry *re, struct got_repository *repo)
7695 const struct got_error *err = NULL;
7696 struct got_object_id *commit_id = NULL;
7697 struct tog_view *tree_view;
7699 *new_view = NULL;
7701 err = resolve_reflist_entry(&commit_id, re, repo);
7702 if (err) {
7703 if (err->code != GOT_ERR_OBJ_TYPE)
7704 return err;
7705 else
7706 return NULL;
7710 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7711 if (tree_view == NULL) {
7712 err = got_error_from_errno("view_open");
7713 goto done;
7716 err = open_tree_view(tree_view, commit_id,
7717 got_ref_get_name(re->ref), repo);
7718 if (err)
7719 goto done;
7721 *new_view = tree_view;
7722 done:
7723 free(commit_id);
7724 return err;
7726 static const struct got_error *
7727 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7729 const struct got_error *err = NULL;
7730 struct tog_ref_view_state *s = &view->state.ref;
7731 struct tog_view *log_view, *tree_view;
7732 struct tog_reflist_entry *re;
7733 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7735 switch (ch) {
7736 case 'i':
7737 s->show_ids = !s->show_ids;
7738 view->count = 0;
7739 break;
7740 case 'm':
7741 s->show_date = !s->show_date;
7742 view->count = 0;
7743 break;
7744 case 'o':
7745 s->sort_by_date = !s->sort_by_date;
7746 view->count = 0;
7747 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7748 got_ref_cmp_by_commit_timestamp_descending :
7749 tog_ref_cmp_by_name, s->repo);
7750 if (err)
7751 break;
7752 got_reflist_object_id_map_free(tog_refs_idmap);
7753 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7754 &tog_refs, s->repo);
7755 if (err)
7756 break;
7757 ref_view_free_refs(s);
7758 err = ref_view_load_refs(s);
7759 break;
7760 case KEY_ENTER:
7761 case '\r':
7762 view->count = 0;
7763 if (!s->selected_entry)
7764 break;
7765 if (view_is_parent_view(view))
7766 view_get_split(view, &begin_y, &begin_x);
7768 err = log_ref_entry(&log_view, begin_y, begin_x,
7769 s->selected_entry, s->repo);
7770 if (err)
7771 break;
7773 if (view_is_parent_view(view) &&
7774 view->mode == TOG_VIEW_SPLIT_HRZN) {
7775 err = view_init_hsplit(view, begin_y);
7776 if (err)
7777 break;
7780 view->focussed = 0;
7781 log_view->focussed = 1;
7782 log_view->mode = view->mode;
7783 log_view->nlines = view->lines - begin_y;
7784 if (view_is_parent_view(view)) {
7785 view_transfer_size(log_view, view);
7786 err = view_close_child(view);
7787 if (err)
7788 return err;
7789 err = view_set_child(view, log_view);
7790 if (err)
7791 return err;
7792 view->focus_child = 1;
7793 } else
7794 *new_view = log_view;
7795 break;
7796 case 't':
7797 view->count = 0;
7798 if (!s->selected_entry)
7799 break;
7800 if (view_is_parent_view(view))
7801 view_get_split(view, &begin_y, &begin_x);
7802 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7803 s->selected_entry, s->repo);
7804 if (err || tree_view == NULL)
7805 break;
7806 if (view_is_parent_view(view) &&
7807 view->mode == TOG_VIEW_SPLIT_HRZN) {
7808 err = view_init_hsplit(view, begin_y);
7809 if (err)
7810 break;
7812 view->focussed = 0;
7813 tree_view->focussed = 1;
7814 tree_view->mode = view->mode;
7815 tree_view->nlines = view->lines - begin_y;
7816 if (view_is_parent_view(view)) {
7817 view_transfer_size(tree_view, view);
7818 err = view_close_child(view);
7819 if (err)
7820 return err;
7821 err = view_set_child(view, tree_view);
7822 if (err)
7823 return err;
7824 view->focus_child = 1;
7825 } else
7826 *new_view = tree_view;
7827 break;
7828 case 'g':
7829 case KEY_HOME:
7830 s->selected = 0;
7831 view->count = 0;
7832 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7833 break;
7834 case 'G':
7835 case KEY_END: {
7836 int eos = view->nlines - 1;
7838 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7839 --eos; /* border */
7840 s->selected = 0;
7841 view->count = 0;
7842 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7843 for (n = 0; n < eos; n++) {
7844 if (re == NULL)
7845 break;
7846 s->first_displayed_entry = re;
7847 re = TAILQ_PREV(re, tog_reflist_head, entry);
7849 if (n > 0)
7850 s->selected = n - 1;
7851 break;
7853 case 'k':
7854 case KEY_UP:
7855 case CTRL('p'):
7856 if (s->selected > 0) {
7857 s->selected--;
7858 break;
7860 ref_scroll_up(s, 1);
7861 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7862 view->count = 0;
7863 break;
7864 case CTRL('u'):
7865 case 'u':
7866 nscroll /= 2;
7867 /* FALL THROUGH */
7868 case KEY_PPAGE:
7869 case CTRL('b'):
7870 case 'b':
7871 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7872 s->selected -= MIN(nscroll, s->selected);
7873 ref_scroll_up(s, MAX(0, nscroll));
7874 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7875 view->count = 0;
7876 break;
7877 case 'j':
7878 case KEY_DOWN:
7879 case CTRL('n'):
7880 if (s->selected < s->ndisplayed - 1) {
7881 s->selected++;
7882 break;
7884 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7885 /* can't scroll any further */
7886 view->count = 0;
7887 break;
7889 ref_scroll_down(view, 1);
7890 break;
7891 case CTRL('d'):
7892 case 'd':
7893 nscroll /= 2;
7894 /* FALL THROUGH */
7895 case KEY_NPAGE:
7896 case CTRL('f'):
7897 case 'f':
7898 case ' ':
7899 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7900 /* can't scroll any further; move cursor down */
7901 if (s->selected < s->ndisplayed - 1)
7902 s->selected += MIN(nscroll,
7903 s->ndisplayed - s->selected - 1);
7904 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7905 s->selected += s->ndisplayed - s->selected - 1;
7906 view->count = 0;
7907 break;
7909 ref_scroll_down(view, nscroll);
7910 break;
7911 case CTRL('l'):
7912 view->count = 0;
7913 tog_free_refs();
7914 err = tog_load_refs(s->repo, s->sort_by_date);
7915 if (err)
7916 break;
7917 ref_view_free_refs(s);
7918 err = ref_view_load_refs(s);
7919 break;
7920 case KEY_RESIZE:
7921 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7922 s->selected = view->nlines - 2;
7923 break;
7924 default:
7925 view->count = 0;
7926 break;
7929 return err;
7932 __dead static void
7933 usage_ref(void)
7935 endwin();
7936 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7937 getprogname());
7938 exit(1);
7941 static const struct got_error *
7942 cmd_ref(int argc, char *argv[])
7944 const struct got_error *error;
7945 struct got_repository *repo = NULL;
7946 struct got_worktree *worktree = NULL;
7947 char *cwd = NULL, *repo_path = NULL;
7948 int ch;
7949 struct tog_view *view;
7950 int *pack_fds = NULL;
7952 while ((ch = getopt(argc, argv, "r:")) != -1) {
7953 switch (ch) {
7954 case 'r':
7955 repo_path = realpath(optarg, NULL);
7956 if (repo_path == NULL)
7957 return got_error_from_errno2("realpath",
7958 optarg);
7959 break;
7960 default:
7961 usage_ref();
7962 /* NOTREACHED */
7966 argc -= optind;
7967 argv += optind;
7969 if (argc > 1)
7970 usage_ref();
7972 error = got_repo_pack_fds_open(&pack_fds);
7973 if (error != NULL)
7974 goto done;
7976 if (repo_path == NULL) {
7977 cwd = getcwd(NULL, 0);
7978 if (cwd == NULL)
7979 return got_error_from_errno("getcwd");
7980 error = got_worktree_open(&worktree, cwd);
7981 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7982 goto done;
7983 if (worktree)
7984 repo_path =
7985 strdup(got_worktree_get_repo_path(worktree));
7986 else
7987 repo_path = strdup(cwd);
7988 if (repo_path == NULL) {
7989 error = got_error_from_errno("strdup");
7990 goto done;
7994 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7995 if (error != NULL)
7996 goto done;
7998 init_curses();
8000 error = apply_unveil(got_repo_get_path(repo), NULL);
8001 if (error)
8002 goto done;
8004 error = tog_load_refs(repo, 0);
8005 if (error)
8006 goto done;
8008 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8009 if (view == NULL) {
8010 error = got_error_from_errno("view_open");
8011 goto done;
8014 error = open_ref_view(view, repo);
8015 if (error)
8016 goto done;
8018 if (worktree) {
8019 /* Release work tree lock. */
8020 got_worktree_close(worktree);
8021 worktree = NULL;
8023 error = view_loop(view);
8024 done:
8025 free(repo_path);
8026 free(cwd);
8027 if (repo) {
8028 const struct got_error *close_err = got_repo_close(repo);
8029 if (close_err)
8030 error = close_err;
8032 if (pack_fds) {
8033 const struct got_error *pack_err =
8034 got_repo_pack_fds_close(pack_fds);
8035 if (error == NULL)
8036 error = pack_err;
8038 tog_free_refs();
8039 return error;
8043 * If view was scrolled down to move the selected line into view when opening a
8044 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8046 static void
8047 offset_selection_up(struct tog_view *view)
8049 switch (view->type) {
8050 case TOG_VIEW_BLAME: {
8051 struct tog_blame_view_state *s = &view->state.blame;
8052 if (s->first_displayed_line == 1) {
8053 s->selected_line = MAX(s->selected_line - view->offset,
8054 1);
8055 break;
8057 if (s->first_displayed_line > view->offset)
8058 s->first_displayed_line -= view->offset;
8059 else
8060 s->first_displayed_line = 1;
8061 s->selected_line += view->offset;
8062 break;
8064 case TOG_VIEW_LOG:
8065 log_scroll_up(&view->state.log, view->offset);
8066 view->state.log.selected += view->offset;
8067 break;
8068 case TOG_VIEW_REF:
8069 ref_scroll_up(&view->state.ref, view->offset);
8070 view->state.ref.selected += view->offset;
8071 break;
8072 case TOG_VIEW_TREE:
8073 tree_scroll_up(&view->state.tree, view->offset);
8074 view->state.tree.selected += view->offset;
8075 break;
8076 default:
8077 break;
8080 view->offset = 0;
8084 * If the selected line is in the section of screen covered by the bottom split,
8085 * scroll down offset lines to move it into view and index its new position.
8087 static const struct got_error *
8088 offset_selection_down(struct tog_view *view)
8090 const struct got_error *err = NULL;
8091 const struct got_error *(*scrolld)(struct tog_view *, int);
8092 int *selected = NULL;
8093 int header, offset;
8095 switch (view->type) {
8096 case TOG_VIEW_BLAME: {
8097 struct tog_blame_view_state *s = &view->state.blame;
8098 header = 3;
8099 scrolld = NULL;
8100 if (s->selected_line > view->nlines - header) {
8101 offset = abs(view->nlines - s->selected_line - header);
8102 s->first_displayed_line += offset;
8103 s->selected_line -= offset;
8104 view->offset = offset;
8106 break;
8108 case TOG_VIEW_LOG: {
8109 struct tog_log_view_state *s = &view->state.log;
8110 scrolld = &log_scroll_down;
8111 header = view_is_parent_view(view) ? 3 : 2;
8112 selected = &s->selected;
8113 break;
8115 case TOG_VIEW_REF: {
8116 struct tog_ref_view_state *s = &view->state.ref;
8117 scrolld = &ref_scroll_down;
8118 header = 3;
8119 selected = &s->selected;
8120 break;
8122 case TOG_VIEW_TREE: {
8123 struct tog_tree_view_state *s = &view->state.tree;
8124 scrolld = &tree_scroll_down;
8125 header = 5;
8126 selected = &s->selected;
8127 break;
8129 default:
8130 selected = NULL;
8131 scrolld = NULL;
8132 header = 0;
8133 break;
8136 if (selected && *selected > view->nlines - header) {
8137 offset = abs(view->nlines - *selected - header);
8138 view->offset = offset;
8139 if (scrolld && offset) {
8140 err = scrolld(view, offset);
8141 *selected -= offset;
8145 return err;
8148 static void
8149 list_commands(FILE *fp)
8151 size_t i;
8153 fprintf(fp, "commands:");
8154 for (i = 0; i < nitems(tog_commands); i++) {
8155 const struct tog_cmd *cmd = &tog_commands[i];
8156 fprintf(fp, " %s", cmd->name);
8158 fputc('\n', fp);
8161 __dead static void
8162 usage(int hflag, int status)
8164 FILE *fp = (status == 0) ? stdout : stderr;
8166 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8167 getprogname());
8168 if (hflag) {
8169 fprintf(fp, "lazy usage: %s path\n", getprogname());
8170 list_commands(fp);
8172 exit(status);
8175 static char **
8176 make_argv(int argc, ...)
8178 va_list ap;
8179 char **argv;
8180 int i;
8182 va_start(ap, argc);
8184 argv = calloc(argc, sizeof(char *));
8185 if (argv == NULL)
8186 err(1, "calloc");
8187 for (i = 0; i < argc; i++) {
8188 argv[i] = strdup(va_arg(ap, char *));
8189 if (argv[i] == NULL)
8190 err(1, "strdup");
8193 va_end(ap);
8194 return argv;
8198 * Try to convert 'tog path' into a 'tog log path' command.
8199 * The user could simply have mistyped the command rather than knowingly
8200 * provided a path. So check whether argv[0] can in fact be resolved
8201 * to a path in the HEAD commit and print a special error if not.
8202 * This hack is for mpi@ <3
8204 static const struct got_error *
8205 tog_log_with_path(int argc, char *argv[])
8207 const struct got_error *error = NULL, *close_err;
8208 const struct tog_cmd *cmd = NULL;
8209 struct got_repository *repo = NULL;
8210 struct got_worktree *worktree = NULL;
8211 struct got_object_id *commit_id = NULL, *id = NULL;
8212 struct got_commit_object *commit = NULL;
8213 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8214 char *commit_id_str = NULL, **cmd_argv = NULL;
8215 int *pack_fds = NULL;
8217 cwd = getcwd(NULL, 0);
8218 if (cwd == NULL)
8219 return got_error_from_errno("getcwd");
8221 error = got_repo_pack_fds_open(&pack_fds);
8222 if (error != NULL)
8223 goto done;
8225 error = got_worktree_open(&worktree, cwd);
8226 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8227 goto done;
8229 if (worktree)
8230 repo_path = strdup(got_worktree_get_repo_path(worktree));
8231 else
8232 repo_path = strdup(cwd);
8233 if (repo_path == NULL) {
8234 error = got_error_from_errno("strdup");
8235 goto done;
8238 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8239 if (error != NULL)
8240 goto done;
8242 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8243 repo, worktree);
8244 if (error)
8245 goto done;
8247 error = tog_load_refs(repo, 0);
8248 if (error)
8249 goto done;
8250 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8251 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8252 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8253 if (error)
8254 goto done;
8256 if (worktree) {
8257 got_worktree_close(worktree);
8258 worktree = NULL;
8261 error = got_object_open_as_commit(&commit, repo, commit_id);
8262 if (error)
8263 goto done;
8265 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8266 if (error) {
8267 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8268 goto done;
8269 fprintf(stderr, "%s: '%s' is no known command or path\n",
8270 getprogname(), argv[0]);
8271 usage(1, 1);
8272 /* not reached */
8275 close_err = got_repo_close(repo);
8276 if (error == NULL)
8277 error = close_err;
8278 repo = NULL;
8280 error = got_object_id_str(&commit_id_str, commit_id);
8281 if (error)
8282 goto done;
8284 cmd = &tog_commands[0]; /* log */
8285 argc = 4;
8286 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8287 error = cmd->cmd_main(argc, cmd_argv);
8288 done:
8289 if (repo) {
8290 close_err = got_repo_close(repo);
8291 if (error == NULL)
8292 error = close_err;
8294 if (commit)
8295 got_object_commit_close(commit);
8296 if (worktree)
8297 got_worktree_close(worktree);
8298 if (pack_fds) {
8299 const struct got_error *pack_err =
8300 got_repo_pack_fds_close(pack_fds);
8301 if (error == NULL)
8302 error = pack_err;
8304 free(id);
8305 free(commit_id_str);
8306 free(commit_id);
8307 free(cwd);
8308 free(repo_path);
8309 free(in_repo_path);
8310 if (cmd_argv) {
8311 int i;
8312 for (i = 0; i < argc; i++)
8313 free(cmd_argv[i]);
8314 free(cmd_argv);
8316 tog_free_refs();
8317 return error;
8320 int
8321 main(int argc, char *argv[])
8323 const struct got_error *error = NULL;
8324 const struct tog_cmd *cmd = NULL;
8325 int ch, hflag = 0, Vflag = 0;
8326 char **cmd_argv = NULL;
8327 static const struct option longopts[] = {
8328 { "version", no_argument, NULL, 'V' },
8329 { NULL, 0, NULL, 0}
8331 char *diff_algo_str = NULL;
8333 setlocale(LC_CTYPE, "");
8335 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8336 switch (ch) {
8337 case 'h':
8338 hflag = 1;
8339 break;
8340 case 'V':
8341 Vflag = 1;
8342 break;
8343 default:
8344 usage(hflag, 1);
8345 /* NOTREACHED */
8349 argc -= optind;
8350 argv += optind;
8351 optind = 1;
8352 optreset = 1;
8354 if (Vflag) {
8355 got_version_print_str();
8356 return 0;
8359 #ifndef PROFILE
8360 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8361 NULL) == -1)
8362 err(1, "pledge");
8363 #endif
8365 if (argc == 0) {
8366 if (hflag)
8367 usage(hflag, 0);
8368 /* Build an argument vector which runs a default command. */
8369 cmd = &tog_commands[0];
8370 argc = 1;
8371 cmd_argv = make_argv(argc, cmd->name);
8372 } else {
8373 size_t i;
8375 /* Did the user specify a command? */
8376 for (i = 0; i < nitems(tog_commands); i++) {
8377 if (strncmp(tog_commands[i].name, argv[0],
8378 strlen(argv[0])) == 0) {
8379 cmd = &tog_commands[i];
8380 break;
8385 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8386 if (diff_algo_str) {
8387 if (strcasecmp(diff_algo_str, "patience") == 0)
8388 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8389 if (strcasecmp(diff_algo_str, "myers") == 0)
8390 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8393 if (cmd == NULL) {
8394 if (argc != 1)
8395 usage(0, 1);
8396 /* No command specified; try log with a path */
8397 error = tog_log_with_path(argc, argv);
8398 } else {
8399 if (hflag)
8400 cmd->cmd_usage();
8401 else
8402 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8405 endwin();
8406 putchar('\n');
8407 if (cmd_argv) {
8408 int i;
8409 for (i = 0; i < argc; i++)
8410 free(cmd_argv[i]);
8411 free(cmd_argv);
8414 if (error && error->code != GOT_ERR_CANCELLED)
8415 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8416 return 0;