Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 #endif
26 #include <curses.h>
27 #include <panel.h>
28 #include <locale.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #include <string.h>
35 #include <err.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
43 #include <sched.h>
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
52 #include "got_diff.h"
53 #include "got_opentemp.h"
54 #include "got_utf8.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_path.h"
60 #include "got_worktree.h"
62 #ifndef MIN
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #endif
66 #ifndef MAX
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #endif
70 #ifndef CTRL
71 #define CTRL(x) ((x) & 0x1f)
72 #endif
74 #ifndef nitems
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 #endif
78 struct tog_cmd {
79 const char *name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 };
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
103 };
105 enum tog_view_type {
106 TOG_VIEW_DIFF,
107 TOG_VIEW_LOG,
108 TOG_VIEW_BLAME,
109 TOG_VIEW_TREE,
110 TOG_VIEW_REF,
111 };
113 enum tog_view_mode {
114 TOG_VIEW_SPLIT_NONE,
115 TOG_VIEW_SPLIT_VERT,
116 TOG_VIEW_SPLIT_HRZN
117 };
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
127 int idx;
128 };
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
131 int ncommits;
132 struct commit_queue_head head;
133 };
135 struct tog_color {
136 STAILQ_ENTRY(tog_color) entry;
137 regex_t regex;
138 short colorpair;
139 };
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
158 *cmp = -1;
159 return NULL;
160 } else if (isbackup1 && !isbackup2) {
161 *cmp = 1;
162 return NULL;
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 return NULL;
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 repo);
177 if (err)
178 return err;
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 repo);
184 static void
185 tog_free_refs(void)
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
200 int regerr = 0;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
203 return NULL;
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
208 if (tc == NULL)
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 if (regerr) {
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
216 sizeof(regerr_msg));
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 regerr_msg);
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 free(tc);
221 return err;
223 tc->colorpair = idx;
224 STAILQ_INSERT_HEAD(colors, tc, entry);
225 return NULL;
228 static void
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
236 regfree(&tc->regex);
237 free(tc);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
248 return tc;
251 return NULL;
254 static int
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 return COLOR_CYAN;
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 return COLOR_YELLOW;
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 return COLOR_GREEN;
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 return COLOR_CYAN;
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 return COLOR_GREEN;
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 return COLOR_GREEN;
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 return COLOR_CYAN;
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 return COLOR_YELLOW;
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 return COLOR_GREEN;
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 return COLOR_YELLOW;
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 return COLOR_CYAN;
288 return -1;
291 static int
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
296 if (val == NULL)
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
300 return COLOR_BLACK;
301 if (strcasecmp(val, "red") == 0)
302 return COLOR_RED;
303 if (strcasecmp(val, "green") == 0)
304 return COLOR_GREEN;
305 if (strcasecmp(val, "yellow") == 0)
306 return COLOR_YELLOW;
307 if (strcasecmp(val, "blue") == 0)
308 return COLOR_BLUE;
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
312 return COLOR_CYAN;
313 if (strcasecmp(val, "white") == 0)
314 return COLOR_WHITE;
315 if (strcasecmp(val, "default") == 0)
316 return -1;
318 return default_color_value(envvar);
322 struct tog_diff_view_state {
323 struct got_object_id *id1, *id2;
324 const char *label1, *label2;
325 FILE *f, *f1, *f2;
326 int fd1, fd2;
327 int first_displayed_line;
328 int last_displayed_line;
329 int eof;
330 int diff_context;
331 int ignore_whitespace;
332 int force_text_diff;
333 struct got_repository *repo;
334 struct tog_colors colors;
335 size_t nlines;
336 off_t *line_offsets;
337 int matched_line;
338 int selected_line;
340 /* passed from log or blame view; may be NULL */
341 struct tog_view *parent_view;
342 };
344 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 static volatile sig_atomic_t tog_thread_error;
347 struct tog_log_thread_args {
348 pthread_cond_t need_commits;
349 pthread_cond_t commit_loaded;
350 int commits_needed;
351 int load_all;
352 struct got_commit_graph *graph;
353 struct commit_queue *commits;
354 const char *in_repo_path;
355 struct got_object_id *start_id;
356 struct got_repository *repo;
357 int *pack_fds;
358 int log_complete;
359 sig_atomic_t *quit;
360 struct commit_queue_entry **first_displayed_entry;
361 struct commit_queue_entry **selected_entry;
362 int *searching;
363 int *search_next_done;
364 regex_t *regex;
365 };
367 struct tog_log_view_state {
368 struct commit_queue commits;
369 struct commit_queue_entry *first_displayed_entry;
370 struct commit_queue_entry *last_displayed_entry;
371 struct commit_queue_entry *selected_entry;
372 int selected;
373 char *in_repo_path;
374 char *head_ref_name;
375 int log_branches;
376 struct got_repository *repo;
377 struct got_object_id *start_id;
378 sig_atomic_t quit;
379 pthread_t thread;
380 struct tog_log_thread_args thread_args;
381 struct commit_queue_entry *matched_entry;
382 struct commit_queue_entry *search_entry;
383 struct tog_colors colors;
384 int use_committer;
385 };
387 #define TOG_COLOR_DIFF_MINUS 1
388 #define TOG_COLOR_DIFF_PLUS 2
389 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 #define TOG_COLOR_DIFF_META 4
391 #define TOG_COLOR_TREE_SUBMODULE 5
392 #define TOG_COLOR_TREE_SYMLINK 6
393 #define TOG_COLOR_TREE_DIRECTORY 7
394 #define TOG_COLOR_TREE_EXECUTABLE 8
395 #define TOG_COLOR_COMMIT 9
396 #define TOG_COLOR_AUTHOR 10
397 #define TOG_COLOR_DATE 11
398 #define TOG_COLOR_REFS_HEADS 12
399 #define TOG_COLOR_REFS_TAGS 13
400 #define TOG_COLOR_REFS_REMOTES 14
401 #define TOG_COLOR_REFS_BACKUP 15
403 struct tog_blame_cb_args {
404 struct tog_blame_line *lines; /* one per line */
405 int nlines;
407 struct tog_view *view;
408 struct got_object_id *commit_id;
409 int *quit;
410 };
412 struct tog_blame_thread_args {
413 const char *path;
414 struct got_repository *repo;
415 struct tog_blame_cb_args *cb_args;
416 int *complete;
417 got_cancel_cb cancel_cb;
418 void *cancel_arg;
419 };
421 struct tog_blame {
422 FILE *f;
423 off_t filesize;
424 struct tog_blame_line *lines;
425 int nlines;
426 off_t *line_offsets;
427 pthread_t thread;
428 struct tog_blame_thread_args thread_args;
429 struct tog_blame_cb_args cb_args;
430 const char *path;
431 int *pack_fds;
432 };
434 struct tog_blame_view_state {
435 int first_displayed_line;
436 int last_displayed_line;
437 int selected_line;
438 int last_diffed_line;
439 int blame_complete;
440 int eof;
441 int done;
442 struct got_object_id_queue blamed_commits;
443 struct got_object_qid *blamed_commit;
444 char *path;
445 struct got_repository *repo;
446 struct got_object_id *commit_id;
447 struct tog_blame blame;
448 int matched_line;
449 struct tog_colors colors;
450 };
452 struct tog_parent_tree {
453 TAILQ_ENTRY(tog_parent_tree) entry;
454 struct got_tree_object *tree;
455 struct got_tree_entry *first_displayed_entry;
456 struct got_tree_entry *selected_entry;
457 int selected;
458 };
460 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
462 struct tog_tree_view_state {
463 char *tree_label;
464 struct got_object_id *commit_id;/* commit which this tree belongs to */
465 struct got_tree_object *root; /* the commit's root tree entry */
466 struct got_tree_object *tree; /* currently displayed (sub-)tree */
467 struct got_tree_entry *first_displayed_entry;
468 struct got_tree_entry *last_displayed_entry;
469 struct got_tree_entry *selected_entry;
470 int ndisplayed, selected, show_ids;
471 struct tog_parent_trees parents; /* parent trees of current sub-tree */
472 char *head_ref_name;
473 struct got_repository *repo;
474 struct got_tree_entry *matched_entry;
475 struct tog_colors colors;
476 };
478 struct tog_reflist_entry {
479 TAILQ_ENTRY(tog_reflist_entry) entry;
480 struct got_reference *ref;
481 int idx;
482 };
484 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
486 struct tog_ref_view_state {
487 struct tog_reflist_head refs;
488 struct tog_reflist_entry *first_displayed_entry;
489 struct tog_reflist_entry *last_displayed_entry;
490 struct tog_reflist_entry *selected_entry;
491 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
492 struct got_repository *repo;
493 struct tog_reflist_entry *matched_entry;
494 struct tog_colors colors;
495 };
497 /*
498 * We implement two types of views: parent views and child views.
500 * The 'Tab' key switches focus between a parent view and its child view.
501 * Child views are shown side-by-side to their parent view, provided
502 * there is enough screen estate.
504 * When a new view is opened from within a parent view, this new view
505 * becomes a child view of the parent view, replacing any existing child.
507 * When a new view is opened from within a child view, this new view
508 * becomes a parent view which will obscure the views below until the
509 * user quits the new parent view by typing 'q'.
511 * This list of views contains parent views only.
512 * Child views are only pointed to by their parent view.
513 */
514 TAILQ_HEAD(tog_view_list_head, tog_view);
516 struct tog_view {
517 TAILQ_ENTRY(tog_view) entry;
518 WINDOW *window;
519 PANEL *panel;
520 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
521 int resized_y, resized_x; /* begin_y/x based on user resizing */
522 int maxx, x; /* max column and current start column */
523 int lines, cols; /* copies of LINES and COLS */
524 int nscrolled, offset; /* lines scrolled and hsplit line offset */
525 int ch, count; /* current keymap and count prefix */
526 int resized; /* set when in a resize event */
527 int focussed; /* Only set on one parent or child view at a time. */
528 int dying;
529 struct tog_view *parent;
530 struct tog_view *child;
532 /*
533 * This flag is initially set on parent views when a new child view
534 * is created. It gets toggled when the 'Tab' key switches focus
535 * between parent and child.
536 * The flag indicates whether focus should be passed on to our child
537 * view if this parent view gets picked for focus after another parent
538 * view was closed. This prevents child views from losing focus in such
539 * situations.
540 */
541 int focus_child;
543 enum tog_view_mode mode;
544 /* type-specific state */
545 enum tog_view_type type;
546 union {
547 struct tog_diff_view_state diff;
548 struct tog_log_view_state log;
549 struct tog_blame_view_state blame;
550 struct tog_tree_view_state tree;
551 struct tog_ref_view_state ref;
552 } state;
554 const struct got_error *(*show)(struct tog_view *);
555 const struct got_error *(*input)(struct tog_view **,
556 struct tog_view *, int);
557 const struct got_error *(*reset)(struct tog_view *);
558 const struct got_error *(*resize)(struct tog_view *, int);
559 const struct got_error *(*close)(struct tog_view *);
561 const struct got_error *(*search_start)(struct tog_view *);
562 const struct got_error *(*search_next)(struct tog_view *);
563 int search_started;
564 int searching;
565 #define TOG_SEARCH_FORWARD 1
566 #define TOG_SEARCH_BACKWARD 2
567 int search_next_done;
568 #define TOG_SEARCH_HAVE_MORE 1
569 #define TOG_SEARCH_NO_MORE 2
570 #define TOG_SEARCH_HAVE_NONE 3
571 regex_t regex;
572 regmatch_t regmatch;
573 };
575 static const struct got_error *open_diff_view(struct tog_view *,
576 struct got_object_id *, struct got_object_id *,
577 const char *, const char *, int, int, int, struct tog_view *,
578 struct got_repository *);
579 static const struct got_error *show_diff_view(struct tog_view *);
580 static const struct got_error *input_diff_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *reset_diff_view(struct tog_view *);
583 static const struct got_error* close_diff_view(struct tog_view *);
584 static const struct got_error *search_start_diff_view(struct tog_view *);
585 static const struct got_error *search_next_diff_view(struct tog_view *);
587 static const struct got_error *open_log_view(struct tog_view *,
588 struct got_object_id *, struct got_repository *,
589 const char *, const char *, int);
590 static const struct got_error * show_log_view(struct tog_view *);
591 static const struct got_error *input_log_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *resize_log_view(struct tog_view *, int);
594 static const struct got_error *close_log_view(struct tog_view *);
595 static const struct got_error *search_start_log_view(struct tog_view *);
596 static const struct got_error *search_next_log_view(struct tog_view *);
598 static const struct got_error *open_blame_view(struct tog_view *, char *,
599 struct got_object_id *, struct got_repository *);
600 static const struct got_error *show_blame_view(struct tog_view *);
601 static const struct got_error *input_blame_view(struct tog_view **,
602 struct tog_view *, int);
603 static const struct got_error *reset_blame_view(struct tog_view *);
604 static const struct got_error *close_blame_view(struct tog_view *);
605 static const struct got_error *search_start_blame_view(struct tog_view *);
606 static const struct got_error *search_next_blame_view(struct tog_view *);
608 static const struct got_error *open_tree_view(struct tog_view *,
609 struct got_object_id *, const char *, struct got_repository *);
610 static const struct got_error *show_tree_view(struct tog_view *);
611 static const struct got_error *input_tree_view(struct tog_view **,
612 struct tog_view *, int);
613 static const struct got_error *close_tree_view(struct tog_view *);
614 static const struct got_error *search_start_tree_view(struct tog_view *);
615 static const struct got_error *search_next_tree_view(struct tog_view *);
617 static const struct got_error *open_ref_view(struct tog_view *,
618 struct got_repository *);
619 static const struct got_error *show_ref_view(struct tog_view *);
620 static const struct got_error *input_ref_view(struct tog_view **,
621 struct tog_view *, int);
622 static const struct got_error *close_ref_view(struct tog_view *);
623 static const struct got_error *search_start_ref_view(struct tog_view *);
624 static const struct got_error *search_next_ref_view(struct tog_view *);
626 static volatile sig_atomic_t tog_sigwinch_received;
627 static volatile sig_atomic_t tog_sigpipe_received;
628 static volatile sig_atomic_t tog_sigcont_received;
629 static volatile sig_atomic_t tog_sigint_received;
630 static volatile sig_atomic_t tog_sigterm_received;
632 static void
633 tog_sigwinch(int signo)
635 tog_sigwinch_received = 1;
638 static void
639 tog_sigpipe(int signo)
641 tog_sigpipe_received = 1;
644 static void
645 tog_sigcont(int signo)
647 tog_sigcont_received = 1;
650 static void
651 tog_sigint(int signo)
653 tog_sigint_received = 1;
656 static void
657 tog_sigterm(int signo)
659 tog_sigterm_received = 1;
662 static int
663 tog_fatal_signal_received(void)
665 return (tog_sigpipe_received ||
666 tog_sigint_received || tog_sigint_received);
669 static const struct got_error *
670 view_close(struct tog_view *view)
672 const struct got_error *err = NULL, *child_err = NULL;
674 if (view->child) {
675 child_err = view_close(view->child);
676 view->child = NULL;
678 if (view->close)
679 err = view->close(view);
680 if (view->panel)
681 del_panel(view->panel);
682 if (view->window)
683 delwin(view->window);
684 free(view);
685 return err ? err : child_err;
688 static struct tog_view *
689 view_open(int nlines, int ncols, int begin_y, int begin_x,
690 enum tog_view_type type)
692 struct tog_view *view = calloc(1, sizeof(*view));
694 if (view == NULL)
695 return NULL;
697 view->type = type;
698 view->lines = LINES;
699 view->cols = COLS;
700 view->nlines = nlines ? nlines : LINES - begin_y;
701 view->ncols = ncols ? ncols : COLS - begin_x;
702 view->begin_y = begin_y;
703 view->begin_x = begin_x;
704 view->window = newwin(nlines, ncols, begin_y, begin_x);
705 if (view->window == NULL) {
706 view_close(view);
707 return NULL;
709 view->panel = new_panel(view->window);
710 if (view->panel == NULL ||
711 set_panel_userptr(view->panel, view) != OK) {
712 view_close(view);
713 return NULL;
716 keypad(view->window, TRUE);
717 return view;
720 static int
721 view_split_begin_x(int begin_x)
723 if (begin_x > 0 || COLS < 120)
724 return 0;
725 return (COLS - MAX(COLS / 2, 80));
728 /* XXX Stub till we decide what to do. */
729 static int
730 view_split_begin_y(int lines)
732 return lines * HSPLIT_SCALE;
735 static const struct got_error *view_resize(struct tog_view *);
737 static const struct got_error *
738 view_splitscreen(struct tog_view *view)
740 const struct got_error *err = NULL;
742 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
743 if (view->resized_y && view->resized_y < view->lines)
744 view->begin_y = view->resized_y;
745 else
746 view->begin_y = view_split_begin_y(view->nlines);
747 view->begin_x = 0;
748 } else if (!view->resized) {
749 if (view->resized_x && view->resized_x < view->cols - 1 &&
750 view->cols > 119)
751 view->begin_x = view->resized_x;
752 else
753 view->begin_x = view_split_begin_x(0);
754 view->begin_y = 0;
756 view->nlines = LINES - view->begin_y;
757 view->ncols = COLS - view->begin_x;
758 view->lines = LINES;
759 view->cols = COLS;
760 err = view_resize(view);
761 if (err)
762 return err;
764 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
765 view->parent->nlines = view->begin_y;
767 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
768 return got_error_from_errno("mvwin");
770 return NULL;
773 static const struct got_error *
774 view_fullscreen(struct tog_view *view)
776 const struct got_error *err = NULL;
778 view->begin_x = 0;
779 view->begin_y = view->resized ? view->begin_y : 0;
780 view->nlines = view->resized ? view->nlines : LINES;
781 view->ncols = COLS;
782 view->lines = LINES;
783 view->cols = COLS;
784 err = view_resize(view);
785 if (err)
786 return err;
788 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
789 return got_error_from_errno("mvwin");
791 return NULL;
794 static int
795 view_is_parent_view(struct tog_view *view)
797 return view->parent == NULL;
800 static int
801 view_is_splitscreen(struct tog_view *view)
803 return view->begin_x > 0 || view->begin_y > 0;
806 static int
807 view_is_fullscreen(struct tog_view *view)
809 return view->nlines == LINES && view->ncols == COLS;
812 static int
813 view_is_hsplit_top(struct tog_view *view)
815 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
816 view_is_splitscreen(view->child);
819 static void
820 view_border(struct tog_view *view)
822 PANEL *panel;
823 const struct tog_view *view_above;
825 if (view->parent)
826 return view_border(view->parent);
828 panel = panel_above(view->panel);
829 if (panel == NULL)
830 return;
832 view_above = panel_userptr(panel);
833 if (view->mode == TOG_VIEW_SPLIT_HRZN)
834 mvwhline(view->window, view_above->begin_y - 1,
835 view->begin_x, got_locale_is_utf8() ?
836 ACS_HLINE : '-', view->ncols);
837 else
838 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
839 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
842 static const struct got_error *view_init_hsplit(struct tog_view *, int);
843 static const struct got_error *request_log_commits(struct tog_view *);
844 static const struct got_error *offset_selection_down(struct tog_view *);
845 static void offset_selection_up(struct tog_view *);
846 static void view_get_split(struct tog_view *, int *, int *);
848 static const struct got_error *
849 view_resize(struct tog_view *view)
851 const struct got_error *err = NULL;
852 int dif, nlines, ncols;
854 dif = LINES - view->lines; /* line difference */
856 if (view->lines > LINES)
857 nlines = view->nlines - (view->lines - LINES);
858 else
859 nlines = view->nlines + (LINES - view->lines);
860 if (view->cols > COLS)
861 ncols = view->ncols - (view->cols - COLS);
862 else
863 ncols = view->ncols + (COLS - view->cols);
865 if (view->child) {
866 int hs = view->child->begin_y;
868 if (!view_is_fullscreen(view))
869 view->child->begin_x = view_split_begin_x(view->begin_x);
870 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
871 view->child->begin_x == 0) {
872 ncols = COLS;
874 view_fullscreen(view->child);
875 if (view->child->focussed)
876 show_panel(view->child->panel);
877 else
878 show_panel(view->panel);
879 } else {
880 ncols = view->child->begin_x;
882 view_splitscreen(view->child);
883 show_panel(view->child->panel);
885 /*
886 * XXX This is ugly and needs to be moved into the above
887 * logic but "works" for now and my attempts at moving it
888 * break either 'tab' or 'F' key maps in horizontal splits.
889 */
890 if (hs) {
891 err = view_splitscreen(view->child);
892 if (err)
893 return err;
894 if (dif < 0) { /* top split decreased */
895 err = offset_selection_down(view);
896 if (err)
897 return err;
899 view_border(view);
900 update_panels();
901 doupdate();
902 show_panel(view->child->panel);
903 nlines = view->nlines;
905 } else if (view->parent == NULL)
906 ncols = COLS;
908 if (view->resize && dif > 0) {
909 err = view->resize(view, dif);
910 if (err)
911 return err;
914 if (wresize(view->window, nlines, ncols) == ERR)
915 return got_error_from_errno("wresize");
916 if (replace_panel(view->panel, view->window) == ERR)
917 return got_error_from_errno("replace_panel");
918 wclear(view->window);
920 view->nlines = nlines;
921 view->ncols = ncols;
922 view->lines = LINES;
923 view->cols = COLS;
925 return NULL;
928 static const struct got_error *
929 resize_log_view(struct tog_view *view, int increase)
931 struct tog_log_view_state *s = &view->state.log;
932 const struct got_error *err = NULL;
933 int n = s->selected_entry->idx + view->lines - s->selected;
935 /*
936 * Request commits to account for the increased
937 * height so we have enough to populate the view.
938 */
939 if (s->commits.ncommits < n) {
940 view->nscrolled = n - s->commits.ncommits + increase + 1;
941 err = request_log_commits(view);
944 return err;
947 static void
948 view_adjust_offset(struct tog_view *view, int n)
950 if (n == 0)
951 return;
953 if (view->parent && view->parent->offset) {
954 if (view->parent->offset + n >= 0)
955 view->parent->offset += n;
956 else
957 view->parent->offset = 0;
958 } else if (view->offset) {
959 if (view->offset - n >= 0)
960 view->offset -= n;
961 else
962 view->offset = 0;
966 static const struct got_error *
967 view_resize_split(struct tog_view *view, int resize)
969 const struct got_error *err = NULL;
970 struct tog_view *v = NULL;
972 if (view->parent)
973 v = view->parent;
974 else
975 v = view;
977 if (!v->child || !view_is_splitscreen(v->child))
978 return NULL;
980 v->resized = v->child->resized = resize; /* lock for resize event */
982 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
983 int y = v->child->begin_y;
985 if (v->child->resized_y)
986 v->child->begin_y = v->child->resized_y;
987 if (view->parent)
988 v->child->begin_y -= resize;
989 else
990 v->child->begin_y += resize;
991 if (v->child->begin_y < 3) {
992 view->count = 0;
993 v->child->begin_y = 3;
994 } else if (v->child->begin_y > LINES - 1) {
995 view->count = 0;
996 v->child->begin_y = LINES - 1;
998 v->ncols = COLS;
999 v->child->ncols = COLS;
1000 view_adjust_offset(view, resize);
1001 err = view_init_hsplit(v, v->child->begin_y);
1002 if (err)
1003 return err;
1004 v->child->resized_y = v->child->begin_y;
1005 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1006 v->child->nscrolled = y - v->child->begin_y;
1007 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1008 v->nscrolled = v->child->begin_y - y;
1009 } else {
1010 if (v->child->resized_x)
1011 v->child->begin_x = v->child->resized_x;
1012 if (view->parent)
1013 v->child->begin_x -= resize;
1014 else
1015 v->child->begin_x += resize;
1016 if (v->child->begin_x < 11) {
1017 view->count = 0;
1018 v->child->begin_x = 11;
1019 } else if (v->child->begin_x > COLS - 1) {
1020 view->count = 0;
1021 v->child->begin_x = COLS - 1;
1023 v->child->resized_x = v->child->begin_x;
1026 v->child->mode = v->mode;
1027 v->child->nlines = v->lines - v->child->begin_y;
1028 v->child->ncols = v->cols - v->child->begin_x;
1029 v->focus_child = 1;
1031 err = view_fullscreen(v);
1032 if (err)
1033 return err;
1034 err = view_splitscreen(v->child);
1035 if (err)
1036 return err;
1038 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1039 err = offset_selection_down(v->child);
1040 if (err)
1041 return err;
1044 if (v->nscrolled)
1045 err = request_log_commits(v);
1046 else if (v->child->nscrolled)
1047 err = request_log_commits(v->child);
1049 v->resized = v->child->resized = 0;
1051 return err;
1054 static void
1055 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1057 struct tog_view *v = src->child ? src->child : src;
1059 dst->resized_x = v->resized_x;
1060 dst->resized_y = v->resized_y;
1063 static const struct got_error *
1064 view_close_child(struct tog_view *view)
1066 const struct got_error *err = NULL;
1068 if (view->child == NULL)
1069 return NULL;
1071 err = view_close(view->child);
1072 view->child = NULL;
1073 return err;
1076 static const struct got_error *
1077 view_set_child(struct tog_view *view, struct tog_view *child)
1079 const struct got_error *err = NULL;
1081 view->child = child;
1082 child->parent = view;
1084 err = view_resize(view);
1085 if (err)
1086 return err;
1088 if (view->child->resized_x || view->child->resized_y)
1089 err = view_resize_split(view, 0);
1091 return err;
1094 static void
1095 tog_resizeterm(void)
1097 int cols, lines;
1098 struct winsize size;
1100 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1101 cols = 80; /* Default */
1102 lines = 24;
1103 } else {
1104 cols = size.ws_col;
1105 lines = size.ws_row;
1107 resize_term(lines, cols);
1110 static const struct got_error *
1111 view_search_start(struct tog_view *view)
1113 const struct got_error *err = NULL;
1114 struct tog_view *v = view;
1115 char pattern[1024];
1116 int ret;
1118 if (view->search_started) {
1119 regfree(&view->regex);
1120 view->searching = 0;
1121 memset(&view->regmatch, 0, sizeof(view->regmatch));
1123 view->search_started = 0;
1125 if (view->nlines < 1)
1126 return NULL;
1128 if (view_is_hsplit_top(view))
1129 v = view->child;
1131 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1132 wclrtoeol(v->window);
1134 nodelay(view->window, FALSE); /* block for search term input */
1135 nocbreak();
1136 echo();
1137 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1138 wrefresh(v->window);
1139 cbreak();
1140 noecho();
1141 nodelay(view->window, TRUE);
1142 if (ret == ERR)
1143 return NULL;
1145 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1146 err = view->search_start(view);
1147 if (err) {
1148 regfree(&view->regex);
1149 return err;
1151 view->search_started = 1;
1152 view->searching = TOG_SEARCH_FORWARD;
1153 view->search_next_done = 0;
1154 view->search_next(view);
1157 return NULL;
1160 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1161 static const struct got_error *
1162 switch_split(struct tog_view *view)
1164 const struct got_error *err = NULL;
1165 struct tog_view *v = NULL;
1167 if (view->parent)
1168 v = view->parent;
1169 else
1170 v = view;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 v->mode = TOG_VIEW_SPLIT_VERT;
1174 else
1175 v->mode = TOG_VIEW_SPLIT_HRZN;
1177 if (!v->child)
1178 return NULL;
1179 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1180 v->mode = TOG_VIEW_SPLIT_NONE;
1182 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1183 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1184 v->child->begin_y = v->child->resized_y;
1185 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1186 v->child->begin_x = v->child->resized_x;
1189 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1190 v->ncols = COLS;
1191 v->child->ncols = COLS;
1192 v->child->nscrolled = LINES - v->child->nlines;
1194 err = view_init_hsplit(v, v->child->begin_y);
1195 if (err)
1196 return err;
1198 v->child->mode = v->mode;
1199 v->child->nlines = v->lines - v->child->begin_y;
1200 v->focus_child = 1;
1202 err = view_fullscreen(v);
1203 if (err)
1204 return err;
1205 err = view_splitscreen(v->child);
1206 if (err)
1207 return err;
1209 if (v->mode == TOG_VIEW_SPLIT_NONE)
1210 v->mode = TOG_VIEW_SPLIT_VERT;
1211 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1212 err = offset_selection_down(v);
1213 err = offset_selection_down(v->child);
1214 } else {
1215 offset_selection_up(v);
1216 offset_selection_up(v->child);
1218 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1219 err = request_log_commits(v);
1220 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1221 err = request_log_commits(v->child);
1223 return err;
1227 * Compute view->count from numeric input. Assign total to view->count and
1228 * return first non-numeric key entered.
1230 static int
1231 get_compound_key(struct tog_view *view, int c)
1233 struct tog_view *v = view;
1234 int x, n = 0;
1236 if (view_is_hsplit_top(view))
1237 v = view->child;
1238 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1239 v = view->parent;
1241 view->count = 0;
1242 cbreak(); /* block for input */
1243 wmove(v->window, v->nlines - 1, 0);
1244 wclrtoeol(v->window);
1245 waddch(v->window, ':');
1247 do {
1248 x = getcurx(v->window);
1249 if (x != ERR && x < view->ncols) {
1250 waddch(v->window, c);
1251 wrefresh(v->window);
1255 * Don't overflow. Max valid request should be the greatest
1256 * between the longest and total lines; cap at 10 million.
1258 if (n >= 9999999)
1259 n = 9999999;
1260 else
1261 n = n * 10 + (c - '0');
1262 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1264 /* Massage excessive or inapplicable values at the input handler. */
1265 view->count = n;
1267 return c;
1270 static const struct got_error *
1271 view_input(struct tog_view **new, int *done, struct tog_view *view,
1272 struct tog_view_list_head *views)
1274 const struct got_error *err = NULL;
1275 struct tog_view *v;
1276 int ch, errcode;
1278 *new = NULL;
1280 /* Clear "no matches" indicator. */
1281 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1282 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1283 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1284 view->count = 0;
1287 if (view->searching && !view->search_next_done) {
1288 errcode = pthread_mutex_unlock(&tog_mutex);
1289 if (errcode)
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_unlock");
1292 sched_yield();
1293 errcode = pthread_mutex_lock(&tog_mutex);
1294 if (errcode)
1295 return got_error_set_errno(errcode,
1296 "pthread_mutex_lock");
1297 view->search_next(view);
1298 return NULL;
1301 nodelay(view->window, FALSE);
1302 /* Allow threads to make progress while we are waiting for input. */
1303 errcode = pthread_mutex_unlock(&tog_mutex);
1304 if (errcode)
1305 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1306 /* If we have an unfinished count, let C-g or backspace abort. */
1307 if (view->count && --view->count) {
1308 cbreak();
1309 nodelay(view->window, TRUE);
1310 ch = wgetch(view->window);
1311 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1312 view->count = 0;
1313 else
1314 ch = view->ch;
1315 } else {
1316 ch = wgetch(view->window);
1317 if (ch >= '1' && ch <= '9')
1318 view->ch = ch = get_compound_key(view, ch);
1320 errcode = pthread_mutex_lock(&tog_mutex);
1321 if (errcode)
1322 return got_error_set_errno(errcode, "pthread_mutex_lock");
1323 nodelay(view->window, TRUE);
1325 if (tog_sigwinch_received || tog_sigcont_received) {
1326 tog_resizeterm();
1327 tog_sigwinch_received = 0;
1328 tog_sigcont_received = 0;
1329 TAILQ_FOREACH(v, views, entry) {
1330 err = view_resize(v);
1331 if (err)
1332 return err;
1333 err = v->input(new, v, KEY_RESIZE);
1334 if (err)
1335 return err;
1336 if (v->child) {
1337 err = view_resize(v->child);
1338 if (err)
1339 return err;
1340 err = v->child->input(new, v->child,
1341 KEY_RESIZE);
1342 if (err)
1343 return err;
1344 if (v->child->resized_x || v->child->resized_y) {
1345 err = view_resize_split(v, 0);
1346 if (err)
1347 return err;
1353 switch (ch) {
1354 case '\t':
1355 view->count = 0;
1356 if (view->child) {
1357 view->focussed = 0;
1358 view->child->focussed = 1;
1359 view->focus_child = 1;
1360 } else if (view->parent) {
1361 view->focussed = 0;
1362 view->parent->focussed = 1;
1363 view->parent->focus_child = 0;
1364 if (!view_is_splitscreen(view)) {
1365 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1366 view->parent->type == TOG_VIEW_LOG) {
1367 err = request_log_commits(view->parent);
1368 if (err)
1369 return err;
1371 offset_selection_up(view->parent);
1372 err = view_fullscreen(view->parent);
1373 if (err)
1374 return err;
1377 break;
1378 case 'q':
1379 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1380 if (view->parent->type == TOG_VIEW_LOG) {
1381 /* might need more commits to fill fullscreen */
1382 err = request_log_commits(view->parent);
1383 if (err)
1384 break;
1386 offset_selection_up(view->parent);
1388 err = view->input(new, view, ch);
1389 view->dying = 1;
1390 break;
1391 case 'Q':
1392 *done = 1;
1393 break;
1394 case 'F':
1395 view->count = 0;
1396 if (view_is_parent_view(view)) {
1397 if (view->child == NULL)
1398 break;
1399 if (view_is_splitscreen(view->child)) {
1400 view->focussed = 0;
1401 view->child->focussed = 1;
1402 err = view_fullscreen(view->child);
1403 } else {
1404 err = view_splitscreen(view->child);
1405 if (!err)
1406 err = view_resize_split(view, 0);
1408 if (err)
1409 break;
1410 err = view->child->input(new, view->child,
1411 KEY_RESIZE);
1412 } else {
1413 if (view_is_splitscreen(view)) {
1414 view->parent->focussed = 0;
1415 view->focussed = 1;
1416 err = view_fullscreen(view);
1417 } else {
1418 err = view_splitscreen(view);
1419 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1420 err = view_resize(view->parent);
1421 if (!err)
1422 err = view_resize_split(view, 0);
1424 if (err)
1425 break;
1426 err = view->input(new, view, KEY_RESIZE);
1428 if (err)
1429 break;
1430 if (view->type == TOG_VIEW_LOG) {
1431 err = request_log_commits(view);
1432 if (err)
1433 break;
1435 if (view->parent)
1436 err = offset_selection_down(view->parent);
1437 if (!err)
1438 err = offset_selection_down(view);
1439 break;
1440 case 'S':
1441 view->count = 0;
1442 err = switch_split(view);
1443 break;
1444 case '-':
1445 err = view_resize_split(view, -1);
1446 break;
1447 case '+':
1448 err = view_resize_split(view, 1);
1449 break;
1450 case KEY_RESIZE:
1451 break;
1452 case '/':
1453 view->count = 0;
1454 if (view->search_start)
1455 view_search_start(view);
1456 else
1457 err = view->input(new, view, ch);
1458 break;
1459 case 'N':
1460 case 'n':
1461 if (view->search_started && view->search_next) {
1462 view->searching = (ch == 'n' ?
1463 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1464 view->search_next_done = 0;
1465 view->search_next(view);
1466 } else
1467 err = view->input(new, view, ch);
1468 break;
1469 case 'A':
1470 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1471 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1472 else
1473 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1474 TAILQ_FOREACH(v, views, entry) {
1475 if (v->reset) {
1476 err = v->reset(v);
1477 if (err)
1478 return err;
1480 if (v->child && v->child->reset) {
1481 err = v->child->reset(v->child);
1482 if (err)
1483 return err;
1486 break;
1487 default:
1488 err = view->input(new, view, ch);
1489 break;
1492 return err;
1495 static int
1496 view_needs_focus_indication(struct tog_view *view)
1498 if (view_is_parent_view(view)) {
1499 if (view->child == NULL || view->child->focussed)
1500 return 0;
1501 if (!view_is_splitscreen(view->child))
1502 return 0;
1503 } else if (!view_is_splitscreen(view))
1504 return 0;
1506 return view->focussed;
1509 static const struct got_error *
1510 view_loop(struct tog_view *view)
1512 const struct got_error *err = NULL;
1513 struct tog_view_list_head views;
1514 struct tog_view *new_view;
1515 char *mode;
1516 int fast_refresh = 10;
1517 int done = 0, errcode;
1519 mode = getenv("TOG_VIEW_SPLIT_MODE");
1520 if (!mode || !(*mode == 'h' || *mode == 'H'))
1521 view->mode = TOG_VIEW_SPLIT_VERT;
1522 else
1523 view->mode = TOG_VIEW_SPLIT_HRZN;
1525 errcode = pthread_mutex_lock(&tog_mutex);
1526 if (errcode)
1527 return got_error_set_errno(errcode, "pthread_mutex_lock");
1529 TAILQ_INIT(&views);
1530 TAILQ_INSERT_HEAD(&views, view, entry);
1532 view->focussed = 1;
1533 err = view->show(view);
1534 if (err)
1535 return err;
1536 update_panels();
1537 doupdate();
1538 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1539 !tog_fatal_signal_received()) {
1540 /* Refresh fast during initialization, then become slower. */
1541 if (fast_refresh && fast_refresh-- == 0)
1542 halfdelay(10); /* switch to once per second */
1544 err = view_input(&new_view, &done, view, &views);
1545 if (err)
1546 break;
1547 if (view->dying) {
1548 struct tog_view *v, *prev = NULL;
1550 if (view_is_parent_view(view))
1551 prev = TAILQ_PREV(view, tog_view_list_head,
1552 entry);
1553 else if (view->parent)
1554 prev = view->parent;
1556 if (view->parent) {
1557 view->parent->child = NULL;
1558 view->parent->focus_child = 0;
1559 /* Restore fullscreen line height. */
1560 view->parent->nlines = view->parent->lines;
1561 err = view_resize(view->parent);
1562 if (err)
1563 break;
1564 /* Make resized splits persist. */
1565 view_transfer_size(view->parent, view);
1566 } else
1567 TAILQ_REMOVE(&views, view, entry);
1569 err = view_close(view);
1570 if (err)
1571 goto done;
1573 view = NULL;
1574 TAILQ_FOREACH(v, &views, entry) {
1575 if (v->focussed)
1576 break;
1578 if (view == NULL && new_view == NULL) {
1579 /* No view has focus. Try to pick one. */
1580 if (prev)
1581 view = prev;
1582 else if (!TAILQ_EMPTY(&views)) {
1583 view = TAILQ_LAST(&views,
1584 tog_view_list_head);
1586 if (view) {
1587 if (view->focus_child) {
1588 view->child->focussed = 1;
1589 view = view->child;
1590 } else
1591 view->focussed = 1;
1595 if (new_view) {
1596 struct tog_view *v, *t;
1597 /* Only allow one parent view per type. */
1598 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1599 if (v->type != new_view->type)
1600 continue;
1601 TAILQ_REMOVE(&views, v, entry);
1602 err = view_close(v);
1603 if (err)
1604 goto done;
1605 break;
1607 TAILQ_INSERT_TAIL(&views, new_view, entry);
1608 view = new_view;
1610 if (view) {
1611 if (view_is_parent_view(view)) {
1612 if (view->child && view->child->focussed)
1613 view = view->child;
1614 } else {
1615 if (view->parent && view->parent->focussed)
1616 view = view->parent;
1618 show_panel(view->panel);
1619 if (view->child && view_is_splitscreen(view->child))
1620 show_panel(view->child->panel);
1621 if (view->parent && view_is_splitscreen(view)) {
1622 err = view->parent->show(view->parent);
1623 if (err)
1624 goto done;
1626 err = view->show(view);
1627 if (err)
1628 goto done;
1629 if (view->child) {
1630 err = view->child->show(view->child);
1631 if (err)
1632 goto done;
1634 update_panels();
1635 doupdate();
1638 done:
1639 while (!TAILQ_EMPTY(&views)) {
1640 const struct got_error *close_err;
1641 view = TAILQ_FIRST(&views);
1642 TAILQ_REMOVE(&views, view, entry);
1643 close_err = view_close(view);
1644 if (close_err && err == NULL)
1645 err = close_err;
1648 errcode = pthread_mutex_unlock(&tog_mutex);
1649 if (errcode && err == NULL)
1650 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1652 return err;
1655 __dead static void
1656 usage_log(void)
1658 endwin();
1659 fprintf(stderr,
1660 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1661 getprogname());
1662 exit(1);
1665 /* Create newly allocated wide-character string equivalent to a byte string. */
1666 static const struct got_error *
1667 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1669 char *vis = NULL;
1670 const struct got_error *err = NULL;
1672 *ws = NULL;
1673 *wlen = mbstowcs(NULL, s, 0);
1674 if (*wlen == (size_t)-1) {
1675 int vislen;
1676 if (errno != EILSEQ)
1677 return got_error_from_errno("mbstowcs");
1679 /* byte string invalid in current encoding; try to "fix" it */
1680 err = got_mbsavis(&vis, &vislen, s);
1681 if (err)
1682 return err;
1683 *wlen = mbstowcs(NULL, vis, 0);
1684 if (*wlen == (size_t)-1) {
1685 err = got_error_from_errno("mbstowcs"); /* give up */
1686 goto done;
1690 *ws = calloc(*wlen + 1, sizeof(**ws));
1691 if (*ws == NULL) {
1692 err = got_error_from_errno("calloc");
1693 goto done;
1696 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1697 err = got_error_from_errno("mbstowcs");
1698 done:
1699 free(vis);
1700 if (err) {
1701 free(*ws);
1702 *ws = NULL;
1703 *wlen = 0;
1705 return err;
1708 static const struct got_error *
1709 expand_tab(char **ptr, const char *src)
1711 char *dst;
1712 size_t len, n, idx = 0, sz = 0;
1714 *ptr = NULL;
1715 n = len = strlen(src);
1716 dst = malloc(n + 1);
1717 if (dst == NULL)
1718 return got_error_from_errno("malloc");
1720 while (idx < len && src[idx]) {
1721 const char c = src[idx];
1723 if (c == '\t') {
1724 size_t nb = TABSIZE - sz % TABSIZE;
1725 char *p;
1727 p = realloc(dst, n + nb);
1728 if (p == NULL) {
1729 free(dst);
1730 return got_error_from_errno("realloc");
1733 dst = p;
1734 n += nb;
1735 memset(dst + sz, ' ', nb);
1736 sz += nb;
1737 } else
1738 dst[sz++] = src[idx];
1739 ++idx;
1742 dst[sz] = '\0';
1743 *ptr = dst;
1744 return NULL;
1748 * Advance at most n columns from wline starting at offset off.
1749 * Return the index to the first character after the span operation.
1750 * Return the combined column width of all spanned wide character in
1751 * *rcol.
1753 static int
1754 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1756 int width, i, cols = 0;
1758 if (n == 0) {
1759 *rcol = cols;
1760 return off;
1763 for (i = off; wline[i] != L'\0'; ++i) {
1764 if (wline[i] == L'\t')
1765 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1766 else
1767 width = wcwidth(wline[i]);
1769 if (width == -1) {
1770 width = 1;
1771 wline[i] = L'.';
1774 if (cols + width > n)
1775 break;
1776 cols += width;
1779 *rcol = cols;
1780 return i;
1784 * Format a line for display, ensuring that it won't overflow a width limit.
1785 * With scrolling, the width returned refers to the scrolled version of the
1786 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1788 static const struct got_error *
1789 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1790 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1792 const struct got_error *err = NULL;
1793 int cols;
1794 wchar_t *wline = NULL;
1795 char *exstr = NULL;
1796 size_t wlen;
1797 int i, scrollx;
1799 *wlinep = NULL;
1800 *widthp = 0;
1802 if (expand) {
1803 err = expand_tab(&exstr, line);
1804 if (err)
1805 return err;
1808 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1809 free(exstr);
1810 if (err)
1811 return err;
1813 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1815 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1816 wline[wlen - 1] = L'\0';
1817 wlen--;
1819 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1820 wline[wlen - 1] = L'\0';
1821 wlen--;
1824 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1825 wline[i] = L'\0';
1827 if (widthp)
1828 *widthp = cols;
1829 if (scrollxp)
1830 *scrollxp = scrollx;
1831 if (err)
1832 free(wline);
1833 else
1834 *wlinep = wline;
1835 return err;
1838 static const struct got_error*
1839 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1840 struct got_object_id *id, struct got_repository *repo)
1842 static const struct got_error *err = NULL;
1843 struct got_reflist_entry *re;
1844 char *s;
1845 const char *name;
1847 *refs_str = NULL;
1849 TAILQ_FOREACH(re, refs, entry) {
1850 struct got_tag_object *tag = NULL;
1851 struct got_object_id *ref_id;
1852 int cmp;
1854 name = got_ref_get_name(re->ref);
1855 if (strcmp(name, GOT_REF_HEAD) == 0)
1856 continue;
1857 if (strncmp(name, "refs/", 5) == 0)
1858 name += 5;
1859 if (strncmp(name, "got/", 4) == 0 &&
1860 strncmp(name, "got/backup/", 11) != 0)
1861 continue;
1862 if (strncmp(name, "heads/", 6) == 0)
1863 name += 6;
1864 if (strncmp(name, "remotes/", 8) == 0) {
1865 name += 8;
1866 s = strstr(name, "/" GOT_REF_HEAD);
1867 if (s != NULL && s[strlen(s)] == '\0')
1868 continue;
1870 err = got_ref_resolve(&ref_id, repo, re->ref);
1871 if (err)
1872 break;
1873 if (strncmp(name, "tags/", 5) == 0) {
1874 err = got_object_open_as_tag(&tag, repo, ref_id);
1875 if (err) {
1876 if (err->code != GOT_ERR_OBJ_TYPE) {
1877 free(ref_id);
1878 break;
1880 /* Ref points at something other than a tag. */
1881 err = NULL;
1882 tag = NULL;
1885 cmp = got_object_id_cmp(tag ?
1886 got_object_tag_get_object_id(tag) : ref_id, id);
1887 free(ref_id);
1888 if (tag)
1889 got_object_tag_close(tag);
1890 if (cmp != 0)
1891 continue;
1892 s = *refs_str;
1893 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1894 s ? ", " : "", name) == -1) {
1895 err = got_error_from_errno("asprintf");
1896 free(s);
1897 *refs_str = NULL;
1898 break;
1900 free(s);
1903 return err;
1906 static const struct got_error *
1907 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1908 int col_tab_align)
1910 char *smallerthan;
1912 smallerthan = strchr(author, '<');
1913 if (smallerthan && smallerthan[1] != '\0')
1914 author = smallerthan + 1;
1915 author[strcspn(author, "@>")] = '\0';
1916 return format_line(wauthor, author_width, NULL, author, 0, limit,
1917 col_tab_align, 0);
1920 static const struct got_error *
1921 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1922 struct got_object_id *id, const size_t date_display_cols,
1923 int author_display_cols)
1925 struct tog_log_view_state *s = &view->state.log;
1926 const struct got_error *err = NULL;
1927 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1928 char *logmsg0 = NULL, *logmsg = NULL;
1929 char *author = NULL;
1930 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1931 int author_width, logmsg_width;
1932 char *newline, *line = NULL;
1933 int col, limit, scrollx;
1934 const int avail = view->ncols;
1935 struct tm tm;
1936 time_t committer_time;
1937 struct tog_color *tc;
1939 committer_time = got_object_commit_get_committer_time(commit);
1940 if (gmtime_r(&committer_time, &tm) == NULL)
1941 return got_error_from_errno("gmtime_r");
1942 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1943 return got_error(GOT_ERR_NO_SPACE);
1945 if (avail <= date_display_cols)
1946 limit = MIN(sizeof(datebuf) - 1, avail);
1947 else
1948 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1949 tc = get_color(&s->colors, TOG_COLOR_DATE);
1950 if (tc)
1951 wattr_on(view->window,
1952 COLOR_PAIR(tc->colorpair), NULL);
1953 waddnstr(view->window, datebuf, limit);
1954 if (tc)
1955 wattr_off(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1957 col = limit;
1958 if (col > avail)
1959 goto done;
1961 if (avail >= 120) {
1962 char *id_str;
1963 err = got_object_id_str(&id_str, id);
1964 if (err)
1965 goto done;
1966 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1967 if (tc)
1968 wattr_on(view->window,
1969 COLOR_PAIR(tc->colorpair), NULL);
1970 wprintw(view->window, "%.8s ", id_str);
1971 if (tc)
1972 wattr_off(view->window,
1973 COLOR_PAIR(tc->colorpair), NULL);
1974 free(id_str);
1975 col += 9;
1976 if (col > avail)
1977 goto done;
1980 if (s->use_committer)
1981 author = strdup(got_object_commit_get_committer(commit));
1982 else
1983 author = strdup(got_object_commit_get_author(commit));
1984 if (author == NULL) {
1985 err = got_error_from_errno("strdup");
1986 goto done;
1988 err = format_author(&wauthor, &author_width, author, avail - col, col);
1989 if (err)
1990 goto done;
1991 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1992 if (tc)
1993 wattr_on(view->window,
1994 COLOR_PAIR(tc->colorpair), NULL);
1995 waddwstr(view->window, wauthor);
1996 if (tc)
1997 wattr_off(view->window,
1998 COLOR_PAIR(tc->colorpair), NULL);
1999 col += author_width;
2000 while (col < avail && author_width < author_display_cols + 2) {
2001 waddch(view->window, ' ');
2002 col++;
2003 author_width++;
2005 if (col > avail)
2006 goto done;
2008 err = got_object_commit_get_logmsg(&logmsg0, commit);
2009 if (err)
2010 goto done;
2011 logmsg = logmsg0;
2012 while (*logmsg == '\n')
2013 logmsg++;
2014 newline = strchr(logmsg, '\n');
2015 if (newline)
2016 *newline = '\0';
2017 limit = avail - col;
2018 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2019 limit--; /* for the border */
2020 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2021 limit, col, 1);
2022 if (err)
2023 goto done;
2024 waddwstr(view->window, &wlogmsg[scrollx]);
2025 col += MAX(logmsg_width, 0);
2026 while (col < avail) {
2027 waddch(view->window, ' ');
2028 col++;
2030 done:
2031 free(logmsg0);
2032 free(wlogmsg);
2033 free(author);
2034 free(wauthor);
2035 free(line);
2036 return err;
2039 static struct commit_queue_entry *
2040 alloc_commit_queue_entry(struct got_commit_object *commit,
2041 struct got_object_id *id)
2043 struct commit_queue_entry *entry;
2045 entry = calloc(1, sizeof(*entry));
2046 if (entry == NULL)
2047 return NULL;
2049 entry->id = id;
2050 entry->commit = commit;
2051 return entry;
2054 static void
2055 pop_commit(struct commit_queue *commits)
2057 struct commit_queue_entry *entry;
2059 entry = TAILQ_FIRST(&commits->head);
2060 TAILQ_REMOVE(&commits->head, entry, entry);
2061 got_object_commit_close(entry->commit);
2062 commits->ncommits--;
2063 /* Don't free entry->id! It is owned by the commit graph. */
2064 free(entry);
2067 static void
2068 free_commits(struct commit_queue *commits)
2070 while (!TAILQ_EMPTY(&commits->head))
2071 pop_commit(commits);
2074 static const struct got_error *
2075 match_commit(int *have_match, struct got_object_id *id,
2076 struct got_commit_object *commit, regex_t *regex)
2078 const struct got_error *err = NULL;
2079 regmatch_t regmatch;
2080 char *id_str = NULL, *logmsg = NULL;
2082 *have_match = 0;
2084 err = got_object_id_str(&id_str, id);
2085 if (err)
2086 return err;
2088 err = got_object_commit_get_logmsg(&logmsg, commit);
2089 if (err)
2090 goto done;
2092 if (regexec(regex, got_object_commit_get_author(commit), 1,
2093 &regmatch, 0) == 0 ||
2094 regexec(regex, got_object_commit_get_committer(commit), 1,
2095 &regmatch, 0) == 0 ||
2096 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2097 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2098 *have_match = 1;
2099 done:
2100 free(id_str);
2101 free(logmsg);
2102 return err;
2105 static const struct got_error *
2106 queue_commits(struct tog_log_thread_args *a)
2108 const struct got_error *err = NULL;
2111 * We keep all commits open throughout the lifetime of the log
2112 * view in order to avoid having to re-fetch commits from disk
2113 * while updating the display.
2115 do {
2116 struct got_object_id *id;
2117 struct got_commit_object *commit;
2118 struct commit_queue_entry *entry;
2119 int errcode;
2121 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2122 NULL, NULL);
2123 if (err || id == NULL)
2124 break;
2126 err = got_object_open_as_commit(&commit, a->repo, id);
2127 if (err)
2128 break;
2129 entry = alloc_commit_queue_entry(commit, id);
2130 if (entry == NULL) {
2131 err = got_error_from_errno("alloc_commit_queue_entry");
2132 break;
2135 errcode = pthread_mutex_lock(&tog_mutex);
2136 if (errcode) {
2137 err = got_error_set_errno(errcode,
2138 "pthread_mutex_lock");
2139 break;
2142 entry->idx = a->commits->ncommits;
2143 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2144 a->commits->ncommits++;
2146 if (*a->searching == TOG_SEARCH_FORWARD &&
2147 !*a->search_next_done) {
2148 int have_match;
2149 err = match_commit(&have_match, id, commit, a->regex);
2150 if (err)
2151 break;
2152 if (have_match)
2153 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2156 errcode = pthread_mutex_unlock(&tog_mutex);
2157 if (errcode && err == NULL)
2158 err = got_error_set_errno(errcode,
2159 "pthread_mutex_unlock");
2160 if (err)
2161 break;
2162 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2164 return err;
2167 static void
2168 select_commit(struct tog_log_view_state *s)
2170 struct commit_queue_entry *entry;
2171 int ncommits = 0;
2173 entry = s->first_displayed_entry;
2174 while (entry) {
2175 if (ncommits == s->selected) {
2176 s->selected_entry = entry;
2177 break;
2179 entry = TAILQ_NEXT(entry, entry);
2180 ncommits++;
2184 static const struct got_error *
2185 draw_commits(struct tog_view *view)
2187 const struct got_error *err = NULL;
2188 struct tog_log_view_state *s = &view->state.log;
2189 struct commit_queue_entry *entry = s->selected_entry;
2190 const int limit = view->nlines;
2191 int width;
2192 int ncommits, author_cols = 4;
2193 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2194 char *refs_str = NULL;
2195 wchar_t *wline;
2196 struct tog_color *tc;
2197 static const size_t date_display_cols = 12;
2199 if (s->selected_entry &&
2200 !(view->searching && view->search_next_done == 0)) {
2201 struct got_reflist_head *refs;
2202 err = got_object_id_str(&id_str, s->selected_entry->id);
2203 if (err)
2204 return err;
2205 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2206 s->selected_entry->id);
2207 if (refs) {
2208 err = build_refs_str(&refs_str, refs,
2209 s->selected_entry->id, s->repo);
2210 if (err)
2211 goto done;
2215 if (s->thread_args.commits_needed == 0)
2216 halfdelay(10); /* disable fast refresh */
2218 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2219 if (asprintf(&ncommits_str, " [%d/%d] %s",
2220 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2221 (view->searching && !view->search_next_done) ?
2222 "searching..." : "loading...") == -1) {
2223 err = got_error_from_errno("asprintf");
2224 goto done;
2226 } else {
2227 const char *search_str = NULL;
2229 if (view->searching) {
2230 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2231 search_str = "no more matches";
2232 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2233 search_str = "no matches found";
2234 else if (!view->search_next_done)
2235 search_str = "searching...";
2238 if (asprintf(&ncommits_str, " [%d/%d] %s",
2239 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2240 search_str ? search_str :
2241 (refs_str ? refs_str : "")) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 goto done;
2247 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2248 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2249 "........................................",
2250 s->in_repo_path, ncommits_str) == -1) {
2251 err = got_error_from_errno("asprintf");
2252 header = NULL;
2253 goto done;
2255 } else if (asprintf(&header, "commit %s%s",
2256 id_str ? id_str : "........................................",
2257 ncommits_str) == -1) {
2258 err = got_error_from_errno("asprintf");
2259 header = NULL;
2260 goto done;
2262 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2263 if (err)
2264 goto done;
2266 werase(view->window);
2268 if (view_needs_focus_indication(view))
2269 wstandout(view->window);
2270 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2271 if (tc)
2272 wattr_on(view->window,
2273 COLOR_PAIR(tc->colorpair), NULL);
2274 waddwstr(view->window, wline);
2275 if (tc)
2276 wattr_off(view->window,
2277 COLOR_PAIR(tc->colorpair), NULL);
2278 while (width < view->ncols) {
2279 waddch(view->window, ' ');
2280 width++;
2282 if (view_needs_focus_indication(view))
2283 wstandend(view->window);
2284 free(wline);
2285 if (limit <= 1)
2286 goto done;
2288 /* Grow author column size if necessary, and set view->maxx. */
2289 entry = s->first_displayed_entry;
2290 ncommits = 0;
2291 view->maxx = 0;
2292 while (entry) {
2293 struct got_commit_object *c = entry->commit;
2294 char *author, *eol, *msg, *msg0;
2295 wchar_t *wauthor, *wmsg;
2296 int width;
2297 if (ncommits >= limit - 1)
2298 break;
2299 if (s->use_committer)
2300 author = strdup(got_object_commit_get_committer(c));
2301 else
2302 author = strdup(got_object_commit_get_author(c));
2303 if (author == NULL) {
2304 err = got_error_from_errno("strdup");
2305 goto done;
2307 err = format_author(&wauthor, &width, author, COLS,
2308 date_display_cols);
2309 if (author_cols < width)
2310 author_cols = width;
2311 free(wauthor);
2312 free(author);
2313 if (err)
2314 goto done;
2315 err = got_object_commit_get_logmsg(&msg0, c);
2316 if (err)
2317 goto done;
2318 msg = msg0;
2319 while (*msg == '\n')
2320 ++msg;
2321 if ((eol = strchr(msg, '\n')))
2322 *eol = '\0';
2323 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2324 date_display_cols + author_cols, 0);
2325 if (err)
2326 goto done;
2327 view->maxx = MAX(view->maxx, width);
2328 free(msg0);
2329 free(wmsg);
2330 ncommits++;
2331 entry = TAILQ_NEXT(entry, entry);
2334 entry = s->first_displayed_entry;
2335 s->last_displayed_entry = s->first_displayed_entry;
2336 ncommits = 0;
2337 while (entry) {
2338 if (ncommits >= limit - 1)
2339 break;
2340 if (ncommits == s->selected)
2341 wstandout(view->window);
2342 err = draw_commit(view, entry->commit, entry->id,
2343 date_display_cols, author_cols);
2344 if (ncommits == s->selected)
2345 wstandend(view->window);
2346 if (err)
2347 goto done;
2348 ncommits++;
2349 s->last_displayed_entry = entry;
2350 entry = TAILQ_NEXT(entry, entry);
2353 view_border(view);
2354 done:
2355 free(id_str);
2356 free(refs_str);
2357 free(ncommits_str);
2358 free(header);
2359 return err;
2362 static void
2363 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2365 struct commit_queue_entry *entry;
2366 int nscrolled = 0;
2368 entry = TAILQ_FIRST(&s->commits.head);
2369 if (s->first_displayed_entry == entry)
2370 return;
2372 entry = s->first_displayed_entry;
2373 while (entry && nscrolled < maxscroll) {
2374 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2375 if (entry) {
2376 s->first_displayed_entry = entry;
2377 nscrolled++;
2382 static const struct got_error *
2383 trigger_log_thread(struct tog_view *view, int wait)
2385 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2386 int errcode;
2388 halfdelay(1); /* fast refresh while loading commits */
2390 while (!ta->log_complete && !tog_thread_error &&
2391 (ta->commits_needed > 0 || ta->load_all)) {
2392 /* Wake the log thread. */
2393 errcode = pthread_cond_signal(&ta->need_commits);
2394 if (errcode)
2395 return got_error_set_errno(errcode,
2396 "pthread_cond_signal");
2399 * The mutex will be released while the view loop waits
2400 * in wgetch(), at which time the log thread will run.
2402 if (!wait)
2403 break;
2405 /* Display progress update in log view. */
2406 show_log_view(view);
2407 update_panels();
2408 doupdate();
2410 /* Wait right here while next commit is being loaded. */
2411 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2412 if (errcode)
2413 return got_error_set_errno(errcode,
2414 "pthread_cond_wait");
2416 /* Display progress update in log view. */
2417 show_log_view(view);
2418 update_panels();
2419 doupdate();
2422 return NULL;
2425 static const struct got_error *
2426 request_log_commits(struct tog_view *view)
2428 struct tog_log_view_state *state = &view->state.log;
2429 const struct got_error *err = NULL;
2431 if (state->thread_args.log_complete)
2432 return NULL;
2434 state->thread_args.commits_needed += view->nscrolled;
2435 err = trigger_log_thread(view, 1);
2436 view->nscrolled = 0;
2438 return err;
2441 static const struct got_error *
2442 log_scroll_down(struct tog_view *view, int maxscroll)
2444 struct tog_log_view_state *s = &view->state.log;
2445 const struct got_error *err = NULL;
2446 struct commit_queue_entry *pentry;
2447 int nscrolled = 0, ncommits_needed;
2449 if (s->last_displayed_entry == NULL)
2450 return NULL;
2452 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2453 if (s->commits.ncommits < ncommits_needed &&
2454 !s->thread_args.log_complete) {
2456 * Ask the log thread for required amount of commits.
2458 s->thread_args.commits_needed += maxscroll;
2459 err = trigger_log_thread(view, 1);
2460 if (err)
2461 return err;
2464 do {
2465 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2466 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2467 break;
2469 s->last_displayed_entry = pentry ?
2470 pentry : s->last_displayed_entry;;
2472 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2473 if (pentry == NULL)
2474 break;
2475 s->first_displayed_entry = pentry;
2476 } while (++nscrolled < maxscroll);
2478 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2479 view->nscrolled += nscrolled;
2480 else
2481 view->nscrolled = 0;
2483 return err;
2486 static const struct got_error *
2487 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2488 struct got_commit_object *commit, struct got_object_id *commit_id,
2489 struct tog_view *log_view, struct got_repository *repo)
2491 const struct got_error *err;
2492 struct got_object_qid *parent_id;
2493 struct tog_view *diff_view;
2495 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2496 if (diff_view == NULL)
2497 return got_error_from_errno("view_open");
2499 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2500 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2501 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2502 if (err == NULL)
2503 *new_view = diff_view;
2504 return err;
2507 static const struct got_error *
2508 tree_view_visit_subtree(struct tog_tree_view_state *s,
2509 struct got_tree_object *subtree)
2511 struct tog_parent_tree *parent;
2513 parent = calloc(1, sizeof(*parent));
2514 if (parent == NULL)
2515 return got_error_from_errno("calloc");
2517 parent->tree = s->tree;
2518 parent->first_displayed_entry = s->first_displayed_entry;
2519 parent->selected_entry = s->selected_entry;
2520 parent->selected = s->selected;
2521 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2522 s->tree = subtree;
2523 s->selected = 0;
2524 s->first_displayed_entry = NULL;
2525 return NULL;
2528 static const struct got_error *
2529 tree_view_walk_path(struct tog_tree_view_state *s,
2530 struct got_commit_object *commit, const char *path)
2532 const struct got_error *err = NULL;
2533 struct got_tree_object *tree = NULL;
2534 const char *p;
2535 char *slash, *subpath = NULL;
2537 /* Walk the path and open corresponding tree objects. */
2538 p = path;
2539 while (*p) {
2540 struct got_tree_entry *te;
2541 struct got_object_id *tree_id;
2542 char *te_name;
2544 while (p[0] == '/')
2545 p++;
2547 /* Ensure the correct subtree entry is selected. */
2548 slash = strchr(p, '/');
2549 if (slash == NULL)
2550 te_name = strdup(p);
2551 else
2552 te_name = strndup(p, slash - p);
2553 if (te_name == NULL) {
2554 err = got_error_from_errno("strndup");
2555 break;
2557 te = got_object_tree_find_entry(s->tree, te_name);
2558 if (te == NULL) {
2559 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2560 free(te_name);
2561 break;
2563 free(te_name);
2564 s->first_displayed_entry = s->selected_entry = te;
2566 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2567 break; /* jump to this file's entry */
2569 slash = strchr(p, '/');
2570 if (slash)
2571 subpath = strndup(path, slash - path);
2572 else
2573 subpath = strdup(path);
2574 if (subpath == NULL) {
2575 err = got_error_from_errno("strdup");
2576 break;
2579 err = got_object_id_by_path(&tree_id, s->repo, commit,
2580 subpath);
2581 if (err)
2582 break;
2584 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2585 free(tree_id);
2586 if (err)
2587 break;
2589 err = tree_view_visit_subtree(s, tree);
2590 if (err) {
2591 got_object_tree_close(tree);
2592 break;
2594 if (slash == NULL)
2595 break;
2596 free(subpath);
2597 subpath = NULL;
2598 p = slash;
2601 free(subpath);
2602 return err;
2605 static const struct got_error *
2606 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2607 struct commit_queue_entry *entry, const char *path,
2608 const char *head_ref_name, struct got_repository *repo)
2610 const struct got_error *err = NULL;
2611 struct tog_tree_view_state *s;
2612 struct tog_view *tree_view;
2614 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2615 if (tree_view == NULL)
2616 return got_error_from_errno("view_open");
2618 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2619 if (err)
2620 return err;
2621 s = &tree_view->state.tree;
2623 *new_view = tree_view;
2625 if (got_path_is_root_dir(path))
2626 return NULL;
2628 return tree_view_walk_path(s, entry->commit, path);
2631 static const struct got_error *
2632 block_signals_used_by_main_thread(void)
2634 sigset_t sigset;
2635 int errcode;
2637 if (sigemptyset(&sigset) == -1)
2638 return got_error_from_errno("sigemptyset");
2640 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2641 if (sigaddset(&sigset, SIGWINCH) == -1)
2642 return got_error_from_errno("sigaddset");
2643 if (sigaddset(&sigset, SIGCONT) == -1)
2644 return got_error_from_errno("sigaddset");
2645 if (sigaddset(&sigset, SIGINT) == -1)
2646 return got_error_from_errno("sigaddset");
2647 if (sigaddset(&sigset, SIGTERM) == -1)
2648 return got_error_from_errno("sigaddset");
2650 /* ncurses handles SIGTSTP */
2651 if (sigaddset(&sigset, SIGTSTP) == -1)
2652 return got_error_from_errno("sigaddset");
2654 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2655 if (errcode)
2656 return got_error_set_errno(errcode, "pthread_sigmask");
2658 return NULL;
2661 static void *
2662 log_thread(void *arg)
2664 const struct got_error *err = NULL;
2665 int errcode = 0;
2666 struct tog_log_thread_args *a = arg;
2667 int done = 0;
2670 * Sync startup with main thread such that we begin our
2671 * work once view_input() has released the mutex.
2673 errcode = pthread_mutex_lock(&tog_mutex);
2674 if (errcode) {
2675 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2676 return (void *)err;
2679 err = block_signals_used_by_main_thread();
2680 if (err) {
2681 pthread_mutex_unlock(&tog_mutex);
2682 goto done;
2685 while (!done && !err && !tog_fatal_signal_received()) {
2686 errcode = pthread_mutex_unlock(&tog_mutex);
2687 if (errcode) {
2688 err = got_error_set_errno(errcode,
2689 "pthread_mutex_unlock");
2690 goto done;
2692 err = queue_commits(a);
2693 if (err) {
2694 if (err->code != GOT_ERR_ITER_COMPLETED)
2695 goto done;
2696 err = NULL;
2697 done = 1;
2698 } else if (a->commits_needed > 0 && !a->load_all)
2699 a->commits_needed--;
2701 errcode = pthread_mutex_lock(&tog_mutex);
2702 if (errcode) {
2703 err = got_error_set_errno(errcode,
2704 "pthread_mutex_lock");
2705 goto done;
2706 } else if (*a->quit)
2707 done = 1;
2708 else if (*a->first_displayed_entry == NULL) {
2709 *a->first_displayed_entry =
2710 TAILQ_FIRST(&a->commits->head);
2711 *a->selected_entry = *a->first_displayed_entry;
2714 errcode = pthread_cond_signal(&a->commit_loaded);
2715 if (errcode) {
2716 err = got_error_set_errno(errcode,
2717 "pthread_cond_signal");
2718 pthread_mutex_unlock(&tog_mutex);
2719 goto done;
2722 if (done)
2723 a->commits_needed = 0;
2724 else {
2725 if (a->commits_needed == 0 && !a->load_all) {
2726 errcode = pthread_cond_wait(&a->need_commits,
2727 &tog_mutex);
2728 if (errcode) {
2729 err = got_error_set_errno(errcode,
2730 "pthread_cond_wait");
2731 pthread_mutex_unlock(&tog_mutex);
2732 goto done;
2734 if (*a->quit)
2735 done = 1;
2739 a->log_complete = 1;
2740 errcode = pthread_mutex_unlock(&tog_mutex);
2741 if (errcode)
2742 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2743 done:
2744 if (err) {
2745 tog_thread_error = 1;
2746 pthread_cond_signal(&a->commit_loaded);
2748 return (void *)err;
2751 static const struct got_error *
2752 stop_log_thread(struct tog_log_view_state *s)
2754 const struct got_error *err = NULL, *thread_err = NULL;
2755 int errcode;
2757 if (s->thread) {
2758 s->quit = 1;
2759 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2760 if (errcode)
2761 return got_error_set_errno(errcode,
2762 "pthread_cond_signal");
2763 errcode = pthread_mutex_unlock(&tog_mutex);
2764 if (errcode)
2765 return got_error_set_errno(errcode,
2766 "pthread_mutex_unlock");
2767 errcode = pthread_join(s->thread, (void **)&thread_err);
2768 if (errcode)
2769 return got_error_set_errno(errcode, "pthread_join");
2770 errcode = pthread_mutex_lock(&tog_mutex);
2771 if (errcode)
2772 return got_error_set_errno(errcode,
2773 "pthread_mutex_lock");
2774 s->thread = 0; //NULL;
2777 if (s->thread_args.repo) {
2778 err = got_repo_close(s->thread_args.repo);
2779 s->thread_args.repo = NULL;
2782 if (s->thread_args.pack_fds) {
2783 const struct got_error *pack_err =
2784 got_repo_pack_fds_close(s->thread_args.pack_fds);
2785 if (err == NULL)
2786 err = pack_err;
2787 s->thread_args.pack_fds = NULL;
2790 if (s->thread_args.graph) {
2791 got_commit_graph_close(s->thread_args.graph);
2792 s->thread_args.graph = NULL;
2795 return err ? err : thread_err;
2798 static const struct got_error *
2799 close_log_view(struct tog_view *view)
2801 const struct got_error *err = NULL;
2802 struct tog_log_view_state *s = &view->state.log;
2803 int errcode;
2805 err = stop_log_thread(s);
2807 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2808 if (errcode && err == NULL)
2809 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2811 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2812 if (errcode && err == NULL)
2813 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2815 free_commits(&s->commits);
2816 free(s->in_repo_path);
2817 s->in_repo_path = NULL;
2818 free(s->start_id);
2819 s->start_id = NULL;
2820 free(s->head_ref_name);
2821 s->head_ref_name = NULL;
2822 return err;
2825 static const struct got_error *
2826 search_start_log_view(struct tog_view *view)
2828 struct tog_log_view_state *s = &view->state.log;
2830 s->matched_entry = NULL;
2831 s->search_entry = NULL;
2832 return NULL;
2835 static const struct got_error *
2836 search_next_log_view(struct tog_view *view)
2838 const struct got_error *err = NULL;
2839 struct tog_log_view_state *s = &view->state.log;
2840 struct commit_queue_entry *entry;
2842 /* Display progress update in log view. */
2843 show_log_view(view);
2844 update_panels();
2845 doupdate();
2847 if (s->search_entry) {
2848 int errcode, ch;
2849 errcode = pthread_mutex_unlock(&tog_mutex);
2850 if (errcode)
2851 return got_error_set_errno(errcode,
2852 "pthread_mutex_unlock");
2853 ch = wgetch(view->window);
2854 errcode = pthread_mutex_lock(&tog_mutex);
2855 if (errcode)
2856 return got_error_set_errno(errcode,
2857 "pthread_mutex_lock");
2858 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2859 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2860 return NULL;
2862 if (view->searching == TOG_SEARCH_FORWARD)
2863 entry = TAILQ_NEXT(s->search_entry, entry);
2864 else
2865 entry = TAILQ_PREV(s->search_entry,
2866 commit_queue_head, entry);
2867 } else if (s->matched_entry) {
2868 int matched_idx = s->matched_entry->idx;
2869 int selected_idx = s->selected_entry->idx;
2872 * If the user has moved the cursor after we hit a match,
2873 * the position from where we should continue searching
2874 * might have changed.
2876 if (view->searching == TOG_SEARCH_FORWARD) {
2877 if (matched_idx > selected_idx)
2878 entry = TAILQ_NEXT(s->selected_entry, entry);
2879 else
2880 entry = TAILQ_NEXT(s->matched_entry, entry);
2881 } else {
2882 if (matched_idx < selected_idx)
2883 entry = TAILQ_PREV(s->selected_entry,
2884 commit_queue_head, entry);
2885 else
2886 entry = TAILQ_PREV(s->matched_entry,
2887 commit_queue_head, entry);
2889 } else {
2890 entry = s->selected_entry;
2893 while (1) {
2894 int have_match = 0;
2896 if (entry == NULL) {
2897 if (s->thread_args.log_complete ||
2898 view->searching == TOG_SEARCH_BACKWARD) {
2899 view->search_next_done =
2900 (s->matched_entry == NULL ?
2901 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2902 s->search_entry = NULL;
2903 return NULL;
2906 * Poke the log thread for more commits and return,
2907 * allowing the main loop to make progress. Search
2908 * will resume at s->search_entry once we come back.
2910 s->thread_args.commits_needed++;
2911 return trigger_log_thread(view, 0);
2914 err = match_commit(&have_match, entry->id, entry->commit,
2915 &view->regex);
2916 if (err)
2917 break;
2918 if (have_match) {
2919 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2920 s->matched_entry = entry;
2921 break;
2924 s->search_entry = entry;
2925 if (view->searching == TOG_SEARCH_FORWARD)
2926 entry = TAILQ_NEXT(entry, entry);
2927 else
2928 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2931 if (s->matched_entry) {
2932 int cur = s->selected_entry->idx;
2933 while (cur < s->matched_entry->idx) {
2934 err = input_log_view(NULL, view, KEY_DOWN);
2935 if (err)
2936 return err;
2937 cur++;
2939 while (cur > s->matched_entry->idx) {
2940 err = input_log_view(NULL, view, KEY_UP);
2941 if (err)
2942 return err;
2943 cur--;
2947 s->search_entry = NULL;
2949 return NULL;
2952 static const struct got_error *
2953 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2954 struct got_repository *repo, const char *head_ref_name,
2955 const char *in_repo_path, int log_branches)
2957 const struct got_error *err = NULL;
2958 struct tog_log_view_state *s = &view->state.log;
2959 struct got_repository *thread_repo = NULL;
2960 struct got_commit_graph *thread_graph = NULL;
2961 int errcode;
2963 if (in_repo_path != s->in_repo_path) {
2964 free(s->in_repo_path);
2965 s->in_repo_path = strdup(in_repo_path);
2966 if (s->in_repo_path == NULL)
2967 return got_error_from_errno("strdup");
2970 /* The commit queue only contains commits being displayed. */
2971 TAILQ_INIT(&s->commits.head);
2972 s->commits.ncommits = 0;
2974 s->repo = repo;
2975 if (head_ref_name) {
2976 s->head_ref_name = strdup(head_ref_name);
2977 if (s->head_ref_name == NULL) {
2978 err = got_error_from_errno("strdup");
2979 goto done;
2982 s->start_id = got_object_id_dup(start_id);
2983 if (s->start_id == NULL) {
2984 err = got_error_from_errno("got_object_id_dup");
2985 goto done;
2987 s->log_branches = log_branches;
2989 STAILQ_INIT(&s->colors);
2990 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2991 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2992 get_color_value("TOG_COLOR_COMMIT"));
2993 if (err)
2994 goto done;
2995 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2996 get_color_value("TOG_COLOR_AUTHOR"));
2997 if (err) {
2998 free_colors(&s->colors);
2999 goto done;
3001 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3002 get_color_value("TOG_COLOR_DATE"));
3003 if (err) {
3004 free_colors(&s->colors);
3005 goto done;
3009 view->show = show_log_view;
3010 view->input = input_log_view;
3011 view->resize = resize_log_view;
3012 view->close = close_log_view;
3013 view->search_start = search_start_log_view;
3014 view->search_next = search_next_log_view;
3016 if (s->thread_args.pack_fds == NULL) {
3017 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3018 if (err)
3019 goto done;
3021 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3022 s->thread_args.pack_fds);
3023 if (err)
3024 goto done;
3025 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3026 !s->log_branches);
3027 if (err)
3028 goto done;
3029 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3030 s->repo, NULL, NULL);
3031 if (err)
3032 goto done;
3034 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3035 if (errcode) {
3036 err = got_error_set_errno(errcode, "pthread_cond_init");
3037 goto done;
3039 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3040 if (errcode) {
3041 err = got_error_set_errno(errcode, "pthread_cond_init");
3042 goto done;
3045 s->thread_args.commits_needed = view->nlines;
3046 s->thread_args.graph = thread_graph;
3047 s->thread_args.commits = &s->commits;
3048 s->thread_args.in_repo_path = s->in_repo_path;
3049 s->thread_args.start_id = s->start_id;
3050 s->thread_args.repo = thread_repo;
3051 s->thread_args.log_complete = 0;
3052 s->thread_args.quit = &s->quit;
3053 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3054 s->thread_args.selected_entry = &s->selected_entry;
3055 s->thread_args.searching = &view->searching;
3056 s->thread_args.search_next_done = &view->search_next_done;
3057 s->thread_args.regex = &view->regex;
3058 done:
3059 if (err)
3060 close_log_view(view);
3061 return err;
3064 static const struct got_error *
3065 show_log_view(struct tog_view *view)
3067 const struct got_error *err;
3068 struct tog_log_view_state *s = &view->state.log;
3070 if (s->thread == 0) { //NULL) {
3071 int errcode = pthread_create(&s->thread, NULL, log_thread,
3072 &s->thread_args);
3073 if (errcode)
3074 return got_error_set_errno(errcode, "pthread_create");
3075 if (s->thread_args.commits_needed > 0) {
3076 err = trigger_log_thread(view, 1);
3077 if (err)
3078 return err;
3082 return draw_commits(view);
3085 static void
3086 log_move_cursor_up(struct tog_view *view, int page, int home)
3088 struct tog_log_view_state *s = &view->state.log;
3090 if (s->selected_entry->idx == 0)
3091 view->count = 0;
3092 if (s->first_displayed_entry == NULL)
3093 return;
3095 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3096 || home)
3097 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3099 if (!page && !home && s->selected > 0)
3100 --s->selected;
3101 else
3102 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3104 select_commit(s);
3105 return;
3108 static const struct got_error *
3109 log_move_cursor_down(struct tog_view *view, int page)
3111 struct tog_log_view_state *s = &view->state.log;
3112 struct commit_queue_entry *first;
3113 const struct got_error *err = NULL;
3115 first = s->first_displayed_entry;
3116 if (first == NULL) {
3117 view->count = 0;
3118 return NULL;
3121 if (s->thread_args.log_complete &&
3122 s->selected_entry->idx >= s->commits.ncommits - 1)
3123 return NULL;
3125 if (!page) {
3126 int eos = view->nlines - 2;
3128 if (view_is_hsplit_top(view))
3129 --eos; /* border consumes the last line */
3130 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3131 ++s->selected;
3132 else
3133 err = log_scroll_down(view, 1);
3134 } else if (s->thread_args.load_all) {
3135 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3136 s->selected += MIN(s->last_displayed_entry->idx -
3137 s->selected_entry->idx, page + 1);
3138 else
3139 err = log_scroll_down(view, MIN(page,
3140 s->commits.ncommits - s->selected_entry->idx - 1));
3141 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3142 } else {
3143 err = log_scroll_down(view, page);
3144 if (err)
3145 return err;
3146 if (first == s->first_displayed_entry && s->selected <
3147 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3148 s->selected = MIN(s->commits.ncommits - 1, page);
3151 if (err)
3152 return err;
3155 * We might necessarily overshoot in horizontal
3156 * splits; if so, select the last displayed commit.
3158 s->selected = MIN(s->selected,
3159 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3161 select_commit(s);
3163 if (s->thread_args.log_complete &&
3164 s->selected_entry->idx == s->commits.ncommits - 1)
3165 view->count = 0;
3167 return NULL;
3170 static void
3171 view_get_split(struct tog_view *view, int *y, int *x)
3173 *x = 0;
3174 *y = 0;
3176 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3177 if (view->child && view->child->resized_y)
3178 *y = view->child->resized_y;
3179 else if (view->resized_y)
3180 *y = view->resized_y;
3181 else
3182 *y = view_split_begin_y(view->lines);
3183 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3184 if (view->child && view->child->resized_x)
3185 *x = view->child->resized_x;
3186 else if (view->resized_x)
3187 *x = view->resized_x;
3188 else
3189 *x = view_split_begin_x(view->begin_x);
3193 /* Split view horizontally at y and offset view->state->selected line. */
3194 static const struct got_error *
3195 view_init_hsplit(struct tog_view *view, int y)
3197 const struct got_error *err = NULL;
3199 view->nlines = y;
3200 view->ncols = COLS;
3201 err = view_resize(view);
3202 if (err)
3203 return err;
3205 err = offset_selection_down(view);
3207 return err;
3210 static const struct got_error *
3211 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3213 const struct got_error *err = NULL;
3214 struct tog_log_view_state *s = &view->state.log;
3215 struct tog_view *diff_view = NULL, *tree_view = NULL;
3216 struct tog_view *ref_view = NULL;
3217 struct commit_queue_entry *entry;
3218 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3220 if (s->thread_args.load_all) {
3221 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3222 s->thread_args.load_all = 0;
3223 else if (s->thread_args.log_complete) {
3224 err = log_move_cursor_down(view, s->commits.ncommits);
3225 s->thread_args.load_all = 0;
3227 return err;
3230 eos = nscroll = view->nlines - 1;
3231 if (view_is_hsplit_top(view))
3232 --eos; /* border */
3234 switch (ch) {
3235 case 'q':
3236 s->quit = 1;
3237 break;
3238 case '0':
3239 view->x = 0;
3240 break;
3241 case '$':
3242 view->x = MAX(view->maxx - view->ncols / 2, 0);
3243 view->count = 0;
3244 break;
3245 case KEY_RIGHT:
3246 case 'l':
3247 if (view->x + view->ncols / 2 < view->maxx)
3248 view->x += 2; /* move two columns right */
3249 else
3250 view->count = 0;
3251 break;
3252 case KEY_LEFT:
3253 case 'h':
3254 view->x -= MIN(view->x, 2); /* move two columns back */
3255 if (view->x <= 0)
3256 view->count = 0;
3257 break;
3258 case 'k':
3259 case KEY_UP:
3260 case '<':
3261 case ',':
3262 case CTRL('p'):
3263 log_move_cursor_up(view, 0, 0);
3264 break;
3265 case 'g':
3266 case KEY_HOME:
3267 log_move_cursor_up(view, 0, 1);
3268 view->count = 0;
3269 break;
3270 case CTRL('u'):
3271 case 'u':
3272 nscroll /= 2;
3273 /* FALL THROUGH */
3274 case KEY_PPAGE:
3275 case CTRL('b'):
3276 case 'b':
3277 log_move_cursor_up(view, nscroll, 0);
3278 break;
3279 case 'j':
3280 case KEY_DOWN:
3281 case '>':
3282 case '.':
3283 case CTRL('n'):
3284 err = log_move_cursor_down(view, 0);
3285 break;
3286 case '@':
3287 s->use_committer = !s->use_committer;
3288 break;
3289 case 'G':
3290 case KEY_END: {
3291 /* We don't know yet how many commits, so we're forced to
3292 * traverse them all. */
3293 view->count = 0;
3294 if (!s->thread_args.log_complete) {
3295 s->thread_args.load_all = 1;
3296 return trigger_log_thread(view, 0);
3299 s->selected = 0;
3300 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3301 for (n = 0; n < eos; n++) {
3302 if (entry == NULL)
3303 break;
3304 s->first_displayed_entry = entry;
3305 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3307 if (n > 0)
3308 s->selected = n - 1;
3309 select_commit(s);
3310 break;
3312 case CTRL('d'):
3313 case 'd':
3314 nscroll /= 2;
3315 /* FALL THROUGH */
3316 case KEY_NPAGE:
3317 case CTRL('f'):
3318 case 'f':
3319 case ' ':
3320 err = log_move_cursor_down(view, nscroll);
3321 break;
3322 case KEY_RESIZE:
3323 if (s->selected > view->nlines - 2)
3324 s->selected = view->nlines - 2;
3325 if (s->selected > s->commits.ncommits - 1)
3326 s->selected = s->commits.ncommits - 1;
3327 select_commit(s);
3328 if (s->commits.ncommits < view->nlines - 1 &&
3329 !s->thread_args.log_complete) {
3330 s->thread_args.commits_needed += (view->nlines - 1) -
3331 s->commits.ncommits;
3332 err = trigger_log_thread(view, 1);
3334 break;
3335 case KEY_ENTER:
3336 case '\r':
3337 view->count = 0;
3338 if (s->selected_entry == NULL)
3339 break;
3341 /* get dimensions--don't split till initialisation succeeds */
3342 if (view_is_parent_view(view))
3343 view_get_split(view, &begin_y, &begin_x);
3345 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3346 s->selected_entry->commit, s->selected_entry->id,
3347 view, s->repo);
3348 if (err)
3349 break;
3351 if (view_is_parent_view(view) &&
3352 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3353 err = view_init_hsplit(view, begin_y);
3354 if (err)
3355 break;
3358 view->focussed = 0;
3359 diff_view->focussed = 1;
3360 diff_view->mode = view->mode;
3361 diff_view->nlines = view->lines - begin_y;
3363 if (view_is_parent_view(view)) {
3364 view_transfer_size(diff_view, view);
3365 err = view_close_child(view);
3366 if (err)
3367 return err;
3368 err = view_set_child(view, diff_view);
3369 if (err)
3370 return err;
3371 view->focus_child = 1;
3372 } else
3373 *new_view = diff_view;
3374 break;
3375 case 't':
3376 view->count = 0;
3377 if (s->selected_entry == NULL)
3378 break;
3379 if (view_is_parent_view(view))
3380 view_get_split(view, &begin_y, &begin_x);
3381 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3382 s->selected_entry, s->in_repo_path, s->head_ref_name,
3383 s->repo);
3384 if (err)
3385 break;
3386 if (view_is_parent_view(view) &&
3387 view->mode == TOG_VIEW_SPLIT_HRZN) {
3388 err = view_init_hsplit(view, begin_y);
3389 if (err)
3390 break;
3392 view->focussed = 0;
3393 tree_view->focussed = 1;
3394 tree_view->mode = view->mode;
3395 tree_view->nlines = view->lines - begin_y;
3396 if (view_is_parent_view(view)) {
3397 view_transfer_size(tree_view, view);
3398 err = view_close_child(view);
3399 if (err)
3400 return err;
3401 err = view_set_child(view, tree_view);
3402 if (err)
3403 return err;
3404 view->focus_child = 1;
3405 } else
3406 *new_view = tree_view;
3407 break;
3408 case KEY_BACKSPACE:
3409 case CTRL('l'):
3410 case 'B':
3411 view->count = 0;
3412 if (ch == KEY_BACKSPACE &&
3413 got_path_is_root_dir(s->in_repo_path))
3414 break;
3415 err = stop_log_thread(s);
3416 if (err)
3417 return err;
3418 if (ch == KEY_BACKSPACE) {
3419 char *parent_path;
3420 err = got_path_dirname(&parent_path, s->in_repo_path);
3421 if (err)
3422 return err;
3423 free(s->in_repo_path);
3424 s->in_repo_path = parent_path;
3425 s->thread_args.in_repo_path = s->in_repo_path;
3426 } else if (ch == CTRL('l')) {
3427 struct got_object_id *start_id;
3428 err = got_repo_match_object_id(&start_id, NULL,
3429 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3430 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3431 if (err)
3432 return err;
3433 free(s->start_id);
3434 s->start_id = start_id;
3435 s->thread_args.start_id = s->start_id;
3436 } else /* 'B' */
3437 s->log_branches = !s->log_branches;
3439 if (s->thread_args.pack_fds == NULL) {
3440 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3441 if (err)
3442 return err;
3444 err = got_repo_open(&s->thread_args.repo,
3445 got_repo_get_path(s->repo), NULL,
3446 s->thread_args.pack_fds);
3447 if (err)
3448 return err;
3449 tog_free_refs();
3450 err = tog_load_refs(s->repo, 0);
3451 if (err)
3452 return err;
3453 err = got_commit_graph_open(&s->thread_args.graph,
3454 s->in_repo_path, !s->log_branches);
3455 if (err)
3456 return err;
3457 err = got_commit_graph_iter_start(s->thread_args.graph,
3458 s->start_id, s->repo, NULL, NULL);
3459 if (err)
3460 return err;
3461 free_commits(&s->commits);
3462 s->first_displayed_entry = NULL;
3463 s->last_displayed_entry = NULL;
3464 s->selected_entry = NULL;
3465 s->selected = 0;
3466 s->thread_args.log_complete = 0;
3467 s->quit = 0;
3468 s->thread_args.commits_needed = view->lines;
3469 s->matched_entry = NULL;
3470 s->search_entry = NULL;
3471 break;
3472 case 'r':
3473 view->count = 0;
3474 if (view_is_parent_view(view))
3475 view_get_split(view, &begin_y, &begin_x);
3476 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3477 if (ref_view == NULL)
3478 return got_error_from_errno("view_open");
3479 err = open_ref_view(ref_view, s->repo);
3480 if (err) {
3481 view_close(ref_view);
3482 return err;
3484 if (view_is_parent_view(view) &&
3485 view->mode == TOG_VIEW_SPLIT_HRZN) {
3486 err = view_init_hsplit(view, begin_y);
3487 if (err)
3488 break;
3490 view->focussed = 0;
3491 ref_view->focussed = 1;
3492 ref_view->mode = view->mode;
3493 ref_view->nlines = view->lines - begin_y;
3494 if (view_is_parent_view(view)) {
3495 view_transfer_size(ref_view, view);
3496 err = view_close_child(view);
3497 if (err)
3498 return err;
3499 err = view_set_child(view, ref_view);
3500 if (err)
3501 return err;
3502 view->focus_child = 1;
3503 } else
3504 *new_view = ref_view;
3505 break;
3506 default:
3507 view->count = 0;
3508 break;
3511 return err;
3514 static const struct got_error *
3515 apply_unveil(const char *repo_path, const char *worktree_path)
3517 const struct got_error *error;
3519 #ifdef PROFILE
3520 if (unveil("gmon.out", "rwc") != 0)
3521 return got_error_from_errno2("unveil", "gmon.out");
3522 #endif
3523 if (repo_path && unveil(repo_path, "r") != 0)
3524 return got_error_from_errno2("unveil", repo_path);
3526 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3527 return got_error_from_errno2("unveil", worktree_path);
3529 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3530 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3532 error = got_privsep_unveil_exec_helpers();
3533 if (error != NULL)
3534 return error;
3536 if (unveil(NULL, NULL) != 0)
3537 return got_error_from_errno("unveil");
3539 return NULL;
3542 static void
3543 init_curses(void)
3546 * Override default signal handlers before starting ncurses.
3547 * This should prevent ncurses from installing its own
3548 * broken cleanup() signal handler.
3550 signal(SIGWINCH, tog_sigwinch);
3551 signal(SIGPIPE, tog_sigpipe);
3552 signal(SIGCONT, tog_sigcont);
3553 signal(SIGINT, tog_sigint);
3554 signal(SIGTERM, tog_sigterm);
3556 initscr();
3557 cbreak();
3558 halfdelay(1); /* Do fast refresh while initial view is loading. */
3559 noecho();
3560 nonl();
3561 intrflush(stdscr, FALSE);
3562 keypad(stdscr, TRUE);
3563 curs_set(0);
3564 if (getenv("TOG_COLORS") != NULL) {
3565 start_color();
3566 use_default_colors();
3570 static const struct got_error *
3571 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3572 struct got_repository *repo, struct got_worktree *worktree)
3574 const struct got_error *err = NULL;
3576 if (argc == 0) {
3577 *in_repo_path = strdup("/");
3578 if (*in_repo_path == NULL)
3579 return got_error_from_errno("strdup");
3580 return NULL;
3583 if (worktree) {
3584 const char *prefix = got_worktree_get_path_prefix(worktree);
3585 char *p;
3587 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3588 if (err)
3589 return err;
3590 if (asprintf(in_repo_path, "%s%s%s", prefix,
3591 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3592 p) == -1) {
3593 err = got_error_from_errno("asprintf");
3594 *in_repo_path = NULL;
3596 free(p);
3597 } else
3598 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3600 return err;
3603 static const struct got_error *
3604 cmd_log(int argc, char *argv[])
3606 const struct got_error *error;
3607 struct got_repository *repo = NULL;
3608 struct got_worktree *worktree = NULL;
3609 struct got_object_id *start_id = NULL;
3610 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3611 char *start_commit = NULL, *label = NULL;
3612 struct got_reference *ref = NULL;
3613 const char *head_ref_name = NULL;
3614 int ch, log_branches = 0;
3615 struct tog_view *view;
3616 int *pack_fds = NULL;
3618 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3619 switch (ch) {
3620 case 'b':
3621 log_branches = 1;
3622 break;
3623 case 'c':
3624 start_commit = optarg;
3625 break;
3626 case 'r':
3627 repo_path = realpath(optarg, NULL);
3628 if (repo_path == NULL)
3629 return got_error_from_errno2("realpath",
3630 optarg);
3631 break;
3632 default:
3633 usage_log();
3634 /* NOTREACHED */
3638 argc -= optind;
3639 argv += optind;
3641 if (argc > 1)
3642 usage_log();
3644 error = got_repo_pack_fds_open(&pack_fds);
3645 if (error != NULL)
3646 goto done;
3648 if (repo_path == NULL) {
3649 cwd = getcwd(NULL, 0);
3650 if (cwd == NULL)
3651 return got_error_from_errno("getcwd");
3652 error = got_worktree_open(&worktree, cwd);
3653 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3654 goto done;
3655 if (worktree)
3656 repo_path =
3657 strdup(got_worktree_get_repo_path(worktree));
3658 else
3659 repo_path = strdup(cwd);
3660 if (repo_path == NULL) {
3661 error = got_error_from_errno("strdup");
3662 goto done;
3666 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3667 if (error != NULL)
3668 goto done;
3670 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3671 repo, worktree);
3672 if (error)
3673 goto done;
3675 init_curses();
3677 error = apply_unveil(got_repo_get_path(repo),
3678 worktree ? got_worktree_get_root_path(worktree) : NULL);
3679 if (error)
3680 goto done;
3682 /* already loaded by tog_log_with_path()? */
3683 if (TAILQ_EMPTY(&tog_refs)) {
3684 error = tog_load_refs(repo, 0);
3685 if (error)
3686 goto done;
3689 if (start_commit == NULL) {
3690 error = got_repo_match_object_id(&start_id, &label,
3691 worktree ? got_worktree_get_head_ref_name(worktree) :
3692 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3693 if (error)
3694 goto done;
3695 head_ref_name = label;
3696 } else {
3697 error = got_ref_open(&ref, repo, start_commit, 0);
3698 if (error == NULL)
3699 head_ref_name = got_ref_get_name(ref);
3700 else if (error->code != GOT_ERR_NOT_REF)
3701 goto done;
3702 error = got_repo_match_object_id(&start_id, NULL,
3703 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3704 if (error)
3705 goto done;
3708 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3709 if (view == NULL) {
3710 error = got_error_from_errno("view_open");
3711 goto done;
3713 error = open_log_view(view, start_id, repo, head_ref_name,
3714 in_repo_path, log_branches);
3715 if (error)
3716 goto done;
3717 if (worktree) {
3718 /* Release work tree lock. */
3719 got_worktree_close(worktree);
3720 worktree = NULL;
3722 error = view_loop(view);
3723 done:
3724 free(in_repo_path);
3725 free(repo_path);
3726 free(cwd);
3727 free(start_id);
3728 free(label);
3729 if (ref)
3730 got_ref_close(ref);
3731 if (repo) {
3732 const struct got_error *close_err = got_repo_close(repo);
3733 if (error == NULL)
3734 error = close_err;
3736 if (worktree)
3737 got_worktree_close(worktree);
3738 if (pack_fds) {
3739 const struct got_error *pack_err =
3740 got_repo_pack_fds_close(pack_fds);
3741 if (error == NULL)
3742 error = pack_err;
3744 tog_free_refs();
3745 return error;
3748 __dead static void
3749 usage_diff(void)
3751 endwin();
3752 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3753 "[-w] object1 object2\n", getprogname());
3754 exit(1);
3757 static int
3758 match_line(const char *line, regex_t *regex, size_t nmatch,
3759 regmatch_t *regmatch)
3761 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3764 static struct tog_color *
3765 match_color(struct tog_colors *colors, const char *line)
3767 struct tog_color *tc = NULL;
3769 STAILQ_FOREACH(tc, colors, entry) {
3770 if (match_line(line, &tc->regex, 0, NULL))
3771 return tc;
3774 return NULL;
3777 static const struct got_error *
3778 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3779 WINDOW *window, int skipcol, regmatch_t *regmatch)
3781 const struct got_error *err = NULL;
3782 char *exstr = NULL;
3783 wchar_t *wline = NULL;
3784 int rme, rms, n, width, scrollx;
3785 int width0 = 0, width1 = 0, width2 = 0;
3786 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3788 *wtotal = 0;
3790 rms = regmatch->rm_so;
3791 rme = regmatch->rm_eo;
3793 err = expand_tab(&exstr, line);
3794 if (err)
3795 return err;
3797 /* Split the line into 3 segments, according to match offsets. */
3798 seg0 = strndup(exstr, rms);
3799 if (seg0 == NULL) {
3800 err = got_error_from_errno("strndup");
3801 goto done;
3803 seg1 = strndup(exstr + rms, rme - rms);
3804 if (seg1 == NULL) {
3805 err = got_error_from_errno("strndup");
3806 goto done;
3808 seg2 = strdup(exstr + rme);
3809 if (seg2 == NULL) {
3810 err = got_error_from_errno("strndup");
3811 goto done;
3814 /* draw up to matched token if we haven't scrolled past it */
3815 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3816 col_tab_align, 1);
3817 if (err)
3818 goto done;
3819 n = MAX(width0 - skipcol, 0);
3820 if (n) {
3821 free(wline);
3822 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3823 wlimit, col_tab_align, 1);
3824 if (err)
3825 goto done;
3826 waddwstr(window, &wline[scrollx]);
3827 wlimit -= width;
3828 *wtotal += width;
3831 if (wlimit > 0) {
3832 int i = 0, w = 0;
3833 size_t wlen;
3835 free(wline);
3836 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3837 col_tab_align, 1);
3838 if (err)
3839 goto done;
3840 wlen = wcslen(wline);
3841 while (i < wlen) {
3842 width = wcwidth(wline[i]);
3843 if (width == -1) {
3844 /* should not happen, tabs are expanded */
3845 err = got_error(GOT_ERR_RANGE);
3846 goto done;
3848 if (width0 + w + width > skipcol)
3849 break;
3850 w += width;
3851 i++;
3853 /* draw (visible part of) matched token (if scrolled into it) */
3854 if (width1 - w > 0) {
3855 wattron(window, A_STANDOUT);
3856 waddwstr(window, &wline[i]);
3857 wattroff(window, A_STANDOUT);
3858 wlimit -= (width1 - w);
3859 *wtotal += (width1 - w);
3863 if (wlimit > 0) { /* draw rest of line */
3864 free(wline);
3865 if (skipcol > width0 + width1) {
3866 err = format_line(&wline, &width2, &scrollx, seg2,
3867 skipcol - (width0 + width1), wlimit,
3868 col_tab_align, 1);
3869 if (err)
3870 goto done;
3871 waddwstr(window, &wline[scrollx]);
3872 } else {
3873 err = format_line(&wline, &width2, NULL, seg2, 0,
3874 wlimit, col_tab_align, 1);
3875 if (err)
3876 goto done;
3877 waddwstr(window, wline);
3879 *wtotal += width2;
3881 done:
3882 free(wline);
3883 free(exstr);
3884 free(seg0);
3885 free(seg1);
3886 free(seg2);
3887 return err;
3890 static const struct got_error *
3891 draw_file(struct tog_view *view, const char *header)
3893 struct tog_diff_view_state *s = &view->state.diff;
3894 regmatch_t *regmatch = &view->regmatch;
3895 const struct got_error *err;
3896 int nprinted = 0;
3897 char *line;
3898 size_t linesize = 0;
3899 ssize_t linelen;
3900 struct tog_color *tc;
3901 wchar_t *wline;
3902 int width;
3903 int max_lines = view->nlines;
3904 int nlines = s->nlines;
3905 off_t line_offset;
3907 line_offset = s->line_offsets[s->first_displayed_line - 1];
3908 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3909 return got_error_from_errno("fseek");
3911 werase(view->window);
3913 if (header) {
3914 if (asprintf(&line, "[%d/%d] %s",
3915 s->first_displayed_line - 1 + s->selected_line, nlines,
3916 header) == -1)
3917 return got_error_from_errno("asprintf");
3918 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3919 0, 0);
3920 free(line);
3921 if (err)
3922 return err;
3924 if (view_needs_focus_indication(view))
3925 wstandout(view->window);
3926 waddwstr(view->window, wline);
3927 free(wline);
3928 wline = NULL;
3929 if (view_needs_focus_indication(view))
3930 wstandend(view->window);
3931 if (width <= view->ncols - 1)
3932 waddch(view->window, '\n');
3934 if (max_lines <= 1)
3935 return NULL;
3936 max_lines--;
3939 s->eof = 0;
3940 view->maxx = 0;
3941 line = NULL;
3942 while (max_lines > 0 && nprinted < max_lines) {
3943 linelen = getline(&line, &linesize, s->f);
3944 if (linelen == -1) {
3945 if (feof(s->f)) {
3946 s->eof = 1;
3947 break;
3949 free(line);
3950 return got_ferror(s->f, GOT_ERR_IO);
3953 /* Set view->maxx based on full line length. */
3954 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3955 view->x ? 1 : 0);
3956 if (err) {
3957 free(line);
3958 return err;
3960 view->maxx = MAX(view->maxx, width);
3961 free(wline);
3962 wline = NULL;
3964 tc = match_color(&s->colors, line);
3965 if (tc)
3966 wattr_on(view->window,
3967 COLOR_PAIR(tc->colorpair), NULL);
3968 if (s->first_displayed_line + nprinted == s->matched_line &&
3969 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3970 err = add_matched_line(&width, line, view->ncols, 0,
3971 view->window, view->x, regmatch);
3972 if (err) {
3973 free(line);
3974 return err;
3976 } else {
3977 int skip;
3978 err = format_line(&wline, &width, &skip, line,
3979 view->x, view->ncols, 0, view->x ? 1 : 0);
3980 if (err) {
3981 free(line);
3982 return err;
3984 waddwstr(view->window, &wline[skip]);
3985 free(wline);
3986 wline = NULL;
3988 if (tc)
3989 wattr_off(view->window,
3990 COLOR_PAIR(tc->colorpair), NULL);
3991 if (width <= view->ncols - 1)
3992 waddch(view->window, '\n');
3993 nprinted++;
3995 free(line);
3996 if (nprinted >= 1)
3997 s->last_displayed_line = s->first_displayed_line +
3998 (nprinted - 1);
3999 else
4000 s->last_displayed_line = s->first_displayed_line;
4002 view_border(view);
4004 if (s->eof) {
4005 while (nprinted < view->nlines) {
4006 waddch(view->window, '\n');
4007 nprinted++;
4010 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4011 view->ncols, 0, 0);
4012 if (err) {
4013 return err;
4016 wstandout(view->window);
4017 waddwstr(view->window, wline);
4018 free(wline);
4019 wline = NULL;
4020 wstandend(view->window);
4023 return NULL;
4026 static char *
4027 get_datestr(time_t *time, char *datebuf)
4029 struct tm mytm, *tm;
4030 char *p, *s;
4032 tm = gmtime_r(time, &mytm);
4033 if (tm == NULL)
4034 return NULL;
4035 s = asctime_r(tm, datebuf);
4036 if (s == NULL)
4037 return NULL;
4038 p = strchr(s, '\n');
4039 if (p)
4040 *p = '\0';
4041 return s;
4044 static const struct got_error *
4045 get_changed_paths(struct got_pathlist_head *paths,
4046 struct got_commit_object *commit, struct got_repository *repo)
4048 const struct got_error *err = NULL;
4049 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4050 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4051 struct got_object_qid *qid;
4053 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4054 if (qid != NULL) {
4055 struct got_commit_object *pcommit;
4056 err = got_object_open_as_commit(&pcommit, repo,
4057 &qid->id);
4058 if (err)
4059 return err;
4061 tree_id1 = got_object_id_dup(
4062 got_object_commit_get_tree_id(pcommit));
4063 if (tree_id1 == NULL) {
4064 got_object_commit_close(pcommit);
4065 return got_error_from_errno("got_object_id_dup");
4067 got_object_commit_close(pcommit);
4071 if (tree_id1) {
4072 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4073 if (err)
4074 goto done;
4077 tree_id2 = got_object_commit_get_tree_id(commit);
4078 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4079 if (err)
4080 goto done;
4082 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4083 got_diff_tree_collect_changed_paths, paths, 0);
4084 done:
4085 if (tree1)
4086 got_object_tree_close(tree1);
4087 if (tree2)
4088 got_object_tree_close(tree2);
4089 free(tree_id1);
4090 return err;
4093 static const struct got_error *
4094 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4096 off_t *p;
4098 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4099 if (p == NULL)
4100 return got_error_from_errno("reallocarray");
4101 *line_offsets = p;
4102 (*line_offsets)[*nlines] = off;
4103 (*nlines)++;
4104 return NULL;
4107 static const struct got_error *
4108 write_commit_info(off_t **line_offsets, size_t *nlines,
4109 struct got_object_id *commit_id, struct got_reflist_head *refs,
4110 struct got_repository *repo, FILE *outfile)
4112 const struct got_error *err = NULL;
4113 char datebuf[26], *datestr;
4114 struct got_commit_object *commit;
4115 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4116 time_t committer_time;
4117 const char *author, *committer;
4118 char *refs_str = NULL;
4119 struct got_pathlist_head changed_paths;
4120 struct got_pathlist_entry *pe;
4121 off_t outoff = 0;
4122 int n;
4124 TAILQ_INIT(&changed_paths);
4126 if (refs) {
4127 err = build_refs_str(&refs_str, refs, commit_id, repo);
4128 if (err)
4129 return err;
4132 err = got_object_open_as_commit(&commit, repo, commit_id);
4133 if (err)
4134 return err;
4136 err = got_object_id_str(&id_str, commit_id);
4137 if (err) {
4138 err = got_error_from_errno("got_object_id_str");
4139 goto done;
4142 err = add_line_offset(line_offsets, nlines, 0);
4143 if (err)
4144 goto done;
4146 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4147 refs_str ? refs_str : "", refs_str ? ")" : "");
4148 if (n < 0) {
4149 err = got_error_from_errno("fprintf");
4150 goto done;
4152 outoff += n;
4153 err = add_line_offset(line_offsets, nlines, outoff);
4154 if (err)
4155 goto done;
4157 n = fprintf(outfile, "from: %s\n",
4158 got_object_commit_get_author(commit));
4159 if (n < 0) {
4160 err = got_error_from_errno("fprintf");
4161 goto done;
4163 outoff += n;
4164 err = add_line_offset(line_offsets, nlines, outoff);
4165 if (err)
4166 goto done;
4168 committer_time = got_object_commit_get_committer_time(commit);
4169 datestr = get_datestr(&committer_time, datebuf);
4170 if (datestr) {
4171 n = fprintf(outfile, "date: %s UTC\n", datestr);
4172 if (n < 0) {
4173 err = got_error_from_errno("fprintf");
4174 goto done;
4176 outoff += n;
4177 err = add_line_offset(line_offsets, nlines, outoff);
4178 if (err)
4179 goto done;
4181 author = got_object_commit_get_author(commit);
4182 committer = got_object_commit_get_committer(commit);
4183 if (strcmp(author, committer) != 0) {
4184 n = fprintf(outfile, "via: %s\n", committer);
4185 if (n < 0) {
4186 err = got_error_from_errno("fprintf");
4187 goto done;
4189 outoff += n;
4190 err = add_line_offset(line_offsets, nlines, outoff);
4191 if (err)
4192 goto done;
4194 if (got_object_commit_get_nparents(commit) > 1) {
4195 const struct got_object_id_queue *parent_ids;
4196 struct got_object_qid *qid;
4197 int pn = 1;
4198 parent_ids = got_object_commit_get_parent_ids(commit);
4199 STAILQ_FOREACH(qid, parent_ids, entry) {
4200 err = got_object_id_str(&id_str, &qid->id);
4201 if (err)
4202 goto done;
4203 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4204 if (n < 0) {
4205 err = got_error_from_errno("fprintf");
4206 goto done;
4208 outoff += n;
4209 err = add_line_offset(line_offsets, nlines, outoff);
4210 if (err)
4211 goto done;
4212 free(id_str);
4213 id_str = NULL;
4217 err = got_object_commit_get_logmsg(&logmsg, commit);
4218 if (err)
4219 goto done;
4220 s = logmsg;
4221 while ((line = strsep(&s, "\n")) != NULL) {
4222 n = fprintf(outfile, "%s\n", line);
4223 if (n < 0) {
4224 err = got_error_from_errno("fprintf");
4225 goto done;
4227 outoff += n;
4228 err = add_line_offset(line_offsets, nlines, outoff);
4229 if (err)
4230 goto done;
4233 err = get_changed_paths(&changed_paths, commit, repo);
4234 if (err)
4235 goto done;
4236 TAILQ_FOREACH(pe, &changed_paths, entry) {
4237 struct got_diff_changed_path *cp = pe->data;
4238 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4239 if (n < 0) {
4240 err = got_error_from_errno("fprintf");
4241 goto done;
4243 outoff += n;
4244 err = add_line_offset(line_offsets, nlines, outoff);
4245 if (err)
4246 goto done;
4247 free((char *)pe->path);
4248 free(pe->data);
4251 fputc('\n', outfile);
4252 outoff++;
4253 err = add_line_offset(line_offsets, nlines, outoff);
4254 done:
4255 got_pathlist_free(&changed_paths);
4256 free(id_str);
4257 free(logmsg);
4258 free(refs_str);
4259 got_object_commit_close(commit);
4260 if (err) {
4261 free(*line_offsets);
4262 *line_offsets = NULL;
4263 *nlines = 0;
4265 return err;
4268 static const struct got_error *
4269 create_diff(struct tog_diff_view_state *s)
4271 const struct got_error *err = NULL;
4272 FILE *f = NULL;
4273 int obj_type;
4275 free(s->line_offsets);
4276 s->line_offsets = malloc(sizeof(off_t));
4277 if (s->line_offsets == NULL)
4278 return got_error_from_errno("malloc");
4279 s->nlines = 0;
4281 f = got_opentemp();
4282 if (f == NULL) {
4283 err = got_error_from_errno("got_opentemp");
4284 goto done;
4286 if (s->f && fclose(s->f) == EOF) {
4287 err = got_error_from_errno("fclose");
4288 goto done;
4290 s->f = f;
4292 if (s->id1)
4293 err = got_object_get_type(&obj_type, s->repo, s->id1);
4294 else
4295 err = got_object_get_type(&obj_type, s->repo, s->id2);
4296 if (err)
4297 goto done;
4299 switch (obj_type) {
4300 case GOT_OBJ_TYPE_BLOB:
4301 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4302 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4303 s->label1, s->label2, tog_diff_algo, s->diff_context,
4304 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4305 break;
4306 case GOT_OBJ_TYPE_TREE:
4307 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4308 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4309 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4310 s->force_text_diff, s->repo, s->f);
4311 break;
4312 case GOT_OBJ_TYPE_COMMIT: {
4313 const struct got_object_id_queue *parent_ids;
4314 struct got_object_qid *pid;
4315 struct got_commit_object *commit2;
4316 struct got_reflist_head *refs;
4318 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4319 if (err)
4320 goto done;
4321 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4322 /* Show commit info if we're diffing to a parent/root commit. */
4323 if (s->id1 == NULL) {
4324 err = write_commit_info(&s->line_offsets, &s->nlines,
4325 s->id2, refs, s->repo, s->f);
4326 if (err)
4327 goto done;
4328 } else {
4329 parent_ids = got_object_commit_get_parent_ids(commit2);
4330 STAILQ_FOREACH(pid, parent_ids, entry) {
4331 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4332 err = write_commit_info(
4333 &s->line_offsets, &s->nlines,
4334 s->id2, refs, s->repo, s->f);
4335 if (err)
4336 goto done;
4337 break;
4341 got_object_commit_close(commit2);
4343 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4344 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4345 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4346 s->force_text_diff, s->repo, s->f);
4347 break;
4349 default:
4350 err = got_error(GOT_ERR_OBJ_TYPE);
4351 break;
4353 if (err)
4354 goto done;
4355 done:
4356 if (s->f && fflush(s->f) != 0 && err == NULL)
4357 err = got_error_from_errno("fflush");
4358 return err;
4361 static void
4362 diff_view_indicate_progress(struct tog_view *view)
4364 mvwaddstr(view->window, 0, 0, "diffing...");
4365 update_panels();
4366 doupdate();
4369 static const struct got_error *
4370 search_start_diff_view(struct tog_view *view)
4372 struct tog_diff_view_state *s = &view->state.diff;
4374 s->matched_line = 0;
4375 return NULL;
4378 static const struct got_error *
4379 search_next_diff_view(struct tog_view *view)
4381 struct tog_diff_view_state *s = &view->state.diff;
4382 const struct got_error *err = NULL;
4383 int lineno;
4384 char *line = NULL;
4385 size_t linesize = 0;
4386 ssize_t linelen;
4388 if (!view->searching) {
4389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4390 return NULL;
4393 if (s->matched_line) {
4394 if (view->searching == TOG_SEARCH_FORWARD)
4395 lineno = s->matched_line + 1;
4396 else
4397 lineno = s->matched_line - 1;
4398 } else
4399 lineno = s->first_displayed_line;
4401 while (1) {
4402 off_t offset;
4404 if (lineno <= 0 || lineno > s->nlines) {
4405 if (s->matched_line == 0) {
4406 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4407 break;
4410 if (view->searching == TOG_SEARCH_FORWARD)
4411 lineno = 1;
4412 else
4413 lineno = s->nlines;
4416 offset = s->line_offsets[lineno - 1];
4417 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4418 free(line);
4419 return got_error_from_errno("fseeko");
4421 linelen = getline(&line, &linesize, s->f);
4422 if (linelen != -1) {
4423 char *exstr;
4424 err = expand_tab(&exstr, line);
4425 if (err)
4426 break;
4427 if (match_line(exstr, &view->regex, 1,
4428 &view->regmatch)) {
4429 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4430 s->matched_line = lineno;
4431 free(exstr);
4432 break;
4434 free(exstr);
4436 if (view->searching == TOG_SEARCH_FORWARD)
4437 lineno++;
4438 else
4439 lineno--;
4441 free(line);
4443 if (s->matched_line) {
4444 s->first_displayed_line = s->matched_line;
4445 s->selected_line = 1;
4448 return err;
4451 static const struct got_error *
4452 close_diff_view(struct tog_view *view)
4454 const struct got_error *err = NULL;
4455 struct tog_diff_view_state *s = &view->state.diff;
4457 free(s->id1);
4458 s->id1 = NULL;
4459 free(s->id2);
4460 s->id2 = NULL;
4461 if (s->f && fclose(s->f) == EOF)
4462 err = got_error_from_errno("fclose");
4463 s->f = NULL;
4464 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4465 err = got_error_from_errno("fclose");
4466 s->f1 = NULL;
4467 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4468 err = got_error_from_errno("fclose");
4469 s->f2 = NULL;
4470 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4471 err = got_error_from_errno("close");
4472 s->fd1 = -1;
4473 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4474 err = got_error_from_errno("close");
4475 s->fd2 = -1;
4476 free_colors(&s->colors);
4477 free(s->line_offsets);
4478 s->line_offsets = NULL;
4479 s->nlines = 0;
4480 return err;
4483 static const struct got_error *
4484 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4485 struct got_object_id *id2, const char *label1, const char *label2,
4486 int diff_context, int ignore_whitespace, int force_text_diff,
4487 struct tog_view *parent_view, struct got_repository *repo)
4489 const struct got_error *err;
4490 struct tog_diff_view_state *s = &view->state.diff;
4492 memset(s, 0, sizeof(*s));
4493 s->fd1 = -1;
4494 s->fd2 = -1;
4496 if (id1 != NULL && id2 != NULL) {
4497 int type1, type2;
4498 err = got_object_get_type(&type1, repo, id1);
4499 if (err)
4500 return err;
4501 err = got_object_get_type(&type2, repo, id2);
4502 if (err)
4503 return err;
4505 if (type1 != type2)
4506 return got_error(GOT_ERR_OBJ_TYPE);
4508 s->first_displayed_line = 1;
4509 s->last_displayed_line = view->nlines;
4510 s->selected_line = 1;
4511 s->repo = repo;
4512 s->id1 = id1;
4513 s->id2 = id2;
4514 s->label1 = label1;
4515 s->label2 = label2;
4517 if (id1) {
4518 s->id1 = got_object_id_dup(id1);
4519 if (s->id1 == NULL)
4520 return got_error_from_errno("got_object_id_dup");
4521 } else
4522 s->id1 = NULL;
4524 s->id2 = got_object_id_dup(id2);
4525 if (s->id2 == NULL) {
4526 err = got_error_from_errno("got_object_id_dup");
4527 goto done;
4530 s->f1 = got_opentemp();
4531 if (s->f1 == NULL) {
4532 err = got_error_from_errno("got_opentemp");
4533 goto done;
4536 s->f2 = got_opentemp();
4537 if (s->f2 == NULL) {
4538 err = got_error_from_errno("got_opentemp");
4539 goto done;
4542 s->fd1 = got_opentempfd();
4543 if (s->fd1 == -1) {
4544 err = got_error_from_errno("got_opentempfd");
4545 goto done;
4548 s->fd2 = got_opentempfd();
4549 if (s->fd2 == -1) {
4550 err = got_error_from_errno("got_opentempfd");
4551 goto done;
4554 s->first_displayed_line = 1;
4555 s->last_displayed_line = view->nlines;
4556 s->diff_context = diff_context;
4557 s->ignore_whitespace = ignore_whitespace;
4558 s->force_text_diff = force_text_diff;
4559 s->parent_view = parent_view;
4560 s->repo = repo;
4562 STAILQ_INIT(&s->colors);
4563 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4564 err = add_color(&s->colors,
4565 "^-", TOG_COLOR_DIFF_MINUS,
4566 get_color_value("TOG_COLOR_DIFF_MINUS"));
4567 if (err)
4568 goto done;
4569 err = add_color(&s->colors, "^\\+",
4570 TOG_COLOR_DIFF_PLUS,
4571 get_color_value("TOG_COLOR_DIFF_PLUS"));
4572 if (err)
4573 goto done;
4574 err = add_color(&s->colors,
4575 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4576 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4577 if (err)
4578 goto done;
4580 err = add_color(&s->colors,
4581 "^(commit [0-9a-f]|parent [0-9]|"
4582 "(blob|file|tree|commit) [-+] |"
4583 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4584 get_color_value("TOG_COLOR_DIFF_META"));
4585 if (err)
4586 goto done;
4588 err = add_color(&s->colors,
4589 "^(from|via): ", TOG_COLOR_AUTHOR,
4590 get_color_value("TOG_COLOR_AUTHOR"));
4591 if (err)
4592 goto done;
4594 err = add_color(&s->colors,
4595 "^date: ", TOG_COLOR_DATE,
4596 get_color_value("TOG_COLOR_DATE"));
4597 if (err)
4598 goto done;
4601 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4602 view_is_splitscreen(view))
4603 show_log_view(parent_view); /* draw border */
4604 diff_view_indicate_progress(view);
4606 err = create_diff(s);
4608 view->show = show_diff_view;
4609 view->input = input_diff_view;
4610 view->reset = reset_diff_view;
4611 view->close = close_diff_view;
4612 view->search_start = search_start_diff_view;
4613 view->search_next = search_next_diff_view;
4614 done:
4615 if (err)
4616 close_diff_view(view);
4617 return err;
4620 static const struct got_error *
4621 show_diff_view(struct tog_view *view)
4623 const struct got_error *err;
4624 struct tog_diff_view_state *s = &view->state.diff;
4625 char *id_str1 = NULL, *id_str2, *header;
4626 const char *label1, *label2;
4628 if (s->id1) {
4629 err = got_object_id_str(&id_str1, s->id1);
4630 if (err)
4631 return err;
4632 label1 = s->label1 ? : id_str1;
4633 } else
4634 label1 = "/dev/null";
4636 err = got_object_id_str(&id_str2, s->id2);
4637 if (err)
4638 return err;
4639 label2 = s->label2 ? : id_str2;
4641 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4642 err = got_error_from_errno("asprintf");
4643 free(id_str1);
4644 free(id_str2);
4645 return err;
4647 free(id_str1);
4648 free(id_str2);
4650 err = draw_file(view, header);
4651 free(header);
4652 return err;
4655 static const struct got_error *
4656 set_selected_commit(struct tog_diff_view_state *s,
4657 struct commit_queue_entry *entry)
4659 const struct got_error *err;
4660 const struct got_object_id_queue *parent_ids;
4661 struct got_commit_object *selected_commit;
4662 struct got_object_qid *pid;
4664 free(s->id2);
4665 s->id2 = got_object_id_dup(entry->id);
4666 if (s->id2 == NULL)
4667 return got_error_from_errno("got_object_id_dup");
4669 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4670 if (err)
4671 return err;
4672 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4673 free(s->id1);
4674 pid = STAILQ_FIRST(parent_ids);
4675 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4676 got_object_commit_close(selected_commit);
4677 return NULL;
4680 static const struct got_error *
4681 reset_diff_view(struct tog_view *view)
4683 struct tog_diff_view_state *s = &view->state.diff;
4685 view->count = 0;
4686 wclear(view->window);
4687 s->first_displayed_line = 1;
4688 s->last_displayed_line = view->nlines;
4689 s->matched_line = 0;
4690 diff_view_indicate_progress(view);
4691 return create_diff(s);
4694 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4695 int, int, int);
4696 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4697 int, int);
4699 static const struct got_error *
4700 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4702 const struct got_error *err = NULL;
4703 struct tog_diff_view_state *s = &view->state.diff;
4704 struct tog_log_view_state *ls;
4705 struct commit_queue_entry *old_selected_entry;
4706 char *line = NULL;
4707 size_t linesize = 0;
4708 ssize_t linelen;
4709 int i, nscroll = view->nlines - 1, up = 0;
4711 switch (ch) {
4712 case '0':
4713 view->x = 0;
4714 break;
4715 case '$':
4716 view->x = MAX(view->maxx - view->ncols / 3, 0);
4717 view->count = 0;
4718 break;
4719 case KEY_RIGHT:
4720 case 'l':
4721 if (view->x + view->ncols / 3 < view->maxx)
4722 view->x += 2; /* move two columns right */
4723 else
4724 view->count = 0;
4725 break;
4726 case KEY_LEFT:
4727 case 'h':
4728 view->x -= MIN(view->x, 2); /* move two columns back */
4729 if (view->x <= 0)
4730 view->count = 0;
4731 break;
4732 case 'a':
4733 case 'w':
4734 if (ch == 'a')
4735 s->force_text_diff = !s->force_text_diff;
4736 if (ch == 'w')
4737 s->ignore_whitespace = !s->ignore_whitespace;
4738 err = reset_diff_view(view);
4739 break;
4740 case 'g':
4741 case KEY_HOME:
4742 s->first_displayed_line = 1;
4743 view->count = 0;
4744 break;
4745 case 'G':
4746 case KEY_END:
4747 view->count = 0;
4748 if (s->eof)
4749 break;
4751 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4752 s->eof = 1;
4753 break;
4754 case 'k':
4755 case KEY_UP:
4756 case CTRL('p'):
4757 if (s->first_displayed_line > 1)
4758 s->first_displayed_line--;
4759 else
4760 view->count = 0;
4761 break;
4762 case CTRL('u'):
4763 case 'u':
4764 nscroll /= 2;
4765 /* FALL THROUGH */
4766 case KEY_PPAGE:
4767 case CTRL('b'):
4768 case 'b':
4769 if (s->first_displayed_line == 1) {
4770 view->count = 0;
4771 break;
4773 i = 0;
4774 while (i++ < nscroll && s->first_displayed_line > 1)
4775 s->first_displayed_line--;
4776 break;
4777 case 'j':
4778 case KEY_DOWN:
4779 case CTRL('n'):
4780 if (!s->eof)
4781 s->first_displayed_line++;
4782 else
4783 view->count = 0;
4784 break;
4785 case CTRL('d'):
4786 case 'd':
4787 nscroll /= 2;
4788 /* FALL THROUGH */
4789 case KEY_NPAGE:
4790 case CTRL('f'):
4791 case 'f':
4792 case ' ':
4793 if (s->eof) {
4794 view->count = 0;
4795 break;
4797 i = 0;
4798 while (!s->eof && i++ < nscroll) {
4799 linelen = getline(&line, &linesize, s->f);
4800 s->first_displayed_line++;
4801 if (linelen == -1) {
4802 if (feof(s->f)) {
4803 s->eof = 1;
4804 } else
4805 err = got_ferror(s->f, GOT_ERR_IO);
4806 break;
4809 free(line);
4810 break;
4811 case '[':
4812 if (s->diff_context > 0) {
4813 s->diff_context--;
4814 s->matched_line = 0;
4815 diff_view_indicate_progress(view);
4816 err = create_diff(s);
4817 if (s->first_displayed_line + view->nlines - 1 >
4818 s->nlines) {
4819 s->first_displayed_line = 1;
4820 s->last_displayed_line = view->nlines;
4822 } else
4823 view->count = 0;
4824 break;
4825 case ']':
4826 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4827 s->diff_context++;
4828 s->matched_line = 0;
4829 diff_view_indicate_progress(view);
4830 err = create_diff(s);
4831 } else
4832 view->count = 0;
4833 break;
4834 case '<':
4835 case ',':
4836 case 'K':
4837 up = 1;
4838 /* FALL THROUGH */
4839 case '>':
4840 case '.':
4841 case 'J':
4842 if (s->parent_view == NULL) {
4843 view->count = 0;
4844 break;
4846 s->parent_view->count = view->count;
4848 if (s->parent_view->type == TOG_VIEW_LOG) {
4849 ls = &s->parent_view->state.log;
4850 old_selected_entry = ls->selected_entry;
4852 err = input_log_view(NULL, s->parent_view,
4853 up ? KEY_UP : KEY_DOWN);
4854 if (err)
4855 break;
4856 view->count = s->parent_view->count;
4858 if (old_selected_entry == ls->selected_entry)
4859 break;
4861 err = set_selected_commit(s, ls->selected_entry);
4862 if (err)
4863 break;
4864 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4865 struct tog_blame_view_state *bs;
4866 struct got_object_id *id, *prev_id;
4868 bs = &s->parent_view->state.blame;
4869 prev_id = get_annotation_for_line(bs->blame.lines,
4870 bs->blame.nlines, bs->last_diffed_line);
4872 err = input_blame_view(&view, s->parent_view,
4873 up ? KEY_UP : KEY_DOWN);
4874 if (err)
4875 break;
4876 view->count = s->parent_view->count;
4878 if (prev_id == NULL)
4879 break;
4880 id = get_selected_commit_id(bs->blame.lines,
4881 bs->blame.nlines, bs->first_displayed_line,
4882 bs->selected_line);
4883 if (id == NULL)
4884 break;
4886 if (!got_object_id_cmp(prev_id, id))
4887 break;
4889 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4890 if (err)
4891 break;
4893 s->first_displayed_line = 1;
4894 s->last_displayed_line = view->nlines;
4895 s->matched_line = 0;
4896 view->x = 0;
4898 diff_view_indicate_progress(view);
4899 err = create_diff(s);
4900 break;
4901 default:
4902 view->count = 0;
4903 break;
4906 return err;
4909 static const struct got_error *
4910 cmd_diff(int argc, char *argv[])
4912 const struct got_error *error = NULL;
4913 struct got_repository *repo = NULL;
4914 struct got_worktree *worktree = NULL;
4915 struct got_object_id *id1 = NULL, *id2 = NULL;
4916 char *repo_path = NULL, *cwd = NULL;
4917 char *id_str1 = NULL, *id_str2 = NULL;
4918 char *label1 = NULL, *label2 = NULL;
4919 int diff_context = 3, ignore_whitespace = 0;
4920 int ch, force_text_diff = 0;
4921 const char *errstr;
4922 struct tog_view *view;
4923 int *pack_fds = NULL;
4925 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4926 switch (ch) {
4927 case 'a':
4928 force_text_diff = 1;
4929 break;
4930 case 'C':
4931 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4932 &errstr);
4933 if (errstr != NULL)
4934 errx(1, "number of context lines is %s: %s",
4935 errstr, errstr);
4936 break;
4937 case 'r':
4938 repo_path = realpath(optarg, NULL);
4939 if (repo_path == NULL)
4940 return got_error_from_errno2("realpath",
4941 optarg);
4942 got_path_strip_trailing_slashes(repo_path);
4943 break;
4944 case 'w':
4945 ignore_whitespace = 1;
4946 break;
4947 default:
4948 usage_diff();
4949 /* NOTREACHED */
4953 argc -= optind;
4954 argv += optind;
4956 if (argc == 0) {
4957 usage_diff(); /* TODO show local worktree changes */
4958 } else if (argc == 2) {
4959 id_str1 = argv[0];
4960 id_str2 = argv[1];
4961 } else
4962 usage_diff();
4964 error = got_repo_pack_fds_open(&pack_fds);
4965 if (error)
4966 goto done;
4968 if (repo_path == NULL) {
4969 cwd = getcwd(NULL, 0);
4970 if (cwd == NULL)
4971 return got_error_from_errno("getcwd");
4972 error = got_worktree_open(&worktree, cwd);
4973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4974 goto done;
4975 if (worktree)
4976 repo_path =
4977 strdup(got_worktree_get_repo_path(worktree));
4978 else
4979 repo_path = strdup(cwd);
4980 if (repo_path == NULL) {
4981 error = got_error_from_errno("strdup");
4982 goto done;
4986 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4987 if (error)
4988 goto done;
4990 init_curses();
4992 error = apply_unveil(got_repo_get_path(repo), NULL);
4993 if (error)
4994 goto done;
4996 error = tog_load_refs(repo, 0);
4997 if (error)
4998 goto done;
5000 error = got_repo_match_object_id(&id1, &label1, id_str1,
5001 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5002 if (error)
5003 goto done;
5005 error = got_repo_match_object_id(&id2, &label2, id_str2,
5006 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5007 if (error)
5008 goto done;
5010 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5011 if (view == NULL) {
5012 error = got_error_from_errno("view_open");
5013 goto done;
5015 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5016 ignore_whitespace, force_text_diff, NULL, repo);
5017 if (error)
5018 goto done;
5019 error = view_loop(view);
5020 done:
5021 free(label1);
5022 free(label2);
5023 free(repo_path);
5024 free(cwd);
5025 if (repo) {
5026 const struct got_error *close_err = got_repo_close(repo);
5027 if (error == NULL)
5028 error = close_err;
5030 if (worktree)
5031 got_worktree_close(worktree);
5032 if (pack_fds) {
5033 const struct got_error *pack_err =
5034 got_repo_pack_fds_close(pack_fds);
5035 if (error == NULL)
5036 error = pack_err;
5038 tog_free_refs();
5039 return error;
5042 __dead static void
5043 usage_blame(void)
5045 endwin();
5046 fprintf(stderr,
5047 "usage: %s blame [-c commit] [-r repository-path] path\n",
5048 getprogname());
5049 exit(1);
5052 struct tog_blame_line {
5053 int annotated;
5054 struct got_object_id *id;
5057 static const struct got_error *
5058 draw_blame(struct tog_view *view)
5060 struct tog_blame_view_state *s = &view->state.blame;
5061 struct tog_blame *blame = &s->blame;
5062 regmatch_t *regmatch = &view->regmatch;
5063 const struct got_error *err;
5064 int lineno = 0, nprinted = 0;
5065 char *line = NULL;
5066 size_t linesize = 0;
5067 ssize_t linelen;
5068 wchar_t *wline;
5069 int width;
5070 struct tog_blame_line *blame_line;
5071 struct got_object_id *prev_id = NULL;
5072 char *id_str;
5073 struct tog_color *tc;
5075 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5076 if (err)
5077 return err;
5079 rewind(blame->f);
5080 werase(view->window);
5082 if (asprintf(&line, "commit %s", id_str) == -1) {
5083 err = got_error_from_errno("asprintf");
5084 free(id_str);
5085 return err;
5088 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5089 free(line);
5090 line = NULL;
5091 if (err)
5092 return err;
5093 if (view_needs_focus_indication(view))
5094 wstandout(view->window);
5095 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5096 if (tc)
5097 wattr_on(view->window,
5098 COLOR_PAIR(tc->colorpair), NULL);
5099 waddwstr(view->window, wline);
5100 if (tc)
5101 wattr_off(view->window,
5102 COLOR_PAIR(tc->colorpair), NULL);
5103 if (view_needs_focus_indication(view))
5104 wstandend(view->window);
5105 free(wline);
5106 wline = NULL;
5107 if (width < view->ncols - 1)
5108 waddch(view->window, '\n');
5110 if (asprintf(&line, "[%d/%d] %s%s",
5111 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5112 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5113 free(id_str);
5114 return got_error_from_errno("asprintf");
5116 free(id_str);
5117 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5118 free(line);
5119 line = NULL;
5120 if (err)
5121 return err;
5122 waddwstr(view->window, wline);
5123 free(wline);
5124 wline = NULL;
5125 if (width < view->ncols - 1)
5126 waddch(view->window, '\n');
5128 s->eof = 0;
5129 view->maxx = 0;
5130 while (nprinted < view->nlines - 2) {
5131 linelen = getline(&line, &linesize, blame->f);
5132 if (linelen == -1) {
5133 if (feof(blame->f)) {
5134 s->eof = 1;
5135 break;
5137 free(line);
5138 return got_ferror(blame->f, GOT_ERR_IO);
5140 if (++lineno < s->first_displayed_line)
5141 continue;
5143 /* Set view->maxx based on full line length. */
5144 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5145 if (err) {
5146 free(line);
5147 return err;
5149 free(wline);
5150 wline = NULL;
5151 view->maxx = MAX(view->maxx, width);
5153 if (nprinted == s->selected_line - 1)
5154 wstandout(view->window);
5156 if (blame->nlines > 0) {
5157 blame_line = &blame->lines[lineno - 1];
5158 if (blame_line->annotated && prev_id &&
5159 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5160 !(nprinted == s->selected_line - 1)) {
5161 waddstr(view->window, " ");
5162 } else if (blame_line->annotated) {
5163 char *id_str;
5164 err = got_object_id_str(&id_str,
5165 blame_line->id);
5166 if (err) {
5167 free(line);
5168 return err;
5170 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5171 if (tc)
5172 wattr_on(view->window,
5173 COLOR_PAIR(tc->colorpair), NULL);
5174 wprintw(view->window, "%.8s", id_str);
5175 if (tc)
5176 wattr_off(view->window,
5177 COLOR_PAIR(tc->colorpair), NULL);
5178 free(id_str);
5179 prev_id = blame_line->id;
5180 } else {
5181 waddstr(view->window, "........");
5182 prev_id = NULL;
5184 } else {
5185 waddstr(view->window, "........");
5186 prev_id = NULL;
5189 if (nprinted == s->selected_line - 1)
5190 wstandend(view->window);
5191 waddstr(view->window, " ");
5193 if (view->ncols <= 9) {
5194 width = 9;
5195 } else if (s->first_displayed_line + nprinted ==
5196 s->matched_line &&
5197 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5198 err = add_matched_line(&width, line, view->ncols - 9, 9,
5199 view->window, view->x, regmatch);
5200 if (err) {
5201 free(line);
5202 return err;
5204 width += 9;
5205 } else {
5206 int skip;
5207 err = format_line(&wline, &width, &skip, line,
5208 view->x, view->ncols - 9, 9, 1);
5209 if (err) {
5210 free(line);
5211 return err;
5213 waddwstr(view->window, &wline[skip]);
5214 width += 9;
5215 free(wline);
5216 wline = NULL;
5219 if (width <= view->ncols - 1)
5220 waddch(view->window, '\n');
5221 if (++nprinted == 1)
5222 s->first_displayed_line = lineno;
5224 free(line);
5225 s->last_displayed_line = lineno;
5227 view_border(view);
5229 return NULL;
5232 static const struct got_error *
5233 blame_cb(void *arg, int nlines, int lineno,
5234 struct got_commit_object *commit, struct got_object_id *id)
5236 const struct got_error *err = NULL;
5237 struct tog_blame_cb_args *a = arg;
5238 struct tog_blame_line *line;
5239 int errcode;
5241 if (nlines != a->nlines ||
5242 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5243 return got_error(GOT_ERR_RANGE);
5245 errcode = pthread_mutex_lock(&tog_mutex);
5246 if (errcode)
5247 return got_error_set_errno(errcode, "pthread_mutex_lock");
5249 if (*a->quit) { /* user has quit the blame view */
5250 err = got_error(GOT_ERR_ITER_COMPLETED);
5251 goto done;
5254 if (lineno == -1)
5255 goto done; /* no change in this commit */
5257 line = &a->lines[lineno - 1];
5258 if (line->annotated)
5259 goto done;
5261 line->id = got_object_id_dup(id);
5262 if (line->id == NULL) {
5263 err = got_error_from_errno("got_object_id_dup");
5264 goto done;
5266 line->annotated = 1;
5267 done:
5268 errcode = pthread_mutex_unlock(&tog_mutex);
5269 if (errcode)
5270 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5271 return err;
5274 static void *
5275 blame_thread(void *arg)
5277 const struct got_error *err, *close_err;
5278 struct tog_blame_thread_args *ta = arg;
5279 struct tog_blame_cb_args *a = ta->cb_args;
5280 int errcode, fd1 = -1, fd2 = -1;
5281 FILE *f1 = NULL, *f2 = NULL;
5283 fd1 = got_opentempfd();
5284 if (fd1 == -1)
5285 return (void *)got_error_from_errno("got_opentempfd");
5287 fd2 = got_opentempfd();
5288 if (fd2 == -1) {
5289 err = got_error_from_errno("got_opentempfd");
5290 goto done;
5293 f1 = got_opentemp();
5294 if (f1 == NULL) {
5295 err = (void *)got_error_from_errno("got_opentemp");
5296 goto done;
5298 f2 = got_opentemp();
5299 if (f2 == NULL) {
5300 err = (void *)got_error_from_errno("got_opentemp");
5301 goto done;
5304 err = block_signals_used_by_main_thread();
5305 if (err)
5306 goto done;
5308 err = got_blame(ta->path, a->commit_id, ta->repo,
5309 tog_diff_algo, blame_cb, ta->cb_args,
5310 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5311 if (err && err->code == GOT_ERR_CANCELLED)
5312 err = NULL;
5314 errcode = pthread_mutex_lock(&tog_mutex);
5315 if (errcode) {
5316 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5317 goto done;
5320 close_err = got_repo_close(ta->repo);
5321 if (err == NULL)
5322 err = close_err;
5323 ta->repo = NULL;
5324 *ta->complete = 1;
5326 errcode = pthread_mutex_unlock(&tog_mutex);
5327 if (errcode && err == NULL)
5328 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5330 done:
5331 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5332 err = got_error_from_errno("close");
5333 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5334 err = got_error_from_errno("close");
5335 if (f1 && fclose(f1) == EOF && err == NULL)
5336 err = got_error_from_errno("fclose");
5337 if (f2 && fclose(f2) == EOF && err == NULL)
5338 err = got_error_from_errno("fclose");
5340 return (void *)err;
5343 static struct got_object_id *
5344 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5345 int first_displayed_line, int selected_line)
5347 struct tog_blame_line *line;
5349 if (nlines <= 0)
5350 return NULL;
5352 line = &lines[first_displayed_line - 1 + selected_line - 1];
5353 if (!line->annotated)
5354 return NULL;
5356 return line->id;
5359 static struct got_object_id *
5360 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5361 int lineno)
5363 struct tog_blame_line *line;
5365 if (nlines <= 0 || lineno >= nlines)
5366 return NULL;
5368 line = &lines[lineno - 1];
5369 if (!line->annotated)
5370 return NULL;
5372 return line->id;
5375 static const struct got_error *
5376 stop_blame(struct tog_blame *blame)
5378 const struct got_error *err = NULL;
5379 int i;
5381 if (blame->thread) {
5382 int errcode;
5383 errcode = pthread_mutex_unlock(&tog_mutex);
5384 if (errcode)
5385 return got_error_set_errno(errcode,
5386 "pthread_mutex_unlock");
5387 errcode = pthread_join(blame->thread, (void **)&err);
5388 if (errcode)
5389 return got_error_set_errno(errcode, "pthread_join");
5390 errcode = pthread_mutex_lock(&tog_mutex);
5391 if (errcode)
5392 return got_error_set_errno(errcode,
5393 "pthread_mutex_lock");
5394 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5395 err = NULL;
5396 blame->thread = 0; //NULL;
5398 if (blame->thread_args.repo) {
5399 const struct got_error *close_err;
5400 close_err = got_repo_close(blame->thread_args.repo);
5401 if (err == NULL)
5402 err = close_err;
5403 blame->thread_args.repo = NULL;
5405 if (blame->f) {
5406 if (fclose(blame->f) == EOF && err == NULL)
5407 err = got_error_from_errno("fclose");
5408 blame->f = NULL;
5410 if (blame->lines) {
5411 for (i = 0; i < blame->nlines; i++)
5412 free(blame->lines[i].id);
5413 free(blame->lines);
5414 blame->lines = NULL;
5416 free(blame->cb_args.commit_id);
5417 blame->cb_args.commit_id = NULL;
5418 if (blame->pack_fds) {
5419 const struct got_error *pack_err =
5420 got_repo_pack_fds_close(blame->pack_fds);
5421 if (err == NULL)
5422 err = pack_err;
5423 blame->pack_fds = NULL;
5425 return err;
5428 static const struct got_error *
5429 cancel_blame_view(void *arg)
5431 const struct got_error *err = NULL;
5432 int *done = arg;
5433 int errcode;
5435 errcode = pthread_mutex_lock(&tog_mutex);
5436 if (errcode)
5437 return got_error_set_errno(errcode,
5438 "pthread_mutex_unlock");
5440 if (*done)
5441 err = got_error(GOT_ERR_CANCELLED);
5443 errcode = pthread_mutex_unlock(&tog_mutex);
5444 if (errcode)
5445 return got_error_set_errno(errcode,
5446 "pthread_mutex_lock");
5448 return err;
5451 static const struct got_error *
5452 run_blame(struct tog_view *view)
5454 struct tog_blame_view_state *s = &view->state.blame;
5455 struct tog_blame *blame = &s->blame;
5456 const struct got_error *err = NULL;
5457 struct got_commit_object *commit = NULL;
5458 struct got_blob_object *blob = NULL;
5459 struct got_repository *thread_repo = NULL;
5460 struct got_object_id *obj_id = NULL;
5461 int obj_type, fd = -1;
5462 int *pack_fds = NULL;
5464 err = got_object_open_as_commit(&commit, s->repo,
5465 &s->blamed_commit->id);
5466 if (err)
5467 return err;
5469 fd = got_opentempfd();
5470 if (fd == -1) {
5471 err = got_error_from_errno("got_opentempfd");
5472 goto done;
5475 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5476 if (err)
5477 goto done;
5479 err = got_object_get_type(&obj_type, s->repo, obj_id);
5480 if (err)
5481 goto done;
5483 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5484 err = got_error(GOT_ERR_OBJ_TYPE);
5485 goto done;
5488 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5489 if (err)
5490 goto done;
5491 blame->f = got_opentemp();
5492 if (blame->f == NULL) {
5493 err = got_error_from_errno("got_opentemp");
5494 goto done;
5496 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5497 &blame->line_offsets, blame->f, blob);
5498 if (err)
5499 goto done;
5500 if (blame->nlines == 0) {
5501 s->blame_complete = 1;
5502 goto done;
5505 /* Don't include \n at EOF in the blame line count. */
5506 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5507 blame->nlines--;
5509 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5510 if (blame->lines == NULL) {
5511 err = got_error_from_errno("calloc");
5512 goto done;
5515 err = got_repo_pack_fds_open(&pack_fds);
5516 if (err)
5517 goto done;
5518 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5519 pack_fds);
5520 if (err)
5521 goto done;
5523 blame->pack_fds = pack_fds;
5524 blame->cb_args.view = view;
5525 blame->cb_args.lines = blame->lines;
5526 blame->cb_args.nlines = blame->nlines;
5527 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5528 if (blame->cb_args.commit_id == NULL) {
5529 err = got_error_from_errno("got_object_id_dup");
5530 goto done;
5532 blame->cb_args.quit = &s->done;
5534 blame->thread_args.path = s->path;
5535 blame->thread_args.repo = thread_repo;
5536 blame->thread_args.cb_args = &blame->cb_args;
5537 blame->thread_args.complete = &s->blame_complete;
5538 blame->thread_args.cancel_cb = cancel_blame_view;
5539 blame->thread_args.cancel_arg = &s->done;
5540 s->blame_complete = 0;
5542 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5543 s->first_displayed_line = 1;
5544 s->last_displayed_line = view->nlines;
5545 s->selected_line = 1;
5547 s->matched_line = 0;
5549 done:
5550 if (commit)
5551 got_object_commit_close(commit);
5552 if (fd != -1 && close(fd) == -1 && err == NULL)
5553 err = got_error_from_errno("close");
5554 if (blob)
5555 got_object_blob_close(blob);
5556 free(obj_id);
5557 if (err)
5558 stop_blame(blame);
5559 return err;
5562 static const struct got_error *
5563 open_blame_view(struct tog_view *view, char *path,
5564 struct got_object_id *commit_id, struct got_repository *repo)
5566 const struct got_error *err = NULL;
5567 struct tog_blame_view_state *s = &view->state.blame;
5569 STAILQ_INIT(&s->blamed_commits);
5571 s->path = strdup(path);
5572 if (s->path == NULL)
5573 return got_error_from_errno("strdup");
5575 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5576 if (err) {
5577 free(s->path);
5578 return err;
5581 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5582 s->first_displayed_line = 1;
5583 s->last_displayed_line = view->nlines;
5584 s->selected_line = 1;
5585 s->blame_complete = 0;
5586 s->repo = repo;
5587 s->commit_id = commit_id;
5588 memset(&s->blame, 0, sizeof(s->blame));
5590 STAILQ_INIT(&s->colors);
5591 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5592 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5593 get_color_value("TOG_COLOR_COMMIT"));
5594 if (err)
5595 return err;
5598 view->show = show_blame_view;
5599 view->input = input_blame_view;
5600 view->reset = reset_blame_view;
5601 view->close = close_blame_view;
5602 view->search_start = search_start_blame_view;
5603 view->search_next = search_next_blame_view;
5605 return run_blame(view);
5608 static const struct got_error *
5609 close_blame_view(struct tog_view *view)
5611 const struct got_error *err = NULL;
5612 struct tog_blame_view_state *s = &view->state.blame;
5614 if (s->blame.thread)
5615 err = stop_blame(&s->blame);
5617 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5618 struct got_object_qid *blamed_commit;
5619 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5620 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5621 got_object_qid_free(blamed_commit);
5624 free(s->path);
5625 free_colors(&s->colors);
5626 return err;
5629 static const struct got_error *
5630 search_start_blame_view(struct tog_view *view)
5632 struct tog_blame_view_state *s = &view->state.blame;
5634 s->matched_line = 0;
5635 return NULL;
5638 static const struct got_error *
5639 search_next_blame_view(struct tog_view *view)
5641 struct tog_blame_view_state *s = &view->state.blame;
5642 const struct got_error *err = NULL;
5643 int lineno;
5644 char *line = NULL;
5645 size_t linesize = 0;
5646 ssize_t linelen;
5648 if (!view->searching) {
5649 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5650 return NULL;
5653 if (s->matched_line) {
5654 if (view->searching == TOG_SEARCH_FORWARD)
5655 lineno = s->matched_line + 1;
5656 else
5657 lineno = s->matched_line - 1;
5658 } else
5659 lineno = s->first_displayed_line - 1 + s->selected_line;
5661 while (1) {
5662 off_t offset;
5664 if (lineno <= 0 || lineno > s->blame.nlines) {
5665 if (s->matched_line == 0) {
5666 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5667 break;
5670 if (view->searching == TOG_SEARCH_FORWARD)
5671 lineno = 1;
5672 else
5673 lineno = s->blame.nlines;
5676 offset = s->blame.line_offsets[lineno - 1];
5677 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5678 free(line);
5679 return got_error_from_errno("fseeko");
5681 linelen = getline(&line, &linesize, s->blame.f);
5682 if (linelen != -1) {
5683 char *exstr;
5684 err = expand_tab(&exstr, line);
5685 if (err)
5686 break;
5687 if (match_line(exstr, &view->regex, 1,
5688 &view->regmatch)) {
5689 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5690 s->matched_line = lineno;
5691 free(exstr);
5692 break;
5694 free(exstr);
5696 if (view->searching == TOG_SEARCH_FORWARD)
5697 lineno++;
5698 else
5699 lineno--;
5701 free(line);
5703 if (s->matched_line) {
5704 s->first_displayed_line = s->matched_line;
5705 s->selected_line = 1;
5708 return err;
5711 static const struct got_error *
5712 show_blame_view(struct tog_view *view)
5714 const struct got_error *err = NULL;
5715 struct tog_blame_view_state *s = &view->state.blame;
5716 int errcode;
5718 if (s->blame.thread == 0 && !s->blame_complete) {
5719 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5720 &s->blame.thread_args);
5721 if (errcode)
5722 return got_error_set_errno(errcode, "pthread_create");
5724 halfdelay(1); /* fast refresh while annotating */
5727 if (s->blame_complete)
5728 halfdelay(10); /* disable fast refresh */
5730 err = draw_blame(view);
5732 view_border(view);
5733 return err;
5736 static const struct got_error *
5737 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
5738 struct got_repository *repo, struct got_object_id *id)
5740 struct tog_view *log_view;
5741 const struct got_error *err = NULL;
5743 *new_view = NULL;
5745 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
5746 if (log_view == NULL)
5747 return got_error_from_errno("view_open");
5749 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
5750 if (err)
5751 view_close(log_view);
5752 else
5753 *new_view = log_view;
5755 return err;
5758 static const struct got_error *
5759 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5761 const struct got_error *err = NULL, *thread_err = NULL;
5762 struct tog_view *diff_view, *log_view;
5763 struct tog_blame_view_state *s = &view->state.blame;
5764 int eos, nscroll, begin_y = 0, begin_x = 0;
5766 eos = nscroll = view->nlines - 2;
5767 if (view_is_hsplit_top(view))
5768 --eos; /* border */
5770 switch (ch) {
5771 case '0':
5772 view->x = 0;
5773 break;
5774 case '$':
5775 view->x = MAX(view->maxx - view->ncols / 3, 0);
5776 view->count = 0;
5777 break;
5778 case KEY_RIGHT:
5779 case 'l':
5780 if (view->x + view->ncols / 3 < view->maxx)
5781 view->x += 2; /* move two columns right */
5782 else
5783 view->count = 0;
5784 break;
5785 case KEY_LEFT:
5786 case 'h':
5787 view->x -= MIN(view->x, 2); /* move two columns back */
5788 if (view->x <= 0)
5789 view->count = 0;
5790 break;
5791 case 'q':
5792 s->done = 1;
5793 break;
5794 case 'g':
5795 case KEY_HOME:
5796 s->selected_line = 1;
5797 s->first_displayed_line = 1;
5798 view->count = 0;
5799 break;
5800 case 'G':
5801 case KEY_END:
5802 if (s->blame.nlines < eos) {
5803 s->selected_line = s->blame.nlines;
5804 s->first_displayed_line = 1;
5805 } else {
5806 s->selected_line = eos;
5807 s->first_displayed_line = s->blame.nlines - (eos - 1);
5809 view->count = 0;
5810 break;
5811 case 'k':
5812 case KEY_UP:
5813 case CTRL('p'):
5814 if (s->selected_line > 1)
5815 s->selected_line--;
5816 else if (s->selected_line == 1 &&
5817 s->first_displayed_line > 1)
5818 s->first_displayed_line--;
5819 else
5820 view->count = 0;
5821 break;
5822 case CTRL('u'):
5823 case 'u':
5824 nscroll /= 2;
5825 /* FALL THROUGH */
5826 case KEY_PPAGE:
5827 case CTRL('b'):
5828 case 'b':
5829 if (s->first_displayed_line == 1) {
5830 if (view->count > 1)
5831 nscroll += nscroll;
5832 s->selected_line = MAX(1, s->selected_line - nscroll);
5833 view->count = 0;
5834 break;
5836 if (s->first_displayed_line > nscroll)
5837 s->first_displayed_line -= nscroll;
5838 else
5839 s->first_displayed_line = 1;
5840 break;
5841 case 'j':
5842 case KEY_DOWN:
5843 case CTRL('n'):
5844 if (s->selected_line < eos && s->first_displayed_line +
5845 s->selected_line <= s->blame.nlines)
5846 s->selected_line++;
5847 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5848 s->first_displayed_line++;
5849 else
5850 view->count = 0;
5851 break;
5852 case 'c':
5853 case 'p': {
5854 struct got_object_id *id = NULL;
5856 view->count = 0;
5857 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5858 s->first_displayed_line, s->selected_line);
5859 if (id == NULL)
5860 break;
5861 if (ch == 'p') {
5862 struct got_commit_object *commit, *pcommit;
5863 struct got_object_qid *pid;
5864 struct got_object_id *blob_id = NULL;
5865 int obj_type;
5866 err = got_object_open_as_commit(&commit,
5867 s->repo, id);
5868 if (err)
5869 break;
5870 pid = STAILQ_FIRST(
5871 got_object_commit_get_parent_ids(commit));
5872 if (pid == NULL) {
5873 got_object_commit_close(commit);
5874 break;
5876 /* Check if path history ends here. */
5877 err = got_object_open_as_commit(&pcommit,
5878 s->repo, &pid->id);
5879 if (err)
5880 break;
5881 err = got_object_id_by_path(&blob_id, s->repo,
5882 pcommit, s->path);
5883 got_object_commit_close(pcommit);
5884 if (err) {
5885 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5886 err = NULL;
5887 got_object_commit_close(commit);
5888 break;
5890 err = got_object_get_type(&obj_type, s->repo,
5891 blob_id);
5892 free(blob_id);
5893 /* Can't blame non-blob type objects. */
5894 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5895 got_object_commit_close(commit);
5896 break;
5898 err = got_object_qid_alloc(&s->blamed_commit,
5899 &pid->id);
5900 got_object_commit_close(commit);
5901 } else {
5902 if (got_object_id_cmp(id,
5903 &s->blamed_commit->id) == 0)
5904 break;
5905 err = got_object_qid_alloc(&s->blamed_commit,
5906 id);
5908 if (err)
5909 break;
5910 s->done = 1;
5911 thread_err = stop_blame(&s->blame);
5912 s->done = 0;
5913 if (thread_err)
5914 break;
5915 STAILQ_INSERT_HEAD(&s->blamed_commits,
5916 s->blamed_commit, entry);
5917 err = run_blame(view);
5918 if (err)
5919 break;
5920 break;
5922 case 'C': {
5923 struct got_object_qid *first;
5925 view->count = 0;
5926 first = STAILQ_FIRST(&s->blamed_commits);
5927 if (!got_object_id_cmp(&first->id, s->commit_id))
5928 break;
5929 s->done = 1;
5930 thread_err = stop_blame(&s->blame);
5931 s->done = 0;
5932 if (thread_err)
5933 break;
5934 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5935 got_object_qid_free(s->blamed_commit);
5936 s->blamed_commit =
5937 STAILQ_FIRST(&s->blamed_commits);
5938 err = run_blame(view);
5939 if (err)
5940 break;
5941 break;
5943 case 'L': {
5944 struct got_object_id *id = NULL;
5946 view->count = 0;
5947 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5948 s->first_displayed_line, s->selected_line);
5949 if (id == NULL)
5950 break;
5952 if (view_is_parent_view(view))
5953 view_get_split(view, &begin_y, &begin_x);
5954 err = log_annotated_line(&log_view, begin_y, begin_x,
5955 s->repo, id);
5956 if (err)
5957 break;
5958 if (view_is_parent_view(view) &&
5959 view->mode == TOG_VIEW_SPLIT_HRZN) {
5960 err = view_init_hsplit(view, begin_y);
5961 if (err)
5962 break;
5965 view->focussed = 0;
5966 log_view->focussed = 1;
5967 log_view->mode = view->mode;
5968 log_view->nlines = view->lines - begin_y;
5969 if (view_is_parent_view(view)) {
5970 view_transfer_size(log_view, view);
5971 err = view_close_child(view);
5972 if (err)
5973 return err;
5974 err = view_set_child(view, log_view);
5975 if (err)
5976 return err;
5977 view->focus_child = 1;
5978 } else
5979 *new_view = log_view;
5980 break;
5982 case KEY_ENTER:
5983 case '\r': {
5984 struct got_object_id *id = NULL;
5985 struct got_object_qid *pid;
5986 struct got_commit_object *commit = NULL;
5988 view->count = 0;
5989 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5990 s->first_displayed_line, s->selected_line);
5991 if (id == NULL)
5992 break;
5993 err = got_object_open_as_commit(&commit, s->repo, id);
5994 if (err)
5995 break;
5996 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5997 if (*new_view) {
5998 /* traversed from diff view, release diff resources */
5999 err = close_diff_view(*new_view);
6000 if (err)
6001 break;
6002 diff_view = *new_view;
6003 } else {
6004 if (view_is_parent_view(view))
6005 view_get_split(view, &begin_y, &begin_x);
6007 diff_view = view_open(0, 0, begin_y, begin_x,
6008 TOG_VIEW_DIFF);
6009 if (diff_view == NULL) {
6010 got_object_commit_close(commit);
6011 err = got_error_from_errno("view_open");
6012 break;
6015 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6016 id, NULL, NULL, 3, 0, 0, view, s->repo);
6017 got_object_commit_close(commit);
6018 if (err) {
6019 view_close(diff_view);
6020 break;
6022 s->last_diffed_line = s->first_displayed_line - 1 +
6023 s->selected_line;
6024 if (*new_view)
6025 break; /* still open from active diff view */
6026 if (view_is_parent_view(view) &&
6027 view->mode == TOG_VIEW_SPLIT_HRZN) {
6028 err = view_init_hsplit(view, begin_y);
6029 if (err)
6030 break;
6033 view->focussed = 0;
6034 diff_view->focussed = 1;
6035 diff_view->mode = view->mode;
6036 diff_view->nlines = view->lines - begin_y;
6037 if (view_is_parent_view(view)) {
6038 view_transfer_size(diff_view, view);
6039 err = view_close_child(view);
6040 if (err)
6041 break;
6042 err = view_set_child(view, diff_view);
6043 if (err)
6044 break;
6045 view->focus_child = 1;
6046 } else
6047 *new_view = diff_view;
6048 if (err)
6049 break;
6050 break;
6052 case CTRL('d'):
6053 case 'd':
6054 nscroll /= 2;
6055 /* FALL THROUGH */
6056 case KEY_NPAGE:
6057 case CTRL('f'):
6058 case 'f':
6059 case ' ':
6060 if (s->last_displayed_line >= s->blame.nlines &&
6061 s->selected_line >= MIN(s->blame.nlines,
6062 view->nlines - 2)) {
6063 view->count = 0;
6064 break;
6066 if (s->last_displayed_line >= s->blame.nlines &&
6067 s->selected_line < view->nlines - 2) {
6068 s->selected_line +=
6069 MIN(nscroll, s->last_displayed_line -
6070 s->first_displayed_line - s->selected_line + 1);
6072 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6073 s->first_displayed_line += nscroll;
6074 else
6075 s->first_displayed_line =
6076 s->blame.nlines - (view->nlines - 3);
6077 break;
6078 case KEY_RESIZE:
6079 if (s->selected_line > view->nlines - 2) {
6080 s->selected_line = MIN(s->blame.nlines,
6081 view->nlines - 2);
6083 break;
6084 default:
6085 view->count = 0;
6086 break;
6088 return thread_err ? thread_err : err;
6091 static const struct got_error *
6092 reset_blame_view(struct tog_view *view)
6094 const struct got_error *err;
6095 struct tog_blame_view_state *s = &view->state.blame;
6097 view->count = 0;
6098 s->done = 1;
6099 err = stop_blame(&s->blame);
6100 s->done = 0;
6101 if (err)
6102 return err;
6103 return run_blame(view);
6106 static const struct got_error *
6107 cmd_blame(int argc, char *argv[])
6109 const struct got_error *error;
6110 struct got_repository *repo = NULL;
6111 struct got_worktree *worktree = NULL;
6112 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6113 char *link_target = NULL;
6114 struct got_object_id *commit_id = NULL;
6115 struct got_commit_object *commit = NULL;
6116 char *commit_id_str = NULL;
6117 int ch;
6118 struct tog_view *view;
6119 int *pack_fds = NULL;
6121 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6122 switch (ch) {
6123 case 'c':
6124 commit_id_str = optarg;
6125 break;
6126 case 'r':
6127 repo_path = realpath(optarg, NULL);
6128 if (repo_path == NULL)
6129 return got_error_from_errno2("realpath",
6130 optarg);
6131 break;
6132 default:
6133 usage_blame();
6134 /* NOTREACHED */
6138 argc -= optind;
6139 argv += optind;
6141 if (argc != 1)
6142 usage_blame();
6144 error = got_repo_pack_fds_open(&pack_fds);
6145 if (error != NULL)
6146 goto done;
6148 if (repo_path == NULL) {
6149 cwd = getcwd(NULL, 0);
6150 if (cwd == NULL)
6151 return got_error_from_errno("getcwd");
6152 error = got_worktree_open(&worktree, cwd);
6153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6154 goto done;
6155 if (worktree)
6156 repo_path =
6157 strdup(got_worktree_get_repo_path(worktree));
6158 else
6159 repo_path = strdup(cwd);
6160 if (repo_path == NULL) {
6161 error = got_error_from_errno("strdup");
6162 goto done;
6166 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6167 if (error != NULL)
6168 goto done;
6170 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6171 worktree);
6172 if (error)
6173 goto done;
6175 init_curses();
6177 error = apply_unveil(got_repo_get_path(repo), NULL);
6178 if (error)
6179 goto done;
6181 error = tog_load_refs(repo, 0);
6182 if (error)
6183 goto done;
6185 if (commit_id_str == NULL) {
6186 struct got_reference *head_ref;
6187 error = got_ref_open(&head_ref, repo, worktree ?
6188 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6189 if (error != NULL)
6190 goto done;
6191 error = got_ref_resolve(&commit_id, repo, head_ref);
6192 got_ref_close(head_ref);
6193 } else {
6194 error = got_repo_match_object_id(&commit_id, NULL,
6195 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6197 if (error != NULL)
6198 goto done;
6200 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6201 if (view == NULL) {
6202 error = got_error_from_errno("view_open");
6203 goto done;
6206 error = got_object_open_as_commit(&commit, repo, commit_id);
6207 if (error)
6208 goto done;
6210 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6211 commit, repo);
6212 if (error)
6213 goto done;
6215 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6216 commit_id, repo);
6217 if (error)
6218 goto done;
6219 if (worktree) {
6220 /* Release work tree lock. */
6221 got_worktree_close(worktree);
6222 worktree = NULL;
6224 error = view_loop(view);
6225 done:
6226 free(repo_path);
6227 free(in_repo_path);
6228 free(link_target);
6229 free(cwd);
6230 free(commit_id);
6231 if (commit)
6232 got_object_commit_close(commit);
6233 if (worktree)
6234 got_worktree_close(worktree);
6235 if (repo) {
6236 const struct got_error *close_err = got_repo_close(repo);
6237 if (error == NULL)
6238 error = close_err;
6240 if (pack_fds) {
6241 const struct got_error *pack_err =
6242 got_repo_pack_fds_close(pack_fds);
6243 if (error == NULL)
6244 error = pack_err;
6246 tog_free_refs();
6247 return error;
6250 static const struct got_error *
6251 draw_tree_entries(struct tog_view *view, const char *parent_path)
6253 struct tog_tree_view_state *s = &view->state.tree;
6254 const struct got_error *err = NULL;
6255 struct got_tree_entry *te;
6256 wchar_t *wline;
6257 struct tog_color *tc;
6258 int width, n, i, nentries;
6259 int limit = view->nlines;
6261 s->ndisplayed = 0;
6262 if (view_is_hsplit_top(view))
6263 --limit; /* border */
6265 werase(view->window);
6267 if (limit == 0)
6268 return NULL;
6270 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6271 0, 0);
6272 if (err)
6273 return err;
6274 if (view_needs_focus_indication(view))
6275 wstandout(view->window);
6276 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6277 if (tc)
6278 wattr_on(view->window,
6279 COLOR_PAIR(tc->colorpair), NULL);
6280 waddwstr(view->window, wline);
6281 if (tc)
6282 wattr_off(view->window,
6283 COLOR_PAIR(tc->colorpair), NULL);
6284 if (view_needs_focus_indication(view))
6285 wstandend(view->window);
6286 free(wline);
6287 wline = NULL;
6288 if (width < view->ncols - 1)
6289 waddch(view->window, '\n');
6290 if (--limit <= 0)
6291 return NULL;
6292 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6293 0, 0);
6294 if (err)
6295 return err;
6296 waddwstr(view->window, wline);
6297 free(wline);
6298 wline = NULL;
6299 if (width < view->ncols - 1)
6300 waddch(view->window, '\n');
6301 if (--limit <= 0)
6302 return NULL;
6303 waddch(view->window, '\n');
6304 if (--limit <= 0)
6305 return NULL;
6307 if (s->first_displayed_entry == NULL) {
6308 te = got_object_tree_get_first_entry(s->tree);
6309 if (s->selected == 0) {
6310 if (view->focussed)
6311 wstandout(view->window);
6312 s->selected_entry = NULL;
6314 waddstr(view->window, " ..\n"); /* parent directory */
6315 if (s->selected == 0 && view->focussed)
6316 wstandend(view->window);
6317 s->ndisplayed++;
6318 if (--limit <= 0)
6319 return NULL;
6320 n = 1;
6321 } else {
6322 n = 0;
6323 te = s->first_displayed_entry;
6326 nentries = got_object_tree_get_nentries(s->tree);
6327 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6328 char *line = NULL, *id_str = NULL, *link_target = NULL;
6329 const char *modestr = "";
6330 mode_t mode;
6332 te = got_object_tree_get_entry(s->tree, i);
6333 mode = got_tree_entry_get_mode(te);
6335 if (s->show_ids) {
6336 err = got_object_id_str(&id_str,
6337 got_tree_entry_get_id(te));
6338 if (err)
6339 return got_error_from_errno(
6340 "got_object_id_str");
6342 if (got_object_tree_entry_is_submodule(te))
6343 modestr = "$";
6344 else if (S_ISLNK(mode)) {
6345 int i;
6347 err = got_tree_entry_get_symlink_target(&link_target,
6348 te, s->repo);
6349 if (err) {
6350 free(id_str);
6351 return err;
6353 for (i = 0; i < strlen(link_target); i++) {
6354 if (!isprint((unsigned char)link_target[i]))
6355 link_target[i] = '?';
6357 modestr = "@";
6359 else if (S_ISDIR(mode))
6360 modestr = "/";
6361 else if (mode & S_IXUSR)
6362 modestr = "*";
6363 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6364 got_tree_entry_get_name(te), modestr,
6365 link_target ? " -> ": "",
6366 link_target ? link_target : "") == -1) {
6367 free(id_str);
6368 free(link_target);
6369 return got_error_from_errno("asprintf");
6371 free(id_str);
6372 free(link_target);
6373 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6374 0, 0);
6375 if (err) {
6376 free(line);
6377 break;
6379 if (n == s->selected) {
6380 if (view->focussed)
6381 wstandout(view->window);
6382 s->selected_entry = te;
6384 tc = match_color(&s->colors, line);
6385 if (tc)
6386 wattr_on(view->window,
6387 COLOR_PAIR(tc->colorpair), NULL);
6388 waddwstr(view->window, wline);
6389 if (tc)
6390 wattr_off(view->window,
6391 COLOR_PAIR(tc->colorpair), NULL);
6392 if (width < view->ncols - 1)
6393 waddch(view->window, '\n');
6394 if (n == s->selected && view->focussed)
6395 wstandend(view->window);
6396 free(line);
6397 free(wline);
6398 wline = NULL;
6399 n++;
6400 s->ndisplayed++;
6401 s->last_displayed_entry = te;
6402 if (--limit <= 0)
6403 break;
6406 return err;
6409 static void
6410 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6412 struct got_tree_entry *te;
6413 int isroot = s->tree == s->root;
6414 int i = 0;
6416 if (s->first_displayed_entry == NULL)
6417 return;
6419 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6420 while (i++ < maxscroll) {
6421 if (te == NULL) {
6422 if (!isroot)
6423 s->first_displayed_entry = NULL;
6424 break;
6426 s->first_displayed_entry = te;
6427 te = got_tree_entry_get_prev(s->tree, te);
6431 static const struct got_error *
6432 tree_scroll_down(struct tog_view *view, int maxscroll)
6434 struct tog_tree_view_state *s = &view->state.tree;
6435 struct got_tree_entry *next, *last;
6436 int n = 0;
6438 if (s->first_displayed_entry)
6439 next = got_tree_entry_get_next(s->tree,
6440 s->first_displayed_entry);
6441 else
6442 next = got_object_tree_get_first_entry(s->tree);
6444 last = s->last_displayed_entry;
6445 while (next && n++ < maxscroll) {
6446 if (last)
6447 last = got_tree_entry_get_next(s->tree, last);
6448 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6449 s->first_displayed_entry = next;
6450 next = got_tree_entry_get_next(s->tree, next);
6454 return NULL;
6457 static const struct got_error *
6458 tree_entry_path(char **path, struct tog_parent_trees *parents,
6459 struct got_tree_entry *te)
6461 const struct got_error *err = NULL;
6462 struct tog_parent_tree *pt;
6463 size_t len = 2; /* for leading slash and NUL */
6465 TAILQ_FOREACH(pt, parents, entry)
6466 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6467 + 1 /* slash */;
6468 if (te)
6469 len += strlen(got_tree_entry_get_name(te));
6471 *path = calloc(1, len);
6472 if (path == NULL)
6473 return got_error_from_errno("calloc");
6475 (*path)[0] = '/';
6476 pt = TAILQ_LAST(parents, tog_parent_trees);
6477 while (pt) {
6478 const char *name = got_tree_entry_get_name(pt->selected_entry);
6479 if (strlcat(*path, name, len) >= len) {
6480 err = got_error(GOT_ERR_NO_SPACE);
6481 goto done;
6483 if (strlcat(*path, "/", len) >= len) {
6484 err = got_error(GOT_ERR_NO_SPACE);
6485 goto done;
6487 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6489 if (te) {
6490 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6491 err = got_error(GOT_ERR_NO_SPACE);
6492 goto done;
6495 done:
6496 if (err) {
6497 free(*path);
6498 *path = NULL;
6500 return err;
6503 static const struct got_error *
6504 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6505 struct got_tree_entry *te, struct tog_parent_trees *parents,
6506 struct got_object_id *commit_id, struct got_repository *repo)
6508 const struct got_error *err = NULL;
6509 char *path;
6510 struct tog_view *blame_view;
6512 *new_view = NULL;
6514 err = tree_entry_path(&path, parents, te);
6515 if (err)
6516 return err;
6518 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6519 if (blame_view == NULL) {
6520 err = got_error_from_errno("view_open");
6521 goto done;
6524 err = open_blame_view(blame_view, path, commit_id, repo);
6525 if (err) {
6526 if (err->code == GOT_ERR_CANCELLED)
6527 err = NULL;
6528 view_close(blame_view);
6529 } else
6530 *new_view = blame_view;
6531 done:
6532 free(path);
6533 return err;
6536 static const struct got_error *
6537 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6538 struct tog_tree_view_state *s)
6540 struct tog_view *log_view;
6541 const struct got_error *err = NULL;
6542 char *path;
6544 *new_view = NULL;
6546 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6547 if (log_view == NULL)
6548 return got_error_from_errno("view_open");
6550 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6551 if (err)
6552 return err;
6554 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6555 path, 0);
6556 if (err)
6557 view_close(log_view);
6558 else
6559 *new_view = log_view;
6560 free(path);
6561 return err;
6564 static const struct got_error *
6565 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6566 const char *head_ref_name, struct got_repository *repo)
6568 const struct got_error *err = NULL;
6569 char *commit_id_str = NULL;
6570 struct tog_tree_view_state *s = &view->state.tree;
6571 struct got_commit_object *commit = NULL;
6573 TAILQ_INIT(&s->parents);
6574 STAILQ_INIT(&s->colors);
6576 s->commit_id = got_object_id_dup(commit_id);
6577 if (s->commit_id == NULL)
6578 return got_error_from_errno("got_object_id_dup");
6580 err = got_object_open_as_commit(&commit, repo, commit_id);
6581 if (err)
6582 goto done;
6585 * The root is opened here and will be closed when the view is closed.
6586 * Any visited subtrees and their path-wise parents are opened and
6587 * closed on demand.
6589 err = got_object_open_as_tree(&s->root, repo,
6590 got_object_commit_get_tree_id(commit));
6591 if (err)
6592 goto done;
6593 s->tree = s->root;
6595 err = got_object_id_str(&commit_id_str, commit_id);
6596 if (err != NULL)
6597 goto done;
6599 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6600 err = got_error_from_errno("asprintf");
6601 goto done;
6604 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6605 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6606 if (head_ref_name) {
6607 s->head_ref_name = strdup(head_ref_name);
6608 if (s->head_ref_name == NULL) {
6609 err = got_error_from_errno("strdup");
6610 goto done;
6613 s->repo = repo;
6615 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6616 err = add_color(&s->colors, "\\$$",
6617 TOG_COLOR_TREE_SUBMODULE,
6618 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6619 if (err)
6620 goto done;
6621 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6622 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6623 if (err)
6624 goto done;
6625 err = add_color(&s->colors, "/$",
6626 TOG_COLOR_TREE_DIRECTORY,
6627 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6628 if (err)
6629 goto done;
6631 err = add_color(&s->colors, "\\*$",
6632 TOG_COLOR_TREE_EXECUTABLE,
6633 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6634 if (err)
6635 goto done;
6637 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6638 get_color_value("TOG_COLOR_COMMIT"));
6639 if (err)
6640 goto done;
6643 view->show = show_tree_view;
6644 view->input = input_tree_view;
6645 view->close = close_tree_view;
6646 view->search_start = search_start_tree_view;
6647 view->search_next = search_next_tree_view;
6648 done:
6649 free(commit_id_str);
6650 if (commit)
6651 got_object_commit_close(commit);
6652 if (err)
6653 close_tree_view(view);
6654 return err;
6657 static const struct got_error *
6658 close_tree_view(struct tog_view *view)
6660 struct tog_tree_view_state *s = &view->state.tree;
6662 free_colors(&s->colors);
6663 free(s->tree_label);
6664 s->tree_label = NULL;
6665 free(s->commit_id);
6666 s->commit_id = NULL;
6667 free(s->head_ref_name);
6668 s->head_ref_name = NULL;
6669 while (!TAILQ_EMPTY(&s->parents)) {
6670 struct tog_parent_tree *parent;
6671 parent = TAILQ_FIRST(&s->parents);
6672 TAILQ_REMOVE(&s->parents, parent, entry);
6673 if (parent->tree != s->root)
6674 got_object_tree_close(parent->tree);
6675 free(parent);
6678 if (s->tree != NULL && s->tree != s->root)
6679 got_object_tree_close(s->tree);
6680 if (s->root)
6681 got_object_tree_close(s->root);
6682 return NULL;
6685 static const struct got_error *
6686 search_start_tree_view(struct tog_view *view)
6688 struct tog_tree_view_state *s = &view->state.tree;
6690 s->matched_entry = NULL;
6691 return NULL;
6694 static int
6695 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6697 regmatch_t regmatch;
6699 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6700 0) == 0;
6703 static const struct got_error *
6704 search_next_tree_view(struct tog_view *view)
6706 struct tog_tree_view_state *s = &view->state.tree;
6707 struct got_tree_entry *te = NULL;
6709 if (!view->searching) {
6710 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6711 return NULL;
6714 if (s->matched_entry) {
6715 if (view->searching == TOG_SEARCH_FORWARD) {
6716 if (s->selected_entry)
6717 te = got_tree_entry_get_next(s->tree,
6718 s->selected_entry);
6719 else
6720 te = got_object_tree_get_first_entry(s->tree);
6721 } else {
6722 if (s->selected_entry == NULL)
6723 te = got_object_tree_get_last_entry(s->tree);
6724 else
6725 te = got_tree_entry_get_prev(s->tree,
6726 s->selected_entry);
6728 } else {
6729 if (s->selected_entry)
6730 te = s->selected_entry;
6731 else if (view->searching == TOG_SEARCH_FORWARD)
6732 te = got_object_tree_get_first_entry(s->tree);
6733 else
6734 te = got_object_tree_get_last_entry(s->tree);
6737 while (1) {
6738 if (te == NULL) {
6739 if (s->matched_entry == NULL) {
6740 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6741 return NULL;
6743 if (view->searching == TOG_SEARCH_FORWARD)
6744 te = got_object_tree_get_first_entry(s->tree);
6745 else
6746 te = got_object_tree_get_last_entry(s->tree);
6749 if (match_tree_entry(te, &view->regex)) {
6750 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6751 s->matched_entry = te;
6752 break;
6755 if (view->searching == TOG_SEARCH_FORWARD)
6756 te = got_tree_entry_get_next(s->tree, te);
6757 else
6758 te = got_tree_entry_get_prev(s->tree, te);
6761 if (s->matched_entry) {
6762 s->first_displayed_entry = s->matched_entry;
6763 s->selected = 0;
6766 return NULL;
6769 static const struct got_error *
6770 show_tree_view(struct tog_view *view)
6772 const struct got_error *err = NULL;
6773 struct tog_tree_view_state *s = &view->state.tree;
6774 char *parent_path;
6776 err = tree_entry_path(&parent_path, &s->parents, NULL);
6777 if (err)
6778 return err;
6780 err = draw_tree_entries(view, parent_path);
6781 free(parent_path);
6783 view_border(view);
6784 return err;
6787 static const struct got_error *
6788 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6790 const struct got_error *err = NULL;
6791 struct tog_tree_view_state *s = &view->state.tree;
6792 struct tog_view *log_view, *ref_view;
6793 struct got_tree_entry *te;
6794 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6796 switch (ch) {
6797 case 'i':
6798 s->show_ids = !s->show_ids;
6799 view->count = 0;
6800 break;
6801 case 'l':
6802 view->count = 0;
6803 if (!s->selected_entry)
6804 break;
6805 if (view_is_parent_view(view))
6806 view_get_split(view, &begin_y, &begin_x);
6807 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6808 if (view_is_parent_view(view) &&
6809 view->mode == TOG_VIEW_SPLIT_HRZN) {
6810 err = view_init_hsplit(view, begin_y);
6811 if (err)
6812 break;
6814 view->focussed = 0;
6815 log_view->focussed = 1;
6816 log_view->mode = view->mode;
6817 log_view->nlines = view->lines - begin_y;
6818 if (view_is_parent_view(view)) {
6819 view_transfer_size(log_view, view);
6820 err = view_close_child(view);
6821 if (err)
6822 return err;
6823 err = view_set_child(view, log_view);
6824 if (err)
6825 return err;
6826 view->focus_child = 1;
6827 } else
6828 *new_view = log_view;
6829 break;
6830 case 'r':
6831 view->count = 0;
6832 if (view_is_parent_view(view))
6833 view_get_split(view, &begin_y, &begin_x);
6834 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6835 if (ref_view == NULL)
6836 return got_error_from_errno("view_open");
6837 err = open_ref_view(ref_view, s->repo);
6838 if (err) {
6839 view_close(ref_view);
6840 return err;
6842 if (view_is_parent_view(view) &&
6843 view->mode == TOG_VIEW_SPLIT_HRZN) {
6844 err = view_init_hsplit(view, begin_y);
6845 if (err)
6846 break;
6848 view->focussed = 0;
6849 ref_view->focussed = 1;
6850 ref_view->mode = view->mode;
6851 ref_view->nlines = view->lines - begin_y;
6852 if (view_is_parent_view(view)) {
6853 view_transfer_size(ref_view, view);
6854 err = view_close_child(view);
6855 if (err)
6856 return err;
6857 err = view_set_child(view, ref_view);
6858 if (err)
6859 return err;
6860 view->focus_child = 1;
6861 } else
6862 *new_view = ref_view;
6863 break;
6864 case 'g':
6865 case KEY_HOME:
6866 s->selected = 0;
6867 view->count = 0;
6868 if (s->tree == s->root)
6869 s->first_displayed_entry =
6870 got_object_tree_get_first_entry(s->tree);
6871 else
6872 s->first_displayed_entry = NULL;
6873 break;
6874 case 'G':
6875 case KEY_END: {
6876 int eos = view->nlines - 3;
6878 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6879 --eos; /* border */
6880 s->selected = 0;
6881 view->count = 0;
6882 te = got_object_tree_get_last_entry(s->tree);
6883 for (n = 0; n < eos; n++) {
6884 if (te == NULL) {
6885 if (s->tree != s->root) {
6886 s->first_displayed_entry = NULL;
6887 n++;
6889 break;
6891 s->first_displayed_entry = te;
6892 te = got_tree_entry_get_prev(s->tree, te);
6894 if (n > 0)
6895 s->selected = n - 1;
6896 break;
6898 case 'k':
6899 case KEY_UP:
6900 case CTRL('p'):
6901 if (s->selected > 0) {
6902 s->selected--;
6903 break;
6905 tree_scroll_up(s, 1);
6906 if (s->selected_entry == NULL ||
6907 (s->tree == s->root && s->selected_entry ==
6908 got_object_tree_get_first_entry(s->tree)))
6909 view->count = 0;
6910 break;
6911 case CTRL('u'):
6912 case 'u':
6913 nscroll /= 2;
6914 /* FALL THROUGH */
6915 case KEY_PPAGE:
6916 case CTRL('b'):
6917 case 'b':
6918 if (s->tree == s->root) {
6919 if (got_object_tree_get_first_entry(s->tree) ==
6920 s->first_displayed_entry)
6921 s->selected -= MIN(s->selected, nscroll);
6922 } else {
6923 if (s->first_displayed_entry == NULL)
6924 s->selected -= MIN(s->selected, nscroll);
6926 tree_scroll_up(s, MAX(0, nscroll));
6927 if (s->selected_entry == NULL ||
6928 (s->tree == s->root && s->selected_entry ==
6929 got_object_tree_get_first_entry(s->tree)))
6930 view->count = 0;
6931 break;
6932 case 'j':
6933 case KEY_DOWN:
6934 case CTRL('n'):
6935 if (s->selected < s->ndisplayed - 1) {
6936 s->selected++;
6937 break;
6939 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6940 == NULL) {
6941 /* can't scroll any further */
6942 view->count = 0;
6943 break;
6945 tree_scroll_down(view, 1);
6946 break;
6947 case CTRL('d'):
6948 case 'd':
6949 nscroll /= 2;
6950 /* FALL THROUGH */
6951 case KEY_NPAGE:
6952 case CTRL('f'):
6953 case 'f':
6954 case ' ':
6955 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6956 == NULL) {
6957 /* can't scroll any further; move cursor down */
6958 if (s->selected < s->ndisplayed - 1)
6959 s->selected += MIN(nscroll,
6960 s->ndisplayed - s->selected - 1);
6961 else
6962 view->count = 0;
6963 break;
6965 tree_scroll_down(view, nscroll);
6966 break;
6967 case KEY_ENTER:
6968 case '\r':
6969 case KEY_BACKSPACE:
6970 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6971 struct tog_parent_tree *parent;
6972 /* user selected '..' */
6973 if (s->tree == s->root) {
6974 view->count = 0;
6975 break;
6977 parent = TAILQ_FIRST(&s->parents);
6978 TAILQ_REMOVE(&s->parents, parent,
6979 entry);
6980 got_object_tree_close(s->tree);
6981 s->tree = parent->tree;
6982 s->first_displayed_entry =
6983 parent->first_displayed_entry;
6984 s->selected_entry =
6985 parent->selected_entry;
6986 s->selected = parent->selected;
6987 if (s->selected > view->nlines - 3) {
6988 err = offset_selection_down(view);
6989 if (err)
6990 break;
6992 free(parent);
6993 } else if (S_ISDIR(got_tree_entry_get_mode(
6994 s->selected_entry))) {
6995 struct got_tree_object *subtree;
6996 view->count = 0;
6997 err = got_object_open_as_tree(&subtree, s->repo,
6998 got_tree_entry_get_id(s->selected_entry));
6999 if (err)
7000 break;
7001 err = tree_view_visit_subtree(s, subtree);
7002 if (err) {
7003 got_object_tree_close(subtree);
7004 break;
7006 } else if (S_ISREG(got_tree_entry_get_mode(
7007 s->selected_entry))) {
7008 struct tog_view *blame_view;
7009 int begin_x = 0, begin_y = 0;
7011 if (view_is_parent_view(view))
7012 view_get_split(view, &begin_y, &begin_x);
7014 err = blame_tree_entry(&blame_view, begin_y, begin_x,
7015 s->selected_entry, &s->parents,
7016 s->commit_id, s->repo);
7017 if (err)
7018 break;
7020 if (view_is_parent_view(view) &&
7021 view->mode == TOG_VIEW_SPLIT_HRZN) {
7022 err = view_init_hsplit(view, begin_y);
7023 if (err)
7024 break;
7027 view->count = 0;
7028 view->focussed = 0;
7029 blame_view->focussed = 1;
7030 blame_view->mode = view->mode;
7031 blame_view->nlines = view->lines - begin_y;
7032 if (view_is_parent_view(view)) {
7033 view_transfer_size(blame_view, view);
7034 err = view_close_child(view);
7035 if (err)
7036 return err;
7037 err = view_set_child(view, blame_view);
7038 if (err)
7039 return err;
7040 view->focus_child = 1;
7041 } else
7042 *new_view = blame_view;
7044 break;
7045 case KEY_RESIZE:
7046 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7047 s->selected = view->nlines - 4;
7048 view->count = 0;
7049 break;
7050 default:
7051 view->count = 0;
7052 break;
7055 return err;
7058 __dead static void
7059 usage_tree(void)
7061 endwin();
7062 fprintf(stderr,
7063 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7064 getprogname());
7065 exit(1);
7068 static const struct got_error *
7069 cmd_tree(int argc, char *argv[])
7071 const struct got_error *error;
7072 struct got_repository *repo = NULL;
7073 struct got_worktree *worktree = NULL;
7074 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7075 struct got_object_id *commit_id = NULL;
7076 struct got_commit_object *commit = NULL;
7077 const char *commit_id_arg = NULL;
7078 char *label = NULL;
7079 struct got_reference *ref = NULL;
7080 const char *head_ref_name = NULL;
7081 int ch;
7082 struct tog_view *view;
7083 int *pack_fds = NULL;
7085 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7086 switch (ch) {
7087 case 'c':
7088 commit_id_arg = optarg;
7089 break;
7090 case 'r':
7091 repo_path = realpath(optarg, NULL);
7092 if (repo_path == NULL)
7093 return got_error_from_errno2("realpath",
7094 optarg);
7095 break;
7096 default:
7097 usage_tree();
7098 /* NOTREACHED */
7102 argc -= optind;
7103 argv += optind;
7105 if (argc > 1)
7106 usage_tree();
7108 error = got_repo_pack_fds_open(&pack_fds);
7109 if (error != NULL)
7110 goto done;
7112 if (repo_path == NULL) {
7113 cwd = getcwd(NULL, 0);
7114 if (cwd == NULL)
7115 return got_error_from_errno("getcwd");
7116 error = got_worktree_open(&worktree, cwd);
7117 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7118 goto done;
7119 if (worktree)
7120 repo_path =
7121 strdup(got_worktree_get_repo_path(worktree));
7122 else
7123 repo_path = strdup(cwd);
7124 if (repo_path == NULL) {
7125 error = got_error_from_errno("strdup");
7126 goto done;
7130 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7131 if (error != NULL)
7132 goto done;
7134 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7135 repo, worktree);
7136 if (error)
7137 goto done;
7139 init_curses();
7141 error = apply_unveil(got_repo_get_path(repo), NULL);
7142 if (error)
7143 goto done;
7145 error = tog_load_refs(repo, 0);
7146 if (error)
7147 goto done;
7149 if (commit_id_arg == NULL) {
7150 error = got_repo_match_object_id(&commit_id, &label,
7151 worktree ? got_worktree_get_head_ref_name(worktree) :
7152 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7153 if (error)
7154 goto done;
7155 head_ref_name = label;
7156 } else {
7157 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7158 if (error == NULL)
7159 head_ref_name = got_ref_get_name(ref);
7160 else if (error->code != GOT_ERR_NOT_REF)
7161 goto done;
7162 error = got_repo_match_object_id(&commit_id, NULL,
7163 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7164 if (error)
7165 goto done;
7168 error = got_object_open_as_commit(&commit, repo, commit_id);
7169 if (error)
7170 goto done;
7172 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7173 if (view == NULL) {
7174 error = got_error_from_errno("view_open");
7175 goto done;
7177 error = open_tree_view(view, commit_id, head_ref_name, repo);
7178 if (error)
7179 goto done;
7180 if (!got_path_is_root_dir(in_repo_path)) {
7181 error = tree_view_walk_path(&view->state.tree, commit,
7182 in_repo_path);
7183 if (error)
7184 goto done;
7187 if (worktree) {
7188 /* Release work tree lock. */
7189 got_worktree_close(worktree);
7190 worktree = NULL;
7192 error = view_loop(view);
7193 done:
7194 free(repo_path);
7195 free(cwd);
7196 free(commit_id);
7197 free(label);
7198 if (ref)
7199 got_ref_close(ref);
7200 if (repo) {
7201 const struct got_error *close_err = got_repo_close(repo);
7202 if (error == NULL)
7203 error = close_err;
7205 if (pack_fds) {
7206 const struct got_error *pack_err =
7207 got_repo_pack_fds_close(pack_fds);
7208 if (error == NULL)
7209 error = pack_err;
7211 tog_free_refs();
7212 return error;
7215 static const struct got_error *
7216 ref_view_load_refs(struct tog_ref_view_state *s)
7218 struct got_reflist_entry *sre;
7219 struct tog_reflist_entry *re;
7221 s->nrefs = 0;
7222 TAILQ_FOREACH(sre, &tog_refs, entry) {
7223 if (strncmp(got_ref_get_name(sre->ref),
7224 "refs/got/", 9) == 0 &&
7225 strncmp(got_ref_get_name(sre->ref),
7226 "refs/got/backup/", 16) != 0)
7227 continue;
7229 re = malloc(sizeof(*re));
7230 if (re == NULL)
7231 return got_error_from_errno("malloc");
7233 re->ref = got_ref_dup(sre->ref);
7234 if (re->ref == NULL)
7235 return got_error_from_errno("got_ref_dup");
7236 re->idx = s->nrefs++;
7237 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7240 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7241 return NULL;
7244 static void
7245 ref_view_free_refs(struct tog_ref_view_state *s)
7247 struct tog_reflist_entry *re;
7249 while (!TAILQ_EMPTY(&s->refs)) {
7250 re = TAILQ_FIRST(&s->refs);
7251 TAILQ_REMOVE(&s->refs, re, entry);
7252 got_ref_close(re->ref);
7253 free(re);
7257 static const struct got_error *
7258 open_ref_view(struct tog_view *view, struct got_repository *repo)
7260 const struct got_error *err = NULL;
7261 struct tog_ref_view_state *s = &view->state.ref;
7263 s->selected_entry = 0;
7264 s->repo = repo;
7266 TAILQ_INIT(&s->refs);
7267 STAILQ_INIT(&s->colors);
7269 err = ref_view_load_refs(s);
7270 if (err)
7271 return err;
7273 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7274 err = add_color(&s->colors, "^refs/heads/",
7275 TOG_COLOR_REFS_HEADS,
7276 get_color_value("TOG_COLOR_REFS_HEADS"));
7277 if (err)
7278 goto done;
7280 err = add_color(&s->colors, "^refs/tags/",
7281 TOG_COLOR_REFS_TAGS,
7282 get_color_value("TOG_COLOR_REFS_TAGS"));
7283 if (err)
7284 goto done;
7286 err = add_color(&s->colors, "^refs/remotes/",
7287 TOG_COLOR_REFS_REMOTES,
7288 get_color_value("TOG_COLOR_REFS_REMOTES"));
7289 if (err)
7290 goto done;
7292 err = add_color(&s->colors, "^refs/got/backup/",
7293 TOG_COLOR_REFS_BACKUP,
7294 get_color_value("TOG_COLOR_REFS_BACKUP"));
7295 if (err)
7296 goto done;
7299 view->show = show_ref_view;
7300 view->input = input_ref_view;
7301 view->close = close_ref_view;
7302 view->search_start = search_start_ref_view;
7303 view->search_next = search_next_ref_view;
7304 done:
7305 if (err)
7306 free_colors(&s->colors);
7307 return err;
7310 static const struct got_error *
7311 close_ref_view(struct tog_view *view)
7313 struct tog_ref_view_state *s = &view->state.ref;
7315 ref_view_free_refs(s);
7316 free_colors(&s->colors);
7318 return NULL;
7321 static const struct got_error *
7322 resolve_reflist_entry(struct got_object_id **commit_id,
7323 struct tog_reflist_entry *re, struct got_repository *repo)
7325 const struct got_error *err = NULL;
7326 struct got_object_id *obj_id;
7327 struct got_tag_object *tag = NULL;
7328 int obj_type;
7330 *commit_id = NULL;
7332 err = got_ref_resolve(&obj_id, repo, re->ref);
7333 if (err)
7334 return err;
7336 err = got_object_get_type(&obj_type, repo, obj_id);
7337 if (err)
7338 goto done;
7340 switch (obj_type) {
7341 case GOT_OBJ_TYPE_COMMIT:
7342 *commit_id = obj_id;
7343 break;
7344 case GOT_OBJ_TYPE_TAG:
7345 err = got_object_open_as_tag(&tag, repo, obj_id);
7346 if (err)
7347 goto done;
7348 free(obj_id);
7349 err = got_object_get_type(&obj_type, repo,
7350 got_object_tag_get_object_id(tag));
7351 if (err)
7352 goto done;
7353 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7354 err = got_error(GOT_ERR_OBJ_TYPE);
7355 goto done;
7357 *commit_id = got_object_id_dup(
7358 got_object_tag_get_object_id(tag));
7359 if (*commit_id == NULL) {
7360 err = got_error_from_errno("got_object_id_dup");
7361 goto done;
7363 break;
7364 default:
7365 err = got_error(GOT_ERR_OBJ_TYPE);
7366 break;
7369 done:
7370 if (tag)
7371 got_object_tag_close(tag);
7372 if (err) {
7373 free(*commit_id);
7374 *commit_id = NULL;
7376 return err;
7379 static const struct got_error *
7380 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7381 struct tog_reflist_entry *re, struct got_repository *repo)
7383 struct tog_view *log_view;
7384 const struct got_error *err = NULL;
7385 struct got_object_id *commit_id = NULL;
7387 *new_view = NULL;
7389 err = resolve_reflist_entry(&commit_id, re, repo);
7390 if (err) {
7391 if (err->code != GOT_ERR_OBJ_TYPE)
7392 return err;
7393 else
7394 return NULL;
7397 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7398 if (log_view == NULL) {
7399 err = got_error_from_errno("view_open");
7400 goto done;
7403 err = open_log_view(log_view, commit_id, repo,
7404 got_ref_get_name(re->ref), "", 0);
7405 done:
7406 if (err)
7407 view_close(log_view);
7408 else
7409 *new_view = log_view;
7410 free(commit_id);
7411 return err;
7414 static void
7415 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7417 struct tog_reflist_entry *re;
7418 int i = 0;
7420 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7421 return;
7423 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7424 while (i++ < maxscroll) {
7425 if (re == NULL)
7426 break;
7427 s->first_displayed_entry = re;
7428 re = TAILQ_PREV(re, tog_reflist_head, entry);
7432 static const struct got_error *
7433 ref_scroll_down(struct tog_view *view, int maxscroll)
7435 struct tog_ref_view_state *s = &view->state.ref;
7436 struct tog_reflist_entry *next, *last;
7437 int n = 0;
7439 if (s->first_displayed_entry)
7440 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7441 else
7442 next = TAILQ_FIRST(&s->refs);
7444 last = s->last_displayed_entry;
7445 while (next && n++ < maxscroll) {
7446 if (last)
7447 last = TAILQ_NEXT(last, entry);
7448 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7449 s->first_displayed_entry = next;
7450 next = TAILQ_NEXT(next, entry);
7454 return NULL;
7457 static const struct got_error *
7458 search_start_ref_view(struct tog_view *view)
7460 struct tog_ref_view_state *s = &view->state.ref;
7462 s->matched_entry = NULL;
7463 return NULL;
7466 static int
7467 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7469 regmatch_t regmatch;
7471 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7472 0) == 0;
7475 static const struct got_error *
7476 search_next_ref_view(struct tog_view *view)
7478 struct tog_ref_view_state *s = &view->state.ref;
7479 struct tog_reflist_entry *re = NULL;
7481 if (!view->searching) {
7482 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7483 return NULL;
7486 if (s->matched_entry) {
7487 if (view->searching == TOG_SEARCH_FORWARD) {
7488 if (s->selected_entry)
7489 re = TAILQ_NEXT(s->selected_entry, entry);
7490 else
7491 re = TAILQ_PREV(s->selected_entry,
7492 tog_reflist_head, entry);
7493 } else {
7494 if (s->selected_entry == NULL)
7495 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7496 else
7497 re = TAILQ_PREV(s->selected_entry,
7498 tog_reflist_head, entry);
7500 } else {
7501 if (s->selected_entry)
7502 re = s->selected_entry;
7503 else if (view->searching == TOG_SEARCH_FORWARD)
7504 re = TAILQ_FIRST(&s->refs);
7505 else
7506 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7509 while (1) {
7510 if (re == NULL) {
7511 if (s->matched_entry == NULL) {
7512 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7513 return NULL;
7515 if (view->searching == TOG_SEARCH_FORWARD)
7516 re = TAILQ_FIRST(&s->refs);
7517 else
7518 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7521 if (match_reflist_entry(re, &view->regex)) {
7522 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7523 s->matched_entry = re;
7524 break;
7527 if (view->searching == TOG_SEARCH_FORWARD)
7528 re = TAILQ_NEXT(re, entry);
7529 else
7530 re = TAILQ_PREV(re, tog_reflist_head, entry);
7533 if (s->matched_entry) {
7534 s->first_displayed_entry = s->matched_entry;
7535 s->selected = 0;
7538 return NULL;
7541 static const struct got_error *
7542 show_ref_view(struct tog_view *view)
7544 const struct got_error *err = NULL;
7545 struct tog_ref_view_state *s = &view->state.ref;
7546 struct tog_reflist_entry *re;
7547 char *line = NULL;
7548 wchar_t *wline;
7549 struct tog_color *tc;
7550 int width, n;
7551 int limit = view->nlines;
7553 werase(view->window);
7555 s->ndisplayed = 0;
7556 if (view_is_hsplit_top(view))
7557 --limit; /* border */
7559 if (limit == 0)
7560 return NULL;
7562 re = s->first_displayed_entry;
7564 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7565 s->nrefs) == -1)
7566 return got_error_from_errno("asprintf");
7568 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7569 if (err) {
7570 free(line);
7571 return err;
7573 if (view_needs_focus_indication(view))
7574 wstandout(view->window);
7575 waddwstr(view->window, wline);
7576 if (view_needs_focus_indication(view))
7577 wstandend(view->window);
7578 free(wline);
7579 wline = NULL;
7580 free(line);
7581 line = NULL;
7582 if (width < view->ncols - 1)
7583 waddch(view->window, '\n');
7584 if (--limit <= 0)
7585 return NULL;
7587 n = 0;
7588 while (re && limit > 0) {
7589 char *line = NULL;
7590 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7592 if (s->show_date) {
7593 struct got_commit_object *ci;
7594 struct got_tag_object *tag;
7595 struct got_object_id *id;
7596 struct tm tm;
7597 time_t t;
7599 err = got_ref_resolve(&id, s->repo, re->ref);
7600 if (err)
7601 return err;
7602 err = got_object_open_as_tag(&tag, s->repo, id);
7603 if (err) {
7604 if (err->code != GOT_ERR_OBJ_TYPE) {
7605 free(id);
7606 return err;
7608 err = got_object_open_as_commit(&ci, s->repo,
7609 id);
7610 if (err) {
7611 free(id);
7612 return err;
7614 t = got_object_commit_get_committer_time(ci);
7615 got_object_commit_close(ci);
7616 } else {
7617 t = got_object_tag_get_tagger_time(tag);
7618 got_object_tag_close(tag);
7620 free(id);
7621 if (gmtime_r(&t, &tm) == NULL)
7622 return got_error_from_errno("gmtime_r");
7623 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7624 return got_error(GOT_ERR_NO_SPACE);
7626 if (got_ref_is_symbolic(re->ref)) {
7627 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7628 ymd : "", got_ref_get_name(re->ref),
7629 got_ref_get_symref_target(re->ref)) == -1)
7630 return got_error_from_errno("asprintf");
7631 } else if (s->show_ids) {
7632 struct got_object_id *id;
7633 char *id_str;
7634 err = got_ref_resolve(&id, s->repo, re->ref);
7635 if (err)
7636 return err;
7637 err = got_object_id_str(&id_str, id);
7638 if (err) {
7639 free(id);
7640 return err;
7642 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7643 got_ref_get_name(re->ref), id_str) == -1) {
7644 err = got_error_from_errno("asprintf");
7645 free(id);
7646 free(id_str);
7647 return err;
7649 free(id);
7650 free(id_str);
7651 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7652 got_ref_get_name(re->ref)) == -1)
7653 return got_error_from_errno("asprintf");
7655 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7656 0, 0);
7657 if (err) {
7658 free(line);
7659 return err;
7661 if (n == s->selected) {
7662 if (view->focussed)
7663 wstandout(view->window);
7664 s->selected_entry = re;
7666 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7667 if (tc)
7668 wattr_on(view->window,
7669 COLOR_PAIR(tc->colorpair), NULL);
7670 waddwstr(view->window, wline);
7671 if (tc)
7672 wattr_off(view->window,
7673 COLOR_PAIR(tc->colorpair), NULL);
7674 if (width < view->ncols - 1)
7675 waddch(view->window, '\n');
7676 if (n == s->selected && view->focussed)
7677 wstandend(view->window);
7678 free(line);
7679 free(wline);
7680 wline = NULL;
7681 n++;
7682 s->ndisplayed++;
7683 s->last_displayed_entry = re;
7685 limit--;
7686 re = TAILQ_NEXT(re, entry);
7689 view_border(view);
7690 return err;
7693 static const struct got_error *
7694 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7695 struct tog_reflist_entry *re, struct got_repository *repo)
7697 const struct got_error *err = NULL;
7698 struct got_object_id *commit_id = NULL;
7699 struct tog_view *tree_view;
7701 *new_view = NULL;
7703 err = resolve_reflist_entry(&commit_id, re, repo);
7704 if (err) {
7705 if (err->code != GOT_ERR_OBJ_TYPE)
7706 return err;
7707 else
7708 return NULL;
7712 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7713 if (tree_view == NULL) {
7714 err = got_error_from_errno("view_open");
7715 goto done;
7718 err = open_tree_view(tree_view, commit_id,
7719 got_ref_get_name(re->ref), repo);
7720 if (err)
7721 goto done;
7723 *new_view = tree_view;
7724 done:
7725 free(commit_id);
7726 return err;
7728 static const struct got_error *
7729 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7731 const struct got_error *err = NULL;
7732 struct tog_ref_view_state *s = &view->state.ref;
7733 struct tog_view *log_view, *tree_view;
7734 struct tog_reflist_entry *re;
7735 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7737 switch (ch) {
7738 case 'i':
7739 s->show_ids = !s->show_ids;
7740 view->count = 0;
7741 break;
7742 case 'm':
7743 s->show_date = !s->show_date;
7744 view->count = 0;
7745 break;
7746 case 'o':
7747 s->sort_by_date = !s->sort_by_date;
7748 view->count = 0;
7749 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7750 got_ref_cmp_by_commit_timestamp_descending :
7751 tog_ref_cmp_by_name, s->repo);
7752 if (err)
7753 break;
7754 got_reflist_object_id_map_free(tog_refs_idmap);
7755 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7756 &tog_refs, s->repo);
7757 if (err)
7758 break;
7759 ref_view_free_refs(s);
7760 err = ref_view_load_refs(s);
7761 break;
7762 case KEY_ENTER:
7763 case '\r':
7764 view->count = 0;
7765 if (!s->selected_entry)
7766 break;
7767 if (view_is_parent_view(view))
7768 view_get_split(view, &begin_y, &begin_x);
7770 err = log_ref_entry(&log_view, begin_y, begin_x,
7771 s->selected_entry, s->repo);
7772 if (err)
7773 break;
7775 if (view_is_parent_view(view) &&
7776 view->mode == TOG_VIEW_SPLIT_HRZN) {
7777 err = view_init_hsplit(view, begin_y);
7778 if (err)
7779 break;
7782 view->focussed = 0;
7783 log_view->focussed = 1;
7784 log_view->mode = view->mode;
7785 log_view->nlines = view->lines - begin_y;
7786 if (view_is_parent_view(view)) {
7787 view_transfer_size(log_view, view);
7788 err = view_close_child(view);
7789 if (err)
7790 return err;
7791 err = view_set_child(view, log_view);
7792 if (err)
7793 return err;
7794 view->focus_child = 1;
7795 } else
7796 *new_view = log_view;
7797 break;
7798 case 't':
7799 view->count = 0;
7800 if (!s->selected_entry)
7801 break;
7802 if (view_is_parent_view(view))
7803 view_get_split(view, &begin_y, &begin_x);
7804 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7805 s->selected_entry, s->repo);
7806 if (err || tree_view == NULL)
7807 break;
7808 if (view_is_parent_view(view) &&
7809 view->mode == TOG_VIEW_SPLIT_HRZN) {
7810 err = view_init_hsplit(view, begin_y);
7811 if (err)
7812 break;
7814 view->focussed = 0;
7815 tree_view->focussed = 1;
7816 tree_view->mode = view->mode;
7817 tree_view->nlines = view->lines - begin_y;
7818 if (view_is_parent_view(view)) {
7819 view_transfer_size(tree_view, view);
7820 err = view_close_child(view);
7821 if (err)
7822 return err;
7823 err = view_set_child(view, tree_view);
7824 if (err)
7825 return err;
7826 view->focus_child = 1;
7827 } else
7828 *new_view = tree_view;
7829 break;
7830 case 'g':
7831 case KEY_HOME:
7832 s->selected = 0;
7833 view->count = 0;
7834 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7835 break;
7836 case 'G':
7837 case KEY_END: {
7838 int eos = view->nlines - 1;
7840 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7841 --eos; /* border */
7842 s->selected = 0;
7843 view->count = 0;
7844 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7845 for (n = 0; n < eos; n++) {
7846 if (re == NULL)
7847 break;
7848 s->first_displayed_entry = re;
7849 re = TAILQ_PREV(re, tog_reflist_head, entry);
7851 if (n > 0)
7852 s->selected = n - 1;
7853 break;
7855 case 'k':
7856 case KEY_UP:
7857 case CTRL('p'):
7858 if (s->selected > 0) {
7859 s->selected--;
7860 break;
7862 ref_scroll_up(s, 1);
7863 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7864 view->count = 0;
7865 break;
7866 case CTRL('u'):
7867 case 'u':
7868 nscroll /= 2;
7869 /* FALL THROUGH */
7870 case KEY_PPAGE:
7871 case CTRL('b'):
7872 case 'b':
7873 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7874 s->selected -= MIN(nscroll, s->selected);
7875 ref_scroll_up(s, MAX(0, nscroll));
7876 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7877 view->count = 0;
7878 break;
7879 case 'j':
7880 case KEY_DOWN:
7881 case CTRL('n'):
7882 if (s->selected < s->ndisplayed - 1) {
7883 s->selected++;
7884 break;
7886 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7887 /* can't scroll any further */
7888 view->count = 0;
7889 break;
7891 ref_scroll_down(view, 1);
7892 break;
7893 case CTRL('d'):
7894 case 'd':
7895 nscroll /= 2;
7896 /* FALL THROUGH */
7897 case KEY_NPAGE:
7898 case CTRL('f'):
7899 case 'f':
7900 case ' ':
7901 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7902 /* can't scroll any further; move cursor down */
7903 if (s->selected < s->ndisplayed - 1)
7904 s->selected += MIN(nscroll,
7905 s->ndisplayed - s->selected - 1);
7906 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7907 s->selected += s->ndisplayed - s->selected - 1;
7908 view->count = 0;
7909 break;
7911 ref_scroll_down(view, nscroll);
7912 break;
7913 case CTRL('l'):
7914 view->count = 0;
7915 tog_free_refs();
7916 err = tog_load_refs(s->repo, s->sort_by_date);
7917 if (err)
7918 break;
7919 ref_view_free_refs(s);
7920 err = ref_view_load_refs(s);
7921 break;
7922 case KEY_RESIZE:
7923 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7924 s->selected = view->nlines - 2;
7925 break;
7926 default:
7927 view->count = 0;
7928 break;
7931 return err;
7934 __dead static void
7935 usage_ref(void)
7937 endwin();
7938 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7939 getprogname());
7940 exit(1);
7943 static const struct got_error *
7944 cmd_ref(int argc, char *argv[])
7946 const struct got_error *error;
7947 struct got_repository *repo = NULL;
7948 struct got_worktree *worktree = NULL;
7949 char *cwd = NULL, *repo_path = NULL;
7950 int ch;
7951 struct tog_view *view;
7952 int *pack_fds = NULL;
7954 while ((ch = getopt(argc, argv, "r:")) != -1) {
7955 switch (ch) {
7956 case 'r':
7957 repo_path = realpath(optarg, NULL);
7958 if (repo_path == NULL)
7959 return got_error_from_errno2("realpath",
7960 optarg);
7961 break;
7962 default:
7963 usage_ref();
7964 /* NOTREACHED */
7968 argc -= optind;
7969 argv += optind;
7971 if (argc > 1)
7972 usage_ref();
7974 error = got_repo_pack_fds_open(&pack_fds);
7975 if (error != NULL)
7976 goto done;
7978 if (repo_path == NULL) {
7979 cwd = getcwd(NULL, 0);
7980 if (cwd == NULL)
7981 return got_error_from_errno("getcwd");
7982 error = got_worktree_open(&worktree, cwd);
7983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7984 goto done;
7985 if (worktree)
7986 repo_path =
7987 strdup(got_worktree_get_repo_path(worktree));
7988 else
7989 repo_path = strdup(cwd);
7990 if (repo_path == NULL) {
7991 error = got_error_from_errno("strdup");
7992 goto done;
7996 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7997 if (error != NULL)
7998 goto done;
8000 init_curses();
8002 error = apply_unveil(got_repo_get_path(repo), NULL);
8003 if (error)
8004 goto done;
8006 error = tog_load_refs(repo, 0);
8007 if (error)
8008 goto done;
8010 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8011 if (view == NULL) {
8012 error = got_error_from_errno("view_open");
8013 goto done;
8016 error = open_ref_view(view, repo);
8017 if (error)
8018 goto done;
8020 if (worktree) {
8021 /* Release work tree lock. */
8022 got_worktree_close(worktree);
8023 worktree = NULL;
8025 error = view_loop(view);
8026 done:
8027 free(repo_path);
8028 free(cwd);
8029 if (repo) {
8030 const struct got_error *close_err = got_repo_close(repo);
8031 if (close_err)
8032 error = close_err;
8034 if (pack_fds) {
8035 const struct got_error *pack_err =
8036 got_repo_pack_fds_close(pack_fds);
8037 if (error == NULL)
8038 error = pack_err;
8040 tog_free_refs();
8041 return error;
8045 * If view was scrolled down to move the selected line into view when opening a
8046 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8048 static void
8049 offset_selection_up(struct tog_view *view)
8051 switch (view->type) {
8052 case TOG_VIEW_BLAME: {
8053 struct tog_blame_view_state *s = &view->state.blame;
8054 if (s->first_displayed_line == 1) {
8055 s->selected_line = MAX(s->selected_line - view->offset,
8056 1);
8057 break;
8059 if (s->first_displayed_line > view->offset)
8060 s->first_displayed_line -= view->offset;
8061 else
8062 s->first_displayed_line = 1;
8063 s->selected_line += view->offset;
8064 break;
8066 case TOG_VIEW_LOG:
8067 log_scroll_up(&view->state.log, view->offset);
8068 view->state.log.selected += view->offset;
8069 break;
8070 case TOG_VIEW_REF:
8071 ref_scroll_up(&view->state.ref, view->offset);
8072 view->state.ref.selected += view->offset;
8073 break;
8074 case TOG_VIEW_TREE:
8075 tree_scroll_up(&view->state.tree, view->offset);
8076 view->state.tree.selected += view->offset;
8077 break;
8078 default:
8079 break;
8082 view->offset = 0;
8086 * If the selected line is in the section of screen covered by the bottom split,
8087 * scroll down offset lines to move it into view and index its new position.
8089 static const struct got_error *
8090 offset_selection_down(struct tog_view *view)
8092 const struct got_error *err = NULL;
8093 const struct got_error *(*scrolld)(struct tog_view *, int);
8094 int *selected = NULL;
8095 int header, offset;
8097 switch (view->type) {
8098 case TOG_VIEW_BLAME: {
8099 struct tog_blame_view_state *s = &view->state.blame;
8100 header = 3;
8101 scrolld = NULL;
8102 if (s->selected_line > view->nlines - header) {
8103 offset = abs(view->nlines - s->selected_line - header);
8104 s->first_displayed_line += offset;
8105 s->selected_line -= offset;
8106 view->offset = offset;
8108 break;
8110 case TOG_VIEW_LOG: {
8111 struct tog_log_view_state *s = &view->state.log;
8112 scrolld = &log_scroll_down;
8113 header = view_is_parent_view(view) ? 3 : 2;
8114 selected = &s->selected;
8115 break;
8117 case TOG_VIEW_REF: {
8118 struct tog_ref_view_state *s = &view->state.ref;
8119 scrolld = &ref_scroll_down;
8120 header = 3;
8121 selected = &s->selected;
8122 break;
8124 case TOG_VIEW_TREE: {
8125 struct tog_tree_view_state *s = &view->state.tree;
8126 scrolld = &tree_scroll_down;
8127 header = 5;
8128 selected = &s->selected;
8129 break;
8131 default:
8132 selected = NULL;
8133 scrolld = NULL;
8134 header = 0;
8135 break;
8138 if (selected && *selected > view->nlines - header) {
8139 offset = abs(view->nlines - *selected - header);
8140 view->offset = offset;
8141 if (scrolld && offset) {
8142 err = scrolld(view, offset);
8143 *selected -= offset;
8147 return err;
8150 static void
8151 list_commands(FILE *fp)
8153 size_t i;
8155 fprintf(fp, "commands:");
8156 for (i = 0; i < nitems(tog_commands); i++) {
8157 const struct tog_cmd *cmd = &tog_commands[i];
8158 fprintf(fp, " %s", cmd->name);
8160 fputc('\n', fp);
8163 __dead static void
8164 usage(int hflag, int status)
8166 FILE *fp = (status == 0) ? stdout : stderr;
8168 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8169 getprogname());
8170 if (hflag) {
8171 fprintf(fp, "lazy usage: %s path\n", getprogname());
8172 list_commands(fp);
8174 exit(status);
8177 static char **
8178 make_argv(int argc, ...)
8180 va_list ap;
8181 char **argv;
8182 int i;
8184 va_start(ap, argc);
8186 argv = calloc(argc, sizeof(char *));
8187 if (argv == NULL)
8188 err(1, "calloc");
8189 for (i = 0; i < argc; i++) {
8190 argv[i] = strdup(va_arg(ap, char *));
8191 if (argv[i] == NULL)
8192 err(1, "strdup");
8195 va_end(ap);
8196 return argv;
8200 * Try to convert 'tog path' into a 'tog log path' command.
8201 * The user could simply have mistyped the command rather than knowingly
8202 * provided a path. So check whether argv[0] can in fact be resolved
8203 * to a path in the HEAD commit and print a special error if not.
8204 * This hack is for mpi@ <3
8206 static const struct got_error *
8207 tog_log_with_path(int argc, char *argv[])
8209 const struct got_error *error = NULL, *close_err;
8210 const struct tog_cmd *cmd = NULL;
8211 struct got_repository *repo = NULL;
8212 struct got_worktree *worktree = NULL;
8213 struct got_object_id *commit_id = NULL, *id = NULL;
8214 struct got_commit_object *commit = NULL;
8215 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8216 char *commit_id_str = NULL, **cmd_argv = NULL;
8217 int *pack_fds = NULL;
8219 cwd = getcwd(NULL, 0);
8220 if (cwd == NULL)
8221 return got_error_from_errno("getcwd");
8223 error = got_repo_pack_fds_open(&pack_fds);
8224 if (error != NULL)
8225 goto done;
8227 error = got_worktree_open(&worktree, cwd);
8228 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8229 goto done;
8231 if (worktree)
8232 repo_path = strdup(got_worktree_get_repo_path(worktree));
8233 else
8234 repo_path = strdup(cwd);
8235 if (repo_path == NULL) {
8236 error = got_error_from_errno("strdup");
8237 goto done;
8240 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8241 if (error != NULL)
8242 goto done;
8244 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8245 repo, worktree);
8246 if (error)
8247 goto done;
8249 error = tog_load_refs(repo, 0);
8250 if (error)
8251 goto done;
8252 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8253 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8254 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8255 if (error)
8256 goto done;
8258 if (worktree) {
8259 got_worktree_close(worktree);
8260 worktree = NULL;
8263 error = got_object_open_as_commit(&commit, repo, commit_id);
8264 if (error)
8265 goto done;
8267 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8268 if (error) {
8269 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8270 goto done;
8271 fprintf(stderr, "%s: '%s' is no known command or path\n",
8272 getprogname(), argv[0]);
8273 usage(1, 1);
8274 /* not reached */
8277 close_err = got_repo_close(repo);
8278 if (error == NULL)
8279 error = close_err;
8280 repo = NULL;
8282 error = got_object_id_str(&commit_id_str, commit_id);
8283 if (error)
8284 goto done;
8286 cmd = &tog_commands[0]; /* log */
8287 argc = 4;
8288 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8289 error = cmd->cmd_main(argc, cmd_argv);
8290 done:
8291 if (repo) {
8292 close_err = got_repo_close(repo);
8293 if (error == NULL)
8294 error = close_err;
8296 if (commit)
8297 got_object_commit_close(commit);
8298 if (worktree)
8299 got_worktree_close(worktree);
8300 if (pack_fds) {
8301 const struct got_error *pack_err =
8302 got_repo_pack_fds_close(pack_fds);
8303 if (error == NULL)
8304 error = pack_err;
8306 free(id);
8307 free(commit_id_str);
8308 free(commit_id);
8309 free(cwd);
8310 free(repo_path);
8311 free(in_repo_path);
8312 if (cmd_argv) {
8313 int i;
8314 for (i = 0; i < argc; i++)
8315 free(cmd_argv[i]);
8316 free(cmd_argv);
8318 tog_free_refs();
8319 return error;
8322 int
8323 main(int argc, char *argv[])
8325 const struct got_error *error = NULL;
8326 const struct tog_cmd *cmd = NULL;
8327 int ch, hflag = 0, Vflag = 0;
8328 char **cmd_argv = NULL;
8329 static const struct option longopts[] = {
8330 { "version", no_argument, NULL, 'V' },
8331 { NULL, 0, NULL, 0}
8333 char *diff_algo_str = NULL;
8335 setlocale(LC_CTYPE, "");
8337 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8338 switch (ch) {
8339 case 'h':
8340 hflag = 1;
8341 break;
8342 case 'V':
8343 Vflag = 1;
8344 break;
8345 default:
8346 usage(hflag, 1);
8347 /* NOTREACHED */
8351 argc -= optind;
8352 argv += optind;
8353 optind = 1;
8354 optreset = 1;
8356 if (Vflag) {
8357 got_version_print_str();
8358 return 0;
8361 #ifndef PROFILE
8362 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8363 NULL) == -1)
8364 err(1, "pledge");
8365 #endif
8367 if (argc == 0) {
8368 if (hflag)
8369 usage(hflag, 0);
8370 /* Build an argument vector which runs a default command. */
8371 cmd = &tog_commands[0];
8372 argc = 1;
8373 cmd_argv = make_argv(argc, cmd->name);
8374 } else {
8375 size_t i;
8377 /* Did the user specify a command? */
8378 for (i = 0; i < nitems(tog_commands); i++) {
8379 if (strncmp(tog_commands[i].name, argv[0],
8380 strlen(argv[0])) == 0) {
8381 cmd = &tog_commands[i];
8382 break;
8387 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8388 if (diff_algo_str) {
8389 if (strcasecmp(diff_algo_str, "patience") == 0)
8390 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8391 if (strcasecmp(diff_algo_str, "myers") == 0)
8392 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8395 if (cmd == NULL) {
8396 if (argc != 1)
8397 usage(0, 1);
8398 /* No command specified; try log with a path */
8399 error = tog_log_with_path(argc, argv);
8400 } else {
8401 if (hflag)
8402 cmd->cmd_usage();
8403 else
8404 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8407 endwin();
8408 putchar('\n');
8409 if (cmd_argv) {
8410 int i;
8411 for (i = 0; i < argc; i++)
8412 free(cmd_argv[i]);
8413 free(cmd_argv);
8416 if (error && error->code != GOT_ERR_CANCELLED)
8417 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8418 return 0;