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 err = offset_selection_down(v->child);
1258 } else {
1259 offset_selection_up(v);
1260 offset_selection_up(v->child);
1262 if (v->resize)
1263 err = v->resize(v, 0);
1264 else if (v->child->resize)
1265 err = v->child->resize(v->child, 0);
1267 return err;
1271 * Compute view->count from numeric input. Assign total to view->count and
1272 * return first non-numeric key entered.
1274 static int
1275 get_compound_key(struct tog_view *view, int c)
1277 struct tog_view *v = view;
1278 int x, n = 0;
1280 if (view_is_hsplit_top(view))
1281 v = view->child;
1282 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1283 v = view->parent;
1285 view->count = 0;
1286 cbreak(); /* block for input */
1287 wmove(v->window, v->nlines - 1, 0);
1288 wclrtoeol(v->window);
1289 waddch(v->window, ':');
1291 do {
1292 x = getcurx(v->window);
1293 if (x != ERR && x < view->ncols) {
1294 waddch(v->window, c);
1295 wrefresh(v->window);
1299 * Don't overflow. Max valid request should be the greatest
1300 * between the longest and total lines; cap at 10 million.
1302 if (n >= 9999999)
1303 n = 9999999;
1304 else
1305 n = n * 10 + (c - '0');
1306 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1308 /* Massage excessive or inapplicable values at the input handler. */
1309 view->count = n;
1311 return c;
1314 static const struct got_error *
1315 view_input(struct tog_view **new, int *done, struct tog_view *view,
1316 struct tog_view_list_head *views)
1318 const struct got_error *err = NULL;
1319 struct tog_view *v;
1320 int ch, errcode;
1322 *new = NULL;
1324 /* Clear "no matches" indicator. */
1325 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1326 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1327 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1328 view->count = 0;
1331 if (view->searching && !view->search_next_done) {
1332 errcode = pthread_mutex_unlock(&tog_mutex);
1333 if (errcode)
1334 return got_error_set_errno(errcode,
1335 "pthread_mutex_unlock");
1336 sched_yield();
1337 errcode = pthread_mutex_lock(&tog_mutex);
1338 if (errcode)
1339 return got_error_set_errno(errcode,
1340 "pthread_mutex_lock");
1341 view->search_next(view);
1342 return NULL;
1345 nodelay(view->window, FALSE);
1346 /* Allow threads to make progress while we are waiting for input. */
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1348 if (errcode)
1349 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1350 /* If we have an unfinished count, let C-g or backspace abort. */
1351 if (view->count && --view->count) {
1352 cbreak();
1353 nodelay(view->window, TRUE);
1354 ch = wgetch(view->window);
1355 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1356 view->count = 0;
1357 else
1358 ch = view->ch;
1359 } else {
1360 ch = wgetch(view->window);
1361 if (ch >= '1' && ch <= '9')
1362 view->ch = ch = get_compound_key(view, ch);
1364 errcode = pthread_mutex_lock(&tog_mutex);
1365 if (errcode)
1366 return got_error_set_errno(errcode, "pthread_mutex_lock");
1367 nodelay(view->window, TRUE);
1369 if (tog_sigwinch_received || tog_sigcont_received) {
1370 tog_resizeterm();
1371 tog_sigwinch_received = 0;
1372 tog_sigcont_received = 0;
1373 TAILQ_FOREACH(v, views, entry) {
1374 err = view_resize(v);
1375 if (err)
1376 return err;
1377 err = v->input(new, v, KEY_RESIZE);
1378 if (err)
1379 return err;
1380 if (v->child) {
1381 err = view_resize(v->child);
1382 if (err)
1383 return err;
1384 err = v->child->input(new, v->child,
1385 KEY_RESIZE);
1386 if (err)
1387 return err;
1388 if (v->child->resized_x || v->child->resized_y) {
1389 err = view_resize_split(v, 0);
1390 if (err)
1391 return err;
1397 switch (ch) {
1398 case '\t':
1399 view->count = 0;
1400 if (view->child) {
1401 view->focussed = 0;
1402 view->child->focussed = 1;
1403 view->focus_child = 1;
1404 } else if (view->parent) {
1405 view->focussed = 0;
1406 view->parent->focussed = 1;
1407 view->parent->focus_child = 0;
1408 if (!view_is_splitscreen(view)) {
1409 if (view->parent->resize) {
1410 err = view->parent->resize(view->parent,
1411 0);
1412 if (err)
1413 return err;
1415 offset_selection_up(view->parent);
1416 err = view_fullscreen(view->parent);
1417 if (err)
1418 return err;
1421 break;
1422 case 'q':
1423 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1424 if (view->parent->resize) {
1425 /* might need more commits to fill fullscreen */
1426 err = view->parent->resize(view->parent, 0);
1427 if (err)
1428 break;
1430 offset_selection_up(view->parent);
1432 err = view->input(new, view, ch);
1433 view->dying = 1;
1434 break;
1435 case 'Q':
1436 *done = 1;
1437 break;
1438 case 'F':
1439 view->count = 0;
1440 if (view_is_parent_view(view)) {
1441 if (view->child == NULL)
1442 break;
1443 if (view_is_splitscreen(view->child)) {
1444 view->focussed = 0;
1445 view->child->focussed = 1;
1446 err = view_fullscreen(view->child);
1447 } else {
1448 err = view_splitscreen(view->child);
1449 if (!err)
1450 err = view_resize_split(view, 0);
1452 if (err)
1453 break;
1454 err = view->child->input(new, view->child,
1455 KEY_RESIZE);
1456 } else {
1457 if (view_is_splitscreen(view)) {
1458 view->parent->focussed = 0;
1459 view->focussed = 1;
1460 err = view_fullscreen(view);
1461 } else {
1462 err = view_splitscreen(view);
1463 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1464 err = view_resize(view->parent);
1465 if (!err)
1466 err = view_resize_split(view, 0);
1468 if (err)
1469 break;
1470 err = view->input(new, view, KEY_RESIZE);
1472 if (err)
1473 break;
1474 if (view->resize) {
1475 err = view->resize(view, 0);
1476 if (err)
1477 break;
1479 if (view->parent)
1480 err = offset_selection_down(view->parent);
1481 if (!err)
1482 err = offset_selection_down(view);
1483 break;
1484 case 'S':
1485 view->count = 0;
1486 err = switch_split(view);
1487 break;
1488 case '-':
1489 err = view_resize_split(view, -1);
1490 break;
1491 case '+':
1492 err = view_resize_split(view, 1);
1493 break;
1494 case KEY_RESIZE:
1495 break;
1496 case '/':
1497 view->count = 0;
1498 if (view->search_start)
1499 view_search_start(view);
1500 else
1501 err = view->input(new, view, ch);
1502 break;
1503 case 'N':
1504 case 'n':
1505 if (view->search_started && view->search_next) {
1506 view->searching = (ch == 'n' ?
1507 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1508 view->search_next_done = 0;
1509 view->search_next(view);
1510 } else
1511 err = view->input(new, view, ch);
1512 break;
1513 case 'A':
1514 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1515 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1516 else
1517 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1518 TAILQ_FOREACH(v, views, entry) {
1519 if (v->reset) {
1520 err = v->reset(v);
1521 if (err)
1522 return err;
1524 if (v->child && v->child->reset) {
1525 err = v->child->reset(v->child);
1526 if (err)
1527 return err;
1530 break;
1531 default:
1532 err = view->input(new, view, ch);
1533 break;
1536 return err;
1539 static int
1540 view_needs_focus_indication(struct tog_view *view)
1542 if (view_is_parent_view(view)) {
1543 if (view->child == NULL || view->child->focussed)
1544 return 0;
1545 if (!view_is_splitscreen(view->child))
1546 return 0;
1547 } else if (!view_is_splitscreen(view))
1548 return 0;
1550 return view->focussed;
1553 static const struct got_error *
1554 view_loop(struct tog_view *view)
1556 const struct got_error *err = NULL;
1557 struct tog_view_list_head views;
1558 struct tog_view *new_view;
1559 char *mode;
1560 int fast_refresh = 10;
1561 int done = 0, errcode;
1563 mode = getenv("TOG_VIEW_SPLIT_MODE");
1564 if (!mode || !(*mode == 'h' || *mode == 'H'))
1565 view->mode = TOG_VIEW_SPLIT_VERT;
1566 else
1567 view->mode = TOG_VIEW_SPLIT_HRZN;
1569 errcode = pthread_mutex_lock(&tog_mutex);
1570 if (errcode)
1571 return got_error_set_errno(errcode, "pthread_mutex_lock");
1573 TAILQ_INIT(&views);
1574 TAILQ_INSERT_HEAD(&views, view, entry);
1576 view->focussed = 1;
1577 err = view->show(view);
1578 if (err)
1579 return err;
1580 update_panels();
1581 doupdate();
1582 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1583 !tog_fatal_signal_received()) {
1584 /* Refresh fast during initialization, then become slower. */
1585 if (fast_refresh && fast_refresh-- == 0)
1586 halfdelay(10); /* switch to once per second */
1588 err = view_input(&new_view, &done, view, &views);
1589 if (err)
1590 break;
1591 if (view->dying) {
1592 struct tog_view *v, *prev = NULL;
1594 if (view_is_parent_view(view))
1595 prev = TAILQ_PREV(view, tog_view_list_head,
1596 entry);
1597 else if (view->parent)
1598 prev = view->parent;
1600 if (view->parent) {
1601 view->parent->child = NULL;
1602 view->parent->focus_child = 0;
1603 /* Restore fullscreen line height. */
1604 view->parent->nlines = view->parent->lines;
1605 err = view_resize(view->parent);
1606 if (err)
1607 break;
1608 /* Make resized splits persist. */
1609 view_transfer_size(view->parent, view);
1610 } else
1611 TAILQ_REMOVE(&views, view, entry);
1613 err = view_close(view);
1614 if (err)
1615 goto done;
1617 view = NULL;
1618 TAILQ_FOREACH(v, &views, entry) {
1619 if (v->focussed)
1620 break;
1622 if (view == NULL && new_view == NULL) {
1623 /* No view has focus. Try to pick one. */
1624 if (prev)
1625 view = prev;
1626 else if (!TAILQ_EMPTY(&views)) {
1627 view = TAILQ_LAST(&views,
1628 tog_view_list_head);
1630 if (view) {
1631 if (view->focus_child) {
1632 view->child->focussed = 1;
1633 view = view->child;
1634 } else
1635 view->focussed = 1;
1639 if (new_view) {
1640 struct tog_view *v, *t;
1641 /* Only allow one parent view per type. */
1642 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1643 if (v->type != new_view->type)
1644 continue;
1645 TAILQ_REMOVE(&views, v, entry);
1646 err = view_close(v);
1647 if (err)
1648 goto done;
1649 break;
1651 TAILQ_INSERT_TAIL(&views, new_view, entry);
1652 view = new_view;
1654 if (view) {
1655 if (view_is_parent_view(view)) {
1656 if (view->child && view->child->focussed)
1657 view = view->child;
1658 } else {
1659 if (view->parent && view->parent->focussed)
1660 view = view->parent;
1662 show_panel(view->panel);
1663 if (view->child && view_is_splitscreen(view->child))
1664 show_panel(view->child->panel);
1665 if (view->parent && view_is_splitscreen(view)) {
1666 err = view->parent->show(view->parent);
1667 if (err)
1668 goto done;
1670 err = view->show(view);
1671 if (err)
1672 goto done;
1673 if (view->child) {
1674 err = view->child->show(view->child);
1675 if (err)
1676 goto done;
1678 update_panels();
1679 doupdate();
1682 done:
1683 while (!TAILQ_EMPTY(&views)) {
1684 const struct got_error *close_err;
1685 view = TAILQ_FIRST(&views);
1686 TAILQ_REMOVE(&views, view, entry);
1687 close_err = view_close(view);
1688 if (close_err && err == NULL)
1689 err = close_err;
1692 errcode = pthread_mutex_unlock(&tog_mutex);
1693 if (errcode && err == NULL)
1694 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1696 return err;
1699 __dead static void
1700 usage_log(void)
1702 endwin();
1703 fprintf(stderr,
1704 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1705 getprogname());
1706 exit(1);
1709 /* Create newly allocated wide-character string equivalent to a byte string. */
1710 static const struct got_error *
1711 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1713 char *vis = NULL;
1714 const struct got_error *err = NULL;
1716 *ws = NULL;
1717 *wlen = mbstowcs(NULL, s, 0);
1718 if (*wlen == (size_t)-1) {
1719 int vislen;
1720 if (errno != EILSEQ)
1721 return got_error_from_errno("mbstowcs");
1723 /* byte string invalid in current encoding; try to "fix" it */
1724 err = got_mbsavis(&vis, &vislen, s);
1725 if (err)
1726 return err;
1727 *wlen = mbstowcs(NULL, vis, 0);
1728 if (*wlen == (size_t)-1) {
1729 err = got_error_from_errno("mbstowcs"); /* give up */
1730 goto done;
1734 *ws = calloc(*wlen + 1, sizeof(**ws));
1735 if (*ws == NULL) {
1736 err = got_error_from_errno("calloc");
1737 goto done;
1740 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1741 err = got_error_from_errno("mbstowcs");
1742 done:
1743 free(vis);
1744 if (err) {
1745 free(*ws);
1746 *ws = NULL;
1747 *wlen = 0;
1749 return err;
1752 static const struct got_error *
1753 expand_tab(char **ptr, const char *src)
1755 char *dst;
1756 size_t len, n, idx = 0, sz = 0;
1758 *ptr = NULL;
1759 n = len = strlen(src);
1760 dst = malloc(n + 1);
1761 if (dst == NULL)
1762 return got_error_from_errno("malloc");
1764 while (idx < len && src[idx]) {
1765 const char c = src[idx];
1767 if (c == '\t') {
1768 size_t nb = TABSIZE - sz % TABSIZE;
1769 char *p;
1771 p = realloc(dst, n + nb);
1772 if (p == NULL) {
1773 free(dst);
1774 return got_error_from_errno("realloc");
1777 dst = p;
1778 n += nb;
1779 memset(dst + sz, ' ', nb);
1780 sz += nb;
1781 } else
1782 dst[sz++] = src[idx];
1783 ++idx;
1786 dst[sz] = '\0';
1787 *ptr = dst;
1788 return NULL;
1792 * Advance at most n columns from wline starting at offset off.
1793 * Return the index to the first character after the span operation.
1794 * Return the combined column width of all spanned wide character in
1795 * *rcol.
1797 static int
1798 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1800 int width, i, cols = 0;
1802 if (n == 0) {
1803 *rcol = cols;
1804 return off;
1807 for (i = off; wline[i] != L'\0'; ++i) {
1808 if (wline[i] == L'\t')
1809 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1810 else
1811 width = wcwidth(wline[i]);
1813 if (width == -1) {
1814 width = 1;
1815 wline[i] = L'.';
1818 if (cols + width > n)
1819 break;
1820 cols += width;
1823 *rcol = cols;
1824 return i;
1828 * Format a line for display, ensuring that it won't overflow a width limit.
1829 * With scrolling, the width returned refers to the scrolled version of the
1830 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1832 static const struct got_error *
1833 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1834 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1836 const struct got_error *err = NULL;
1837 int cols;
1838 wchar_t *wline = NULL;
1839 char *exstr = NULL;
1840 size_t wlen;
1841 int i, scrollx;
1843 *wlinep = NULL;
1844 *widthp = 0;
1846 if (expand) {
1847 err = expand_tab(&exstr, line);
1848 if (err)
1849 return err;
1852 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1853 free(exstr);
1854 if (err)
1855 return err;
1857 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1859 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1860 wline[wlen - 1] = L'\0';
1861 wlen--;
1863 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1864 wline[wlen - 1] = L'\0';
1865 wlen--;
1868 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1869 wline[i] = L'\0';
1871 if (widthp)
1872 *widthp = cols;
1873 if (scrollxp)
1874 *scrollxp = scrollx;
1875 if (err)
1876 free(wline);
1877 else
1878 *wlinep = wline;
1879 return err;
1882 static const struct got_error*
1883 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1884 struct got_object_id *id, struct got_repository *repo)
1886 static const struct got_error *err = NULL;
1887 struct got_reflist_entry *re;
1888 char *s;
1889 const char *name;
1891 *refs_str = NULL;
1893 TAILQ_FOREACH(re, refs, entry) {
1894 struct got_tag_object *tag = NULL;
1895 struct got_object_id *ref_id;
1896 int cmp;
1898 name = got_ref_get_name(re->ref);
1899 if (strcmp(name, GOT_REF_HEAD) == 0)
1900 continue;
1901 if (strncmp(name, "refs/", 5) == 0)
1902 name += 5;
1903 if (strncmp(name, "got/", 4) == 0 &&
1904 strncmp(name, "got/backup/", 11) != 0)
1905 continue;
1906 if (strncmp(name, "heads/", 6) == 0)
1907 name += 6;
1908 if (strncmp(name, "remotes/", 8) == 0) {
1909 name += 8;
1910 s = strstr(name, "/" GOT_REF_HEAD);
1911 if (s != NULL && s[strlen(s)] == '\0')
1912 continue;
1914 err = got_ref_resolve(&ref_id, repo, re->ref);
1915 if (err)
1916 break;
1917 if (strncmp(name, "tags/", 5) == 0) {
1918 err = got_object_open_as_tag(&tag, repo, ref_id);
1919 if (err) {
1920 if (err->code != GOT_ERR_OBJ_TYPE) {
1921 free(ref_id);
1922 break;
1924 /* Ref points at something other than a tag. */
1925 err = NULL;
1926 tag = NULL;
1929 cmp = got_object_id_cmp(tag ?
1930 got_object_tag_get_object_id(tag) : ref_id, id);
1931 free(ref_id);
1932 if (tag)
1933 got_object_tag_close(tag);
1934 if (cmp != 0)
1935 continue;
1936 s = *refs_str;
1937 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1938 s ? ", " : "", name) == -1) {
1939 err = got_error_from_errno("asprintf");
1940 free(s);
1941 *refs_str = NULL;
1942 break;
1944 free(s);
1947 return err;
1950 static const struct got_error *
1951 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1952 int col_tab_align)
1954 char *smallerthan;
1956 smallerthan = strchr(author, '<');
1957 if (smallerthan && smallerthan[1] != '\0')
1958 author = smallerthan + 1;
1959 author[strcspn(author, "@>")] = '\0';
1960 return format_line(wauthor, author_width, NULL, author, 0, limit,
1961 col_tab_align, 0);
1964 static const struct got_error *
1965 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1966 struct got_object_id *id, const size_t date_display_cols,
1967 int author_display_cols)
1969 struct tog_log_view_state *s = &view->state.log;
1970 const struct got_error *err = NULL;
1971 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1972 char *logmsg0 = NULL, *logmsg = NULL;
1973 char *author = NULL;
1974 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1975 int author_width, logmsg_width;
1976 char *newline, *line = NULL;
1977 int col, limit, scrollx;
1978 const int avail = view->ncols;
1979 struct tm tm;
1980 time_t committer_time;
1981 struct tog_color *tc;
1983 committer_time = got_object_commit_get_committer_time(commit);
1984 if (gmtime_r(&committer_time, &tm) == NULL)
1985 return got_error_from_errno("gmtime_r");
1986 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1987 return got_error(GOT_ERR_NO_SPACE);
1989 if (avail <= date_display_cols)
1990 limit = MIN(sizeof(datebuf) - 1, avail);
1991 else
1992 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1993 tc = get_color(&s->colors, TOG_COLOR_DATE);
1994 if (tc)
1995 wattr_on(view->window,
1996 COLOR_PAIR(tc->colorpair), NULL);
1997 waddnstr(view->window, datebuf, limit);
1998 if (tc)
1999 wattr_off(view->window,
2000 COLOR_PAIR(tc->colorpair), NULL);
2001 col = limit;
2002 if (col > avail)
2003 goto done;
2005 if (avail >= 120) {
2006 char *id_str;
2007 err = got_object_id_str(&id_str, id);
2008 if (err)
2009 goto done;
2010 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2011 if (tc)
2012 wattr_on(view->window,
2013 COLOR_PAIR(tc->colorpair), NULL);
2014 wprintw(view->window, "%.8s ", id_str);
2015 if (tc)
2016 wattr_off(view->window,
2017 COLOR_PAIR(tc->colorpair), NULL);
2018 free(id_str);
2019 col += 9;
2020 if (col > avail)
2021 goto done;
2024 if (s->use_committer)
2025 author = strdup(got_object_commit_get_committer(commit));
2026 else
2027 author = strdup(got_object_commit_get_author(commit));
2028 if (author == NULL) {
2029 err = got_error_from_errno("strdup");
2030 goto done;
2032 err = format_author(&wauthor, &author_width, author, avail - col, col);
2033 if (err)
2034 goto done;
2035 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2036 if (tc)
2037 wattr_on(view->window,
2038 COLOR_PAIR(tc->colorpair), NULL);
2039 waddwstr(view->window, wauthor);
2040 if (tc)
2041 wattr_off(view->window,
2042 COLOR_PAIR(tc->colorpair), NULL);
2043 col += author_width;
2044 while (col < avail && author_width < author_display_cols + 2) {
2045 waddch(view->window, ' ');
2046 col++;
2047 author_width++;
2049 if (col > avail)
2050 goto done;
2052 err = got_object_commit_get_logmsg(&logmsg0, commit);
2053 if (err)
2054 goto done;
2055 logmsg = logmsg0;
2056 while (*logmsg == '\n')
2057 logmsg++;
2058 newline = strchr(logmsg, '\n');
2059 if (newline)
2060 *newline = '\0';
2061 limit = avail - col;
2062 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2063 limit--; /* for the border */
2064 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2065 limit, col, 1);
2066 if (err)
2067 goto done;
2068 waddwstr(view->window, &wlogmsg[scrollx]);
2069 col += MAX(logmsg_width, 0);
2070 while (col < avail) {
2071 waddch(view->window, ' ');
2072 col++;
2074 done:
2075 free(logmsg0);
2076 free(wlogmsg);
2077 free(author);
2078 free(wauthor);
2079 free(line);
2080 return err;
2083 static struct commit_queue_entry *
2084 alloc_commit_queue_entry(struct got_commit_object *commit,
2085 struct got_object_id *id)
2087 struct commit_queue_entry *entry;
2089 entry = calloc(1, sizeof(*entry));
2090 if (entry == NULL)
2091 return NULL;
2093 entry->id = id;
2094 entry->commit = commit;
2095 return entry;
2098 static void
2099 pop_commit(struct commit_queue *commits)
2101 struct commit_queue_entry *entry;
2103 entry = TAILQ_FIRST(&commits->head);
2104 TAILQ_REMOVE(&commits->head, entry, entry);
2105 got_object_commit_close(entry->commit);
2106 commits->ncommits--;
2107 /* Don't free entry->id! It is owned by the commit graph. */
2108 free(entry);
2111 static void
2112 free_commits(struct commit_queue *commits)
2114 while (!TAILQ_EMPTY(&commits->head))
2115 pop_commit(commits);
2118 static const struct got_error *
2119 match_commit(int *have_match, struct got_object_id *id,
2120 struct got_commit_object *commit, regex_t *regex)
2122 const struct got_error *err = NULL;
2123 regmatch_t regmatch;
2124 char *id_str = NULL, *logmsg = NULL;
2126 *have_match = 0;
2128 err = got_object_id_str(&id_str, id);
2129 if (err)
2130 return err;
2132 err = got_object_commit_get_logmsg(&logmsg, commit);
2133 if (err)
2134 goto done;
2136 if (regexec(regex, got_object_commit_get_author(commit), 1,
2137 &regmatch, 0) == 0 ||
2138 regexec(regex, got_object_commit_get_committer(commit), 1,
2139 &regmatch, 0) == 0 ||
2140 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2141 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2142 *have_match = 1;
2143 done:
2144 free(id_str);
2145 free(logmsg);
2146 return err;
2149 static const struct got_error *
2150 queue_commits(struct tog_log_thread_args *a)
2152 const struct got_error *err = NULL;
2155 * We keep all commits open throughout the lifetime of the log
2156 * view in order to avoid having to re-fetch commits from disk
2157 * while updating the display.
2159 do {
2160 struct got_object_id *id;
2161 struct got_commit_object *commit;
2162 struct commit_queue_entry *entry;
2163 int errcode;
2165 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2166 NULL, NULL);
2167 if (err || id == NULL)
2168 break;
2170 err = got_object_open_as_commit(&commit, a->repo, id);
2171 if (err)
2172 break;
2173 entry = alloc_commit_queue_entry(commit, id);
2174 if (entry == NULL) {
2175 err = got_error_from_errno("alloc_commit_queue_entry");
2176 break;
2179 errcode = pthread_mutex_lock(&tog_mutex);
2180 if (errcode) {
2181 err = got_error_set_errno(errcode,
2182 "pthread_mutex_lock");
2183 break;
2186 entry->idx = a->commits->ncommits;
2187 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2188 a->commits->ncommits++;
2190 if (*a->searching == TOG_SEARCH_FORWARD &&
2191 !*a->search_next_done) {
2192 int have_match;
2193 err = match_commit(&have_match, id, commit, a->regex);
2194 if (err)
2195 break;
2196 if (have_match)
2197 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2200 errcode = pthread_mutex_unlock(&tog_mutex);
2201 if (errcode && err == NULL)
2202 err = got_error_set_errno(errcode,
2203 "pthread_mutex_unlock");
2204 if (err)
2205 break;
2206 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2208 return err;
2211 static void
2212 select_commit(struct tog_log_view_state *s)
2214 struct commit_queue_entry *entry;
2215 int ncommits = 0;
2217 entry = s->first_displayed_entry;
2218 while (entry) {
2219 if (ncommits == s->selected) {
2220 s->selected_entry = entry;
2221 break;
2223 entry = TAILQ_NEXT(entry, entry);
2224 ncommits++;
2228 static const struct got_error *
2229 draw_commits(struct tog_view *view)
2231 const struct got_error *err = NULL;
2232 struct tog_log_view_state *s = &view->state.log;
2233 struct commit_queue_entry *entry = s->selected_entry;
2234 const int limit = view->nlines;
2235 int width;
2236 int ncommits, author_cols = 4;
2237 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2238 char *refs_str = NULL;
2239 wchar_t *wline;
2240 struct tog_color *tc;
2241 static const size_t date_display_cols = 12;
2243 if (s->selected_entry &&
2244 !(view->searching && view->search_next_done == 0)) {
2245 struct got_reflist_head *refs;
2246 err = got_object_id_str(&id_str, s->selected_entry->id);
2247 if (err)
2248 return err;
2249 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2250 s->selected_entry->id);
2251 if (refs) {
2252 err = build_refs_str(&refs_str, refs,
2253 s->selected_entry->id, s->repo);
2254 if (err)
2255 goto done;
2259 if (s->thread_args.commits_needed == 0)
2260 halfdelay(10); /* disable fast refresh */
2262 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2263 if (asprintf(&ncommits_str, " [%d/%d] %s",
2264 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2265 (view->searching && !view->search_next_done) ?
2266 "searching..." : "loading...") == -1) {
2267 err = got_error_from_errno("asprintf");
2268 goto done;
2270 } else {
2271 const char *search_str = NULL;
2273 if (view->searching) {
2274 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2275 search_str = "no more matches";
2276 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2277 search_str = "no matches found";
2278 else if (!view->search_next_done)
2279 search_str = "searching...";
2282 if (asprintf(&ncommits_str, " [%d/%d] %s",
2283 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2284 search_str ? search_str :
2285 (refs_str ? refs_str : "")) == -1) {
2286 err = got_error_from_errno("asprintf");
2287 goto done;
2291 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2292 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2293 "........................................",
2294 s->in_repo_path, ncommits_str) == -1) {
2295 err = got_error_from_errno("asprintf");
2296 header = NULL;
2297 goto done;
2299 } else if (asprintf(&header, "commit %s%s",
2300 id_str ? id_str : "........................................",
2301 ncommits_str) == -1) {
2302 err = got_error_from_errno("asprintf");
2303 header = NULL;
2304 goto done;
2306 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2307 if (err)
2308 goto done;
2310 werase(view->window);
2312 if (view_needs_focus_indication(view))
2313 wstandout(view->window);
2314 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2315 if (tc)
2316 wattr_on(view->window,
2317 COLOR_PAIR(tc->colorpair), NULL);
2318 waddwstr(view->window, wline);
2319 if (tc)
2320 wattr_off(view->window,
2321 COLOR_PAIR(tc->colorpair), NULL);
2322 while (width < view->ncols) {
2323 waddch(view->window, ' ');
2324 width++;
2326 if (view_needs_focus_indication(view))
2327 wstandend(view->window);
2328 free(wline);
2329 if (limit <= 1)
2330 goto done;
2332 /* Grow author column size if necessary, and set view->maxx. */
2333 entry = s->first_displayed_entry;
2334 ncommits = 0;
2335 view->maxx = 0;
2336 while (entry) {
2337 struct got_commit_object *c = entry->commit;
2338 char *author, *eol, *msg, *msg0;
2339 wchar_t *wauthor, *wmsg;
2340 int width;
2341 if (ncommits >= limit - 1)
2342 break;
2343 if (s->use_committer)
2344 author = strdup(got_object_commit_get_committer(c));
2345 else
2346 author = strdup(got_object_commit_get_author(c));
2347 if (author == NULL) {
2348 err = got_error_from_errno("strdup");
2349 goto done;
2351 err = format_author(&wauthor, &width, author, COLS,
2352 date_display_cols);
2353 if (author_cols < width)
2354 author_cols = width;
2355 free(wauthor);
2356 free(author);
2357 if (err)
2358 goto done;
2359 err = got_object_commit_get_logmsg(&msg0, c);
2360 if (err)
2361 goto done;
2362 msg = msg0;
2363 while (*msg == '\n')
2364 ++msg;
2365 if ((eol = strchr(msg, '\n')))
2366 *eol = '\0';
2367 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2368 date_display_cols + author_cols, 0);
2369 if (err)
2370 goto done;
2371 view->maxx = MAX(view->maxx, width);
2372 free(msg0);
2373 free(wmsg);
2374 ncommits++;
2375 entry = TAILQ_NEXT(entry, entry);
2378 entry = s->first_displayed_entry;
2379 s->last_displayed_entry = s->first_displayed_entry;
2380 ncommits = 0;
2381 while (entry) {
2382 if (ncommits >= limit - 1)
2383 break;
2384 if (ncommits == s->selected)
2385 wstandout(view->window);
2386 err = draw_commit(view, entry->commit, entry->id,
2387 date_display_cols, author_cols);
2388 if (ncommits == s->selected)
2389 wstandend(view->window);
2390 if (err)
2391 goto done;
2392 ncommits++;
2393 s->last_displayed_entry = entry;
2394 entry = TAILQ_NEXT(entry, entry);
2397 view_border(view);
2398 done:
2399 free(id_str);
2400 free(refs_str);
2401 free(ncommits_str);
2402 free(header);
2403 return err;
2406 static void
2407 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2409 struct commit_queue_entry *entry;
2410 int nscrolled = 0;
2412 entry = TAILQ_FIRST(&s->commits.head);
2413 if (s->first_displayed_entry == entry)
2414 return;
2416 entry = s->first_displayed_entry;
2417 while (entry && nscrolled < maxscroll) {
2418 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2419 if (entry) {
2420 s->first_displayed_entry = entry;
2421 nscrolled++;
2426 static const struct got_error *
2427 trigger_log_thread(struct tog_view *view, int wait)
2429 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2430 int errcode;
2432 halfdelay(1); /* fast refresh while loading commits */
2434 while (!ta->log_complete && !tog_thread_error &&
2435 (ta->commits_needed > 0 || ta->load_all)) {
2436 /* Wake the log thread. */
2437 errcode = pthread_cond_signal(&ta->need_commits);
2438 if (errcode)
2439 return got_error_set_errno(errcode,
2440 "pthread_cond_signal");
2443 * The mutex will be released while the view loop waits
2444 * in wgetch(), at which time the log thread will run.
2446 if (!wait)
2447 break;
2449 /* Display progress update in log view. */
2450 show_log_view(view);
2451 update_panels();
2452 doupdate();
2454 /* Wait right here while next commit is being loaded. */
2455 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2456 if (errcode)
2457 return got_error_set_errno(errcode,
2458 "pthread_cond_wait");
2460 /* Display progress update in log view. */
2461 show_log_view(view);
2462 update_panels();
2463 doupdate();
2466 return NULL;
2469 static const struct got_error *
2470 request_log_commits(struct tog_view *view)
2472 struct tog_log_view_state *state = &view->state.log;
2473 const struct got_error *err = NULL;
2475 if (state->thread_args.log_complete)
2476 return NULL;
2478 state->thread_args.commits_needed += view->nscrolled;
2479 err = trigger_log_thread(view, 1);
2480 view->nscrolled = 0;
2482 return err;
2485 static const struct got_error *
2486 log_scroll_down(struct tog_view *view, int maxscroll)
2488 struct tog_log_view_state *s = &view->state.log;
2489 const struct got_error *err = NULL;
2490 struct commit_queue_entry *pentry;
2491 int nscrolled = 0, ncommits_needed;
2493 if (s->last_displayed_entry == NULL)
2494 return NULL;
2496 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2497 if (s->commits.ncommits < ncommits_needed &&
2498 !s->thread_args.log_complete) {
2500 * Ask the log thread for required amount of commits.
2502 s->thread_args.commits_needed += maxscroll;
2503 err = trigger_log_thread(view, 1);
2504 if (err)
2505 return err;
2508 do {
2509 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2510 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2511 break;
2513 s->last_displayed_entry = pentry ?
2514 pentry : s->last_displayed_entry;;
2516 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2517 if (pentry == NULL)
2518 break;
2519 s->first_displayed_entry = pentry;
2520 } while (++nscrolled < maxscroll);
2522 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2523 view->nscrolled += nscrolled;
2524 else
2525 view->nscrolled = 0;
2527 return err;
2530 static const struct got_error *
2531 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2532 struct got_commit_object *commit, struct got_object_id *commit_id,
2533 struct tog_view *log_view, struct got_repository *repo)
2535 const struct got_error *err;
2536 struct got_object_qid *parent_id;
2537 struct tog_view *diff_view;
2539 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2540 if (diff_view == NULL)
2541 return got_error_from_errno("view_open");
2543 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2544 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2545 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2546 if (err == NULL)
2547 *new_view = diff_view;
2548 return err;
2551 static const struct got_error *
2552 tree_view_visit_subtree(struct tog_tree_view_state *s,
2553 struct got_tree_object *subtree)
2555 struct tog_parent_tree *parent;
2557 parent = calloc(1, sizeof(*parent));
2558 if (parent == NULL)
2559 return got_error_from_errno("calloc");
2561 parent->tree = s->tree;
2562 parent->first_displayed_entry = s->first_displayed_entry;
2563 parent->selected_entry = s->selected_entry;
2564 parent->selected = s->selected;
2565 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2566 s->tree = subtree;
2567 s->selected = 0;
2568 s->first_displayed_entry = NULL;
2569 return NULL;
2572 static const struct got_error *
2573 tree_view_walk_path(struct tog_tree_view_state *s,
2574 struct got_commit_object *commit, const char *path)
2576 const struct got_error *err = NULL;
2577 struct got_tree_object *tree = NULL;
2578 const char *p;
2579 char *slash, *subpath = NULL;
2581 /* Walk the path and open corresponding tree objects. */
2582 p = path;
2583 while (*p) {
2584 struct got_tree_entry *te;
2585 struct got_object_id *tree_id;
2586 char *te_name;
2588 while (p[0] == '/')
2589 p++;
2591 /* Ensure the correct subtree entry is selected. */
2592 slash = strchr(p, '/');
2593 if (slash == NULL)
2594 te_name = strdup(p);
2595 else
2596 te_name = strndup(p, slash - p);
2597 if (te_name == NULL) {
2598 err = got_error_from_errno("strndup");
2599 break;
2601 te = got_object_tree_find_entry(s->tree, te_name);
2602 if (te == NULL) {
2603 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2604 free(te_name);
2605 break;
2607 free(te_name);
2608 s->first_displayed_entry = s->selected_entry = te;
2610 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2611 break; /* jump to this file's entry */
2613 slash = strchr(p, '/');
2614 if (slash)
2615 subpath = strndup(path, slash - path);
2616 else
2617 subpath = strdup(path);
2618 if (subpath == NULL) {
2619 err = got_error_from_errno("strdup");
2620 break;
2623 err = got_object_id_by_path(&tree_id, s->repo, commit,
2624 subpath);
2625 if (err)
2626 break;
2628 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2629 free(tree_id);
2630 if (err)
2631 break;
2633 err = tree_view_visit_subtree(s, tree);
2634 if (err) {
2635 got_object_tree_close(tree);
2636 break;
2638 if (slash == NULL)
2639 break;
2640 free(subpath);
2641 subpath = NULL;
2642 p = slash;
2645 free(subpath);
2646 return err;
2649 static const struct got_error *
2650 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2651 struct commit_queue_entry *entry, const char *path,
2652 const char *head_ref_name, struct got_repository *repo)
2654 const struct got_error *err = NULL;
2655 struct tog_tree_view_state *s;
2656 struct tog_view *tree_view;
2658 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2659 if (tree_view == NULL)
2660 return got_error_from_errno("view_open");
2662 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2663 if (err)
2664 return err;
2665 s = &tree_view->state.tree;
2667 *new_view = tree_view;
2669 if (got_path_is_root_dir(path))
2670 return NULL;
2672 return tree_view_walk_path(s, entry->commit, path);
2675 static const struct got_error *
2676 block_signals_used_by_main_thread(void)
2678 sigset_t sigset;
2679 int errcode;
2681 if (sigemptyset(&sigset) == -1)
2682 return got_error_from_errno("sigemptyset");
2684 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2685 if (sigaddset(&sigset, SIGWINCH) == -1)
2686 return got_error_from_errno("sigaddset");
2687 if (sigaddset(&sigset, SIGCONT) == -1)
2688 return got_error_from_errno("sigaddset");
2689 if (sigaddset(&sigset, SIGINT) == -1)
2690 return got_error_from_errno("sigaddset");
2691 if (sigaddset(&sigset, SIGTERM) == -1)
2692 return got_error_from_errno("sigaddset");
2694 /* ncurses handles SIGTSTP */
2695 if (sigaddset(&sigset, SIGTSTP) == -1)
2696 return got_error_from_errno("sigaddset");
2698 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2699 if (errcode)
2700 return got_error_set_errno(errcode, "pthread_sigmask");
2702 return NULL;
2705 static void *
2706 log_thread(void *arg)
2708 const struct got_error *err = NULL;
2709 int errcode = 0;
2710 struct tog_log_thread_args *a = arg;
2711 int done = 0;
2714 * Sync startup with main thread such that we begin our
2715 * work once view_input() has released the mutex.
2717 errcode = pthread_mutex_lock(&tog_mutex);
2718 if (errcode) {
2719 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2720 return (void *)err;
2723 err = block_signals_used_by_main_thread();
2724 if (err) {
2725 pthread_mutex_unlock(&tog_mutex);
2726 goto done;
2729 while (!done && !err && !tog_fatal_signal_received()) {
2730 errcode = pthread_mutex_unlock(&tog_mutex);
2731 if (errcode) {
2732 err = got_error_set_errno(errcode,
2733 "pthread_mutex_unlock");
2734 goto done;
2736 err = queue_commits(a);
2737 if (err) {
2738 if (err->code != GOT_ERR_ITER_COMPLETED)
2739 goto done;
2740 err = NULL;
2741 done = 1;
2742 } else if (a->commits_needed > 0 && !a->load_all)
2743 a->commits_needed--;
2745 errcode = pthread_mutex_lock(&tog_mutex);
2746 if (errcode) {
2747 err = got_error_set_errno(errcode,
2748 "pthread_mutex_lock");
2749 goto done;
2750 } else if (*a->quit)
2751 done = 1;
2752 else if (*a->first_displayed_entry == NULL) {
2753 *a->first_displayed_entry =
2754 TAILQ_FIRST(&a->commits->head);
2755 *a->selected_entry = *a->first_displayed_entry;
2758 errcode = pthread_cond_signal(&a->commit_loaded);
2759 if (errcode) {
2760 err = got_error_set_errno(errcode,
2761 "pthread_cond_signal");
2762 pthread_mutex_unlock(&tog_mutex);
2763 goto done;
2766 if (done)
2767 a->commits_needed = 0;
2768 else {
2769 if (a->commits_needed == 0 && !a->load_all) {
2770 errcode = pthread_cond_wait(&a->need_commits,
2771 &tog_mutex);
2772 if (errcode) {
2773 err = got_error_set_errno(errcode,
2774 "pthread_cond_wait");
2775 pthread_mutex_unlock(&tog_mutex);
2776 goto done;
2778 if (*a->quit)
2779 done = 1;
2783 a->log_complete = 1;
2784 errcode = pthread_mutex_unlock(&tog_mutex);
2785 if (errcode)
2786 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2787 done:
2788 if (err) {
2789 tog_thread_error = 1;
2790 pthread_cond_signal(&a->commit_loaded);
2792 return (void *)err;
2795 static const struct got_error *
2796 stop_log_thread(struct tog_log_view_state *s)
2798 const struct got_error *err = NULL, *thread_err = NULL;
2799 int errcode;
2801 if (s->thread) {
2802 s->quit = 1;
2803 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2804 if (errcode)
2805 return got_error_set_errno(errcode,
2806 "pthread_cond_signal");
2807 errcode = pthread_mutex_unlock(&tog_mutex);
2808 if (errcode)
2809 return got_error_set_errno(errcode,
2810 "pthread_mutex_unlock");
2811 errcode = pthread_join(s->thread, (void **)&thread_err);
2812 if (errcode)
2813 return got_error_set_errno(errcode, "pthread_join");
2814 errcode = pthread_mutex_lock(&tog_mutex);
2815 if (errcode)
2816 return got_error_set_errno(errcode,
2817 "pthread_mutex_lock");
2818 s->thread = 0; //NULL;
2821 if (s->thread_args.repo) {
2822 err = got_repo_close(s->thread_args.repo);
2823 s->thread_args.repo = NULL;
2826 if (s->thread_args.pack_fds) {
2827 const struct got_error *pack_err =
2828 got_repo_pack_fds_close(s->thread_args.pack_fds);
2829 if (err == NULL)
2830 err = pack_err;
2831 s->thread_args.pack_fds = NULL;
2834 if (s->thread_args.graph) {
2835 got_commit_graph_close(s->thread_args.graph);
2836 s->thread_args.graph = NULL;
2839 return err ? err : thread_err;
2842 static const struct got_error *
2843 close_log_view(struct tog_view *view)
2845 const struct got_error *err = NULL;
2846 struct tog_log_view_state *s = &view->state.log;
2847 int errcode;
2849 err = stop_log_thread(s);
2851 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2852 if (errcode && err == NULL)
2853 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2855 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2856 if (errcode && err == NULL)
2857 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2859 free_commits(&s->commits);
2860 free(s->in_repo_path);
2861 s->in_repo_path = NULL;
2862 free(s->start_id);
2863 s->start_id = NULL;
2864 free(s->head_ref_name);
2865 s->head_ref_name = NULL;
2866 return err;
2869 static const struct got_error *
2870 search_start_log_view(struct tog_view *view)
2872 struct tog_log_view_state *s = &view->state.log;
2874 s->matched_entry = NULL;
2875 s->search_entry = NULL;
2876 return NULL;
2879 static const struct got_error *
2880 search_next_log_view(struct tog_view *view)
2882 const struct got_error *err = NULL;
2883 struct tog_log_view_state *s = &view->state.log;
2884 struct commit_queue_entry *entry;
2886 /* Display progress update in log view. */
2887 show_log_view(view);
2888 update_panels();
2889 doupdate();
2891 if (s->search_entry) {
2892 int errcode, ch;
2893 errcode = pthread_mutex_unlock(&tog_mutex);
2894 if (errcode)
2895 return got_error_set_errno(errcode,
2896 "pthread_mutex_unlock");
2897 ch = wgetch(view->window);
2898 errcode = pthread_mutex_lock(&tog_mutex);
2899 if (errcode)
2900 return got_error_set_errno(errcode,
2901 "pthread_mutex_lock");
2902 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2903 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2904 return NULL;
2906 if (view->searching == TOG_SEARCH_FORWARD)
2907 entry = TAILQ_NEXT(s->search_entry, entry);
2908 else
2909 entry = TAILQ_PREV(s->search_entry,
2910 commit_queue_head, entry);
2911 } else if (s->matched_entry) {
2912 int matched_idx = s->matched_entry->idx;
2913 int selected_idx = s->selected_entry->idx;
2916 * If the user has moved the cursor after we hit a match,
2917 * the position from where we should continue searching
2918 * might have changed.
2920 if (view->searching == TOG_SEARCH_FORWARD) {
2921 if (matched_idx > selected_idx)
2922 entry = TAILQ_NEXT(s->selected_entry, entry);
2923 else
2924 entry = TAILQ_NEXT(s->matched_entry, entry);
2925 } else {
2926 if (matched_idx < selected_idx)
2927 entry = TAILQ_PREV(s->selected_entry,
2928 commit_queue_head, entry);
2929 else
2930 entry = TAILQ_PREV(s->matched_entry,
2931 commit_queue_head, entry);
2933 } else {
2934 entry = s->selected_entry;
2937 while (1) {
2938 int have_match = 0;
2940 if (entry == NULL) {
2941 if (s->thread_args.log_complete ||
2942 view->searching == TOG_SEARCH_BACKWARD) {
2943 view->search_next_done =
2944 (s->matched_entry == NULL ?
2945 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2946 s->search_entry = NULL;
2947 return NULL;
2950 * Poke the log thread for more commits and return,
2951 * allowing the main loop to make progress. Search
2952 * will resume at s->search_entry once we come back.
2954 s->thread_args.commits_needed++;
2955 return trigger_log_thread(view, 0);
2958 err = match_commit(&have_match, entry->id, entry->commit,
2959 &view->regex);
2960 if (err)
2961 break;
2962 if (have_match) {
2963 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2964 s->matched_entry = entry;
2965 break;
2968 s->search_entry = entry;
2969 if (view->searching == TOG_SEARCH_FORWARD)
2970 entry = TAILQ_NEXT(entry, entry);
2971 else
2972 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2975 if (s->matched_entry) {
2976 int cur = s->selected_entry->idx;
2977 while (cur < s->matched_entry->idx) {
2978 err = input_log_view(NULL, view, KEY_DOWN);
2979 if (err)
2980 return err;
2981 cur++;
2983 while (cur > s->matched_entry->idx) {
2984 err = input_log_view(NULL, view, KEY_UP);
2985 if (err)
2986 return err;
2987 cur--;
2991 s->search_entry = NULL;
2993 return NULL;
2996 static const struct got_error *
2997 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2998 struct got_repository *repo, const char *head_ref_name,
2999 const char *in_repo_path, int log_branches)
3001 const struct got_error *err = NULL;
3002 struct tog_log_view_state *s = &view->state.log;
3003 struct got_repository *thread_repo = NULL;
3004 struct got_commit_graph *thread_graph = NULL;
3005 int errcode;
3007 if (in_repo_path != s->in_repo_path) {
3008 free(s->in_repo_path);
3009 s->in_repo_path = strdup(in_repo_path);
3010 if (s->in_repo_path == NULL)
3011 return got_error_from_errno("strdup");
3014 /* The commit queue only contains commits being displayed. */
3015 TAILQ_INIT(&s->commits.head);
3016 s->commits.ncommits = 0;
3018 s->repo = repo;
3019 if (head_ref_name) {
3020 s->head_ref_name = strdup(head_ref_name);
3021 if (s->head_ref_name == NULL) {
3022 err = got_error_from_errno("strdup");
3023 goto done;
3026 s->start_id = got_object_id_dup(start_id);
3027 if (s->start_id == NULL) {
3028 err = got_error_from_errno("got_object_id_dup");
3029 goto done;
3031 s->log_branches = log_branches;
3033 STAILQ_INIT(&s->colors);
3034 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3035 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3036 get_color_value("TOG_COLOR_COMMIT"));
3037 if (err)
3038 goto done;
3039 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3040 get_color_value("TOG_COLOR_AUTHOR"));
3041 if (err) {
3042 free_colors(&s->colors);
3043 goto done;
3045 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3046 get_color_value("TOG_COLOR_DATE"));
3047 if (err) {
3048 free_colors(&s->colors);
3049 goto done;
3053 view->show = show_log_view;
3054 view->input = input_log_view;
3055 view->resize = resize_log_view;
3056 view->close = close_log_view;
3057 view->search_start = search_start_log_view;
3058 view->search_next = search_next_log_view;
3060 if (s->thread_args.pack_fds == NULL) {
3061 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3062 if (err)
3063 goto done;
3065 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3066 s->thread_args.pack_fds);
3067 if (err)
3068 goto done;
3069 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3070 !s->log_branches);
3071 if (err)
3072 goto done;
3073 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3074 s->repo, NULL, NULL);
3075 if (err)
3076 goto done;
3078 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3079 if (errcode) {
3080 err = got_error_set_errno(errcode, "pthread_cond_init");
3081 goto done;
3083 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3084 if (errcode) {
3085 err = got_error_set_errno(errcode, "pthread_cond_init");
3086 goto done;
3089 s->thread_args.commits_needed = view->nlines;
3090 s->thread_args.graph = thread_graph;
3091 s->thread_args.commits = &s->commits;
3092 s->thread_args.in_repo_path = s->in_repo_path;
3093 s->thread_args.start_id = s->start_id;
3094 s->thread_args.repo = thread_repo;
3095 s->thread_args.log_complete = 0;
3096 s->thread_args.quit = &s->quit;
3097 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3098 s->thread_args.selected_entry = &s->selected_entry;
3099 s->thread_args.searching = &view->searching;
3100 s->thread_args.search_next_done = &view->search_next_done;
3101 s->thread_args.regex = &view->regex;
3102 done:
3103 if (err)
3104 close_log_view(view);
3105 return err;
3108 static const struct got_error *
3109 show_log_view(struct tog_view *view)
3111 const struct got_error *err;
3112 struct tog_log_view_state *s = &view->state.log;
3114 if (s->thread == 0) { //NULL) {
3115 int errcode = pthread_create(&s->thread, NULL, log_thread,
3116 &s->thread_args);
3117 if (errcode)
3118 return got_error_set_errno(errcode, "pthread_create");
3119 if (s->thread_args.commits_needed > 0) {
3120 err = trigger_log_thread(view, 1);
3121 if (err)
3122 return err;
3126 return draw_commits(view);
3129 static void
3130 log_move_cursor_up(struct tog_view *view, int page, int home)
3132 struct tog_log_view_state *s = &view->state.log;
3134 if (s->selected_entry->idx == 0)
3135 view->count = 0;
3136 if (s->first_displayed_entry == NULL)
3137 return;
3139 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3140 || home)
3141 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3143 if (!page && !home && s->selected > 0)
3144 --s->selected;
3145 else
3146 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3148 select_commit(s);
3149 return;
3152 static const struct got_error *
3153 log_move_cursor_down(struct tog_view *view, int page)
3155 struct tog_log_view_state *s = &view->state.log;
3156 struct commit_queue_entry *first;
3157 const struct got_error *err = NULL;
3159 first = s->first_displayed_entry;
3160 if (first == NULL) {
3161 view->count = 0;
3162 return NULL;
3165 if (s->thread_args.log_complete &&
3166 s->selected_entry->idx >= s->commits.ncommits - 1)
3167 return NULL;
3169 if (!page) {
3170 int eos = view->nlines - 2;
3172 if (view_is_hsplit_top(view))
3173 --eos; /* border consumes the last line */
3174 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3175 ++s->selected;
3176 else
3177 err = log_scroll_down(view, 1);
3178 } else if (s->thread_args.load_all) {
3179 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3180 s->selected += MIN(s->last_displayed_entry->idx -
3181 s->selected_entry->idx, page + 1);
3182 else
3183 err = log_scroll_down(view, MIN(page,
3184 s->commits.ncommits - s->selected_entry->idx - 1));
3185 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3186 } else {
3187 err = log_scroll_down(view, page);
3188 if (err)
3189 return err;
3190 if (first == s->first_displayed_entry && s->selected <
3191 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3192 s->selected = MIN(s->commits.ncommits - 1, page);
3195 if (err)
3196 return err;
3199 * We might necessarily overshoot in horizontal
3200 * splits; if so, select the last displayed commit.
3202 s->selected = MIN(s->selected,
3203 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3205 select_commit(s);
3207 if (s->thread_args.log_complete &&
3208 s->selected_entry->idx == s->commits.ncommits - 1)
3209 view->count = 0;
3211 return NULL;
3214 static void
3215 view_get_split(struct tog_view *view, int *y, int *x)
3217 *x = 0;
3218 *y = 0;
3220 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3221 if (view->child && view->child->resized_y)
3222 *y = view->child->resized_y;
3223 else if (view->resized_y)
3224 *y = view->resized_y;
3225 else
3226 *y = view_split_begin_y(view->lines);
3227 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3228 if (view->child && view->child->resized_x)
3229 *x = view->child->resized_x;
3230 else if (view->resized_x)
3231 *x = view->resized_x;
3232 else
3233 *x = view_split_begin_x(view->begin_x);
3237 /* Split view horizontally at y and offset view->state->selected line. */
3238 static const struct got_error *
3239 view_init_hsplit(struct tog_view *view, int y)
3241 const struct got_error *err = NULL;
3243 view->nlines = y;
3244 view->ncols = COLS;
3245 err = view_resize(view);
3246 if (err)
3247 return err;
3249 err = offset_selection_down(view);
3251 return err;
3254 static const struct got_error *
3255 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3257 const struct got_error *err = NULL;
3258 struct tog_log_view_state *s = &view->state.log;
3259 struct commit_queue_entry *entry;
3260 int eos, n, nscroll;
3262 if (s->thread_args.load_all) {
3263 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3264 s->thread_args.load_all = 0;
3265 else if (s->thread_args.log_complete) {
3266 err = log_move_cursor_down(view, s->commits.ncommits);
3267 s->thread_args.load_all = 0;
3269 return err;
3272 eos = nscroll = view->nlines - 1;
3273 if (view_is_hsplit_top(view))
3274 --eos; /* border */
3276 switch (ch) {
3277 case 'q':
3278 s->quit = 1;
3279 break;
3280 case '0':
3281 view->x = 0;
3282 break;
3283 case '$':
3284 view->x = MAX(view->maxx - view->ncols / 2, 0);
3285 view->count = 0;
3286 break;
3287 case KEY_RIGHT:
3288 case 'l':
3289 if (view->x + view->ncols / 2 < view->maxx)
3290 view->x += 2; /* move two columns right */
3291 else
3292 view->count = 0;
3293 break;
3294 case KEY_LEFT:
3295 case 'h':
3296 view->x -= MIN(view->x, 2); /* move two columns back */
3297 if (view->x <= 0)
3298 view->count = 0;
3299 break;
3300 case 'k':
3301 case KEY_UP:
3302 case '<':
3303 case ',':
3304 case CTRL('p'):
3305 log_move_cursor_up(view, 0, 0);
3306 break;
3307 case 'g':
3308 case KEY_HOME:
3309 log_move_cursor_up(view, 0, 1);
3310 view->count = 0;
3311 break;
3312 case CTRL('u'):
3313 case 'u':
3314 nscroll /= 2;
3315 /* FALL THROUGH */
3316 case KEY_PPAGE:
3317 case CTRL('b'):
3318 case 'b':
3319 log_move_cursor_up(view, nscroll, 0);
3320 break;
3321 case 'j':
3322 case KEY_DOWN:
3323 case '>':
3324 case '.':
3325 case CTRL('n'):
3326 err = log_move_cursor_down(view, 0);
3327 break;
3328 case '@':
3329 s->use_committer = !s->use_committer;
3330 break;
3331 case 'G':
3332 case KEY_END: {
3333 /* We don't know yet how many commits, so we're forced to
3334 * traverse them all. */
3335 view->count = 0;
3336 if (!s->thread_args.log_complete) {
3337 s->thread_args.load_all = 1;
3338 return trigger_log_thread(view, 0);
3341 s->selected = 0;
3342 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3343 for (n = 0; n < eos; n++) {
3344 if (entry == NULL)
3345 break;
3346 s->first_displayed_entry = entry;
3347 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3349 if (n > 0)
3350 s->selected = n - 1;
3351 select_commit(s);
3352 break;
3354 case CTRL('d'):
3355 case 'd':
3356 nscroll /= 2;
3357 /* FALL THROUGH */
3358 case KEY_NPAGE:
3359 case CTRL('f'):
3360 case 'f':
3361 case ' ':
3362 err = log_move_cursor_down(view, nscroll);
3363 break;
3364 case KEY_RESIZE:
3365 if (s->selected > view->nlines - 2)
3366 s->selected = view->nlines - 2;
3367 if (s->selected > s->commits.ncommits - 1)
3368 s->selected = s->commits.ncommits - 1;
3369 select_commit(s);
3370 if (s->commits.ncommits < view->nlines - 1 &&
3371 !s->thread_args.log_complete) {
3372 s->thread_args.commits_needed += (view->nlines - 1) -
3373 s->commits.ncommits;
3374 err = trigger_log_thread(view, 1);
3376 break;
3377 case KEY_ENTER:
3378 case '\r':
3379 view->count = 0;
3380 if (s->selected_entry == NULL)
3381 break;
3382 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3383 break;
3384 case 'T':
3385 view->count = 0;
3386 if (s->selected_entry == NULL)
3387 break;
3388 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3389 break;
3390 case KEY_BACKSPACE:
3391 case CTRL('l'):
3392 case 'B':
3393 view->count = 0;
3394 if (ch == KEY_BACKSPACE &&
3395 got_path_is_root_dir(s->in_repo_path))
3396 break;
3397 err = stop_log_thread(s);
3398 if (err)
3399 return err;
3400 if (ch == KEY_BACKSPACE) {
3401 char *parent_path;
3402 err = got_path_dirname(&parent_path, s->in_repo_path);
3403 if (err)
3404 return err;
3405 free(s->in_repo_path);
3406 s->in_repo_path = parent_path;
3407 s->thread_args.in_repo_path = s->in_repo_path;
3408 } else if (ch == CTRL('l')) {
3409 struct got_object_id *start_id;
3410 err = got_repo_match_object_id(&start_id, NULL,
3411 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3412 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3413 if (err)
3414 return err;
3415 free(s->start_id);
3416 s->start_id = start_id;
3417 s->thread_args.start_id = s->start_id;
3418 } else /* 'B' */
3419 s->log_branches = !s->log_branches;
3421 if (s->thread_args.pack_fds == NULL) {
3422 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3423 if (err)
3424 return err;
3426 err = got_repo_open(&s->thread_args.repo,
3427 got_repo_get_path(s->repo), NULL,
3428 s->thread_args.pack_fds);
3429 if (err)
3430 return err;
3431 tog_free_refs();
3432 err = tog_load_refs(s->repo, 0);
3433 if (err)
3434 return err;
3435 err = got_commit_graph_open(&s->thread_args.graph,
3436 s->in_repo_path, !s->log_branches);
3437 if (err)
3438 return err;
3439 err = got_commit_graph_iter_start(s->thread_args.graph,
3440 s->start_id, s->repo, NULL, NULL);
3441 if (err)
3442 return err;
3443 free_commits(&s->commits);
3444 s->first_displayed_entry = NULL;
3445 s->last_displayed_entry = NULL;
3446 s->selected_entry = NULL;
3447 s->selected = 0;
3448 s->thread_args.log_complete = 0;
3449 s->quit = 0;
3450 s->thread_args.commits_needed = view->lines;
3451 s->matched_entry = NULL;
3452 s->search_entry = NULL;
3453 view->offset = 0;
3454 break;
3455 case 'R':
3456 view->count = 0;
3457 err = view_request_new(new_view, view, TOG_VIEW_REF);
3458 break;
3459 default:
3460 view->count = 0;
3461 break;
3464 return err;
3467 static const struct got_error *
3468 apply_unveil(const char *repo_path, const char *worktree_path)
3470 const struct got_error *error;
3472 #ifdef PROFILE
3473 if (unveil("gmon.out", "rwc") != 0)
3474 return got_error_from_errno2("unveil", "gmon.out");
3475 #endif
3476 if (repo_path && unveil(repo_path, "r") != 0)
3477 return got_error_from_errno2("unveil", repo_path);
3479 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3480 return got_error_from_errno2("unveil", worktree_path);
3482 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3483 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3485 error = got_privsep_unveil_exec_helpers();
3486 if (error != NULL)
3487 return error;
3489 if (unveil(NULL, NULL) != 0)
3490 return got_error_from_errno("unveil");
3492 return NULL;
3495 static void
3496 init_curses(void)
3499 * Override default signal handlers before starting ncurses.
3500 * This should prevent ncurses from installing its own
3501 * broken cleanup() signal handler.
3503 signal(SIGWINCH, tog_sigwinch);
3504 signal(SIGPIPE, tog_sigpipe);
3505 signal(SIGCONT, tog_sigcont);
3506 signal(SIGINT, tog_sigint);
3507 signal(SIGTERM, tog_sigterm);
3509 initscr();
3510 cbreak();
3511 halfdelay(1); /* Do fast refresh while initial view is loading. */
3512 noecho();
3513 nonl();
3514 intrflush(stdscr, FALSE);
3515 keypad(stdscr, TRUE);
3516 curs_set(0);
3517 if (getenv("TOG_COLORS") != NULL) {
3518 start_color();
3519 use_default_colors();
3523 static const struct got_error *
3524 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3525 struct got_repository *repo, struct got_worktree *worktree)
3527 const struct got_error *err = NULL;
3529 if (argc == 0) {
3530 *in_repo_path = strdup("/");
3531 if (*in_repo_path == NULL)
3532 return got_error_from_errno("strdup");
3533 return NULL;
3536 if (worktree) {
3537 const char *prefix = got_worktree_get_path_prefix(worktree);
3538 char *p;
3540 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3541 if (err)
3542 return err;
3543 if (asprintf(in_repo_path, "%s%s%s", prefix,
3544 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3545 p) == -1) {
3546 err = got_error_from_errno("asprintf");
3547 *in_repo_path = NULL;
3549 free(p);
3550 } else
3551 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3553 return err;
3556 static const struct got_error *
3557 cmd_log(int argc, char *argv[])
3559 const struct got_error *error;
3560 struct got_repository *repo = NULL;
3561 struct got_worktree *worktree = NULL;
3562 struct got_object_id *start_id = NULL;
3563 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3564 char *start_commit = NULL, *label = NULL;
3565 struct got_reference *ref = NULL;
3566 const char *head_ref_name = NULL;
3567 int ch, log_branches = 0;
3568 struct tog_view *view;
3569 int *pack_fds = NULL;
3571 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3572 switch (ch) {
3573 case 'b':
3574 log_branches = 1;
3575 break;
3576 case 'c':
3577 start_commit = optarg;
3578 break;
3579 case 'r':
3580 repo_path = realpath(optarg, NULL);
3581 if (repo_path == NULL)
3582 return got_error_from_errno2("realpath",
3583 optarg);
3584 break;
3585 default:
3586 usage_log();
3587 /* NOTREACHED */
3591 argc -= optind;
3592 argv += optind;
3594 if (argc > 1)
3595 usage_log();
3597 error = got_repo_pack_fds_open(&pack_fds);
3598 if (error != NULL)
3599 goto done;
3601 if (repo_path == NULL) {
3602 cwd = getcwd(NULL, 0);
3603 if (cwd == NULL)
3604 return got_error_from_errno("getcwd");
3605 error = got_worktree_open(&worktree, cwd);
3606 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3607 goto done;
3608 if (worktree)
3609 repo_path =
3610 strdup(got_worktree_get_repo_path(worktree));
3611 else
3612 repo_path = strdup(cwd);
3613 if (repo_path == NULL) {
3614 error = got_error_from_errno("strdup");
3615 goto done;
3619 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3620 if (error != NULL)
3621 goto done;
3623 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3624 repo, worktree);
3625 if (error)
3626 goto done;
3628 init_curses();
3630 error = apply_unveil(got_repo_get_path(repo),
3631 worktree ? got_worktree_get_root_path(worktree) : NULL);
3632 if (error)
3633 goto done;
3635 /* already loaded by tog_log_with_path()? */
3636 if (TAILQ_EMPTY(&tog_refs)) {
3637 error = tog_load_refs(repo, 0);
3638 if (error)
3639 goto done;
3642 if (start_commit == NULL) {
3643 error = got_repo_match_object_id(&start_id, &label,
3644 worktree ? got_worktree_get_head_ref_name(worktree) :
3645 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3646 if (error)
3647 goto done;
3648 head_ref_name = label;
3649 } else {
3650 error = got_ref_open(&ref, repo, start_commit, 0);
3651 if (error == NULL)
3652 head_ref_name = got_ref_get_name(ref);
3653 else if (error->code != GOT_ERR_NOT_REF)
3654 goto done;
3655 error = got_repo_match_object_id(&start_id, NULL,
3656 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3657 if (error)
3658 goto done;
3661 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3662 if (view == NULL) {
3663 error = got_error_from_errno("view_open");
3664 goto done;
3666 error = open_log_view(view, start_id, repo, head_ref_name,
3667 in_repo_path, log_branches);
3668 if (error)
3669 goto done;
3670 if (worktree) {
3671 /* Release work tree lock. */
3672 got_worktree_close(worktree);
3673 worktree = NULL;
3675 error = view_loop(view);
3676 done:
3677 free(in_repo_path);
3678 free(repo_path);
3679 free(cwd);
3680 free(start_id);
3681 free(label);
3682 if (ref)
3683 got_ref_close(ref);
3684 if (repo) {
3685 const struct got_error *close_err = got_repo_close(repo);
3686 if (error == NULL)
3687 error = close_err;
3689 if (worktree)
3690 got_worktree_close(worktree);
3691 if (pack_fds) {
3692 const struct got_error *pack_err =
3693 got_repo_pack_fds_close(pack_fds);
3694 if (error == NULL)
3695 error = pack_err;
3697 tog_free_refs();
3698 return error;
3701 __dead static void
3702 usage_diff(void)
3704 endwin();
3705 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3706 "[-w] object1 object2\n", getprogname());
3707 exit(1);
3710 static int
3711 match_line(const char *line, regex_t *regex, size_t nmatch,
3712 regmatch_t *regmatch)
3714 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3717 static struct tog_color *
3718 match_color(struct tog_colors *colors, const char *line)
3720 struct tog_color *tc = NULL;
3722 STAILQ_FOREACH(tc, colors, entry) {
3723 if (match_line(line, &tc->regex, 0, NULL))
3724 return tc;
3727 return NULL;
3730 static const struct got_error *
3731 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3732 WINDOW *window, int skipcol, regmatch_t *regmatch)
3734 const struct got_error *err = NULL;
3735 char *exstr = NULL;
3736 wchar_t *wline = NULL;
3737 int rme, rms, n, width, scrollx;
3738 int width0 = 0, width1 = 0, width2 = 0;
3739 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3741 *wtotal = 0;
3743 rms = regmatch->rm_so;
3744 rme = regmatch->rm_eo;
3746 err = expand_tab(&exstr, line);
3747 if (err)
3748 return err;
3750 /* Split the line into 3 segments, according to match offsets. */
3751 seg0 = strndup(exstr, rms);
3752 if (seg0 == NULL) {
3753 err = got_error_from_errno("strndup");
3754 goto done;
3756 seg1 = strndup(exstr + rms, rme - rms);
3757 if (seg1 == NULL) {
3758 err = got_error_from_errno("strndup");
3759 goto done;
3761 seg2 = strdup(exstr + rme);
3762 if (seg2 == NULL) {
3763 err = got_error_from_errno("strndup");
3764 goto done;
3767 /* draw up to matched token if we haven't scrolled past it */
3768 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3769 col_tab_align, 1);
3770 if (err)
3771 goto done;
3772 n = MAX(width0 - skipcol, 0);
3773 if (n) {
3774 free(wline);
3775 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3776 wlimit, col_tab_align, 1);
3777 if (err)
3778 goto done;
3779 waddwstr(window, &wline[scrollx]);
3780 wlimit -= width;
3781 *wtotal += width;
3784 if (wlimit > 0) {
3785 int i = 0, w = 0;
3786 size_t wlen;
3788 free(wline);
3789 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3790 col_tab_align, 1);
3791 if (err)
3792 goto done;
3793 wlen = wcslen(wline);
3794 while (i < wlen) {
3795 width = wcwidth(wline[i]);
3796 if (width == -1) {
3797 /* should not happen, tabs are expanded */
3798 err = got_error(GOT_ERR_RANGE);
3799 goto done;
3801 if (width0 + w + width > skipcol)
3802 break;
3803 w += width;
3804 i++;
3806 /* draw (visible part of) matched token (if scrolled into it) */
3807 if (width1 - w > 0) {
3808 wattron(window, A_STANDOUT);
3809 waddwstr(window, &wline[i]);
3810 wattroff(window, A_STANDOUT);
3811 wlimit -= (width1 - w);
3812 *wtotal += (width1 - w);
3816 if (wlimit > 0) { /* draw rest of line */
3817 free(wline);
3818 if (skipcol > width0 + width1) {
3819 err = format_line(&wline, &width2, &scrollx, seg2,
3820 skipcol - (width0 + width1), wlimit,
3821 col_tab_align, 1);
3822 if (err)
3823 goto done;
3824 waddwstr(window, &wline[scrollx]);
3825 } else {
3826 err = format_line(&wline, &width2, NULL, seg2, 0,
3827 wlimit, col_tab_align, 1);
3828 if (err)
3829 goto done;
3830 waddwstr(window, wline);
3832 *wtotal += width2;
3834 done:
3835 free(wline);
3836 free(exstr);
3837 free(seg0);
3838 free(seg1);
3839 free(seg2);
3840 return err;
3843 static const struct got_error *
3844 draw_file(struct tog_view *view, const char *header)
3846 struct tog_diff_view_state *s = &view->state.diff;
3847 regmatch_t *regmatch = &view->regmatch;
3848 const struct got_error *err;
3849 int nprinted = 0;
3850 char *line;
3851 size_t linesize = 0;
3852 ssize_t linelen;
3853 struct tog_color *tc;
3854 wchar_t *wline;
3855 int width;
3856 int max_lines = view->nlines;
3857 int nlines = s->nlines;
3858 off_t line_offset;
3860 line_offset = s->line_offsets[s->first_displayed_line - 1];
3861 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3862 return got_error_from_errno("fseek");
3864 werase(view->window);
3866 if (header) {
3867 if (asprintf(&line, "[%d/%d] %s",
3868 s->first_displayed_line - 1 + s->selected_line, nlines,
3869 header) == -1)
3870 return got_error_from_errno("asprintf");
3871 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3872 0, 0);
3873 free(line);
3874 if (err)
3875 return err;
3877 if (view_needs_focus_indication(view))
3878 wstandout(view->window);
3879 waddwstr(view->window, wline);
3880 free(wline);
3881 wline = NULL;
3882 if (view_needs_focus_indication(view))
3883 wstandend(view->window);
3884 if (width <= view->ncols - 1)
3885 waddch(view->window, '\n');
3887 if (max_lines <= 1)
3888 return NULL;
3889 max_lines--;
3892 s->eof = 0;
3893 view->maxx = 0;
3894 line = NULL;
3895 while (max_lines > 0 && nprinted < max_lines) {
3896 linelen = getline(&line, &linesize, s->f);
3897 if (linelen == -1) {
3898 if (feof(s->f)) {
3899 s->eof = 1;
3900 break;
3902 free(line);
3903 return got_ferror(s->f, GOT_ERR_IO);
3906 /* Set view->maxx based on full line length. */
3907 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3908 view->x ? 1 : 0);
3909 if (err) {
3910 free(line);
3911 return err;
3913 view->maxx = MAX(view->maxx, width);
3914 free(wline);
3915 wline = NULL;
3917 tc = match_color(&s->colors, line);
3918 if (tc)
3919 wattr_on(view->window,
3920 COLOR_PAIR(tc->colorpair), NULL);
3921 if (s->first_displayed_line + nprinted == s->matched_line &&
3922 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3923 err = add_matched_line(&width, line, view->ncols, 0,
3924 view->window, view->x, regmatch);
3925 if (err) {
3926 free(line);
3927 return err;
3929 } else {
3930 int skip;
3931 err = format_line(&wline, &width, &skip, line,
3932 view->x, view->ncols, 0, view->x ? 1 : 0);
3933 if (err) {
3934 free(line);
3935 return err;
3937 waddwstr(view->window, &wline[skip]);
3938 free(wline);
3939 wline = NULL;
3941 if (tc)
3942 wattr_off(view->window,
3943 COLOR_PAIR(tc->colorpair), NULL);
3944 if (width <= view->ncols - 1)
3945 waddch(view->window, '\n');
3946 nprinted++;
3948 free(line);
3949 if (nprinted >= 1)
3950 s->last_displayed_line = s->first_displayed_line +
3951 (nprinted - 1);
3952 else
3953 s->last_displayed_line = s->first_displayed_line;
3955 view_border(view);
3957 if (s->eof) {
3958 while (nprinted < view->nlines) {
3959 waddch(view->window, '\n');
3960 nprinted++;
3963 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3964 view->ncols, 0, 0);
3965 if (err) {
3966 return err;
3969 wstandout(view->window);
3970 waddwstr(view->window, wline);
3971 free(wline);
3972 wline = NULL;
3973 wstandend(view->window);
3976 return NULL;
3979 static char *
3980 get_datestr(time_t *time, char *datebuf)
3982 struct tm mytm, *tm;
3983 char *p, *s;
3985 tm = gmtime_r(time, &mytm);
3986 if (tm == NULL)
3987 return NULL;
3988 s = asctime_r(tm, datebuf);
3989 if (s == NULL)
3990 return NULL;
3991 p = strchr(s, '\n');
3992 if (p)
3993 *p = '\0';
3994 return s;
3997 static const struct got_error *
3998 get_changed_paths(struct got_pathlist_head *paths,
3999 struct got_commit_object *commit, struct got_repository *repo)
4001 const struct got_error *err = NULL;
4002 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4003 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4004 struct got_object_qid *qid;
4006 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4007 if (qid != NULL) {
4008 struct got_commit_object *pcommit;
4009 err = got_object_open_as_commit(&pcommit, repo,
4010 &qid->id);
4011 if (err)
4012 return err;
4014 tree_id1 = got_object_id_dup(
4015 got_object_commit_get_tree_id(pcommit));
4016 if (tree_id1 == NULL) {
4017 got_object_commit_close(pcommit);
4018 return got_error_from_errno("got_object_id_dup");
4020 got_object_commit_close(pcommit);
4024 if (tree_id1) {
4025 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4026 if (err)
4027 goto done;
4030 tree_id2 = got_object_commit_get_tree_id(commit);
4031 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4032 if (err)
4033 goto done;
4035 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4036 got_diff_tree_collect_changed_paths, paths, 0);
4037 done:
4038 if (tree1)
4039 got_object_tree_close(tree1);
4040 if (tree2)
4041 got_object_tree_close(tree2);
4042 free(tree_id1);
4043 return err;
4046 static const struct got_error *
4047 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4049 off_t *p;
4051 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4052 if (p == NULL)
4053 return got_error_from_errno("reallocarray");
4054 *line_offsets = p;
4055 (*line_offsets)[*nlines] = off;
4056 (*nlines)++;
4057 return NULL;
4060 static const struct got_error *
4061 write_commit_info(off_t **line_offsets, size_t *nlines,
4062 struct got_object_id *commit_id, struct got_reflist_head *refs,
4063 struct got_repository *repo, FILE *outfile)
4065 const struct got_error *err = NULL;
4066 char datebuf[26], *datestr;
4067 struct got_commit_object *commit;
4068 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4069 time_t committer_time;
4070 const char *author, *committer;
4071 char *refs_str = NULL;
4072 struct got_pathlist_head changed_paths;
4073 struct got_pathlist_entry *pe;
4074 off_t outoff = 0;
4075 int n;
4077 TAILQ_INIT(&changed_paths);
4079 if (refs) {
4080 err = build_refs_str(&refs_str, refs, commit_id, repo);
4081 if (err)
4082 return err;
4085 err = got_object_open_as_commit(&commit, repo, commit_id);
4086 if (err)
4087 return err;
4089 err = got_object_id_str(&id_str, commit_id);
4090 if (err) {
4091 err = got_error_from_errno("got_object_id_str");
4092 goto done;
4095 err = add_line_offset(line_offsets, nlines, 0);
4096 if (err)
4097 goto done;
4099 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4100 refs_str ? refs_str : "", refs_str ? ")" : "");
4101 if (n < 0) {
4102 err = got_error_from_errno("fprintf");
4103 goto done;
4105 outoff += n;
4106 err = add_line_offset(line_offsets, nlines, outoff);
4107 if (err)
4108 goto done;
4110 n = fprintf(outfile, "from: %s\n",
4111 got_object_commit_get_author(commit));
4112 if (n < 0) {
4113 err = got_error_from_errno("fprintf");
4114 goto done;
4116 outoff += n;
4117 err = add_line_offset(line_offsets, nlines, outoff);
4118 if (err)
4119 goto done;
4121 committer_time = got_object_commit_get_committer_time(commit);
4122 datestr = get_datestr(&committer_time, datebuf);
4123 if (datestr) {
4124 n = fprintf(outfile, "date: %s UTC\n", datestr);
4125 if (n < 0) {
4126 err = got_error_from_errno("fprintf");
4127 goto done;
4129 outoff += n;
4130 err = add_line_offset(line_offsets, nlines, outoff);
4131 if (err)
4132 goto done;
4134 author = got_object_commit_get_author(commit);
4135 committer = got_object_commit_get_committer(commit);
4136 if (strcmp(author, committer) != 0) {
4137 n = fprintf(outfile, "via: %s\n", committer);
4138 if (n < 0) {
4139 err = got_error_from_errno("fprintf");
4140 goto done;
4142 outoff += n;
4143 err = add_line_offset(line_offsets, nlines, outoff);
4144 if (err)
4145 goto done;
4147 if (got_object_commit_get_nparents(commit) > 1) {
4148 const struct got_object_id_queue *parent_ids;
4149 struct got_object_qid *qid;
4150 int pn = 1;
4151 parent_ids = got_object_commit_get_parent_ids(commit);
4152 STAILQ_FOREACH(qid, parent_ids, entry) {
4153 err = got_object_id_str(&id_str, &qid->id);
4154 if (err)
4155 goto done;
4156 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4157 if (n < 0) {
4158 err = got_error_from_errno("fprintf");
4159 goto done;
4161 outoff += n;
4162 err = add_line_offset(line_offsets, nlines, outoff);
4163 if (err)
4164 goto done;
4165 free(id_str);
4166 id_str = NULL;
4170 err = got_object_commit_get_logmsg(&logmsg, commit);
4171 if (err)
4172 goto done;
4173 s = logmsg;
4174 while ((line = strsep(&s, "\n")) != NULL) {
4175 n = fprintf(outfile, "%s\n", line);
4176 if (n < 0) {
4177 err = got_error_from_errno("fprintf");
4178 goto done;
4180 outoff += n;
4181 err = add_line_offset(line_offsets, nlines, outoff);
4182 if (err)
4183 goto done;
4186 err = get_changed_paths(&changed_paths, commit, repo);
4187 if (err)
4188 goto done;
4189 TAILQ_FOREACH(pe, &changed_paths, entry) {
4190 struct got_diff_changed_path *cp = pe->data;
4191 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4192 if (n < 0) {
4193 err = got_error_from_errno("fprintf");
4194 goto done;
4196 outoff += n;
4197 err = add_line_offset(line_offsets, nlines, outoff);
4198 if (err)
4199 goto done;
4200 free((char *)pe->path);
4201 free(pe->data);
4204 fputc('\n', outfile);
4205 outoff++;
4206 err = add_line_offset(line_offsets, nlines, outoff);
4207 done:
4208 got_pathlist_free(&changed_paths);
4209 free(id_str);
4210 free(logmsg);
4211 free(refs_str);
4212 got_object_commit_close(commit);
4213 if (err) {
4214 free(*line_offsets);
4215 *line_offsets = NULL;
4216 *nlines = 0;
4218 return err;
4221 static const struct got_error *
4222 create_diff(struct tog_diff_view_state *s)
4224 const struct got_error *err = NULL;
4225 FILE *f = NULL;
4226 int obj_type;
4228 free(s->line_offsets);
4229 s->line_offsets = malloc(sizeof(off_t));
4230 if (s->line_offsets == NULL)
4231 return got_error_from_errno("malloc");
4232 s->nlines = 0;
4234 f = got_opentemp();
4235 if (f == NULL) {
4236 err = got_error_from_errno("got_opentemp");
4237 goto done;
4239 if (s->f && fclose(s->f) == EOF) {
4240 err = got_error_from_errno("fclose");
4241 goto done;
4243 s->f = f;
4245 if (s->id1)
4246 err = got_object_get_type(&obj_type, s->repo, s->id1);
4247 else
4248 err = got_object_get_type(&obj_type, s->repo, s->id2);
4249 if (err)
4250 goto done;
4252 switch (obj_type) {
4253 case GOT_OBJ_TYPE_BLOB:
4254 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4255 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4256 s->label1, s->label2, tog_diff_algo, s->diff_context,
4257 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4258 break;
4259 case GOT_OBJ_TYPE_TREE:
4260 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4261 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4262 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4263 s->force_text_diff, s->repo, s->f);
4264 break;
4265 case GOT_OBJ_TYPE_COMMIT: {
4266 const struct got_object_id_queue *parent_ids;
4267 struct got_object_qid *pid;
4268 struct got_commit_object *commit2;
4269 struct got_reflist_head *refs;
4271 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4272 if (err)
4273 goto done;
4274 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4275 /* Show commit info if we're diffing to a parent/root commit. */
4276 if (s->id1 == NULL) {
4277 err = write_commit_info(&s->line_offsets, &s->nlines,
4278 s->id2, refs, s->repo, s->f);
4279 if (err)
4280 goto done;
4281 } else {
4282 parent_ids = got_object_commit_get_parent_ids(commit2);
4283 STAILQ_FOREACH(pid, parent_ids, entry) {
4284 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4285 err = write_commit_info(
4286 &s->line_offsets, &s->nlines,
4287 s->id2, refs, s->repo, s->f);
4288 if (err)
4289 goto done;
4290 break;
4294 got_object_commit_close(commit2);
4296 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4297 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4298 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4299 s->force_text_diff, s->repo, s->f);
4300 break;
4302 default:
4303 err = got_error(GOT_ERR_OBJ_TYPE);
4304 break;
4306 if (err)
4307 goto done;
4308 done:
4309 if (s->f && fflush(s->f) != 0 && err == NULL)
4310 err = got_error_from_errno("fflush");
4311 return err;
4314 static void
4315 diff_view_indicate_progress(struct tog_view *view)
4317 mvwaddstr(view->window, 0, 0, "diffing...");
4318 update_panels();
4319 doupdate();
4322 static const struct got_error *
4323 search_start_diff_view(struct tog_view *view)
4325 struct tog_diff_view_state *s = &view->state.diff;
4327 s->matched_line = 0;
4328 return NULL;
4331 static const struct got_error *
4332 search_next_diff_view(struct tog_view *view)
4334 struct tog_diff_view_state *s = &view->state.diff;
4335 const struct got_error *err = NULL;
4336 int lineno;
4337 char *line = NULL;
4338 size_t linesize = 0;
4339 ssize_t linelen;
4341 if (!view->searching) {
4342 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4343 return NULL;
4346 if (s->matched_line) {
4347 if (view->searching == TOG_SEARCH_FORWARD)
4348 lineno = s->matched_line + 1;
4349 else
4350 lineno = s->matched_line - 1;
4351 } else
4352 lineno = s->first_displayed_line;
4354 while (1) {
4355 off_t offset;
4357 if (lineno <= 0 || lineno > s->nlines) {
4358 if (s->matched_line == 0) {
4359 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4360 break;
4363 if (view->searching == TOG_SEARCH_FORWARD)
4364 lineno = 1;
4365 else
4366 lineno = s->nlines;
4369 offset = s->line_offsets[lineno - 1];
4370 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4371 free(line);
4372 return got_error_from_errno("fseeko");
4374 linelen = getline(&line, &linesize, s->f);
4375 if (linelen != -1) {
4376 char *exstr;
4377 err = expand_tab(&exstr, line);
4378 if (err)
4379 break;
4380 if (match_line(exstr, &view->regex, 1,
4381 &view->regmatch)) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 s->matched_line = lineno;
4384 free(exstr);
4385 break;
4387 free(exstr);
4389 if (view->searching == TOG_SEARCH_FORWARD)
4390 lineno++;
4391 else
4392 lineno--;
4394 free(line);
4396 if (s->matched_line) {
4397 s->first_displayed_line = s->matched_line;
4398 s->selected_line = 1;
4401 return err;
4404 static const struct got_error *
4405 close_diff_view(struct tog_view *view)
4407 const struct got_error *err = NULL;
4408 struct tog_diff_view_state *s = &view->state.diff;
4410 free(s->id1);
4411 s->id1 = NULL;
4412 free(s->id2);
4413 s->id2 = NULL;
4414 if (s->f && fclose(s->f) == EOF)
4415 err = got_error_from_errno("fclose");
4416 s->f = NULL;
4417 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4418 err = got_error_from_errno("fclose");
4419 s->f1 = NULL;
4420 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4421 err = got_error_from_errno("fclose");
4422 s->f2 = NULL;
4423 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4424 err = got_error_from_errno("close");
4425 s->fd1 = -1;
4426 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4427 err = got_error_from_errno("close");
4428 s->fd2 = -1;
4429 free_colors(&s->colors);
4430 free(s->line_offsets);
4431 s->line_offsets = NULL;
4432 s->nlines = 0;
4433 return err;
4436 static const struct got_error *
4437 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4438 struct got_object_id *id2, const char *label1, const char *label2,
4439 int diff_context, int ignore_whitespace, int force_text_diff,
4440 struct tog_view *parent_view, struct got_repository *repo)
4442 const struct got_error *err;
4443 struct tog_diff_view_state *s = &view->state.diff;
4445 memset(s, 0, sizeof(*s));
4446 s->fd1 = -1;
4447 s->fd2 = -1;
4449 if (id1 != NULL && id2 != NULL) {
4450 int type1, type2;
4451 err = got_object_get_type(&type1, repo, id1);
4452 if (err)
4453 return err;
4454 err = got_object_get_type(&type2, repo, id2);
4455 if (err)
4456 return err;
4458 if (type1 != type2)
4459 return got_error(GOT_ERR_OBJ_TYPE);
4461 s->first_displayed_line = 1;
4462 s->last_displayed_line = view->nlines;
4463 s->selected_line = 1;
4464 s->repo = repo;
4465 s->id1 = id1;
4466 s->id2 = id2;
4467 s->label1 = label1;
4468 s->label2 = label2;
4470 if (id1) {
4471 s->id1 = got_object_id_dup(id1);
4472 if (s->id1 == NULL)
4473 return got_error_from_errno("got_object_id_dup");
4474 } else
4475 s->id1 = NULL;
4477 s->id2 = got_object_id_dup(id2);
4478 if (s->id2 == NULL) {
4479 err = got_error_from_errno("got_object_id_dup");
4480 goto done;
4483 s->f1 = got_opentemp();
4484 if (s->f1 == NULL) {
4485 err = got_error_from_errno("got_opentemp");
4486 goto done;
4489 s->f2 = got_opentemp();
4490 if (s->f2 == NULL) {
4491 err = got_error_from_errno("got_opentemp");
4492 goto done;
4495 s->fd1 = got_opentempfd();
4496 if (s->fd1 == -1) {
4497 err = got_error_from_errno("got_opentempfd");
4498 goto done;
4501 s->fd2 = got_opentempfd();
4502 if (s->fd2 == -1) {
4503 err = got_error_from_errno("got_opentempfd");
4504 goto done;
4507 s->first_displayed_line = 1;
4508 s->last_displayed_line = view->nlines;
4509 s->diff_context = diff_context;
4510 s->ignore_whitespace = ignore_whitespace;
4511 s->force_text_diff = force_text_diff;
4512 s->parent_view = parent_view;
4513 s->repo = repo;
4515 STAILQ_INIT(&s->colors);
4516 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4517 err = add_color(&s->colors,
4518 "^-", TOG_COLOR_DIFF_MINUS,
4519 get_color_value("TOG_COLOR_DIFF_MINUS"));
4520 if (err)
4521 goto done;
4522 err = add_color(&s->colors, "^\\+",
4523 TOG_COLOR_DIFF_PLUS,
4524 get_color_value("TOG_COLOR_DIFF_PLUS"));
4525 if (err)
4526 goto done;
4527 err = add_color(&s->colors,
4528 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4529 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4530 if (err)
4531 goto done;
4533 err = add_color(&s->colors,
4534 "^(commit [0-9a-f]|parent [0-9]|"
4535 "(blob|file|tree|commit) [-+] |"
4536 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4537 get_color_value("TOG_COLOR_DIFF_META"));
4538 if (err)
4539 goto done;
4541 err = add_color(&s->colors,
4542 "^(from|via): ", TOG_COLOR_AUTHOR,
4543 get_color_value("TOG_COLOR_AUTHOR"));
4544 if (err)
4545 goto done;
4547 err = add_color(&s->colors,
4548 "^date: ", TOG_COLOR_DATE,
4549 get_color_value("TOG_COLOR_DATE"));
4550 if (err)
4551 goto done;
4554 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4555 view_is_splitscreen(view))
4556 show_log_view(parent_view); /* draw border */
4557 diff_view_indicate_progress(view);
4559 err = create_diff(s);
4561 view->show = show_diff_view;
4562 view->input = input_diff_view;
4563 view->reset = reset_diff_view;
4564 view->close = close_diff_view;
4565 view->search_start = search_start_diff_view;
4566 view->search_next = search_next_diff_view;
4567 done:
4568 if (err)
4569 close_diff_view(view);
4570 return err;
4573 static const struct got_error *
4574 show_diff_view(struct tog_view *view)
4576 const struct got_error *err;
4577 struct tog_diff_view_state *s = &view->state.diff;
4578 char *id_str1 = NULL, *id_str2, *header;
4579 const char *label1, *label2;
4581 if (s->id1) {
4582 err = got_object_id_str(&id_str1, s->id1);
4583 if (err)
4584 return err;
4585 label1 = s->label1 ? : id_str1;
4586 } else
4587 label1 = "/dev/null";
4589 err = got_object_id_str(&id_str2, s->id2);
4590 if (err)
4591 return err;
4592 label2 = s->label2 ? : id_str2;
4594 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4595 err = got_error_from_errno("asprintf");
4596 free(id_str1);
4597 free(id_str2);
4598 return err;
4600 free(id_str1);
4601 free(id_str2);
4603 err = draw_file(view, header);
4604 free(header);
4605 return err;
4608 static const struct got_error *
4609 set_selected_commit(struct tog_diff_view_state *s,
4610 struct commit_queue_entry *entry)
4612 const struct got_error *err;
4613 const struct got_object_id_queue *parent_ids;
4614 struct got_commit_object *selected_commit;
4615 struct got_object_qid *pid;
4617 free(s->id2);
4618 s->id2 = got_object_id_dup(entry->id);
4619 if (s->id2 == NULL)
4620 return got_error_from_errno("got_object_id_dup");
4622 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4623 if (err)
4624 return err;
4625 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4626 free(s->id1);
4627 pid = STAILQ_FIRST(parent_ids);
4628 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4629 got_object_commit_close(selected_commit);
4630 return NULL;
4633 static const struct got_error *
4634 reset_diff_view(struct tog_view *view)
4636 struct tog_diff_view_state *s = &view->state.diff;
4638 view->count = 0;
4639 wclear(view->window);
4640 s->first_displayed_line = 1;
4641 s->last_displayed_line = view->nlines;
4642 s->matched_line = 0;
4643 diff_view_indicate_progress(view);
4644 return create_diff(s);
4647 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4648 int, int, int);
4649 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4650 int, int);
4652 static const struct got_error *
4653 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4655 const struct got_error *err = NULL;
4656 struct tog_diff_view_state *s = &view->state.diff;
4657 struct tog_log_view_state *ls;
4658 struct commit_queue_entry *old_selected_entry;
4659 char *line = NULL;
4660 size_t linesize = 0;
4661 ssize_t linelen;
4662 int i, nscroll = view->nlines - 1, up = 0;
4664 switch (ch) {
4665 case '0':
4666 view->x = 0;
4667 break;
4668 case '$':
4669 view->x = MAX(view->maxx - view->ncols / 3, 0);
4670 view->count = 0;
4671 break;
4672 case KEY_RIGHT:
4673 case 'l':
4674 if (view->x + view->ncols / 3 < view->maxx)
4675 view->x += 2; /* move two columns right */
4676 else
4677 view->count = 0;
4678 break;
4679 case KEY_LEFT:
4680 case 'h':
4681 view->x -= MIN(view->x, 2); /* move two columns back */
4682 if (view->x <= 0)
4683 view->count = 0;
4684 break;
4685 case 'a':
4686 case 'w':
4687 if (ch == 'a')
4688 s->force_text_diff = !s->force_text_diff;
4689 if (ch == 'w')
4690 s->ignore_whitespace = !s->ignore_whitespace;
4691 err = reset_diff_view(view);
4692 break;
4693 case 'g':
4694 case KEY_HOME:
4695 s->first_displayed_line = 1;
4696 view->count = 0;
4697 break;
4698 case 'G':
4699 case KEY_END:
4700 view->count = 0;
4701 if (s->eof)
4702 break;
4704 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4705 s->eof = 1;
4706 break;
4707 case 'k':
4708 case KEY_UP:
4709 case CTRL('p'):
4710 if (s->first_displayed_line > 1)
4711 s->first_displayed_line--;
4712 else
4713 view->count = 0;
4714 break;
4715 case CTRL('u'):
4716 case 'u':
4717 nscroll /= 2;
4718 /* FALL THROUGH */
4719 case KEY_PPAGE:
4720 case CTRL('b'):
4721 case 'b':
4722 if (s->first_displayed_line == 1) {
4723 view->count = 0;
4724 break;
4726 i = 0;
4727 while (i++ < nscroll && s->first_displayed_line > 1)
4728 s->first_displayed_line--;
4729 break;
4730 case 'j':
4731 case KEY_DOWN:
4732 case CTRL('n'):
4733 if (!s->eof)
4734 s->first_displayed_line++;
4735 else
4736 view->count = 0;
4737 break;
4738 case CTRL('d'):
4739 case 'd':
4740 nscroll /= 2;
4741 /* FALL THROUGH */
4742 case KEY_NPAGE:
4743 case CTRL('f'):
4744 case 'f':
4745 case ' ':
4746 if (s->eof) {
4747 view->count = 0;
4748 break;
4750 i = 0;
4751 while (!s->eof && i++ < nscroll) {
4752 linelen = getline(&line, &linesize, s->f);
4753 s->first_displayed_line++;
4754 if (linelen == -1) {
4755 if (feof(s->f)) {
4756 s->eof = 1;
4757 } else
4758 err = got_ferror(s->f, GOT_ERR_IO);
4759 break;
4762 free(line);
4763 break;
4764 case '[':
4765 if (s->diff_context > 0) {
4766 s->diff_context--;
4767 s->matched_line = 0;
4768 diff_view_indicate_progress(view);
4769 err = create_diff(s);
4770 if (s->first_displayed_line + view->nlines - 1 >
4771 s->nlines) {
4772 s->first_displayed_line = 1;
4773 s->last_displayed_line = view->nlines;
4775 } else
4776 view->count = 0;
4777 break;
4778 case ']':
4779 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4780 s->diff_context++;
4781 s->matched_line = 0;
4782 diff_view_indicate_progress(view);
4783 err = create_diff(s);
4784 } else
4785 view->count = 0;
4786 break;
4787 case '<':
4788 case ',':
4789 case 'K':
4790 up = 1;
4791 /* FALL THROUGH */
4792 case '>':
4793 case '.':
4794 case 'J':
4795 if (s->parent_view == NULL) {
4796 view->count = 0;
4797 break;
4799 s->parent_view->count = view->count;
4801 if (s->parent_view->type == TOG_VIEW_LOG) {
4802 ls = &s->parent_view->state.log;
4803 old_selected_entry = ls->selected_entry;
4805 err = input_log_view(NULL, s->parent_view,
4806 up ? KEY_UP : KEY_DOWN);
4807 if (err)
4808 break;
4809 view->count = s->parent_view->count;
4811 if (old_selected_entry == ls->selected_entry)
4812 break;
4814 err = set_selected_commit(s, ls->selected_entry);
4815 if (err)
4816 break;
4817 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4818 struct tog_blame_view_state *bs;
4819 struct got_object_id *id, *prev_id;
4821 bs = &s->parent_view->state.blame;
4822 prev_id = get_annotation_for_line(bs->blame.lines,
4823 bs->blame.nlines, bs->last_diffed_line);
4825 err = input_blame_view(&view, s->parent_view,
4826 up ? KEY_UP : KEY_DOWN);
4827 if (err)
4828 break;
4829 view->count = s->parent_view->count;
4831 if (prev_id == NULL)
4832 break;
4833 id = get_selected_commit_id(bs->blame.lines,
4834 bs->blame.nlines, bs->first_displayed_line,
4835 bs->selected_line);
4836 if (id == NULL)
4837 break;
4839 if (!got_object_id_cmp(prev_id, id))
4840 break;
4842 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4843 if (err)
4844 break;
4846 s->first_displayed_line = 1;
4847 s->last_displayed_line = view->nlines;
4848 s->matched_line = 0;
4849 view->x = 0;
4851 diff_view_indicate_progress(view);
4852 err = create_diff(s);
4853 break;
4854 default:
4855 view->count = 0;
4856 break;
4859 return err;
4862 static const struct got_error *
4863 cmd_diff(int argc, char *argv[])
4865 const struct got_error *error = NULL;
4866 struct got_repository *repo = NULL;
4867 struct got_worktree *worktree = NULL;
4868 struct got_object_id *id1 = NULL, *id2 = NULL;
4869 char *repo_path = NULL, *cwd = NULL;
4870 char *id_str1 = NULL, *id_str2 = NULL;
4871 char *label1 = NULL, *label2 = NULL;
4872 int diff_context = 3, ignore_whitespace = 0;
4873 int ch, force_text_diff = 0;
4874 const char *errstr;
4875 struct tog_view *view;
4876 int *pack_fds = NULL;
4878 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4879 switch (ch) {
4880 case 'a':
4881 force_text_diff = 1;
4882 break;
4883 case 'C':
4884 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4885 &errstr);
4886 if (errstr != NULL)
4887 errx(1, "number of context lines is %s: %s",
4888 errstr, errstr);
4889 break;
4890 case 'r':
4891 repo_path = realpath(optarg, NULL);
4892 if (repo_path == NULL)
4893 return got_error_from_errno2("realpath",
4894 optarg);
4895 got_path_strip_trailing_slashes(repo_path);
4896 break;
4897 case 'w':
4898 ignore_whitespace = 1;
4899 break;
4900 default:
4901 usage_diff();
4902 /* NOTREACHED */
4906 argc -= optind;
4907 argv += optind;
4909 if (argc == 0) {
4910 usage_diff(); /* TODO show local worktree changes */
4911 } else if (argc == 2) {
4912 id_str1 = argv[0];
4913 id_str2 = argv[1];
4914 } else
4915 usage_diff();
4917 error = got_repo_pack_fds_open(&pack_fds);
4918 if (error)
4919 goto done;
4921 if (repo_path == NULL) {
4922 cwd = getcwd(NULL, 0);
4923 if (cwd == NULL)
4924 return got_error_from_errno("getcwd");
4925 error = got_worktree_open(&worktree, cwd);
4926 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4927 goto done;
4928 if (worktree)
4929 repo_path =
4930 strdup(got_worktree_get_repo_path(worktree));
4931 else
4932 repo_path = strdup(cwd);
4933 if (repo_path == NULL) {
4934 error = got_error_from_errno("strdup");
4935 goto done;
4939 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4940 if (error)
4941 goto done;
4943 init_curses();
4945 error = apply_unveil(got_repo_get_path(repo), NULL);
4946 if (error)
4947 goto done;
4949 error = tog_load_refs(repo, 0);
4950 if (error)
4951 goto done;
4953 error = got_repo_match_object_id(&id1, &label1, id_str1,
4954 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4955 if (error)
4956 goto done;
4958 error = got_repo_match_object_id(&id2, &label2, id_str2,
4959 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4960 if (error)
4961 goto done;
4963 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4964 if (view == NULL) {
4965 error = got_error_from_errno("view_open");
4966 goto done;
4968 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4969 ignore_whitespace, force_text_diff, NULL, repo);
4970 if (error)
4971 goto done;
4972 error = view_loop(view);
4973 done:
4974 free(label1);
4975 free(label2);
4976 free(repo_path);
4977 free(cwd);
4978 if (repo) {
4979 const struct got_error *close_err = got_repo_close(repo);
4980 if (error == NULL)
4981 error = close_err;
4983 if (worktree)
4984 got_worktree_close(worktree);
4985 if (pack_fds) {
4986 const struct got_error *pack_err =
4987 got_repo_pack_fds_close(pack_fds);
4988 if (error == NULL)
4989 error = pack_err;
4991 tog_free_refs();
4992 return error;
4995 __dead static void
4996 usage_blame(void)
4998 endwin();
4999 fprintf(stderr,
5000 "usage: %s blame [-c commit] [-r repository-path] path\n",
5001 getprogname());
5002 exit(1);
5005 struct tog_blame_line {
5006 int annotated;
5007 struct got_object_id *id;
5010 static const struct got_error *
5011 draw_blame(struct tog_view *view)
5013 struct tog_blame_view_state *s = &view->state.blame;
5014 struct tog_blame *blame = &s->blame;
5015 regmatch_t *regmatch = &view->regmatch;
5016 const struct got_error *err;
5017 int lineno = 0, nprinted = 0;
5018 char *line = NULL;
5019 size_t linesize = 0;
5020 ssize_t linelen;
5021 wchar_t *wline;
5022 int width;
5023 struct tog_blame_line *blame_line;
5024 struct got_object_id *prev_id = NULL;
5025 char *id_str;
5026 struct tog_color *tc;
5028 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5029 if (err)
5030 return err;
5032 rewind(blame->f);
5033 werase(view->window);
5035 if (asprintf(&line, "commit %s", id_str) == -1) {
5036 err = got_error_from_errno("asprintf");
5037 free(id_str);
5038 return err;
5041 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5042 free(line);
5043 line = NULL;
5044 if (err)
5045 return err;
5046 if (view_needs_focus_indication(view))
5047 wstandout(view->window);
5048 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5049 if (tc)
5050 wattr_on(view->window,
5051 COLOR_PAIR(tc->colorpair), NULL);
5052 waddwstr(view->window, wline);
5053 if (tc)
5054 wattr_off(view->window,
5055 COLOR_PAIR(tc->colorpair), NULL);
5056 if (view_needs_focus_indication(view))
5057 wstandend(view->window);
5058 free(wline);
5059 wline = NULL;
5060 if (width < view->ncols - 1)
5061 waddch(view->window, '\n');
5063 if (asprintf(&line, "[%d/%d] %s%s",
5064 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5065 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5066 free(id_str);
5067 return got_error_from_errno("asprintf");
5069 free(id_str);
5070 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5071 free(line);
5072 line = NULL;
5073 if (err)
5074 return err;
5075 waddwstr(view->window, wline);
5076 free(wline);
5077 wline = NULL;
5078 if (width < view->ncols - 1)
5079 waddch(view->window, '\n');
5081 s->eof = 0;
5082 view->maxx = 0;
5083 while (nprinted < view->nlines - 2) {
5084 linelen = getline(&line, &linesize, blame->f);
5085 if (linelen == -1) {
5086 if (feof(blame->f)) {
5087 s->eof = 1;
5088 break;
5090 free(line);
5091 return got_ferror(blame->f, GOT_ERR_IO);
5093 if (++lineno < s->first_displayed_line)
5094 continue;
5096 /* Set view->maxx based on full line length. */
5097 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5098 if (err) {
5099 free(line);
5100 return err;
5102 free(wline);
5103 wline = NULL;
5104 view->maxx = MAX(view->maxx, width);
5106 if (nprinted == s->selected_line - 1)
5107 wstandout(view->window);
5109 if (blame->nlines > 0) {
5110 blame_line = &blame->lines[lineno - 1];
5111 if (blame_line->annotated && prev_id &&
5112 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5113 !(nprinted == s->selected_line - 1)) {
5114 waddstr(view->window, " ");
5115 } else if (blame_line->annotated) {
5116 char *id_str;
5117 err = got_object_id_str(&id_str,
5118 blame_line->id);
5119 if (err) {
5120 free(line);
5121 return err;
5123 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5124 if (tc)
5125 wattr_on(view->window,
5126 COLOR_PAIR(tc->colorpair), NULL);
5127 wprintw(view->window, "%.8s", id_str);
5128 if (tc)
5129 wattr_off(view->window,
5130 COLOR_PAIR(tc->colorpair), NULL);
5131 free(id_str);
5132 prev_id = blame_line->id;
5133 } else {
5134 waddstr(view->window, "........");
5135 prev_id = NULL;
5137 } else {
5138 waddstr(view->window, "........");
5139 prev_id = NULL;
5142 if (nprinted == s->selected_line - 1)
5143 wstandend(view->window);
5144 waddstr(view->window, " ");
5146 if (view->ncols <= 9) {
5147 width = 9;
5148 } else if (s->first_displayed_line + nprinted ==
5149 s->matched_line &&
5150 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5151 err = add_matched_line(&width, line, view->ncols - 9, 9,
5152 view->window, view->x, regmatch);
5153 if (err) {
5154 free(line);
5155 return err;
5157 width += 9;
5158 } else {
5159 int skip;
5160 err = format_line(&wline, &width, &skip, line,
5161 view->x, view->ncols - 9, 9, 1);
5162 if (err) {
5163 free(line);
5164 return err;
5166 waddwstr(view->window, &wline[skip]);
5167 width += 9;
5168 free(wline);
5169 wline = NULL;
5172 if (width <= view->ncols - 1)
5173 waddch(view->window, '\n');
5174 if (++nprinted == 1)
5175 s->first_displayed_line = lineno;
5177 free(line);
5178 s->last_displayed_line = lineno;
5180 view_border(view);
5182 return NULL;
5185 static const struct got_error *
5186 blame_cb(void *arg, int nlines, int lineno,
5187 struct got_commit_object *commit, struct got_object_id *id)
5189 const struct got_error *err = NULL;
5190 struct tog_blame_cb_args *a = arg;
5191 struct tog_blame_line *line;
5192 int errcode;
5194 if (nlines != a->nlines ||
5195 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5196 return got_error(GOT_ERR_RANGE);
5198 errcode = pthread_mutex_lock(&tog_mutex);
5199 if (errcode)
5200 return got_error_set_errno(errcode, "pthread_mutex_lock");
5202 if (*a->quit) { /* user has quit the blame view */
5203 err = got_error(GOT_ERR_ITER_COMPLETED);
5204 goto done;
5207 if (lineno == -1)
5208 goto done; /* no change in this commit */
5210 line = &a->lines[lineno - 1];
5211 if (line->annotated)
5212 goto done;
5214 line->id = got_object_id_dup(id);
5215 if (line->id == NULL) {
5216 err = got_error_from_errno("got_object_id_dup");
5217 goto done;
5219 line->annotated = 1;
5220 done:
5221 errcode = pthread_mutex_unlock(&tog_mutex);
5222 if (errcode)
5223 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5224 return err;
5227 static void *
5228 blame_thread(void *arg)
5230 const struct got_error *err, *close_err;
5231 struct tog_blame_thread_args *ta = arg;
5232 struct tog_blame_cb_args *a = ta->cb_args;
5233 int errcode, fd1 = -1, fd2 = -1;
5234 FILE *f1 = NULL, *f2 = NULL;
5236 fd1 = got_opentempfd();
5237 if (fd1 == -1)
5238 return (void *)got_error_from_errno("got_opentempfd");
5240 fd2 = got_opentempfd();
5241 if (fd2 == -1) {
5242 err = got_error_from_errno("got_opentempfd");
5243 goto done;
5246 f1 = got_opentemp();
5247 if (f1 == NULL) {
5248 err = (void *)got_error_from_errno("got_opentemp");
5249 goto done;
5251 f2 = got_opentemp();
5252 if (f2 == NULL) {
5253 err = (void *)got_error_from_errno("got_opentemp");
5254 goto done;
5257 err = block_signals_used_by_main_thread();
5258 if (err)
5259 goto done;
5261 err = got_blame(ta->path, a->commit_id, ta->repo,
5262 tog_diff_algo, blame_cb, ta->cb_args,
5263 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5264 if (err && err->code == GOT_ERR_CANCELLED)
5265 err = NULL;
5267 errcode = pthread_mutex_lock(&tog_mutex);
5268 if (errcode) {
5269 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5270 goto done;
5273 close_err = got_repo_close(ta->repo);
5274 if (err == NULL)
5275 err = close_err;
5276 ta->repo = NULL;
5277 *ta->complete = 1;
5279 errcode = pthread_mutex_unlock(&tog_mutex);
5280 if (errcode && err == NULL)
5281 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5283 done:
5284 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5285 err = got_error_from_errno("close");
5286 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5287 err = got_error_from_errno("close");
5288 if (f1 && fclose(f1) == EOF && err == NULL)
5289 err = got_error_from_errno("fclose");
5290 if (f2 && fclose(f2) == EOF && err == NULL)
5291 err = got_error_from_errno("fclose");
5293 return (void *)err;
5296 static struct got_object_id *
5297 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5298 int first_displayed_line, int selected_line)
5300 struct tog_blame_line *line;
5302 if (nlines <= 0)
5303 return NULL;
5305 line = &lines[first_displayed_line - 1 + selected_line - 1];
5306 if (!line->annotated)
5307 return NULL;
5309 return line->id;
5312 static struct got_object_id *
5313 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5314 int lineno)
5316 struct tog_blame_line *line;
5318 if (nlines <= 0 || lineno >= nlines)
5319 return NULL;
5321 line = &lines[lineno - 1];
5322 if (!line->annotated)
5323 return NULL;
5325 return line->id;
5328 static const struct got_error *
5329 stop_blame(struct tog_blame *blame)
5331 const struct got_error *err = NULL;
5332 int i;
5334 if (blame->thread) {
5335 int errcode;
5336 errcode = pthread_mutex_unlock(&tog_mutex);
5337 if (errcode)
5338 return got_error_set_errno(errcode,
5339 "pthread_mutex_unlock");
5340 errcode = pthread_join(blame->thread, (void **)&err);
5341 if (errcode)
5342 return got_error_set_errno(errcode, "pthread_join");
5343 errcode = pthread_mutex_lock(&tog_mutex);
5344 if (errcode)
5345 return got_error_set_errno(errcode,
5346 "pthread_mutex_lock");
5347 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5348 err = NULL;
5349 blame->thread = 0; //NULL;
5351 if (blame->thread_args.repo) {
5352 const struct got_error *close_err;
5353 close_err = got_repo_close(blame->thread_args.repo);
5354 if (err == NULL)
5355 err = close_err;
5356 blame->thread_args.repo = NULL;
5358 if (blame->f) {
5359 if (fclose(blame->f) == EOF && err == NULL)
5360 err = got_error_from_errno("fclose");
5361 blame->f = NULL;
5363 if (blame->lines) {
5364 for (i = 0; i < blame->nlines; i++)
5365 free(blame->lines[i].id);
5366 free(blame->lines);
5367 blame->lines = NULL;
5369 free(blame->cb_args.commit_id);
5370 blame->cb_args.commit_id = NULL;
5371 if (blame->pack_fds) {
5372 const struct got_error *pack_err =
5373 got_repo_pack_fds_close(blame->pack_fds);
5374 if (err == NULL)
5375 err = pack_err;
5376 blame->pack_fds = NULL;
5378 return err;
5381 static const struct got_error *
5382 cancel_blame_view(void *arg)
5384 const struct got_error *err = NULL;
5385 int *done = arg;
5386 int errcode;
5388 errcode = pthread_mutex_lock(&tog_mutex);
5389 if (errcode)
5390 return got_error_set_errno(errcode,
5391 "pthread_mutex_unlock");
5393 if (*done)
5394 err = got_error(GOT_ERR_CANCELLED);
5396 errcode = pthread_mutex_unlock(&tog_mutex);
5397 if (errcode)
5398 return got_error_set_errno(errcode,
5399 "pthread_mutex_lock");
5401 return err;
5404 static const struct got_error *
5405 run_blame(struct tog_view *view)
5407 struct tog_blame_view_state *s = &view->state.blame;
5408 struct tog_blame *blame = &s->blame;
5409 const struct got_error *err = NULL;
5410 struct got_commit_object *commit = NULL;
5411 struct got_blob_object *blob = NULL;
5412 struct got_repository *thread_repo = NULL;
5413 struct got_object_id *obj_id = NULL;
5414 int obj_type, fd = -1;
5415 int *pack_fds = NULL;
5417 err = got_object_open_as_commit(&commit, s->repo,
5418 &s->blamed_commit->id);
5419 if (err)
5420 return err;
5422 fd = got_opentempfd();
5423 if (fd == -1) {
5424 err = got_error_from_errno("got_opentempfd");
5425 goto done;
5428 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5429 if (err)
5430 goto done;
5432 err = got_object_get_type(&obj_type, s->repo, obj_id);
5433 if (err)
5434 goto done;
5436 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5437 err = got_error(GOT_ERR_OBJ_TYPE);
5438 goto done;
5441 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5442 if (err)
5443 goto done;
5444 blame->f = got_opentemp();
5445 if (blame->f == NULL) {
5446 err = got_error_from_errno("got_opentemp");
5447 goto done;
5449 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5450 &blame->line_offsets, blame->f, blob);
5451 if (err)
5452 goto done;
5453 if (blame->nlines == 0) {
5454 s->blame_complete = 1;
5455 goto done;
5458 /* Don't include \n at EOF in the blame line count. */
5459 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5460 blame->nlines--;
5462 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5463 if (blame->lines == NULL) {
5464 err = got_error_from_errno("calloc");
5465 goto done;
5468 err = got_repo_pack_fds_open(&pack_fds);
5469 if (err)
5470 goto done;
5471 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5472 pack_fds);
5473 if (err)
5474 goto done;
5476 blame->pack_fds = pack_fds;
5477 blame->cb_args.view = view;
5478 blame->cb_args.lines = blame->lines;
5479 blame->cb_args.nlines = blame->nlines;
5480 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5481 if (blame->cb_args.commit_id == NULL) {
5482 err = got_error_from_errno("got_object_id_dup");
5483 goto done;
5485 blame->cb_args.quit = &s->done;
5487 blame->thread_args.path = s->path;
5488 blame->thread_args.repo = thread_repo;
5489 blame->thread_args.cb_args = &blame->cb_args;
5490 blame->thread_args.complete = &s->blame_complete;
5491 blame->thread_args.cancel_cb = cancel_blame_view;
5492 blame->thread_args.cancel_arg = &s->done;
5493 s->blame_complete = 0;
5495 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5496 s->first_displayed_line = 1;
5497 s->last_displayed_line = view->nlines;
5498 s->selected_line = 1;
5500 s->matched_line = 0;
5502 done:
5503 if (commit)
5504 got_object_commit_close(commit);
5505 if (fd != -1 && close(fd) == -1 && err == NULL)
5506 err = got_error_from_errno("close");
5507 if (blob)
5508 got_object_blob_close(blob);
5509 free(obj_id);
5510 if (err)
5511 stop_blame(blame);
5512 return err;
5515 static const struct got_error *
5516 open_blame_view(struct tog_view *view, char *path,
5517 struct got_object_id *commit_id, struct got_repository *repo)
5519 const struct got_error *err = NULL;
5520 struct tog_blame_view_state *s = &view->state.blame;
5522 STAILQ_INIT(&s->blamed_commits);
5524 s->path = strdup(path);
5525 if (s->path == NULL)
5526 return got_error_from_errno("strdup");
5528 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5529 if (err) {
5530 free(s->path);
5531 return err;
5534 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5535 s->first_displayed_line = 1;
5536 s->last_displayed_line = view->nlines;
5537 s->selected_line = 1;
5538 s->blame_complete = 0;
5539 s->repo = repo;
5540 s->commit_id = commit_id;
5541 memset(&s->blame, 0, sizeof(s->blame));
5543 STAILQ_INIT(&s->colors);
5544 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5545 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5546 get_color_value("TOG_COLOR_COMMIT"));
5547 if (err)
5548 return err;
5551 view->show = show_blame_view;
5552 view->input = input_blame_view;
5553 view->reset = reset_blame_view;
5554 view->close = close_blame_view;
5555 view->search_start = search_start_blame_view;
5556 view->search_next = search_next_blame_view;
5558 return run_blame(view);
5561 static const struct got_error *
5562 close_blame_view(struct tog_view *view)
5564 const struct got_error *err = NULL;
5565 struct tog_blame_view_state *s = &view->state.blame;
5567 if (s->blame.thread)
5568 err = stop_blame(&s->blame);
5570 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5571 struct got_object_qid *blamed_commit;
5572 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5573 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5574 got_object_qid_free(blamed_commit);
5577 free(s->path);
5578 free_colors(&s->colors);
5579 return err;
5582 static const struct got_error *
5583 search_start_blame_view(struct tog_view *view)
5585 struct tog_blame_view_state *s = &view->state.blame;
5587 s->matched_line = 0;
5588 return NULL;
5591 static const struct got_error *
5592 search_next_blame_view(struct tog_view *view)
5594 struct tog_blame_view_state *s = &view->state.blame;
5595 const struct got_error *err = NULL;
5596 int lineno;
5597 char *line = NULL;
5598 size_t linesize = 0;
5599 ssize_t linelen;
5601 if (!view->searching) {
5602 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5603 return NULL;
5606 if (s->matched_line) {
5607 if (view->searching == TOG_SEARCH_FORWARD)
5608 lineno = s->matched_line + 1;
5609 else
5610 lineno = s->matched_line - 1;
5611 } else
5612 lineno = s->first_displayed_line - 1 + s->selected_line;
5614 while (1) {
5615 off_t offset;
5617 if (lineno <= 0 || lineno > s->blame.nlines) {
5618 if (s->matched_line == 0) {
5619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5620 break;
5623 if (view->searching == TOG_SEARCH_FORWARD)
5624 lineno = 1;
5625 else
5626 lineno = s->blame.nlines;
5629 offset = s->blame.line_offsets[lineno - 1];
5630 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5631 free(line);
5632 return got_error_from_errno("fseeko");
5634 linelen = getline(&line, &linesize, s->blame.f);
5635 if (linelen != -1) {
5636 char *exstr;
5637 err = expand_tab(&exstr, line);
5638 if (err)
5639 break;
5640 if (match_line(exstr, &view->regex, 1,
5641 &view->regmatch)) {
5642 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 s->matched_line = lineno;
5644 free(exstr);
5645 break;
5647 free(exstr);
5649 if (view->searching == TOG_SEARCH_FORWARD)
5650 lineno++;
5651 else
5652 lineno--;
5654 free(line);
5656 if (s->matched_line) {
5657 s->first_displayed_line = s->matched_line;
5658 s->selected_line = 1;
5661 return err;
5664 static const struct got_error *
5665 show_blame_view(struct tog_view *view)
5667 const struct got_error *err = NULL;
5668 struct tog_blame_view_state *s = &view->state.blame;
5669 int errcode;
5671 if (s->blame.thread == 0 && !s->blame_complete) {
5672 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5673 &s->blame.thread_args);
5674 if (errcode)
5675 return got_error_set_errno(errcode, "pthread_create");
5677 halfdelay(1); /* fast refresh while annotating */
5680 if (s->blame_complete)
5681 halfdelay(10); /* disable fast refresh */
5683 err = draw_blame(view);
5685 view_border(view);
5686 return err;
5689 static const struct got_error *
5690 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5691 struct got_repository *repo, struct got_object_id *id)
5693 struct tog_view *log_view;
5694 const struct got_error *err = NULL;
5696 *new_view = NULL;
5698 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5699 if (log_view == NULL)
5700 return got_error_from_errno("view_open");
5702 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5703 if (err)
5704 view_close(log_view);
5705 else
5706 *new_view = log_view;
5708 return err;
5711 static const struct got_error *
5712 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5714 const struct got_error *err = NULL, *thread_err = NULL;
5715 struct tog_view *diff_view;
5716 struct tog_blame_view_state *s = &view->state.blame;
5717 int eos, nscroll, begin_y = 0, begin_x = 0;
5719 eos = nscroll = view->nlines - 2;
5720 if (view_is_hsplit_top(view))
5721 --eos; /* border */
5723 switch (ch) {
5724 case '0':
5725 view->x = 0;
5726 break;
5727 case '$':
5728 view->x = MAX(view->maxx - view->ncols / 3, 0);
5729 view->count = 0;
5730 break;
5731 case KEY_RIGHT:
5732 case 'l':
5733 if (view->x + view->ncols / 3 < view->maxx)
5734 view->x += 2; /* move two columns right */
5735 else
5736 view->count = 0;
5737 break;
5738 case KEY_LEFT:
5739 case 'h':
5740 view->x -= MIN(view->x, 2); /* move two columns back */
5741 if (view->x <= 0)
5742 view->count = 0;
5743 break;
5744 case 'q':
5745 s->done = 1;
5746 break;
5747 case 'g':
5748 case KEY_HOME:
5749 s->selected_line = 1;
5750 s->first_displayed_line = 1;
5751 view->count = 0;
5752 break;
5753 case 'G':
5754 case KEY_END:
5755 if (s->blame.nlines < eos) {
5756 s->selected_line = s->blame.nlines;
5757 s->first_displayed_line = 1;
5758 } else {
5759 s->selected_line = eos;
5760 s->first_displayed_line = s->blame.nlines - (eos - 1);
5762 view->count = 0;
5763 break;
5764 case 'k':
5765 case KEY_UP:
5766 case CTRL('p'):
5767 if (s->selected_line > 1)
5768 s->selected_line--;
5769 else if (s->selected_line == 1 &&
5770 s->first_displayed_line > 1)
5771 s->first_displayed_line--;
5772 else
5773 view->count = 0;
5774 break;
5775 case CTRL('u'):
5776 case 'u':
5777 nscroll /= 2;
5778 /* FALL THROUGH */
5779 case KEY_PPAGE:
5780 case CTRL('b'):
5781 case 'b':
5782 if (s->first_displayed_line == 1) {
5783 if (view->count > 1)
5784 nscroll += nscroll;
5785 s->selected_line = MAX(1, s->selected_line - nscroll);
5786 view->count = 0;
5787 break;
5789 if (s->first_displayed_line > nscroll)
5790 s->first_displayed_line -= nscroll;
5791 else
5792 s->first_displayed_line = 1;
5793 break;
5794 case 'j':
5795 case KEY_DOWN:
5796 case CTRL('n'):
5797 if (s->selected_line < eos && s->first_displayed_line +
5798 s->selected_line <= s->blame.nlines)
5799 s->selected_line++;
5800 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5801 s->first_displayed_line++;
5802 else
5803 view->count = 0;
5804 break;
5805 case 'c':
5806 case 'p': {
5807 struct got_object_id *id = NULL;
5809 view->count = 0;
5810 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5811 s->first_displayed_line, s->selected_line);
5812 if (id == NULL)
5813 break;
5814 if (ch == 'p') {
5815 struct got_commit_object *commit, *pcommit;
5816 struct got_object_qid *pid;
5817 struct got_object_id *blob_id = NULL;
5818 int obj_type;
5819 err = got_object_open_as_commit(&commit,
5820 s->repo, id);
5821 if (err)
5822 break;
5823 pid = STAILQ_FIRST(
5824 got_object_commit_get_parent_ids(commit));
5825 if (pid == NULL) {
5826 got_object_commit_close(commit);
5827 break;
5829 /* Check if path history ends here. */
5830 err = got_object_open_as_commit(&pcommit,
5831 s->repo, &pid->id);
5832 if (err)
5833 break;
5834 err = got_object_id_by_path(&blob_id, s->repo,
5835 pcommit, s->path);
5836 got_object_commit_close(pcommit);
5837 if (err) {
5838 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5839 err = NULL;
5840 got_object_commit_close(commit);
5841 break;
5843 err = got_object_get_type(&obj_type, s->repo,
5844 blob_id);
5845 free(blob_id);
5846 /* Can't blame non-blob type objects. */
5847 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5848 got_object_commit_close(commit);
5849 break;
5851 err = got_object_qid_alloc(&s->blamed_commit,
5852 &pid->id);
5853 got_object_commit_close(commit);
5854 } else {
5855 if (got_object_id_cmp(id,
5856 &s->blamed_commit->id) == 0)
5857 break;
5858 err = got_object_qid_alloc(&s->blamed_commit,
5859 id);
5861 if (err)
5862 break;
5863 s->done = 1;
5864 thread_err = stop_blame(&s->blame);
5865 s->done = 0;
5866 if (thread_err)
5867 break;
5868 STAILQ_INSERT_HEAD(&s->blamed_commits,
5869 s->blamed_commit, entry);
5870 err = run_blame(view);
5871 if (err)
5872 break;
5873 break;
5875 case 'C': {
5876 struct got_object_qid *first;
5878 view->count = 0;
5879 first = STAILQ_FIRST(&s->blamed_commits);
5880 if (!got_object_id_cmp(&first->id, s->commit_id))
5881 break;
5882 s->done = 1;
5883 thread_err = stop_blame(&s->blame);
5884 s->done = 0;
5885 if (thread_err)
5886 break;
5887 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5888 got_object_qid_free(s->blamed_commit);
5889 s->blamed_commit =
5890 STAILQ_FIRST(&s->blamed_commits);
5891 err = run_blame(view);
5892 if (err)
5893 break;
5894 break;
5896 case 'L':
5897 view->count = 0;
5898 s->id_to_log = get_selected_commit_id(s->blame.lines,
5899 s->blame.nlines, s->first_displayed_line, s->selected_line);
5900 if (s->id_to_log)
5901 err = view_request_new(new_view, view, TOG_VIEW_LOG);
5902 break;
5903 case KEY_ENTER:
5904 case '\r': {
5905 struct got_object_id *id = NULL;
5906 struct got_object_qid *pid;
5907 struct got_commit_object *commit = NULL;
5909 view->count = 0;
5910 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5911 s->first_displayed_line, s->selected_line);
5912 if (id == NULL)
5913 break;
5914 err = got_object_open_as_commit(&commit, s->repo, id);
5915 if (err)
5916 break;
5917 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5918 if (*new_view) {
5919 /* traversed from diff view, release diff resources */
5920 err = close_diff_view(*new_view);
5921 if (err)
5922 break;
5923 diff_view = *new_view;
5924 } else {
5925 if (view_is_parent_view(view))
5926 view_get_split(view, &begin_y, &begin_x);
5928 diff_view = view_open(0, 0, begin_y, begin_x,
5929 TOG_VIEW_DIFF);
5930 if (diff_view == NULL) {
5931 got_object_commit_close(commit);
5932 err = got_error_from_errno("view_open");
5933 break;
5936 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5937 id, NULL, NULL, 3, 0, 0, view, s->repo);
5938 got_object_commit_close(commit);
5939 if (err) {
5940 view_close(diff_view);
5941 break;
5943 s->last_diffed_line = s->first_displayed_line - 1 +
5944 s->selected_line;
5945 if (*new_view)
5946 break; /* still open from active diff view */
5947 if (view_is_parent_view(view) &&
5948 view->mode == TOG_VIEW_SPLIT_HRZN) {
5949 err = view_init_hsplit(view, begin_y);
5950 if (err)
5951 break;
5954 view->focussed = 0;
5955 diff_view->focussed = 1;
5956 diff_view->mode = view->mode;
5957 diff_view->nlines = view->lines - begin_y;
5958 if (view_is_parent_view(view)) {
5959 view_transfer_size(diff_view, view);
5960 err = view_close_child(view);
5961 if (err)
5962 break;
5963 err = view_set_child(view, diff_view);
5964 if (err)
5965 break;
5966 view->focus_child = 1;
5967 } else
5968 *new_view = diff_view;
5969 if (err)
5970 break;
5971 break;
5973 case CTRL('d'):
5974 case 'd':
5975 nscroll /= 2;
5976 /* FALL THROUGH */
5977 case KEY_NPAGE:
5978 case CTRL('f'):
5979 case 'f':
5980 case ' ':
5981 if (s->last_displayed_line >= s->blame.nlines &&
5982 s->selected_line >= MIN(s->blame.nlines,
5983 view->nlines - 2)) {
5984 view->count = 0;
5985 break;
5987 if (s->last_displayed_line >= s->blame.nlines &&
5988 s->selected_line < view->nlines - 2) {
5989 s->selected_line +=
5990 MIN(nscroll, s->last_displayed_line -
5991 s->first_displayed_line - s->selected_line + 1);
5993 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5994 s->first_displayed_line += nscroll;
5995 else
5996 s->first_displayed_line =
5997 s->blame.nlines - (view->nlines - 3);
5998 break;
5999 case KEY_RESIZE:
6000 if (s->selected_line > view->nlines - 2) {
6001 s->selected_line = MIN(s->blame.nlines,
6002 view->nlines - 2);
6004 break;
6005 default:
6006 view->count = 0;
6007 break;
6009 return thread_err ? thread_err : err;
6012 static const struct got_error *
6013 reset_blame_view(struct tog_view *view)
6015 const struct got_error *err;
6016 struct tog_blame_view_state *s = &view->state.blame;
6018 view->count = 0;
6019 s->done = 1;
6020 err = stop_blame(&s->blame);
6021 s->done = 0;
6022 if (err)
6023 return err;
6024 return run_blame(view);
6027 static const struct got_error *
6028 cmd_blame(int argc, char *argv[])
6030 const struct got_error *error;
6031 struct got_repository *repo = NULL;
6032 struct got_worktree *worktree = NULL;
6033 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6034 char *link_target = NULL;
6035 struct got_object_id *commit_id = NULL;
6036 struct got_commit_object *commit = NULL;
6037 char *commit_id_str = NULL;
6038 int ch;
6039 struct tog_view *view;
6040 int *pack_fds = NULL;
6042 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6043 switch (ch) {
6044 case 'c':
6045 commit_id_str = optarg;
6046 break;
6047 case 'r':
6048 repo_path = realpath(optarg, NULL);
6049 if (repo_path == NULL)
6050 return got_error_from_errno2("realpath",
6051 optarg);
6052 break;
6053 default:
6054 usage_blame();
6055 /* NOTREACHED */
6059 argc -= optind;
6060 argv += optind;
6062 if (argc != 1)
6063 usage_blame();
6065 error = got_repo_pack_fds_open(&pack_fds);
6066 if (error != NULL)
6067 goto done;
6069 if (repo_path == NULL) {
6070 cwd = getcwd(NULL, 0);
6071 if (cwd == NULL)
6072 return got_error_from_errno("getcwd");
6073 error = got_worktree_open(&worktree, cwd);
6074 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6075 goto done;
6076 if (worktree)
6077 repo_path =
6078 strdup(got_worktree_get_repo_path(worktree));
6079 else
6080 repo_path = strdup(cwd);
6081 if (repo_path == NULL) {
6082 error = got_error_from_errno("strdup");
6083 goto done;
6087 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6088 if (error != NULL)
6089 goto done;
6091 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6092 worktree);
6093 if (error)
6094 goto done;
6096 init_curses();
6098 error = apply_unveil(got_repo_get_path(repo), NULL);
6099 if (error)
6100 goto done;
6102 error = tog_load_refs(repo, 0);
6103 if (error)
6104 goto done;
6106 if (commit_id_str == NULL) {
6107 struct got_reference *head_ref;
6108 error = got_ref_open(&head_ref, repo, worktree ?
6109 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6110 if (error != NULL)
6111 goto done;
6112 error = got_ref_resolve(&commit_id, repo, head_ref);
6113 got_ref_close(head_ref);
6114 } else {
6115 error = got_repo_match_object_id(&commit_id, NULL,
6116 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6118 if (error != NULL)
6119 goto done;
6121 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6122 if (view == NULL) {
6123 error = got_error_from_errno("view_open");
6124 goto done;
6127 error = got_object_open_as_commit(&commit, repo, commit_id);
6128 if (error)
6129 goto done;
6131 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6132 commit, repo);
6133 if (error)
6134 goto done;
6136 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6137 commit_id, repo);
6138 if (error)
6139 goto done;
6140 if (worktree) {
6141 /* Release work tree lock. */
6142 got_worktree_close(worktree);
6143 worktree = NULL;
6145 error = view_loop(view);
6146 done:
6147 free(repo_path);
6148 free(in_repo_path);
6149 free(link_target);
6150 free(cwd);
6151 free(commit_id);
6152 if (commit)
6153 got_object_commit_close(commit);
6154 if (worktree)
6155 got_worktree_close(worktree);
6156 if (repo) {
6157 const struct got_error *close_err = got_repo_close(repo);
6158 if (error == NULL)
6159 error = close_err;
6161 if (pack_fds) {
6162 const struct got_error *pack_err =
6163 got_repo_pack_fds_close(pack_fds);
6164 if (error == NULL)
6165 error = pack_err;
6167 tog_free_refs();
6168 return error;
6171 static const struct got_error *
6172 draw_tree_entries(struct tog_view *view, const char *parent_path)
6174 struct tog_tree_view_state *s = &view->state.tree;
6175 const struct got_error *err = NULL;
6176 struct got_tree_entry *te;
6177 wchar_t *wline;
6178 struct tog_color *tc;
6179 int width, n, i, nentries;
6180 int limit = view->nlines;
6182 s->ndisplayed = 0;
6183 if (view_is_hsplit_top(view))
6184 --limit; /* border */
6186 werase(view->window);
6188 if (limit == 0)
6189 return NULL;
6191 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6192 0, 0);
6193 if (err)
6194 return err;
6195 if (view_needs_focus_indication(view))
6196 wstandout(view->window);
6197 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6198 if (tc)
6199 wattr_on(view->window,
6200 COLOR_PAIR(tc->colorpair), NULL);
6201 waddwstr(view->window, wline);
6202 if (tc)
6203 wattr_off(view->window,
6204 COLOR_PAIR(tc->colorpair), NULL);
6205 if (view_needs_focus_indication(view))
6206 wstandend(view->window);
6207 free(wline);
6208 wline = NULL;
6209 if (width < view->ncols - 1)
6210 waddch(view->window, '\n');
6211 if (--limit <= 0)
6212 return NULL;
6213 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6214 0, 0);
6215 if (err)
6216 return err;
6217 waddwstr(view->window, wline);
6218 free(wline);
6219 wline = NULL;
6220 if (width < view->ncols - 1)
6221 waddch(view->window, '\n');
6222 if (--limit <= 0)
6223 return NULL;
6224 waddch(view->window, '\n');
6225 if (--limit <= 0)
6226 return NULL;
6228 if (s->first_displayed_entry == NULL) {
6229 te = got_object_tree_get_first_entry(s->tree);
6230 if (s->selected == 0) {
6231 if (view->focussed)
6232 wstandout(view->window);
6233 s->selected_entry = NULL;
6235 waddstr(view->window, " ..\n"); /* parent directory */
6236 if (s->selected == 0 && view->focussed)
6237 wstandend(view->window);
6238 s->ndisplayed++;
6239 if (--limit <= 0)
6240 return NULL;
6241 n = 1;
6242 } else {
6243 n = 0;
6244 te = s->first_displayed_entry;
6247 nentries = got_object_tree_get_nentries(s->tree);
6248 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6249 char *line = NULL, *id_str = NULL, *link_target = NULL;
6250 const char *modestr = "";
6251 mode_t mode;
6253 te = got_object_tree_get_entry(s->tree, i);
6254 mode = got_tree_entry_get_mode(te);
6256 if (s->show_ids) {
6257 err = got_object_id_str(&id_str,
6258 got_tree_entry_get_id(te));
6259 if (err)
6260 return got_error_from_errno(
6261 "got_object_id_str");
6263 if (got_object_tree_entry_is_submodule(te))
6264 modestr = "$";
6265 else if (S_ISLNK(mode)) {
6266 int i;
6268 err = got_tree_entry_get_symlink_target(&link_target,
6269 te, s->repo);
6270 if (err) {
6271 free(id_str);
6272 return err;
6274 for (i = 0; i < strlen(link_target); i++) {
6275 if (!isprint((unsigned char)link_target[i]))
6276 link_target[i] = '?';
6278 modestr = "@";
6280 else if (S_ISDIR(mode))
6281 modestr = "/";
6282 else if (mode & S_IXUSR)
6283 modestr = "*";
6284 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6285 got_tree_entry_get_name(te), modestr,
6286 link_target ? " -> ": "",
6287 link_target ? link_target : "") == -1) {
6288 free(id_str);
6289 free(link_target);
6290 return got_error_from_errno("asprintf");
6292 free(id_str);
6293 free(link_target);
6294 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6295 0, 0);
6296 if (err) {
6297 free(line);
6298 break;
6300 if (n == s->selected) {
6301 if (view->focussed)
6302 wstandout(view->window);
6303 s->selected_entry = te;
6305 tc = match_color(&s->colors, line);
6306 if (tc)
6307 wattr_on(view->window,
6308 COLOR_PAIR(tc->colorpair), NULL);
6309 waddwstr(view->window, wline);
6310 if (tc)
6311 wattr_off(view->window,
6312 COLOR_PAIR(tc->colorpair), NULL);
6313 if (width < view->ncols - 1)
6314 waddch(view->window, '\n');
6315 if (n == s->selected && view->focussed)
6316 wstandend(view->window);
6317 free(line);
6318 free(wline);
6319 wline = NULL;
6320 n++;
6321 s->ndisplayed++;
6322 s->last_displayed_entry = te;
6323 if (--limit <= 0)
6324 break;
6327 return err;
6330 static void
6331 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6333 struct got_tree_entry *te;
6334 int isroot = s->tree == s->root;
6335 int i = 0;
6337 if (s->first_displayed_entry == NULL)
6338 return;
6340 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6341 while (i++ < maxscroll) {
6342 if (te == NULL) {
6343 if (!isroot)
6344 s->first_displayed_entry = NULL;
6345 break;
6347 s->first_displayed_entry = te;
6348 te = got_tree_entry_get_prev(s->tree, te);
6352 static const struct got_error *
6353 tree_scroll_down(struct tog_view *view, int maxscroll)
6355 struct tog_tree_view_state *s = &view->state.tree;
6356 struct got_tree_entry *next, *last;
6357 int n = 0;
6359 if (s->first_displayed_entry)
6360 next = got_tree_entry_get_next(s->tree,
6361 s->first_displayed_entry);
6362 else
6363 next = got_object_tree_get_first_entry(s->tree);
6365 last = s->last_displayed_entry;
6366 while (next && n++ < maxscroll) {
6367 if (last)
6368 last = got_tree_entry_get_next(s->tree, last);
6369 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6370 s->first_displayed_entry = next;
6371 next = got_tree_entry_get_next(s->tree, next);
6375 return NULL;
6378 static const struct got_error *
6379 tree_entry_path(char **path, struct tog_parent_trees *parents,
6380 struct got_tree_entry *te)
6382 const struct got_error *err = NULL;
6383 struct tog_parent_tree *pt;
6384 size_t len = 2; /* for leading slash and NUL */
6386 TAILQ_FOREACH(pt, parents, entry)
6387 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6388 + 1 /* slash */;
6389 if (te)
6390 len += strlen(got_tree_entry_get_name(te));
6392 *path = calloc(1, len);
6393 if (path == NULL)
6394 return got_error_from_errno("calloc");
6396 (*path)[0] = '/';
6397 pt = TAILQ_LAST(parents, tog_parent_trees);
6398 while (pt) {
6399 const char *name = got_tree_entry_get_name(pt->selected_entry);
6400 if (strlcat(*path, name, len) >= len) {
6401 err = got_error(GOT_ERR_NO_SPACE);
6402 goto done;
6404 if (strlcat(*path, "/", len) >= len) {
6405 err = got_error(GOT_ERR_NO_SPACE);
6406 goto done;
6408 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6410 if (te) {
6411 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6412 err = got_error(GOT_ERR_NO_SPACE);
6413 goto done;
6416 done:
6417 if (err) {
6418 free(*path);
6419 *path = NULL;
6421 return err;
6424 static const struct got_error *
6425 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6426 struct got_tree_entry *te, struct tog_parent_trees *parents,
6427 struct got_object_id *commit_id, struct got_repository *repo)
6429 const struct got_error *err = NULL;
6430 char *path;
6431 struct tog_view *blame_view;
6433 *new_view = NULL;
6435 err = tree_entry_path(&path, parents, te);
6436 if (err)
6437 return err;
6439 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6440 if (blame_view == NULL) {
6441 err = got_error_from_errno("view_open");
6442 goto done;
6445 err = open_blame_view(blame_view, path, commit_id, repo);
6446 if (err) {
6447 if (err->code == GOT_ERR_CANCELLED)
6448 err = NULL;
6449 view_close(blame_view);
6450 } else
6451 *new_view = blame_view;
6452 done:
6453 free(path);
6454 return err;
6457 static const struct got_error *
6458 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6459 struct tog_tree_view_state *s)
6461 struct tog_view *log_view;
6462 const struct got_error *err = NULL;
6463 char *path;
6465 *new_view = NULL;
6467 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6468 if (log_view == NULL)
6469 return got_error_from_errno("view_open");
6471 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6472 if (err)
6473 return err;
6475 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6476 path, 0);
6477 if (err)
6478 view_close(log_view);
6479 else
6480 *new_view = log_view;
6481 free(path);
6482 return err;
6485 static const struct got_error *
6486 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6487 const char *head_ref_name, struct got_repository *repo)
6489 const struct got_error *err = NULL;
6490 char *commit_id_str = NULL;
6491 struct tog_tree_view_state *s = &view->state.tree;
6492 struct got_commit_object *commit = NULL;
6494 TAILQ_INIT(&s->parents);
6495 STAILQ_INIT(&s->colors);
6497 s->commit_id = got_object_id_dup(commit_id);
6498 if (s->commit_id == NULL)
6499 return got_error_from_errno("got_object_id_dup");
6501 err = got_object_open_as_commit(&commit, repo, commit_id);
6502 if (err)
6503 goto done;
6506 * The root is opened here and will be closed when the view is closed.
6507 * Any visited subtrees and their path-wise parents are opened and
6508 * closed on demand.
6510 err = got_object_open_as_tree(&s->root, repo,
6511 got_object_commit_get_tree_id(commit));
6512 if (err)
6513 goto done;
6514 s->tree = s->root;
6516 err = got_object_id_str(&commit_id_str, commit_id);
6517 if (err != NULL)
6518 goto done;
6520 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6521 err = got_error_from_errno("asprintf");
6522 goto done;
6525 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6526 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6527 if (head_ref_name) {
6528 s->head_ref_name = strdup(head_ref_name);
6529 if (s->head_ref_name == NULL) {
6530 err = got_error_from_errno("strdup");
6531 goto done;
6534 s->repo = repo;
6536 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6537 err = add_color(&s->colors, "\\$$",
6538 TOG_COLOR_TREE_SUBMODULE,
6539 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6540 if (err)
6541 goto done;
6542 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6543 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6544 if (err)
6545 goto done;
6546 err = add_color(&s->colors, "/$",
6547 TOG_COLOR_TREE_DIRECTORY,
6548 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6549 if (err)
6550 goto done;
6552 err = add_color(&s->colors, "\\*$",
6553 TOG_COLOR_TREE_EXECUTABLE,
6554 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6555 if (err)
6556 goto done;
6558 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6559 get_color_value("TOG_COLOR_COMMIT"));
6560 if (err)
6561 goto done;
6564 view->show = show_tree_view;
6565 view->input = input_tree_view;
6566 view->close = close_tree_view;
6567 view->search_start = search_start_tree_view;
6568 view->search_next = search_next_tree_view;
6569 done:
6570 free(commit_id_str);
6571 if (commit)
6572 got_object_commit_close(commit);
6573 if (err)
6574 close_tree_view(view);
6575 return err;
6578 static const struct got_error *
6579 close_tree_view(struct tog_view *view)
6581 struct tog_tree_view_state *s = &view->state.tree;
6583 free_colors(&s->colors);
6584 free(s->tree_label);
6585 s->tree_label = NULL;
6586 free(s->commit_id);
6587 s->commit_id = NULL;
6588 free(s->head_ref_name);
6589 s->head_ref_name = NULL;
6590 while (!TAILQ_EMPTY(&s->parents)) {
6591 struct tog_parent_tree *parent;
6592 parent = TAILQ_FIRST(&s->parents);
6593 TAILQ_REMOVE(&s->parents, parent, entry);
6594 if (parent->tree != s->root)
6595 got_object_tree_close(parent->tree);
6596 free(parent);
6599 if (s->tree != NULL && s->tree != s->root)
6600 got_object_tree_close(s->tree);
6601 if (s->root)
6602 got_object_tree_close(s->root);
6603 return NULL;
6606 static const struct got_error *
6607 search_start_tree_view(struct tog_view *view)
6609 struct tog_tree_view_state *s = &view->state.tree;
6611 s->matched_entry = NULL;
6612 return NULL;
6615 static int
6616 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6618 regmatch_t regmatch;
6620 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6621 0) == 0;
6624 static const struct got_error *
6625 search_next_tree_view(struct tog_view *view)
6627 struct tog_tree_view_state *s = &view->state.tree;
6628 struct got_tree_entry *te = NULL;
6630 if (!view->searching) {
6631 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6632 return NULL;
6635 if (s->matched_entry) {
6636 if (view->searching == TOG_SEARCH_FORWARD) {
6637 if (s->selected_entry)
6638 te = got_tree_entry_get_next(s->tree,
6639 s->selected_entry);
6640 else
6641 te = got_object_tree_get_first_entry(s->tree);
6642 } else {
6643 if (s->selected_entry == NULL)
6644 te = got_object_tree_get_last_entry(s->tree);
6645 else
6646 te = got_tree_entry_get_prev(s->tree,
6647 s->selected_entry);
6649 } else {
6650 if (s->selected_entry)
6651 te = s->selected_entry;
6652 else if (view->searching == TOG_SEARCH_FORWARD)
6653 te = got_object_tree_get_first_entry(s->tree);
6654 else
6655 te = got_object_tree_get_last_entry(s->tree);
6658 while (1) {
6659 if (te == NULL) {
6660 if (s->matched_entry == NULL) {
6661 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6662 return NULL;
6664 if (view->searching == TOG_SEARCH_FORWARD)
6665 te = got_object_tree_get_first_entry(s->tree);
6666 else
6667 te = got_object_tree_get_last_entry(s->tree);
6670 if (match_tree_entry(te, &view->regex)) {
6671 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6672 s->matched_entry = te;
6673 break;
6676 if (view->searching == TOG_SEARCH_FORWARD)
6677 te = got_tree_entry_get_next(s->tree, te);
6678 else
6679 te = got_tree_entry_get_prev(s->tree, te);
6682 if (s->matched_entry) {
6683 s->first_displayed_entry = s->matched_entry;
6684 s->selected = 0;
6687 return NULL;
6690 static const struct got_error *
6691 show_tree_view(struct tog_view *view)
6693 const struct got_error *err = NULL;
6694 struct tog_tree_view_state *s = &view->state.tree;
6695 char *parent_path;
6697 err = tree_entry_path(&parent_path, &s->parents, NULL);
6698 if (err)
6699 return err;
6701 err = draw_tree_entries(view, parent_path);
6702 free(parent_path);
6704 view_border(view);
6705 return err;
6708 static const struct got_error *
6709 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6711 const struct got_error *err = NULL;
6712 struct tog_tree_view_state *s = &view->state.tree;
6713 struct got_tree_entry *te;
6714 int n, nscroll = view->nlines - 3;
6716 switch (ch) {
6717 case 'i':
6718 s->show_ids = !s->show_ids;
6719 view->count = 0;
6720 break;
6721 case 'L':
6722 view->count = 0;
6723 if (!s->selected_entry)
6724 break;
6725 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6726 break;
6727 case 'R':
6728 view->count = 0;
6729 err = view_request_new(new_view, view, TOG_VIEW_REF);
6730 break;
6731 case 'g':
6732 case KEY_HOME:
6733 s->selected = 0;
6734 view->count = 0;
6735 if (s->tree == s->root)
6736 s->first_displayed_entry =
6737 got_object_tree_get_first_entry(s->tree);
6738 else
6739 s->first_displayed_entry = NULL;
6740 break;
6741 case 'G':
6742 case KEY_END: {
6743 int eos = view->nlines - 3;
6745 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6746 --eos; /* border */
6747 s->selected = 0;
6748 view->count = 0;
6749 te = got_object_tree_get_last_entry(s->tree);
6750 for (n = 0; n < eos; n++) {
6751 if (te == NULL) {
6752 if (s->tree != s->root) {
6753 s->first_displayed_entry = NULL;
6754 n++;
6756 break;
6758 s->first_displayed_entry = te;
6759 te = got_tree_entry_get_prev(s->tree, te);
6761 if (n > 0)
6762 s->selected = n - 1;
6763 break;
6765 case 'k':
6766 case KEY_UP:
6767 case CTRL('p'):
6768 if (s->selected > 0) {
6769 s->selected--;
6770 break;
6772 tree_scroll_up(s, 1);
6773 if (s->selected_entry == NULL ||
6774 (s->tree == s->root && s->selected_entry ==
6775 got_object_tree_get_first_entry(s->tree)))
6776 view->count = 0;
6777 break;
6778 case CTRL('u'):
6779 case 'u':
6780 nscroll /= 2;
6781 /* FALL THROUGH */
6782 case KEY_PPAGE:
6783 case CTRL('b'):
6784 case 'b':
6785 if (s->tree == s->root) {
6786 if (got_object_tree_get_first_entry(s->tree) ==
6787 s->first_displayed_entry)
6788 s->selected -= MIN(s->selected, nscroll);
6789 } else {
6790 if (s->first_displayed_entry == NULL)
6791 s->selected -= MIN(s->selected, nscroll);
6793 tree_scroll_up(s, MAX(0, nscroll));
6794 if (s->selected_entry == NULL ||
6795 (s->tree == s->root && s->selected_entry ==
6796 got_object_tree_get_first_entry(s->tree)))
6797 view->count = 0;
6798 break;
6799 case 'j':
6800 case KEY_DOWN:
6801 case CTRL('n'):
6802 if (s->selected < s->ndisplayed - 1) {
6803 s->selected++;
6804 break;
6806 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6807 == NULL) {
6808 /* can't scroll any further */
6809 view->count = 0;
6810 break;
6812 tree_scroll_down(view, 1);
6813 break;
6814 case CTRL('d'):
6815 case 'd':
6816 nscroll /= 2;
6817 /* FALL THROUGH */
6818 case KEY_NPAGE:
6819 case CTRL('f'):
6820 case 'f':
6821 case ' ':
6822 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6823 == NULL) {
6824 /* can't scroll any further; move cursor down */
6825 if (s->selected < s->ndisplayed - 1)
6826 s->selected += MIN(nscroll,
6827 s->ndisplayed - s->selected - 1);
6828 else
6829 view->count = 0;
6830 break;
6832 tree_scroll_down(view, nscroll);
6833 break;
6834 case KEY_ENTER:
6835 case '\r':
6836 case KEY_BACKSPACE:
6837 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6838 struct tog_parent_tree *parent;
6839 /* user selected '..' */
6840 if (s->tree == s->root) {
6841 view->count = 0;
6842 break;
6844 parent = TAILQ_FIRST(&s->parents);
6845 TAILQ_REMOVE(&s->parents, parent,
6846 entry);
6847 got_object_tree_close(s->tree);
6848 s->tree = parent->tree;
6849 s->first_displayed_entry =
6850 parent->first_displayed_entry;
6851 s->selected_entry =
6852 parent->selected_entry;
6853 s->selected = parent->selected;
6854 if (s->selected > view->nlines - 3) {
6855 err = offset_selection_down(view);
6856 if (err)
6857 break;
6859 free(parent);
6860 } else if (S_ISDIR(got_tree_entry_get_mode(
6861 s->selected_entry))) {
6862 struct got_tree_object *subtree;
6863 view->count = 0;
6864 err = got_object_open_as_tree(&subtree, s->repo,
6865 got_tree_entry_get_id(s->selected_entry));
6866 if (err)
6867 break;
6868 err = tree_view_visit_subtree(s, subtree);
6869 if (err) {
6870 got_object_tree_close(subtree);
6871 break;
6873 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
6874 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
6875 break;
6876 case KEY_RESIZE:
6877 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6878 s->selected = view->nlines - 4;
6879 view->count = 0;
6880 break;
6881 default:
6882 view->count = 0;
6883 break;
6886 return err;
6889 __dead static void
6890 usage_tree(void)
6892 endwin();
6893 fprintf(stderr,
6894 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6895 getprogname());
6896 exit(1);
6899 static const struct got_error *
6900 cmd_tree(int argc, char *argv[])
6902 const struct got_error *error;
6903 struct got_repository *repo = NULL;
6904 struct got_worktree *worktree = NULL;
6905 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6906 struct got_object_id *commit_id = NULL;
6907 struct got_commit_object *commit = NULL;
6908 const char *commit_id_arg = NULL;
6909 char *label = NULL;
6910 struct got_reference *ref = NULL;
6911 const char *head_ref_name = NULL;
6912 int ch;
6913 struct tog_view *view;
6914 int *pack_fds = NULL;
6916 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6917 switch (ch) {
6918 case 'c':
6919 commit_id_arg = optarg;
6920 break;
6921 case 'r':
6922 repo_path = realpath(optarg, NULL);
6923 if (repo_path == NULL)
6924 return got_error_from_errno2("realpath",
6925 optarg);
6926 break;
6927 default:
6928 usage_tree();
6929 /* NOTREACHED */
6933 argc -= optind;
6934 argv += optind;
6936 if (argc > 1)
6937 usage_tree();
6939 error = got_repo_pack_fds_open(&pack_fds);
6940 if (error != NULL)
6941 goto done;
6943 if (repo_path == NULL) {
6944 cwd = getcwd(NULL, 0);
6945 if (cwd == NULL)
6946 return got_error_from_errno("getcwd");
6947 error = got_worktree_open(&worktree, cwd);
6948 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6949 goto done;
6950 if (worktree)
6951 repo_path =
6952 strdup(got_worktree_get_repo_path(worktree));
6953 else
6954 repo_path = strdup(cwd);
6955 if (repo_path == NULL) {
6956 error = got_error_from_errno("strdup");
6957 goto done;
6961 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6962 if (error != NULL)
6963 goto done;
6965 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6966 repo, worktree);
6967 if (error)
6968 goto done;
6970 init_curses();
6972 error = apply_unveil(got_repo_get_path(repo), NULL);
6973 if (error)
6974 goto done;
6976 error = tog_load_refs(repo, 0);
6977 if (error)
6978 goto done;
6980 if (commit_id_arg == NULL) {
6981 error = got_repo_match_object_id(&commit_id, &label,
6982 worktree ? got_worktree_get_head_ref_name(worktree) :
6983 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6984 if (error)
6985 goto done;
6986 head_ref_name = label;
6987 } else {
6988 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6989 if (error == NULL)
6990 head_ref_name = got_ref_get_name(ref);
6991 else if (error->code != GOT_ERR_NOT_REF)
6992 goto done;
6993 error = got_repo_match_object_id(&commit_id, NULL,
6994 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6995 if (error)
6996 goto done;
6999 error = got_object_open_as_commit(&commit, repo, commit_id);
7000 if (error)
7001 goto done;
7003 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7004 if (view == NULL) {
7005 error = got_error_from_errno("view_open");
7006 goto done;
7008 error = open_tree_view(view, commit_id, head_ref_name, repo);
7009 if (error)
7010 goto done;
7011 if (!got_path_is_root_dir(in_repo_path)) {
7012 error = tree_view_walk_path(&view->state.tree, commit,
7013 in_repo_path);
7014 if (error)
7015 goto done;
7018 if (worktree) {
7019 /* Release work tree lock. */
7020 got_worktree_close(worktree);
7021 worktree = NULL;
7023 error = view_loop(view);
7024 done:
7025 free(repo_path);
7026 free(cwd);
7027 free(commit_id);
7028 free(label);
7029 if (ref)
7030 got_ref_close(ref);
7031 if (repo) {
7032 const struct got_error *close_err = got_repo_close(repo);
7033 if (error == NULL)
7034 error = close_err;
7036 if (pack_fds) {
7037 const struct got_error *pack_err =
7038 got_repo_pack_fds_close(pack_fds);
7039 if (error == NULL)
7040 error = pack_err;
7042 tog_free_refs();
7043 return error;
7046 static const struct got_error *
7047 ref_view_load_refs(struct tog_ref_view_state *s)
7049 struct got_reflist_entry *sre;
7050 struct tog_reflist_entry *re;
7052 s->nrefs = 0;
7053 TAILQ_FOREACH(sre, &tog_refs, entry) {
7054 if (strncmp(got_ref_get_name(sre->ref),
7055 "refs/got/", 9) == 0 &&
7056 strncmp(got_ref_get_name(sre->ref),
7057 "refs/got/backup/", 16) != 0)
7058 continue;
7060 re = malloc(sizeof(*re));
7061 if (re == NULL)
7062 return got_error_from_errno("malloc");
7064 re->ref = got_ref_dup(sre->ref);
7065 if (re->ref == NULL)
7066 return got_error_from_errno("got_ref_dup");
7067 re->idx = s->nrefs++;
7068 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7071 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7072 return NULL;
7075 static void
7076 ref_view_free_refs(struct tog_ref_view_state *s)
7078 struct tog_reflist_entry *re;
7080 while (!TAILQ_EMPTY(&s->refs)) {
7081 re = TAILQ_FIRST(&s->refs);
7082 TAILQ_REMOVE(&s->refs, re, entry);
7083 got_ref_close(re->ref);
7084 free(re);
7088 static const struct got_error *
7089 open_ref_view(struct tog_view *view, struct got_repository *repo)
7091 const struct got_error *err = NULL;
7092 struct tog_ref_view_state *s = &view->state.ref;
7094 s->selected_entry = 0;
7095 s->repo = repo;
7097 TAILQ_INIT(&s->refs);
7098 STAILQ_INIT(&s->colors);
7100 err = ref_view_load_refs(s);
7101 if (err)
7102 return err;
7104 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7105 err = add_color(&s->colors, "^refs/heads/",
7106 TOG_COLOR_REFS_HEADS,
7107 get_color_value("TOG_COLOR_REFS_HEADS"));
7108 if (err)
7109 goto done;
7111 err = add_color(&s->colors, "^refs/tags/",
7112 TOG_COLOR_REFS_TAGS,
7113 get_color_value("TOG_COLOR_REFS_TAGS"));
7114 if (err)
7115 goto done;
7117 err = add_color(&s->colors, "^refs/remotes/",
7118 TOG_COLOR_REFS_REMOTES,
7119 get_color_value("TOG_COLOR_REFS_REMOTES"));
7120 if (err)
7121 goto done;
7123 err = add_color(&s->colors, "^refs/got/backup/",
7124 TOG_COLOR_REFS_BACKUP,
7125 get_color_value("TOG_COLOR_REFS_BACKUP"));
7126 if (err)
7127 goto done;
7130 view->show = show_ref_view;
7131 view->input = input_ref_view;
7132 view->close = close_ref_view;
7133 view->search_start = search_start_ref_view;
7134 view->search_next = search_next_ref_view;
7135 done:
7136 if (err)
7137 free_colors(&s->colors);
7138 return err;
7141 static const struct got_error *
7142 close_ref_view(struct tog_view *view)
7144 struct tog_ref_view_state *s = &view->state.ref;
7146 ref_view_free_refs(s);
7147 free_colors(&s->colors);
7149 return NULL;
7152 static const struct got_error *
7153 resolve_reflist_entry(struct got_object_id **commit_id,
7154 struct tog_reflist_entry *re, struct got_repository *repo)
7156 const struct got_error *err = NULL;
7157 struct got_object_id *obj_id;
7158 struct got_tag_object *tag = NULL;
7159 int obj_type;
7161 *commit_id = NULL;
7163 err = got_ref_resolve(&obj_id, repo, re->ref);
7164 if (err)
7165 return err;
7167 err = got_object_get_type(&obj_type, repo, obj_id);
7168 if (err)
7169 goto done;
7171 switch (obj_type) {
7172 case GOT_OBJ_TYPE_COMMIT:
7173 *commit_id = obj_id;
7174 break;
7175 case GOT_OBJ_TYPE_TAG:
7176 err = got_object_open_as_tag(&tag, repo, obj_id);
7177 if (err)
7178 goto done;
7179 free(obj_id);
7180 err = got_object_get_type(&obj_type, repo,
7181 got_object_tag_get_object_id(tag));
7182 if (err)
7183 goto done;
7184 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7185 err = got_error(GOT_ERR_OBJ_TYPE);
7186 goto done;
7188 *commit_id = got_object_id_dup(
7189 got_object_tag_get_object_id(tag));
7190 if (*commit_id == NULL) {
7191 err = got_error_from_errno("got_object_id_dup");
7192 goto done;
7194 break;
7195 default:
7196 err = got_error(GOT_ERR_OBJ_TYPE);
7197 break;
7200 done:
7201 if (tag)
7202 got_object_tag_close(tag);
7203 if (err) {
7204 free(*commit_id);
7205 *commit_id = NULL;
7207 return err;
7210 static const struct got_error *
7211 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7212 struct tog_reflist_entry *re, struct got_repository *repo)
7214 struct tog_view *log_view;
7215 const struct got_error *err = NULL;
7216 struct got_object_id *commit_id = NULL;
7218 *new_view = NULL;
7220 err = resolve_reflist_entry(&commit_id, re, repo);
7221 if (err) {
7222 if (err->code != GOT_ERR_OBJ_TYPE)
7223 return err;
7224 else
7225 return NULL;
7228 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7229 if (log_view == NULL) {
7230 err = got_error_from_errno("view_open");
7231 goto done;
7234 err = open_log_view(log_view, commit_id, repo,
7235 got_ref_get_name(re->ref), "", 0);
7236 done:
7237 if (err)
7238 view_close(log_view);
7239 else
7240 *new_view = log_view;
7241 free(commit_id);
7242 return err;
7245 static void
7246 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7248 struct tog_reflist_entry *re;
7249 int i = 0;
7251 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7252 return;
7254 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7255 while (i++ < maxscroll) {
7256 if (re == NULL)
7257 break;
7258 s->first_displayed_entry = re;
7259 re = TAILQ_PREV(re, tog_reflist_head, entry);
7263 static const struct got_error *
7264 ref_scroll_down(struct tog_view *view, int maxscroll)
7266 struct tog_ref_view_state *s = &view->state.ref;
7267 struct tog_reflist_entry *next, *last;
7268 int n = 0;
7270 if (s->first_displayed_entry)
7271 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7272 else
7273 next = TAILQ_FIRST(&s->refs);
7275 last = s->last_displayed_entry;
7276 while (next && n++ < maxscroll) {
7277 if (last)
7278 last = TAILQ_NEXT(last, entry);
7279 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7280 s->first_displayed_entry = next;
7281 next = TAILQ_NEXT(next, entry);
7285 return NULL;
7288 static const struct got_error *
7289 search_start_ref_view(struct tog_view *view)
7291 struct tog_ref_view_state *s = &view->state.ref;
7293 s->matched_entry = NULL;
7294 return NULL;
7297 static int
7298 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7300 regmatch_t regmatch;
7302 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7303 0) == 0;
7306 static const struct got_error *
7307 search_next_ref_view(struct tog_view *view)
7309 struct tog_ref_view_state *s = &view->state.ref;
7310 struct tog_reflist_entry *re = NULL;
7312 if (!view->searching) {
7313 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7314 return NULL;
7317 if (s->matched_entry) {
7318 if (view->searching == TOG_SEARCH_FORWARD) {
7319 if (s->selected_entry)
7320 re = TAILQ_NEXT(s->selected_entry, entry);
7321 else
7322 re = TAILQ_PREV(s->selected_entry,
7323 tog_reflist_head, entry);
7324 } else {
7325 if (s->selected_entry == NULL)
7326 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7327 else
7328 re = TAILQ_PREV(s->selected_entry,
7329 tog_reflist_head, entry);
7331 } else {
7332 if (s->selected_entry)
7333 re = s->selected_entry;
7334 else if (view->searching == TOG_SEARCH_FORWARD)
7335 re = TAILQ_FIRST(&s->refs);
7336 else
7337 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7340 while (1) {
7341 if (re == NULL) {
7342 if (s->matched_entry == NULL) {
7343 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7344 return NULL;
7346 if (view->searching == TOG_SEARCH_FORWARD)
7347 re = TAILQ_FIRST(&s->refs);
7348 else
7349 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7352 if (match_reflist_entry(re, &view->regex)) {
7353 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7354 s->matched_entry = re;
7355 break;
7358 if (view->searching == TOG_SEARCH_FORWARD)
7359 re = TAILQ_NEXT(re, entry);
7360 else
7361 re = TAILQ_PREV(re, tog_reflist_head, entry);
7364 if (s->matched_entry) {
7365 s->first_displayed_entry = s->matched_entry;
7366 s->selected = 0;
7369 return NULL;
7372 static const struct got_error *
7373 show_ref_view(struct tog_view *view)
7375 const struct got_error *err = NULL;
7376 struct tog_ref_view_state *s = &view->state.ref;
7377 struct tog_reflist_entry *re;
7378 char *line = NULL;
7379 wchar_t *wline;
7380 struct tog_color *tc;
7381 int width, n;
7382 int limit = view->nlines;
7384 werase(view->window);
7386 s->ndisplayed = 0;
7387 if (view_is_hsplit_top(view))
7388 --limit; /* border */
7390 if (limit == 0)
7391 return NULL;
7393 re = s->first_displayed_entry;
7395 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7396 s->nrefs) == -1)
7397 return got_error_from_errno("asprintf");
7399 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7400 if (err) {
7401 free(line);
7402 return err;
7404 if (view_needs_focus_indication(view))
7405 wstandout(view->window);
7406 waddwstr(view->window, wline);
7407 if (view_needs_focus_indication(view))
7408 wstandend(view->window);
7409 free(wline);
7410 wline = NULL;
7411 free(line);
7412 line = NULL;
7413 if (width < view->ncols - 1)
7414 waddch(view->window, '\n');
7415 if (--limit <= 0)
7416 return NULL;
7418 n = 0;
7419 while (re && limit > 0) {
7420 char *line = NULL;
7421 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7423 if (s->show_date) {
7424 struct got_commit_object *ci;
7425 struct got_tag_object *tag;
7426 struct got_object_id *id;
7427 struct tm tm;
7428 time_t t;
7430 err = got_ref_resolve(&id, s->repo, re->ref);
7431 if (err)
7432 return err;
7433 err = got_object_open_as_tag(&tag, s->repo, id);
7434 if (err) {
7435 if (err->code != GOT_ERR_OBJ_TYPE) {
7436 free(id);
7437 return err;
7439 err = got_object_open_as_commit(&ci, s->repo,
7440 id);
7441 if (err) {
7442 free(id);
7443 return err;
7445 t = got_object_commit_get_committer_time(ci);
7446 got_object_commit_close(ci);
7447 } else {
7448 t = got_object_tag_get_tagger_time(tag);
7449 got_object_tag_close(tag);
7451 free(id);
7452 if (gmtime_r(&t, &tm) == NULL)
7453 return got_error_from_errno("gmtime_r");
7454 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7455 return got_error(GOT_ERR_NO_SPACE);
7457 if (got_ref_is_symbolic(re->ref)) {
7458 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7459 ymd : "", got_ref_get_name(re->ref),
7460 got_ref_get_symref_target(re->ref)) == -1)
7461 return got_error_from_errno("asprintf");
7462 } else if (s->show_ids) {
7463 struct got_object_id *id;
7464 char *id_str;
7465 err = got_ref_resolve(&id, s->repo, re->ref);
7466 if (err)
7467 return err;
7468 err = got_object_id_str(&id_str, id);
7469 if (err) {
7470 free(id);
7471 return err;
7473 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7474 got_ref_get_name(re->ref), id_str) == -1) {
7475 err = got_error_from_errno("asprintf");
7476 free(id);
7477 free(id_str);
7478 return err;
7480 free(id);
7481 free(id_str);
7482 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7483 got_ref_get_name(re->ref)) == -1)
7484 return got_error_from_errno("asprintf");
7486 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7487 0, 0);
7488 if (err) {
7489 free(line);
7490 return err;
7492 if (n == s->selected) {
7493 if (view->focussed)
7494 wstandout(view->window);
7495 s->selected_entry = re;
7497 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7498 if (tc)
7499 wattr_on(view->window,
7500 COLOR_PAIR(tc->colorpair), NULL);
7501 waddwstr(view->window, wline);
7502 if (tc)
7503 wattr_off(view->window,
7504 COLOR_PAIR(tc->colorpair), NULL);
7505 if (width < view->ncols - 1)
7506 waddch(view->window, '\n');
7507 if (n == s->selected && view->focussed)
7508 wstandend(view->window);
7509 free(line);
7510 free(wline);
7511 wline = NULL;
7512 n++;
7513 s->ndisplayed++;
7514 s->last_displayed_entry = re;
7516 limit--;
7517 re = TAILQ_NEXT(re, entry);
7520 view_border(view);
7521 return err;
7524 static const struct got_error *
7525 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7526 struct tog_reflist_entry *re, struct got_repository *repo)
7528 const struct got_error *err = NULL;
7529 struct got_object_id *commit_id = NULL;
7530 struct tog_view *tree_view;
7532 *new_view = NULL;
7534 err = resolve_reflist_entry(&commit_id, re, repo);
7535 if (err) {
7536 if (err->code != GOT_ERR_OBJ_TYPE)
7537 return err;
7538 else
7539 return NULL;
7543 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7544 if (tree_view == NULL) {
7545 err = got_error_from_errno("view_open");
7546 goto done;
7549 err = open_tree_view(tree_view, commit_id,
7550 got_ref_get_name(re->ref), repo);
7551 if (err)
7552 goto done;
7554 *new_view = tree_view;
7555 done:
7556 free(commit_id);
7557 return err;
7559 static const struct got_error *
7560 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7562 const struct got_error *err = NULL;
7563 struct tog_ref_view_state *s = &view->state.ref;
7564 struct tog_reflist_entry *re;
7565 int n, nscroll = view->nlines - 1;
7567 switch (ch) {
7568 case 'i':
7569 s->show_ids = !s->show_ids;
7570 view->count = 0;
7571 break;
7572 case 'm':
7573 s->show_date = !s->show_date;
7574 view->count = 0;
7575 break;
7576 case 'o':
7577 s->sort_by_date = !s->sort_by_date;
7578 view->count = 0;
7579 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7580 got_ref_cmp_by_commit_timestamp_descending :
7581 tog_ref_cmp_by_name, s->repo);
7582 if (err)
7583 break;
7584 got_reflist_object_id_map_free(tog_refs_idmap);
7585 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7586 &tog_refs, s->repo);
7587 if (err)
7588 break;
7589 ref_view_free_refs(s);
7590 err = ref_view_load_refs(s);
7591 break;
7592 case KEY_ENTER:
7593 case '\r':
7594 view->count = 0;
7595 if (!s->selected_entry)
7596 break;
7597 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7598 break;
7599 case 'T':
7600 view->count = 0;
7601 if (!s->selected_entry)
7602 break;
7603 err = view_request_new(new_view, view, TOG_VIEW_TREE);
7604 break;
7605 case 'g':
7606 case KEY_HOME:
7607 s->selected = 0;
7608 view->count = 0;
7609 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7610 break;
7611 case 'G':
7612 case KEY_END: {
7613 int eos = view->nlines - 1;
7615 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7616 --eos; /* border */
7617 s->selected = 0;
7618 view->count = 0;
7619 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7620 for (n = 0; n < eos; n++) {
7621 if (re == NULL)
7622 break;
7623 s->first_displayed_entry = re;
7624 re = TAILQ_PREV(re, tog_reflist_head, entry);
7626 if (n > 0)
7627 s->selected = n - 1;
7628 break;
7630 case 'k':
7631 case KEY_UP:
7632 case CTRL('p'):
7633 if (s->selected > 0) {
7634 s->selected--;
7635 break;
7637 ref_scroll_up(s, 1);
7638 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7639 view->count = 0;
7640 break;
7641 case CTRL('u'):
7642 case 'u':
7643 nscroll /= 2;
7644 /* FALL THROUGH */
7645 case KEY_PPAGE:
7646 case CTRL('b'):
7647 case 'b':
7648 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7649 s->selected -= MIN(nscroll, s->selected);
7650 ref_scroll_up(s, MAX(0, nscroll));
7651 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7652 view->count = 0;
7653 break;
7654 case 'j':
7655 case KEY_DOWN:
7656 case CTRL('n'):
7657 if (s->selected < s->ndisplayed - 1) {
7658 s->selected++;
7659 break;
7661 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7662 /* can't scroll any further */
7663 view->count = 0;
7664 break;
7666 ref_scroll_down(view, 1);
7667 break;
7668 case CTRL('d'):
7669 case 'd':
7670 nscroll /= 2;
7671 /* FALL THROUGH */
7672 case KEY_NPAGE:
7673 case CTRL('f'):
7674 case 'f':
7675 case ' ':
7676 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7677 /* can't scroll any further; move cursor down */
7678 if (s->selected < s->ndisplayed - 1)
7679 s->selected += MIN(nscroll,
7680 s->ndisplayed - s->selected - 1);
7681 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7682 s->selected += s->ndisplayed - s->selected - 1;
7683 view->count = 0;
7684 break;
7686 ref_scroll_down(view, nscroll);
7687 break;
7688 case CTRL('l'):
7689 view->count = 0;
7690 tog_free_refs();
7691 err = tog_load_refs(s->repo, s->sort_by_date);
7692 if (err)
7693 break;
7694 ref_view_free_refs(s);
7695 err = ref_view_load_refs(s);
7696 break;
7697 case KEY_RESIZE:
7698 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7699 s->selected = view->nlines - 2;
7700 break;
7701 default:
7702 view->count = 0;
7703 break;
7706 return err;
7709 __dead static void
7710 usage_ref(void)
7712 endwin();
7713 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7714 getprogname());
7715 exit(1);
7718 static const struct got_error *
7719 cmd_ref(int argc, char *argv[])
7721 const struct got_error *error;
7722 struct got_repository *repo = NULL;
7723 struct got_worktree *worktree = NULL;
7724 char *cwd = NULL, *repo_path = NULL;
7725 int ch;
7726 struct tog_view *view;
7727 int *pack_fds = NULL;
7729 while ((ch = getopt(argc, argv, "r:")) != -1) {
7730 switch (ch) {
7731 case 'r':
7732 repo_path = realpath(optarg, NULL);
7733 if (repo_path == NULL)
7734 return got_error_from_errno2("realpath",
7735 optarg);
7736 break;
7737 default:
7738 usage_ref();
7739 /* NOTREACHED */
7743 argc -= optind;
7744 argv += optind;
7746 if (argc > 1)
7747 usage_ref();
7749 error = got_repo_pack_fds_open(&pack_fds);
7750 if (error != NULL)
7751 goto done;
7753 if (repo_path == NULL) {
7754 cwd = getcwd(NULL, 0);
7755 if (cwd == NULL)
7756 return got_error_from_errno("getcwd");
7757 error = got_worktree_open(&worktree, cwd);
7758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7759 goto done;
7760 if (worktree)
7761 repo_path =
7762 strdup(got_worktree_get_repo_path(worktree));
7763 else
7764 repo_path = strdup(cwd);
7765 if (repo_path == NULL) {
7766 error = got_error_from_errno("strdup");
7767 goto done;
7771 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7772 if (error != NULL)
7773 goto done;
7775 init_curses();
7777 error = apply_unveil(got_repo_get_path(repo), NULL);
7778 if (error)
7779 goto done;
7781 error = tog_load_refs(repo, 0);
7782 if (error)
7783 goto done;
7785 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7786 if (view == NULL) {
7787 error = got_error_from_errno("view_open");
7788 goto done;
7791 error = open_ref_view(view, repo);
7792 if (error)
7793 goto done;
7795 if (worktree) {
7796 /* Release work tree lock. */
7797 got_worktree_close(worktree);
7798 worktree = NULL;
7800 error = view_loop(view);
7801 done:
7802 free(repo_path);
7803 free(cwd);
7804 if (repo) {
7805 const struct got_error *close_err = got_repo_close(repo);
7806 if (close_err)
7807 error = close_err;
7809 if (pack_fds) {
7810 const struct got_error *pack_err =
7811 got_repo_pack_fds_close(pack_fds);
7812 if (error == NULL)
7813 error = pack_err;
7815 tog_free_refs();
7816 return error;
7819 static const struct got_error *
7820 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
7821 enum tog_view_type request, int y, int x)
7823 const struct got_error *err = NULL;
7825 *new_view = NULL;
7827 switch (request) {
7828 case TOG_VIEW_DIFF:
7829 if (view->type == TOG_VIEW_LOG) {
7830 struct tog_log_view_state *s = &view->state.log;
7832 err = open_diff_view_for_commit(new_view, y, x,
7833 s->selected_entry->commit, s->selected_entry->id,
7834 view, s->repo);
7835 } else
7836 return got_error_msg(GOT_ERR_NOT_IMPL,
7837 "parent/child view pair not supported");
7838 break;
7839 case TOG_VIEW_BLAME:
7840 if (view->type == TOG_VIEW_TREE) {
7841 struct tog_tree_view_state *s = &view->state.tree;
7843 err = blame_tree_entry(new_view, y, x,
7844 s->selected_entry, &s->parents, s->commit_id,
7845 s->repo);
7846 } else
7847 return got_error_msg(GOT_ERR_NOT_IMPL,
7848 "parent/child view pair not supported");
7849 break;
7850 case TOG_VIEW_LOG:
7851 if (view->type == TOG_VIEW_BLAME)
7852 err = log_annotated_line(new_view, y, x,
7853 view->state.blame.repo, view->state.blame.id_to_log);
7854 else if (view->type == TOG_VIEW_TREE)
7855 err = log_selected_tree_entry(new_view, y, x,
7856 &view->state.tree);
7857 else if (view->type == TOG_VIEW_REF)
7858 err = log_ref_entry(new_view, y, x,
7859 view->state.ref.selected_entry,
7860 view->state.ref.repo);
7861 else
7862 return got_error_msg(GOT_ERR_NOT_IMPL,
7863 "parent/child view pair not supported");
7864 break;
7865 case TOG_VIEW_TREE:
7866 if (view->type == TOG_VIEW_LOG)
7867 err = browse_commit_tree(new_view, y, x,
7868 view->state.log.selected_entry,
7869 view->state.log.in_repo_path,
7870 view->state.log.head_ref_name,
7871 view->state.log.repo);
7872 else if (view->type == TOG_VIEW_REF)
7873 err = browse_ref_tree(new_view, y, x,
7874 view->state.ref.selected_entry,
7875 view->state.ref.repo);
7876 else
7877 return got_error_msg(GOT_ERR_NOT_IMPL,
7878 "parent/child view pair not supported");
7879 break;
7880 case TOG_VIEW_REF:
7881 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
7882 if (*new_view == NULL)
7883 return got_error_from_errno("view_open");
7884 if (view->type == TOG_VIEW_LOG)
7885 err = open_ref_view(*new_view, view->state.log.repo);
7886 else if (view->type == TOG_VIEW_TREE)
7887 err = open_ref_view(*new_view, view->state.tree.repo);
7888 else
7889 err = got_error_msg(GOT_ERR_NOT_IMPL,
7890 "parent/child view pair not supported");
7891 if (err)
7892 view_close(*new_view);
7893 break;
7894 default:
7895 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
7898 return err;
7902 * If view was scrolled down to move the selected line into view when opening a
7903 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7905 static void
7906 offset_selection_up(struct tog_view *view)
7908 switch (view->type) {
7909 case TOG_VIEW_BLAME: {
7910 struct tog_blame_view_state *s = &view->state.blame;
7911 if (s->first_displayed_line == 1) {
7912 s->selected_line = MAX(s->selected_line - view->offset,
7913 1);
7914 break;
7916 if (s->first_displayed_line > view->offset)
7917 s->first_displayed_line -= view->offset;
7918 else
7919 s->first_displayed_line = 1;
7920 s->selected_line += view->offset;
7921 break;
7923 case TOG_VIEW_LOG:
7924 log_scroll_up(&view->state.log, view->offset);
7925 view->state.log.selected += view->offset;
7926 break;
7927 case TOG_VIEW_REF:
7928 ref_scroll_up(&view->state.ref, view->offset);
7929 view->state.ref.selected += view->offset;
7930 break;
7931 case TOG_VIEW_TREE:
7932 tree_scroll_up(&view->state.tree, view->offset);
7933 view->state.tree.selected += view->offset;
7934 break;
7935 default:
7936 break;
7939 view->offset = 0;
7943 * If the selected line is in the section of screen covered by the bottom split,
7944 * scroll down offset lines to move it into view and index its new position.
7946 static const struct got_error *
7947 offset_selection_down(struct tog_view *view)
7949 const struct got_error *err = NULL;
7950 const struct got_error *(*scrolld)(struct tog_view *, int);
7951 int *selected = NULL;
7952 int header, offset;
7954 switch (view->type) {
7955 case TOG_VIEW_BLAME: {
7956 struct tog_blame_view_state *s = &view->state.blame;
7957 header = 3;
7958 scrolld = NULL;
7959 if (s->selected_line > view->nlines - header) {
7960 offset = abs(view->nlines - s->selected_line - header);
7961 s->first_displayed_line += offset;
7962 s->selected_line -= offset;
7963 view->offset = offset;
7965 break;
7967 case TOG_VIEW_LOG: {
7968 struct tog_log_view_state *s = &view->state.log;
7969 scrolld = &log_scroll_down;
7970 header = view_is_parent_view(view) ? 3 : 2;
7971 selected = &s->selected;
7972 break;
7974 case TOG_VIEW_REF: {
7975 struct tog_ref_view_state *s = &view->state.ref;
7976 scrolld = &ref_scroll_down;
7977 header = 3;
7978 selected = &s->selected;
7979 break;
7981 case TOG_VIEW_TREE: {
7982 struct tog_tree_view_state *s = &view->state.tree;
7983 scrolld = &tree_scroll_down;
7984 header = 5;
7985 selected = &s->selected;
7986 break;
7988 default:
7989 selected = NULL;
7990 scrolld = NULL;
7991 header = 0;
7992 break;
7995 if (selected && *selected > view->nlines - header) {
7996 offset = abs(view->nlines - *selected - header);
7997 view->offset = offset;
7998 if (scrolld && offset) {
7999 err = scrolld(view, offset);
8000 *selected -= offset;
8004 return err;
8007 static void
8008 list_commands(FILE *fp)
8010 size_t i;
8012 fprintf(fp, "commands:");
8013 for (i = 0; i < nitems(tog_commands); i++) {
8014 const struct tog_cmd *cmd = &tog_commands[i];
8015 fprintf(fp, " %s", cmd->name);
8017 fputc('\n', fp);
8020 __dead static void
8021 usage(int hflag, int status)
8023 FILE *fp = (status == 0) ? stdout : stderr;
8025 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8026 getprogname());
8027 if (hflag) {
8028 fprintf(fp, "lazy usage: %s path\n", getprogname());
8029 list_commands(fp);
8031 exit(status);
8034 static char **
8035 make_argv(int argc, ...)
8037 va_list ap;
8038 char **argv;
8039 int i;
8041 va_start(ap, argc);
8043 argv = calloc(argc, sizeof(char *));
8044 if (argv == NULL)
8045 err(1, "calloc");
8046 for (i = 0; i < argc; i++) {
8047 argv[i] = strdup(va_arg(ap, char *));
8048 if (argv[i] == NULL)
8049 err(1, "strdup");
8052 va_end(ap);
8053 return argv;
8057 * Try to convert 'tog path' into a 'tog log path' command.
8058 * The user could simply have mistyped the command rather than knowingly
8059 * provided a path. So check whether argv[0] can in fact be resolved
8060 * to a path in the HEAD commit and print a special error if not.
8061 * This hack is for mpi@ <3
8063 static const struct got_error *
8064 tog_log_with_path(int argc, char *argv[])
8066 const struct got_error *error = NULL, *close_err;
8067 const struct tog_cmd *cmd = NULL;
8068 struct got_repository *repo = NULL;
8069 struct got_worktree *worktree = NULL;
8070 struct got_object_id *commit_id = NULL, *id = NULL;
8071 struct got_commit_object *commit = NULL;
8072 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8073 char *commit_id_str = NULL, **cmd_argv = NULL;
8074 int *pack_fds = NULL;
8076 cwd = getcwd(NULL, 0);
8077 if (cwd == NULL)
8078 return got_error_from_errno("getcwd");
8080 error = got_repo_pack_fds_open(&pack_fds);
8081 if (error != NULL)
8082 goto done;
8084 error = got_worktree_open(&worktree, cwd);
8085 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8086 goto done;
8088 if (worktree)
8089 repo_path = strdup(got_worktree_get_repo_path(worktree));
8090 else
8091 repo_path = strdup(cwd);
8092 if (repo_path == NULL) {
8093 error = got_error_from_errno("strdup");
8094 goto done;
8097 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8098 if (error != NULL)
8099 goto done;
8101 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8102 repo, worktree);
8103 if (error)
8104 goto done;
8106 error = tog_load_refs(repo, 0);
8107 if (error)
8108 goto done;
8109 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8110 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8111 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8112 if (error)
8113 goto done;
8115 if (worktree) {
8116 got_worktree_close(worktree);
8117 worktree = NULL;
8120 error = got_object_open_as_commit(&commit, repo, commit_id);
8121 if (error)
8122 goto done;
8124 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8125 if (error) {
8126 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8127 goto done;
8128 fprintf(stderr, "%s: '%s' is no known command or path\n",
8129 getprogname(), argv[0]);
8130 usage(1, 1);
8131 /* not reached */
8134 close_err = got_repo_close(repo);
8135 if (error == NULL)
8136 error = close_err;
8137 repo = NULL;
8139 error = got_object_id_str(&commit_id_str, commit_id);
8140 if (error)
8141 goto done;
8143 cmd = &tog_commands[0]; /* log */
8144 argc = 4;
8145 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8146 error = cmd->cmd_main(argc, cmd_argv);
8147 done:
8148 if (repo) {
8149 close_err = got_repo_close(repo);
8150 if (error == NULL)
8151 error = close_err;
8153 if (commit)
8154 got_object_commit_close(commit);
8155 if (worktree)
8156 got_worktree_close(worktree);
8157 if (pack_fds) {
8158 const struct got_error *pack_err =
8159 got_repo_pack_fds_close(pack_fds);
8160 if (error == NULL)
8161 error = pack_err;
8163 free(id);
8164 free(commit_id_str);
8165 free(commit_id);
8166 free(cwd);
8167 free(repo_path);
8168 free(in_repo_path);
8169 if (cmd_argv) {
8170 int i;
8171 for (i = 0; i < argc; i++)
8172 free(cmd_argv[i]);
8173 free(cmd_argv);
8175 tog_free_refs();
8176 return error;
8179 int
8180 main(int argc, char *argv[])
8182 const struct got_error *error = NULL;
8183 const struct tog_cmd *cmd = NULL;
8184 int ch, hflag = 0, Vflag = 0;
8185 char **cmd_argv = NULL;
8186 static const struct option longopts[] = {
8187 { "version", no_argument, NULL, 'V' },
8188 { NULL, 0, NULL, 0}
8190 char *diff_algo_str = NULL;
8192 setlocale(LC_CTYPE, "");
8194 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8195 switch (ch) {
8196 case 'h':
8197 hflag = 1;
8198 break;
8199 case 'V':
8200 Vflag = 1;
8201 break;
8202 default:
8203 usage(hflag, 1);
8204 /* NOTREACHED */
8208 argc -= optind;
8209 argv += optind;
8210 optind = 1;
8211 optreset = 1;
8213 if (Vflag) {
8214 got_version_print_str();
8215 return 0;
8218 #ifndef PROFILE
8219 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8220 NULL) == -1)
8221 err(1, "pledge");
8222 #endif
8224 if (argc == 0) {
8225 if (hflag)
8226 usage(hflag, 0);
8227 /* Build an argument vector which runs a default command. */
8228 cmd = &tog_commands[0];
8229 argc = 1;
8230 cmd_argv = make_argv(argc, cmd->name);
8231 } else {
8232 size_t i;
8234 /* Did the user specify a command? */
8235 for (i = 0; i < nitems(tog_commands); i++) {
8236 if (strncmp(tog_commands[i].name, argv[0],
8237 strlen(argv[0])) == 0) {
8238 cmd = &tog_commands[i];
8239 break;
8244 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8245 if (diff_algo_str) {
8246 if (strcasecmp(diff_algo_str, "patience") == 0)
8247 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8248 if (strcasecmp(diff_algo_str, "myers") == 0)
8249 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8252 if (cmd == NULL) {
8253 if (argc != 1)
8254 usage(0, 1);
8255 /* No command specified; try log with a path */
8256 error = tog_log_with_path(argc, argv);
8257 } else {
8258 if (hflag)
8259 cmd->cmd_usage();
8260 else
8261 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8264 endwin();
8265 putchar('\n');
8266 if (cmd_argv) {
8267 int i;
8268 for (i = 0; i < argc; i++)
8269 free(cmd_argv[i]);
8270 free(cmd_argv);
8273 if (error && error->code != GOT_ERR_CANCELLED)
8274 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8275 return 0;