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 && v->child->type == TOG_VIEW_LOG)
991 v->child->nscrolled = y - v->child->begin_y;
992 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
993 v->nscrolled = v->child->begin_y - y;
994 } else {
995 if (v->child->resized_x)
996 v->child->begin_x = v->child->resized_x;
997 if (view->parent)
998 v->child->begin_x -= resize;
999 else
1000 v->child->begin_x += resize;
1001 if (v->child->begin_x < 11) {
1002 view->count = 0;
1003 v->child->begin_x = 11;
1004 } else if (v->child->begin_x > COLS - 1) {
1005 view->count = 0;
1006 v->child->begin_x = COLS - 1;
1008 v->child->resized_x = v->child->begin_x;
1011 v->child->mode = v->mode;
1012 v->child->nlines = v->lines - v->child->begin_y;
1013 v->child->ncols = v->cols - v->child->begin_x;
1014 v->focus_child = 1;
1016 err = view_fullscreen(v);
1017 if (err)
1018 return err;
1019 err = view_splitscreen(v->child);
1020 if (err)
1021 return err;
1023 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1024 err = offset_selection_down(v->child);
1025 if (err)
1026 return err;
1029 if (v->nscrolled)
1030 err = request_log_commits(v);
1031 else if (v->child->nscrolled)
1032 err = request_log_commits(v->child);
1034 v->resize = v->child->resize = 0;
1036 return err;
1039 static void
1040 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1042 struct tog_view *v = src->child ? src->child : src;
1044 dst->resized_x = v->resized_x;
1045 dst->resized_y = v->resized_y;
1048 static const struct got_error *
1049 view_close_child(struct tog_view *view)
1051 const struct got_error *err = NULL;
1053 if (view->child == NULL)
1054 return NULL;
1056 err = view_close(view->child);
1057 view->child = NULL;
1058 return err;
1061 static const struct got_error *
1062 view_set_child(struct tog_view *view, struct tog_view *child)
1064 const struct got_error *err = NULL;
1066 view->child = child;
1067 child->parent = view;
1069 err = view_resize(view);
1070 if (err)
1071 return err;
1073 if (view->child->resized_x || view->child->resized_y)
1074 err = view_resize_split(view, 0);
1076 return err;
1079 static void
1080 tog_resizeterm(void)
1082 int cols, lines;
1083 struct winsize size;
1085 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1086 cols = 80; /* Default */
1087 lines = 24;
1088 } else {
1089 cols = size.ws_col;
1090 lines = size.ws_row;
1092 resize_term(lines, cols);
1095 static const struct got_error *
1096 view_search_start(struct tog_view *view)
1098 const struct got_error *err = NULL;
1099 struct tog_view *v = view;
1100 char pattern[1024];
1101 int ret;
1103 if (view->search_started) {
1104 regfree(&view->regex);
1105 view->searching = 0;
1106 memset(&view->regmatch, 0, sizeof(view->regmatch));
1108 view->search_started = 0;
1110 if (view->nlines < 1)
1111 return NULL;
1113 if (view_is_hsplit_top(view))
1114 v = view->child;
1116 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1117 wclrtoeol(v->window);
1119 nodelay(view->window, FALSE); /* block for search term input */
1120 nocbreak();
1121 echo();
1122 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1123 wrefresh(v->window);
1124 cbreak();
1125 noecho();
1126 nodelay(view->window, TRUE);
1127 if (ret == ERR)
1128 return NULL;
1130 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1131 err = view->search_start(view);
1132 if (err) {
1133 regfree(&view->regex);
1134 return err;
1136 view->search_started = 1;
1137 view->searching = TOG_SEARCH_FORWARD;
1138 view->search_next_done = 0;
1139 view->search_next(view);
1142 return NULL;
1145 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1146 static const struct got_error *
1147 switch_split(struct tog_view *view)
1149 const struct got_error *err = NULL;
1150 struct tog_view *v = NULL;
1152 if (view->parent)
1153 v = view->parent;
1154 else
1155 v = view;
1157 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1158 v->mode = TOG_VIEW_SPLIT_VERT;
1159 else
1160 v->mode = TOG_VIEW_SPLIT_HRZN;
1162 if (!v->child)
1163 return NULL;
1164 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1165 v->mode = TOG_VIEW_SPLIT_NONE;
1167 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1168 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1169 v->child->begin_y = v->child->resized_y;
1170 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1171 v->child->begin_x = v->child->resized_x;
1174 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1175 v->ncols = COLS;
1176 v->child->ncols = COLS;
1177 v->child->nscrolled = LINES - v->child->nlines;
1179 err = view_init_hsplit(v, v->child->begin_y);
1180 if (err)
1181 return err;
1183 v->child->mode = v->mode;
1184 v->child->nlines = v->lines - v->child->begin_y;
1185 v->focus_child = 1;
1187 err = view_fullscreen(v);
1188 if (err)
1189 return err;
1190 err = view_splitscreen(v->child);
1191 if (err)
1192 return err;
1194 if (v->mode == TOG_VIEW_SPLIT_NONE)
1195 v->mode = TOG_VIEW_SPLIT_VERT;
1196 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1197 err = offset_selection_down(v);
1198 err = offset_selection_down(v->child);
1199 } else {
1200 offset_selection_up(v);
1201 offset_selection_up(v->child);
1203 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1204 err = request_log_commits(v);
1205 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1206 err = request_log_commits(v->child);
1208 return err;
1212 * Compute view->count from numeric input. Assign total to view->count and
1213 * return first non-numeric key entered.
1215 static int
1216 get_compound_key(struct tog_view *view, int c)
1218 struct tog_view *v = view;
1219 int x, n = 0;
1221 if (view_is_hsplit_top(view))
1222 v = view->child;
1223 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1224 v = view->parent;
1226 view->count = 0;
1227 cbreak(); /* block for input */
1228 wmove(v->window, v->nlines - 1, 0);
1229 wclrtoeol(v->window);
1230 waddch(v->window, ':');
1232 do {
1233 x = getcurx(v->window);
1234 if (x != ERR && x < view->ncols) {
1235 waddch(v->window, c);
1236 wrefresh(v->window);
1240 * Don't overflow. Max valid request should be the greatest
1241 * between the longest and total lines; cap at 10 million.
1243 if (n >= 9999999)
1244 n = 9999999;
1245 else
1246 n = n * 10 + (c - '0');
1247 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1249 /* Massage excessive or inapplicable values at the input handler. */
1250 view->count = n;
1252 return c;
1255 static const struct got_error *
1256 view_input(struct tog_view **new, int *done, struct tog_view *view,
1257 struct tog_view_list_head *views)
1259 const struct got_error *err = NULL;
1260 struct tog_view *v;
1261 int ch, errcode;
1263 *new = NULL;
1265 /* Clear "no matches" indicator. */
1266 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1267 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1268 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1269 view->count = 0;
1272 if (view->searching && !view->search_next_done) {
1273 errcode = pthread_mutex_unlock(&tog_mutex);
1274 if (errcode)
1275 return got_error_set_errno(errcode,
1276 "pthread_mutex_unlock");
1277 sched_yield();
1278 errcode = pthread_mutex_lock(&tog_mutex);
1279 if (errcode)
1280 return got_error_set_errno(errcode,
1281 "pthread_mutex_lock");
1282 view->search_next(view);
1283 return NULL;
1286 nodelay(view->window, FALSE);
1287 /* Allow threads to make progress while we are waiting for input. */
1288 errcode = pthread_mutex_unlock(&tog_mutex);
1289 if (errcode)
1290 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1291 /* If we have an unfinished count, let C-g or backspace abort. */
1292 if (view->count && --view->count) {
1293 cbreak();
1294 nodelay(view->window, TRUE);
1295 ch = wgetch(view->window);
1296 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1297 view->count = 0;
1298 else
1299 ch = view->ch;
1300 } else {
1301 ch = wgetch(view->window);
1302 if (ch >= '1' && ch <= '9')
1303 view->ch = ch = get_compound_key(view, ch);
1305 errcode = pthread_mutex_lock(&tog_mutex);
1306 if (errcode)
1307 return got_error_set_errno(errcode, "pthread_mutex_lock");
1308 nodelay(view->window, TRUE);
1310 if (tog_sigwinch_received || tog_sigcont_received) {
1311 tog_resizeterm();
1312 tog_sigwinch_received = 0;
1313 tog_sigcont_received = 0;
1314 TAILQ_FOREACH(v, views, entry) {
1315 err = view_resize(v);
1316 if (err)
1317 return err;
1318 err = v->input(new, v, KEY_RESIZE);
1319 if (err)
1320 return err;
1321 if (v->child) {
1322 err = view_resize(v->child);
1323 if (err)
1324 return err;
1325 err = v->child->input(new, v->child,
1326 KEY_RESIZE);
1327 if (err)
1328 return err;
1329 if (v->child->resized_x || v->child->resized_y) {
1330 err = view_resize_split(v, 0);
1331 if (err)
1332 return err;
1338 switch (ch) {
1339 case '\t':
1340 view->count = 0;
1341 if (view->child) {
1342 view->focussed = 0;
1343 view->child->focussed = 1;
1344 view->focus_child = 1;
1345 } else if (view->parent) {
1346 view->focussed = 0;
1347 view->parent->focussed = 1;
1348 view->parent->focus_child = 0;
1349 if (!view_is_splitscreen(view)) {
1350 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1351 view->parent->type == TOG_VIEW_LOG) {
1352 err = request_log_commits(view->parent);
1353 if (err)
1354 return err;
1356 offset_selection_up(view->parent);
1357 err = view_fullscreen(view->parent);
1358 if (err)
1359 return err;
1362 break;
1363 case 'q':
1364 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1365 if (view->parent->type == TOG_VIEW_LOG) {
1366 /* might need more commits to fill fullscreen */
1367 err = request_log_commits(view->parent);
1368 if (err)
1369 break;
1371 offset_selection_up(view->parent);
1373 err = view->input(new, view, ch);
1374 view->dying = 1;
1375 break;
1376 case 'Q':
1377 *done = 1;
1378 break;
1379 case 'F':
1380 view->count = 0;
1381 if (view_is_parent_view(view)) {
1382 if (view->child == NULL)
1383 break;
1384 if (view_is_splitscreen(view->child)) {
1385 view->focussed = 0;
1386 view->child->focussed = 1;
1387 err = view_fullscreen(view->child);
1388 } else {
1389 err = view_splitscreen(view->child);
1390 if (!err)
1391 err = view_resize_split(view, 0);
1393 if (err)
1394 break;
1395 err = view->child->input(new, view->child,
1396 KEY_RESIZE);
1397 } else {
1398 if (view_is_splitscreen(view)) {
1399 view->parent->focussed = 0;
1400 view->focussed = 1;
1401 err = view_fullscreen(view);
1402 } else {
1403 err = view_splitscreen(view);
1404 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1405 err = view_resize(view->parent);
1406 if (!err)
1407 err = view_resize_split(view, 0);
1409 if (err)
1410 break;
1411 err = view->input(new, view, KEY_RESIZE);
1413 if (err)
1414 break;
1415 if (view->type == TOG_VIEW_LOG) {
1416 err = request_log_commits(view);
1417 if (err)
1418 break;
1420 if (view->parent)
1421 err = offset_selection_down(view->parent);
1422 if (!err)
1423 err = offset_selection_down(view);
1424 break;
1425 case 'S':
1426 view->count = 0;
1427 err = switch_split(view);
1428 break;
1429 case '-':
1430 err = view_resize_split(view, -1);
1431 break;
1432 case '+':
1433 err = view_resize_split(view, 1);
1434 break;
1435 case KEY_RESIZE:
1436 break;
1437 case '/':
1438 view->count = 0;
1439 if (view->search_start)
1440 view_search_start(view);
1441 else
1442 err = view->input(new, view, ch);
1443 break;
1444 case 'N':
1445 case 'n':
1446 if (view->search_started && view->search_next) {
1447 view->searching = (ch == 'n' ?
1448 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1449 view->search_next_done = 0;
1450 view->search_next(view);
1451 } else
1452 err = view->input(new, view, ch);
1453 break;
1454 case 'A':
1455 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1456 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1457 else
1458 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1459 TAILQ_FOREACH(v, views, entry) {
1460 if (v->reset) {
1461 err = v->reset(v);
1462 if (err)
1463 return err;
1465 if (v->child && v->child->reset) {
1466 err = v->child->reset(v->child);
1467 if (err)
1468 return err;
1471 break;
1472 default:
1473 err = view->input(new, view, ch);
1474 break;
1477 return err;
1480 static int
1481 view_needs_focus_indication(struct tog_view *view)
1483 if (view_is_parent_view(view)) {
1484 if (view->child == NULL || view->child->focussed)
1485 return 0;
1486 if (!view_is_splitscreen(view->child))
1487 return 0;
1488 } else if (!view_is_splitscreen(view))
1489 return 0;
1491 return view->focussed;
1494 static const struct got_error *
1495 view_loop(struct tog_view *view)
1497 const struct got_error *err = NULL;
1498 struct tog_view_list_head views;
1499 struct tog_view *new_view;
1500 char *mode;
1501 int fast_refresh = 10;
1502 int done = 0, errcode;
1504 mode = getenv("TOG_VIEW_SPLIT_MODE");
1505 if (!mode || !(*mode == 'h' || *mode == 'H'))
1506 view->mode = TOG_VIEW_SPLIT_VERT;
1507 else
1508 view->mode = TOG_VIEW_SPLIT_HRZN;
1510 errcode = pthread_mutex_lock(&tog_mutex);
1511 if (errcode)
1512 return got_error_set_errno(errcode, "pthread_mutex_lock");
1514 TAILQ_INIT(&views);
1515 TAILQ_INSERT_HEAD(&views, view, entry);
1517 view->focussed = 1;
1518 err = view->show(view);
1519 if (err)
1520 return err;
1521 update_panels();
1522 doupdate();
1523 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1524 !tog_fatal_signal_received()) {
1525 /* Refresh fast during initialization, then become slower. */
1526 if (fast_refresh && fast_refresh-- == 0)
1527 halfdelay(10); /* switch to once per second */
1529 err = view_input(&new_view, &done, view, &views);
1530 if (err)
1531 break;
1532 if (view->dying) {
1533 struct tog_view *v, *prev = NULL;
1535 if (view_is_parent_view(view))
1536 prev = TAILQ_PREV(view, tog_view_list_head,
1537 entry);
1538 else if (view->parent)
1539 prev = view->parent;
1541 if (view->parent) {
1542 view->parent->child = NULL;
1543 view->parent->focus_child = 0;
1544 /* Restore fullscreen line height. */
1545 view->parent->nlines = view->parent->lines;
1546 err = view_resize(view->parent);
1547 if (err)
1548 break;
1549 /* Make resized splits persist. */
1550 view_transfer_size(view->parent, view);
1551 } else
1552 TAILQ_REMOVE(&views, view, entry);
1554 err = view_close(view);
1555 if (err)
1556 goto done;
1558 view = NULL;
1559 TAILQ_FOREACH(v, &views, entry) {
1560 if (v->focussed)
1561 break;
1563 if (view == NULL && new_view == NULL) {
1564 /* No view has focus. Try to pick one. */
1565 if (prev)
1566 view = prev;
1567 else if (!TAILQ_EMPTY(&views)) {
1568 view = TAILQ_LAST(&views,
1569 tog_view_list_head);
1571 if (view) {
1572 if (view->focus_child) {
1573 view->child->focussed = 1;
1574 view = view->child;
1575 } else
1576 view->focussed = 1;
1580 if (new_view) {
1581 struct tog_view *v, *t;
1582 /* Only allow one parent view per type. */
1583 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1584 if (v->type != new_view->type)
1585 continue;
1586 TAILQ_REMOVE(&views, v, entry);
1587 err = view_close(v);
1588 if (err)
1589 goto done;
1590 break;
1592 TAILQ_INSERT_TAIL(&views, new_view, entry);
1593 view = new_view;
1595 if (view) {
1596 if (view_is_parent_view(view)) {
1597 if (view->child && view->child->focussed)
1598 view = view->child;
1599 } else {
1600 if (view->parent && view->parent->focussed)
1601 view = view->parent;
1603 show_panel(view->panel);
1604 if (view->child && view_is_splitscreen(view->child))
1605 show_panel(view->child->panel);
1606 if (view->parent && view_is_splitscreen(view)) {
1607 err = view->parent->show(view->parent);
1608 if (err)
1609 goto done;
1611 err = view->show(view);
1612 if (err)
1613 goto done;
1614 if (view->child) {
1615 err = view->child->show(view->child);
1616 if (err)
1617 goto done;
1619 update_panels();
1620 doupdate();
1623 done:
1624 while (!TAILQ_EMPTY(&views)) {
1625 const struct got_error *close_err;
1626 view = TAILQ_FIRST(&views);
1627 TAILQ_REMOVE(&views, view, entry);
1628 close_err = view_close(view);
1629 if (close_err && err == NULL)
1630 err = close_err;
1633 errcode = pthread_mutex_unlock(&tog_mutex);
1634 if (errcode && err == NULL)
1635 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1637 return err;
1640 __dead static void
1641 usage_log(void)
1643 endwin();
1644 fprintf(stderr,
1645 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1646 getprogname());
1647 exit(1);
1650 /* Create newly allocated wide-character string equivalent to a byte string. */
1651 static const struct got_error *
1652 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1654 char *vis = NULL;
1655 const struct got_error *err = NULL;
1657 *ws = NULL;
1658 *wlen = mbstowcs(NULL, s, 0);
1659 if (*wlen == (size_t)-1) {
1660 int vislen;
1661 if (errno != EILSEQ)
1662 return got_error_from_errno("mbstowcs");
1664 /* byte string invalid in current encoding; try to "fix" it */
1665 err = got_mbsavis(&vis, &vislen, s);
1666 if (err)
1667 return err;
1668 *wlen = mbstowcs(NULL, vis, 0);
1669 if (*wlen == (size_t)-1) {
1670 err = got_error_from_errno("mbstowcs"); /* give up */
1671 goto done;
1675 *ws = calloc(*wlen + 1, sizeof(**ws));
1676 if (*ws == NULL) {
1677 err = got_error_from_errno("calloc");
1678 goto done;
1681 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1682 err = got_error_from_errno("mbstowcs");
1683 done:
1684 free(vis);
1685 if (err) {
1686 free(*ws);
1687 *ws = NULL;
1688 *wlen = 0;
1690 return err;
1693 static const struct got_error *
1694 expand_tab(char **ptr, const char *src)
1696 char *dst;
1697 size_t len, n, idx = 0, sz = 0;
1699 *ptr = NULL;
1700 n = len = strlen(src);
1701 dst = malloc(n + 1);
1702 if (dst == NULL)
1703 return got_error_from_errno("malloc");
1705 while (idx < len && src[idx]) {
1706 const char c = src[idx];
1708 if (c == '\t') {
1709 size_t nb = TABSIZE - sz % TABSIZE;
1710 char *p;
1712 p = realloc(dst, n + nb);
1713 if (p == NULL) {
1714 free(dst);
1715 return got_error_from_errno("realloc");
1718 dst = p;
1719 n += nb;
1720 memset(dst + sz, ' ', nb);
1721 sz += nb;
1722 } else
1723 dst[sz++] = src[idx];
1724 ++idx;
1727 dst[sz] = '\0';
1728 *ptr = dst;
1729 return NULL;
1733 * Advance at most n columns from wline starting at offset off.
1734 * Return the index to the first character after the span operation.
1735 * Return the combined column width of all spanned wide character in
1736 * *rcol.
1738 static int
1739 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1741 int width, i, cols = 0;
1743 if (n == 0) {
1744 *rcol = cols;
1745 return off;
1748 for (i = off; wline[i] != L'\0'; ++i) {
1749 if (wline[i] == L'\t')
1750 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1751 else
1752 width = wcwidth(wline[i]);
1754 if (width == -1) {
1755 width = 1;
1756 wline[i] = L'.';
1759 if (cols + width > n)
1760 break;
1761 cols += width;
1764 *rcol = cols;
1765 return i;
1769 * Format a line for display, ensuring that it won't overflow a width limit.
1770 * With scrolling, the width returned refers to the scrolled version of the
1771 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1773 static const struct got_error *
1774 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1775 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1777 const struct got_error *err = NULL;
1778 int cols;
1779 wchar_t *wline = NULL;
1780 char *exstr = NULL;
1781 size_t wlen;
1782 int i, scrollx;
1784 *wlinep = NULL;
1785 *widthp = 0;
1787 if (expand) {
1788 err = expand_tab(&exstr, line);
1789 if (err)
1790 return err;
1793 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1794 free(exstr);
1795 if (err)
1796 return err;
1798 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1800 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1801 wline[wlen - 1] = L'\0';
1802 wlen--;
1804 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1805 wline[wlen - 1] = L'\0';
1806 wlen--;
1809 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1810 wline[i] = L'\0';
1812 if (widthp)
1813 *widthp = cols;
1814 if (scrollxp)
1815 *scrollxp = scrollx;
1816 if (err)
1817 free(wline);
1818 else
1819 *wlinep = wline;
1820 return err;
1823 static const struct got_error*
1824 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1825 struct got_object_id *id, struct got_repository *repo)
1827 static const struct got_error *err = NULL;
1828 struct got_reflist_entry *re;
1829 char *s;
1830 const char *name;
1832 *refs_str = NULL;
1834 TAILQ_FOREACH(re, refs, entry) {
1835 struct got_tag_object *tag = NULL;
1836 struct got_object_id *ref_id;
1837 int cmp;
1839 name = got_ref_get_name(re->ref);
1840 if (strcmp(name, GOT_REF_HEAD) == 0)
1841 continue;
1842 if (strncmp(name, "refs/", 5) == 0)
1843 name += 5;
1844 if (strncmp(name, "got/", 4) == 0 &&
1845 strncmp(name, "got/backup/", 11) != 0)
1846 continue;
1847 if (strncmp(name, "heads/", 6) == 0)
1848 name += 6;
1849 if (strncmp(name, "remotes/", 8) == 0) {
1850 name += 8;
1851 s = strstr(name, "/" GOT_REF_HEAD);
1852 if (s != NULL && s[strlen(s)] == '\0')
1853 continue;
1855 err = got_ref_resolve(&ref_id, repo, re->ref);
1856 if (err)
1857 break;
1858 if (strncmp(name, "tags/", 5) == 0) {
1859 err = got_object_open_as_tag(&tag, repo, ref_id);
1860 if (err) {
1861 if (err->code != GOT_ERR_OBJ_TYPE) {
1862 free(ref_id);
1863 break;
1865 /* Ref points at something other than a tag. */
1866 err = NULL;
1867 tag = NULL;
1870 cmp = got_object_id_cmp(tag ?
1871 got_object_tag_get_object_id(tag) : ref_id, id);
1872 free(ref_id);
1873 if (tag)
1874 got_object_tag_close(tag);
1875 if (cmp != 0)
1876 continue;
1877 s = *refs_str;
1878 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1879 s ? ", " : "", name) == -1) {
1880 err = got_error_from_errno("asprintf");
1881 free(s);
1882 *refs_str = NULL;
1883 break;
1885 free(s);
1888 return err;
1891 static const struct got_error *
1892 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1893 int col_tab_align)
1895 char *smallerthan;
1897 smallerthan = strchr(author, '<');
1898 if (smallerthan && smallerthan[1] != '\0')
1899 author = smallerthan + 1;
1900 author[strcspn(author, "@>")] = '\0';
1901 return format_line(wauthor, author_width, NULL, author, 0, limit,
1902 col_tab_align, 0);
1905 static const struct got_error *
1906 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1907 struct got_object_id *id, const size_t date_display_cols,
1908 int author_display_cols)
1910 struct tog_log_view_state *s = &view->state.log;
1911 const struct got_error *err = NULL;
1912 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1913 char *logmsg0 = NULL, *logmsg = NULL;
1914 char *author = NULL;
1915 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1916 int author_width, logmsg_width;
1917 char *newline, *line = NULL;
1918 int col, limit, scrollx;
1919 const int avail = view->ncols;
1920 struct tm tm;
1921 time_t committer_time;
1922 struct tog_color *tc;
1924 committer_time = got_object_commit_get_committer_time(commit);
1925 if (gmtime_r(&committer_time, &tm) == NULL)
1926 return got_error_from_errno("gmtime_r");
1927 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1928 return got_error(GOT_ERR_NO_SPACE);
1930 if (avail <= date_display_cols)
1931 limit = MIN(sizeof(datebuf) - 1, avail);
1932 else
1933 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1934 tc = get_color(&s->colors, TOG_COLOR_DATE);
1935 if (tc)
1936 wattr_on(view->window,
1937 COLOR_PAIR(tc->colorpair), NULL);
1938 waddnstr(view->window, datebuf, limit);
1939 if (tc)
1940 wattr_off(view->window,
1941 COLOR_PAIR(tc->colorpair), NULL);
1942 col = limit;
1943 if (col > avail)
1944 goto done;
1946 if (avail >= 120) {
1947 char *id_str;
1948 err = got_object_id_str(&id_str, id);
1949 if (err)
1950 goto done;
1951 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1952 if (tc)
1953 wattr_on(view->window,
1954 COLOR_PAIR(tc->colorpair), NULL);
1955 wprintw(view->window, "%.8s ", id_str);
1956 if (tc)
1957 wattr_off(view->window,
1958 COLOR_PAIR(tc->colorpair), NULL);
1959 free(id_str);
1960 col += 9;
1961 if (col > avail)
1962 goto done;
1965 author = strdup(got_object_commit_get_author(commit));
1966 if (author == NULL) {
1967 err = got_error_from_errno("strdup");
1968 goto done;
1970 err = format_author(&wauthor, &author_width, author, avail - col, col);
1971 if (err)
1972 goto done;
1973 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1974 if (tc)
1975 wattr_on(view->window,
1976 COLOR_PAIR(tc->colorpair), NULL);
1977 waddwstr(view->window, wauthor);
1978 if (tc)
1979 wattr_off(view->window,
1980 COLOR_PAIR(tc->colorpair), NULL);
1981 col += author_width;
1982 while (col < avail && author_width < author_display_cols + 2) {
1983 waddch(view->window, ' ');
1984 col++;
1985 author_width++;
1987 if (col > avail)
1988 goto done;
1990 err = got_object_commit_get_logmsg(&logmsg0, commit);
1991 if (err)
1992 goto done;
1993 logmsg = logmsg0;
1994 while (*logmsg == '\n')
1995 logmsg++;
1996 newline = strchr(logmsg, '\n');
1997 if (newline)
1998 *newline = '\0';
1999 limit = avail - col;
2000 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2001 limit--; /* for the border */
2002 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2003 limit, col, 1);
2004 if (err)
2005 goto done;
2006 waddwstr(view->window, &wlogmsg[scrollx]);
2007 col += MAX(logmsg_width, 0);
2008 while (col < avail) {
2009 waddch(view->window, ' ');
2010 col++;
2012 done:
2013 free(logmsg0);
2014 free(wlogmsg);
2015 free(author);
2016 free(wauthor);
2017 free(line);
2018 return err;
2021 static struct commit_queue_entry *
2022 alloc_commit_queue_entry(struct got_commit_object *commit,
2023 struct got_object_id *id)
2025 struct commit_queue_entry *entry;
2027 entry = calloc(1, sizeof(*entry));
2028 if (entry == NULL)
2029 return NULL;
2031 entry->id = id;
2032 entry->commit = commit;
2033 return entry;
2036 static void
2037 pop_commit(struct commit_queue *commits)
2039 struct commit_queue_entry *entry;
2041 entry = TAILQ_FIRST(&commits->head);
2042 TAILQ_REMOVE(&commits->head, entry, entry);
2043 got_object_commit_close(entry->commit);
2044 commits->ncommits--;
2045 /* Don't free entry->id! It is owned by the commit graph. */
2046 free(entry);
2049 static void
2050 free_commits(struct commit_queue *commits)
2052 while (!TAILQ_EMPTY(&commits->head))
2053 pop_commit(commits);
2056 static const struct got_error *
2057 match_commit(int *have_match, struct got_object_id *id,
2058 struct got_commit_object *commit, regex_t *regex)
2060 const struct got_error *err = NULL;
2061 regmatch_t regmatch;
2062 char *id_str = NULL, *logmsg = NULL;
2064 *have_match = 0;
2066 err = got_object_id_str(&id_str, id);
2067 if (err)
2068 return err;
2070 err = got_object_commit_get_logmsg(&logmsg, commit);
2071 if (err)
2072 goto done;
2074 if (regexec(regex, got_object_commit_get_author(commit), 1,
2075 &regmatch, 0) == 0 ||
2076 regexec(regex, got_object_commit_get_committer(commit), 1,
2077 &regmatch, 0) == 0 ||
2078 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2079 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2080 *have_match = 1;
2081 done:
2082 free(id_str);
2083 free(logmsg);
2084 return err;
2087 static const struct got_error *
2088 queue_commits(struct tog_log_thread_args *a)
2090 const struct got_error *err = NULL;
2093 * We keep all commits open throughout the lifetime of the log
2094 * view in order to avoid having to re-fetch commits from disk
2095 * while updating the display.
2097 do {
2098 struct got_object_id *id;
2099 struct got_commit_object *commit;
2100 struct commit_queue_entry *entry;
2101 int errcode;
2103 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2104 NULL, NULL);
2105 if (err || id == NULL)
2106 break;
2108 err = got_object_open_as_commit(&commit, a->repo, id);
2109 if (err)
2110 break;
2111 entry = alloc_commit_queue_entry(commit, id);
2112 if (entry == NULL) {
2113 err = got_error_from_errno("alloc_commit_queue_entry");
2114 break;
2117 errcode = pthread_mutex_lock(&tog_mutex);
2118 if (errcode) {
2119 err = got_error_set_errno(errcode,
2120 "pthread_mutex_lock");
2121 break;
2124 entry->idx = a->commits->ncommits;
2125 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2126 a->commits->ncommits++;
2128 if (*a->searching == TOG_SEARCH_FORWARD &&
2129 !*a->search_next_done) {
2130 int have_match;
2131 err = match_commit(&have_match, id, commit, a->regex);
2132 if (err)
2133 break;
2134 if (have_match)
2135 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2138 errcode = pthread_mutex_unlock(&tog_mutex);
2139 if (errcode && err == NULL)
2140 err = got_error_set_errno(errcode,
2141 "pthread_mutex_unlock");
2142 if (err)
2143 break;
2144 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2146 return err;
2149 static void
2150 select_commit(struct tog_log_view_state *s)
2152 struct commit_queue_entry *entry;
2153 int ncommits = 0;
2155 entry = s->first_displayed_entry;
2156 while (entry) {
2157 if (ncommits == s->selected) {
2158 s->selected_entry = entry;
2159 break;
2161 entry = TAILQ_NEXT(entry, entry);
2162 ncommits++;
2166 static const struct got_error *
2167 draw_commits(struct tog_view *view)
2169 const struct got_error *err = NULL;
2170 struct tog_log_view_state *s = &view->state.log;
2171 struct commit_queue_entry *entry = s->selected_entry;
2172 const int limit = view->nlines;
2173 int width;
2174 int ncommits, author_cols = 4;
2175 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2176 char *refs_str = NULL;
2177 wchar_t *wline;
2178 struct tog_color *tc;
2179 static const size_t date_display_cols = 12;
2181 if (s->selected_entry &&
2182 !(view->searching && view->search_next_done == 0)) {
2183 struct got_reflist_head *refs;
2184 err = got_object_id_str(&id_str, s->selected_entry->id);
2185 if (err)
2186 return err;
2187 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2188 s->selected_entry->id);
2189 if (refs) {
2190 err = build_refs_str(&refs_str, refs,
2191 s->selected_entry->id, s->repo);
2192 if (err)
2193 goto done;
2197 if (s->thread_args.commits_needed == 0)
2198 halfdelay(10); /* disable fast refresh */
2200 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2201 if (asprintf(&ncommits_str, " [%d/%d] %s",
2202 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2203 (view->searching && !view->search_next_done) ?
2204 "searching..." : "loading...") == -1) {
2205 err = got_error_from_errno("asprintf");
2206 goto done;
2208 } else {
2209 const char *search_str = NULL;
2211 if (view->searching) {
2212 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2213 search_str = "no more matches";
2214 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2215 search_str = "no matches found";
2216 else if (!view->search_next_done)
2217 search_str = "searching...";
2220 if (asprintf(&ncommits_str, " [%d/%d] %s",
2221 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2222 search_str ? search_str :
2223 (refs_str ? refs_str : "")) == -1) {
2224 err = got_error_from_errno("asprintf");
2225 goto done;
2229 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2230 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2231 "........................................",
2232 s->in_repo_path, ncommits_str) == -1) {
2233 err = got_error_from_errno("asprintf");
2234 header = NULL;
2235 goto done;
2237 } else if (asprintf(&header, "commit %s%s",
2238 id_str ? id_str : "........................................",
2239 ncommits_str) == -1) {
2240 err = got_error_from_errno("asprintf");
2241 header = NULL;
2242 goto done;
2244 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2245 if (err)
2246 goto done;
2248 werase(view->window);
2250 if (view_needs_focus_indication(view))
2251 wstandout(view->window);
2252 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2253 if (tc)
2254 wattr_on(view->window,
2255 COLOR_PAIR(tc->colorpair), NULL);
2256 waddwstr(view->window, wline);
2257 if (tc)
2258 wattr_off(view->window,
2259 COLOR_PAIR(tc->colorpair), NULL);
2260 while (width < view->ncols) {
2261 waddch(view->window, ' ');
2262 width++;
2264 if (view_needs_focus_indication(view))
2265 wstandend(view->window);
2266 free(wline);
2267 if (limit <= 1)
2268 goto done;
2270 /* Grow author column size if necessary, and set view->maxx. */
2271 entry = s->first_displayed_entry;
2272 ncommits = 0;
2273 view->maxx = 0;
2274 while (entry) {
2275 char *author, *eol, *msg, *msg0;
2276 wchar_t *wauthor, *wmsg;
2277 int width;
2278 if (ncommits >= limit - 1)
2279 break;
2280 author = strdup(got_object_commit_get_author(entry->commit));
2281 if (author == NULL) {
2282 err = got_error_from_errno("strdup");
2283 goto done;
2285 err = format_author(&wauthor, &width, author, COLS,
2286 date_display_cols);
2287 if (author_cols < width)
2288 author_cols = width;
2289 free(wauthor);
2290 free(author);
2291 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2292 if (err)
2293 goto done;
2294 msg = msg0;
2295 while (*msg == '\n')
2296 ++msg;
2297 if ((eol = strchr(msg, '\n')))
2298 *eol = '\0';
2299 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2300 date_display_cols + author_cols, 0);
2301 if (err)
2302 goto done;
2303 view->maxx = MAX(view->maxx, width);
2304 free(msg0);
2305 free(wmsg);
2306 ncommits++;
2307 entry = TAILQ_NEXT(entry, entry);
2310 entry = s->first_displayed_entry;
2311 s->last_displayed_entry = s->first_displayed_entry;
2312 ncommits = 0;
2313 while (entry) {
2314 if (ncommits >= limit - 1)
2315 break;
2316 if (ncommits == s->selected)
2317 wstandout(view->window);
2318 err = draw_commit(view, entry->commit, entry->id,
2319 date_display_cols, author_cols);
2320 if (ncommits == s->selected)
2321 wstandend(view->window);
2322 if (err)
2323 goto done;
2324 ncommits++;
2325 s->last_displayed_entry = entry;
2326 entry = TAILQ_NEXT(entry, entry);
2329 view_border(view);
2330 done:
2331 free(id_str);
2332 free(refs_str);
2333 free(ncommits_str);
2334 free(header);
2335 return err;
2338 static void
2339 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2341 struct commit_queue_entry *entry;
2342 int nscrolled = 0;
2344 entry = TAILQ_FIRST(&s->commits.head);
2345 if (s->first_displayed_entry == entry)
2346 return;
2348 entry = s->first_displayed_entry;
2349 while (entry && nscrolled < maxscroll) {
2350 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2351 if (entry) {
2352 s->first_displayed_entry = entry;
2353 nscrolled++;
2358 static const struct got_error *
2359 trigger_log_thread(struct tog_view *view, int wait)
2361 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2362 int errcode;
2364 halfdelay(1); /* fast refresh while loading commits */
2366 while (!ta->log_complete && !tog_thread_error &&
2367 (ta->commits_needed > 0 || ta->load_all)) {
2368 /* Wake the log thread. */
2369 errcode = pthread_cond_signal(&ta->need_commits);
2370 if (errcode)
2371 return got_error_set_errno(errcode,
2372 "pthread_cond_signal");
2375 * The mutex will be released while the view loop waits
2376 * in wgetch(), at which time the log thread will run.
2378 if (!wait)
2379 break;
2381 /* Display progress update in log view. */
2382 show_log_view(view);
2383 update_panels();
2384 doupdate();
2386 /* Wait right here while next commit is being loaded. */
2387 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2388 if (errcode)
2389 return got_error_set_errno(errcode,
2390 "pthread_cond_wait");
2392 /* Display progress update in log view. */
2393 show_log_view(view);
2394 update_panels();
2395 doupdate();
2398 return NULL;
2401 static const struct got_error *
2402 request_log_commits(struct tog_view *view)
2404 struct tog_log_view_state *state = &view->state.log;
2405 const struct got_error *err = NULL;
2407 if (state->thread_args.log_complete)
2408 return NULL;
2410 state->thread_args.commits_needed += view->nscrolled;
2411 err = trigger_log_thread(view, 1);
2412 view->nscrolled = 0;
2414 return err;
2417 static const struct got_error *
2418 log_scroll_down(struct tog_view *view, int maxscroll)
2420 struct tog_log_view_state *s = &view->state.log;
2421 const struct got_error *err = NULL;
2422 struct commit_queue_entry *pentry;
2423 int nscrolled = 0, ncommits_needed;
2425 if (s->last_displayed_entry == NULL)
2426 return NULL;
2428 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2429 if (s->commits.ncommits < ncommits_needed &&
2430 !s->thread_args.log_complete) {
2432 * Ask the log thread for required amount of commits.
2434 s->thread_args.commits_needed += maxscroll;
2435 err = trigger_log_thread(view, 1);
2436 if (err)
2437 return err;
2440 do {
2441 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2442 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2443 break;
2445 s->last_displayed_entry = pentry ?
2446 pentry : s->last_displayed_entry;;
2448 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2449 if (pentry == NULL)
2450 break;
2451 s->first_displayed_entry = pentry;
2452 } while (++nscrolled < maxscroll);
2454 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2455 view->nscrolled += nscrolled;
2456 else
2457 view->nscrolled = 0;
2459 return err;
2462 static const struct got_error *
2463 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2464 struct got_commit_object *commit, struct got_object_id *commit_id,
2465 struct tog_view *log_view, struct got_repository *repo)
2467 const struct got_error *err;
2468 struct got_object_qid *parent_id;
2469 struct tog_view *diff_view;
2471 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2472 if (diff_view == NULL)
2473 return got_error_from_errno("view_open");
2475 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2476 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2477 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2478 if (err == NULL)
2479 *new_view = diff_view;
2480 return err;
2483 static const struct got_error *
2484 tree_view_visit_subtree(struct tog_tree_view_state *s,
2485 struct got_tree_object *subtree)
2487 struct tog_parent_tree *parent;
2489 parent = calloc(1, sizeof(*parent));
2490 if (parent == NULL)
2491 return got_error_from_errno("calloc");
2493 parent->tree = s->tree;
2494 parent->first_displayed_entry = s->first_displayed_entry;
2495 parent->selected_entry = s->selected_entry;
2496 parent->selected = s->selected;
2497 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2498 s->tree = subtree;
2499 s->selected = 0;
2500 s->first_displayed_entry = NULL;
2501 return NULL;
2504 static const struct got_error *
2505 tree_view_walk_path(struct tog_tree_view_state *s,
2506 struct got_commit_object *commit, const char *path)
2508 const struct got_error *err = NULL;
2509 struct got_tree_object *tree = NULL;
2510 const char *p;
2511 char *slash, *subpath = NULL;
2513 /* Walk the path and open corresponding tree objects. */
2514 p = path;
2515 while (*p) {
2516 struct got_tree_entry *te;
2517 struct got_object_id *tree_id;
2518 char *te_name;
2520 while (p[0] == '/')
2521 p++;
2523 /* Ensure the correct subtree entry is selected. */
2524 slash = strchr(p, '/');
2525 if (slash == NULL)
2526 te_name = strdup(p);
2527 else
2528 te_name = strndup(p, slash - p);
2529 if (te_name == NULL) {
2530 err = got_error_from_errno("strndup");
2531 break;
2533 te = got_object_tree_find_entry(s->tree, te_name);
2534 if (te == NULL) {
2535 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2536 free(te_name);
2537 break;
2539 free(te_name);
2540 s->first_displayed_entry = s->selected_entry = te;
2542 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2543 break; /* jump to this file's entry */
2545 slash = strchr(p, '/');
2546 if (slash)
2547 subpath = strndup(path, slash - path);
2548 else
2549 subpath = strdup(path);
2550 if (subpath == NULL) {
2551 err = got_error_from_errno("strdup");
2552 break;
2555 err = got_object_id_by_path(&tree_id, s->repo, commit,
2556 subpath);
2557 if (err)
2558 break;
2560 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2561 free(tree_id);
2562 if (err)
2563 break;
2565 err = tree_view_visit_subtree(s, tree);
2566 if (err) {
2567 got_object_tree_close(tree);
2568 break;
2570 if (slash == NULL)
2571 break;
2572 free(subpath);
2573 subpath = NULL;
2574 p = slash;
2577 free(subpath);
2578 return err;
2581 static const struct got_error *
2582 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2583 struct commit_queue_entry *entry, const char *path,
2584 const char *head_ref_name, struct got_repository *repo)
2586 const struct got_error *err = NULL;
2587 struct tog_tree_view_state *s;
2588 struct tog_view *tree_view;
2590 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2591 if (tree_view == NULL)
2592 return got_error_from_errno("view_open");
2594 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2595 if (err)
2596 return err;
2597 s = &tree_view->state.tree;
2599 *new_view = tree_view;
2601 if (got_path_is_root_dir(path))
2602 return NULL;
2604 return tree_view_walk_path(s, entry->commit, path);
2607 static const struct got_error *
2608 block_signals_used_by_main_thread(void)
2610 sigset_t sigset;
2611 int errcode;
2613 if (sigemptyset(&sigset) == -1)
2614 return got_error_from_errno("sigemptyset");
2616 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2617 if (sigaddset(&sigset, SIGWINCH) == -1)
2618 return got_error_from_errno("sigaddset");
2619 if (sigaddset(&sigset, SIGCONT) == -1)
2620 return got_error_from_errno("sigaddset");
2621 if (sigaddset(&sigset, SIGINT) == -1)
2622 return got_error_from_errno("sigaddset");
2623 if (sigaddset(&sigset, SIGTERM) == -1)
2624 return got_error_from_errno("sigaddset");
2626 /* ncurses handles SIGTSTP */
2627 if (sigaddset(&sigset, SIGTSTP) == -1)
2628 return got_error_from_errno("sigaddset");
2630 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2631 if (errcode)
2632 return got_error_set_errno(errcode, "pthread_sigmask");
2634 return NULL;
2637 static void *
2638 log_thread(void *arg)
2640 const struct got_error *err = NULL;
2641 int errcode = 0;
2642 struct tog_log_thread_args *a = arg;
2643 int done = 0;
2646 * Sync startup with main thread such that we begin our
2647 * work once view_input() has released the mutex.
2649 errcode = pthread_mutex_lock(&tog_mutex);
2650 if (errcode) {
2651 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2652 return (void *)err;
2655 err = block_signals_used_by_main_thread();
2656 if (err) {
2657 pthread_mutex_unlock(&tog_mutex);
2658 goto done;
2661 while (!done && !err && !tog_fatal_signal_received()) {
2662 errcode = pthread_mutex_unlock(&tog_mutex);
2663 if (errcode) {
2664 err = got_error_set_errno(errcode,
2665 "pthread_mutex_unlock");
2666 goto done;
2668 err = queue_commits(a);
2669 if (err) {
2670 if (err->code != GOT_ERR_ITER_COMPLETED)
2671 goto done;
2672 err = NULL;
2673 done = 1;
2674 } else if (a->commits_needed > 0 && !a->load_all)
2675 a->commits_needed--;
2677 errcode = pthread_mutex_lock(&tog_mutex);
2678 if (errcode) {
2679 err = got_error_set_errno(errcode,
2680 "pthread_mutex_lock");
2681 goto done;
2682 } else if (*a->quit)
2683 done = 1;
2684 else if (*a->first_displayed_entry == NULL) {
2685 *a->first_displayed_entry =
2686 TAILQ_FIRST(&a->commits->head);
2687 *a->selected_entry = *a->first_displayed_entry;
2690 errcode = pthread_cond_signal(&a->commit_loaded);
2691 if (errcode) {
2692 err = got_error_set_errno(errcode,
2693 "pthread_cond_signal");
2694 pthread_mutex_unlock(&tog_mutex);
2695 goto done;
2698 if (done)
2699 a->commits_needed = 0;
2700 else {
2701 if (a->commits_needed == 0 && !a->load_all) {
2702 errcode = pthread_cond_wait(&a->need_commits,
2703 &tog_mutex);
2704 if (errcode) {
2705 err = got_error_set_errno(errcode,
2706 "pthread_cond_wait");
2707 pthread_mutex_unlock(&tog_mutex);
2708 goto done;
2710 if (*a->quit)
2711 done = 1;
2715 a->log_complete = 1;
2716 errcode = pthread_mutex_unlock(&tog_mutex);
2717 if (errcode)
2718 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2719 done:
2720 if (err) {
2721 tog_thread_error = 1;
2722 pthread_cond_signal(&a->commit_loaded);
2724 return (void *)err;
2727 static const struct got_error *
2728 stop_log_thread(struct tog_log_view_state *s)
2730 const struct got_error *err = NULL, *thread_err = NULL;
2731 int errcode;
2733 if (s->thread) {
2734 s->quit = 1;
2735 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2736 if (errcode)
2737 return got_error_set_errno(errcode,
2738 "pthread_cond_signal");
2739 errcode = pthread_mutex_unlock(&tog_mutex);
2740 if (errcode)
2741 return got_error_set_errno(errcode,
2742 "pthread_mutex_unlock");
2743 errcode = pthread_join(s->thread, (void **)&thread_err);
2744 if (errcode)
2745 return got_error_set_errno(errcode, "pthread_join");
2746 errcode = pthread_mutex_lock(&tog_mutex);
2747 if (errcode)
2748 return got_error_set_errno(errcode,
2749 "pthread_mutex_lock");
2750 s->thread = NULL;
2753 if (s->thread_args.repo) {
2754 err = got_repo_close(s->thread_args.repo);
2755 s->thread_args.repo = NULL;
2758 if (s->thread_args.pack_fds) {
2759 const struct got_error *pack_err =
2760 got_repo_pack_fds_close(s->thread_args.pack_fds);
2761 if (err == NULL)
2762 err = pack_err;
2763 s->thread_args.pack_fds = NULL;
2766 if (s->thread_args.graph) {
2767 got_commit_graph_close(s->thread_args.graph);
2768 s->thread_args.graph = NULL;
2771 return err ? err : thread_err;
2774 static const struct got_error *
2775 close_log_view(struct tog_view *view)
2777 const struct got_error *err = NULL;
2778 struct tog_log_view_state *s = &view->state.log;
2779 int errcode;
2781 err = stop_log_thread(s);
2783 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2784 if (errcode && err == NULL)
2785 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2787 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2788 if (errcode && err == NULL)
2789 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2791 free_commits(&s->commits);
2792 free(s->in_repo_path);
2793 s->in_repo_path = NULL;
2794 free(s->start_id);
2795 s->start_id = NULL;
2796 free(s->head_ref_name);
2797 s->head_ref_name = NULL;
2798 return err;
2801 static const struct got_error *
2802 search_start_log_view(struct tog_view *view)
2804 struct tog_log_view_state *s = &view->state.log;
2806 s->matched_entry = NULL;
2807 s->search_entry = NULL;
2808 return NULL;
2811 static const struct got_error *
2812 search_next_log_view(struct tog_view *view)
2814 const struct got_error *err = NULL;
2815 struct tog_log_view_state *s = &view->state.log;
2816 struct commit_queue_entry *entry;
2818 /* Display progress update in log view. */
2819 show_log_view(view);
2820 update_panels();
2821 doupdate();
2823 if (s->search_entry) {
2824 int errcode, ch;
2825 errcode = pthread_mutex_unlock(&tog_mutex);
2826 if (errcode)
2827 return got_error_set_errno(errcode,
2828 "pthread_mutex_unlock");
2829 ch = wgetch(view->window);
2830 errcode = pthread_mutex_lock(&tog_mutex);
2831 if (errcode)
2832 return got_error_set_errno(errcode,
2833 "pthread_mutex_lock");
2834 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2835 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2836 return NULL;
2838 if (view->searching == TOG_SEARCH_FORWARD)
2839 entry = TAILQ_NEXT(s->search_entry, entry);
2840 else
2841 entry = TAILQ_PREV(s->search_entry,
2842 commit_queue_head, entry);
2843 } else if (s->matched_entry) {
2844 int matched_idx = s->matched_entry->idx;
2845 int selected_idx = s->selected_entry->idx;
2848 * If the user has moved the cursor after we hit a match,
2849 * the position from where we should continue searching
2850 * might have changed.
2852 if (view->searching == TOG_SEARCH_FORWARD) {
2853 if (matched_idx > selected_idx)
2854 entry = TAILQ_NEXT(s->selected_entry, entry);
2855 else
2856 entry = TAILQ_NEXT(s->matched_entry, entry);
2857 } else {
2858 if (matched_idx < selected_idx)
2859 entry = TAILQ_PREV(s->selected_entry,
2860 commit_queue_head, entry);
2861 else
2862 entry = TAILQ_PREV(s->matched_entry,
2863 commit_queue_head, entry);
2865 } else {
2866 entry = s->selected_entry;
2869 while (1) {
2870 int have_match = 0;
2872 if (entry == NULL) {
2873 if (s->thread_args.log_complete ||
2874 view->searching == TOG_SEARCH_BACKWARD) {
2875 view->search_next_done =
2876 (s->matched_entry == NULL ?
2877 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2878 s->search_entry = NULL;
2879 return NULL;
2882 * Poke the log thread for more commits and return,
2883 * allowing the main loop to make progress. Search
2884 * will resume at s->search_entry once we come back.
2886 s->thread_args.commits_needed++;
2887 return trigger_log_thread(view, 0);
2890 err = match_commit(&have_match, entry->id, entry->commit,
2891 &view->regex);
2892 if (err)
2893 break;
2894 if (have_match) {
2895 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2896 s->matched_entry = entry;
2897 break;
2900 s->search_entry = entry;
2901 if (view->searching == TOG_SEARCH_FORWARD)
2902 entry = TAILQ_NEXT(entry, entry);
2903 else
2904 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2907 if (s->matched_entry) {
2908 int cur = s->selected_entry->idx;
2909 while (cur < s->matched_entry->idx) {
2910 err = input_log_view(NULL, view, KEY_DOWN);
2911 if (err)
2912 return err;
2913 cur++;
2915 while (cur > s->matched_entry->idx) {
2916 err = input_log_view(NULL, view, KEY_UP);
2917 if (err)
2918 return err;
2919 cur--;
2923 s->search_entry = NULL;
2925 return NULL;
2928 static const struct got_error *
2929 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2930 struct got_repository *repo, const char *head_ref_name,
2931 const char *in_repo_path, int log_branches)
2933 const struct got_error *err = NULL;
2934 struct tog_log_view_state *s = &view->state.log;
2935 struct got_repository *thread_repo = NULL;
2936 struct got_commit_graph *thread_graph = NULL;
2937 int errcode;
2939 if (in_repo_path != s->in_repo_path) {
2940 free(s->in_repo_path);
2941 s->in_repo_path = strdup(in_repo_path);
2942 if (s->in_repo_path == NULL)
2943 return got_error_from_errno("strdup");
2946 /* The commit queue only contains commits being displayed. */
2947 TAILQ_INIT(&s->commits.head);
2948 s->commits.ncommits = 0;
2950 s->repo = repo;
2951 if (head_ref_name) {
2952 s->head_ref_name = strdup(head_ref_name);
2953 if (s->head_ref_name == NULL) {
2954 err = got_error_from_errno("strdup");
2955 goto done;
2958 s->start_id = got_object_id_dup(start_id);
2959 if (s->start_id == NULL) {
2960 err = got_error_from_errno("got_object_id_dup");
2961 goto done;
2963 s->log_branches = log_branches;
2965 STAILQ_INIT(&s->colors);
2966 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2967 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2968 get_color_value("TOG_COLOR_COMMIT"));
2969 if (err)
2970 goto done;
2971 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2972 get_color_value("TOG_COLOR_AUTHOR"));
2973 if (err) {
2974 free_colors(&s->colors);
2975 goto done;
2977 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2978 get_color_value("TOG_COLOR_DATE"));
2979 if (err) {
2980 free_colors(&s->colors);
2981 goto done;
2985 view->show = show_log_view;
2986 view->input = input_log_view;
2987 view->close = close_log_view;
2988 view->search_start = search_start_log_view;
2989 view->search_next = search_next_log_view;
2991 if (s->thread_args.pack_fds == NULL) {
2992 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2993 if (err)
2994 goto done;
2996 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2997 s->thread_args.pack_fds);
2998 if (err)
2999 goto done;
3000 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3001 !s->log_branches);
3002 if (err)
3003 goto done;
3004 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3005 s->repo, NULL, NULL);
3006 if (err)
3007 goto done;
3009 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3010 if (errcode) {
3011 err = got_error_set_errno(errcode, "pthread_cond_init");
3012 goto done;
3014 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3015 if (errcode) {
3016 err = got_error_set_errno(errcode, "pthread_cond_init");
3017 goto done;
3020 s->thread_args.commits_needed = view->nlines;
3021 s->thread_args.graph = thread_graph;
3022 s->thread_args.commits = &s->commits;
3023 s->thread_args.in_repo_path = s->in_repo_path;
3024 s->thread_args.start_id = s->start_id;
3025 s->thread_args.repo = thread_repo;
3026 s->thread_args.log_complete = 0;
3027 s->thread_args.quit = &s->quit;
3028 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3029 s->thread_args.selected_entry = &s->selected_entry;
3030 s->thread_args.searching = &view->searching;
3031 s->thread_args.search_next_done = &view->search_next_done;
3032 s->thread_args.regex = &view->regex;
3033 done:
3034 if (err)
3035 close_log_view(view);
3036 return err;
3039 static const struct got_error *
3040 show_log_view(struct tog_view *view)
3042 const struct got_error *err;
3043 struct tog_log_view_state *s = &view->state.log;
3045 if (s->thread == NULL) {
3046 int errcode = pthread_create(&s->thread, NULL, log_thread,
3047 &s->thread_args);
3048 if (errcode)
3049 return got_error_set_errno(errcode, "pthread_create");
3050 if (s->thread_args.commits_needed > 0) {
3051 err = trigger_log_thread(view, 1);
3052 if (err)
3053 return err;
3057 return draw_commits(view);
3060 static void
3061 log_move_cursor_up(struct tog_view *view, int page, int home)
3063 struct tog_log_view_state *s = &view->state.log;
3065 if (s->selected_entry->idx == 0)
3066 view->count = 0;
3067 if (s->first_displayed_entry == NULL)
3068 return;
3070 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3071 || home)
3072 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3074 if (!page && !home && s->selected > 0)
3075 --s->selected;
3076 else
3077 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3079 select_commit(s);
3080 return;
3083 static const struct got_error *
3084 log_move_cursor_down(struct tog_view *view, int page)
3086 struct tog_log_view_state *s = &view->state.log;
3087 struct commit_queue_entry *first;
3088 const struct got_error *err = NULL;
3090 first = s->first_displayed_entry;
3091 if (first == NULL) {
3092 view->count = 0;
3093 return NULL;
3096 if (s->thread_args.log_complete &&
3097 s->selected_entry->idx >= s->commits.ncommits - 1)
3098 return NULL;
3100 if (!page) {
3101 int eos = view->nlines - 2;
3103 if (view_is_hsplit_top(view))
3104 --eos; /* border consumes the last line */
3105 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3106 ++s->selected;
3107 else
3108 err = log_scroll_down(view, 1);
3109 } else if (s->thread_args.load_all) {
3110 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3111 s->selected += MIN(s->last_displayed_entry->idx -
3112 s->selected_entry->idx, page + 1);
3113 else
3114 err = log_scroll_down(view, MIN(page,
3115 s->commits.ncommits - s->selected_entry->idx - 1));
3116 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3117 } else {
3118 err = log_scroll_down(view, page);
3119 if (err)
3120 return err;
3121 if (first == s->first_displayed_entry && s->selected <
3122 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3123 s->selected = MIN(s->commits.ncommits - 1, page);
3126 if (err)
3127 return err;
3130 * We might necessarily overshoot in horizontal
3131 * splits; if so, select the last displayed commit.
3133 s->selected = MIN(s->selected,
3134 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3136 select_commit(s);
3138 if (s->thread_args.log_complete &&
3139 s->selected_entry->idx == s->commits.ncommits - 1)
3140 view->count = 0;
3142 return NULL;
3145 static void
3146 view_get_split(struct tog_view *view, int *y, int *x)
3148 *x = 0;
3149 *y = 0;
3151 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3152 if (view->child && view->child->resized_y)
3153 *y = view->child->resized_y;
3154 else if (view->resized_y)
3155 *y = view->resized_y;
3156 else
3157 *y = view_split_begin_y(view->lines);
3158 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3159 if (view->child && view->child->resized_x)
3160 *x = view->child->resized_x;
3161 else if (view->resized_x)
3162 *x = view->resized_x;
3163 else
3164 *x = view_split_begin_x(view->begin_x);
3168 /* Split view horizontally at y and offset view->state->selected line. */
3169 static const struct got_error *
3170 view_init_hsplit(struct tog_view *view, int y)
3172 const struct got_error *err = NULL;
3174 view->nlines = y;
3175 view->ncols = COLS;
3176 err = view_resize(view);
3177 if (err)
3178 return err;
3180 err = offset_selection_down(view);
3182 return err;
3185 static const struct got_error *
3186 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3188 const struct got_error *err = NULL;
3189 struct tog_log_view_state *s = &view->state.log;
3190 struct tog_view *diff_view = NULL, *tree_view = NULL;
3191 struct tog_view *ref_view = NULL;
3192 struct commit_queue_entry *entry;
3193 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3195 if (s->thread_args.load_all) {
3196 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3197 s->thread_args.load_all = 0;
3198 else if (s->thread_args.log_complete) {
3199 err = log_move_cursor_down(view, s->commits.ncommits);
3200 s->thread_args.load_all = 0;
3202 return err;
3205 eos = nscroll = view->nlines - 1;
3206 if (view_is_hsplit_top(view))
3207 --eos; /* border */
3209 switch (ch) {
3210 case 'q':
3211 s->quit = 1;
3212 break;
3213 case '0':
3214 view->x = 0;
3215 break;
3216 case '$':
3217 view->x = MAX(view->maxx - view->ncols / 2, 0);
3218 view->count = 0;
3219 break;
3220 case KEY_RIGHT:
3221 case 'l':
3222 if (view->x + view->ncols / 2 < view->maxx)
3223 view->x += 2; /* move two columns right */
3224 else
3225 view->count = 0;
3226 break;
3227 case KEY_LEFT:
3228 case 'h':
3229 view->x -= MIN(view->x, 2); /* move two columns back */
3230 if (view->x <= 0)
3231 view->count = 0;
3232 break;
3233 case 'k':
3234 case KEY_UP:
3235 case '<':
3236 case ',':
3237 case CTRL('p'):
3238 log_move_cursor_up(view, 0, 0);
3239 break;
3240 case 'g':
3241 case KEY_HOME:
3242 log_move_cursor_up(view, 0, 1);
3243 view->count = 0;
3244 break;
3245 case CTRL('u'):
3246 case 'u':
3247 nscroll /= 2;
3248 /* FALL THROUGH */
3249 case KEY_PPAGE:
3250 case CTRL('b'):
3251 case 'b':
3252 log_move_cursor_up(view, nscroll, 0);
3253 break;
3254 case 'j':
3255 case KEY_DOWN:
3256 case '>':
3257 case '.':
3258 case CTRL('n'):
3259 err = log_move_cursor_down(view, 0);
3260 break;
3261 case 'G':
3262 case KEY_END: {
3263 /* We don't know yet how many commits, so we're forced to
3264 * traverse them all. */
3265 view->count = 0;
3266 if (!s->thread_args.log_complete) {
3267 s->thread_args.load_all = 1;
3268 return trigger_log_thread(view, 0);
3271 s->selected = 0;
3272 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3273 for (n = 0; n < eos; n++) {
3274 if (entry == NULL)
3275 break;
3276 s->first_displayed_entry = entry;
3277 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3279 if (n > 0)
3280 s->selected = n - 1;
3281 select_commit(s);
3282 break;
3284 case CTRL('d'):
3285 case 'd':
3286 nscroll /= 2;
3287 /* FALL THROUGH */
3288 case KEY_NPAGE:
3289 case CTRL('f'):
3290 case 'f':
3291 case ' ':
3292 err = log_move_cursor_down(view, nscroll);
3293 break;
3294 case KEY_RESIZE:
3295 if (s->selected > view->nlines - 2)
3296 s->selected = view->nlines - 2;
3297 if (s->selected > s->commits.ncommits - 1)
3298 s->selected = s->commits.ncommits - 1;
3299 select_commit(s);
3300 if (s->commits.ncommits < view->nlines - 1 &&
3301 !s->thread_args.log_complete) {
3302 s->thread_args.commits_needed += (view->nlines - 1) -
3303 s->commits.ncommits;
3304 err = trigger_log_thread(view, 1);
3306 break;
3307 case KEY_ENTER:
3308 case '\r':
3309 view->count = 0;
3310 if (s->selected_entry == NULL)
3311 break;
3313 /* get dimensions--don't split till initialisation succeeds */
3314 if (view_is_parent_view(view))
3315 view_get_split(view, &begin_y, &begin_x);
3317 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3318 s->selected_entry->commit, s->selected_entry->id,
3319 view, s->repo);
3320 if (err)
3321 break;
3323 if (view_is_parent_view(view) &&
3324 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3325 err = view_init_hsplit(view, begin_y);
3326 if (err)
3327 break;
3330 view->focussed = 0;
3331 diff_view->focussed = 1;
3332 diff_view->mode = view->mode;
3333 diff_view->nlines = view->lines - begin_y;
3335 if (view_is_parent_view(view)) {
3336 view_transfer_size(diff_view, view);
3337 err = view_close_child(view);
3338 if (err)
3339 return err;
3340 err = view_set_child(view, diff_view);
3341 if (err)
3342 return err;
3343 view->focus_child = 1;
3344 } else
3345 *new_view = diff_view;
3346 break;
3347 case 't':
3348 view->count = 0;
3349 if (s->selected_entry == NULL)
3350 break;
3351 if (view_is_parent_view(view))
3352 view_get_split(view, &begin_y, &begin_x);
3353 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3354 s->selected_entry, s->in_repo_path, s->head_ref_name,
3355 s->repo);
3356 if (err)
3357 break;
3358 if (view_is_parent_view(view) &&
3359 view->mode == TOG_VIEW_SPLIT_HRZN) {
3360 err = view_init_hsplit(view, begin_y);
3361 if (err)
3362 break;
3364 view->focussed = 0;
3365 tree_view->focussed = 1;
3366 tree_view->mode = view->mode;
3367 tree_view->nlines = view->lines - begin_y;
3368 if (view_is_parent_view(view)) {
3369 view_transfer_size(tree_view, view);
3370 err = view_close_child(view);
3371 if (err)
3372 return err;
3373 err = view_set_child(view, tree_view);
3374 if (err)
3375 return err;
3376 view->focus_child = 1;
3377 } else
3378 *new_view = tree_view;
3379 break;
3380 case KEY_BACKSPACE:
3381 case CTRL('l'):
3382 case 'B':
3383 view->count = 0;
3384 if (ch == KEY_BACKSPACE &&
3385 got_path_is_root_dir(s->in_repo_path))
3386 break;
3387 err = stop_log_thread(s);
3388 if (err)
3389 return err;
3390 if (ch == KEY_BACKSPACE) {
3391 char *parent_path;
3392 err = got_path_dirname(&parent_path, s->in_repo_path);
3393 if (err)
3394 return err;
3395 free(s->in_repo_path);
3396 s->in_repo_path = parent_path;
3397 s->thread_args.in_repo_path = s->in_repo_path;
3398 } else if (ch == CTRL('l')) {
3399 struct got_object_id *start_id;
3400 err = got_repo_match_object_id(&start_id, NULL,
3401 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3402 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3403 if (err)
3404 return err;
3405 free(s->start_id);
3406 s->start_id = start_id;
3407 s->thread_args.start_id = s->start_id;
3408 } else /* 'B' */
3409 s->log_branches = !s->log_branches;
3411 if (s->thread_args.pack_fds == NULL) {
3412 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3413 if (err)
3414 return err;
3416 err = got_repo_open(&s->thread_args.repo,
3417 got_repo_get_path(s->repo), NULL,
3418 s->thread_args.pack_fds);
3419 if (err)
3420 return err;
3421 tog_free_refs();
3422 err = tog_load_refs(s->repo, 0);
3423 if (err)
3424 return err;
3425 err = got_commit_graph_open(&s->thread_args.graph,
3426 s->in_repo_path, !s->log_branches);
3427 if (err)
3428 return err;
3429 err = got_commit_graph_iter_start(s->thread_args.graph,
3430 s->start_id, s->repo, NULL, NULL);
3431 if (err)
3432 return err;
3433 free_commits(&s->commits);
3434 s->first_displayed_entry = NULL;
3435 s->last_displayed_entry = NULL;
3436 s->selected_entry = NULL;
3437 s->selected = 0;
3438 s->thread_args.log_complete = 0;
3439 s->quit = 0;
3440 s->thread_args.commits_needed = view->lines;
3441 s->matched_entry = NULL;
3442 s->search_entry = NULL;
3443 break;
3444 case 'r':
3445 view->count = 0;
3446 if (view_is_parent_view(view))
3447 view_get_split(view, &begin_y, &begin_x);
3448 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3449 if (ref_view == NULL)
3450 return got_error_from_errno("view_open");
3451 err = open_ref_view(ref_view, s->repo);
3452 if (err) {
3453 view_close(ref_view);
3454 return err;
3456 if (view_is_parent_view(view) &&
3457 view->mode == TOG_VIEW_SPLIT_HRZN) {
3458 err = view_init_hsplit(view, begin_y);
3459 if (err)
3460 break;
3462 view->focussed = 0;
3463 ref_view->focussed = 1;
3464 ref_view->mode = view->mode;
3465 ref_view->nlines = view->lines - begin_y;
3466 if (view_is_parent_view(view)) {
3467 view_transfer_size(ref_view, view);
3468 err = view_close_child(view);
3469 if (err)
3470 return err;
3471 err = view_set_child(view, ref_view);
3472 if (err)
3473 return err;
3474 view->focus_child = 1;
3475 } else
3476 *new_view = ref_view;
3477 break;
3478 default:
3479 view->count = 0;
3480 break;
3483 return err;
3486 static const struct got_error *
3487 apply_unveil(const char *repo_path, const char *worktree_path)
3489 const struct got_error *error;
3491 #ifdef PROFILE
3492 if (unveil("gmon.out", "rwc") != 0)
3493 return got_error_from_errno2("unveil", "gmon.out");
3494 #endif
3495 if (repo_path && unveil(repo_path, "r") != 0)
3496 return got_error_from_errno2("unveil", repo_path);
3498 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3499 return got_error_from_errno2("unveil", worktree_path);
3501 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3502 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3504 error = got_privsep_unveil_exec_helpers();
3505 if (error != NULL)
3506 return error;
3508 if (unveil(NULL, NULL) != 0)
3509 return got_error_from_errno("unveil");
3511 return NULL;
3514 static void
3515 init_curses(void)
3518 * Override default signal handlers before starting ncurses.
3519 * This should prevent ncurses from installing its own
3520 * broken cleanup() signal handler.
3522 signal(SIGWINCH, tog_sigwinch);
3523 signal(SIGPIPE, tog_sigpipe);
3524 signal(SIGCONT, tog_sigcont);
3525 signal(SIGINT, tog_sigint);
3526 signal(SIGTERM, tog_sigterm);
3528 initscr();
3529 cbreak();
3530 halfdelay(1); /* Do fast refresh while initial view is loading. */
3531 noecho();
3532 nonl();
3533 intrflush(stdscr, FALSE);
3534 keypad(stdscr, TRUE);
3535 curs_set(0);
3536 if (getenv("TOG_COLORS") != NULL) {
3537 start_color();
3538 use_default_colors();
3542 static const struct got_error *
3543 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3544 struct got_repository *repo, struct got_worktree *worktree)
3546 const struct got_error *err = NULL;
3548 if (argc == 0) {
3549 *in_repo_path = strdup("/");
3550 if (*in_repo_path == NULL)
3551 return got_error_from_errno("strdup");
3552 return NULL;
3555 if (worktree) {
3556 const char *prefix = got_worktree_get_path_prefix(worktree);
3557 char *p;
3559 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3560 if (err)
3561 return err;
3562 if (asprintf(in_repo_path, "%s%s%s", prefix,
3563 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3564 p) == -1) {
3565 err = got_error_from_errno("asprintf");
3566 *in_repo_path = NULL;
3568 free(p);
3569 } else
3570 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3572 return err;
3575 static const struct got_error *
3576 cmd_log(int argc, char *argv[])
3578 const struct got_error *error;
3579 struct got_repository *repo = NULL;
3580 struct got_worktree *worktree = NULL;
3581 struct got_object_id *start_id = NULL;
3582 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3583 char *start_commit = NULL, *label = NULL;
3584 struct got_reference *ref = NULL;
3585 const char *head_ref_name = NULL;
3586 int ch, log_branches = 0;
3587 struct tog_view *view;
3588 int *pack_fds = NULL;
3590 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3591 switch (ch) {
3592 case 'b':
3593 log_branches = 1;
3594 break;
3595 case 'c':
3596 start_commit = optarg;
3597 break;
3598 case 'r':
3599 repo_path = realpath(optarg, NULL);
3600 if (repo_path == NULL)
3601 return got_error_from_errno2("realpath",
3602 optarg);
3603 break;
3604 default:
3605 usage_log();
3606 /* NOTREACHED */
3610 argc -= optind;
3611 argv += optind;
3613 if (argc > 1)
3614 usage_log();
3616 error = got_repo_pack_fds_open(&pack_fds);
3617 if (error != NULL)
3618 goto done;
3620 if (repo_path == NULL) {
3621 cwd = getcwd(NULL, 0);
3622 if (cwd == NULL)
3623 return got_error_from_errno("getcwd");
3624 error = got_worktree_open(&worktree, cwd);
3625 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3626 goto done;
3627 if (worktree)
3628 repo_path =
3629 strdup(got_worktree_get_repo_path(worktree));
3630 else
3631 repo_path = strdup(cwd);
3632 if (repo_path == NULL) {
3633 error = got_error_from_errno("strdup");
3634 goto done;
3638 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3639 if (error != NULL)
3640 goto done;
3642 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3643 repo, worktree);
3644 if (error)
3645 goto done;
3647 init_curses();
3649 error = apply_unveil(got_repo_get_path(repo),
3650 worktree ? got_worktree_get_root_path(worktree) : NULL);
3651 if (error)
3652 goto done;
3654 /* already loaded by tog_log_with_path()? */
3655 if (TAILQ_EMPTY(&tog_refs)) {
3656 error = tog_load_refs(repo, 0);
3657 if (error)
3658 goto done;
3661 if (start_commit == NULL) {
3662 error = got_repo_match_object_id(&start_id, &label,
3663 worktree ? got_worktree_get_head_ref_name(worktree) :
3664 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3665 if (error)
3666 goto done;
3667 head_ref_name = label;
3668 } else {
3669 error = got_ref_open(&ref, repo, start_commit, 0);
3670 if (error == NULL)
3671 head_ref_name = got_ref_get_name(ref);
3672 else if (error->code != GOT_ERR_NOT_REF)
3673 goto done;
3674 error = got_repo_match_object_id(&start_id, NULL,
3675 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3676 if (error)
3677 goto done;
3680 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3681 if (view == NULL) {
3682 error = got_error_from_errno("view_open");
3683 goto done;
3685 error = open_log_view(view, start_id, repo, head_ref_name,
3686 in_repo_path, log_branches);
3687 if (error)
3688 goto done;
3689 if (worktree) {
3690 /* Release work tree lock. */
3691 got_worktree_close(worktree);
3692 worktree = NULL;
3694 error = view_loop(view);
3695 done:
3696 free(in_repo_path);
3697 free(repo_path);
3698 free(cwd);
3699 free(start_id);
3700 free(label);
3701 if (ref)
3702 got_ref_close(ref);
3703 if (repo) {
3704 const struct got_error *close_err = got_repo_close(repo);
3705 if (error == NULL)
3706 error = close_err;
3708 if (worktree)
3709 got_worktree_close(worktree);
3710 if (pack_fds) {
3711 const struct got_error *pack_err =
3712 got_repo_pack_fds_close(pack_fds);
3713 if (error == NULL)
3714 error = pack_err;
3716 tog_free_refs();
3717 return error;
3720 __dead static void
3721 usage_diff(void)
3723 endwin();
3724 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3725 "[-w] object1 object2\n", getprogname());
3726 exit(1);
3729 static int
3730 match_line(const char *line, regex_t *regex, size_t nmatch,
3731 regmatch_t *regmatch)
3733 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3736 static struct tog_color *
3737 match_color(struct tog_colors *colors, const char *line)
3739 struct tog_color *tc = NULL;
3741 STAILQ_FOREACH(tc, colors, entry) {
3742 if (match_line(line, &tc->regex, 0, NULL))
3743 return tc;
3746 return NULL;
3749 static const struct got_error *
3750 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3751 WINDOW *window, int skipcol, regmatch_t *regmatch)
3753 const struct got_error *err = NULL;
3754 char *exstr = NULL;
3755 wchar_t *wline = NULL;
3756 int rme, rms, n, width, scrollx;
3757 int width0 = 0, width1 = 0, width2 = 0;
3758 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3760 *wtotal = 0;
3762 rms = regmatch->rm_so;
3763 rme = regmatch->rm_eo;
3765 err = expand_tab(&exstr, line);
3766 if (err)
3767 return err;
3769 /* Split the line into 3 segments, according to match offsets. */
3770 seg0 = strndup(exstr, rms);
3771 if (seg0 == NULL) {
3772 err = got_error_from_errno("strndup");
3773 goto done;
3775 seg1 = strndup(exstr + rms, rme - rms);
3776 if (seg1 == NULL) {
3777 err = got_error_from_errno("strndup");
3778 goto done;
3780 seg2 = strdup(exstr + rme);
3781 if (seg2 == NULL) {
3782 err = got_error_from_errno("strndup");
3783 goto done;
3786 /* draw up to matched token if we haven't scrolled past it */
3787 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3788 col_tab_align, 1);
3789 if (err)
3790 goto done;
3791 n = MAX(width0 - skipcol, 0);
3792 if (n) {
3793 free(wline);
3794 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3795 wlimit, col_tab_align, 1);
3796 if (err)
3797 goto done;
3798 waddwstr(window, &wline[scrollx]);
3799 wlimit -= width;
3800 *wtotal += width;
3803 if (wlimit > 0) {
3804 int i = 0, w = 0;
3805 size_t wlen;
3807 free(wline);
3808 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3809 col_tab_align, 1);
3810 if (err)
3811 goto done;
3812 wlen = wcslen(wline);
3813 while (i < wlen) {
3814 width = wcwidth(wline[i]);
3815 if (width == -1) {
3816 /* should not happen, tabs are expanded */
3817 err = got_error(GOT_ERR_RANGE);
3818 goto done;
3820 if (width0 + w + width > skipcol)
3821 break;
3822 w += width;
3823 i++;
3825 /* draw (visible part of) matched token (if scrolled into it) */
3826 if (width1 - w > 0) {
3827 wattron(window, A_STANDOUT);
3828 waddwstr(window, &wline[i]);
3829 wattroff(window, A_STANDOUT);
3830 wlimit -= (width1 - w);
3831 *wtotal += (width1 - w);
3835 if (wlimit > 0) { /* draw rest of line */
3836 free(wline);
3837 if (skipcol > width0 + width1) {
3838 err = format_line(&wline, &width2, &scrollx, seg2,
3839 skipcol - (width0 + width1), wlimit,
3840 col_tab_align, 1);
3841 if (err)
3842 goto done;
3843 waddwstr(window, &wline[scrollx]);
3844 } else {
3845 err = format_line(&wline, &width2, NULL, seg2, 0,
3846 wlimit, col_tab_align, 1);
3847 if (err)
3848 goto done;
3849 waddwstr(window, wline);
3851 *wtotal += width2;
3853 done:
3854 free(wline);
3855 free(exstr);
3856 free(seg0);
3857 free(seg1);
3858 free(seg2);
3859 return err;
3862 static const struct got_error *
3863 draw_file(struct tog_view *view, const char *header)
3865 struct tog_diff_view_state *s = &view->state.diff;
3866 regmatch_t *regmatch = &view->regmatch;
3867 const struct got_error *err;
3868 int nprinted = 0;
3869 char *line;
3870 size_t linesize = 0;
3871 ssize_t linelen;
3872 struct tog_color *tc;
3873 wchar_t *wline;
3874 int width;
3875 int max_lines = view->nlines;
3876 int nlines = s->nlines;
3877 off_t line_offset;
3879 line_offset = s->line_offsets[s->first_displayed_line - 1];
3880 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3881 return got_error_from_errno("fseek");
3883 werase(view->window);
3885 if (header) {
3886 if (asprintf(&line, "[%d/%d] %s",
3887 s->first_displayed_line - 1 + s->selected_line, nlines,
3888 header) == -1)
3889 return got_error_from_errno("asprintf");
3890 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3891 0, 0);
3892 free(line);
3893 if (err)
3894 return err;
3896 if (view_needs_focus_indication(view))
3897 wstandout(view->window);
3898 waddwstr(view->window, wline);
3899 free(wline);
3900 wline = NULL;
3901 if (view_needs_focus_indication(view))
3902 wstandend(view->window);
3903 if (width <= view->ncols - 1)
3904 waddch(view->window, '\n');
3906 if (max_lines <= 1)
3907 return NULL;
3908 max_lines--;
3911 s->eof = 0;
3912 view->maxx = 0;
3913 line = NULL;
3914 while (max_lines > 0 && nprinted < max_lines) {
3915 linelen = getline(&line, &linesize, s->f);
3916 if (linelen == -1) {
3917 if (feof(s->f)) {
3918 s->eof = 1;
3919 break;
3921 free(line);
3922 return got_ferror(s->f, GOT_ERR_IO);
3925 /* Set view->maxx based on full line length. */
3926 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3927 view->x ? 1 : 0);
3928 if (err) {
3929 free(line);
3930 return err;
3932 view->maxx = MAX(view->maxx, width);
3933 free(wline);
3934 wline = NULL;
3936 tc = match_color(&s->colors, line);
3937 if (tc)
3938 wattr_on(view->window,
3939 COLOR_PAIR(tc->colorpair), NULL);
3940 if (s->first_displayed_line + nprinted == s->matched_line &&
3941 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3942 err = add_matched_line(&width, line, view->ncols, 0,
3943 view->window, view->x, regmatch);
3944 if (err) {
3945 free(line);
3946 return err;
3948 } else {
3949 int skip;
3950 err = format_line(&wline, &width, &skip, line,
3951 view->x, view->ncols, 0, view->x ? 1 : 0);
3952 if (err) {
3953 free(line);
3954 return err;
3956 waddwstr(view->window, &wline[skip]);
3957 free(wline);
3958 wline = NULL;
3960 if (tc)
3961 wattr_off(view->window,
3962 COLOR_PAIR(tc->colorpair), NULL);
3963 if (width <= view->ncols - 1)
3964 waddch(view->window, '\n');
3965 nprinted++;
3967 free(line);
3968 if (nprinted >= 1)
3969 s->last_displayed_line = s->first_displayed_line +
3970 (nprinted - 1);
3971 else
3972 s->last_displayed_line = s->first_displayed_line;
3974 view_border(view);
3976 if (s->eof) {
3977 while (nprinted < view->nlines) {
3978 waddch(view->window, '\n');
3979 nprinted++;
3982 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3983 view->ncols, 0, 0);
3984 if (err) {
3985 return err;
3988 wstandout(view->window);
3989 waddwstr(view->window, wline);
3990 free(wline);
3991 wline = NULL;
3992 wstandend(view->window);
3995 return NULL;
3998 static char *
3999 get_datestr(time_t *time, char *datebuf)
4001 struct tm mytm, *tm;
4002 char *p, *s;
4004 tm = gmtime_r(time, &mytm);
4005 if (tm == NULL)
4006 return NULL;
4007 s = asctime_r(tm, datebuf);
4008 if (s == NULL)
4009 return NULL;
4010 p = strchr(s, '\n');
4011 if (p)
4012 *p = '\0';
4013 return s;
4016 static const struct got_error *
4017 get_changed_paths(struct got_pathlist_head *paths,
4018 struct got_commit_object *commit, struct got_repository *repo)
4020 const struct got_error *err = NULL;
4021 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4022 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4023 struct got_object_qid *qid;
4025 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4026 if (qid != NULL) {
4027 struct got_commit_object *pcommit;
4028 err = got_object_open_as_commit(&pcommit, repo,
4029 &qid->id);
4030 if (err)
4031 return err;
4033 tree_id1 = got_object_id_dup(
4034 got_object_commit_get_tree_id(pcommit));
4035 if (tree_id1 == NULL) {
4036 got_object_commit_close(pcommit);
4037 return got_error_from_errno("got_object_id_dup");
4039 got_object_commit_close(pcommit);
4043 if (tree_id1) {
4044 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4045 if (err)
4046 goto done;
4049 tree_id2 = got_object_commit_get_tree_id(commit);
4050 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4051 if (err)
4052 goto done;
4054 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4055 got_diff_tree_collect_changed_paths, paths, 0);
4056 done:
4057 if (tree1)
4058 got_object_tree_close(tree1);
4059 if (tree2)
4060 got_object_tree_close(tree2);
4061 free(tree_id1);
4062 return err;
4065 static const struct got_error *
4066 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4068 off_t *p;
4070 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4071 if (p == NULL)
4072 return got_error_from_errno("reallocarray");
4073 *line_offsets = p;
4074 (*line_offsets)[*nlines] = off;
4075 (*nlines)++;
4076 return NULL;
4079 static const struct got_error *
4080 write_commit_info(off_t **line_offsets, size_t *nlines,
4081 struct got_object_id *commit_id, struct got_reflist_head *refs,
4082 struct got_repository *repo, FILE *outfile)
4084 const struct got_error *err = NULL;
4085 char datebuf[26], *datestr;
4086 struct got_commit_object *commit;
4087 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4088 time_t committer_time;
4089 const char *author, *committer;
4090 char *refs_str = NULL;
4091 struct got_pathlist_head changed_paths;
4092 struct got_pathlist_entry *pe;
4093 off_t outoff = 0;
4094 int n;
4096 TAILQ_INIT(&changed_paths);
4098 if (refs) {
4099 err = build_refs_str(&refs_str, refs, commit_id, repo);
4100 if (err)
4101 return err;
4104 err = got_object_open_as_commit(&commit, repo, commit_id);
4105 if (err)
4106 return err;
4108 err = got_object_id_str(&id_str, commit_id);
4109 if (err) {
4110 err = got_error_from_errno("got_object_id_str");
4111 goto done;
4114 err = add_line_offset(line_offsets, nlines, 0);
4115 if (err)
4116 goto done;
4118 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4119 refs_str ? refs_str : "", refs_str ? ")" : "");
4120 if (n < 0) {
4121 err = got_error_from_errno("fprintf");
4122 goto done;
4124 outoff += n;
4125 err = add_line_offset(line_offsets, nlines, outoff);
4126 if (err)
4127 goto done;
4129 n = fprintf(outfile, "from: %s\n",
4130 got_object_commit_get_author(commit));
4131 if (n < 0) {
4132 err = got_error_from_errno("fprintf");
4133 goto done;
4135 outoff += n;
4136 err = add_line_offset(line_offsets, nlines, outoff);
4137 if (err)
4138 goto done;
4140 committer_time = got_object_commit_get_committer_time(commit);
4141 datestr = get_datestr(&committer_time, datebuf);
4142 if (datestr) {
4143 n = fprintf(outfile, "date: %s UTC\n", datestr);
4144 if (n < 0) {
4145 err = got_error_from_errno("fprintf");
4146 goto done;
4148 outoff += n;
4149 err = add_line_offset(line_offsets, nlines, outoff);
4150 if (err)
4151 goto done;
4153 author = got_object_commit_get_author(commit);
4154 committer = got_object_commit_get_committer(commit);
4155 if (strcmp(author, committer) != 0) {
4156 n = fprintf(outfile, "via: %s\n", committer);
4157 if (n < 0) {
4158 err = got_error_from_errno("fprintf");
4159 goto done;
4161 outoff += n;
4162 err = add_line_offset(line_offsets, nlines, outoff);
4163 if (err)
4164 goto done;
4166 if (got_object_commit_get_nparents(commit) > 1) {
4167 const struct got_object_id_queue *parent_ids;
4168 struct got_object_qid *qid;
4169 int pn = 1;
4170 parent_ids = got_object_commit_get_parent_ids(commit);
4171 STAILQ_FOREACH(qid, parent_ids, entry) {
4172 err = got_object_id_str(&id_str, &qid->id);
4173 if (err)
4174 goto done;
4175 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4176 if (n < 0) {
4177 err = got_error_from_errno("fprintf");
4178 goto done;
4180 outoff += n;
4181 err = add_line_offset(line_offsets, nlines, outoff);
4182 if (err)
4183 goto done;
4184 free(id_str);
4185 id_str = NULL;
4189 err = got_object_commit_get_logmsg(&logmsg, commit);
4190 if (err)
4191 goto done;
4192 s = logmsg;
4193 while ((line = strsep(&s, "\n")) != NULL) {
4194 n = fprintf(outfile, "%s\n", line);
4195 if (n < 0) {
4196 err = got_error_from_errno("fprintf");
4197 goto done;
4199 outoff += n;
4200 err = add_line_offset(line_offsets, nlines, outoff);
4201 if (err)
4202 goto done;
4205 err = get_changed_paths(&changed_paths, commit, repo);
4206 if (err)
4207 goto done;
4208 TAILQ_FOREACH(pe, &changed_paths, entry) {
4209 struct got_diff_changed_path *cp = pe->data;
4210 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4211 if (n < 0) {
4212 err = got_error_from_errno("fprintf");
4213 goto done;
4215 outoff += n;
4216 err = add_line_offset(line_offsets, nlines, outoff);
4217 if (err)
4218 goto done;
4219 free((char *)pe->path);
4220 free(pe->data);
4223 fputc('\n', outfile);
4224 outoff++;
4225 err = add_line_offset(line_offsets, nlines, outoff);
4226 done:
4227 got_pathlist_free(&changed_paths);
4228 free(id_str);
4229 free(logmsg);
4230 free(refs_str);
4231 got_object_commit_close(commit);
4232 if (err) {
4233 free(*line_offsets);
4234 *line_offsets = NULL;
4235 *nlines = 0;
4237 return err;
4240 static const struct got_error *
4241 create_diff(struct tog_diff_view_state *s)
4243 const struct got_error *err = NULL;
4244 FILE *f = NULL;
4245 int obj_type;
4247 free(s->line_offsets);
4248 s->line_offsets = malloc(sizeof(off_t));
4249 if (s->line_offsets == NULL)
4250 return got_error_from_errno("malloc");
4251 s->nlines = 0;
4253 f = got_opentemp();
4254 if (f == NULL) {
4255 err = got_error_from_errno("got_opentemp");
4256 goto done;
4258 if (s->f && fclose(s->f) == EOF) {
4259 err = got_error_from_errno("fclose");
4260 goto done;
4262 s->f = f;
4264 if (s->id1)
4265 err = got_object_get_type(&obj_type, s->repo, s->id1);
4266 else
4267 err = got_object_get_type(&obj_type, s->repo, s->id2);
4268 if (err)
4269 goto done;
4271 switch (obj_type) {
4272 case GOT_OBJ_TYPE_BLOB:
4273 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4274 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4275 s->label1, s->label2, tog_diff_algo, s->diff_context,
4276 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4277 break;
4278 case GOT_OBJ_TYPE_TREE:
4279 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4280 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4281 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4282 s->force_text_diff, s->repo, s->f);
4283 break;
4284 case GOT_OBJ_TYPE_COMMIT: {
4285 const struct got_object_id_queue *parent_ids;
4286 struct got_object_qid *pid;
4287 struct got_commit_object *commit2;
4288 struct got_reflist_head *refs;
4290 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4291 if (err)
4292 goto done;
4293 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4294 /* Show commit info if we're diffing to a parent/root commit. */
4295 if (s->id1 == NULL) {
4296 err = write_commit_info(&s->line_offsets, &s->nlines,
4297 s->id2, refs, s->repo, s->f);
4298 if (err)
4299 goto done;
4300 } else {
4301 parent_ids = got_object_commit_get_parent_ids(commit2);
4302 STAILQ_FOREACH(pid, parent_ids, entry) {
4303 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4304 err = write_commit_info(
4305 &s->line_offsets, &s->nlines,
4306 s->id2, refs, s->repo, s->f);
4307 if (err)
4308 goto done;
4309 break;
4313 got_object_commit_close(commit2);
4315 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4316 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4317 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4318 s->force_text_diff, s->repo, s->f);
4319 break;
4321 default:
4322 err = got_error(GOT_ERR_OBJ_TYPE);
4323 break;
4325 if (err)
4326 goto done;
4327 done:
4328 if (s->f && fflush(s->f) != 0 && err == NULL)
4329 err = got_error_from_errno("fflush");
4330 return err;
4333 static void
4334 diff_view_indicate_progress(struct tog_view *view)
4336 mvwaddstr(view->window, 0, 0, "diffing...");
4337 update_panels();
4338 doupdate();
4341 static const struct got_error *
4342 search_start_diff_view(struct tog_view *view)
4344 struct tog_diff_view_state *s = &view->state.diff;
4346 s->matched_line = 0;
4347 return NULL;
4350 static const struct got_error *
4351 search_next_diff_view(struct tog_view *view)
4353 struct tog_diff_view_state *s = &view->state.diff;
4354 const struct got_error *err = NULL;
4355 int lineno;
4356 char *line = NULL;
4357 size_t linesize = 0;
4358 ssize_t linelen;
4360 if (!view->searching) {
4361 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4362 return NULL;
4365 if (s->matched_line) {
4366 if (view->searching == TOG_SEARCH_FORWARD)
4367 lineno = s->matched_line + 1;
4368 else
4369 lineno = s->matched_line - 1;
4370 } else
4371 lineno = s->first_displayed_line;
4373 while (1) {
4374 off_t offset;
4376 if (lineno <= 0 || lineno > s->nlines) {
4377 if (s->matched_line == 0) {
4378 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4379 break;
4382 if (view->searching == TOG_SEARCH_FORWARD)
4383 lineno = 1;
4384 else
4385 lineno = s->nlines;
4388 offset = s->line_offsets[lineno - 1];
4389 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4390 free(line);
4391 return got_error_from_errno("fseeko");
4393 linelen = getline(&line, &linesize, s->f);
4394 if (linelen != -1) {
4395 char *exstr;
4396 err = expand_tab(&exstr, line);
4397 if (err)
4398 break;
4399 if (match_line(exstr, &view->regex, 1,
4400 &view->regmatch)) {
4401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4402 s->matched_line = lineno;
4403 free(exstr);
4404 break;
4406 free(exstr);
4408 if (view->searching == TOG_SEARCH_FORWARD)
4409 lineno++;
4410 else
4411 lineno--;
4413 free(line);
4415 if (s->matched_line) {
4416 s->first_displayed_line = s->matched_line;
4417 s->selected_line = 1;
4420 return err;
4423 static const struct got_error *
4424 close_diff_view(struct tog_view *view)
4426 const struct got_error *err = NULL;
4427 struct tog_diff_view_state *s = &view->state.diff;
4429 free(s->id1);
4430 s->id1 = NULL;
4431 free(s->id2);
4432 s->id2 = NULL;
4433 if (s->f && fclose(s->f) == EOF)
4434 err = got_error_from_errno("fclose");
4435 s->f = NULL;
4436 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4437 err = got_error_from_errno("fclose");
4438 s->f1 = NULL;
4439 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4440 err = got_error_from_errno("fclose");
4441 s->f2 = NULL;
4442 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4443 err = got_error_from_errno("close");
4444 s->fd1 = -1;
4445 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4446 err = got_error_from_errno("close");
4447 s->fd2 = -1;
4448 free_colors(&s->colors);
4449 free(s->line_offsets);
4450 s->line_offsets = NULL;
4451 s->nlines = 0;
4452 return err;
4455 static const struct got_error *
4456 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4457 struct got_object_id *id2, const char *label1, const char *label2,
4458 int diff_context, int ignore_whitespace, int force_text_diff,
4459 struct tog_view *parent_view, struct got_repository *repo)
4461 const struct got_error *err;
4462 struct tog_diff_view_state *s = &view->state.diff;
4464 memset(s, 0, sizeof(*s));
4465 s->fd1 = -1;
4466 s->fd2 = -1;
4468 if (id1 != NULL && id2 != NULL) {
4469 int type1, type2;
4470 err = got_object_get_type(&type1, repo, id1);
4471 if (err)
4472 return err;
4473 err = got_object_get_type(&type2, repo, id2);
4474 if (err)
4475 return err;
4477 if (type1 != type2)
4478 return got_error(GOT_ERR_OBJ_TYPE);
4480 s->first_displayed_line = 1;
4481 s->last_displayed_line = view->nlines;
4482 s->selected_line = 1;
4483 s->repo = repo;
4484 s->id1 = id1;
4485 s->id2 = id2;
4486 s->label1 = label1;
4487 s->label2 = label2;
4489 if (id1) {
4490 s->id1 = got_object_id_dup(id1);
4491 if (s->id1 == NULL)
4492 return got_error_from_errno("got_object_id_dup");
4493 } else
4494 s->id1 = NULL;
4496 s->id2 = got_object_id_dup(id2);
4497 if (s->id2 == NULL) {
4498 err = got_error_from_errno("got_object_id_dup");
4499 goto done;
4502 s->f1 = got_opentemp();
4503 if (s->f1 == NULL) {
4504 err = got_error_from_errno("got_opentemp");
4505 goto done;
4508 s->f2 = got_opentemp();
4509 if (s->f2 == NULL) {
4510 err = got_error_from_errno("got_opentemp");
4511 goto done;
4514 s->fd1 = got_opentempfd();
4515 if (s->fd1 == -1) {
4516 err = got_error_from_errno("got_opentempfd");
4517 goto done;
4520 s->fd2 = got_opentempfd();
4521 if (s->fd2 == -1) {
4522 err = got_error_from_errno("got_opentempfd");
4523 goto done;
4526 s->first_displayed_line = 1;
4527 s->last_displayed_line = view->nlines;
4528 s->diff_context = diff_context;
4529 s->ignore_whitespace = ignore_whitespace;
4530 s->force_text_diff = force_text_diff;
4531 s->parent_view = parent_view;
4532 s->repo = repo;
4534 STAILQ_INIT(&s->colors);
4535 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4536 err = add_color(&s->colors,
4537 "^-", TOG_COLOR_DIFF_MINUS,
4538 get_color_value("TOG_COLOR_DIFF_MINUS"));
4539 if (err)
4540 goto done;
4541 err = add_color(&s->colors, "^\\+",
4542 TOG_COLOR_DIFF_PLUS,
4543 get_color_value("TOG_COLOR_DIFF_PLUS"));
4544 if (err)
4545 goto done;
4546 err = add_color(&s->colors,
4547 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4548 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4549 if (err)
4550 goto done;
4552 err = add_color(&s->colors,
4553 "^(commit [0-9a-f]|parent [0-9]|"
4554 "(blob|file|tree|commit) [-+] |"
4555 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4556 get_color_value("TOG_COLOR_DIFF_META"));
4557 if (err)
4558 goto done;
4560 err = add_color(&s->colors,
4561 "^(from|via): ", TOG_COLOR_AUTHOR,
4562 get_color_value("TOG_COLOR_AUTHOR"));
4563 if (err)
4564 goto done;
4566 err = add_color(&s->colors,
4567 "^date: ", TOG_COLOR_DATE,
4568 get_color_value("TOG_COLOR_DATE"));
4569 if (err)
4570 goto done;
4573 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4574 view_is_splitscreen(view))
4575 show_log_view(parent_view); /* draw border */
4576 diff_view_indicate_progress(view);
4578 err = create_diff(s);
4580 view->show = show_diff_view;
4581 view->input = input_diff_view;
4582 view->reset = reset_diff_view;
4583 view->close = close_diff_view;
4584 view->search_start = search_start_diff_view;
4585 view->search_next = search_next_diff_view;
4586 done:
4587 if (err)
4588 close_diff_view(view);
4589 return err;
4592 static const struct got_error *
4593 show_diff_view(struct tog_view *view)
4595 const struct got_error *err;
4596 struct tog_diff_view_state *s = &view->state.diff;
4597 char *id_str1 = NULL, *id_str2, *header;
4598 const char *label1, *label2;
4600 if (s->id1) {
4601 err = got_object_id_str(&id_str1, s->id1);
4602 if (err)
4603 return err;
4604 label1 = s->label1 ? : id_str1;
4605 } else
4606 label1 = "/dev/null";
4608 err = got_object_id_str(&id_str2, s->id2);
4609 if (err)
4610 return err;
4611 label2 = s->label2 ? : id_str2;
4613 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4614 err = got_error_from_errno("asprintf");
4615 free(id_str1);
4616 free(id_str2);
4617 return err;
4619 free(id_str1);
4620 free(id_str2);
4622 err = draw_file(view, header);
4623 free(header);
4624 return err;
4627 static const struct got_error *
4628 set_selected_commit(struct tog_diff_view_state *s,
4629 struct commit_queue_entry *entry)
4631 const struct got_error *err;
4632 const struct got_object_id_queue *parent_ids;
4633 struct got_commit_object *selected_commit;
4634 struct got_object_qid *pid;
4636 free(s->id2);
4637 s->id2 = got_object_id_dup(entry->id);
4638 if (s->id2 == NULL)
4639 return got_error_from_errno("got_object_id_dup");
4641 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4642 if (err)
4643 return err;
4644 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4645 free(s->id1);
4646 pid = STAILQ_FIRST(parent_ids);
4647 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4648 got_object_commit_close(selected_commit);
4649 return NULL;
4652 static const struct got_error *
4653 reset_diff_view(struct tog_view *view)
4655 struct tog_diff_view_state *s = &view->state.diff;
4657 view->count = 0;
4658 wclear(view->window);
4659 s->first_displayed_line = 1;
4660 s->last_displayed_line = view->nlines;
4661 s->matched_line = 0;
4662 diff_view_indicate_progress(view);
4663 return create_diff(s);
4666 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4667 int, int, int);
4668 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4669 int, int);
4671 static const struct got_error *
4672 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4674 const struct got_error *err = NULL;
4675 struct tog_diff_view_state *s = &view->state.diff;
4676 struct tog_log_view_state *ls;
4677 struct commit_queue_entry *old_selected_entry;
4678 char *line = NULL;
4679 size_t linesize = 0;
4680 ssize_t linelen;
4681 int i, nscroll = view->nlines - 1, up = 0;
4683 switch (ch) {
4684 case '0':
4685 view->x = 0;
4686 break;
4687 case '$':
4688 view->x = MAX(view->maxx - view->ncols / 3, 0);
4689 view->count = 0;
4690 break;
4691 case KEY_RIGHT:
4692 case 'l':
4693 if (view->x + view->ncols / 3 < view->maxx)
4694 view->x += 2; /* move two columns right */
4695 else
4696 view->count = 0;
4697 break;
4698 case KEY_LEFT:
4699 case 'h':
4700 view->x -= MIN(view->x, 2); /* move two columns back */
4701 if (view->x <= 0)
4702 view->count = 0;
4703 break;
4704 case 'a':
4705 case 'w':
4706 if (ch == 'a')
4707 s->force_text_diff = !s->force_text_diff;
4708 if (ch == 'w')
4709 s->ignore_whitespace = !s->ignore_whitespace;
4710 err = reset_diff_view(view);
4711 break;
4712 case 'g':
4713 case KEY_HOME:
4714 s->first_displayed_line = 1;
4715 view->count = 0;
4716 break;
4717 case 'G':
4718 case KEY_END:
4719 view->count = 0;
4720 if (s->eof)
4721 break;
4723 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4724 s->eof = 1;
4725 break;
4726 case 'k':
4727 case KEY_UP:
4728 case CTRL('p'):
4729 if (s->first_displayed_line > 1)
4730 s->first_displayed_line--;
4731 else
4732 view->count = 0;
4733 break;
4734 case CTRL('u'):
4735 case 'u':
4736 nscroll /= 2;
4737 /* FALL THROUGH */
4738 case KEY_PPAGE:
4739 case CTRL('b'):
4740 case 'b':
4741 if (s->first_displayed_line == 1) {
4742 view->count = 0;
4743 break;
4745 i = 0;
4746 while (i++ < nscroll && s->first_displayed_line > 1)
4747 s->first_displayed_line--;
4748 break;
4749 case 'j':
4750 case KEY_DOWN:
4751 case CTRL('n'):
4752 if (!s->eof)
4753 s->first_displayed_line++;
4754 else
4755 view->count = 0;
4756 break;
4757 case CTRL('d'):
4758 case 'd':
4759 nscroll /= 2;
4760 /* FALL THROUGH */
4761 case KEY_NPAGE:
4762 case CTRL('f'):
4763 case 'f':
4764 case ' ':
4765 if (s->eof) {
4766 view->count = 0;
4767 break;
4769 i = 0;
4770 while (!s->eof && i++ < nscroll) {
4771 linelen = getline(&line, &linesize, s->f);
4772 s->first_displayed_line++;
4773 if (linelen == -1) {
4774 if (feof(s->f)) {
4775 s->eof = 1;
4776 } else
4777 err = got_ferror(s->f, GOT_ERR_IO);
4778 break;
4781 free(line);
4782 break;
4783 case '[':
4784 if (s->diff_context > 0) {
4785 s->diff_context--;
4786 s->matched_line = 0;
4787 diff_view_indicate_progress(view);
4788 err = create_diff(s);
4789 if (s->first_displayed_line + view->nlines - 1 >
4790 s->nlines) {
4791 s->first_displayed_line = 1;
4792 s->last_displayed_line = view->nlines;
4794 } else
4795 view->count = 0;
4796 break;
4797 case ']':
4798 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4799 s->diff_context++;
4800 s->matched_line = 0;
4801 diff_view_indicate_progress(view);
4802 err = create_diff(s);
4803 } else
4804 view->count = 0;
4805 break;
4806 case '<':
4807 case ',':
4808 up = 1;
4809 /* FALL THROUGH */
4810 case '>':
4811 case '.':
4812 if (s->parent_view == NULL) {
4813 view->count = 0;
4814 break;
4816 s->parent_view->count = view->count;
4818 if (s->parent_view->type == TOG_VIEW_LOG) {
4819 ls = &s->parent_view->state.log;
4820 old_selected_entry = ls->selected_entry;
4822 err = input_log_view(NULL, s->parent_view,
4823 up ? KEY_UP : KEY_DOWN);
4824 if (err)
4825 break;
4826 view->count = s->parent_view->count;
4828 if (old_selected_entry == ls->selected_entry)
4829 break;
4831 err = set_selected_commit(s, ls->selected_entry);
4832 if (err)
4833 break;
4834 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4835 struct tog_blame_view_state *bs;
4836 struct got_object_id *id, *prev_id;
4838 bs = &s->parent_view->state.blame;
4839 prev_id = get_annotation_for_line(bs->blame.lines,
4840 bs->blame.nlines, bs->last_diffed_line);
4842 err = input_blame_view(&view, s->parent_view,
4843 up ? KEY_UP : KEY_DOWN);
4844 if (err)
4845 break;
4846 view->count = s->parent_view->count;
4848 if (prev_id == NULL)
4849 break;
4850 id = get_selected_commit_id(bs->blame.lines,
4851 bs->blame.nlines, bs->first_displayed_line,
4852 bs->selected_line);
4853 if (id == NULL)
4854 break;
4856 if (!got_object_id_cmp(prev_id, id))
4857 break;
4859 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4860 if (err)
4861 break;
4863 s->first_displayed_line = 1;
4864 s->last_displayed_line = view->nlines;
4865 s->matched_line = 0;
4866 view->x = 0;
4868 diff_view_indicate_progress(view);
4869 err = create_diff(s);
4870 break;
4871 default:
4872 view->count = 0;
4873 break;
4876 return err;
4879 static const struct got_error *
4880 cmd_diff(int argc, char *argv[])
4882 const struct got_error *error = NULL;
4883 struct got_repository *repo = NULL;
4884 struct got_worktree *worktree = NULL;
4885 struct got_object_id *id1 = NULL, *id2 = NULL;
4886 char *repo_path = NULL, *cwd = NULL;
4887 char *id_str1 = NULL, *id_str2 = NULL;
4888 char *label1 = NULL, *label2 = NULL;
4889 int diff_context = 3, ignore_whitespace = 0;
4890 int ch, force_text_diff = 0;
4891 const char *errstr;
4892 struct tog_view *view;
4893 int *pack_fds = NULL;
4895 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4896 switch (ch) {
4897 case 'a':
4898 force_text_diff = 1;
4899 break;
4900 case 'C':
4901 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4902 &errstr);
4903 if (errstr != NULL)
4904 errx(1, "number of context lines is %s: %s",
4905 errstr, errstr);
4906 break;
4907 case 'r':
4908 repo_path = realpath(optarg, NULL);
4909 if (repo_path == NULL)
4910 return got_error_from_errno2("realpath",
4911 optarg);
4912 got_path_strip_trailing_slashes(repo_path);
4913 break;
4914 case 'w':
4915 ignore_whitespace = 1;
4916 break;
4917 default:
4918 usage_diff();
4919 /* NOTREACHED */
4923 argc -= optind;
4924 argv += optind;
4926 if (argc == 0) {
4927 usage_diff(); /* TODO show local worktree changes */
4928 } else if (argc == 2) {
4929 id_str1 = argv[0];
4930 id_str2 = argv[1];
4931 } else
4932 usage_diff();
4934 error = got_repo_pack_fds_open(&pack_fds);
4935 if (error)
4936 goto done;
4938 if (repo_path == NULL) {
4939 cwd = getcwd(NULL, 0);
4940 if (cwd == NULL)
4941 return got_error_from_errno("getcwd");
4942 error = got_worktree_open(&worktree, cwd);
4943 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4944 goto done;
4945 if (worktree)
4946 repo_path =
4947 strdup(got_worktree_get_repo_path(worktree));
4948 else
4949 repo_path = strdup(cwd);
4950 if (repo_path == NULL) {
4951 error = got_error_from_errno("strdup");
4952 goto done;
4956 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4957 if (error)
4958 goto done;
4960 init_curses();
4962 error = apply_unveil(got_repo_get_path(repo), NULL);
4963 if (error)
4964 goto done;
4966 error = tog_load_refs(repo, 0);
4967 if (error)
4968 goto done;
4970 error = got_repo_match_object_id(&id1, &label1, id_str1,
4971 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4972 if (error)
4973 goto done;
4975 error = got_repo_match_object_id(&id2, &label2, id_str2,
4976 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4977 if (error)
4978 goto done;
4980 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4981 if (view == NULL) {
4982 error = got_error_from_errno("view_open");
4983 goto done;
4985 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4986 ignore_whitespace, force_text_diff, NULL, repo);
4987 if (error)
4988 goto done;
4989 error = view_loop(view);
4990 done:
4991 free(label1);
4992 free(label2);
4993 free(repo_path);
4994 free(cwd);
4995 if (repo) {
4996 const struct got_error *close_err = got_repo_close(repo);
4997 if (error == NULL)
4998 error = close_err;
5000 if (worktree)
5001 got_worktree_close(worktree);
5002 if (pack_fds) {
5003 const struct got_error *pack_err =
5004 got_repo_pack_fds_close(pack_fds);
5005 if (error == NULL)
5006 error = pack_err;
5008 tog_free_refs();
5009 return error;
5012 __dead static void
5013 usage_blame(void)
5015 endwin();
5016 fprintf(stderr,
5017 "usage: %s blame [-c commit] [-r repository-path] path\n",
5018 getprogname());
5019 exit(1);
5022 struct tog_blame_line {
5023 int annotated;
5024 struct got_object_id *id;
5027 static const struct got_error *
5028 draw_blame(struct tog_view *view)
5030 struct tog_blame_view_state *s = &view->state.blame;
5031 struct tog_blame *blame = &s->blame;
5032 regmatch_t *regmatch = &view->regmatch;
5033 const struct got_error *err;
5034 int lineno = 0, nprinted = 0;
5035 char *line = NULL;
5036 size_t linesize = 0;
5037 ssize_t linelen;
5038 wchar_t *wline;
5039 int width;
5040 struct tog_blame_line *blame_line;
5041 struct got_object_id *prev_id = NULL;
5042 char *id_str;
5043 struct tog_color *tc;
5045 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5046 if (err)
5047 return err;
5049 rewind(blame->f);
5050 werase(view->window);
5052 if (asprintf(&line, "commit %s", id_str) == -1) {
5053 err = got_error_from_errno("asprintf");
5054 free(id_str);
5055 return err;
5058 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5059 free(line);
5060 line = NULL;
5061 if (err)
5062 return err;
5063 if (view_needs_focus_indication(view))
5064 wstandout(view->window);
5065 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5066 if (tc)
5067 wattr_on(view->window,
5068 COLOR_PAIR(tc->colorpair), NULL);
5069 waddwstr(view->window, wline);
5070 if (tc)
5071 wattr_off(view->window,
5072 COLOR_PAIR(tc->colorpair), NULL);
5073 if (view_needs_focus_indication(view))
5074 wstandend(view->window);
5075 free(wline);
5076 wline = NULL;
5077 if (width < view->ncols - 1)
5078 waddch(view->window, '\n');
5080 if (asprintf(&line, "[%d/%d] %s%s",
5081 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5082 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5083 free(id_str);
5084 return got_error_from_errno("asprintf");
5086 free(id_str);
5087 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5088 free(line);
5089 line = NULL;
5090 if (err)
5091 return err;
5092 waddwstr(view->window, wline);
5093 free(wline);
5094 wline = NULL;
5095 if (width < view->ncols - 1)
5096 waddch(view->window, '\n');
5098 s->eof = 0;
5099 view->maxx = 0;
5100 while (nprinted < view->nlines - 2) {
5101 linelen = getline(&line, &linesize, blame->f);
5102 if (linelen == -1) {
5103 if (feof(blame->f)) {
5104 s->eof = 1;
5105 break;
5107 free(line);
5108 return got_ferror(blame->f, GOT_ERR_IO);
5110 if (++lineno < s->first_displayed_line)
5111 continue;
5113 /* Set view->maxx based on full line length. */
5114 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5115 if (err) {
5116 free(line);
5117 return err;
5119 free(wline);
5120 wline = NULL;
5121 view->maxx = MAX(view->maxx, width);
5123 if (nprinted == s->selected_line - 1)
5124 wstandout(view->window);
5126 if (blame->nlines > 0) {
5127 blame_line = &blame->lines[lineno - 1];
5128 if (blame_line->annotated && prev_id &&
5129 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5130 !(nprinted == s->selected_line - 1)) {
5131 waddstr(view->window, " ");
5132 } else if (blame_line->annotated) {
5133 char *id_str;
5134 err = got_object_id_str(&id_str,
5135 blame_line->id);
5136 if (err) {
5137 free(line);
5138 return err;
5140 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5141 if (tc)
5142 wattr_on(view->window,
5143 COLOR_PAIR(tc->colorpair), NULL);
5144 wprintw(view->window, "%.8s", id_str);
5145 if (tc)
5146 wattr_off(view->window,
5147 COLOR_PAIR(tc->colorpair), NULL);
5148 free(id_str);
5149 prev_id = blame_line->id;
5150 } else {
5151 waddstr(view->window, "........");
5152 prev_id = NULL;
5154 } else {
5155 waddstr(view->window, "........");
5156 prev_id = NULL;
5159 if (nprinted == s->selected_line - 1)
5160 wstandend(view->window);
5161 waddstr(view->window, " ");
5163 if (view->ncols <= 9) {
5164 width = 9;
5165 } else if (s->first_displayed_line + nprinted ==
5166 s->matched_line &&
5167 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5168 err = add_matched_line(&width, line, view->ncols - 9, 9,
5169 view->window, view->x, regmatch);
5170 if (err) {
5171 free(line);
5172 return err;
5174 width += 9;
5175 } else {
5176 int skip;
5177 err = format_line(&wline, &width, &skip, line,
5178 view->x, view->ncols - 9, 9, 1);
5179 if (err) {
5180 free(line);
5181 return err;
5183 waddwstr(view->window, &wline[skip]);
5184 width += 9;
5185 free(wline);
5186 wline = NULL;
5189 if (width <= view->ncols - 1)
5190 waddch(view->window, '\n');
5191 if (++nprinted == 1)
5192 s->first_displayed_line = lineno;
5194 free(line);
5195 s->last_displayed_line = lineno;
5197 view_border(view);
5199 return NULL;
5202 static const struct got_error *
5203 blame_cb(void *arg, int nlines, int lineno,
5204 struct got_commit_object *commit, struct got_object_id *id)
5206 const struct got_error *err = NULL;
5207 struct tog_blame_cb_args *a = arg;
5208 struct tog_blame_line *line;
5209 int errcode;
5211 if (nlines != a->nlines ||
5212 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5213 return got_error(GOT_ERR_RANGE);
5215 errcode = pthread_mutex_lock(&tog_mutex);
5216 if (errcode)
5217 return got_error_set_errno(errcode, "pthread_mutex_lock");
5219 if (*a->quit) { /* user has quit the blame view */
5220 err = got_error(GOT_ERR_ITER_COMPLETED);
5221 goto done;
5224 if (lineno == -1)
5225 goto done; /* no change in this commit */
5227 line = &a->lines[lineno - 1];
5228 if (line->annotated)
5229 goto done;
5231 line->id = got_object_id_dup(id);
5232 if (line->id == NULL) {
5233 err = got_error_from_errno("got_object_id_dup");
5234 goto done;
5236 line->annotated = 1;
5237 done:
5238 errcode = pthread_mutex_unlock(&tog_mutex);
5239 if (errcode)
5240 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5241 return err;
5244 static void *
5245 blame_thread(void *arg)
5247 const struct got_error *err, *close_err;
5248 struct tog_blame_thread_args *ta = arg;
5249 struct tog_blame_cb_args *a = ta->cb_args;
5250 int errcode, fd1 = -1, fd2 = -1;
5251 FILE *f1 = NULL, *f2 = NULL;
5253 fd1 = got_opentempfd();
5254 if (fd1 == -1)
5255 return (void *)got_error_from_errno("got_opentempfd");
5257 fd2 = got_opentempfd();
5258 if (fd2 == -1) {
5259 err = got_error_from_errno("got_opentempfd");
5260 goto done;
5263 f1 = got_opentemp();
5264 if (f1 == NULL) {
5265 err = (void *)got_error_from_errno("got_opentemp");
5266 goto done;
5268 f2 = got_opentemp();
5269 if (f2 == NULL) {
5270 err = (void *)got_error_from_errno("got_opentemp");
5271 goto done;
5274 err = block_signals_used_by_main_thread();
5275 if (err)
5276 goto done;
5278 err = got_blame(ta->path, a->commit_id, ta->repo,
5279 tog_diff_algo, blame_cb, ta->cb_args,
5280 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5281 if (err && err->code == GOT_ERR_CANCELLED)
5282 err = NULL;
5284 errcode = pthread_mutex_lock(&tog_mutex);
5285 if (errcode) {
5286 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5287 goto done;
5290 close_err = got_repo_close(ta->repo);
5291 if (err == NULL)
5292 err = close_err;
5293 ta->repo = NULL;
5294 *ta->complete = 1;
5296 errcode = pthread_mutex_unlock(&tog_mutex);
5297 if (errcode && err == NULL)
5298 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5300 done:
5301 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5302 err = got_error_from_errno("close");
5303 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5304 err = got_error_from_errno("close");
5305 if (f1 && fclose(f1) == EOF && err == NULL)
5306 err = got_error_from_errno("fclose");
5307 if (f2 && fclose(f2) == EOF && err == NULL)
5308 err = got_error_from_errno("fclose");
5310 return (void *)err;
5313 static struct got_object_id *
5314 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5315 int first_displayed_line, int selected_line)
5317 struct tog_blame_line *line;
5319 if (nlines <= 0)
5320 return NULL;
5322 line = &lines[first_displayed_line - 1 + selected_line - 1];
5323 if (!line->annotated)
5324 return NULL;
5326 return line->id;
5329 static struct got_object_id *
5330 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5331 int lineno)
5333 struct tog_blame_line *line;
5335 if (nlines <= 0 || lineno >= nlines)
5336 return NULL;
5338 line = &lines[lineno - 1];
5339 if (!line->annotated)
5340 return NULL;
5342 return line->id;
5345 static const struct got_error *
5346 stop_blame(struct tog_blame *blame)
5348 const struct got_error *err = NULL;
5349 int i;
5351 if (blame->thread) {
5352 int errcode;
5353 errcode = pthread_mutex_unlock(&tog_mutex);
5354 if (errcode)
5355 return got_error_set_errno(errcode,
5356 "pthread_mutex_unlock");
5357 errcode = pthread_join(blame->thread, (void **)&err);
5358 if (errcode)
5359 return got_error_set_errno(errcode, "pthread_join");
5360 errcode = pthread_mutex_lock(&tog_mutex);
5361 if (errcode)
5362 return got_error_set_errno(errcode,
5363 "pthread_mutex_lock");
5364 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5365 err = NULL;
5366 blame->thread = NULL;
5368 if (blame->thread_args.repo) {
5369 const struct got_error *close_err;
5370 close_err = got_repo_close(blame->thread_args.repo);
5371 if (err == NULL)
5372 err = close_err;
5373 blame->thread_args.repo = NULL;
5375 if (blame->f) {
5376 if (fclose(blame->f) == EOF && err == NULL)
5377 err = got_error_from_errno("fclose");
5378 blame->f = NULL;
5380 if (blame->lines) {
5381 for (i = 0; i < blame->nlines; i++)
5382 free(blame->lines[i].id);
5383 free(blame->lines);
5384 blame->lines = NULL;
5386 free(blame->cb_args.commit_id);
5387 blame->cb_args.commit_id = NULL;
5388 if (blame->pack_fds) {
5389 const struct got_error *pack_err =
5390 got_repo_pack_fds_close(blame->pack_fds);
5391 if (err == NULL)
5392 err = pack_err;
5393 blame->pack_fds = NULL;
5395 return err;
5398 static const struct got_error *
5399 cancel_blame_view(void *arg)
5401 const struct got_error *err = NULL;
5402 int *done = arg;
5403 int errcode;
5405 errcode = pthread_mutex_lock(&tog_mutex);
5406 if (errcode)
5407 return got_error_set_errno(errcode,
5408 "pthread_mutex_unlock");
5410 if (*done)
5411 err = got_error(GOT_ERR_CANCELLED);
5413 errcode = pthread_mutex_unlock(&tog_mutex);
5414 if (errcode)
5415 return got_error_set_errno(errcode,
5416 "pthread_mutex_lock");
5418 return err;
5421 static const struct got_error *
5422 run_blame(struct tog_view *view)
5424 struct tog_blame_view_state *s = &view->state.blame;
5425 struct tog_blame *blame = &s->blame;
5426 const struct got_error *err = NULL;
5427 struct got_commit_object *commit = NULL;
5428 struct got_blob_object *blob = NULL;
5429 struct got_repository *thread_repo = NULL;
5430 struct got_object_id *obj_id = NULL;
5431 int obj_type, fd = -1;
5432 int *pack_fds = NULL;
5434 err = got_object_open_as_commit(&commit, s->repo,
5435 &s->blamed_commit->id);
5436 if (err)
5437 return err;
5439 fd = got_opentempfd();
5440 if (fd == -1) {
5441 err = got_error_from_errno("got_opentempfd");
5442 goto done;
5445 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5446 if (err)
5447 goto done;
5449 err = got_object_get_type(&obj_type, s->repo, obj_id);
5450 if (err)
5451 goto done;
5453 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5454 err = got_error(GOT_ERR_OBJ_TYPE);
5455 goto done;
5458 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5459 if (err)
5460 goto done;
5461 blame->f = got_opentemp();
5462 if (blame->f == NULL) {
5463 err = got_error_from_errno("got_opentemp");
5464 goto done;
5466 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5467 &blame->line_offsets, blame->f, blob);
5468 if (err)
5469 goto done;
5470 if (blame->nlines == 0) {
5471 s->blame_complete = 1;
5472 goto done;
5475 /* Don't include \n at EOF in the blame line count. */
5476 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5477 blame->nlines--;
5479 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5480 if (blame->lines == NULL) {
5481 err = got_error_from_errno("calloc");
5482 goto done;
5485 err = got_repo_pack_fds_open(&pack_fds);
5486 if (err)
5487 goto done;
5488 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5489 pack_fds);
5490 if (err)
5491 goto done;
5493 blame->pack_fds = pack_fds;
5494 blame->cb_args.view = view;
5495 blame->cb_args.lines = blame->lines;
5496 blame->cb_args.nlines = blame->nlines;
5497 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5498 if (blame->cb_args.commit_id == NULL) {
5499 err = got_error_from_errno("got_object_id_dup");
5500 goto done;
5502 blame->cb_args.quit = &s->done;
5504 blame->thread_args.path = s->path;
5505 blame->thread_args.repo = thread_repo;
5506 blame->thread_args.cb_args = &blame->cb_args;
5507 blame->thread_args.complete = &s->blame_complete;
5508 blame->thread_args.cancel_cb = cancel_blame_view;
5509 blame->thread_args.cancel_arg = &s->done;
5510 s->blame_complete = 0;
5512 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5513 s->first_displayed_line = 1;
5514 s->last_displayed_line = view->nlines;
5515 s->selected_line = 1;
5517 s->matched_line = 0;
5519 done:
5520 if (commit)
5521 got_object_commit_close(commit);
5522 if (fd != -1 && close(fd) == -1 && err == NULL)
5523 err = got_error_from_errno("close");
5524 if (blob)
5525 got_object_blob_close(blob);
5526 free(obj_id);
5527 if (err)
5528 stop_blame(blame);
5529 return err;
5532 static const struct got_error *
5533 open_blame_view(struct tog_view *view, char *path,
5534 struct got_object_id *commit_id, struct got_repository *repo)
5536 const struct got_error *err = NULL;
5537 struct tog_blame_view_state *s = &view->state.blame;
5539 STAILQ_INIT(&s->blamed_commits);
5541 s->path = strdup(path);
5542 if (s->path == NULL)
5543 return got_error_from_errno("strdup");
5545 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5546 if (err) {
5547 free(s->path);
5548 return err;
5551 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5552 s->first_displayed_line = 1;
5553 s->last_displayed_line = view->nlines;
5554 s->selected_line = 1;
5555 s->blame_complete = 0;
5556 s->repo = repo;
5557 s->commit_id = commit_id;
5558 memset(&s->blame, 0, sizeof(s->blame));
5560 STAILQ_INIT(&s->colors);
5561 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5562 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5563 get_color_value("TOG_COLOR_COMMIT"));
5564 if (err)
5565 return err;
5568 view->show = show_blame_view;
5569 view->input = input_blame_view;
5570 view->reset = reset_blame_view;
5571 view->close = close_blame_view;
5572 view->search_start = search_start_blame_view;
5573 view->search_next = search_next_blame_view;
5575 return run_blame(view);
5578 static const struct got_error *
5579 close_blame_view(struct tog_view *view)
5581 const struct got_error *err = NULL;
5582 struct tog_blame_view_state *s = &view->state.blame;
5584 if (s->blame.thread)
5585 err = stop_blame(&s->blame);
5587 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5588 struct got_object_qid *blamed_commit;
5589 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5590 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5591 got_object_qid_free(blamed_commit);
5594 free(s->path);
5595 free_colors(&s->colors);
5596 return err;
5599 static const struct got_error *
5600 search_start_blame_view(struct tog_view *view)
5602 struct tog_blame_view_state *s = &view->state.blame;
5604 s->matched_line = 0;
5605 return NULL;
5608 static const struct got_error *
5609 search_next_blame_view(struct tog_view *view)
5611 struct tog_blame_view_state *s = &view->state.blame;
5612 const struct got_error *err = NULL;
5613 int lineno;
5614 char *line = NULL;
5615 size_t linesize = 0;
5616 ssize_t linelen;
5618 if (!view->searching) {
5619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5620 return NULL;
5623 if (s->matched_line) {
5624 if (view->searching == TOG_SEARCH_FORWARD)
5625 lineno = s->matched_line + 1;
5626 else
5627 lineno = s->matched_line - 1;
5628 } else
5629 lineno = s->first_displayed_line - 1 + s->selected_line;
5631 while (1) {
5632 off_t offset;
5634 if (lineno <= 0 || lineno > s->blame.nlines) {
5635 if (s->matched_line == 0) {
5636 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5637 break;
5640 if (view->searching == TOG_SEARCH_FORWARD)
5641 lineno = 1;
5642 else
5643 lineno = s->blame.nlines;
5646 offset = s->blame.line_offsets[lineno - 1];
5647 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5648 free(line);
5649 return got_error_from_errno("fseeko");
5651 linelen = getline(&line, &linesize, s->blame.f);
5652 if (linelen != -1) {
5653 char *exstr;
5654 err = expand_tab(&exstr, line);
5655 if (err)
5656 break;
5657 if (match_line(exstr, &view->regex, 1,
5658 &view->regmatch)) {
5659 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5660 s->matched_line = lineno;
5661 free(exstr);
5662 break;
5664 free(exstr);
5666 if (view->searching == TOG_SEARCH_FORWARD)
5667 lineno++;
5668 else
5669 lineno--;
5671 free(line);
5673 if (s->matched_line) {
5674 s->first_displayed_line = s->matched_line;
5675 s->selected_line = 1;
5678 return err;
5681 static const struct got_error *
5682 show_blame_view(struct tog_view *view)
5684 const struct got_error *err = NULL;
5685 struct tog_blame_view_state *s = &view->state.blame;
5686 int errcode;
5688 if (s->blame.thread == NULL && !s->blame_complete) {
5689 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5690 &s->blame.thread_args);
5691 if (errcode)
5692 return got_error_set_errno(errcode, "pthread_create");
5694 halfdelay(1); /* fast refresh while annotating */
5697 if (s->blame_complete)
5698 halfdelay(10); /* disable fast refresh */
5700 err = draw_blame(view);
5702 view_border(view);
5703 return err;
5706 static const struct got_error *
5707 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5709 const struct got_error *err = NULL, *thread_err = NULL;
5710 struct tog_view *diff_view;
5711 struct tog_blame_view_state *s = &view->state.blame;
5712 int eos, nscroll, begin_y = 0, begin_x = 0;
5714 eos = nscroll = view->nlines - 2;
5715 if (view_is_hsplit_top(view))
5716 --eos; /* border */
5718 switch (ch) {
5719 case '0':
5720 view->x = 0;
5721 break;
5722 case '$':
5723 view->x = MAX(view->maxx - view->ncols / 3, 0);
5724 view->count = 0;
5725 break;
5726 case KEY_RIGHT:
5727 case 'l':
5728 if (view->x + view->ncols / 3 < view->maxx)
5729 view->x += 2; /* move two columns right */
5730 else
5731 view->count = 0;
5732 break;
5733 case KEY_LEFT:
5734 case 'h':
5735 view->x -= MIN(view->x, 2); /* move two columns back */
5736 if (view->x <= 0)
5737 view->count = 0;
5738 break;
5739 case 'q':
5740 s->done = 1;
5741 break;
5742 case 'g':
5743 case KEY_HOME:
5744 s->selected_line = 1;
5745 s->first_displayed_line = 1;
5746 view->count = 0;
5747 break;
5748 case 'G':
5749 case KEY_END:
5750 if (s->blame.nlines < eos) {
5751 s->selected_line = s->blame.nlines;
5752 s->first_displayed_line = 1;
5753 } else {
5754 s->selected_line = eos;
5755 s->first_displayed_line = s->blame.nlines - (eos - 1);
5757 view->count = 0;
5758 break;
5759 case 'k':
5760 case KEY_UP:
5761 case CTRL('p'):
5762 if (s->selected_line > 1)
5763 s->selected_line--;
5764 else if (s->selected_line == 1 &&
5765 s->first_displayed_line > 1)
5766 s->first_displayed_line--;
5767 else
5768 view->count = 0;
5769 break;
5770 case CTRL('u'):
5771 case 'u':
5772 nscroll /= 2;
5773 /* FALL THROUGH */
5774 case KEY_PPAGE:
5775 case CTRL('b'):
5776 case 'b':
5777 if (s->first_displayed_line == 1) {
5778 if (view->count > 1)
5779 nscroll += nscroll;
5780 s->selected_line = MAX(1, s->selected_line - nscroll);
5781 view->count = 0;
5782 break;
5784 if (s->first_displayed_line > nscroll)
5785 s->first_displayed_line -= nscroll;
5786 else
5787 s->first_displayed_line = 1;
5788 break;
5789 case 'j':
5790 case KEY_DOWN:
5791 case CTRL('n'):
5792 if (s->selected_line < eos && s->first_displayed_line +
5793 s->selected_line <= s->blame.nlines)
5794 s->selected_line++;
5795 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5796 s->first_displayed_line++;
5797 else
5798 view->count = 0;
5799 break;
5800 case 'c':
5801 case 'p': {
5802 struct got_object_id *id = NULL;
5804 view->count = 0;
5805 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5806 s->first_displayed_line, s->selected_line);
5807 if (id == NULL)
5808 break;
5809 if (ch == 'p') {
5810 struct got_commit_object *commit, *pcommit;
5811 struct got_object_qid *pid;
5812 struct got_object_id *blob_id = NULL;
5813 int obj_type;
5814 err = got_object_open_as_commit(&commit,
5815 s->repo, id);
5816 if (err)
5817 break;
5818 pid = STAILQ_FIRST(
5819 got_object_commit_get_parent_ids(commit));
5820 if (pid == NULL) {
5821 got_object_commit_close(commit);
5822 break;
5824 /* Check if path history ends here. */
5825 err = got_object_open_as_commit(&pcommit,
5826 s->repo, &pid->id);
5827 if (err)
5828 break;
5829 err = got_object_id_by_path(&blob_id, s->repo,
5830 pcommit, s->path);
5831 got_object_commit_close(pcommit);
5832 if (err) {
5833 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5834 err = NULL;
5835 got_object_commit_close(commit);
5836 break;
5838 err = got_object_get_type(&obj_type, s->repo,
5839 blob_id);
5840 free(blob_id);
5841 /* Can't blame non-blob type objects. */
5842 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5843 got_object_commit_close(commit);
5844 break;
5846 err = got_object_qid_alloc(&s->blamed_commit,
5847 &pid->id);
5848 got_object_commit_close(commit);
5849 } else {
5850 if (got_object_id_cmp(id,
5851 &s->blamed_commit->id) == 0)
5852 break;
5853 err = got_object_qid_alloc(&s->blamed_commit,
5854 id);
5856 if (err)
5857 break;
5858 s->done = 1;
5859 thread_err = stop_blame(&s->blame);
5860 s->done = 0;
5861 if (thread_err)
5862 break;
5863 STAILQ_INSERT_HEAD(&s->blamed_commits,
5864 s->blamed_commit, entry);
5865 err = run_blame(view);
5866 if (err)
5867 break;
5868 break;
5870 case 'C': {
5871 struct got_object_qid *first;
5873 view->count = 0;
5874 first = STAILQ_FIRST(&s->blamed_commits);
5875 if (!got_object_id_cmp(&first->id, s->commit_id))
5876 break;
5877 s->done = 1;
5878 thread_err = stop_blame(&s->blame);
5879 s->done = 0;
5880 if (thread_err)
5881 break;
5882 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5883 got_object_qid_free(s->blamed_commit);
5884 s->blamed_commit =
5885 STAILQ_FIRST(&s->blamed_commits);
5886 err = run_blame(view);
5887 if (err)
5888 break;
5889 break;
5891 case KEY_ENTER:
5892 case '\r': {
5893 struct got_object_id *id = NULL;
5894 struct got_object_qid *pid;
5895 struct got_commit_object *commit = NULL;
5897 view->count = 0;
5898 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5899 s->first_displayed_line, s->selected_line);
5900 if (id == NULL)
5901 break;
5902 err = got_object_open_as_commit(&commit, s->repo, id);
5903 if (err)
5904 break;
5905 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5906 if (*new_view) {
5907 /* traversed from diff view, release diff resources */
5908 err = close_diff_view(*new_view);
5909 if (err)
5910 break;
5911 diff_view = *new_view;
5912 } else {
5913 if (view_is_parent_view(view))
5914 view_get_split(view, &begin_y, &begin_x);
5916 diff_view = view_open(0, 0, begin_y, begin_x,
5917 TOG_VIEW_DIFF);
5918 if (diff_view == NULL) {
5919 got_object_commit_close(commit);
5920 err = got_error_from_errno("view_open");
5921 break;
5924 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5925 id, NULL, NULL, 3, 0, 0, view, s->repo);
5926 got_object_commit_close(commit);
5927 if (err) {
5928 view_close(diff_view);
5929 break;
5931 s->last_diffed_line = s->first_displayed_line - 1 +
5932 s->selected_line;
5933 if (*new_view)
5934 break; /* still open from active diff view */
5935 if (view_is_parent_view(view) &&
5936 view->mode == TOG_VIEW_SPLIT_HRZN) {
5937 err = view_init_hsplit(view, begin_y);
5938 if (err)
5939 break;
5942 view->focussed = 0;
5943 diff_view->focussed = 1;
5944 diff_view->mode = view->mode;
5945 diff_view->nlines = view->lines - begin_y;
5946 if (view_is_parent_view(view)) {
5947 view_transfer_size(diff_view, view);
5948 err = view_close_child(view);
5949 if (err)
5950 break;
5951 err = view_set_child(view, diff_view);
5952 if (err)
5953 break;
5954 view->focus_child = 1;
5955 } else
5956 *new_view = diff_view;
5957 if (err)
5958 break;
5959 break;
5961 case CTRL('d'):
5962 case 'd':
5963 nscroll /= 2;
5964 /* FALL THROUGH */
5965 case KEY_NPAGE:
5966 case CTRL('f'):
5967 case 'f':
5968 case ' ':
5969 if (s->last_displayed_line >= s->blame.nlines &&
5970 s->selected_line >= MIN(s->blame.nlines,
5971 view->nlines - 2)) {
5972 view->count = 0;
5973 break;
5975 if (s->last_displayed_line >= s->blame.nlines &&
5976 s->selected_line < view->nlines - 2) {
5977 s->selected_line +=
5978 MIN(nscroll, s->last_displayed_line -
5979 s->first_displayed_line - s->selected_line + 1);
5981 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5982 s->first_displayed_line += nscroll;
5983 else
5984 s->first_displayed_line =
5985 s->blame.nlines - (view->nlines - 3);
5986 break;
5987 case KEY_RESIZE:
5988 if (s->selected_line > view->nlines - 2) {
5989 s->selected_line = MIN(s->blame.nlines,
5990 view->nlines - 2);
5992 break;
5993 default:
5994 view->count = 0;
5995 break;
5997 return thread_err ? thread_err : err;
6000 static const struct got_error *
6001 reset_blame_view(struct tog_view *view)
6003 const struct got_error *err;
6004 struct tog_blame_view_state *s = &view->state.blame;
6006 view->count = 0;
6007 s->done = 1;
6008 err = stop_blame(&s->blame);
6009 s->done = 0;
6010 if (err)
6011 return err;
6012 return run_blame(view);
6015 static const struct got_error *
6016 cmd_blame(int argc, char *argv[])
6018 const struct got_error *error;
6019 struct got_repository *repo = NULL;
6020 struct got_worktree *worktree = NULL;
6021 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6022 char *link_target = NULL;
6023 struct got_object_id *commit_id = NULL;
6024 struct got_commit_object *commit = NULL;
6025 char *commit_id_str = NULL;
6026 int ch;
6027 struct tog_view *view;
6028 int *pack_fds = NULL;
6030 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6031 switch (ch) {
6032 case 'c':
6033 commit_id_str = optarg;
6034 break;
6035 case 'r':
6036 repo_path = realpath(optarg, NULL);
6037 if (repo_path == NULL)
6038 return got_error_from_errno2("realpath",
6039 optarg);
6040 break;
6041 default:
6042 usage_blame();
6043 /* NOTREACHED */
6047 argc -= optind;
6048 argv += optind;
6050 if (argc != 1)
6051 usage_blame();
6053 error = got_repo_pack_fds_open(&pack_fds);
6054 if (error != NULL)
6055 goto done;
6057 if (repo_path == NULL) {
6058 cwd = getcwd(NULL, 0);
6059 if (cwd == NULL)
6060 return got_error_from_errno("getcwd");
6061 error = got_worktree_open(&worktree, cwd);
6062 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6063 goto done;
6064 if (worktree)
6065 repo_path =
6066 strdup(got_worktree_get_repo_path(worktree));
6067 else
6068 repo_path = strdup(cwd);
6069 if (repo_path == NULL) {
6070 error = got_error_from_errno("strdup");
6071 goto done;
6075 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6076 if (error != NULL)
6077 goto done;
6079 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6080 worktree);
6081 if (error)
6082 goto done;
6084 init_curses();
6086 error = apply_unveil(got_repo_get_path(repo), NULL);
6087 if (error)
6088 goto done;
6090 error = tog_load_refs(repo, 0);
6091 if (error)
6092 goto done;
6094 if (commit_id_str == NULL) {
6095 struct got_reference *head_ref;
6096 error = got_ref_open(&head_ref, repo, worktree ?
6097 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6098 if (error != NULL)
6099 goto done;
6100 error = got_ref_resolve(&commit_id, repo, head_ref);
6101 got_ref_close(head_ref);
6102 } else {
6103 error = got_repo_match_object_id(&commit_id, NULL,
6104 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6106 if (error != NULL)
6107 goto done;
6109 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6110 if (view == NULL) {
6111 error = got_error_from_errno("view_open");
6112 goto done;
6115 error = got_object_open_as_commit(&commit, repo, commit_id);
6116 if (error)
6117 goto done;
6119 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6120 commit, repo);
6121 if (error)
6122 goto done;
6124 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6125 commit_id, repo);
6126 if (error)
6127 goto done;
6128 if (worktree) {
6129 /* Release work tree lock. */
6130 got_worktree_close(worktree);
6131 worktree = NULL;
6133 error = view_loop(view);
6134 done:
6135 free(repo_path);
6136 free(in_repo_path);
6137 free(link_target);
6138 free(cwd);
6139 free(commit_id);
6140 if (commit)
6141 got_object_commit_close(commit);
6142 if (worktree)
6143 got_worktree_close(worktree);
6144 if (repo) {
6145 const struct got_error *close_err = got_repo_close(repo);
6146 if (error == NULL)
6147 error = close_err;
6149 if (pack_fds) {
6150 const struct got_error *pack_err =
6151 got_repo_pack_fds_close(pack_fds);
6152 if (error == NULL)
6153 error = pack_err;
6155 tog_free_refs();
6156 return error;
6159 static const struct got_error *
6160 draw_tree_entries(struct tog_view *view, const char *parent_path)
6162 struct tog_tree_view_state *s = &view->state.tree;
6163 const struct got_error *err = NULL;
6164 struct got_tree_entry *te;
6165 wchar_t *wline;
6166 struct tog_color *tc;
6167 int width, n, i, nentries;
6168 int limit = view->nlines;
6170 s->ndisplayed = 0;
6171 if (view_is_hsplit_top(view))
6172 --limit; /* border */
6174 werase(view->window);
6176 if (limit == 0)
6177 return NULL;
6179 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6180 0, 0);
6181 if (err)
6182 return err;
6183 if (view_needs_focus_indication(view))
6184 wstandout(view->window);
6185 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6186 if (tc)
6187 wattr_on(view->window,
6188 COLOR_PAIR(tc->colorpair), NULL);
6189 waddwstr(view->window, wline);
6190 if (tc)
6191 wattr_off(view->window,
6192 COLOR_PAIR(tc->colorpair), NULL);
6193 if (view_needs_focus_indication(view))
6194 wstandend(view->window);
6195 free(wline);
6196 wline = NULL;
6197 if (width < view->ncols - 1)
6198 waddch(view->window, '\n');
6199 if (--limit <= 0)
6200 return NULL;
6201 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6202 0, 0);
6203 if (err)
6204 return err;
6205 waddwstr(view->window, wline);
6206 free(wline);
6207 wline = NULL;
6208 if (width < view->ncols - 1)
6209 waddch(view->window, '\n');
6210 if (--limit <= 0)
6211 return NULL;
6212 waddch(view->window, '\n');
6213 if (--limit <= 0)
6214 return NULL;
6216 if (s->first_displayed_entry == NULL) {
6217 te = got_object_tree_get_first_entry(s->tree);
6218 if (s->selected == 0) {
6219 if (view->focussed)
6220 wstandout(view->window);
6221 s->selected_entry = NULL;
6223 waddstr(view->window, " ..\n"); /* parent directory */
6224 if (s->selected == 0 && view->focussed)
6225 wstandend(view->window);
6226 s->ndisplayed++;
6227 if (--limit <= 0)
6228 return NULL;
6229 n = 1;
6230 } else {
6231 n = 0;
6232 te = s->first_displayed_entry;
6235 nentries = got_object_tree_get_nentries(s->tree);
6236 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6237 char *line = NULL, *id_str = NULL, *link_target = NULL;
6238 const char *modestr = "";
6239 mode_t mode;
6241 te = got_object_tree_get_entry(s->tree, i);
6242 mode = got_tree_entry_get_mode(te);
6244 if (s->show_ids) {
6245 err = got_object_id_str(&id_str,
6246 got_tree_entry_get_id(te));
6247 if (err)
6248 return got_error_from_errno(
6249 "got_object_id_str");
6251 if (got_object_tree_entry_is_submodule(te))
6252 modestr = "$";
6253 else if (S_ISLNK(mode)) {
6254 int i;
6256 err = got_tree_entry_get_symlink_target(&link_target,
6257 te, s->repo);
6258 if (err) {
6259 free(id_str);
6260 return err;
6262 for (i = 0; i < strlen(link_target); i++) {
6263 if (!isprint((unsigned char)link_target[i]))
6264 link_target[i] = '?';
6266 modestr = "@";
6268 else if (S_ISDIR(mode))
6269 modestr = "/";
6270 else if (mode & S_IXUSR)
6271 modestr = "*";
6272 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6273 got_tree_entry_get_name(te), modestr,
6274 link_target ? " -> ": "",
6275 link_target ? link_target : "") == -1) {
6276 free(id_str);
6277 free(link_target);
6278 return got_error_from_errno("asprintf");
6280 free(id_str);
6281 free(link_target);
6282 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6283 0, 0);
6284 if (err) {
6285 free(line);
6286 break;
6288 if (n == s->selected) {
6289 if (view->focussed)
6290 wstandout(view->window);
6291 s->selected_entry = te;
6293 tc = match_color(&s->colors, line);
6294 if (tc)
6295 wattr_on(view->window,
6296 COLOR_PAIR(tc->colorpair), NULL);
6297 waddwstr(view->window, wline);
6298 if (tc)
6299 wattr_off(view->window,
6300 COLOR_PAIR(tc->colorpair), NULL);
6301 if (width < view->ncols - 1)
6302 waddch(view->window, '\n');
6303 if (n == s->selected && view->focussed)
6304 wstandend(view->window);
6305 free(line);
6306 free(wline);
6307 wline = NULL;
6308 n++;
6309 s->ndisplayed++;
6310 s->last_displayed_entry = te;
6311 if (--limit <= 0)
6312 break;
6315 return err;
6318 static void
6319 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6321 struct got_tree_entry *te;
6322 int isroot = s->tree == s->root;
6323 int i = 0;
6325 if (s->first_displayed_entry == NULL)
6326 return;
6328 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6329 while (i++ < maxscroll) {
6330 if (te == NULL) {
6331 if (!isroot)
6332 s->first_displayed_entry = NULL;
6333 break;
6335 s->first_displayed_entry = te;
6336 te = got_tree_entry_get_prev(s->tree, te);
6340 static const struct got_error *
6341 tree_scroll_down(struct tog_view *view, int maxscroll)
6343 struct tog_tree_view_state *s = &view->state.tree;
6344 struct got_tree_entry *next, *last;
6345 int n = 0;
6347 if (s->first_displayed_entry)
6348 next = got_tree_entry_get_next(s->tree,
6349 s->first_displayed_entry);
6350 else
6351 next = got_object_tree_get_first_entry(s->tree);
6353 last = s->last_displayed_entry;
6354 while (next && n++ < maxscroll) {
6355 if (last)
6356 last = got_tree_entry_get_next(s->tree, last);
6357 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6358 s->first_displayed_entry = next;
6359 next = got_tree_entry_get_next(s->tree, next);
6363 return NULL;
6366 static const struct got_error *
6367 tree_entry_path(char **path, struct tog_parent_trees *parents,
6368 struct got_tree_entry *te)
6370 const struct got_error *err = NULL;
6371 struct tog_parent_tree *pt;
6372 size_t len = 2; /* for leading slash and NUL */
6374 TAILQ_FOREACH(pt, parents, entry)
6375 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6376 + 1 /* slash */;
6377 if (te)
6378 len += strlen(got_tree_entry_get_name(te));
6380 *path = calloc(1, len);
6381 if (path == NULL)
6382 return got_error_from_errno("calloc");
6384 (*path)[0] = '/';
6385 pt = TAILQ_LAST(parents, tog_parent_trees);
6386 while (pt) {
6387 const char *name = got_tree_entry_get_name(pt->selected_entry);
6388 if (strlcat(*path, name, len) >= len) {
6389 err = got_error(GOT_ERR_NO_SPACE);
6390 goto done;
6392 if (strlcat(*path, "/", len) >= len) {
6393 err = got_error(GOT_ERR_NO_SPACE);
6394 goto done;
6396 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6398 if (te) {
6399 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6400 err = got_error(GOT_ERR_NO_SPACE);
6401 goto done;
6404 done:
6405 if (err) {
6406 free(*path);
6407 *path = NULL;
6409 return err;
6412 static const struct got_error *
6413 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6414 struct got_tree_entry *te, struct tog_parent_trees *parents,
6415 struct got_object_id *commit_id, struct got_repository *repo)
6417 const struct got_error *err = NULL;
6418 char *path;
6419 struct tog_view *blame_view;
6421 *new_view = NULL;
6423 err = tree_entry_path(&path, parents, te);
6424 if (err)
6425 return err;
6427 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6428 if (blame_view == NULL) {
6429 err = got_error_from_errno("view_open");
6430 goto done;
6433 err = open_blame_view(blame_view, path, commit_id, repo);
6434 if (err) {
6435 if (err->code == GOT_ERR_CANCELLED)
6436 err = NULL;
6437 view_close(blame_view);
6438 } else
6439 *new_view = blame_view;
6440 done:
6441 free(path);
6442 return err;
6445 static const struct got_error *
6446 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6447 struct tog_tree_view_state *s)
6449 struct tog_view *log_view;
6450 const struct got_error *err = NULL;
6451 char *path;
6453 *new_view = NULL;
6455 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6456 if (log_view == NULL)
6457 return got_error_from_errno("view_open");
6459 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6460 if (err)
6461 return err;
6463 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6464 path, 0);
6465 if (err)
6466 view_close(log_view);
6467 else
6468 *new_view = log_view;
6469 free(path);
6470 return err;
6473 static const struct got_error *
6474 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6475 const char *head_ref_name, struct got_repository *repo)
6477 const struct got_error *err = NULL;
6478 char *commit_id_str = NULL;
6479 struct tog_tree_view_state *s = &view->state.tree;
6480 struct got_commit_object *commit = NULL;
6482 TAILQ_INIT(&s->parents);
6483 STAILQ_INIT(&s->colors);
6485 s->commit_id = got_object_id_dup(commit_id);
6486 if (s->commit_id == NULL)
6487 return got_error_from_errno("got_object_id_dup");
6489 err = got_object_open_as_commit(&commit, repo, commit_id);
6490 if (err)
6491 goto done;
6494 * The root is opened here and will be closed when the view is closed.
6495 * Any visited subtrees and their path-wise parents are opened and
6496 * closed on demand.
6498 err = got_object_open_as_tree(&s->root, repo,
6499 got_object_commit_get_tree_id(commit));
6500 if (err)
6501 goto done;
6502 s->tree = s->root;
6504 err = got_object_id_str(&commit_id_str, commit_id);
6505 if (err != NULL)
6506 goto done;
6508 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6509 err = got_error_from_errno("asprintf");
6510 goto done;
6513 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6514 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6515 if (head_ref_name) {
6516 s->head_ref_name = strdup(head_ref_name);
6517 if (s->head_ref_name == NULL) {
6518 err = got_error_from_errno("strdup");
6519 goto done;
6522 s->repo = repo;
6524 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6525 err = add_color(&s->colors, "\\$$",
6526 TOG_COLOR_TREE_SUBMODULE,
6527 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6528 if (err)
6529 goto done;
6530 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6531 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6532 if (err)
6533 goto done;
6534 err = add_color(&s->colors, "/$",
6535 TOG_COLOR_TREE_DIRECTORY,
6536 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6537 if (err)
6538 goto done;
6540 err = add_color(&s->colors, "\\*$",
6541 TOG_COLOR_TREE_EXECUTABLE,
6542 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6543 if (err)
6544 goto done;
6546 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6547 get_color_value("TOG_COLOR_COMMIT"));
6548 if (err)
6549 goto done;
6552 view->show = show_tree_view;
6553 view->input = input_tree_view;
6554 view->close = close_tree_view;
6555 view->search_start = search_start_tree_view;
6556 view->search_next = search_next_tree_view;
6557 done:
6558 free(commit_id_str);
6559 if (commit)
6560 got_object_commit_close(commit);
6561 if (err)
6562 close_tree_view(view);
6563 return err;
6566 static const struct got_error *
6567 close_tree_view(struct tog_view *view)
6569 struct tog_tree_view_state *s = &view->state.tree;
6571 free_colors(&s->colors);
6572 free(s->tree_label);
6573 s->tree_label = NULL;
6574 free(s->commit_id);
6575 s->commit_id = NULL;
6576 free(s->head_ref_name);
6577 s->head_ref_name = NULL;
6578 while (!TAILQ_EMPTY(&s->parents)) {
6579 struct tog_parent_tree *parent;
6580 parent = TAILQ_FIRST(&s->parents);
6581 TAILQ_REMOVE(&s->parents, parent, entry);
6582 if (parent->tree != s->root)
6583 got_object_tree_close(parent->tree);
6584 free(parent);
6587 if (s->tree != NULL && s->tree != s->root)
6588 got_object_tree_close(s->tree);
6589 if (s->root)
6590 got_object_tree_close(s->root);
6591 return NULL;
6594 static const struct got_error *
6595 search_start_tree_view(struct tog_view *view)
6597 struct tog_tree_view_state *s = &view->state.tree;
6599 s->matched_entry = NULL;
6600 return NULL;
6603 static int
6604 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6606 regmatch_t regmatch;
6608 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6609 0) == 0;
6612 static const struct got_error *
6613 search_next_tree_view(struct tog_view *view)
6615 struct tog_tree_view_state *s = &view->state.tree;
6616 struct got_tree_entry *te = NULL;
6618 if (!view->searching) {
6619 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6620 return NULL;
6623 if (s->matched_entry) {
6624 if (view->searching == TOG_SEARCH_FORWARD) {
6625 if (s->selected_entry)
6626 te = got_tree_entry_get_next(s->tree,
6627 s->selected_entry);
6628 else
6629 te = got_object_tree_get_first_entry(s->tree);
6630 } else {
6631 if (s->selected_entry == NULL)
6632 te = got_object_tree_get_last_entry(s->tree);
6633 else
6634 te = got_tree_entry_get_prev(s->tree,
6635 s->selected_entry);
6637 } else {
6638 if (s->selected_entry)
6639 te = s->selected_entry;
6640 else if (view->searching == TOG_SEARCH_FORWARD)
6641 te = got_object_tree_get_first_entry(s->tree);
6642 else
6643 te = got_object_tree_get_last_entry(s->tree);
6646 while (1) {
6647 if (te == NULL) {
6648 if (s->matched_entry == NULL) {
6649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6650 return NULL;
6652 if (view->searching == TOG_SEARCH_FORWARD)
6653 te = got_object_tree_get_first_entry(s->tree);
6654 else
6655 te = got_object_tree_get_last_entry(s->tree);
6658 if (match_tree_entry(te, &view->regex)) {
6659 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6660 s->matched_entry = te;
6661 break;
6664 if (view->searching == TOG_SEARCH_FORWARD)
6665 te = got_tree_entry_get_next(s->tree, te);
6666 else
6667 te = got_tree_entry_get_prev(s->tree, te);
6670 if (s->matched_entry) {
6671 s->first_displayed_entry = s->matched_entry;
6672 s->selected = 0;
6675 return NULL;
6678 static const struct got_error *
6679 show_tree_view(struct tog_view *view)
6681 const struct got_error *err = NULL;
6682 struct tog_tree_view_state *s = &view->state.tree;
6683 char *parent_path;
6685 err = tree_entry_path(&parent_path, &s->parents, NULL);
6686 if (err)
6687 return err;
6689 err = draw_tree_entries(view, parent_path);
6690 free(parent_path);
6692 view_border(view);
6693 return err;
6696 static const struct got_error *
6697 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6699 const struct got_error *err = NULL;
6700 struct tog_tree_view_state *s = &view->state.tree;
6701 struct tog_view *log_view, *ref_view;
6702 struct got_tree_entry *te;
6703 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6705 switch (ch) {
6706 case 'i':
6707 s->show_ids = !s->show_ids;
6708 view->count = 0;
6709 break;
6710 case 'l':
6711 view->count = 0;
6712 if (!s->selected_entry)
6713 break;
6714 if (view_is_parent_view(view))
6715 view_get_split(view, &begin_y, &begin_x);
6716 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6717 if (view_is_parent_view(view) &&
6718 view->mode == TOG_VIEW_SPLIT_HRZN) {
6719 err = view_init_hsplit(view, begin_y);
6720 if (err)
6721 break;
6723 view->focussed = 0;
6724 log_view->focussed = 1;
6725 log_view->mode = view->mode;
6726 log_view->nlines = view->lines - begin_y;
6727 if (view_is_parent_view(view)) {
6728 view_transfer_size(log_view, view);
6729 err = view_close_child(view);
6730 if (err)
6731 return err;
6732 err = view_set_child(view, log_view);
6733 if (err)
6734 return err;
6735 view->focus_child = 1;
6736 } else
6737 *new_view = log_view;
6738 break;
6739 case 'r':
6740 view->count = 0;
6741 if (view_is_parent_view(view))
6742 view_get_split(view, &begin_y, &begin_x);
6743 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6744 if (ref_view == NULL)
6745 return got_error_from_errno("view_open");
6746 err = open_ref_view(ref_view, s->repo);
6747 if (err) {
6748 view_close(ref_view);
6749 return err;
6751 if (view_is_parent_view(view) &&
6752 view->mode == TOG_VIEW_SPLIT_HRZN) {
6753 err = view_init_hsplit(view, begin_y);
6754 if (err)
6755 break;
6757 view->focussed = 0;
6758 ref_view->focussed = 1;
6759 ref_view->mode = view->mode;
6760 ref_view->nlines = view->lines - begin_y;
6761 if (view_is_parent_view(view)) {
6762 view_transfer_size(ref_view, view);
6763 err = view_close_child(view);
6764 if (err)
6765 return err;
6766 err = view_set_child(view, ref_view);
6767 if (err)
6768 return err;
6769 view->focus_child = 1;
6770 } else
6771 *new_view = ref_view;
6772 break;
6773 case 'g':
6774 case KEY_HOME:
6775 s->selected = 0;
6776 view->count = 0;
6777 if (s->tree == s->root)
6778 s->first_displayed_entry =
6779 got_object_tree_get_first_entry(s->tree);
6780 else
6781 s->first_displayed_entry = NULL;
6782 break;
6783 case 'G':
6784 case KEY_END: {
6785 int eos = view->nlines - 3;
6787 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6788 --eos; /* border */
6789 s->selected = 0;
6790 view->count = 0;
6791 te = got_object_tree_get_last_entry(s->tree);
6792 for (n = 0; n < eos; n++) {
6793 if (te == NULL) {
6794 if(s->tree != s->root) {
6795 s->first_displayed_entry = NULL;
6796 n++;
6798 break;
6800 s->first_displayed_entry = te;
6801 te = got_tree_entry_get_prev(s->tree, te);
6803 if (n > 0)
6804 s->selected = n - 1;
6805 break;
6807 case 'k':
6808 case KEY_UP:
6809 case CTRL('p'):
6810 if (s->selected > 0) {
6811 s->selected--;
6812 break;
6814 tree_scroll_up(s, 1);
6815 if (s->selected_entry == NULL ||
6816 (s->tree == s->root && s->selected_entry ==
6817 got_object_tree_get_first_entry(s->tree)))
6818 view->count = 0;
6819 break;
6820 case CTRL('u'):
6821 case 'u':
6822 nscroll /= 2;
6823 /* FALL THROUGH */
6824 case KEY_PPAGE:
6825 case CTRL('b'):
6826 case 'b':
6827 if (s->tree == s->root) {
6828 if (got_object_tree_get_first_entry(s->tree) ==
6829 s->first_displayed_entry)
6830 s->selected -= MIN(s->selected, nscroll);
6831 } else {
6832 if (s->first_displayed_entry == NULL)
6833 s->selected -= MIN(s->selected, nscroll);
6835 tree_scroll_up(s, MAX(0, nscroll));
6836 if (s->selected_entry == NULL ||
6837 (s->tree == s->root && s->selected_entry ==
6838 got_object_tree_get_first_entry(s->tree)))
6839 view->count = 0;
6840 break;
6841 case 'j':
6842 case KEY_DOWN:
6843 case CTRL('n'):
6844 if (s->selected < s->ndisplayed - 1) {
6845 s->selected++;
6846 break;
6848 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6849 == NULL) {
6850 /* can't scroll any further */
6851 view->count = 0;
6852 break;
6854 tree_scroll_down(view, 1);
6855 break;
6856 case CTRL('d'):
6857 case 'd':
6858 nscroll /= 2;
6859 /* FALL THROUGH */
6860 case KEY_NPAGE:
6861 case CTRL('f'):
6862 case 'f':
6863 case ' ':
6864 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6865 == NULL) {
6866 /* can't scroll any further; move cursor down */
6867 if (s->selected < s->ndisplayed - 1)
6868 s->selected += MIN(nscroll,
6869 s->ndisplayed - s->selected - 1);
6870 else
6871 view->count = 0;
6872 break;
6874 tree_scroll_down(view, nscroll);
6875 break;
6876 case KEY_ENTER:
6877 case '\r':
6878 case KEY_BACKSPACE:
6879 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6880 struct tog_parent_tree *parent;
6881 /* user selected '..' */
6882 if (s->tree == s->root) {
6883 view->count = 0;
6884 break;
6886 parent = TAILQ_FIRST(&s->parents);
6887 TAILQ_REMOVE(&s->parents, parent,
6888 entry);
6889 got_object_tree_close(s->tree);
6890 s->tree = parent->tree;
6891 s->first_displayed_entry =
6892 parent->first_displayed_entry;
6893 s->selected_entry =
6894 parent->selected_entry;
6895 s->selected = parent->selected;
6896 if (s->selected > view->nlines - 3) {
6897 err = offset_selection_down(view);
6898 if (err)
6899 break;
6901 free(parent);
6902 } else if (S_ISDIR(got_tree_entry_get_mode(
6903 s->selected_entry))) {
6904 struct got_tree_object *subtree;
6905 view->count = 0;
6906 err = got_object_open_as_tree(&subtree, s->repo,
6907 got_tree_entry_get_id(s->selected_entry));
6908 if (err)
6909 break;
6910 err = tree_view_visit_subtree(s, subtree);
6911 if (err) {
6912 got_object_tree_close(subtree);
6913 break;
6915 } else if (S_ISREG(got_tree_entry_get_mode(
6916 s->selected_entry))) {
6917 struct tog_view *blame_view;
6918 int begin_x = 0, begin_y = 0;
6920 if (view_is_parent_view(view))
6921 view_get_split(view, &begin_y, &begin_x);
6923 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6924 s->selected_entry, &s->parents,
6925 s->commit_id, s->repo);
6926 if (err)
6927 break;
6929 if (view_is_parent_view(view) &&
6930 view->mode == TOG_VIEW_SPLIT_HRZN) {
6931 err = view_init_hsplit(view, begin_y);
6932 if (err)
6933 break;
6936 view->count = 0;
6937 view->focussed = 0;
6938 blame_view->focussed = 1;
6939 blame_view->mode = view->mode;
6940 blame_view->nlines = view->lines - begin_y;
6941 if (view_is_parent_view(view)) {
6942 view_transfer_size(blame_view, view);
6943 err = view_close_child(view);
6944 if (err)
6945 return err;
6946 err = view_set_child(view, blame_view);
6947 if (err)
6948 return err;
6949 view->focus_child = 1;
6950 } else
6951 *new_view = blame_view;
6953 break;
6954 case KEY_RESIZE:
6955 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6956 s->selected = view->nlines - 4;
6957 view->count = 0;
6958 break;
6959 default:
6960 view->count = 0;
6961 break;
6964 return err;
6967 __dead static void
6968 usage_tree(void)
6970 endwin();
6971 fprintf(stderr,
6972 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6973 getprogname());
6974 exit(1);
6977 static const struct got_error *
6978 cmd_tree(int argc, char *argv[])
6980 const struct got_error *error;
6981 struct got_repository *repo = NULL;
6982 struct got_worktree *worktree = NULL;
6983 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6984 struct got_object_id *commit_id = NULL;
6985 struct got_commit_object *commit = NULL;
6986 const char *commit_id_arg = NULL;
6987 char *label = NULL;
6988 struct got_reference *ref = NULL;
6989 const char *head_ref_name = NULL;
6990 int ch;
6991 struct tog_view *view;
6992 int *pack_fds = NULL;
6994 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6995 switch (ch) {
6996 case 'c':
6997 commit_id_arg = optarg;
6998 break;
6999 case 'r':
7000 repo_path = realpath(optarg, NULL);
7001 if (repo_path == NULL)
7002 return got_error_from_errno2("realpath",
7003 optarg);
7004 break;
7005 default:
7006 usage_tree();
7007 /* NOTREACHED */
7011 argc -= optind;
7012 argv += optind;
7014 if (argc > 1)
7015 usage_tree();
7017 error = got_repo_pack_fds_open(&pack_fds);
7018 if (error != NULL)
7019 goto done;
7021 if (repo_path == NULL) {
7022 cwd = getcwd(NULL, 0);
7023 if (cwd == NULL)
7024 return got_error_from_errno("getcwd");
7025 error = got_worktree_open(&worktree, cwd);
7026 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7027 goto done;
7028 if (worktree)
7029 repo_path =
7030 strdup(got_worktree_get_repo_path(worktree));
7031 else
7032 repo_path = strdup(cwd);
7033 if (repo_path == NULL) {
7034 error = got_error_from_errno("strdup");
7035 goto done;
7039 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7040 if (error != NULL)
7041 goto done;
7043 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7044 repo, worktree);
7045 if (error)
7046 goto done;
7048 init_curses();
7050 error = apply_unveil(got_repo_get_path(repo), NULL);
7051 if (error)
7052 goto done;
7054 error = tog_load_refs(repo, 0);
7055 if (error)
7056 goto done;
7058 if (commit_id_arg == NULL) {
7059 error = got_repo_match_object_id(&commit_id, &label,
7060 worktree ? got_worktree_get_head_ref_name(worktree) :
7061 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7062 if (error)
7063 goto done;
7064 head_ref_name = label;
7065 } else {
7066 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7067 if (error == NULL)
7068 head_ref_name = got_ref_get_name(ref);
7069 else if (error->code != GOT_ERR_NOT_REF)
7070 goto done;
7071 error = got_repo_match_object_id(&commit_id, NULL,
7072 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7073 if (error)
7074 goto done;
7077 error = got_object_open_as_commit(&commit, repo, commit_id);
7078 if (error)
7079 goto done;
7081 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7082 if (view == NULL) {
7083 error = got_error_from_errno("view_open");
7084 goto done;
7086 error = open_tree_view(view, commit_id, head_ref_name, repo);
7087 if (error)
7088 goto done;
7089 if (!got_path_is_root_dir(in_repo_path)) {
7090 error = tree_view_walk_path(&view->state.tree, commit,
7091 in_repo_path);
7092 if (error)
7093 goto done;
7096 if (worktree) {
7097 /* Release work tree lock. */
7098 got_worktree_close(worktree);
7099 worktree = NULL;
7101 error = view_loop(view);
7102 done:
7103 free(repo_path);
7104 free(cwd);
7105 free(commit_id);
7106 free(label);
7107 if (ref)
7108 got_ref_close(ref);
7109 if (repo) {
7110 const struct got_error *close_err = got_repo_close(repo);
7111 if (error == NULL)
7112 error = close_err;
7114 if (pack_fds) {
7115 const struct got_error *pack_err =
7116 got_repo_pack_fds_close(pack_fds);
7117 if (error == NULL)
7118 error = pack_err;
7120 tog_free_refs();
7121 return error;
7124 static const struct got_error *
7125 ref_view_load_refs(struct tog_ref_view_state *s)
7127 struct got_reflist_entry *sre;
7128 struct tog_reflist_entry *re;
7130 s->nrefs = 0;
7131 TAILQ_FOREACH(sre, &tog_refs, entry) {
7132 if (strncmp(got_ref_get_name(sre->ref),
7133 "refs/got/", 9) == 0 &&
7134 strncmp(got_ref_get_name(sre->ref),
7135 "refs/got/backup/", 16) != 0)
7136 continue;
7138 re = malloc(sizeof(*re));
7139 if (re == NULL)
7140 return got_error_from_errno("malloc");
7142 re->ref = got_ref_dup(sre->ref);
7143 if (re->ref == NULL)
7144 return got_error_from_errno("got_ref_dup");
7145 re->idx = s->nrefs++;
7146 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7149 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7150 return NULL;
7153 static void
7154 ref_view_free_refs(struct tog_ref_view_state *s)
7156 struct tog_reflist_entry *re;
7158 while (!TAILQ_EMPTY(&s->refs)) {
7159 re = TAILQ_FIRST(&s->refs);
7160 TAILQ_REMOVE(&s->refs, re, entry);
7161 got_ref_close(re->ref);
7162 free(re);
7166 static const struct got_error *
7167 open_ref_view(struct tog_view *view, struct got_repository *repo)
7169 const struct got_error *err = NULL;
7170 struct tog_ref_view_state *s = &view->state.ref;
7172 s->selected_entry = 0;
7173 s->repo = repo;
7175 TAILQ_INIT(&s->refs);
7176 STAILQ_INIT(&s->colors);
7178 err = ref_view_load_refs(s);
7179 if (err)
7180 return err;
7182 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7183 err = add_color(&s->colors, "^refs/heads/",
7184 TOG_COLOR_REFS_HEADS,
7185 get_color_value("TOG_COLOR_REFS_HEADS"));
7186 if (err)
7187 goto done;
7189 err = add_color(&s->colors, "^refs/tags/",
7190 TOG_COLOR_REFS_TAGS,
7191 get_color_value("TOG_COLOR_REFS_TAGS"));
7192 if (err)
7193 goto done;
7195 err = add_color(&s->colors, "^refs/remotes/",
7196 TOG_COLOR_REFS_REMOTES,
7197 get_color_value("TOG_COLOR_REFS_REMOTES"));
7198 if (err)
7199 goto done;
7201 err = add_color(&s->colors, "^refs/got/backup/",
7202 TOG_COLOR_REFS_BACKUP,
7203 get_color_value("TOG_COLOR_REFS_BACKUP"));
7204 if (err)
7205 goto done;
7208 view->show = show_ref_view;
7209 view->input = input_ref_view;
7210 view->close = close_ref_view;
7211 view->search_start = search_start_ref_view;
7212 view->search_next = search_next_ref_view;
7213 done:
7214 if (err)
7215 free_colors(&s->colors);
7216 return err;
7219 static const struct got_error *
7220 close_ref_view(struct tog_view *view)
7222 struct tog_ref_view_state *s = &view->state.ref;
7224 ref_view_free_refs(s);
7225 free_colors(&s->colors);
7227 return NULL;
7230 static const struct got_error *
7231 resolve_reflist_entry(struct got_object_id **commit_id,
7232 struct tog_reflist_entry *re, struct got_repository *repo)
7234 const struct got_error *err = NULL;
7235 struct got_object_id *obj_id;
7236 struct got_tag_object *tag = NULL;
7237 int obj_type;
7239 *commit_id = NULL;
7241 err = got_ref_resolve(&obj_id, repo, re->ref);
7242 if (err)
7243 return err;
7245 err = got_object_get_type(&obj_type, repo, obj_id);
7246 if (err)
7247 goto done;
7249 switch (obj_type) {
7250 case GOT_OBJ_TYPE_COMMIT:
7251 *commit_id = obj_id;
7252 break;
7253 case GOT_OBJ_TYPE_TAG:
7254 err = got_object_open_as_tag(&tag, repo, obj_id);
7255 if (err)
7256 goto done;
7257 free(obj_id);
7258 err = got_object_get_type(&obj_type, repo,
7259 got_object_tag_get_object_id(tag));
7260 if (err)
7261 goto done;
7262 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7263 err = got_error(GOT_ERR_OBJ_TYPE);
7264 goto done;
7266 *commit_id = got_object_id_dup(
7267 got_object_tag_get_object_id(tag));
7268 if (*commit_id == NULL) {
7269 err = got_error_from_errno("got_object_id_dup");
7270 goto done;
7272 break;
7273 default:
7274 err = got_error(GOT_ERR_OBJ_TYPE);
7275 break;
7278 done:
7279 if (tag)
7280 got_object_tag_close(tag);
7281 if (err) {
7282 free(*commit_id);
7283 *commit_id = NULL;
7285 return err;
7288 static const struct got_error *
7289 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7290 struct tog_reflist_entry *re, struct got_repository *repo)
7292 struct tog_view *log_view;
7293 const struct got_error *err = NULL;
7294 struct got_object_id *commit_id = NULL;
7296 *new_view = NULL;
7298 err = resolve_reflist_entry(&commit_id, re, repo);
7299 if (err) {
7300 if (err->code != GOT_ERR_OBJ_TYPE)
7301 return err;
7302 else
7303 return NULL;
7306 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7307 if (log_view == NULL) {
7308 err = got_error_from_errno("view_open");
7309 goto done;
7312 err = open_log_view(log_view, commit_id, repo,
7313 got_ref_get_name(re->ref), "", 0);
7314 done:
7315 if (err)
7316 view_close(log_view);
7317 else
7318 *new_view = log_view;
7319 free(commit_id);
7320 return err;
7323 static void
7324 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7326 struct tog_reflist_entry *re;
7327 int i = 0;
7329 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7330 return;
7332 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7333 while (i++ < maxscroll) {
7334 if (re == NULL)
7335 break;
7336 s->first_displayed_entry = re;
7337 re = TAILQ_PREV(re, tog_reflist_head, entry);
7341 static const struct got_error *
7342 ref_scroll_down(struct tog_view *view, int maxscroll)
7344 struct tog_ref_view_state *s = &view->state.ref;
7345 struct tog_reflist_entry *next, *last;
7346 int n = 0;
7348 if (s->first_displayed_entry)
7349 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7350 else
7351 next = TAILQ_FIRST(&s->refs);
7353 last = s->last_displayed_entry;
7354 while (next && n++ < maxscroll) {
7355 if (last)
7356 last = TAILQ_NEXT(last, entry);
7357 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7358 s->first_displayed_entry = next;
7359 next = TAILQ_NEXT(next, entry);
7363 return NULL;
7366 static const struct got_error *
7367 search_start_ref_view(struct tog_view *view)
7369 struct tog_ref_view_state *s = &view->state.ref;
7371 s->matched_entry = NULL;
7372 return NULL;
7375 static int
7376 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7378 regmatch_t regmatch;
7380 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7381 0) == 0;
7384 static const struct got_error *
7385 search_next_ref_view(struct tog_view *view)
7387 struct tog_ref_view_state *s = &view->state.ref;
7388 struct tog_reflist_entry *re = NULL;
7390 if (!view->searching) {
7391 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7392 return NULL;
7395 if (s->matched_entry) {
7396 if (view->searching == TOG_SEARCH_FORWARD) {
7397 if (s->selected_entry)
7398 re = TAILQ_NEXT(s->selected_entry, entry);
7399 else
7400 re = TAILQ_PREV(s->selected_entry,
7401 tog_reflist_head, entry);
7402 } else {
7403 if (s->selected_entry == NULL)
7404 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7405 else
7406 re = TAILQ_PREV(s->selected_entry,
7407 tog_reflist_head, entry);
7409 } else {
7410 if (s->selected_entry)
7411 re = s->selected_entry;
7412 else if (view->searching == TOG_SEARCH_FORWARD)
7413 re = TAILQ_FIRST(&s->refs);
7414 else
7415 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7418 while (1) {
7419 if (re == NULL) {
7420 if (s->matched_entry == NULL) {
7421 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7422 return NULL;
7424 if (view->searching == TOG_SEARCH_FORWARD)
7425 re = TAILQ_FIRST(&s->refs);
7426 else
7427 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7430 if (match_reflist_entry(re, &view->regex)) {
7431 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7432 s->matched_entry = re;
7433 break;
7436 if (view->searching == TOG_SEARCH_FORWARD)
7437 re = TAILQ_NEXT(re, entry);
7438 else
7439 re = TAILQ_PREV(re, tog_reflist_head, entry);
7442 if (s->matched_entry) {
7443 s->first_displayed_entry = s->matched_entry;
7444 s->selected = 0;
7447 return NULL;
7450 static const struct got_error *
7451 show_ref_view(struct tog_view *view)
7453 const struct got_error *err = NULL;
7454 struct tog_ref_view_state *s = &view->state.ref;
7455 struct tog_reflist_entry *re;
7456 char *line = NULL;
7457 wchar_t *wline;
7458 struct tog_color *tc;
7459 int width, n;
7460 int limit = view->nlines;
7462 werase(view->window);
7464 s->ndisplayed = 0;
7465 if (view_is_hsplit_top(view))
7466 --limit; /* border */
7468 if (limit == 0)
7469 return NULL;
7471 re = s->first_displayed_entry;
7473 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7474 s->nrefs) == -1)
7475 return got_error_from_errno("asprintf");
7477 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7478 if (err) {
7479 free(line);
7480 return err;
7482 if (view_needs_focus_indication(view))
7483 wstandout(view->window);
7484 waddwstr(view->window, wline);
7485 if (view_needs_focus_indication(view))
7486 wstandend(view->window);
7487 free(wline);
7488 wline = NULL;
7489 free(line);
7490 line = NULL;
7491 if (width < view->ncols - 1)
7492 waddch(view->window, '\n');
7493 if (--limit <= 0)
7494 return NULL;
7496 n = 0;
7497 while (re && limit > 0) {
7498 char *line = NULL;
7499 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7501 if (s->show_date) {
7502 struct got_commit_object *ci;
7503 struct got_tag_object *tag;
7504 struct got_object_id *id;
7505 struct tm tm;
7506 time_t t;
7508 err = got_ref_resolve(&id, s->repo, re->ref);
7509 if (err)
7510 return err;
7511 err = got_object_open_as_tag(&tag, s->repo, id);
7512 if (err) {
7513 if (err->code != GOT_ERR_OBJ_TYPE) {
7514 free(id);
7515 return err;
7517 err = got_object_open_as_commit(&ci, s->repo,
7518 id);
7519 if (err) {
7520 free(id);
7521 return err;
7523 t = got_object_commit_get_committer_time(ci);
7524 got_object_commit_close(ci);
7525 } else {
7526 t = got_object_tag_get_tagger_time(tag);
7527 got_object_tag_close(tag);
7529 free(id);
7530 if (gmtime_r(&t, &tm) == NULL)
7531 return got_error_from_errno("gmtime_r");
7532 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7533 return got_error(GOT_ERR_NO_SPACE);
7535 if (got_ref_is_symbolic(re->ref)) {
7536 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7537 ymd : "", got_ref_get_name(re->ref),
7538 got_ref_get_symref_target(re->ref)) == -1)
7539 return got_error_from_errno("asprintf");
7540 } else if (s->show_ids) {
7541 struct got_object_id *id;
7542 char *id_str;
7543 err = got_ref_resolve(&id, s->repo, re->ref);
7544 if (err)
7545 return err;
7546 err = got_object_id_str(&id_str, id);
7547 if (err) {
7548 free(id);
7549 return err;
7551 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7552 got_ref_get_name(re->ref), id_str) == -1) {
7553 err = got_error_from_errno("asprintf");
7554 free(id);
7555 free(id_str);
7556 return err;
7558 free(id);
7559 free(id_str);
7560 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7561 got_ref_get_name(re->ref)) == -1)
7562 return got_error_from_errno("asprintf");
7564 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7565 0, 0);
7566 if (err) {
7567 free(line);
7568 return err;
7570 if (n == s->selected) {
7571 if (view->focussed)
7572 wstandout(view->window);
7573 s->selected_entry = re;
7575 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7576 if (tc)
7577 wattr_on(view->window,
7578 COLOR_PAIR(tc->colorpair), NULL);
7579 waddwstr(view->window, wline);
7580 if (tc)
7581 wattr_off(view->window,
7582 COLOR_PAIR(tc->colorpair), NULL);
7583 if (width < view->ncols - 1)
7584 waddch(view->window, '\n');
7585 if (n == s->selected && view->focussed)
7586 wstandend(view->window);
7587 free(line);
7588 free(wline);
7589 wline = NULL;
7590 n++;
7591 s->ndisplayed++;
7592 s->last_displayed_entry = re;
7594 limit--;
7595 re = TAILQ_NEXT(re, entry);
7598 view_border(view);
7599 return err;
7602 static const struct got_error *
7603 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7604 struct tog_reflist_entry *re, struct got_repository *repo)
7606 const struct got_error *err = NULL;
7607 struct got_object_id *commit_id = NULL;
7608 struct tog_view *tree_view;
7610 *new_view = NULL;
7612 err = resolve_reflist_entry(&commit_id, re, repo);
7613 if (err) {
7614 if (err->code != GOT_ERR_OBJ_TYPE)
7615 return err;
7616 else
7617 return NULL;
7621 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7622 if (tree_view == NULL) {
7623 err = got_error_from_errno("view_open");
7624 goto done;
7627 err = open_tree_view(tree_view, commit_id,
7628 got_ref_get_name(re->ref), repo);
7629 if (err)
7630 goto done;
7632 *new_view = tree_view;
7633 done:
7634 free(commit_id);
7635 return err;
7637 static const struct got_error *
7638 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7640 const struct got_error *err = NULL;
7641 struct tog_ref_view_state *s = &view->state.ref;
7642 struct tog_view *log_view, *tree_view;
7643 struct tog_reflist_entry *re;
7644 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7646 switch (ch) {
7647 case 'i':
7648 s->show_ids = !s->show_ids;
7649 view->count = 0;
7650 break;
7651 case 'm':
7652 s->show_date = !s->show_date;
7653 view->count = 0;
7654 break;
7655 case 'o':
7656 s->sort_by_date = !s->sort_by_date;
7657 view->count = 0;
7658 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7659 got_ref_cmp_by_commit_timestamp_descending :
7660 tog_ref_cmp_by_name, s->repo);
7661 if (err)
7662 break;
7663 got_reflist_object_id_map_free(tog_refs_idmap);
7664 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7665 &tog_refs, s->repo);
7666 if (err)
7667 break;
7668 ref_view_free_refs(s);
7669 err = ref_view_load_refs(s);
7670 break;
7671 case KEY_ENTER:
7672 case '\r':
7673 view->count = 0;
7674 if (!s->selected_entry)
7675 break;
7676 if (view_is_parent_view(view))
7677 view_get_split(view, &begin_y, &begin_x);
7679 err = log_ref_entry(&log_view, begin_y, begin_x,
7680 s->selected_entry, s->repo);
7681 if (err)
7682 break;
7684 if (view_is_parent_view(view) &&
7685 view->mode == TOG_VIEW_SPLIT_HRZN) {
7686 err = view_init_hsplit(view, begin_y);
7687 if (err)
7688 break;
7691 view->focussed = 0;
7692 log_view->focussed = 1;
7693 log_view->mode = view->mode;
7694 log_view->nlines = view->lines - begin_y;
7695 if (view_is_parent_view(view)) {
7696 view_transfer_size(log_view, view);
7697 err = view_close_child(view);
7698 if (err)
7699 return err;
7700 err = view_set_child(view, log_view);
7701 if (err)
7702 return err;
7703 view->focus_child = 1;
7704 } else
7705 *new_view = log_view;
7706 break;
7707 case 't':
7708 view->count = 0;
7709 if (!s->selected_entry)
7710 break;
7711 if (view_is_parent_view(view))
7712 view_get_split(view, &begin_y, &begin_x);
7713 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7714 s->selected_entry, s->repo);
7715 if (err || tree_view == NULL)
7716 break;
7717 if (view_is_parent_view(view) &&
7718 view->mode == TOG_VIEW_SPLIT_HRZN) {
7719 err = view_init_hsplit(view, begin_y);
7720 if (err)
7721 break;
7723 view->focussed = 0;
7724 tree_view->focussed = 1;
7725 tree_view->mode = view->mode;
7726 tree_view->nlines = view->lines - begin_y;
7727 if (view_is_parent_view(view)) {
7728 view_transfer_size(tree_view, view);
7729 err = view_close_child(view);
7730 if (err)
7731 return err;
7732 err = view_set_child(view, tree_view);
7733 if (err)
7734 return err;
7735 view->focus_child = 1;
7736 } else
7737 *new_view = tree_view;
7738 break;
7739 case 'g':
7740 case KEY_HOME:
7741 s->selected = 0;
7742 view->count = 0;
7743 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7744 break;
7745 case 'G':
7746 case KEY_END: {
7747 int eos = view->nlines - 1;
7749 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7750 --eos; /* border */
7751 s->selected = 0;
7752 view->count = 0;
7753 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7754 for (n = 0; n < eos; n++) {
7755 if (re == NULL)
7756 break;
7757 s->first_displayed_entry = re;
7758 re = TAILQ_PREV(re, tog_reflist_head, entry);
7760 if (n > 0)
7761 s->selected = n - 1;
7762 break;
7764 case 'k':
7765 case KEY_UP:
7766 case CTRL('p'):
7767 if (s->selected > 0) {
7768 s->selected--;
7769 break;
7771 ref_scroll_up(s, 1);
7772 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7773 view->count = 0;
7774 break;
7775 case CTRL('u'):
7776 case 'u':
7777 nscroll /= 2;
7778 /* FALL THROUGH */
7779 case KEY_PPAGE:
7780 case CTRL('b'):
7781 case 'b':
7782 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7783 s->selected -= MIN(nscroll, s->selected);
7784 ref_scroll_up(s, MAX(0, nscroll));
7785 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7786 view->count = 0;
7787 break;
7788 case 'j':
7789 case KEY_DOWN:
7790 case CTRL('n'):
7791 if (s->selected < s->ndisplayed - 1) {
7792 s->selected++;
7793 break;
7795 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7796 /* can't scroll any further */
7797 view->count = 0;
7798 break;
7800 ref_scroll_down(view, 1);
7801 break;
7802 case CTRL('d'):
7803 case 'd':
7804 nscroll /= 2;
7805 /* FALL THROUGH */
7806 case KEY_NPAGE:
7807 case CTRL('f'):
7808 case 'f':
7809 case ' ':
7810 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7811 /* can't scroll any further; move cursor down */
7812 if (s->selected < s->ndisplayed - 1)
7813 s->selected += MIN(nscroll,
7814 s->ndisplayed - s->selected - 1);
7815 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7816 s->selected += s->ndisplayed - s->selected - 1;
7817 view->count = 0;
7818 break;
7820 ref_scroll_down(view, nscroll);
7821 break;
7822 case CTRL('l'):
7823 view->count = 0;
7824 tog_free_refs();
7825 err = tog_load_refs(s->repo, s->sort_by_date);
7826 if (err)
7827 break;
7828 ref_view_free_refs(s);
7829 err = ref_view_load_refs(s);
7830 break;
7831 case KEY_RESIZE:
7832 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7833 s->selected = view->nlines - 2;
7834 break;
7835 default:
7836 view->count = 0;
7837 break;
7840 return err;
7843 __dead static void
7844 usage_ref(void)
7846 endwin();
7847 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7848 getprogname());
7849 exit(1);
7852 static const struct got_error *
7853 cmd_ref(int argc, char *argv[])
7855 const struct got_error *error;
7856 struct got_repository *repo = NULL;
7857 struct got_worktree *worktree = NULL;
7858 char *cwd = NULL, *repo_path = NULL;
7859 int ch;
7860 struct tog_view *view;
7861 int *pack_fds = NULL;
7863 while ((ch = getopt(argc, argv, "r:")) != -1) {
7864 switch (ch) {
7865 case 'r':
7866 repo_path = realpath(optarg, NULL);
7867 if (repo_path == NULL)
7868 return got_error_from_errno2("realpath",
7869 optarg);
7870 break;
7871 default:
7872 usage_ref();
7873 /* NOTREACHED */
7877 argc -= optind;
7878 argv += optind;
7880 if (argc > 1)
7881 usage_ref();
7883 error = got_repo_pack_fds_open(&pack_fds);
7884 if (error != NULL)
7885 goto done;
7887 if (repo_path == NULL) {
7888 cwd = getcwd(NULL, 0);
7889 if (cwd == NULL)
7890 return got_error_from_errno("getcwd");
7891 error = got_worktree_open(&worktree, cwd);
7892 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7893 goto done;
7894 if (worktree)
7895 repo_path =
7896 strdup(got_worktree_get_repo_path(worktree));
7897 else
7898 repo_path = strdup(cwd);
7899 if (repo_path == NULL) {
7900 error = got_error_from_errno("strdup");
7901 goto done;
7905 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7906 if (error != NULL)
7907 goto done;
7909 init_curses();
7911 error = apply_unveil(got_repo_get_path(repo), NULL);
7912 if (error)
7913 goto done;
7915 error = tog_load_refs(repo, 0);
7916 if (error)
7917 goto done;
7919 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7920 if (view == NULL) {
7921 error = got_error_from_errno("view_open");
7922 goto done;
7925 error = open_ref_view(view, repo);
7926 if (error)
7927 goto done;
7929 if (worktree) {
7930 /* Release work tree lock. */
7931 got_worktree_close(worktree);
7932 worktree = NULL;
7934 error = view_loop(view);
7935 done:
7936 free(repo_path);
7937 free(cwd);
7938 if (repo) {
7939 const struct got_error *close_err = got_repo_close(repo);
7940 if (close_err)
7941 error = close_err;
7943 if (pack_fds) {
7944 const struct got_error *pack_err =
7945 got_repo_pack_fds_close(pack_fds);
7946 if (error == NULL)
7947 error = pack_err;
7949 tog_free_refs();
7950 return error;
7954 * If view was scrolled down to move the selected line into view when opening a
7955 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7957 static void
7958 offset_selection_up(struct tog_view *view)
7960 switch (view->type) {
7961 case TOG_VIEW_BLAME: {
7962 struct tog_blame_view_state *s = &view->state.blame;
7963 if (s->first_displayed_line == 1) {
7964 s->selected_line = MAX(s->selected_line - view->offset,
7965 1);
7966 break;
7968 if (s->first_displayed_line > view->offset)
7969 s->first_displayed_line -= view->offset;
7970 else
7971 s->first_displayed_line = 1;
7972 s->selected_line += view->offset;
7973 break;
7975 case TOG_VIEW_LOG:
7976 log_scroll_up(&view->state.log, view->offset);
7977 view->state.log.selected += view->offset;
7978 break;
7979 case TOG_VIEW_REF:
7980 ref_scroll_up(&view->state.ref, view->offset);
7981 view->state.ref.selected += view->offset;
7982 break;
7983 case TOG_VIEW_TREE:
7984 tree_scroll_up(&view->state.tree, view->offset);
7985 view->state.tree.selected += view->offset;
7986 break;
7987 default:
7988 break;
7991 view->offset = 0;
7995 * If the selected line is in the section of screen covered by the bottom split,
7996 * scroll down offset lines to move it into view and index its new position.
7998 static const struct got_error *
7999 offset_selection_down(struct tog_view *view)
8001 const struct got_error *err = NULL;
8002 const struct got_error *(*scrolld)(struct tog_view *, int);
8003 int *selected = NULL;
8004 int header, offset;
8006 switch (view->type) {
8007 case TOG_VIEW_BLAME: {
8008 struct tog_blame_view_state *s = &view->state.blame;
8009 header = 3;
8010 scrolld = NULL;
8011 if (s->selected_line > view->nlines - header) {
8012 offset = abs(view->nlines - s->selected_line - header);
8013 s->first_displayed_line += offset;
8014 s->selected_line -= offset;
8015 view->offset = offset;
8017 break;
8019 case TOG_VIEW_LOG: {
8020 struct tog_log_view_state *s = &view->state.log;
8021 scrolld = &log_scroll_down;
8022 header = view_is_parent_view(view) ? 3 : 2;
8023 selected = &s->selected;
8024 break;
8026 case TOG_VIEW_REF: {
8027 struct tog_ref_view_state *s = &view->state.ref;
8028 scrolld = &ref_scroll_down;
8029 header = 3;
8030 selected = &s->selected;
8031 break;
8033 case TOG_VIEW_TREE: {
8034 struct tog_tree_view_state *s = &view->state.tree;
8035 scrolld = &tree_scroll_down;
8036 header = 5;
8037 selected = &s->selected;
8038 break;
8040 default:
8041 selected = NULL;
8042 scrolld = NULL;
8043 header = 0;
8044 break;
8047 if (selected && *selected > view->nlines - header) {
8048 offset = abs(view->nlines - *selected - header);
8049 view->offset = offset;
8050 if (scrolld && offset) {
8051 err = scrolld(view, offset);
8052 *selected -= offset;
8056 return err;
8059 static void
8060 list_commands(FILE *fp)
8062 size_t i;
8064 fprintf(fp, "commands:");
8065 for (i = 0; i < nitems(tog_commands); i++) {
8066 const struct tog_cmd *cmd = &tog_commands[i];
8067 fprintf(fp, " %s", cmd->name);
8069 fputc('\n', fp);
8072 __dead static void
8073 usage(int hflag, int status)
8075 FILE *fp = (status == 0) ? stdout : stderr;
8077 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8078 getprogname());
8079 if (hflag) {
8080 fprintf(fp, "lazy usage: %s path\n", getprogname());
8081 list_commands(fp);
8083 exit(status);
8086 static char **
8087 make_argv(int argc, ...)
8089 va_list ap;
8090 char **argv;
8091 int i;
8093 va_start(ap, argc);
8095 argv = calloc(argc, sizeof(char *));
8096 if (argv == NULL)
8097 err(1, "calloc");
8098 for (i = 0; i < argc; i++) {
8099 argv[i] = strdup(va_arg(ap, char *));
8100 if (argv[i] == NULL)
8101 err(1, "strdup");
8104 va_end(ap);
8105 return argv;
8109 * Try to convert 'tog path' into a 'tog log path' command.
8110 * The user could simply have mistyped the command rather than knowingly
8111 * provided a path. So check whether argv[0] can in fact be resolved
8112 * to a path in the HEAD commit and print a special error if not.
8113 * This hack is for mpi@ <3
8115 static const struct got_error *
8116 tog_log_with_path(int argc, char *argv[])
8118 const struct got_error *error = NULL, *close_err;
8119 const struct tog_cmd *cmd = NULL;
8120 struct got_repository *repo = NULL;
8121 struct got_worktree *worktree = NULL;
8122 struct got_object_id *commit_id = NULL, *id = NULL;
8123 struct got_commit_object *commit = NULL;
8124 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8125 char *commit_id_str = NULL, **cmd_argv = NULL;
8126 int *pack_fds = NULL;
8128 cwd = getcwd(NULL, 0);
8129 if (cwd == NULL)
8130 return got_error_from_errno("getcwd");
8132 error = got_repo_pack_fds_open(&pack_fds);
8133 if (error != NULL)
8134 goto done;
8136 error = got_worktree_open(&worktree, cwd);
8137 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8138 goto done;
8140 if (worktree)
8141 repo_path = strdup(got_worktree_get_repo_path(worktree));
8142 else
8143 repo_path = strdup(cwd);
8144 if (repo_path == NULL) {
8145 error = got_error_from_errno("strdup");
8146 goto done;
8149 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8150 if (error != NULL)
8151 goto done;
8153 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8154 repo, worktree);
8155 if (error)
8156 goto done;
8158 error = tog_load_refs(repo, 0);
8159 if (error)
8160 goto done;
8161 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8162 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8163 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8164 if (error)
8165 goto done;
8167 if (worktree) {
8168 got_worktree_close(worktree);
8169 worktree = NULL;
8172 error = got_object_open_as_commit(&commit, repo, commit_id);
8173 if (error)
8174 goto done;
8176 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8177 if (error) {
8178 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8179 goto done;
8180 fprintf(stderr, "%s: '%s' is no known command or path\n",
8181 getprogname(), argv[0]);
8182 usage(1, 1);
8183 /* not reached */
8186 close_err = got_repo_close(repo);
8187 if (error == NULL)
8188 error = close_err;
8189 repo = NULL;
8191 error = got_object_id_str(&commit_id_str, commit_id);
8192 if (error)
8193 goto done;
8195 cmd = &tog_commands[0]; /* log */
8196 argc = 4;
8197 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8198 error = cmd->cmd_main(argc, cmd_argv);
8199 done:
8200 if (repo) {
8201 close_err = got_repo_close(repo);
8202 if (error == NULL)
8203 error = close_err;
8205 if (commit)
8206 got_object_commit_close(commit);
8207 if (worktree)
8208 got_worktree_close(worktree);
8209 if (pack_fds) {
8210 const struct got_error *pack_err =
8211 got_repo_pack_fds_close(pack_fds);
8212 if (error == NULL)
8213 error = pack_err;
8215 free(id);
8216 free(commit_id_str);
8217 free(commit_id);
8218 free(cwd);
8219 free(repo_path);
8220 free(in_repo_path);
8221 if (cmd_argv) {
8222 int i;
8223 for (i = 0; i < argc; i++)
8224 free(cmd_argv[i]);
8225 free(cmd_argv);
8227 tog_free_refs();
8228 return error;
8231 int
8232 main(int argc, char *argv[])
8234 const struct got_error *error = NULL;
8235 const struct tog_cmd *cmd = NULL;
8236 int ch, hflag = 0, Vflag = 0;
8237 char **cmd_argv = NULL;
8238 static const struct option longopts[] = {
8239 { "version", no_argument, NULL, 'V' },
8240 { NULL, 0, NULL, 0}
8242 char *diff_algo_str = NULL;
8244 setlocale(LC_CTYPE, "");
8246 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8247 switch (ch) {
8248 case 'h':
8249 hflag = 1;
8250 break;
8251 case 'V':
8252 Vflag = 1;
8253 break;
8254 default:
8255 usage(hflag, 1);
8256 /* NOTREACHED */
8260 argc -= optind;
8261 argv += optind;
8262 optind = 1;
8263 optreset = 1;
8265 if (Vflag) {
8266 got_version_print_str();
8267 return 0;
8270 #ifndef PROFILE
8271 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8272 NULL) == -1)
8273 err(1, "pledge");
8274 #endif
8276 if (argc == 0) {
8277 if (hflag)
8278 usage(hflag, 0);
8279 /* Build an argument vector which runs a default command. */
8280 cmd = &tog_commands[0];
8281 argc = 1;
8282 cmd_argv = make_argv(argc, cmd->name);
8283 } else {
8284 size_t i;
8286 /* Did the user specify a command? */
8287 for (i = 0; i < nitems(tog_commands); i++) {
8288 if (strncmp(tog_commands[i].name, argv[0],
8289 strlen(argv[0])) == 0) {
8290 cmd = &tog_commands[i];
8291 break;
8296 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8297 if (diff_algo_str) {
8298 if (strcasecmp(diff_algo_str, "patience") == 0)
8299 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8300 if (strcasecmp(diff_algo_str, "myers") == 0)
8301 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8304 if (cmd == NULL) {
8305 if (argc != 1)
8306 usage(0, 1);
8307 /* No command specified; try log with a path */
8308 error = tog_log_with_path(argc, argv);
8309 } else {
8310 if (hflag)
8311 cmd->cmd_usage();
8312 else
8313 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8316 endwin();
8317 putchar('\n');
8318 if (cmd_argv) {
8319 int i;
8320 for (i = 0; i < argc; i++)
8321 free(cmd_argv[i]);
8322 free(cmd_argv);
8325 if (error && error->code != GOT_ERR_CANCELLED)
8326 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8327 return 0;