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 int y = v->child->begin_y;
970 if (v->child->resized_y)
971 v->child->begin_y = v->child->resized_y;
972 if (view->parent)
973 v->child->begin_y -= resize;
974 else
975 v->child->begin_y += resize;
976 if (v->child->begin_y < 3) {
977 view->count = 0;
978 v->child->begin_y = 3;
979 } else if (v->child->begin_y > LINES - 1) {
980 view->count = 0;
981 v->child->begin_y = LINES - 1;
983 v->ncols = COLS;
984 v->child->ncols = COLS;
985 view_adjust_offset(view, resize);
986 err = view_init_hsplit(v, v->child->begin_y);
987 if (err)
988 return err;
989 v->child->resized_y = v->child->begin_y;
990 if (y > v->child->begin_y) /* split increased */
991 v->child->nscrolled = y - v->child->begin_y;
992 } else {
993 if (v->child->resized_x)
994 v->child->begin_x = v->child->resized_x;
995 if (view->parent)
996 v->child->begin_x -= resize;
997 else
998 v->child->begin_x += resize;
999 if (v->child->begin_x < 11) {
1000 view->count = 0;
1001 v->child->begin_x = 11;
1002 } else if (v->child->begin_x > COLS - 1) {
1003 view->count = 0;
1004 v->child->begin_x = COLS - 1;
1006 v->child->resized_x = v->child->begin_x;
1009 v->child->mode = v->mode;
1010 v->child->nlines = v->lines - v->child->begin_y;
1011 v->child->ncols = v->cols - v->child->begin_x;
1012 v->focus_child = 1;
1014 err = view_fullscreen(v);
1015 if (err)
1016 return err;
1017 err = view_splitscreen(v->child);
1018 if (err)
1019 return err;
1021 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1022 err = offset_selection_down(v->child);
1023 if (err)
1024 return err;
1027 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1028 err = request_log_commits(v);
1029 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1030 err = request_log_commits(v->child);
1032 v->resize = v->child->resize = 0;
1034 return err;
1037 static void
1038 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1040 struct tog_view *v = src->child ? src->child : src;
1042 dst->resized_x = v->resized_x;
1043 dst->resized_y = v->resized_y;
1046 static const struct got_error *
1047 view_close_child(struct tog_view *view)
1049 const struct got_error *err = NULL;
1051 if (view->child == NULL)
1052 return NULL;
1054 err = view_close(view->child);
1055 view->child = NULL;
1056 return err;
1059 static const struct got_error *
1060 view_set_child(struct tog_view *view, struct tog_view *child)
1062 const struct got_error *err = NULL;
1064 view->child = child;
1065 child->parent = view;
1067 err = view_resize(view);
1068 if (err)
1069 return err;
1071 if (view->child->resized_x || view->child->resized_y)
1072 err = view_resize_split(view, 0);
1074 return err;
1077 static void
1078 tog_resizeterm(void)
1080 int cols, lines;
1081 struct winsize size;
1083 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1084 cols = 80; /* Default */
1085 lines = 24;
1086 } else {
1087 cols = size.ws_col;
1088 lines = size.ws_row;
1090 resize_term(lines, cols);
1093 static const struct got_error *
1094 view_search_start(struct tog_view *view)
1096 const struct got_error *err = NULL;
1097 struct tog_view *v = view;
1098 char pattern[1024];
1099 int ret;
1101 if (view->search_started) {
1102 regfree(&view->regex);
1103 view->searching = 0;
1104 memset(&view->regmatch, 0, sizeof(view->regmatch));
1106 view->search_started = 0;
1108 if (view->nlines < 1)
1109 return NULL;
1111 if (view_is_hsplit_top(view))
1112 v = view->child;
1114 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1115 wclrtoeol(v->window);
1117 nodelay(view->window, FALSE); /* block for search term input */
1118 nocbreak();
1119 echo();
1120 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1121 wrefresh(v->window);
1122 cbreak();
1123 noecho();
1124 nodelay(view->window, TRUE);
1125 if (ret == ERR)
1126 return NULL;
1128 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1129 err = view->search_start(view);
1130 if (err) {
1131 regfree(&view->regex);
1132 return err;
1134 view->search_started = 1;
1135 view->searching = TOG_SEARCH_FORWARD;
1136 view->search_next_done = 0;
1137 view->search_next(view);
1140 return NULL;
1143 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1144 static const struct got_error *
1145 switch_split(struct tog_view *view)
1147 const struct got_error *err = NULL;
1148 struct tog_view *v = NULL;
1150 if (view->parent)
1151 v = view->parent;
1152 else
1153 v = view;
1155 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1156 v->mode = TOG_VIEW_SPLIT_VERT;
1157 else
1158 v->mode = TOG_VIEW_SPLIT_HRZN;
1160 if (!v->child)
1161 return NULL;
1162 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1163 v->mode = TOG_VIEW_SPLIT_NONE;
1165 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1166 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1167 v->child->begin_y = v->child->resized_y;
1168 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1169 v->child->begin_x = v->child->resized_x;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1173 v->ncols = COLS;
1174 v->child->ncols = COLS;
1175 v->child->nscrolled = LINES - v->child->nlines;
1177 err = view_init_hsplit(v, v->child->begin_y);
1178 if (err)
1179 return err;
1181 v->child->mode = v->mode;
1182 v->child->nlines = v->lines - v->child->begin_y;
1183 v->focus_child = 1;
1185 err = view_fullscreen(v);
1186 if (err)
1187 return err;
1188 err = view_splitscreen(v->child);
1189 if (err)
1190 return err;
1192 if (v->mode == TOG_VIEW_SPLIT_NONE)
1193 v->mode = TOG_VIEW_SPLIT_VERT;
1194 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1195 err = offset_selection_down(v);
1196 err = offset_selection_down(v->child);
1197 } else {
1198 offset_selection_up(v);
1199 offset_selection_up(v->child);
1201 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1202 err = request_log_commits(v);
1203 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1204 err = request_log_commits(v->child);
1206 return err;
1210 * Compute view->count from numeric input. Assign total to view->count and
1211 * return first non-numeric key entered.
1213 static int
1214 get_compound_key(struct tog_view *view, int c)
1216 struct tog_view *v = view;
1217 int x, n = 0;
1219 if (view_is_hsplit_top(view))
1220 v = view->child;
1221 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1222 v = view->parent;
1224 view->count = 0;
1225 cbreak(); /* block for input */
1226 wmove(v->window, v->nlines - 1, 0);
1227 wclrtoeol(v->window);
1228 waddch(v->window, ':');
1230 do {
1231 x = getcurx(v->window);
1232 if (x != ERR && x < view->ncols) {
1233 waddch(v->window, c);
1234 wrefresh(v->window);
1238 * Don't overflow. Max valid request should be the greatest
1239 * between the longest and total lines; cap at 10 million.
1241 if (n >= 9999999)
1242 n = 9999999;
1243 else
1244 n = n * 10 + (c - '0');
1245 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1247 /* Massage excessive or inapplicable values at the input handler. */
1248 view->count = n;
1250 return c;
1253 static const struct got_error *
1254 view_input(struct tog_view **new, int *done, struct tog_view *view,
1255 struct tog_view_list_head *views)
1257 const struct got_error *err = NULL;
1258 struct tog_view *v;
1259 int ch, errcode;
1261 *new = NULL;
1263 /* Clear "no matches" indicator. */
1264 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1265 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1266 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1267 view->count = 0;
1270 if (view->searching && !view->search_next_done) {
1271 errcode = pthread_mutex_unlock(&tog_mutex);
1272 if (errcode)
1273 return got_error_set_errno(errcode,
1274 "pthread_mutex_unlock");
1275 sched_yield();
1276 errcode = pthread_mutex_lock(&tog_mutex);
1277 if (errcode)
1278 return got_error_set_errno(errcode,
1279 "pthread_mutex_lock");
1280 view->search_next(view);
1281 return NULL;
1284 nodelay(view->window, FALSE);
1285 /* Allow threads to make progress while we are waiting for input. */
1286 errcode = pthread_mutex_unlock(&tog_mutex);
1287 if (errcode)
1288 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1289 /* If we have an unfinished count, let C-g or backspace abort. */
1290 if (view->count && --view->count) {
1291 cbreak();
1292 nodelay(view->window, TRUE);
1293 ch = wgetch(view->window);
1294 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1295 view->count = 0;
1296 else
1297 ch = view->ch;
1298 } else {
1299 ch = wgetch(view->window);
1300 if (ch >= '1' && ch <= '9')
1301 view->ch = ch = get_compound_key(view, ch);
1303 errcode = pthread_mutex_lock(&tog_mutex);
1304 if (errcode)
1305 return got_error_set_errno(errcode, "pthread_mutex_lock");
1306 nodelay(view->window, TRUE);
1308 if (tog_sigwinch_received || tog_sigcont_received) {
1309 tog_resizeterm();
1310 tog_sigwinch_received = 0;
1311 tog_sigcont_received = 0;
1312 TAILQ_FOREACH(v, views, entry) {
1313 err = view_resize(v);
1314 if (err)
1315 return err;
1316 err = v->input(new, v, KEY_RESIZE);
1317 if (err)
1318 return err;
1319 if (v->child) {
1320 err = view_resize(v->child);
1321 if (err)
1322 return err;
1323 err = v->child->input(new, v->child,
1324 KEY_RESIZE);
1325 if (err)
1326 return err;
1327 if (v->child->resized_x || v->child->resized_y) {
1328 err = view_resize_split(v, 0);
1329 if (err)
1330 return err;
1336 switch (ch) {
1337 case '\t':
1338 view->count = 0;
1339 if (view->child) {
1340 view->focussed = 0;
1341 view->child->focussed = 1;
1342 view->focus_child = 1;
1343 } else if (view->parent) {
1344 view->focussed = 0;
1345 view->parent->focussed = 1;
1346 view->parent->focus_child = 0;
1347 if (!view_is_splitscreen(view)) {
1348 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1349 view->parent->type == TOG_VIEW_LOG) {
1350 err = request_log_commits(view->parent);
1351 if (err)
1352 return err;
1354 offset_selection_up(view->parent);
1355 err = view_fullscreen(view->parent);
1356 if (err)
1357 return err;
1360 break;
1361 case 'q':
1362 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1363 if (view->parent->type == TOG_VIEW_LOG) {
1364 /* might need more commits to fill fullscreen */
1365 err = request_log_commits(view->parent);
1366 if (err)
1367 break;
1369 offset_selection_up(view->parent);
1371 err = view->input(new, view, ch);
1372 view->dying = 1;
1373 break;
1374 case 'Q':
1375 *done = 1;
1376 break;
1377 case 'F':
1378 view->count = 0;
1379 if (view_is_parent_view(view)) {
1380 if (view->child == NULL)
1381 break;
1382 if (view_is_splitscreen(view->child)) {
1383 view->focussed = 0;
1384 view->child->focussed = 1;
1385 err = view_fullscreen(view->child);
1386 } else {
1387 err = view_splitscreen(view->child);
1388 if (!err)
1389 err = view_resize_split(view, 0);
1391 if (err)
1392 break;
1393 err = view->child->input(new, view->child,
1394 KEY_RESIZE);
1395 } else {
1396 if (view_is_splitscreen(view)) {
1397 view->parent->focussed = 0;
1398 view->focussed = 1;
1399 err = view_fullscreen(view);
1400 } else {
1401 err = view_splitscreen(view);
1402 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1403 err = view_resize(view->parent);
1404 if (!err)
1405 err = view_resize_split(view, 0);
1407 if (err)
1408 break;
1409 err = view->input(new, view, KEY_RESIZE);
1411 if (err)
1412 break;
1413 if (view->type == TOG_VIEW_LOG) {
1414 err = request_log_commits(view);
1415 if (err)
1416 break;
1418 if (view->parent)
1419 err = offset_selection_down(view->parent);
1420 if (!err)
1421 err = offset_selection_down(view);
1422 break;
1423 case 'S':
1424 view->count = 0;
1425 err = switch_split(view);
1426 break;
1427 case '-':
1428 err = view_resize_split(view, -1);
1429 break;
1430 case '+':
1431 err = view_resize_split(view, 1);
1432 break;
1433 case KEY_RESIZE:
1434 break;
1435 case '/':
1436 view->count = 0;
1437 if (view->search_start)
1438 view_search_start(view);
1439 else
1440 err = view->input(new, view, ch);
1441 break;
1442 case 'N':
1443 case 'n':
1444 if (view->search_started && view->search_next) {
1445 view->searching = (ch == 'n' ?
1446 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1447 view->search_next_done = 0;
1448 view->search_next(view);
1449 } else
1450 err = view->input(new, view, ch);
1451 break;
1452 case 'A':
1453 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1454 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1455 else
1456 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1457 TAILQ_FOREACH(v, views, entry) {
1458 if (v->reset) {
1459 err = v->reset(v);
1460 if (err)
1461 return err;
1463 if (v->child && v->child->reset) {
1464 err = v->child->reset(v->child);
1465 if (err)
1466 return err;
1469 break;
1470 default:
1471 err = view->input(new, view, ch);
1472 break;
1475 return err;
1478 static int
1479 view_needs_focus_indication(struct tog_view *view)
1481 if (view_is_parent_view(view)) {
1482 if (view->child == NULL || view->child->focussed)
1483 return 0;
1484 if (!view_is_splitscreen(view->child))
1485 return 0;
1486 } else if (!view_is_splitscreen(view))
1487 return 0;
1489 return view->focussed;
1492 static const struct got_error *
1493 view_loop(struct tog_view *view)
1495 const struct got_error *err = NULL;
1496 struct tog_view_list_head views;
1497 struct tog_view *new_view;
1498 char *mode;
1499 int fast_refresh = 10;
1500 int done = 0, errcode;
1502 mode = getenv("TOG_VIEW_SPLIT_MODE");
1503 if (!mode || !(*mode == 'h' || *mode == 'H'))
1504 view->mode = TOG_VIEW_SPLIT_VERT;
1505 else
1506 view->mode = TOG_VIEW_SPLIT_HRZN;
1508 errcode = pthread_mutex_lock(&tog_mutex);
1509 if (errcode)
1510 return got_error_set_errno(errcode, "pthread_mutex_lock");
1512 TAILQ_INIT(&views);
1513 TAILQ_INSERT_HEAD(&views, view, entry);
1515 view->focussed = 1;
1516 err = view->show(view);
1517 if (err)
1518 return err;
1519 update_panels();
1520 doupdate();
1521 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1522 !tog_fatal_signal_received()) {
1523 /* Refresh fast during initialization, then become slower. */
1524 if (fast_refresh && fast_refresh-- == 0)
1525 halfdelay(10); /* switch to once per second */
1527 err = view_input(&new_view, &done, view, &views);
1528 if (err)
1529 break;
1530 if (view->dying) {
1531 struct tog_view *v, *prev = NULL;
1533 if (view_is_parent_view(view))
1534 prev = TAILQ_PREV(view, tog_view_list_head,
1535 entry);
1536 else if (view->parent)
1537 prev = view->parent;
1539 if (view->parent) {
1540 view->parent->child = NULL;
1541 view->parent->focus_child = 0;
1542 /* Restore fullscreen line height. */
1543 view->parent->nlines = view->parent->lines;
1544 err = view_resize(view->parent);
1545 if (err)
1546 break;
1547 /* Make resized splits persist. */
1548 view_transfer_size(view->parent, view);
1549 } else
1550 TAILQ_REMOVE(&views, view, entry);
1552 err = view_close(view);
1553 if (err)
1554 goto done;
1556 view = NULL;
1557 TAILQ_FOREACH(v, &views, entry) {
1558 if (v->focussed)
1559 break;
1561 if (view == NULL && new_view == NULL) {
1562 /* No view has focus. Try to pick one. */
1563 if (prev)
1564 view = prev;
1565 else if (!TAILQ_EMPTY(&views)) {
1566 view = TAILQ_LAST(&views,
1567 tog_view_list_head);
1569 if (view) {
1570 if (view->focus_child) {
1571 view->child->focussed = 1;
1572 view = view->child;
1573 } else
1574 view->focussed = 1;
1578 if (new_view) {
1579 struct tog_view *v, *t;
1580 /* Only allow one parent view per type. */
1581 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1582 if (v->type != new_view->type)
1583 continue;
1584 TAILQ_REMOVE(&views, v, entry);
1585 err = view_close(v);
1586 if (err)
1587 goto done;
1588 break;
1590 TAILQ_INSERT_TAIL(&views, new_view, entry);
1591 view = new_view;
1593 if (view) {
1594 if (view_is_parent_view(view)) {
1595 if (view->child && view->child->focussed)
1596 view = view->child;
1597 } else {
1598 if (view->parent && view->parent->focussed)
1599 view = view->parent;
1601 show_panel(view->panel);
1602 if (view->child && view_is_splitscreen(view->child))
1603 show_panel(view->child->panel);
1604 if (view->parent && view_is_splitscreen(view)) {
1605 err = view->parent->show(view->parent);
1606 if (err)
1607 goto done;
1609 err = view->show(view);
1610 if (err)
1611 goto done;
1612 if (view->child) {
1613 err = view->child->show(view->child);
1614 if (err)
1615 goto done;
1617 update_panels();
1618 doupdate();
1621 done:
1622 while (!TAILQ_EMPTY(&views)) {
1623 const struct got_error *close_err;
1624 view = TAILQ_FIRST(&views);
1625 TAILQ_REMOVE(&views, view, entry);
1626 close_err = view_close(view);
1627 if (close_err && err == NULL)
1628 err = close_err;
1631 errcode = pthread_mutex_unlock(&tog_mutex);
1632 if (errcode && err == NULL)
1633 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1635 return err;
1638 __dead static void
1639 usage_log(void)
1641 endwin();
1642 fprintf(stderr,
1643 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1644 getprogname());
1645 exit(1);
1648 /* Create newly allocated wide-character string equivalent to a byte string. */
1649 static const struct got_error *
1650 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1652 char *vis = NULL;
1653 const struct got_error *err = NULL;
1655 *ws = NULL;
1656 *wlen = mbstowcs(NULL, s, 0);
1657 if (*wlen == (size_t)-1) {
1658 int vislen;
1659 if (errno != EILSEQ)
1660 return got_error_from_errno("mbstowcs");
1662 /* byte string invalid in current encoding; try to "fix" it */
1663 err = got_mbsavis(&vis, &vislen, s);
1664 if (err)
1665 return err;
1666 *wlen = mbstowcs(NULL, vis, 0);
1667 if (*wlen == (size_t)-1) {
1668 err = got_error_from_errno("mbstowcs"); /* give up */
1669 goto done;
1673 *ws = calloc(*wlen + 1, sizeof(**ws));
1674 if (*ws == NULL) {
1675 err = got_error_from_errno("calloc");
1676 goto done;
1679 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1680 err = got_error_from_errno("mbstowcs");
1681 done:
1682 free(vis);
1683 if (err) {
1684 free(*ws);
1685 *ws = NULL;
1686 *wlen = 0;
1688 return err;
1691 static const struct got_error *
1692 expand_tab(char **ptr, const char *src)
1694 char *dst;
1695 size_t len, n, idx = 0, sz = 0;
1697 *ptr = NULL;
1698 n = len = strlen(src);
1699 dst = malloc(n + 1);
1700 if (dst == NULL)
1701 return got_error_from_errno("malloc");
1703 while (idx < len && src[idx]) {
1704 const char c = src[idx];
1706 if (c == '\t') {
1707 size_t nb = TABSIZE - sz % TABSIZE;
1708 char *p;
1710 p = realloc(dst, n + nb);
1711 if (p == NULL) {
1712 free(dst);
1713 return got_error_from_errno("realloc");
1716 dst = p;
1717 n += nb;
1718 memset(dst + sz, ' ', nb);
1719 sz += nb;
1720 } else
1721 dst[sz++] = src[idx];
1722 ++idx;
1725 dst[sz] = '\0';
1726 *ptr = dst;
1727 return NULL;
1731 * Advance at most n columns from wline starting at offset off.
1732 * Return the index to the first character after the span operation.
1733 * Return the combined column width of all spanned wide character in
1734 * *rcol.
1736 static int
1737 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1739 int width, i, cols = 0;
1741 if (n == 0) {
1742 *rcol = cols;
1743 return off;
1746 for (i = off; wline[i] != L'\0'; ++i) {
1747 if (wline[i] == L'\t')
1748 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1749 else
1750 width = wcwidth(wline[i]);
1752 if (width == -1) {
1753 width = 1;
1754 wline[i] = L'.';
1757 if (cols + width > n)
1758 break;
1759 cols += width;
1762 *rcol = cols;
1763 return i;
1767 * Format a line for display, ensuring that it won't overflow a width limit.
1768 * With scrolling, the width returned refers to the scrolled version of the
1769 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1771 static const struct got_error *
1772 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1773 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1775 const struct got_error *err = NULL;
1776 int cols;
1777 wchar_t *wline = NULL;
1778 char *exstr = NULL;
1779 size_t wlen;
1780 int i, scrollx;
1782 *wlinep = NULL;
1783 *widthp = 0;
1785 if (expand) {
1786 err = expand_tab(&exstr, line);
1787 if (err)
1788 return err;
1791 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1792 free(exstr);
1793 if (err)
1794 return err;
1796 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1798 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1799 wline[wlen - 1] = L'\0';
1800 wlen--;
1802 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1803 wline[wlen - 1] = L'\0';
1804 wlen--;
1807 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1808 wline[i] = L'\0';
1810 if (widthp)
1811 *widthp = cols;
1812 if (scrollxp)
1813 *scrollxp = scrollx;
1814 if (err)
1815 free(wline);
1816 else
1817 *wlinep = wline;
1818 return err;
1821 static const struct got_error*
1822 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1823 struct got_object_id *id, struct got_repository *repo)
1825 static const struct got_error *err = NULL;
1826 struct got_reflist_entry *re;
1827 char *s;
1828 const char *name;
1830 *refs_str = NULL;
1832 TAILQ_FOREACH(re, refs, entry) {
1833 struct got_tag_object *tag = NULL;
1834 struct got_object_id *ref_id;
1835 int cmp;
1837 name = got_ref_get_name(re->ref);
1838 if (strcmp(name, GOT_REF_HEAD) == 0)
1839 continue;
1840 if (strncmp(name, "refs/", 5) == 0)
1841 name += 5;
1842 if (strncmp(name, "got/", 4) == 0 &&
1843 strncmp(name, "got/backup/", 11) != 0)
1844 continue;
1845 if (strncmp(name, "heads/", 6) == 0)
1846 name += 6;
1847 if (strncmp(name, "remotes/", 8) == 0) {
1848 name += 8;
1849 s = strstr(name, "/" GOT_REF_HEAD);
1850 if (s != NULL && s[strlen(s)] == '\0')
1851 continue;
1853 err = got_ref_resolve(&ref_id, repo, re->ref);
1854 if (err)
1855 break;
1856 if (strncmp(name, "tags/", 5) == 0) {
1857 err = got_object_open_as_tag(&tag, repo, ref_id);
1858 if (err) {
1859 if (err->code != GOT_ERR_OBJ_TYPE) {
1860 free(ref_id);
1861 break;
1863 /* Ref points at something other than a tag. */
1864 err = NULL;
1865 tag = NULL;
1868 cmp = got_object_id_cmp(tag ?
1869 got_object_tag_get_object_id(tag) : ref_id, id);
1870 free(ref_id);
1871 if (tag)
1872 got_object_tag_close(tag);
1873 if (cmp != 0)
1874 continue;
1875 s = *refs_str;
1876 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1877 s ? ", " : "", name) == -1) {
1878 err = got_error_from_errno("asprintf");
1879 free(s);
1880 *refs_str = NULL;
1881 break;
1883 free(s);
1886 return err;
1889 static const struct got_error *
1890 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1891 int col_tab_align)
1893 char *smallerthan;
1895 smallerthan = strchr(author, '<');
1896 if (smallerthan && smallerthan[1] != '\0')
1897 author = smallerthan + 1;
1898 author[strcspn(author, "@>")] = '\0';
1899 return format_line(wauthor, author_width, NULL, author, 0, limit,
1900 col_tab_align, 0);
1903 static const struct got_error *
1904 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1905 struct got_object_id *id, const size_t date_display_cols,
1906 int author_display_cols)
1908 struct tog_log_view_state *s = &view->state.log;
1909 const struct got_error *err = NULL;
1910 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1911 char *logmsg0 = NULL, *logmsg = NULL;
1912 char *author = NULL;
1913 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1914 int author_width, logmsg_width;
1915 char *newline, *line = NULL;
1916 int col, limit, scrollx;
1917 const int avail = view->ncols;
1918 struct tm tm;
1919 time_t committer_time;
1920 struct tog_color *tc;
1922 committer_time = got_object_commit_get_committer_time(commit);
1923 if (gmtime_r(&committer_time, &tm) == NULL)
1924 return got_error_from_errno("gmtime_r");
1925 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1926 return got_error(GOT_ERR_NO_SPACE);
1928 if (avail <= date_display_cols)
1929 limit = MIN(sizeof(datebuf) - 1, avail);
1930 else
1931 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1932 tc = get_color(&s->colors, TOG_COLOR_DATE);
1933 if (tc)
1934 wattr_on(view->window,
1935 COLOR_PAIR(tc->colorpair), NULL);
1936 waddnstr(view->window, datebuf, limit);
1937 if (tc)
1938 wattr_off(view->window,
1939 COLOR_PAIR(tc->colorpair), NULL);
1940 col = limit;
1941 if (col > avail)
1942 goto done;
1944 if (avail >= 120) {
1945 char *id_str;
1946 err = got_object_id_str(&id_str, id);
1947 if (err)
1948 goto done;
1949 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1950 if (tc)
1951 wattr_on(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 wprintw(view->window, "%.8s ", id_str);
1954 if (tc)
1955 wattr_off(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1957 free(id_str);
1958 col += 9;
1959 if (col > avail)
1960 goto done;
1963 author = strdup(got_object_commit_get_author(commit));
1964 if (author == NULL) {
1965 err = got_error_from_errno("strdup");
1966 goto done;
1968 err = format_author(&wauthor, &author_width, author, avail - col, col);
1969 if (err)
1970 goto done;
1971 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1972 if (tc)
1973 wattr_on(view->window,
1974 COLOR_PAIR(tc->colorpair), NULL);
1975 waddwstr(view->window, wauthor);
1976 if (tc)
1977 wattr_off(view->window,
1978 COLOR_PAIR(tc->colorpair), NULL);
1979 col += author_width;
1980 while (col < avail && author_width < author_display_cols + 2) {
1981 waddch(view->window, ' ');
1982 col++;
1983 author_width++;
1985 if (col > avail)
1986 goto done;
1988 err = got_object_commit_get_logmsg(&logmsg0, commit);
1989 if (err)
1990 goto done;
1991 logmsg = logmsg0;
1992 while (*logmsg == '\n')
1993 logmsg++;
1994 newline = strchr(logmsg, '\n');
1995 if (newline)
1996 *newline = '\0';
1997 limit = avail - col;
1998 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1999 limit--; /* for the border */
2000 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2001 limit, col, 1);
2002 if (err)
2003 goto done;
2004 waddwstr(view->window, &wlogmsg[scrollx]);
2005 col += MAX(logmsg_width, 0);
2006 while (col < avail) {
2007 waddch(view->window, ' ');
2008 col++;
2010 done:
2011 free(logmsg0);
2012 free(wlogmsg);
2013 free(author);
2014 free(wauthor);
2015 free(line);
2016 return err;
2019 static struct commit_queue_entry *
2020 alloc_commit_queue_entry(struct got_commit_object *commit,
2021 struct got_object_id *id)
2023 struct commit_queue_entry *entry;
2025 entry = calloc(1, sizeof(*entry));
2026 if (entry == NULL)
2027 return NULL;
2029 entry->id = id;
2030 entry->commit = commit;
2031 return entry;
2034 static void
2035 pop_commit(struct commit_queue *commits)
2037 struct commit_queue_entry *entry;
2039 entry = TAILQ_FIRST(&commits->head);
2040 TAILQ_REMOVE(&commits->head, entry, entry);
2041 got_object_commit_close(entry->commit);
2042 commits->ncommits--;
2043 /* Don't free entry->id! It is owned by the commit graph. */
2044 free(entry);
2047 static void
2048 free_commits(struct commit_queue *commits)
2050 while (!TAILQ_EMPTY(&commits->head))
2051 pop_commit(commits);
2054 static const struct got_error *
2055 match_commit(int *have_match, struct got_object_id *id,
2056 struct got_commit_object *commit, regex_t *regex)
2058 const struct got_error *err = NULL;
2059 regmatch_t regmatch;
2060 char *id_str = NULL, *logmsg = NULL;
2062 *have_match = 0;
2064 err = got_object_id_str(&id_str, id);
2065 if (err)
2066 return err;
2068 err = got_object_commit_get_logmsg(&logmsg, commit);
2069 if (err)
2070 goto done;
2072 if (regexec(regex, got_object_commit_get_author(commit), 1,
2073 &regmatch, 0) == 0 ||
2074 regexec(regex, got_object_commit_get_committer(commit), 1,
2075 &regmatch, 0) == 0 ||
2076 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2077 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2078 *have_match = 1;
2079 done:
2080 free(id_str);
2081 free(logmsg);
2082 return err;
2085 static const struct got_error *
2086 queue_commits(struct tog_log_thread_args *a)
2088 const struct got_error *err = NULL;
2091 * We keep all commits open throughout the lifetime of the log
2092 * view in order to avoid having to re-fetch commits from disk
2093 * while updating the display.
2095 do {
2096 struct got_object_id *id;
2097 struct got_commit_object *commit;
2098 struct commit_queue_entry *entry;
2099 int errcode;
2101 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2102 NULL, NULL);
2103 if (err || id == NULL)
2104 break;
2106 err = got_object_open_as_commit(&commit, a->repo, id);
2107 if (err)
2108 break;
2109 entry = alloc_commit_queue_entry(commit, id);
2110 if (entry == NULL) {
2111 err = got_error_from_errno("alloc_commit_queue_entry");
2112 break;
2115 errcode = pthread_mutex_lock(&tog_mutex);
2116 if (errcode) {
2117 err = got_error_set_errno(errcode,
2118 "pthread_mutex_lock");
2119 break;
2122 entry->idx = a->commits->ncommits;
2123 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2124 a->commits->ncommits++;
2126 if (*a->searching == TOG_SEARCH_FORWARD &&
2127 !*a->search_next_done) {
2128 int have_match;
2129 err = match_commit(&have_match, id, commit, a->regex);
2130 if (err)
2131 break;
2132 if (have_match)
2133 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2136 errcode = pthread_mutex_unlock(&tog_mutex);
2137 if (errcode && err == NULL)
2138 err = got_error_set_errno(errcode,
2139 "pthread_mutex_unlock");
2140 if (err)
2141 break;
2142 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2144 return err;
2147 static void
2148 select_commit(struct tog_log_view_state *s)
2150 struct commit_queue_entry *entry;
2151 int ncommits = 0;
2153 entry = s->first_displayed_entry;
2154 while (entry) {
2155 if (ncommits == s->selected) {
2156 s->selected_entry = entry;
2157 break;
2159 entry = TAILQ_NEXT(entry, entry);
2160 ncommits++;
2164 static const struct got_error *
2165 draw_commits(struct tog_view *view)
2167 const struct got_error *err = NULL;
2168 struct tog_log_view_state *s = &view->state.log;
2169 struct commit_queue_entry *entry = s->selected_entry;
2170 const int limit = view->nlines;
2171 int width;
2172 int ncommits, author_cols = 4;
2173 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2174 char *refs_str = NULL;
2175 wchar_t *wline;
2176 struct tog_color *tc;
2177 static const size_t date_display_cols = 12;
2179 if (s->selected_entry &&
2180 !(view->searching && view->search_next_done == 0)) {
2181 struct got_reflist_head *refs;
2182 err = got_object_id_str(&id_str, s->selected_entry->id);
2183 if (err)
2184 return err;
2185 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2186 s->selected_entry->id);
2187 if (refs) {
2188 err = build_refs_str(&refs_str, refs,
2189 s->selected_entry->id, s->repo);
2190 if (err)
2191 goto done;
2195 if (s->thread_args.commits_needed == 0)
2196 halfdelay(10); /* disable fast refresh */
2198 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2199 if (asprintf(&ncommits_str, " [%d/%d] %s",
2200 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2201 (view->searching && !view->search_next_done) ?
2202 "searching..." : "loading...") == -1) {
2203 err = got_error_from_errno("asprintf");
2204 goto done;
2206 } else {
2207 const char *search_str = NULL;
2209 if (view->searching) {
2210 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2211 search_str = "no more matches";
2212 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2213 search_str = "no matches found";
2214 else if (!view->search_next_done)
2215 search_str = "searching...";
2218 if (asprintf(&ncommits_str, " [%d/%d] %s",
2219 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2220 search_str ? search_str :
2221 (refs_str ? refs_str : "")) == -1) {
2222 err = got_error_from_errno("asprintf");
2223 goto done;
2227 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2228 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2229 "........................................",
2230 s->in_repo_path, ncommits_str) == -1) {
2231 err = got_error_from_errno("asprintf");
2232 header = NULL;
2233 goto done;
2235 } else if (asprintf(&header, "commit %s%s",
2236 id_str ? id_str : "........................................",
2237 ncommits_str) == -1) {
2238 err = got_error_from_errno("asprintf");
2239 header = NULL;
2240 goto done;
2242 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2243 if (err)
2244 goto done;
2246 werase(view->window);
2248 if (view_needs_focus_indication(view))
2249 wstandout(view->window);
2250 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2251 if (tc)
2252 wattr_on(view->window,
2253 COLOR_PAIR(tc->colorpair), NULL);
2254 waddwstr(view->window, wline);
2255 if (tc)
2256 wattr_off(view->window,
2257 COLOR_PAIR(tc->colorpair), NULL);
2258 while (width < view->ncols) {
2259 waddch(view->window, ' ');
2260 width++;
2262 if (view_needs_focus_indication(view))
2263 wstandend(view->window);
2264 free(wline);
2265 if (limit <= 1)
2266 goto done;
2268 /* Grow author column size if necessary, and set view->maxx. */
2269 entry = s->first_displayed_entry;
2270 ncommits = 0;
2271 view->maxx = 0;
2272 while (entry) {
2273 char *author, *eol, *msg, *msg0;
2274 wchar_t *wauthor, *wmsg;
2275 int width;
2276 if (ncommits >= limit - 1)
2277 break;
2278 author = strdup(got_object_commit_get_author(entry->commit));
2279 if (author == NULL) {
2280 err = got_error_from_errno("strdup");
2281 goto done;
2283 err = format_author(&wauthor, &width, author, COLS,
2284 date_display_cols);
2285 if (author_cols < width)
2286 author_cols = width;
2287 free(wauthor);
2288 free(author);
2289 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2290 if (err)
2291 goto done;
2292 msg = msg0;
2293 while (*msg == '\n')
2294 ++msg;
2295 if ((eol = strchr(msg, '\n')))
2296 *eol = '\0';
2297 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2298 date_display_cols + author_cols, 0);
2299 if (err)
2300 goto done;
2301 view->maxx = MAX(view->maxx, width);
2302 free(msg0);
2303 free(wmsg);
2304 ncommits++;
2305 entry = TAILQ_NEXT(entry, entry);
2308 entry = s->first_displayed_entry;
2309 s->last_displayed_entry = s->first_displayed_entry;
2310 ncommits = 0;
2311 while (entry) {
2312 if (ncommits >= limit - 1)
2313 break;
2314 if (ncommits == s->selected)
2315 wstandout(view->window);
2316 err = draw_commit(view, entry->commit, entry->id,
2317 date_display_cols, author_cols);
2318 if (ncommits == s->selected)
2319 wstandend(view->window);
2320 if (err)
2321 goto done;
2322 ncommits++;
2323 s->last_displayed_entry = entry;
2324 entry = TAILQ_NEXT(entry, entry);
2327 view_border(view);
2328 done:
2329 free(id_str);
2330 free(refs_str);
2331 free(ncommits_str);
2332 free(header);
2333 return err;
2336 static void
2337 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2339 struct commit_queue_entry *entry;
2340 int nscrolled = 0;
2342 entry = TAILQ_FIRST(&s->commits.head);
2343 if (s->first_displayed_entry == entry)
2344 return;
2346 entry = s->first_displayed_entry;
2347 while (entry && nscrolled < maxscroll) {
2348 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2349 if (entry) {
2350 s->first_displayed_entry = entry;
2351 nscrolled++;
2356 static const struct got_error *
2357 trigger_log_thread(struct tog_view *view, int wait)
2359 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2360 int errcode;
2362 halfdelay(1); /* fast refresh while loading commits */
2364 while (!ta->log_complete && !tog_thread_error &&
2365 (ta->commits_needed > 0 || ta->load_all)) {
2366 /* Wake the log thread. */
2367 errcode = pthread_cond_signal(&ta->need_commits);
2368 if (errcode)
2369 return got_error_set_errno(errcode,
2370 "pthread_cond_signal");
2373 * The mutex will be released while the view loop waits
2374 * in wgetch(), at which time the log thread will run.
2376 if (!wait)
2377 break;
2379 /* Display progress update in log view. */
2380 show_log_view(view);
2381 update_panels();
2382 doupdate();
2384 /* Wait right here while next commit is being loaded. */
2385 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2386 if (errcode)
2387 return got_error_set_errno(errcode,
2388 "pthread_cond_wait");
2390 /* Display progress update in log view. */
2391 show_log_view(view);
2392 update_panels();
2393 doupdate();
2396 return NULL;
2399 static const struct got_error *
2400 request_log_commits(struct tog_view *view)
2402 struct tog_log_view_state *state = &view->state.log;
2403 const struct got_error *err = NULL;
2405 state->thread_args.commits_needed += view->nscrolled;
2406 err = trigger_log_thread(view, 1);
2407 view->nscrolled = 0;
2409 return err;
2412 static const struct got_error *
2413 log_scroll_down(struct tog_view *view, int maxscroll)
2415 struct tog_log_view_state *s = &view->state.log;
2416 const struct got_error *err = NULL;
2417 struct commit_queue_entry *pentry;
2418 int nscrolled = 0, ncommits_needed;
2420 if (s->last_displayed_entry == NULL)
2421 return NULL;
2423 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2424 if (s->commits.ncommits < ncommits_needed &&
2425 !s->thread_args.log_complete) {
2427 * Ask the log thread for required amount of commits.
2429 s->thread_args.commits_needed += maxscroll;
2430 err = trigger_log_thread(view, 1);
2431 if (err)
2432 return err;
2435 do {
2436 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2437 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2438 break;
2440 s->last_displayed_entry = pentry ?
2441 pentry : s->last_displayed_entry;;
2443 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2444 if (pentry == NULL)
2445 break;
2446 s->first_displayed_entry = pentry;
2447 } while (++nscrolled < maxscroll);
2449 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2450 view->nscrolled += nscrolled;
2451 else
2452 view->nscrolled = 0;
2454 return err;
2457 static const struct got_error *
2458 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2459 struct got_commit_object *commit, struct got_object_id *commit_id,
2460 struct tog_view *log_view, struct got_repository *repo)
2462 const struct got_error *err;
2463 struct got_object_qid *parent_id;
2464 struct tog_view *diff_view;
2466 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2467 if (diff_view == NULL)
2468 return got_error_from_errno("view_open");
2470 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2471 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2472 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2473 if (err == NULL)
2474 *new_view = diff_view;
2475 return err;
2478 static const struct got_error *
2479 tree_view_visit_subtree(struct tog_tree_view_state *s,
2480 struct got_tree_object *subtree)
2482 struct tog_parent_tree *parent;
2484 parent = calloc(1, sizeof(*parent));
2485 if (parent == NULL)
2486 return got_error_from_errno("calloc");
2488 parent->tree = s->tree;
2489 parent->first_displayed_entry = s->first_displayed_entry;
2490 parent->selected_entry = s->selected_entry;
2491 parent->selected = s->selected;
2492 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2493 s->tree = subtree;
2494 s->selected = 0;
2495 s->first_displayed_entry = NULL;
2496 return NULL;
2499 static const struct got_error *
2500 tree_view_walk_path(struct tog_tree_view_state *s,
2501 struct got_commit_object *commit, const char *path)
2503 const struct got_error *err = NULL;
2504 struct got_tree_object *tree = NULL;
2505 const char *p;
2506 char *slash, *subpath = NULL;
2508 /* Walk the path and open corresponding tree objects. */
2509 p = path;
2510 while (*p) {
2511 struct got_tree_entry *te;
2512 struct got_object_id *tree_id;
2513 char *te_name;
2515 while (p[0] == '/')
2516 p++;
2518 /* Ensure the correct subtree entry is selected. */
2519 slash = strchr(p, '/');
2520 if (slash == NULL)
2521 te_name = strdup(p);
2522 else
2523 te_name = strndup(p, slash - p);
2524 if (te_name == NULL) {
2525 err = got_error_from_errno("strndup");
2526 break;
2528 te = got_object_tree_find_entry(s->tree, te_name);
2529 if (te == NULL) {
2530 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2531 free(te_name);
2532 break;
2534 free(te_name);
2535 s->first_displayed_entry = s->selected_entry = te;
2537 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2538 break; /* jump to this file's entry */
2540 slash = strchr(p, '/');
2541 if (slash)
2542 subpath = strndup(path, slash - path);
2543 else
2544 subpath = strdup(path);
2545 if (subpath == NULL) {
2546 err = got_error_from_errno("strdup");
2547 break;
2550 err = got_object_id_by_path(&tree_id, s->repo, commit,
2551 subpath);
2552 if (err)
2553 break;
2555 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2556 free(tree_id);
2557 if (err)
2558 break;
2560 err = tree_view_visit_subtree(s, tree);
2561 if (err) {
2562 got_object_tree_close(tree);
2563 break;
2565 if (slash == NULL)
2566 break;
2567 free(subpath);
2568 subpath = NULL;
2569 p = slash;
2572 free(subpath);
2573 return err;
2576 static const struct got_error *
2577 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2578 struct commit_queue_entry *entry, const char *path,
2579 const char *head_ref_name, struct got_repository *repo)
2581 const struct got_error *err = NULL;
2582 struct tog_tree_view_state *s;
2583 struct tog_view *tree_view;
2585 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2586 if (tree_view == NULL)
2587 return got_error_from_errno("view_open");
2589 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2590 if (err)
2591 return err;
2592 s = &tree_view->state.tree;
2594 *new_view = tree_view;
2596 if (got_path_is_root_dir(path))
2597 return NULL;
2599 return tree_view_walk_path(s, entry->commit, path);
2602 static const struct got_error *
2603 block_signals_used_by_main_thread(void)
2605 sigset_t sigset;
2606 int errcode;
2608 if (sigemptyset(&sigset) == -1)
2609 return got_error_from_errno("sigemptyset");
2611 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2612 if (sigaddset(&sigset, SIGWINCH) == -1)
2613 return got_error_from_errno("sigaddset");
2614 if (sigaddset(&sigset, SIGCONT) == -1)
2615 return got_error_from_errno("sigaddset");
2616 if (sigaddset(&sigset, SIGINT) == -1)
2617 return got_error_from_errno("sigaddset");
2618 if (sigaddset(&sigset, SIGTERM) == -1)
2619 return got_error_from_errno("sigaddset");
2621 /* ncurses handles SIGTSTP */
2622 if (sigaddset(&sigset, SIGTSTP) == -1)
2623 return got_error_from_errno("sigaddset");
2625 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2626 if (errcode)
2627 return got_error_set_errno(errcode, "pthread_sigmask");
2629 return NULL;
2632 static void *
2633 log_thread(void *arg)
2635 const struct got_error *err = NULL;
2636 int errcode = 0;
2637 struct tog_log_thread_args *a = arg;
2638 int done = 0;
2641 * Sync startup with main thread such that we begin our
2642 * work once view_input() has released the mutex.
2644 errcode = pthread_mutex_lock(&tog_mutex);
2645 if (errcode) {
2646 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2647 return (void *)err;
2650 err = block_signals_used_by_main_thread();
2651 if (err) {
2652 pthread_mutex_unlock(&tog_mutex);
2653 goto done;
2656 while (!done && !err && !tog_fatal_signal_received()) {
2657 errcode = pthread_mutex_unlock(&tog_mutex);
2658 if (errcode) {
2659 err = got_error_set_errno(errcode,
2660 "pthread_mutex_unlock");
2661 goto done;
2663 err = queue_commits(a);
2664 if (err) {
2665 if (err->code != GOT_ERR_ITER_COMPLETED)
2666 goto done;
2667 err = NULL;
2668 done = 1;
2669 } else if (a->commits_needed > 0 && !a->load_all)
2670 a->commits_needed--;
2672 errcode = pthread_mutex_lock(&tog_mutex);
2673 if (errcode) {
2674 err = got_error_set_errno(errcode,
2675 "pthread_mutex_lock");
2676 goto done;
2677 } else if (*a->quit)
2678 done = 1;
2679 else if (*a->first_displayed_entry == NULL) {
2680 *a->first_displayed_entry =
2681 TAILQ_FIRST(&a->commits->head);
2682 *a->selected_entry = *a->first_displayed_entry;
2685 errcode = pthread_cond_signal(&a->commit_loaded);
2686 if (errcode) {
2687 err = got_error_set_errno(errcode,
2688 "pthread_cond_signal");
2689 pthread_mutex_unlock(&tog_mutex);
2690 goto done;
2693 if (done)
2694 a->commits_needed = 0;
2695 else {
2696 if (a->commits_needed == 0 && !a->load_all) {
2697 errcode = pthread_cond_wait(&a->need_commits,
2698 &tog_mutex);
2699 if (errcode) {
2700 err = got_error_set_errno(errcode,
2701 "pthread_cond_wait");
2702 pthread_mutex_unlock(&tog_mutex);
2703 goto done;
2705 if (*a->quit)
2706 done = 1;
2710 a->log_complete = 1;
2711 errcode = pthread_mutex_unlock(&tog_mutex);
2712 if (errcode)
2713 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2714 done:
2715 if (err) {
2716 tog_thread_error = 1;
2717 pthread_cond_signal(&a->commit_loaded);
2719 return (void *)err;
2722 static const struct got_error *
2723 stop_log_thread(struct tog_log_view_state *s)
2725 const struct got_error *err = NULL, *thread_err = NULL;
2726 int errcode;
2728 if (s->thread) {
2729 s->quit = 1;
2730 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2731 if (errcode)
2732 return got_error_set_errno(errcode,
2733 "pthread_cond_signal");
2734 errcode = pthread_mutex_unlock(&tog_mutex);
2735 if (errcode)
2736 return got_error_set_errno(errcode,
2737 "pthread_mutex_unlock");
2738 errcode = pthread_join(s->thread, (void **)&thread_err);
2739 if (errcode)
2740 return got_error_set_errno(errcode, "pthread_join");
2741 errcode = pthread_mutex_lock(&tog_mutex);
2742 if (errcode)
2743 return got_error_set_errno(errcode,
2744 "pthread_mutex_lock");
2745 s->thread = NULL;
2748 if (s->thread_args.repo) {
2749 err = got_repo_close(s->thread_args.repo);
2750 s->thread_args.repo = NULL;
2753 if (s->thread_args.pack_fds) {
2754 const struct got_error *pack_err =
2755 got_repo_pack_fds_close(s->thread_args.pack_fds);
2756 if (err == NULL)
2757 err = pack_err;
2758 s->thread_args.pack_fds = NULL;
2761 if (s->thread_args.graph) {
2762 got_commit_graph_close(s->thread_args.graph);
2763 s->thread_args.graph = NULL;
2766 return err ? err : thread_err;
2769 static const struct got_error *
2770 close_log_view(struct tog_view *view)
2772 const struct got_error *err = NULL;
2773 struct tog_log_view_state *s = &view->state.log;
2774 int errcode;
2776 err = stop_log_thread(s);
2778 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2779 if (errcode && err == NULL)
2780 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2782 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2783 if (errcode && err == NULL)
2784 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2786 free_commits(&s->commits);
2787 free(s->in_repo_path);
2788 s->in_repo_path = NULL;
2789 free(s->start_id);
2790 s->start_id = NULL;
2791 free(s->head_ref_name);
2792 s->head_ref_name = NULL;
2793 return err;
2796 static const struct got_error *
2797 search_start_log_view(struct tog_view *view)
2799 struct tog_log_view_state *s = &view->state.log;
2801 s->matched_entry = NULL;
2802 s->search_entry = NULL;
2803 return NULL;
2806 static const struct got_error *
2807 search_next_log_view(struct tog_view *view)
2809 const struct got_error *err = NULL;
2810 struct tog_log_view_state *s = &view->state.log;
2811 struct commit_queue_entry *entry;
2813 /* Display progress update in log view. */
2814 show_log_view(view);
2815 update_panels();
2816 doupdate();
2818 if (s->search_entry) {
2819 int errcode, ch;
2820 errcode = pthread_mutex_unlock(&tog_mutex);
2821 if (errcode)
2822 return got_error_set_errno(errcode,
2823 "pthread_mutex_unlock");
2824 ch = wgetch(view->window);
2825 errcode = pthread_mutex_lock(&tog_mutex);
2826 if (errcode)
2827 return got_error_set_errno(errcode,
2828 "pthread_mutex_lock");
2829 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2830 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2831 return NULL;
2833 if (view->searching == TOG_SEARCH_FORWARD)
2834 entry = TAILQ_NEXT(s->search_entry, entry);
2835 else
2836 entry = TAILQ_PREV(s->search_entry,
2837 commit_queue_head, entry);
2838 } else if (s->matched_entry) {
2839 int matched_idx = s->matched_entry->idx;
2840 int selected_idx = s->selected_entry->idx;
2843 * If the user has moved the cursor after we hit a match,
2844 * the position from where we should continue searching
2845 * might have changed.
2847 if (view->searching == TOG_SEARCH_FORWARD) {
2848 if (matched_idx > selected_idx)
2849 entry = TAILQ_NEXT(s->selected_entry, entry);
2850 else
2851 entry = TAILQ_NEXT(s->matched_entry, entry);
2852 } else {
2853 if (matched_idx < selected_idx)
2854 entry = TAILQ_PREV(s->selected_entry,
2855 commit_queue_head, entry);
2856 else
2857 entry = TAILQ_PREV(s->matched_entry,
2858 commit_queue_head, entry);
2860 } else {
2861 entry = s->selected_entry;
2864 while (1) {
2865 int have_match = 0;
2867 if (entry == NULL) {
2868 if (s->thread_args.log_complete ||
2869 view->searching == TOG_SEARCH_BACKWARD) {
2870 view->search_next_done =
2871 (s->matched_entry == NULL ?
2872 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2873 s->search_entry = NULL;
2874 return NULL;
2877 * Poke the log thread for more commits and return,
2878 * allowing the main loop to make progress. Search
2879 * will resume at s->search_entry once we come back.
2881 s->thread_args.commits_needed++;
2882 return trigger_log_thread(view, 0);
2885 err = match_commit(&have_match, entry->id, entry->commit,
2886 &view->regex);
2887 if (err)
2888 break;
2889 if (have_match) {
2890 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2891 s->matched_entry = entry;
2892 break;
2895 s->search_entry = entry;
2896 if (view->searching == TOG_SEARCH_FORWARD)
2897 entry = TAILQ_NEXT(entry, entry);
2898 else
2899 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2902 if (s->matched_entry) {
2903 int cur = s->selected_entry->idx;
2904 while (cur < s->matched_entry->idx) {
2905 err = input_log_view(NULL, view, KEY_DOWN);
2906 if (err)
2907 return err;
2908 cur++;
2910 while (cur > s->matched_entry->idx) {
2911 err = input_log_view(NULL, view, KEY_UP);
2912 if (err)
2913 return err;
2914 cur--;
2918 s->search_entry = NULL;
2920 return NULL;
2923 static const struct got_error *
2924 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2925 struct got_repository *repo, const char *head_ref_name,
2926 const char *in_repo_path, int log_branches)
2928 const struct got_error *err = NULL;
2929 struct tog_log_view_state *s = &view->state.log;
2930 struct got_repository *thread_repo = NULL;
2931 struct got_commit_graph *thread_graph = NULL;
2932 int errcode;
2934 if (in_repo_path != s->in_repo_path) {
2935 free(s->in_repo_path);
2936 s->in_repo_path = strdup(in_repo_path);
2937 if (s->in_repo_path == NULL)
2938 return got_error_from_errno("strdup");
2941 /* The commit queue only contains commits being displayed. */
2942 TAILQ_INIT(&s->commits.head);
2943 s->commits.ncommits = 0;
2945 s->repo = repo;
2946 if (head_ref_name) {
2947 s->head_ref_name = strdup(head_ref_name);
2948 if (s->head_ref_name == NULL) {
2949 err = got_error_from_errno("strdup");
2950 goto done;
2953 s->start_id = got_object_id_dup(start_id);
2954 if (s->start_id == NULL) {
2955 err = got_error_from_errno("got_object_id_dup");
2956 goto done;
2958 s->log_branches = log_branches;
2960 STAILQ_INIT(&s->colors);
2961 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2962 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2963 get_color_value("TOG_COLOR_COMMIT"));
2964 if (err)
2965 goto done;
2966 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2967 get_color_value("TOG_COLOR_AUTHOR"));
2968 if (err) {
2969 free_colors(&s->colors);
2970 goto done;
2972 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2973 get_color_value("TOG_COLOR_DATE"));
2974 if (err) {
2975 free_colors(&s->colors);
2976 goto done;
2980 view->show = show_log_view;
2981 view->input = input_log_view;
2982 view->close = close_log_view;
2983 view->search_start = search_start_log_view;
2984 view->search_next = search_next_log_view;
2986 if (s->thread_args.pack_fds == NULL) {
2987 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2988 if (err)
2989 goto done;
2991 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2992 s->thread_args.pack_fds);
2993 if (err)
2994 goto done;
2995 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2996 !s->log_branches);
2997 if (err)
2998 goto done;
2999 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3000 s->repo, NULL, NULL);
3001 if (err)
3002 goto done;
3004 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3005 if (errcode) {
3006 err = got_error_set_errno(errcode, "pthread_cond_init");
3007 goto done;
3009 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3010 if (errcode) {
3011 err = got_error_set_errno(errcode, "pthread_cond_init");
3012 goto done;
3015 s->thread_args.commits_needed = view->nlines;
3016 s->thread_args.graph = thread_graph;
3017 s->thread_args.commits = &s->commits;
3018 s->thread_args.in_repo_path = s->in_repo_path;
3019 s->thread_args.start_id = s->start_id;
3020 s->thread_args.repo = thread_repo;
3021 s->thread_args.log_complete = 0;
3022 s->thread_args.quit = &s->quit;
3023 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3024 s->thread_args.selected_entry = &s->selected_entry;
3025 s->thread_args.searching = &view->searching;
3026 s->thread_args.search_next_done = &view->search_next_done;
3027 s->thread_args.regex = &view->regex;
3028 done:
3029 if (err)
3030 close_log_view(view);
3031 return err;
3034 static const struct got_error *
3035 show_log_view(struct tog_view *view)
3037 const struct got_error *err;
3038 struct tog_log_view_state *s = &view->state.log;
3040 if (s->thread == NULL) {
3041 int errcode = pthread_create(&s->thread, NULL, log_thread,
3042 &s->thread_args);
3043 if (errcode)
3044 return got_error_set_errno(errcode, "pthread_create");
3045 if (s->thread_args.commits_needed > 0) {
3046 err = trigger_log_thread(view, 1);
3047 if (err)
3048 return err;
3052 return draw_commits(view);
3055 static void
3056 log_move_cursor_up(struct tog_view *view, int page, int home)
3058 struct tog_log_view_state *s = &view->state.log;
3060 if (s->selected_entry->idx == 0)
3061 view->count = 0;
3062 if (s->first_displayed_entry == NULL)
3063 return;
3065 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3066 || home)
3067 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3069 if (!page && !home && s->selected > 0)
3070 --s->selected;
3071 else
3072 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3074 select_commit(s);
3075 return;
3078 static const struct got_error *
3079 log_move_cursor_down(struct tog_view *view, int page)
3081 struct tog_log_view_state *s = &view->state.log;
3082 struct commit_queue_entry *first;
3083 const struct got_error *err = NULL;
3085 first = s->first_displayed_entry;
3086 if (first == NULL) {
3087 view->count = 0;
3088 return NULL;
3091 if (s->thread_args.log_complete &&
3092 s->selected_entry->idx >= s->commits.ncommits - 1)
3093 return NULL;
3095 if (!page) {
3096 int eos = view->nlines - 2;
3098 if (view_is_hsplit_top(view))
3099 --eos; /* border consumes the last line */
3100 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3101 ++s->selected;
3102 else
3103 err = log_scroll_down(view, 1);
3104 } else if (s->thread_args.load_all) {
3105 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3106 s->selected += MIN(s->last_displayed_entry->idx -
3107 s->selected_entry->idx, page + 1);
3108 else
3109 err = log_scroll_down(view, MIN(page,
3110 s->commits.ncommits - s->selected_entry->idx - 1));
3111 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3112 } else {
3113 err = log_scroll_down(view, page);
3114 if (err)
3115 return err;
3116 if (first == s->first_displayed_entry && s->selected <
3117 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3118 s->selected = MIN(s->commits.ncommits - 1, page);
3121 if (err)
3122 return err;
3125 * We might necessarily overshoot in horizontal
3126 * splits; if so, select the last displayed commit.
3128 s->selected = MIN(s->selected,
3129 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3131 select_commit(s);
3133 if (s->thread_args.log_complete &&
3134 s->selected_entry->idx == s->commits.ncommits - 1)
3135 view->count = 0;
3137 return NULL;
3140 static void
3141 view_get_split(struct tog_view *view, int *y, int *x)
3143 *x = 0;
3144 *y = 0;
3146 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3147 if (view->child && view->child->resized_y)
3148 *y = view->child->resized_y;
3149 else if (view->resized_y)
3150 *y = view->resized_y;
3151 else
3152 *y = view_split_begin_y(view->lines);
3153 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3154 if (view->child && view->child->resized_x)
3155 *x = view->child->resized_x;
3156 else if (view->resized_x)
3157 *x = view->resized_x;
3158 else
3159 *x = view_split_begin_x(view->begin_x);
3163 /* Split view horizontally at y and offset view->state->selected line. */
3164 static const struct got_error *
3165 view_init_hsplit(struct tog_view *view, int y)
3167 const struct got_error *err = NULL;
3169 view->nlines = y;
3170 view->ncols = COLS;
3171 err = view_resize(view);
3172 if (err)
3173 return err;
3175 err = offset_selection_down(view);
3177 return err;
3180 static const struct got_error *
3181 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3183 const struct got_error *err = NULL;
3184 struct tog_log_view_state *s = &view->state.log;
3185 struct tog_view *diff_view = NULL, *tree_view = NULL;
3186 struct tog_view *ref_view = NULL;
3187 struct commit_queue_entry *entry;
3188 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3190 if (s->thread_args.load_all) {
3191 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3192 s->thread_args.load_all = 0;
3193 else if (s->thread_args.log_complete) {
3194 err = log_move_cursor_down(view, s->commits.ncommits);
3195 s->thread_args.load_all = 0;
3197 return err;
3200 eos = nscroll = view->nlines - 1;
3201 if (view_is_hsplit_top(view))
3202 --eos; /* border */
3204 switch (ch) {
3205 case 'q':
3206 s->quit = 1;
3207 break;
3208 case '0':
3209 view->x = 0;
3210 break;
3211 case '$':
3212 view->x = MAX(view->maxx - view->ncols / 2, 0);
3213 view->count = 0;
3214 break;
3215 case KEY_RIGHT:
3216 case 'l':
3217 if (view->x + view->ncols / 2 < view->maxx)
3218 view->x += 2; /* move two columns right */
3219 else
3220 view->count = 0;
3221 break;
3222 case KEY_LEFT:
3223 case 'h':
3224 view->x -= MIN(view->x, 2); /* move two columns back */
3225 if (view->x <= 0)
3226 view->count = 0;
3227 break;
3228 case 'k':
3229 case KEY_UP:
3230 case '<':
3231 case ',':
3232 case CTRL('p'):
3233 log_move_cursor_up(view, 0, 0);
3234 break;
3235 case 'g':
3236 case KEY_HOME:
3237 log_move_cursor_up(view, 0, 1);
3238 view->count = 0;
3239 break;
3240 case CTRL('u'):
3241 case 'u':
3242 nscroll /= 2;
3243 /* FALL THROUGH */
3244 case KEY_PPAGE:
3245 case CTRL('b'):
3246 case 'b':
3247 log_move_cursor_up(view, nscroll, 0);
3248 break;
3249 case 'j':
3250 case KEY_DOWN:
3251 case '>':
3252 case '.':
3253 case CTRL('n'):
3254 err = log_move_cursor_down(view, 0);
3255 break;
3256 case 'G':
3257 case KEY_END: {
3258 /* We don't know yet how many commits, so we're forced to
3259 * traverse them all. */
3260 view->count = 0;
3261 if (!s->thread_args.log_complete) {
3262 s->thread_args.load_all = 1;
3263 return trigger_log_thread(view, 0);
3266 s->selected = 0;
3267 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3268 for (n = 0; n < eos; n++) {
3269 if (entry == NULL)
3270 break;
3271 s->first_displayed_entry = entry;
3272 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3274 if (n > 0)
3275 s->selected = n - 1;
3276 select_commit(s);
3277 break;
3279 case CTRL('d'):
3280 case 'd':
3281 nscroll /= 2;
3282 /* FALL THROUGH */
3283 case KEY_NPAGE:
3284 case CTRL('f'):
3285 case 'f':
3286 case ' ':
3287 err = log_move_cursor_down(view, nscroll);
3288 break;
3289 case KEY_RESIZE:
3290 if (s->selected > view->nlines - 2)
3291 s->selected = view->nlines - 2;
3292 if (s->selected > s->commits.ncommits - 1)
3293 s->selected = s->commits.ncommits - 1;
3294 select_commit(s);
3295 if (s->commits.ncommits < view->nlines - 1 &&
3296 !s->thread_args.log_complete) {
3297 s->thread_args.commits_needed += (view->nlines - 1) -
3298 s->commits.ncommits;
3299 err = trigger_log_thread(view, 1);
3301 break;
3302 case KEY_ENTER:
3303 case '\r':
3304 view->count = 0;
3305 if (s->selected_entry == NULL)
3306 break;
3308 /* get dimensions--don't split till initialisation succeeds */
3309 if (view_is_parent_view(view))
3310 view_get_split(view, &begin_y, &begin_x);
3312 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3313 s->selected_entry->commit, s->selected_entry->id,
3314 view, s->repo);
3315 if (err)
3316 break;
3318 if (view_is_parent_view(view) &&
3319 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3320 err = view_init_hsplit(view, begin_y);
3321 if (err)
3322 break;
3325 view->focussed = 0;
3326 diff_view->focussed = 1;
3327 diff_view->mode = view->mode;
3328 diff_view->nlines = view->lines - begin_y;
3330 if (view_is_parent_view(view)) {
3331 view_transfer_size(diff_view, view);
3332 err = view_close_child(view);
3333 if (err)
3334 return err;
3335 err = view_set_child(view, diff_view);
3336 if (err)
3337 return err;
3338 view->focus_child = 1;
3339 } else
3340 *new_view = diff_view;
3341 break;
3342 case 't':
3343 view->count = 0;
3344 if (s->selected_entry == NULL)
3345 break;
3346 if (view_is_parent_view(view))
3347 view_get_split(view, &begin_y, &begin_x);
3348 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3349 s->selected_entry, s->in_repo_path, s->head_ref_name,
3350 s->repo);
3351 if (err)
3352 break;
3353 if (view_is_parent_view(view) &&
3354 view->mode == TOG_VIEW_SPLIT_HRZN) {
3355 err = view_init_hsplit(view, begin_y);
3356 if (err)
3357 break;
3359 view->focussed = 0;
3360 tree_view->focussed = 1;
3361 tree_view->mode = view->mode;
3362 tree_view->nlines = view->lines - begin_y;
3363 if (view_is_parent_view(view)) {
3364 view_transfer_size(tree_view, view);
3365 err = view_close_child(view);
3366 if (err)
3367 return err;
3368 err = view_set_child(view, tree_view);
3369 if (err)
3370 return err;
3371 view->focus_child = 1;
3372 } else
3373 *new_view = tree_view;
3374 break;
3375 case KEY_BACKSPACE:
3376 case CTRL('l'):
3377 case 'B':
3378 view->count = 0;
3379 if (ch == KEY_BACKSPACE &&
3380 got_path_is_root_dir(s->in_repo_path))
3381 break;
3382 err = stop_log_thread(s);
3383 if (err)
3384 return err;
3385 if (ch == KEY_BACKSPACE) {
3386 char *parent_path;
3387 err = got_path_dirname(&parent_path, s->in_repo_path);
3388 if (err)
3389 return err;
3390 free(s->in_repo_path);
3391 s->in_repo_path = parent_path;
3392 s->thread_args.in_repo_path = s->in_repo_path;
3393 } else if (ch == CTRL('l')) {
3394 struct got_object_id *start_id;
3395 err = got_repo_match_object_id(&start_id, NULL,
3396 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3397 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3398 if (err)
3399 return err;
3400 free(s->start_id);
3401 s->start_id = start_id;
3402 s->thread_args.start_id = s->start_id;
3403 } else /* 'B' */
3404 s->log_branches = !s->log_branches;
3406 if (s->thread_args.pack_fds == NULL) {
3407 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3408 if (err)
3409 return err;
3411 err = got_repo_open(&s->thread_args.repo,
3412 got_repo_get_path(s->repo), NULL,
3413 s->thread_args.pack_fds);
3414 if (err)
3415 return err;
3416 tog_free_refs();
3417 err = tog_load_refs(s->repo, 0);
3418 if (err)
3419 return err;
3420 err = got_commit_graph_open(&s->thread_args.graph,
3421 s->in_repo_path, !s->log_branches);
3422 if (err)
3423 return err;
3424 err = got_commit_graph_iter_start(s->thread_args.graph,
3425 s->start_id, s->repo, NULL, NULL);
3426 if (err)
3427 return err;
3428 free_commits(&s->commits);
3429 s->first_displayed_entry = NULL;
3430 s->last_displayed_entry = NULL;
3431 s->selected_entry = NULL;
3432 s->selected = 0;
3433 s->thread_args.log_complete = 0;
3434 s->quit = 0;
3435 s->thread_args.commits_needed = view->lines;
3436 s->matched_entry = NULL;
3437 s->search_entry = NULL;
3438 break;
3439 case 'r':
3440 view->count = 0;
3441 if (view_is_parent_view(view))
3442 view_get_split(view, &begin_y, &begin_x);
3443 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3444 if (ref_view == NULL)
3445 return got_error_from_errno("view_open");
3446 err = open_ref_view(ref_view, s->repo);
3447 if (err) {
3448 view_close(ref_view);
3449 return err;
3451 if (view_is_parent_view(view) &&
3452 view->mode == TOG_VIEW_SPLIT_HRZN) {
3453 err = view_init_hsplit(view, begin_y);
3454 if (err)
3455 break;
3457 view->focussed = 0;
3458 ref_view->focussed = 1;
3459 ref_view->mode = view->mode;
3460 ref_view->nlines = view->lines - begin_y;
3461 if (view_is_parent_view(view)) {
3462 view_transfer_size(ref_view, view);
3463 err = view_close_child(view);
3464 if (err)
3465 return err;
3466 err = view_set_child(view, ref_view);
3467 if (err)
3468 return err;
3469 view->focus_child = 1;
3470 } else
3471 *new_view = ref_view;
3472 break;
3473 default:
3474 view->count = 0;
3475 break;
3478 return err;
3481 static const struct got_error *
3482 apply_unveil(const char *repo_path, const char *worktree_path)
3484 const struct got_error *error;
3486 #ifdef PROFILE
3487 if (unveil("gmon.out", "rwc") != 0)
3488 return got_error_from_errno2("unveil", "gmon.out");
3489 #endif
3490 if (repo_path && unveil(repo_path, "r") != 0)
3491 return got_error_from_errno2("unveil", repo_path);
3493 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3494 return got_error_from_errno2("unveil", worktree_path);
3496 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3497 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3499 error = got_privsep_unveil_exec_helpers();
3500 if (error != NULL)
3501 return error;
3503 if (unveil(NULL, NULL) != 0)
3504 return got_error_from_errno("unveil");
3506 return NULL;
3509 static void
3510 init_curses(void)
3513 * Override default signal handlers before starting ncurses.
3514 * This should prevent ncurses from installing its own
3515 * broken cleanup() signal handler.
3517 signal(SIGWINCH, tog_sigwinch);
3518 signal(SIGPIPE, tog_sigpipe);
3519 signal(SIGCONT, tog_sigcont);
3520 signal(SIGINT, tog_sigint);
3521 signal(SIGTERM, tog_sigterm);
3523 initscr();
3524 cbreak();
3525 halfdelay(1); /* Do fast refresh while initial view is loading. */
3526 noecho();
3527 nonl();
3528 intrflush(stdscr, FALSE);
3529 keypad(stdscr, TRUE);
3530 curs_set(0);
3531 if (getenv("TOG_COLORS") != NULL) {
3532 start_color();
3533 use_default_colors();
3537 static const struct got_error *
3538 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3539 struct got_repository *repo, struct got_worktree *worktree)
3541 const struct got_error *err = NULL;
3543 if (argc == 0) {
3544 *in_repo_path = strdup("/");
3545 if (*in_repo_path == NULL)
3546 return got_error_from_errno("strdup");
3547 return NULL;
3550 if (worktree) {
3551 const char *prefix = got_worktree_get_path_prefix(worktree);
3552 char *p;
3554 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3555 if (err)
3556 return err;
3557 if (asprintf(in_repo_path, "%s%s%s", prefix,
3558 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3559 p) == -1) {
3560 err = got_error_from_errno("asprintf");
3561 *in_repo_path = NULL;
3563 free(p);
3564 } else
3565 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3567 return err;
3570 static const struct got_error *
3571 cmd_log(int argc, char *argv[])
3573 const struct got_error *error;
3574 struct got_repository *repo = NULL;
3575 struct got_worktree *worktree = NULL;
3576 struct got_object_id *start_id = NULL;
3577 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3578 char *start_commit = NULL, *label = NULL;
3579 struct got_reference *ref = NULL;
3580 const char *head_ref_name = NULL;
3581 int ch, log_branches = 0;
3582 struct tog_view *view;
3583 int *pack_fds = NULL;
3585 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3586 switch (ch) {
3587 case 'b':
3588 log_branches = 1;
3589 break;
3590 case 'c':
3591 start_commit = optarg;
3592 break;
3593 case 'r':
3594 repo_path = realpath(optarg, NULL);
3595 if (repo_path == NULL)
3596 return got_error_from_errno2("realpath",
3597 optarg);
3598 break;
3599 default:
3600 usage_log();
3601 /* NOTREACHED */
3605 argc -= optind;
3606 argv += optind;
3608 if (argc > 1)
3609 usage_log();
3611 error = got_repo_pack_fds_open(&pack_fds);
3612 if (error != NULL)
3613 goto done;
3615 if (repo_path == NULL) {
3616 cwd = getcwd(NULL, 0);
3617 if (cwd == NULL)
3618 return got_error_from_errno("getcwd");
3619 error = got_worktree_open(&worktree, cwd);
3620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3621 goto done;
3622 if (worktree)
3623 repo_path =
3624 strdup(got_worktree_get_repo_path(worktree));
3625 else
3626 repo_path = strdup(cwd);
3627 if (repo_path == NULL) {
3628 error = got_error_from_errno("strdup");
3629 goto done;
3633 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3634 if (error != NULL)
3635 goto done;
3637 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3638 repo, worktree);
3639 if (error)
3640 goto done;
3642 init_curses();
3644 error = apply_unveil(got_repo_get_path(repo),
3645 worktree ? got_worktree_get_root_path(worktree) : NULL);
3646 if (error)
3647 goto done;
3649 /* already loaded by tog_log_with_path()? */
3650 if (TAILQ_EMPTY(&tog_refs)) {
3651 error = tog_load_refs(repo, 0);
3652 if (error)
3653 goto done;
3656 if (start_commit == NULL) {
3657 error = got_repo_match_object_id(&start_id, &label,
3658 worktree ? got_worktree_get_head_ref_name(worktree) :
3659 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3660 if (error)
3661 goto done;
3662 head_ref_name = label;
3663 } else {
3664 error = got_ref_open(&ref, repo, start_commit, 0);
3665 if (error == NULL)
3666 head_ref_name = got_ref_get_name(ref);
3667 else if (error->code != GOT_ERR_NOT_REF)
3668 goto done;
3669 error = got_repo_match_object_id(&start_id, NULL,
3670 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3671 if (error)
3672 goto done;
3675 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3676 if (view == NULL) {
3677 error = got_error_from_errno("view_open");
3678 goto done;
3680 error = open_log_view(view, start_id, repo, head_ref_name,
3681 in_repo_path, log_branches);
3682 if (error)
3683 goto done;
3684 if (worktree) {
3685 /* Release work tree lock. */
3686 got_worktree_close(worktree);
3687 worktree = NULL;
3689 error = view_loop(view);
3690 done:
3691 free(in_repo_path);
3692 free(repo_path);
3693 free(cwd);
3694 free(start_id);
3695 free(label);
3696 if (ref)
3697 got_ref_close(ref);
3698 if (repo) {
3699 const struct got_error *close_err = got_repo_close(repo);
3700 if (error == NULL)
3701 error = close_err;
3703 if (worktree)
3704 got_worktree_close(worktree);
3705 if (pack_fds) {
3706 const struct got_error *pack_err =
3707 got_repo_pack_fds_close(pack_fds);
3708 if (error == NULL)
3709 error = pack_err;
3711 tog_free_refs();
3712 return error;
3715 __dead static void
3716 usage_diff(void)
3718 endwin();
3719 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3720 "[-w] object1 object2\n", getprogname());
3721 exit(1);
3724 static int
3725 match_line(const char *line, regex_t *regex, size_t nmatch,
3726 regmatch_t *regmatch)
3728 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3731 static struct tog_color *
3732 match_color(struct tog_colors *colors, const char *line)
3734 struct tog_color *tc = NULL;
3736 STAILQ_FOREACH(tc, colors, entry) {
3737 if (match_line(line, &tc->regex, 0, NULL))
3738 return tc;
3741 return NULL;
3744 static const struct got_error *
3745 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3746 WINDOW *window, int skipcol, regmatch_t *regmatch)
3748 const struct got_error *err = NULL;
3749 char *exstr = NULL;
3750 wchar_t *wline = NULL;
3751 int rme, rms, n, width, scrollx;
3752 int width0 = 0, width1 = 0, width2 = 0;
3753 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3755 *wtotal = 0;
3757 rms = regmatch->rm_so;
3758 rme = regmatch->rm_eo;
3760 err = expand_tab(&exstr, line);
3761 if (err)
3762 return err;
3764 /* Split the line into 3 segments, according to match offsets. */
3765 seg0 = strndup(exstr, rms);
3766 if (seg0 == NULL) {
3767 err = got_error_from_errno("strndup");
3768 goto done;
3770 seg1 = strndup(exstr + rms, rme - rms);
3771 if (seg1 == NULL) {
3772 err = got_error_from_errno("strndup");
3773 goto done;
3775 seg2 = strdup(exstr + rme);
3776 if (seg2 == NULL) {
3777 err = got_error_from_errno("strndup");
3778 goto done;
3781 /* draw up to matched token if we haven't scrolled past it */
3782 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3783 col_tab_align, 1);
3784 if (err)
3785 goto done;
3786 n = MAX(width0 - skipcol, 0);
3787 if (n) {
3788 free(wline);
3789 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3790 wlimit, col_tab_align, 1);
3791 if (err)
3792 goto done;
3793 waddwstr(window, &wline[scrollx]);
3794 wlimit -= width;
3795 *wtotal += width;
3798 if (wlimit > 0) {
3799 int i = 0, w = 0;
3800 size_t wlen;
3802 free(wline);
3803 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3804 col_tab_align, 1);
3805 if (err)
3806 goto done;
3807 wlen = wcslen(wline);
3808 while (i < wlen) {
3809 width = wcwidth(wline[i]);
3810 if (width == -1) {
3811 /* should not happen, tabs are expanded */
3812 err = got_error(GOT_ERR_RANGE);
3813 goto done;
3815 if (width0 + w + width > skipcol)
3816 break;
3817 w += width;
3818 i++;
3820 /* draw (visible part of) matched token (if scrolled into it) */
3821 if (width1 - w > 0) {
3822 wattron(window, A_STANDOUT);
3823 waddwstr(window, &wline[i]);
3824 wattroff(window, A_STANDOUT);
3825 wlimit -= (width1 - w);
3826 *wtotal += (width1 - w);
3830 if (wlimit > 0) { /* draw rest of line */
3831 free(wline);
3832 if (skipcol > width0 + width1) {
3833 err = format_line(&wline, &width2, &scrollx, seg2,
3834 skipcol - (width0 + width1), wlimit,
3835 col_tab_align, 1);
3836 if (err)
3837 goto done;
3838 waddwstr(window, &wline[scrollx]);
3839 } else {
3840 err = format_line(&wline, &width2, NULL, seg2, 0,
3841 wlimit, col_tab_align, 1);
3842 if (err)
3843 goto done;
3844 waddwstr(window, wline);
3846 *wtotal += width2;
3848 done:
3849 free(wline);
3850 free(exstr);
3851 free(seg0);
3852 free(seg1);
3853 free(seg2);
3854 return err;
3857 static const struct got_error *
3858 draw_file(struct tog_view *view, const char *header)
3860 struct tog_diff_view_state *s = &view->state.diff;
3861 regmatch_t *regmatch = &view->regmatch;
3862 const struct got_error *err;
3863 int nprinted = 0;
3864 char *line;
3865 size_t linesize = 0;
3866 ssize_t linelen;
3867 struct tog_color *tc;
3868 wchar_t *wline;
3869 int width;
3870 int max_lines = view->nlines;
3871 int nlines = s->nlines;
3872 off_t line_offset;
3874 line_offset = s->line_offsets[s->first_displayed_line - 1];
3875 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3876 return got_error_from_errno("fseek");
3878 werase(view->window);
3880 if (header) {
3881 if (asprintf(&line, "[%d/%d] %s",
3882 s->first_displayed_line - 1 + s->selected_line, nlines,
3883 header) == -1)
3884 return got_error_from_errno("asprintf");
3885 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3886 0, 0);
3887 free(line);
3888 if (err)
3889 return err;
3891 if (view_needs_focus_indication(view))
3892 wstandout(view->window);
3893 waddwstr(view->window, wline);
3894 free(wline);
3895 wline = NULL;
3896 if (view_needs_focus_indication(view))
3897 wstandend(view->window);
3898 if (width <= view->ncols - 1)
3899 waddch(view->window, '\n');
3901 if (max_lines <= 1)
3902 return NULL;
3903 max_lines--;
3906 s->eof = 0;
3907 view->maxx = 0;
3908 line = NULL;
3909 while (max_lines > 0 && nprinted < max_lines) {
3910 linelen = getline(&line, &linesize, s->f);
3911 if (linelen == -1) {
3912 if (feof(s->f)) {
3913 s->eof = 1;
3914 break;
3916 free(line);
3917 return got_ferror(s->f, GOT_ERR_IO);
3920 /* Set view->maxx based on full line length. */
3921 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3922 view->x ? 1 : 0);
3923 if (err) {
3924 free(line);
3925 return err;
3927 view->maxx = MAX(view->maxx, width);
3928 free(wline);
3929 wline = NULL;
3931 tc = match_color(&s->colors, line);
3932 if (tc)
3933 wattr_on(view->window,
3934 COLOR_PAIR(tc->colorpair), NULL);
3935 if (s->first_displayed_line + nprinted == s->matched_line &&
3936 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3937 err = add_matched_line(&width, line, view->ncols, 0,
3938 view->window, view->x, regmatch);
3939 if (err) {
3940 free(line);
3941 return err;
3943 } else {
3944 int skip;
3945 err = format_line(&wline, &width, &skip, line,
3946 view->x, view->ncols, 0, view->x ? 1 : 0);
3947 if (err) {
3948 free(line);
3949 return err;
3951 waddwstr(view->window, &wline[skip]);
3952 free(wline);
3953 wline = NULL;
3955 if (tc)
3956 wattr_off(view->window,
3957 COLOR_PAIR(tc->colorpair), NULL);
3958 if (width <= view->ncols - 1)
3959 waddch(view->window, '\n');
3960 nprinted++;
3962 free(line);
3963 if (nprinted >= 1)
3964 s->last_displayed_line = s->first_displayed_line +
3965 (nprinted - 1);
3966 else
3967 s->last_displayed_line = s->first_displayed_line;
3969 view_border(view);
3971 if (s->eof) {
3972 while (nprinted < view->nlines) {
3973 waddch(view->window, '\n');
3974 nprinted++;
3977 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3978 view->ncols, 0, 0);
3979 if (err) {
3980 return err;
3983 wstandout(view->window);
3984 waddwstr(view->window, wline);
3985 free(wline);
3986 wline = NULL;
3987 wstandend(view->window);
3990 return NULL;
3993 static char *
3994 get_datestr(time_t *time, char *datebuf)
3996 struct tm mytm, *tm;
3997 char *p, *s;
3999 tm = gmtime_r(time, &mytm);
4000 if (tm == NULL)
4001 return NULL;
4002 s = asctime_r(tm, datebuf);
4003 if (s == NULL)
4004 return NULL;
4005 p = strchr(s, '\n');
4006 if (p)
4007 *p = '\0';
4008 return s;
4011 static const struct got_error *
4012 get_changed_paths(struct got_pathlist_head *paths,
4013 struct got_commit_object *commit, struct got_repository *repo)
4015 const struct got_error *err = NULL;
4016 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4017 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4018 struct got_object_qid *qid;
4020 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4021 if (qid != NULL) {
4022 struct got_commit_object *pcommit;
4023 err = got_object_open_as_commit(&pcommit, repo,
4024 &qid->id);
4025 if (err)
4026 return err;
4028 tree_id1 = got_object_id_dup(
4029 got_object_commit_get_tree_id(pcommit));
4030 if (tree_id1 == NULL) {
4031 got_object_commit_close(pcommit);
4032 return got_error_from_errno("got_object_id_dup");
4034 got_object_commit_close(pcommit);
4038 if (tree_id1) {
4039 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4040 if (err)
4041 goto done;
4044 tree_id2 = got_object_commit_get_tree_id(commit);
4045 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4046 if (err)
4047 goto done;
4049 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4050 got_diff_tree_collect_changed_paths, paths, 0);
4051 done:
4052 if (tree1)
4053 got_object_tree_close(tree1);
4054 if (tree2)
4055 got_object_tree_close(tree2);
4056 free(tree_id1);
4057 return err;
4060 static const struct got_error *
4061 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4063 off_t *p;
4065 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4066 if (p == NULL)
4067 return got_error_from_errno("reallocarray");
4068 *line_offsets = p;
4069 (*line_offsets)[*nlines] = off;
4070 (*nlines)++;
4071 return NULL;
4074 static const struct got_error *
4075 write_commit_info(off_t **line_offsets, size_t *nlines,
4076 struct got_object_id *commit_id, struct got_reflist_head *refs,
4077 struct got_repository *repo, FILE *outfile)
4079 const struct got_error *err = NULL;
4080 char datebuf[26], *datestr;
4081 struct got_commit_object *commit;
4082 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4083 time_t committer_time;
4084 const char *author, *committer;
4085 char *refs_str = NULL;
4086 struct got_pathlist_head changed_paths;
4087 struct got_pathlist_entry *pe;
4088 off_t outoff = 0;
4089 int n;
4091 TAILQ_INIT(&changed_paths);
4093 if (refs) {
4094 err = build_refs_str(&refs_str, refs, commit_id, repo);
4095 if (err)
4096 return err;
4099 err = got_object_open_as_commit(&commit, repo, commit_id);
4100 if (err)
4101 return err;
4103 err = got_object_id_str(&id_str, commit_id);
4104 if (err) {
4105 err = got_error_from_errno("got_object_id_str");
4106 goto done;
4109 err = add_line_offset(line_offsets, nlines, 0);
4110 if (err)
4111 goto done;
4113 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4114 refs_str ? refs_str : "", refs_str ? ")" : "");
4115 if (n < 0) {
4116 err = got_error_from_errno("fprintf");
4117 goto done;
4119 outoff += n;
4120 err = add_line_offset(line_offsets, nlines, outoff);
4121 if (err)
4122 goto done;
4124 n = fprintf(outfile, "from: %s\n",
4125 got_object_commit_get_author(commit));
4126 if (n < 0) {
4127 err = got_error_from_errno("fprintf");
4128 goto done;
4130 outoff += n;
4131 err = add_line_offset(line_offsets, nlines, outoff);
4132 if (err)
4133 goto done;
4135 committer_time = got_object_commit_get_committer_time(commit);
4136 datestr = get_datestr(&committer_time, datebuf);
4137 if (datestr) {
4138 n = fprintf(outfile, "date: %s UTC\n", datestr);
4139 if (n < 0) {
4140 err = got_error_from_errno("fprintf");
4141 goto done;
4143 outoff += n;
4144 err = add_line_offset(line_offsets, nlines, outoff);
4145 if (err)
4146 goto done;
4148 author = got_object_commit_get_author(commit);
4149 committer = got_object_commit_get_committer(commit);
4150 if (strcmp(author, committer) != 0) {
4151 n = fprintf(outfile, "via: %s\n", committer);
4152 if (n < 0) {
4153 err = got_error_from_errno("fprintf");
4154 goto done;
4156 outoff += n;
4157 err = add_line_offset(line_offsets, nlines, outoff);
4158 if (err)
4159 goto done;
4161 if (got_object_commit_get_nparents(commit) > 1) {
4162 const struct got_object_id_queue *parent_ids;
4163 struct got_object_qid *qid;
4164 int pn = 1;
4165 parent_ids = got_object_commit_get_parent_ids(commit);
4166 STAILQ_FOREACH(qid, parent_ids, entry) {
4167 err = got_object_id_str(&id_str, &qid->id);
4168 if (err)
4169 goto done;
4170 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4171 if (n < 0) {
4172 err = got_error_from_errno("fprintf");
4173 goto done;
4175 outoff += n;
4176 err = add_line_offset(line_offsets, nlines, outoff);
4177 if (err)
4178 goto done;
4179 free(id_str);
4180 id_str = NULL;
4184 err = got_object_commit_get_logmsg(&logmsg, commit);
4185 if (err)
4186 goto done;
4187 s = logmsg;
4188 while ((line = strsep(&s, "\n")) != NULL) {
4189 n = fprintf(outfile, "%s\n", line);
4190 if (n < 0) {
4191 err = got_error_from_errno("fprintf");
4192 goto done;
4194 outoff += n;
4195 err = add_line_offset(line_offsets, nlines, outoff);
4196 if (err)
4197 goto done;
4200 err = get_changed_paths(&changed_paths, commit, repo);
4201 if (err)
4202 goto done;
4203 TAILQ_FOREACH(pe, &changed_paths, entry) {
4204 struct got_diff_changed_path *cp = pe->data;
4205 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4206 if (n < 0) {
4207 err = got_error_from_errno("fprintf");
4208 goto done;
4210 outoff += n;
4211 err = add_line_offset(line_offsets, nlines, outoff);
4212 if (err)
4213 goto done;
4214 free((char *)pe->path);
4215 free(pe->data);
4218 fputc('\n', outfile);
4219 outoff++;
4220 err = add_line_offset(line_offsets, nlines, outoff);
4221 done:
4222 got_pathlist_free(&changed_paths);
4223 free(id_str);
4224 free(logmsg);
4225 free(refs_str);
4226 got_object_commit_close(commit);
4227 if (err) {
4228 free(*line_offsets);
4229 *line_offsets = NULL;
4230 *nlines = 0;
4232 return err;
4235 static const struct got_error *
4236 create_diff(struct tog_diff_view_state *s)
4238 const struct got_error *err = NULL;
4239 FILE *f = NULL;
4240 int obj_type;
4242 free(s->line_offsets);
4243 s->line_offsets = malloc(sizeof(off_t));
4244 if (s->line_offsets == NULL)
4245 return got_error_from_errno("malloc");
4246 s->nlines = 0;
4248 f = got_opentemp();
4249 if (f == NULL) {
4250 err = got_error_from_errno("got_opentemp");
4251 goto done;
4253 if (s->f && fclose(s->f) == EOF) {
4254 err = got_error_from_errno("fclose");
4255 goto done;
4257 s->f = f;
4259 if (s->id1)
4260 err = got_object_get_type(&obj_type, s->repo, s->id1);
4261 else
4262 err = got_object_get_type(&obj_type, s->repo, s->id2);
4263 if (err)
4264 goto done;
4266 switch (obj_type) {
4267 case GOT_OBJ_TYPE_BLOB:
4268 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4269 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4270 s->label1, s->label2, tog_diff_algo, s->diff_context,
4271 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4272 break;
4273 case GOT_OBJ_TYPE_TREE:
4274 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4275 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4276 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4277 s->force_text_diff, s->repo, s->f);
4278 break;
4279 case GOT_OBJ_TYPE_COMMIT: {
4280 const struct got_object_id_queue *parent_ids;
4281 struct got_object_qid *pid;
4282 struct got_commit_object *commit2;
4283 struct got_reflist_head *refs;
4285 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4286 if (err)
4287 goto done;
4288 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4289 /* Show commit info if we're diffing to a parent/root commit. */
4290 if (s->id1 == NULL) {
4291 err = write_commit_info(&s->line_offsets, &s->nlines,
4292 s->id2, refs, s->repo, s->f);
4293 if (err)
4294 goto done;
4295 } else {
4296 parent_ids = got_object_commit_get_parent_ids(commit2);
4297 STAILQ_FOREACH(pid, parent_ids, entry) {
4298 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4299 err = write_commit_info(
4300 &s->line_offsets, &s->nlines,
4301 s->id2, refs, s->repo, s->f);
4302 if (err)
4303 goto done;
4304 break;
4308 got_object_commit_close(commit2);
4310 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4311 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4312 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4313 s->force_text_diff, s->repo, s->f);
4314 break;
4316 default:
4317 err = got_error(GOT_ERR_OBJ_TYPE);
4318 break;
4320 if (err)
4321 goto done;
4322 done:
4323 if (s->f && fflush(s->f) != 0 && err == NULL)
4324 err = got_error_from_errno("fflush");
4325 return err;
4328 static void
4329 diff_view_indicate_progress(struct tog_view *view)
4331 mvwaddstr(view->window, 0, 0, "diffing...");
4332 update_panels();
4333 doupdate();
4336 static const struct got_error *
4337 search_start_diff_view(struct tog_view *view)
4339 struct tog_diff_view_state *s = &view->state.diff;
4341 s->matched_line = 0;
4342 return NULL;
4345 static const struct got_error *
4346 search_next_diff_view(struct tog_view *view)
4348 struct tog_diff_view_state *s = &view->state.diff;
4349 const struct got_error *err = NULL;
4350 int lineno;
4351 char *line = NULL;
4352 size_t linesize = 0;
4353 ssize_t linelen;
4355 if (!view->searching) {
4356 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4357 return NULL;
4360 if (s->matched_line) {
4361 if (view->searching == TOG_SEARCH_FORWARD)
4362 lineno = s->matched_line + 1;
4363 else
4364 lineno = s->matched_line - 1;
4365 } else
4366 lineno = s->first_displayed_line;
4368 while (1) {
4369 off_t offset;
4371 if (lineno <= 0 || lineno > s->nlines) {
4372 if (s->matched_line == 0) {
4373 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4374 break;
4377 if (view->searching == TOG_SEARCH_FORWARD)
4378 lineno = 1;
4379 else
4380 lineno = s->nlines;
4383 offset = s->line_offsets[lineno - 1];
4384 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4385 free(line);
4386 return got_error_from_errno("fseeko");
4388 linelen = getline(&line, &linesize, s->f);
4389 if (linelen != -1) {
4390 char *exstr;
4391 err = expand_tab(&exstr, line);
4392 if (err)
4393 break;
4394 if (match_line(exstr, &view->regex, 1,
4395 &view->regmatch)) {
4396 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4397 s->matched_line = lineno;
4398 free(exstr);
4399 break;
4401 free(exstr);
4403 if (view->searching == TOG_SEARCH_FORWARD)
4404 lineno++;
4405 else
4406 lineno--;
4408 free(line);
4410 if (s->matched_line) {
4411 s->first_displayed_line = s->matched_line;
4412 s->selected_line = 1;
4415 return err;
4418 static const struct got_error *
4419 close_diff_view(struct tog_view *view)
4421 const struct got_error *err = NULL;
4422 struct tog_diff_view_state *s = &view->state.diff;
4424 free(s->id1);
4425 s->id1 = NULL;
4426 free(s->id2);
4427 s->id2 = NULL;
4428 if (s->f && fclose(s->f) == EOF)
4429 err = got_error_from_errno("fclose");
4430 s->f = NULL;
4431 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4432 err = got_error_from_errno("fclose");
4433 s->f1 = NULL;
4434 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4435 err = got_error_from_errno("fclose");
4436 s->f2 = NULL;
4437 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4438 err = got_error_from_errno("close");
4439 s->fd1 = -1;
4440 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4441 err = got_error_from_errno("close");
4442 s->fd2 = -1;
4443 free_colors(&s->colors);
4444 free(s->line_offsets);
4445 s->line_offsets = NULL;
4446 s->nlines = 0;
4447 return err;
4450 static const struct got_error *
4451 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4452 struct got_object_id *id2, const char *label1, const char *label2,
4453 int diff_context, int ignore_whitespace, int force_text_diff,
4454 struct tog_view *parent_view, struct got_repository *repo)
4456 const struct got_error *err;
4457 struct tog_diff_view_state *s = &view->state.diff;
4459 memset(s, 0, sizeof(*s));
4460 s->fd1 = -1;
4461 s->fd2 = -1;
4463 if (id1 != NULL && id2 != NULL) {
4464 int type1, type2;
4465 err = got_object_get_type(&type1, repo, id1);
4466 if (err)
4467 return err;
4468 err = got_object_get_type(&type2, repo, id2);
4469 if (err)
4470 return err;
4472 if (type1 != type2)
4473 return got_error(GOT_ERR_OBJ_TYPE);
4475 s->first_displayed_line = 1;
4476 s->last_displayed_line = view->nlines;
4477 s->selected_line = 1;
4478 s->repo = repo;
4479 s->id1 = id1;
4480 s->id2 = id2;
4481 s->label1 = label1;
4482 s->label2 = label2;
4484 if (id1) {
4485 s->id1 = got_object_id_dup(id1);
4486 if (s->id1 == NULL)
4487 return got_error_from_errno("got_object_id_dup");
4488 } else
4489 s->id1 = NULL;
4491 s->id2 = got_object_id_dup(id2);
4492 if (s->id2 == NULL) {
4493 err = got_error_from_errno("got_object_id_dup");
4494 goto done;
4497 s->f1 = got_opentemp();
4498 if (s->f1 == NULL) {
4499 err = got_error_from_errno("got_opentemp");
4500 goto done;
4503 s->f2 = got_opentemp();
4504 if (s->f2 == NULL) {
4505 err = got_error_from_errno("got_opentemp");
4506 goto done;
4509 s->fd1 = got_opentempfd();
4510 if (s->fd1 == -1) {
4511 err = got_error_from_errno("got_opentempfd");
4512 goto done;
4515 s->fd2 = got_opentempfd();
4516 if (s->fd2 == -1) {
4517 err = got_error_from_errno("got_opentempfd");
4518 goto done;
4521 s->first_displayed_line = 1;
4522 s->last_displayed_line = view->nlines;
4523 s->diff_context = diff_context;
4524 s->ignore_whitespace = ignore_whitespace;
4525 s->force_text_diff = force_text_diff;
4526 s->parent_view = parent_view;
4527 s->repo = repo;
4529 STAILQ_INIT(&s->colors);
4530 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4531 err = add_color(&s->colors,
4532 "^-", TOG_COLOR_DIFF_MINUS,
4533 get_color_value("TOG_COLOR_DIFF_MINUS"));
4534 if (err)
4535 goto done;
4536 err = add_color(&s->colors, "^\\+",
4537 TOG_COLOR_DIFF_PLUS,
4538 get_color_value("TOG_COLOR_DIFF_PLUS"));
4539 if (err)
4540 goto done;
4541 err = add_color(&s->colors,
4542 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4543 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4544 if (err)
4545 goto done;
4547 err = add_color(&s->colors,
4548 "^(commit [0-9a-f]|parent [0-9]|"
4549 "(blob|file|tree|commit) [-+] |"
4550 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4551 get_color_value("TOG_COLOR_DIFF_META"));
4552 if (err)
4553 goto done;
4555 err = add_color(&s->colors,
4556 "^(from|via): ", TOG_COLOR_AUTHOR,
4557 get_color_value("TOG_COLOR_AUTHOR"));
4558 if (err)
4559 goto done;
4561 err = add_color(&s->colors,
4562 "^date: ", TOG_COLOR_DATE,
4563 get_color_value("TOG_COLOR_DATE"));
4564 if (err)
4565 goto done;
4568 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4569 view_is_splitscreen(view))
4570 show_log_view(parent_view); /* draw border */
4571 diff_view_indicate_progress(view);
4573 err = create_diff(s);
4575 view->show = show_diff_view;
4576 view->input = input_diff_view;
4577 view->reset = reset_diff_view;
4578 view->close = close_diff_view;
4579 view->search_start = search_start_diff_view;
4580 view->search_next = search_next_diff_view;
4581 done:
4582 if (err)
4583 close_diff_view(view);
4584 return err;
4587 static const struct got_error *
4588 show_diff_view(struct tog_view *view)
4590 const struct got_error *err;
4591 struct tog_diff_view_state *s = &view->state.diff;
4592 char *id_str1 = NULL, *id_str2, *header;
4593 const char *label1, *label2;
4595 if (s->id1) {
4596 err = got_object_id_str(&id_str1, s->id1);
4597 if (err)
4598 return err;
4599 label1 = s->label1 ? : id_str1;
4600 } else
4601 label1 = "/dev/null";
4603 err = got_object_id_str(&id_str2, s->id2);
4604 if (err)
4605 return err;
4606 label2 = s->label2 ? : id_str2;
4608 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4609 err = got_error_from_errno("asprintf");
4610 free(id_str1);
4611 free(id_str2);
4612 return err;
4614 free(id_str1);
4615 free(id_str2);
4617 err = draw_file(view, header);
4618 free(header);
4619 return err;
4622 static const struct got_error *
4623 set_selected_commit(struct tog_diff_view_state *s,
4624 struct commit_queue_entry *entry)
4626 const struct got_error *err;
4627 const struct got_object_id_queue *parent_ids;
4628 struct got_commit_object *selected_commit;
4629 struct got_object_qid *pid;
4631 free(s->id2);
4632 s->id2 = got_object_id_dup(entry->id);
4633 if (s->id2 == NULL)
4634 return got_error_from_errno("got_object_id_dup");
4636 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4637 if (err)
4638 return err;
4639 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4640 free(s->id1);
4641 pid = STAILQ_FIRST(parent_ids);
4642 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4643 got_object_commit_close(selected_commit);
4644 return NULL;
4647 static const struct got_error *
4648 reset_diff_view(struct tog_view *view)
4650 struct tog_diff_view_state *s = &view->state.diff;
4652 view->count = 0;
4653 wclear(view->window);
4654 s->first_displayed_line = 1;
4655 s->last_displayed_line = view->nlines;
4656 s->matched_line = 0;
4657 diff_view_indicate_progress(view);
4658 return create_diff(s);
4661 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4662 int, int, int);
4663 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4664 int, int);
4666 static const struct got_error *
4667 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4669 const struct got_error *err = NULL;
4670 struct tog_diff_view_state *s = &view->state.diff;
4671 struct tog_log_view_state *ls;
4672 struct commit_queue_entry *old_selected_entry;
4673 char *line = NULL;
4674 size_t linesize = 0;
4675 ssize_t linelen;
4676 int i, nscroll = view->nlines - 1, up = 0;
4678 switch (ch) {
4679 case '0':
4680 view->x = 0;
4681 break;
4682 case '$':
4683 view->x = MAX(view->maxx - view->ncols / 3, 0);
4684 view->count = 0;
4685 break;
4686 case KEY_RIGHT:
4687 case 'l':
4688 if (view->x + view->ncols / 3 < view->maxx)
4689 view->x += 2; /* move two columns right */
4690 else
4691 view->count = 0;
4692 break;
4693 case KEY_LEFT:
4694 case 'h':
4695 view->x -= MIN(view->x, 2); /* move two columns back */
4696 if (view->x <= 0)
4697 view->count = 0;
4698 break;
4699 case 'a':
4700 case 'w':
4701 if (ch == 'a')
4702 s->force_text_diff = !s->force_text_diff;
4703 if (ch == 'w')
4704 s->ignore_whitespace = !s->ignore_whitespace;
4705 err = reset_diff_view(view);
4706 break;
4707 case 'g':
4708 case KEY_HOME:
4709 s->first_displayed_line = 1;
4710 view->count = 0;
4711 break;
4712 case 'G':
4713 case KEY_END:
4714 view->count = 0;
4715 if (s->eof)
4716 break;
4718 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4719 s->eof = 1;
4720 break;
4721 case 'k':
4722 case KEY_UP:
4723 case CTRL('p'):
4724 if (s->first_displayed_line > 1)
4725 s->first_displayed_line--;
4726 else
4727 view->count = 0;
4728 break;
4729 case CTRL('u'):
4730 case 'u':
4731 nscroll /= 2;
4732 /* FALL THROUGH */
4733 case KEY_PPAGE:
4734 case CTRL('b'):
4735 case 'b':
4736 if (s->first_displayed_line == 1) {
4737 view->count = 0;
4738 break;
4740 i = 0;
4741 while (i++ < nscroll && s->first_displayed_line > 1)
4742 s->first_displayed_line--;
4743 break;
4744 case 'j':
4745 case KEY_DOWN:
4746 case CTRL('n'):
4747 if (!s->eof)
4748 s->first_displayed_line++;
4749 else
4750 view->count = 0;
4751 break;
4752 case CTRL('d'):
4753 case 'd':
4754 nscroll /= 2;
4755 /* FALL THROUGH */
4756 case KEY_NPAGE:
4757 case CTRL('f'):
4758 case 'f':
4759 case ' ':
4760 if (s->eof) {
4761 view->count = 0;
4762 break;
4764 i = 0;
4765 while (!s->eof && i++ < nscroll) {
4766 linelen = getline(&line, &linesize, s->f);
4767 s->first_displayed_line++;
4768 if (linelen == -1) {
4769 if (feof(s->f)) {
4770 s->eof = 1;
4771 } else
4772 err = got_ferror(s->f, GOT_ERR_IO);
4773 break;
4776 free(line);
4777 break;
4778 case '[':
4779 if (s->diff_context > 0) {
4780 s->diff_context--;
4781 s->matched_line = 0;
4782 diff_view_indicate_progress(view);
4783 err = create_diff(s);
4784 if (s->first_displayed_line + view->nlines - 1 >
4785 s->nlines) {
4786 s->first_displayed_line = 1;
4787 s->last_displayed_line = view->nlines;
4789 } else
4790 view->count = 0;
4791 break;
4792 case ']':
4793 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4794 s->diff_context++;
4795 s->matched_line = 0;
4796 diff_view_indicate_progress(view);
4797 err = create_diff(s);
4798 } else
4799 view->count = 0;
4800 break;
4801 case '<':
4802 case ',':
4803 up = 1;
4804 /* FALL THROUGH */
4805 case '>':
4806 case '.':
4807 if (s->parent_view == NULL) {
4808 view->count = 0;
4809 break;
4811 s->parent_view->count = view->count;
4813 if (s->parent_view->type == TOG_VIEW_LOG) {
4814 ls = &s->parent_view->state.log;
4815 old_selected_entry = ls->selected_entry;
4817 err = input_log_view(NULL, s->parent_view,
4818 up ? KEY_UP : KEY_DOWN);
4819 if (err)
4820 break;
4821 view->count = s->parent_view->count;
4823 if (old_selected_entry == ls->selected_entry)
4824 break;
4826 err = set_selected_commit(s, ls->selected_entry);
4827 if (err)
4828 break;
4829 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4830 struct tog_blame_view_state *bs;
4831 struct got_object_id *id, *prev_id;
4833 bs = &s->parent_view->state.blame;
4834 prev_id = get_annotation_for_line(bs->blame.lines,
4835 bs->blame.nlines, bs->last_diffed_line);
4837 err = input_blame_view(&view, s->parent_view,
4838 up ? KEY_UP : KEY_DOWN);
4839 if (err)
4840 break;
4841 view->count = s->parent_view->count;
4843 if (prev_id == NULL)
4844 break;
4845 id = get_selected_commit_id(bs->blame.lines,
4846 bs->blame.nlines, bs->first_displayed_line,
4847 bs->selected_line);
4848 if (id == NULL)
4849 break;
4851 if (!got_object_id_cmp(prev_id, id))
4852 break;
4854 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4855 if (err)
4856 break;
4858 s->first_displayed_line = 1;
4859 s->last_displayed_line = view->nlines;
4860 s->matched_line = 0;
4861 view->x = 0;
4863 diff_view_indicate_progress(view);
4864 err = create_diff(s);
4865 break;
4866 default:
4867 view->count = 0;
4868 break;
4871 return err;
4874 static const struct got_error *
4875 cmd_diff(int argc, char *argv[])
4877 const struct got_error *error = NULL;
4878 struct got_repository *repo = NULL;
4879 struct got_worktree *worktree = NULL;
4880 struct got_object_id *id1 = NULL, *id2 = NULL;
4881 char *repo_path = NULL, *cwd = NULL;
4882 char *id_str1 = NULL, *id_str2 = NULL;
4883 char *label1 = NULL, *label2 = NULL;
4884 int diff_context = 3, ignore_whitespace = 0;
4885 int ch, force_text_diff = 0;
4886 const char *errstr;
4887 struct tog_view *view;
4888 int *pack_fds = NULL;
4890 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4891 switch (ch) {
4892 case 'a':
4893 force_text_diff = 1;
4894 break;
4895 case 'C':
4896 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4897 &errstr);
4898 if (errstr != NULL)
4899 errx(1, "number of context lines is %s: %s",
4900 errstr, errstr);
4901 break;
4902 case 'r':
4903 repo_path = realpath(optarg, NULL);
4904 if (repo_path == NULL)
4905 return got_error_from_errno2("realpath",
4906 optarg);
4907 got_path_strip_trailing_slashes(repo_path);
4908 break;
4909 case 'w':
4910 ignore_whitespace = 1;
4911 break;
4912 default:
4913 usage_diff();
4914 /* NOTREACHED */
4918 argc -= optind;
4919 argv += optind;
4921 if (argc == 0) {
4922 usage_diff(); /* TODO show local worktree changes */
4923 } else if (argc == 2) {
4924 id_str1 = argv[0];
4925 id_str2 = argv[1];
4926 } else
4927 usage_diff();
4929 error = got_repo_pack_fds_open(&pack_fds);
4930 if (error)
4931 goto done;
4933 if (repo_path == NULL) {
4934 cwd = getcwd(NULL, 0);
4935 if (cwd == NULL)
4936 return got_error_from_errno("getcwd");
4937 error = got_worktree_open(&worktree, cwd);
4938 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4939 goto done;
4940 if (worktree)
4941 repo_path =
4942 strdup(got_worktree_get_repo_path(worktree));
4943 else
4944 repo_path = strdup(cwd);
4945 if (repo_path == NULL) {
4946 error = got_error_from_errno("strdup");
4947 goto done;
4951 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4952 if (error)
4953 goto done;
4955 init_curses();
4957 error = apply_unveil(got_repo_get_path(repo), NULL);
4958 if (error)
4959 goto done;
4961 error = tog_load_refs(repo, 0);
4962 if (error)
4963 goto done;
4965 error = got_repo_match_object_id(&id1, &label1, id_str1,
4966 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4967 if (error)
4968 goto done;
4970 error = got_repo_match_object_id(&id2, &label2, id_str2,
4971 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4972 if (error)
4973 goto done;
4975 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4976 if (view == NULL) {
4977 error = got_error_from_errno("view_open");
4978 goto done;
4980 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4981 ignore_whitespace, force_text_diff, NULL, repo);
4982 if (error)
4983 goto done;
4984 error = view_loop(view);
4985 done:
4986 free(label1);
4987 free(label2);
4988 free(repo_path);
4989 free(cwd);
4990 if (repo) {
4991 const struct got_error *close_err = got_repo_close(repo);
4992 if (error == NULL)
4993 error = close_err;
4995 if (worktree)
4996 got_worktree_close(worktree);
4997 if (pack_fds) {
4998 const struct got_error *pack_err =
4999 got_repo_pack_fds_close(pack_fds);
5000 if (error == NULL)
5001 error = pack_err;
5003 tog_free_refs();
5004 return error;
5007 __dead static void
5008 usage_blame(void)
5010 endwin();
5011 fprintf(stderr,
5012 "usage: %s blame [-c commit] [-r repository-path] path\n",
5013 getprogname());
5014 exit(1);
5017 struct tog_blame_line {
5018 int annotated;
5019 struct got_object_id *id;
5022 static const struct got_error *
5023 draw_blame(struct tog_view *view)
5025 struct tog_blame_view_state *s = &view->state.blame;
5026 struct tog_blame *blame = &s->blame;
5027 regmatch_t *regmatch = &view->regmatch;
5028 const struct got_error *err;
5029 int lineno = 0, nprinted = 0;
5030 char *line = NULL;
5031 size_t linesize = 0;
5032 ssize_t linelen;
5033 wchar_t *wline;
5034 int width;
5035 struct tog_blame_line *blame_line;
5036 struct got_object_id *prev_id = NULL;
5037 char *id_str;
5038 struct tog_color *tc;
5040 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5041 if (err)
5042 return err;
5044 rewind(blame->f);
5045 werase(view->window);
5047 if (asprintf(&line, "commit %s", id_str) == -1) {
5048 err = got_error_from_errno("asprintf");
5049 free(id_str);
5050 return err;
5053 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5054 free(line);
5055 line = NULL;
5056 if (err)
5057 return err;
5058 if (view_needs_focus_indication(view))
5059 wstandout(view->window);
5060 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5061 if (tc)
5062 wattr_on(view->window,
5063 COLOR_PAIR(tc->colorpair), NULL);
5064 waddwstr(view->window, wline);
5065 if (tc)
5066 wattr_off(view->window,
5067 COLOR_PAIR(tc->colorpair), NULL);
5068 if (view_needs_focus_indication(view))
5069 wstandend(view->window);
5070 free(wline);
5071 wline = NULL;
5072 if (width < view->ncols - 1)
5073 waddch(view->window, '\n');
5075 if (asprintf(&line, "[%d/%d] %s%s",
5076 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5077 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5078 free(id_str);
5079 return got_error_from_errno("asprintf");
5081 free(id_str);
5082 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5083 free(line);
5084 line = NULL;
5085 if (err)
5086 return err;
5087 waddwstr(view->window, wline);
5088 free(wline);
5089 wline = NULL;
5090 if (width < view->ncols - 1)
5091 waddch(view->window, '\n');
5093 s->eof = 0;
5094 view->maxx = 0;
5095 while (nprinted < view->nlines - 2) {
5096 linelen = getline(&line, &linesize, blame->f);
5097 if (linelen == -1) {
5098 if (feof(blame->f)) {
5099 s->eof = 1;
5100 break;
5102 free(line);
5103 return got_ferror(blame->f, GOT_ERR_IO);
5105 if (++lineno < s->first_displayed_line)
5106 continue;
5108 /* Set view->maxx based on full line length. */
5109 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5110 if (err) {
5111 free(line);
5112 return err;
5114 free(wline);
5115 wline = NULL;
5116 view->maxx = MAX(view->maxx, width);
5118 if (nprinted == s->selected_line - 1)
5119 wstandout(view->window);
5121 if (blame->nlines > 0) {
5122 blame_line = &blame->lines[lineno - 1];
5123 if (blame_line->annotated && prev_id &&
5124 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5125 !(nprinted == s->selected_line - 1)) {
5126 waddstr(view->window, " ");
5127 } else if (blame_line->annotated) {
5128 char *id_str;
5129 err = got_object_id_str(&id_str,
5130 blame_line->id);
5131 if (err) {
5132 free(line);
5133 return err;
5135 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5136 if (tc)
5137 wattr_on(view->window,
5138 COLOR_PAIR(tc->colorpair), NULL);
5139 wprintw(view->window, "%.8s", id_str);
5140 if (tc)
5141 wattr_off(view->window,
5142 COLOR_PAIR(tc->colorpair), NULL);
5143 free(id_str);
5144 prev_id = blame_line->id;
5145 } else {
5146 waddstr(view->window, "........");
5147 prev_id = NULL;
5149 } else {
5150 waddstr(view->window, "........");
5151 prev_id = NULL;
5154 if (nprinted == s->selected_line - 1)
5155 wstandend(view->window);
5156 waddstr(view->window, " ");
5158 if (view->ncols <= 9) {
5159 width = 9;
5160 } else if (s->first_displayed_line + nprinted ==
5161 s->matched_line &&
5162 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5163 err = add_matched_line(&width, line, view->ncols - 9, 9,
5164 view->window, view->x, regmatch);
5165 if (err) {
5166 free(line);
5167 return err;
5169 width += 9;
5170 } else {
5171 int skip;
5172 err = format_line(&wline, &width, &skip, line,
5173 view->x, view->ncols - 9, 9, 1);
5174 if (err) {
5175 free(line);
5176 return err;
5178 waddwstr(view->window, &wline[skip]);
5179 width += 9;
5180 free(wline);
5181 wline = NULL;
5184 if (width <= view->ncols - 1)
5185 waddch(view->window, '\n');
5186 if (++nprinted == 1)
5187 s->first_displayed_line = lineno;
5189 free(line);
5190 s->last_displayed_line = lineno;
5192 view_border(view);
5194 return NULL;
5197 static const struct got_error *
5198 blame_cb(void *arg, int nlines, int lineno,
5199 struct got_commit_object *commit, struct got_object_id *id)
5201 const struct got_error *err = NULL;
5202 struct tog_blame_cb_args *a = arg;
5203 struct tog_blame_line *line;
5204 int errcode;
5206 if (nlines != a->nlines ||
5207 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5208 return got_error(GOT_ERR_RANGE);
5210 errcode = pthread_mutex_lock(&tog_mutex);
5211 if (errcode)
5212 return got_error_set_errno(errcode, "pthread_mutex_lock");
5214 if (*a->quit) { /* user has quit the blame view */
5215 err = got_error(GOT_ERR_ITER_COMPLETED);
5216 goto done;
5219 if (lineno == -1)
5220 goto done; /* no change in this commit */
5222 line = &a->lines[lineno - 1];
5223 if (line->annotated)
5224 goto done;
5226 line->id = got_object_id_dup(id);
5227 if (line->id == NULL) {
5228 err = got_error_from_errno("got_object_id_dup");
5229 goto done;
5231 line->annotated = 1;
5232 done:
5233 errcode = pthread_mutex_unlock(&tog_mutex);
5234 if (errcode)
5235 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5236 return err;
5239 static void *
5240 blame_thread(void *arg)
5242 const struct got_error *err, *close_err;
5243 struct tog_blame_thread_args *ta = arg;
5244 struct tog_blame_cb_args *a = ta->cb_args;
5245 int errcode, fd1 = -1, fd2 = -1;
5246 FILE *f1 = NULL, *f2 = NULL;
5248 fd1 = got_opentempfd();
5249 if (fd1 == -1)
5250 return (void *)got_error_from_errno("got_opentempfd");
5252 fd2 = got_opentempfd();
5253 if (fd2 == -1) {
5254 err = got_error_from_errno("got_opentempfd");
5255 goto done;
5258 f1 = got_opentemp();
5259 if (f1 == NULL) {
5260 err = (void *)got_error_from_errno("got_opentemp");
5261 goto done;
5263 f2 = got_opentemp();
5264 if (f2 == NULL) {
5265 err = (void *)got_error_from_errno("got_opentemp");
5266 goto done;
5269 err = block_signals_used_by_main_thread();
5270 if (err)
5271 goto done;
5273 err = got_blame(ta->path, a->commit_id, ta->repo,
5274 tog_diff_algo, blame_cb, ta->cb_args,
5275 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5276 if (err && err->code == GOT_ERR_CANCELLED)
5277 err = NULL;
5279 errcode = pthread_mutex_lock(&tog_mutex);
5280 if (errcode) {
5281 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5282 goto done;
5285 close_err = got_repo_close(ta->repo);
5286 if (err == NULL)
5287 err = close_err;
5288 ta->repo = NULL;
5289 *ta->complete = 1;
5291 errcode = pthread_mutex_unlock(&tog_mutex);
5292 if (errcode && err == NULL)
5293 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5295 done:
5296 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5297 err = got_error_from_errno("close");
5298 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5299 err = got_error_from_errno("close");
5300 if (f1 && fclose(f1) == EOF && err == NULL)
5301 err = got_error_from_errno("fclose");
5302 if (f2 && fclose(f2) == EOF && err == NULL)
5303 err = got_error_from_errno("fclose");
5305 return (void *)err;
5308 static struct got_object_id *
5309 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5310 int first_displayed_line, int selected_line)
5312 struct tog_blame_line *line;
5314 if (nlines <= 0)
5315 return NULL;
5317 line = &lines[first_displayed_line - 1 + selected_line - 1];
5318 if (!line->annotated)
5319 return NULL;
5321 return line->id;
5324 static struct got_object_id *
5325 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5326 int lineno)
5328 struct tog_blame_line *line;
5330 if (nlines <= 0 || lineno >= nlines)
5331 return NULL;
5333 line = &lines[lineno - 1];
5334 if (!line->annotated)
5335 return NULL;
5337 return line->id;
5340 static const struct got_error *
5341 stop_blame(struct tog_blame *blame)
5343 const struct got_error *err = NULL;
5344 int i;
5346 if (blame->thread) {
5347 int errcode;
5348 errcode = pthread_mutex_unlock(&tog_mutex);
5349 if (errcode)
5350 return got_error_set_errno(errcode,
5351 "pthread_mutex_unlock");
5352 errcode = pthread_join(blame->thread, (void **)&err);
5353 if (errcode)
5354 return got_error_set_errno(errcode, "pthread_join");
5355 errcode = pthread_mutex_lock(&tog_mutex);
5356 if (errcode)
5357 return got_error_set_errno(errcode,
5358 "pthread_mutex_lock");
5359 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5360 err = NULL;
5361 blame->thread = NULL;
5363 if (blame->thread_args.repo) {
5364 const struct got_error *close_err;
5365 close_err = got_repo_close(blame->thread_args.repo);
5366 if (err == NULL)
5367 err = close_err;
5368 blame->thread_args.repo = NULL;
5370 if (blame->f) {
5371 if (fclose(blame->f) == EOF && err == NULL)
5372 err = got_error_from_errno("fclose");
5373 blame->f = NULL;
5375 if (blame->lines) {
5376 for (i = 0; i < blame->nlines; i++)
5377 free(blame->lines[i].id);
5378 free(blame->lines);
5379 blame->lines = NULL;
5381 free(blame->cb_args.commit_id);
5382 blame->cb_args.commit_id = NULL;
5383 if (blame->pack_fds) {
5384 const struct got_error *pack_err =
5385 got_repo_pack_fds_close(blame->pack_fds);
5386 if (err == NULL)
5387 err = pack_err;
5388 blame->pack_fds = NULL;
5390 return err;
5393 static const struct got_error *
5394 cancel_blame_view(void *arg)
5396 const struct got_error *err = NULL;
5397 int *done = arg;
5398 int errcode;
5400 errcode = pthread_mutex_lock(&tog_mutex);
5401 if (errcode)
5402 return got_error_set_errno(errcode,
5403 "pthread_mutex_unlock");
5405 if (*done)
5406 err = got_error(GOT_ERR_CANCELLED);
5408 errcode = pthread_mutex_unlock(&tog_mutex);
5409 if (errcode)
5410 return got_error_set_errno(errcode,
5411 "pthread_mutex_lock");
5413 return err;
5416 static const struct got_error *
5417 run_blame(struct tog_view *view)
5419 struct tog_blame_view_state *s = &view->state.blame;
5420 struct tog_blame *blame = &s->blame;
5421 const struct got_error *err = NULL;
5422 struct got_commit_object *commit = NULL;
5423 struct got_blob_object *blob = NULL;
5424 struct got_repository *thread_repo = NULL;
5425 struct got_object_id *obj_id = NULL;
5426 int obj_type, fd = -1;
5427 int *pack_fds = NULL;
5429 err = got_object_open_as_commit(&commit, s->repo,
5430 &s->blamed_commit->id);
5431 if (err)
5432 return err;
5434 fd = got_opentempfd();
5435 if (fd == -1) {
5436 err = got_error_from_errno("got_opentempfd");
5437 goto done;
5440 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5441 if (err)
5442 goto done;
5444 err = got_object_get_type(&obj_type, s->repo, obj_id);
5445 if (err)
5446 goto done;
5448 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5449 err = got_error(GOT_ERR_OBJ_TYPE);
5450 goto done;
5453 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5454 if (err)
5455 goto done;
5456 blame->f = got_opentemp();
5457 if (blame->f == NULL) {
5458 err = got_error_from_errno("got_opentemp");
5459 goto done;
5461 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5462 &blame->line_offsets, blame->f, blob);
5463 if (err)
5464 goto done;
5465 if (blame->nlines == 0) {
5466 s->blame_complete = 1;
5467 goto done;
5470 /* Don't include \n at EOF in the blame line count. */
5471 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5472 blame->nlines--;
5474 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5475 if (blame->lines == NULL) {
5476 err = got_error_from_errno("calloc");
5477 goto done;
5480 err = got_repo_pack_fds_open(&pack_fds);
5481 if (err)
5482 goto done;
5483 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5484 pack_fds);
5485 if (err)
5486 goto done;
5488 blame->pack_fds = pack_fds;
5489 blame->cb_args.view = view;
5490 blame->cb_args.lines = blame->lines;
5491 blame->cb_args.nlines = blame->nlines;
5492 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5493 if (blame->cb_args.commit_id == NULL) {
5494 err = got_error_from_errno("got_object_id_dup");
5495 goto done;
5497 blame->cb_args.quit = &s->done;
5499 blame->thread_args.path = s->path;
5500 blame->thread_args.repo = thread_repo;
5501 blame->thread_args.cb_args = &blame->cb_args;
5502 blame->thread_args.complete = &s->blame_complete;
5503 blame->thread_args.cancel_cb = cancel_blame_view;
5504 blame->thread_args.cancel_arg = &s->done;
5505 s->blame_complete = 0;
5507 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5508 s->first_displayed_line = 1;
5509 s->last_displayed_line = view->nlines;
5510 s->selected_line = 1;
5512 s->matched_line = 0;
5514 done:
5515 if (commit)
5516 got_object_commit_close(commit);
5517 if (fd != -1 && close(fd) == -1 && err == NULL)
5518 err = got_error_from_errno("close");
5519 if (blob)
5520 got_object_blob_close(blob);
5521 free(obj_id);
5522 if (err)
5523 stop_blame(blame);
5524 return err;
5527 static const struct got_error *
5528 open_blame_view(struct tog_view *view, char *path,
5529 struct got_object_id *commit_id, struct got_repository *repo)
5531 const struct got_error *err = NULL;
5532 struct tog_blame_view_state *s = &view->state.blame;
5534 STAILQ_INIT(&s->blamed_commits);
5536 s->path = strdup(path);
5537 if (s->path == NULL)
5538 return got_error_from_errno("strdup");
5540 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5541 if (err) {
5542 free(s->path);
5543 return err;
5546 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5547 s->first_displayed_line = 1;
5548 s->last_displayed_line = view->nlines;
5549 s->selected_line = 1;
5550 s->blame_complete = 0;
5551 s->repo = repo;
5552 s->commit_id = commit_id;
5553 memset(&s->blame, 0, sizeof(s->blame));
5555 STAILQ_INIT(&s->colors);
5556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5557 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5558 get_color_value("TOG_COLOR_COMMIT"));
5559 if (err)
5560 return err;
5563 view->show = show_blame_view;
5564 view->input = input_blame_view;
5565 view->reset = reset_blame_view;
5566 view->close = close_blame_view;
5567 view->search_start = search_start_blame_view;
5568 view->search_next = search_next_blame_view;
5570 return run_blame(view);
5573 static const struct got_error *
5574 close_blame_view(struct tog_view *view)
5576 const struct got_error *err = NULL;
5577 struct tog_blame_view_state *s = &view->state.blame;
5579 if (s->blame.thread)
5580 err = stop_blame(&s->blame);
5582 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5583 struct got_object_qid *blamed_commit;
5584 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5585 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5586 got_object_qid_free(blamed_commit);
5589 free(s->path);
5590 free_colors(&s->colors);
5591 return err;
5594 static const struct got_error *
5595 search_start_blame_view(struct tog_view *view)
5597 struct tog_blame_view_state *s = &view->state.blame;
5599 s->matched_line = 0;
5600 return NULL;
5603 static const struct got_error *
5604 search_next_blame_view(struct tog_view *view)
5606 struct tog_blame_view_state *s = &view->state.blame;
5607 const struct got_error *err = NULL;
5608 int lineno;
5609 char *line = NULL;
5610 size_t linesize = 0;
5611 ssize_t linelen;
5613 if (!view->searching) {
5614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5615 return NULL;
5618 if (s->matched_line) {
5619 if (view->searching == TOG_SEARCH_FORWARD)
5620 lineno = s->matched_line + 1;
5621 else
5622 lineno = s->matched_line - 1;
5623 } else
5624 lineno = s->first_displayed_line - 1 + s->selected_line;
5626 while (1) {
5627 off_t offset;
5629 if (lineno <= 0 || lineno > s->blame.nlines) {
5630 if (s->matched_line == 0) {
5631 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5632 break;
5635 if (view->searching == TOG_SEARCH_FORWARD)
5636 lineno = 1;
5637 else
5638 lineno = s->blame.nlines;
5641 offset = s->blame.line_offsets[lineno - 1];
5642 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5643 free(line);
5644 return got_error_from_errno("fseeko");
5646 linelen = getline(&line, &linesize, s->blame.f);
5647 if (linelen != -1) {
5648 char *exstr;
5649 err = expand_tab(&exstr, line);
5650 if (err)
5651 break;
5652 if (match_line(exstr, &view->regex, 1,
5653 &view->regmatch)) {
5654 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5655 s->matched_line = lineno;
5656 free(exstr);
5657 break;
5659 free(exstr);
5661 if (view->searching == TOG_SEARCH_FORWARD)
5662 lineno++;
5663 else
5664 lineno--;
5666 free(line);
5668 if (s->matched_line) {
5669 s->first_displayed_line = s->matched_line;
5670 s->selected_line = 1;
5673 return err;
5676 static const struct got_error *
5677 show_blame_view(struct tog_view *view)
5679 const struct got_error *err = NULL;
5680 struct tog_blame_view_state *s = &view->state.blame;
5681 int errcode;
5683 if (s->blame.thread == NULL && !s->blame_complete) {
5684 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5685 &s->blame.thread_args);
5686 if (errcode)
5687 return got_error_set_errno(errcode, "pthread_create");
5689 halfdelay(1); /* fast refresh while annotating */
5692 if (s->blame_complete)
5693 halfdelay(10); /* disable fast refresh */
5695 err = draw_blame(view);
5697 view_border(view);
5698 return err;
5701 static const struct got_error *
5702 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5704 const struct got_error *err = NULL, *thread_err = NULL;
5705 struct tog_view *diff_view;
5706 struct tog_blame_view_state *s = &view->state.blame;
5707 int eos, nscroll, begin_y = 0, begin_x = 0;
5709 eos = nscroll = view->nlines - 2;
5710 if (view_is_hsplit_top(view))
5711 --eos; /* border */
5713 switch (ch) {
5714 case '0':
5715 view->x = 0;
5716 break;
5717 case '$':
5718 view->x = MAX(view->maxx - view->ncols / 3, 0);
5719 view->count = 0;
5720 break;
5721 case KEY_RIGHT:
5722 case 'l':
5723 if (view->x + view->ncols / 3 < view->maxx)
5724 view->x += 2; /* move two columns right */
5725 else
5726 view->count = 0;
5727 break;
5728 case KEY_LEFT:
5729 case 'h':
5730 view->x -= MIN(view->x, 2); /* move two columns back */
5731 if (view->x <= 0)
5732 view->count = 0;
5733 break;
5734 case 'q':
5735 s->done = 1;
5736 break;
5737 case 'g':
5738 case KEY_HOME:
5739 s->selected_line = 1;
5740 s->first_displayed_line = 1;
5741 view->count = 0;
5742 break;
5743 case 'G':
5744 case KEY_END:
5745 if (s->blame.nlines < eos) {
5746 s->selected_line = s->blame.nlines;
5747 s->first_displayed_line = 1;
5748 } else {
5749 s->selected_line = eos;
5750 s->first_displayed_line = s->blame.nlines - (eos - 1);
5752 view->count = 0;
5753 break;
5754 case 'k':
5755 case KEY_UP:
5756 case CTRL('p'):
5757 if (s->selected_line > 1)
5758 s->selected_line--;
5759 else if (s->selected_line == 1 &&
5760 s->first_displayed_line > 1)
5761 s->first_displayed_line--;
5762 else
5763 view->count = 0;
5764 break;
5765 case CTRL('u'):
5766 case 'u':
5767 nscroll /= 2;
5768 /* FALL THROUGH */
5769 case KEY_PPAGE:
5770 case CTRL('b'):
5771 case 'b':
5772 if (s->first_displayed_line == 1) {
5773 if (view->count > 1)
5774 nscroll += nscroll;
5775 s->selected_line = MAX(1, s->selected_line - nscroll);
5776 view->count = 0;
5777 break;
5779 if (s->first_displayed_line > nscroll)
5780 s->first_displayed_line -= nscroll;
5781 else
5782 s->first_displayed_line = 1;
5783 break;
5784 case 'j':
5785 case KEY_DOWN:
5786 case CTRL('n'):
5787 if (s->selected_line < eos && s->first_displayed_line +
5788 s->selected_line <= s->blame.nlines)
5789 s->selected_line++;
5790 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5791 s->first_displayed_line++;
5792 else
5793 view->count = 0;
5794 break;
5795 case 'c':
5796 case 'p': {
5797 struct got_object_id *id = NULL;
5799 view->count = 0;
5800 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5801 s->first_displayed_line, s->selected_line);
5802 if (id == NULL)
5803 break;
5804 if (ch == 'p') {
5805 struct got_commit_object *commit, *pcommit;
5806 struct got_object_qid *pid;
5807 struct got_object_id *blob_id = NULL;
5808 int obj_type;
5809 err = got_object_open_as_commit(&commit,
5810 s->repo, id);
5811 if (err)
5812 break;
5813 pid = STAILQ_FIRST(
5814 got_object_commit_get_parent_ids(commit));
5815 if (pid == NULL) {
5816 got_object_commit_close(commit);
5817 break;
5819 /* Check if path history ends here. */
5820 err = got_object_open_as_commit(&pcommit,
5821 s->repo, &pid->id);
5822 if (err)
5823 break;
5824 err = got_object_id_by_path(&blob_id, s->repo,
5825 pcommit, s->path);
5826 got_object_commit_close(pcommit);
5827 if (err) {
5828 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5829 err = NULL;
5830 got_object_commit_close(commit);
5831 break;
5833 err = got_object_get_type(&obj_type, s->repo,
5834 blob_id);
5835 free(blob_id);
5836 /* Can't blame non-blob type objects. */
5837 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5838 got_object_commit_close(commit);
5839 break;
5841 err = got_object_qid_alloc(&s->blamed_commit,
5842 &pid->id);
5843 got_object_commit_close(commit);
5844 } else {
5845 if (got_object_id_cmp(id,
5846 &s->blamed_commit->id) == 0)
5847 break;
5848 err = got_object_qid_alloc(&s->blamed_commit,
5849 id);
5851 if (err)
5852 break;
5853 s->done = 1;
5854 thread_err = stop_blame(&s->blame);
5855 s->done = 0;
5856 if (thread_err)
5857 break;
5858 STAILQ_INSERT_HEAD(&s->blamed_commits,
5859 s->blamed_commit, entry);
5860 err = run_blame(view);
5861 if (err)
5862 break;
5863 break;
5865 case 'C': {
5866 struct got_object_qid *first;
5868 view->count = 0;
5869 first = STAILQ_FIRST(&s->blamed_commits);
5870 if (!got_object_id_cmp(&first->id, s->commit_id))
5871 break;
5872 s->done = 1;
5873 thread_err = stop_blame(&s->blame);
5874 s->done = 0;
5875 if (thread_err)
5876 break;
5877 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5878 got_object_qid_free(s->blamed_commit);
5879 s->blamed_commit =
5880 STAILQ_FIRST(&s->blamed_commits);
5881 err = run_blame(view);
5882 if (err)
5883 break;
5884 break;
5886 case KEY_ENTER:
5887 case '\r': {
5888 struct got_object_id *id = NULL;
5889 struct got_object_qid *pid;
5890 struct got_commit_object *commit = NULL;
5892 view->count = 0;
5893 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5894 s->first_displayed_line, s->selected_line);
5895 if (id == NULL)
5896 break;
5897 err = got_object_open_as_commit(&commit, s->repo, id);
5898 if (err)
5899 break;
5900 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5901 if (*new_view) {
5902 /* traversed from diff view, release diff resources */
5903 err = close_diff_view(*new_view);
5904 if (err)
5905 break;
5906 diff_view = *new_view;
5907 } else {
5908 if (view_is_parent_view(view))
5909 view_get_split(view, &begin_y, &begin_x);
5911 diff_view = view_open(0, 0, begin_y, begin_x,
5912 TOG_VIEW_DIFF);
5913 if (diff_view == NULL) {
5914 got_object_commit_close(commit);
5915 err = got_error_from_errno("view_open");
5916 break;
5919 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5920 id, NULL, NULL, 3, 0, 0, view, s->repo);
5921 got_object_commit_close(commit);
5922 if (err) {
5923 view_close(diff_view);
5924 break;
5926 s->last_diffed_line = s->first_displayed_line - 1 +
5927 s->selected_line;
5928 if (*new_view)
5929 break; /* still open from active diff view */
5930 if (view_is_parent_view(view) &&
5931 view->mode == TOG_VIEW_SPLIT_HRZN) {
5932 err = view_init_hsplit(view, begin_y);
5933 if (err)
5934 break;
5937 view->focussed = 0;
5938 diff_view->focussed = 1;
5939 diff_view->mode = view->mode;
5940 diff_view->nlines = view->lines - begin_y;
5941 if (view_is_parent_view(view)) {
5942 view_transfer_size(diff_view, view);
5943 err = view_close_child(view);
5944 if (err)
5945 break;
5946 err = view_set_child(view, diff_view);
5947 if (err)
5948 break;
5949 view->focus_child = 1;
5950 } else
5951 *new_view = diff_view;
5952 if (err)
5953 break;
5954 break;
5956 case CTRL('d'):
5957 case 'd':
5958 nscroll /= 2;
5959 /* FALL THROUGH */
5960 case KEY_NPAGE:
5961 case CTRL('f'):
5962 case 'f':
5963 case ' ':
5964 if (s->last_displayed_line >= s->blame.nlines &&
5965 s->selected_line >= MIN(s->blame.nlines,
5966 view->nlines - 2)) {
5967 view->count = 0;
5968 break;
5970 if (s->last_displayed_line >= s->blame.nlines &&
5971 s->selected_line < view->nlines - 2) {
5972 s->selected_line +=
5973 MIN(nscroll, s->last_displayed_line -
5974 s->first_displayed_line - s->selected_line + 1);
5976 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5977 s->first_displayed_line += nscroll;
5978 else
5979 s->first_displayed_line =
5980 s->blame.nlines - (view->nlines - 3);
5981 break;
5982 case KEY_RESIZE:
5983 if (s->selected_line > view->nlines - 2) {
5984 s->selected_line = MIN(s->blame.nlines,
5985 view->nlines - 2);
5987 break;
5988 default:
5989 view->count = 0;
5990 break;
5992 return thread_err ? thread_err : err;
5995 static const struct got_error *
5996 reset_blame_view(struct tog_view *view)
5998 const struct got_error *err;
5999 struct tog_blame_view_state *s = &view->state.blame;
6001 view->count = 0;
6002 s->done = 1;
6003 err = stop_blame(&s->blame);
6004 s->done = 0;
6005 if (err)
6006 return err;
6007 return run_blame(view);
6010 static const struct got_error *
6011 cmd_blame(int argc, char *argv[])
6013 const struct got_error *error;
6014 struct got_repository *repo = NULL;
6015 struct got_worktree *worktree = NULL;
6016 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6017 char *link_target = NULL;
6018 struct got_object_id *commit_id = NULL;
6019 struct got_commit_object *commit = NULL;
6020 char *commit_id_str = NULL;
6021 int ch;
6022 struct tog_view *view;
6023 int *pack_fds = NULL;
6025 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6026 switch (ch) {
6027 case 'c':
6028 commit_id_str = optarg;
6029 break;
6030 case 'r':
6031 repo_path = realpath(optarg, NULL);
6032 if (repo_path == NULL)
6033 return got_error_from_errno2("realpath",
6034 optarg);
6035 break;
6036 default:
6037 usage_blame();
6038 /* NOTREACHED */
6042 argc -= optind;
6043 argv += optind;
6045 if (argc != 1)
6046 usage_blame();
6048 error = got_repo_pack_fds_open(&pack_fds);
6049 if (error != NULL)
6050 goto done;
6052 if (repo_path == NULL) {
6053 cwd = getcwd(NULL, 0);
6054 if (cwd == NULL)
6055 return got_error_from_errno("getcwd");
6056 error = got_worktree_open(&worktree, cwd);
6057 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6058 goto done;
6059 if (worktree)
6060 repo_path =
6061 strdup(got_worktree_get_repo_path(worktree));
6062 else
6063 repo_path = strdup(cwd);
6064 if (repo_path == NULL) {
6065 error = got_error_from_errno("strdup");
6066 goto done;
6070 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6071 if (error != NULL)
6072 goto done;
6074 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6075 worktree);
6076 if (error)
6077 goto done;
6079 init_curses();
6081 error = apply_unveil(got_repo_get_path(repo), NULL);
6082 if (error)
6083 goto done;
6085 error = tog_load_refs(repo, 0);
6086 if (error)
6087 goto done;
6089 if (commit_id_str == NULL) {
6090 struct got_reference *head_ref;
6091 error = got_ref_open(&head_ref, repo, worktree ?
6092 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6093 if (error != NULL)
6094 goto done;
6095 error = got_ref_resolve(&commit_id, repo, head_ref);
6096 got_ref_close(head_ref);
6097 } else {
6098 error = got_repo_match_object_id(&commit_id, NULL,
6099 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6101 if (error != NULL)
6102 goto done;
6104 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6105 if (view == NULL) {
6106 error = got_error_from_errno("view_open");
6107 goto done;
6110 error = got_object_open_as_commit(&commit, repo, commit_id);
6111 if (error)
6112 goto done;
6114 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6115 commit, repo);
6116 if (error)
6117 goto done;
6119 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6120 commit_id, repo);
6121 if (error)
6122 goto done;
6123 if (worktree) {
6124 /* Release work tree lock. */
6125 got_worktree_close(worktree);
6126 worktree = NULL;
6128 error = view_loop(view);
6129 done:
6130 free(repo_path);
6131 free(in_repo_path);
6132 free(link_target);
6133 free(cwd);
6134 free(commit_id);
6135 if (commit)
6136 got_object_commit_close(commit);
6137 if (worktree)
6138 got_worktree_close(worktree);
6139 if (repo) {
6140 const struct got_error *close_err = got_repo_close(repo);
6141 if (error == NULL)
6142 error = close_err;
6144 if (pack_fds) {
6145 const struct got_error *pack_err =
6146 got_repo_pack_fds_close(pack_fds);
6147 if (error == NULL)
6148 error = pack_err;
6150 tog_free_refs();
6151 return error;
6154 static const struct got_error *
6155 draw_tree_entries(struct tog_view *view, const char *parent_path)
6157 struct tog_tree_view_state *s = &view->state.tree;
6158 const struct got_error *err = NULL;
6159 struct got_tree_entry *te;
6160 wchar_t *wline;
6161 struct tog_color *tc;
6162 int width, n, i, nentries;
6163 int limit = view->nlines;
6165 s->ndisplayed = 0;
6166 if (view_is_hsplit_top(view))
6167 --limit; /* border */
6169 werase(view->window);
6171 if (limit == 0)
6172 return NULL;
6174 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6175 0, 0);
6176 if (err)
6177 return err;
6178 if (view_needs_focus_indication(view))
6179 wstandout(view->window);
6180 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6181 if (tc)
6182 wattr_on(view->window,
6183 COLOR_PAIR(tc->colorpair), NULL);
6184 waddwstr(view->window, wline);
6185 if (tc)
6186 wattr_off(view->window,
6187 COLOR_PAIR(tc->colorpair), NULL);
6188 if (view_needs_focus_indication(view))
6189 wstandend(view->window);
6190 free(wline);
6191 wline = NULL;
6192 if (width < view->ncols - 1)
6193 waddch(view->window, '\n');
6194 if (--limit <= 0)
6195 return NULL;
6196 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6197 0, 0);
6198 if (err)
6199 return err;
6200 waddwstr(view->window, wline);
6201 free(wline);
6202 wline = NULL;
6203 if (width < view->ncols - 1)
6204 waddch(view->window, '\n');
6205 if (--limit <= 0)
6206 return NULL;
6207 waddch(view->window, '\n');
6208 if (--limit <= 0)
6209 return NULL;
6211 if (s->first_displayed_entry == NULL) {
6212 te = got_object_tree_get_first_entry(s->tree);
6213 if (s->selected == 0) {
6214 if (view->focussed)
6215 wstandout(view->window);
6216 s->selected_entry = NULL;
6218 waddstr(view->window, " ..\n"); /* parent directory */
6219 if (s->selected == 0 && view->focussed)
6220 wstandend(view->window);
6221 s->ndisplayed++;
6222 if (--limit <= 0)
6223 return NULL;
6224 n = 1;
6225 } else {
6226 n = 0;
6227 te = s->first_displayed_entry;
6230 nentries = got_object_tree_get_nentries(s->tree);
6231 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6232 char *line = NULL, *id_str = NULL, *link_target = NULL;
6233 const char *modestr = "";
6234 mode_t mode;
6236 te = got_object_tree_get_entry(s->tree, i);
6237 mode = got_tree_entry_get_mode(te);
6239 if (s->show_ids) {
6240 err = got_object_id_str(&id_str,
6241 got_tree_entry_get_id(te));
6242 if (err)
6243 return got_error_from_errno(
6244 "got_object_id_str");
6246 if (got_object_tree_entry_is_submodule(te))
6247 modestr = "$";
6248 else if (S_ISLNK(mode)) {
6249 int i;
6251 err = got_tree_entry_get_symlink_target(&link_target,
6252 te, s->repo);
6253 if (err) {
6254 free(id_str);
6255 return err;
6257 for (i = 0; i < strlen(link_target); i++) {
6258 if (!isprint((unsigned char)link_target[i]))
6259 link_target[i] = '?';
6261 modestr = "@";
6263 else if (S_ISDIR(mode))
6264 modestr = "/";
6265 else if (mode & S_IXUSR)
6266 modestr = "*";
6267 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6268 got_tree_entry_get_name(te), modestr,
6269 link_target ? " -> ": "",
6270 link_target ? link_target : "") == -1) {
6271 free(id_str);
6272 free(link_target);
6273 return got_error_from_errno("asprintf");
6275 free(id_str);
6276 free(link_target);
6277 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6278 0, 0);
6279 if (err) {
6280 free(line);
6281 break;
6283 if (n == s->selected) {
6284 if (view->focussed)
6285 wstandout(view->window);
6286 s->selected_entry = te;
6288 tc = match_color(&s->colors, line);
6289 if (tc)
6290 wattr_on(view->window,
6291 COLOR_PAIR(tc->colorpair), NULL);
6292 waddwstr(view->window, wline);
6293 if (tc)
6294 wattr_off(view->window,
6295 COLOR_PAIR(tc->colorpair), NULL);
6296 if (width < view->ncols - 1)
6297 waddch(view->window, '\n');
6298 if (n == s->selected && view->focussed)
6299 wstandend(view->window);
6300 free(line);
6301 free(wline);
6302 wline = NULL;
6303 n++;
6304 s->ndisplayed++;
6305 s->last_displayed_entry = te;
6306 if (--limit <= 0)
6307 break;
6310 return err;
6313 static void
6314 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6316 struct got_tree_entry *te;
6317 int isroot = s->tree == s->root;
6318 int i = 0;
6320 if (s->first_displayed_entry == NULL)
6321 return;
6323 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6324 while (i++ < maxscroll) {
6325 if (te == NULL) {
6326 if (!isroot)
6327 s->first_displayed_entry = NULL;
6328 break;
6330 s->first_displayed_entry = te;
6331 te = got_tree_entry_get_prev(s->tree, te);
6335 static const struct got_error *
6336 tree_scroll_down(struct tog_view *view, int maxscroll)
6338 struct tog_tree_view_state *s = &view->state.tree;
6339 struct got_tree_entry *next, *last;
6340 int n = 0;
6342 if (s->first_displayed_entry)
6343 next = got_tree_entry_get_next(s->tree,
6344 s->first_displayed_entry);
6345 else
6346 next = got_object_tree_get_first_entry(s->tree);
6348 last = s->last_displayed_entry;
6349 while (next && n++ < maxscroll) {
6350 if (last)
6351 last = got_tree_entry_get_next(s->tree, last);
6352 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6353 s->first_displayed_entry = next;
6354 next = got_tree_entry_get_next(s->tree, next);
6358 return NULL;
6361 static const struct got_error *
6362 tree_entry_path(char **path, struct tog_parent_trees *parents,
6363 struct got_tree_entry *te)
6365 const struct got_error *err = NULL;
6366 struct tog_parent_tree *pt;
6367 size_t len = 2; /* for leading slash and NUL */
6369 TAILQ_FOREACH(pt, parents, entry)
6370 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6371 + 1 /* slash */;
6372 if (te)
6373 len += strlen(got_tree_entry_get_name(te));
6375 *path = calloc(1, len);
6376 if (path == NULL)
6377 return got_error_from_errno("calloc");
6379 (*path)[0] = '/';
6380 pt = TAILQ_LAST(parents, tog_parent_trees);
6381 while (pt) {
6382 const char *name = got_tree_entry_get_name(pt->selected_entry);
6383 if (strlcat(*path, name, len) >= len) {
6384 err = got_error(GOT_ERR_NO_SPACE);
6385 goto done;
6387 if (strlcat(*path, "/", len) >= len) {
6388 err = got_error(GOT_ERR_NO_SPACE);
6389 goto done;
6391 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6393 if (te) {
6394 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6395 err = got_error(GOT_ERR_NO_SPACE);
6396 goto done;
6399 done:
6400 if (err) {
6401 free(*path);
6402 *path = NULL;
6404 return err;
6407 static const struct got_error *
6408 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6409 struct got_tree_entry *te, struct tog_parent_trees *parents,
6410 struct got_object_id *commit_id, struct got_repository *repo)
6412 const struct got_error *err = NULL;
6413 char *path;
6414 struct tog_view *blame_view;
6416 *new_view = NULL;
6418 err = tree_entry_path(&path, parents, te);
6419 if (err)
6420 return err;
6422 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6423 if (blame_view == NULL) {
6424 err = got_error_from_errno("view_open");
6425 goto done;
6428 err = open_blame_view(blame_view, path, commit_id, repo);
6429 if (err) {
6430 if (err->code == GOT_ERR_CANCELLED)
6431 err = NULL;
6432 view_close(blame_view);
6433 } else
6434 *new_view = blame_view;
6435 done:
6436 free(path);
6437 return err;
6440 static const struct got_error *
6441 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6442 struct tog_tree_view_state *s)
6444 struct tog_view *log_view;
6445 const struct got_error *err = NULL;
6446 char *path;
6448 *new_view = NULL;
6450 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6451 if (log_view == NULL)
6452 return got_error_from_errno("view_open");
6454 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6455 if (err)
6456 return err;
6458 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6459 path, 0);
6460 if (err)
6461 view_close(log_view);
6462 else
6463 *new_view = log_view;
6464 free(path);
6465 return err;
6468 static const struct got_error *
6469 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6470 const char *head_ref_name, struct got_repository *repo)
6472 const struct got_error *err = NULL;
6473 char *commit_id_str = NULL;
6474 struct tog_tree_view_state *s = &view->state.tree;
6475 struct got_commit_object *commit = NULL;
6477 TAILQ_INIT(&s->parents);
6478 STAILQ_INIT(&s->colors);
6480 s->commit_id = got_object_id_dup(commit_id);
6481 if (s->commit_id == NULL)
6482 return got_error_from_errno("got_object_id_dup");
6484 err = got_object_open_as_commit(&commit, repo, commit_id);
6485 if (err)
6486 goto done;
6489 * The root is opened here and will be closed when the view is closed.
6490 * Any visited subtrees and their path-wise parents are opened and
6491 * closed on demand.
6493 err = got_object_open_as_tree(&s->root, repo,
6494 got_object_commit_get_tree_id(commit));
6495 if (err)
6496 goto done;
6497 s->tree = s->root;
6499 err = got_object_id_str(&commit_id_str, commit_id);
6500 if (err != NULL)
6501 goto done;
6503 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6504 err = got_error_from_errno("asprintf");
6505 goto done;
6508 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6509 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6510 if (head_ref_name) {
6511 s->head_ref_name = strdup(head_ref_name);
6512 if (s->head_ref_name == NULL) {
6513 err = got_error_from_errno("strdup");
6514 goto done;
6517 s->repo = repo;
6519 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6520 err = add_color(&s->colors, "\\$$",
6521 TOG_COLOR_TREE_SUBMODULE,
6522 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6523 if (err)
6524 goto done;
6525 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6526 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6527 if (err)
6528 goto done;
6529 err = add_color(&s->colors, "/$",
6530 TOG_COLOR_TREE_DIRECTORY,
6531 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6532 if (err)
6533 goto done;
6535 err = add_color(&s->colors, "\\*$",
6536 TOG_COLOR_TREE_EXECUTABLE,
6537 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6538 if (err)
6539 goto done;
6541 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6542 get_color_value("TOG_COLOR_COMMIT"));
6543 if (err)
6544 goto done;
6547 view->show = show_tree_view;
6548 view->input = input_tree_view;
6549 view->close = close_tree_view;
6550 view->search_start = search_start_tree_view;
6551 view->search_next = search_next_tree_view;
6552 done:
6553 free(commit_id_str);
6554 if (commit)
6555 got_object_commit_close(commit);
6556 if (err)
6557 close_tree_view(view);
6558 return err;
6561 static const struct got_error *
6562 close_tree_view(struct tog_view *view)
6564 struct tog_tree_view_state *s = &view->state.tree;
6566 free_colors(&s->colors);
6567 free(s->tree_label);
6568 s->tree_label = NULL;
6569 free(s->commit_id);
6570 s->commit_id = NULL;
6571 free(s->head_ref_name);
6572 s->head_ref_name = NULL;
6573 while (!TAILQ_EMPTY(&s->parents)) {
6574 struct tog_parent_tree *parent;
6575 parent = TAILQ_FIRST(&s->parents);
6576 TAILQ_REMOVE(&s->parents, parent, entry);
6577 if (parent->tree != s->root)
6578 got_object_tree_close(parent->tree);
6579 free(parent);
6582 if (s->tree != NULL && s->tree != s->root)
6583 got_object_tree_close(s->tree);
6584 if (s->root)
6585 got_object_tree_close(s->root);
6586 return NULL;
6589 static const struct got_error *
6590 search_start_tree_view(struct tog_view *view)
6592 struct tog_tree_view_state *s = &view->state.tree;
6594 s->matched_entry = NULL;
6595 return NULL;
6598 static int
6599 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6601 regmatch_t regmatch;
6603 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6604 0) == 0;
6607 static const struct got_error *
6608 search_next_tree_view(struct tog_view *view)
6610 struct tog_tree_view_state *s = &view->state.tree;
6611 struct got_tree_entry *te = NULL;
6613 if (!view->searching) {
6614 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6615 return NULL;
6618 if (s->matched_entry) {
6619 if (view->searching == TOG_SEARCH_FORWARD) {
6620 if (s->selected_entry)
6621 te = got_tree_entry_get_next(s->tree,
6622 s->selected_entry);
6623 else
6624 te = got_object_tree_get_first_entry(s->tree);
6625 } else {
6626 if (s->selected_entry == NULL)
6627 te = got_object_tree_get_last_entry(s->tree);
6628 else
6629 te = got_tree_entry_get_prev(s->tree,
6630 s->selected_entry);
6632 } else {
6633 if (s->selected_entry)
6634 te = s->selected_entry;
6635 else if (view->searching == TOG_SEARCH_FORWARD)
6636 te = got_object_tree_get_first_entry(s->tree);
6637 else
6638 te = got_object_tree_get_last_entry(s->tree);
6641 while (1) {
6642 if (te == NULL) {
6643 if (s->matched_entry == NULL) {
6644 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6645 return NULL;
6647 if (view->searching == TOG_SEARCH_FORWARD)
6648 te = got_object_tree_get_first_entry(s->tree);
6649 else
6650 te = got_object_tree_get_last_entry(s->tree);
6653 if (match_tree_entry(te, &view->regex)) {
6654 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6655 s->matched_entry = te;
6656 break;
6659 if (view->searching == TOG_SEARCH_FORWARD)
6660 te = got_tree_entry_get_next(s->tree, te);
6661 else
6662 te = got_tree_entry_get_prev(s->tree, te);
6665 if (s->matched_entry) {
6666 s->first_displayed_entry = s->matched_entry;
6667 s->selected = 0;
6670 return NULL;
6673 static const struct got_error *
6674 show_tree_view(struct tog_view *view)
6676 const struct got_error *err = NULL;
6677 struct tog_tree_view_state *s = &view->state.tree;
6678 char *parent_path;
6680 err = tree_entry_path(&parent_path, &s->parents, NULL);
6681 if (err)
6682 return err;
6684 err = draw_tree_entries(view, parent_path);
6685 free(parent_path);
6687 view_border(view);
6688 return err;
6691 static const struct got_error *
6692 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6694 const struct got_error *err = NULL;
6695 struct tog_tree_view_state *s = &view->state.tree;
6696 struct tog_view *log_view, *ref_view;
6697 struct got_tree_entry *te;
6698 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6700 switch (ch) {
6701 case 'i':
6702 s->show_ids = !s->show_ids;
6703 view->count = 0;
6704 break;
6705 case 'l':
6706 view->count = 0;
6707 if (!s->selected_entry)
6708 break;
6709 if (view_is_parent_view(view))
6710 view_get_split(view, &begin_y, &begin_x);
6711 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6712 if (view_is_parent_view(view) &&
6713 view->mode == TOG_VIEW_SPLIT_HRZN) {
6714 err = view_init_hsplit(view, begin_y);
6715 if (err)
6716 break;
6718 view->focussed = 0;
6719 log_view->focussed = 1;
6720 log_view->mode = view->mode;
6721 log_view->nlines = view->lines - begin_y;
6722 if (view_is_parent_view(view)) {
6723 view_transfer_size(log_view, view);
6724 err = view_close_child(view);
6725 if (err)
6726 return err;
6727 err = view_set_child(view, log_view);
6728 if (err)
6729 return err;
6730 view->focus_child = 1;
6731 } else
6732 *new_view = log_view;
6733 break;
6734 case 'r':
6735 view->count = 0;
6736 if (view_is_parent_view(view))
6737 view_get_split(view, &begin_y, &begin_x);
6738 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6739 if (ref_view == NULL)
6740 return got_error_from_errno("view_open");
6741 err = open_ref_view(ref_view, s->repo);
6742 if (err) {
6743 view_close(ref_view);
6744 return err;
6746 if (view_is_parent_view(view) &&
6747 view->mode == TOG_VIEW_SPLIT_HRZN) {
6748 err = view_init_hsplit(view, begin_y);
6749 if (err)
6750 break;
6752 view->focussed = 0;
6753 ref_view->focussed = 1;
6754 ref_view->mode = view->mode;
6755 ref_view->nlines = view->lines - begin_y;
6756 if (view_is_parent_view(view)) {
6757 view_transfer_size(ref_view, view);
6758 err = view_close_child(view);
6759 if (err)
6760 return err;
6761 err = view_set_child(view, ref_view);
6762 if (err)
6763 return err;
6764 view->focus_child = 1;
6765 } else
6766 *new_view = ref_view;
6767 break;
6768 case 'g':
6769 case KEY_HOME:
6770 s->selected = 0;
6771 view->count = 0;
6772 if (s->tree == s->root)
6773 s->first_displayed_entry =
6774 got_object_tree_get_first_entry(s->tree);
6775 else
6776 s->first_displayed_entry = NULL;
6777 break;
6778 case 'G':
6779 case KEY_END: {
6780 int eos = view->nlines - 3;
6782 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6783 --eos; /* border */
6784 s->selected = 0;
6785 view->count = 0;
6786 te = got_object_tree_get_last_entry(s->tree);
6787 for (n = 0; n < eos; n++) {
6788 if (te == NULL) {
6789 if(s->tree != s->root) {
6790 s->first_displayed_entry = NULL;
6791 n++;
6793 break;
6795 s->first_displayed_entry = te;
6796 te = got_tree_entry_get_prev(s->tree, te);
6798 if (n > 0)
6799 s->selected = n - 1;
6800 break;
6802 case 'k':
6803 case KEY_UP:
6804 case CTRL('p'):
6805 if (s->selected > 0) {
6806 s->selected--;
6807 break;
6809 tree_scroll_up(s, 1);
6810 if (s->selected_entry == NULL ||
6811 (s->tree == s->root && s->selected_entry ==
6812 got_object_tree_get_first_entry(s->tree)))
6813 view->count = 0;
6814 break;
6815 case CTRL('u'):
6816 case 'u':
6817 nscroll /= 2;
6818 /* FALL THROUGH */
6819 case KEY_PPAGE:
6820 case CTRL('b'):
6821 case 'b':
6822 if (s->tree == s->root) {
6823 if (got_object_tree_get_first_entry(s->tree) ==
6824 s->first_displayed_entry)
6825 s->selected -= MIN(s->selected, nscroll);
6826 } else {
6827 if (s->first_displayed_entry == NULL)
6828 s->selected -= MIN(s->selected, nscroll);
6830 tree_scroll_up(s, MAX(0, nscroll));
6831 if (s->selected_entry == NULL ||
6832 (s->tree == s->root && s->selected_entry ==
6833 got_object_tree_get_first_entry(s->tree)))
6834 view->count = 0;
6835 break;
6836 case 'j':
6837 case KEY_DOWN:
6838 case CTRL('n'):
6839 if (s->selected < s->ndisplayed - 1) {
6840 s->selected++;
6841 break;
6843 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6844 == NULL) {
6845 /* can't scroll any further */
6846 view->count = 0;
6847 break;
6849 tree_scroll_down(view, 1);
6850 break;
6851 case CTRL('d'):
6852 case 'd':
6853 nscroll /= 2;
6854 /* FALL THROUGH */
6855 case KEY_NPAGE:
6856 case CTRL('f'):
6857 case 'f':
6858 case ' ':
6859 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6860 == NULL) {
6861 /* can't scroll any further; move cursor down */
6862 if (s->selected < s->ndisplayed - 1)
6863 s->selected += MIN(nscroll,
6864 s->ndisplayed - s->selected - 1);
6865 else
6866 view->count = 0;
6867 break;
6869 tree_scroll_down(view, nscroll);
6870 break;
6871 case KEY_ENTER:
6872 case '\r':
6873 case KEY_BACKSPACE:
6874 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6875 struct tog_parent_tree *parent;
6876 /* user selected '..' */
6877 if (s->tree == s->root) {
6878 view->count = 0;
6879 break;
6881 parent = TAILQ_FIRST(&s->parents);
6882 TAILQ_REMOVE(&s->parents, parent,
6883 entry);
6884 got_object_tree_close(s->tree);
6885 s->tree = parent->tree;
6886 s->first_displayed_entry =
6887 parent->first_displayed_entry;
6888 s->selected_entry =
6889 parent->selected_entry;
6890 s->selected = parent->selected;
6891 if (s->selected > view->nlines - 3) {
6892 err = offset_selection_down(view);
6893 if (err)
6894 break;
6896 free(parent);
6897 } else if (S_ISDIR(got_tree_entry_get_mode(
6898 s->selected_entry))) {
6899 struct got_tree_object *subtree;
6900 view->count = 0;
6901 err = got_object_open_as_tree(&subtree, s->repo,
6902 got_tree_entry_get_id(s->selected_entry));
6903 if (err)
6904 break;
6905 err = tree_view_visit_subtree(s, subtree);
6906 if (err) {
6907 got_object_tree_close(subtree);
6908 break;
6910 } else if (S_ISREG(got_tree_entry_get_mode(
6911 s->selected_entry))) {
6912 struct tog_view *blame_view;
6913 int begin_x = 0, begin_y = 0;
6915 if (view_is_parent_view(view))
6916 view_get_split(view, &begin_y, &begin_x);
6918 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6919 s->selected_entry, &s->parents,
6920 s->commit_id, s->repo);
6921 if (err)
6922 break;
6924 if (view_is_parent_view(view) &&
6925 view->mode == TOG_VIEW_SPLIT_HRZN) {
6926 err = view_init_hsplit(view, begin_y);
6927 if (err)
6928 break;
6931 view->count = 0;
6932 view->focussed = 0;
6933 blame_view->focussed = 1;
6934 blame_view->mode = view->mode;
6935 blame_view->nlines = view->lines - begin_y;
6936 if (view_is_parent_view(view)) {
6937 view_transfer_size(blame_view, view);
6938 err = view_close_child(view);
6939 if (err)
6940 return err;
6941 err = view_set_child(view, blame_view);
6942 if (err)
6943 return err;
6944 view->focus_child = 1;
6945 } else
6946 *new_view = blame_view;
6948 break;
6949 case KEY_RESIZE:
6950 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6951 s->selected = view->nlines - 4;
6952 view->count = 0;
6953 break;
6954 default:
6955 view->count = 0;
6956 break;
6959 return err;
6962 __dead static void
6963 usage_tree(void)
6965 endwin();
6966 fprintf(stderr,
6967 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6968 getprogname());
6969 exit(1);
6972 static const struct got_error *
6973 cmd_tree(int argc, char *argv[])
6975 const struct got_error *error;
6976 struct got_repository *repo = NULL;
6977 struct got_worktree *worktree = NULL;
6978 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6979 struct got_object_id *commit_id = NULL;
6980 struct got_commit_object *commit = NULL;
6981 const char *commit_id_arg = NULL;
6982 char *label = NULL;
6983 struct got_reference *ref = NULL;
6984 const char *head_ref_name = NULL;
6985 int ch;
6986 struct tog_view *view;
6987 int *pack_fds = NULL;
6989 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6990 switch (ch) {
6991 case 'c':
6992 commit_id_arg = optarg;
6993 break;
6994 case 'r':
6995 repo_path = realpath(optarg, NULL);
6996 if (repo_path == NULL)
6997 return got_error_from_errno2("realpath",
6998 optarg);
6999 break;
7000 default:
7001 usage_tree();
7002 /* NOTREACHED */
7006 argc -= optind;
7007 argv += optind;
7009 if (argc > 1)
7010 usage_tree();
7012 error = got_repo_pack_fds_open(&pack_fds);
7013 if (error != NULL)
7014 goto done;
7016 if (repo_path == NULL) {
7017 cwd = getcwd(NULL, 0);
7018 if (cwd == NULL)
7019 return got_error_from_errno("getcwd");
7020 error = got_worktree_open(&worktree, cwd);
7021 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7022 goto done;
7023 if (worktree)
7024 repo_path =
7025 strdup(got_worktree_get_repo_path(worktree));
7026 else
7027 repo_path = strdup(cwd);
7028 if (repo_path == NULL) {
7029 error = got_error_from_errno("strdup");
7030 goto done;
7034 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7035 if (error != NULL)
7036 goto done;
7038 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7039 repo, worktree);
7040 if (error)
7041 goto done;
7043 init_curses();
7045 error = apply_unveil(got_repo_get_path(repo), NULL);
7046 if (error)
7047 goto done;
7049 error = tog_load_refs(repo, 0);
7050 if (error)
7051 goto done;
7053 if (commit_id_arg == NULL) {
7054 error = got_repo_match_object_id(&commit_id, &label,
7055 worktree ? got_worktree_get_head_ref_name(worktree) :
7056 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7057 if (error)
7058 goto done;
7059 head_ref_name = label;
7060 } else {
7061 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7062 if (error == NULL)
7063 head_ref_name = got_ref_get_name(ref);
7064 else if (error->code != GOT_ERR_NOT_REF)
7065 goto done;
7066 error = got_repo_match_object_id(&commit_id, NULL,
7067 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7068 if (error)
7069 goto done;
7072 error = got_object_open_as_commit(&commit, repo, commit_id);
7073 if (error)
7074 goto done;
7076 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7077 if (view == NULL) {
7078 error = got_error_from_errno("view_open");
7079 goto done;
7081 error = open_tree_view(view, commit_id, head_ref_name, repo);
7082 if (error)
7083 goto done;
7084 if (!got_path_is_root_dir(in_repo_path)) {
7085 error = tree_view_walk_path(&view->state.tree, commit,
7086 in_repo_path);
7087 if (error)
7088 goto done;
7091 if (worktree) {
7092 /* Release work tree lock. */
7093 got_worktree_close(worktree);
7094 worktree = NULL;
7096 error = view_loop(view);
7097 done:
7098 free(repo_path);
7099 free(cwd);
7100 free(commit_id);
7101 free(label);
7102 if (ref)
7103 got_ref_close(ref);
7104 if (repo) {
7105 const struct got_error *close_err = got_repo_close(repo);
7106 if (error == NULL)
7107 error = close_err;
7109 if (pack_fds) {
7110 const struct got_error *pack_err =
7111 got_repo_pack_fds_close(pack_fds);
7112 if (error == NULL)
7113 error = pack_err;
7115 tog_free_refs();
7116 return error;
7119 static const struct got_error *
7120 ref_view_load_refs(struct tog_ref_view_state *s)
7122 struct got_reflist_entry *sre;
7123 struct tog_reflist_entry *re;
7125 s->nrefs = 0;
7126 TAILQ_FOREACH(sre, &tog_refs, entry) {
7127 if (strncmp(got_ref_get_name(sre->ref),
7128 "refs/got/", 9) == 0 &&
7129 strncmp(got_ref_get_name(sre->ref),
7130 "refs/got/backup/", 16) != 0)
7131 continue;
7133 re = malloc(sizeof(*re));
7134 if (re == NULL)
7135 return got_error_from_errno("malloc");
7137 re->ref = got_ref_dup(sre->ref);
7138 if (re->ref == NULL)
7139 return got_error_from_errno("got_ref_dup");
7140 re->idx = s->nrefs++;
7141 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7144 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7145 return NULL;
7148 static void
7149 ref_view_free_refs(struct tog_ref_view_state *s)
7151 struct tog_reflist_entry *re;
7153 while (!TAILQ_EMPTY(&s->refs)) {
7154 re = TAILQ_FIRST(&s->refs);
7155 TAILQ_REMOVE(&s->refs, re, entry);
7156 got_ref_close(re->ref);
7157 free(re);
7161 static const struct got_error *
7162 open_ref_view(struct tog_view *view, struct got_repository *repo)
7164 const struct got_error *err = NULL;
7165 struct tog_ref_view_state *s = &view->state.ref;
7167 s->selected_entry = 0;
7168 s->repo = repo;
7170 TAILQ_INIT(&s->refs);
7171 STAILQ_INIT(&s->colors);
7173 err = ref_view_load_refs(s);
7174 if (err)
7175 return err;
7177 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7178 err = add_color(&s->colors, "^refs/heads/",
7179 TOG_COLOR_REFS_HEADS,
7180 get_color_value("TOG_COLOR_REFS_HEADS"));
7181 if (err)
7182 goto done;
7184 err = add_color(&s->colors, "^refs/tags/",
7185 TOG_COLOR_REFS_TAGS,
7186 get_color_value("TOG_COLOR_REFS_TAGS"));
7187 if (err)
7188 goto done;
7190 err = add_color(&s->colors, "^refs/remotes/",
7191 TOG_COLOR_REFS_REMOTES,
7192 get_color_value("TOG_COLOR_REFS_REMOTES"));
7193 if (err)
7194 goto done;
7196 err = add_color(&s->colors, "^refs/got/backup/",
7197 TOG_COLOR_REFS_BACKUP,
7198 get_color_value("TOG_COLOR_REFS_BACKUP"));
7199 if (err)
7200 goto done;
7203 view->show = show_ref_view;
7204 view->input = input_ref_view;
7205 view->close = close_ref_view;
7206 view->search_start = search_start_ref_view;
7207 view->search_next = search_next_ref_view;
7208 done:
7209 if (err)
7210 free_colors(&s->colors);
7211 return err;
7214 static const struct got_error *
7215 close_ref_view(struct tog_view *view)
7217 struct tog_ref_view_state *s = &view->state.ref;
7219 ref_view_free_refs(s);
7220 free_colors(&s->colors);
7222 return NULL;
7225 static const struct got_error *
7226 resolve_reflist_entry(struct got_object_id **commit_id,
7227 struct tog_reflist_entry *re, struct got_repository *repo)
7229 const struct got_error *err = NULL;
7230 struct got_object_id *obj_id;
7231 struct got_tag_object *tag = NULL;
7232 int obj_type;
7234 *commit_id = NULL;
7236 err = got_ref_resolve(&obj_id, repo, re->ref);
7237 if (err)
7238 return err;
7240 err = got_object_get_type(&obj_type, repo, obj_id);
7241 if (err)
7242 goto done;
7244 switch (obj_type) {
7245 case GOT_OBJ_TYPE_COMMIT:
7246 *commit_id = obj_id;
7247 break;
7248 case GOT_OBJ_TYPE_TAG:
7249 err = got_object_open_as_tag(&tag, repo, obj_id);
7250 if (err)
7251 goto done;
7252 free(obj_id);
7253 err = got_object_get_type(&obj_type, repo,
7254 got_object_tag_get_object_id(tag));
7255 if (err)
7256 goto done;
7257 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7258 err = got_error(GOT_ERR_OBJ_TYPE);
7259 goto done;
7261 *commit_id = got_object_id_dup(
7262 got_object_tag_get_object_id(tag));
7263 if (*commit_id == NULL) {
7264 err = got_error_from_errno("got_object_id_dup");
7265 goto done;
7267 break;
7268 default:
7269 err = got_error(GOT_ERR_OBJ_TYPE);
7270 break;
7273 done:
7274 if (tag)
7275 got_object_tag_close(tag);
7276 if (err) {
7277 free(*commit_id);
7278 *commit_id = NULL;
7280 return err;
7283 static const struct got_error *
7284 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7285 struct tog_reflist_entry *re, struct got_repository *repo)
7287 struct tog_view *log_view;
7288 const struct got_error *err = NULL;
7289 struct got_object_id *commit_id = NULL;
7291 *new_view = NULL;
7293 err = resolve_reflist_entry(&commit_id, re, repo);
7294 if (err) {
7295 if (err->code != GOT_ERR_OBJ_TYPE)
7296 return err;
7297 else
7298 return NULL;
7301 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7302 if (log_view == NULL) {
7303 err = got_error_from_errno("view_open");
7304 goto done;
7307 err = open_log_view(log_view, commit_id, repo,
7308 got_ref_get_name(re->ref), "", 0);
7309 done:
7310 if (err)
7311 view_close(log_view);
7312 else
7313 *new_view = log_view;
7314 free(commit_id);
7315 return err;
7318 static void
7319 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7321 struct tog_reflist_entry *re;
7322 int i = 0;
7324 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7325 return;
7327 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7328 while (i++ < maxscroll) {
7329 if (re == NULL)
7330 break;
7331 s->first_displayed_entry = re;
7332 re = TAILQ_PREV(re, tog_reflist_head, entry);
7336 static const struct got_error *
7337 ref_scroll_down(struct tog_view *view, int maxscroll)
7339 struct tog_ref_view_state *s = &view->state.ref;
7340 struct tog_reflist_entry *next, *last;
7341 int n = 0;
7343 if (s->first_displayed_entry)
7344 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7345 else
7346 next = TAILQ_FIRST(&s->refs);
7348 last = s->last_displayed_entry;
7349 while (next && n++ < maxscroll) {
7350 if (last)
7351 last = TAILQ_NEXT(last, entry);
7352 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7353 s->first_displayed_entry = next;
7354 next = TAILQ_NEXT(next, entry);
7358 return NULL;
7361 static const struct got_error *
7362 search_start_ref_view(struct tog_view *view)
7364 struct tog_ref_view_state *s = &view->state.ref;
7366 s->matched_entry = NULL;
7367 return NULL;
7370 static int
7371 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7373 regmatch_t regmatch;
7375 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7376 0) == 0;
7379 static const struct got_error *
7380 search_next_ref_view(struct tog_view *view)
7382 struct tog_ref_view_state *s = &view->state.ref;
7383 struct tog_reflist_entry *re = NULL;
7385 if (!view->searching) {
7386 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7387 return NULL;
7390 if (s->matched_entry) {
7391 if (view->searching == TOG_SEARCH_FORWARD) {
7392 if (s->selected_entry)
7393 re = TAILQ_NEXT(s->selected_entry, entry);
7394 else
7395 re = TAILQ_PREV(s->selected_entry,
7396 tog_reflist_head, entry);
7397 } else {
7398 if (s->selected_entry == NULL)
7399 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7400 else
7401 re = TAILQ_PREV(s->selected_entry,
7402 tog_reflist_head, entry);
7404 } else {
7405 if (s->selected_entry)
7406 re = s->selected_entry;
7407 else if (view->searching == TOG_SEARCH_FORWARD)
7408 re = TAILQ_FIRST(&s->refs);
7409 else
7410 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7413 while (1) {
7414 if (re == NULL) {
7415 if (s->matched_entry == NULL) {
7416 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7417 return NULL;
7419 if (view->searching == TOG_SEARCH_FORWARD)
7420 re = TAILQ_FIRST(&s->refs);
7421 else
7422 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7425 if (match_reflist_entry(re, &view->regex)) {
7426 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7427 s->matched_entry = re;
7428 break;
7431 if (view->searching == TOG_SEARCH_FORWARD)
7432 re = TAILQ_NEXT(re, entry);
7433 else
7434 re = TAILQ_PREV(re, tog_reflist_head, entry);
7437 if (s->matched_entry) {
7438 s->first_displayed_entry = s->matched_entry;
7439 s->selected = 0;
7442 return NULL;
7445 static const struct got_error *
7446 show_ref_view(struct tog_view *view)
7448 const struct got_error *err = NULL;
7449 struct tog_ref_view_state *s = &view->state.ref;
7450 struct tog_reflist_entry *re;
7451 char *line = NULL;
7452 wchar_t *wline;
7453 struct tog_color *tc;
7454 int width, n;
7455 int limit = view->nlines;
7457 werase(view->window);
7459 s->ndisplayed = 0;
7460 if (view_is_hsplit_top(view))
7461 --limit; /* border */
7463 if (limit == 0)
7464 return NULL;
7466 re = s->first_displayed_entry;
7468 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7469 s->nrefs) == -1)
7470 return got_error_from_errno("asprintf");
7472 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7473 if (err) {
7474 free(line);
7475 return err;
7477 if (view_needs_focus_indication(view))
7478 wstandout(view->window);
7479 waddwstr(view->window, wline);
7480 if (view_needs_focus_indication(view))
7481 wstandend(view->window);
7482 free(wline);
7483 wline = NULL;
7484 free(line);
7485 line = NULL;
7486 if (width < view->ncols - 1)
7487 waddch(view->window, '\n');
7488 if (--limit <= 0)
7489 return NULL;
7491 n = 0;
7492 while (re && limit > 0) {
7493 char *line = NULL;
7494 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7496 if (s->show_date) {
7497 struct got_commit_object *ci;
7498 struct got_tag_object *tag;
7499 struct got_object_id *id;
7500 struct tm tm;
7501 time_t t;
7503 err = got_ref_resolve(&id, s->repo, re->ref);
7504 if (err)
7505 return err;
7506 err = got_object_open_as_tag(&tag, s->repo, id);
7507 if (err) {
7508 if (err->code != GOT_ERR_OBJ_TYPE) {
7509 free(id);
7510 return err;
7512 err = got_object_open_as_commit(&ci, s->repo,
7513 id);
7514 if (err) {
7515 free(id);
7516 return err;
7518 t = got_object_commit_get_committer_time(ci);
7519 got_object_commit_close(ci);
7520 } else {
7521 t = got_object_tag_get_tagger_time(tag);
7522 got_object_tag_close(tag);
7524 free(id);
7525 if (gmtime_r(&t, &tm) == NULL)
7526 return got_error_from_errno("gmtime_r");
7527 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7528 return got_error(GOT_ERR_NO_SPACE);
7530 if (got_ref_is_symbolic(re->ref)) {
7531 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7532 ymd : "", got_ref_get_name(re->ref),
7533 got_ref_get_symref_target(re->ref)) == -1)
7534 return got_error_from_errno("asprintf");
7535 } else if (s->show_ids) {
7536 struct got_object_id *id;
7537 char *id_str;
7538 err = got_ref_resolve(&id, s->repo, re->ref);
7539 if (err)
7540 return err;
7541 err = got_object_id_str(&id_str, id);
7542 if (err) {
7543 free(id);
7544 return err;
7546 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7547 got_ref_get_name(re->ref), id_str) == -1) {
7548 err = got_error_from_errno("asprintf");
7549 free(id);
7550 free(id_str);
7551 return err;
7553 free(id);
7554 free(id_str);
7555 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7556 got_ref_get_name(re->ref)) == -1)
7557 return got_error_from_errno("asprintf");
7559 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7560 0, 0);
7561 if (err) {
7562 free(line);
7563 return err;
7565 if (n == s->selected) {
7566 if (view->focussed)
7567 wstandout(view->window);
7568 s->selected_entry = re;
7570 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7571 if (tc)
7572 wattr_on(view->window,
7573 COLOR_PAIR(tc->colorpair), NULL);
7574 waddwstr(view->window, wline);
7575 if (tc)
7576 wattr_off(view->window,
7577 COLOR_PAIR(tc->colorpair), NULL);
7578 if (width < view->ncols - 1)
7579 waddch(view->window, '\n');
7580 if (n == s->selected && view->focussed)
7581 wstandend(view->window);
7582 free(line);
7583 free(wline);
7584 wline = NULL;
7585 n++;
7586 s->ndisplayed++;
7587 s->last_displayed_entry = re;
7589 limit--;
7590 re = TAILQ_NEXT(re, entry);
7593 view_border(view);
7594 return err;
7597 static const struct got_error *
7598 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7599 struct tog_reflist_entry *re, struct got_repository *repo)
7601 const struct got_error *err = NULL;
7602 struct got_object_id *commit_id = NULL;
7603 struct tog_view *tree_view;
7605 *new_view = NULL;
7607 err = resolve_reflist_entry(&commit_id, re, repo);
7608 if (err) {
7609 if (err->code != GOT_ERR_OBJ_TYPE)
7610 return err;
7611 else
7612 return NULL;
7616 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7617 if (tree_view == NULL) {
7618 err = got_error_from_errno("view_open");
7619 goto done;
7622 err = open_tree_view(tree_view, commit_id,
7623 got_ref_get_name(re->ref), repo);
7624 if (err)
7625 goto done;
7627 *new_view = tree_view;
7628 done:
7629 free(commit_id);
7630 return err;
7632 static const struct got_error *
7633 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7635 const struct got_error *err = NULL;
7636 struct tog_ref_view_state *s = &view->state.ref;
7637 struct tog_view *log_view, *tree_view;
7638 struct tog_reflist_entry *re;
7639 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7641 switch (ch) {
7642 case 'i':
7643 s->show_ids = !s->show_ids;
7644 view->count = 0;
7645 break;
7646 case 'm':
7647 s->show_date = !s->show_date;
7648 view->count = 0;
7649 break;
7650 case 'o':
7651 s->sort_by_date = !s->sort_by_date;
7652 view->count = 0;
7653 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7654 got_ref_cmp_by_commit_timestamp_descending :
7655 tog_ref_cmp_by_name, s->repo);
7656 if (err)
7657 break;
7658 got_reflist_object_id_map_free(tog_refs_idmap);
7659 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7660 &tog_refs, s->repo);
7661 if (err)
7662 break;
7663 ref_view_free_refs(s);
7664 err = ref_view_load_refs(s);
7665 break;
7666 case KEY_ENTER:
7667 case '\r':
7668 view->count = 0;
7669 if (!s->selected_entry)
7670 break;
7671 if (view_is_parent_view(view))
7672 view_get_split(view, &begin_y, &begin_x);
7674 err = log_ref_entry(&log_view, begin_y, begin_x,
7675 s->selected_entry, s->repo);
7676 if (err)
7677 break;
7679 if (view_is_parent_view(view) &&
7680 view->mode == TOG_VIEW_SPLIT_HRZN) {
7681 err = view_init_hsplit(view, begin_y);
7682 if (err)
7683 break;
7686 view->focussed = 0;
7687 log_view->focussed = 1;
7688 log_view->mode = view->mode;
7689 log_view->nlines = view->lines - begin_y;
7690 if (view_is_parent_view(view)) {
7691 view_transfer_size(log_view, view);
7692 err = view_close_child(view);
7693 if (err)
7694 return err;
7695 err = view_set_child(view, log_view);
7696 if (err)
7697 return err;
7698 view->focus_child = 1;
7699 } else
7700 *new_view = log_view;
7701 break;
7702 case 't':
7703 view->count = 0;
7704 if (!s->selected_entry)
7705 break;
7706 if (view_is_parent_view(view))
7707 view_get_split(view, &begin_y, &begin_x);
7708 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7709 s->selected_entry, s->repo);
7710 if (err || tree_view == NULL)
7711 break;
7712 if (view_is_parent_view(view) &&
7713 view->mode == TOG_VIEW_SPLIT_HRZN) {
7714 err = view_init_hsplit(view, begin_y);
7715 if (err)
7716 break;
7718 view->focussed = 0;
7719 tree_view->focussed = 1;
7720 tree_view->mode = view->mode;
7721 tree_view->nlines = view->lines - begin_y;
7722 if (view_is_parent_view(view)) {
7723 view_transfer_size(tree_view, view);
7724 err = view_close_child(view);
7725 if (err)
7726 return err;
7727 err = view_set_child(view, tree_view);
7728 if (err)
7729 return err;
7730 view->focus_child = 1;
7731 } else
7732 *new_view = tree_view;
7733 break;
7734 case 'g':
7735 case KEY_HOME:
7736 s->selected = 0;
7737 view->count = 0;
7738 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7739 break;
7740 case 'G':
7741 case KEY_END: {
7742 int eos = view->nlines - 1;
7744 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7745 --eos; /* border */
7746 s->selected = 0;
7747 view->count = 0;
7748 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7749 for (n = 0; n < eos; n++) {
7750 if (re == NULL)
7751 break;
7752 s->first_displayed_entry = re;
7753 re = TAILQ_PREV(re, tog_reflist_head, entry);
7755 if (n > 0)
7756 s->selected = n - 1;
7757 break;
7759 case 'k':
7760 case KEY_UP:
7761 case CTRL('p'):
7762 if (s->selected > 0) {
7763 s->selected--;
7764 break;
7766 ref_scroll_up(s, 1);
7767 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7768 view->count = 0;
7769 break;
7770 case CTRL('u'):
7771 case 'u':
7772 nscroll /= 2;
7773 /* FALL THROUGH */
7774 case KEY_PPAGE:
7775 case CTRL('b'):
7776 case 'b':
7777 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7778 s->selected -= MIN(nscroll, s->selected);
7779 ref_scroll_up(s, MAX(0, nscroll));
7780 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7781 view->count = 0;
7782 break;
7783 case 'j':
7784 case KEY_DOWN:
7785 case CTRL('n'):
7786 if (s->selected < s->ndisplayed - 1) {
7787 s->selected++;
7788 break;
7790 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7791 /* can't scroll any further */
7792 view->count = 0;
7793 break;
7795 ref_scroll_down(view, 1);
7796 break;
7797 case CTRL('d'):
7798 case 'd':
7799 nscroll /= 2;
7800 /* FALL THROUGH */
7801 case KEY_NPAGE:
7802 case CTRL('f'):
7803 case 'f':
7804 case ' ':
7805 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7806 /* can't scroll any further; move cursor down */
7807 if (s->selected < s->ndisplayed - 1)
7808 s->selected += MIN(nscroll,
7809 s->ndisplayed - s->selected - 1);
7810 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7811 s->selected += s->ndisplayed - s->selected - 1;
7812 view->count = 0;
7813 break;
7815 ref_scroll_down(view, nscroll);
7816 break;
7817 case CTRL('l'):
7818 view->count = 0;
7819 tog_free_refs();
7820 err = tog_load_refs(s->repo, s->sort_by_date);
7821 if (err)
7822 break;
7823 ref_view_free_refs(s);
7824 err = ref_view_load_refs(s);
7825 break;
7826 case KEY_RESIZE:
7827 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7828 s->selected = view->nlines - 2;
7829 break;
7830 default:
7831 view->count = 0;
7832 break;
7835 return err;
7838 __dead static void
7839 usage_ref(void)
7841 endwin();
7842 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7843 getprogname());
7844 exit(1);
7847 static const struct got_error *
7848 cmd_ref(int argc, char *argv[])
7850 const struct got_error *error;
7851 struct got_repository *repo = NULL;
7852 struct got_worktree *worktree = NULL;
7853 char *cwd = NULL, *repo_path = NULL;
7854 int ch;
7855 struct tog_view *view;
7856 int *pack_fds = NULL;
7858 while ((ch = getopt(argc, argv, "r:")) != -1) {
7859 switch (ch) {
7860 case 'r':
7861 repo_path = realpath(optarg, NULL);
7862 if (repo_path == NULL)
7863 return got_error_from_errno2("realpath",
7864 optarg);
7865 break;
7866 default:
7867 usage_ref();
7868 /* NOTREACHED */
7872 argc -= optind;
7873 argv += optind;
7875 if (argc > 1)
7876 usage_ref();
7878 error = got_repo_pack_fds_open(&pack_fds);
7879 if (error != NULL)
7880 goto done;
7882 if (repo_path == NULL) {
7883 cwd = getcwd(NULL, 0);
7884 if (cwd == NULL)
7885 return got_error_from_errno("getcwd");
7886 error = got_worktree_open(&worktree, cwd);
7887 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7888 goto done;
7889 if (worktree)
7890 repo_path =
7891 strdup(got_worktree_get_repo_path(worktree));
7892 else
7893 repo_path = strdup(cwd);
7894 if (repo_path == NULL) {
7895 error = got_error_from_errno("strdup");
7896 goto done;
7900 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7901 if (error != NULL)
7902 goto done;
7904 init_curses();
7906 error = apply_unveil(got_repo_get_path(repo), NULL);
7907 if (error)
7908 goto done;
7910 error = tog_load_refs(repo, 0);
7911 if (error)
7912 goto done;
7914 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7915 if (view == NULL) {
7916 error = got_error_from_errno("view_open");
7917 goto done;
7920 error = open_ref_view(view, repo);
7921 if (error)
7922 goto done;
7924 if (worktree) {
7925 /* Release work tree lock. */
7926 got_worktree_close(worktree);
7927 worktree = NULL;
7929 error = view_loop(view);
7930 done:
7931 free(repo_path);
7932 free(cwd);
7933 if (repo) {
7934 const struct got_error *close_err = got_repo_close(repo);
7935 if (close_err)
7936 error = close_err;
7938 if (pack_fds) {
7939 const struct got_error *pack_err =
7940 got_repo_pack_fds_close(pack_fds);
7941 if (error == NULL)
7942 error = pack_err;
7944 tog_free_refs();
7945 return error;
7949 * If view was scrolled down to move the selected line into view when opening a
7950 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7952 static void
7953 offset_selection_up(struct tog_view *view)
7955 switch (view->type) {
7956 case TOG_VIEW_BLAME: {
7957 struct tog_blame_view_state *s = &view->state.blame;
7958 if (s->first_displayed_line == 1) {
7959 s->selected_line = MAX(s->selected_line - view->offset,
7960 1);
7961 break;
7963 if (s->first_displayed_line > view->offset)
7964 s->first_displayed_line -= view->offset;
7965 else
7966 s->first_displayed_line = 1;
7967 s->selected_line += view->offset;
7968 break;
7970 case TOG_VIEW_LOG:
7971 log_scroll_up(&view->state.log, view->offset);
7972 view->state.log.selected += view->offset;
7973 break;
7974 case TOG_VIEW_REF:
7975 ref_scroll_up(&view->state.ref, view->offset);
7976 view->state.ref.selected += view->offset;
7977 break;
7978 case TOG_VIEW_TREE:
7979 tree_scroll_up(&view->state.tree, view->offset);
7980 view->state.tree.selected += view->offset;
7981 break;
7982 default:
7983 break;
7986 view->offset = 0;
7990 * If the selected line is in the section of screen covered by the bottom split,
7991 * scroll down offset lines to move it into view and index its new position.
7993 static const struct got_error *
7994 offset_selection_down(struct tog_view *view)
7996 const struct got_error *err = NULL;
7997 const struct got_error *(*scrolld)(struct tog_view *, int);
7998 int *selected = NULL;
7999 int header, offset;
8001 switch (view->type) {
8002 case TOG_VIEW_BLAME: {
8003 struct tog_blame_view_state *s = &view->state.blame;
8004 header = 3;
8005 scrolld = NULL;
8006 if (s->selected_line > view->nlines - header) {
8007 offset = abs(view->nlines - s->selected_line - header);
8008 s->first_displayed_line += offset;
8009 s->selected_line -= offset;
8010 view->offset = offset;
8012 break;
8014 case TOG_VIEW_LOG: {
8015 struct tog_log_view_state *s = &view->state.log;
8016 scrolld = &log_scroll_down;
8017 header = view_is_parent_view(view) ? 3 : 2;
8018 selected = &s->selected;
8019 break;
8021 case TOG_VIEW_REF: {
8022 struct tog_ref_view_state *s = &view->state.ref;
8023 scrolld = &ref_scroll_down;
8024 header = 3;
8025 selected = &s->selected;
8026 break;
8028 case TOG_VIEW_TREE: {
8029 struct tog_tree_view_state *s = &view->state.tree;
8030 scrolld = &tree_scroll_down;
8031 header = 5;
8032 selected = &s->selected;
8033 break;
8035 default:
8036 selected = NULL;
8037 scrolld = NULL;
8038 header = 0;
8039 break;
8042 if (selected && *selected > view->nlines - header) {
8043 offset = abs(view->nlines - *selected - header);
8044 view->offset = offset;
8045 if (scrolld && offset) {
8046 err = scrolld(view, offset);
8047 *selected -= offset;
8051 return err;
8054 static void
8055 list_commands(FILE *fp)
8057 size_t i;
8059 fprintf(fp, "commands:");
8060 for (i = 0; i < nitems(tog_commands); i++) {
8061 const struct tog_cmd *cmd = &tog_commands[i];
8062 fprintf(fp, " %s", cmd->name);
8064 fputc('\n', fp);
8067 __dead static void
8068 usage(int hflag, int status)
8070 FILE *fp = (status == 0) ? stdout : stderr;
8072 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8073 getprogname());
8074 if (hflag) {
8075 fprintf(fp, "lazy usage: %s path\n", getprogname());
8076 list_commands(fp);
8078 exit(status);
8081 static char **
8082 make_argv(int argc, ...)
8084 va_list ap;
8085 char **argv;
8086 int i;
8088 va_start(ap, argc);
8090 argv = calloc(argc, sizeof(char *));
8091 if (argv == NULL)
8092 err(1, "calloc");
8093 for (i = 0; i < argc; i++) {
8094 argv[i] = strdup(va_arg(ap, char *));
8095 if (argv[i] == NULL)
8096 err(1, "strdup");
8099 va_end(ap);
8100 return argv;
8104 * Try to convert 'tog path' into a 'tog log path' command.
8105 * The user could simply have mistyped the command rather than knowingly
8106 * provided a path. So check whether argv[0] can in fact be resolved
8107 * to a path in the HEAD commit and print a special error if not.
8108 * This hack is for mpi@ <3
8110 static const struct got_error *
8111 tog_log_with_path(int argc, char *argv[])
8113 const struct got_error *error = NULL, *close_err;
8114 const struct tog_cmd *cmd = NULL;
8115 struct got_repository *repo = NULL;
8116 struct got_worktree *worktree = NULL;
8117 struct got_object_id *commit_id = NULL, *id = NULL;
8118 struct got_commit_object *commit = NULL;
8119 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8120 char *commit_id_str = NULL, **cmd_argv = NULL;
8121 int *pack_fds = NULL;
8123 cwd = getcwd(NULL, 0);
8124 if (cwd == NULL)
8125 return got_error_from_errno("getcwd");
8127 error = got_repo_pack_fds_open(&pack_fds);
8128 if (error != NULL)
8129 goto done;
8131 error = got_worktree_open(&worktree, cwd);
8132 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8133 goto done;
8135 if (worktree)
8136 repo_path = strdup(got_worktree_get_repo_path(worktree));
8137 else
8138 repo_path = strdup(cwd);
8139 if (repo_path == NULL) {
8140 error = got_error_from_errno("strdup");
8141 goto done;
8144 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8145 if (error != NULL)
8146 goto done;
8148 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8149 repo, worktree);
8150 if (error)
8151 goto done;
8153 error = tog_load_refs(repo, 0);
8154 if (error)
8155 goto done;
8156 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8157 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8158 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8159 if (error)
8160 goto done;
8162 if (worktree) {
8163 got_worktree_close(worktree);
8164 worktree = NULL;
8167 error = got_object_open_as_commit(&commit, repo, commit_id);
8168 if (error)
8169 goto done;
8171 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8172 if (error) {
8173 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8174 goto done;
8175 fprintf(stderr, "%s: '%s' is no known command or path\n",
8176 getprogname(), argv[0]);
8177 usage(1, 1);
8178 /* not reached */
8181 close_err = got_repo_close(repo);
8182 if (error == NULL)
8183 error = close_err;
8184 repo = NULL;
8186 error = got_object_id_str(&commit_id_str, commit_id);
8187 if (error)
8188 goto done;
8190 cmd = &tog_commands[0]; /* log */
8191 argc = 4;
8192 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8193 error = cmd->cmd_main(argc, cmd_argv);
8194 done:
8195 if (repo) {
8196 close_err = got_repo_close(repo);
8197 if (error == NULL)
8198 error = close_err;
8200 if (commit)
8201 got_object_commit_close(commit);
8202 if (worktree)
8203 got_worktree_close(worktree);
8204 if (pack_fds) {
8205 const struct got_error *pack_err =
8206 got_repo_pack_fds_close(pack_fds);
8207 if (error == NULL)
8208 error = pack_err;
8210 free(id);
8211 free(commit_id_str);
8212 free(commit_id);
8213 free(cwd);
8214 free(repo_path);
8215 free(in_repo_path);
8216 if (cmd_argv) {
8217 int i;
8218 for (i = 0; i < argc; i++)
8219 free(cmd_argv[i]);
8220 free(cmd_argv);
8222 tog_free_refs();
8223 return error;
8226 int
8227 main(int argc, char *argv[])
8229 const struct got_error *error = NULL;
8230 const struct tog_cmd *cmd = NULL;
8231 int ch, hflag = 0, Vflag = 0;
8232 char **cmd_argv = NULL;
8233 static const struct option longopts[] = {
8234 { "version", no_argument, NULL, 'V' },
8235 { NULL, 0, NULL, 0}
8237 char *diff_algo_str = NULL;
8239 setlocale(LC_CTYPE, "");
8241 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8242 switch (ch) {
8243 case 'h':
8244 hflag = 1;
8245 break;
8246 case 'V':
8247 Vflag = 1;
8248 break;
8249 default:
8250 usage(hflag, 1);
8251 /* NOTREACHED */
8255 argc -= optind;
8256 argv += optind;
8257 optind = 1;
8258 optreset = 1;
8260 if (Vflag) {
8261 got_version_print_str();
8262 return 0;
8265 #ifndef PROFILE
8266 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8267 NULL) == -1)
8268 err(1, "pledge");
8269 #endif
8271 if (argc == 0) {
8272 if (hflag)
8273 usage(hflag, 0);
8274 /* Build an argument vector which runs a default command. */
8275 cmd = &tog_commands[0];
8276 argc = 1;
8277 cmd_argv = make_argv(argc, cmd->name);
8278 } else {
8279 size_t i;
8281 /* Did the user specify a command? */
8282 for (i = 0; i < nitems(tog_commands); i++) {
8283 if (strncmp(tog_commands[i].name, argv[0],
8284 strlen(argv[0])) == 0) {
8285 cmd = &tog_commands[i];
8286 break;
8291 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8292 if (diff_algo_str) {
8293 if (strcasecmp(diff_algo_str, "patience") == 0)
8294 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8295 if (strcasecmp(diff_algo_str, "myers") == 0)
8296 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8299 if (cmd == NULL) {
8300 if (argc != 1)
8301 usage(0, 1);
8302 /* No command specified; try log with a path */
8303 error = tog_log_with_path(argc, argv);
8304 } else {
8305 if (hflag)
8306 cmd->cmd_usage();
8307 else
8308 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8311 endwin();
8312 putchar('\n');
8313 if (cmd_argv) {
8314 int i;
8315 for (i = 0; i < argc; i++)
8316 free(cmd_argv[i]);
8317 free(cmd_argv);
8320 if (error && error->code != GOT_ERR_CANCELLED)
8321 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8322 return 0;