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 got_object_id *id_to_log;
448 struct tog_blame blame;
449 int matched_line;
450 struct tog_colors colors;
451 };
453 struct tog_parent_tree {
454 TAILQ_ENTRY(tog_parent_tree) entry;
455 struct got_tree_object *tree;
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *selected_entry;
458 int selected;
459 };
461 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
463 struct tog_tree_view_state {
464 char *tree_label;
465 struct got_object_id *commit_id;/* commit which this tree belongs to */
466 struct got_tree_object *root; /* the commit's root tree entry */
467 struct got_tree_object *tree; /* currently displayed (sub-)tree */
468 struct got_tree_entry *first_displayed_entry;
469 struct got_tree_entry *last_displayed_entry;
470 struct got_tree_entry *selected_entry;
471 int ndisplayed, selected, show_ids;
472 struct tog_parent_trees parents; /* parent trees of current sub-tree */
473 char *head_ref_name;
474 struct got_repository *repo;
475 struct got_tree_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 struct tog_reflist_entry {
480 TAILQ_ENTRY(tog_reflist_entry) entry;
481 struct got_reference *ref;
482 int idx;
483 };
485 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
487 struct tog_ref_view_state {
488 struct tog_reflist_head refs;
489 struct tog_reflist_entry *first_displayed_entry;
490 struct tog_reflist_entry *last_displayed_entry;
491 struct tog_reflist_entry *selected_entry;
492 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
493 struct got_repository *repo;
494 struct tog_reflist_entry *matched_entry;
495 struct tog_colors colors;
496 };
498 /*
499 * We implement two types of views: parent views and child views.
501 * The 'Tab' key switches focus between a parent view and its child view.
502 * Child views are shown side-by-side to their parent view, provided
503 * there is enough screen estate.
505 * When a new view is opened from within a parent view, this new view
506 * becomes a child view of the parent view, replacing any existing child.
508 * When a new view is opened from within a child view, this new view
509 * becomes a parent view which will obscure the views below until the
510 * user quits the new parent view by typing 'q'.
512 * This list of views contains parent views only.
513 * Child views are only pointed to by their parent view.
514 */
515 TAILQ_HEAD(tog_view_list_head, tog_view);
517 struct tog_view {
518 TAILQ_ENTRY(tog_view) entry;
519 WINDOW *window;
520 PANEL *panel;
521 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
522 int resized_y, resized_x; /* begin_y/x based on user resizing */
523 int maxx, x; /* max column and current start column */
524 int lines, cols; /* copies of LINES and COLS */
525 int nscrolled, offset; /* lines scrolled and hsplit line offset */
526 int ch, count; /* current keymap and count prefix */
527 int resized; /* set when in a resize event */
528 int focussed; /* Only set on one parent or child view at a time. */
529 int dying;
530 struct tog_view *parent;
531 struct tog_view *child;
533 /*
534 * This flag is initially set on parent views when a new child view
535 * is created. It gets toggled when the 'Tab' key switches focus
536 * between parent and child.
537 * The flag indicates whether focus should be passed on to our child
538 * view if this parent view gets picked for focus after another parent
539 * view was closed. This prevents child views from losing focus in such
540 * situations.
541 */
542 int focus_child;
544 enum tog_view_mode mode;
545 /* type-specific state */
546 enum tog_view_type type;
547 union {
548 struct tog_diff_view_state diff;
549 struct tog_log_view_state log;
550 struct tog_blame_view_state blame;
551 struct tog_tree_view_state tree;
552 struct tog_ref_view_state ref;
553 } state;
555 const struct got_error *(*show)(struct tog_view *);
556 const struct got_error *(*input)(struct tog_view **,
557 struct tog_view *, int);
558 const struct got_error *(*reset)(struct tog_view *);
559 const struct got_error *(*resize)(struct tog_view *, int);
560 const struct got_error *(*close)(struct tog_view *);
562 const struct got_error *(*search_start)(struct tog_view *);
563 const struct got_error *(*search_next)(struct tog_view *);
564 int search_started;
565 int searching;
566 #define TOG_SEARCH_FORWARD 1
567 #define TOG_SEARCH_BACKWARD 2
568 int search_next_done;
569 #define TOG_SEARCH_HAVE_MORE 1
570 #define TOG_SEARCH_NO_MORE 2
571 #define TOG_SEARCH_HAVE_NONE 3
572 regex_t regex;
573 regmatch_t regmatch;
574 };
576 static const struct got_error *open_diff_view(struct tog_view *,
577 struct got_object_id *, struct got_object_id *,
578 const char *, const char *, int, int, int, struct tog_view *,
579 struct got_repository *);
580 static const struct got_error *show_diff_view(struct tog_view *);
581 static const struct got_error *input_diff_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *reset_diff_view(struct tog_view *);
584 static const struct got_error* close_diff_view(struct tog_view *);
585 static const struct got_error *search_start_diff_view(struct tog_view *);
586 static const struct got_error *search_next_diff_view(struct tog_view *);
588 static const struct got_error *open_log_view(struct tog_view *,
589 struct got_object_id *, struct got_repository *,
590 const char *, const char *, int);
591 static const struct got_error * show_log_view(struct tog_view *);
592 static const struct got_error *input_log_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *resize_log_view(struct tog_view *, int);
595 static const struct got_error *close_log_view(struct tog_view *);
596 static const struct got_error *search_start_log_view(struct tog_view *);
597 static const struct got_error *search_next_log_view(struct tog_view *);
599 static const struct got_error *open_blame_view(struct tog_view *, char *,
600 struct got_object_id *, struct got_repository *);
601 static const struct got_error *show_blame_view(struct tog_view *);
602 static const struct got_error *input_blame_view(struct tog_view **,
603 struct tog_view *, int);
604 static const struct got_error *reset_blame_view(struct tog_view *);
605 static const struct got_error *close_blame_view(struct tog_view *);
606 static const struct got_error *search_start_blame_view(struct tog_view *);
607 static const struct got_error *search_next_blame_view(struct tog_view *);
609 static const struct got_error *open_tree_view(struct tog_view *,
610 struct got_object_id *, const char *, struct got_repository *);
611 static const struct got_error *show_tree_view(struct tog_view *);
612 static const struct got_error *input_tree_view(struct tog_view **,
613 struct tog_view *, int);
614 static const struct got_error *close_tree_view(struct tog_view *);
615 static const struct got_error *search_start_tree_view(struct tog_view *);
616 static const struct got_error *search_next_tree_view(struct tog_view *);
618 static const struct got_error *open_ref_view(struct tog_view *,
619 struct got_repository *);
620 static const struct got_error *show_ref_view(struct tog_view *);
621 static const struct got_error *input_ref_view(struct tog_view **,
622 struct tog_view *, int);
623 static const struct got_error *close_ref_view(struct tog_view *);
624 static const struct got_error *search_start_ref_view(struct tog_view *);
625 static const struct got_error *search_next_ref_view(struct tog_view *);
627 static volatile sig_atomic_t tog_sigwinch_received;
628 static volatile sig_atomic_t tog_sigpipe_received;
629 static volatile sig_atomic_t tog_sigcont_received;
630 static volatile sig_atomic_t tog_sigint_received;
631 static volatile sig_atomic_t tog_sigterm_received;
633 static void
634 tog_sigwinch(int signo)
636 tog_sigwinch_received = 1;
639 static void
640 tog_sigpipe(int signo)
642 tog_sigpipe_received = 1;
645 static void
646 tog_sigcont(int signo)
648 tog_sigcont_received = 1;
651 static void
652 tog_sigint(int signo)
654 tog_sigint_received = 1;
657 static void
658 tog_sigterm(int signo)
660 tog_sigterm_received = 1;
663 static int
664 tog_fatal_signal_received(void)
666 return (tog_sigpipe_received ||
667 tog_sigint_received || tog_sigint_received);
670 static const struct got_error *
671 view_close(struct tog_view *view)
673 const struct got_error *err = NULL, *child_err = NULL;
675 if (view->child) {
676 child_err = view_close(view->child);
677 view->child = NULL;
679 if (view->close)
680 err = view->close(view);
681 if (view->panel)
682 del_panel(view->panel);
683 if (view->window)
684 delwin(view->window);
685 free(view);
686 return err ? err : child_err;
689 static struct tog_view *
690 view_open(int nlines, int ncols, int begin_y, int begin_x,
691 enum tog_view_type type)
693 struct tog_view *view = calloc(1, sizeof(*view));
695 if (view == NULL)
696 return NULL;
698 view->type = type;
699 view->lines = LINES;
700 view->cols = COLS;
701 view->nlines = nlines ? nlines : LINES - begin_y;
702 view->ncols = ncols ? ncols : COLS - begin_x;
703 view->begin_y = begin_y;
704 view->begin_x = begin_x;
705 view->window = newwin(nlines, ncols, begin_y, begin_x);
706 if (view->window == NULL) {
707 view_close(view);
708 return NULL;
710 view->panel = new_panel(view->window);
711 if (view->panel == NULL ||
712 set_panel_userptr(view->panel, view) != OK) {
713 view_close(view);
714 return NULL;
717 keypad(view->window, TRUE);
718 return view;
721 static int
722 view_split_begin_x(int begin_x)
724 if (begin_x > 0 || COLS < 120)
725 return 0;
726 return (COLS - MAX(COLS / 2, 80));
729 /* XXX Stub till we decide what to do. */
730 static int
731 view_split_begin_y(int lines)
733 return lines * HSPLIT_SCALE;
736 static const struct got_error *view_resize(struct tog_view *);
738 static const struct got_error *
739 view_splitscreen(struct tog_view *view)
741 const struct got_error *err = NULL;
743 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
744 if (view->resized_y && view->resized_y < view->lines)
745 view->begin_y = view->resized_y;
746 else
747 view->begin_y = view_split_begin_y(view->nlines);
748 view->begin_x = 0;
749 } else if (!view->resized) {
750 if (view->resized_x && view->resized_x < view->cols - 1 &&
751 view->cols > 119)
752 view->begin_x = view->resized_x;
753 else
754 view->begin_x = view_split_begin_x(0);
755 view->begin_y = 0;
757 view->nlines = LINES - view->begin_y;
758 view->ncols = COLS - view->begin_x;
759 view->lines = LINES;
760 view->cols = COLS;
761 err = view_resize(view);
762 if (err)
763 return err;
765 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
766 view->parent->nlines = view->begin_y;
768 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
769 return got_error_from_errno("mvwin");
771 return NULL;
774 static const struct got_error *
775 view_fullscreen(struct tog_view *view)
777 const struct got_error *err = NULL;
779 view->begin_x = 0;
780 view->begin_y = view->resized ? view->begin_y : 0;
781 view->nlines = view->resized ? view->nlines : LINES;
782 view->ncols = COLS;
783 view->lines = LINES;
784 view->cols = COLS;
785 err = view_resize(view);
786 if (err)
787 return err;
789 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
790 return got_error_from_errno("mvwin");
792 return NULL;
795 static int
796 view_is_parent_view(struct tog_view *view)
798 return view->parent == NULL;
801 static int
802 view_is_splitscreen(struct tog_view *view)
804 return view->begin_x > 0 || view->begin_y > 0;
807 static int
808 view_is_fullscreen(struct tog_view *view)
810 return view->nlines == LINES && view->ncols == COLS;
813 static int
814 view_is_hsplit_top(struct tog_view *view)
816 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
817 view_is_splitscreen(view->child);
820 static void
821 view_border(struct tog_view *view)
823 PANEL *panel;
824 const struct tog_view *view_above;
826 if (view->parent)
827 return view_border(view->parent);
829 panel = panel_above(view->panel);
830 if (panel == NULL)
831 return;
833 view_above = panel_userptr(panel);
834 if (view->mode == TOG_VIEW_SPLIT_HRZN)
835 mvwhline(view->window, view_above->begin_y - 1,
836 view->begin_x, got_locale_is_utf8() ?
837 ACS_HLINE : '-', view->ncols);
838 else
839 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
840 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
843 static const struct got_error *view_init_hsplit(struct tog_view *, int);
844 static const struct got_error *request_log_commits(struct tog_view *);
845 static const struct got_error *offset_selection_down(struct tog_view *);
846 static void offset_selection_up(struct tog_view *);
847 static void view_get_split(struct tog_view *, int *, int *);
849 static const struct got_error *
850 view_resize(struct tog_view *view)
852 const struct got_error *err = NULL;
853 int dif, nlines, ncols;
855 dif = LINES - view->lines; /* line difference */
857 if (view->lines > LINES)
858 nlines = view->nlines - (view->lines - LINES);
859 else
860 nlines = view->nlines + (LINES - view->lines);
861 if (view->cols > COLS)
862 ncols = view->ncols - (view->cols - COLS);
863 else
864 ncols = view->ncols + (COLS - view->cols);
866 if (view->child) {
867 int hs = view->child->begin_y;
869 if (!view_is_fullscreen(view))
870 view->child->begin_x = view_split_begin_x(view->begin_x);
871 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
872 view->child->begin_x == 0) {
873 ncols = COLS;
875 view_fullscreen(view->child);
876 if (view->child->focussed)
877 show_panel(view->child->panel);
878 else
879 show_panel(view->panel);
880 } else {
881 ncols = view->child->begin_x;
883 view_splitscreen(view->child);
884 show_panel(view->child->panel);
886 /*
887 * XXX This is ugly and needs to be moved into the above
888 * logic but "works" for now and my attempts at moving it
889 * break either 'tab' or 'F' key maps in horizontal splits.
890 */
891 if (hs) {
892 err = view_splitscreen(view->child);
893 if (err)
894 return err;
895 if (dif < 0) { /* top split decreased */
896 err = offset_selection_down(view);
897 if (err)
898 return err;
900 view_border(view);
901 update_panels();
902 doupdate();
903 show_panel(view->child->panel);
904 nlines = view->nlines;
906 } else if (view->parent == NULL)
907 ncols = COLS;
909 if (view->resize && dif > 0) {
910 err = view->resize(view, dif);
911 if (err)
912 return err;
915 if (wresize(view->window, nlines, ncols) == ERR)
916 return got_error_from_errno("wresize");
917 if (replace_panel(view->panel, view->window) == ERR)
918 return got_error_from_errno("replace_panel");
919 wclear(view->window);
921 view->nlines = nlines;
922 view->ncols = ncols;
923 view->lines = LINES;
924 view->cols = COLS;
926 return NULL;
929 static const struct got_error *
930 resize_log_view(struct tog_view *view, int increase)
932 struct tog_log_view_state *s = &view->state.log;
933 const struct got_error *err = NULL;
934 int n = 0;
936 if (s->selected_entry)
937 n = s->selected_entry->idx + view->lines - s->selected;
939 /*
940 * Request commits to account for the increased
941 * height so we have enough to populate the view.
942 */
943 if (s->commits.ncommits < n) {
944 view->nscrolled = n - s->commits.ncommits + increase + 1;
945 err = request_log_commits(view);
948 return err;
951 static void
952 view_adjust_offset(struct tog_view *view, int n)
954 if (n == 0)
955 return;
957 if (view->parent && view->parent->offset) {
958 if (view->parent->offset + n >= 0)
959 view->parent->offset += n;
960 else
961 view->parent->offset = 0;
962 } else if (view->offset) {
963 if (view->offset - n >= 0)
964 view->offset -= n;
965 else
966 view->offset = 0;
970 static const struct got_error *
971 view_resize_split(struct tog_view *view, int resize)
973 const struct got_error *err = NULL;
974 struct tog_view *v = NULL;
976 if (view->parent)
977 v = view->parent;
978 else
979 v = view;
981 if (!v->child || !view_is_splitscreen(v->child))
982 return NULL;
984 v->resized = v->child->resized = resize; /* lock for resize event */
986 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
987 if (v->child->resized_y)
988 v->child->begin_y = v->child->resized_y;
989 if (view->parent)
990 v->child->begin_y -= resize;
991 else
992 v->child->begin_y += resize;
993 if (v->child->begin_y < 3) {
994 view->count = 0;
995 v->child->begin_y = 3;
996 } else if (v->child->begin_y > LINES - 1) {
997 view->count = 0;
998 v->child->begin_y = LINES - 1;
1000 v->ncols = COLS;
1001 v->child->ncols = COLS;
1002 view_adjust_offset(view, resize);
1003 err = view_init_hsplit(v, v->child->begin_y);
1004 if (err)
1005 return err;
1006 v->child->resized_y = v->child->begin_y;
1007 } else {
1008 if (v->child->resized_x)
1009 v->child->begin_x = v->child->resized_x;
1010 if (view->parent)
1011 v->child->begin_x -= resize;
1012 else
1013 v->child->begin_x += resize;
1014 if (v->child->begin_x < 11) {
1015 view->count = 0;
1016 v->child->begin_x = 11;
1017 } else if (v->child->begin_x > COLS - 1) {
1018 view->count = 0;
1019 v->child->begin_x = COLS - 1;
1021 v->child->resized_x = v->child->begin_x;
1024 v->child->mode = v->mode;
1025 v->child->nlines = v->lines - v->child->begin_y;
1026 v->child->ncols = v->cols - v->child->begin_x;
1027 v->focus_child = 1;
1029 err = view_fullscreen(v);
1030 if (err)
1031 return err;
1032 err = view_splitscreen(v->child);
1033 if (err)
1034 return err;
1036 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1037 err = offset_selection_down(v->child);
1038 if (err)
1039 return err;
1042 if (v->resize)
1043 err = v->resize(v, 0);
1044 else if (v->child->resize)
1045 err = v->child->resize(v->child, 0);
1047 v->resized = v->child->resized = 0;
1049 return err;
1052 static void
1053 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1055 struct tog_view *v = src->child ? src->child : src;
1057 dst->resized_x = v->resized_x;
1058 dst->resized_y = v->resized_y;
1061 static const struct got_error *
1062 view_close_child(struct tog_view *view)
1064 const struct got_error *err = NULL;
1066 if (view->child == NULL)
1067 return NULL;
1069 err = view_close(view->child);
1070 view->child = NULL;
1071 return err;
1074 static const struct got_error *
1075 view_set_child(struct tog_view *view, struct tog_view *child)
1077 const struct got_error *err = NULL;
1079 view->child = child;
1080 child->parent = view;
1082 err = view_resize(view);
1083 if (err)
1084 return err;
1086 if (view->child->resized_x || view->child->resized_y)
1087 err = view_resize_split(view, 0);
1089 return err;
1092 static const struct got_error *view_dispatch_request(struct tog_view **,
1093 struct tog_view *, enum tog_view_type, int, int);
1095 static const struct got_error *
1096 view_request_new(struct tog_view **requested, struct tog_view *view,
1097 enum tog_view_type request)
1099 struct tog_view *new_view = NULL;
1100 const struct got_error *err;
1101 int y = 0, x = 0;
1103 *requested = NULL;
1105 if (view_is_parent_view(view))
1106 view_get_split(view, &y, &x);
1108 err = view_dispatch_request(&new_view, view, request, y, x);
1109 if (err)
1110 return err;
1112 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1113 err = view_init_hsplit(view, y);
1114 if (err)
1115 return err;
1118 view->focussed = 0;
1119 new_view->focussed = 1;
1120 new_view->mode = view->mode;
1121 new_view->nlines = view->lines - y;
1123 if (view_is_parent_view(view)) {
1124 view_transfer_size(new_view, view);
1125 err = view_close_child(view);
1126 if (err)
1127 return err;
1128 err = view_set_child(view, new_view);
1129 if (err)
1130 return err;
1131 view->focus_child = 1;
1132 } else
1133 *requested = new_view;
1135 return NULL;
1138 static void
1139 tog_resizeterm(void)
1141 int cols, lines;
1142 struct winsize size;
1144 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1145 cols = 80; /* Default */
1146 lines = 24;
1147 } else {
1148 cols = size.ws_col;
1149 lines = size.ws_row;
1151 resize_term(lines, cols);
1154 static const struct got_error *
1155 view_search_start(struct tog_view *view)
1157 const struct got_error *err = NULL;
1158 struct tog_view *v = view;
1159 char pattern[1024];
1160 int ret;
1162 if (view->search_started) {
1163 regfree(&view->regex);
1164 view->searching = 0;
1165 memset(&view->regmatch, 0, sizeof(view->regmatch));
1167 view->search_started = 0;
1169 if (view->nlines < 1)
1170 return NULL;
1172 if (view_is_hsplit_top(view))
1173 v = view->child;
1175 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1176 wclrtoeol(v->window);
1178 nodelay(view->window, FALSE); /* block for search term input */
1179 nocbreak();
1180 echo();
1181 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1182 wrefresh(v->window);
1183 cbreak();
1184 noecho();
1185 nodelay(view->window, TRUE);
1186 if (ret == ERR)
1187 return NULL;
1189 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1190 err = view->search_start(view);
1191 if (err) {
1192 regfree(&view->regex);
1193 return err;
1195 view->search_started = 1;
1196 view->searching = TOG_SEARCH_FORWARD;
1197 view->search_next_done = 0;
1198 view->search_next(view);
1201 return NULL;
1204 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1205 static const struct got_error *
1206 switch_split(struct tog_view *view)
1208 const struct got_error *err = NULL;
1209 struct tog_view *v = NULL;
1211 if (view->parent)
1212 v = view->parent;
1213 else
1214 v = view;
1216 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1217 v->mode = TOG_VIEW_SPLIT_VERT;
1218 else
1219 v->mode = TOG_VIEW_SPLIT_HRZN;
1221 if (!v->child)
1222 return NULL;
1223 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1224 v->mode = TOG_VIEW_SPLIT_NONE;
1226 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1227 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1228 v->child->begin_y = v->child->resized_y;
1229 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1230 v->child->begin_x = v->child->resized_x;
1233 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1234 v->ncols = COLS;
1235 v->child->ncols = COLS;
1236 v->child->nscrolled = LINES - v->child->nlines;
1238 err = view_init_hsplit(v, v->child->begin_y);
1239 if (err)
1240 return err;
1242 v->child->mode = v->mode;
1243 v->child->nlines = v->lines - v->child->begin_y;
1244 v->focus_child = 1;
1246 err = view_fullscreen(v);
1247 if (err)
1248 return err;
1249 err = view_splitscreen(v->child);
1250 if (err)
1251 return err;
1253 if (v->mode == TOG_VIEW_SPLIT_NONE)
1254 v->mode = TOG_VIEW_SPLIT_VERT;
1255 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1256 err = offset_selection_down(v);
1257 if (err)
1258 return err;
1259 err = offset_selection_down(v->child);
1260 if (err)
1261 return err;
1262 } else {
1263 offset_selection_up(v);
1264 offset_selection_up(v->child);
1266 if (v->resize)
1267 err = v->resize(v, 0);
1268 else if (v->child->resize)
1269 err = v->child->resize(v->child, 0);
1271 return err;
1275 * Compute view->count from numeric input. Assign total to view->count and
1276 * return first non-numeric key entered.
1278 static int
1279 get_compound_key(struct tog_view *view, int c)
1281 struct tog_view *v = view;
1282 int x, n = 0;
1284 if (view_is_hsplit_top(view))
1285 v = view->child;
1286 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1287 v = view->parent;
1289 view->count = 0;
1290 cbreak(); /* block for input */
1291 wmove(v->window, v->nlines - 1, 0);
1292 wclrtoeol(v->window);
1293 waddch(v->window, ':');
1295 do {
1296 x = getcurx(v->window);
1297 if (x != ERR && x < view->ncols) {
1298 waddch(v->window, c);
1299 wrefresh(v->window);
1303 * Don't overflow. Max valid request should be the greatest
1304 * between the longest and total lines; cap at 10 million.
1306 if (n >= 9999999)
1307 n = 9999999;
1308 else
1309 n = n * 10 + (c - '0');
1310 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1312 /* Massage excessive or inapplicable values at the input handler. */
1313 view->count = n;
1315 return c;
1318 static const struct got_error *
1319 view_input(struct tog_view **new, int *done, struct tog_view *view,
1320 struct tog_view_list_head *views)
1322 const struct got_error *err = NULL;
1323 struct tog_view *v;
1324 int ch, errcode;
1326 *new = NULL;
1328 /* Clear "no matches" indicator. */
1329 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1330 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1331 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1332 view->count = 0;
1335 if (view->searching && !view->search_next_done) {
1336 errcode = pthread_mutex_unlock(&tog_mutex);
1337 if (errcode)
1338 return got_error_set_errno(errcode,
1339 "pthread_mutex_unlock");
1340 sched_yield();
1341 errcode = pthread_mutex_lock(&tog_mutex);
1342 if (errcode)
1343 return got_error_set_errno(errcode,
1344 "pthread_mutex_lock");
1345 view->search_next(view);
1346 return NULL;
1349 nodelay(view->window, FALSE);
1350 /* Allow threads to make progress while we are waiting for input. */
1351 errcode = pthread_mutex_unlock(&tog_mutex);
1352 if (errcode)
1353 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1354 /* If we have an unfinished count, let C-g or backspace abort. */
1355 if (view->count && --view->count) {
1356 cbreak();
1357 nodelay(view->window, TRUE);
1358 ch = wgetch(view->window);
1359 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1360 view->count = 0;
1361 else
1362 ch = view->ch;
1363 } else {
1364 ch = wgetch(view->window);
1365 if (ch >= '1' && ch <= '9')
1366 view->ch = ch = get_compound_key(view, ch);
1368 errcode = pthread_mutex_lock(&tog_mutex);
1369 if (errcode)
1370 return got_error_set_errno(errcode, "pthread_mutex_lock");
1371 nodelay(view->window, TRUE);
1373 if (tog_sigwinch_received || tog_sigcont_received) {
1374 tog_resizeterm();
1375 tog_sigwinch_received = 0;
1376 tog_sigcont_received = 0;
1377 TAILQ_FOREACH(v, views, entry) {
1378 err = view_resize(v);
1379 if (err)
1380 return err;
1381 err = v->input(new, v, KEY_RESIZE);
1382 if (err)
1383 return err;
1384 if (v->child) {
1385 err = view_resize(v->child);
1386 if (err)
1387 return err;
1388 err = v->child->input(new, v->child,
1389 KEY_RESIZE);
1390 if (err)
1391 return err;
1392 if (v->child->resized_x || v->child->resized_y) {
1393 err = view_resize_split(v, 0);
1394 if (err)
1395 return err;
1401 switch (ch) {
1402 case '\t':
1403 view->count = 0;
1404 if (view->child) {
1405 view->focussed = 0;
1406 view->child->focussed = 1;
1407 view->focus_child = 1;
1408 } else if (view->parent) {
1409 view->focussed = 0;
1410 view->parent->focussed = 1;
1411 view->parent->focus_child = 0;
1412 if (!view_is_splitscreen(view)) {
1413 if (view->parent->resize) {
1414 err = view->parent->resize(view->parent,
1415 0);
1416 if (err)
1417 return err;
1419 offset_selection_up(view->parent);
1420 err = view_fullscreen(view->parent);
1421 if (err)
1422 return err;
1425 break;
1426 case 'q':
1427 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1428 if (view->parent->resize) {
1429 /* might need more commits to fill fullscreen */
1430 err = view->parent->resize(view->parent, 0);
1431 if (err)
1432 break;
1434 offset_selection_up(view->parent);
1436 err = view->input(new, view, ch);
1437 view->dying = 1;
1438 break;
1439 case 'Q':
1440 *done = 1;
1441 break;
1442 case 'F':
1443 view->count = 0;
1444 if (view_is_parent_view(view)) {
1445 if (view->child == NULL)
1446 break;
1447 if (view_is_splitscreen(view->child)) {
1448 view->focussed = 0;
1449 view->child->focussed = 1;
1450 err = view_fullscreen(view->child);
1451 } else {
1452 err = view_splitscreen(view->child);
1453 if (!err)
1454 err = view_resize_split(view, 0);
1456 if (err)
1457 break;
1458 err = view->child->input(new, view->child,
1459 KEY_RESIZE);
1460 } else {
1461 if (view_is_splitscreen(view)) {
1462 view->parent->focussed = 0;
1463 view->focussed = 1;
1464 err = view_fullscreen(view);
1465 } else {
1466 err = view_splitscreen(view);
1467 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1468 err = view_resize(view->parent);
1469 if (!err)
1470 err = view_resize_split(view, 0);
1472 if (err)
1473 break;
1474 err = view->input(new, view, KEY_RESIZE);
1476 if (err)
1477 break;
1478 if (view->resize) {
1479 err = view->resize(view, 0);
1480 if (err)
1481 break;
1483 if (view->parent)
1484 err = offset_selection_down(view->parent);
1485 if (!err)
1486 err = offset_selection_down(view);
1487 break;
1488 case 'S':
1489 view->count = 0;
1490 err = switch_split(view);
1491 break;
1492 case '-':
1493 err = view_resize_split(view, -1);
1494 break;
1495 case '+':
1496 err = view_resize_split(view, 1);
1497 break;
1498 case KEY_RESIZE:
1499 break;
1500 case '/':
1501 view->count = 0;
1502 if (view->search_start)
1503 view_search_start(view);
1504 else
1505 err = view->input(new, view, ch);
1506 break;
1507 case 'N':
1508 case 'n':
1509 if (view->search_started && view->search_next) {
1510 view->searching = (ch == 'n' ?
1511 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1512 view->search_next_done = 0;
1513 view->search_next(view);
1514 } else
1515 err = view->input(new, view, ch);
1516 break;
1517 case 'A':
1518 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1519 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1520 else
1521 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1522 TAILQ_FOREACH(v, views, entry) {
1523 if (v->reset) {
1524 err = v->reset(v);
1525 if (err)
1526 return err;
1528 if (v->child && v->child->reset) {
1529 err = v->child->reset(v->child);
1530 if (err)
1531 return err;
1534 break;
1535 default:
1536 err = view->input(new, view, ch);
1537 break;
1540 return err;
1543 static int
1544 view_needs_focus_indication(struct tog_view *view)
1546 if (view_is_parent_view(view)) {
1547 if (view->child == NULL || view->child->focussed)
1548 return 0;
1549 if (!view_is_splitscreen(view->child))
1550 return 0;
1551 } else if (!view_is_splitscreen(view))
1552 return 0;
1554 return view->focussed;
1557 static const struct got_error *
1558 view_loop(struct tog_view *view)
1560 const struct got_error *err = NULL;
1561 struct tog_view_list_head views;
1562 struct tog_view *new_view;
1563 char *mode;
1564 int fast_refresh = 10;
1565 int done = 0, errcode;
1567 mode = getenv("TOG_VIEW_SPLIT_MODE");
1568 if (!mode || !(*mode == 'h' || *mode == 'H'))
1569 view->mode = TOG_VIEW_SPLIT_VERT;
1570 else
1571 view->mode = TOG_VIEW_SPLIT_HRZN;
1573 errcode = pthread_mutex_lock(&tog_mutex);
1574 if (errcode)
1575 return got_error_set_errno(errcode, "pthread_mutex_lock");
1577 TAILQ_INIT(&views);
1578 TAILQ_INSERT_HEAD(&views, view, entry);
1580 view->focussed = 1;
1581 err = view->show(view);
1582 if (err)
1583 return err;
1584 update_panels();
1585 doupdate();
1586 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1587 !tog_fatal_signal_received()) {
1588 /* Refresh fast during initialization, then become slower. */
1589 if (fast_refresh && fast_refresh-- == 0)
1590 halfdelay(10); /* switch to once per second */
1592 err = view_input(&new_view, &done, view, &views);
1593 if (err)
1594 break;
1595 if (view->dying) {
1596 struct tog_view *v, *prev = NULL;
1598 if (view_is_parent_view(view))
1599 prev = TAILQ_PREV(view, tog_view_list_head,
1600 entry);
1601 else if (view->parent)
1602 prev = view->parent;
1604 if (view->parent) {
1605 view->parent->child = NULL;
1606 view->parent->focus_child = 0;
1607 /* Restore fullscreen line height. */
1608 view->parent->nlines = view->parent->lines;
1609 err = view_resize(view->parent);
1610 if (err)
1611 break;
1612 /* Make resized splits persist. */
1613 view_transfer_size(view->parent, view);
1614 } else
1615 TAILQ_REMOVE(&views, view, entry);
1617 err = view_close(view);
1618 if (err)
1619 goto done;
1621 view = NULL;
1622 TAILQ_FOREACH(v, &views, entry) {
1623 if (v->focussed)
1624 break;
1626 if (view == NULL && new_view == NULL) {
1627 /* No view has focus. Try to pick one. */
1628 if (prev)
1629 view = prev;
1630 else if (!TAILQ_EMPTY(&views)) {
1631 view = TAILQ_LAST(&views,
1632 tog_view_list_head);
1634 if (view) {
1635 if (view->focus_child) {
1636 view->child->focussed = 1;
1637 view = view->child;
1638 } else
1639 view->focussed = 1;
1643 if (new_view) {
1644 struct tog_view *v, *t;
1645 /* Only allow one parent view per type. */
1646 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1647 if (v->type != new_view->type)
1648 continue;
1649 TAILQ_REMOVE(&views, v, entry);
1650 err = view_close(v);
1651 if (err)
1652 goto done;
1653 break;
1655 TAILQ_INSERT_TAIL(&views, new_view, entry);
1656 view = new_view;
1658 if (view) {
1659 if (view_is_parent_view(view)) {
1660 if (view->child && view->child->focussed)
1661 view = view->child;
1662 } else {
1663 if (view->parent && view->parent->focussed)
1664 view = view->parent;
1666 show_panel(view->panel);
1667 if (view->child && view_is_splitscreen(view->child))
1668 show_panel(view->child->panel);
1669 if (view->parent && view_is_splitscreen(view)) {
1670 err = view->parent->show(view->parent);
1671 if (err)
1672 goto done;
1674 err = view->show(view);
1675 if (err)
1676 goto done;
1677 if (view->child) {
1678 err = view->child->show(view->child);
1679 if (err)
1680 goto done;
1682 update_panels();
1683 doupdate();
1686 done:
1687 while (!TAILQ_EMPTY(&views)) {
1688 const struct got_error *close_err;
1689 view = TAILQ_FIRST(&views);
1690 TAILQ_REMOVE(&views, view, entry);
1691 close_err = view_close(view);
1692 if (close_err && err == NULL)
1693 err = close_err;
1696 errcode = pthread_mutex_unlock(&tog_mutex);
1697 if (errcode && err == NULL)
1698 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1700 return err;
1703 __dead static void
1704 usage_log(void)
1706 endwin();
1707 fprintf(stderr,
1708 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1709 getprogname());
1710 exit(1);
1713 /* Create newly allocated wide-character string equivalent to a byte string. */
1714 static const struct got_error *
1715 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1717 char *vis = NULL;
1718 const struct got_error *err = NULL;
1720 *ws = NULL;
1721 *wlen = mbstowcs(NULL, s, 0);
1722 if (*wlen == (size_t)-1) {
1723 int vislen;
1724 if (errno != EILSEQ)
1725 return got_error_from_errno("mbstowcs");
1727 /* byte string invalid in current encoding; try to "fix" it */
1728 err = got_mbsavis(&vis, &vislen, s);
1729 if (err)
1730 return err;
1731 *wlen = mbstowcs(NULL, vis, 0);
1732 if (*wlen == (size_t)-1) {
1733 err = got_error_from_errno("mbstowcs"); /* give up */
1734 goto done;
1738 *ws = calloc(*wlen + 1, sizeof(**ws));
1739 if (*ws == NULL) {
1740 err = got_error_from_errno("calloc");
1741 goto done;
1744 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1745 err = got_error_from_errno("mbstowcs");
1746 done:
1747 free(vis);
1748 if (err) {
1749 free(*ws);
1750 *ws = NULL;
1751 *wlen = 0;
1753 return err;
1756 static const struct got_error *
1757 expand_tab(char **ptr, const char *src)
1759 char *dst;
1760 size_t len, n, idx = 0, sz = 0;
1762 *ptr = NULL;
1763 n = len = strlen(src);
1764 dst = malloc(n + 1);
1765 if (dst == NULL)
1766 return got_error_from_errno("malloc");
1768 while (idx < len && src[idx]) {
1769 const char c = src[idx];
1771 if (c == '\t') {
1772 size_t nb = TABSIZE - sz % TABSIZE;
1773 char *p;
1775 p = realloc(dst, n + nb);
1776 if (p == NULL) {
1777 free(dst);
1778 return got_error_from_errno("realloc");
1781 dst = p;
1782 n += nb;
1783 memset(dst + sz, ' ', nb);
1784 sz += nb;
1785 } else
1786 dst[sz++] = src[idx];
1787 ++idx;
1790 dst[sz] = '\0';
1791 *ptr = dst;
1792 return NULL;
1796 * Advance at most n columns from wline starting at offset off.
1797 * Return the index to the first character after the span operation.
1798 * Return the combined column width of all spanned wide character in
1799 * *rcol.
1801 static int
1802 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1804 int width, i, cols = 0;
1806 if (n == 0) {
1807 *rcol = cols;
1808 return off;
1811 for (i = off; wline[i] != L'\0'; ++i) {
1812 if (wline[i] == L'\t')
1813 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1814 else
1815 width = wcwidth(wline[i]);
1817 if (width == -1) {
1818 width = 1;
1819 wline[i] = L'.';
1822 if (cols + width > n)
1823 break;
1824 cols += width;
1827 *rcol = cols;
1828 return i;
1832 * Format a line for display, ensuring that it won't overflow a width limit.
1833 * With scrolling, the width returned refers to the scrolled version of the
1834 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1836 static const struct got_error *
1837 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1838 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1840 const struct got_error *err = NULL;
1841 int cols;
1842 wchar_t *wline = NULL;
1843 char *exstr = NULL;
1844 size_t wlen;
1845 int i, scrollx;
1847 *wlinep = NULL;
1848 *widthp = 0;
1850 if (expand) {
1851 err = expand_tab(&exstr, line);
1852 if (err)
1853 return err;
1856 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1857 free(exstr);
1858 if (err)
1859 return err;
1861 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1863 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1864 wline[wlen - 1] = L'\0';
1865 wlen--;
1867 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1868 wline[wlen - 1] = L'\0';
1869 wlen--;
1872 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1873 wline[i] = L'\0';
1875 if (widthp)
1876 *widthp = cols;
1877 if (scrollxp)
1878 *scrollxp = scrollx;
1879 if (err)
1880 free(wline);
1881 else
1882 *wlinep = wline;
1883 return err;
1886 static const struct got_error*
1887 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1888 struct got_object_id *id, struct got_repository *repo)
1890 static const struct got_error *err = NULL;
1891 struct got_reflist_entry *re;
1892 char *s;
1893 const char *name;
1895 *refs_str = NULL;
1897 TAILQ_FOREACH(re, refs, entry) {
1898 struct got_tag_object *tag = NULL;
1899 struct got_object_id *ref_id;
1900 int cmp;
1902 name = got_ref_get_name(re->ref);
1903 if (strcmp(name, GOT_REF_HEAD) == 0)
1904 continue;
1905 if (strncmp(name, "refs/", 5) == 0)
1906 name += 5;
1907 if (strncmp(name, "got/", 4) == 0 &&
1908 strncmp(name, "got/backup/", 11) != 0)
1909 continue;
1910 if (strncmp(name, "heads/", 6) == 0)
1911 name += 6;
1912 if (strncmp(name, "remotes/", 8) == 0) {
1913 name += 8;
1914 s = strstr(name, "/" GOT_REF_HEAD);
1915 if (s != NULL && s[strlen(s)] == '\0')
1916 continue;
1918 err = got_ref_resolve(&ref_id, repo, re->ref);
1919 if (err)
1920 break;
1921 if (strncmp(name, "tags/", 5) == 0) {
1922 err = got_object_open_as_tag(&tag, repo, ref_id);
1923 if (err) {
1924 if (err->code != GOT_ERR_OBJ_TYPE) {
1925 free(ref_id);
1926 break;
1928 /* Ref points at something other than a tag. */
1929 err = NULL;
1930 tag = NULL;
1933 cmp = got_object_id_cmp(tag ?
1934 got_object_tag_get_object_id(tag) : ref_id, id);
1935 free(ref_id);
1936 if (tag)
1937 got_object_tag_close(tag);
1938 if (cmp != 0)
1939 continue;
1940 s = *refs_str;
1941 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1942 s ? ", " : "", name) == -1) {
1943 err = got_error_from_errno("asprintf");
1944 free(s);
1945 *refs_str = NULL;
1946 break;
1948 free(s);
1951 return err;
1954 static const struct got_error *
1955 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1956 int col_tab_align)
1958 char *smallerthan;
1960 smallerthan = strchr(author, '<');
1961 if (smallerthan && smallerthan[1] != '\0')
1962 author = smallerthan + 1;
1963 author[strcspn(author, "@>")] = '\0';
1964 return format_line(wauthor, author_width, NULL, author, 0, limit,
1965 col_tab_align, 0);
1968 static const struct got_error *
1969 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1970 struct got_object_id *id, const size_t date_display_cols,
1971 int author_display_cols)
1973 struct tog_log_view_state *s = &view->state.log;
1974 const struct got_error *err = NULL;
1975 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1976 char *logmsg0 = NULL, *logmsg = NULL;
1977 char *author = NULL;
1978 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1979 int author_width, logmsg_width;
1980 char *newline, *line = NULL;
1981 int col, limit, scrollx;
1982 const int avail = view->ncols;
1983 struct tm tm;
1984 time_t committer_time;
1985 struct tog_color *tc;
1987 committer_time = got_object_commit_get_committer_time(commit);
1988 if (gmtime_r(&committer_time, &tm) == NULL)
1989 return got_error_from_errno("gmtime_r");
1990 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1991 return got_error(GOT_ERR_NO_SPACE);
1993 if (avail <= date_display_cols)
1994 limit = MIN(sizeof(datebuf) - 1, avail);
1995 else
1996 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1997 tc = get_color(&s->colors, TOG_COLOR_DATE);
1998 if (tc)
1999 wattr_on(view->window,
2000 COLOR_PAIR(tc->colorpair), NULL);
2001 waddnstr(view->window, datebuf, limit);
2002 if (tc)
2003 wattr_off(view->window,
2004 COLOR_PAIR(tc->colorpair), NULL);
2005 col = limit;
2006 if (col > avail)
2007 goto done;
2009 if (avail >= 120) {
2010 char *id_str;
2011 err = got_object_id_str(&id_str, id);
2012 if (err)
2013 goto done;
2014 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2015 if (tc)
2016 wattr_on(view->window,
2017 COLOR_PAIR(tc->colorpair), NULL);
2018 wprintw(view->window, "%.8s ", id_str);
2019 if (tc)
2020 wattr_off(view->window,
2021 COLOR_PAIR(tc->colorpair), NULL);
2022 free(id_str);
2023 col += 9;
2024 if (col > avail)
2025 goto done;
2028 if (s->use_committer)
2029 author = strdup(got_object_commit_get_committer(commit));
2030 else
2031 author = strdup(got_object_commit_get_author(commit));
2032 if (author == NULL) {
2033 err = got_error_from_errno("strdup");
2034 goto done;
2036 err = format_author(&wauthor, &author_width, author, avail - col, col);
2037 if (err)
2038 goto done;
2039 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2040 if (tc)
2041 wattr_on(view->window,
2042 COLOR_PAIR(tc->colorpair), NULL);
2043 waddwstr(view->window, wauthor);
2044 if (tc)
2045 wattr_off(view->window,
2046 COLOR_PAIR(tc->colorpair), NULL);
2047 col += author_width;
2048 while (col < avail && author_width < author_display_cols + 2) {
2049 waddch(view->window, ' ');
2050 col++;
2051 author_width++;
2053 if (col > avail)
2054 goto done;
2056 err = got_object_commit_get_logmsg(&logmsg0, commit);
2057 if (err)
2058 goto done;
2059 logmsg = logmsg0;
2060 while (*logmsg == '\n')
2061 logmsg++;
2062 newline = strchr(logmsg, '\n');
2063 if (newline)
2064 *newline = '\0';
2065 limit = avail - col;
2066 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2067 limit--; /* for the border */
2068 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2069 limit, col, 1);
2070 if (err)
2071 goto done;
2072 waddwstr(view->window, &wlogmsg[scrollx]);
2073 col += MAX(logmsg_width, 0);
2074 while (col < avail) {
2075 waddch(view->window, ' ');
2076 col++;
2078 done:
2079 free(logmsg0);
2080 free(wlogmsg);
2081 free(author);
2082 free(wauthor);
2083 free(line);
2084 return err;
2087 static struct commit_queue_entry *
2088 alloc_commit_queue_entry(struct got_commit_object *commit,
2089 struct got_object_id *id)
2091 struct commit_queue_entry *entry;
2093 entry = calloc(1, sizeof(*entry));
2094 if (entry == NULL)
2095 return NULL;
2097 entry->id = id;
2098 entry->commit = commit;
2099 return entry;
2102 static void
2103 pop_commit(struct commit_queue *commits)
2105 struct commit_queue_entry *entry;
2107 entry = TAILQ_FIRST(&commits->head);
2108 TAILQ_REMOVE(&commits->head, entry, entry);
2109 got_object_commit_close(entry->commit);
2110 commits->ncommits--;
2111 /* Don't free entry->id! It is owned by the commit graph. */
2112 free(entry);
2115 static void
2116 free_commits(struct commit_queue *commits)
2118 while (!TAILQ_EMPTY(&commits->head))
2119 pop_commit(commits);
2122 static const struct got_error *
2123 match_commit(int *have_match, struct got_object_id *id,
2124 struct got_commit_object *commit, regex_t *regex)
2126 const struct got_error *err = NULL;
2127 regmatch_t regmatch;
2128 char *id_str = NULL, *logmsg = NULL;
2130 *have_match = 0;
2132 err = got_object_id_str(&id_str, id);
2133 if (err)
2134 return err;
2136 err = got_object_commit_get_logmsg(&logmsg, commit);
2137 if (err)
2138 goto done;
2140 if (regexec(regex, got_object_commit_get_author(commit), 1,
2141 &regmatch, 0) == 0 ||
2142 regexec(regex, got_object_commit_get_committer(commit), 1,
2143 &regmatch, 0) == 0 ||
2144 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2145 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2146 *have_match = 1;
2147 done:
2148 free(id_str);
2149 free(logmsg);
2150 return err;
2153 static const struct got_error *
2154 queue_commits(struct tog_log_thread_args *a)
2156 const struct got_error *err = NULL;
2159 * We keep all commits open throughout the lifetime of the log
2160 * view in order to avoid having to re-fetch commits from disk
2161 * while updating the display.
2163 do {
2164 struct got_object_id *id;
2165 struct got_commit_object *commit;
2166 struct commit_queue_entry *entry;
2167 int errcode;
2169 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2170 NULL, NULL);
2171 if (err || id == NULL)
2172 break;
2174 err = got_object_open_as_commit(&commit, a->repo, id);
2175 if (err)
2176 break;
2177 entry = alloc_commit_queue_entry(commit, id);
2178 if (entry == NULL) {
2179 err = got_error_from_errno("alloc_commit_queue_entry");
2180 break;
2183 errcode = pthread_mutex_lock(&tog_mutex);
2184 if (errcode) {
2185 err = got_error_set_errno(errcode,
2186 "pthread_mutex_lock");
2187 break;
2190 entry->idx = a->commits->ncommits;
2191 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2192 a->commits->ncommits++;
2194 if (*a->searching == TOG_SEARCH_FORWARD &&
2195 !*a->search_next_done) {
2196 int have_match;
2197 err = match_commit(&have_match, id, commit, a->regex);
2198 if (err)
2199 break;
2200 if (have_match)
2201 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2204 errcode = pthread_mutex_unlock(&tog_mutex);
2205 if (errcode && err == NULL)
2206 err = got_error_set_errno(errcode,
2207 "pthread_mutex_unlock");
2208 if (err)
2209 break;
2210 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2212 return err;
2215 static void
2216 select_commit(struct tog_log_view_state *s)
2218 struct commit_queue_entry *entry;
2219 int ncommits = 0;
2221 entry = s->first_displayed_entry;
2222 while (entry) {
2223 if (ncommits == s->selected) {
2224 s->selected_entry = entry;
2225 break;
2227 entry = TAILQ_NEXT(entry, entry);
2228 ncommits++;
2232 static const struct got_error *
2233 draw_commits(struct tog_view *view)
2235 const struct got_error *err = NULL;
2236 struct tog_log_view_state *s = &view->state.log;
2237 struct commit_queue_entry *entry = s->selected_entry;
2238 const int limit = view->nlines;
2239 int width;
2240 int ncommits, author_cols = 4;
2241 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2242 char *refs_str = NULL;
2243 wchar_t *wline;
2244 struct tog_color *tc;
2245 static const size_t date_display_cols = 12;
2247 if (s->selected_entry &&
2248 !(view->searching && view->search_next_done == 0)) {
2249 struct got_reflist_head *refs;
2250 err = got_object_id_str(&id_str, s->selected_entry->id);
2251 if (err)
2252 return err;
2253 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2254 s->selected_entry->id);
2255 if (refs) {
2256 err = build_refs_str(&refs_str, refs,
2257 s->selected_entry->id, s->repo);
2258 if (err)
2259 goto done;
2263 if (s->thread_args.commits_needed == 0)
2264 halfdelay(10); /* disable fast refresh */
2266 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2267 if (asprintf(&ncommits_str, " [%d/%d] %s",
2268 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2269 (view->searching && !view->search_next_done) ?
2270 "searching..." : "loading...") == -1) {
2271 err = got_error_from_errno("asprintf");
2272 goto done;
2274 } else {
2275 const char *search_str = NULL;
2277 if (view->searching) {
2278 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2279 search_str = "no more matches";
2280 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2281 search_str = "no matches found";
2282 else if (!view->search_next_done)
2283 search_str = "searching...";
2286 if (asprintf(&ncommits_str, " [%d/%d] %s",
2287 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2288 search_str ? search_str :
2289 (refs_str ? refs_str : "")) == -1) {
2290 err = got_error_from_errno("asprintf");
2291 goto done;
2295 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2296 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2297 "........................................",
2298 s->in_repo_path, ncommits_str) == -1) {
2299 err = got_error_from_errno("asprintf");
2300 header = NULL;
2301 goto done;
2303 } else if (asprintf(&header, "commit %s%s",
2304 id_str ? id_str : "........................................",
2305 ncommits_str) == -1) {
2306 err = got_error_from_errno("asprintf");
2307 header = NULL;
2308 goto done;
2310 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2311 if (err)
2312 goto done;
2314 werase(view->window);
2316 if (view_needs_focus_indication(view))
2317 wstandout(view->window);
2318 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2319 if (tc)
2320 wattr_on(view->window,
2321 COLOR_PAIR(tc->colorpair), NULL);
2322 waddwstr(view->window, wline);
2323 if (tc)
2324 wattr_off(view->window,
2325 COLOR_PAIR(tc->colorpair), NULL);
2326 while (width < view->ncols) {
2327 waddch(view->window, ' ');
2328 width++;
2330 if (view_needs_focus_indication(view))
2331 wstandend(view->window);
2332 free(wline);
2333 if (limit <= 1)
2334 goto done;
2336 /* Grow author column size if necessary, and set view->maxx. */
2337 entry = s->first_displayed_entry;
2338 ncommits = 0;
2339 view->maxx = 0;
2340 while (entry) {
2341 struct got_commit_object *c = entry->commit;
2342 char *author, *eol, *msg, *msg0;
2343 wchar_t *wauthor, *wmsg;
2344 int width;
2345 if (ncommits >= limit - 1)
2346 break;
2347 if (s->use_committer)
2348 author = strdup(got_object_commit_get_committer(c));
2349 else
2350 author = strdup(got_object_commit_get_author(c));
2351 if (author == NULL) {
2352 err = got_error_from_errno("strdup");
2353 goto done;
2355 err = format_author(&wauthor, &width, author, COLS,
2356 date_display_cols);
2357 if (author_cols < width)
2358 author_cols = width;
2359 free(wauthor);
2360 free(author);
2361 if (err)
2362 goto done;
2363 err = got_object_commit_get_logmsg(&msg0, c);
2364 if (err)
2365 goto done;
2366 msg = msg0;
2367 while (*msg == '\n')
2368 ++msg;
2369 if ((eol = strchr(msg, '\n')))
2370 *eol = '\0';
2371 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2372 date_display_cols + author_cols, 0);
2373 if (err)
2374 goto done;
2375 view->maxx = MAX(view->maxx, width);
2376 free(msg0);
2377 free(wmsg);
2378 ncommits++;
2379 entry = TAILQ_NEXT(entry, entry);
2382 entry = s->first_displayed_entry;
2383 s->last_displayed_entry = s->first_displayed_entry;
2384 ncommits = 0;
2385 while (entry) {
2386 if (ncommits >= limit - 1)
2387 break;
2388 if (ncommits == s->selected)
2389 wstandout(view->window);
2390 err = draw_commit(view, entry->commit, entry->id,
2391 date_display_cols, author_cols);
2392 if (ncommits == s->selected)
2393 wstandend(view->window);
2394 if (err)
2395 goto done;
2396 ncommits++;
2397 s->last_displayed_entry = entry;
2398 entry = TAILQ_NEXT(entry, entry);
2401 view_border(view);
2402 done:
2403 free(id_str);
2404 free(refs_str);
2405 free(ncommits_str);
2406 free(header);
2407 return err;
2410 static void
2411 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2413 struct commit_queue_entry *entry;
2414 int nscrolled = 0;
2416 entry = TAILQ_FIRST(&s->commits.head);
2417 if (s->first_displayed_entry == entry)
2418 return;
2420 entry = s->first_displayed_entry;
2421 while (entry && nscrolled < maxscroll) {
2422 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2423 if (entry) {
2424 s->first_displayed_entry = entry;
2425 nscrolled++;
2430 static const struct got_error *
2431 trigger_log_thread(struct tog_view *view, int wait)
2433 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2434 int errcode;
2436 halfdelay(1); /* fast refresh while loading commits */
2438 while (!ta->log_complete && !tog_thread_error &&
2439 (ta->commits_needed > 0 || ta->load_all)) {
2440 /* Wake the log thread. */
2441 errcode = pthread_cond_signal(&ta->need_commits);
2442 if (errcode)
2443 return got_error_set_errno(errcode,
2444 "pthread_cond_signal");
2447 * The mutex will be released while the view loop waits
2448 * in wgetch(), at which time the log thread will run.
2450 if (!wait)
2451 break;
2453 /* Display progress update in log view. */
2454 show_log_view(view);
2455 update_panels();
2456 doupdate();
2458 /* Wait right here while next commit is being loaded. */
2459 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2460 if (errcode)
2461 return got_error_set_errno(errcode,
2462 "pthread_cond_wait");
2464 /* Display progress update in log view. */
2465 show_log_view(view);
2466 update_panels();
2467 doupdate();
2470 return NULL;
2473 static const struct got_error *
2474 request_log_commits(struct tog_view *view)
2476 struct tog_log_view_state *state = &view->state.log;
2477 const struct got_error *err = NULL;
2479 if (state->thread_args.log_complete)
2480 return NULL;
2482 state->thread_args.commits_needed += view->nscrolled;
2483 err = trigger_log_thread(view, 1);
2484 view->nscrolled = 0;
2486 return err;
2489 static const struct got_error *
2490 log_scroll_down(struct tog_view *view, int maxscroll)
2492 struct tog_log_view_state *s = &view->state.log;
2493 const struct got_error *err = NULL;
2494 struct commit_queue_entry *pentry;
2495 int nscrolled = 0, ncommits_needed;
2497 if (s->last_displayed_entry == NULL)
2498 return NULL;
2500 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2501 if (s->commits.ncommits < ncommits_needed &&
2502 !s->thread_args.log_complete) {
2504 * Ask the log thread for required amount of commits.
2506 s->thread_args.commits_needed += maxscroll;
2507 err = trigger_log_thread(view, 1);
2508 if (err)
2509 return err;
2512 do {
2513 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2514 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2515 break;
2517 s->last_displayed_entry = pentry ?
2518 pentry : s->last_displayed_entry;;
2520 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2521 if (pentry == NULL)
2522 break;
2523 s->first_displayed_entry = pentry;
2524 } while (++nscrolled < maxscroll);
2526 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2527 view->nscrolled += nscrolled;
2528 else
2529 view->nscrolled = 0;
2531 return err;
2534 static const struct got_error *
2535 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2536 struct got_commit_object *commit, struct got_object_id *commit_id,
2537 struct tog_view *log_view, struct got_repository *repo)
2539 const struct got_error *err;
2540 struct got_object_qid *parent_id;
2541 struct tog_view *diff_view;
2543 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2544 if (diff_view == NULL)
2545 return got_error_from_errno("view_open");
2547 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2548 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2549 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2550 if (err == NULL)
2551 *new_view = diff_view;
2552 return err;
2555 static const struct got_error *
2556 tree_view_visit_subtree(struct tog_tree_view_state *s,
2557 struct got_tree_object *subtree)
2559 struct tog_parent_tree *parent;
2561 parent = calloc(1, sizeof(*parent));
2562 if (parent == NULL)
2563 return got_error_from_errno("calloc");
2565 parent->tree = s->tree;
2566 parent->first_displayed_entry = s->first_displayed_entry;
2567 parent->selected_entry = s->selected_entry;
2568 parent->selected = s->selected;
2569 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2570 s->tree = subtree;
2571 s->selected = 0;
2572 s->first_displayed_entry = NULL;
2573 return NULL;
2576 static const struct got_error *
2577 tree_view_walk_path(struct tog_tree_view_state *s,
2578 struct got_commit_object *commit, const char *path)
2580 const struct got_error *err = NULL;
2581 struct got_tree_object *tree = NULL;
2582 const char *p;
2583 char *slash, *subpath = NULL;
2585 /* Walk the path and open corresponding tree objects. */
2586 p = path;
2587 while (*p) {
2588 struct got_tree_entry *te;
2589 struct got_object_id *tree_id;
2590 char *te_name;
2592 while (p[0] == '/')
2593 p++;
2595 /* Ensure the correct subtree entry is selected. */
2596 slash = strchr(p, '/');
2597 if (slash == NULL)
2598 te_name = strdup(p);
2599 else
2600 te_name = strndup(p, slash - p);
2601 if (te_name == NULL) {
2602 err = got_error_from_errno("strndup");
2603 break;
2605 te = got_object_tree_find_entry(s->tree, te_name);
2606 if (te == NULL) {
2607 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2608 free(te_name);
2609 break;
2611 free(te_name);
2612 s->first_displayed_entry = s->selected_entry = te;
2614 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2615 break; /* jump to this file's entry */
2617 slash = strchr(p, '/');
2618 if (slash)
2619 subpath = strndup(path, slash - path);
2620 else
2621 subpath = strdup(path);
2622 if (subpath == NULL) {
2623 err = got_error_from_errno("strdup");
2624 break;
2627 err = got_object_id_by_path(&tree_id, s->repo, commit,
2628 subpath);
2629 if (err)
2630 break;
2632 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2633 free(tree_id);
2634 if (err)
2635 break;
2637 err = tree_view_visit_subtree(s, tree);
2638 if (err) {
2639 got_object_tree_close(tree);
2640 break;
2642 if (slash == NULL)
2643 break;
2644 free(subpath);
2645 subpath = NULL;
2646 p = slash;
2649 free(subpath);
2650 return err;
2653 static const struct got_error *
2654 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2655 struct commit_queue_entry *entry, const char *path,
2656 const char *head_ref_name, struct got_repository *repo)
2658 const struct got_error *err = NULL;
2659 struct tog_tree_view_state *s;
2660 struct tog_view *tree_view;
2662 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2663 if (tree_view == NULL)
2664 return got_error_from_errno("view_open");
2666 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2667 if (err)
2668 return err;
2669 s = &tree_view->state.tree;
2671 *new_view = tree_view;
2673 if (got_path_is_root_dir(path))
2674 return NULL;
2676 return tree_view_walk_path(s, entry->commit, path);
2679 static const struct got_error *
2680 block_signals_used_by_main_thread(void)
2682 sigset_t sigset;
2683 int errcode;
2685 if (sigemptyset(&sigset) == -1)
2686 return got_error_from_errno("sigemptyset");
2688 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2689 if (sigaddset(&sigset, SIGWINCH) == -1)
2690 return got_error_from_errno("sigaddset");
2691 if (sigaddset(&sigset, SIGCONT) == -1)
2692 return got_error_from_errno("sigaddset");
2693 if (sigaddset(&sigset, SIGINT) == -1)
2694 return got_error_from_errno("sigaddset");
2695 if (sigaddset(&sigset, SIGTERM) == -1)
2696 return got_error_from_errno("sigaddset");
2698 /* ncurses handles SIGTSTP */
2699 if (sigaddset(&sigset, SIGTSTP) == -1)
2700 return got_error_from_errno("sigaddset");
2702 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2703 if (errcode)
2704 return got_error_set_errno(errcode, "pthread_sigmask");
2706 return NULL;
2709 static void *
2710 log_thread(void *arg)
2712 const struct got_error *err = NULL;
2713 int errcode = 0;
2714 struct tog_log_thread_args *a = arg;
2715 int done = 0;
2718 * Sync startup with main thread such that we begin our
2719 * work once view_input() has released the mutex.
2721 errcode = pthread_mutex_lock(&tog_mutex);
2722 if (errcode) {
2723 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2724 return (void *)err;
2727 err = block_signals_used_by_main_thread();
2728 if (err) {
2729 pthread_mutex_unlock(&tog_mutex);
2730 goto done;
2733 while (!done && !err && !tog_fatal_signal_received()) {
2734 errcode = pthread_mutex_unlock(&tog_mutex);
2735 if (errcode) {
2736 err = got_error_set_errno(errcode,
2737 "pthread_mutex_unlock");
2738 goto done;
2740 err = queue_commits(a);
2741 if (err) {
2742 if (err->code != GOT_ERR_ITER_COMPLETED)
2743 goto done;
2744 err = NULL;
2745 done = 1;
2746 } else if (a->commits_needed > 0 && !a->load_all)
2747 a->commits_needed--;
2749 errcode = pthread_mutex_lock(&tog_mutex);
2750 if (errcode) {
2751 err = got_error_set_errno(errcode,
2752 "pthread_mutex_lock");
2753 goto done;
2754 } else if (*a->quit)
2755 done = 1;
2756 else if (*a->first_displayed_entry == NULL) {
2757 *a->first_displayed_entry =
2758 TAILQ_FIRST(&a->commits->head);
2759 *a->selected_entry = *a->first_displayed_entry;
2762 errcode = pthread_cond_signal(&a->commit_loaded);
2763 if (errcode) {
2764 err = got_error_set_errno(errcode,
2765 "pthread_cond_signal");
2766 pthread_mutex_unlock(&tog_mutex);
2767 goto done;
2770 if (done)
2771 a->commits_needed = 0;
2772 else {
2773 if (a->commits_needed == 0 && !a->load_all) {
2774 errcode = pthread_cond_wait(&a->need_commits,
2775 &tog_mutex);
2776 if (errcode) {
2777 err = got_error_set_errno(errcode,
2778 "pthread_cond_wait");
2779 pthread_mutex_unlock(&tog_mutex);
2780 goto done;
2782 if (*a->quit)
2783 done = 1;
2787 a->log_complete = 1;
2788 errcode = pthread_mutex_unlock(&tog_mutex);
2789 if (errcode)
2790 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2791 done:
2792 if (err) {
2793 tog_thread_error = 1;
2794 pthread_cond_signal(&a->commit_loaded);
2796 return (void *)err;
2799 static const struct got_error *
2800 stop_log_thread(struct tog_log_view_state *s)
2802 const struct got_error *err = NULL, *thread_err = NULL;
2803 int errcode;
2805 if (s->thread) {
2806 s->quit = 1;
2807 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2808 if (errcode)
2809 return got_error_set_errno(errcode,
2810 "pthread_cond_signal");
2811 errcode = pthread_mutex_unlock(&tog_mutex);
2812 if (errcode)
2813 return got_error_set_errno(errcode,
2814 "pthread_mutex_unlock");
2815 errcode = pthread_join(s->thread, (void **)&thread_err);
2816 if (errcode)
2817 return got_error_set_errno(errcode, "pthread_join");
2818 errcode = pthread_mutex_lock(&tog_mutex);
2819 if (errcode)
2820 return got_error_set_errno(errcode,
2821 "pthread_mutex_lock");
2822 s->thread = 0; //NULL;
2825 if (s->thread_args.repo) {
2826 err = got_repo_close(s->thread_args.repo);
2827 s->thread_args.repo = NULL;
2830 if (s->thread_args.pack_fds) {
2831 const struct got_error *pack_err =
2832 got_repo_pack_fds_close(s->thread_args.pack_fds);
2833 if (err == NULL)
2834 err = pack_err;
2835 s->thread_args.pack_fds = NULL;
2838 if (s->thread_args.graph) {
2839 got_commit_graph_close(s->thread_args.graph);
2840 s->thread_args.graph = NULL;
2843 return err ? err : thread_err;
2846 static const struct got_error *
2847 close_log_view(struct tog_view *view)
2849 const struct got_error *err = NULL;
2850 struct tog_log_view_state *s = &view->state.log;
2851 int errcode;
2853 err = stop_log_thread(s);
2855 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2856 if (errcode && err == NULL)
2857 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2859 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2860 if (errcode && err == NULL)
2861 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2863 free_commits(&s->commits);
2864 free(s->in_repo_path);
2865 s->in_repo_path = NULL;
2866 free(s->start_id);
2867 s->start_id = NULL;
2868 free(s->head_ref_name);
2869 s->head_ref_name = NULL;
2870 return err;
2873 static const struct got_error *
2874 search_start_log_view(struct tog_view *view)
2876 struct tog_log_view_state *s = &view->state.log;
2878 s->matched_entry = NULL;
2879 s->search_entry = NULL;
2880 return NULL;
2883 static const struct got_error *
2884 search_next_log_view(struct tog_view *view)
2886 const struct got_error *err = NULL;
2887 struct tog_log_view_state *s = &view->state.log;
2888 struct commit_queue_entry *entry;
2890 /* Display progress update in log view. */
2891 show_log_view(view);
2892 update_panels();
2893 doupdate();
2895 if (s->search_entry) {
2896 int errcode, ch;
2897 errcode = pthread_mutex_unlock(&tog_mutex);
2898 if (errcode)
2899 return got_error_set_errno(errcode,
2900 "pthread_mutex_unlock");
2901 ch = wgetch(view->window);
2902 errcode = pthread_mutex_lock(&tog_mutex);
2903 if (errcode)
2904 return got_error_set_errno(errcode,
2905 "pthread_mutex_lock");
2906 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2907 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2908 return NULL;
2910 if (view->searching == TOG_SEARCH_FORWARD)
2911 entry = TAILQ_NEXT(s->search_entry, entry);
2912 else
2913 entry = TAILQ_PREV(s->search_entry,
2914 commit_queue_head, entry);
2915 } else if (s->matched_entry) {
2916 int matched_idx = s->matched_entry->idx;
2917 int selected_idx = s->selected_entry->idx;
2920 * If the user has moved the cursor after we hit a match,
2921 * the position from where we should continue searching
2922 * might have changed.
2924 if (view->searching == TOG_SEARCH_FORWARD) {
2925 if (matched_idx > selected_idx)
2926 entry = TAILQ_NEXT(s->selected_entry, entry);
2927 else
2928 entry = TAILQ_NEXT(s->matched_entry, entry);
2929 } else {
2930 if (matched_idx < selected_idx)
2931 entry = TAILQ_PREV(s->selected_entry,
2932 commit_queue_head, entry);
2933 else
2934 entry = TAILQ_PREV(s->matched_entry,
2935 commit_queue_head, entry);
2937 } else {
2938 entry = s->selected_entry;
2941 while (1) {
2942 int have_match = 0;
2944 if (entry == NULL) {
2945 if (s->thread_args.log_complete ||
2946 view->searching == TOG_SEARCH_BACKWARD) {
2947 view->search_next_done =
2948 (s->matched_entry == NULL ?
2949 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2950 s->search_entry = NULL;
2951 return NULL;
2954 * Poke the log thread for more commits and return,
2955 * allowing the main loop to make progress. Search
2956 * will resume at s->search_entry once we come back.
2958 s->thread_args.commits_needed++;
2959 return trigger_log_thread(view, 0);
2962 err = match_commit(&have_match, entry->id, entry->commit,
2963 &view->regex);
2964 if (err)
2965 break;
2966 if (have_match) {
2967 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2968 s->matched_entry = entry;
2969 break;
2972 s->search_entry = entry;
2973 if (view->searching == TOG_SEARCH_FORWARD)
2974 entry = TAILQ_NEXT(entry, entry);
2975 else
2976 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2979 if (s->matched_entry) {
2980 int cur = s->selected_entry->idx;
2981 while (cur < s->matched_entry->idx) {
2982 err = input_log_view(NULL, view, KEY_DOWN);
2983 if (err)
2984 return err;
2985 cur++;
2987 while (cur > s->matched_entry->idx) {
2988 err = input_log_view(NULL, view, KEY_UP);
2989 if (err)
2990 return err;
2991 cur--;
2995 s->search_entry = NULL;
2997 return NULL;
3000 static const struct got_error *
3001 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3002 struct got_repository *repo, const char *head_ref_name,
3003 const char *in_repo_path, int log_branches)
3005 const struct got_error *err = NULL;
3006 struct tog_log_view_state *s = &view->state.log;
3007 struct got_repository *thread_repo = NULL;
3008 struct got_commit_graph *thread_graph = NULL;
3009 int errcode;
3011 if (in_repo_path != s->in_repo_path) {
3012 free(s->in_repo_path);
3013 s->in_repo_path = strdup(in_repo_path);
3014 if (s->in_repo_path == NULL)
3015 return got_error_from_errno("strdup");
3018 /* The commit queue only contains commits being displayed. */
3019 TAILQ_INIT(&s->commits.head);
3020 s->commits.ncommits = 0;
3022 s->repo = repo;
3023 if (head_ref_name) {
3024 s->head_ref_name = strdup(head_ref_name);
3025 if (s->head_ref_name == NULL) {
3026 err = got_error_from_errno("strdup");
3027 goto done;
3030 s->start_id = got_object_id_dup(start_id);
3031 if (s->start_id == NULL) {
3032 err = got_error_from_errno("got_object_id_dup");
3033 goto done;
3035 s->log_branches = log_branches;
3037 STAILQ_INIT(&s->colors);
3038 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3039 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3040 get_color_value("TOG_COLOR_COMMIT"));
3041 if (err)
3042 goto done;
3043 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3044 get_color_value("TOG_COLOR_AUTHOR"));
3045 if (err) {
3046 free_colors(&s->colors);
3047 goto done;
3049 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3050 get_color_value("TOG_COLOR_DATE"));
3051 if (err) {
3052 free_colors(&s->colors);
3053 goto done;
3057 view->show = show_log_view;
3058 view->input = input_log_view;
3059 view->resize = resize_log_view;
3060 view->close = close_log_view;
3061 view->search_start = search_start_log_view;
3062 view->search_next = search_next_log_view;
3064 if (s->thread_args.pack_fds == NULL) {
3065 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3066 if (err)
3067 goto done;
3069 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3070 s->thread_args.pack_fds);
3071 if (err)
3072 goto done;
3073 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3074 !s->log_branches);
3075 if (err)
3076 goto done;
3077 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3078 s->repo, NULL, NULL);
3079 if (err)
3080 goto done;
3082 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3083 if (errcode) {
3084 err = got_error_set_errno(errcode, "pthread_cond_init");
3085 goto done;
3087 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3088 if (errcode) {
3089 err = got_error_set_errno(errcode, "pthread_cond_init");
3090 goto done;
3093 s->thread_args.commits_needed = view->nlines;
3094 s->thread_args.graph = thread_graph;
3095 s->thread_args.commits = &s->commits;
3096 s->thread_args.in_repo_path = s->in_repo_path;
3097 s->thread_args.start_id = s->start_id;
3098 s->thread_args.repo = thread_repo;
3099 s->thread_args.log_complete = 0;
3100 s->thread_args.quit = &s->quit;
3101 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3102 s->thread_args.selected_entry = &s->selected_entry;
3103 s->thread_args.searching = &view->searching;
3104 s->thread_args.search_next_done = &view->search_next_done;
3105 s->thread_args.regex = &view->regex;
3106 done:
3107 if (err)
3108 close_log_view(view);
3109 return err;
3112 static const struct got_error *
3113 show_log_view(struct tog_view *view)
3115 const struct got_error *err;
3116 struct tog_log_view_state *s = &view->state.log;
3118 if (s->thread == 0) { //NULL) {
3119 int errcode = pthread_create(&s->thread, NULL, log_thread,
3120 &s->thread_args);
3121 if (errcode)
3122 return got_error_set_errno(errcode, "pthread_create");
3123 if (s->thread_args.commits_needed > 0) {
3124 err = trigger_log_thread(view, 1);
3125 if (err)
3126 return err;
3130 return draw_commits(view);
3133 static void
3134 log_move_cursor_up(struct tog_view *view, int page, int home)
3136 struct tog_log_view_state *s = &view->state.log;
3138 if (s->selected_entry->idx == 0)
3139 view->count = 0;
3140 if (s->first_displayed_entry == NULL)
3141 return;
3143 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3144 || home)
3145 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3147 if (!page && !home && s->selected > 0)
3148 --s->selected;
3149 else
3150 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3152 select_commit(s);
3153 return;
3156 static const struct got_error *
3157 log_move_cursor_down(struct tog_view *view, int page)
3159 struct tog_log_view_state *s = &view->state.log;
3160 struct commit_queue_entry *first;
3161 const struct got_error *err = NULL;
3163 first = s->first_displayed_entry;
3164 if (first == NULL) {
3165 view->count = 0;
3166 return NULL;
3169 if (s->thread_args.log_complete &&
3170 s->selected_entry->idx >= s->commits.ncommits - 1)
3171 return NULL;
3173 if (!page) {
3174 int eos = view->nlines - 2;
3176 if (view_is_hsplit_top(view))
3177 --eos; /* border consumes the last line */
3178 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3179 ++s->selected;
3180 else
3181 err = log_scroll_down(view, 1);
3182 } else if (s->thread_args.load_all) {
3183 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3184 s->selected += MIN(s->last_displayed_entry->idx -
3185 s->selected_entry->idx, page + 1);
3186 else
3187 err = log_scroll_down(view, MIN(page,
3188 s->commits.ncommits - s->selected_entry->idx - 1));
3189 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3190 } else {
3191 err = log_scroll_down(view, page);
3192 if (err)
3193 return err;
3194 if (first == s->first_displayed_entry && s->selected <
3195 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3196 s->selected = MIN(s->commits.ncommits - 1, page);
3199 if (err)
3200 return err;
3203 * We might necessarily overshoot in horizontal
3204 * splits; if so, select the last displayed commit.
3206 s->selected = MIN(s->selected,
3207 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3209 select_commit(s);
3211 if (s->thread_args.log_complete &&
3212 s->selected_entry->idx == s->commits.ncommits - 1)
3213 view->count = 0;
3215 return NULL;
3218 static void
3219 view_get_split(struct tog_view *view, int *y, int *x)
3221 *x = 0;
3222 *y = 0;
3224 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3225 if (view->child && view->child->resized_y)
3226 *y = view->child->resized_y;
3227 else if (view->resized_y)
3228 *y = view->resized_y;
3229 else
3230 *y = view_split_begin_y(view->lines);
3231 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3232 if (view->child && view->child->resized_x)
3233 *x = view->child->resized_x;
3234 else if (view->resized_x)
3235 *x = view->resized_x;
3236 else
3237 *x = view_split_begin_x(view->begin_x);
3241 /* Split view horizontally at y and offset view->state->selected line. */
3242 static const struct got_error *
3243 view_init_hsplit(struct tog_view *view, int y)
3245 const struct got_error *err = NULL;
3247 view->nlines = y;
3248 view->ncols = COLS;
3249 err = view_resize(view);
3250 if (err)
3251 return err;
3253 err = offset_selection_down(view);
3255 return err;
3258 static const struct got_error *
3259 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3261 const struct got_error *err = NULL;
3262 struct tog_log_view_state *s = &view->state.log;
3263 struct commit_queue_entry *entry;
3264 int eos, n, nscroll;
3266 if (s->thread_args.load_all) {
3267 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3268 s->thread_args.load_all = 0;
3269 else if (s->thread_args.log_complete) {
3270 err = log_move_cursor_down(view, s->commits.ncommits);
3271 s->thread_args.load_all = 0;
3273 return err;
3276 eos = nscroll = view->nlines - 1;
3277 if (view_is_hsplit_top(view))
3278 --eos; /* border */
3280 switch (ch) {
3281 case 'q':
3282 s->quit = 1;
3283 break;
3284 case '0':
3285 view->x = 0;
3286 break;
3287 case '$':
3288 view->x = MAX(view->maxx - view->ncols / 2, 0);
3289 view->count = 0;
3290 break;
3291 case KEY_RIGHT:
3292 case 'l':
3293 if (view->x + view->ncols / 2 < view->maxx)
3294 view->x += 2; /* move two columns right */
3295 else
3296 view->count = 0;
3297 break;
3298 case KEY_LEFT:
3299 case 'h':
3300 view->x -= MIN(view->x, 2); /* move two columns back */
3301 if (view->x <= 0)
3302 view->count = 0;
3303 break;
3304 case 'k':
3305 case KEY_UP:
3306 case '<':
3307 case ',':
3308 case CTRL('p'):
3309 log_move_cursor_up(view, 0, 0);
3310 break;
3311 case 'g':
3312 case KEY_HOME:
3313 log_move_cursor_up(view, 0, 1);
3314 view->count = 0;
3315 break;
3316 case CTRL('u'):
3317 case 'u':
3318 nscroll /= 2;
3319 /* FALL THROUGH */
3320 case KEY_PPAGE:
3321 case CTRL('b'):
3322 case 'b':
3323 log_move_cursor_up(view, nscroll, 0);
3324 break;
3325 case 'j':
3326 case KEY_DOWN:
3327 case '>':
3328 case '.':
3329 case CTRL('n'):
3330 err = log_move_cursor_down(view, 0);
3331 break;
3332 case '@':
3333 s->use_committer = !s->use_committer;
3334 break;
3335 case 'G':
3336 case KEY_END: {
3337 /* We don't know yet how many commits, so we're forced to
3338 * traverse them all. */
3339 view->count = 0;
3340 if (!s->thread_args.log_complete) {
3341 s->thread_args.load_all = 1;
3342 return trigger_log_thread(view, 0);
3345 s->selected = 0;
3346 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3347 for (n = 0; n < eos; n++) {
3348 if (entry == NULL)
3349 break;
3350 s->first_displayed_entry = entry;
3351 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3353 if (n > 0)
3354 s->selected = n - 1;
3355 select_commit(s);
3356 break;
3358 case CTRL('d'):
3359 case 'd':
3360 nscroll /= 2;
3361 /* FALL THROUGH */
3362 case KEY_NPAGE:
3363 case CTRL('f'):
3364 case 'f':
3365 case ' ':
3366 err = log_move_cursor_down(view, nscroll);
3367 break;
3368 case KEY_RESIZE:
3369 if (s->selected > view->nlines - 2)
3370 s->selected = view->nlines - 2;
3371 if (s->selected > s->commits.ncommits - 1)
3372 s->selected = s->commits.ncommits - 1;
3373 select_commit(s);
3374 if (s->commits.ncommits < view->nlines - 1 &&
3375 !s->thread_args.log_complete) {
3376 s->thread_args.commits_needed += (view->nlines - 1) -
3377 s->commits.ncommits;
3378 err = trigger_log_thread(view, 1);
3380 break;
3381 case KEY_ENTER:
3382 case '\r':
3383 view->count = 0;
3384 if (s->selected_entry == NULL)
3385 break;
3386 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3387 break;
3388 case 'T':
3389 view->count = 0;
3390 if (s->selected_entry == NULL)
3391 break;
3392 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3393 break;
3394 case KEY_BACKSPACE:
3395 case CTRL('l'):
3396 case 'B':
3397 view->count = 0;
3398 if (ch == KEY_BACKSPACE &&
3399 got_path_is_root_dir(s->in_repo_path))
3400 break;
3401 err = stop_log_thread(s);
3402 if (err)
3403 return err;
3404 if (ch == KEY_BACKSPACE) {
3405 char *parent_path;
3406 err = got_path_dirname(&parent_path, s->in_repo_path);
3407 if (err)
3408 return err;
3409 free(s->in_repo_path);
3410 s->in_repo_path = parent_path;
3411 s->thread_args.in_repo_path = s->in_repo_path;
3412 } else if (ch == CTRL('l')) {
3413 struct got_object_id *start_id;
3414 err = got_repo_match_object_id(&start_id, NULL,
3415 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3416 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3417 if (err)
3418 return err;
3419 free(s->start_id);
3420 s->start_id = start_id;
3421 s->thread_args.start_id = s->start_id;
3422 } else /* 'B' */
3423 s->log_branches = !s->log_branches;
3425 if (s->thread_args.pack_fds == NULL) {
3426 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3427 if (err)
3428 return err;
3430 err = got_repo_open(&s->thread_args.repo,
3431 got_repo_get_path(s->repo), NULL,
3432 s->thread_args.pack_fds);
3433 if (err)
3434 return err;
3435 tog_free_refs();
3436 err = tog_load_refs(s->repo, 0);
3437 if (err)
3438 return err;
3439 err = got_commit_graph_open(&s->thread_args.graph,
3440 s->in_repo_path, !s->log_branches);
3441 if (err)
3442 return err;
3443 err = got_commit_graph_iter_start(s->thread_args.graph,
3444 s->start_id, s->repo, NULL, NULL);
3445 if (err)
3446 return err;
3447 free_commits(&s->commits);
3448 s->first_displayed_entry = NULL;
3449 s->last_displayed_entry = NULL;
3450 s->selected_entry = NULL;
3451 s->selected = 0;
3452 s->thread_args.log_complete = 0;
3453 s->quit = 0;
3454 s->thread_args.commits_needed = view->lines;
3455 s->matched_entry = NULL;
3456 s->search_entry = NULL;
3457 view->offset = 0;
3458 break;
3459 case 'R':
3460 view->count = 0;
3461 err = view_request_new(new_view, view, TOG_VIEW_REF);
3462 break;
3463 default:
3464 view->count = 0;
3465 break;
3468 return err;
3471 static const struct got_error *
3472 apply_unveil(const char *repo_path, const char *worktree_path)
3474 const struct got_error *error;
3476 #ifdef PROFILE
3477 if (unveil("gmon.out", "rwc") != 0)
3478 return got_error_from_errno2("unveil", "gmon.out");
3479 #endif
3480 if (repo_path && unveil(repo_path, "r") != 0)
3481 return got_error_from_errno2("unveil", repo_path);
3483 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3484 return got_error_from_errno2("unveil", worktree_path);
3486 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3487 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3489 error = got_privsep_unveil_exec_helpers();
3490 if (error != NULL)
3491 return error;
3493 if (unveil(NULL, NULL) != 0)
3494 return got_error_from_errno("unveil");
3496 return NULL;
3499 static void
3500 init_curses(void)
3503 * Override default signal handlers before starting ncurses.
3504 * This should prevent ncurses from installing its own
3505 * broken cleanup() signal handler.
3507 signal(SIGWINCH, tog_sigwinch);
3508 signal(SIGPIPE, tog_sigpipe);
3509 signal(SIGCONT, tog_sigcont);
3510 signal(SIGINT, tog_sigint);
3511 signal(SIGTERM, tog_sigterm);
3513 initscr();
3514 cbreak();
3515 halfdelay(1); /* Do fast refresh while initial view is loading. */
3516 noecho();
3517 nonl();
3518 intrflush(stdscr, FALSE);
3519 keypad(stdscr, TRUE);
3520 curs_set(0);
3521 if (getenv("TOG_COLORS") != NULL) {
3522 start_color();
3523 use_default_colors();
3527 static const struct got_error *
3528 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3529 struct got_repository *repo, struct got_worktree *worktree)
3531 const struct got_error *err = NULL;
3533 if (argc == 0) {
3534 *in_repo_path = strdup("/");
3535 if (*in_repo_path == NULL)
3536 return got_error_from_errno("strdup");
3537 return NULL;
3540 if (worktree) {
3541 const char *prefix = got_worktree_get_path_prefix(worktree);
3542 char *p;
3544 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3545 if (err)
3546 return err;
3547 if (asprintf(in_repo_path, "%s%s%s", prefix,
3548 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3549 p) == -1) {
3550 err = got_error_from_errno("asprintf");
3551 *in_repo_path = NULL;
3553 free(p);
3554 } else
3555 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3557 return err;
3560 static const struct got_error *
3561 cmd_log(int argc, char *argv[])
3563 const struct got_error *error;
3564 struct got_repository *repo = NULL;
3565 struct got_worktree *worktree = NULL;
3566 struct got_object_id *start_id = NULL;
3567 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3568 char *start_commit = NULL, *label = NULL;
3569 struct got_reference *ref = NULL;
3570 const char *head_ref_name = NULL;
3571 int ch, log_branches = 0;
3572 struct tog_view *view;
3573 int *pack_fds = NULL;
3575 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3576 switch (ch) {
3577 case 'b':
3578 log_branches = 1;
3579 break;
3580 case 'c':
3581 start_commit = optarg;
3582 break;
3583 case 'r':
3584 repo_path = realpath(optarg, NULL);
3585 if (repo_path == NULL)
3586 return got_error_from_errno2("realpath",
3587 optarg);
3588 break;
3589 default:
3590 usage_log();
3591 /* NOTREACHED */
3595 argc -= optind;
3596 argv += optind;
3598 if (argc > 1)
3599 usage_log();
3601 error = got_repo_pack_fds_open(&pack_fds);
3602 if (error != NULL)
3603 goto done;
3605 if (repo_path == NULL) {
3606 cwd = getcwd(NULL, 0);
3607 if (cwd == NULL)
3608 return got_error_from_errno("getcwd");
3609 error = got_worktree_open(&worktree, cwd);
3610 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3611 goto done;
3612 if (worktree)
3613 repo_path =
3614 strdup(got_worktree_get_repo_path(worktree));
3615 else
3616 repo_path = strdup(cwd);
3617 if (repo_path == NULL) {
3618 error = got_error_from_errno("strdup");
3619 goto done;
3623 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3624 if (error != NULL)
3625 goto done;
3627 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3628 repo, worktree);
3629 if (error)
3630 goto done;
3632 init_curses();
3634 error = apply_unveil(got_repo_get_path(repo),
3635 worktree ? got_worktree_get_root_path(worktree) : NULL);
3636 if (error)
3637 goto done;
3639 /* already loaded by tog_log_with_path()? */
3640 if (TAILQ_EMPTY(&tog_refs)) {
3641 error = tog_load_refs(repo, 0);
3642 if (error)
3643 goto done;
3646 if (start_commit == NULL) {
3647 error = got_repo_match_object_id(&start_id, &label,
3648 worktree ? got_worktree_get_head_ref_name(worktree) :
3649 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3650 if (error)
3651 goto done;
3652 head_ref_name = label;
3653 } else {
3654 error = got_ref_open(&ref, repo, start_commit, 0);
3655 if (error == NULL)
3656 head_ref_name = got_ref_get_name(ref);
3657 else if (error->code != GOT_ERR_NOT_REF)
3658 goto done;
3659 error = got_repo_match_object_id(&start_id, NULL,
3660 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3661 if (error)
3662 goto done;
3665 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3666 if (view == NULL) {
3667 error = got_error_from_errno("view_open");
3668 goto done;
3670 error = open_log_view(view, start_id, repo, head_ref_name,
3671 in_repo_path, log_branches);
3672 if (error)
3673 goto done;
3674 if (worktree) {
3675 /* Release work tree lock. */
3676 got_worktree_close(worktree);
3677 worktree = NULL;
3679 error = view_loop(view);
3680 done:
3681 free(in_repo_path);
3682 free(repo_path);
3683 free(cwd);
3684 free(start_id);
3685 free(label);
3686 if (ref)
3687 got_ref_close(ref);
3688 if (repo) {
3689 const struct got_error *close_err = got_repo_close(repo);
3690 if (error == NULL)
3691 error = close_err;
3693 if (worktree)
3694 got_worktree_close(worktree);
3695 if (pack_fds) {
3696 const struct got_error *pack_err =
3697 got_repo_pack_fds_close(pack_fds);
3698 if (error == NULL)
3699 error = pack_err;
3701 tog_free_refs();
3702 return error;
3705 __dead static void
3706 usage_diff(void)
3708 endwin();
3709 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3710 "[-w] object1 object2\n", getprogname());
3711 exit(1);
3714 static int
3715 match_line(const char *line, regex_t *regex, size_t nmatch,
3716 regmatch_t *regmatch)
3718 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3721 static struct tog_color *
3722 match_color(struct tog_colors *colors, const char *line)
3724 struct tog_color *tc = NULL;
3726 STAILQ_FOREACH(tc, colors, entry) {
3727 if (match_line(line, &tc->regex, 0, NULL))
3728 return tc;
3731 return NULL;
3734 static const struct got_error *
3735 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3736 WINDOW *window, int skipcol, regmatch_t *regmatch)
3738 const struct got_error *err = NULL;
3739 char *exstr = NULL;
3740 wchar_t *wline = NULL;
3741 int rme, rms, n, width, scrollx;
3742 int width0 = 0, width1 = 0, width2 = 0;
3743 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3745 *wtotal = 0;
3747 rms = regmatch->rm_so;
3748 rme = regmatch->rm_eo;
3750 err = expand_tab(&exstr, line);
3751 if (err)
3752 return err;
3754 /* Split the line into 3 segments, according to match offsets. */
3755 seg0 = strndup(exstr, rms);
3756 if (seg0 == NULL) {
3757 err = got_error_from_errno("strndup");
3758 goto done;
3760 seg1 = strndup(exstr + rms, rme - rms);
3761 if (seg1 == NULL) {
3762 err = got_error_from_errno("strndup");
3763 goto done;
3765 seg2 = strdup(exstr + rme);
3766 if (seg2 == NULL) {
3767 err = got_error_from_errno("strndup");
3768 goto done;
3771 /* draw up to matched token if we haven't scrolled past it */
3772 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3773 col_tab_align, 1);
3774 if (err)
3775 goto done;
3776 n = MAX(width0 - skipcol, 0);
3777 if (n) {
3778 free(wline);
3779 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3780 wlimit, col_tab_align, 1);
3781 if (err)
3782 goto done;
3783 waddwstr(window, &wline[scrollx]);
3784 wlimit -= width;
3785 *wtotal += width;
3788 if (wlimit > 0) {
3789 int i = 0, w = 0;
3790 size_t wlen;
3792 free(wline);
3793 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3794 col_tab_align, 1);
3795 if (err)
3796 goto done;
3797 wlen = wcslen(wline);
3798 while (i < wlen) {
3799 width = wcwidth(wline[i]);
3800 if (width == -1) {
3801 /* should not happen, tabs are expanded */
3802 err = got_error(GOT_ERR_RANGE);
3803 goto done;
3805 if (width0 + w + width > skipcol)
3806 break;
3807 w += width;
3808 i++;
3810 /* draw (visible part of) matched token (if scrolled into it) */
3811 if (width1 - w > 0) {
3812 wattron(window, A_STANDOUT);
3813 waddwstr(window, &wline[i]);
3814 wattroff(window, A_STANDOUT);
3815 wlimit -= (width1 - w);
3816 *wtotal += (width1 - w);
3820 if (wlimit > 0) { /* draw rest of line */
3821 free(wline);
3822 if (skipcol > width0 + width1) {
3823 err = format_line(&wline, &width2, &scrollx, seg2,
3824 skipcol - (width0 + width1), wlimit,
3825 col_tab_align, 1);
3826 if (err)
3827 goto done;
3828 waddwstr(window, &wline[scrollx]);
3829 } else {
3830 err = format_line(&wline, &width2, NULL, seg2, 0,
3831 wlimit, col_tab_align, 1);
3832 if (err)
3833 goto done;
3834 waddwstr(window, wline);
3836 *wtotal += width2;
3838 done:
3839 free(wline);
3840 free(exstr);
3841 free(seg0);
3842 free(seg1);
3843 free(seg2);
3844 return err;
3847 static const struct got_error *
3848 draw_file(struct tog_view *view, const char *header)
3850 struct tog_diff_view_state *s = &view->state.diff;
3851 regmatch_t *regmatch = &view->regmatch;
3852 const struct got_error *err;
3853 int nprinted = 0;
3854 char *line;
3855 size_t linesize = 0;
3856 ssize_t linelen;
3857 struct tog_color *tc;
3858 wchar_t *wline;
3859 int width;
3860 int max_lines = view->nlines;
3861 int nlines = s->nlines;
3862 off_t line_offset;
3864 line_offset = s->line_offsets[s->first_displayed_line - 1];
3865 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3866 return got_error_from_errno("fseek");
3868 werase(view->window);
3870 if (header) {
3871 if (asprintf(&line, "[%d/%d] %s",
3872 s->first_displayed_line - 1 + s->selected_line, nlines,
3873 header) == -1)
3874 return got_error_from_errno("asprintf");
3875 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3876 0, 0);
3877 free(line);
3878 if (err)
3879 return err;
3881 if (view_needs_focus_indication(view))
3882 wstandout(view->window);
3883 waddwstr(view->window, wline);
3884 free(wline);
3885 wline = NULL;
3886 if (view_needs_focus_indication(view))
3887 wstandend(view->window);
3888 if (width <= view->ncols - 1)
3889 waddch(view->window, '\n');
3891 if (max_lines <= 1)
3892 return NULL;
3893 max_lines--;
3896 s->eof = 0;
3897 view->maxx = 0;
3898 line = NULL;
3899 while (max_lines > 0 && nprinted < max_lines) {
3900 linelen = getline(&line, &linesize, s->f);
3901 if (linelen == -1) {
3902 if (feof(s->f)) {
3903 s->eof = 1;
3904 break;
3906 free(line);
3907 return got_ferror(s->f, GOT_ERR_IO);
3910 /* Set view->maxx based on full line length. */
3911 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3912 view->x ? 1 : 0);
3913 if (err) {
3914 free(line);
3915 return err;
3917 view->maxx = MAX(view->maxx, width);
3918 free(wline);
3919 wline = NULL;
3921 tc = match_color(&s->colors, line);
3922 if (tc)
3923 wattr_on(view->window,
3924 COLOR_PAIR(tc->colorpair), NULL);
3925 if (s->first_displayed_line + nprinted == s->matched_line &&
3926 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3927 err = add_matched_line(&width, line, view->ncols, 0,
3928 view->window, view->x, regmatch);
3929 if (err) {
3930 free(line);
3931 return err;
3933 } else {
3934 int skip;
3935 err = format_line(&wline, &width, &skip, line,
3936 view->x, view->ncols, 0, view->x ? 1 : 0);
3937 if (err) {
3938 free(line);
3939 return err;
3941 waddwstr(view->window, &wline[skip]);
3942 free(wline);
3943 wline = NULL;
3945 if (tc)
3946 wattr_off(view->window,
3947 COLOR_PAIR(tc->colorpair), NULL);
3948 if (width <= view->ncols - 1)
3949 waddch(view->window, '\n');
3950 nprinted++;
3952 free(line);
3953 if (nprinted >= 1)
3954 s->last_displayed_line = s->first_displayed_line +
3955 (nprinted - 1);
3956 else
3957 s->last_displayed_line = s->first_displayed_line;
3959 view_border(view);
3961 if (s->eof) {
3962 while (nprinted < view->nlines) {
3963 waddch(view->window, '\n');
3964 nprinted++;
3967 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3968 view->ncols, 0, 0);
3969 if (err) {
3970 return err;
3973 wstandout(view->window);
3974 waddwstr(view->window, wline);
3975 free(wline);
3976 wline = NULL;
3977 wstandend(view->window);
3980 return NULL;
3983 static char *
3984 get_datestr(time_t *time, char *datebuf)
3986 struct tm mytm, *tm;
3987 char *p, *s;
3989 tm = gmtime_r(time, &mytm);
3990 if (tm == NULL)
3991 return NULL;
3992 s = asctime_r(tm, datebuf);
3993 if (s == NULL)
3994 return NULL;
3995 p = strchr(s, '\n');
3996 if (p)
3997 *p = '\0';
3998 return s;
4001 static const struct got_error *
4002 get_changed_paths(struct got_pathlist_head *paths,
4003 struct got_commit_object *commit, struct got_repository *repo)
4005 const struct got_error *err = NULL;
4006 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4007 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4008 struct got_object_qid *qid;
4010 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4011 if (qid != NULL) {
4012 struct got_commit_object *pcommit;
4013 err = got_object_open_as_commit(&pcommit, repo,
4014 &qid->id);
4015 if (err)
4016 return err;
4018 tree_id1 = got_object_id_dup(
4019 got_object_commit_get_tree_id(pcommit));
4020 if (tree_id1 == NULL) {
4021 got_object_commit_close(pcommit);
4022 return got_error_from_errno("got_object_id_dup");
4024 got_object_commit_close(pcommit);
4028 if (tree_id1) {
4029 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4030 if (err)
4031 goto done;
4034 tree_id2 = got_object_commit_get_tree_id(commit);
4035 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4036 if (err)
4037 goto done;
4039 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4040 got_diff_tree_collect_changed_paths, paths, 0);
4041 done:
4042 if (tree1)
4043 got_object_tree_close(tree1);
4044 if (tree2)
4045 got_object_tree_close(tree2);
4046 free(tree_id1);
4047 return err;
4050 static const struct got_error *
4051 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4053 off_t *p;
4055 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4056 if (p == NULL)
4057 return got_error_from_errno("reallocarray");
4058 *line_offsets = p;
4059 (*line_offsets)[*nlines] = off;
4060 (*nlines)++;
4061 return NULL;
4064 static const struct got_error *
4065 write_commit_info(off_t **line_offsets, size_t *nlines,
4066 struct got_object_id *commit_id, struct got_reflist_head *refs,
4067 struct got_repository *repo, FILE *outfile)
4069 const struct got_error *err = NULL;
4070 char datebuf[26], *datestr;
4071 struct got_commit_object *commit;
4072 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4073 time_t committer_time;
4074 const char *author, *committer;
4075 char *refs_str = NULL;
4076 struct got_pathlist_head changed_paths;
4077 struct got_pathlist_entry *pe;
4078 off_t outoff = 0;
4079 int n;
4081 TAILQ_INIT(&changed_paths);
4083 if (refs) {
4084 err = build_refs_str(&refs_str, refs, commit_id, repo);
4085 if (err)
4086 return err;
4089 err = got_object_open_as_commit(&commit, repo, commit_id);
4090 if (err)
4091 return err;
4093 err = got_object_id_str(&id_str, commit_id);
4094 if (err) {
4095 err = got_error_from_errno("got_object_id_str");
4096 goto done;
4099 err = add_line_offset(line_offsets, nlines, 0);
4100 if (err)
4101 goto done;
4103 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4104 refs_str ? refs_str : "", refs_str ? ")" : "");
4105 if (n < 0) {
4106 err = got_error_from_errno("fprintf");
4107 goto done;
4109 outoff += n;
4110 err = add_line_offset(line_offsets, nlines, outoff);
4111 if (err)
4112 goto done;
4114 n = fprintf(outfile, "from: %s\n",
4115 got_object_commit_get_author(commit));
4116 if (n < 0) {
4117 err = got_error_from_errno("fprintf");
4118 goto done;
4120 outoff += n;
4121 err = add_line_offset(line_offsets, nlines, outoff);
4122 if (err)
4123 goto done;
4125 committer_time = got_object_commit_get_committer_time(commit);
4126 datestr = get_datestr(&committer_time, datebuf);
4127 if (datestr) {
4128 n = fprintf(outfile, "date: %s UTC\n", datestr);
4129 if (n < 0) {
4130 err = got_error_from_errno("fprintf");
4131 goto done;
4133 outoff += n;
4134 err = add_line_offset(line_offsets, nlines, outoff);
4135 if (err)
4136 goto done;
4138 author = got_object_commit_get_author(commit);
4139 committer = got_object_commit_get_committer(commit);
4140 if (strcmp(author, committer) != 0) {
4141 n = fprintf(outfile, "via: %s\n", committer);
4142 if (n < 0) {
4143 err = got_error_from_errno("fprintf");
4144 goto done;
4146 outoff += n;
4147 err = add_line_offset(line_offsets, nlines, outoff);
4148 if (err)
4149 goto done;
4151 if (got_object_commit_get_nparents(commit) > 1) {
4152 const struct got_object_id_queue *parent_ids;
4153 struct got_object_qid *qid;
4154 int pn = 1;
4155 parent_ids = got_object_commit_get_parent_ids(commit);
4156 STAILQ_FOREACH(qid, parent_ids, entry) {
4157 err = got_object_id_str(&id_str, &qid->id);
4158 if (err)
4159 goto done;
4160 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4161 if (n < 0) {
4162 err = got_error_from_errno("fprintf");
4163 goto done;
4165 outoff += n;
4166 err = add_line_offset(line_offsets, nlines, outoff);
4167 if (err)
4168 goto done;
4169 free(id_str);
4170 id_str = NULL;
4174 err = got_object_commit_get_logmsg(&logmsg, commit);
4175 if (err)
4176 goto done;
4177 s = logmsg;
4178 while ((line = strsep(&s, "\n")) != NULL) {
4179 n = fprintf(outfile, "%s\n", line);
4180 if (n < 0) {
4181 err = got_error_from_errno("fprintf");
4182 goto done;
4184 outoff += n;
4185 err = add_line_offset(line_offsets, nlines, outoff);
4186 if (err)
4187 goto done;
4190 err = get_changed_paths(&changed_paths, commit, repo);
4191 if (err)
4192 goto done;
4193 TAILQ_FOREACH(pe, &changed_paths, entry) {
4194 struct got_diff_changed_path *cp = pe->data;
4195 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4196 if (n < 0) {
4197 err = got_error_from_errno("fprintf");
4198 goto done;
4200 outoff += n;
4201 err = add_line_offset(line_offsets, nlines, outoff);
4202 if (err)
4203 goto done;
4204 free((char *)pe->path);
4205 free(pe->data);
4208 fputc('\n', outfile);
4209 outoff++;
4210 err = add_line_offset(line_offsets, nlines, outoff);
4211 done:
4212 got_pathlist_free(&changed_paths);
4213 free(id_str);
4214 free(logmsg);
4215 free(refs_str);
4216 got_object_commit_close(commit);
4217 if (err) {
4218 free(*line_offsets);
4219 *line_offsets = NULL;
4220 *nlines = 0;
4222 return err;
4225 static const struct got_error *
4226 create_diff(struct tog_diff_view_state *s)
4228 const struct got_error *err = NULL;
4229 FILE *f = NULL;
4230 int obj_type;
4232 free(s->line_offsets);
4233 s->line_offsets = malloc(sizeof(off_t));
4234 if (s->line_offsets == NULL)
4235 return got_error_from_errno("malloc");
4236 s->nlines = 0;
4238 f = got_opentemp();
4239 if (f == NULL) {
4240 err = got_error_from_errno("got_opentemp");
4241 goto done;
4243 if (s->f && fclose(s->f) == EOF) {
4244 err = got_error_from_errno("fclose");
4245 goto done;
4247 s->f = f;
4249 if (s->id1)
4250 err = got_object_get_type(&obj_type, s->repo, s->id1);
4251 else
4252 err = got_object_get_type(&obj_type, s->repo, s->id2);
4253 if (err)
4254 goto done;
4256 switch (obj_type) {
4257 case GOT_OBJ_TYPE_BLOB:
4258 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4259 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4260 s->label1, s->label2, tog_diff_algo, s->diff_context,
4261 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4262 break;
4263 case GOT_OBJ_TYPE_TREE:
4264 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4265 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4266 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4267 s->force_text_diff, s->repo, s->f);
4268 break;
4269 case GOT_OBJ_TYPE_COMMIT: {
4270 const struct got_object_id_queue *parent_ids;
4271 struct got_object_qid *pid;
4272 struct got_commit_object *commit2;
4273 struct got_reflist_head *refs;
4275 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4276 if (err)
4277 goto done;
4278 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4279 /* Show commit info if we're diffing to a parent/root commit. */
4280 if (s->id1 == NULL) {
4281 err = write_commit_info(&s->line_offsets, &s->nlines,
4282 s->id2, refs, s->repo, s->f);
4283 if (err)
4284 goto done;
4285 } else {
4286 parent_ids = got_object_commit_get_parent_ids(commit2);
4287 STAILQ_FOREACH(pid, parent_ids, entry) {
4288 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4289 err = write_commit_info(
4290 &s->line_offsets, &s->nlines,
4291 s->id2, refs, s->repo, s->f);
4292 if (err)
4293 goto done;
4294 break;
4298 got_object_commit_close(commit2);
4300 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4301 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4302 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 s->force_text_diff, s->repo, s->f);
4304 break;
4306 default:
4307 err = got_error(GOT_ERR_OBJ_TYPE);
4308 break;
4310 if (err)
4311 goto done;
4312 done:
4313 if (s->f && fflush(s->f) != 0 && err == NULL)
4314 err = got_error_from_errno("fflush");
4315 return err;
4318 static void
4319 diff_view_indicate_progress(struct tog_view *view)
4321 mvwaddstr(view->window, 0, 0, "diffing...");
4322 update_panels();
4323 doupdate();
4326 static const struct got_error *
4327 search_start_diff_view(struct tog_view *view)
4329 struct tog_diff_view_state *s = &view->state.diff;
4331 s->matched_line = 0;
4332 return NULL;
4335 static const struct got_error *
4336 search_next_diff_view(struct tog_view *view)
4338 struct tog_diff_view_state *s = &view->state.diff;
4339 const struct got_error *err = NULL;
4340 int lineno;
4341 char *line = NULL;
4342 size_t linesize = 0;
4343 ssize_t linelen;
4345 if (!view->searching) {
4346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4347 return NULL;
4350 if (s->matched_line) {
4351 if (view->searching == TOG_SEARCH_FORWARD)
4352 lineno = s->matched_line + 1;
4353 else
4354 lineno = s->matched_line - 1;
4355 } else
4356 lineno = s->first_displayed_line;
4358 while (1) {
4359 off_t offset;
4361 if (lineno <= 0 || lineno > s->nlines) {
4362 if (s->matched_line == 0) {
4363 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4364 break;
4367 if (view->searching == TOG_SEARCH_FORWARD)
4368 lineno = 1;
4369 else
4370 lineno = s->nlines;
4373 offset = s->line_offsets[lineno - 1];
4374 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4375 free(line);
4376 return got_error_from_errno("fseeko");
4378 linelen = getline(&line, &linesize, s->f);
4379 if (linelen != -1) {
4380 char *exstr;
4381 err = expand_tab(&exstr, line);
4382 if (err)
4383 break;
4384 if (match_line(exstr, &view->regex, 1,
4385 &view->regmatch)) {
4386 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4387 s->matched_line = lineno;
4388 free(exstr);
4389 break;
4391 free(exstr);
4393 if (view->searching == TOG_SEARCH_FORWARD)
4394 lineno++;
4395 else
4396 lineno--;
4398 free(line);
4400 if (s->matched_line) {
4401 s->first_displayed_line = s->matched_line;
4402 s->selected_line = 1;
4405 return err;
4408 static const struct got_error *
4409 close_diff_view(struct tog_view *view)
4411 const struct got_error *err = NULL;
4412 struct tog_diff_view_state *s = &view->state.diff;
4414 free(s->id1);
4415 s->id1 = NULL;
4416 free(s->id2);
4417 s->id2 = NULL;
4418 if (s->f && fclose(s->f) == EOF)
4419 err = got_error_from_errno("fclose");
4420 s->f = NULL;
4421 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4422 err = got_error_from_errno("fclose");
4423 s->f1 = NULL;
4424 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4425 err = got_error_from_errno("fclose");
4426 s->f2 = NULL;
4427 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4428 err = got_error_from_errno("close");
4429 s->fd1 = -1;
4430 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4431 err = got_error_from_errno("close");
4432 s->fd2 = -1;
4433 free_colors(&s->colors);
4434 free(s->line_offsets);
4435 s->line_offsets = NULL;
4436 s->nlines = 0;
4437 return err;
4440 static const struct got_error *
4441 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4442 struct got_object_id *id2, const char *label1, const char *label2,
4443 int diff_context, int ignore_whitespace, int force_text_diff,
4444 struct tog_view *parent_view, struct got_repository *repo)
4446 const struct got_error *err;
4447 struct tog_diff_view_state *s = &view->state.diff;
4449 memset(s, 0, sizeof(*s));
4450 s->fd1 = -1;
4451 s->fd2 = -1;
4453 if (id1 != NULL && id2 != NULL) {
4454 int type1, type2;
4455 err = got_object_get_type(&type1, repo, id1);
4456 if (err)
4457 return err;
4458 err = got_object_get_type(&type2, repo, id2);
4459 if (err)
4460 return err;
4462 if (type1 != type2)
4463 return got_error(GOT_ERR_OBJ_TYPE);
4465 s->first_displayed_line = 1;
4466 s->last_displayed_line = view->nlines;
4467 s->selected_line = 1;
4468 s->repo = repo;
4469 s->id1 = id1;
4470 s->id2 = id2;
4471 s->label1 = label1;
4472 s->label2 = label2;
4474 if (id1) {
4475 s->id1 = got_object_id_dup(id1);
4476 if (s->id1 == NULL)
4477 return got_error_from_errno("got_object_id_dup");
4478 } else
4479 s->id1 = NULL;
4481 s->id2 = got_object_id_dup(id2);
4482 if (s->id2 == NULL) {
4483 err = got_error_from_errno("got_object_id_dup");
4484 goto done;
4487 s->f1 = got_opentemp();
4488 if (s->f1 == NULL) {
4489 err = got_error_from_errno("got_opentemp");
4490 goto done;
4493 s->f2 = got_opentemp();
4494 if (s->f2 == NULL) {
4495 err = got_error_from_errno("got_opentemp");
4496 goto done;
4499 s->fd1 = got_opentempfd();
4500 if (s->fd1 == -1) {
4501 err = got_error_from_errno("got_opentempfd");
4502 goto done;
4505 s->fd2 = got_opentempfd();
4506 if (s->fd2 == -1) {
4507 err = got_error_from_errno("got_opentempfd");
4508 goto done;
4511 s->first_displayed_line = 1;
4512 s->last_displayed_line = view->nlines;
4513 s->diff_context = diff_context;
4514 s->ignore_whitespace = ignore_whitespace;
4515 s->force_text_diff = force_text_diff;
4516 s->parent_view = parent_view;
4517 s->repo = repo;
4519 STAILQ_INIT(&s->colors);
4520 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4521 err = add_color(&s->colors,
4522 "^-", TOG_COLOR_DIFF_MINUS,
4523 get_color_value("TOG_COLOR_DIFF_MINUS"));
4524 if (err)
4525 goto done;
4526 err = add_color(&s->colors, "^\\+",
4527 TOG_COLOR_DIFF_PLUS,
4528 get_color_value("TOG_COLOR_DIFF_PLUS"));
4529 if (err)
4530 goto done;
4531 err = add_color(&s->colors,
4532 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4533 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4534 if (err)
4535 goto done;
4537 err = add_color(&s->colors,
4538 "^(commit [0-9a-f]|parent [0-9]|"
4539 "(blob|file|tree|commit) [-+] |"
4540 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4541 get_color_value("TOG_COLOR_DIFF_META"));
4542 if (err)
4543 goto done;
4545 err = add_color(&s->colors,
4546 "^(from|via): ", TOG_COLOR_AUTHOR,
4547 get_color_value("TOG_COLOR_AUTHOR"));
4548 if (err)
4549 goto done;
4551 err = add_color(&s->colors,
4552 "^date: ", TOG_COLOR_DATE,
4553 get_color_value("TOG_COLOR_DATE"));
4554 if (err)
4555 goto done;
4558 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4559 view_is_splitscreen(view))
4560 show_log_view(parent_view); /* draw border */
4561 diff_view_indicate_progress(view);
4563 err = create_diff(s);
4565 view->show = show_diff_view;
4566 view->input = input_diff_view;
4567 view->reset = reset_diff_view;
4568 view->close = close_diff_view;
4569 view->search_start = search_start_diff_view;
4570 view->search_next = search_next_diff_view;
4571 done:
4572 if (err)
4573 close_diff_view(view);
4574 return err;
4577 static const struct got_error *
4578 show_diff_view(struct tog_view *view)
4580 const struct got_error *err;
4581 struct tog_diff_view_state *s = &view->state.diff;
4582 char *id_str1 = NULL, *id_str2, *header;
4583 const char *label1, *label2;
4585 if (s->id1) {
4586 err = got_object_id_str(&id_str1, s->id1);
4587 if (err)
4588 return err;
4589 label1 = s->label1 ? : id_str1;
4590 } else
4591 label1 = "/dev/null";
4593 err = got_object_id_str(&id_str2, s->id2);
4594 if (err)
4595 return err;
4596 label2 = s->label2 ? : id_str2;
4598 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4599 err = got_error_from_errno("asprintf");
4600 free(id_str1);
4601 free(id_str2);
4602 return err;
4604 free(id_str1);
4605 free(id_str2);
4607 err = draw_file(view, header);
4608 free(header);
4609 return err;
4612 static const struct got_error *
4613 set_selected_commit(struct tog_diff_view_state *s,
4614 struct commit_queue_entry *entry)
4616 const struct got_error *err;
4617 const struct got_object_id_queue *parent_ids;
4618 struct got_commit_object *selected_commit;
4619 struct got_object_qid *pid;
4621 free(s->id2);
4622 s->id2 = got_object_id_dup(entry->id);
4623 if (s->id2 == NULL)
4624 return got_error_from_errno("got_object_id_dup");
4626 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4627 if (err)
4628 return err;
4629 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4630 free(s->id1);
4631 pid = STAILQ_FIRST(parent_ids);
4632 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4633 got_object_commit_close(selected_commit);
4634 return NULL;
4637 static const struct got_error *
4638 reset_diff_view(struct tog_view *view)
4640 struct tog_diff_view_state *s = &view->state.diff;
4642 view->count = 0;
4643 wclear(view->window);
4644 s->first_displayed_line = 1;
4645 s->last_displayed_line = view->nlines;
4646 s->matched_line = 0;
4647 diff_view_indicate_progress(view);
4648 return create_diff(s);
4651 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4652 int, int, int);
4653 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4654 int, int);
4656 static const struct got_error *
4657 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4659 const struct got_error *err = NULL;
4660 struct tog_diff_view_state *s = &view->state.diff;
4661 struct tog_log_view_state *ls;
4662 struct commit_queue_entry *old_selected_entry;
4663 char *line = NULL;
4664 size_t linesize = 0;
4665 ssize_t linelen;
4666 int i, nscroll = view->nlines - 1, up = 0;
4668 switch (ch) {
4669 case '0':
4670 view->x = 0;
4671 break;
4672 case '$':
4673 view->x = MAX(view->maxx - view->ncols / 3, 0);
4674 view->count = 0;
4675 break;
4676 case KEY_RIGHT:
4677 case 'l':
4678 if (view->x + view->ncols / 3 < view->maxx)
4679 view->x += 2; /* move two columns right */
4680 else
4681 view->count = 0;
4682 break;
4683 case KEY_LEFT:
4684 case 'h':
4685 view->x -= MIN(view->x, 2); /* move two columns back */
4686 if (view->x <= 0)
4687 view->count = 0;
4688 break;
4689 case 'a':
4690 case 'w':
4691 if (ch == 'a')
4692 s->force_text_diff = !s->force_text_diff;
4693 if (ch == 'w')
4694 s->ignore_whitespace = !s->ignore_whitespace;
4695 err = reset_diff_view(view);
4696 break;
4697 case 'g':
4698 case KEY_HOME:
4699 s->first_displayed_line = 1;
4700 view->count = 0;
4701 break;
4702 case 'G':
4703 case KEY_END:
4704 view->count = 0;
4705 if (s->eof)
4706 break;
4708 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4709 s->eof = 1;
4710 break;
4711 case 'k':
4712 case KEY_UP:
4713 case CTRL('p'):
4714 if (s->first_displayed_line > 1)
4715 s->first_displayed_line--;
4716 else
4717 view->count = 0;
4718 break;
4719 case CTRL('u'):
4720 case 'u':
4721 nscroll /= 2;
4722 /* FALL THROUGH */
4723 case KEY_PPAGE:
4724 case CTRL('b'):
4725 case 'b':
4726 if (s->first_displayed_line == 1) {
4727 view->count = 0;
4728 break;
4730 i = 0;
4731 while (i++ < nscroll && s->first_displayed_line > 1)
4732 s->first_displayed_line--;
4733 break;
4734 case 'j':
4735 case KEY_DOWN:
4736 case CTRL('n'):
4737 if (!s->eof)
4738 s->first_displayed_line++;
4739 else
4740 view->count = 0;
4741 break;
4742 case CTRL('d'):
4743 case 'd':
4744 nscroll /= 2;
4745 /* FALL THROUGH */
4746 case KEY_NPAGE:
4747 case CTRL('f'):
4748 case 'f':
4749 case ' ':
4750 if (s->eof) {
4751 view->count = 0;
4752 break;
4754 i = 0;
4755 while (!s->eof && i++ < nscroll) {
4756 linelen = getline(&line, &linesize, s->f);
4757 s->first_displayed_line++;
4758 if (linelen == -1) {
4759 if (feof(s->f)) {
4760 s->eof = 1;
4761 } else
4762 err = got_ferror(s->f, GOT_ERR_IO);
4763 break;
4766 free(line);
4767 break;
4768 case '[':
4769 if (s->diff_context > 0) {
4770 s->diff_context--;
4771 s->matched_line = 0;
4772 diff_view_indicate_progress(view);
4773 err = create_diff(s);
4774 if (s->first_displayed_line + view->nlines - 1 >
4775 s->nlines) {
4776 s->first_displayed_line = 1;
4777 s->last_displayed_line = view->nlines;
4779 } else
4780 view->count = 0;
4781 break;
4782 case ']':
4783 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4784 s->diff_context++;
4785 s->matched_line = 0;
4786 diff_view_indicate_progress(view);
4787 err = create_diff(s);
4788 } else
4789 view->count = 0;
4790 break;
4791 case '<':
4792 case ',':
4793 case 'K':
4794 up = 1;
4795 /* FALL THROUGH */
4796 case '>':
4797 case '.':
4798 case 'J':
4799 if (s->parent_view == NULL) {
4800 view->count = 0;
4801 break;
4803 s->parent_view->count = view->count;
4805 if (s->parent_view->type == TOG_VIEW_LOG) {
4806 ls = &s->parent_view->state.log;
4807 old_selected_entry = ls->selected_entry;
4809 err = input_log_view(NULL, s->parent_view,
4810 up ? KEY_UP : KEY_DOWN);
4811 if (err)
4812 break;
4813 view->count = s->parent_view->count;
4815 if (old_selected_entry == ls->selected_entry)
4816 break;
4818 err = set_selected_commit(s, ls->selected_entry);
4819 if (err)
4820 break;
4821 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4822 struct tog_blame_view_state *bs;
4823 struct got_object_id *id, *prev_id;
4825 bs = &s->parent_view->state.blame;
4826 prev_id = get_annotation_for_line(bs->blame.lines,
4827 bs->blame.nlines, bs->last_diffed_line);
4829 err = input_blame_view(&view, s->parent_view,
4830 up ? KEY_UP : KEY_DOWN);
4831 if (err)
4832 break;
4833 view->count = s->parent_view->count;
4835 if (prev_id == NULL)
4836 break;
4837 id = get_selected_commit_id(bs->blame.lines,
4838 bs->blame.nlines, bs->first_displayed_line,
4839 bs->selected_line);
4840 if (id == NULL)
4841 break;
4843 if (!got_object_id_cmp(prev_id, id))
4844 break;
4846 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4847 if (err)
4848 break;
4850 s->first_displayed_line = 1;
4851 s->last_displayed_line = view->nlines;
4852 s->matched_line = 0;
4853 view->x = 0;
4855 diff_view_indicate_progress(view);
4856 err = create_diff(s);
4857 break;
4858 default:
4859 view->count = 0;
4860 break;
4863 return err;
4866 static const struct got_error *
4867 cmd_diff(int argc, char *argv[])
4869 const struct got_error *error = NULL;
4870 struct got_repository *repo = NULL;
4871 struct got_worktree *worktree = NULL;
4872 struct got_object_id *id1 = NULL, *id2 = NULL;
4873 char *repo_path = NULL, *cwd = NULL;
4874 char *id_str1 = NULL, *id_str2 = NULL;
4875 char *label1 = NULL, *label2 = NULL;
4876 int diff_context = 3, ignore_whitespace = 0;
4877 int ch, force_text_diff = 0;
4878 const char *errstr;
4879 struct tog_view *view;
4880 int *pack_fds = NULL;
4882 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4883 switch (ch) {
4884 case 'a':
4885 force_text_diff = 1;
4886 break;
4887 case 'C':
4888 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4889 &errstr);
4890 if (errstr != NULL)
4891 errx(1, "number of context lines is %s: %s",
4892 errstr, errstr);
4893 break;
4894 case 'r':
4895 repo_path = realpath(optarg, NULL);
4896 if (repo_path == NULL)
4897 return got_error_from_errno2("realpath",
4898 optarg);
4899 got_path_strip_trailing_slashes(repo_path);
4900 break;
4901 case 'w':
4902 ignore_whitespace = 1;
4903 break;
4904 default:
4905 usage_diff();
4906 /* NOTREACHED */
4910 argc -= optind;
4911 argv += optind;
4913 if (argc == 0) {
4914 usage_diff(); /* TODO show local worktree changes */
4915 } else if (argc == 2) {
4916 id_str1 = argv[0];
4917 id_str2 = argv[1];
4918 } else
4919 usage_diff();
4921 error = got_repo_pack_fds_open(&pack_fds);
4922 if (error)
4923 goto done;
4925 if (repo_path == NULL) {
4926 cwd = getcwd(NULL, 0);
4927 if (cwd == NULL)
4928 return got_error_from_errno("getcwd");
4929 error = got_worktree_open(&worktree, cwd);
4930 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4931 goto done;
4932 if (worktree)
4933 repo_path =
4934 strdup(got_worktree_get_repo_path(worktree));
4935 else
4936 repo_path = strdup(cwd);
4937 if (repo_path == NULL) {
4938 error = got_error_from_errno("strdup");
4939 goto done;
4943 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4944 if (error)
4945 goto done;
4947 init_curses();
4949 error = apply_unveil(got_repo_get_path(repo), NULL);
4950 if (error)
4951 goto done;
4953 error = tog_load_refs(repo, 0);
4954 if (error)
4955 goto done;
4957 error = got_repo_match_object_id(&id1, &label1, id_str1,
4958 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4959 if (error)
4960 goto done;
4962 error = got_repo_match_object_id(&id2, &label2, id_str2,
4963 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4964 if (error)
4965 goto done;
4967 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4968 if (view == NULL) {
4969 error = got_error_from_errno("view_open");
4970 goto done;
4972 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4973 ignore_whitespace, force_text_diff, NULL, repo);
4974 if (error)
4975 goto done;
4976 error = view_loop(view);
4977 done:
4978 free(label1);
4979 free(label2);
4980 free(repo_path);
4981 free(cwd);
4982 if (repo) {
4983 const struct got_error *close_err = got_repo_close(repo);
4984 if (error == NULL)
4985 error = close_err;
4987 if (worktree)
4988 got_worktree_close(worktree);
4989 if (pack_fds) {
4990 const struct got_error *pack_err =
4991 got_repo_pack_fds_close(pack_fds);
4992 if (error == NULL)
4993 error = pack_err;
4995 tog_free_refs();
4996 return error;
4999 __dead static void
5000 usage_blame(void)
5002 endwin();
5003 fprintf(stderr,
5004 "usage: %s blame [-c commit] [-r repository-path] path\n",
5005 getprogname());
5006 exit(1);
5009 struct tog_blame_line {
5010 int annotated;
5011 struct got_object_id *id;
5014 static const struct got_error *
5015 draw_blame(struct tog_view *view)
5017 struct tog_blame_view_state *s = &view->state.blame;
5018 struct tog_blame *blame = &s->blame;
5019 regmatch_t *regmatch = &view->regmatch;
5020 const struct got_error *err;
5021 int lineno = 0, nprinted = 0;
5022 char *line = NULL;
5023 size_t linesize = 0;
5024 ssize_t linelen;
5025 wchar_t *wline;
5026 int width;
5027 struct tog_blame_line *blame_line;
5028 struct got_object_id *prev_id = NULL;
5029 char *id_str;
5030 struct tog_color *tc;
5032 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5033 if (err)
5034 return err;
5036 rewind(blame->f);
5037 werase(view->window);
5039 if (asprintf(&line, "commit %s", id_str) == -1) {
5040 err = got_error_from_errno("asprintf");
5041 free(id_str);
5042 return err;
5045 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5046 free(line);
5047 line = NULL;
5048 if (err)
5049 return err;
5050 if (view_needs_focus_indication(view))
5051 wstandout(view->window);
5052 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5053 if (tc)
5054 wattr_on(view->window,
5055 COLOR_PAIR(tc->colorpair), NULL);
5056 waddwstr(view->window, wline);
5057 if (tc)
5058 wattr_off(view->window,
5059 COLOR_PAIR(tc->colorpair), NULL);
5060 if (view_needs_focus_indication(view))
5061 wstandend(view->window);
5062 free(wline);
5063 wline = NULL;
5064 if (width < view->ncols - 1)
5065 waddch(view->window, '\n');
5067 if (asprintf(&line, "[%d/%d] %s%s",
5068 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5069 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5070 free(id_str);
5071 return got_error_from_errno("asprintf");
5073 free(id_str);
5074 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5075 free(line);
5076 line = NULL;
5077 if (err)
5078 return err;
5079 waddwstr(view->window, wline);
5080 free(wline);
5081 wline = NULL;
5082 if (width < view->ncols - 1)
5083 waddch(view->window, '\n');
5085 s->eof = 0;
5086 view->maxx = 0;
5087 while (nprinted < view->nlines - 2) {
5088 linelen = getline(&line, &linesize, blame->f);
5089 if (linelen == -1) {
5090 if (feof(blame->f)) {
5091 s->eof = 1;
5092 break;
5094 free(line);
5095 return got_ferror(blame->f, GOT_ERR_IO);
5097 if (++lineno < s->first_displayed_line)
5098 continue;
5100 /* Set view->maxx based on full line length. */
5101 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5102 if (err) {
5103 free(line);
5104 return err;
5106 free(wline);
5107 wline = NULL;
5108 view->maxx = MAX(view->maxx, width);
5110 if (nprinted == s->selected_line - 1)
5111 wstandout(view->window);
5113 if (blame->nlines > 0) {
5114 blame_line = &blame->lines[lineno - 1];
5115 if (blame_line->annotated && prev_id &&
5116 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5117 !(nprinted == s->selected_line - 1)) {
5118 waddstr(view->window, " ");
5119 } else if (blame_line->annotated) {
5120 char *id_str;
5121 err = got_object_id_str(&id_str,
5122 blame_line->id);
5123 if (err) {
5124 free(line);
5125 return err;
5127 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5128 if (tc)
5129 wattr_on(view->window,
5130 COLOR_PAIR(tc->colorpair), NULL);
5131 wprintw(view->window, "%.8s", id_str);
5132 if (tc)
5133 wattr_off(view->window,
5134 COLOR_PAIR(tc->colorpair), NULL);
5135 free(id_str);
5136 prev_id = blame_line->id;
5137 } else {
5138 waddstr(view->window, "........");
5139 prev_id = NULL;
5141 } else {
5142 waddstr(view->window, "........");
5143 prev_id = NULL;
5146 if (nprinted == s->selected_line - 1)
5147 wstandend(view->window);
5148 waddstr(view->window, " ");
5150 if (view->ncols <= 9) {
5151 width = 9;
5152 } else if (s->first_displayed_line + nprinted ==
5153 s->matched_line &&
5154 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5155 err = add_matched_line(&width, line, view->ncols - 9, 9,
5156 view->window, view->x, regmatch);
5157 if (err) {
5158 free(line);
5159 return err;
5161 width += 9;
5162 } else {
5163 int skip;
5164 err = format_line(&wline, &width, &skip, line,
5165 view->x, view->ncols - 9, 9, 1);
5166 if (err) {
5167 free(line);
5168 return err;
5170 waddwstr(view->window, &wline[skip]);
5171 width += 9;
5172 free(wline);
5173 wline = NULL;
5176 if (width <= view->ncols - 1)
5177 waddch(view->window, '\n');
5178 if (++nprinted == 1)
5179 s->first_displayed_line = lineno;
5181 free(line);
5182 s->last_displayed_line = lineno;
5184 view_border(view);
5186 return NULL;
5189 static const struct got_error *
5190 blame_cb(void *arg, int nlines, int lineno,
5191 struct got_commit_object *commit, struct got_object_id *id)
5193 const struct got_error *err = NULL;
5194 struct tog_blame_cb_args *a = arg;
5195 struct tog_blame_line *line;
5196 int errcode;
5198 if (nlines != a->nlines ||
5199 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5200 return got_error(GOT_ERR_RANGE);
5202 errcode = pthread_mutex_lock(&tog_mutex);
5203 if (errcode)
5204 return got_error_set_errno(errcode, "pthread_mutex_lock");
5206 if (*a->quit) { /* user has quit the blame view */
5207 err = got_error(GOT_ERR_ITER_COMPLETED);
5208 goto done;
5211 if (lineno == -1)
5212 goto done; /* no change in this commit */
5214 line = &a->lines[lineno - 1];
5215 if (line->annotated)
5216 goto done;
5218 line->id = got_object_id_dup(id);
5219 if (line->id == NULL) {
5220 err = got_error_from_errno("got_object_id_dup");
5221 goto done;
5223 line->annotated = 1;
5224 done:
5225 errcode = pthread_mutex_unlock(&tog_mutex);
5226 if (errcode)
5227 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5228 return err;
5231 static void *
5232 blame_thread(void *arg)
5234 const struct got_error *err, *close_err;
5235 struct tog_blame_thread_args *ta = arg;
5236 struct tog_blame_cb_args *a = ta->cb_args;
5237 int errcode, fd1 = -1, fd2 = -1;
5238 FILE *f1 = NULL, *f2 = NULL;
5240 fd1 = got_opentempfd();
5241 if (fd1 == -1)
5242 return (void *)got_error_from_errno("got_opentempfd");
5244 fd2 = got_opentempfd();
5245 if (fd2 == -1) {
5246 err = got_error_from_errno("got_opentempfd");
5247 goto done;
5250 f1 = got_opentemp();
5251 if (f1 == NULL) {
5252 err = (void *)got_error_from_errno("got_opentemp");
5253 goto done;
5255 f2 = got_opentemp();
5256 if (f2 == NULL) {
5257 err = (void *)got_error_from_errno("got_opentemp");
5258 goto done;
5261 err = block_signals_used_by_main_thread();
5262 if (err)
5263 goto done;
5265 err = got_blame(ta->path, a->commit_id, ta->repo,
5266 tog_diff_algo, blame_cb, ta->cb_args,
5267 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5268 if (err && err->code == GOT_ERR_CANCELLED)
5269 err = NULL;
5271 errcode = pthread_mutex_lock(&tog_mutex);
5272 if (errcode) {
5273 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5274 goto done;
5277 close_err = got_repo_close(ta->repo);
5278 if (err == NULL)
5279 err = close_err;
5280 ta->repo = NULL;
5281 *ta->complete = 1;
5283 errcode = pthread_mutex_unlock(&tog_mutex);
5284 if (errcode && err == NULL)
5285 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5287 done:
5288 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5289 err = got_error_from_errno("close");
5290 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5291 err = got_error_from_errno("close");
5292 if (f1 && fclose(f1) == EOF && err == NULL)
5293 err = got_error_from_errno("fclose");
5294 if (f2 && fclose(f2) == EOF && err == NULL)
5295 err = got_error_from_errno("fclose");
5297 return (void *)err;
5300 static struct got_object_id *
5301 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5302 int first_displayed_line, int selected_line)
5304 struct tog_blame_line *line;
5306 if (nlines <= 0)
5307 return NULL;
5309 line = &lines[first_displayed_line - 1 + selected_line - 1];
5310 if (!line->annotated)
5311 return NULL;
5313 return line->id;
5316 static struct got_object_id *
5317 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5318 int lineno)
5320 struct tog_blame_line *line;
5322 if (nlines <= 0 || lineno >= nlines)
5323 return NULL;
5325 line = &lines[lineno - 1];
5326 if (!line->annotated)
5327 return NULL;
5329 return line->id;
5332 static const struct got_error *
5333 stop_blame(struct tog_blame *blame)
5335 const struct got_error *err = NULL;
5336 int i;
5338 if (blame->thread) {
5339 int errcode;
5340 errcode = pthread_mutex_unlock(&tog_mutex);
5341 if (errcode)
5342 return got_error_set_errno(errcode,
5343 "pthread_mutex_unlock");
5344 errcode = pthread_join(blame->thread, (void **)&err);
5345 if (errcode)
5346 return got_error_set_errno(errcode, "pthread_join");
5347 errcode = pthread_mutex_lock(&tog_mutex);
5348 if (errcode)
5349 return got_error_set_errno(errcode,
5350 "pthread_mutex_lock");
5351 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5352 err = NULL;
5353 blame->thread = 0; //NULL;
5355 if (blame->thread_args.repo) {
5356 const struct got_error *close_err;
5357 close_err = got_repo_close(blame->thread_args.repo);
5358 if (err == NULL)
5359 err = close_err;
5360 blame->thread_args.repo = NULL;
5362 if (blame->f) {
5363 if (fclose(blame->f) == EOF && err == NULL)
5364 err = got_error_from_errno("fclose");
5365 blame->f = NULL;
5367 if (blame->lines) {
5368 for (i = 0; i < blame->nlines; i++)
5369 free(blame->lines[i].id);
5370 free(blame->lines);
5371 blame->lines = NULL;
5373 free(blame->cb_args.commit_id);
5374 blame->cb_args.commit_id = NULL;
5375 if (blame->pack_fds) {
5376 const struct got_error *pack_err =
5377 got_repo_pack_fds_close(blame->pack_fds);
5378 if (err == NULL)
5379 err = pack_err;
5380 blame->pack_fds = NULL;
5382 return err;
5385 static const struct got_error *
5386 cancel_blame_view(void *arg)
5388 const struct got_error *err = NULL;
5389 int *done = arg;
5390 int errcode;
5392 errcode = pthread_mutex_lock(&tog_mutex);
5393 if (errcode)
5394 return got_error_set_errno(errcode,
5395 "pthread_mutex_unlock");
5397 if (*done)
5398 err = got_error(GOT_ERR_CANCELLED);
5400 errcode = pthread_mutex_unlock(&tog_mutex);
5401 if (errcode)
5402 return got_error_set_errno(errcode,
5403 "pthread_mutex_lock");
5405 return err;
5408 static const struct got_error *
5409 run_blame(struct tog_view *view)
5411 struct tog_blame_view_state *s = &view->state.blame;
5412 struct tog_blame *blame = &s->blame;
5413 const struct got_error *err = NULL;
5414 struct got_commit_object *commit = NULL;
5415 struct got_blob_object *blob = NULL;
5416 struct got_repository *thread_repo = NULL;
5417 struct got_object_id *obj_id = NULL;
5418 int obj_type, fd = -1;
5419 int *pack_fds = NULL;
5421 err = got_object_open_as_commit(&commit, s->repo,
5422 &s->blamed_commit->id);
5423 if (err)
5424 return err;
5426 fd = got_opentempfd();
5427 if (fd == -1) {
5428 err = got_error_from_errno("got_opentempfd");
5429 goto done;
5432 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5433 if (err)
5434 goto done;
5436 err = got_object_get_type(&obj_type, s->repo, obj_id);
5437 if (err)
5438 goto done;
5440 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5441 err = got_error(GOT_ERR_OBJ_TYPE);
5442 goto done;
5445 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5446 if (err)
5447 goto done;
5448 blame->f = got_opentemp();
5449 if (blame->f == NULL) {
5450 err = got_error_from_errno("got_opentemp");
5451 goto done;
5453 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5454 &blame->line_offsets, blame->f, blob);
5455 if (err)
5456 goto done;
5457 if (blame->nlines == 0) {
5458 s->blame_complete = 1;
5459 goto done;
5462 /* Don't include \n at EOF in the blame line count. */
5463 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5464 blame->nlines--;
5466 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5467 if (blame->lines == NULL) {
5468 err = got_error_from_errno("calloc");
5469 goto done;
5472 err = got_repo_pack_fds_open(&pack_fds);
5473 if (err)
5474 goto done;
5475 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5476 pack_fds);
5477 if (err)
5478 goto done;
5480 blame->pack_fds = pack_fds;
5481 blame->cb_args.view = view;
5482 blame->cb_args.lines = blame->lines;
5483 blame->cb_args.nlines = blame->nlines;
5484 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5485 if (blame->cb_args.commit_id == NULL) {
5486 err = got_error_from_errno("got_object_id_dup");
5487 goto done;
5489 blame->cb_args.quit = &s->done;
5491 blame->thread_args.path = s->path;
5492 blame->thread_args.repo = thread_repo;
5493 blame->thread_args.cb_args = &blame->cb_args;
5494 blame->thread_args.complete = &s->blame_complete;
5495 blame->thread_args.cancel_cb = cancel_blame_view;
5496 blame->thread_args.cancel_arg = &s->done;
5497 s->blame_complete = 0;
5499 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5500 s->first_displayed_line = 1;
5501 s->last_displayed_line = view->nlines;
5502 s->selected_line = 1;
5504 s->matched_line = 0;
5506 done:
5507 if (commit)
5508 got_object_commit_close(commit);
5509 if (fd != -1 && close(fd) == -1 && err == NULL)
5510 err = got_error_from_errno("close");
5511 if (blob)
5512 got_object_blob_close(blob);
5513 free(obj_id);
5514 if (err)
5515 stop_blame(blame);
5516 return err;
5519 static const struct got_error *
5520 open_blame_view(struct tog_view *view, char *path,
5521 struct got_object_id *commit_id, struct got_repository *repo)
5523 const struct got_error *err = NULL;
5524 struct tog_blame_view_state *s = &view->state.blame;
5526 STAILQ_INIT(&s->blamed_commits);
5528 s->path = strdup(path);
5529 if (s->path == NULL)
5530 return got_error_from_errno("strdup");
5532 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5533 if (err) {
5534 free(s->path);
5535 return err;
5538 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5539 s->first_displayed_line = 1;
5540 s->last_displayed_line = view->nlines;
5541 s->selected_line = 1;
5542 s->blame_complete = 0;
5543 s->repo = repo;
5544 s->commit_id = commit_id;
5545 memset(&s->blame, 0, sizeof(s->blame));
5547 STAILQ_INIT(&s->colors);
5548 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5549 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5550 get_color_value("TOG_COLOR_COMMIT"));
5551 if (err)
5552 return err;
5555 view->show = show_blame_view;
5556 view->input = input_blame_view;
5557 view->reset = reset_blame_view;
5558 view->close = close_blame_view;
5559 view->search_start = search_start_blame_view;
5560 view->search_next = search_next_blame_view;
5562 return run_blame(view);
5565 static const struct got_error *
5566 close_blame_view(struct tog_view *view)
5568 const struct got_error *err = NULL;
5569 struct tog_blame_view_state *s = &view->state.blame;
5571 if (s->blame.thread)
5572 err = stop_blame(&s->blame);
5574 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5575 struct got_object_qid *blamed_commit;
5576 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5577 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5578 got_object_qid_free(blamed_commit);
5581 free(s->path);
5582 free_colors(&s->colors);
5583 return err;
5586 static const struct got_error *
5587 search_start_blame_view(struct tog_view *view)
5589 struct tog_blame_view_state *s = &view->state.blame;
5591 s->matched_line = 0;
5592 return NULL;
5595 static const struct got_error *
5596 search_next_blame_view(struct tog_view *view)
5598 struct tog_blame_view_state *s = &view->state.blame;
5599 const struct got_error *err = NULL;
5600 int lineno;
5601 char *line = NULL;
5602 size_t linesize = 0;
5603 ssize_t linelen;
5605 if (!view->searching) {
5606 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5607 return NULL;
5610 if (s->matched_line) {
5611 if (view->searching == TOG_SEARCH_FORWARD)
5612 lineno = s->matched_line + 1;
5613 else
5614 lineno = s->matched_line - 1;
5615 } else
5616 lineno = s->first_displayed_line - 1 + s->selected_line;
5618 while (1) {
5619 off_t offset;
5621 if (lineno <= 0 || lineno > s->blame.nlines) {
5622 if (s->matched_line == 0) {
5623 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5624 break;
5627 if (view->searching == TOG_SEARCH_FORWARD)
5628 lineno = 1;
5629 else
5630 lineno = s->blame.nlines;
5633 offset = s->blame.line_offsets[lineno - 1];
5634 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5635 free(line);
5636 return got_error_from_errno("fseeko");
5638 linelen = getline(&line, &linesize, s->blame.f);
5639 if (linelen != -1) {
5640 char *exstr;
5641 err = expand_tab(&exstr, line);
5642 if (err)
5643 break;
5644 if (match_line(exstr, &view->regex, 1,
5645 &view->regmatch)) {
5646 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5647 s->matched_line = lineno;
5648 free(exstr);
5649 break;
5651 free(exstr);
5653 if (view->searching == TOG_SEARCH_FORWARD)
5654 lineno++;
5655 else
5656 lineno--;
5658 free(line);
5660 if (s->matched_line) {
5661 s->first_displayed_line = s->matched_line;
5662 s->selected_line = 1;
5665 return err;
5668 static const struct got_error *
5669 show_blame_view(struct tog_view *view)
5671 const struct got_error *err = NULL;
5672 struct tog_blame_view_state *s = &view->state.blame;
5673 int errcode;
5675 if (s->blame.thread == 0 && !s->blame_complete) {
5676 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5677 &s->blame.thread_args);
5678 if (errcode)
5679 return got_error_set_errno(errcode, "pthread_create");
5681 halfdelay(1); /* fast refresh while annotating */
5684 if (s->blame_complete)
5685 halfdelay(10); /* disable fast refresh */
5687 err = draw_blame(view);
5689 view_border(view);
5690 return err;
5693 static const struct got_error *
5694 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5695 struct got_repository *repo, struct got_object_id *id)
5697 struct tog_view *log_view;
5698 const struct got_error *err = NULL;
5700 *new_view = NULL;
5702 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5703 if (log_view == NULL)
5704 return got_error_from_errno("view_open");
5706 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5707 if (err)
5708 view_close(log_view);
5709 else
5710 *new_view = log_view;
5712 return err;
5715 static const struct got_error *
5716 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5718 const struct got_error *err = NULL, *thread_err = NULL;
5719 struct tog_view *diff_view;
5720 struct tog_blame_view_state *s = &view->state.blame;
5721 int eos, nscroll, begin_y = 0, begin_x = 0;
5723 eos = nscroll = view->nlines - 2;
5724 if (view_is_hsplit_top(view))
5725 --eos; /* border */
5727 switch (ch) {
5728 case '0':
5729 view->x = 0;
5730 break;
5731 case '$':
5732 view->x = MAX(view->maxx - view->ncols / 3, 0);
5733 view->count = 0;
5734 break;
5735 case KEY_RIGHT:
5736 case 'l':
5737 if (view->x + view->ncols / 3 < view->maxx)
5738 view->x += 2; /* move two columns right */
5739 else
5740 view->count = 0;
5741 break;
5742 case KEY_LEFT:
5743 case 'h':
5744 view->x -= MIN(view->x, 2); /* move two columns back */
5745 if (view->x <= 0)
5746 view->count = 0;
5747 break;
5748 case 'q':
5749 s->done = 1;
5750 break;
5751 case 'g':
5752 case KEY_HOME:
5753 s->selected_line = 1;
5754 s->first_displayed_line = 1;
5755 view->count = 0;
5756 break;
5757 case 'G':
5758 case KEY_END:
5759 if (s->blame.nlines < eos) {
5760 s->selected_line = s->blame.nlines;
5761 s->first_displayed_line = 1;
5762 } else {
5763 s->selected_line = eos;
5764 s->first_displayed_line = s->blame.nlines - (eos - 1);
5766 view->count = 0;
5767 break;
5768 case 'k':
5769 case KEY_UP:
5770 case CTRL('p'):
5771 if (s->selected_line > 1)
5772 s->selected_line--;
5773 else if (s->selected_line == 1 &&
5774 s->first_displayed_line > 1)
5775 s->first_displayed_line--;
5776 else
5777 view->count = 0;
5778 break;
5779 case CTRL('u'):
5780 case 'u':
5781 nscroll /= 2;
5782 /* FALL THROUGH */
5783 case KEY_PPAGE:
5784 case CTRL('b'):
5785 case 'b':
5786 if (s->first_displayed_line == 1) {
5787 if (view->count > 1)
5788 nscroll += nscroll;
5789 s->selected_line = MAX(1, s->selected_line - nscroll);
5790 view->count = 0;
5791 break;
5793 if (s->first_displayed_line > nscroll)
5794 s->first_displayed_line -= nscroll;
5795 else
5796 s->first_displayed_line = 1;
5797 break;
5798 case 'j':
5799 case KEY_DOWN:
5800 case CTRL('n'):
5801 if (s->selected_line < eos && s->first_displayed_line +
5802 s->selected_line <= s->blame.nlines)
5803 s->selected_line++;
5804 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5805 s->first_displayed_line++;
5806 else
5807 view->count = 0;
5808 break;
5809 case 'c':
5810 case 'p': {
5811 struct got_object_id *id = NULL;
5813 view->count = 0;
5814 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5815 s->first_displayed_line, s->selected_line);
5816 if (id == NULL)
5817 break;
5818 if (ch == 'p') {
5819 struct got_commit_object *commit, *pcommit;
5820 struct got_object_qid *pid;
5821 struct got_object_id *blob_id = NULL;
5822 int obj_type;
5823 err = got_object_open_as_commit(&commit,
5824 s->repo, id);
5825 if (err)
5826 break;
5827 pid = STAILQ_FIRST(
5828 got_object_commit_get_parent_ids(commit));
5829 if (pid == NULL) {
5830 got_object_commit_close(commit);
5831 break;
5833 /* Check if path history ends here. */
5834 err = got_object_open_as_commit(&pcommit,
5835 s->repo, &pid->id);
5836 if (err)
5837 break;
5838 err = got_object_id_by_path(&blob_id, s->repo,
5839 pcommit, s->path);
5840 got_object_commit_close(pcommit);
5841 if (err) {
5842 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5843 err = NULL;
5844 got_object_commit_close(commit);
5845 break;
5847 err = got_object_get_type(&obj_type, s->repo,
5848 blob_id);
5849 free(blob_id);
5850 /* Can't blame non-blob type objects. */
5851 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5852 got_object_commit_close(commit);
5853 break;
5855 err = got_object_qid_alloc(&s->blamed_commit,
5856 &pid->id);
5857 got_object_commit_close(commit);
5858 } else {
5859 if (got_object_id_cmp(id,
5860 &s->blamed_commit->id) == 0)
5861 break;
5862 err = got_object_qid_alloc(&s->blamed_commit,
5863 id);
5865 if (err)
5866 break;
5867 s->done = 1;
5868 thread_err = stop_blame(&s->blame);
5869 s->done = 0;
5870 if (thread_err)
5871 break;
5872 STAILQ_INSERT_HEAD(&s->blamed_commits,
5873 s->blamed_commit, entry);
5874 err = run_blame(view);
5875 if (err)
5876 break;
5877 break;
5879 case 'C': {
5880 struct got_object_qid *first;
5882 view->count = 0;
5883 first = STAILQ_FIRST(&s->blamed_commits);
5884 if (!got_object_id_cmp(&first->id, s->commit_id))
5885 break;
5886 s->done = 1;
5887 thread_err = stop_blame(&s->blame);
5888 s->done = 0;
5889 if (thread_err)
5890 break;
5891 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5892 got_object_qid_free(s->blamed_commit);
5893 s->blamed_commit =
5894 STAILQ_FIRST(&s->blamed_commits);
5895 err = run_blame(view);
5896 if (err)
5897 break;
5898 break;
5900 case 'L':
5901 view->count = 0;
5902 s->id_to_log = get_selected_commit_id(s->blame.lines,
5903 s->blame.nlines, s->first_displayed_line, s->selected_line);
5904 if (s->id_to_log)
5905 err = view_request_new(new_view, view, TOG_VIEW_LOG);
5906 break;
5907 case KEY_ENTER:
5908 case '\r': {
5909 struct got_object_id *id = NULL;
5910 struct got_object_qid *pid;
5911 struct got_commit_object *commit = NULL;
5913 view->count = 0;
5914 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5915 s->first_displayed_line, s->selected_line);
5916 if (id == NULL)
5917 break;
5918 err = got_object_open_as_commit(&commit, s->repo, id);
5919 if (err)
5920 break;
5921 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5922 if (*new_view) {
5923 /* traversed from diff view, release diff resources */
5924 err = close_diff_view(*new_view);
5925 if (err)
5926 break;
5927 diff_view = *new_view;
5928 } else {
5929 if (view_is_parent_view(view))
5930 view_get_split(view, &begin_y, &begin_x);
5932 diff_view = view_open(0, 0, begin_y, begin_x,
5933 TOG_VIEW_DIFF);
5934 if (diff_view == NULL) {
5935 got_object_commit_close(commit);
5936 err = got_error_from_errno("view_open");
5937 break;
5940 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5941 id, NULL, NULL, 3, 0, 0, view, s->repo);
5942 got_object_commit_close(commit);
5943 if (err) {
5944 view_close(diff_view);
5945 break;
5947 s->last_diffed_line = s->first_displayed_line - 1 +
5948 s->selected_line;
5949 if (*new_view)
5950 break; /* still open from active diff view */
5951 if (view_is_parent_view(view) &&
5952 view->mode == TOG_VIEW_SPLIT_HRZN) {
5953 err = view_init_hsplit(view, begin_y);
5954 if (err)
5955 break;
5958 view->focussed = 0;
5959 diff_view->focussed = 1;
5960 diff_view->mode = view->mode;
5961 diff_view->nlines = view->lines - begin_y;
5962 if (view_is_parent_view(view)) {
5963 view_transfer_size(diff_view, view);
5964 err = view_close_child(view);
5965 if (err)
5966 break;
5967 err = view_set_child(view, diff_view);
5968 if (err)
5969 break;
5970 view->focus_child = 1;
5971 } else
5972 *new_view = diff_view;
5973 if (err)
5974 break;
5975 break;
5977 case CTRL('d'):
5978 case 'd':
5979 nscroll /= 2;
5980 /* FALL THROUGH */
5981 case KEY_NPAGE:
5982 case CTRL('f'):
5983 case 'f':
5984 case ' ':
5985 if (s->last_displayed_line >= s->blame.nlines &&
5986 s->selected_line >= MIN(s->blame.nlines,
5987 view->nlines - 2)) {
5988 view->count = 0;
5989 break;
5991 if (s->last_displayed_line >= s->blame.nlines &&
5992 s->selected_line < view->nlines - 2) {
5993 s->selected_line +=
5994 MIN(nscroll, s->last_displayed_line -
5995 s->first_displayed_line - s->selected_line + 1);
5997 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5998 s->first_displayed_line += nscroll;
5999 else
6000 s->first_displayed_line =
6001 s->blame.nlines - (view->nlines - 3);
6002 break;
6003 case KEY_RESIZE:
6004 if (s->selected_line > view->nlines - 2) {
6005 s->selected_line = MIN(s->blame.nlines,
6006 view->nlines - 2);
6008 break;
6009 default:
6010 view->count = 0;
6011 break;
6013 return thread_err ? thread_err : err;
6016 static const struct got_error *
6017 reset_blame_view(struct tog_view *view)
6019 const struct got_error *err;
6020 struct tog_blame_view_state *s = &view->state.blame;
6022 view->count = 0;
6023 s->done = 1;
6024 err = stop_blame(&s->blame);
6025 s->done = 0;
6026 if (err)
6027 return err;
6028 return run_blame(view);
6031 static const struct got_error *
6032 cmd_blame(int argc, char *argv[])
6034 const struct got_error *error;
6035 struct got_repository *repo = NULL;
6036 struct got_worktree *worktree = NULL;
6037 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6038 char *link_target = NULL;
6039 struct got_object_id *commit_id = NULL;
6040 struct got_commit_object *commit = NULL;
6041 char *commit_id_str = NULL;
6042 int ch;
6043 struct tog_view *view;
6044 int *pack_fds = NULL;
6046 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6047 switch (ch) {
6048 case 'c':
6049 commit_id_str = optarg;
6050 break;
6051 case 'r':
6052 repo_path = realpath(optarg, NULL);
6053 if (repo_path == NULL)
6054 return got_error_from_errno2("realpath",
6055 optarg);
6056 break;
6057 default:
6058 usage_blame();
6059 /* NOTREACHED */
6063 argc -= optind;
6064 argv += optind;
6066 if (argc != 1)
6067 usage_blame();
6069 error = got_repo_pack_fds_open(&pack_fds);
6070 if (error != NULL)
6071 goto done;
6073 if (repo_path == NULL) {
6074 cwd = getcwd(NULL, 0);
6075 if (cwd == NULL)
6076 return got_error_from_errno("getcwd");
6077 error = got_worktree_open(&worktree, cwd);
6078 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6079 goto done;
6080 if (worktree)
6081 repo_path =
6082 strdup(got_worktree_get_repo_path(worktree));
6083 else
6084 repo_path = strdup(cwd);
6085 if (repo_path == NULL) {
6086 error = got_error_from_errno("strdup");
6087 goto done;
6091 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6092 if (error != NULL)
6093 goto done;
6095 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6096 worktree);
6097 if (error)
6098 goto done;
6100 init_curses();
6102 error = apply_unveil(got_repo_get_path(repo), NULL);
6103 if (error)
6104 goto done;
6106 error = tog_load_refs(repo, 0);
6107 if (error)
6108 goto done;
6110 if (commit_id_str == NULL) {
6111 struct got_reference *head_ref;
6112 error = got_ref_open(&head_ref, repo, worktree ?
6113 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6114 if (error != NULL)
6115 goto done;
6116 error = got_ref_resolve(&commit_id, repo, head_ref);
6117 got_ref_close(head_ref);
6118 } else {
6119 error = got_repo_match_object_id(&commit_id, NULL,
6120 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6122 if (error != NULL)
6123 goto done;
6125 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6126 if (view == NULL) {
6127 error = got_error_from_errno("view_open");
6128 goto done;
6131 error = got_object_open_as_commit(&commit, repo, commit_id);
6132 if (error)
6133 goto done;
6135 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6136 commit, repo);
6137 if (error)
6138 goto done;
6140 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6141 commit_id, repo);
6142 if (error)
6143 goto done;
6144 if (worktree) {
6145 /* Release work tree lock. */
6146 got_worktree_close(worktree);
6147 worktree = NULL;
6149 error = view_loop(view);
6150 done:
6151 free(repo_path);
6152 free(in_repo_path);
6153 free(link_target);
6154 free(cwd);
6155 free(commit_id);
6156 if (commit)
6157 got_object_commit_close(commit);
6158 if (worktree)
6159 got_worktree_close(worktree);
6160 if (repo) {
6161 const struct got_error *close_err = got_repo_close(repo);
6162 if (error == NULL)
6163 error = close_err;
6165 if (pack_fds) {
6166 const struct got_error *pack_err =
6167 got_repo_pack_fds_close(pack_fds);
6168 if (error == NULL)
6169 error = pack_err;
6171 tog_free_refs();
6172 return error;
6175 static const struct got_error *
6176 draw_tree_entries(struct tog_view *view, const char *parent_path)
6178 struct tog_tree_view_state *s = &view->state.tree;
6179 const struct got_error *err = NULL;
6180 struct got_tree_entry *te;
6181 wchar_t *wline;
6182 struct tog_color *tc;
6183 int width, n, i, nentries;
6184 int limit = view->nlines;
6186 s->ndisplayed = 0;
6187 if (view_is_hsplit_top(view))
6188 --limit; /* border */
6190 werase(view->window);
6192 if (limit == 0)
6193 return NULL;
6195 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6196 0, 0);
6197 if (err)
6198 return err;
6199 if (view_needs_focus_indication(view))
6200 wstandout(view->window);
6201 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6202 if (tc)
6203 wattr_on(view->window,
6204 COLOR_PAIR(tc->colorpair), NULL);
6205 waddwstr(view->window, wline);
6206 if (tc)
6207 wattr_off(view->window,
6208 COLOR_PAIR(tc->colorpair), NULL);
6209 if (view_needs_focus_indication(view))
6210 wstandend(view->window);
6211 free(wline);
6212 wline = NULL;
6213 if (width < view->ncols - 1)
6214 waddch(view->window, '\n');
6215 if (--limit <= 0)
6216 return NULL;
6217 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6218 0, 0);
6219 if (err)
6220 return err;
6221 waddwstr(view->window, wline);
6222 free(wline);
6223 wline = NULL;
6224 if (width < view->ncols - 1)
6225 waddch(view->window, '\n');
6226 if (--limit <= 0)
6227 return NULL;
6228 waddch(view->window, '\n');
6229 if (--limit <= 0)
6230 return NULL;
6232 if (s->first_displayed_entry == NULL) {
6233 te = got_object_tree_get_first_entry(s->tree);
6234 if (s->selected == 0) {
6235 if (view->focussed)
6236 wstandout(view->window);
6237 s->selected_entry = NULL;
6239 waddstr(view->window, " ..\n"); /* parent directory */
6240 if (s->selected == 0 && view->focussed)
6241 wstandend(view->window);
6242 s->ndisplayed++;
6243 if (--limit <= 0)
6244 return NULL;
6245 n = 1;
6246 } else {
6247 n = 0;
6248 te = s->first_displayed_entry;
6251 nentries = got_object_tree_get_nentries(s->tree);
6252 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6253 char *line = NULL, *id_str = NULL, *link_target = NULL;
6254 const char *modestr = "";
6255 mode_t mode;
6257 te = got_object_tree_get_entry(s->tree, i);
6258 mode = got_tree_entry_get_mode(te);
6260 if (s->show_ids) {
6261 err = got_object_id_str(&id_str,
6262 got_tree_entry_get_id(te));
6263 if (err)
6264 return got_error_from_errno(
6265 "got_object_id_str");
6267 if (got_object_tree_entry_is_submodule(te))
6268 modestr = "$";
6269 else if (S_ISLNK(mode)) {
6270 int i;
6272 err = got_tree_entry_get_symlink_target(&link_target,
6273 te, s->repo);
6274 if (err) {
6275 free(id_str);
6276 return err;
6278 for (i = 0; i < strlen(link_target); i++) {
6279 if (!isprint((unsigned char)link_target[i]))
6280 link_target[i] = '?';
6282 modestr = "@";
6284 else if (S_ISDIR(mode))
6285 modestr = "/";
6286 else if (mode & S_IXUSR)
6287 modestr = "*";
6288 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6289 got_tree_entry_get_name(te), modestr,
6290 link_target ? " -> ": "",
6291 link_target ? link_target : "") == -1) {
6292 free(id_str);
6293 free(link_target);
6294 return got_error_from_errno("asprintf");
6296 free(id_str);
6297 free(link_target);
6298 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6299 0, 0);
6300 if (err) {
6301 free(line);
6302 break;
6304 if (n == s->selected) {
6305 if (view->focussed)
6306 wstandout(view->window);
6307 s->selected_entry = te;
6309 tc = match_color(&s->colors, line);
6310 if (tc)
6311 wattr_on(view->window,
6312 COLOR_PAIR(tc->colorpair), NULL);
6313 waddwstr(view->window, wline);
6314 if (tc)
6315 wattr_off(view->window,
6316 COLOR_PAIR(tc->colorpair), NULL);
6317 if (width < view->ncols - 1)
6318 waddch(view->window, '\n');
6319 if (n == s->selected && view->focussed)
6320 wstandend(view->window);
6321 free(line);
6322 free(wline);
6323 wline = NULL;
6324 n++;
6325 s->ndisplayed++;
6326 s->last_displayed_entry = te;
6327 if (--limit <= 0)
6328 break;
6331 return err;
6334 static void
6335 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6337 struct got_tree_entry *te;
6338 int isroot = s->tree == s->root;
6339 int i = 0;
6341 if (s->first_displayed_entry == NULL)
6342 return;
6344 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6345 while (i++ < maxscroll) {
6346 if (te == NULL) {
6347 if (!isroot)
6348 s->first_displayed_entry = NULL;
6349 break;
6351 s->first_displayed_entry = te;
6352 te = got_tree_entry_get_prev(s->tree, te);
6356 static const struct got_error *
6357 tree_scroll_down(struct tog_view *view, int maxscroll)
6359 struct tog_tree_view_state *s = &view->state.tree;
6360 struct got_tree_entry *next, *last;
6361 int n = 0;
6363 if (s->first_displayed_entry)
6364 next = got_tree_entry_get_next(s->tree,
6365 s->first_displayed_entry);
6366 else
6367 next = got_object_tree_get_first_entry(s->tree);
6369 last = s->last_displayed_entry;
6370 while (next && n++ < maxscroll) {
6371 if (last)
6372 last = got_tree_entry_get_next(s->tree, last);
6373 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6374 s->first_displayed_entry = next;
6375 next = got_tree_entry_get_next(s->tree, next);
6379 return NULL;
6382 static const struct got_error *
6383 tree_entry_path(char **path, struct tog_parent_trees *parents,
6384 struct got_tree_entry *te)
6386 const struct got_error *err = NULL;
6387 struct tog_parent_tree *pt;
6388 size_t len = 2; /* for leading slash and NUL */
6390 TAILQ_FOREACH(pt, parents, entry)
6391 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6392 + 1 /* slash */;
6393 if (te)
6394 len += strlen(got_tree_entry_get_name(te));
6396 *path = calloc(1, len);
6397 if (path == NULL)
6398 return got_error_from_errno("calloc");
6400 (*path)[0] = '/';
6401 pt = TAILQ_LAST(parents, tog_parent_trees);
6402 while (pt) {
6403 const char *name = got_tree_entry_get_name(pt->selected_entry);
6404 if (strlcat(*path, name, len) >= len) {
6405 err = got_error(GOT_ERR_NO_SPACE);
6406 goto done;
6408 if (strlcat(*path, "/", len) >= len) {
6409 err = got_error(GOT_ERR_NO_SPACE);
6410 goto done;
6412 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6414 if (te) {
6415 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6416 err = got_error(GOT_ERR_NO_SPACE);
6417 goto done;
6420 done:
6421 if (err) {
6422 free(*path);
6423 *path = NULL;
6425 return err;
6428 static const struct got_error *
6429 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6430 struct got_tree_entry *te, struct tog_parent_trees *parents,
6431 struct got_object_id *commit_id, struct got_repository *repo)
6433 const struct got_error *err = NULL;
6434 char *path;
6435 struct tog_view *blame_view;
6437 *new_view = NULL;
6439 err = tree_entry_path(&path, parents, te);
6440 if (err)
6441 return err;
6443 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6444 if (blame_view == NULL) {
6445 err = got_error_from_errno("view_open");
6446 goto done;
6449 err = open_blame_view(blame_view, path, commit_id, repo);
6450 if (err) {
6451 if (err->code == GOT_ERR_CANCELLED)
6452 err = NULL;
6453 view_close(blame_view);
6454 } else
6455 *new_view = blame_view;
6456 done:
6457 free(path);
6458 return err;
6461 static const struct got_error *
6462 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6463 struct tog_tree_view_state *s)
6465 struct tog_view *log_view;
6466 const struct got_error *err = NULL;
6467 char *path;
6469 *new_view = NULL;
6471 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6472 if (log_view == NULL)
6473 return got_error_from_errno("view_open");
6475 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6476 if (err)
6477 return err;
6479 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6480 path, 0);
6481 if (err)
6482 view_close(log_view);
6483 else
6484 *new_view = log_view;
6485 free(path);
6486 return err;
6489 static const struct got_error *
6490 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6491 const char *head_ref_name, struct got_repository *repo)
6493 const struct got_error *err = NULL;
6494 char *commit_id_str = NULL;
6495 struct tog_tree_view_state *s = &view->state.tree;
6496 struct got_commit_object *commit = NULL;
6498 TAILQ_INIT(&s->parents);
6499 STAILQ_INIT(&s->colors);
6501 s->commit_id = got_object_id_dup(commit_id);
6502 if (s->commit_id == NULL)
6503 return got_error_from_errno("got_object_id_dup");
6505 err = got_object_open_as_commit(&commit, repo, commit_id);
6506 if (err)
6507 goto done;
6510 * The root is opened here and will be closed when the view is closed.
6511 * Any visited subtrees and their path-wise parents are opened and
6512 * closed on demand.
6514 err = got_object_open_as_tree(&s->root, repo,
6515 got_object_commit_get_tree_id(commit));
6516 if (err)
6517 goto done;
6518 s->tree = s->root;
6520 err = got_object_id_str(&commit_id_str, commit_id);
6521 if (err != NULL)
6522 goto done;
6524 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6525 err = got_error_from_errno("asprintf");
6526 goto done;
6529 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6530 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6531 if (head_ref_name) {
6532 s->head_ref_name = strdup(head_ref_name);
6533 if (s->head_ref_name == NULL) {
6534 err = got_error_from_errno("strdup");
6535 goto done;
6538 s->repo = repo;
6540 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6541 err = add_color(&s->colors, "\\$$",
6542 TOG_COLOR_TREE_SUBMODULE,
6543 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6544 if (err)
6545 goto done;
6546 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6547 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6548 if (err)
6549 goto done;
6550 err = add_color(&s->colors, "/$",
6551 TOG_COLOR_TREE_DIRECTORY,
6552 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6553 if (err)
6554 goto done;
6556 err = add_color(&s->colors, "\\*$",
6557 TOG_COLOR_TREE_EXECUTABLE,
6558 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6559 if (err)
6560 goto done;
6562 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6563 get_color_value("TOG_COLOR_COMMIT"));
6564 if (err)
6565 goto done;
6568 view->show = show_tree_view;
6569 view->input = input_tree_view;
6570 view->close = close_tree_view;
6571 view->search_start = search_start_tree_view;
6572 view->search_next = search_next_tree_view;
6573 done:
6574 free(commit_id_str);
6575 if (commit)
6576 got_object_commit_close(commit);
6577 if (err)
6578 close_tree_view(view);
6579 return err;
6582 static const struct got_error *
6583 close_tree_view(struct tog_view *view)
6585 struct tog_tree_view_state *s = &view->state.tree;
6587 free_colors(&s->colors);
6588 free(s->tree_label);
6589 s->tree_label = NULL;
6590 free(s->commit_id);
6591 s->commit_id = NULL;
6592 free(s->head_ref_name);
6593 s->head_ref_name = NULL;
6594 while (!TAILQ_EMPTY(&s->parents)) {
6595 struct tog_parent_tree *parent;
6596 parent = TAILQ_FIRST(&s->parents);
6597 TAILQ_REMOVE(&s->parents, parent, entry);
6598 if (parent->tree != s->root)
6599 got_object_tree_close(parent->tree);
6600 free(parent);
6603 if (s->tree != NULL && s->tree != s->root)
6604 got_object_tree_close(s->tree);
6605 if (s->root)
6606 got_object_tree_close(s->root);
6607 return NULL;
6610 static const struct got_error *
6611 search_start_tree_view(struct tog_view *view)
6613 struct tog_tree_view_state *s = &view->state.tree;
6615 s->matched_entry = NULL;
6616 return NULL;
6619 static int
6620 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6622 regmatch_t regmatch;
6624 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6625 0) == 0;
6628 static const struct got_error *
6629 search_next_tree_view(struct tog_view *view)
6631 struct tog_tree_view_state *s = &view->state.tree;
6632 struct got_tree_entry *te = NULL;
6634 if (!view->searching) {
6635 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6636 return NULL;
6639 if (s->matched_entry) {
6640 if (view->searching == TOG_SEARCH_FORWARD) {
6641 if (s->selected_entry)
6642 te = got_tree_entry_get_next(s->tree,
6643 s->selected_entry);
6644 else
6645 te = got_object_tree_get_first_entry(s->tree);
6646 } else {
6647 if (s->selected_entry == NULL)
6648 te = got_object_tree_get_last_entry(s->tree);
6649 else
6650 te = got_tree_entry_get_prev(s->tree,
6651 s->selected_entry);
6653 } else {
6654 if (s->selected_entry)
6655 te = s->selected_entry;
6656 else if (view->searching == TOG_SEARCH_FORWARD)
6657 te = got_object_tree_get_first_entry(s->tree);
6658 else
6659 te = got_object_tree_get_last_entry(s->tree);
6662 while (1) {
6663 if (te == NULL) {
6664 if (s->matched_entry == NULL) {
6665 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6666 return NULL;
6668 if (view->searching == TOG_SEARCH_FORWARD)
6669 te = got_object_tree_get_first_entry(s->tree);
6670 else
6671 te = got_object_tree_get_last_entry(s->tree);
6674 if (match_tree_entry(te, &view->regex)) {
6675 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6676 s->matched_entry = te;
6677 break;
6680 if (view->searching == TOG_SEARCH_FORWARD)
6681 te = got_tree_entry_get_next(s->tree, te);
6682 else
6683 te = got_tree_entry_get_prev(s->tree, te);
6686 if (s->matched_entry) {
6687 s->first_displayed_entry = s->matched_entry;
6688 s->selected = 0;
6691 return NULL;
6694 static const struct got_error *
6695 show_tree_view(struct tog_view *view)
6697 const struct got_error *err = NULL;
6698 struct tog_tree_view_state *s = &view->state.tree;
6699 char *parent_path;
6701 err = tree_entry_path(&parent_path, &s->parents, NULL);
6702 if (err)
6703 return err;
6705 err = draw_tree_entries(view, parent_path);
6706 free(parent_path);
6708 view_border(view);
6709 return err;
6712 static const struct got_error *
6713 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6715 const struct got_error *err = NULL;
6716 struct tog_tree_view_state *s = &view->state.tree;
6717 struct got_tree_entry *te;
6718 int n, nscroll = view->nlines - 3;
6720 switch (ch) {
6721 case 'i':
6722 s->show_ids = !s->show_ids;
6723 view->count = 0;
6724 break;
6725 case 'L':
6726 view->count = 0;
6727 if (!s->selected_entry)
6728 break;
6729 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6730 break;
6731 case 'R':
6732 view->count = 0;
6733 err = view_request_new(new_view, view, TOG_VIEW_REF);
6734 break;
6735 case 'g':
6736 case KEY_HOME:
6737 s->selected = 0;
6738 view->count = 0;
6739 if (s->tree == s->root)
6740 s->first_displayed_entry =
6741 got_object_tree_get_first_entry(s->tree);
6742 else
6743 s->first_displayed_entry = NULL;
6744 break;
6745 case 'G':
6746 case KEY_END: {
6747 int eos = view->nlines - 3;
6749 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6750 --eos; /* border */
6751 s->selected = 0;
6752 view->count = 0;
6753 te = got_object_tree_get_last_entry(s->tree);
6754 for (n = 0; n < eos; n++) {
6755 if (te == NULL) {
6756 if (s->tree != s->root) {
6757 s->first_displayed_entry = NULL;
6758 n++;
6760 break;
6762 s->first_displayed_entry = te;
6763 te = got_tree_entry_get_prev(s->tree, te);
6765 if (n > 0)
6766 s->selected = n - 1;
6767 break;
6769 case 'k':
6770 case KEY_UP:
6771 case CTRL('p'):
6772 if (s->selected > 0) {
6773 s->selected--;
6774 break;
6776 tree_scroll_up(s, 1);
6777 if (s->selected_entry == NULL ||
6778 (s->tree == s->root && s->selected_entry ==
6779 got_object_tree_get_first_entry(s->tree)))
6780 view->count = 0;
6781 break;
6782 case CTRL('u'):
6783 case 'u':
6784 nscroll /= 2;
6785 /* FALL THROUGH */
6786 case KEY_PPAGE:
6787 case CTRL('b'):
6788 case 'b':
6789 if (s->tree == s->root) {
6790 if (got_object_tree_get_first_entry(s->tree) ==
6791 s->first_displayed_entry)
6792 s->selected -= MIN(s->selected, nscroll);
6793 } else {
6794 if (s->first_displayed_entry == NULL)
6795 s->selected -= MIN(s->selected, nscroll);
6797 tree_scroll_up(s, MAX(0, nscroll));
6798 if (s->selected_entry == NULL ||
6799 (s->tree == s->root && s->selected_entry ==
6800 got_object_tree_get_first_entry(s->tree)))
6801 view->count = 0;
6802 break;
6803 case 'j':
6804 case KEY_DOWN:
6805 case CTRL('n'):
6806 if (s->selected < s->ndisplayed - 1) {
6807 s->selected++;
6808 break;
6810 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6811 == NULL) {
6812 /* can't scroll any further */
6813 view->count = 0;
6814 break;
6816 tree_scroll_down(view, 1);
6817 break;
6818 case CTRL('d'):
6819 case 'd':
6820 nscroll /= 2;
6821 /* FALL THROUGH */
6822 case KEY_NPAGE:
6823 case CTRL('f'):
6824 case 'f':
6825 case ' ':
6826 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6827 == NULL) {
6828 /* can't scroll any further; move cursor down */
6829 if (s->selected < s->ndisplayed - 1)
6830 s->selected += MIN(nscroll,
6831 s->ndisplayed - s->selected - 1);
6832 else
6833 view->count = 0;
6834 break;
6836 tree_scroll_down(view, nscroll);
6837 break;
6838 case KEY_ENTER:
6839 case '\r':
6840 case KEY_BACKSPACE:
6841 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6842 struct tog_parent_tree *parent;
6843 /* user selected '..' */
6844 if (s->tree == s->root) {
6845 view->count = 0;
6846 break;
6848 parent = TAILQ_FIRST(&s->parents);
6849 TAILQ_REMOVE(&s->parents, parent,
6850 entry);
6851 got_object_tree_close(s->tree);
6852 s->tree = parent->tree;
6853 s->first_displayed_entry =
6854 parent->first_displayed_entry;
6855 s->selected_entry =
6856 parent->selected_entry;
6857 s->selected = parent->selected;
6858 if (s->selected > view->nlines - 3) {
6859 err = offset_selection_down(view);
6860 if (err)
6861 break;
6863 free(parent);
6864 } else if (S_ISDIR(got_tree_entry_get_mode(
6865 s->selected_entry))) {
6866 struct got_tree_object *subtree;
6867 view->count = 0;
6868 err = got_object_open_as_tree(&subtree, s->repo,
6869 got_tree_entry_get_id(s->selected_entry));
6870 if (err)
6871 break;
6872 err = tree_view_visit_subtree(s, subtree);
6873 if (err) {
6874 got_object_tree_close(subtree);
6875 break;
6877 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6878 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6879 break;
6880 case KEY_RESIZE:
6881 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6882 s->selected = view->nlines - 4;
6883 view->count = 0;
6884 break;
6885 default:
6886 view->count = 0;
6887 break;
6890 return err;
6893 __dead static void
6894 usage_tree(void)
6896 endwin();
6897 fprintf(stderr,
6898 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6899 getprogname());
6900 exit(1);
6903 static const struct got_error *
6904 cmd_tree(int argc, char *argv[])
6906 const struct got_error *error;
6907 struct got_repository *repo = NULL;
6908 struct got_worktree *worktree = NULL;
6909 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6910 struct got_object_id *commit_id = NULL;
6911 struct got_commit_object *commit = NULL;
6912 const char *commit_id_arg = NULL;
6913 char *label = NULL;
6914 struct got_reference *ref = NULL;
6915 const char *head_ref_name = NULL;
6916 int ch;
6917 struct tog_view *view;
6918 int *pack_fds = NULL;
6920 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6921 switch (ch) {
6922 case 'c':
6923 commit_id_arg = optarg;
6924 break;
6925 case 'r':
6926 repo_path = realpath(optarg, NULL);
6927 if (repo_path == NULL)
6928 return got_error_from_errno2("realpath",
6929 optarg);
6930 break;
6931 default:
6932 usage_tree();
6933 /* NOTREACHED */
6937 argc -= optind;
6938 argv += optind;
6940 if (argc > 1)
6941 usage_tree();
6943 error = got_repo_pack_fds_open(&pack_fds);
6944 if (error != NULL)
6945 goto done;
6947 if (repo_path == NULL) {
6948 cwd = getcwd(NULL, 0);
6949 if (cwd == NULL)
6950 return got_error_from_errno("getcwd");
6951 error = got_worktree_open(&worktree, cwd);
6952 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6953 goto done;
6954 if (worktree)
6955 repo_path =
6956 strdup(got_worktree_get_repo_path(worktree));
6957 else
6958 repo_path = strdup(cwd);
6959 if (repo_path == NULL) {
6960 error = got_error_from_errno("strdup");
6961 goto done;
6965 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6966 if (error != NULL)
6967 goto done;
6969 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6970 repo, worktree);
6971 if (error)
6972 goto done;
6974 init_curses();
6976 error = apply_unveil(got_repo_get_path(repo), NULL);
6977 if (error)
6978 goto done;
6980 error = tog_load_refs(repo, 0);
6981 if (error)
6982 goto done;
6984 if (commit_id_arg == NULL) {
6985 error = got_repo_match_object_id(&commit_id, &label,
6986 worktree ? got_worktree_get_head_ref_name(worktree) :
6987 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6988 if (error)
6989 goto done;
6990 head_ref_name = label;
6991 } else {
6992 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6993 if (error == NULL)
6994 head_ref_name = got_ref_get_name(ref);
6995 else if (error->code != GOT_ERR_NOT_REF)
6996 goto done;
6997 error = got_repo_match_object_id(&commit_id, NULL,
6998 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6999 if (error)
7000 goto done;
7003 error = got_object_open_as_commit(&commit, repo, commit_id);
7004 if (error)
7005 goto done;
7007 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7008 if (view == NULL) {
7009 error = got_error_from_errno("view_open");
7010 goto done;
7012 error = open_tree_view(view, commit_id, head_ref_name, repo);
7013 if (error)
7014 goto done;
7015 if (!got_path_is_root_dir(in_repo_path)) {
7016 error = tree_view_walk_path(&view->state.tree, commit,
7017 in_repo_path);
7018 if (error)
7019 goto done;
7022 if (worktree) {
7023 /* Release work tree lock. */
7024 got_worktree_close(worktree);
7025 worktree = NULL;
7027 error = view_loop(view);
7028 done:
7029 free(repo_path);
7030 free(cwd);
7031 free(commit_id);
7032 free(label);
7033 if (ref)
7034 got_ref_close(ref);
7035 if (repo) {
7036 const struct got_error *close_err = got_repo_close(repo);
7037 if (error == NULL)
7038 error = close_err;
7040 if (pack_fds) {
7041 const struct got_error *pack_err =
7042 got_repo_pack_fds_close(pack_fds);
7043 if (error == NULL)
7044 error = pack_err;
7046 tog_free_refs();
7047 return error;
7050 static const struct got_error *
7051 ref_view_load_refs(struct tog_ref_view_state *s)
7053 struct got_reflist_entry *sre;
7054 struct tog_reflist_entry *re;
7056 s->nrefs = 0;
7057 TAILQ_FOREACH(sre, &tog_refs, entry) {
7058 if (strncmp(got_ref_get_name(sre->ref),
7059 "refs/got/", 9) == 0 &&
7060 strncmp(got_ref_get_name(sre->ref),
7061 "refs/got/backup/", 16) != 0)
7062 continue;
7064 re = malloc(sizeof(*re));
7065 if (re == NULL)
7066 return got_error_from_errno("malloc");
7068 re->ref = got_ref_dup(sre->ref);
7069 if (re->ref == NULL)
7070 return got_error_from_errno("got_ref_dup");
7071 re->idx = s->nrefs++;
7072 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7075 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7076 return NULL;
7079 static void
7080 ref_view_free_refs(struct tog_ref_view_state *s)
7082 struct tog_reflist_entry *re;
7084 while (!TAILQ_EMPTY(&s->refs)) {
7085 re = TAILQ_FIRST(&s->refs);
7086 TAILQ_REMOVE(&s->refs, re, entry);
7087 got_ref_close(re->ref);
7088 free(re);
7092 static const struct got_error *
7093 open_ref_view(struct tog_view *view, struct got_repository *repo)
7095 const struct got_error *err = NULL;
7096 struct tog_ref_view_state *s = &view->state.ref;
7098 s->selected_entry = 0;
7099 s->repo = repo;
7101 TAILQ_INIT(&s->refs);
7102 STAILQ_INIT(&s->colors);
7104 err = ref_view_load_refs(s);
7105 if (err)
7106 return err;
7108 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7109 err = add_color(&s->colors, "^refs/heads/",
7110 TOG_COLOR_REFS_HEADS,
7111 get_color_value("TOG_COLOR_REFS_HEADS"));
7112 if (err)
7113 goto done;
7115 err = add_color(&s->colors, "^refs/tags/",
7116 TOG_COLOR_REFS_TAGS,
7117 get_color_value("TOG_COLOR_REFS_TAGS"));
7118 if (err)
7119 goto done;
7121 err = add_color(&s->colors, "^refs/remotes/",
7122 TOG_COLOR_REFS_REMOTES,
7123 get_color_value("TOG_COLOR_REFS_REMOTES"));
7124 if (err)
7125 goto done;
7127 err = add_color(&s->colors, "^refs/got/backup/",
7128 TOG_COLOR_REFS_BACKUP,
7129 get_color_value("TOG_COLOR_REFS_BACKUP"));
7130 if (err)
7131 goto done;
7134 view->show = show_ref_view;
7135 view->input = input_ref_view;
7136 view->close = close_ref_view;
7137 view->search_start = search_start_ref_view;
7138 view->search_next = search_next_ref_view;
7139 done:
7140 if (err)
7141 free_colors(&s->colors);
7142 return err;
7145 static const struct got_error *
7146 close_ref_view(struct tog_view *view)
7148 struct tog_ref_view_state *s = &view->state.ref;
7150 ref_view_free_refs(s);
7151 free_colors(&s->colors);
7153 return NULL;
7156 static const struct got_error *
7157 resolve_reflist_entry(struct got_object_id **commit_id,
7158 struct tog_reflist_entry *re, struct got_repository *repo)
7160 const struct got_error *err = NULL;
7161 struct got_object_id *obj_id;
7162 struct got_tag_object *tag = NULL;
7163 int obj_type;
7165 *commit_id = NULL;
7167 err = got_ref_resolve(&obj_id, repo, re->ref);
7168 if (err)
7169 return err;
7171 err = got_object_get_type(&obj_type, repo, obj_id);
7172 if (err)
7173 goto done;
7175 switch (obj_type) {
7176 case GOT_OBJ_TYPE_COMMIT:
7177 *commit_id = obj_id;
7178 break;
7179 case GOT_OBJ_TYPE_TAG:
7180 err = got_object_open_as_tag(&tag, repo, obj_id);
7181 if (err)
7182 goto done;
7183 free(obj_id);
7184 err = got_object_get_type(&obj_type, repo,
7185 got_object_tag_get_object_id(tag));
7186 if (err)
7187 goto done;
7188 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7189 err = got_error(GOT_ERR_OBJ_TYPE);
7190 goto done;
7192 *commit_id = got_object_id_dup(
7193 got_object_tag_get_object_id(tag));
7194 if (*commit_id == NULL) {
7195 err = got_error_from_errno("got_object_id_dup");
7196 goto done;
7198 break;
7199 default:
7200 err = got_error(GOT_ERR_OBJ_TYPE);
7201 break;
7204 done:
7205 if (tag)
7206 got_object_tag_close(tag);
7207 if (err) {
7208 free(*commit_id);
7209 *commit_id = NULL;
7211 return err;
7214 static const struct got_error *
7215 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7216 struct tog_reflist_entry *re, struct got_repository *repo)
7218 struct tog_view *log_view;
7219 const struct got_error *err = NULL;
7220 struct got_object_id *commit_id = NULL;
7222 *new_view = NULL;
7224 err = resolve_reflist_entry(&commit_id, re, repo);
7225 if (err) {
7226 if (err->code != GOT_ERR_OBJ_TYPE)
7227 return err;
7228 else
7229 return NULL;
7232 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7233 if (log_view == NULL) {
7234 err = got_error_from_errno("view_open");
7235 goto done;
7238 err = open_log_view(log_view, commit_id, repo,
7239 got_ref_get_name(re->ref), "", 0);
7240 done:
7241 if (err)
7242 view_close(log_view);
7243 else
7244 *new_view = log_view;
7245 free(commit_id);
7246 return err;
7249 static void
7250 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7252 struct tog_reflist_entry *re;
7253 int i = 0;
7255 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7256 return;
7258 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7259 while (i++ < maxscroll) {
7260 if (re == NULL)
7261 break;
7262 s->first_displayed_entry = re;
7263 re = TAILQ_PREV(re, tog_reflist_head, entry);
7267 static const struct got_error *
7268 ref_scroll_down(struct tog_view *view, int maxscroll)
7270 struct tog_ref_view_state *s = &view->state.ref;
7271 struct tog_reflist_entry *next, *last;
7272 int n = 0;
7274 if (s->first_displayed_entry)
7275 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7276 else
7277 next = TAILQ_FIRST(&s->refs);
7279 last = s->last_displayed_entry;
7280 while (next && n++ < maxscroll) {
7281 if (last)
7282 last = TAILQ_NEXT(last, entry);
7283 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7284 s->first_displayed_entry = next;
7285 next = TAILQ_NEXT(next, entry);
7289 return NULL;
7292 static const struct got_error *
7293 search_start_ref_view(struct tog_view *view)
7295 struct tog_ref_view_state *s = &view->state.ref;
7297 s->matched_entry = NULL;
7298 return NULL;
7301 static int
7302 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7304 regmatch_t regmatch;
7306 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7307 0) == 0;
7310 static const struct got_error *
7311 search_next_ref_view(struct tog_view *view)
7313 struct tog_ref_view_state *s = &view->state.ref;
7314 struct tog_reflist_entry *re = NULL;
7316 if (!view->searching) {
7317 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7318 return NULL;
7321 if (s->matched_entry) {
7322 if (view->searching == TOG_SEARCH_FORWARD) {
7323 if (s->selected_entry)
7324 re = TAILQ_NEXT(s->selected_entry, entry);
7325 else
7326 re = TAILQ_PREV(s->selected_entry,
7327 tog_reflist_head, entry);
7328 } else {
7329 if (s->selected_entry == NULL)
7330 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7331 else
7332 re = TAILQ_PREV(s->selected_entry,
7333 tog_reflist_head, entry);
7335 } else {
7336 if (s->selected_entry)
7337 re = s->selected_entry;
7338 else if (view->searching == TOG_SEARCH_FORWARD)
7339 re = TAILQ_FIRST(&s->refs);
7340 else
7341 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7344 while (1) {
7345 if (re == NULL) {
7346 if (s->matched_entry == NULL) {
7347 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7348 return NULL;
7350 if (view->searching == TOG_SEARCH_FORWARD)
7351 re = TAILQ_FIRST(&s->refs);
7352 else
7353 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7356 if (match_reflist_entry(re, &view->regex)) {
7357 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7358 s->matched_entry = re;
7359 break;
7362 if (view->searching == TOG_SEARCH_FORWARD)
7363 re = TAILQ_NEXT(re, entry);
7364 else
7365 re = TAILQ_PREV(re, tog_reflist_head, entry);
7368 if (s->matched_entry) {
7369 s->first_displayed_entry = s->matched_entry;
7370 s->selected = 0;
7373 return NULL;
7376 static const struct got_error *
7377 show_ref_view(struct tog_view *view)
7379 const struct got_error *err = NULL;
7380 struct tog_ref_view_state *s = &view->state.ref;
7381 struct tog_reflist_entry *re;
7382 char *line = NULL;
7383 wchar_t *wline;
7384 struct tog_color *tc;
7385 int width, n;
7386 int limit = view->nlines;
7388 werase(view->window);
7390 s->ndisplayed = 0;
7391 if (view_is_hsplit_top(view))
7392 --limit; /* border */
7394 if (limit == 0)
7395 return NULL;
7397 re = s->first_displayed_entry;
7399 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7400 s->nrefs) == -1)
7401 return got_error_from_errno("asprintf");
7403 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7404 if (err) {
7405 free(line);
7406 return err;
7408 if (view_needs_focus_indication(view))
7409 wstandout(view->window);
7410 waddwstr(view->window, wline);
7411 if (view_needs_focus_indication(view))
7412 wstandend(view->window);
7413 free(wline);
7414 wline = NULL;
7415 free(line);
7416 line = NULL;
7417 if (width < view->ncols - 1)
7418 waddch(view->window, '\n');
7419 if (--limit <= 0)
7420 return NULL;
7422 n = 0;
7423 while (re && limit > 0) {
7424 char *line = NULL;
7425 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7427 if (s->show_date) {
7428 struct got_commit_object *ci;
7429 struct got_tag_object *tag;
7430 struct got_object_id *id;
7431 struct tm tm;
7432 time_t t;
7434 err = got_ref_resolve(&id, s->repo, re->ref);
7435 if (err)
7436 return err;
7437 err = got_object_open_as_tag(&tag, s->repo, id);
7438 if (err) {
7439 if (err->code != GOT_ERR_OBJ_TYPE) {
7440 free(id);
7441 return err;
7443 err = got_object_open_as_commit(&ci, s->repo,
7444 id);
7445 if (err) {
7446 free(id);
7447 return err;
7449 t = got_object_commit_get_committer_time(ci);
7450 got_object_commit_close(ci);
7451 } else {
7452 t = got_object_tag_get_tagger_time(tag);
7453 got_object_tag_close(tag);
7455 free(id);
7456 if (gmtime_r(&t, &tm) == NULL)
7457 return got_error_from_errno("gmtime_r");
7458 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7459 return got_error(GOT_ERR_NO_SPACE);
7461 if (got_ref_is_symbolic(re->ref)) {
7462 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7463 ymd : "", got_ref_get_name(re->ref),
7464 got_ref_get_symref_target(re->ref)) == -1)
7465 return got_error_from_errno("asprintf");
7466 } else if (s->show_ids) {
7467 struct got_object_id *id;
7468 char *id_str;
7469 err = got_ref_resolve(&id, s->repo, re->ref);
7470 if (err)
7471 return err;
7472 err = got_object_id_str(&id_str, id);
7473 if (err) {
7474 free(id);
7475 return err;
7477 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7478 got_ref_get_name(re->ref), id_str) == -1) {
7479 err = got_error_from_errno("asprintf");
7480 free(id);
7481 free(id_str);
7482 return err;
7484 free(id);
7485 free(id_str);
7486 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7487 got_ref_get_name(re->ref)) == -1)
7488 return got_error_from_errno("asprintf");
7490 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7491 0, 0);
7492 if (err) {
7493 free(line);
7494 return err;
7496 if (n == s->selected) {
7497 if (view->focussed)
7498 wstandout(view->window);
7499 s->selected_entry = re;
7501 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7502 if (tc)
7503 wattr_on(view->window,
7504 COLOR_PAIR(tc->colorpair), NULL);
7505 waddwstr(view->window, wline);
7506 if (tc)
7507 wattr_off(view->window,
7508 COLOR_PAIR(tc->colorpair), NULL);
7509 if (width < view->ncols - 1)
7510 waddch(view->window, '\n');
7511 if (n == s->selected && view->focussed)
7512 wstandend(view->window);
7513 free(line);
7514 free(wline);
7515 wline = NULL;
7516 n++;
7517 s->ndisplayed++;
7518 s->last_displayed_entry = re;
7520 limit--;
7521 re = TAILQ_NEXT(re, entry);
7524 view_border(view);
7525 return err;
7528 static const struct got_error *
7529 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7530 struct tog_reflist_entry *re, struct got_repository *repo)
7532 const struct got_error *err = NULL;
7533 struct got_object_id *commit_id = NULL;
7534 struct tog_view *tree_view;
7536 *new_view = NULL;
7538 err = resolve_reflist_entry(&commit_id, re, repo);
7539 if (err) {
7540 if (err->code != GOT_ERR_OBJ_TYPE)
7541 return err;
7542 else
7543 return NULL;
7547 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7548 if (tree_view == NULL) {
7549 err = got_error_from_errno("view_open");
7550 goto done;
7553 err = open_tree_view(tree_view, commit_id,
7554 got_ref_get_name(re->ref), repo);
7555 if (err)
7556 goto done;
7558 *new_view = tree_view;
7559 done:
7560 free(commit_id);
7561 return err;
7563 static const struct got_error *
7564 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7566 const struct got_error *err = NULL;
7567 struct tog_ref_view_state *s = &view->state.ref;
7568 struct tog_reflist_entry *re;
7569 int n, nscroll = view->nlines - 1;
7571 switch (ch) {
7572 case 'i':
7573 s->show_ids = !s->show_ids;
7574 view->count = 0;
7575 break;
7576 case 'm':
7577 s->show_date = !s->show_date;
7578 view->count = 0;
7579 break;
7580 case 'o':
7581 s->sort_by_date = !s->sort_by_date;
7582 view->count = 0;
7583 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7584 got_ref_cmp_by_commit_timestamp_descending :
7585 tog_ref_cmp_by_name, s->repo);
7586 if (err)
7587 break;
7588 got_reflist_object_id_map_free(tog_refs_idmap);
7589 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7590 &tog_refs, s->repo);
7591 if (err)
7592 break;
7593 ref_view_free_refs(s);
7594 err = ref_view_load_refs(s);
7595 break;
7596 case KEY_ENTER:
7597 case '\r':
7598 view->count = 0;
7599 if (!s->selected_entry)
7600 break;
7601 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7602 break;
7603 case 'T':
7604 view->count = 0;
7605 if (!s->selected_entry)
7606 break;
7607 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7608 break;
7609 case 'g':
7610 case KEY_HOME:
7611 s->selected = 0;
7612 view->count = 0;
7613 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7614 break;
7615 case 'G':
7616 case KEY_END: {
7617 int eos = view->nlines - 1;
7619 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7620 --eos; /* border */
7621 s->selected = 0;
7622 view->count = 0;
7623 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7624 for (n = 0; n < eos; n++) {
7625 if (re == NULL)
7626 break;
7627 s->first_displayed_entry = re;
7628 re = TAILQ_PREV(re, tog_reflist_head, entry);
7630 if (n > 0)
7631 s->selected = n - 1;
7632 break;
7634 case 'k':
7635 case KEY_UP:
7636 case CTRL('p'):
7637 if (s->selected > 0) {
7638 s->selected--;
7639 break;
7641 ref_scroll_up(s, 1);
7642 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7643 view->count = 0;
7644 break;
7645 case CTRL('u'):
7646 case 'u':
7647 nscroll /= 2;
7648 /* FALL THROUGH */
7649 case KEY_PPAGE:
7650 case CTRL('b'):
7651 case 'b':
7652 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7653 s->selected -= MIN(nscroll, s->selected);
7654 ref_scroll_up(s, MAX(0, nscroll));
7655 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7656 view->count = 0;
7657 break;
7658 case 'j':
7659 case KEY_DOWN:
7660 case CTRL('n'):
7661 if (s->selected < s->ndisplayed - 1) {
7662 s->selected++;
7663 break;
7665 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7666 /* can't scroll any further */
7667 view->count = 0;
7668 break;
7670 ref_scroll_down(view, 1);
7671 break;
7672 case CTRL('d'):
7673 case 'd':
7674 nscroll /= 2;
7675 /* FALL THROUGH */
7676 case KEY_NPAGE:
7677 case CTRL('f'):
7678 case 'f':
7679 case ' ':
7680 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7681 /* can't scroll any further; move cursor down */
7682 if (s->selected < s->ndisplayed - 1)
7683 s->selected += MIN(nscroll,
7684 s->ndisplayed - s->selected - 1);
7685 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7686 s->selected += s->ndisplayed - s->selected - 1;
7687 view->count = 0;
7688 break;
7690 ref_scroll_down(view, nscroll);
7691 break;
7692 case CTRL('l'):
7693 view->count = 0;
7694 tog_free_refs();
7695 err = tog_load_refs(s->repo, s->sort_by_date);
7696 if (err)
7697 break;
7698 ref_view_free_refs(s);
7699 err = ref_view_load_refs(s);
7700 break;
7701 case KEY_RESIZE:
7702 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7703 s->selected = view->nlines - 2;
7704 break;
7705 default:
7706 view->count = 0;
7707 break;
7710 return err;
7713 __dead static void
7714 usage_ref(void)
7716 endwin();
7717 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7718 getprogname());
7719 exit(1);
7722 static const struct got_error *
7723 cmd_ref(int argc, char *argv[])
7725 const struct got_error *error;
7726 struct got_repository *repo = NULL;
7727 struct got_worktree *worktree = NULL;
7728 char *cwd = NULL, *repo_path = NULL;
7729 int ch;
7730 struct tog_view *view;
7731 int *pack_fds = NULL;
7733 while ((ch = getopt(argc, argv, "r:")) != -1) {
7734 switch (ch) {
7735 case 'r':
7736 repo_path = realpath(optarg, NULL);
7737 if (repo_path == NULL)
7738 return got_error_from_errno2("realpath",
7739 optarg);
7740 break;
7741 default:
7742 usage_ref();
7743 /* NOTREACHED */
7747 argc -= optind;
7748 argv += optind;
7750 if (argc > 1)
7751 usage_ref();
7753 error = got_repo_pack_fds_open(&pack_fds);
7754 if (error != NULL)
7755 goto done;
7757 if (repo_path == NULL) {
7758 cwd = getcwd(NULL, 0);
7759 if (cwd == NULL)
7760 return got_error_from_errno("getcwd");
7761 error = got_worktree_open(&worktree, cwd);
7762 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7763 goto done;
7764 if (worktree)
7765 repo_path =
7766 strdup(got_worktree_get_repo_path(worktree));
7767 else
7768 repo_path = strdup(cwd);
7769 if (repo_path == NULL) {
7770 error = got_error_from_errno("strdup");
7771 goto done;
7775 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7776 if (error != NULL)
7777 goto done;
7779 init_curses();
7781 error = apply_unveil(got_repo_get_path(repo), NULL);
7782 if (error)
7783 goto done;
7785 error = tog_load_refs(repo, 0);
7786 if (error)
7787 goto done;
7789 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7790 if (view == NULL) {
7791 error = got_error_from_errno("view_open");
7792 goto done;
7795 error = open_ref_view(view, repo);
7796 if (error)
7797 goto done;
7799 if (worktree) {
7800 /* Release work tree lock. */
7801 got_worktree_close(worktree);
7802 worktree = NULL;
7804 error = view_loop(view);
7805 done:
7806 free(repo_path);
7807 free(cwd);
7808 if (repo) {
7809 const struct got_error *close_err = got_repo_close(repo);
7810 if (close_err)
7811 error = close_err;
7813 if (pack_fds) {
7814 const struct got_error *pack_err =
7815 got_repo_pack_fds_close(pack_fds);
7816 if (error == NULL)
7817 error = pack_err;
7819 tog_free_refs();
7820 return error;
7823 static const struct got_error *
7824 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7825 enum tog_view_type request, int y, int x)
7827 const struct got_error *err = NULL;
7829 *new_view = NULL;
7831 switch (request) {
7832 case TOG_VIEW_DIFF:
7833 if (view->type == TOG_VIEW_LOG) {
7834 struct tog_log_view_state *s = &view->state.log;
7836 err = open_diff_view_for_commit(new_view, y, x,
7837 s->selected_entry->commit, s->selected_entry->id,
7838 view, s->repo);
7839 } else
7840 return got_error_msg(GOT_ERR_NOT_IMPL,
7841 "parent/child view pair not supported");
7842 break;
7843 case TOG_VIEW_BLAME:
7844 if (view->type == TOG_VIEW_TREE) {
7845 struct tog_tree_view_state *s = &view->state.tree;
7847 err = blame_tree_entry(new_view, y, x,
7848 s->selected_entry, &s->parents, s->commit_id,
7849 s->repo);
7850 } else
7851 return got_error_msg(GOT_ERR_NOT_IMPL,
7852 "parent/child view pair not supported");
7853 break;
7854 case TOG_VIEW_LOG:
7855 if (view->type == TOG_VIEW_BLAME)
7856 err = log_annotated_line(new_view, y, x,
7857 view->state.blame.repo, view->state.blame.id_to_log);
7858 else if (view->type == TOG_VIEW_TREE)
7859 err = log_selected_tree_entry(new_view, y, x,
7860 &view->state.tree);
7861 else if (view->type == TOG_VIEW_REF)
7862 err = log_ref_entry(new_view, y, x,
7863 view->state.ref.selected_entry,
7864 view->state.ref.repo);
7865 else
7866 return got_error_msg(GOT_ERR_NOT_IMPL,
7867 "parent/child view pair not supported");
7868 break;
7869 case TOG_VIEW_TREE:
7870 if (view->type == TOG_VIEW_LOG)
7871 err = browse_commit_tree(new_view, y, x,
7872 view->state.log.selected_entry,
7873 view->state.log.in_repo_path,
7874 view->state.log.head_ref_name,
7875 view->state.log.repo);
7876 else if (view->type == TOG_VIEW_REF)
7877 err = browse_ref_tree(new_view, y, x,
7878 view->state.ref.selected_entry,
7879 view->state.ref.repo);
7880 else
7881 return got_error_msg(GOT_ERR_NOT_IMPL,
7882 "parent/child view pair not supported");
7883 break;
7884 case TOG_VIEW_REF:
7885 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7886 if (*new_view == NULL)
7887 return got_error_from_errno("view_open");
7888 if (view->type == TOG_VIEW_LOG)
7889 err = open_ref_view(*new_view, view->state.log.repo);
7890 else if (view->type == TOG_VIEW_TREE)
7891 err = open_ref_view(*new_view, view->state.tree.repo);
7892 else
7893 err = got_error_msg(GOT_ERR_NOT_IMPL,
7894 "parent/child view pair not supported");
7895 if (err)
7896 view_close(*new_view);
7897 break;
7898 default:
7899 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7902 return err;
7906 * If view was scrolled down to move the selected line into view when opening a
7907 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7909 static void
7910 offset_selection_up(struct tog_view *view)
7912 switch (view->type) {
7913 case TOG_VIEW_BLAME: {
7914 struct tog_blame_view_state *s = &view->state.blame;
7915 if (s->first_displayed_line == 1) {
7916 s->selected_line = MAX(s->selected_line - view->offset,
7917 1);
7918 break;
7920 if (s->first_displayed_line > view->offset)
7921 s->first_displayed_line -= view->offset;
7922 else
7923 s->first_displayed_line = 1;
7924 s->selected_line += view->offset;
7925 break;
7927 case TOG_VIEW_LOG:
7928 log_scroll_up(&view->state.log, view->offset);
7929 view->state.log.selected += view->offset;
7930 break;
7931 case TOG_VIEW_REF:
7932 ref_scroll_up(&view->state.ref, view->offset);
7933 view->state.ref.selected += view->offset;
7934 break;
7935 case TOG_VIEW_TREE:
7936 tree_scroll_up(&view->state.tree, view->offset);
7937 view->state.tree.selected += view->offset;
7938 break;
7939 default:
7940 break;
7943 view->offset = 0;
7947 * If the selected line is in the section of screen covered by the bottom split,
7948 * scroll down offset lines to move it into view and index its new position.
7950 static const struct got_error *
7951 offset_selection_down(struct tog_view *view)
7953 const struct got_error *err = NULL;
7954 const struct got_error *(*scrolld)(struct tog_view *, int);
7955 int *selected = NULL;
7956 int header, offset;
7958 switch (view->type) {
7959 case TOG_VIEW_BLAME: {
7960 struct tog_blame_view_state *s = &view->state.blame;
7961 header = 3;
7962 scrolld = NULL;
7963 if (s->selected_line > view->nlines - header) {
7964 offset = abs(view->nlines - s->selected_line - header);
7965 s->first_displayed_line += offset;
7966 s->selected_line -= offset;
7967 view->offset = offset;
7969 break;
7971 case TOG_VIEW_LOG: {
7972 struct tog_log_view_state *s = &view->state.log;
7973 scrolld = &log_scroll_down;
7974 header = view_is_parent_view(view) ? 3 : 2;
7975 selected = &s->selected;
7976 break;
7978 case TOG_VIEW_REF: {
7979 struct tog_ref_view_state *s = &view->state.ref;
7980 scrolld = &ref_scroll_down;
7981 header = 3;
7982 selected = &s->selected;
7983 break;
7985 case TOG_VIEW_TREE: {
7986 struct tog_tree_view_state *s = &view->state.tree;
7987 scrolld = &tree_scroll_down;
7988 header = 5;
7989 selected = &s->selected;
7990 break;
7992 default:
7993 selected = NULL;
7994 scrolld = NULL;
7995 header = 0;
7996 break;
7999 if (selected && *selected > view->nlines - header) {
8000 offset = abs(view->nlines - *selected - header);
8001 view->offset = offset;
8002 if (scrolld && offset) {
8003 err = scrolld(view, offset);
8004 *selected -= offset;
8008 return err;
8011 static void
8012 list_commands(FILE *fp)
8014 size_t i;
8016 fprintf(fp, "commands:");
8017 for (i = 0; i < nitems(tog_commands); i++) {
8018 const struct tog_cmd *cmd = &tog_commands[i];
8019 fprintf(fp, " %s", cmd->name);
8021 fputc('\n', fp);
8024 __dead static void
8025 usage(int hflag, int status)
8027 FILE *fp = (status == 0) ? stdout : stderr;
8029 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8030 getprogname());
8031 if (hflag) {
8032 fprintf(fp, "lazy usage: %s path\n", getprogname());
8033 list_commands(fp);
8035 exit(status);
8038 static char **
8039 make_argv(int argc, ...)
8041 va_list ap;
8042 char **argv;
8043 int i;
8045 va_start(ap, argc);
8047 argv = calloc(argc, sizeof(char *));
8048 if (argv == NULL)
8049 err(1, "calloc");
8050 for (i = 0; i < argc; i++) {
8051 argv[i] = strdup(va_arg(ap, char *));
8052 if (argv[i] == NULL)
8053 err(1, "strdup");
8056 va_end(ap);
8057 return argv;
8061 * Try to convert 'tog path' into a 'tog log path' command.
8062 * The user could simply have mistyped the command rather than knowingly
8063 * provided a path. So check whether argv[0] can in fact be resolved
8064 * to a path in the HEAD commit and print a special error if not.
8065 * This hack is for mpi@ <3
8067 static const struct got_error *
8068 tog_log_with_path(int argc, char *argv[])
8070 const struct got_error *error = NULL, *close_err;
8071 const struct tog_cmd *cmd = NULL;
8072 struct got_repository *repo = NULL;
8073 struct got_worktree *worktree = NULL;
8074 struct got_object_id *commit_id = NULL, *id = NULL;
8075 struct got_commit_object *commit = NULL;
8076 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8077 char *commit_id_str = NULL, **cmd_argv = NULL;
8078 int *pack_fds = NULL;
8080 cwd = getcwd(NULL, 0);
8081 if (cwd == NULL)
8082 return got_error_from_errno("getcwd");
8084 error = got_repo_pack_fds_open(&pack_fds);
8085 if (error != NULL)
8086 goto done;
8088 error = got_worktree_open(&worktree, cwd);
8089 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8090 goto done;
8092 if (worktree)
8093 repo_path = strdup(got_worktree_get_repo_path(worktree));
8094 else
8095 repo_path = strdup(cwd);
8096 if (repo_path == NULL) {
8097 error = got_error_from_errno("strdup");
8098 goto done;
8101 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8102 if (error != NULL)
8103 goto done;
8105 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8106 repo, worktree);
8107 if (error)
8108 goto done;
8110 error = tog_load_refs(repo, 0);
8111 if (error)
8112 goto done;
8113 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8114 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8115 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8116 if (error)
8117 goto done;
8119 if (worktree) {
8120 got_worktree_close(worktree);
8121 worktree = NULL;
8124 error = got_object_open_as_commit(&commit, repo, commit_id);
8125 if (error)
8126 goto done;
8128 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8129 if (error) {
8130 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8131 goto done;
8132 fprintf(stderr, "%s: '%s' is no known command or path\n",
8133 getprogname(), argv[0]);
8134 usage(1, 1);
8135 /* not reached */
8138 error = got_object_id_str(&commit_id_str, commit_id);
8139 if (error)
8140 goto done;
8142 cmd = &tog_commands[0]; /* log */
8143 argc = 4;
8144 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8145 error = cmd->cmd_main(argc, cmd_argv);
8146 done:
8147 if (repo) {
8148 close_err = got_repo_close(repo);
8149 if (error == NULL)
8150 error = close_err;
8152 if (commit)
8153 got_object_commit_close(commit);
8154 if (worktree)
8155 got_worktree_close(worktree);
8156 if (pack_fds) {
8157 const struct got_error *pack_err =
8158 got_repo_pack_fds_close(pack_fds);
8159 if (error == NULL)
8160 error = pack_err;
8162 free(id);
8163 free(commit_id_str);
8164 free(commit_id);
8165 free(cwd);
8166 free(repo_path);
8167 free(in_repo_path);
8168 if (cmd_argv) {
8169 int i;
8170 for (i = 0; i < argc; i++)
8171 free(cmd_argv[i]);
8172 free(cmd_argv);
8174 tog_free_refs();
8175 return error;
8178 int
8179 main(int argc, char *argv[])
8181 const struct got_error *error = NULL;
8182 const struct tog_cmd *cmd = NULL;
8183 int ch, hflag = 0, Vflag = 0;
8184 char **cmd_argv = NULL;
8185 static const struct option longopts[] = {
8186 { "version", no_argument, NULL, 'V' },
8187 { NULL, 0, NULL, 0}
8189 char *diff_algo_str = NULL;
8191 setlocale(LC_CTYPE, "");
8193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8194 switch (ch) {
8195 case 'h':
8196 hflag = 1;
8197 break;
8198 case 'V':
8199 Vflag = 1;
8200 break;
8201 default:
8202 usage(hflag, 1);
8203 /* NOTREACHED */
8207 argc -= optind;
8208 argv += optind;
8209 optind = 1;
8210 optreset = 1;
8212 if (Vflag) {
8213 got_version_print_str();
8214 return 0;
8217 #ifndef PROFILE
8218 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8219 NULL) == -1)
8220 err(1, "pledge");
8221 #endif
8223 if (argc == 0) {
8224 if (hflag)
8225 usage(hflag, 0);
8226 /* Build an argument vector which runs a default command. */
8227 cmd = &tog_commands[0];
8228 argc = 1;
8229 cmd_argv = make_argv(argc, cmd->name);
8230 } else {
8231 size_t i;
8233 /* Did the user specify a command? */
8234 for (i = 0; i < nitems(tog_commands); i++) {
8235 if (strncmp(tog_commands[i].name, argv[0],
8236 strlen(argv[0])) == 0) {
8237 cmd = &tog_commands[i];
8238 break;
8243 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8244 if (diff_algo_str) {
8245 if (strcasecmp(diff_algo_str, "patience") == 0)
8246 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8247 if (strcasecmp(diff_algo_str, "myers") == 0)
8248 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8251 if (cmd == NULL) {
8252 if (argc != 1)
8253 usage(0, 1);
8254 /* No command specified; try log with a path */
8255 error = tog_log_with_path(argc, argv);
8256 } else {
8257 if (hflag)
8258 cmd->cmd_usage();
8259 else
8260 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8263 endwin();
8264 putchar('\n');
8265 if (cmd_argv) {
8266 int i;
8267 for (i = 0; i < argc; i++)
8268 free(cmd_argv[i]);
8269 free(cmd_argv);
8272 if (error && error->code != GOT_ERR_CANCELLED)
8273 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8274 return 0;