Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 enum tog_view_mode {
109 TOG_VIEW_SPLIT_NONE,
110 TOG_VIEW_SPLIT_VERT,
111 TOG_VIEW_SPLIT_HRZN
112 };
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
122 int idx;
123 };
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
126 int ncommits;
127 struct commit_queue_head head;
128 };
130 struct tog_color {
131 STAILQ_ENTRY(tog_color) entry;
132 regex_t regex;
133 short colorpair;
134 };
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
153 *cmp = -1;
154 return NULL;
155 } else if (isbackup1 && !isbackup2) {
156 *cmp = 1;
157 return NULL;
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 return NULL;
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 repo);
172 if (err)
173 return err;
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 repo);
179 static void
180 tog_free_refs(void)
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
195 int regerr = 0;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 return NULL;
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
203 if (tc == NULL)
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 if (regerr) {
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
211 sizeof(regerr_msg));
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 regerr_msg);
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 free(tc);
216 return err;
218 tc->colorpair = idx;
219 STAILQ_INSERT_HEAD(colors, tc, entry);
220 return NULL;
223 static void
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
231 regfree(&tc->regex);
232 free(tc);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
243 return tc;
246 return NULL;
249 static int
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 return COLOR_CYAN;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 return COLOR_YELLOW;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 return COLOR_GREEN;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 return COLOR_CYAN;
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 return COLOR_GREEN;
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 return COLOR_CYAN;
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 return COLOR_YELLOW;
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 return COLOR_GREEN;
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 return COLOR_YELLOW;
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 return COLOR_CYAN;
283 return -1;
286 static int
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
291 if (val == NULL)
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
295 return COLOR_BLACK;
296 if (strcasecmp(val, "red") == 0)
297 return COLOR_RED;
298 if (strcasecmp(val, "green") == 0)
299 return COLOR_GREEN;
300 if (strcasecmp(val, "yellow") == 0)
301 return COLOR_YELLOW;
302 if (strcasecmp(val, "blue") == 0)
303 return COLOR_BLUE;
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
307 return COLOR_CYAN;
308 if (strcasecmp(val, "white") == 0)
309 return COLOR_WHITE;
310 if (strcasecmp(val, "default") == 0)
311 return -1;
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
320 FILE *f, *f1, *f2;
321 int fd1, fd2;
322 int first_displayed_line;
323 int last_displayed_line;
324 int eof;
325 int diff_context;
326 int ignore_whitespace;
327 int force_text_diff;
328 struct got_repository *repo;
329 struct tog_colors colors;
330 size_t nlines;
331 off_t *line_offsets;
332 int matched_line;
333 int selected_line;
335 /* passed from log or blame view; may be NULL */
336 struct tog_view *parent_view;
337 };
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 static volatile sig_atomic_t tog_thread_error;
342 struct tog_log_thread_args {
343 pthread_cond_t need_commits;
344 pthread_cond_t commit_loaded;
345 int commits_needed;
346 int load_all;
347 struct got_commit_graph *graph;
348 struct commit_queue *commits;
349 const char *in_repo_path;
350 struct got_object_id *start_id;
351 struct got_repository *repo;
352 int *pack_fds;
353 int log_complete;
354 sig_atomic_t *quit;
355 struct commit_queue_entry **first_displayed_entry;
356 struct commit_queue_entry **selected_entry;
357 int *searching;
358 int *search_next_done;
359 regex_t *regex;
360 };
362 struct tog_log_view_state {
363 struct commit_queue commits;
364 struct commit_queue_entry *first_displayed_entry;
365 struct commit_queue_entry *last_displayed_entry;
366 struct commit_queue_entry *selected_entry;
367 int selected;
368 char *in_repo_path;
369 char *head_ref_name;
370 int log_branches;
371 struct got_repository *repo;
372 struct got_object_id *start_id;
373 sig_atomic_t quit;
374 pthread_t thread;
375 struct tog_log_thread_args thread_args;
376 struct commit_queue_entry *matched_entry;
377 struct commit_queue_entry *search_entry;
378 struct tog_colors colors;
379 };
381 #define TOG_COLOR_DIFF_MINUS 1
382 #define TOG_COLOR_DIFF_PLUS 2
383 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
384 #define TOG_COLOR_DIFF_META 4
385 #define TOG_COLOR_TREE_SUBMODULE 5
386 #define TOG_COLOR_TREE_SYMLINK 6
387 #define TOG_COLOR_TREE_DIRECTORY 7
388 #define TOG_COLOR_TREE_EXECUTABLE 8
389 #define TOG_COLOR_COMMIT 9
390 #define TOG_COLOR_AUTHOR 10
391 #define TOG_COLOR_DATE 11
392 #define TOG_COLOR_REFS_HEADS 12
393 #define TOG_COLOR_REFS_TAGS 13
394 #define TOG_COLOR_REFS_REMOTES 14
395 #define TOG_COLOR_REFS_BACKUP 15
397 struct tog_blame_cb_args {
398 struct tog_blame_line *lines; /* one per line */
399 int nlines;
401 struct tog_view *view;
402 struct got_object_id *commit_id;
403 int *quit;
404 };
406 struct tog_blame_thread_args {
407 const char *path;
408 struct got_repository *repo;
409 struct tog_blame_cb_args *cb_args;
410 int *complete;
411 got_cancel_cb cancel_cb;
412 void *cancel_arg;
413 };
415 struct tog_blame {
416 FILE *f;
417 off_t filesize;
418 struct tog_blame_line *lines;
419 int nlines;
420 off_t *line_offsets;
421 pthread_t thread;
422 struct tog_blame_thread_args thread_args;
423 struct tog_blame_cb_args cb_args;
424 const char *path;
425 int *pack_fds;
426 };
428 struct tog_blame_view_state {
429 int first_displayed_line;
430 int last_displayed_line;
431 int selected_line;
432 int last_diffed_line;
433 int blame_complete;
434 int eof;
435 int done;
436 struct got_object_id_queue blamed_commits;
437 struct got_object_qid *blamed_commit;
438 char *path;
439 struct got_repository *repo;
440 struct got_object_id *commit_id;
441 struct tog_blame blame;
442 int matched_line;
443 struct tog_colors colors;
444 };
446 struct tog_parent_tree {
447 TAILQ_ENTRY(tog_parent_tree) entry;
448 struct got_tree_object *tree;
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *selected_entry;
451 int selected;
452 };
454 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
456 struct tog_tree_view_state {
457 char *tree_label;
458 struct got_object_id *commit_id;/* commit which this tree belongs to */
459 struct got_tree_object *root; /* the commit's root tree entry */
460 struct got_tree_object *tree; /* currently displayed (sub-)tree */
461 struct got_tree_entry *first_displayed_entry;
462 struct got_tree_entry *last_displayed_entry;
463 struct got_tree_entry *selected_entry;
464 int ndisplayed, selected, show_ids;
465 struct tog_parent_trees parents; /* parent trees of current sub-tree */
466 char *head_ref_name;
467 struct got_repository *repo;
468 struct got_tree_entry *matched_entry;
469 struct tog_colors colors;
470 };
472 struct tog_reflist_entry {
473 TAILQ_ENTRY(tog_reflist_entry) entry;
474 struct got_reference *ref;
475 int idx;
476 };
478 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
480 struct tog_ref_view_state {
481 struct tog_reflist_head refs;
482 struct tog_reflist_entry *first_displayed_entry;
483 struct tog_reflist_entry *last_displayed_entry;
484 struct tog_reflist_entry *selected_entry;
485 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
486 struct got_repository *repo;
487 struct tog_reflist_entry *matched_entry;
488 struct tog_colors colors;
489 };
491 /*
492 * We implement two types of views: parent views and child views.
494 * The 'Tab' key switches focus between a parent view and its child view.
495 * Child views are shown side-by-side to their parent view, provided
496 * there is enough screen estate.
498 * When a new view is opened from within a parent view, this new view
499 * becomes a child view of the parent view, replacing any existing child.
501 * When a new view is opened from within a child view, this new view
502 * becomes a parent view which will obscure the views below until the
503 * user quits the new parent view by typing 'q'.
505 * This list of views contains parent views only.
506 * Child views are only pointed to by their parent view.
507 */
508 TAILQ_HEAD(tog_view_list_head, tog_view);
510 struct tog_view {
511 TAILQ_ENTRY(tog_view) entry;
512 WINDOW *window;
513 PANEL *panel;
514 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
515 int resized_y, resized_x; /* begin_y/x based on user resizing */
516 int maxx, x; /* max column and current start column */
517 int lines, cols; /* copies of LINES and COLS */
518 int nscrolled, offset; /* lines scrolled and hsplit line offset */
519 int ch, count; /* current keymap and count prefix */
520 int resize; /* set when in a resize event */
521 int focussed; /* Only set on one parent or child view at a time. */
522 int dying;
523 struct tog_view *parent;
524 struct tog_view *child;
526 /*
527 * This flag is initially set on parent views when a new child view
528 * is created. It gets toggled when the 'Tab' key switches focus
529 * between parent and child.
530 * The flag indicates whether focus should be passed on to our child
531 * view if this parent view gets picked for focus after another parent
532 * view was closed. This prevents child views from losing focus in such
533 * situations.
534 */
535 int focus_child;
537 enum tog_view_mode mode;
538 /* type-specific state */
539 enum tog_view_type type;
540 union {
541 struct tog_diff_view_state diff;
542 struct tog_log_view_state log;
543 struct tog_blame_view_state blame;
544 struct tog_tree_view_state tree;
545 struct tog_ref_view_state ref;
546 } state;
548 const struct got_error *(*show)(struct tog_view *);
549 const struct got_error *(*input)(struct tog_view **,
550 struct tog_view *, int);
551 const struct got_error *(*reset)(struct tog_view *);
552 const struct got_error *(*close)(struct tog_view *);
554 const struct got_error *(*search_start)(struct tog_view *);
555 const struct got_error *(*search_next)(struct tog_view *);
556 int search_started;
557 int searching;
558 #define TOG_SEARCH_FORWARD 1
559 #define TOG_SEARCH_BACKWARD 2
560 int search_next_done;
561 #define TOG_SEARCH_HAVE_MORE 1
562 #define TOG_SEARCH_NO_MORE 2
563 #define TOG_SEARCH_HAVE_NONE 3
564 regex_t regex;
565 regmatch_t regmatch;
566 };
568 static const struct got_error *open_diff_view(struct tog_view *,
569 struct got_object_id *, struct got_object_id *,
570 const char *, const char *, int, int, int, struct tog_view *,
571 struct got_repository *);
572 static const struct got_error *show_diff_view(struct tog_view *);
573 static const struct got_error *input_diff_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *reset_diff_view(struct tog_view *);
576 static const struct got_error* close_diff_view(struct tog_view *);
577 static const struct got_error *search_start_diff_view(struct tog_view *);
578 static const struct got_error *search_next_diff_view(struct tog_view *);
580 static const struct got_error *open_log_view(struct tog_view *,
581 struct got_object_id *, struct got_repository *,
582 const char *, const char *, int);
583 static const struct got_error * show_log_view(struct tog_view *);
584 static const struct got_error *input_log_view(struct tog_view **,
585 struct tog_view *, int);
586 static const struct got_error *close_log_view(struct tog_view *);
587 static const struct got_error *search_start_log_view(struct tog_view *);
588 static const struct got_error *search_next_log_view(struct tog_view *);
590 static const struct got_error *open_blame_view(struct tog_view *, char *,
591 struct got_object_id *, struct got_repository *);
592 static const struct got_error *show_blame_view(struct tog_view *);
593 static const struct got_error *input_blame_view(struct tog_view **,
594 struct tog_view *, int);
595 static const struct got_error *reset_blame_view(struct tog_view *);
596 static const struct got_error *close_blame_view(struct tog_view *);
597 static const struct got_error *search_start_blame_view(struct tog_view *);
598 static const struct got_error *search_next_blame_view(struct tog_view *);
600 static const struct got_error *open_tree_view(struct tog_view *,
601 struct got_object_id *, const char *, struct got_repository *);
602 static const struct got_error *show_tree_view(struct tog_view *);
603 static const struct got_error *input_tree_view(struct tog_view **,
604 struct tog_view *, int);
605 static const struct got_error *close_tree_view(struct tog_view *);
606 static const struct got_error *search_start_tree_view(struct tog_view *);
607 static const struct got_error *search_next_tree_view(struct tog_view *);
609 static const struct got_error *open_ref_view(struct tog_view *,
610 struct got_repository *);
611 static const struct got_error *show_ref_view(struct tog_view *);
612 static const struct got_error *input_ref_view(struct tog_view **,
613 struct tog_view *, int);
614 static const struct got_error *close_ref_view(struct tog_view *);
615 static const struct got_error *search_start_ref_view(struct tog_view *);
616 static const struct got_error *search_next_ref_view(struct tog_view *);
618 static volatile sig_atomic_t tog_sigwinch_received;
619 static volatile sig_atomic_t tog_sigpipe_received;
620 static volatile sig_atomic_t tog_sigcont_received;
621 static volatile sig_atomic_t tog_sigint_received;
622 static volatile sig_atomic_t tog_sigterm_received;
624 static void
625 tog_sigwinch(int signo)
627 tog_sigwinch_received = 1;
630 static void
631 tog_sigpipe(int signo)
633 tog_sigpipe_received = 1;
636 static void
637 tog_sigcont(int signo)
639 tog_sigcont_received = 1;
642 static void
643 tog_sigint(int signo)
645 tog_sigint_received = 1;
648 static void
649 tog_sigterm(int signo)
651 tog_sigterm_received = 1;
654 static int
655 tog_fatal_signal_received(void)
657 return (tog_sigpipe_received ||
658 tog_sigint_received || tog_sigint_received);
661 static const struct got_error *
662 view_close(struct tog_view *view)
664 const struct got_error *err = NULL, *child_err = NULL;
666 if (view->child) {
667 child_err = view_close(view->child);
668 view->child = NULL;
670 if (view->close)
671 err = view->close(view);
672 if (view->panel)
673 del_panel(view->panel);
674 if (view->window)
675 delwin(view->window);
676 free(view);
677 return err ? err : child_err;
680 static struct tog_view *
681 view_open(int nlines, int ncols, int begin_y, int begin_x,
682 enum tog_view_type type)
684 struct tog_view *view = calloc(1, sizeof(*view));
686 if (view == NULL)
687 return NULL;
689 view->type = type;
690 view->lines = LINES;
691 view->cols = COLS;
692 view->nlines = nlines ? nlines : LINES - begin_y;
693 view->ncols = ncols ? ncols : COLS - begin_x;
694 view->begin_y = begin_y;
695 view->begin_x = begin_x;
696 view->window = newwin(nlines, ncols, begin_y, begin_x);
697 if (view->window == NULL) {
698 view_close(view);
699 return NULL;
701 view->panel = new_panel(view->window);
702 if (view->panel == NULL ||
703 set_panel_userptr(view->panel, view) != OK) {
704 view_close(view);
705 return NULL;
708 keypad(view->window, TRUE);
709 return view;
712 static int
713 view_split_begin_x(int begin_x)
715 if (begin_x > 0 || COLS < 120)
716 return 0;
717 return (COLS - MAX(COLS / 2, 80));
720 /* XXX Stub till we decide what to do. */
721 static int
722 view_split_begin_y(int lines)
724 return lines * HSPLIT_SCALE;
727 static const struct got_error *view_resize(struct tog_view *);
729 static const struct got_error *
730 view_splitscreen(struct tog_view *view)
732 const struct got_error *err = NULL;
734 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
735 if (view->resized_y && view->resized_y < view->lines)
736 view->begin_y = view->resized_y;
737 else
738 view->begin_y = view_split_begin_y(view->nlines);
739 view->begin_x = 0;
740 } else if (!view->resize) {
741 if (view->resized_x && view->resized_x < view->cols - 1 &&
742 view->cols > 119)
743 view->begin_x = view->resized_x;
744 else
745 view->begin_x = view_split_begin_x(0);
746 view->begin_y = 0;
748 view->nlines = LINES - view->begin_y;
749 view->ncols = COLS - view->begin_x;
750 view->lines = LINES;
751 view->cols = COLS;
752 err = view_resize(view);
753 if (err)
754 return err;
756 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
757 view->parent->nlines = view->begin_y;
759 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
760 return got_error_from_errno("mvwin");
762 return NULL;
765 static const struct got_error *
766 view_fullscreen(struct tog_view *view)
768 const struct got_error *err = NULL;
770 view->begin_x = 0;
771 view->begin_y = view->resize ? view->begin_y : 0;
772 view->nlines = view->resize ? view->nlines : LINES;
773 view->ncols = COLS;
774 view->lines = LINES;
775 view->cols = COLS;
776 err = view_resize(view);
777 if (err)
778 return err;
780 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
781 return got_error_from_errno("mvwin");
783 return NULL;
786 static int
787 view_is_parent_view(struct tog_view *view)
789 return view->parent == NULL;
792 static int
793 view_is_splitscreen(struct tog_view *view)
795 return view->begin_x > 0 || view->begin_y > 0;
798 static int
799 view_is_fullscreen(struct tog_view *view)
801 return view->nlines == LINES && view->ncols == COLS;
804 static int
805 view_is_hsplit_top(struct tog_view *view)
807 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
808 view_is_splitscreen(view->child);
811 static void
812 view_border(struct tog_view *view)
814 PANEL *panel;
815 const struct tog_view *view_above;
817 if (view->parent)
818 return view_border(view->parent);
820 panel = panel_above(view->panel);
821 if (panel == NULL)
822 return;
824 view_above = panel_userptr(panel);
825 if (view->mode == TOG_VIEW_SPLIT_HRZN)
826 mvwhline(view->window, view_above->begin_y - 1,
827 view->begin_x, got_locale_is_utf8() ?
828 ACS_HLINE : '-', view->ncols);
829 else
830 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
831 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
834 static const struct got_error *view_init_hsplit(struct tog_view *, int);
835 static const struct got_error *request_log_commits(struct tog_view *);
836 static const struct got_error *offset_selection_down(struct tog_view *);
837 static void offset_selection_up(struct tog_view *);
838 static void view_get_split(struct tog_view *, int *, int *);
840 static const struct got_error *
841 view_resize(struct tog_view *view)
843 const struct got_error *err = NULL;
844 int dif, nlines, ncols;
846 dif = LINES - view->lines; /* line difference */
848 if (view->lines > LINES)
849 nlines = view->nlines - (view->lines - LINES);
850 else
851 nlines = view->nlines + (LINES - view->lines);
852 if (view->cols > COLS)
853 ncols = view->ncols - (view->cols - COLS);
854 else
855 ncols = view->ncols + (COLS - view->cols);
857 if (view->child) {
858 int hs = view->child->begin_y;
860 if (!view_is_fullscreen(view))
861 view->child->begin_x = view_split_begin_x(view->begin_x);
862 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
863 view->child->begin_x == 0) {
864 ncols = COLS;
866 view_fullscreen(view->child);
867 if (view->child->focussed)
868 show_panel(view->child->panel);
869 else
870 show_panel(view->panel);
871 } else {
872 ncols = view->child->begin_x;
874 view_splitscreen(view->child);
875 show_panel(view->child->panel);
877 /*
878 * Request commits if terminal height was increased in a log
879 * view so we have enough commits loaded to populate the view.
880 */
881 if (view->type == TOG_VIEW_LOG && dif > 0) {
882 struct tog_log_view_state *ts = &view->state.log;
884 if (ts->commits.ncommits < ts->selected_entry->idx +
885 view->lines - ts->selected) {
886 view->nscrolled = ts->selected_entry->idx +
887 view->lines - ts->selected -
888 ts->commits.ncommits + dif;
889 err = request_log_commits(view);
890 if (err)
891 return err;
895 /*
896 * XXX This is ugly and needs to be moved into the above
897 * logic but "works" for now and my attempts at moving it
898 * break either 'tab' or 'F' key maps in horizontal splits.
899 */
900 if (hs) {
901 err = view_splitscreen(view->child);
902 if (err)
903 return err;
904 if (dif < 0) { /* top split decreased */
905 err = offset_selection_down(view);
906 if (err)
907 return err;
909 view_border(view);
910 update_panels();
911 doupdate();
912 show_panel(view->child->panel);
913 nlines = view->nlines;
915 } else if (view->parent == NULL)
916 ncols = COLS;
918 if (wresize(view->window, nlines, ncols) == ERR)
919 return got_error_from_errno("wresize");
920 if (replace_panel(view->panel, view->window) == ERR)
921 return got_error_from_errno("replace_panel");
922 wclear(view->window);
924 view->nlines = nlines;
925 view->ncols = ncols;
926 view->lines = LINES;
927 view->cols = COLS;
929 return NULL;
932 static void
933 view_adjust_offset(struct tog_view *view, int n)
935 if (n == 0)
936 return;
938 if (view->parent && view->parent->offset) {
939 if (view->parent->offset + n >= 0)
940 view->parent->offset += n;
941 else
942 view->parent->offset = 0;
943 } else if (view->offset) {
944 if (view->offset - n >= 0)
945 view->offset -= n;
946 else
947 view->offset = 0;
951 static const struct got_error *
952 view_resize_split(struct tog_view *view, int resize)
954 const struct got_error *err = NULL;
955 struct tog_view *v = NULL;
957 if (view->parent)
958 v = view->parent;
959 else
960 v = view;
962 if (!v->child || !view_is_splitscreen(v->child))
963 return NULL;
965 v->resize = v->child->resize = resize; /* lock for resize event */
967 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
968 if (v->child->resized_y)
969 v->child->begin_y = v->child->resized_y;
970 if (view->parent)
971 v->child->begin_y -= resize;
972 else
973 v->child->begin_y += resize;
974 if (v->child->begin_y < 3) {
975 view->count = 0;
976 v->child->begin_y = 3;
977 } else if (v->child->begin_y > LINES - 1) {
978 view->count = 0;
979 v->child->begin_y = LINES - 1;
981 v->ncols = COLS;
982 v->child->ncols = COLS;
983 view_adjust_offset(view, resize);
984 err = view_init_hsplit(v, v->child->begin_y);
985 if (err)
986 return err;
987 v->child->resized_y = v->child->begin_y;
988 } else {
989 if (v->child->resized_x)
990 v->child->begin_x = v->child->resized_x;
991 if (view->parent)
992 v->child->begin_x -= resize;
993 else
994 v->child->begin_x += resize;
995 if (v->child->begin_x < 11) {
996 view->count = 0;
997 v->child->begin_x = 11;
998 } else if (v->child->begin_x > COLS - 1) {
999 view->count = 0;
1000 v->child->begin_x = COLS - 1;
1002 v->child->resized_x = v->child->begin_x;
1005 v->child->mode = v->mode;
1006 v->child->nlines = v->lines - v->child->begin_y;
1007 v->child->ncols = v->cols - v->child->begin_x;
1008 v->focus_child = 1;
1010 err = view_fullscreen(v);
1011 if (err)
1012 return err;
1013 err = view_splitscreen(v->child);
1014 if (err)
1015 return err;
1017 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1018 err = offset_selection_down(v->child);
1019 if (err)
1020 return err;
1023 if (v->type == TOG_VIEW_LOG)
1024 err = request_log_commits(v);
1025 else if (v->child->type == TOG_VIEW_LOG)
1026 err = request_log_commits(v->child);
1028 v->resize = v->child->resize = 0;
1030 return err;
1033 static void
1034 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1036 struct tog_view *v = src->child ? src->child : src;
1038 dst->resized_x = v->resized_x;
1039 dst->resized_y = v->resized_y;
1042 static const struct got_error *
1043 view_close_child(struct tog_view *view)
1045 const struct got_error *err = NULL;
1047 if (view->child == NULL)
1048 return NULL;
1050 err = view_close(view->child);
1051 view->child = NULL;
1052 return err;
1055 static const struct got_error *
1056 view_set_child(struct tog_view *view, struct tog_view *child)
1058 const struct got_error *err = NULL;
1060 view->child = child;
1061 child->parent = view;
1063 err = view_resize(view);
1064 if (err)
1065 return err;
1067 if (view->child->resized_x || view->child->resized_y)
1068 err = view_resize_split(view, 0);
1070 return err;
1073 static void
1074 tog_resizeterm(void)
1076 int cols, lines;
1077 struct winsize size;
1079 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1080 cols = 80; /* Default */
1081 lines = 24;
1082 } else {
1083 cols = size.ws_col;
1084 lines = size.ws_row;
1086 resize_term(lines, cols);
1089 static const struct got_error *
1090 view_search_start(struct tog_view *view)
1092 const struct got_error *err = NULL;
1093 struct tog_view *v = view;
1094 char pattern[1024];
1095 int ret;
1097 if (view->search_started) {
1098 regfree(&view->regex);
1099 view->searching = 0;
1100 memset(&view->regmatch, 0, sizeof(view->regmatch));
1102 view->search_started = 0;
1104 if (view->nlines < 1)
1105 return NULL;
1107 if (view_is_hsplit_top(view))
1108 v = view->child;
1110 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1111 wclrtoeol(v->window);
1113 nodelay(view->window, FALSE); /* block for search term input */
1114 nocbreak();
1115 echo();
1116 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1117 wrefresh(v->window);
1118 cbreak();
1119 noecho();
1120 nodelay(view->window, TRUE);
1121 if (ret == ERR)
1122 return NULL;
1124 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1125 err = view->search_start(view);
1126 if (err) {
1127 regfree(&view->regex);
1128 return err;
1130 view->search_started = 1;
1131 view->searching = TOG_SEARCH_FORWARD;
1132 view->search_next_done = 0;
1133 view->search_next(view);
1136 return NULL;
1139 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1140 static const struct got_error *
1141 switch_split(struct tog_view *view)
1143 const struct got_error *err = NULL;
1144 struct tog_view *v = NULL;
1146 if (view->parent)
1147 v = view->parent;
1148 else
1149 v = view;
1151 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1152 v->mode = TOG_VIEW_SPLIT_VERT;
1153 else
1154 v->mode = TOG_VIEW_SPLIT_HRZN;
1156 if (!v->child)
1157 return NULL;
1158 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1159 v->mode = TOG_VIEW_SPLIT_NONE;
1161 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1162 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1163 v->child->begin_y = v->child->resized_y;
1164 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1165 v->child->begin_x = v->child->resized_x;
1168 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1169 v->ncols = COLS;
1170 v->child->ncols = COLS;
1171 v->child->nscrolled = LINES - v->child->nlines;
1173 err = view_init_hsplit(v, v->child->begin_y);
1174 if (err)
1175 return err;
1177 v->child->mode = v->mode;
1178 v->child->nlines = v->lines - v->child->begin_y;
1179 v->focus_child = 1;
1181 err = view_fullscreen(v);
1182 if (err)
1183 return err;
1184 err = view_splitscreen(v->child);
1185 if (err)
1186 return err;
1188 if (v->mode == TOG_VIEW_SPLIT_NONE)
1189 v->mode = TOG_VIEW_SPLIT_VERT;
1190 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1191 err = offset_selection_down(v);
1192 err = offset_selection_down(v->child);
1193 } else {
1194 offset_selection_up(v);
1195 offset_selection_up(v->child);
1197 if (v->type == TOG_VIEW_LOG)
1198 err = request_log_commits(v);
1199 else if (v->child->type == TOG_VIEW_LOG)
1200 err = request_log_commits(v->child);
1202 return err;
1206 * Compute view->count from numeric input. Assign total to view->count and
1207 * return first non-numeric key entered.
1209 static int
1210 get_compound_key(struct tog_view *view, int c)
1212 struct tog_view *v = view;
1213 int x, n = 0;
1215 if (view_is_hsplit_top(view))
1216 v = view->child;
1217 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1218 v = view->parent;
1220 view->count = 0;
1221 cbreak(); /* block for input */
1222 wmove(v->window, v->nlines - 1, 0);
1223 wclrtoeol(v->window);
1224 waddch(v->window, ':');
1226 do {
1227 x = getcurx(v->window);
1228 if (x != ERR && x < view->ncols) {
1229 waddch(v->window, c);
1230 wrefresh(v->window);
1234 * Don't overflow. Max valid request should be the greatest
1235 * between the longest and total lines; cap at 10 million.
1237 if (n >= 9999999)
1238 n = 9999999;
1239 else
1240 n = n * 10 + (c - '0');
1241 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1243 /* Massage excessive or inapplicable values at the input handler. */
1244 view->count = n;
1246 return c;
1249 static const struct got_error *
1250 view_input(struct tog_view **new, int *done, struct tog_view *view,
1251 struct tog_view_list_head *views)
1253 const struct got_error *err = NULL;
1254 struct tog_view *v;
1255 int ch, errcode;
1257 *new = NULL;
1259 /* Clear "no matches" indicator. */
1260 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1261 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1262 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1263 view->count = 0;
1266 if (view->searching && !view->search_next_done) {
1267 errcode = pthread_mutex_unlock(&tog_mutex);
1268 if (errcode)
1269 return got_error_set_errno(errcode,
1270 "pthread_mutex_unlock");
1271 sched_yield();
1272 errcode = pthread_mutex_lock(&tog_mutex);
1273 if (errcode)
1274 return got_error_set_errno(errcode,
1275 "pthread_mutex_lock");
1276 view->search_next(view);
1277 return NULL;
1280 nodelay(view->window, FALSE);
1281 /* Allow threads to make progress while we are waiting for input. */
1282 errcode = pthread_mutex_unlock(&tog_mutex);
1283 if (errcode)
1284 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1285 /* If we have an unfinished count, let C-g or backspace abort. */
1286 if (view->count && --view->count) {
1287 cbreak();
1288 nodelay(view->window, TRUE);
1289 ch = wgetch(view->window);
1290 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1291 view->count = 0;
1292 else
1293 ch = view->ch;
1294 } else {
1295 ch = wgetch(view->window);
1296 if (ch >= '1' && ch <= '9')
1297 view->ch = ch = get_compound_key(view, ch);
1299 errcode = pthread_mutex_lock(&tog_mutex);
1300 if (errcode)
1301 return got_error_set_errno(errcode, "pthread_mutex_lock");
1302 nodelay(view->window, TRUE);
1304 if (tog_sigwinch_received || tog_sigcont_received) {
1305 tog_resizeterm();
1306 tog_sigwinch_received = 0;
1307 tog_sigcont_received = 0;
1308 TAILQ_FOREACH(v, views, entry) {
1309 err = view_resize(v);
1310 if (err)
1311 return err;
1312 err = v->input(new, v, KEY_RESIZE);
1313 if (err)
1314 return err;
1315 if (v->child) {
1316 err = view_resize(v->child);
1317 if (err)
1318 return err;
1319 err = v->child->input(new, v->child,
1320 KEY_RESIZE);
1321 if (err)
1322 return err;
1323 if (v->child->resized_x || v->child->resized_y) {
1324 err = view_resize_split(v, 0);
1325 if (err)
1326 return err;
1332 switch (ch) {
1333 case '\t':
1334 view->count = 0;
1335 if (view->child) {
1336 view->focussed = 0;
1337 view->child->focussed = 1;
1338 view->focus_child = 1;
1339 } else if (view->parent) {
1340 view->focussed = 0;
1341 view->parent->focussed = 1;
1342 view->parent->focus_child = 0;
1343 if (!view_is_splitscreen(view)) {
1344 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1345 view->parent->type == TOG_VIEW_LOG) {
1346 err = request_log_commits(view->parent);
1347 if (err)
1348 return err;
1350 offset_selection_up(view->parent);
1351 err = view_fullscreen(view->parent);
1352 if (err)
1353 return err;
1356 break;
1357 case 'q':
1358 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1359 if (view->parent->type == TOG_VIEW_LOG) {
1360 /* might need more commits to fill fullscreen */
1361 err = request_log_commits(view->parent);
1362 if (err)
1363 break;
1365 offset_selection_up(view->parent);
1367 err = view->input(new, view, ch);
1368 view->dying = 1;
1369 break;
1370 case 'Q':
1371 *done = 1;
1372 break;
1373 case 'F':
1374 view->count = 0;
1375 if (view_is_parent_view(view)) {
1376 if (view->child == NULL)
1377 break;
1378 if (view_is_splitscreen(view->child)) {
1379 view->focussed = 0;
1380 view->child->focussed = 1;
1381 err = view_fullscreen(view->child);
1382 } else {
1383 err = view_splitscreen(view->child);
1384 if (!err)
1385 err = view_resize_split(view, 0);
1387 if (err)
1388 break;
1389 err = view->child->input(new, view->child,
1390 KEY_RESIZE);
1391 } else {
1392 if (view_is_splitscreen(view)) {
1393 view->parent->focussed = 0;
1394 view->focussed = 1;
1395 err = view_fullscreen(view);
1396 } else {
1397 err = view_splitscreen(view);
1398 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1399 err = view_resize(view->parent);
1400 if (!err)
1401 err = view_resize_split(view, 0);
1403 if (err)
1404 break;
1405 err = view->input(new, view, KEY_RESIZE);
1407 if (err)
1408 break;
1409 if (view->type == TOG_VIEW_LOG) {
1410 err = request_log_commits(view);
1411 if (err)
1412 break;
1414 if (view->parent)
1415 err = offset_selection_down(view->parent);
1416 if (!err)
1417 err = offset_selection_down(view);
1418 break;
1419 case 'S':
1420 view->count = 0;
1421 err = switch_split(view);
1422 break;
1423 case '-':
1424 err = view_resize_split(view, -1);
1425 break;
1426 case '+':
1427 err = view_resize_split(view, 1);
1428 break;
1429 case KEY_RESIZE:
1430 break;
1431 case '/':
1432 view->count = 0;
1433 if (view->search_start)
1434 view_search_start(view);
1435 else
1436 err = view->input(new, view, ch);
1437 break;
1438 case 'N':
1439 case 'n':
1440 if (view->search_started && view->search_next) {
1441 view->searching = (ch == 'n' ?
1442 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1443 view->search_next_done = 0;
1444 view->search_next(view);
1445 } else
1446 err = view->input(new, view, ch);
1447 break;
1448 case 'A':
1449 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1450 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1451 else
1452 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1453 TAILQ_FOREACH(v, views, entry) {
1454 if (v->reset) {
1455 err = v->reset(v);
1456 if (err)
1457 return err;
1459 if (v->child && v->child->reset) {
1460 err = v->child->reset(v->child);
1461 if (err)
1462 return err;
1465 break;
1466 default:
1467 err = view->input(new, view, ch);
1468 break;
1471 return err;
1474 static int
1475 view_needs_focus_indication(struct tog_view *view)
1477 if (view_is_parent_view(view)) {
1478 if (view->child == NULL || view->child->focussed)
1479 return 0;
1480 if (!view_is_splitscreen(view->child))
1481 return 0;
1482 } else if (!view_is_splitscreen(view))
1483 return 0;
1485 return view->focussed;
1488 static const struct got_error *
1489 view_loop(struct tog_view *view)
1491 const struct got_error *err = NULL;
1492 struct tog_view_list_head views;
1493 struct tog_view *new_view;
1494 char *mode;
1495 int fast_refresh = 10;
1496 int done = 0, errcode;
1498 mode = getenv("TOG_VIEW_SPLIT_MODE");
1499 if (!mode || !(*mode == 'h' || *mode == 'H'))
1500 view->mode = TOG_VIEW_SPLIT_VERT;
1501 else
1502 view->mode = TOG_VIEW_SPLIT_HRZN;
1504 errcode = pthread_mutex_lock(&tog_mutex);
1505 if (errcode)
1506 return got_error_set_errno(errcode, "pthread_mutex_lock");
1508 TAILQ_INIT(&views);
1509 TAILQ_INSERT_HEAD(&views, view, entry);
1511 view->focussed = 1;
1512 err = view->show(view);
1513 if (err)
1514 return err;
1515 update_panels();
1516 doupdate();
1517 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1518 !tog_fatal_signal_received()) {
1519 /* Refresh fast during initialization, then become slower. */
1520 if (fast_refresh && fast_refresh-- == 0)
1521 halfdelay(10); /* switch to once per second */
1523 err = view_input(&new_view, &done, view, &views);
1524 if (err)
1525 break;
1526 if (view->dying) {
1527 struct tog_view *v, *prev = NULL;
1529 if (view_is_parent_view(view))
1530 prev = TAILQ_PREV(view, tog_view_list_head,
1531 entry);
1532 else if (view->parent)
1533 prev = view->parent;
1535 if (view->parent) {
1536 view->parent->child = NULL;
1537 view->parent->focus_child = 0;
1538 /* Restore fullscreen line height. */
1539 view->parent->nlines = view->parent->lines;
1540 err = view_resize(view->parent);
1541 if (err)
1542 break;
1543 /* Make resized splits persist. */
1544 view_transfer_size(view->parent, view);
1545 } else
1546 TAILQ_REMOVE(&views, view, entry);
1548 err = view_close(view);
1549 if (err)
1550 goto done;
1552 view = NULL;
1553 TAILQ_FOREACH(v, &views, entry) {
1554 if (v->focussed)
1555 break;
1557 if (view == NULL && new_view == NULL) {
1558 /* No view has focus. Try to pick one. */
1559 if (prev)
1560 view = prev;
1561 else if (!TAILQ_EMPTY(&views)) {
1562 view = TAILQ_LAST(&views,
1563 tog_view_list_head);
1565 if (view) {
1566 if (view->focus_child) {
1567 view->child->focussed = 1;
1568 view = view->child;
1569 } else
1570 view->focussed = 1;
1574 if (new_view) {
1575 struct tog_view *v, *t;
1576 /* Only allow one parent view per type. */
1577 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1578 if (v->type != new_view->type)
1579 continue;
1580 TAILQ_REMOVE(&views, v, entry);
1581 err = view_close(v);
1582 if (err)
1583 goto done;
1584 break;
1586 TAILQ_INSERT_TAIL(&views, new_view, entry);
1587 view = new_view;
1589 if (view) {
1590 if (view_is_parent_view(view)) {
1591 if (view->child && view->child->focussed)
1592 view = view->child;
1593 } else {
1594 if (view->parent && view->parent->focussed)
1595 view = view->parent;
1597 show_panel(view->panel);
1598 if (view->child && view_is_splitscreen(view->child))
1599 show_panel(view->child->panel);
1600 if (view->parent && view_is_splitscreen(view)) {
1601 err = view->parent->show(view->parent);
1602 if (err)
1603 goto done;
1605 err = view->show(view);
1606 if (err)
1607 goto done;
1608 if (view->child) {
1609 err = view->child->show(view->child);
1610 if (err)
1611 goto done;
1613 update_panels();
1614 doupdate();
1617 done:
1618 while (!TAILQ_EMPTY(&views)) {
1619 const struct got_error *close_err;
1620 view = TAILQ_FIRST(&views);
1621 TAILQ_REMOVE(&views, view, entry);
1622 close_err = view_close(view);
1623 if (close_err && err == NULL)
1624 err = close_err;
1627 errcode = pthread_mutex_unlock(&tog_mutex);
1628 if (errcode && err == NULL)
1629 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1631 return err;
1634 __dead static void
1635 usage_log(void)
1637 endwin();
1638 fprintf(stderr,
1639 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1640 getprogname());
1641 exit(1);
1644 /* Create newly allocated wide-character string equivalent to a byte string. */
1645 static const struct got_error *
1646 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1648 char *vis = NULL;
1649 const struct got_error *err = NULL;
1651 *ws = NULL;
1652 *wlen = mbstowcs(NULL, s, 0);
1653 if (*wlen == (size_t)-1) {
1654 int vislen;
1655 if (errno != EILSEQ)
1656 return got_error_from_errno("mbstowcs");
1658 /* byte string invalid in current encoding; try to "fix" it */
1659 err = got_mbsavis(&vis, &vislen, s);
1660 if (err)
1661 return err;
1662 *wlen = mbstowcs(NULL, vis, 0);
1663 if (*wlen == (size_t)-1) {
1664 err = got_error_from_errno("mbstowcs"); /* give up */
1665 goto done;
1669 *ws = calloc(*wlen + 1, sizeof(**ws));
1670 if (*ws == NULL) {
1671 err = got_error_from_errno("calloc");
1672 goto done;
1675 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1676 err = got_error_from_errno("mbstowcs");
1677 done:
1678 free(vis);
1679 if (err) {
1680 free(*ws);
1681 *ws = NULL;
1682 *wlen = 0;
1684 return err;
1687 static const struct got_error *
1688 expand_tab(char **ptr, const char *src)
1690 char *dst;
1691 size_t len, n, idx = 0, sz = 0;
1693 *ptr = NULL;
1694 n = len = strlen(src);
1695 dst = malloc(n + 1);
1696 if (dst == NULL)
1697 return got_error_from_errno("malloc");
1699 while (idx < len && src[idx]) {
1700 const char c = src[idx];
1702 if (c == '\t') {
1703 size_t nb = TABSIZE - sz % TABSIZE;
1704 char *p;
1706 p = realloc(dst, n + nb);
1707 if (p == NULL) {
1708 free(dst);
1709 return got_error_from_errno("realloc");
1712 dst = p;
1713 n += nb;
1714 memset(dst + sz, ' ', nb);
1715 sz += nb;
1716 } else
1717 dst[sz++] = src[idx];
1718 ++idx;
1721 dst[sz] = '\0';
1722 *ptr = dst;
1723 return NULL;
1727 * Advance at most n columns from wline starting at offset off.
1728 * Return the index to the first character after the span operation.
1729 * Return the combined column width of all spanned wide character in
1730 * *rcol.
1732 static int
1733 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1735 int width, i, cols = 0;
1737 if (n == 0) {
1738 *rcol = cols;
1739 return off;
1742 for (i = off; wline[i] != L'\0'; ++i) {
1743 if (wline[i] == L'\t')
1744 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1745 else
1746 width = wcwidth(wline[i]);
1748 if (width == -1) {
1749 width = 1;
1750 wline[i] = L'.';
1753 if (cols + width > n)
1754 break;
1755 cols += width;
1758 *rcol = cols;
1759 return i;
1763 * Format a line for display, ensuring that it won't overflow a width limit.
1764 * With scrolling, the width returned refers to the scrolled version of the
1765 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1767 static const struct got_error *
1768 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1769 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1771 const struct got_error *err = NULL;
1772 int cols;
1773 wchar_t *wline = NULL;
1774 char *exstr = NULL;
1775 size_t wlen;
1776 int i, scrollx;
1778 *wlinep = NULL;
1779 *widthp = 0;
1781 if (expand) {
1782 err = expand_tab(&exstr, line);
1783 if (err)
1784 return err;
1787 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1788 free(exstr);
1789 if (err)
1790 return err;
1792 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1794 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1795 wline[wlen - 1] = L'\0';
1796 wlen--;
1798 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1799 wline[wlen - 1] = L'\0';
1800 wlen--;
1803 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1804 wline[i] = L'\0';
1806 if (widthp)
1807 *widthp = cols;
1808 if (scrollxp)
1809 *scrollxp = scrollx;
1810 if (err)
1811 free(wline);
1812 else
1813 *wlinep = wline;
1814 return err;
1817 static const struct got_error*
1818 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1819 struct got_object_id *id, struct got_repository *repo)
1821 static const struct got_error *err = NULL;
1822 struct got_reflist_entry *re;
1823 char *s;
1824 const char *name;
1826 *refs_str = NULL;
1828 TAILQ_FOREACH(re, refs, entry) {
1829 struct got_tag_object *tag = NULL;
1830 struct got_object_id *ref_id;
1831 int cmp;
1833 name = got_ref_get_name(re->ref);
1834 if (strcmp(name, GOT_REF_HEAD) == 0)
1835 continue;
1836 if (strncmp(name, "refs/", 5) == 0)
1837 name += 5;
1838 if (strncmp(name, "got/", 4) == 0 &&
1839 strncmp(name, "got/backup/", 11) != 0)
1840 continue;
1841 if (strncmp(name, "heads/", 6) == 0)
1842 name += 6;
1843 if (strncmp(name, "remotes/", 8) == 0) {
1844 name += 8;
1845 s = strstr(name, "/" GOT_REF_HEAD);
1846 if (s != NULL && s[strlen(s)] == '\0')
1847 continue;
1849 err = got_ref_resolve(&ref_id, repo, re->ref);
1850 if (err)
1851 break;
1852 if (strncmp(name, "tags/", 5) == 0) {
1853 err = got_object_open_as_tag(&tag, repo, ref_id);
1854 if (err) {
1855 if (err->code != GOT_ERR_OBJ_TYPE) {
1856 free(ref_id);
1857 break;
1859 /* Ref points at something other than a tag. */
1860 err = NULL;
1861 tag = NULL;
1864 cmp = got_object_id_cmp(tag ?
1865 got_object_tag_get_object_id(tag) : ref_id, id);
1866 free(ref_id);
1867 if (tag)
1868 got_object_tag_close(tag);
1869 if (cmp != 0)
1870 continue;
1871 s = *refs_str;
1872 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1873 s ? ", " : "", name) == -1) {
1874 err = got_error_from_errno("asprintf");
1875 free(s);
1876 *refs_str = NULL;
1877 break;
1879 free(s);
1882 return err;
1885 static const struct got_error *
1886 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1887 int col_tab_align)
1889 char *smallerthan;
1891 smallerthan = strchr(author, '<');
1892 if (smallerthan && smallerthan[1] != '\0')
1893 author = smallerthan + 1;
1894 author[strcspn(author, "@>")] = '\0';
1895 return format_line(wauthor, author_width, NULL, author, 0, limit,
1896 col_tab_align, 0);
1899 static const struct got_error *
1900 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1901 struct got_object_id *id, const size_t date_display_cols,
1902 int author_display_cols)
1904 struct tog_log_view_state *s = &view->state.log;
1905 const struct got_error *err = NULL;
1906 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1907 char *logmsg0 = NULL, *logmsg = NULL;
1908 char *author = NULL;
1909 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1910 int author_width, logmsg_width;
1911 char *newline, *line = NULL;
1912 int col, limit, scrollx;
1913 const int avail = view->ncols;
1914 struct tm tm;
1915 time_t committer_time;
1916 struct tog_color *tc;
1918 committer_time = got_object_commit_get_committer_time(commit);
1919 if (gmtime_r(&committer_time, &tm) == NULL)
1920 return got_error_from_errno("gmtime_r");
1921 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1922 return got_error(GOT_ERR_NO_SPACE);
1924 if (avail <= date_display_cols)
1925 limit = MIN(sizeof(datebuf) - 1, avail);
1926 else
1927 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1928 tc = get_color(&s->colors, TOG_COLOR_DATE);
1929 if (tc)
1930 wattr_on(view->window,
1931 COLOR_PAIR(tc->colorpair), NULL);
1932 waddnstr(view->window, datebuf, limit);
1933 if (tc)
1934 wattr_off(view->window,
1935 COLOR_PAIR(tc->colorpair), NULL);
1936 col = limit;
1937 if (col > avail)
1938 goto done;
1940 if (avail >= 120) {
1941 char *id_str;
1942 err = got_object_id_str(&id_str, id);
1943 if (err)
1944 goto done;
1945 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1946 if (tc)
1947 wattr_on(view->window,
1948 COLOR_PAIR(tc->colorpair), NULL);
1949 wprintw(view->window, "%.8s ", id_str);
1950 if (tc)
1951 wattr_off(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 free(id_str);
1954 col += 9;
1955 if (col > avail)
1956 goto done;
1959 author = strdup(got_object_commit_get_author(commit));
1960 if (author == NULL) {
1961 err = got_error_from_errno("strdup");
1962 goto done;
1964 err = format_author(&wauthor, &author_width, author, avail - col, col);
1965 if (err)
1966 goto done;
1967 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1968 if (tc)
1969 wattr_on(view->window,
1970 COLOR_PAIR(tc->colorpair), NULL);
1971 waddwstr(view->window, wauthor);
1972 if (tc)
1973 wattr_off(view->window,
1974 COLOR_PAIR(tc->colorpair), NULL);
1975 col += author_width;
1976 while (col < avail && author_width < author_display_cols + 2) {
1977 waddch(view->window, ' ');
1978 col++;
1979 author_width++;
1981 if (col > avail)
1982 goto done;
1984 err = got_object_commit_get_logmsg(&logmsg0, commit);
1985 if (err)
1986 goto done;
1987 logmsg = logmsg0;
1988 while (*logmsg == '\n')
1989 logmsg++;
1990 newline = strchr(logmsg, '\n');
1991 if (newline)
1992 *newline = '\0';
1993 limit = avail - col;
1994 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1995 limit--; /* for the border */
1996 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1997 limit, col, 1);
1998 if (err)
1999 goto done;
2000 waddwstr(view->window, &wlogmsg[scrollx]);
2001 col += MAX(logmsg_width, 0);
2002 while (col < avail) {
2003 waddch(view->window, ' ');
2004 col++;
2006 done:
2007 free(logmsg0);
2008 free(wlogmsg);
2009 free(author);
2010 free(wauthor);
2011 free(line);
2012 return err;
2015 static struct commit_queue_entry *
2016 alloc_commit_queue_entry(struct got_commit_object *commit,
2017 struct got_object_id *id)
2019 struct commit_queue_entry *entry;
2021 entry = calloc(1, sizeof(*entry));
2022 if (entry == NULL)
2023 return NULL;
2025 entry->id = id;
2026 entry->commit = commit;
2027 return entry;
2030 static void
2031 pop_commit(struct commit_queue *commits)
2033 struct commit_queue_entry *entry;
2035 entry = TAILQ_FIRST(&commits->head);
2036 TAILQ_REMOVE(&commits->head, entry, entry);
2037 got_object_commit_close(entry->commit);
2038 commits->ncommits--;
2039 /* Don't free entry->id! It is owned by the commit graph. */
2040 free(entry);
2043 static void
2044 free_commits(struct commit_queue *commits)
2046 while (!TAILQ_EMPTY(&commits->head))
2047 pop_commit(commits);
2050 static const struct got_error *
2051 match_commit(int *have_match, struct got_object_id *id,
2052 struct got_commit_object *commit, regex_t *regex)
2054 const struct got_error *err = NULL;
2055 regmatch_t regmatch;
2056 char *id_str = NULL, *logmsg = NULL;
2058 *have_match = 0;
2060 err = got_object_id_str(&id_str, id);
2061 if (err)
2062 return err;
2064 err = got_object_commit_get_logmsg(&logmsg, commit);
2065 if (err)
2066 goto done;
2068 if (regexec(regex, got_object_commit_get_author(commit), 1,
2069 &regmatch, 0) == 0 ||
2070 regexec(regex, got_object_commit_get_committer(commit), 1,
2071 &regmatch, 0) == 0 ||
2072 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2073 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2074 *have_match = 1;
2075 done:
2076 free(id_str);
2077 free(logmsg);
2078 return err;
2081 static const struct got_error *
2082 queue_commits(struct tog_log_thread_args *a)
2084 const struct got_error *err = NULL;
2087 * We keep all commits open throughout the lifetime of the log
2088 * view in order to avoid having to re-fetch commits from disk
2089 * while updating the display.
2091 do {
2092 struct got_object_id *id;
2093 struct got_commit_object *commit;
2094 struct commit_queue_entry *entry;
2095 int errcode;
2097 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2098 NULL, NULL);
2099 if (err || id == NULL)
2100 break;
2102 err = got_object_open_as_commit(&commit, a->repo, id);
2103 if (err)
2104 break;
2105 entry = alloc_commit_queue_entry(commit, id);
2106 if (entry == NULL) {
2107 err = got_error_from_errno("alloc_commit_queue_entry");
2108 break;
2111 errcode = pthread_mutex_lock(&tog_mutex);
2112 if (errcode) {
2113 err = got_error_set_errno(errcode,
2114 "pthread_mutex_lock");
2115 break;
2118 entry->idx = a->commits->ncommits;
2119 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2120 a->commits->ncommits++;
2122 if (*a->searching == TOG_SEARCH_FORWARD &&
2123 !*a->search_next_done) {
2124 int have_match;
2125 err = match_commit(&have_match, id, commit, a->regex);
2126 if (err)
2127 break;
2128 if (have_match)
2129 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2132 errcode = pthread_mutex_unlock(&tog_mutex);
2133 if (errcode && err == NULL)
2134 err = got_error_set_errno(errcode,
2135 "pthread_mutex_unlock");
2136 if (err)
2137 break;
2138 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2140 return err;
2143 static void
2144 select_commit(struct tog_log_view_state *s)
2146 struct commit_queue_entry *entry;
2147 int ncommits = 0;
2149 entry = s->first_displayed_entry;
2150 while (entry) {
2151 if (ncommits == s->selected) {
2152 s->selected_entry = entry;
2153 break;
2155 entry = TAILQ_NEXT(entry, entry);
2156 ncommits++;
2160 static const struct got_error *
2161 draw_commits(struct tog_view *view)
2163 const struct got_error *err = NULL;
2164 struct tog_log_view_state *s = &view->state.log;
2165 struct commit_queue_entry *entry = s->selected_entry;
2166 const int limit = view->nlines;
2167 int width;
2168 int ncommits, author_cols = 4;
2169 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2170 char *refs_str = NULL;
2171 wchar_t *wline;
2172 struct tog_color *tc;
2173 static const size_t date_display_cols = 12;
2175 if (s->selected_entry &&
2176 !(view->searching && view->search_next_done == 0)) {
2177 struct got_reflist_head *refs;
2178 err = got_object_id_str(&id_str, s->selected_entry->id);
2179 if (err)
2180 return err;
2181 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2182 s->selected_entry->id);
2183 if (refs) {
2184 err = build_refs_str(&refs_str, refs,
2185 s->selected_entry->id, s->repo);
2186 if (err)
2187 goto done;
2191 if (s->thread_args.commits_needed == 0)
2192 halfdelay(10); /* disable fast refresh */
2194 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2195 if (asprintf(&ncommits_str, " [%d/%d] %s",
2196 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2197 (view->searching && !view->search_next_done) ?
2198 "searching..." : "loading...") == -1) {
2199 err = got_error_from_errno("asprintf");
2200 goto done;
2202 } else {
2203 const char *search_str = NULL;
2205 if (view->searching) {
2206 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2207 search_str = "no more matches";
2208 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2209 search_str = "no matches found";
2210 else if (!view->search_next_done)
2211 search_str = "searching...";
2214 if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 search_str ? search_str :
2217 (refs_str ? refs_str : "")) == -1) {
2218 err = got_error_from_errno("asprintf");
2219 goto done;
2223 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2224 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2225 "........................................",
2226 s->in_repo_path, ncommits_str) == -1) {
2227 err = got_error_from_errno("asprintf");
2228 header = NULL;
2229 goto done;
2231 } else if (asprintf(&header, "commit %s%s",
2232 id_str ? id_str : "........................................",
2233 ncommits_str) == -1) {
2234 err = got_error_from_errno("asprintf");
2235 header = NULL;
2236 goto done;
2238 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2239 if (err)
2240 goto done;
2242 werase(view->window);
2244 if (view_needs_focus_indication(view))
2245 wstandout(view->window);
2246 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2247 if (tc)
2248 wattr_on(view->window,
2249 COLOR_PAIR(tc->colorpair), NULL);
2250 waddwstr(view->window, wline);
2251 if (tc)
2252 wattr_off(view->window,
2253 COLOR_PAIR(tc->colorpair), NULL);
2254 while (width < view->ncols) {
2255 waddch(view->window, ' ');
2256 width++;
2258 if (view_needs_focus_indication(view))
2259 wstandend(view->window);
2260 free(wline);
2261 if (limit <= 1)
2262 goto done;
2264 /* Grow author column size if necessary, and set view->maxx. */
2265 entry = s->first_displayed_entry;
2266 ncommits = 0;
2267 view->maxx = 0;
2268 while (entry) {
2269 char *author, *eol, *msg, *msg0;
2270 wchar_t *wauthor, *wmsg;
2271 int width;
2272 if (ncommits >= limit - 1)
2273 break;
2274 author = strdup(got_object_commit_get_author(entry->commit));
2275 if (author == NULL) {
2276 err = got_error_from_errno("strdup");
2277 goto done;
2279 err = format_author(&wauthor, &width, author, COLS,
2280 date_display_cols);
2281 if (author_cols < width)
2282 author_cols = width;
2283 free(wauthor);
2284 free(author);
2285 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2286 if (err)
2287 goto done;
2288 msg = msg0;
2289 while (*msg == '\n')
2290 ++msg;
2291 if ((eol = strchr(msg, '\n')))
2292 *eol = '\0';
2293 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2294 date_display_cols + author_cols, 0);
2295 if (err)
2296 goto done;
2297 view->maxx = MAX(view->maxx, width);
2298 free(msg0);
2299 free(wmsg);
2300 ncommits++;
2301 entry = TAILQ_NEXT(entry, entry);
2304 entry = s->first_displayed_entry;
2305 s->last_displayed_entry = s->first_displayed_entry;
2306 ncommits = 0;
2307 while (entry) {
2308 if (ncommits >= limit - 1)
2309 break;
2310 if (ncommits == s->selected)
2311 wstandout(view->window);
2312 err = draw_commit(view, entry->commit, entry->id,
2313 date_display_cols, author_cols);
2314 if (ncommits == s->selected)
2315 wstandend(view->window);
2316 if (err)
2317 goto done;
2318 ncommits++;
2319 s->last_displayed_entry = entry;
2320 entry = TAILQ_NEXT(entry, entry);
2323 view_border(view);
2324 done:
2325 free(id_str);
2326 free(refs_str);
2327 free(ncommits_str);
2328 free(header);
2329 return err;
2332 static void
2333 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2335 struct commit_queue_entry *entry;
2336 int nscrolled = 0;
2338 entry = TAILQ_FIRST(&s->commits.head);
2339 if (s->first_displayed_entry == entry)
2340 return;
2342 entry = s->first_displayed_entry;
2343 while (entry && nscrolled < maxscroll) {
2344 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2345 if (entry) {
2346 s->first_displayed_entry = entry;
2347 nscrolled++;
2352 static const struct got_error *
2353 trigger_log_thread(struct tog_view *view, int wait)
2355 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2356 int errcode;
2358 halfdelay(1); /* fast refresh while loading commits */
2360 while (!ta->log_complete && !tog_thread_error &&
2361 (ta->commits_needed > 0 || ta->load_all)) {
2362 /* Wake the log thread. */
2363 errcode = pthread_cond_signal(&ta->need_commits);
2364 if (errcode)
2365 return got_error_set_errno(errcode,
2366 "pthread_cond_signal");
2369 * The mutex will be released while the view loop waits
2370 * in wgetch(), at which time the log thread will run.
2372 if (!wait)
2373 break;
2375 /* Display progress update in log view. */
2376 show_log_view(view);
2377 update_panels();
2378 doupdate();
2380 /* Wait right here while next commit is being loaded. */
2381 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2382 if (errcode)
2383 return got_error_set_errno(errcode,
2384 "pthread_cond_wait");
2386 /* Display progress update in log view. */
2387 show_log_view(view);
2388 update_panels();
2389 doupdate();
2392 return NULL;
2395 static const struct got_error *
2396 request_log_commits(struct tog_view *view)
2398 struct tog_log_view_state *state = &view->state.log;
2399 const struct got_error *err = NULL;
2401 state->thread_args.commits_needed = view->nscrolled;
2402 err = trigger_log_thread(view, 1);
2403 view->nscrolled = 0;
2405 return err;
2408 static const struct got_error *
2409 log_scroll_down(struct tog_view *view, int maxscroll)
2411 struct tog_log_view_state *s = &view->state.log;
2412 const struct got_error *err = NULL;
2413 struct commit_queue_entry *pentry;
2414 int nscrolled = 0, ncommits_needed;
2416 if (s->last_displayed_entry == NULL)
2417 return NULL;
2419 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2420 if (s->commits.ncommits < ncommits_needed &&
2421 !s->thread_args.log_complete) {
2423 * Ask the log thread for required amount of commits.
2425 s->thread_args.commits_needed += maxscroll;
2426 err = trigger_log_thread(view, 1);
2427 if (err)
2428 return err;
2431 do {
2432 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2433 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2434 break;
2436 s->last_displayed_entry = pentry ?
2437 pentry : s->last_displayed_entry;;
2439 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2440 if (pentry == NULL)
2441 break;
2442 s->first_displayed_entry = pentry;
2443 } while (++nscrolled < maxscroll);
2445 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2446 view->nscrolled += nscrolled;
2447 else
2448 view->nscrolled = 0;
2450 return err;
2453 static const struct got_error *
2454 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2455 struct got_commit_object *commit, struct got_object_id *commit_id,
2456 struct tog_view *log_view, struct got_repository *repo)
2458 const struct got_error *err;
2459 struct got_object_qid *parent_id;
2460 struct tog_view *diff_view;
2462 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2463 if (diff_view == NULL)
2464 return got_error_from_errno("view_open");
2466 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2467 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2468 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2469 if (err == NULL)
2470 *new_view = diff_view;
2471 return err;
2474 static const struct got_error *
2475 tree_view_visit_subtree(struct tog_tree_view_state *s,
2476 struct got_tree_object *subtree)
2478 struct tog_parent_tree *parent;
2480 parent = calloc(1, sizeof(*parent));
2481 if (parent == NULL)
2482 return got_error_from_errno("calloc");
2484 parent->tree = s->tree;
2485 parent->first_displayed_entry = s->first_displayed_entry;
2486 parent->selected_entry = s->selected_entry;
2487 parent->selected = s->selected;
2488 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2489 s->tree = subtree;
2490 s->selected = 0;
2491 s->first_displayed_entry = NULL;
2492 return NULL;
2495 static const struct got_error *
2496 tree_view_walk_path(struct tog_tree_view_state *s,
2497 struct got_commit_object *commit, const char *path)
2499 const struct got_error *err = NULL;
2500 struct got_tree_object *tree = NULL;
2501 const char *p;
2502 char *slash, *subpath = NULL;
2504 /* Walk the path and open corresponding tree objects. */
2505 p = path;
2506 while (*p) {
2507 struct got_tree_entry *te;
2508 struct got_object_id *tree_id;
2509 char *te_name;
2511 while (p[0] == '/')
2512 p++;
2514 /* Ensure the correct subtree entry is selected. */
2515 slash = strchr(p, '/');
2516 if (slash == NULL)
2517 te_name = strdup(p);
2518 else
2519 te_name = strndup(p, slash - p);
2520 if (te_name == NULL) {
2521 err = got_error_from_errno("strndup");
2522 break;
2524 te = got_object_tree_find_entry(s->tree, te_name);
2525 if (te == NULL) {
2526 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2527 free(te_name);
2528 break;
2530 free(te_name);
2531 s->first_displayed_entry = s->selected_entry = te;
2533 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2534 break; /* jump to this file's entry */
2536 slash = strchr(p, '/');
2537 if (slash)
2538 subpath = strndup(path, slash - path);
2539 else
2540 subpath = strdup(path);
2541 if (subpath == NULL) {
2542 err = got_error_from_errno("strdup");
2543 break;
2546 err = got_object_id_by_path(&tree_id, s->repo, commit,
2547 subpath);
2548 if (err)
2549 break;
2551 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2552 free(tree_id);
2553 if (err)
2554 break;
2556 err = tree_view_visit_subtree(s, tree);
2557 if (err) {
2558 got_object_tree_close(tree);
2559 break;
2561 if (slash == NULL)
2562 break;
2563 free(subpath);
2564 subpath = NULL;
2565 p = slash;
2568 free(subpath);
2569 return err;
2572 static const struct got_error *
2573 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2574 struct commit_queue_entry *entry, const char *path,
2575 const char *head_ref_name, struct got_repository *repo)
2577 const struct got_error *err = NULL;
2578 struct tog_tree_view_state *s;
2579 struct tog_view *tree_view;
2581 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2582 if (tree_view == NULL)
2583 return got_error_from_errno("view_open");
2585 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2586 if (err)
2587 return err;
2588 s = &tree_view->state.tree;
2590 *new_view = tree_view;
2592 if (got_path_is_root_dir(path))
2593 return NULL;
2595 return tree_view_walk_path(s, entry->commit, path);
2598 static const struct got_error *
2599 block_signals_used_by_main_thread(void)
2601 sigset_t sigset;
2602 int errcode;
2604 if (sigemptyset(&sigset) == -1)
2605 return got_error_from_errno("sigemptyset");
2607 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2608 if (sigaddset(&sigset, SIGWINCH) == -1)
2609 return got_error_from_errno("sigaddset");
2610 if (sigaddset(&sigset, SIGCONT) == -1)
2611 return got_error_from_errno("sigaddset");
2612 if (sigaddset(&sigset, SIGINT) == -1)
2613 return got_error_from_errno("sigaddset");
2614 if (sigaddset(&sigset, SIGTERM) == -1)
2615 return got_error_from_errno("sigaddset");
2617 /* ncurses handles SIGTSTP */
2618 if (sigaddset(&sigset, SIGTSTP) == -1)
2619 return got_error_from_errno("sigaddset");
2621 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2622 if (errcode)
2623 return got_error_set_errno(errcode, "pthread_sigmask");
2625 return NULL;
2628 static void *
2629 log_thread(void *arg)
2631 const struct got_error *err = NULL;
2632 int errcode = 0;
2633 struct tog_log_thread_args *a = arg;
2634 int done = 0;
2637 * Sync startup with main thread such that we begin our
2638 * work once view_input() has released the mutex.
2640 errcode = pthread_mutex_lock(&tog_mutex);
2641 if (errcode) {
2642 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2643 return (void *)err;
2646 err = block_signals_used_by_main_thread();
2647 if (err) {
2648 pthread_mutex_unlock(&tog_mutex);
2649 goto done;
2652 while (!done && !err && !tog_fatal_signal_received()) {
2653 errcode = pthread_mutex_unlock(&tog_mutex);
2654 if (errcode) {
2655 err = got_error_set_errno(errcode,
2656 "pthread_mutex_unlock");
2657 goto done;
2659 err = queue_commits(a);
2660 if (err) {
2661 if (err->code != GOT_ERR_ITER_COMPLETED)
2662 goto done;
2663 err = NULL;
2664 done = 1;
2665 } else if (a->commits_needed > 0 && !a->load_all)
2666 a->commits_needed--;
2668 errcode = pthread_mutex_lock(&tog_mutex);
2669 if (errcode) {
2670 err = got_error_set_errno(errcode,
2671 "pthread_mutex_lock");
2672 goto done;
2673 } else if (*a->quit)
2674 done = 1;
2675 else if (*a->first_displayed_entry == NULL) {
2676 *a->first_displayed_entry =
2677 TAILQ_FIRST(&a->commits->head);
2678 *a->selected_entry = *a->first_displayed_entry;
2681 errcode = pthread_cond_signal(&a->commit_loaded);
2682 if (errcode) {
2683 err = got_error_set_errno(errcode,
2684 "pthread_cond_signal");
2685 pthread_mutex_unlock(&tog_mutex);
2686 goto done;
2689 if (done)
2690 a->commits_needed = 0;
2691 else {
2692 if (a->commits_needed == 0 && !a->load_all) {
2693 errcode = pthread_cond_wait(&a->need_commits,
2694 &tog_mutex);
2695 if (errcode) {
2696 err = got_error_set_errno(errcode,
2697 "pthread_cond_wait");
2698 pthread_mutex_unlock(&tog_mutex);
2699 goto done;
2701 if (*a->quit)
2702 done = 1;
2706 a->log_complete = 1;
2707 errcode = pthread_mutex_unlock(&tog_mutex);
2708 if (errcode)
2709 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2710 done:
2711 if (err) {
2712 tog_thread_error = 1;
2713 pthread_cond_signal(&a->commit_loaded);
2715 return (void *)err;
2718 static const struct got_error *
2719 stop_log_thread(struct tog_log_view_state *s)
2721 const struct got_error *err = NULL, *thread_err = NULL;
2722 int errcode;
2724 if (s->thread) {
2725 s->quit = 1;
2726 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2727 if (errcode)
2728 return got_error_set_errno(errcode,
2729 "pthread_cond_signal");
2730 errcode = pthread_mutex_unlock(&tog_mutex);
2731 if (errcode)
2732 return got_error_set_errno(errcode,
2733 "pthread_mutex_unlock");
2734 errcode = pthread_join(s->thread, (void **)&thread_err);
2735 if (errcode)
2736 return got_error_set_errno(errcode, "pthread_join");
2737 errcode = pthread_mutex_lock(&tog_mutex);
2738 if (errcode)
2739 return got_error_set_errno(errcode,
2740 "pthread_mutex_lock");
2741 s->thread = NULL;
2744 if (s->thread_args.repo) {
2745 err = got_repo_close(s->thread_args.repo);
2746 s->thread_args.repo = NULL;
2749 if (s->thread_args.pack_fds) {
2750 const struct got_error *pack_err =
2751 got_repo_pack_fds_close(s->thread_args.pack_fds);
2752 if (err == NULL)
2753 err = pack_err;
2754 s->thread_args.pack_fds = NULL;
2757 if (s->thread_args.graph) {
2758 got_commit_graph_close(s->thread_args.graph);
2759 s->thread_args.graph = NULL;
2762 return err ? err : thread_err;
2765 static const struct got_error *
2766 close_log_view(struct tog_view *view)
2768 const struct got_error *err = NULL;
2769 struct tog_log_view_state *s = &view->state.log;
2770 int errcode;
2772 err = stop_log_thread(s);
2774 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2775 if (errcode && err == NULL)
2776 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2778 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2779 if (errcode && err == NULL)
2780 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2782 free_commits(&s->commits);
2783 free(s->in_repo_path);
2784 s->in_repo_path = NULL;
2785 free(s->start_id);
2786 s->start_id = NULL;
2787 free(s->head_ref_name);
2788 s->head_ref_name = NULL;
2789 return err;
2792 static const struct got_error *
2793 search_start_log_view(struct tog_view *view)
2795 struct tog_log_view_state *s = &view->state.log;
2797 s->matched_entry = NULL;
2798 s->search_entry = NULL;
2799 return NULL;
2802 static const struct got_error *
2803 search_next_log_view(struct tog_view *view)
2805 const struct got_error *err = NULL;
2806 struct tog_log_view_state *s = &view->state.log;
2807 struct commit_queue_entry *entry;
2809 /* Display progress update in log view. */
2810 show_log_view(view);
2811 update_panels();
2812 doupdate();
2814 if (s->search_entry) {
2815 int errcode, ch;
2816 errcode = pthread_mutex_unlock(&tog_mutex);
2817 if (errcode)
2818 return got_error_set_errno(errcode,
2819 "pthread_mutex_unlock");
2820 ch = wgetch(view->window);
2821 errcode = pthread_mutex_lock(&tog_mutex);
2822 if (errcode)
2823 return got_error_set_errno(errcode,
2824 "pthread_mutex_lock");
2825 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2826 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2827 return NULL;
2829 if (view->searching == TOG_SEARCH_FORWARD)
2830 entry = TAILQ_NEXT(s->search_entry, entry);
2831 else
2832 entry = TAILQ_PREV(s->search_entry,
2833 commit_queue_head, entry);
2834 } else if (s->matched_entry) {
2835 int matched_idx = s->matched_entry->idx;
2836 int selected_idx = s->selected_entry->idx;
2839 * If the user has moved the cursor after we hit a match,
2840 * the position from where we should continue searching
2841 * might have changed.
2843 if (view->searching == TOG_SEARCH_FORWARD) {
2844 if (matched_idx > selected_idx)
2845 entry = TAILQ_NEXT(s->selected_entry, entry);
2846 else
2847 entry = TAILQ_NEXT(s->matched_entry, entry);
2848 } else {
2849 if (matched_idx < selected_idx)
2850 entry = TAILQ_PREV(s->selected_entry,
2851 commit_queue_head, entry);
2852 else
2853 entry = TAILQ_PREV(s->matched_entry,
2854 commit_queue_head, entry);
2856 } else {
2857 entry = s->selected_entry;
2860 while (1) {
2861 int have_match = 0;
2863 if (entry == NULL) {
2864 if (s->thread_args.log_complete ||
2865 view->searching == TOG_SEARCH_BACKWARD) {
2866 view->search_next_done =
2867 (s->matched_entry == NULL ?
2868 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2869 s->search_entry = NULL;
2870 return NULL;
2873 * Poke the log thread for more commits and return,
2874 * allowing the main loop to make progress. Search
2875 * will resume at s->search_entry once we come back.
2877 s->thread_args.commits_needed++;
2878 return trigger_log_thread(view, 0);
2881 err = match_commit(&have_match, entry->id, entry->commit,
2882 &view->regex);
2883 if (err)
2884 break;
2885 if (have_match) {
2886 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2887 s->matched_entry = entry;
2888 break;
2891 s->search_entry = entry;
2892 if (view->searching == TOG_SEARCH_FORWARD)
2893 entry = TAILQ_NEXT(entry, entry);
2894 else
2895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2898 if (s->matched_entry) {
2899 int cur = s->selected_entry->idx;
2900 while (cur < s->matched_entry->idx) {
2901 err = input_log_view(NULL, view, KEY_DOWN);
2902 if (err)
2903 return err;
2904 cur++;
2906 while (cur > s->matched_entry->idx) {
2907 err = input_log_view(NULL, view, KEY_UP);
2908 if (err)
2909 return err;
2910 cur--;
2914 s->search_entry = NULL;
2916 return NULL;
2919 static const struct got_error *
2920 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2921 struct got_repository *repo, const char *head_ref_name,
2922 const char *in_repo_path, int log_branches)
2924 const struct got_error *err = NULL;
2925 struct tog_log_view_state *s = &view->state.log;
2926 struct got_repository *thread_repo = NULL;
2927 struct got_commit_graph *thread_graph = NULL;
2928 int errcode;
2930 if (in_repo_path != s->in_repo_path) {
2931 free(s->in_repo_path);
2932 s->in_repo_path = strdup(in_repo_path);
2933 if (s->in_repo_path == NULL)
2934 return got_error_from_errno("strdup");
2937 /* The commit queue only contains commits being displayed. */
2938 TAILQ_INIT(&s->commits.head);
2939 s->commits.ncommits = 0;
2941 s->repo = repo;
2942 if (head_ref_name) {
2943 s->head_ref_name = strdup(head_ref_name);
2944 if (s->head_ref_name == NULL) {
2945 err = got_error_from_errno("strdup");
2946 goto done;
2949 s->start_id = got_object_id_dup(start_id);
2950 if (s->start_id == NULL) {
2951 err = got_error_from_errno("got_object_id_dup");
2952 goto done;
2954 s->log_branches = log_branches;
2956 STAILQ_INIT(&s->colors);
2957 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2958 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2959 get_color_value("TOG_COLOR_COMMIT"));
2960 if (err)
2961 goto done;
2962 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2963 get_color_value("TOG_COLOR_AUTHOR"));
2964 if (err) {
2965 free_colors(&s->colors);
2966 goto done;
2968 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2969 get_color_value("TOG_COLOR_DATE"));
2970 if (err) {
2971 free_colors(&s->colors);
2972 goto done;
2976 view->show = show_log_view;
2977 view->input = input_log_view;
2978 view->close = close_log_view;
2979 view->search_start = search_start_log_view;
2980 view->search_next = search_next_log_view;
2982 if (s->thread_args.pack_fds == NULL) {
2983 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2984 if (err)
2985 goto done;
2987 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2988 s->thread_args.pack_fds);
2989 if (err)
2990 goto done;
2991 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2992 !s->log_branches);
2993 if (err)
2994 goto done;
2995 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2996 s->repo, NULL, NULL);
2997 if (err)
2998 goto done;
3000 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3001 if (errcode) {
3002 err = got_error_set_errno(errcode, "pthread_cond_init");
3003 goto done;
3005 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3006 if (errcode) {
3007 err = got_error_set_errno(errcode, "pthread_cond_init");
3008 goto done;
3011 s->thread_args.commits_needed = view->nlines;
3012 s->thread_args.graph = thread_graph;
3013 s->thread_args.commits = &s->commits;
3014 s->thread_args.in_repo_path = s->in_repo_path;
3015 s->thread_args.start_id = s->start_id;
3016 s->thread_args.repo = thread_repo;
3017 s->thread_args.log_complete = 0;
3018 s->thread_args.quit = &s->quit;
3019 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3020 s->thread_args.selected_entry = &s->selected_entry;
3021 s->thread_args.searching = &view->searching;
3022 s->thread_args.search_next_done = &view->search_next_done;
3023 s->thread_args.regex = &view->regex;
3024 done:
3025 if (err)
3026 close_log_view(view);
3027 return err;
3030 static const struct got_error *
3031 show_log_view(struct tog_view *view)
3033 const struct got_error *err;
3034 struct tog_log_view_state *s = &view->state.log;
3036 if (s->thread == NULL) {
3037 int errcode = pthread_create(&s->thread, NULL, log_thread,
3038 &s->thread_args);
3039 if (errcode)
3040 return got_error_set_errno(errcode, "pthread_create");
3041 if (s->thread_args.commits_needed > 0) {
3042 err = trigger_log_thread(view, 1);
3043 if (err)
3044 return err;
3048 return draw_commits(view);
3051 static void
3052 log_move_cursor_up(struct tog_view *view, int page, int home)
3054 struct tog_log_view_state *s = &view->state.log;
3056 if (s->selected_entry->idx == 0)
3057 view->count = 0;
3058 if (s->first_displayed_entry == NULL)
3059 return;
3061 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3062 || home)
3063 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3065 if (!page && !home && s->selected > 0)
3066 --s->selected;
3067 else
3068 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3070 select_commit(s);
3071 return;
3074 static const struct got_error *
3075 log_move_cursor_down(struct tog_view *view, int page)
3077 struct tog_log_view_state *s = &view->state.log;
3078 struct commit_queue_entry *first;
3079 const struct got_error *err = NULL;
3081 first = s->first_displayed_entry;
3082 if (first == NULL) {
3083 view->count = 0;
3084 return NULL;
3087 if (s->thread_args.log_complete &&
3088 s->selected_entry->idx >= s->commits.ncommits - 1)
3089 return NULL;
3091 if (!page) {
3092 int eos = view->nlines - 2;
3094 if (view_is_hsplit_top(view))
3095 --eos; /* border consumes the last line */
3096 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3097 ++s->selected;
3098 else
3099 err = log_scroll_down(view, 1);
3100 } else if (s->thread_args.load_all) {
3101 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3102 s->selected += MIN(s->last_displayed_entry->idx -
3103 s->selected_entry->idx, page + 1);
3104 else
3105 err = log_scroll_down(view, MIN(page,
3106 s->commits.ncommits - s->selected_entry->idx - 1));
3107 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3108 } else {
3109 err = log_scroll_down(view, page);
3110 if (err)
3111 return err;
3112 if (first == s->first_displayed_entry && s->selected <
3113 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3114 s->selected = MIN(s->commits.ncommits - 1, page);
3117 if (err)
3118 return err;
3121 * We might necessarily overshoot in horizontal
3122 * splits; if so, select the last displayed commit.
3124 s->selected = MIN(s->selected,
3125 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3127 select_commit(s);
3129 if (s->thread_args.log_complete &&
3130 s->selected_entry->idx == s->commits.ncommits - 1)
3131 view->count = 0;
3133 return NULL;
3136 static void
3137 view_get_split(struct tog_view *view, int *y, int *x)
3139 *x = 0;
3140 *y = 0;
3142 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3143 if (view->child && view->child->resized_y)
3144 *y = view->child->resized_y;
3145 else if (view->resized_y)
3146 *y = view->resized_y;
3147 else
3148 *y = view_split_begin_y(view->lines);
3149 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3150 if (view->child && view->child->resized_x)
3151 *x = view->child->resized_x;
3152 else if (view->resized_x)
3153 *x = view->resized_x;
3154 else
3155 *x = view_split_begin_x(view->begin_x);
3159 /* Split view horizontally at y and offset view->state->selected line. */
3160 static const struct got_error *
3161 view_init_hsplit(struct tog_view *view, int y)
3163 const struct got_error *err = NULL;
3165 view->nlines = y;
3166 view->ncols = COLS;
3167 err = view_resize(view);
3168 if (err)
3169 return err;
3171 err = offset_selection_down(view);
3173 return err;
3176 static const struct got_error *
3177 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3179 const struct got_error *err = NULL;
3180 struct tog_log_view_state *s = &view->state.log;
3181 struct tog_view *diff_view = NULL, *tree_view = NULL;
3182 struct tog_view *ref_view = NULL;
3183 struct commit_queue_entry *entry;
3184 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3186 if (s->thread_args.load_all) {
3187 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3188 s->thread_args.load_all = 0;
3189 else if (s->thread_args.log_complete) {
3190 err = log_move_cursor_down(view, s->commits.ncommits);
3191 s->thread_args.load_all = 0;
3193 return err;
3196 eos = nscroll = view->nlines - 1;
3197 if (view_is_hsplit_top(view))
3198 --eos; /* border */
3200 switch (ch) {
3201 case 'q':
3202 s->quit = 1;
3203 break;
3204 case '0':
3205 view->x = 0;
3206 break;
3207 case '$':
3208 view->x = MAX(view->maxx - view->ncols / 2, 0);
3209 view->count = 0;
3210 break;
3211 case KEY_RIGHT:
3212 case 'l':
3213 if (view->x + view->ncols / 2 < view->maxx)
3214 view->x += 2; /* move two columns right */
3215 else
3216 view->count = 0;
3217 break;
3218 case KEY_LEFT:
3219 case 'h':
3220 view->x -= MIN(view->x, 2); /* move two columns back */
3221 if (view->x <= 0)
3222 view->count = 0;
3223 break;
3224 case 'k':
3225 case KEY_UP:
3226 case '<':
3227 case ',':
3228 case CTRL('p'):
3229 log_move_cursor_up(view, 0, 0);
3230 break;
3231 case 'g':
3232 case KEY_HOME:
3233 log_move_cursor_up(view, 0, 1);
3234 view->count = 0;
3235 break;
3236 case CTRL('u'):
3237 case 'u':
3238 nscroll /= 2;
3239 /* FALL THROUGH */
3240 case KEY_PPAGE:
3241 case CTRL('b'):
3242 case 'b':
3243 log_move_cursor_up(view, nscroll, 0);
3244 break;
3245 case 'j':
3246 case KEY_DOWN:
3247 case '>':
3248 case '.':
3249 case CTRL('n'):
3250 err = log_move_cursor_down(view, 0);
3251 break;
3252 case 'G':
3253 case KEY_END: {
3254 /* We don't know yet how many commits, so we're forced to
3255 * traverse them all. */
3256 view->count = 0;
3257 if (!s->thread_args.log_complete) {
3258 s->thread_args.load_all = 1;
3259 return trigger_log_thread(view, 0);
3262 s->selected = 0;
3263 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3264 for (n = 0; n < eos; n++) {
3265 if (entry == NULL)
3266 break;
3267 s->first_displayed_entry = entry;
3268 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3270 if (n > 0)
3271 s->selected = n - 1;
3272 select_commit(s);
3273 break;
3275 case CTRL('d'):
3276 case 'd':
3277 nscroll /= 2;
3278 /* FALL THROUGH */
3279 case KEY_NPAGE:
3280 case CTRL('f'):
3281 case 'f':
3282 case ' ':
3283 err = log_move_cursor_down(view, nscroll);
3284 break;
3285 case KEY_RESIZE:
3286 if (s->selected > view->nlines - 2)
3287 s->selected = view->nlines - 2;
3288 if (s->selected > s->commits.ncommits - 1)
3289 s->selected = s->commits.ncommits - 1;
3290 select_commit(s);
3291 if (s->commits.ncommits < view->nlines - 1 &&
3292 !s->thread_args.log_complete) {
3293 s->thread_args.commits_needed += (view->nlines - 1) -
3294 s->commits.ncommits;
3295 err = trigger_log_thread(view, 1);
3297 break;
3298 case KEY_ENTER:
3299 case '\r':
3300 view->count = 0;
3301 if (s->selected_entry == NULL)
3302 break;
3304 /* get dimensions--don't split till initialisation succeeds */
3305 if (view_is_parent_view(view))
3306 view_get_split(view, &begin_y, &begin_x);
3308 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3309 s->selected_entry->commit, s->selected_entry->id,
3310 view, s->repo);
3311 if (err)
3312 break;
3314 if (view_is_parent_view(view) &&
3315 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3316 err = view_init_hsplit(view, begin_y);
3317 if (err)
3318 break;
3321 view->focussed = 0;
3322 diff_view->focussed = 1;
3323 diff_view->mode = view->mode;
3324 diff_view->nlines = view->lines - begin_y;
3326 if (view_is_parent_view(view)) {
3327 view_transfer_size(diff_view, view);
3328 err = view_close_child(view);
3329 if (err)
3330 return err;
3331 err = view_set_child(view, diff_view);
3332 if (err)
3333 return err;
3334 view->focus_child = 1;
3335 } else
3336 *new_view = diff_view;
3337 break;
3338 case 't':
3339 view->count = 0;
3340 if (s->selected_entry == NULL)
3341 break;
3342 if (view_is_parent_view(view))
3343 view_get_split(view, &begin_y, &begin_x);
3344 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3345 s->selected_entry, s->in_repo_path, s->head_ref_name,
3346 s->repo);
3347 if (err)
3348 break;
3349 if (view_is_parent_view(view) &&
3350 view->mode == TOG_VIEW_SPLIT_HRZN) {
3351 err = view_init_hsplit(view, begin_y);
3352 if (err)
3353 break;
3355 view->focussed = 0;
3356 tree_view->focussed = 1;
3357 tree_view->mode = view->mode;
3358 tree_view->nlines = view->lines - begin_y;
3359 if (view_is_parent_view(view)) {
3360 view_transfer_size(tree_view, view);
3361 err = view_close_child(view);
3362 if (err)
3363 return err;
3364 err = view_set_child(view, tree_view);
3365 if (err)
3366 return err;
3367 view->focus_child = 1;
3368 } else
3369 *new_view = tree_view;
3370 break;
3371 case KEY_BACKSPACE:
3372 case CTRL('l'):
3373 case 'B':
3374 view->count = 0;
3375 if (ch == KEY_BACKSPACE &&
3376 got_path_is_root_dir(s->in_repo_path))
3377 break;
3378 err = stop_log_thread(s);
3379 if (err)
3380 return err;
3381 if (ch == KEY_BACKSPACE) {
3382 char *parent_path;
3383 err = got_path_dirname(&parent_path, s->in_repo_path);
3384 if (err)
3385 return err;
3386 free(s->in_repo_path);
3387 s->in_repo_path = parent_path;
3388 s->thread_args.in_repo_path = s->in_repo_path;
3389 } else if (ch == CTRL('l')) {
3390 struct got_object_id *start_id;
3391 err = got_repo_match_object_id(&start_id, NULL,
3392 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3393 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3394 if (err)
3395 return err;
3396 free(s->start_id);
3397 s->start_id = start_id;
3398 s->thread_args.start_id = s->start_id;
3399 } else /* 'B' */
3400 s->log_branches = !s->log_branches;
3402 if (s->thread_args.pack_fds == NULL) {
3403 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3404 if (err)
3405 return err;
3407 err = got_repo_open(&s->thread_args.repo,
3408 got_repo_get_path(s->repo), NULL,
3409 s->thread_args.pack_fds);
3410 if (err)
3411 return err;
3412 tog_free_refs();
3413 err = tog_load_refs(s->repo, 0);
3414 if (err)
3415 return err;
3416 err = got_commit_graph_open(&s->thread_args.graph,
3417 s->in_repo_path, !s->log_branches);
3418 if (err)
3419 return err;
3420 err = got_commit_graph_iter_start(s->thread_args.graph,
3421 s->start_id, s->repo, NULL, NULL);
3422 if (err)
3423 return err;
3424 free_commits(&s->commits);
3425 s->first_displayed_entry = NULL;
3426 s->last_displayed_entry = NULL;
3427 s->selected_entry = NULL;
3428 s->selected = 0;
3429 s->thread_args.log_complete = 0;
3430 s->quit = 0;
3431 s->thread_args.commits_needed = view->lines;
3432 s->matched_entry = NULL;
3433 s->search_entry = NULL;
3434 break;
3435 case 'r':
3436 view->count = 0;
3437 if (view_is_parent_view(view))
3438 view_get_split(view, &begin_y, &begin_x);
3439 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3440 if (ref_view == NULL)
3441 return got_error_from_errno("view_open");
3442 err = open_ref_view(ref_view, s->repo);
3443 if (err) {
3444 view_close(ref_view);
3445 return err;
3447 if (view_is_parent_view(view) &&
3448 view->mode == TOG_VIEW_SPLIT_HRZN) {
3449 err = view_init_hsplit(view, begin_y);
3450 if (err)
3451 break;
3453 view->focussed = 0;
3454 ref_view->focussed = 1;
3455 ref_view->mode = view->mode;
3456 ref_view->nlines = view->lines - begin_y;
3457 if (view_is_parent_view(view)) {
3458 view_transfer_size(ref_view, view);
3459 err = view_close_child(view);
3460 if (err)
3461 return err;
3462 err = view_set_child(view, ref_view);
3463 if (err)
3464 return err;
3465 view->focus_child = 1;
3466 } else
3467 *new_view = ref_view;
3468 break;
3469 default:
3470 view->count = 0;
3471 break;
3474 return err;
3477 static const struct got_error *
3478 apply_unveil(const char *repo_path, const char *worktree_path)
3480 const struct got_error *error;
3482 #ifdef PROFILE
3483 if (unveil("gmon.out", "rwc") != 0)
3484 return got_error_from_errno2("unveil", "gmon.out");
3485 #endif
3486 if (repo_path && unveil(repo_path, "r") != 0)
3487 return got_error_from_errno2("unveil", repo_path);
3489 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3490 return got_error_from_errno2("unveil", worktree_path);
3492 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3493 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3495 error = got_privsep_unveil_exec_helpers();
3496 if (error != NULL)
3497 return error;
3499 if (unveil(NULL, NULL) != 0)
3500 return got_error_from_errno("unveil");
3502 return NULL;
3505 static void
3506 init_curses(void)
3509 * Override default signal handlers before starting ncurses.
3510 * This should prevent ncurses from installing its own
3511 * broken cleanup() signal handler.
3513 signal(SIGWINCH, tog_sigwinch);
3514 signal(SIGPIPE, tog_sigpipe);
3515 signal(SIGCONT, tog_sigcont);
3516 signal(SIGINT, tog_sigint);
3517 signal(SIGTERM, tog_sigterm);
3519 initscr();
3520 cbreak();
3521 halfdelay(1); /* Do fast refresh while initial view is loading. */
3522 noecho();
3523 nonl();
3524 intrflush(stdscr, FALSE);
3525 keypad(stdscr, TRUE);
3526 curs_set(0);
3527 if (getenv("TOG_COLORS") != NULL) {
3528 start_color();
3529 use_default_colors();
3533 static const struct got_error *
3534 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3535 struct got_repository *repo, struct got_worktree *worktree)
3537 const struct got_error *err = NULL;
3539 if (argc == 0) {
3540 *in_repo_path = strdup("/");
3541 if (*in_repo_path == NULL)
3542 return got_error_from_errno("strdup");
3543 return NULL;
3546 if (worktree) {
3547 const char *prefix = got_worktree_get_path_prefix(worktree);
3548 char *p;
3550 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3551 if (err)
3552 return err;
3553 if (asprintf(in_repo_path, "%s%s%s", prefix,
3554 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3555 p) == -1) {
3556 err = got_error_from_errno("asprintf");
3557 *in_repo_path = NULL;
3559 free(p);
3560 } else
3561 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3563 return err;
3566 static const struct got_error *
3567 cmd_log(int argc, char *argv[])
3569 const struct got_error *error;
3570 struct got_repository *repo = NULL;
3571 struct got_worktree *worktree = NULL;
3572 struct got_object_id *start_id = NULL;
3573 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3574 char *start_commit = NULL, *label = NULL;
3575 struct got_reference *ref = NULL;
3576 const char *head_ref_name = NULL;
3577 int ch, log_branches = 0;
3578 struct tog_view *view;
3579 int *pack_fds = NULL;
3581 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3582 switch (ch) {
3583 case 'b':
3584 log_branches = 1;
3585 break;
3586 case 'c':
3587 start_commit = optarg;
3588 break;
3589 case 'r':
3590 repo_path = realpath(optarg, NULL);
3591 if (repo_path == NULL)
3592 return got_error_from_errno2("realpath",
3593 optarg);
3594 break;
3595 default:
3596 usage_log();
3597 /* NOTREACHED */
3601 argc -= optind;
3602 argv += optind;
3604 if (argc > 1)
3605 usage_log();
3607 error = got_repo_pack_fds_open(&pack_fds);
3608 if (error != NULL)
3609 goto done;
3611 if (repo_path == NULL) {
3612 cwd = getcwd(NULL, 0);
3613 if (cwd == NULL)
3614 return got_error_from_errno("getcwd");
3615 error = got_worktree_open(&worktree, cwd);
3616 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3617 goto done;
3618 if (worktree)
3619 repo_path =
3620 strdup(got_worktree_get_repo_path(worktree));
3621 else
3622 repo_path = strdup(cwd);
3623 if (repo_path == NULL) {
3624 error = got_error_from_errno("strdup");
3625 goto done;
3629 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3630 if (error != NULL)
3631 goto done;
3633 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3634 repo, worktree);
3635 if (error)
3636 goto done;
3638 init_curses();
3640 error = apply_unveil(got_repo_get_path(repo),
3641 worktree ? got_worktree_get_root_path(worktree) : NULL);
3642 if (error)
3643 goto done;
3645 /* already loaded by tog_log_with_path()? */
3646 if (TAILQ_EMPTY(&tog_refs)) {
3647 error = tog_load_refs(repo, 0);
3648 if (error)
3649 goto done;
3652 if (start_commit == NULL) {
3653 error = got_repo_match_object_id(&start_id, &label,
3654 worktree ? got_worktree_get_head_ref_name(worktree) :
3655 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3656 if (error)
3657 goto done;
3658 head_ref_name = label;
3659 } else {
3660 error = got_ref_open(&ref, repo, start_commit, 0);
3661 if (error == NULL)
3662 head_ref_name = got_ref_get_name(ref);
3663 else if (error->code != GOT_ERR_NOT_REF)
3664 goto done;
3665 error = got_repo_match_object_id(&start_id, NULL,
3666 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3667 if (error)
3668 goto done;
3671 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3672 if (view == NULL) {
3673 error = got_error_from_errno("view_open");
3674 goto done;
3676 error = open_log_view(view, start_id, repo, head_ref_name,
3677 in_repo_path, log_branches);
3678 if (error)
3679 goto done;
3680 if (worktree) {
3681 /* Release work tree lock. */
3682 got_worktree_close(worktree);
3683 worktree = NULL;
3685 error = view_loop(view);
3686 done:
3687 free(in_repo_path);
3688 free(repo_path);
3689 free(cwd);
3690 free(start_id);
3691 free(label);
3692 if (ref)
3693 got_ref_close(ref);
3694 if (repo) {
3695 const struct got_error *close_err = got_repo_close(repo);
3696 if (error == NULL)
3697 error = close_err;
3699 if (worktree)
3700 got_worktree_close(worktree);
3701 if (pack_fds) {
3702 const struct got_error *pack_err =
3703 got_repo_pack_fds_close(pack_fds);
3704 if (error == NULL)
3705 error = pack_err;
3707 tog_free_refs();
3708 return error;
3711 __dead static void
3712 usage_diff(void)
3714 endwin();
3715 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3716 "[-w] object1 object2\n", getprogname());
3717 exit(1);
3720 static int
3721 match_line(const char *line, regex_t *regex, size_t nmatch,
3722 regmatch_t *regmatch)
3724 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3727 static struct tog_color *
3728 match_color(struct tog_colors *colors, const char *line)
3730 struct tog_color *tc = NULL;
3732 STAILQ_FOREACH(tc, colors, entry) {
3733 if (match_line(line, &tc->regex, 0, NULL))
3734 return tc;
3737 return NULL;
3740 static const struct got_error *
3741 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3742 WINDOW *window, int skipcol, regmatch_t *regmatch)
3744 const struct got_error *err = NULL;
3745 char *exstr = NULL;
3746 wchar_t *wline = NULL;
3747 int rme, rms, n, width, scrollx;
3748 int width0 = 0, width1 = 0, width2 = 0;
3749 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3751 *wtotal = 0;
3753 rms = regmatch->rm_so;
3754 rme = regmatch->rm_eo;
3756 err = expand_tab(&exstr, line);
3757 if (err)
3758 return err;
3760 /* Split the line into 3 segments, according to match offsets. */
3761 seg0 = strndup(exstr, rms);
3762 if (seg0 == NULL) {
3763 err = got_error_from_errno("strndup");
3764 goto done;
3766 seg1 = strndup(exstr + rms, rme - rms);
3767 if (seg1 == NULL) {
3768 err = got_error_from_errno("strndup");
3769 goto done;
3771 seg2 = strdup(exstr + rme);
3772 if (seg2 == NULL) {
3773 err = got_error_from_errno("strndup");
3774 goto done;
3777 /* draw up to matched token if we haven't scrolled past it */
3778 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3779 col_tab_align, 1);
3780 if (err)
3781 goto done;
3782 n = MAX(width0 - skipcol, 0);
3783 if (n) {
3784 free(wline);
3785 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3786 wlimit, col_tab_align, 1);
3787 if (err)
3788 goto done;
3789 waddwstr(window, &wline[scrollx]);
3790 wlimit -= width;
3791 *wtotal += width;
3794 if (wlimit > 0) {
3795 int i = 0, w = 0;
3796 size_t wlen;
3798 free(wline);
3799 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3800 col_tab_align, 1);
3801 if (err)
3802 goto done;
3803 wlen = wcslen(wline);
3804 while (i < wlen) {
3805 width = wcwidth(wline[i]);
3806 if (width == -1) {
3807 /* should not happen, tabs are expanded */
3808 err = got_error(GOT_ERR_RANGE);
3809 goto done;
3811 if (width0 + w + width > skipcol)
3812 break;
3813 w += width;
3814 i++;
3816 /* draw (visible part of) matched token (if scrolled into it) */
3817 if (width1 - w > 0) {
3818 wattron(window, A_STANDOUT);
3819 waddwstr(window, &wline[i]);
3820 wattroff(window, A_STANDOUT);
3821 wlimit -= (width1 - w);
3822 *wtotal += (width1 - w);
3826 if (wlimit > 0) { /* draw rest of line */
3827 free(wline);
3828 if (skipcol > width0 + width1) {
3829 err = format_line(&wline, &width2, &scrollx, seg2,
3830 skipcol - (width0 + width1), wlimit,
3831 col_tab_align, 1);
3832 if (err)
3833 goto done;
3834 waddwstr(window, &wline[scrollx]);
3835 } else {
3836 err = format_line(&wline, &width2, NULL, seg2, 0,
3837 wlimit, col_tab_align, 1);
3838 if (err)
3839 goto done;
3840 waddwstr(window, wline);
3842 *wtotal += width2;
3844 done:
3845 free(wline);
3846 free(exstr);
3847 free(seg0);
3848 free(seg1);
3849 free(seg2);
3850 return err;
3853 static const struct got_error *
3854 draw_file(struct tog_view *view, const char *header)
3856 struct tog_diff_view_state *s = &view->state.diff;
3857 regmatch_t *regmatch = &view->regmatch;
3858 const struct got_error *err;
3859 int nprinted = 0;
3860 char *line;
3861 size_t linesize = 0;
3862 ssize_t linelen;
3863 struct tog_color *tc;
3864 wchar_t *wline;
3865 int width;
3866 int max_lines = view->nlines;
3867 int nlines = s->nlines;
3868 off_t line_offset;
3870 line_offset = s->line_offsets[s->first_displayed_line - 1];
3871 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3872 return got_error_from_errno("fseek");
3874 werase(view->window);
3876 if (header) {
3877 if (asprintf(&line, "[%d/%d] %s",
3878 s->first_displayed_line - 1 + s->selected_line, nlines,
3879 header) == -1)
3880 return got_error_from_errno("asprintf");
3881 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3882 0, 0);
3883 free(line);
3884 if (err)
3885 return err;
3887 if (view_needs_focus_indication(view))
3888 wstandout(view->window);
3889 waddwstr(view->window, wline);
3890 free(wline);
3891 wline = NULL;
3892 if (view_needs_focus_indication(view))
3893 wstandend(view->window);
3894 if (width <= view->ncols - 1)
3895 waddch(view->window, '\n');
3897 if (max_lines <= 1)
3898 return NULL;
3899 max_lines--;
3902 s->eof = 0;
3903 view->maxx = 0;
3904 line = NULL;
3905 while (max_lines > 0 && nprinted < max_lines) {
3906 linelen = getline(&line, &linesize, s->f);
3907 if (linelen == -1) {
3908 if (feof(s->f)) {
3909 s->eof = 1;
3910 break;
3912 free(line);
3913 return got_ferror(s->f, GOT_ERR_IO);
3916 /* Set view->maxx based on full line length. */
3917 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3918 view->x ? 1 : 0);
3919 if (err) {
3920 free(line);
3921 return err;
3923 view->maxx = MAX(view->maxx, width);
3924 free(wline);
3925 wline = NULL;
3927 tc = match_color(&s->colors, line);
3928 if (tc)
3929 wattr_on(view->window,
3930 COLOR_PAIR(tc->colorpair), NULL);
3931 if (s->first_displayed_line + nprinted == s->matched_line &&
3932 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3933 err = add_matched_line(&width, line, view->ncols, 0,
3934 view->window, view->x, regmatch);
3935 if (err) {
3936 free(line);
3937 return err;
3939 } else {
3940 int skip;
3941 err = format_line(&wline, &width, &skip, line,
3942 view->x, view->ncols, 0, view->x ? 1 : 0);
3943 if (err) {
3944 free(line);
3945 return err;
3947 waddwstr(view->window, &wline[skip]);
3948 free(wline);
3949 wline = NULL;
3951 if (tc)
3952 wattr_off(view->window,
3953 COLOR_PAIR(tc->colorpair), NULL);
3954 if (width <= view->ncols - 1)
3955 waddch(view->window, '\n');
3956 nprinted++;
3958 free(line);
3959 if (nprinted >= 1)
3960 s->last_displayed_line = s->first_displayed_line +
3961 (nprinted - 1);
3962 else
3963 s->last_displayed_line = s->first_displayed_line;
3965 view_border(view);
3967 if (s->eof) {
3968 while (nprinted < view->nlines) {
3969 waddch(view->window, '\n');
3970 nprinted++;
3973 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3974 view->ncols, 0, 0);
3975 if (err) {
3976 return err;
3979 wstandout(view->window);
3980 waddwstr(view->window, wline);
3981 free(wline);
3982 wline = NULL;
3983 wstandend(view->window);
3986 return NULL;
3989 static char *
3990 get_datestr(time_t *time, char *datebuf)
3992 struct tm mytm, *tm;
3993 char *p, *s;
3995 tm = gmtime_r(time, &mytm);
3996 if (tm == NULL)
3997 return NULL;
3998 s = asctime_r(tm, datebuf);
3999 if (s == NULL)
4000 return NULL;
4001 p = strchr(s, '\n');
4002 if (p)
4003 *p = '\0';
4004 return s;
4007 static const struct got_error *
4008 get_changed_paths(struct got_pathlist_head *paths,
4009 struct got_commit_object *commit, struct got_repository *repo)
4011 const struct got_error *err = NULL;
4012 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4013 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4014 struct got_object_qid *qid;
4016 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4017 if (qid != NULL) {
4018 struct got_commit_object *pcommit;
4019 err = got_object_open_as_commit(&pcommit, repo,
4020 &qid->id);
4021 if (err)
4022 return err;
4024 tree_id1 = got_object_id_dup(
4025 got_object_commit_get_tree_id(pcommit));
4026 if (tree_id1 == NULL) {
4027 got_object_commit_close(pcommit);
4028 return got_error_from_errno("got_object_id_dup");
4030 got_object_commit_close(pcommit);
4034 if (tree_id1) {
4035 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4036 if (err)
4037 goto done;
4040 tree_id2 = got_object_commit_get_tree_id(commit);
4041 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4042 if (err)
4043 goto done;
4045 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4046 got_diff_tree_collect_changed_paths, paths, 0);
4047 done:
4048 if (tree1)
4049 got_object_tree_close(tree1);
4050 if (tree2)
4051 got_object_tree_close(tree2);
4052 free(tree_id1);
4053 return err;
4056 static const struct got_error *
4057 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4059 off_t *p;
4061 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4062 if (p == NULL)
4063 return got_error_from_errno("reallocarray");
4064 *line_offsets = p;
4065 (*line_offsets)[*nlines] = off;
4066 (*nlines)++;
4067 return NULL;
4070 static const struct got_error *
4071 write_commit_info(off_t **line_offsets, size_t *nlines,
4072 struct got_object_id *commit_id, struct got_reflist_head *refs,
4073 struct got_repository *repo, FILE *outfile)
4075 const struct got_error *err = NULL;
4076 char datebuf[26], *datestr;
4077 struct got_commit_object *commit;
4078 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4079 time_t committer_time;
4080 const char *author, *committer;
4081 char *refs_str = NULL;
4082 struct got_pathlist_head changed_paths;
4083 struct got_pathlist_entry *pe;
4084 off_t outoff = 0;
4085 int n;
4087 TAILQ_INIT(&changed_paths);
4089 if (refs) {
4090 err = build_refs_str(&refs_str, refs, commit_id, repo);
4091 if (err)
4092 return err;
4095 err = got_object_open_as_commit(&commit, repo, commit_id);
4096 if (err)
4097 return err;
4099 err = got_object_id_str(&id_str, commit_id);
4100 if (err) {
4101 err = got_error_from_errno("got_object_id_str");
4102 goto done;
4105 err = add_line_offset(line_offsets, nlines, 0);
4106 if (err)
4107 goto done;
4109 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4110 refs_str ? refs_str : "", refs_str ? ")" : "");
4111 if (n < 0) {
4112 err = got_error_from_errno("fprintf");
4113 goto done;
4115 outoff += n;
4116 err = add_line_offset(line_offsets, nlines, outoff);
4117 if (err)
4118 goto done;
4120 n = fprintf(outfile, "from: %s\n",
4121 got_object_commit_get_author(commit));
4122 if (n < 0) {
4123 err = got_error_from_errno("fprintf");
4124 goto done;
4126 outoff += n;
4127 err = add_line_offset(line_offsets, nlines, outoff);
4128 if (err)
4129 goto done;
4131 committer_time = got_object_commit_get_committer_time(commit);
4132 datestr = get_datestr(&committer_time, datebuf);
4133 if (datestr) {
4134 n = fprintf(outfile, "date: %s UTC\n", datestr);
4135 if (n < 0) {
4136 err = got_error_from_errno("fprintf");
4137 goto done;
4139 outoff += n;
4140 err = add_line_offset(line_offsets, nlines, outoff);
4141 if (err)
4142 goto done;
4144 author = got_object_commit_get_author(commit);
4145 committer = got_object_commit_get_committer(commit);
4146 if (strcmp(author, committer) != 0) {
4147 n = fprintf(outfile, "via: %s\n", committer);
4148 if (n < 0) {
4149 err = got_error_from_errno("fprintf");
4150 goto done;
4152 outoff += n;
4153 err = add_line_offset(line_offsets, nlines, outoff);
4154 if (err)
4155 goto done;
4157 if (got_object_commit_get_nparents(commit) > 1) {
4158 const struct got_object_id_queue *parent_ids;
4159 struct got_object_qid *qid;
4160 int pn = 1;
4161 parent_ids = got_object_commit_get_parent_ids(commit);
4162 STAILQ_FOREACH(qid, parent_ids, entry) {
4163 err = got_object_id_str(&id_str, &qid->id);
4164 if (err)
4165 goto done;
4166 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4167 if (n < 0) {
4168 err = got_error_from_errno("fprintf");
4169 goto done;
4171 outoff += n;
4172 err = add_line_offset(line_offsets, nlines, outoff);
4173 if (err)
4174 goto done;
4175 free(id_str);
4176 id_str = NULL;
4180 err = got_object_commit_get_logmsg(&logmsg, commit);
4181 if (err)
4182 goto done;
4183 s = logmsg;
4184 while ((line = strsep(&s, "\n")) != NULL) {
4185 n = fprintf(outfile, "%s\n", line);
4186 if (n < 0) {
4187 err = got_error_from_errno("fprintf");
4188 goto done;
4190 outoff += n;
4191 err = add_line_offset(line_offsets, nlines, outoff);
4192 if (err)
4193 goto done;
4196 err = get_changed_paths(&changed_paths, commit, repo);
4197 if (err)
4198 goto done;
4199 TAILQ_FOREACH(pe, &changed_paths, entry) {
4200 struct got_diff_changed_path *cp = pe->data;
4201 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4202 if (n < 0) {
4203 err = got_error_from_errno("fprintf");
4204 goto done;
4206 outoff += n;
4207 err = add_line_offset(line_offsets, nlines, outoff);
4208 if (err)
4209 goto done;
4210 free((char *)pe->path);
4211 free(pe->data);
4214 fputc('\n', outfile);
4215 outoff++;
4216 err = add_line_offset(line_offsets, nlines, outoff);
4217 done:
4218 got_pathlist_free(&changed_paths);
4219 free(id_str);
4220 free(logmsg);
4221 free(refs_str);
4222 got_object_commit_close(commit);
4223 if (err) {
4224 free(*line_offsets);
4225 *line_offsets = NULL;
4226 *nlines = 0;
4228 return err;
4231 static const struct got_error *
4232 create_diff(struct tog_diff_view_state *s)
4234 const struct got_error *err = NULL;
4235 FILE *f = NULL;
4236 int obj_type;
4238 free(s->line_offsets);
4239 s->line_offsets = malloc(sizeof(off_t));
4240 if (s->line_offsets == NULL)
4241 return got_error_from_errno("malloc");
4242 s->nlines = 0;
4244 f = got_opentemp();
4245 if (f == NULL) {
4246 err = got_error_from_errno("got_opentemp");
4247 goto done;
4249 if (s->f && fclose(s->f) == EOF) {
4250 err = got_error_from_errno("fclose");
4251 goto done;
4253 s->f = f;
4255 if (s->id1)
4256 err = got_object_get_type(&obj_type, s->repo, s->id1);
4257 else
4258 err = got_object_get_type(&obj_type, s->repo, s->id2);
4259 if (err)
4260 goto done;
4262 switch (obj_type) {
4263 case GOT_OBJ_TYPE_BLOB:
4264 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4265 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4266 s->label1, s->label2, tog_diff_algo, s->diff_context,
4267 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4268 break;
4269 case GOT_OBJ_TYPE_TREE:
4270 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4271 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4272 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4273 s->force_text_diff, s->repo, s->f);
4274 break;
4275 case GOT_OBJ_TYPE_COMMIT: {
4276 const struct got_object_id_queue *parent_ids;
4277 struct got_object_qid *pid;
4278 struct got_commit_object *commit2;
4279 struct got_reflist_head *refs;
4281 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4282 if (err)
4283 goto done;
4284 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4285 /* Show commit info if we're diffing to a parent/root commit. */
4286 if (s->id1 == NULL) {
4287 err = write_commit_info(&s->line_offsets, &s->nlines,
4288 s->id2, refs, s->repo, s->f);
4289 if (err)
4290 goto done;
4291 } else {
4292 parent_ids = got_object_commit_get_parent_ids(commit2);
4293 STAILQ_FOREACH(pid, parent_ids, entry) {
4294 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4295 err = write_commit_info(
4296 &s->line_offsets, &s->nlines,
4297 s->id2, refs, s->repo, s->f);
4298 if (err)
4299 goto done;
4300 break;
4304 got_object_commit_close(commit2);
4306 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4307 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4308 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4309 s->force_text_diff, s->repo, s->f);
4310 break;
4312 default:
4313 err = got_error(GOT_ERR_OBJ_TYPE);
4314 break;
4316 if (err)
4317 goto done;
4318 done:
4319 if (s->f && fflush(s->f) != 0 && err == NULL)
4320 err = got_error_from_errno("fflush");
4321 return err;
4324 static void
4325 diff_view_indicate_progress(struct tog_view *view)
4327 mvwaddstr(view->window, 0, 0, "diffing...");
4328 update_panels();
4329 doupdate();
4332 static const struct got_error *
4333 search_start_diff_view(struct tog_view *view)
4335 struct tog_diff_view_state *s = &view->state.diff;
4337 s->matched_line = 0;
4338 return NULL;
4341 static const struct got_error *
4342 search_next_diff_view(struct tog_view *view)
4344 struct tog_diff_view_state *s = &view->state.diff;
4345 const struct got_error *err = NULL;
4346 int lineno;
4347 char *line = NULL;
4348 size_t linesize = 0;
4349 ssize_t linelen;
4351 if (!view->searching) {
4352 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4353 return NULL;
4356 if (s->matched_line) {
4357 if (view->searching == TOG_SEARCH_FORWARD)
4358 lineno = s->matched_line + 1;
4359 else
4360 lineno = s->matched_line - 1;
4361 } else
4362 lineno = s->first_displayed_line;
4364 while (1) {
4365 off_t offset;
4367 if (lineno <= 0 || lineno > s->nlines) {
4368 if (s->matched_line == 0) {
4369 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4370 break;
4373 if (view->searching == TOG_SEARCH_FORWARD)
4374 lineno = 1;
4375 else
4376 lineno = s->nlines;
4379 offset = s->line_offsets[lineno - 1];
4380 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4381 free(line);
4382 return got_error_from_errno("fseeko");
4384 linelen = getline(&line, &linesize, s->f);
4385 if (linelen != -1) {
4386 char *exstr;
4387 err = expand_tab(&exstr, line);
4388 if (err)
4389 break;
4390 if (match_line(exstr, &view->regex, 1,
4391 &view->regmatch)) {
4392 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4393 s->matched_line = lineno;
4394 free(exstr);
4395 break;
4397 free(exstr);
4399 if (view->searching == TOG_SEARCH_FORWARD)
4400 lineno++;
4401 else
4402 lineno--;
4404 free(line);
4406 if (s->matched_line) {
4407 s->first_displayed_line = s->matched_line;
4408 s->selected_line = 1;
4411 return err;
4414 static const struct got_error *
4415 close_diff_view(struct tog_view *view)
4417 const struct got_error *err = NULL;
4418 struct tog_diff_view_state *s = &view->state.diff;
4420 free(s->id1);
4421 s->id1 = NULL;
4422 free(s->id2);
4423 s->id2 = NULL;
4424 if (s->f && fclose(s->f) == EOF)
4425 err = got_error_from_errno("fclose");
4426 s->f = NULL;
4427 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4428 err = got_error_from_errno("fclose");
4429 s->f1 = NULL;
4430 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4431 err = got_error_from_errno("fclose");
4432 s->f2 = NULL;
4433 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4434 err = got_error_from_errno("close");
4435 s->fd1 = -1;
4436 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4437 err = got_error_from_errno("close");
4438 s->fd2 = -1;
4439 free_colors(&s->colors);
4440 free(s->line_offsets);
4441 s->line_offsets = NULL;
4442 s->nlines = 0;
4443 return err;
4446 static const struct got_error *
4447 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4448 struct got_object_id *id2, const char *label1, const char *label2,
4449 int diff_context, int ignore_whitespace, int force_text_diff,
4450 struct tog_view *parent_view, struct got_repository *repo)
4452 const struct got_error *err;
4453 struct tog_diff_view_state *s = &view->state.diff;
4455 memset(s, 0, sizeof(*s));
4456 s->fd1 = -1;
4457 s->fd2 = -1;
4459 if (id1 != NULL && id2 != NULL) {
4460 int type1, type2;
4461 err = got_object_get_type(&type1, repo, id1);
4462 if (err)
4463 return err;
4464 err = got_object_get_type(&type2, repo, id2);
4465 if (err)
4466 return err;
4468 if (type1 != type2)
4469 return got_error(GOT_ERR_OBJ_TYPE);
4471 s->first_displayed_line = 1;
4472 s->last_displayed_line = view->nlines;
4473 s->selected_line = 1;
4474 s->repo = repo;
4475 s->id1 = id1;
4476 s->id2 = id2;
4477 s->label1 = label1;
4478 s->label2 = label2;
4480 if (id1) {
4481 s->id1 = got_object_id_dup(id1);
4482 if (s->id1 == NULL)
4483 return got_error_from_errno("got_object_id_dup");
4484 } else
4485 s->id1 = NULL;
4487 s->id2 = got_object_id_dup(id2);
4488 if (s->id2 == NULL) {
4489 err = got_error_from_errno("got_object_id_dup");
4490 goto done;
4493 s->f1 = got_opentemp();
4494 if (s->f1 == NULL) {
4495 err = got_error_from_errno("got_opentemp");
4496 goto done;
4499 s->f2 = got_opentemp();
4500 if (s->f2 == NULL) {
4501 err = got_error_from_errno("got_opentemp");
4502 goto done;
4505 s->fd1 = got_opentempfd();
4506 if (s->fd1 == -1) {
4507 err = got_error_from_errno("got_opentempfd");
4508 goto done;
4511 s->fd2 = got_opentempfd();
4512 if (s->fd2 == -1) {
4513 err = got_error_from_errno("got_opentempfd");
4514 goto done;
4517 s->first_displayed_line = 1;
4518 s->last_displayed_line = view->nlines;
4519 s->diff_context = diff_context;
4520 s->ignore_whitespace = ignore_whitespace;
4521 s->force_text_diff = force_text_diff;
4522 s->parent_view = parent_view;
4523 s->repo = repo;
4525 STAILQ_INIT(&s->colors);
4526 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4527 err = add_color(&s->colors,
4528 "^-", TOG_COLOR_DIFF_MINUS,
4529 get_color_value("TOG_COLOR_DIFF_MINUS"));
4530 if (err)
4531 goto done;
4532 err = add_color(&s->colors, "^\\+",
4533 TOG_COLOR_DIFF_PLUS,
4534 get_color_value("TOG_COLOR_DIFF_PLUS"));
4535 if (err)
4536 goto done;
4537 err = add_color(&s->colors,
4538 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4539 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4540 if (err)
4541 goto done;
4543 err = add_color(&s->colors,
4544 "^(commit [0-9a-f]|parent [0-9]|"
4545 "(blob|file|tree|commit) [-+] |"
4546 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4547 get_color_value("TOG_COLOR_DIFF_META"));
4548 if (err)
4549 goto done;
4551 err = add_color(&s->colors,
4552 "^(from|via): ", TOG_COLOR_AUTHOR,
4553 get_color_value("TOG_COLOR_AUTHOR"));
4554 if (err)
4555 goto done;
4557 err = add_color(&s->colors,
4558 "^date: ", TOG_COLOR_DATE,
4559 get_color_value("TOG_COLOR_DATE"));
4560 if (err)
4561 goto done;
4564 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4565 view_is_splitscreen(view))
4566 show_log_view(parent_view); /* draw border */
4567 diff_view_indicate_progress(view);
4569 err = create_diff(s);
4571 view->show = show_diff_view;
4572 view->input = input_diff_view;
4573 view->reset = reset_diff_view;
4574 view->close = close_diff_view;
4575 view->search_start = search_start_diff_view;
4576 view->search_next = search_next_diff_view;
4577 done:
4578 if (err)
4579 close_diff_view(view);
4580 return err;
4583 static const struct got_error *
4584 show_diff_view(struct tog_view *view)
4586 const struct got_error *err;
4587 struct tog_diff_view_state *s = &view->state.diff;
4588 char *id_str1 = NULL, *id_str2, *header;
4589 const char *label1, *label2;
4591 if (s->id1) {
4592 err = got_object_id_str(&id_str1, s->id1);
4593 if (err)
4594 return err;
4595 label1 = s->label1 ? : id_str1;
4596 } else
4597 label1 = "/dev/null";
4599 err = got_object_id_str(&id_str2, s->id2);
4600 if (err)
4601 return err;
4602 label2 = s->label2 ? : id_str2;
4604 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4605 err = got_error_from_errno("asprintf");
4606 free(id_str1);
4607 free(id_str2);
4608 return err;
4610 free(id_str1);
4611 free(id_str2);
4613 err = draw_file(view, header);
4614 free(header);
4615 return err;
4618 static const struct got_error *
4619 set_selected_commit(struct tog_diff_view_state *s,
4620 struct commit_queue_entry *entry)
4622 const struct got_error *err;
4623 const struct got_object_id_queue *parent_ids;
4624 struct got_commit_object *selected_commit;
4625 struct got_object_qid *pid;
4627 free(s->id2);
4628 s->id2 = got_object_id_dup(entry->id);
4629 if (s->id2 == NULL)
4630 return got_error_from_errno("got_object_id_dup");
4632 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4633 if (err)
4634 return err;
4635 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4636 free(s->id1);
4637 pid = STAILQ_FIRST(parent_ids);
4638 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4639 got_object_commit_close(selected_commit);
4640 return NULL;
4643 static const struct got_error *
4644 reset_diff_view(struct tog_view *view)
4646 struct tog_diff_view_state *s = &view->state.diff;
4648 view->count = 0;
4649 wclear(view->window);
4650 s->first_displayed_line = 1;
4651 s->last_displayed_line = view->nlines;
4652 s->matched_line = 0;
4653 diff_view_indicate_progress(view);
4654 return create_diff(s);
4657 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4658 int, int, int);
4659 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4660 int, int);
4662 static const struct got_error *
4663 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4665 const struct got_error *err = NULL;
4666 struct tog_diff_view_state *s = &view->state.diff;
4667 struct tog_log_view_state *ls;
4668 struct commit_queue_entry *old_selected_entry;
4669 char *line = NULL;
4670 size_t linesize = 0;
4671 ssize_t linelen;
4672 int i, nscroll = view->nlines - 1, up = 0;
4674 switch (ch) {
4675 case '0':
4676 view->x = 0;
4677 break;
4678 case '$':
4679 view->x = MAX(view->maxx - view->ncols / 3, 0);
4680 view->count = 0;
4681 break;
4682 case KEY_RIGHT:
4683 case 'l':
4684 if (view->x + view->ncols / 3 < view->maxx)
4685 view->x += 2; /* move two columns right */
4686 else
4687 view->count = 0;
4688 break;
4689 case KEY_LEFT:
4690 case 'h':
4691 view->x -= MIN(view->x, 2); /* move two columns back */
4692 if (view->x <= 0)
4693 view->count = 0;
4694 break;
4695 case 'a':
4696 case 'w':
4697 if (ch == 'a')
4698 s->force_text_diff = !s->force_text_diff;
4699 if (ch == 'w')
4700 s->ignore_whitespace = !s->ignore_whitespace;
4701 err = reset_diff_view(view);
4702 break;
4703 case 'g':
4704 case KEY_HOME:
4705 s->first_displayed_line = 1;
4706 view->count = 0;
4707 break;
4708 case 'G':
4709 case KEY_END:
4710 view->count = 0;
4711 if (s->eof)
4712 break;
4714 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4715 s->eof = 1;
4716 break;
4717 case 'k':
4718 case KEY_UP:
4719 case CTRL('p'):
4720 if (s->first_displayed_line > 1)
4721 s->first_displayed_line--;
4722 else
4723 view->count = 0;
4724 break;
4725 case CTRL('u'):
4726 case 'u':
4727 nscroll /= 2;
4728 /* FALL THROUGH */
4729 case KEY_PPAGE:
4730 case CTRL('b'):
4731 case 'b':
4732 if (s->first_displayed_line == 1) {
4733 view->count = 0;
4734 break;
4736 i = 0;
4737 while (i++ < nscroll && s->first_displayed_line > 1)
4738 s->first_displayed_line--;
4739 break;
4740 case 'j':
4741 case KEY_DOWN:
4742 case CTRL('n'):
4743 if (!s->eof)
4744 s->first_displayed_line++;
4745 else
4746 view->count = 0;
4747 break;
4748 case CTRL('d'):
4749 case 'd':
4750 nscroll /= 2;
4751 /* FALL THROUGH */
4752 case KEY_NPAGE:
4753 case CTRL('f'):
4754 case 'f':
4755 case ' ':
4756 if (s->eof) {
4757 view->count = 0;
4758 break;
4760 i = 0;
4761 while (!s->eof && i++ < nscroll) {
4762 linelen = getline(&line, &linesize, s->f);
4763 s->first_displayed_line++;
4764 if (linelen == -1) {
4765 if (feof(s->f)) {
4766 s->eof = 1;
4767 } else
4768 err = got_ferror(s->f, GOT_ERR_IO);
4769 break;
4772 free(line);
4773 break;
4774 case '[':
4775 if (s->diff_context > 0) {
4776 s->diff_context--;
4777 s->matched_line = 0;
4778 diff_view_indicate_progress(view);
4779 err = create_diff(s);
4780 if (s->first_displayed_line + view->nlines - 1 >
4781 s->nlines) {
4782 s->first_displayed_line = 1;
4783 s->last_displayed_line = view->nlines;
4785 } else
4786 view->count = 0;
4787 break;
4788 case ']':
4789 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4790 s->diff_context++;
4791 s->matched_line = 0;
4792 diff_view_indicate_progress(view);
4793 err = create_diff(s);
4794 } else
4795 view->count = 0;
4796 break;
4797 case '<':
4798 case ',':
4799 up = 1;
4800 /* FALL THROUGH */
4801 case '>':
4802 case '.':
4803 if (s->parent_view == NULL) {
4804 view->count = 0;
4805 break;
4807 s->parent_view->count = view->count;
4809 if (s->parent_view->type == TOG_VIEW_LOG) {
4810 ls = &s->parent_view->state.log;
4811 old_selected_entry = ls->selected_entry;
4813 err = input_log_view(NULL, s->parent_view,
4814 up ? KEY_UP : KEY_DOWN);
4815 if (err)
4816 break;
4817 view->count = s->parent_view->count;
4819 if (old_selected_entry == ls->selected_entry)
4820 break;
4822 err = set_selected_commit(s, ls->selected_entry);
4823 if (err)
4824 break;
4825 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4826 struct tog_blame_view_state *bs;
4827 struct got_object_id *id, *prev_id;
4829 bs = &s->parent_view->state.blame;
4830 prev_id = get_annotation_for_line(bs->blame.lines,
4831 bs->blame.nlines, bs->last_diffed_line);
4833 err = input_blame_view(&view, s->parent_view,
4834 up ? KEY_UP : KEY_DOWN);
4835 if (err)
4836 break;
4837 view->count = s->parent_view->count;
4839 if (prev_id == NULL)
4840 break;
4841 id = get_selected_commit_id(bs->blame.lines,
4842 bs->blame.nlines, bs->first_displayed_line,
4843 bs->selected_line);
4844 if (id == NULL)
4845 break;
4847 if (!got_object_id_cmp(prev_id, id))
4848 break;
4850 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4851 if (err)
4852 break;
4854 s->first_displayed_line = 1;
4855 s->last_displayed_line = view->nlines;
4856 s->matched_line = 0;
4857 view->x = 0;
4859 diff_view_indicate_progress(view);
4860 err = create_diff(s);
4861 break;
4862 default:
4863 view->count = 0;
4864 break;
4867 return err;
4870 static const struct got_error *
4871 cmd_diff(int argc, char *argv[])
4873 const struct got_error *error = NULL;
4874 struct got_repository *repo = NULL;
4875 struct got_worktree *worktree = NULL;
4876 struct got_object_id *id1 = NULL, *id2 = NULL;
4877 char *repo_path = NULL, *cwd = NULL;
4878 char *id_str1 = NULL, *id_str2 = NULL;
4879 char *label1 = NULL, *label2 = NULL;
4880 int diff_context = 3, ignore_whitespace = 0;
4881 int ch, force_text_diff = 0;
4882 const char *errstr;
4883 struct tog_view *view;
4884 int *pack_fds = NULL;
4886 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4887 switch (ch) {
4888 case 'a':
4889 force_text_diff = 1;
4890 break;
4891 case 'C':
4892 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4893 &errstr);
4894 if (errstr != NULL)
4895 errx(1, "number of context lines is %s: %s",
4896 errstr, errstr);
4897 break;
4898 case 'r':
4899 repo_path = realpath(optarg, NULL);
4900 if (repo_path == NULL)
4901 return got_error_from_errno2("realpath",
4902 optarg);
4903 got_path_strip_trailing_slashes(repo_path);
4904 break;
4905 case 'w':
4906 ignore_whitespace = 1;
4907 break;
4908 default:
4909 usage_diff();
4910 /* NOTREACHED */
4914 argc -= optind;
4915 argv += optind;
4917 if (argc == 0) {
4918 usage_diff(); /* TODO show local worktree changes */
4919 } else if (argc == 2) {
4920 id_str1 = argv[0];
4921 id_str2 = argv[1];
4922 } else
4923 usage_diff();
4925 error = got_repo_pack_fds_open(&pack_fds);
4926 if (error)
4927 goto done;
4929 if (repo_path == NULL) {
4930 cwd = getcwd(NULL, 0);
4931 if (cwd == NULL)
4932 return got_error_from_errno("getcwd");
4933 error = got_worktree_open(&worktree, cwd);
4934 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4935 goto done;
4936 if (worktree)
4937 repo_path =
4938 strdup(got_worktree_get_repo_path(worktree));
4939 else
4940 repo_path = strdup(cwd);
4941 if (repo_path == NULL) {
4942 error = got_error_from_errno("strdup");
4943 goto done;
4947 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4948 if (error)
4949 goto done;
4951 init_curses();
4953 error = apply_unveil(got_repo_get_path(repo), NULL);
4954 if (error)
4955 goto done;
4957 error = tog_load_refs(repo, 0);
4958 if (error)
4959 goto done;
4961 error = got_repo_match_object_id(&id1, &label1, id_str1,
4962 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4963 if (error)
4964 goto done;
4966 error = got_repo_match_object_id(&id2, &label2, id_str2,
4967 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4968 if (error)
4969 goto done;
4971 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4972 if (view == NULL) {
4973 error = got_error_from_errno("view_open");
4974 goto done;
4976 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4977 ignore_whitespace, force_text_diff, NULL, repo);
4978 if (error)
4979 goto done;
4980 error = view_loop(view);
4981 done:
4982 free(label1);
4983 free(label2);
4984 free(repo_path);
4985 free(cwd);
4986 if (repo) {
4987 const struct got_error *close_err = got_repo_close(repo);
4988 if (error == NULL)
4989 error = close_err;
4991 if (worktree)
4992 got_worktree_close(worktree);
4993 if (pack_fds) {
4994 const struct got_error *pack_err =
4995 got_repo_pack_fds_close(pack_fds);
4996 if (error == NULL)
4997 error = pack_err;
4999 tog_free_refs();
5000 return error;
5003 __dead static void
5004 usage_blame(void)
5006 endwin();
5007 fprintf(stderr,
5008 "usage: %s blame [-c commit] [-r repository-path] path\n",
5009 getprogname());
5010 exit(1);
5013 struct tog_blame_line {
5014 int annotated;
5015 struct got_object_id *id;
5018 static const struct got_error *
5019 draw_blame(struct tog_view *view)
5021 struct tog_blame_view_state *s = &view->state.blame;
5022 struct tog_blame *blame = &s->blame;
5023 regmatch_t *regmatch = &view->regmatch;
5024 const struct got_error *err;
5025 int lineno = 0, nprinted = 0;
5026 char *line = NULL;
5027 size_t linesize = 0;
5028 ssize_t linelen;
5029 wchar_t *wline;
5030 int width;
5031 struct tog_blame_line *blame_line;
5032 struct got_object_id *prev_id = NULL;
5033 char *id_str;
5034 struct tog_color *tc;
5036 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5037 if (err)
5038 return err;
5040 rewind(blame->f);
5041 werase(view->window);
5043 if (asprintf(&line, "commit %s", id_str) == -1) {
5044 err = got_error_from_errno("asprintf");
5045 free(id_str);
5046 return err;
5049 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5050 free(line);
5051 line = NULL;
5052 if (err)
5053 return err;
5054 if (view_needs_focus_indication(view))
5055 wstandout(view->window);
5056 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5057 if (tc)
5058 wattr_on(view->window,
5059 COLOR_PAIR(tc->colorpair), NULL);
5060 waddwstr(view->window, wline);
5061 if (tc)
5062 wattr_off(view->window,
5063 COLOR_PAIR(tc->colorpair), NULL);
5064 if (view_needs_focus_indication(view))
5065 wstandend(view->window);
5066 free(wline);
5067 wline = NULL;
5068 if (width < view->ncols - 1)
5069 waddch(view->window, '\n');
5071 if (asprintf(&line, "[%d/%d] %s%s",
5072 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5073 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5074 free(id_str);
5075 return got_error_from_errno("asprintf");
5077 free(id_str);
5078 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5079 free(line);
5080 line = NULL;
5081 if (err)
5082 return err;
5083 waddwstr(view->window, wline);
5084 free(wline);
5085 wline = NULL;
5086 if (width < view->ncols - 1)
5087 waddch(view->window, '\n');
5089 s->eof = 0;
5090 view->maxx = 0;
5091 while (nprinted < view->nlines - 2) {
5092 linelen = getline(&line, &linesize, blame->f);
5093 if (linelen == -1) {
5094 if (feof(blame->f)) {
5095 s->eof = 1;
5096 break;
5098 free(line);
5099 return got_ferror(blame->f, GOT_ERR_IO);
5101 if (++lineno < s->first_displayed_line)
5102 continue;
5104 /* Set view->maxx based on full line length. */
5105 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5106 if (err) {
5107 free(line);
5108 return err;
5110 free(wline);
5111 wline = NULL;
5112 view->maxx = MAX(view->maxx, width);
5114 if (nprinted == s->selected_line - 1)
5115 wstandout(view->window);
5117 if (blame->nlines > 0) {
5118 blame_line = &blame->lines[lineno - 1];
5119 if (blame_line->annotated && prev_id &&
5120 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5121 !(nprinted == s->selected_line - 1)) {
5122 waddstr(view->window, " ");
5123 } else if (blame_line->annotated) {
5124 char *id_str;
5125 err = got_object_id_str(&id_str,
5126 blame_line->id);
5127 if (err) {
5128 free(line);
5129 return err;
5131 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5132 if (tc)
5133 wattr_on(view->window,
5134 COLOR_PAIR(tc->colorpair), NULL);
5135 wprintw(view->window, "%.8s", id_str);
5136 if (tc)
5137 wattr_off(view->window,
5138 COLOR_PAIR(tc->colorpair), NULL);
5139 free(id_str);
5140 prev_id = blame_line->id;
5141 } else {
5142 waddstr(view->window, "........");
5143 prev_id = NULL;
5145 } else {
5146 waddstr(view->window, "........");
5147 prev_id = NULL;
5150 if (nprinted == s->selected_line - 1)
5151 wstandend(view->window);
5152 waddstr(view->window, " ");
5154 if (view->ncols <= 9) {
5155 width = 9;
5156 } else if (s->first_displayed_line + nprinted ==
5157 s->matched_line &&
5158 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5159 err = add_matched_line(&width, line, view->ncols - 9, 9,
5160 view->window, view->x, regmatch);
5161 if (err) {
5162 free(line);
5163 return err;
5165 width += 9;
5166 } else {
5167 int skip;
5168 err = format_line(&wline, &width, &skip, line,
5169 view->x, view->ncols - 9, 9, 1);
5170 if (err) {
5171 free(line);
5172 return err;
5174 waddwstr(view->window, &wline[skip]);
5175 width += 9;
5176 free(wline);
5177 wline = NULL;
5180 if (width <= view->ncols - 1)
5181 waddch(view->window, '\n');
5182 if (++nprinted == 1)
5183 s->first_displayed_line = lineno;
5185 free(line);
5186 s->last_displayed_line = lineno;
5188 view_border(view);
5190 return NULL;
5193 static const struct got_error *
5194 blame_cb(void *arg, int nlines, int lineno,
5195 struct got_commit_object *commit, struct got_object_id *id)
5197 const struct got_error *err = NULL;
5198 struct tog_blame_cb_args *a = arg;
5199 struct tog_blame_line *line;
5200 int errcode;
5202 if (nlines != a->nlines ||
5203 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5204 return got_error(GOT_ERR_RANGE);
5206 errcode = pthread_mutex_lock(&tog_mutex);
5207 if (errcode)
5208 return got_error_set_errno(errcode, "pthread_mutex_lock");
5210 if (*a->quit) { /* user has quit the blame view */
5211 err = got_error(GOT_ERR_ITER_COMPLETED);
5212 goto done;
5215 if (lineno == -1)
5216 goto done; /* no change in this commit */
5218 line = &a->lines[lineno - 1];
5219 if (line->annotated)
5220 goto done;
5222 line->id = got_object_id_dup(id);
5223 if (line->id == NULL) {
5224 err = got_error_from_errno("got_object_id_dup");
5225 goto done;
5227 line->annotated = 1;
5228 done:
5229 errcode = pthread_mutex_unlock(&tog_mutex);
5230 if (errcode)
5231 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5232 return err;
5235 static void *
5236 blame_thread(void *arg)
5238 const struct got_error *err, *close_err;
5239 struct tog_blame_thread_args *ta = arg;
5240 struct tog_blame_cb_args *a = ta->cb_args;
5241 int errcode, fd1 = -1, fd2 = -1;
5242 FILE *f1 = NULL, *f2 = NULL;
5244 fd1 = got_opentempfd();
5245 if (fd1 == -1)
5246 return (void *)got_error_from_errno("got_opentempfd");
5248 fd2 = got_opentempfd();
5249 if (fd2 == -1) {
5250 err = got_error_from_errno("got_opentempfd");
5251 goto done;
5254 f1 = got_opentemp();
5255 if (f1 == NULL) {
5256 err = (void *)got_error_from_errno("got_opentemp");
5257 goto done;
5259 f2 = got_opentemp();
5260 if (f2 == NULL) {
5261 err = (void *)got_error_from_errno("got_opentemp");
5262 goto done;
5265 err = block_signals_used_by_main_thread();
5266 if (err)
5267 goto done;
5269 err = got_blame(ta->path, a->commit_id, ta->repo,
5270 tog_diff_algo, blame_cb, ta->cb_args,
5271 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5272 if (err && err->code == GOT_ERR_CANCELLED)
5273 err = NULL;
5275 errcode = pthread_mutex_lock(&tog_mutex);
5276 if (errcode) {
5277 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5278 goto done;
5281 close_err = got_repo_close(ta->repo);
5282 if (err == NULL)
5283 err = close_err;
5284 ta->repo = NULL;
5285 *ta->complete = 1;
5287 errcode = pthread_mutex_unlock(&tog_mutex);
5288 if (errcode && err == NULL)
5289 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5291 done:
5292 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5293 err = got_error_from_errno("close");
5294 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5295 err = got_error_from_errno("close");
5296 if (f1 && fclose(f1) == EOF && err == NULL)
5297 err = got_error_from_errno("fclose");
5298 if (f2 && fclose(f2) == EOF && err == NULL)
5299 err = got_error_from_errno("fclose");
5301 return (void *)err;
5304 static struct got_object_id *
5305 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5306 int first_displayed_line, int selected_line)
5308 struct tog_blame_line *line;
5310 if (nlines <= 0)
5311 return NULL;
5313 line = &lines[first_displayed_line - 1 + selected_line - 1];
5314 if (!line->annotated)
5315 return NULL;
5317 return line->id;
5320 static struct got_object_id *
5321 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5322 int lineno)
5324 struct tog_blame_line *line;
5326 if (nlines <= 0 || lineno >= nlines)
5327 return NULL;
5329 line = &lines[lineno - 1];
5330 if (!line->annotated)
5331 return NULL;
5333 return line->id;
5336 static const struct got_error *
5337 stop_blame(struct tog_blame *blame)
5339 const struct got_error *err = NULL;
5340 int i;
5342 if (blame->thread) {
5343 int errcode;
5344 errcode = pthread_mutex_unlock(&tog_mutex);
5345 if (errcode)
5346 return got_error_set_errno(errcode,
5347 "pthread_mutex_unlock");
5348 errcode = pthread_join(blame->thread, (void **)&err);
5349 if (errcode)
5350 return got_error_set_errno(errcode, "pthread_join");
5351 errcode = pthread_mutex_lock(&tog_mutex);
5352 if (errcode)
5353 return got_error_set_errno(errcode,
5354 "pthread_mutex_lock");
5355 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5356 err = NULL;
5357 blame->thread = NULL;
5359 if (blame->thread_args.repo) {
5360 const struct got_error *close_err;
5361 close_err = got_repo_close(blame->thread_args.repo);
5362 if (err == NULL)
5363 err = close_err;
5364 blame->thread_args.repo = NULL;
5366 if (blame->f) {
5367 if (fclose(blame->f) == EOF && err == NULL)
5368 err = got_error_from_errno("fclose");
5369 blame->f = NULL;
5371 if (blame->lines) {
5372 for (i = 0; i < blame->nlines; i++)
5373 free(blame->lines[i].id);
5374 free(blame->lines);
5375 blame->lines = NULL;
5377 free(blame->cb_args.commit_id);
5378 blame->cb_args.commit_id = NULL;
5379 if (blame->pack_fds) {
5380 const struct got_error *pack_err =
5381 got_repo_pack_fds_close(blame->pack_fds);
5382 if (err == NULL)
5383 err = pack_err;
5384 blame->pack_fds = NULL;
5386 return err;
5389 static const struct got_error *
5390 cancel_blame_view(void *arg)
5392 const struct got_error *err = NULL;
5393 int *done = arg;
5394 int errcode;
5396 errcode = pthread_mutex_lock(&tog_mutex);
5397 if (errcode)
5398 return got_error_set_errno(errcode,
5399 "pthread_mutex_unlock");
5401 if (*done)
5402 err = got_error(GOT_ERR_CANCELLED);
5404 errcode = pthread_mutex_unlock(&tog_mutex);
5405 if (errcode)
5406 return got_error_set_errno(errcode,
5407 "pthread_mutex_lock");
5409 return err;
5412 static const struct got_error *
5413 run_blame(struct tog_view *view)
5415 struct tog_blame_view_state *s = &view->state.blame;
5416 struct tog_blame *blame = &s->blame;
5417 const struct got_error *err = NULL;
5418 struct got_commit_object *commit = NULL;
5419 struct got_blob_object *blob = NULL;
5420 struct got_repository *thread_repo = NULL;
5421 struct got_object_id *obj_id = NULL;
5422 int obj_type, fd = -1;
5423 int *pack_fds = NULL;
5425 err = got_object_open_as_commit(&commit, s->repo,
5426 &s->blamed_commit->id);
5427 if (err)
5428 return err;
5430 fd = got_opentempfd();
5431 if (fd == -1) {
5432 err = got_error_from_errno("got_opentempfd");
5433 goto done;
5436 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5437 if (err)
5438 goto done;
5440 err = got_object_get_type(&obj_type, s->repo, obj_id);
5441 if (err)
5442 goto done;
5444 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5445 err = got_error(GOT_ERR_OBJ_TYPE);
5446 goto done;
5449 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5450 if (err)
5451 goto done;
5452 blame->f = got_opentemp();
5453 if (blame->f == NULL) {
5454 err = got_error_from_errno("got_opentemp");
5455 goto done;
5457 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5458 &blame->line_offsets, blame->f, blob);
5459 if (err)
5460 goto done;
5461 if (blame->nlines == 0) {
5462 s->blame_complete = 1;
5463 goto done;
5466 /* Don't include \n at EOF in the blame line count. */
5467 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5468 blame->nlines--;
5470 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5471 if (blame->lines == NULL) {
5472 err = got_error_from_errno("calloc");
5473 goto done;
5476 err = got_repo_pack_fds_open(&pack_fds);
5477 if (err)
5478 goto done;
5479 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5480 pack_fds);
5481 if (err)
5482 goto done;
5484 blame->pack_fds = pack_fds;
5485 blame->cb_args.view = view;
5486 blame->cb_args.lines = blame->lines;
5487 blame->cb_args.nlines = blame->nlines;
5488 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5489 if (blame->cb_args.commit_id == NULL) {
5490 err = got_error_from_errno("got_object_id_dup");
5491 goto done;
5493 blame->cb_args.quit = &s->done;
5495 blame->thread_args.path = s->path;
5496 blame->thread_args.repo = thread_repo;
5497 blame->thread_args.cb_args = &blame->cb_args;
5498 blame->thread_args.complete = &s->blame_complete;
5499 blame->thread_args.cancel_cb = cancel_blame_view;
5500 blame->thread_args.cancel_arg = &s->done;
5501 s->blame_complete = 0;
5503 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5504 s->first_displayed_line = 1;
5505 s->last_displayed_line = view->nlines;
5506 s->selected_line = 1;
5508 s->matched_line = 0;
5510 done:
5511 if (commit)
5512 got_object_commit_close(commit);
5513 if (fd != -1 && close(fd) == -1 && err == NULL)
5514 err = got_error_from_errno("close");
5515 if (blob)
5516 got_object_blob_close(blob);
5517 free(obj_id);
5518 if (err)
5519 stop_blame(blame);
5520 return err;
5523 static const struct got_error *
5524 open_blame_view(struct tog_view *view, char *path,
5525 struct got_object_id *commit_id, struct got_repository *repo)
5527 const struct got_error *err = NULL;
5528 struct tog_blame_view_state *s = &view->state.blame;
5530 STAILQ_INIT(&s->blamed_commits);
5532 s->path = strdup(path);
5533 if (s->path == NULL)
5534 return got_error_from_errno("strdup");
5536 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5537 if (err) {
5538 free(s->path);
5539 return err;
5542 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5543 s->first_displayed_line = 1;
5544 s->last_displayed_line = view->nlines;
5545 s->selected_line = 1;
5546 s->blame_complete = 0;
5547 s->repo = repo;
5548 s->commit_id = commit_id;
5549 memset(&s->blame, 0, sizeof(s->blame));
5551 STAILQ_INIT(&s->colors);
5552 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5553 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5554 get_color_value("TOG_COLOR_COMMIT"));
5555 if (err)
5556 return err;
5559 view->show = show_blame_view;
5560 view->input = input_blame_view;
5561 view->reset = reset_blame_view;
5562 view->close = close_blame_view;
5563 view->search_start = search_start_blame_view;
5564 view->search_next = search_next_blame_view;
5566 return run_blame(view);
5569 static const struct got_error *
5570 close_blame_view(struct tog_view *view)
5572 const struct got_error *err = NULL;
5573 struct tog_blame_view_state *s = &view->state.blame;
5575 if (s->blame.thread)
5576 err = stop_blame(&s->blame);
5578 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5579 struct got_object_qid *blamed_commit;
5580 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5581 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5582 got_object_qid_free(blamed_commit);
5585 free(s->path);
5586 free_colors(&s->colors);
5587 return err;
5590 static const struct got_error *
5591 search_start_blame_view(struct tog_view *view)
5593 struct tog_blame_view_state *s = &view->state.blame;
5595 s->matched_line = 0;
5596 return NULL;
5599 static const struct got_error *
5600 search_next_blame_view(struct tog_view *view)
5602 struct tog_blame_view_state *s = &view->state.blame;
5603 const struct got_error *err = NULL;
5604 int lineno;
5605 char *line = NULL;
5606 size_t linesize = 0;
5607 ssize_t linelen;
5609 if (!view->searching) {
5610 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5611 return NULL;
5614 if (s->matched_line) {
5615 if (view->searching == TOG_SEARCH_FORWARD)
5616 lineno = s->matched_line + 1;
5617 else
5618 lineno = s->matched_line - 1;
5619 } else
5620 lineno = s->first_displayed_line - 1 + s->selected_line;
5622 while (1) {
5623 off_t offset;
5625 if (lineno <= 0 || lineno > s->blame.nlines) {
5626 if (s->matched_line == 0) {
5627 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5628 break;
5631 if (view->searching == TOG_SEARCH_FORWARD)
5632 lineno = 1;
5633 else
5634 lineno = s->blame.nlines;
5637 offset = s->blame.line_offsets[lineno - 1];
5638 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5639 free(line);
5640 return got_error_from_errno("fseeko");
5642 linelen = getline(&line, &linesize, s->blame.f);
5643 if (linelen != -1) {
5644 char *exstr;
5645 err = expand_tab(&exstr, line);
5646 if (err)
5647 break;
5648 if (match_line(exstr, &view->regex, 1,
5649 &view->regmatch)) {
5650 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5651 s->matched_line = lineno;
5652 free(exstr);
5653 break;
5655 free(exstr);
5657 if (view->searching == TOG_SEARCH_FORWARD)
5658 lineno++;
5659 else
5660 lineno--;
5662 free(line);
5664 if (s->matched_line) {
5665 s->first_displayed_line = s->matched_line;
5666 s->selected_line = 1;
5669 return err;
5672 static const struct got_error *
5673 show_blame_view(struct tog_view *view)
5675 const struct got_error *err = NULL;
5676 struct tog_blame_view_state *s = &view->state.blame;
5677 int errcode;
5679 if (s->blame.thread == NULL && !s->blame_complete) {
5680 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5681 &s->blame.thread_args);
5682 if (errcode)
5683 return got_error_set_errno(errcode, "pthread_create");
5685 halfdelay(1); /* fast refresh while annotating */
5688 if (s->blame_complete)
5689 halfdelay(10); /* disable fast refresh */
5691 err = draw_blame(view);
5693 view_border(view);
5694 return err;
5697 static const struct got_error *
5698 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5700 const struct got_error *err = NULL, *thread_err = NULL;
5701 struct tog_view *diff_view;
5702 struct tog_blame_view_state *s = &view->state.blame;
5703 int eos, nscroll, begin_y = 0, begin_x = 0;
5705 eos = nscroll = view->nlines - 2;
5706 if (view_is_hsplit_top(view))
5707 --eos; /* border */
5709 switch (ch) {
5710 case '0':
5711 view->x = 0;
5712 break;
5713 case '$':
5714 view->x = MAX(view->maxx - view->ncols / 3, 0);
5715 view->count = 0;
5716 break;
5717 case KEY_RIGHT:
5718 case 'l':
5719 if (view->x + view->ncols / 3 < view->maxx)
5720 view->x += 2; /* move two columns right */
5721 else
5722 view->count = 0;
5723 break;
5724 case KEY_LEFT:
5725 case 'h':
5726 view->x -= MIN(view->x, 2); /* move two columns back */
5727 if (view->x <= 0)
5728 view->count = 0;
5729 break;
5730 case 'q':
5731 s->done = 1;
5732 break;
5733 case 'g':
5734 case KEY_HOME:
5735 s->selected_line = 1;
5736 s->first_displayed_line = 1;
5737 view->count = 0;
5738 break;
5739 case 'G':
5740 case KEY_END:
5741 if (s->blame.nlines < eos) {
5742 s->selected_line = s->blame.nlines;
5743 s->first_displayed_line = 1;
5744 } else {
5745 s->selected_line = eos;
5746 s->first_displayed_line = s->blame.nlines - (eos - 1);
5748 view->count = 0;
5749 break;
5750 case 'k':
5751 case KEY_UP:
5752 case CTRL('p'):
5753 if (s->selected_line > 1)
5754 s->selected_line--;
5755 else if (s->selected_line == 1 &&
5756 s->first_displayed_line > 1)
5757 s->first_displayed_line--;
5758 else
5759 view->count = 0;
5760 break;
5761 case CTRL('u'):
5762 case 'u':
5763 nscroll /= 2;
5764 /* FALL THROUGH */
5765 case KEY_PPAGE:
5766 case CTRL('b'):
5767 case 'b':
5768 if (s->first_displayed_line == 1) {
5769 if (view->count > 1)
5770 nscroll += nscroll;
5771 s->selected_line = MAX(1, s->selected_line - nscroll);
5772 view->count = 0;
5773 break;
5775 if (s->first_displayed_line > nscroll)
5776 s->first_displayed_line -= nscroll;
5777 else
5778 s->first_displayed_line = 1;
5779 break;
5780 case 'j':
5781 case KEY_DOWN:
5782 case CTRL('n'):
5783 if (s->selected_line < eos && s->first_displayed_line +
5784 s->selected_line <= s->blame.nlines)
5785 s->selected_line++;
5786 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5787 s->first_displayed_line++;
5788 else
5789 view->count = 0;
5790 break;
5791 case 'c':
5792 case 'p': {
5793 struct got_object_id *id = NULL;
5795 view->count = 0;
5796 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5797 s->first_displayed_line, s->selected_line);
5798 if (id == NULL)
5799 break;
5800 if (ch == 'p') {
5801 struct got_commit_object *commit, *pcommit;
5802 struct got_object_qid *pid;
5803 struct got_object_id *blob_id = NULL;
5804 int obj_type;
5805 err = got_object_open_as_commit(&commit,
5806 s->repo, id);
5807 if (err)
5808 break;
5809 pid = STAILQ_FIRST(
5810 got_object_commit_get_parent_ids(commit));
5811 if (pid == NULL) {
5812 got_object_commit_close(commit);
5813 break;
5815 /* Check if path history ends here. */
5816 err = got_object_open_as_commit(&pcommit,
5817 s->repo, &pid->id);
5818 if (err)
5819 break;
5820 err = got_object_id_by_path(&blob_id, s->repo,
5821 pcommit, s->path);
5822 got_object_commit_close(pcommit);
5823 if (err) {
5824 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5825 err = NULL;
5826 got_object_commit_close(commit);
5827 break;
5829 err = got_object_get_type(&obj_type, s->repo,
5830 blob_id);
5831 free(blob_id);
5832 /* Can't blame non-blob type objects. */
5833 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5834 got_object_commit_close(commit);
5835 break;
5837 err = got_object_qid_alloc(&s->blamed_commit,
5838 &pid->id);
5839 got_object_commit_close(commit);
5840 } else {
5841 if (got_object_id_cmp(id,
5842 &s->blamed_commit->id) == 0)
5843 break;
5844 err = got_object_qid_alloc(&s->blamed_commit,
5845 id);
5847 if (err)
5848 break;
5849 s->done = 1;
5850 thread_err = stop_blame(&s->blame);
5851 s->done = 0;
5852 if (thread_err)
5853 break;
5854 STAILQ_INSERT_HEAD(&s->blamed_commits,
5855 s->blamed_commit, entry);
5856 err = run_blame(view);
5857 if (err)
5858 break;
5859 break;
5861 case 'C': {
5862 struct got_object_qid *first;
5864 view->count = 0;
5865 first = STAILQ_FIRST(&s->blamed_commits);
5866 if (!got_object_id_cmp(&first->id, s->commit_id))
5867 break;
5868 s->done = 1;
5869 thread_err = stop_blame(&s->blame);
5870 s->done = 0;
5871 if (thread_err)
5872 break;
5873 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5874 got_object_qid_free(s->blamed_commit);
5875 s->blamed_commit =
5876 STAILQ_FIRST(&s->blamed_commits);
5877 err = run_blame(view);
5878 if (err)
5879 break;
5880 break;
5882 case KEY_ENTER:
5883 case '\r': {
5884 struct got_object_id *id = NULL;
5885 struct got_object_qid *pid;
5886 struct got_commit_object *commit = NULL;
5888 view->count = 0;
5889 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5890 s->first_displayed_line, s->selected_line);
5891 if (id == NULL)
5892 break;
5893 err = got_object_open_as_commit(&commit, s->repo, id);
5894 if (err)
5895 break;
5896 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5897 if (*new_view) {
5898 /* traversed from diff view, release diff resources */
5899 err = close_diff_view(*new_view);
5900 if (err)
5901 break;
5902 diff_view = *new_view;
5903 } else {
5904 if (view_is_parent_view(view))
5905 view_get_split(view, &begin_y, &begin_x);
5907 diff_view = view_open(0, 0, begin_y, begin_x,
5908 TOG_VIEW_DIFF);
5909 if (diff_view == NULL) {
5910 got_object_commit_close(commit);
5911 err = got_error_from_errno("view_open");
5912 break;
5915 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5916 id, NULL, NULL, 3, 0, 0, view, s->repo);
5917 got_object_commit_close(commit);
5918 if (err) {
5919 view_close(diff_view);
5920 break;
5922 s->last_diffed_line = s->first_displayed_line - 1 +
5923 s->selected_line;
5924 if (*new_view)
5925 break; /* still open from active diff view */
5926 if (view_is_parent_view(view) &&
5927 view->mode == TOG_VIEW_SPLIT_HRZN) {
5928 err = view_init_hsplit(view, begin_y);
5929 if (err)
5930 break;
5933 view->focussed = 0;
5934 diff_view->focussed = 1;
5935 diff_view->mode = view->mode;
5936 diff_view->nlines = view->lines - begin_y;
5937 if (view_is_parent_view(view)) {
5938 view_transfer_size(diff_view, view);
5939 err = view_close_child(view);
5940 if (err)
5941 break;
5942 err = view_set_child(view, diff_view);
5943 if (err)
5944 break;
5945 view->focus_child = 1;
5946 } else
5947 *new_view = diff_view;
5948 if (err)
5949 break;
5950 break;
5952 case CTRL('d'):
5953 case 'd':
5954 nscroll /= 2;
5955 /* FALL THROUGH */
5956 case KEY_NPAGE:
5957 case CTRL('f'):
5958 case 'f':
5959 case ' ':
5960 if (s->last_displayed_line >= s->blame.nlines &&
5961 s->selected_line >= MIN(s->blame.nlines,
5962 view->nlines - 2)) {
5963 view->count = 0;
5964 break;
5966 if (s->last_displayed_line >= s->blame.nlines &&
5967 s->selected_line < view->nlines - 2) {
5968 s->selected_line +=
5969 MIN(nscroll, s->last_displayed_line -
5970 s->first_displayed_line - s->selected_line + 1);
5972 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5973 s->first_displayed_line += nscroll;
5974 else
5975 s->first_displayed_line =
5976 s->blame.nlines - (view->nlines - 3);
5977 break;
5978 case KEY_RESIZE:
5979 if (s->selected_line > view->nlines - 2) {
5980 s->selected_line = MIN(s->blame.nlines,
5981 view->nlines - 2);
5983 break;
5984 default:
5985 view->count = 0;
5986 break;
5988 return thread_err ? thread_err : err;
5991 static const struct got_error *
5992 reset_blame_view(struct tog_view *view)
5994 const struct got_error *err;
5995 struct tog_blame_view_state *s = &view->state.blame;
5997 view->count = 0;
5998 s->done = 1;
5999 err = stop_blame(&s->blame);
6000 s->done = 0;
6001 if (err)
6002 return err;
6003 return run_blame(view);
6006 static const struct got_error *
6007 cmd_blame(int argc, char *argv[])
6009 const struct got_error *error;
6010 struct got_repository *repo = NULL;
6011 struct got_worktree *worktree = NULL;
6012 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6013 char *link_target = NULL;
6014 struct got_object_id *commit_id = NULL;
6015 struct got_commit_object *commit = NULL;
6016 char *commit_id_str = NULL;
6017 int ch;
6018 struct tog_view *view;
6019 int *pack_fds = NULL;
6021 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6022 switch (ch) {
6023 case 'c':
6024 commit_id_str = optarg;
6025 break;
6026 case 'r':
6027 repo_path = realpath(optarg, NULL);
6028 if (repo_path == NULL)
6029 return got_error_from_errno2("realpath",
6030 optarg);
6031 break;
6032 default:
6033 usage_blame();
6034 /* NOTREACHED */
6038 argc -= optind;
6039 argv += optind;
6041 if (argc != 1)
6042 usage_blame();
6044 error = got_repo_pack_fds_open(&pack_fds);
6045 if (error != NULL)
6046 goto done;
6048 if (repo_path == NULL) {
6049 cwd = getcwd(NULL, 0);
6050 if (cwd == NULL)
6051 return got_error_from_errno("getcwd");
6052 error = got_worktree_open(&worktree, cwd);
6053 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6054 goto done;
6055 if (worktree)
6056 repo_path =
6057 strdup(got_worktree_get_repo_path(worktree));
6058 else
6059 repo_path = strdup(cwd);
6060 if (repo_path == NULL) {
6061 error = got_error_from_errno("strdup");
6062 goto done;
6066 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6067 if (error != NULL)
6068 goto done;
6070 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6071 worktree);
6072 if (error)
6073 goto done;
6075 init_curses();
6077 error = apply_unveil(got_repo_get_path(repo), NULL);
6078 if (error)
6079 goto done;
6081 error = tog_load_refs(repo, 0);
6082 if (error)
6083 goto done;
6085 if (commit_id_str == NULL) {
6086 struct got_reference *head_ref;
6087 error = got_ref_open(&head_ref, repo, worktree ?
6088 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6089 if (error != NULL)
6090 goto done;
6091 error = got_ref_resolve(&commit_id, repo, head_ref);
6092 got_ref_close(head_ref);
6093 } else {
6094 error = got_repo_match_object_id(&commit_id, NULL,
6095 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6097 if (error != NULL)
6098 goto done;
6100 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6101 if (view == NULL) {
6102 error = got_error_from_errno("view_open");
6103 goto done;
6106 error = got_object_open_as_commit(&commit, repo, commit_id);
6107 if (error)
6108 goto done;
6110 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6111 commit, repo);
6112 if (error)
6113 goto done;
6115 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6116 commit_id, repo);
6117 if (error)
6118 goto done;
6119 if (worktree) {
6120 /* Release work tree lock. */
6121 got_worktree_close(worktree);
6122 worktree = NULL;
6124 error = view_loop(view);
6125 done:
6126 free(repo_path);
6127 free(in_repo_path);
6128 free(link_target);
6129 free(cwd);
6130 free(commit_id);
6131 if (commit)
6132 got_object_commit_close(commit);
6133 if (worktree)
6134 got_worktree_close(worktree);
6135 if (repo) {
6136 const struct got_error *close_err = got_repo_close(repo);
6137 if (error == NULL)
6138 error = close_err;
6140 if (pack_fds) {
6141 const struct got_error *pack_err =
6142 got_repo_pack_fds_close(pack_fds);
6143 if (error == NULL)
6144 error = pack_err;
6146 tog_free_refs();
6147 return error;
6150 static const struct got_error *
6151 draw_tree_entries(struct tog_view *view, const char *parent_path)
6153 struct tog_tree_view_state *s = &view->state.tree;
6154 const struct got_error *err = NULL;
6155 struct got_tree_entry *te;
6156 wchar_t *wline;
6157 struct tog_color *tc;
6158 int width, n, i, nentries;
6159 int limit = view->nlines;
6161 s->ndisplayed = 0;
6162 if (view_is_hsplit_top(view))
6163 --limit; /* border */
6165 werase(view->window);
6167 if (limit == 0)
6168 return NULL;
6170 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6171 0, 0);
6172 if (err)
6173 return err;
6174 if (view_needs_focus_indication(view))
6175 wstandout(view->window);
6176 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6177 if (tc)
6178 wattr_on(view->window,
6179 COLOR_PAIR(tc->colorpair), NULL);
6180 waddwstr(view->window, wline);
6181 if (tc)
6182 wattr_off(view->window,
6183 COLOR_PAIR(tc->colorpair), NULL);
6184 if (view_needs_focus_indication(view))
6185 wstandend(view->window);
6186 free(wline);
6187 wline = NULL;
6188 if (width < view->ncols - 1)
6189 waddch(view->window, '\n');
6190 if (--limit <= 0)
6191 return NULL;
6192 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6193 0, 0);
6194 if (err)
6195 return err;
6196 waddwstr(view->window, wline);
6197 free(wline);
6198 wline = NULL;
6199 if (width < view->ncols - 1)
6200 waddch(view->window, '\n');
6201 if (--limit <= 0)
6202 return NULL;
6203 waddch(view->window, '\n');
6204 if (--limit <= 0)
6205 return NULL;
6207 if (s->first_displayed_entry == NULL) {
6208 te = got_object_tree_get_first_entry(s->tree);
6209 if (s->selected == 0) {
6210 if (view->focussed)
6211 wstandout(view->window);
6212 s->selected_entry = NULL;
6214 waddstr(view->window, " ..\n"); /* parent directory */
6215 if (s->selected == 0 && view->focussed)
6216 wstandend(view->window);
6217 s->ndisplayed++;
6218 if (--limit <= 0)
6219 return NULL;
6220 n = 1;
6221 } else {
6222 n = 0;
6223 te = s->first_displayed_entry;
6226 nentries = got_object_tree_get_nentries(s->tree);
6227 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6228 char *line = NULL, *id_str = NULL, *link_target = NULL;
6229 const char *modestr = "";
6230 mode_t mode;
6232 te = got_object_tree_get_entry(s->tree, i);
6233 mode = got_tree_entry_get_mode(te);
6235 if (s->show_ids) {
6236 err = got_object_id_str(&id_str,
6237 got_tree_entry_get_id(te));
6238 if (err)
6239 return got_error_from_errno(
6240 "got_object_id_str");
6242 if (got_object_tree_entry_is_submodule(te))
6243 modestr = "$";
6244 else if (S_ISLNK(mode)) {
6245 int i;
6247 err = got_tree_entry_get_symlink_target(&link_target,
6248 te, s->repo);
6249 if (err) {
6250 free(id_str);
6251 return err;
6253 for (i = 0; i < strlen(link_target); i++) {
6254 if (!isprint((unsigned char)link_target[i]))
6255 link_target[i] = '?';
6257 modestr = "@";
6259 else if (S_ISDIR(mode))
6260 modestr = "/";
6261 else if (mode & S_IXUSR)
6262 modestr = "*";
6263 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6264 got_tree_entry_get_name(te), modestr,
6265 link_target ? " -> ": "",
6266 link_target ? link_target : "") == -1) {
6267 free(id_str);
6268 free(link_target);
6269 return got_error_from_errno("asprintf");
6271 free(id_str);
6272 free(link_target);
6273 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6274 0, 0);
6275 if (err) {
6276 free(line);
6277 break;
6279 if (n == s->selected) {
6280 if (view->focussed)
6281 wstandout(view->window);
6282 s->selected_entry = te;
6284 tc = match_color(&s->colors, line);
6285 if (tc)
6286 wattr_on(view->window,
6287 COLOR_PAIR(tc->colorpair), NULL);
6288 waddwstr(view->window, wline);
6289 if (tc)
6290 wattr_off(view->window,
6291 COLOR_PAIR(tc->colorpair), NULL);
6292 if (width < view->ncols - 1)
6293 waddch(view->window, '\n');
6294 if (n == s->selected && view->focussed)
6295 wstandend(view->window);
6296 free(line);
6297 free(wline);
6298 wline = NULL;
6299 n++;
6300 s->ndisplayed++;
6301 s->last_displayed_entry = te;
6302 if (--limit <= 0)
6303 break;
6306 return err;
6309 static void
6310 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6312 struct got_tree_entry *te;
6313 int isroot = s->tree == s->root;
6314 int i = 0;
6316 if (s->first_displayed_entry == NULL)
6317 return;
6319 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6320 while (i++ < maxscroll) {
6321 if (te == NULL) {
6322 if (!isroot)
6323 s->first_displayed_entry = NULL;
6324 break;
6326 s->first_displayed_entry = te;
6327 te = got_tree_entry_get_prev(s->tree, te);
6331 static const struct got_error *
6332 tree_scroll_down(struct tog_view *view, int maxscroll)
6334 struct tog_tree_view_state *s = &view->state.tree;
6335 struct got_tree_entry *next, *last;
6336 int n = 0;
6338 if (s->first_displayed_entry)
6339 next = got_tree_entry_get_next(s->tree,
6340 s->first_displayed_entry);
6341 else
6342 next = got_object_tree_get_first_entry(s->tree);
6344 last = s->last_displayed_entry;
6345 while (next && n++ < maxscroll) {
6346 if (last)
6347 last = got_tree_entry_get_next(s->tree, last);
6348 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6349 s->first_displayed_entry = next;
6350 next = got_tree_entry_get_next(s->tree, next);
6354 return NULL;
6357 static const struct got_error *
6358 tree_entry_path(char **path, struct tog_parent_trees *parents,
6359 struct got_tree_entry *te)
6361 const struct got_error *err = NULL;
6362 struct tog_parent_tree *pt;
6363 size_t len = 2; /* for leading slash and NUL */
6365 TAILQ_FOREACH(pt, parents, entry)
6366 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6367 + 1 /* slash */;
6368 if (te)
6369 len += strlen(got_tree_entry_get_name(te));
6371 *path = calloc(1, len);
6372 if (path == NULL)
6373 return got_error_from_errno("calloc");
6375 (*path)[0] = '/';
6376 pt = TAILQ_LAST(parents, tog_parent_trees);
6377 while (pt) {
6378 const char *name = got_tree_entry_get_name(pt->selected_entry);
6379 if (strlcat(*path, name, len) >= len) {
6380 err = got_error(GOT_ERR_NO_SPACE);
6381 goto done;
6383 if (strlcat(*path, "/", len) >= len) {
6384 err = got_error(GOT_ERR_NO_SPACE);
6385 goto done;
6387 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6389 if (te) {
6390 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6391 err = got_error(GOT_ERR_NO_SPACE);
6392 goto done;
6395 done:
6396 if (err) {
6397 free(*path);
6398 *path = NULL;
6400 return err;
6403 static const struct got_error *
6404 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6405 struct got_tree_entry *te, struct tog_parent_trees *parents,
6406 struct got_object_id *commit_id, struct got_repository *repo)
6408 const struct got_error *err = NULL;
6409 char *path;
6410 struct tog_view *blame_view;
6412 *new_view = NULL;
6414 err = tree_entry_path(&path, parents, te);
6415 if (err)
6416 return err;
6418 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6419 if (blame_view == NULL) {
6420 err = got_error_from_errno("view_open");
6421 goto done;
6424 err = open_blame_view(blame_view, path, commit_id, repo);
6425 if (err) {
6426 if (err->code == GOT_ERR_CANCELLED)
6427 err = NULL;
6428 view_close(blame_view);
6429 } else
6430 *new_view = blame_view;
6431 done:
6432 free(path);
6433 return err;
6436 static const struct got_error *
6437 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6438 struct tog_tree_view_state *s)
6440 struct tog_view *log_view;
6441 const struct got_error *err = NULL;
6442 char *path;
6444 *new_view = NULL;
6446 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6447 if (log_view == NULL)
6448 return got_error_from_errno("view_open");
6450 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6451 if (err)
6452 return err;
6454 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6455 path, 0);
6456 if (err)
6457 view_close(log_view);
6458 else
6459 *new_view = log_view;
6460 free(path);
6461 return err;
6464 static const struct got_error *
6465 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6466 const char *head_ref_name, struct got_repository *repo)
6468 const struct got_error *err = NULL;
6469 char *commit_id_str = NULL;
6470 struct tog_tree_view_state *s = &view->state.tree;
6471 struct got_commit_object *commit = NULL;
6473 TAILQ_INIT(&s->parents);
6474 STAILQ_INIT(&s->colors);
6476 s->commit_id = got_object_id_dup(commit_id);
6477 if (s->commit_id == NULL)
6478 return got_error_from_errno("got_object_id_dup");
6480 err = got_object_open_as_commit(&commit, repo, commit_id);
6481 if (err)
6482 goto done;
6485 * The root is opened here and will be closed when the view is closed.
6486 * Any visited subtrees and their path-wise parents are opened and
6487 * closed on demand.
6489 err = got_object_open_as_tree(&s->root, repo,
6490 got_object_commit_get_tree_id(commit));
6491 if (err)
6492 goto done;
6493 s->tree = s->root;
6495 err = got_object_id_str(&commit_id_str, commit_id);
6496 if (err != NULL)
6497 goto done;
6499 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6500 err = got_error_from_errno("asprintf");
6501 goto done;
6504 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6505 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6506 if (head_ref_name) {
6507 s->head_ref_name = strdup(head_ref_name);
6508 if (s->head_ref_name == NULL) {
6509 err = got_error_from_errno("strdup");
6510 goto done;
6513 s->repo = repo;
6515 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6516 err = add_color(&s->colors, "\\$$",
6517 TOG_COLOR_TREE_SUBMODULE,
6518 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6519 if (err)
6520 goto done;
6521 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6522 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6523 if (err)
6524 goto done;
6525 err = add_color(&s->colors, "/$",
6526 TOG_COLOR_TREE_DIRECTORY,
6527 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6528 if (err)
6529 goto done;
6531 err = add_color(&s->colors, "\\*$",
6532 TOG_COLOR_TREE_EXECUTABLE,
6533 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6534 if (err)
6535 goto done;
6537 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6538 get_color_value("TOG_COLOR_COMMIT"));
6539 if (err)
6540 goto done;
6543 view->show = show_tree_view;
6544 view->input = input_tree_view;
6545 view->close = close_tree_view;
6546 view->search_start = search_start_tree_view;
6547 view->search_next = search_next_tree_view;
6548 done:
6549 free(commit_id_str);
6550 if (commit)
6551 got_object_commit_close(commit);
6552 if (err)
6553 close_tree_view(view);
6554 return err;
6557 static const struct got_error *
6558 close_tree_view(struct tog_view *view)
6560 struct tog_tree_view_state *s = &view->state.tree;
6562 free_colors(&s->colors);
6563 free(s->tree_label);
6564 s->tree_label = NULL;
6565 free(s->commit_id);
6566 s->commit_id = NULL;
6567 free(s->head_ref_name);
6568 s->head_ref_name = NULL;
6569 while (!TAILQ_EMPTY(&s->parents)) {
6570 struct tog_parent_tree *parent;
6571 parent = TAILQ_FIRST(&s->parents);
6572 TAILQ_REMOVE(&s->parents, parent, entry);
6573 if (parent->tree != s->root)
6574 got_object_tree_close(parent->tree);
6575 free(parent);
6578 if (s->tree != NULL && s->tree != s->root)
6579 got_object_tree_close(s->tree);
6580 if (s->root)
6581 got_object_tree_close(s->root);
6582 return NULL;
6585 static const struct got_error *
6586 search_start_tree_view(struct tog_view *view)
6588 struct tog_tree_view_state *s = &view->state.tree;
6590 s->matched_entry = NULL;
6591 return NULL;
6594 static int
6595 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6597 regmatch_t regmatch;
6599 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6600 0) == 0;
6603 static const struct got_error *
6604 search_next_tree_view(struct tog_view *view)
6606 struct tog_tree_view_state *s = &view->state.tree;
6607 struct got_tree_entry *te = NULL;
6609 if (!view->searching) {
6610 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6611 return NULL;
6614 if (s->matched_entry) {
6615 if (view->searching == TOG_SEARCH_FORWARD) {
6616 if (s->selected_entry)
6617 te = got_tree_entry_get_next(s->tree,
6618 s->selected_entry);
6619 else
6620 te = got_object_tree_get_first_entry(s->tree);
6621 } else {
6622 if (s->selected_entry == NULL)
6623 te = got_object_tree_get_last_entry(s->tree);
6624 else
6625 te = got_tree_entry_get_prev(s->tree,
6626 s->selected_entry);
6628 } else {
6629 if (s->selected_entry)
6630 te = s->selected_entry;
6631 else if (view->searching == TOG_SEARCH_FORWARD)
6632 te = got_object_tree_get_first_entry(s->tree);
6633 else
6634 te = got_object_tree_get_last_entry(s->tree);
6637 while (1) {
6638 if (te == NULL) {
6639 if (s->matched_entry == NULL) {
6640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6641 return NULL;
6643 if (view->searching == TOG_SEARCH_FORWARD)
6644 te = got_object_tree_get_first_entry(s->tree);
6645 else
6646 te = got_object_tree_get_last_entry(s->tree);
6649 if (match_tree_entry(te, &view->regex)) {
6650 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6651 s->matched_entry = te;
6652 break;
6655 if (view->searching == TOG_SEARCH_FORWARD)
6656 te = got_tree_entry_get_next(s->tree, te);
6657 else
6658 te = got_tree_entry_get_prev(s->tree, te);
6661 if (s->matched_entry) {
6662 s->first_displayed_entry = s->matched_entry;
6663 s->selected = 0;
6666 return NULL;
6669 static const struct got_error *
6670 show_tree_view(struct tog_view *view)
6672 const struct got_error *err = NULL;
6673 struct tog_tree_view_state *s = &view->state.tree;
6674 char *parent_path;
6676 err = tree_entry_path(&parent_path, &s->parents, NULL);
6677 if (err)
6678 return err;
6680 err = draw_tree_entries(view, parent_path);
6681 free(parent_path);
6683 view_border(view);
6684 return err;
6687 static const struct got_error *
6688 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6690 const struct got_error *err = NULL;
6691 struct tog_tree_view_state *s = &view->state.tree;
6692 struct tog_view *log_view, *ref_view;
6693 struct got_tree_entry *te;
6694 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6696 switch (ch) {
6697 case 'i':
6698 s->show_ids = !s->show_ids;
6699 view->count = 0;
6700 break;
6701 case 'l':
6702 view->count = 0;
6703 if (!s->selected_entry)
6704 break;
6705 if (view_is_parent_view(view))
6706 view_get_split(view, &begin_y, &begin_x);
6707 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6708 if (view_is_parent_view(view) &&
6709 view->mode == TOG_VIEW_SPLIT_HRZN) {
6710 err = view_init_hsplit(view, begin_y);
6711 if (err)
6712 break;
6714 view->focussed = 0;
6715 log_view->focussed = 1;
6716 log_view->mode = view->mode;
6717 log_view->nlines = view->lines - begin_y;
6718 if (view_is_parent_view(view)) {
6719 view_transfer_size(log_view, view);
6720 err = view_close_child(view);
6721 if (err)
6722 return err;
6723 err = view_set_child(view, log_view);
6724 if (err)
6725 return err;
6726 view->focus_child = 1;
6727 } else
6728 *new_view = log_view;
6729 break;
6730 case 'r':
6731 view->count = 0;
6732 if (view_is_parent_view(view))
6733 view_get_split(view, &begin_y, &begin_x);
6734 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6735 if (ref_view == NULL)
6736 return got_error_from_errno("view_open");
6737 err = open_ref_view(ref_view, s->repo);
6738 if (err) {
6739 view_close(ref_view);
6740 return err;
6742 if (view_is_parent_view(view) &&
6743 view->mode == TOG_VIEW_SPLIT_HRZN) {
6744 err = view_init_hsplit(view, begin_y);
6745 if (err)
6746 break;
6748 view->focussed = 0;
6749 ref_view->focussed = 1;
6750 ref_view->mode = view->mode;
6751 ref_view->nlines = view->lines - begin_y;
6752 if (view_is_parent_view(view)) {
6753 view_transfer_size(ref_view, view);
6754 err = view_close_child(view);
6755 if (err)
6756 return err;
6757 err = view_set_child(view, ref_view);
6758 if (err)
6759 return err;
6760 view->focus_child = 1;
6761 } else
6762 *new_view = ref_view;
6763 break;
6764 case 'g':
6765 case KEY_HOME:
6766 s->selected = 0;
6767 view->count = 0;
6768 if (s->tree == s->root)
6769 s->first_displayed_entry =
6770 got_object_tree_get_first_entry(s->tree);
6771 else
6772 s->first_displayed_entry = NULL;
6773 break;
6774 case 'G':
6775 case KEY_END: {
6776 int eos = view->nlines - 3;
6778 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6779 --eos; /* border */
6780 s->selected = 0;
6781 view->count = 0;
6782 te = got_object_tree_get_last_entry(s->tree);
6783 for (n = 0; n < eos; n++) {
6784 if (te == NULL) {
6785 if(s->tree != s->root) {
6786 s->first_displayed_entry = NULL;
6787 n++;
6789 break;
6791 s->first_displayed_entry = te;
6792 te = got_tree_entry_get_prev(s->tree, te);
6794 if (n > 0)
6795 s->selected = n - 1;
6796 break;
6798 case 'k':
6799 case KEY_UP:
6800 case CTRL('p'):
6801 if (s->selected > 0) {
6802 s->selected--;
6803 break;
6805 tree_scroll_up(s, 1);
6806 if (s->selected_entry == NULL ||
6807 (s->tree == s->root && s->selected_entry ==
6808 got_object_tree_get_first_entry(s->tree)))
6809 view->count = 0;
6810 break;
6811 case CTRL('u'):
6812 case 'u':
6813 nscroll /= 2;
6814 /* FALL THROUGH */
6815 case KEY_PPAGE:
6816 case CTRL('b'):
6817 case 'b':
6818 if (s->tree == s->root) {
6819 if (got_object_tree_get_first_entry(s->tree) ==
6820 s->first_displayed_entry)
6821 s->selected -= MIN(s->selected, nscroll);
6822 } else {
6823 if (s->first_displayed_entry == NULL)
6824 s->selected -= MIN(s->selected, nscroll);
6826 tree_scroll_up(s, MAX(0, nscroll));
6827 if (s->selected_entry == NULL ||
6828 (s->tree == s->root && s->selected_entry ==
6829 got_object_tree_get_first_entry(s->tree)))
6830 view->count = 0;
6831 break;
6832 case 'j':
6833 case KEY_DOWN:
6834 case CTRL('n'):
6835 if (s->selected < s->ndisplayed - 1) {
6836 s->selected++;
6837 break;
6839 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6840 == NULL) {
6841 /* can't scroll any further */
6842 view->count = 0;
6843 break;
6845 tree_scroll_down(view, 1);
6846 break;
6847 case CTRL('d'):
6848 case 'd':
6849 nscroll /= 2;
6850 /* FALL THROUGH */
6851 case KEY_NPAGE:
6852 case CTRL('f'):
6853 case 'f':
6854 case ' ':
6855 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6856 == NULL) {
6857 /* can't scroll any further; move cursor down */
6858 if (s->selected < s->ndisplayed - 1)
6859 s->selected += MIN(nscroll,
6860 s->ndisplayed - s->selected - 1);
6861 else
6862 view->count = 0;
6863 break;
6865 tree_scroll_down(view, nscroll);
6866 break;
6867 case KEY_ENTER:
6868 case '\r':
6869 case KEY_BACKSPACE:
6870 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6871 struct tog_parent_tree *parent;
6872 /* user selected '..' */
6873 if (s->tree == s->root) {
6874 view->count = 0;
6875 break;
6877 parent = TAILQ_FIRST(&s->parents);
6878 TAILQ_REMOVE(&s->parents, parent,
6879 entry);
6880 got_object_tree_close(s->tree);
6881 s->tree = parent->tree;
6882 s->first_displayed_entry =
6883 parent->first_displayed_entry;
6884 s->selected_entry =
6885 parent->selected_entry;
6886 s->selected = parent->selected;
6887 if (s->selected > view->nlines - 3) {
6888 err = offset_selection_down(view);
6889 if (err)
6890 break;
6892 free(parent);
6893 } else if (S_ISDIR(got_tree_entry_get_mode(
6894 s->selected_entry))) {
6895 struct got_tree_object *subtree;
6896 view->count = 0;
6897 err = got_object_open_as_tree(&subtree, s->repo,
6898 got_tree_entry_get_id(s->selected_entry));
6899 if (err)
6900 break;
6901 err = tree_view_visit_subtree(s, subtree);
6902 if (err) {
6903 got_object_tree_close(subtree);
6904 break;
6906 } else if (S_ISREG(got_tree_entry_get_mode(
6907 s->selected_entry))) {
6908 struct tog_view *blame_view;
6909 int begin_x = 0, begin_y = 0;
6911 if (view_is_parent_view(view))
6912 view_get_split(view, &begin_y, &begin_x);
6914 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6915 s->selected_entry, &s->parents,
6916 s->commit_id, s->repo);
6917 if (err)
6918 break;
6920 if (view_is_parent_view(view) &&
6921 view->mode == TOG_VIEW_SPLIT_HRZN) {
6922 err = view_init_hsplit(view, begin_y);
6923 if (err)
6924 break;
6927 view->count = 0;
6928 view->focussed = 0;
6929 blame_view->focussed = 1;
6930 blame_view->mode = view->mode;
6931 blame_view->nlines = view->lines - begin_y;
6932 if (view_is_parent_view(view)) {
6933 view_transfer_size(blame_view, view);
6934 err = view_close_child(view);
6935 if (err)
6936 return err;
6937 err = view_set_child(view, blame_view);
6938 if (err)
6939 return err;
6940 view->focus_child = 1;
6941 } else
6942 *new_view = blame_view;
6944 break;
6945 case KEY_RESIZE:
6946 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6947 s->selected = view->nlines - 4;
6948 view->count = 0;
6949 break;
6950 default:
6951 view->count = 0;
6952 break;
6955 return err;
6958 __dead static void
6959 usage_tree(void)
6961 endwin();
6962 fprintf(stderr,
6963 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6964 getprogname());
6965 exit(1);
6968 static const struct got_error *
6969 cmd_tree(int argc, char *argv[])
6971 const struct got_error *error;
6972 struct got_repository *repo = NULL;
6973 struct got_worktree *worktree = NULL;
6974 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6975 struct got_object_id *commit_id = NULL;
6976 struct got_commit_object *commit = NULL;
6977 const char *commit_id_arg = NULL;
6978 char *label = NULL;
6979 struct got_reference *ref = NULL;
6980 const char *head_ref_name = NULL;
6981 int ch;
6982 struct tog_view *view;
6983 int *pack_fds = NULL;
6985 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6986 switch (ch) {
6987 case 'c':
6988 commit_id_arg = optarg;
6989 break;
6990 case 'r':
6991 repo_path = realpath(optarg, NULL);
6992 if (repo_path == NULL)
6993 return got_error_from_errno2("realpath",
6994 optarg);
6995 break;
6996 default:
6997 usage_tree();
6998 /* NOTREACHED */
7002 argc -= optind;
7003 argv += optind;
7005 if (argc > 1)
7006 usage_tree();
7008 error = got_repo_pack_fds_open(&pack_fds);
7009 if (error != NULL)
7010 goto done;
7012 if (repo_path == NULL) {
7013 cwd = getcwd(NULL, 0);
7014 if (cwd == NULL)
7015 return got_error_from_errno("getcwd");
7016 error = got_worktree_open(&worktree, cwd);
7017 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7018 goto done;
7019 if (worktree)
7020 repo_path =
7021 strdup(got_worktree_get_repo_path(worktree));
7022 else
7023 repo_path = strdup(cwd);
7024 if (repo_path == NULL) {
7025 error = got_error_from_errno("strdup");
7026 goto done;
7030 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7031 if (error != NULL)
7032 goto done;
7034 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7035 repo, worktree);
7036 if (error)
7037 goto done;
7039 init_curses();
7041 error = apply_unveil(got_repo_get_path(repo), NULL);
7042 if (error)
7043 goto done;
7045 error = tog_load_refs(repo, 0);
7046 if (error)
7047 goto done;
7049 if (commit_id_arg == NULL) {
7050 error = got_repo_match_object_id(&commit_id, &label,
7051 worktree ? got_worktree_get_head_ref_name(worktree) :
7052 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7053 if (error)
7054 goto done;
7055 head_ref_name = label;
7056 } else {
7057 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7058 if (error == NULL)
7059 head_ref_name = got_ref_get_name(ref);
7060 else if (error->code != GOT_ERR_NOT_REF)
7061 goto done;
7062 error = got_repo_match_object_id(&commit_id, NULL,
7063 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7064 if (error)
7065 goto done;
7068 error = got_object_open_as_commit(&commit, repo, commit_id);
7069 if (error)
7070 goto done;
7072 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7073 if (view == NULL) {
7074 error = got_error_from_errno("view_open");
7075 goto done;
7077 error = open_tree_view(view, commit_id, head_ref_name, repo);
7078 if (error)
7079 goto done;
7080 if (!got_path_is_root_dir(in_repo_path)) {
7081 error = tree_view_walk_path(&view->state.tree, commit,
7082 in_repo_path);
7083 if (error)
7084 goto done;
7087 if (worktree) {
7088 /* Release work tree lock. */
7089 got_worktree_close(worktree);
7090 worktree = NULL;
7092 error = view_loop(view);
7093 done:
7094 free(repo_path);
7095 free(cwd);
7096 free(commit_id);
7097 free(label);
7098 if (ref)
7099 got_ref_close(ref);
7100 if (repo) {
7101 const struct got_error *close_err = got_repo_close(repo);
7102 if (error == NULL)
7103 error = close_err;
7105 if (pack_fds) {
7106 const struct got_error *pack_err =
7107 got_repo_pack_fds_close(pack_fds);
7108 if (error == NULL)
7109 error = pack_err;
7111 tog_free_refs();
7112 return error;
7115 static const struct got_error *
7116 ref_view_load_refs(struct tog_ref_view_state *s)
7118 struct got_reflist_entry *sre;
7119 struct tog_reflist_entry *re;
7121 s->nrefs = 0;
7122 TAILQ_FOREACH(sre, &tog_refs, entry) {
7123 if (strncmp(got_ref_get_name(sre->ref),
7124 "refs/got/", 9) == 0 &&
7125 strncmp(got_ref_get_name(sre->ref),
7126 "refs/got/backup/", 16) != 0)
7127 continue;
7129 re = malloc(sizeof(*re));
7130 if (re == NULL)
7131 return got_error_from_errno("malloc");
7133 re->ref = got_ref_dup(sre->ref);
7134 if (re->ref == NULL)
7135 return got_error_from_errno("got_ref_dup");
7136 re->idx = s->nrefs++;
7137 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7140 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7141 return NULL;
7144 static void
7145 ref_view_free_refs(struct tog_ref_view_state *s)
7147 struct tog_reflist_entry *re;
7149 while (!TAILQ_EMPTY(&s->refs)) {
7150 re = TAILQ_FIRST(&s->refs);
7151 TAILQ_REMOVE(&s->refs, re, entry);
7152 got_ref_close(re->ref);
7153 free(re);
7157 static const struct got_error *
7158 open_ref_view(struct tog_view *view, struct got_repository *repo)
7160 const struct got_error *err = NULL;
7161 struct tog_ref_view_state *s = &view->state.ref;
7163 s->selected_entry = 0;
7164 s->repo = repo;
7166 TAILQ_INIT(&s->refs);
7167 STAILQ_INIT(&s->colors);
7169 err = ref_view_load_refs(s);
7170 if (err)
7171 return err;
7173 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7174 err = add_color(&s->colors, "^refs/heads/",
7175 TOG_COLOR_REFS_HEADS,
7176 get_color_value("TOG_COLOR_REFS_HEADS"));
7177 if (err)
7178 goto done;
7180 err = add_color(&s->colors, "^refs/tags/",
7181 TOG_COLOR_REFS_TAGS,
7182 get_color_value("TOG_COLOR_REFS_TAGS"));
7183 if (err)
7184 goto done;
7186 err = add_color(&s->colors, "^refs/remotes/",
7187 TOG_COLOR_REFS_REMOTES,
7188 get_color_value("TOG_COLOR_REFS_REMOTES"));
7189 if (err)
7190 goto done;
7192 err = add_color(&s->colors, "^refs/got/backup/",
7193 TOG_COLOR_REFS_BACKUP,
7194 get_color_value("TOG_COLOR_REFS_BACKUP"));
7195 if (err)
7196 goto done;
7199 view->show = show_ref_view;
7200 view->input = input_ref_view;
7201 view->close = close_ref_view;
7202 view->search_start = search_start_ref_view;
7203 view->search_next = search_next_ref_view;
7204 done:
7205 if (err)
7206 free_colors(&s->colors);
7207 return err;
7210 static const struct got_error *
7211 close_ref_view(struct tog_view *view)
7213 struct tog_ref_view_state *s = &view->state.ref;
7215 ref_view_free_refs(s);
7216 free_colors(&s->colors);
7218 return NULL;
7221 static const struct got_error *
7222 resolve_reflist_entry(struct got_object_id **commit_id,
7223 struct tog_reflist_entry *re, struct got_repository *repo)
7225 const struct got_error *err = NULL;
7226 struct got_object_id *obj_id;
7227 struct got_tag_object *tag = NULL;
7228 int obj_type;
7230 *commit_id = NULL;
7232 err = got_ref_resolve(&obj_id, repo, re->ref);
7233 if (err)
7234 return err;
7236 err = got_object_get_type(&obj_type, repo, obj_id);
7237 if (err)
7238 goto done;
7240 switch (obj_type) {
7241 case GOT_OBJ_TYPE_COMMIT:
7242 *commit_id = obj_id;
7243 break;
7244 case GOT_OBJ_TYPE_TAG:
7245 err = got_object_open_as_tag(&tag, repo, obj_id);
7246 if (err)
7247 goto done;
7248 free(obj_id);
7249 err = got_object_get_type(&obj_type, repo,
7250 got_object_tag_get_object_id(tag));
7251 if (err)
7252 goto done;
7253 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7254 err = got_error(GOT_ERR_OBJ_TYPE);
7255 goto done;
7257 *commit_id = got_object_id_dup(
7258 got_object_tag_get_object_id(tag));
7259 if (*commit_id == NULL) {
7260 err = got_error_from_errno("got_object_id_dup");
7261 goto done;
7263 break;
7264 default:
7265 err = got_error(GOT_ERR_OBJ_TYPE);
7266 break;
7269 done:
7270 if (tag)
7271 got_object_tag_close(tag);
7272 if (err) {
7273 free(*commit_id);
7274 *commit_id = NULL;
7276 return err;
7279 static const struct got_error *
7280 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7281 struct tog_reflist_entry *re, struct got_repository *repo)
7283 struct tog_view *log_view;
7284 const struct got_error *err = NULL;
7285 struct got_object_id *commit_id = NULL;
7287 *new_view = NULL;
7289 err = resolve_reflist_entry(&commit_id, re, repo);
7290 if (err) {
7291 if (err->code != GOT_ERR_OBJ_TYPE)
7292 return err;
7293 else
7294 return NULL;
7297 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7298 if (log_view == NULL) {
7299 err = got_error_from_errno("view_open");
7300 goto done;
7303 err = open_log_view(log_view, commit_id, repo,
7304 got_ref_get_name(re->ref), "", 0);
7305 done:
7306 if (err)
7307 view_close(log_view);
7308 else
7309 *new_view = log_view;
7310 free(commit_id);
7311 return err;
7314 static void
7315 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7317 struct tog_reflist_entry *re;
7318 int i = 0;
7320 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7321 return;
7323 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7324 while (i++ < maxscroll) {
7325 if (re == NULL)
7326 break;
7327 s->first_displayed_entry = re;
7328 re = TAILQ_PREV(re, tog_reflist_head, entry);
7332 static const struct got_error *
7333 ref_scroll_down(struct tog_view *view, int maxscroll)
7335 struct tog_ref_view_state *s = &view->state.ref;
7336 struct tog_reflist_entry *next, *last;
7337 int n = 0;
7339 if (s->first_displayed_entry)
7340 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7341 else
7342 next = TAILQ_FIRST(&s->refs);
7344 last = s->last_displayed_entry;
7345 while (next && n++ < maxscroll) {
7346 if (last)
7347 last = TAILQ_NEXT(last, entry);
7348 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7349 s->first_displayed_entry = next;
7350 next = TAILQ_NEXT(next, entry);
7354 return NULL;
7357 static const struct got_error *
7358 search_start_ref_view(struct tog_view *view)
7360 struct tog_ref_view_state *s = &view->state.ref;
7362 s->matched_entry = NULL;
7363 return NULL;
7366 static int
7367 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7369 regmatch_t regmatch;
7371 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7372 0) == 0;
7375 static const struct got_error *
7376 search_next_ref_view(struct tog_view *view)
7378 struct tog_ref_view_state *s = &view->state.ref;
7379 struct tog_reflist_entry *re = NULL;
7381 if (!view->searching) {
7382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7383 return NULL;
7386 if (s->matched_entry) {
7387 if (view->searching == TOG_SEARCH_FORWARD) {
7388 if (s->selected_entry)
7389 re = TAILQ_NEXT(s->selected_entry, entry);
7390 else
7391 re = TAILQ_PREV(s->selected_entry,
7392 tog_reflist_head, entry);
7393 } else {
7394 if (s->selected_entry == NULL)
7395 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7396 else
7397 re = TAILQ_PREV(s->selected_entry,
7398 tog_reflist_head, entry);
7400 } else {
7401 if (s->selected_entry)
7402 re = s->selected_entry;
7403 else if (view->searching == TOG_SEARCH_FORWARD)
7404 re = TAILQ_FIRST(&s->refs);
7405 else
7406 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7409 while (1) {
7410 if (re == NULL) {
7411 if (s->matched_entry == NULL) {
7412 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7413 return NULL;
7415 if (view->searching == TOG_SEARCH_FORWARD)
7416 re = TAILQ_FIRST(&s->refs);
7417 else
7418 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7421 if (match_reflist_entry(re, &view->regex)) {
7422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7423 s->matched_entry = re;
7424 break;
7427 if (view->searching == TOG_SEARCH_FORWARD)
7428 re = TAILQ_NEXT(re, entry);
7429 else
7430 re = TAILQ_PREV(re, tog_reflist_head, entry);
7433 if (s->matched_entry) {
7434 s->first_displayed_entry = s->matched_entry;
7435 s->selected = 0;
7438 return NULL;
7441 static const struct got_error *
7442 show_ref_view(struct tog_view *view)
7444 const struct got_error *err = NULL;
7445 struct tog_ref_view_state *s = &view->state.ref;
7446 struct tog_reflist_entry *re;
7447 char *line = NULL;
7448 wchar_t *wline;
7449 struct tog_color *tc;
7450 int width, n;
7451 int limit = view->nlines;
7453 werase(view->window);
7455 s->ndisplayed = 0;
7456 if (view_is_hsplit_top(view))
7457 --limit; /* border */
7459 if (limit == 0)
7460 return NULL;
7462 re = s->first_displayed_entry;
7464 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7465 s->nrefs) == -1)
7466 return got_error_from_errno("asprintf");
7468 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7469 if (err) {
7470 free(line);
7471 return err;
7473 if (view_needs_focus_indication(view))
7474 wstandout(view->window);
7475 waddwstr(view->window, wline);
7476 if (view_needs_focus_indication(view))
7477 wstandend(view->window);
7478 free(wline);
7479 wline = NULL;
7480 free(line);
7481 line = NULL;
7482 if (width < view->ncols - 1)
7483 waddch(view->window, '\n');
7484 if (--limit <= 0)
7485 return NULL;
7487 n = 0;
7488 while (re && limit > 0) {
7489 char *line = NULL;
7490 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7492 if (s->show_date) {
7493 struct got_commit_object *ci;
7494 struct got_tag_object *tag;
7495 struct got_object_id *id;
7496 struct tm tm;
7497 time_t t;
7499 err = got_ref_resolve(&id, s->repo, re->ref);
7500 if (err)
7501 return err;
7502 err = got_object_open_as_tag(&tag, s->repo, id);
7503 if (err) {
7504 if (err->code != GOT_ERR_OBJ_TYPE) {
7505 free(id);
7506 return err;
7508 err = got_object_open_as_commit(&ci, s->repo,
7509 id);
7510 if (err) {
7511 free(id);
7512 return err;
7514 t = got_object_commit_get_committer_time(ci);
7515 got_object_commit_close(ci);
7516 } else {
7517 t = got_object_tag_get_tagger_time(tag);
7518 got_object_tag_close(tag);
7520 free(id);
7521 if (gmtime_r(&t, &tm) == NULL)
7522 return got_error_from_errno("gmtime_r");
7523 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7524 return got_error(GOT_ERR_NO_SPACE);
7526 if (got_ref_is_symbolic(re->ref)) {
7527 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7528 ymd : "", got_ref_get_name(re->ref),
7529 got_ref_get_symref_target(re->ref)) == -1)
7530 return got_error_from_errno("asprintf");
7531 } else if (s->show_ids) {
7532 struct got_object_id *id;
7533 char *id_str;
7534 err = got_ref_resolve(&id, s->repo, re->ref);
7535 if (err)
7536 return err;
7537 err = got_object_id_str(&id_str, id);
7538 if (err) {
7539 free(id);
7540 return err;
7542 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7543 got_ref_get_name(re->ref), id_str) == -1) {
7544 err = got_error_from_errno("asprintf");
7545 free(id);
7546 free(id_str);
7547 return err;
7549 free(id);
7550 free(id_str);
7551 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7552 got_ref_get_name(re->ref)) == -1)
7553 return got_error_from_errno("asprintf");
7555 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7556 0, 0);
7557 if (err) {
7558 free(line);
7559 return err;
7561 if (n == s->selected) {
7562 if (view->focussed)
7563 wstandout(view->window);
7564 s->selected_entry = re;
7566 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7567 if (tc)
7568 wattr_on(view->window,
7569 COLOR_PAIR(tc->colorpair), NULL);
7570 waddwstr(view->window, wline);
7571 if (tc)
7572 wattr_off(view->window,
7573 COLOR_PAIR(tc->colorpair), NULL);
7574 if (width < view->ncols - 1)
7575 waddch(view->window, '\n');
7576 if (n == s->selected && view->focussed)
7577 wstandend(view->window);
7578 free(line);
7579 free(wline);
7580 wline = NULL;
7581 n++;
7582 s->ndisplayed++;
7583 s->last_displayed_entry = re;
7585 limit--;
7586 re = TAILQ_NEXT(re, entry);
7589 view_border(view);
7590 return err;
7593 static const struct got_error *
7594 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7595 struct tog_reflist_entry *re, struct got_repository *repo)
7597 const struct got_error *err = NULL;
7598 struct got_object_id *commit_id = NULL;
7599 struct tog_view *tree_view;
7601 *new_view = NULL;
7603 err = resolve_reflist_entry(&commit_id, re, repo);
7604 if (err) {
7605 if (err->code != GOT_ERR_OBJ_TYPE)
7606 return err;
7607 else
7608 return NULL;
7612 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7613 if (tree_view == NULL) {
7614 err = got_error_from_errno("view_open");
7615 goto done;
7618 err = open_tree_view(tree_view, commit_id,
7619 got_ref_get_name(re->ref), repo);
7620 if (err)
7621 goto done;
7623 *new_view = tree_view;
7624 done:
7625 free(commit_id);
7626 return err;
7628 static const struct got_error *
7629 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7631 const struct got_error *err = NULL;
7632 struct tog_ref_view_state *s = &view->state.ref;
7633 struct tog_view *log_view, *tree_view;
7634 struct tog_reflist_entry *re;
7635 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7637 switch (ch) {
7638 case 'i':
7639 s->show_ids = !s->show_ids;
7640 view->count = 0;
7641 break;
7642 case 'm':
7643 s->show_date = !s->show_date;
7644 view->count = 0;
7645 break;
7646 case 'o':
7647 s->sort_by_date = !s->sort_by_date;
7648 view->count = 0;
7649 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7650 got_ref_cmp_by_commit_timestamp_descending :
7651 tog_ref_cmp_by_name, s->repo);
7652 if (err)
7653 break;
7654 got_reflist_object_id_map_free(tog_refs_idmap);
7655 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7656 &tog_refs, s->repo);
7657 if (err)
7658 break;
7659 ref_view_free_refs(s);
7660 err = ref_view_load_refs(s);
7661 break;
7662 case KEY_ENTER:
7663 case '\r':
7664 view->count = 0;
7665 if (!s->selected_entry)
7666 break;
7667 if (view_is_parent_view(view))
7668 view_get_split(view, &begin_y, &begin_x);
7670 err = log_ref_entry(&log_view, begin_y, begin_x,
7671 s->selected_entry, s->repo);
7672 if (err)
7673 break;
7675 if (view_is_parent_view(view) &&
7676 view->mode == TOG_VIEW_SPLIT_HRZN) {
7677 err = view_init_hsplit(view, begin_y);
7678 if (err)
7679 break;
7682 view->focussed = 0;
7683 log_view->focussed = 1;
7684 log_view->mode = view->mode;
7685 log_view->nlines = view->lines - begin_y;
7686 if (view_is_parent_view(view)) {
7687 view_transfer_size(log_view, view);
7688 err = view_close_child(view);
7689 if (err)
7690 return err;
7691 err = view_set_child(view, log_view);
7692 if (err)
7693 return err;
7694 view->focus_child = 1;
7695 } else
7696 *new_view = log_view;
7697 break;
7698 case 't':
7699 view->count = 0;
7700 if (!s->selected_entry)
7701 break;
7702 if (view_is_parent_view(view))
7703 view_get_split(view, &begin_y, &begin_x);
7704 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7705 s->selected_entry, s->repo);
7706 if (err || tree_view == NULL)
7707 break;
7708 if (view_is_parent_view(view) &&
7709 view->mode == TOG_VIEW_SPLIT_HRZN) {
7710 err = view_init_hsplit(view, begin_y);
7711 if (err)
7712 break;
7714 view->focussed = 0;
7715 tree_view->focussed = 1;
7716 tree_view->mode = view->mode;
7717 tree_view->nlines = view->lines - begin_y;
7718 if (view_is_parent_view(view)) {
7719 view_transfer_size(tree_view, view);
7720 err = view_close_child(view);
7721 if (err)
7722 return err;
7723 err = view_set_child(view, tree_view);
7724 if (err)
7725 return err;
7726 view->focus_child = 1;
7727 } else
7728 *new_view = tree_view;
7729 break;
7730 case 'g':
7731 case KEY_HOME:
7732 s->selected = 0;
7733 view->count = 0;
7734 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7735 break;
7736 case 'G':
7737 case KEY_END: {
7738 int eos = view->nlines - 1;
7740 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7741 --eos; /* border */
7742 s->selected = 0;
7743 view->count = 0;
7744 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7745 for (n = 0; n < eos; n++) {
7746 if (re == NULL)
7747 break;
7748 s->first_displayed_entry = re;
7749 re = TAILQ_PREV(re, tog_reflist_head, entry);
7751 if (n > 0)
7752 s->selected = n - 1;
7753 break;
7755 case 'k':
7756 case KEY_UP:
7757 case CTRL('p'):
7758 if (s->selected > 0) {
7759 s->selected--;
7760 break;
7762 ref_scroll_up(s, 1);
7763 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7764 view->count = 0;
7765 break;
7766 case CTRL('u'):
7767 case 'u':
7768 nscroll /= 2;
7769 /* FALL THROUGH */
7770 case KEY_PPAGE:
7771 case CTRL('b'):
7772 case 'b':
7773 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7774 s->selected -= MIN(nscroll, s->selected);
7775 ref_scroll_up(s, MAX(0, nscroll));
7776 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7777 view->count = 0;
7778 break;
7779 case 'j':
7780 case KEY_DOWN:
7781 case CTRL('n'):
7782 if (s->selected < s->ndisplayed - 1) {
7783 s->selected++;
7784 break;
7786 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7787 /* can't scroll any further */
7788 view->count = 0;
7789 break;
7791 ref_scroll_down(view, 1);
7792 break;
7793 case CTRL('d'):
7794 case 'd':
7795 nscroll /= 2;
7796 /* FALL THROUGH */
7797 case KEY_NPAGE:
7798 case CTRL('f'):
7799 case 'f':
7800 case ' ':
7801 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7802 /* can't scroll any further; move cursor down */
7803 if (s->selected < s->ndisplayed - 1)
7804 s->selected += MIN(nscroll,
7805 s->ndisplayed - s->selected - 1);
7806 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7807 s->selected += s->ndisplayed - s->selected - 1;
7808 view->count = 0;
7809 break;
7811 ref_scroll_down(view, nscroll);
7812 break;
7813 case CTRL('l'):
7814 view->count = 0;
7815 tog_free_refs();
7816 err = tog_load_refs(s->repo, s->sort_by_date);
7817 if (err)
7818 break;
7819 ref_view_free_refs(s);
7820 err = ref_view_load_refs(s);
7821 break;
7822 case KEY_RESIZE:
7823 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7824 s->selected = view->nlines - 2;
7825 break;
7826 default:
7827 view->count = 0;
7828 break;
7831 return err;
7834 __dead static void
7835 usage_ref(void)
7837 endwin();
7838 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7839 getprogname());
7840 exit(1);
7843 static const struct got_error *
7844 cmd_ref(int argc, char *argv[])
7846 const struct got_error *error;
7847 struct got_repository *repo = NULL;
7848 struct got_worktree *worktree = NULL;
7849 char *cwd = NULL, *repo_path = NULL;
7850 int ch;
7851 struct tog_view *view;
7852 int *pack_fds = NULL;
7854 while ((ch = getopt(argc, argv, "r:")) != -1) {
7855 switch (ch) {
7856 case 'r':
7857 repo_path = realpath(optarg, NULL);
7858 if (repo_path == NULL)
7859 return got_error_from_errno2("realpath",
7860 optarg);
7861 break;
7862 default:
7863 usage_ref();
7864 /* NOTREACHED */
7868 argc -= optind;
7869 argv += optind;
7871 if (argc > 1)
7872 usage_ref();
7874 error = got_repo_pack_fds_open(&pack_fds);
7875 if (error != NULL)
7876 goto done;
7878 if (repo_path == NULL) {
7879 cwd = getcwd(NULL, 0);
7880 if (cwd == NULL)
7881 return got_error_from_errno("getcwd");
7882 error = got_worktree_open(&worktree, cwd);
7883 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7884 goto done;
7885 if (worktree)
7886 repo_path =
7887 strdup(got_worktree_get_repo_path(worktree));
7888 else
7889 repo_path = strdup(cwd);
7890 if (repo_path == NULL) {
7891 error = got_error_from_errno("strdup");
7892 goto done;
7896 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7897 if (error != NULL)
7898 goto done;
7900 init_curses();
7902 error = apply_unveil(got_repo_get_path(repo), NULL);
7903 if (error)
7904 goto done;
7906 error = tog_load_refs(repo, 0);
7907 if (error)
7908 goto done;
7910 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7911 if (view == NULL) {
7912 error = got_error_from_errno("view_open");
7913 goto done;
7916 error = open_ref_view(view, repo);
7917 if (error)
7918 goto done;
7920 if (worktree) {
7921 /* Release work tree lock. */
7922 got_worktree_close(worktree);
7923 worktree = NULL;
7925 error = view_loop(view);
7926 done:
7927 free(repo_path);
7928 free(cwd);
7929 if (repo) {
7930 const struct got_error *close_err = got_repo_close(repo);
7931 if (close_err)
7932 error = close_err;
7934 if (pack_fds) {
7935 const struct got_error *pack_err =
7936 got_repo_pack_fds_close(pack_fds);
7937 if (error == NULL)
7938 error = pack_err;
7940 tog_free_refs();
7941 return error;
7945 * If view was scrolled down to move the selected line into view when opening a
7946 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7948 static void
7949 offset_selection_up(struct tog_view *view)
7951 switch (view->type) {
7952 case TOG_VIEW_BLAME: {
7953 struct tog_blame_view_state *s = &view->state.blame;
7954 if (s->first_displayed_line == 1) {
7955 s->selected_line = MAX(s->selected_line - view->offset,
7956 1);
7957 break;
7959 if (s->first_displayed_line > view->offset)
7960 s->first_displayed_line -= view->offset;
7961 else
7962 s->first_displayed_line = 1;
7963 s->selected_line += view->offset;
7964 break;
7966 case TOG_VIEW_LOG:
7967 log_scroll_up(&view->state.log, view->offset);
7968 view->state.log.selected += view->offset;
7969 break;
7970 case TOG_VIEW_REF:
7971 ref_scroll_up(&view->state.ref, view->offset);
7972 view->state.ref.selected += view->offset;
7973 break;
7974 case TOG_VIEW_TREE:
7975 tree_scroll_up(&view->state.tree, view->offset);
7976 view->state.tree.selected += view->offset;
7977 break;
7978 default:
7979 break;
7982 view->offset = 0;
7986 * If the selected line is in the section of screen covered by the bottom split,
7987 * scroll down offset lines to move it into view and index its new position.
7989 static const struct got_error *
7990 offset_selection_down(struct tog_view *view)
7992 const struct got_error *err = NULL;
7993 const struct got_error *(*scrolld)(struct tog_view *, int);
7994 int *selected = NULL;
7995 int header, offset;
7997 switch (view->type) {
7998 case TOG_VIEW_BLAME: {
7999 struct tog_blame_view_state *s = &view->state.blame;
8000 header = 3;
8001 scrolld = NULL;
8002 if (s->selected_line > view->nlines - header) {
8003 offset = abs(view->nlines - s->selected_line - header);
8004 s->first_displayed_line += offset;
8005 s->selected_line -= offset;
8006 view->offset = offset;
8008 break;
8010 case TOG_VIEW_LOG: {
8011 struct tog_log_view_state *s = &view->state.log;
8012 scrolld = &log_scroll_down;
8013 header = view_is_parent_view(view) ? 3 : 2;
8014 selected = &s->selected;
8015 break;
8017 case TOG_VIEW_REF: {
8018 struct tog_ref_view_state *s = &view->state.ref;
8019 scrolld = &ref_scroll_down;
8020 header = 3;
8021 selected = &s->selected;
8022 break;
8024 case TOG_VIEW_TREE: {
8025 struct tog_tree_view_state *s = &view->state.tree;
8026 scrolld = &tree_scroll_down;
8027 header = 5;
8028 selected = &s->selected;
8029 break;
8031 default:
8032 selected = NULL;
8033 scrolld = NULL;
8034 header = 0;
8035 break;
8038 if (selected && *selected > view->nlines - header) {
8039 offset = abs(view->nlines - *selected - header);
8040 view->offset = offset;
8041 if (scrolld && offset) {
8042 err = scrolld(view, offset);
8043 *selected -= offset;
8047 return err;
8050 static void
8051 list_commands(FILE *fp)
8053 size_t i;
8055 fprintf(fp, "commands:");
8056 for (i = 0; i < nitems(tog_commands); i++) {
8057 const struct tog_cmd *cmd = &tog_commands[i];
8058 fprintf(fp, " %s", cmd->name);
8060 fputc('\n', fp);
8063 __dead static void
8064 usage(int hflag, int status)
8066 FILE *fp = (status == 0) ? stdout : stderr;
8068 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8069 getprogname());
8070 if (hflag) {
8071 fprintf(fp, "lazy usage: %s path\n", getprogname());
8072 list_commands(fp);
8074 exit(status);
8077 static char **
8078 make_argv(int argc, ...)
8080 va_list ap;
8081 char **argv;
8082 int i;
8084 va_start(ap, argc);
8086 argv = calloc(argc, sizeof(char *));
8087 if (argv == NULL)
8088 err(1, "calloc");
8089 for (i = 0; i < argc; i++) {
8090 argv[i] = strdup(va_arg(ap, char *));
8091 if (argv[i] == NULL)
8092 err(1, "strdup");
8095 va_end(ap);
8096 return argv;
8100 * Try to convert 'tog path' into a 'tog log path' command.
8101 * The user could simply have mistyped the command rather than knowingly
8102 * provided a path. So check whether argv[0] can in fact be resolved
8103 * to a path in the HEAD commit and print a special error if not.
8104 * This hack is for mpi@ <3
8106 static const struct got_error *
8107 tog_log_with_path(int argc, char *argv[])
8109 const struct got_error *error = NULL, *close_err;
8110 const struct tog_cmd *cmd = NULL;
8111 struct got_repository *repo = NULL;
8112 struct got_worktree *worktree = NULL;
8113 struct got_object_id *commit_id = NULL, *id = NULL;
8114 struct got_commit_object *commit = NULL;
8115 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8116 char *commit_id_str = NULL, **cmd_argv = NULL;
8117 int *pack_fds = NULL;
8119 cwd = getcwd(NULL, 0);
8120 if (cwd == NULL)
8121 return got_error_from_errno("getcwd");
8123 error = got_repo_pack_fds_open(&pack_fds);
8124 if (error != NULL)
8125 goto done;
8127 error = got_worktree_open(&worktree, cwd);
8128 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8129 goto done;
8131 if (worktree)
8132 repo_path = strdup(got_worktree_get_repo_path(worktree));
8133 else
8134 repo_path = strdup(cwd);
8135 if (repo_path == NULL) {
8136 error = got_error_from_errno("strdup");
8137 goto done;
8140 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8141 if (error != NULL)
8142 goto done;
8144 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8145 repo, worktree);
8146 if (error)
8147 goto done;
8149 error = tog_load_refs(repo, 0);
8150 if (error)
8151 goto done;
8152 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8153 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8154 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8155 if (error)
8156 goto done;
8158 if (worktree) {
8159 got_worktree_close(worktree);
8160 worktree = NULL;
8163 error = got_object_open_as_commit(&commit, repo, commit_id);
8164 if (error)
8165 goto done;
8167 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8168 if (error) {
8169 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8170 goto done;
8171 fprintf(stderr, "%s: '%s' is no known command or path\n",
8172 getprogname(), argv[0]);
8173 usage(1, 1);
8174 /* not reached */
8177 close_err = got_repo_close(repo);
8178 if (error == NULL)
8179 error = close_err;
8180 repo = NULL;
8182 error = got_object_id_str(&commit_id_str, commit_id);
8183 if (error)
8184 goto done;
8186 cmd = &tog_commands[0]; /* log */
8187 argc = 4;
8188 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8189 error = cmd->cmd_main(argc, cmd_argv);
8190 done:
8191 if (repo) {
8192 close_err = got_repo_close(repo);
8193 if (error == NULL)
8194 error = close_err;
8196 if (commit)
8197 got_object_commit_close(commit);
8198 if (worktree)
8199 got_worktree_close(worktree);
8200 if (pack_fds) {
8201 const struct got_error *pack_err =
8202 got_repo_pack_fds_close(pack_fds);
8203 if (error == NULL)
8204 error = pack_err;
8206 free(id);
8207 free(commit_id_str);
8208 free(commit_id);
8209 free(cwd);
8210 free(repo_path);
8211 free(in_repo_path);
8212 if (cmd_argv) {
8213 int i;
8214 for (i = 0; i < argc; i++)
8215 free(cmd_argv[i]);
8216 free(cmd_argv);
8218 tog_free_refs();
8219 return error;
8222 int
8223 main(int argc, char *argv[])
8225 const struct got_error *error = NULL;
8226 const struct tog_cmd *cmd = NULL;
8227 int ch, hflag = 0, Vflag = 0;
8228 char **cmd_argv = NULL;
8229 static const struct option longopts[] = {
8230 { "version", no_argument, NULL, 'V' },
8231 { NULL, 0, NULL, 0}
8233 char *diff_algo_str = NULL;
8235 setlocale(LC_CTYPE, "");
8237 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8238 switch (ch) {
8239 case 'h':
8240 hflag = 1;
8241 break;
8242 case 'V':
8243 Vflag = 1;
8244 break;
8245 default:
8246 usage(hflag, 1);
8247 /* NOTREACHED */
8251 argc -= optind;
8252 argv += optind;
8253 optind = 1;
8254 optreset = 1;
8256 if (Vflag) {
8257 got_version_print_str();
8258 return 0;
8261 #ifndef PROFILE
8262 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8263 NULL) == -1)
8264 err(1, "pledge");
8265 #endif
8267 if (argc == 0) {
8268 if (hflag)
8269 usage(hflag, 0);
8270 /* Build an argument vector which runs a default command. */
8271 cmd = &tog_commands[0];
8272 argc = 1;
8273 cmd_argv = make_argv(argc, cmd->name);
8274 } else {
8275 size_t i;
8277 /* Did the user specify a command? */
8278 for (i = 0; i < nitems(tog_commands); i++) {
8279 if (strncmp(tog_commands[i].name, argv[0],
8280 strlen(argv[0])) == 0) {
8281 cmd = &tog_commands[i];
8282 break;
8287 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8288 if (diff_algo_str) {
8289 if (strcasecmp(diff_algo_str, "patience") == 0)
8290 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8291 if (strcasecmp(diff_algo_str, "myers") == 0)
8292 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8295 if (cmd == NULL) {
8296 if (argc != 1)
8297 usage(0, 1);
8298 /* No command specified; try log with a path */
8299 error = tog_log_with_path(argc, argv);
8300 } else {
8301 if (hflag)
8302 cmd->cmd_usage();
8303 else
8304 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8307 endwin();
8308 putchar('\n');
8309 if (cmd_argv) {
8310 int i;
8311 for (i = 0; i < argc; i++)
8312 free(cmd_argv[i]);
8313 free(cmd_argv);
8316 if (error && error->code != GOT_ERR_CANCELLED)
8317 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8318 return 0;