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/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #ifndef MAX
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #endif
69 #ifndef CTRL
70 #define CTRL(x) ((x) & 0x1f)
71 #endif
73 #ifndef nitems
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 #endif
77 struct tog_cmd {
78 const char *name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 };
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static const struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
102 };
104 enum tog_view_type {
105 TOG_VIEW_DIFF,
106 TOG_VIEW_LOG,
107 TOG_VIEW_BLAME,
108 TOG_VIEW_TREE,
109 TOG_VIEW_REF,
110 };
112 enum tog_view_mode {
113 TOG_VIEW_SPLIT_NONE,
114 TOG_VIEW_SPLIT_VERT,
115 TOG_VIEW_SPLIT_HRZN
116 };
118 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 #define TOG_EOF_STRING "(END)"
122 struct commit_queue_entry {
123 TAILQ_ENTRY(commit_queue_entry) entry;
124 struct got_object_id *id;
125 struct got_commit_object *commit;
126 int idx;
127 };
128 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 struct commit_queue {
130 int ncommits;
131 struct commit_queue_head head;
132 };
134 struct tog_color {
135 STAILQ_ENTRY(tog_color) entry;
136 regex_t regex;
137 short colorpair;
138 };
139 STAILQ_HEAD(tog_colors, tog_color);
141 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 static struct got_reflist_object_id_map *tog_refs_idmap;
143 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 static const struct got_error *
146 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 struct got_reference* re2)
149 const char *name1 = got_ref_get_name(re1);
150 const char *name2 = got_ref_get_name(re2);
151 int isbackup1, isbackup2;
153 /* Sort backup refs towards the bottom of the list. */
154 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 if (!isbackup1 && isbackup2) {
157 *cmp = -1;
158 return NULL;
159 } else if (isbackup1 && !isbackup2) {
160 *cmp = 1;
161 return NULL;
164 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
165 return NULL;
168 static const struct got_error *
169 tog_load_refs(struct got_repository *repo, int sort_by_date)
171 const struct got_error *err;
173 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 repo);
176 if (err)
177 return err;
179 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
180 repo);
183 static void
184 tog_free_refs(void)
186 if (tog_refs_idmap) {
187 got_reflist_object_id_map_free(tog_refs_idmap);
188 tog_refs_idmap = NULL;
190 got_ref_list_free(&tog_refs);
193 static const struct got_error *
194 add_color(struct tog_colors *colors, const char *pattern,
195 int idx, short color)
197 const struct got_error *err = NULL;
198 struct tog_color *tc;
199 int regerr = 0;
201 if (idx < 1 || idx > COLOR_PAIRS - 1)
202 return NULL;
204 init_pair(idx, color, -1);
206 tc = calloc(1, sizeof(*tc));
207 if (tc == NULL)
208 return got_error_from_errno("calloc");
209 regerr = regcomp(&tc->regex, pattern,
210 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
211 if (regerr) {
212 static char regerr_msg[512];
213 static char err_msg[512];
214 regerror(regerr, &tc->regex, regerr_msg,
215 sizeof(regerr_msg));
216 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
217 regerr_msg);
218 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 free(tc);
220 return err;
222 tc->colorpair = idx;
223 STAILQ_INSERT_HEAD(colors, tc, entry);
224 return NULL;
227 static void
228 free_colors(struct tog_colors *colors)
230 struct tog_color *tc;
232 while (!STAILQ_EMPTY(colors)) {
233 tc = STAILQ_FIRST(colors);
234 STAILQ_REMOVE_HEAD(colors, entry);
235 regfree(&tc->regex);
236 free(tc);
240 static struct tog_color *
241 get_color(struct tog_colors *colors, int colorpair)
243 struct tog_color *tc = NULL;
245 STAILQ_FOREACH(tc, colors, entry) {
246 if (tc->colorpair == colorpair)
247 return tc;
250 return NULL;
253 static int
254 default_color_value(const char *envvar)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 return COLOR_MAGENTA;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
259 return COLOR_CYAN;
260 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
261 return COLOR_YELLOW;
262 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
263 return COLOR_GREEN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 return COLOR_MAGENTA;
266 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
271 return COLOR_GREEN;
272 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
275 return COLOR_CYAN;
276 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
279 return COLOR_GREEN;
280 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
283 return COLOR_YELLOW;
284 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 return COLOR_CYAN;
287 return -1;
290 static int
291 get_color_value(const char *envvar)
293 const char *val = getenv(envvar);
295 if (val == NULL)
296 return default_color_value(envvar);
298 if (strcasecmp(val, "black") == 0)
299 return COLOR_BLACK;
300 if (strcasecmp(val, "red") == 0)
301 return COLOR_RED;
302 if (strcasecmp(val, "green") == 0)
303 return COLOR_GREEN;
304 if (strcasecmp(val, "yellow") == 0)
305 return COLOR_YELLOW;
306 if (strcasecmp(val, "blue") == 0)
307 return COLOR_BLUE;
308 if (strcasecmp(val, "magenta") == 0)
309 return COLOR_MAGENTA;
310 if (strcasecmp(val, "cyan") == 0)
311 return COLOR_CYAN;
312 if (strcasecmp(val, "white") == 0)
313 return COLOR_WHITE;
314 if (strcasecmp(val, "default") == 0)
315 return -1;
317 return default_color_value(envvar);
321 struct tog_diff_view_state {
322 struct got_object_id *id1, *id2;
323 const char *label1, *label2;
324 FILE *f, *f1, *f2;
325 int fd1, fd2;
326 int first_displayed_line;
327 int last_displayed_line;
328 int eof;
329 int diff_context;
330 int ignore_whitespace;
331 int force_text_diff;
332 struct got_repository *repo;
333 struct tog_colors colors;
334 size_t nlines;
335 off_t *line_offsets;
336 int matched_line;
337 int selected_line;
339 /* passed from log or blame view; may be NULL */
340 struct tog_view *parent_view;
341 };
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 static volatile sig_atomic_t tog_thread_error;
346 struct tog_log_thread_args {
347 pthread_cond_t need_commits;
348 pthread_cond_t commit_loaded;
349 int commits_needed;
350 int load_all;
351 struct got_commit_graph *graph;
352 struct commit_queue *commits;
353 const char *in_repo_path;
354 struct got_object_id *start_id;
355 struct got_repository *repo;
356 int *pack_fds;
357 int log_complete;
358 sig_atomic_t *quit;
359 struct commit_queue_entry **first_displayed_entry;
360 struct commit_queue_entry **selected_entry;
361 int *searching;
362 int *search_next_done;
363 regex_t *regex;
364 };
366 struct tog_log_view_state {
367 struct commit_queue commits;
368 struct commit_queue_entry *first_displayed_entry;
369 struct commit_queue_entry *last_displayed_entry;
370 struct commit_queue_entry *selected_entry;
371 int selected;
372 char *in_repo_path;
373 char *head_ref_name;
374 int log_branches;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
377 sig_atomic_t quit;
378 pthread_t thread;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
383 };
385 #define TOG_COLOR_DIFF_MINUS 1
386 #define TOG_COLOR_DIFF_PLUS 2
387 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
388 #define TOG_COLOR_DIFF_META 4
389 #define TOG_COLOR_TREE_SUBMODULE 5
390 #define TOG_COLOR_TREE_SYMLINK 6
391 #define TOG_COLOR_TREE_DIRECTORY 7
392 #define TOG_COLOR_TREE_EXECUTABLE 8
393 #define TOG_COLOR_COMMIT 9
394 #define TOG_COLOR_AUTHOR 10
395 #define TOG_COLOR_DATE 11
396 #define TOG_COLOR_REFS_HEADS 12
397 #define TOG_COLOR_REFS_TAGS 13
398 #define TOG_COLOR_REFS_REMOTES 14
399 #define TOG_COLOR_REFS_BACKUP 15
401 struct tog_blame_cb_args {
402 struct tog_blame_line *lines; /* one per line */
403 int nlines;
405 struct tog_view *view;
406 struct got_object_id *commit_id;
407 int *quit;
408 };
410 struct tog_blame_thread_args {
411 const char *path;
412 struct got_repository *repo;
413 struct tog_blame_cb_args *cb_args;
414 int *complete;
415 got_cancel_cb cancel_cb;
416 void *cancel_arg;
417 };
419 struct tog_blame {
420 FILE *f;
421 off_t filesize;
422 struct tog_blame_line *lines;
423 int nlines;
424 off_t *line_offsets;
425 pthread_t thread;
426 struct tog_blame_thread_args thread_args;
427 struct tog_blame_cb_args cb_args;
428 const char *path;
429 int *pack_fds;
430 };
432 struct tog_blame_view_state {
433 int first_displayed_line;
434 int last_displayed_line;
435 int selected_line;
436 int last_diffed_line;
437 int blame_complete;
438 int eof;
439 int done;
440 struct got_object_id_queue blamed_commits;
441 struct got_object_qid *blamed_commit;
442 char *path;
443 struct got_repository *repo;
444 struct got_object_id *commit_id;
445 struct tog_blame blame;
446 int matched_line;
447 struct tog_colors colors;
448 };
450 struct tog_parent_tree {
451 TAILQ_ENTRY(tog_parent_tree) entry;
452 struct got_tree_object *tree;
453 struct got_tree_entry *first_displayed_entry;
454 struct got_tree_entry *selected_entry;
455 int selected;
456 };
458 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
460 struct tog_tree_view_state {
461 char *tree_label;
462 struct got_object_id *commit_id;/* commit which this tree belongs to */
463 struct got_tree_object *root; /* the commit's root tree entry */
464 struct got_tree_object *tree; /* currently displayed (sub-)tree */
465 struct got_tree_entry *first_displayed_entry;
466 struct got_tree_entry *last_displayed_entry;
467 struct got_tree_entry *selected_entry;
468 int ndisplayed, selected, show_ids;
469 struct tog_parent_trees parents; /* parent trees of current sub-tree */
470 char *head_ref_name;
471 struct got_repository *repo;
472 struct got_tree_entry *matched_entry;
473 struct tog_colors colors;
474 };
476 struct tog_reflist_entry {
477 TAILQ_ENTRY(tog_reflist_entry) entry;
478 struct got_reference *ref;
479 int idx;
480 };
482 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
484 struct tog_ref_view_state {
485 struct tog_reflist_head refs;
486 struct tog_reflist_entry *first_displayed_entry;
487 struct tog_reflist_entry *last_displayed_entry;
488 struct tog_reflist_entry *selected_entry;
489 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
490 struct got_repository *repo;
491 struct tog_reflist_entry *matched_entry;
492 struct tog_colors colors;
493 };
495 /*
496 * We implement two types of views: parent views and child views.
498 * The 'Tab' key switches focus between a parent view and its child view.
499 * Child views are shown side-by-side to their parent view, provided
500 * there is enough screen estate.
502 * When a new view is opened from within a parent view, this new view
503 * becomes a child view of the parent view, replacing any existing child.
505 * When a new view is opened from within a child view, this new view
506 * becomes a parent view which will obscure the views below until the
507 * user quits the new parent view by typing 'q'.
509 * This list of views contains parent views only.
510 * Child views are only pointed to by their parent view.
511 */
512 TAILQ_HEAD(tog_view_list_head, tog_view);
514 struct tog_view {
515 TAILQ_ENTRY(tog_view) entry;
516 WINDOW *window;
517 PANEL *panel;
518 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
519 int resized_y, resized_x; /* begin_y/x based on user resizing */
520 int maxx, x; /* max column and current start column */
521 int lines, cols; /* copies of LINES and COLS */
522 int nscrolled, offset; /* lines scrolled and hsplit line offset */
523 int ch, count; /* current keymap and count prefix */
524 int resize; /* set when in a resize event */
525 int focussed; /* Only set on one parent or child view at a time. */
526 int dying;
527 struct tog_view *parent;
528 struct tog_view *child;
530 /*
531 * This flag is initially set on parent views when a new child view
532 * is created. It gets toggled when the 'Tab' key switches focus
533 * between parent and child.
534 * The flag indicates whether focus should be passed on to our child
535 * view if this parent view gets picked for focus after another parent
536 * view was closed. This prevents child views from losing focus in such
537 * situations.
538 */
539 int focus_child;
541 enum tog_view_mode mode;
542 /* type-specific state */
543 enum tog_view_type type;
544 union {
545 struct tog_diff_view_state diff;
546 struct tog_log_view_state log;
547 struct tog_blame_view_state blame;
548 struct tog_tree_view_state tree;
549 struct tog_ref_view_state ref;
550 } state;
552 const struct got_error *(*show)(struct tog_view *);
553 const struct got_error *(*input)(struct tog_view **,
554 struct tog_view *, int);
555 const struct got_error *(*reset)(struct tog_view *);
556 const struct got_error *(*close)(struct tog_view *);
558 const struct got_error *(*search_start)(struct tog_view *);
559 const struct got_error *(*search_next)(struct tog_view *);
560 int search_started;
561 int searching;
562 #define TOG_SEARCH_FORWARD 1
563 #define TOG_SEARCH_BACKWARD 2
564 int search_next_done;
565 #define TOG_SEARCH_HAVE_MORE 1
566 #define TOG_SEARCH_NO_MORE 2
567 #define TOG_SEARCH_HAVE_NONE 3
568 regex_t regex;
569 regmatch_t regmatch;
570 };
572 static const struct got_error *open_diff_view(struct tog_view *,
573 struct got_object_id *, struct got_object_id *,
574 const char *, const char *, int, int, int, struct tog_view *,
575 struct got_repository *);
576 static const struct got_error *show_diff_view(struct tog_view *);
577 static const struct got_error *input_diff_view(struct tog_view **,
578 struct tog_view *, int);
579 static const struct got_error *reset_diff_view(struct tog_view *);
580 static const struct got_error* close_diff_view(struct tog_view *);
581 static const struct got_error *search_start_diff_view(struct tog_view *);
582 static const struct got_error *search_next_diff_view(struct tog_view *);
584 static const struct got_error *open_log_view(struct tog_view *,
585 struct got_object_id *, struct got_repository *,
586 const char *, const char *, int);
587 static const struct got_error * show_log_view(struct tog_view *);
588 static const struct got_error *input_log_view(struct tog_view **,
589 struct tog_view *, int);
590 static const struct got_error *close_log_view(struct tog_view *);
591 static const struct got_error *search_start_log_view(struct tog_view *);
592 static const struct got_error *search_next_log_view(struct tog_view *);
594 static const struct got_error *open_blame_view(struct tog_view *, char *,
595 struct got_object_id *, struct got_repository *);
596 static const struct got_error *show_blame_view(struct tog_view *);
597 static const struct got_error *input_blame_view(struct tog_view **,
598 struct tog_view *, int);
599 static const struct got_error *reset_blame_view(struct tog_view *);
600 static const struct got_error *close_blame_view(struct tog_view *);
601 static const struct got_error *search_start_blame_view(struct tog_view *);
602 static const struct got_error *search_next_blame_view(struct tog_view *);
604 static const struct got_error *open_tree_view(struct tog_view *,
605 struct got_object_id *, const char *, struct got_repository *);
606 static const struct got_error *show_tree_view(struct tog_view *);
607 static const struct got_error *input_tree_view(struct tog_view **,
608 struct tog_view *, int);
609 static const struct got_error *close_tree_view(struct tog_view *);
610 static const struct got_error *search_start_tree_view(struct tog_view *);
611 static const struct got_error *search_next_tree_view(struct tog_view *);
613 static const struct got_error *open_ref_view(struct tog_view *,
614 struct got_repository *);
615 static const struct got_error *show_ref_view(struct tog_view *);
616 static const struct got_error *input_ref_view(struct tog_view **,
617 struct tog_view *, int);
618 static const struct got_error *close_ref_view(struct tog_view *);
619 static const struct got_error *search_start_ref_view(struct tog_view *);
620 static const struct got_error *search_next_ref_view(struct tog_view *);
622 static volatile sig_atomic_t tog_sigwinch_received;
623 static volatile sig_atomic_t tog_sigpipe_received;
624 static volatile sig_atomic_t tog_sigcont_received;
625 static volatile sig_atomic_t tog_sigint_received;
626 static volatile sig_atomic_t tog_sigterm_received;
628 static void
629 tog_sigwinch(int signo)
631 tog_sigwinch_received = 1;
634 static void
635 tog_sigpipe(int signo)
637 tog_sigpipe_received = 1;
640 static void
641 tog_sigcont(int signo)
643 tog_sigcont_received = 1;
646 static void
647 tog_sigint(int signo)
649 tog_sigint_received = 1;
652 static void
653 tog_sigterm(int signo)
655 tog_sigterm_received = 1;
658 static int
659 tog_fatal_signal_received(void)
661 return (tog_sigpipe_received ||
662 tog_sigint_received || tog_sigint_received);
665 static const struct got_error *
666 view_close(struct tog_view *view)
668 const struct got_error *err = NULL, *child_err = NULL;
670 if (view->child) {
671 child_err = view_close(view->child);
672 view->child = NULL;
674 if (view->close)
675 err = view->close(view);
676 if (view->panel)
677 del_panel(view->panel);
678 if (view->window)
679 delwin(view->window);
680 free(view);
681 return err ? err : child_err;
684 static struct tog_view *
685 view_open(int nlines, int ncols, int begin_y, int begin_x,
686 enum tog_view_type type)
688 struct tog_view *view = calloc(1, sizeof(*view));
690 if (view == NULL)
691 return NULL;
693 view->type = type;
694 view->lines = LINES;
695 view->cols = COLS;
696 view->nlines = nlines ? nlines : LINES - begin_y;
697 view->ncols = ncols ? ncols : COLS - begin_x;
698 view->begin_y = begin_y;
699 view->begin_x = begin_x;
700 view->window = newwin(nlines, ncols, begin_y, begin_x);
701 if (view->window == NULL) {
702 view_close(view);
703 return NULL;
705 view->panel = new_panel(view->window);
706 if (view->panel == NULL ||
707 set_panel_userptr(view->panel, view) != OK) {
708 view_close(view);
709 return NULL;
712 keypad(view->window, TRUE);
713 return view;
716 static int
717 view_split_begin_x(int begin_x)
719 if (begin_x > 0 || COLS < 120)
720 return 0;
721 return (COLS - MAX(COLS / 2, 80));
724 /* XXX Stub till we decide what to do. */
725 static int
726 view_split_begin_y(int lines)
728 return lines * HSPLIT_SCALE;
731 static const struct got_error *view_resize(struct tog_view *);
733 static const struct got_error *
734 view_splitscreen(struct tog_view *view)
736 const struct got_error *err = NULL;
738 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 if (view->resized_y && view->resized_y < view->lines)
740 view->begin_y = view->resized_y;
741 else
742 view->begin_y = view_split_begin_y(view->nlines);
743 view->begin_x = 0;
744 } else if (!view->resize) {
745 if (view->resized_x && view->resized_x < view->cols - 1 &&
746 view->cols > 119)
747 view->begin_x = view->resized_x;
748 else
749 view->begin_x = view_split_begin_x(0);
750 view->begin_y = 0;
752 view->nlines = LINES - view->begin_y;
753 view->ncols = COLS - view->begin_x;
754 view->lines = LINES;
755 view->cols = COLS;
756 err = view_resize(view);
757 if (err)
758 return err;
760 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 view->parent->nlines = view->begin_y;
763 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 return got_error_from_errno("mvwin");
766 return NULL;
769 static const struct got_error *
770 view_fullscreen(struct tog_view *view)
772 const struct got_error *err = NULL;
774 view->begin_x = 0;
775 view->begin_y = view->resize ? view->begin_y : 0;
776 view->nlines = view->resize ? view->nlines : LINES;
777 view->ncols = COLS;
778 view->lines = LINES;
779 view->cols = COLS;
780 err = view_resize(view);
781 if (err)
782 return err;
784 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 return got_error_from_errno("mvwin");
787 return NULL;
790 static int
791 view_is_parent_view(struct tog_view *view)
793 return view->parent == NULL;
796 static int
797 view_is_splitscreen(struct tog_view *view)
799 return view->begin_x > 0 || view->begin_y > 0;
802 static int
803 view_is_fullscreen(struct tog_view *view)
805 return view->nlines == LINES && view->ncols == COLS;
808 static int
809 view_is_hsplit_top(struct tog_view *view)
811 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 view_is_splitscreen(view->child);
815 static void
816 view_border(struct tog_view *view)
818 PANEL *panel;
819 const struct tog_view *view_above;
821 if (view->parent)
822 return view_border(view->parent);
824 panel = panel_above(view->panel);
825 if (panel == NULL)
826 return;
828 view_above = panel_userptr(panel);
829 if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 mvwhline(view->window, view_above->begin_y - 1,
831 view->begin_x, got_locale_is_utf8() ?
832 ACS_HLINE : '-', view->ncols);
833 else
834 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
838 static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 static const struct got_error *request_log_commits(struct tog_view *);
840 static const struct got_error *offset_selection_down(struct tog_view *);
841 static void offset_selection_up(struct tog_view *);
842 static void view_get_split(struct tog_view *, int *, int *);
844 static const struct got_error *
845 view_resize(struct tog_view *view)
847 const struct got_error *err = NULL;
848 int dif, nlines, ncols;
850 dif = LINES - view->lines; /* line difference */
852 if (view->lines > LINES)
853 nlines = view->nlines - (view->lines - LINES);
854 else
855 nlines = view->nlines + (LINES - view->lines);
856 if (view->cols > COLS)
857 ncols = view->ncols - (view->cols - COLS);
858 else
859 ncols = view->ncols + (COLS - view->cols);
861 if (view->child) {
862 int hs = view->child->begin_y;
864 if (!view_is_fullscreen(view))
865 view->child->begin_x = view_split_begin_x(view->begin_x);
866 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 view->child->begin_x == 0) {
868 ncols = COLS;
870 view_fullscreen(view->child);
871 if (view->child->focussed)
872 show_panel(view->child->panel);
873 else
874 show_panel(view->panel);
875 } else {
876 ncols = view->child->begin_x;
878 view_splitscreen(view->child);
879 show_panel(view->child->panel);
881 /*
882 * Request commits if terminal height was increased in a log
883 * view so we have enough commits loaded to populate the view.
884 */
885 if (view->type == TOG_VIEW_LOG && dif > 0) {
886 struct tog_log_view_state *ts = &view->state.log;
888 if (ts->commits.ncommits < ts->selected_entry->idx +
889 view->lines - ts->selected) {
890 view->nscrolled = ts->selected_entry->idx +
891 view->lines - ts->selected -
892 ts->commits.ncommits + dif;
893 err = request_log_commits(view);
894 if (err)
895 return err;
899 /*
900 * XXX This is ugly and needs to be moved into the above
901 * logic but "works" for now and my attempts at moving it
902 * break either 'tab' or 'F' key maps in horizontal splits.
903 */
904 if (hs) {
905 err = view_splitscreen(view->child);
906 if (err)
907 return err;
908 if (dif < 0) { /* top split decreased */
909 err = offset_selection_down(view);
910 if (err)
911 return err;
913 view_border(view);
914 update_panels();
915 doupdate();
916 show_panel(view->child->panel);
917 nlines = view->nlines;
919 } else if (view->parent == NULL)
920 ncols = COLS;
922 if (wresize(view->window, nlines, ncols) == ERR)
923 return got_error_from_errno("wresize");
924 if (replace_panel(view->panel, view->window) == ERR)
925 return got_error_from_errno("replace_panel");
926 wclear(view->window);
928 view->nlines = nlines;
929 view->ncols = ncols;
930 view->lines = LINES;
931 view->cols = COLS;
933 return NULL;
936 static void
937 view_adjust_offset(struct tog_view *view, int n)
939 if (n == 0)
940 return;
942 if (view->parent && view->parent->offset) {
943 if (view->parent->offset + n >= 0)
944 view->parent->offset += n;
945 else
946 view->parent->offset = 0;
947 } else if (view->offset) {
948 if (view->offset - n >= 0)
949 view->offset -= n;
950 else
951 view->offset = 0;
955 static const struct got_error *
956 view_resize_split(struct tog_view *view, int resize)
958 const struct got_error *err = NULL;
959 struct tog_view *v = NULL;
961 if (view->parent)
962 v = view->parent;
963 else
964 v = view;
966 if (!v->child || !view_is_splitscreen(v->child))
967 return NULL;
969 v->resize = v->child->resize = resize; /* lock for resize event */
971 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
972 int y = v->child->begin_y;
974 if (v->child->resized_y)
975 v->child->begin_y = v->child->resized_y;
976 if (view->parent)
977 v->child->begin_y -= resize;
978 else
979 v->child->begin_y += resize;
980 if (v->child->begin_y < 3) {
981 view->count = 0;
982 v->child->begin_y = 3;
983 } else if (v->child->begin_y > LINES - 1) {
984 view->count = 0;
985 v->child->begin_y = LINES - 1;
987 v->ncols = COLS;
988 v->child->ncols = COLS;
989 view_adjust_offset(view, resize);
990 err = view_init_hsplit(v, v->child->begin_y);
991 if (err)
992 return err;
993 v->child->resized_y = v->child->begin_y;
994 if (y > v->child->begin_y) /* split increased */
995 v->child->nscrolled = y - v->child->begin_y;
996 } else {
997 if (v->child->resized_x)
998 v->child->begin_x = v->child->resized_x;
999 if (view->parent)
1000 v->child->begin_x -= resize;
1001 else
1002 v->child->begin_x += resize;
1003 if (v->child->begin_x < 11) {
1004 view->count = 0;
1005 v->child->begin_x = 11;
1006 } else if (v->child->begin_x > COLS - 1) {
1007 view->count = 0;
1008 v->child->begin_x = COLS - 1;
1010 v->child->resized_x = v->child->begin_x;
1013 v->child->mode = v->mode;
1014 v->child->nlines = v->lines - v->child->begin_y;
1015 v->child->ncols = v->cols - v->child->begin_x;
1016 v->focus_child = 1;
1018 err = view_fullscreen(v);
1019 if (err)
1020 return err;
1021 err = view_splitscreen(v->child);
1022 if (err)
1023 return err;
1025 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1026 err = offset_selection_down(v->child);
1027 if (err)
1028 return err;
1031 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1032 err = request_log_commits(v);
1033 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1034 err = request_log_commits(v->child);
1036 v->resize = v->child->resize = 0;
1038 return err;
1041 static void
1042 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1044 struct tog_view *v = src->child ? src->child : src;
1046 dst->resized_x = v->resized_x;
1047 dst->resized_y = v->resized_y;
1050 static const struct got_error *
1051 view_close_child(struct tog_view *view)
1053 const struct got_error *err = NULL;
1055 if (view->child == NULL)
1056 return NULL;
1058 err = view_close(view->child);
1059 view->child = NULL;
1060 return err;
1063 static const struct got_error *
1064 view_set_child(struct tog_view *view, struct tog_view *child)
1066 const struct got_error *err = NULL;
1068 view->child = child;
1069 child->parent = view;
1071 err = view_resize(view);
1072 if (err)
1073 return err;
1075 if (view->child->resized_x || view->child->resized_y)
1076 err = view_resize_split(view, 0);
1078 return err;
1081 static void
1082 tog_resizeterm(void)
1084 int cols, lines;
1085 struct winsize size;
1087 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1088 cols = 80; /* Default */
1089 lines = 24;
1090 } else {
1091 cols = size.ws_col;
1092 lines = size.ws_row;
1094 resize_term(lines, cols);
1097 static const struct got_error *
1098 view_search_start(struct tog_view *view)
1100 const struct got_error *err = NULL;
1101 struct tog_view *v = view;
1102 char pattern[1024];
1103 int ret;
1105 if (view->search_started) {
1106 regfree(&view->regex);
1107 view->searching = 0;
1108 memset(&view->regmatch, 0, sizeof(view->regmatch));
1110 view->search_started = 0;
1112 if (view->nlines < 1)
1113 return NULL;
1115 if (view_is_hsplit_top(view))
1116 v = view->child;
1118 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1119 wclrtoeol(v->window);
1121 nodelay(view->window, FALSE); /* block for search term input */
1122 nocbreak();
1123 echo();
1124 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1125 wrefresh(v->window);
1126 cbreak();
1127 noecho();
1128 nodelay(view->window, TRUE);
1129 if (ret == ERR)
1130 return NULL;
1132 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1133 err = view->search_start(view);
1134 if (err) {
1135 regfree(&view->regex);
1136 return err;
1138 view->search_started = 1;
1139 view->searching = TOG_SEARCH_FORWARD;
1140 view->search_next_done = 0;
1141 view->search_next(view);
1144 return NULL;
1147 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1148 static const struct got_error *
1149 switch_split(struct tog_view *view)
1151 const struct got_error *err = NULL;
1152 struct tog_view *v = NULL;
1154 if (view->parent)
1155 v = view->parent;
1156 else
1157 v = view;
1159 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1160 v->mode = TOG_VIEW_SPLIT_VERT;
1161 else
1162 v->mode = TOG_VIEW_SPLIT_HRZN;
1164 if (!v->child)
1165 return NULL;
1166 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1167 v->mode = TOG_VIEW_SPLIT_NONE;
1169 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1170 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1171 v->child->begin_y = v->child->resized_y;
1172 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1173 v->child->begin_x = v->child->resized_x;
1176 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1177 v->ncols = COLS;
1178 v->child->ncols = COLS;
1179 v->child->nscrolled = LINES - v->child->nlines;
1181 err = view_init_hsplit(v, v->child->begin_y);
1182 if (err)
1183 return err;
1185 v->child->mode = v->mode;
1186 v->child->nlines = v->lines - v->child->begin_y;
1187 v->focus_child = 1;
1189 err = view_fullscreen(v);
1190 if (err)
1191 return err;
1192 err = view_splitscreen(v->child);
1193 if (err)
1194 return err;
1196 if (v->mode == TOG_VIEW_SPLIT_NONE)
1197 v->mode = TOG_VIEW_SPLIT_VERT;
1198 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1199 err = offset_selection_down(v);
1200 err = offset_selection_down(v->child);
1201 } else {
1202 offset_selection_up(v);
1203 offset_selection_up(v->child);
1205 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1206 err = request_log_commits(v);
1207 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1208 err = request_log_commits(v->child);
1210 return err;
1214 * Compute view->count from numeric input. Assign total to view->count and
1215 * return first non-numeric key entered.
1217 static int
1218 get_compound_key(struct tog_view *view, int c)
1220 struct tog_view *v = view;
1221 int x, n = 0;
1223 if (view_is_hsplit_top(view))
1224 v = view->child;
1225 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1226 v = view->parent;
1228 view->count = 0;
1229 cbreak(); /* block for input */
1230 wmove(v->window, v->nlines - 1, 0);
1231 wclrtoeol(v->window);
1232 waddch(v->window, ':');
1234 do {
1235 x = getcurx(v->window);
1236 if (x != ERR && x < view->ncols) {
1237 waddch(v->window, c);
1238 wrefresh(v->window);
1242 * Don't overflow. Max valid request should be the greatest
1243 * between the longest and total lines; cap at 10 million.
1245 if (n >= 9999999)
1246 n = 9999999;
1247 else
1248 n = n * 10 + (c - '0');
1249 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1251 /* Massage excessive or inapplicable values at the input handler. */
1252 view->count = n;
1254 return c;
1257 static const struct got_error *
1258 view_input(struct tog_view **new, int *done, struct tog_view *view,
1259 struct tog_view_list_head *views)
1261 const struct got_error *err = NULL;
1262 struct tog_view *v;
1263 int ch, errcode;
1265 *new = NULL;
1267 /* Clear "no matches" indicator. */
1268 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1269 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1270 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1271 view->count = 0;
1274 if (view->searching && !view->search_next_done) {
1275 errcode = pthread_mutex_unlock(&tog_mutex);
1276 if (errcode)
1277 return got_error_set_errno(errcode,
1278 "pthread_mutex_unlock");
1279 sched_yield();
1280 errcode = pthread_mutex_lock(&tog_mutex);
1281 if (errcode)
1282 return got_error_set_errno(errcode,
1283 "pthread_mutex_lock");
1284 view->search_next(view);
1285 return NULL;
1288 nodelay(view->window, FALSE);
1289 /* Allow threads to make progress while we are waiting for input. */
1290 errcode = pthread_mutex_unlock(&tog_mutex);
1291 if (errcode)
1292 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1293 /* If we have an unfinished count, let C-g or backspace abort. */
1294 if (view->count && --view->count) {
1295 cbreak();
1296 nodelay(view->window, TRUE);
1297 ch = wgetch(view->window);
1298 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1299 view->count = 0;
1300 else
1301 ch = view->ch;
1302 } else {
1303 ch = wgetch(view->window);
1304 if (ch >= '1' && ch <= '9')
1305 view->ch = ch = get_compound_key(view, ch);
1307 errcode = pthread_mutex_lock(&tog_mutex);
1308 if (errcode)
1309 return got_error_set_errno(errcode, "pthread_mutex_lock");
1310 nodelay(view->window, TRUE);
1312 if (tog_sigwinch_received || tog_sigcont_received) {
1313 tog_resizeterm();
1314 tog_sigwinch_received = 0;
1315 tog_sigcont_received = 0;
1316 TAILQ_FOREACH(v, views, entry) {
1317 err = view_resize(v);
1318 if (err)
1319 return err;
1320 err = v->input(new, v, KEY_RESIZE);
1321 if (err)
1322 return err;
1323 if (v->child) {
1324 err = view_resize(v->child);
1325 if (err)
1326 return err;
1327 err = v->child->input(new, v->child,
1328 KEY_RESIZE);
1329 if (err)
1330 return err;
1331 if (v->child->resized_x || v->child->resized_y) {
1332 err = view_resize_split(v, 0);
1333 if (err)
1334 return err;
1340 switch (ch) {
1341 case '\t':
1342 view->count = 0;
1343 if (view->child) {
1344 view->focussed = 0;
1345 view->child->focussed = 1;
1346 view->focus_child = 1;
1347 } else if (view->parent) {
1348 view->focussed = 0;
1349 view->parent->focussed = 1;
1350 view->parent->focus_child = 0;
1351 if (!view_is_splitscreen(view)) {
1352 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1353 view->parent->type == TOG_VIEW_LOG) {
1354 err = request_log_commits(view->parent);
1355 if (err)
1356 return err;
1358 offset_selection_up(view->parent);
1359 err = view_fullscreen(view->parent);
1360 if (err)
1361 return err;
1364 break;
1365 case 'q':
1366 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1367 if (view->parent->type == TOG_VIEW_LOG) {
1368 /* might need more commits to fill fullscreen */
1369 err = request_log_commits(view->parent);
1370 if (err)
1371 break;
1373 offset_selection_up(view->parent);
1375 err = view->input(new, view, ch);
1376 view->dying = 1;
1377 break;
1378 case 'Q':
1379 *done = 1;
1380 break;
1381 case 'F':
1382 view->count = 0;
1383 if (view_is_parent_view(view)) {
1384 if (view->child == NULL)
1385 break;
1386 if (view_is_splitscreen(view->child)) {
1387 view->focussed = 0;
1388 view->child->focussed = 1;
1389 err = view_fullscreen(view->child);
1390 } else {
1391 err = view_splitscreen(view->child);
1392 if (!err)
1393 err = view_resize_split(view, 0);
1395 if (err)
1396 break;
1397 err = view->child->input(new, view->child,
1398 KEY_RESIZE);
1399 } else {
1400 if (view_is_splitscreen(view)) {
1401 view->parent->focussed = 0;
1402 view->focussed = 1;
1403 err = view_fullscreen(view);
1404 } else {
1405 err = view_splitscreen(view);
1406 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1407 err = view_resize(view->parent);
1408 if (!err)
1409 err = view_resize_split(view, 0);
1411 if (err)
1412 break;
1413 err = view->input(new, view, KEY_RESIZE);
1415 if (err)
1416 break;
1417 if (view->type == TOG_VIEW_LOG) {
1418 err = request_log_commits(view);
1419 if (err)
1420 break;
1422 if (view->parent)
1423 err = offset_selection_down(view->parent);
1424 if (!err)
1425 err = offset_selection_down(view);
1426 break;
1427 case 'S':
1428 view->count = 0;
1429 err = switch_split(view);
1430 break;
1431 case '-':
1432 err = view_resize_split(view, -1);
1433 break;
1434 case '+':
1435 err = view_resize_split(view, 1);
1436 break;
1437 case KEY_RESIZE:
1438 break;
1439 case '/':
1440 view->count = 0;
1441 if (view->search_start)
1442 view_search_start(view);
1443 else
1444 err = view->input(new, view, ch);
1445 break;
1446 case 'N':
1447 case 'n':
1448 if (view->search_started && view->search_next) {
1449 view->searching = (ch == 'n' ?
1450 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1451 view->search_next_done = 0;
1452 view->search_next(view);
1453 } else
1454 err = view->input(new, view, ch);
1455 break;
1456 case 'A':
1457 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1458 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1459 else
1460 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1461 TAILQ_FOREACH(v, views, entry) {
1462 if (v->reset) {
1463 err = v->reset(v);
1464 if (err)
1465 return err;
1467 if (v->child && v->child->reset) {
1468 err = v->child->reset(v->child);
1469 if (err)
1470 return err;
1473 break;
1474 default:
1475 err = view->input(new, view, ch);
1476 break;
1479 return err;
1482 static int
1483 view_needs_focus_indication(struct tog_view *view)
1485 if (view_is_parent_view(view)) {
1486 if (view->child == NULL || view->child->focussed)
1487 return 0;
1488 if (!view_is_splitscreen(view->child))
1489 return 0;
1490 } else if (!view_is_splitscreen(view))
1491 return 0;
1493 return view->focussed;
1496 static const struct got_error *
1497 view_loop(struct tog_view *view)
1499 const struct got_error *err = NULL;
1500 struct tog_view_list_head views;
1501 struct tog_view *new_view;
1502 char *mode;
1503 int fast_refresh = 10;
1504 int done = 0, errcode;
1506 mode = getenv("TOG_VIEW_SPLIT_MODE");
1507 if (!mode || !(*mode == 'h' || *mode == 'H'))
1508 view->mode = TOG_VIEW_SPLIT_VERT;
1509 else
1510 view->mode = TOG_VIEW_SPLIT_HRZN;
1512 errcode = pthread_mutex_lock(&tog_mutex);
1513 if (errcode)
1514 return got_error_set_errno(errcode, "pthread_mutex_lock");
1516 TAILQ_INIT(&views);
1517 TAILQ_INSERT_HEAD(&views, view, entry);
1519 view->focussed = 1;
1520 err = view->show(view);
1521 if (err)
1522 return err;
1523 update_panels();
1524 doupdate();
1525 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1526 !tog_fatal_signal_received()) {
1527 /* Refresh fast during initialization, then become slower. */
1528 if (fast_refresh && fast_refresh-- == 0)
1529 halfdelay(10); /* switch to once per second */
1531 err = view_input(&new_view, &done, view, &views);
1532 if (err)
1533 break;
1534 if (view->dying) {
1535 struct tog_view *v, *prev = NULL;
1537 if (view_is_parent_view(view))
1538 prev = TAILQ_PREV(view, tog_view_list_head,
1539 entry);
1540 else if (view->parent)
1541 prev = view->parent;
1543 if (view->parent) {
1544 view->parent->child = NULL;
1545 view->parent->focus_child = 0;
1546 /* Restore fullscreen line height. */
1547 view->parent->nlines = view->parent->lines;
1548 err = view_resize(view->parent);
1549 if (err)
1550 break;
1551 /* Make resized splits persist. */
1552 view_transfer_size(view->parent, view);
1553 } else
1554 TAILQ_REMOVE(&views, view, entry);
1556 err = view_close(view);
1557 if (err)
1558 goto done;
1560 view = NULL;
1561 TAILQ_FOREACH(v, &views, entry) {
1562 if (v->focussed)
1563 break;
1565 if (view == NULL && new_view == NULL) {
1566 /* No view has focus. Try to pick one. */
1567 if (prev)
1568 view = prev;
1569 else if (!TAILQ_EMPTY(&views)) {
1570 view = TAILQ_LAST(&views,
1571 tog_view_list_head);
1573 if (view) {
1574 if (view->focus_child) {
1575 view->child->focussed = 1;
1576 view = view->child;
1577 } else
1578 view->focussed = 1;
1582 if (new_view) {
1583 struct tog_view *v, *t;
1584 /* Only allow one parent view per type. */
1585 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1586 if (v->type != new_view->type)
1587 continue;
1588 TAILQ_REMOVE(&views, v, entry);
1589 err = view_close(v);
1590 if (err)
1591 goto done;
1592 break;
1594 TAILQ_INSERT_TAIL(&views, new_view, entry);
1595 view = new_view;
1597 if (view) {
1598 if (view_is_parent_view(view)) {
1599 if (view->child && view->child->focussed)
1600 view = view->child;
1601 } else {
1602 if (view->parent && view->parent->focussed)
1603 view = view->parent;
1605 show_panel(view->panel);
1606 if (view->child && view_is_splitscreen(view->child))
1607 show_panel(view->child->panel);
1608 if (view->parent && view_is_splitscreen(view)) {
1609 err = view->parent->show(view->parent);
1610 if (err)
1611 goto done;
1613 err = view->show(view);
1614 if (err)
1615 goto done;
1616 if (view->child) {
1617 err = view->child->show(view->child);
1618 if (err)
1619 goto done;
1621 update_panels();
1622 doupdate();
1625 done:
1626 while (!TAILQ_EMPTY(&views)) {
1627 const struct got_error *close_err;
1628 view = TAILQ_FIRST(&views);
1629 TAILQ_REMOVE(&views, view, entry);
1630 close_err = view_close(view);
1631 if (close_err && err == NULL)
1632 err = close_err;
1635 errcode = pthread_mutex_unlock(&tog_mutex);
1636 if (errcode && err == NULL)
1637 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1639 return err;
1642 __dead static void
1643 usage_log(void)
1645 endwin();
1646 fprintf(stderr,
1647 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1648 getprogname());
1649 exit(1);
1652 /* Create newly allocated wide-character string equivalent to a byte string. */
1653 static const struct got_error *
1654 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1656 char *vis = NULL;
1657 const struct got_error *err = NULL;
1659 *ws = NULL;
1660 *wlen = mbstowcs(NULL, s, 0);
1661 if (*wlen == (size_t)-1) {
1662 int vislen;
1663 if (errno != EILSEQ)
1664 return got_error_from_errno("mbstowcs");
1666 /* byte string invalid in current encoding; try to "fix" it */
1667 err = got_mbsavis(&vis, &vislen, s);
1668 if (err)
1669 return err;
1670 *wlen = mbstowcs(NULL, vis, 0);
1671 if (*wlen == (size_t)-1) {
1672 err = got_error_from_errno("mbstowcs"); /* give up */
1673 goto done;
1677 *ws = calloc(*wlen + 1, sizeof(**ws));
1678 if (*ws == NULL) {
1679 err = got_error_from_errno("calloc");
1680 goto done;
1683 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1684 err = got_error_from_errno("mbstowcs");
1685 done:
1686 free(vis);
1687 if (err) {
1688 free(*ws);
1689 *ws = NULL;
1690 *wlen = 0;
1692 return err;
1695 static const struct got_error *
1696 expand_tab(char **ptr, const char *src)
1698 char *dst;
1699 size_t len, n, idx = 0, sz = 0;
1701 *ptr = NULL;
1702 n = len = strlen(src);
1703 dst = malloc(n + 1);
1704 if (dst == NULL)
1705 return got_error_from_errno("malloc");
1707 while (idx < len && src[idx]) {
1708 const char c = src[idx];
1710 if (c == '\t') {
1711 size_t nb = TABSIZE - sz % TABSIZE;
1712 char *p;
1714 p = realloc(dst, n + nb);
1715 if (p == NULL) {
1716 free(dst);
1717 return got_error_from_errno("realloc");
1720 dst = p;
1721 n += nb;
1722 memset(dst + sz, ' ', nb);
1723 sz += nb;
1724 } else
1725 dst[sz++] = src[idx];
1726 ++idx;
1729 dst[sz] = '\0';
1730 *ptr = dst;
1731 return NULL;
1735 * Advance at most n columns from wline starting at offset off.
1736 * Return the index to the first character after the span operation.
1737 * Return the combined column width of all spanned wide character in
1738 * *rcol.
1740 static int
1741 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1743 int width, i, cols = 0;
1745 if (n == 0) {
1746 *rcol = cols;
1747 return off;
1750 for (i = off; wline[i] != L'\0'; ++i) {
1751 if (wline[i] == L'\t')
1752 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1753 else
1754 width = wcwidth(wline[i]);
1756 if (width == -1) {
1757 width = 1;
1758 wline[i] = L'.';
1761 if (cols + width > n)
1762 break;
1763 cols += width;
1766 *rcol = cols;
1767 return i;
1771 * Format a line for display, ensuring that it won't overflow a width limit.
1772 * With scrolling, the width returned refers to the scrolled version of the
1773 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1775 static const struct got_error *
1776 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1777 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1779 const struct got_error *err = NULL;
1780 int cols;
1781 wchar_t *wline = NULL;
1782 char *exstr = NULL;
1783 size_t wlen;
1784 int i, scrollx;
1786 *wlinep = NULL;
1787 *widthp = 0;
1789 if (expand) {
1790 err = expand_tab(&exstr, line);
1791 if (err)
1792 return err;
1795 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1796 free(exstr);
1797 if (err)
1798 return err;
1800 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1802 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1803 wline[wlen - 1] = L'\0';
1804 wlen--;
1806 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1807 wline[wlen - 1] = L'\0';
1808 wlen--;
1811 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1812 wline[i] = L'\0';
1814 if (widthp)
1815 *widthp = cols;
1816 if (scrollxp)
1817 *scrollxp = scrollx;
1818 if (err)
1819 free(wline);
1820 else
1821 *wlinep = wline;
1822 return err;
1825 static const struct got_error*
1826 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1827 struct got_object_id *id, struct got_repository *repo)
1829 static const struct got_error *err = NULL;
1830 struct got_reflist_entry *re;
1831 char *s;
1832 const char *name;
1834 *refs_str = NULL;
1836 TAILQ_FOREACH(re, refs, entry) {
1837 struct got_tag_object *tag = NULL;
1838 struct got_object_id *ref_id;
1839 int cmp;
1841 name = got_ref_get_name(re->ref);
1842 if (strcmp(name, GOT_REF_HEAD) == 0)
1843 continue;
1844 if (strncmp(name, "refs/", 5) == 0)
1845 name += 5;
1846 if (strncmp(name, "got/", 4) == 0 &&
1847 strncmp(name, "got/backup/", 11) != 0)
1848 continue;
1849 if (strncmp(name, "heads/", 6) == 0)
1850 name += 6;
1851 if (strncmp(name, "remotes/", 8) == 0) {
1852 name += 8;
1853 s = strstr(name, "/" GOT_REF_HEAD);
1854 if (s != NULL && s[strlen(s)] == '\0')
1855 continue;
1857 err = got_ref_resolve(&ref_id, repo, re->ref);
1858 if (err)
1859 break;
1860 if (strncmp(name, "tags/", 5) == 0) {
1861 err = got_object_open_as_tag(&tag, repo, ref_id);
1862 if (err) {
1863 if (err->code != GOT_ERR_OBJ_TYPE) {
1864 free(ref_id);
1865 break;
1867 /* Ref points at something other than a tag. */
1868 err = NULL;
1869 tag = NULL;
1872 cmp = got_object_id_cmp(tag ?
1873 got_object_tag_get_object_id(tag) : ref_id, id);
1874 free(ref_id);
1875 if (tag)
1876 got_object_tag_close(tag);
1877 if (cmp != 0)
1878 continue;
1879 s = *refs_str;
1880 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1881 s ? ", " : "", name) == -1) {
1882 err = got_error_from_errno("asprintf");
1883 free(s);
1884 *refs_str = NULL;
1885 break;
1887 free(s);
1890 return err;
1893 static const struct got_error *
1894 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1895 int col_tab_align)
1897 char *smallerthan;
1899 smallerthan = strchr(author, '<');
1900 if (smallerthan && smallerthan[1] != '\0')
1901 author = smallerthan + 1;
1902 author[strcspn(author, "@>")] = '\0';
1903 return format_line(wauthor, author_width, NULL, author, 0, limit,
1904 col_tab_align, 0);
1907 static const struct got_error *
1908 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1909 struct got_object_id *id, const size_t date_display_cols,
1910 int author_display_cols)
1912 struct tog_log_view_state *s = &view->state.log;
1913 const struct got_error *err = NULL;
1914 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1915 char *logmsg0 = NULL, *logmsg = NULL;
1916 char *author = NULL;
1917 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1918 int author_width, logmsg_width;
1919 char *newline, *line = NULL;
1920 int col, limit, scrollx;
1921 const int avail = view->ncols;
1922 struct tm tm;
1923 time_t committer_time;
1924 struct tog_color *tc;
1926 committer_time = got_object_commit_get_committer_time(commit);
1927 if (gmtime_r(&committer_time, &tm) == NULL)
1928 return got_error_from_errno("gmtime_r");
1929 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1930 return got_error(GOT_ERR_NO_SPACE);
1932 if (avail <= date_display_cols)
1933 limit = MIN(sizeof(datebuf) - 1, avail);
1934 else
1935 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1936 tc = get_color(&s->colors, TOG_COLOR_DATE);
1937 if (tc)
1938 wattr_on(view->window,
1939 COLOR_PAIR(tc->colorpair), NULL);
1940 waddnstr(view->window, datebuf, limit);
1941 if (tc)
1942 wattr_off(view->window,
1943 COLOR_PAIR(tc->colorpair), NULL);
1944 col = limit;
1945 if (col > avail)
1946 goto done;
1948 if (avail >= 120) {
1949 char *id_str;
1950 err = got_object_id_str(&id_str, id);
1951 if (err)
1952 goto done;
1953 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1954 if (tc)
1955 wattr_on(view->window,
1956 COLOR_PAIR(tc->colorpair), NULL);
1957 wprintw(view->window, "%.8s ", id_str);
1958 if (tc)
1959 wattr_off(view->window,
1960 COLOR_PAIR(tc->colorpair), NULL);
1961 free(id_str);
1962 col += 9;
1963 if (col > avail)
1964 goto done;
1967 author = strdup(got_object_commit_get_author(commit));
1968 if (author == NULL) {
1969 err = got_error_from_errno("strdup");
1970 goto done;
1972 err = format_author(&wauthor, &author_width, author, avail - col, col);
1973 if (err)
1974 goto done;
1975 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1976 if (tc)
1977 wattr_on(view->window,
1978 COLOR_PAIR(tc->colorpair), NULL);
1979 waddwstr(view->window, wauthor);
1980 if (tc)
1981 wattr_off(view->window,
1982 COLOR_PAIR(tc->colorpair), NULL);
1983 col += author_width;
1984 while (col < avail && author_width < author_display_cols + 2) {
1985 waddch(view->window, ' ');
1986 col++;
1987 author_width++;
1989 if (col > avail)
1990 goto done;
1992 err = got_object_commit_get_logmsg(&logmsg0, commit);
1993 if (err)
1994 goto done;
1995 logmsg = logmsg0;
1996 while (*logmsg == '\n')
1997 logmsg++;
1998 newline = strchr(logmsg, '\n');
1999 if (newline)
2000 *newline = '\0';
2001 limit = avail - col;
2002 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2003 limit--; /* for the border */
2004 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2005 limit, col, 1);
2006 if (err)
2007 goto done;
2008 waddwstr(view->window, &wlogmsg[scrollx]);
2009 col += MAX(logmsg_width, 0);
2010 while (col < avail) {
2011 waddch(view->window, ' ');
2012 col++;
2014 done:
2015 free(logmsg0);
2016 free(wlogmsg);
2017 free(author);
2018 free(wauthor);
2019 free(line);
2020 return err;
2023 static struct commit_queue_entry *
2024 alloc_commit_queue_entry(struct got_commit_object *commit,
2025 struct got_object_id *id)
2027 struct commit_queue_entry *entry;
2029 entry = calloc(1, sizeof(*entry));
2030 if (entry == NULL)
2031 return NULL;
2033 entry->id = id;
2034 entry->commit = commit;
2035 return entry;
2038 static void
2039 pop_commit(struct commit_queue *commits)
2041 struct commit_queue_entry *entry;
2043 entry = TAILQ_FIRST(&commits->head);
2044 TAILQ_REMOVE(&commits->head, entry, entry);
2045 got_object_commit_close(entry->commit);
2046 commits->ncommits--;
2047 /* Don't free entry->id! It is owned by the commit graph. */
2048 free(entry);
2051 static void
2052 free_commits(struct commit_queue *commits)
2054 while (!TAILQ_EMPTY(&commits->head))
2055 pop_commit(commits);
2058 static const struct got_error *
2059 match_commit(int *have_match, struct got_object_id *id,
2060 struct got_commit_object *commit, regex_t *regex)
2062 const struct got_error *err = NULL;
2063 regmatch_t regmatch;
2064 char *id_str = NULL, *logmsg = NULL;
2066 *have_match = 0;
2068 err = got_object_id_str(&id_str, id);
2069 if (err)
2070 return err;
2072 err = got_object_commit_get_logmsg(&logmsg, commit);
2073 if (err)
2074 goto done;
2076 if (regexec(regex, got_object_commit_get_author(commit), 1,
2077 &regmatch, 0) == 0 ||
2078 regexec(regex, got_object_commit_get_committer(commit), 1,
2079 &regmatch, 0) == 0 ||
2080 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2081 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2082 *have_match = 1;
2083 done:
2084 free(id_str);
2085 free(logmsg);
2086 return err;
2089 static const struct got_error *
2090 queue_commits(struct tog_log_thread_args *a)
2092 const struct got_error *err = NULL;
2095 * We keep all commits open throughout the lifetime of the log
2096 * view in order to avoid having to re-fetch commits from disk
2097 * while updating the display.
2099 do {
2100 struct got_object_id *id;
2101 struct got_commit_object *commit;
2102 struct commit_queue_entry *entry;
2103 int errcode;
2105 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2106 NULL, NULL);
2107 if (err || id == NULL)
2108 break;
2110 err = got_object_open_as_commit(&commit, a->repo, id);
2111 if (err)
2112 break;
2113 entry = alloc_commit_queue_entry(commit, id);
2114 if (entry == NULL) {
2115 err = got_error_from_errno("alloc_commit_queue_entry");
2116 break;
2119 errcode = pthread_mutex_lock(&tog_mutex);
2120 if (errcode) {
2121 err = got_error_set_errno(errcode,
2122 "pthread_mutex_lock");
2123 break;
2126 entry->idx = a->commits->ncommits;
2127 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2128 a->commits->ncommits++;
2130 if (*a->searching == TOG_SEARCH_FORWARD &&
2131 !*a->search_next_done) {
2132 int have_match;
2133 err = match_commit(&have_match, id, commit, a->regex);
2134 if (err)
2135 break;
2136 if (have_match)
2137 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 errcode = pthread_mutex_unlock(&tog_mutex);
2141 if (errcode && err == NULL)
2142 err = got_error_set_errno(errcode,
2143 "pthread_mutex_unlock");
2144 if (err)
2145 break;
2146 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2148 return err;
2151 static void
2152 select_commit(struct tog_log_view_state *s)
2154 struct commit_queue_entry *entry;
2155 int ncommits = 0;
2157 entry = s->first_displayed_entry;
2158 while (entry) {
2159 if (ncommits == s->selected) {
2160 s->selected_entry = entry;
2161 break;
2163 entry = TAILQ_NEXT(entry, entry);
2164 ncommits++;
2168 static const struct got_error *
2169 draw_commits(struct tog_view *view)
2171 const struct got_error *err = NULL;
2172 struct tog_log_view_state *s = &view->state.log;
2173 struct commit_queue_entry *entry = s->selected_entry;
2174 const int limit = view->nlines;
2175 int width;
2176 int ncommits, author_cols = 4;
2177 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2178 char *refs_str = NULL;
2179 wchar_t *wline;
2180 struct tog_color *tc;
2181 static const size_t date_display_cols = 12;
2183 if (s->selected_entry &&
2184 !(view->searching && view->search_next_done == 0)) {
2185 struct got_reflist_head *refs;
2186 err = got_object_id_str(&id_str, s->selected_entry->id);
2187 if (err)
2188 return err;
2189 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2190 s->selected_entry->id);
2191 if (refs) {
2192 err = build_refs_str(&refs_str, refs,
2193 s->selected_entry->id, s->repo);
2194 if (err)
2195 goto done;
2199 if (s->thread_args.commits_needed == 0)
2200 halfdelay(10); /* disable fast refresh */
2202 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2203 if (asprintf(&ncommits_str, " [%d/%d] %s",
2204 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2205 (view->searching && !view->search_next_done) ?
2206 "searching..." : "loading...") == -1) {
2207 err = got_error_from_errno("asprintf");
2208 goto done;
2210 } else {
2211 const char *search_str = NULL;
2213 if (view->searching) {
2214 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2215 search_str = "no more matches";
2216 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2217 search_str = "no matches found";
2218 else if (!view->search_next_done)
2219 search_str = "searching...";
2222 if (asprintf(&ncommits_str, " [%d/%d] %s",
2223 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2224 search_str ? search_str :
2225 (refs_str ? refs_str : "")) == -1) {
2226 err = got_error_from_errno("asprintf");
2227 goto done;
2231 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2232 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2233 "........................................",
2234 s->in_repo_path, ncommits_str) == -1) {
2235 err = got_error_from_errno("asprintf");
2236 header = NULL;
2237 goto done;
2239 } else if (asprintf(&header, "commit %s%s",
2240 id_str ? id_str : "........................................",
2241 ncommits_str) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 header = NULL;
2244 goto done;
2246 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2247 if (err)
2248 goto done;
2250 werase(view->window);
2252 if (view_needs_focus_indication(view))
2253 wstandout(view->window);
2254 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2255 if (tc)
2256 wattr_on(view->window,
2257 COLOR_PAIR(tc->colorpair), NULL);
2258 waddwstr(view->window, wline);
2259 if (tc)
2260 wattr_off(view->window,
2261 COLOR_PAIR(tc->colorpair), NULL);
2262 while (width < view->ncols) {
2263 waddch(view->window, ' ');
2264 width++;
2266 if (view_needs_focus_indication(view))
2267 wstandend(view->window);
2268 free(wline);
2269 if (limit <= 1)
2270 goto done;
2272 /* Grow author column size if necessary, and set view->maxx. */
2273 entry = s->first_displayed_entry;
2274 ncommits = 0;
2275 view->maxx = 0;
2276 while (entry) {
2277 char *author, *eol, *msg, *msg0;
2278 wchar_t *wauthor, *wmsg;
2279 int width;
2280 if (ncommits >= limit - 1)
2281 break;
2282 author = strdup(got_object_commit_get_author(entry->commit));
2283 if (author == NULL) {
2284 err = got_error_from_errno("strdup");
2285 goto done;
2287 err = format_author(&wauthor, &width, author, COLS,
2288 date_display_cols);
2289 if (author_cols < width)
2290 author_cols = width;
2291 free(wauthor);
2292 free(author);
2293 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2294 if (err)
2295 goto done;
2296 msg = msg0;
2297 while (*msg == '\n')
2298 ++msg;
2299 if ((eol = strchr(msg, '\n')))
2300 *eol = '\0';
2301 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2302 date_display_cols + author_cols, 0);
2303 if (err)
2304 goto done;
2305 view->maxx = MAX(view->maxx, width);
2306 free(msg0);
2307 free(wmsg);
2308 ncommits++;
2309 entry = TAILQ_NEXT(entry, entry);
2312 entry = s->first_displayed_entry;
2313 s->last_displayed_entry = s->first_displayed_entry;
2314 ncommits = 0;
2315 while (entry) {
2316 if (ncommits >= limit - 1)
2317 break;
2318 if (ncommits == s->selected)
2319 wstandout(view->window);
2320 err = draw_commit(view, entry->commit, entry->id,
2321 date_display_cols, author_cols);
2322 if (ncommits == s->selected)
2323 wstandend(view->window);
2324 if (err)
2325 goto done;
2326 ncommits++;
2327 s->last_displayed_entry = entry;
2328 entry = TAILQ_NEXT(entry, entry);
2331 view_border(view);
2332 done:
2333 free(id_str);
2334 free(refs_str);
2335 free(ncommits_str);
2336 free(header);
2337 return err;
2340 static void
2341 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2343 struct commit_queue_entry *entry;
2344 int nscrolled = 0;
2346 entry = TAILQ_FIRST(&s->commits.head);
2347 if (s->first_displayed_entry == entry)
2348 return;
2350 entry = s->first_displayed_entry;
2351 while (entry && nscrolled < maxscroll) {
2352 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2353 if (entry) {
2354 s->first_displayed_entry = entry;
2355 nscrolled++;
2360 static const struct got_error *
2361 trigger_log_thread(struct tog_view *view, int wait)
2363 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2364 int errcode;
2366 halfdelay(1); /* fast refresh while loading commits */
2368 while (!ta->log_complete && !tog_thread_error &&
2369 (ta->commits_needed > 0 || ta->load_all)) {
2370 /* Wake the log thread. */
2371 errcode = pthread_cond_signal(&ta->need_commits);
2372 if (errcode)
2373 return got_error_set_errno(errcode,
2374 "pthread_cond_signal");
2377 * The mutex will be released while the view loop waits
2378 * in wgetch(), at which time the log thread will run.
2380 if (!wait)
2381 break;
2383 /* Display progress update in log view. */
2384 show_log_view(view);
2385 update_panels();
2386 doupdate();
2388 /* Wait right here while next commit is being loaded. */
2389 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2390 if (errcode)
2391 return got_error_set_errno(errcode,
2392 "pthread_cond_wait");
2394 /* Display progress update in log view. */
2395 show_log_view(view);
2396 update_panels();
2397 doupdate();
2400 return NULL;
2403 static const struct got_error *
2404 request_log_commits(struct tog_view *view)
2406 struct tog_log_view_state *state = &view->state.log;
2407 const struct got_error *err = NULL;
2409 state->thread_args.commits_needed += view->nscrolled;
2410 err = trigger_log_thread(view, 1);
2411 view->nscrolled = 0;
2413 return err;
2416 static const struct got_error *
2417 log_scroll_down(struct tog_view *view, int maxscroll)
2419 struct tog_log_view_state *s = &view->state.log;
2420 const struct got_error *err = NULL;
2421 struct commit_queue_entry *pentry;
2422 int nscrolled = 0, ncommits_needed;
2424 if (s->last_displayed_entry == NULL)
2425 return NULL;
2427 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2428 if (s->commits.ncommits < ncommits_needed &&
2429 !s->thread_args.log_complete) {
2431 * Ask the log thread for required amount of commits.
2433 s->thread_args.commits_needed += maxscroll;
2434 err = trigger_log_thread(view, 1);
2435 if (err)
2436 return err;
2439 do {
2440 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2441 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2442 break;
2444 s->last_displayed_entry = pentry ?
2445 pentry : s->last_displayed_entry;;
2447 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2448 if (pentry == NULL)
2449 break;
2450 s->first_displayed_entry = pentry;
2451 } while (++nscrolled < maxscroll);
2453 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2454 view->nscrolled += nscrolled;
2455 else
2456 view->nscrolled = 0;
2458 return err;
2461 static const struct got_error *
2462 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2463 struct got_commit_object *commit, struct got_object_id *commit_id,
2464 struct tog_view *log_view, struct got_repository *repo)
2466 const struct got_error *err;
2467 struct got_object_qid *parent_id;
2468 struct tog_view *diff_view;
2470 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2471 if (diff_view == NULL)
2472 return got_error_from_errno("view_open");
2474 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2475 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2476 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2477 if (err == NULL)
2478 *new_view = diff_view;
2479 return err;
2482 static const struct got_error *
2483 tree_view_visit_subtree(struct tog_tree_view_state *s,
2484 struct got_tree_object *subtree)
2486 struct tog_parent_tree *parent;
2488 parent = calloc(1, sizeof(*parent));
2489 if (parent == NULL)
2490 return got_error_from_errno("calloc");
2492 parent->tree = s->tree;
2493 parent->first_displayed_entry = s->first_displayed_entry;
2494 parent->selected_entry = s->selected_entry;
2495 parent->selected = s->selected;
2496 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2497 s->tree = subtree;
2498 s->selected = 0;
2499 s->first_displayed_entry = NULL;
2500 return NULL;
2503 static const struct got_error *
2504 tree_view_walk_path(struct tog_tree_view_state *s,
2505 struct got_commit_object *commit, const char *path)
2507 const struct got_error *err = NULL;
2508 struct got_tree_object *tree = NULL;
2509 const char *p;
2510 char *slash, *subpath = NULL;
2512 /* Walk the path and open corresponding tree objects. */
2513 p = path;
2514 while (*p) {
2515 struct got_tree_entry *te;
2516 struct got_object_id *tree_id;
2517 char *te_name;
2519 while (p[0] == '/')
2520 p++;
2522 /* Ensure the correct subtree entry is selected. */
2523 slash = strchr(p, '/');
2524 if (slash == NULL)
2525 te_name = strdup(p);
2526 else
2527 te_name = strndup(p, slash - p);
2528 if (te_name == NULL) {
2529 err = got_error_from_errno("strndup");
2530 break;
2532 te = got_object_tree_find_entry(s->tree, te_name);
2533 if (te == NULL) {
2534 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2535 free(te_name);
2536 break;
2538 free(te_name);
2539 s->first_displayed_entry = s->selected_entry = te;
2541 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2542 break; /* jump to this file's entry */
2544 slash = strchr(p, '/');
2545 if (slash)
2546 subpath = strndup(path, slash - path);
2547 else
2548 subpath = strdup(path);
2549 if (subpath == NULL) {
2550 err = got_error_from_errno("strdup");
2551 break;
2554 err = got_object_id_by_path(&tree_id, s->repo, commit,
2555 subpath);
2556 if (err)
2557 break;
2559 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2560 free(tree_id);
2561 if (err)
2562 break;
2564 err = tree_view_visit_subtree(s, tree);
2565 if (err) {
2566 got_object_tree_close(tree);
2567 break;
2569 if (slash == NULL)
2570 break;
2571 free(subpath);
2572 subpath = NULL;
2573 p = slash;
2576 free(subpath);
2577 return err;
2580 static const struct got_error *
2581 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2582 struct commit_queue_entry *entry, const char *path,
2583 const char *head_ref_name, struct got_repository *repo)
2585 const struct got_error *err = NULL;
2586 struct tog_tree_view_state *s;
2587 struct tog_view *tree_view;
2589 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2590 if (tree_view == NULL)
2591 return got_error_from_errno("view_open");
2593 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2594 if (err)
2595 return err;
2596 s = &tree_view->state.tree;
2598 *new_view = tree_view;
2600 if (got_path_is_root_dir(path))
2601 return NULL;
2603 return tree_view_walk_path(s, entry->commit, path);
2606 static const struct got_error *
2607 block_signals_used_by_main_thread(void)
2609 sigset_t sigset;
2610 int errcode;
2612 if (sigemptyset(&sigset) == -1)
2613 return got_error_from_errno("sigemptyset");
2615 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2616 if (sigaddset(&sigset, SIGWINCH) == -1)
2617 return got_error_from_errno("sigaddset");
2618 if (sigaddset(&sigset, SIGCONT) == -1)
2619 return got_error_from_errno("sigaddset");
2620 if (sigaddset(&sigset, SIGINT) == -1)
2621 return got_error_from_errno("sigaddset");
2622 if (sigaddset(&sigset, SIGTERM) == -1)
2623 return got_error_from_errno("sigaddset");
2625 /* ncurses handles SIGTSTP */
2626 if (sigaddset(&sigset, SIGTSTP) == -1)
2627 return got_error_from_errno("sigaddset");
2629 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2630 if (errcode)
2631 return got_error_set_errno(errcode, "pthread_sigmask");
2633 return NULL;
2636 static void *
2637 log_thread(void *arg)
2639 const struct got_error *err = NULL;
2640 int errcode = 0;
2641 struct tog_log_thread_args *a = arg;
2642 int done = 0;
2645 * Sync startup with main thread such that we begin our
2646 * work once view_input() has released the mutex.
2648 errcode = pthread_mutex_lock(&tog_mutex);
2649 if (errcode) {
2650 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2651 return (void *)err;
2654 err = block_signals_used_by_main_thread();
2655 if (err) {
2656 pthread_mutex_unlock(&tog_mutex);
2657 goto done;
2660 while (!done && !err && !tog_fatal_signal_received()) {
2661 errcode = pthread_mutex_unlock(&tog_mutex);
2662 if (errcode) {
2663 err = got_error_set_errno(errcode,
2664 "pthread_mutex_unlock");
2665 goto done;
2667 err = queue_commits(a);
2668 if (err) {
2669 if (err->code != GOT_ERR_ITER_COMPLETED)
2670 goto done;
2671 err = NULL;
2672 done = 1;
2673 } else if (a->commits_needed > 0 && !a->load_all)
2674 a->commits_needed--;
2676 errcode = pthread_mutex_lock(&tog_mutex);
2677 if (errcode) {
2678 err = got_error_set_errno(errcode,
2679 "pthread_mutex_lock");
2680 goto done;
2681 } else if (*a->quit)
2682 done = 1;
2683 else if (*a->first_displayed_entry == NULL) {
2684 *a->first_displayed_entry =
2685 TAILQ_FIRST(&a->commits->head);
2686 *a->selected_entry = *a->first_displayed_entry;
2689 errcode = pthread_cond_signal(&a->commit_loaded);
2690 if (errcode) {
2691 err = got_error_set_errno(errcode,
2692 "pthread_cond_signal");
2693 pthread_mutex_unlock(&tog_mutex);
2694 goto done;
2697 if (done)
2698 a->commits_needed = 0;
2699 else {
2700 if (a->commits_needed == 0 && !a->load_all) {
2701 errcode = pthread_cond_wait(&a->need_commits,
2702 &tog_mutex);
2703 if (errcode) {
2704 err = got_error_set_errno(errcode,
2705 "pthread_cond_wait");
2706 pthread_mutex_unlock(&tog_mutex);
2707 goto done;
2709 if (*a->quit)
2710 done = 1;
2714 a->log_complete = 1;
2715 errcode = pthread_mutex_unlock(&tog_mutex);
2716 if (errcode)
2717 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2718 done:
2719 if (err) {
2720 tog_thread_error = 1;
2721 pthread_cond_signal(&a->commit_loaded);
2723 return (void *)err;
2726 static const struct got_error *
2727 stop_log_thread(struct tog_log_view_state *s)
2729 const struct got_error *err = NULL, *thread_err = NULL;
2730 int errcode;
2732 if (s->thread) {
2733 s->quit = 1;
2734 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2735 if (errcode)
2736 return got_error_set_errno(errcode,
2737 "pthread_cond_signal");
2738 errcode = pthread_mutex_unlock(&tog_mutex);
2739 if (errcode)
2740 return got_error_set_errno(errcode,
2741 "pthread_mutex_unlock");
2742 errcode = pthread_join(s->thread, (void **)&thread_err);
2743 if (errcode)
2744 return got_error_set_errno(errcode, "pthread_join");
2745 errcode = pthread_mutex_lock(&tog_mutex);
2746 if (errcode)
2747 return got_error_set_errno(errcode,
2748 "pthread_mutex_lock");
2749 s->thread = 0; //NULL;
2752 if (s->thread_args.repo) {
2753 err = got_repo_close(s->thread_args.repo);
2754 s->thread_args.repo = NULL;
2757 if (s->thread_args.pack_fds) {
2758 const struct got_error *pack_err =
2759 got_repo_pack_fds_close(s->thread_args.pack_fds);
2760 if (err == NULL)
2761 err = pack_err;
2762 s->thread_args.pack_fds = NULL;
2765 if (s->thread_args.graph) {
2766 got_commit_graph_close(s->thread_args.graph);
2767 s->thread_args.graph = NULL;
2770 return err ? err : thread_err;
2773 static const struct got_error *
2774 close_log_view(struct tog_view *view)
2776 const struct got_error *err = NULL;
2777 struct tog_log_view_state *s = &view->state.log;
2778 int errcode;
2780 err = stop_log_thread(s);
2782 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2783 if (errcode && err == NULL)
2784 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2786 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2787 if (errcode && err == NULL)
2788 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2790 free_commits(&s->commits);
2791 free(s->in_repo_path);
2792 s->in_repo_path = NULL;
2793 free(s->start_id);
2794 s->start_id = NULL;
2795 free(s->head_ref_name);
2796 s->head_ref_name = NULL;
2797 return err;
2800 static const struct got_error *
2801 search_start_log_view(struct tog_view *view)
2803 struct tog_log_view_state *s = &view->state.log;
2805 s->matched_entry = NULL;
2806 s->search_entry = NULL;
2807 return NULL;
2810 static const struct got_error *
2811 search_next_log_view(struct tog_view *view)
2813 const struct got_error *err = NULL;
2814 struct tog_log_view_state *s = &view->state.log;
2815 struct commit_queue_entry *entry;
2817 /* Display progress update in log view. */
2818 show_log_view(view);
2819 update_panels();
2820 doupdate();
2822 if (s->search_entry) {
2823 int errcode, ch;
2824 errcode = pthread_mutex_unlock(&tog_mutex);
2825 if (errcode)
2826 return got_error_set_errno(errcode,
2827 "pthread_mutex_unlock");
2828 ch = wgetch(view->window);
2829 errcode = pthread_mutex_lock(&tog_mutex);
2830 if (errcode)
2831 return got_error_set_errno(errcode,
2832 "pthread_mutex_lock");
2833 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2834 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2835 return NULL;
2837 if (view->searching == TOG_SEARCH_FORWARD)
2838 entry = TAILQ_NEXT(s->search_entry, entry);
2839 else
2840 entry = TAILQ_PREV(s->search_entry,
2841 commit_queue_head, entry);
2842 } else if (s->matched_entry) {
2843 int matched_idx = s->matched_entry->idx;
2844 int selected_idx = s->selected_entry->idx;
2847 * If the user has moved the cursor after we hit a match,
2848 * the position from where we should continue searching
2849 * might have changed.
2851 if (view->searching == TOG_SEARCH_FORWARD) {
2852 if (matched_idx > selected_idx)
2853 entry = TAILQ_NEXT(s->selected_entry, entry);
2854 else
2855 entry = TAILQ_NEXT(s->matched_entry, entry);
2856 } else {
2857 if (matched_idx < selected_idx)
2858 entry = TAILQ_PREV(s->selected_entry,
2859 commit_queue_head, entry);
2860 else
2861 entry = TAILQ_PREV(s->matched_entry,
2862 commit_queue_head, entry);
2864 } else {
2865 entry = s->selected_entry;
2868 while (1) {
2869 int have_match = 0;
2871 if (entry == NULL) {
2872 if (s->thread_args.log_complete ||
2873 view->searching == TOG_SEARCH_BACKWARD) {
2874 view->search_next_done =
2875 (s->matched_entry == NULL ?
2876 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2877 s->search_entry = NULL;
2878 return NULL;
2881 * Poke the log thread for more commits and return,
2882 * allowing the main loop to make progress. Search
2883 * will resume at s->search_entry once we come back.
2885 s->thread_args.commits_needed++;
2886 return trigger_log_thread(view, 0);
2889 err = match_commit(&have_match, entry->id, entry->commit,
2890 &view->regex);
2891 if (err)
2892 break;
2893 if (have_match) {
2894 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2895 s->matched_entry = entry;
2896 break;
2899 s->search_entry = entry;
2900 if (view->searching == TOG_SEARCH_FORWARD)
2901 entry = TAILQ_NEXT(entry, entry);
2902 else
2903 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2906 if (s->matched_entry) {
2907 int cur = s->selected_entry->idx;
2908 while (cur < s->matched_entry->idx) {
2909 err = input_log_view(NULL, view, KEY_DOWN);
2910 if (err)
2911 return err;
2912 cur++;
2914 while (cur > s->matched_entry->idx) {
2915 err = input_log_view(NULL, view, KEY_UP);
2916 if (err)
2917 return err;
2918 cur--;
2922 s->search_entry = NULL;
2924 return NULL;
2927 static const struct got_error *
2928 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2929 struct got_repository *repo, const char *head_ref_name,
2930 const char *in_repo_path, int log_branches)
2932 const struct got_error *err = NULL;
2933 struct tog_log_view_state *s = &view->state.log;
2934 struct got_repository *thread_repo = NULL;
2935 struct got_commit_graph *thread_graph = NULL;
2936 int errcode;
2938 if (in_repo_path != s->in_repo_path) {
2939 free(s->in_repo_path);
2940 s->in_repo_path = strdup(in_repo_path);
2941 if (s->in_repo_path == NULL)
2942 return got_error_from_errno("strdup");
2945 /* The commit queue only contains commits being displayed. */
2946 TAILQ_INIT(&s->commits.head);
2947 s->commits.ncommits = 0;
2949 s->repo = repo;
2950 if (head_ref_name) {
2951 s->head_ref_name = strdup(head_ref_name);
2952 if (s->head_ref_name == NULL) {
2953 err = got_error_from_errno("strdup");
2954 goto done;
2957 s->start_id = got_object_id_dup(start_id);
2958 if (s->start_id == NULL) {
2959 err = got_error_from_errno("got_object_id_dup");
2960 goto done;
2962 s->log_branches = log_branches;
2964 STAILQ_INIT(&s->colors);
2965 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2966 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2967 get_color_value("TOG_COLOR_COMMIT"));
2968 if (err)
2969 goto done;
2970 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2971 get_color_value("TOG_COLOR_AUTHOR"));
2972 if (err) {
2973 free_colors(&s->colors);
2974 goto done;
2976 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2977 get_color_value("TOG_COLOR_DATE"));
2978 if (err) {
2979 free_colors(&s->colors);
2980 goto done;
2984 view->show = show_log_view;
2985 view->input = input_log_view;
2986 view->close = close_log_view;
2987 view->search_start = search_start_log_view;
2988 view->search_next = search_next_log_view;
2990 if (s->thread_args.pack_fds == NULL) {
2991 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2992 if (err)
2993 goto done;
2995 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2996 s->thread_args.pack_fds);
2997 if (err)
2998 goto done;
2999 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3000 !s->log_branches);
3001 if (err)
3002 goto done;
3003 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3004 s->repo, NULL, NULL);
3005 if (err)
3006 goto done;
3008 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3009 if (errcode) {
3010 err = got_error_set_errno(errcode, "pthread_cond_init");
3011 goto done;
3013 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3014 if (errcode) {
3015 err = got_error_set_errno(errcode, "pthread_cond_init");
3016 goto done;
3019 s->thread_args.commits_needed = view->nlines;
3020 s->thread_args.graph = thread_graph;
3021 s->thread_args.commits = &s->commits;
3022 s->thread_args.in_repo_path = s->in_repo_path;
3023 s->thread_args.start_id = s->start_id;
3024 s->thread_args.repo = thread_repo;
3025 s->thread_args.log_complete = 0;
3026 s->thread_args.quit = &s->quit;
3027 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3028 s->thread_args.selected_entry = &s->selected_entry;
3029 s->thread_args.searching = &view->searching;
3030 s->thread_args.search_next_done = &view->search_next_done;
3031 s->thread_args.regex = &view->regex;
3032 done:
3033 if (err)
3034 close_log_view(view);
3035 return err;
3038 static const struct got_error *
3039 show_log_view(struct tog_view *view)
3041 const struct got_error *err;
3042 struct tog_log_view_state *s = &view->state.log;
3044 if (s->thread == 0) { //NULL) {
3045 int errcode = pthread_create(&s->thread, NULL, log_thread,
3046 &s->thread_args);
3047 if (errcode)
3048 return got_error_set_errno(errcode, "pthread_create");
3049 if (s->thread_args.commits_needed > 0) {
3050 err = trigger_log_thread(view, 1);
3051 if (err)
3052 return err;
3056 return draw_commits(view);
3059 static void
3060 log_move_cursor_up(struct tog_view *view, int page, int home)
3062 struct tog_log_view_state *s = &view->state.log;
3064 if (s->selected_entry->idx == 0)
3065 view->count = 0;
3066 if (s->first_displayed_entry == NULL)
3067 return;
3069 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3070 || home)
3071 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3073 if (!page && !home && s->selected > 0)
3074 --s->selected;
3075 else
3076 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3078 select_commit(s);
3079 return;
3082 static const struct got_error *
3083 log_move_cursor_down(struct tog_view *view, int page)
3085 struct tog_log_view_state *s = &view->state.log;
3086 struct commit_queue_entry *first;
3087 const struct got_error *err = NULL;
3089 first = s->first_displayed_entry;
3090 if (first == NULL) {
3091 view->count = 0;
3092 return NULL;
3095 if (s->thread_args.log_complete &&
3096 s->selected_entry->idx >= s->commits.ncommits - 1)
3097 return NULL;
3099 if (!page) {
3100 int eos = view->nlines - 2;
3102 if (view_is_hsplit_top(view))
3103 --eos; /* border consumes the last line */
3104 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3105 ++s->selected;
3106 else
3107 err = log_scroll_down(view, 1);
3108 } else if (s->thread_args.load_all) {
3109 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3110 s->selected += MIN(s->last_displayed_entry->idx -
3111 s->selected_entry->idx, page + 1);
3112 else
3113 err = log_scroll_down(view, MIN(page,
3114 s->commits.ncommits - s->selected_entry->idx - 1));
3115 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3116 } else {
3117 err = log_scroll_down(view, page);
3118 if (err)
3119 return err;
3120 if (first == s->first_displayed_entry && s->selected <
3121 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3122 s->selected = MIN(s->commits.ncommits - 1, page);
3125 if (err)
3126 return err;
3129 * We might necessarily overshoot in horizontal
3130 * splits; if so, select the last displayed commit.
3132 s->selected = MIN(s->selected,
3133 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3135 select_commit(s);
3137 if (s->thread_args.log_complete &&
3138 s->selected_entry->idx == s->commits.ncommits - 1)
3139 view->count = 0;
3141 return NULL;
3144 static void
3145 view_get_split(struct tog_view *view, int *y, int *x)
3147 *x = 0;
3148 *y = 0;
3150 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3151 if (view->child && view->child->resized_y)
3152 *y = view->child->resized_y;
3153 else if (view->resized_y)
3154 *y = view->resized_y;
3155 else
3156 *y = view_split_begin_y(view->lines);
3157 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3158 if (view->child && view->child->resized_x)
3159 *x = view->child->resized_x;
3160 else if (view->resized_x)
3161 *x = view->resized_x;
3162 else
3163 *x = view_split_begin_x(view->begin_x);
3167 /* Split view horizontally at y and offset view->state->selected line. */
3168 static const struct got_error *
3169 view_init_hsplit(struct tog_view *view, int y)
3171 const struct got_error *err = NULL;
3173 view->nlines = y;
3174 view->ncols = COLS;
3175 err = view_resize(view);
3176 if (err)
3177 return err;
3179 err = offset_selection_down(view);
3181 return err;
3184 static const struct got_error *
3185 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3187 const struct got_error *err = NULL;
3188 struct tog_log_view_state *s = &view->state.log;
3189 struct tog_view *diff_view = NULL, *tree_view = NULL;
3190 struct tog_view *ref_view = NULL;
3191 struct commit_queue_entry *entry;
3192 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3194 if (s->thread_args.load_all) {
3195 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3196 s->thread_args.load_all = 0;
3197 else if (s->thread_args.log_complete) {
3198 err = log_move_cursor_down(view, s->commits.ncommits);
3199 s->thread_args.load_all = 0;
3201 return err;
3204 eos = nscroll = view->nlines - 1;
3205 if (view_is_hsplit_top(view))
3206 --eos; /* border */
3208 switch (ch) {
3209 case 'q':
3210 s->quit = 1;
3211 break;
3212 case '0':
3213 view->x = 0;
3214 break;
3215 case '$':
3216 view->x = MAX(view->maxx - view->ncols / 2, 0);
3217 view->count = 0;
3218 break;
3219 case KEY_RIGHT:
3220 case 'l':
3221 if (view->x + view->ncols / 2 < view->maxx)
3222 view->x += 2; /* move two columns right */
3223 else
3224 view->count = 0;
3225 break;
3226 case KEY_LEFT:
3227 case 'h':
3228 view->x -= MIN(view->x, 2); /* move two columns back */
3229 if (view->x <= 0)
3230 view->count = 0;
3231 break;
3232 case 'k':
3233 case KEY_UP:
3234 case '<':
3235 case ',':
3236 case CTRL('p'):
3237 log_move_cursor_up(view, 0, 0);
3238 break;
3239 case 'g':
3240 case KEY_HOME:
3241 log_move_cursor_up(view, 0, 1);
3242 view->count = 0;
3243 break;
3244 case CTRL('u'):
3245 case 'u':
3246 nscroll /= 2;
3247 /* FALL THROUGH */
3248 case KEY_PPAGE:
3249 case CTRL('b'):
3250 case 'b':
3251 log_move_cursor_up(view, nscroll, 0);
3252 break;
3253 case 'j':
3254 case KEY_DOWN:
3255 case '>':
3256 case '.':
3257 case CTRL('n'):
3258 err = log_move_cursor_down(view, 0);
3259 break;
3260 case 'G':
3261 case KEY_END: {
3262 /* We don't know yet how many commits, so we're forced to
3263 * traverse them all. */
3264 view->count = 0;
3265 if (!s->thread_args.log_complete) {
3266 s->thread_args.load_all = 1;
3267 return trigger_log_thread(view, 0);
3270 s->selected = 0;
3271 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3272 for (n = 0; n < eos; n++) {
3273 if (entry == NULL)
3274 break;
3275 s->first_displayed_entry = entry;
3276 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3278 if (n > 0)
3279 s->selected = n - 1;
3280 select_commit(s);
3281 break;
3283 case CTRL('d'):
3284 case 'd':
3285 nscroll /= 2;
3286 /* FALL THROUGH */
3287 case KEY_NPAGE:
3288 case CTRL('f'):
3289 case 'f':
3290 case ' ':
3291 err = log_move_cursor_down(view, nscroll);
3292 break;
3293 case KEY_RESIZE:
3294 if (s->selected > view->nlines - 2)
3295 s->selected = view->nlines - 2;
3296 if (s->selected > s->commits.ncommits - 1)
3297 s->selected = s->commits.ncommits - 1;
3298 select_commit(s);
3299 if (s->commits.ncommits < view->nlines - 1 &&
3300 !s->thread_args.log_complete) {
3301 s->thread_args.commits_needed += (view->nlines - 1) -
3302 s->commits.ncommits;
3303 err = trigger_log_thread(view, 1);
3305 break;
3306 case KEY_ENTER:
3307 case '\r':
3308 view->count = 0;
3309 if (s->selected_entry == NULL)
3310 break;
3312 /* get dimensions--don't split till initialisation succeeds */
3313 if (view_is_parent_view(view))
3314 view_get_split(view, &begin_y, &begin_x);
3316 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3317 s->selected_entry->commit, s->selected_entry->id,
3318 view, s->repo);
3319 if (err)
3320 break;
3322 if (view_is_parent_view(view) &&
3323 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3324 err = view_init_hsplit(view, begin_y);
3325 if (err)
3326 break;
3329 view->focussed = 0;
3330 diff_view->focussed = 1;
3331 diff_view->mode = view->mode;
3332 diff_view->nlines = view->lines - begin_y;
3334 if (view_is_parent_view(view)) {
3335 view_transfer_size(diff_view, view);
3336 err = view_close_child(view);
3337 if (err)
3338 return err;
3339 err = view_set_child(view, diff_view);
3340 if (err)
3341 return err;
3342 view->focus_child = 1;
3343 } else
3344 *new_view = diff_view;
3345 break;
3346 case 't':
3347 view->count = 0;
3348 if (s->selected_entry == NULL)
3349 break;
3350 if (view_is_parent_view(view))
3351 view_get_split(view, &begin_y, &begin_x);
3352 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3353 s->selected_entry, s->in_repo_path, s->head_ref_name,
3354 s->repo);
3355 if (err)
3356 break;
3357 if (view_is_parent_view(view) &&
3358 view->mode == TOG_VIEW_SPLIT_HRZN) {
3359 err = view_init_hsplit(view, begin_y);
3360 if (err)
3361 break;
3363 view->focussed = 0;
3364 tree_view->focussed = 1;
3365 tree_view->mode = view->mode;
3366 tree_view->nlines = view->lines - begin_y;
3367 if (view_is_parent_view(view)) {
3368 view_transfer_size(tree_view, view);
3369 err = view_close_child(view);
3370 if (err)
3371 return err;
3372 err = view_set_child(view, tree_view);
3373 if (err)
3374 return err;
3375 view->focus_child = 1;
3376 } else
3377 *new_view = tree_view;
3378 break;
3379 case KEY_BACKSPACE:
3380 case CTRL('l'):
3381 case 'B':
3382 view->count = 0;
3383 if (ch == KEY_BACKSPACE &&
3384 got_path_is_root_dir(s->in_repo_path))
3385 break;
3386 err = stop_log_thread(s);
3387 if (err)
3388 return err;
3389 if (ch == KEY_BACKSPACE) {
3390 char *parent_path;
3391 err = got_path_dirname(&parent_path, s->in_repo_path);
3392 if (err)
3393 return err;
3394 free(s->in_repo_path);
3395 s->in_repo_path = parent_path;
3396 s->thread_args.in_repo_path = s->in_repo_path;
3397 } else if (ch == CTRL('l')) {
3398 struct got_object_id *start_id;
3399 err = got_repo_match_object_id(&start_id, NULL,
3400 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3401 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3402 if (err)
3403 return err;
3404 free(s->start_id);
3405 s->start_id = start_id;
3406 s->thread_args.start_id = s->start_id;
3407 } else /* 'B' */
3408 s->log_branches = !s->log_branches;
3410 if (s->thread_args.pack_fds == NULL) {
3411 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3412 if (err)
3413 return err;
3415 err = got_repo_open(&s->thread_args.repo,
3416 got_repo_get_path(s->repo), NULL,
3417 s->thread_args.pack_fds);
3418 if (err)
3419 return err;
3420 tog_free_refs();
3421 err = tog_load_refs(s->repo, 0);
3422 if (err)
3423 return err;
3424 err = got_commit_graph_open(&s->thread_args.graph,
3425 s->in_repo_path, !s->log_branches);
3426 if (err)
3427 return err;
3428 err = got_commit_graph_iter_start(s->thread_args.graph,
3429 s->start_id, s->repo, NULL, NULL);
3430 if (err)
3431 return err;
3432 free_commits(&s->commits);
3433 s->first_displayed_entry = NULL;
3434 s->last_displayed_entry = NULL;
3435 s->selected_entry = NULL;
3436 s->selected = 0;
3437 s->thread_args.log_complete = 0;
3438 s->quit = 0;
3439 s->thread_args.commits_needed = view->lines;
3440 s->matched_entry = NULL;
3441 s->search_entry = NULL;
3442 break;
3443 case 'r':
3444 view->count = 0;
3445 if (view_is_parent_view(view))
3446 view_get_split(view, &begin_y, &begin_x);
3447 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3448 if (ref_view == NULL)
3449 return got_error_from_errno("view_open");
3450 err = open_ref_view(ref_view, s->repo);
3451 if (err) {
3452 view_close(ref_view);
3453 return err;
3455 if (view_is_parent_view(view) &&
3456 view->mode == TOG_VIEW_SPLIT_HRZN) {
3457 err = view_init_hsplit(view, begin_y);
3458 if (err)
3459 break;
3461 view->focussed = 0;
3462 ref_view->focussed = 1;
3463 ref_view->mode = view->mode;
3464 ref_view->nlines = view->lines - begin_y;
3465 if (view_is_parent_view(view)) {
3466 view_transfer_size(ref_view, view);
3467 err = view_close_child(view);
3468 if (err)
3469 return err;
3470 err = view_set_child(view, ref_view);
3471 if (err)
3472 return err;
3473 view->focus_child = 1;
3474 } else
3475 *new_view = ref_view;
3476 break;
3477 default:
3478 view->count = 0;
3479 break;
3482 return err;
3485 static const struct got_error *
3486 apply_unveil(const char *repo_path, const char *worktree_path)
3488 const struct got_error *error;
3490 #ifdef PROFILE
3491 if (unveil("gmon.out", "rwc") != 0)
3492 return got_error_from_errno2("unveil", "gmon.out");
3493 #endif
3494 if (repo_path && unveil(repo_path, "r") != 0)
3495 return got_error_from_errno2("unveil", repo_path);
3497 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3498 return got_error_from_errno2("unveil", worktree_path);
3500 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3501 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3503 error = got_privsep_unveil_exec_helpers();
3504 if (error != NULL)
3505 return error;
3507 if (unveil(NULL, NULL) != 0)
3508 return got_error_from_errno("unveil");
3510 return NULL;
3513 static void
3514 init_curses(void)
3517 * Override default signal handlers before starting ncurses.
3518 * This should prevent ncurses from installing its own
3519 * broken cleanup() signal handler.
3521 signal(SIGWINCH, tog_sigwinch);
3522 signal(SIGPIPE, tog_sigpipe);
3523 signal(SIGCONT, tog_sigcont);
3524 signal(SIGINT, tog_sigint);
3525 signal(SIGTERM, tog_sigterm);
3527 initscr();
3528 cbreak();
3529 halfdelay(1); /* Do fast refresh while initial view is loading. */
3530 noecho();
3531 nonl();
3532 intrflush(stdscr, FALSE);
3533 keypad(stdscr, TRUE);
3534 curs_set(0);
3535 if (getenv("TOG_COLORS") != NULL) {
3536 start_color();
3537 use_default_colors();
3541 static const struct got_error *
3542 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3543 struct got_repository *repo, struct got_worktree *worktree)
3545 const struct got_error *err = NULL;
3547 if (argc == 0) {
3548 *in_repo_path = strdup("/");
3549 if (*in_repo_path == NULL)
3550 return got_error_from_errno("strdup");
3551 return NULL;
3554 if (worktree) {
3555 const char *prefix = got_worktree_get_path_prefix(worktree);
3556 char *p;
3558 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3559 if (err)
3560 return err;
3561 if (asprintf(in_repo_path, "%s%s%s", prefix,
3562 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3563 p) == -1) {
3564 err = got_error_from_errno("asprintf");
3565 *in_repo_path = NULL;
3567 free(p);
3568 } else
3569 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3571 return err;
3574 static const struct got_error *
3575 cmd_log(int argc, char *argv[])
3577 const struct got_error *error;
3578 struct got_repository *repo = NULL;
3579 struct got_worktree *worktree = NULL;
3580 struct got_object_id *start_id = NULL;
3581 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3582 char *start_commit = NULL, *label = NULL;
3583 struct got_reference *ref = NULL;
3584 const char *head_ref_name = NULL;
3585 int ch, log_branches = 0;
3586 struct tog_view *view;
3587 int *pack_fds = NULL;
3589 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3590 switch (ch) {
3591 case 'b':
3592 log_branches = 1;
3593 break;
3594 case 'c':
3595 start_commit = optarg;
3596 break;
3597 case 'r':
3598 repo_path = realpath(optarg, NULL);
3599 if (repo_path == NULL)
3600 return got_error_from_errno2("realpath",
3601 optarg);
3602 break;
3603 default:
3604 usage_log();
3605 /* NOTREACHED */
3609 argc -= optind;
3610 argv += optind;
3612 if (argc > 1)
3613 usage_log();
3615 error = got_repo_pack_fds_open(&pack_fds);
3616 if (error != NULL)
3617 goto done;
3619 if (repo_path == NULL) {
3620 cwd = getcwd(NULL, 0);
3621 if (cwd == NULL)
3622 return got_error_from_errno("getcwd");
3623 error = got_worktree_open(&worktree, cwd);
3624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3625 goto done;
3626 if (worktree)
3627 repo_path =
3628 strdup(got_worktree_get_repo_path(worktree));
3629 else
3630 repo_path = strdup(cwd);
3631 if (repo_path == NULL) {
3632 error = got_error_from_errno("strdup");
3633 goto done;
3637 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3638 if (error != NULL)
3639 goto done;
3641 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3642 repo, worktree);
3643 if (error)
3644 goto done;
3646 init_curses();
3648 error = apply_unveil(got_repo_get_path(repo),
3649 worktree ? got_worktree_get_root_path(worktree) : NULL);
3650 if (error)
3651 goto done;
3653 /* already loaded by tog_log_with_path()? */
3654 if (TAILQ_EMPTY(&tog_refs)) {
3655 error = tog_load_refs(repo, 0);
3656 if (error)
3657 goto done;
3660 if (start_commit == NULL) {
3661 error = got_repo_match_object_id(&start_id, &label,
3662 worktree ? got_worktree_get_head_ref_name(worktree) :
3663 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3664 if (error)
3665 goto done;
3666 head_ref_name = label;
3667 } else {
3668 error = got_ref_open(&ref, repo, start_commit, 0);
3669 if (error == NULL)
3670 head_ref_name = got_ref_get_name(ref);
3671 else if (error->code != GOT_ERR_NOT_REF)
3672 goto done;
3673 error = got_repo_match_object_id(&start_id, NULL,
3674 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3675 if (error)
3676 goto done;
3679 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3680 if (view == NULL) {
3681 error = got_error_from_errno("view_open");
3682 goto done;
3684 error = open_log_view(view, start_id, repo, head_ref_name,
3685 in_repo_path, log_branches);
3686 if (error)
3687 goto done;
3688 if (worktree) {
3689 /* Release work tree lock. */
3690 got_worktree_close(worktree);
3691 worktree = NULL;
3693 error = view_loop(view);
3694 done:
3695 free(in_repo_path);
3696 free(repo_path);
3697 free(cwd);
3698 free(start_id);
3699 free(label);
3700 if (ref)
3701 got_ref_close(ref);
3702 if (repo) {
3703 const struct got_error *close_err = got_repo_close(repo);
3704 if (error == NULL)
3705 error = close_err;
3707 if (worktree)
3708 got_worktree_close(worktree);
3709 if (pack_fds) {
3710 const struct got_error *pack_err =
3711 got_repo_pack_fds_close(pack_fds);
3712 if (error == NULL)
3713 error = pack_err;
3715 tog_free_refs();
3716 return error;
3719 __dead static void
3720 usage_diff(void)
3722 endwin();
3723 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3724 "[-w] object1 object2\n", getprogname());
3725 exit(1);
3728 static int
3729 match_line(const char *line, regex_t *regex, size_t nmatch,
3730 regmatch_t *regmatch)
3732 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3735 static struct tog_color *
3736 match_color(struct tog_colors *colors, const char *line)
3738 struct tog_color *tc = NULL;
3740 STAILQ_FOREACH(tc, colors, entry) {
3741 if (match_line(line, &tc->regex, 0, NULL))
3742 return tc;
3745 return NULL;
3748 static const struct got_error *
3749 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3750 WINDOW *window, int skipcol, regmatch_t *regmatch)
3752 const struct got_error *err = NULL;
3753 char *exstr = NULL;
3754 wchar_t *wline = NULL;
3755 int rme, rms, n, width, scrollx;
3756 int width0 = 0, width1 = 0, width2 = 0;
3757 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3759 *wtotal = 0;
3761 rms = regmatch->rm_so;
3762 rme = regmatch->rm_eo;
3764 err = expand_tab(&exstr, line);
3765 if (err)
3766 return err;
3768 /* Split the line into 3 segments, according to match offsets. */
3769 seg0 = strndup(exstr, rms);
3770 if (seg0 == NULL) {
3771 err = got_error_from_errno("strndup");
3772 goto done;
3774 seg1 = strndup(exstr + rms, rme - rms);
3775 if (seg1 == NULL) {
3776 err = got_error_from_errno("strndup");
3777 goto done;
3779 seg2 = strdup(exstr + rme);
3780 if (seg2 == NULL) {
3781 err = got_error_from_errno("strndup");
3782 goto done;
3785 /* draw up to matched token if we haven't scrolled past it */
3786 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3787 col_tab_align, 1);
3788 if (err)
3789 goto done;
3790 n = MAX(width0 - skipcol, 0);
3791 if (n) {
3792 free(wline);
3793 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3794 wlimit, col_tab_align, 1);
3795 if (err)
3796 goto done;
3797 waddwstr(window, &wline[scrollx]);
3798 wlimit -= width;
3799 *wtotal += width;
3802 if (wlimit > 0) {
3803 int i = 0, w = 0;
3804 size_t wlen;
3806 free(wline);
3807 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3808 col_tab_align, 1);
3809 if (err)
3810 goto done;
3811 wlen = wcslen(wline);
3812 while (i < wlen) {
3813 width = wcwidth(wline[i]);
3814 if (width == -1) {
3815 /* should not happen, tabs are expanded */
3816 err = got_error(GOT_ERR_RANGE);
3817 goto done;
3819 if (width0 + w + width > skipcol)
3820 break;
3821 w += width;
3822 i++;
3824 /* draw (visible part of) matched token (if scrolled into it) */
3825 if (width1 - w > 0) {
3826 wattron(window, A_STANDOUT);
3827 waddwstr(window, &wline[i]);
3828 wattroff(window, A_STANDOUT);
3829 wlimit -= (width1 - w);
3830 *wtotal += (width1 - w);
3834 if (wlimit > 0) { /* draw rest of line */
3835 free(wline);
3836 if (skipcol > width0 + width1) {
3837 err = format_line(&wline, &width2, &scrollx, seg2,
3838 skipcol - (width0 + width1), wlimit,
3839 col_tab_align, 1);
3840 if (err)
3841 goto done;
3842 waddwstr(window, &wline[scrollx]);
3843 } else {
3844 err = format_line(&wline, &width2, NULL, seg2, 0,
3845 wlimit, col_tab_align, 1);
3846 if (err)
3847 goto done;
3848 waddwstr(window, wline);
3850 *wtotal += width2;
3852 done:
3853 free(wline);
3854 free(exstr);
3855 free(seg0);
3856 free(seg1);
3857 free(seg2);
3858 return err;
3861 static const struct got_error *
3862 draw_file(struct tog_view *view, const char *header)
3864 struct tog_diff_view_state *s = &view->state.diff;
3865 regmatch_t *regmatch = &view->regmatch;
3866 const struct got_error *err;
3867 int nprinted = 0;
3868 char *line;
3869 size_t linesize = 0;
3870 ssize_t linelen;
3871 struct tog_color *tc;
3872 wchar_t *wline;
3873 int width;
3874 int max_lines = view->nlines;
3875 int nlines = s->nlines;
3876 off_t line_offset;
3878 line_offset = s->line_offsets[s->first_displayed_line - 1];
3879 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3880 return got_error_from_errno("fseek");
3882 werase(view->window);
3884 if (header) {
3885 if (asprintf(&line, "[%d/%d] %s",
3886 s->first_displayed_line - 1 + s->selected_line, nlines,
3887 header) == -1)
3888 return got_error_from_errno("asprintf");
3889 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3890 0, 0);
3891 free(line);
3892 if (err)
3893 return err;
3895 if (view_needs_focus_indication(view))
3896 wstandout(view->window);
3897 waddwstr(view->window, wline);
3898 free(wline);
3899 wline = NULL;
3900 if (view_needs_focus_indication(view))
3901 wstandend(view->window);
3902 if (width <= view->ncols - 1)
3903 waddch(view->window, '\n');
3905 if (max_lines <= 1)
3906 return NULL;
3907 max_lines--;
3910 s->eof = 0;
3911 view->maxx = 0;
3912 line = NULL;
3913 while (max_lines > 0 && nprinted < max_lines) {
3914 linelen = getline(&line, &linesize, s->f);
3915 if (linelen == -1) {
3916 if (feof(s->f)) {
3917 s->eof = 1;
3918 break;
3920 free(line);
3921 return got_ferror(s->f, GOT_ERR_IO);
3924 /* Set view->maxx based on full line length. */
3925 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3926 view->x ? 1 : 0);
3927 if (err) {
3928 free(line);
3929 return err;
3931 view->maxx = MAX(view->maxx, width);
3932 free(wline);
3933 wline = NULL;
3935 tc = match_color(&s->colors, line);
3936 if (tc)
3937 wattr_on(view->window,
3938 COLOR_PAIR(tc->colorpair), NULL);
3939 if (s->first_displayed_line + nprinted == s->matched_line &&
3940 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3941 err = add_matched_line(&width, line, view->ncols, 0,
3942 view->window, view->x, regmatch);
3943 if (err) {
3944 free(line);
3945 return err;
3947 } else {
3948 int skip;
3949 err = format_line(&wline, &width, &skip, line,
3950 view->x, view->ncols, 0, view->x ? 1 : 0);
3951 if (err) {
3952 free(line);
3953 return err;
3955 waddwstr(view->window, &wline[skip]);
3956 free(wline);
3957 wline = NULL;
3959 if (tc)
3960 wattr_off(view->window,
3961 COLOR_PAIR(tc->colorpair), NULL);
3962 if (width <= view->ncols - 1)
3963 waddch(view->window, '\n');
3964 nprinted++;
3966 free(line);
3967 if (nprinted >= 1)
3968 s->last_displayed_line = s->first_displayed_line +
3969 (nprinted - 1);
3970 else
3971 s->last_displayed_line = s->first_displayed_line;
3973 view_border(view);
3975 if (s->eof) {
3976 while (nprinted < view->nlines) {
3977 waddch(view->window, '\n');
3978 nprinted++;
3981 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3982 view->ncols, 0, 0);
3983 if (err) {
3984 return err;
3987 wstandout(view->window);
3988 waddwstr(view->window, wline);
3989 free(wline);
3990 wline = NULL;
3991 wstandend(view->window);
3994 return NULL;
3997 static char *
3998 get_datestr(time_t *time, char *datebuf)
4000 struct tm mytm, *tm;
4001 char *p, *s;
4003 tm = gmtime_r(time, &mytm);
4004 if (tm == NULL)
4005 return NULL;
4006 s = asctime_r(tm, datebuf);
4007 if (s == NULL)
4008 return NULL;
4009 p = strchr(s, '\n');
4010 if (p)
4011 *p = '\0';
4012 return s;
4015 static const struct got_error *
4016 get_changed_paths(struct got_pathlist_head *paths,
4017 struct got_commit_object *commit, struct got_repository *repo)
4019 const struct got_error *err = NULL;
4020 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4021 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4022 struct got_object_qid *qid;
4024 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4025 if (qid != NULL) {
4026 struct got_commit_object *pcommit;
4027 err = got_object_open_as_commit(&pcommit, repo,
4028 &qid->id);
4029 if (err)
4030 return err;
4032 tree_id1 = got_object_id_dup(
4033 got_object_commit_get_tree_id(pcommit));
4034 if (tree_id1 == NULL) {
4035 got_object_commit_close(pcommit);
4036 return got_error_from_errno("got_object_id_dup");
4038 got_object_commit_close(pcommit);
4042 if (tree_id1) {
4043 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4044 if (err)
4045 goto done;
4048 tree_id2 = got_object_commit_get_tree_id(commit);
4049 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4050 if (err)
4051 goto done;
4053 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4054 got_diff_tree_collect_changed_paths, paths, 0);
4055 done:
4056 if (tree1)
4057 got_object_tree_close(tree1);
4058 if (tree2)
4059 got_object_tree_close(tree2);
4060 free(tree_id1);
4061 return err;
4064 static const struct got_error *
4065 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4067 off_t *p;
4069 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4070 if (p == NULL)
4071 return got_error_from_errno("reallocarray");
4072 *line_offsets = p;
4073 (*line_offsets)[*nlines] = off;
4074 (*nlines)++;
4075 return NULL;
4078 static const struct got_error *
4079 write_commit_info(off_t **line_offsets, size_t *nlines,
4080 struct got_object_id *commit_id, struct got_reflist_head *refs,
4081 struct got_repository *repo, FILE *outfile)
4083 const struct got_error *err = NULL;
4084 char datebuf[26], *datestr;
4085 struct got_commit_object *commit;
4086 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4087 time_t committer_time;
4088 const char *author, *committer;
4089 char *refs_str = NULL;
4090 struct got_pathlist_head changed_paths;
4091 struct got_pathlist_entry *pe;
4092 off_t outoff = 0;
4093 int n;
4095 TAILQ_INIT(&changed_paths);
4097 if (refs) {
4098 err = build_refs_str(&refs_str, refs, commit_id, repo);
4099 if (err)
4100 return err;
4103 err = got_object_open_as_commit(&commit, repo, commit_id);
4104 if (err)
4105 return err;
4107 err = got_object_id_str(&id_str, commit_id);
4108 if (err) {
4109 err = got_error_from_errno("got_object_id_str");
4110 goto done;
4113 err = add_line_offset(line_offsets, nlines, 0);
4114 if (err)
4115 goto done;
4117 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4118 refs_str ? refs_str : "", refs_str ? ")" : "");
4119 if (n < 0) {
4120 err = got_error_from_errno("fprintf");
4121 goto done;
4123 outoff += n;
4124 err = add_line_offset(line_offsets, nlines, outoff);
4125 if (err)
4126 goto done;
4128 n = fprintf(outfile, "from: %s\n",
4129 got_object_commit_get_author(commit));
4130 if (n < 0) {
4131 err = got_error_from_errno("fprintf");
4132 goto done;
4134 outoff += n;
4135 err = add_line_offset(line_offsets, nlines, outoff);
4136 if (err)
4137 goto done;
4139 committer_time = got_object_commit_get_committer_time(commit);
4140 datestr = get_datestr(&committer_time, datebuf);
4141 if (datestr) {
4142 n = fprintf(outfile, "date: %s UTC\n", datestr);
4143 if (n < 0) {
4144 err = got_error_from_errno("fprintf");
4145 goto done;
4147 outoff += n;
4148 err = add_line_offset(line_offsets, nlines, outoff);
4149 if (err)
4150 goto done;
4152 author = got_object_commit_get_author(commit);
4153 committer = got_object_commit_get_committer(commit);
4154 if (strcmp(author, committer) != 0) {
4155 n = fprintf(outfile, "via: %s\n", committer);
4156 if (n < 0) {
4157 err = got_error_from_errno("fprintf");
4158 goto done;
4160 outoff += n;
4161 err = add_line_offset(line_offsets, nlines, outoff);
4162 if (err)
4163 goto done;
4165 if (got_object_commit_get_nparents(commit) > 1) {
4166 const struct got_object_id_queue *parent_ids;
4167 struct got_object_qid *qid;
4168 int pn = 1;
4169 parent_ids = got_object_commit_get_parent_ids(commit);
4170 STAILQ_FOREACH(qid, parent_ids, entry) {
4171 err = got_object_id_str(&id_str, &qid->id);
4172 if (err)
4173 goto done;
4174 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4175 if (n < 0) {
4176 err = got_error_from_errno("fprintf");
4177 goto done;
4179 outoff += n;
4180 err = add_line_offset(line_offsets, nlines, outoff);
4181 if (err)
4182 goto done;
4183 free(id_str);
4184 id_str = NULL;
4188 err = got_object_commit_get_logmsg(&logmsg, commit);
4189 if (err)
4190 goto done;
4191 s = logmsg;
4192 while ((line = strsep(&s, "\n")) != NULL) {
4193 n = fprintf(outfile, "%s\n", line);
4194 if (n < 0) {
4195 err = got_error_from_errno("fprintf");
4196 goto done;
4198 outoff += n;
4199 err = add_line_offset(line_offsets, nlines, outoff);
4200 if (err)
4201 goto done;
4204 err = get_changed_paths(&changed_paths, commit, repo);
4205 if (err)
4206 goto done;
4207 TAILQ_FOREACH(pe, &changed_paths, entry) {
4208 struct got_diff_changed_path *cp = pe->data;
4209 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4210 if (n < 0) {
4211 err = got_error_from_errno("fprintf");
4212 goto done;
4214 outoff += n;
4215 err = add_line_offset(line_offsets, nlines, outoff);
4216 if (err)
4217 goto done;
4218 free((char *)pe->path);
4219 free(pe->data);
4222 fputc('\n', outfile);
4223 outoff++;
4224 err = add_line_offset(line_offsets, nlines, outoff);
4225 done:
4226 got_pathlist_free(&changed_paths);
4227 free(id_str);
4228 free(logmsg);
4229 free(refs_str);
4230 got_object_commit_close(commit);
4231 if (err) {
4232 free(*line_offsets);
4233 *line_offsets = NULL;
4234 *nlines = 0;
4236 return err;
4239 static const struct got_error *
4240 create_diff(struct tog_diff_view_state *s)
4242 const struct got_error *err = NULL;
4243 FILE *f = NULL;
4244 int obj_type;
4246 free(s->line_offsets);
4247 s->line_offsets = malloc(sizeof(off_t));
4248 if (s->line_offsets == NULL)
4249 return got_error_from_errno("malloc");
4250 s->nlines = 0;
4252 f = got_opentemp();
4253 if (f == NULL) {
4254 err = got_error_from_errno("got_opentemp");
4255 goto done;
4257 if (s->f && fclose(s->f) == EOF) {
4258 err = got_error_from_errno("fclose");
4259 goto done;
4261 s->f = f;
4263 if (s->id1)
4264 err = got_object_get_type(&obj_type, s->repo, s->id1);
4265 else
4266 err = got_object_get_type(&obj_type, s->repo, s->id2);
4267 if (err)
4268 goto done;
4270 switch (obj_type) {
4271 case GOT_OBJ_TYPE_BLOB:
4272 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4273 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4274 s->label1, s->label2, tog_diff_algo, s->diff_context,
4275 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4276 break;
4277 case GOT_OBJ_TYPE_TREE:
4278 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4279 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4280 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4281 s->force_text_diff, s->repo, s->f);
4282 break;
4283 case GOT_OBJ_TYPE_COMMIT: {
4284 const struct got_object_id_queue *parent_ids;
4285 struct got_object_qid *pid;
4286 struct got_commit_object *commit2;
4287 struct got_reflist_head *refs;
4289 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4290 if (err)
4291 goto done;
4292 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4293 /* Show commit info if we're diffing to a parent/root commit. */
4294 if (s->id1 == NULL) {
4295 err = write_commit_info(&s->line_offsets, &s->nlines,
4296 s->id2, refs, s->repo, s->f);
4297 if (err)
4298 goto done;
4299 } else {
4300 parent_ids = got_object_commit_get_parent_ids(commit2);
4301 STAILQ_FOREACH(pid, parent_ids, entry) {
4302 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4303 err = write_commit_info(
4304 &s->line_offsets, &s->nlines,
4305 s->id2, refs, s->repo, s->f);
4306 if (err)
4307 goto done;
4308 break;
4312 got_object_commit_close(commit2);
4314 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4315 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4316 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4317 s->force_text_diff, s->repo, s->f);
4318 break;
4320 default:
4321 err = got_error(GOT_ERR_OBJ_TYPE);
4322 break;
4324 if (err)
4325 goto done;
4326 done:
4327 if (s->f && fflush(s->f) != 0 && err == NULL)
4328 err = got_error_from_errno("fflush");
4329 return err;
4332 static void
4333 diff_view_indicate_progress(struct tog_view *view)
4335 mvwaddstr(view->window, 0, 0, "diffing...");
4336 update_panels();
4337 doupdate();
4340 static const struct got_error *
4341 search_start_diff_view(struct tog_view *view)
4343 struct tog_diff_view_state *s = &view->state.diff;
4345 s->matched_line = 0;
4346 return NULL;
4349 static const struct got_error *
4350 search_next_diff_view(struct tog_view *view)
4352 struct tog_diff_view_state *s = &view->state.diff;
4353 const struct got_error *err = NULL;
4354 int lineno;
4355 char *line = NULL;
4356 size_t linesize = 0;
4357 ssize_t linelen;
4359 if (!view->searching) {
4360 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4361 return NULL;
4364 if (s->matched_line) {
4365 if (view->searching == TOG_SEARCH_FORWARD)
4366 lineno = s->matched_line + 1;
4367 else
4368 lineno = s->matched_line - 1;
4369 } else
4370 lineno = s->first_displayed_line;
4372 while (1) {
4373 off_t offset;
4375 if (lineno <= 0 || lineno > s->nlines) {
4376 if (s->matched_line == 0) {
4377 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4378 break;
4381 if (view->searching == TOG_SEARCH_FORWARD)
4382 lineno = 1;
4383 else
4384 lineno = s->nlines;
4387 offset = s->line_offsets[lineno - 1];
4388 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4389 free(line);
4390 return got_error_from_errno("fseeko");
4392 linelen = getline(&line, &linesize, s->f);
4393 if (linelen != -1) {
4394 char *exstr;
4395 err = expand_tab(&exstr, line);
4396 if (err)
4397 break;
4398 if (match_line(exstr, &view->regex, 1,
4399 &view->regmatch)) {
4400 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 s->matched_line = lineno;
4402 free(exstr);
4403 break;
4405 free(exstr);
4407 if (view->searching == TOG_SEARCH_FORWARD)
4408 lineno++;
4409 else
4410 lineno--;
4412 free(line);
4414 if (s->matched_line) {
4415 s->first_displayed_line = s->matched_line;
4416 s->selected_line = 1;
4419 return err;
4422 static const struct got_error *
4423 close_diff_view(struct tog_view *view)
4425 const struct got_error *err = NULL;
4426 struct tog_diff_view_state *s = &view->state.diff;
4428 free(s->id1);
4429 s->id1 = NULL;
4430 free(s->id2);
4431 s->id2 = NULL;
4432 if (s->f && fclose(s->f) == EOF)
4433 err = got_error_from_errno("fclose");
4434 s->f = NULL;
4435 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4436 err = got_error_from_errno("fclose");
4437 s->f1 = NULL;
4438 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4439 err = got_error_from_errno("fclose");
4440 s->f2 = NULL;
4441 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4442 err = got_error_from_errno("close");
4443 s->fd1 = -1;
4444 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4445 err = got_error_from_errno("close");
4446 s->fd2 = -1;
4447 free_colors(&s->colors);
4448 free(s->line_offsets);
4449 s->line_offsets = NULL;
4450 s->nlines = 0;
4451 return err;
4454 static const struct got_error *
4455 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4456 struct got_object_id *id2, const char *label1, const char *label2,
4457 int diff_context, int ignore_whitespace, int force_text_diff,
4458 struct tog_view *parent_view, struct got_repository *repo)
4460 const struct got_error *err;
4461 struct tog_diff_view_state *s = &view->state.diff;
4463 memset(s, 0, sizeof(*s));
4464 s->fd1 = -1;
4465 s->fd2 = -1;
4467 if (id1 != NULL && id2 != NULL) {
4468 int type1, type2;
4469 err = got_object_get_type(&type1, repo, id1);
4470 if (err)
4471 return err;
4472 err = got_object_get_type(&type2, repo, id2);
4473 if (err)
4474 return err;
4476 if (type1 != type2)
4477 return got_error(GOT_ERR_OBJ_TYPE);
4479 s->first_displayed_line = 1;
4480 s->last_displayed_line = view->nlines;
4481 s->selected_line = 1;
4482 s->repo = repo;
4483 s->id1 = id1;
4484 s->id2 = id2;
4485 s->label1 = label1;
4486 s->label2 = label2;
4488 if (id1) {
4489 s->id1 = got_object_id_dup(id1);
4490 if (s->id1 == NULL)
4491 return got_error_from_errno("got_object_id_dup");
4492 } else
4493 s->id1 = NULL;
4495 s->id2 = got_object_id_dup(id2);
4496 if (s->id2 == NULL) {
4497 err = got_error_from_errno("got_object_id_dup");
4498 goto done;
4501 s->f1 = got_opentemp();
4502 if (s->f1 == NULL) {
4503 err = got_error_from_errno("got_opentemp");
4504 goto done;
4507 s->f2 = got_opentemp();
4508 if (s->f2 == NULL) {
4509 err = got_error_from_errno("got_opentemp");
4510 goto done;
4513 s->fd1 = got_opentempfd();
4514 if (s->fd1 == -1) {
4515 err = got_error_from_errno("got_opentempfd");
4516 goto done;
4519 s->fd2 = got_opentempfd();
4520 if (s->fd2 == -1) {
4521 err = got_error_from_errno("got_opentempfd");
4522 goto done;
4525 s->first_displayed_line = 1;
4526 s->last_displayed_line = view->nlines;
4527 s->diff_context = diff_context;
4528 s->ignore_whitespace = ignore_whitespace;
4529 s->force_text_diff = force_text_diff;
4530 s->parent_view = parent_view;
4531 s->repo = repo;
4533 STAILQ_INIT(&s->colors);
4534 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4535 err = add_color(&s->colors,
4536 "^-", TOG_COLOR_DIFF_MINUS,
4537 get_color_value("TOG_COLOR_DIFF_MINUS"));
4538 if (err)
4539 goto done;
4540 err = add_color(&s->colors, "^\\+",
4541 TOG_COLOR_DIFF_PLUS,
4542 get_color_value("TOG_COLOR_DIFF_PLUS"));
4543 if (err)
4544 goto done;
4545 err = add_color(&s->colors,
4546 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4547 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4548 if (err)
4549 goto done;
4551 err = add_color(&s->colors,
4552 "^(commit [0-9a-f]|parent [0-9]|"
4553 "(blob|file|tree|commit) [-+] |"
4554 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4555 get_color_value("TOG_COLOR_DIFF_META"));
4556 if (err)
4557 goto done;
4559 err = add_color(&s->colors,
4560 "^(from|via): ", TOG_COLOR_AUTHOR,
4561 get_color_value("TOG_COLOR_AUTHOR"));
4562 if (err)
4563 goto done;
4565 err = add_color(&s->colors,
4566 "^date: ", TOG_COLOR_DATE,
4567 get_color_value("TOG_COLOR_DATE"));
4568 if (err)
4569 goto done;
4572 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4573 view_is_splitscreen(view))
4574 show_log_view(parent_view); /* draw border */
4575 diff_view_indicate_progress(view);
4577 err = create_diff(s);
4579 view->show = show_diff_view;
4580 view->input = input_diff_view;
4581 view->reset = reset_diff_view;
4582 view->close = close_diff_view;
4583 view->search_start = search_start_diff_view;
4584 view->search_next = search_next_diff_view;
4585 done:
4586 if (err)
4587 close_diff_view(view);
4588 return err;
4591 static const struct got_error *
4592 show_diff_view(struct tog_view *view)
4594 const struct got_error *err;
4595 struct tog_diff_view_state *s = &view->state.diff;
4596 char *id_str1 = NULL, *id_str2, *header;
4597 const char *label1, *label2;
4599 if (s->id1) {
4600 err = got_object_id_str(&id_str1, s->id1);
4601 if (err)
4602 return err;
4603 label1 = s->label1 ? : id_str1;
4604 } else
4605 label1 = "/dev/null";
4607 err = got_object_id_str(&id_str2, s->id2);
4608 if (err)
4609 return err;
4610 label2 = s->label2 ? : id_str2;
4612 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4613 err = got_error_from_errno("asprintf");
4614 free(id_str1);
4615 free(id_str2);
4616 return err;
4618 free(id_str1);
4619 free(id_str2);
4621 err = draw_file(view, header);
4622 free(header);
4623 return err;
4626 static const struct got_error *
4627 set_selected_commit(struct tog_diff_view_state *s,
4628 struct commit_queue_entry *entry)
4630 const struct got_error *err;
4631 const struct got_object_id_queue *parent_ids;
4632 struct got_commit_object *selected_commit;
4633 struct got_object_qid *pid;
4635 free(s->id2);
4636 s->id2 = got_object_id_dup(entry->id);
4637 if (s->id2 == NULL)
4638 return got_error_from_errno("got_object_id_dup");
4640 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4641 if (err)
4642 return err;
4643 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4644 free(s->id1);
4645 pid = STAILQ_FIRST(parent_ids);
4646 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4647 got_object_commit_close(selected_commit);
4648 return NULL;
4651 static const struct got_error *
4652 reset_diff_view(struct tog_view *view)
4654 struct tog_diff_view_state *s = &view->state.diff;
4656 view->count = 0;
4657 wclear(view->window);
4658 s->first_displayed_line = 1;
4659 s->last_displayed_line = view->nlines;
4660 s->matched_line = 0;
4661 diff_view_indicate_progress(view);
4662 return create_diff(s);
4665 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4666 int, int, int);
4667 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4668 int, int);
4670 static const struct got_error *
4671 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4673 const struct got_error *err = NULL;
4674 struct tog_diff_view_state *s = &view->state.diff;
4675 struct tog_log_view_state *ls;
4676 struct commit_queue_entry *old_selected_entry;
4677 char *line = NULL;
4678 size_t linesize = 0;
4679 ssize_t linelen;
4680 int i, nscroll = view->nlines - 1, up = 0;
4682 switch (ch) {
4683 case '0':
4684 view->x = 0;
4685 break;
4686 case '$':
4687 view->x = MAX(view->maxx - view->ncols / 3, 0);
4688 view->count = 0;
4689 break;
4690 case KEY_RIGHT:
4691 case 'l':
4692 if (view->x + view->ncols / 3 < view->maxx)
4693 view->x += 2; /* move two columns right */
4694 else
4695 view->count = 0;
4696 break;
4697 case KEY_LEFT:
4698 case 'h':
4699 view->x -= MIN(view->x, 2); /* move two columns back */
4700 if (view->x <= 0)
4701 view->count = 0;
4702 break;
4703 case 'a':
4704 case 'w':
4705 if (ch == 'a')
4706 s->force_text_diff = !s->force_text_diff;
4707 if (ch == 'w')
4708 s->ignore_whitespace = !s->ignore_whitespace;
4709 err = reset_diff_view(view);
4710 break;
4711 case 'g':
4712 case KEY_HOME:
4713 s->first_displayed_line = 1;
4714 view->count = 0;
4715 break;
4716 case 'G':
4717 case KEY_END:
4718 view->count = 0;
4719 if (s->eof)
4720 break;
4722 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4723 s->eof = 1;
4724 break;
4725 case 'k':
4726 case KEY_UP:
4727 case CTRL('p'):
4728 if (s->first_displayed_line > 1)
4729 s->first_displayed_line--;
4730 else
4731 view->count = 0;
4732 break;
4733 case CTRL('u'):
4734 case 'u':
4735 nscroll /= 2;
4736 /* FALL THROUGH */
4737 case KEY_PPAGE:
4738 case CTRL('b'):
4739 case 'b':
4740 if (s->first_displayed_line == 1) {
4741 view->count = 0;
4742 break;
4744 i = 0;
4745 while (i++ < nscroll && s->first_displayed_line > 1)
4746 s->first_displayed_line--;
4747 break;
4748 case 'j':
4749 case KEY_DOWN:
4750 case CTRL('n'):
4751 if (!s->eof)
4752 s->first_displayed_line++;
4753 else
4754 view->count = 0;
4755 break;
4756 case CTRL('d'):
4757 case 'd':
4758 nscroll /= 2;
4759 /* FALL THROUGH */
4760 case KEY_NPAGE:
4761 case CTRL('f'):
4762 case 'f':
4763 case ' ':
4764 if (s->eof) {
4765 view->count = 0;
4766 break;
4768 i = 0;
4769 while (!s->eof && i++ < nscroll) {
4770 linelen = getline(&line, &linesize, s->f);
4771 s->first_displayed_line++;
4772 if (linelen == -1) {
4773 if (feof(s->f)) {
4774 s->eof = 1;
4775 } else
4776 err = got_ferror(s->f, GOT_ERR_IO);
4777 break;
4780 free(line);
4781 break;
4782 case '[':
4783 if (s->diff_context > 0) {
4784 s->diff_context--;
4785 s->matched_line = 0;
4786 diff_view_indicate_progress(view);
4787 err = create_diff(s);
4788 if (s->first_displayed_line + view->nlines - 1 >
4789 s->nlines) {
4790 s->first_displayed_line = 1;
4791 s->last_displayed_line = view->nlines;
4793 } else
4794 view->count = 0;
4795 break;
4796 case ']':
4797 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4798 s->diff_context++;
4799 s->matched_line = 0;
4800 diff_view_indicate_progress(view);
4801 err = create_diff(s);
4802 } else
4803 view->count = 0;
4804 break;
4805 case '<':
4806 case ',':
4807 up = 1;
4808 /* FALL THROUGH */
4809 case '>':
4810 case '.':
4811 if (s->parent_view == NULL) {
4812 view->count = 0;
4813 break;
4815 s->parent_view->count = view->count;
4817 if (s->parent_view->type == TOG_VIEW_LOG) {
4818 ls = &s->parent_view->state.log;
4819 old_selected_entry = ls->selected_entry;
4821 err = input_log_view(NULL, s->parent_view,
4822 up ? KEY_UP : KEY_DOWN);
4823 if (err)
4824 break;
4825 view->count = s->parent_view->count;
4827 if (old_selected_entry == ls->selected_entry)
4828 break;
4830 err = set_selected_commit(s, ls->selected_entry);
4831 if (err)
4832 break;
4833 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4834 struct tog_blame_view_state *bs;
4835 struct got_object_id *id, *prev_id;
4837 bs = &s->parent_view->state.blame;
4838 prev_id = get_annotation_for_line(bs->blame.lines,
4839 bs->blame.nlines, bs->last_diffed_line);
4841 err = input_blame_view(&view, s->parent_view,
4842 up ? KEY_UP : KEY_DOWN);
4843 if (err)
4844 break;
4845 view->count = s->parent_view->count;
4847 if (prev_id == NULL)
4848 break;
4849 id = get_selected_commit_id(bs->blame.lines,
4850 bs->blame.nlines, bs->first_displayed_line,
4851 bs->selected_line);
4852 if (id == NULL)
4853 break;
4855 if (!got_object_id_cmp(prev_id, id))
4856 break;
4858 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4859 if (err)
4860 break;
4862 s->first_displayed_line = 1;
4863 s->last_displayed_line = view->nlines;
4864 s->matched_line = 0;
4865 view->x = 0;
4867 diff_view_indicate_progress(view);
4868 err = create_diff(s);
4869 break;
4870 default:
4871 view->count = 0;
4872 break;
4875 return err;
4878 static const struct got_error *
4879 cmd_diff(int argc, char *argv[])
4881 const struct got_error *error = NULL;
4882 struct got_repository *repo = NULL;
4883 struct got_worktree *worktree = NULL;
4884 struct got_object_id *id1 = NULL, *id2 = NULL;
4885 char *repo_path = NULL, *cwd = NULL;
4886 char *id_str1 = NULL, *id_str2 = NULL;
4887 char *label1 = NULL, *label2 = NULL;
4888 int diff_context = 3, ignore_whitespace = 0;
4889 int ch, force_text_diff = 0;
4890 const char *errstr;
4891 struct tog_view *view;
4892 int *pack_fds = NULL;
4894 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4895 switch (ch) {
4896 case 'a':
4897 force_text_diff = 1;
4898 break;
4899 case 'C':
4900 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4901 &errstr);
4902 if (errstr != NULL)
4903 errx(1, "number of context lines is %s: %s",
4904 errstr, errstr);
4905 break;
4906 case 'r':
4907 repo_path = realpath(optarg, NULL);
4908 if (repo_path == NULL)
4909 return got_error_from_errno2("realpath",
4910 optarg);
4911 got_path_strip_trailing_slashes(repo_path);
4912 break;
4913 case 'w':
4914 ignore_whitespace = 1;
4915 break;
4916 default:
4917 usage_diff();
4918 /* NOTREACHED */
4922 argc -= optind;
4923 argv += optind;
4925 if (argc == 0) {
4926 usage_diff(); /* TODO show local worktree changes */
4927 } else if (argc == 2) {
4928 id_str1 = argv[0];
4929 id_str2 = argv[1];
4930 } else
4931 usage_diff();
4933 error = got_repo_pack_fds_open(&pack_fds);
4934 if (error)
4935 goto done;
4937 if (repo_path == NULL) {
4938 cwd = getcwd(NULL, 0);
4939 if (cwd == NULL)
4940 return got_error_from_errno("getcwd");
4941 error = got_worktree_open(&worktree, cwd);
4942 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4943 goto done;
4944 if (worktree)
4945 repo_path =
4946 strdup(got_worktree_get_repo_path(worktree));
4947 else
4948 repo_path = strdup(cwd);
4949 if (repo_path == NULL) {
4950 error = got_error_from_errno("strdup");
4951 goto done;
4955 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4956 if (error)
4957 goto done;
4959 init_curses();
4961 error = apply_unveil(got_repo_get_path(repo), NULL);
4962 if (error)
4963 goto done;
4965 error = tog_load_refs(repo, 0);
4966 if (error)
4967 goto done;
4969 error = got_repo_match_object_id(&id1, &label1, id_str1,
4970 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4971 if (error)
4972 goto done;
4974 error = got_repo_match_object_id(&id2, &label2, id_str2,
4975 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4976 if (error)
4977 goto done;
4979 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4980 if (view == NULL) {
4981 error = got_error_from_errno("view_open");
4982 goto done;
4984 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4985 ignore_whitespace, force_text_diff, NULL, repo);
4986 if (error)
4987 goto done;
4988 error = view_loop(view);
4989 done:
4990 free(label1);
4991 free(label2);
4992 free(repo_path);
4993 free(cwd);
4994 if (repo) {
4995 const struct got_error *close_err = got_repo_close(repo);
4996 if (error == NULL)
4997 error = close_err;
4999 if (worktree)
5000 got_worktree_close(worktree);
5001 if (pack_fds) {
5002 const struct got_error *pack_err =
5003 got_repo_pack_fds_close(pack_fds);
5004 if (error == NULL)
5005 error = pack_err;
5007 tog_free_refs();
5008 return error;
5011 __dead static void
5012 usage_blame(void)
5014 endwin();
5015 fprintf(stderr,
5016 "usage: %s blame [-c commit] [-r repository-path] path\n",
5017 getprogname());
5018 exit(1);
5021 struct tog_blame_line {
5022 int annotated;
5023 struct got_object_id *id;
5026 static const struct got_error *
5027 draw_blame(struct tog_view *view)
5029 struct tog_blame_view_state *s = &view->state.blame;
5030 struct tog_blame *blame = &s->blame;
5031 regmatch_t *regmatch = &view->regmatch;
5032 const struct got_error *err;
5033 int lineno = 0, nprinted = 0;
5034 char *line = NULL;
5035 size_t linesize = 0;
5036 ssize_t linelen;
5037 wchar_t *wline;
5038 int width;
5039 struct tog_blame_line *blame_line;
5040 struct got_object_id *prev_id = NULL;
5041 char *id_str;
5042 struct tog_color *tc;
5044 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5045 if (err)
5046 return err;
5048 rewind(blame->f);
5049 werase(view->window);
5051 if (asprintf(&line, "commit %s", id_str) == -1) {
5052 err = got_error_from_errno("asprintf");
5053 free(id_str);
5054 return err;
5057 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5058 free(line);
5059 line = NULL;
5060 if (err)
5061 return err;
5062 if (view_needs_focus_indication(view))
5063 wstandout(view->window);
5064 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5065 if (tc)
5066 wattr_on(view->window,
5067 COLOR_PAIR(tc->colorpair), NULL);
5068 waddwstr(view->window, wline);
5069 if (tc)
5070 wattr_off(view->window,
5071 COLOR_PAIR(tc->colorpair), NULL);
5072 if (view_needs_focus_indication(view))
5073 wstandend(view->window);
5074 free(wline);
5075 wline = NULL;
5076 if (width < view->ncols - 1)
5077 waddch(view->window, '\n');
5079 if (asprintf(&line, "[%d/%d] %s%s",
5080 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5081 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5082 free(id_str);
5083 return got_error_from_errno("asprintf");
5085 free(id_str);
5086 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 free(line);
5088 line = NULL;
5089 if (err)
5090 return err;
5091 waddwstr(view->window, wline);
5092 free(wline);
5093 wline = NULL;
5094 if (width < view->ncols - 1)
5095 waddch(view->window, '\n');
5097 s->eof = 0;
5098 view->maxx = 0;
5099 while (nprinted < view->nlines - 2) {
5100 linelen = getline(&line, &linesize, blame->f);
5101 if (linelen == -1) {
5102 if (feof(blame->f)) {
5103 s->eof = 1;
5104 break;
5106 free(line);
5107 return got_ferror(blame->f, GOT_ERR_IO);
5109 if (++lineno < s->first_displayed_line)
5110 continue;
5112 /* Set view->maxx based on full line length. */
5113 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5114 if (err) {
5115 free(line);
5116 return err;
5118 free(wline);
5119 wline = NULL;
5120 view->maxx = MAX(view->maxx, width);
5122 if (nprinted == s->selected_line - 1)
5123 wstandout(view->window);
5125 if (blame->nlines > 0) {
5126 blame_line = &blame->lines[lineno - 1];
5127 if (blame_line->annotated && prev_id &&
5128 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5129 !(nprinted == s->selected_line - 1)) {
5130 waddstr(view->window, " ");
5131 } else if (blame_line->annotated) {
5132 char *id_str;
5133 err = got_object_id_str(&id_str,
5134 blame_line->id);
5135 if (err) {
5136 free(line);
5137 return err;
5139 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5140 if (tc)
5141 wattr_on(view->window,
5142 COLOR_PAIR(tc->colorpair), NULL);
5143 wprintw(view->window, "%.8s", id_str);
5144 if (tc)
5145 wattr_off(view->window,
5146 COLOR_PAIR(tc->colorpair), NULL);
5147 free(id_str);
5148 prev_id = blame_line->id;
5149 } else {
5150 waddstr(view->window, "........");
5151 prev_id = NULL;
5153 } else {
5154 waddstr(view->window, "........");
5155 prev_id = NULL;
5158 if (nprinted == s->selected_line - 1)
5159 wstandend(view->window);
5160 waddstr(view->window, " ");
5162 if (view->ncols <= 9) {
5163 width = 9;
5164 } else if (s->first_displayed_line + nprinted ==
5165 s->matched_line &&
5166 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5167 err = add_matched_line(&width, line, view->ncols - 9, 9,
5168 view->window, view->x, regmatch);
5169 if (err) {
5170 free(line);
5171 return err;
5173 width += 9;
5174 } else {
5175 int skip;
5176 err = format_line(&wline, &width, &skip, line,
5177 view->x, view->ncols - 9, 9, 1);
5178 if (err) {
5179 free(line);
5180 return err;
5182 waddwstr(view->window, &wline[skip]);
5183 width += 9;
5184 free(wline);
5185 wline = NULL;
5188 if (width <= view->ncols - 1)
5189 waddch(view->window, '\n');
5190 if (++nprinted == 1)
5191 s->first_displayed_line = lineno;
5193 free(line);
5194 s->last_displayed_line = lineno;
5196 view_border(view);
5198 return NULL;
5201 static const struct got_error *
5202 blame_cb(void *arg, int nlines, int lineno,
5203 struct got_commit_object *commit, struct got_object_id *id)
5205 const struct got_error *err = NULL;
5206 struct tog_blame_cb_args *a = arg;
5207 struct tog_blame_line *line;
5208 int errcode;
5210 if (nlines != a->nlines ||
5211 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5212 return got_error(GOT_ERR_RANGE);
5214 errcode = pthread_mutex_lock(&tog_mutex);
5215 if (errcode)
5216 return got_error_set_errno(errcode, "pthread_mutex_lock");
5218 if (*a->quit) { /* user has quit the blame view */
5219 err = got_error(GOT_ERR_ITER_COMPLETED);
5220 goto done;
5223 if (lineno == -1)
5224 goto done; /* no change in this commit */
5226 line = &a->lines[lineno - 1];
5227 if (line->annotated)
5228 goto done;
5230 line->id = got_object_id_dup(id);
5231 if (line->id == NULL) {
5232 err = got_error_from_errno("got_object_id_dup");
5233 goto done;
5235 line->annotated = 1;
5236 done:
5237 errcode = pthread_mutex_unlock(&tog_mutex);
5238 if (errcode)
5239 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5240 return err;
5243 static void *
5244 blame_thread(void *arg)
5246 const struct got_error *err, *close_err;
5247 struct tog_blame_thread_args *ta = arg;
5248 struct tog_blame_cb_args *a = ta->cb_args;
5249 int errcode, fd1 = -1, fd2 = -1;
5250 FILE *f1 = NULL, *f2 = NULL;
5252 fd1 = got_opentempfd();
5253 if (fd1 == -1)
5254 return (void *)got_error_from_errno("got_opentempfd");
5256 fd2 = got_opentempfd();
5257 if (fd2 == -1) {
5258 err = got_error_from_errno("got_opentempfd");
5259 goto done;
5262 f1 = got_opentemp();
5263 if (f1 == NULL) {
5264 err = (void *)got_error_from_errno("got_opentemp");
5265 goto done;
5267 f2 = got_opentemp();
5268 if (f2 == NULL) {
5269 err = (void *)got_error_from_errno("got_opentemp");
5270 goto done;
5273 err = block_signals_used_by_main_thread();
5274 if (err)
5275 goto done;
5277 err = got_blame(ta->path, a->commit_id, ta->repo,
5278 tog_diff_algo, blame_cb, ta->cb_args,
5279 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5280 if (err && err->code == GOT_ERR_CANCELLED)
5281 err = NULL;
5283 errcode = pthread_mutex_lock(&tog_mutex);
5284 if (errcode) {
5285 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5286 goto done;
5289 close_err = got_repo_close(ta->repo);
5290 if (err == NULL)
5291 err = close_err;
5292 ta->repo = NULL;
5293 *ta->complete = 1;
5295 errcode = pthread_mutex_unlock(&tog_mutex);
5296 if (errcode && err == NULL)
5297 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5299 done:
5300 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5301 err = got_error_from_errno("close");
5302 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5303 err = got_error_from_errno("close");
5304 if (f1 && fclose(f1) == EOF && err == NULL)
5305 err = got_error_from_errno("fclose");
5306 if (f2 && fclose(f2) == EOF && err == NULL)
5307 err = got_error_from_errno("fclose");
5309 return (void *)err;
5312 static struct got_object_id *
5313 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5314 int first_displayed_line, int selected_line)
5316 struct tog_blame_line *line;
5318 if (nlines <= 0)
5319 return NULL;
5321 line = &lines[first_displayed_line - 1 + selected_line - 1];
5322 if (!line->annotated)
5323 return NULL;
5325 return line->id;
5328 static struct got_object_id *
5329 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5330 int lineno)
5332 struct tog_blame_line *line;
5334 if (nlines <= 0 || lineno >= nlines)
5335 return NULL;
5337 line = &lines[lineno - 1];
5338 if (!line->annotated)
5339 return NULL;
5341 return line->id;
5344 static const struct got_error *
5345 stop_blame(struct tog_blame *blame)
5347 const struct got_error *err = NULL;
5348 int i;
5350 if (blame->thread) {
5351 int errcode;
5352 errcode = pthread_mutex_unlock(&tog_mutex);
5353 if (errcode)
5354 return got_error_set_errno(errcode,
5355 "pthread_mutex_unlock");
5356 errcode = pthread_join(blame->thread, (void **)&err);
5357 if (errcode)
5358 return got_error_set_errno(errcode, "pthread_join");
5359 errcode = pthread_mutex_lock(&tog_mutex);
5360 if (errcode)
5361 return got_error_set_errno(errcode,
5362 "pthread_mutex_lock");
5363 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5364 err = NULL;
5365 blame->thread = 0; //NULL;
5367 if (blame->thread_args.repo) {
5368 const struct got_error *close_err;
5369 close_err = got_repo_close(blame->thread_args.repo);
5370 if (err == NULL)
5371 err = close_err;
5372 blame->thread_args.repo = NULL;
5374 if (blame->f) {
5375 if (fclose(blame->f) == EOF && err == NULL)
5376 err = got_error_from_errno("fclose");
5377 blame->f = NULL;
5379 if (blame->lines) {
5380 for (i = 0; i < blame->nlines; i++)
5381 free(blame->lines[i].id);
5382 free(blame->lines);
5383 blame->lines = NULL;
5385 free(blame->cb_args.commit_id);
5386 blame->cb_args.commit_id = NULL;
5387 if (blame->pack_fds) {
5388 const struct got_error *pack_err =
5389 got_repo_pack_fds_close(blame->pack_fds);
5390 if (err == NULL)
5391 err = pack_err;
5392 blame->pack_fds = NULL;
5394 return err;
5397 static const struct got_error *
5398 cancel_blame_view(void *arg)
5400 const struct got_error *err = NULL;
5401 int *done = arg;
5402 int errcode;
5404 errcode = pthread_mutex_lock(&tog_mutex);
5405 if (errcode)
5406 return got_error_set_errno(errcode,
5407 "pthread_mutex_unlock");
5409 if (*done)
5410 err = got_error(GOT_ERR_CANCELLED);
5412 errcode = pthread_mutex_unlock(&tog_mutex);
5413 if (errcode)
5414 return got_error_set_errno(errcode,
5415 "pthread_mutex_lock");
5417 return err;
5420 static const struct got_error *
5421 run_blame(struct tog_view *view)
5423 struct tog_blame_view_state *s = &view->state.blame;
5424 struct tog_blame *blame = &s->blame;
5425 const struct got_error *err = NULL;
5426 struct got_commit_object *commit = NULL;
5427 struct got_blob_object *blob = NULL;
5428 struct got_repository *thread_repo = NULL;
5429 struct got_object_id *obj_id = NULL;
5430 int obj_type, fd = -1;
5431 int *pack_fds = NULL;
5433 err = got_object_open_as_commit(&commit, s->repo,
5434 &s->blamed_commit->id);
5435 if (err)
5436 return err;
5438 fd = got_opentempfd();
5439 if (fd == -1) {
5440 err = got_error_from_errno("got_opentempfd");
5441 goto done;
5444 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5445 if (err)
5446 goto done;
5448 err = got_object_get_type(&obj_type, s->repo, obj_id);
5449 if (err)
5450 goto done;
5452 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5453 err = got_error(GOT_ERR_OBJ_TYPE);
5454 goto done;
5457 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5458 if (err)
5459 goto done;
5460 blame->f = got_opentemp();
5461 if (blame->f == NULL) {
5462 err = got_error_from_errno("got_opentemp");
5463 goto done;
5465 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5466 &blame->line_offsets, blame->f, blob);
5467 if (err)
5468 goto done;
5469 if (blame->nlines == 0) {
5470 s->blame_complete = 1;
5471 goto done;
5474 /* Don't include \n at EOF in the blame line count. */
5475 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5476 blame->nlines--;
5478 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5479 if (blame->lines == NULL) {
5480 err = got_error_from_errno("calloc");
5481 goto done;
5484 err = got_repo_pack_fds_open(&pack_fds);
5485 if (err)
5486 goto done;
5487 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5488 pack_fds);
5489 if (err)
5490 goto done;
5492 blame->pack_fds = pack_fds;
5493 blame->cb_args.view = view;
5494 blame->cb_args.lines = blame->lines;
5495 blame->cb_args.nlines = blame->nlines;
5496 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5497 if (blame->cb_args.commit_id == NULL) {
5498 err = got_error_from_errno("got_object_id_dup");
5499 goto done;
5501 blame->cb_args.quit = &s->done;
5503 blame->thread_args.path = s->path;
5504 blame->thread_args.repo = thread_repo;
5505 blame->thread_args.cb_args = &blame->cb_args;
5506 blame->thread_args.complete = &s->blame_complete;
5507 blame->thread_args.cancel_cb = cancel_blame_view;
5508 blame->thread_args.cancel_arg = &s->done;
5509 s->blame_complete = 0;
5511 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5512 s->first_displayed_line = 1;
5513 s->last_displayed_line = view->nlines;
5514 s->selected_line = 1;
5516 s->matched_line = 0;
5518 done:
5519 if (commit)
5520 got_object_commit_close(commit);
5521 if (fd != -1 && close(fd) == -1 && err == NULL)
5522 err = got_error_from_errno("close");
5523 if (blob)
5524 got_object_blob_close(blob);
5525 free(obj_id);
5526 if (err)
5527 stop_blame(blame);
5528 return err;
5531 static const struct got_error *
5532 open_blame_view(struct tog_view *view, char *path,
5533 struct got_object_id *commit_id, struct got_repository *repo)
5535 const struct got_error *err = NULL;
5536 struct tog_blame_view_state *s = &view->state.blame;
5538 STAILQ_INIT(&s->blamed_commits);
5540 s->path = strdup(path);
5541 if (s->path == NULL)
5542 return got_error_from_errno("strdup");
5544 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5545 if (err) {
5546 free(s->path);
5547 return err;
5550 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5551 s->first_displayed_line = 1;
5552 s->last_displayed_line = view->nlines;
5553 s->selected_line = 1;
5554 s->blame_complete = 0;
5555 s->repo = repo;
5556 s->commit_id = commit_id;
5557 memset(&s->blame, 0, sizeof(s->blame));
5559 STAILQ_INIT(&s->colors);
5560 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5561 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5562 get_color_value("TOG_COLOR_COMMIT"));
5563 if (err)
5564 return err;
5567 view->show = show_blame_view;
5568 view->input = input_blame_view;
5569 view->reset = reset_blame_view;
5570 view->close = close_blame_view;
5571 view->search_start = search_start_blame_view;
5572 view->search_next = search_next_blame_view;
5574 return run_blame(view);
5577 static const struct got_error *
5578 close_blame_view(struct tog_view *view)
5580 const struct got_error *err = NULL;
5581 struct tog_blame_view_state *s = &view->state.blame;
5583 if (s->blame.thread)
5584 err = stop_blame(&s->blame);
5586 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5587 struct got_object_qid *blamed_commit;
5588 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5589 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5590 got_object_qid_free(blamed_commit);
5593 free(s->path);
5594 free_colors(&s->colors);
5595 return err;
5598 static const struct got_error *
5599 search_start_blame_view(struct tog_view *view)
5601 struct tog_blame_view_state *s = &view->state.blame;
5603 s->matched_line = 0;
5604 return NULL;
5607 static const struct got_error *
5608 search_next_blame_view(struct tog_view *view)
5610 struct tog_blame_view_state *s = &view->state.blame;
5611 const struct got_error *err = NULL;
5612 int lineno;
5613 char *line = NULL;
5614 size_t linesize = 0;
5615 ssize_t linelen;
5617 if (!view->searching) {
5618 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5619 return NULL;
5622 if (s->matched_line) {
5623 if (view->searching == TOG_SEARCH_FORWARD)
5624 lineno = s->matched_line + 1;
5625 else
5626 lineno = s->matched_line - 1;
5627 } else
5628 lineno = s->first_displayed_line - 1 + s->selected_line;
5630 while (1) {
5631 off_t offset;
5633 if (lineno <= 0 || lineno > s->blame.nlines) {
5634 if (s->matched_line == 0) {
5635 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5636 break;
5639 if (view->searching == TOG_SEARCH_FORWARD)
5640 lineno = 1;
5641 else
5642 lineno = s->blame.nlines;
5645 offset = s->blame.line_offsets[lineno - 1];
5646 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5647 free(line);
5648 return got_error_from_errno("fseeko");
5650 linelen = getline(&line, &linesize, s->blame.f);
5651 if (linelen != -1) {
5652 char *exstr;
5653 err = expand_tab(&exstr, line);
5654 if (err)
5655 break;
5656 if (match_line(exstr, &view->regex, 1,
5657 &view->regmatch)) {
5658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5659 s->matched_line = lineno;
5660 free(exstr);
5661 break;
5663 free(exstr);
5665 if (view->searching == TOG_SEARCH_FORWARD)
5666 lineno++;
5667 else
5668 lineno--;
5670 free(line);
5672 if (s->matched_line) {
5673 s->first_displayed_line = s->matched_line;
5674 s->selected_line = 1;
5677 return err;
5680 static const struct got_error *
5681 show_blame_view(struct tog_view *view)
5683 const struct got_error *err = NULL;
5684 struct tog_blame_view_state *s = &view->state.blame;
5685 int errcode;
5687 if (s->blame.thread == 0 && !s->blame_complete) {
5688 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5689 &s->blame.thread_args);
5690 if (errcode)
5691 return got_error_set_errno(errcode, "pthread_create");
5693 halfdelay(1); /* fast refresh while annotating */
5696 if (s->blame_complete)
5697 halfdelay(10); /* disable fast refresh */
5699 err = draw_blame(view);
5701 view_border(view);
5702 return err;
5705 static const struct got_error *
5706 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5708 const struct got_error *err = NULL, *thread_err = NULL;
5709 struct tog_view *diff_view;
5710 struct tog_blame_view_state *s = &view->state.blame;
5711 int eos, nscroll, begin_y = 0, begin_x = 0;
5713 eos = nscroll = view->nlines - 2;
5714 if (view_is_hsplit_top(view))
5715 --eos; /* border */
5717 switch (ch) {
5718 case '0':
5719 view->x = 0;
5720 break;
5721 case '$':
5722 view->x = MAX(view->maxx - view->ncols / 3, 0);
5723 view->count = 0;
5724 break;
5725 case KEY_RIGHT:
5726 case 'l':
5727 if (view->x + view->ncols / 3 < view->maxx)
5728 view->x += 2; /* move two columns right */
5729 else
5730 view->count = 0;
5731 break;
5732 case KEY_LEFT:
5733 case 'h':
5734 view->x -= MIN(view->x, 2); /* move two columns back */
5735 if (view->x <= 0)
5736 view->count = 0;
5737 break;
5738 case 'q':
5739 s->done = 1;
5740 break;
5741 case 'g':
5742 case KEY_HOME:
5743 s->selected_line = 1;
5744 s->first_displayed_line = 1;
5745 view->count = 0;
5746 break;
5747 case 'G':
5748 case KEY_END:
5749 if (s->blame.nlines < eos) {
5750 s->selected_line = s->blame.nlines;
5751 s->first_displayed_line = 1;
5752 } else {
5753 s->selected_line = eos;
5754 s->first_displayed_line = s->blame.nlines - (eos - 1);
5756 view->count = 0;
5757 break;
5758 case 'k':
5759 case KEY_UP:
5760 case CTRL('p'):
5761 if (s->selected_line > 1)
5762 s->selected_line--;
5763 else if (s->selected_line == 1 &&
5764 s->first_displayed_line > 1)
5765 s->first_displayed_line--;
5766 else
5767 view->count = 0;
5768 break;
5769 case CTRL('u'):
5770 case 'u':
5771 nscroll /= 2;
5772 /* FALL THROUGH */
5773 case KEY_PPAGE:
5774 case CTRL('b'):
5775 case 'b':
5776 if (s->first_displayed_line == 1) {
5777 if (view->count > 1)
5778 nscroll += nscroll;
5779 s->selected_line = MAX(1, s->selected_line - nscroll);
5780 view->count = 0;
5781 break;
5783 if (s->first_displayed_line > nscroll)
5784 s->first_displayed_line -= nscroll;
5785 else
5786 s->first_displayed_line = 1;
5787 break;
5788 case 'j':
5789 case KEY_DOWN:
5790 case CTRL('n'):
5791 if (s->selected_line < eos && s->first_displayed_line +
5792 s->selected_line <= s->blame.nlines)
5793 s->selected_line++;
5794 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5795 s->first_displayed_line++;
5796 else
5797 view->count = 0;
5798 break;
5799 case 'c':
5800 case 'p': {
5801 struct got_object_id *id = NULL;
5803 view->count = 0;
5804 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5805 s->first_displayed_line, s->selected_line);
5806 if (id == NULL)
5807 break;
5808 if (ch == 'p') {
5809 struct got_commit_object *commit, *pcommit;
5810 struct got_object_qid *pid;
5811 struct got_object_id *blob_id = NULL;
5812 int obj_type;
5813 err = got_object_open_as_commit(&commit,
5814 s->repo, id);
5815 if (err)
5816 break;
5817 pid = STAILQ_FIRST(
5818 got_object_commit_get_parent_ids(commit));
5819 if (pid == NULL) {
5820 got_object_commit_close(commit);
5821 break;
5823 /* Check if path history ends here. */
5824 err = got_object_open_as_commit(&pcommit,
5825 s->repo, &pid->id);
5826 if (err)
5827 break;
5828 err = got_object_id_by_path(&blob_id, s->repo,
5829 pcommit, s->path);
5830 got_object_commit_close(pcommit);
5831 if (err) {
5832 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5833 err = NULL;
5834 got_object_commit_close(commit);
5835 break;
5837 err = got_object_get_type(&obj_type, s->repo,
5838 blob_id);
5839 free(blob_id);
5840 /* Can't blame non-blob type objects. */
5841 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5842 got_object_commit_close(commit);
5843 break;
5845 err = got_object_qid_alloc(&s->blamed_commit,
5846 &pid->id);
5847 got_object_commit_close(commit);
5848 } else {
5849 if (got_object_id_cmp(id,
5850 &s->blamed_commit->id) == 0)
5851 break;
5852 err = got_object_qid_alloc(&s->blamed_commit,
5853 id);
5855 if (err)
5856 break;
5857 s->done = 1;
5858 thread_err = stop_blame(&s->blame);
5859 s->done = 0;
5860 if (thread_err)
5861 break;
5862 STAILQ_INSERT_HEAD(&s->blamed_commits,
5863 s->blamed_commit, entry);
5864 err = run_blame(view);
5865 if (err)
5866 break;
5867 break;
5869 case 'C': {
5870 struct got_object_qid *first;
5872 view->count = 0;
5873 first = STAILQ_FIRST(&s->blamed_commits);
5874 if (!got_object_id_cmp(&first->id, s->commit_id))
5875 break;
5876 s->done = 1;
5877 thread_err = stop_blame(&s->blame);
5878 s->done = 0;
5879 if (thread_err)
5880 break;
5881 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5882 got_object_qid_free(s->blamed_commit);
5883 s->blamed_commit =
5884 STAILQ_FIRST(&s->blamed_commits);
5885 err = run_blame(view);
5886 if (err)
5887 break;
5888 break;
5890 case KEY_ENTER:
5891 case '\r': {
5892 struct got_object_id *id = NULL;
5893 struct got_object_qid *pid;
5894 struct got_commit_object *commit = NULL;
5896 view->count = 0;
5897 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5898 s->first_displayed_line, s->selected_line);
5899 if (id == NULL)
5900 break;
5901 err = got_object_open_as_commit(&commit, s->repo, id);
5902 if (err)
5903 break;
5904 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5905 if (*new_view) {
5906 /* traversed from diff view, release diff resources */
5907 err = close_diff_view(*new_view);
5908 if (err)
5909 break;
5910 diff_view = *new_view;
5911 } else {
5912 if (view_is_parent_view(view))
5913 view_get_split(view, &begin_y, &begin_x);
5915 diff_view = view_open(0, 0, begin_y, begin_x,
5916 TOG_VIEW_DIFF);
5917 if (diff_view == NULL) {
5918 got_object_commit_close(commit);
5919 err = got_error_from_errno("view_open");
5920 break;
5923 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5924 id, NULL, NULL, 3, 0, 0, view, s->repo);
5925 got_object_commit_close(commit);
5926 if (err) {
5927 view_close(diff_view);
5928 break;
5930 s->last_diffed_line = s->first_displayed_line - 1 +
5931 s->selected_line;
5932 if (*new_view)
5933 break; /* still open from active diff view */
5934 if (view_is_parent_view(view) &&
5935 view->mode == TOG_VIEW_SPLIT_HRZN) {
5936 err = view_init_hsplit(view, begin_y);
5937 if (err)
5938 break;
5941 view->focussed = 0;
5942 diff_view->focussed = 1;
5943 diff_view->mode = view->mode;
5944 diff_view->nlines = view->lines - begin_y;
5945 if (view_is_parent_view(view)) {
5946 view_transfer_size(diff_view, view);
5947 err = view_close_child(view);
5948 if (err)
5949 break;
5950 err = view_set_child(view, diff_view);
5951 if (err)
5952 break;
5953 view->focus_child = 1;
5954 } else
5955 *new_view = diff_view;
5956 if (err)
5957 break;
5958 break;
5960 case CTRL('d'):
5961 case 'd':
5962 nscroll /= 2;
5963 /* FALL THROUGH */
5964 case KEY_NPAGE:
5965 case CTRL('f'):
5966 case 'f':
5967 case ' ':
5968 if (s->last_displayed_line >= s->blame.nlines &&
5969 s->selected_line >= MIN(s->blame.nlines,
5970 view->nlines - 2)) {
5971 view->count = 0;
5972 break;
5974 if (s->last_displayed_line >= s->blame.nlines &&
5975 s->selected_line < view->nlines - 2) {
5976 s->selected_line +=
5977 MIN(nscroll, s->last_displayed_line -
5978 s->first_displayed_line - s->selected_line + 1);
5980 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5981 s->first_displayed_line += nscroll;
5982 else
5983 s->first_displayed_line =
5984 s->blame.nlines - (view->nlines - 3);
5985 break;
5986 case KEY_RESIZE:
5987 if (s->selected_line > view->nlines - 2) {
5988 s->selected_line = MIN(s->blame.nlines,
5989 view->nlines - 2);
5991 break;
5992 default:
5993 view->count = 0;
5994 break;
5996 return thread_err ? thread_err : err;
5999 static const struct got_error *
6000 reset_blame_view(struct tog_view *view)
6002 const struct got_error *err;
6003 struct tog_blame_view_state *s = &view->state.blame;
6005 view->count = 0;
6006 s->done = 1;
6007 err = stop_blame(&s->blame);
6008 s->done = 0;
6009 if (err)
6010 return err;
6011 return run_blame(view);
6014 static const struct got_error *
6015 cmd_blame(int argc, char *argv[])
6017 const struct got_error *error;
6018 struct got_repository *repo = NULL;
6019 struct got_worktree *worktree = NULL;
6020 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6021 char *link_target = NULL;
6022 struct got_object_id *commit_id = NULL;
6023 struct got_commit_object *commit = NULL;
6024 char *commit_id_str = NULL;
6025 int ch;
6026 struct tog_view *view;
6027 int *pack_fds = NULL;
6029 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6030 switch (ch) {
6031 case 'c':
6032 commit_id_str = optarg;
6033 break;
6034 case 'r':
6035 repo_path = realpath(optarg, NULL);
6036 if (repo_path == NULL)
6037 return got_error_from_errno2("realpath",
6038 optarg);
6039 break;
6040 default:
6041 usage_blame();
6042 /* NOTREACHED */
6046 argc -= optind;
6047 argv += optind;
6049 if (argc != 1)
6050 usage_blame();
6052 error = got_repo_pack_fds_open(&pack_fds);
6053 if (error != NULL)
6054 goto done;
6056 if (repo_path == NULL) {
6057 cwd = getcwd(NULL, 0);
6058 if (cwd == NULL)
6059 return got_error_from_errno("getcwd");
6060 error = got_worktree_open(&worktree, cwd);
6061 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6062 goto done;
6063 if (worktree)
6064 repo_path =
6065 strdup(got_worktree_get_repo_path(worktree));
6066 else
6067 repo_path = strdup(cwd);
6068 if (repo_path == NULL) {
6069 error = got_error_from_errno("strdup");
6070 goto done;
6074 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6075 if (error != NULL)
6076 goto done;
6078 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6079 worktree);
6080 if (error)
6081 goto done;
6083 init_curses();
6085 error = apply_unveil(got_repo_get_path(repo), NULL);
6086 if (error)
6087 goto done;
6089 error = tog_load_refs(repo, 0);
6090 if (error)
6091 goto done;
6093 if (commit_id_str == NULL) {
6094 struct got_reference *head_ref;
6095 error = got_ref_open(&head_ref, repo, worktree ?
6096 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6097 if (error != NULL)
6098 goto done;
6099 error = got_ref_resolve(&commit_id, repo, head_ref);
6100 got_ref_close(head_ref);
6101 } else {
6102 error = got_repo_match_object_id(&commit_id, NULL,
6103 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6105 if (error != NULL)
6106 goto done;
6108 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6109 if (view == NULL) {
6110 error = got_error_from_errno("view_open");
6111 goto done;
6114 error = got_object_open_as_commit(&commit, repo, commit_id);
6115 if (error)
6116 goto done;
6118 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6119 commit, repo);
6120 if (error)
6121 goto done;
6123 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6124 commit_id, repo);
6125 if (error)
6126 goto done;
6127 if (worktree) {
6128 /* Release work tree lock. */
6129 got_worktree_close(worktree);
6130 worktree = NULL;
6132 error = view_loop(view);
6133 done:
6134 free(repo_path);
6135 free(in_repo_path);
6136 free(link_target);
6137 free(cwd);
6138 free(commit_id);
6139 if (commit)
6140 got_object_commit_close(commit);
6141 if (worktree)
6142 got_worktree_close(worktree);
6143 if (repo) {
6144 const struct got_error *close_err = got_repo_close(repo);
6145 if (error == NULL)
6146 error = close_err;
6148 if (pack_fds) {
6149 const struct got_error *pack_err =
6150 got_repo_pack_fds_close(pack_fds);
6151 if (error == NULL)
6152 error = pack_err;
6154 tog_free_refs();
6155 return error;
6158 static const struct got_error *
6159 draw_tree_entries(struct tog_view *view, const char *parent_path)
6161 struct tog_tree_view_state *s = &view->state.tree;
6162 const struct got_error *err = NULL;
6163 struct got_tree_entry *te;
6164 wchar_t *wline;
6165 struct tog_color *tc;
6166 int width, n, i, nentries;
6167 int limit = view->nlines;
6169 s->ndisplayed = 0;
6170 if (view_is_hsplit_top(view))
6171 --limit; /* border */
6173 werase(view->window);
6175 if (limit == 0)
6176 return NULL;
6178 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6179 0, 0);
6180 if (err)
6181 return err;
6182 if (view_needs_focus_indication(view))
6183 wstandout(view->window);
6184 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6185 if (tc)
6186 wattr_on(view->window,
6187 COLOR_PAIR(tc->colorpair), NULL);
6188 waddwstr(view->window, wline);
6189 if (tc)
6190 wattr_off(view->window,
6191 COLOR_PAIR(tc->colorpair), NULL);
6192 if (view_needs_focus_indication(view))
6193 wstandend(view->window);
6194 free(wline);
6195 wline = NULL;
6196 if (width < view->ncols - 1)
6197 waddch(view->window, '\n');
6198 if (--limit <= 0)
6199 return NULL;
6200 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6201 0, 0);
6202 if (err)
6203 return err;
6204 waddwstr(view->window, wline);
6205 free(wline);
6206 wline = NULL;
6207 if (width < view->ncols - 1)
6208 waddch(view->window, '\n');
6209 if (--limit <= 0)
6210 return NULL;
6211 waddch(view->window, '\n');
6212 if (--limit <= 0)
6213 return NULL;
6215 if (s->first_displayed_entry == NULL) {
6216 te = got_object_tree_get_first_entry(s->tree);
6217 if (s->selected == 0) {
6218 if (view->focussed)
6219 wstandout(view->window);
6220 s->selected_entry = NULL;
6222 waddstr(view->window, " ..\n"); /* parent directory */
6223 if (s->selected == 0 && view->focussed)
6224 wstandend(view->window);
6225 s->ndisplayed++;
6226 if (--limit <= 0)
6227 return NULL;
6228 n = 1;
6229 } else {
6230 n = 0;
6231 te = s->first_displayed_entry;
6234 nentries = got_object_tree_get_nentries(s->tree);
6235 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6236 char *line = NULL, *id_str = NULL, *link_target = NULL;
6237 const char *modestr = "";
6238 mode_t mode;
6240 te = got_object_tree_get_entry(s->tree, i);
6241 mode = got_tree_entry_get_mode(te);
6243 if (s->show_ids) {
6244 err = got_object_id_str(&id_str,
6245 got_tree_entry_get_id(te));
6246 if (err)
6247 return got_error_from_errno(
6248 "got_object_id_str");
6250 if (got_object_tree_entry_is_submodule(te))
6251 modestr = "$";
6252 else if (S_ISLNK(mode)) {
6253 int i;
6255 err = got_tree_entry_get_symlink_target(&link_target,
6256 te, s->repo);
6257 if (err) {
6258 free(id_str);
6259 return err;
6261 for (i = 0; i < strlen(link_target); i++) {
6262 if (!isprint((unsigned char)link_target[i]))
6263 link_target[i] = '?';
6265 modestr = "@";
6267 else if (S_ISDIR(mode))
6268 modestr = "/";
6269 else if (mode & S_IXUSR)
6270 modestr = "*";
6271 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6272 got_tree_entry_get_name(te), modestr,
6273 link_target ? " -> ": "",
6274 link_target ? link_target : "") == -1) {
6275 free(id_str);
6276 free(link_target);
6277 return got_error_from_errno("asprintf");
6279 free(id_str);
6280 free(link_target);
6281 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6282 0, 0);
6283 if (err) {
6284 free(line);
6285 break;
6287 if (n == s->selected) {
6288 if (view->focussed)
6289 wstandout(view->window);
6290 s->selected_entry = te;
6292 tc = match_color(&s->colors, line);
6293 if (tc)
6294 wattr_on(view->window,
6295 COLOR_PAIR(tc->colorpair), NULL);
6296 waddwstr(view->window, wline);
6297 if (tc)
6298 wattr_off(view->window,
6299 COLOR_PAIR(tc->colorpair), NULL);
6300 if (width < view->ncols - 1)
6301 waddch(view->window, '\n');
6302 if (n == s->selected && view->focussed)
6303 wstandend(view->window);
6304 free(line);
6305 free(wline);
6306 wline = NULL;
6307 n++;
6308 s->ndisplayed++;
6309 s->last_displayed_entry = te;
6310 if (--limit <= 0)
6311 break;
6314 return err;
6317 static void
6318 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6320 struct got_tree_entry *te;
6321 int isroot = s->tree == s->root;
6322 int i = 0;
6324 if (s->first_displayed_entry == NULL)
6325 return;
6327 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6328 while (i++ < maxscroll) {
6329 if (te == NULL) {
6330 if (!isroot)
6331 s->first_displayed_entry = NULL;
6332 break;
6334 s->first_displayed_entry = te;
6335 te = got_tree_entry_get_prev(s->tree, te);
6339 static const struct got_error *
6340 tree_scroll_down(struct tog_view *view, int maxscroll)
6342 struct tog_tree_view_state *s = &view->state.tree;
6343 struct got_tree_entry *next, *last;
6344 int n = 0;
6346 if (s->first_displayed_entry)
6347 next = got_tree_entry_get_next(s->tree,
6348 s->first_displayed_entry);
6349 else
6350 next = got_object_tree_get_first_entry(s->tree);
6352 last = s->last_displayed_entry;
6353 while (next && n++ < maxscroll) {
6354 if (last)
6355 last = got_tree_entry_get_next(s->tree, last);
6356 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6357 s->first_displayed_entry = next;
6358 next = got_tree_entry_get_next(s->tree, next);
6362 return NULL;
6365 static const struct got_error *
6366 tree_entry_path(char **path, struct tog_parent_trees *parents,
6367 struct got_tree_entry *te)
6369 const struct got_error *err = NULL;
6370 struct tog_parent_tree *pt;
6371 size_t len = 2; /* for leading slash and NUL */
6373 TAILQ_FOREACH(pt, parents, entry)
6374 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6375 + 1 /* slash */;
6376 if (te)
6377 len += strlen(got_tree_entry_get_name(te));
6379 *path = calloc(1, len);
6380 if (path == NULL)
6381 return got_error_from_errno("calloc");
6383 (*path)[0] = '/';
6384 pt = TAILQ_LAST(parents, tog_parent_trees);
6385 while (pt) {
6386 const char *name = got_tree_entry_get_name(pt->selected_entry);
6387 if (strlcat(*path, name, len) >= len) {
6388 err = got_error(GOT_ERR_NO_SPACE);
6389 goto done;
6391 if (strlcat(*path, "/", len) >= len) {
6392 err = got_error(GOT_ERR_NO_SPACE);
6393 goto done;
6395 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6397 if (te) {
6398 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6399 err = got_error(GOT_ERR_NO_SPACE);
6400 goto done;
6403 done:
6404 if (err) {
6405 free(*path);
6406 *path = NULL;
6408 return err;
6411 static const struct got_error *
6412 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6413 struct got_tree_entry *te, struct tog_parent_trees *parents,
6414 struct got_object_id *commit_id, struct got_repository *repo)
6416 const struct got_error *err = NULL;
6417 char *path;
6418 struct tog_view *blame_view;
6420 *new_view = NULL;
6422 err = tree_entry_path(&path, parents, te);
6423 if (err)
6424 return err;
6426 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6427 if (blame_view == NULL) {
6428 err = got_error_from_errno("view_open");
6429 goto done;
6432 err = open_blame_view(blame_view, path, commit_id, repo);
6433 if (err) {
6434 if (err->code == GOT_ERR_CANCELLED)
6435 err = NULL;
6436 view_close(blame_view);
6437 } else
6438 *new_view = blame_view;
6439 done:
6440 free(path);
6441 return err;
6444 static const struct got_error *
6445 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6446 struct tog_tree_view_state *s)
6448 struct tog_view *log_view;
6449 const struct got_error *err = NULL;
6450 char *path;
6452 *new_view = NULL;
6454 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6455 if (log_view == NULL)
6456 return got_error_from_errno("view_open");
6458 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6459 if (err)
6460 return err;
6462 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6463 path, 0);
6464 if (err)
6465 view_close(log_view);
6466 else
6467 *new_view = log_view;
6468 free(path);
6469 return err;
6472 static const struct got_error *
6473 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6474 const char *head_ref_name, struct got_repository *repo)
6476 const struct got_error *err = NULL;
6477 char *commit_id_str = NULL;
6478 struct tog_tree_view_state *s = &view->state.tree;
6479 struct got_commit_object *commit = NULL;
6481 TAILQ_INIT(&s->parents);
6482 STAILQ_INIT(&s->colors);
6484 s->commit_id = got_object_id_dup(commit_id);
6485 if (s->commit_id == NULL)
6486 return got_error_from_errno("got_object_id_dup");
6488 err = got_object_open_as_commit(&commit, repo, commit_id);
6489 if (err)
6490 goto done;
6493 * The root is opened here and will be closed when the view is closed.
6494 * Any visited subtrees and their path-wise parents are opened and
6495 * closed on demand.
6497 err = got_object_open_as_tree(&s->root, repo,
6498 got_object_commit_get_tree_id(commit));
6499 if (err)
6500 goto done;
6501 s->tree = s->root;
6503 err = got_object_id_str(&commit_id_str, commit_id);
6504 if (err != NULL)
6505 goto done;
6507 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6508 err = got_error_from_errno("asprintf");
6509 goto done;
6512 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6513 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6514 if (head_ref_name) {
6515 s->head_ref_name = strdup(head_ref_name);
6516 if (s->head_ref_name == NULL) {
6517 err = got_error_from_errno("strdup");
6518 goto done;
6521 s->repo = repo;
6523 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6524 err = add_color(&s->colors, "\\$$",
6525 TOG_COLOR_TREE_SUBMODULE,
6526 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6527 if (err)
6528 goto done;
6529 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6530 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6531 if (err)
6532 goto done;
6533 err = add_color(&s->colors, "/$",
6534 TOG_COLOR_TREE_DIRECTORY,
6535 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6536 if (err)
6537 goto done;
6539 err = add_color(&s->colors, "\\*$",
6540 TOG_COLOR_TREE_EXECUTABLE,
6541 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6542 if (err)
6543 goto done;
6545 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6546 get_color_value("TOG_COLOR_COMMIT"));
6547 if (err)
6548 goto done;
6551 view->show = show_tree_view;
6552 view->input = input_tree_view;
6553 view->close = close_tree_view;
6554 view->search_start = search_start_tree_view;
6555 view->search_next = search_next_tree_view;
6556 done:
6557 free(commit_id_str);
6558 if (commit)
6559 got_object_commit_close(commit);
6560 if (err)
6561 close_tree_view(view);
6562 return err;
6565 static const struct got_error *
6566 close_tree_view(struct tog_view *view)
6568 struct tog_tree_view_state *s = &view->state.tree;
6570 free_colors(&s->colors);
6571 free(s->tree_label);
6572 s->tree_label = NULL;
6573 free(s->commit_id);
6574 s->commit_id = NULL;
6575 free(s->head_ref_name);
6576 s->head_ref_name = NULL;
6577 while (!TAILQ_EMPTY(&s->parents)) {
6578 struct tog_parent_tree *parent;
6579 parent = TAILQ_FIRST(&s->parents);
6580 TAILQ_REMOVE(&s->parents, parent, entry);
6581 if (parent->tree != s->root)
6582 got_object_tree_close(parent->tree);
6583 free(parent);
6586 if (s->tree != NULL && s->tree != s->root)
6587 got_object_tree_close(s->tree);
6588 if (s->root)
6589 got_object_tree_close(s->root);
6590 return NULL;
6593 static const struct got_error *
6594 search_start_tree_view(struct tog_view *view)
6596 struct tog_tree_view_state *s = &view->state.tree;
6598 s->matched_entry = NULL;
6599 return NULL;
6602 static int
6603 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6605 regmatch_t regmatch;
6607 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6608 0) == 0;
6611 static const struct got_error *
6612 search_next_tree_view(struct tog_view *view)
6614 struct tog_tree_view_state *s = &view->state.tree;
6615 struct got_tree_entry *te = NULL;
6617 if (!view->searching) {
6618 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6619 return NULL;
6622 if (s->matched_entry) {
6623 if (view->searching == TOG_SEARCH_FORWARD) {
6624 if (s->selected_entry)
6625 te = got_tree_entry_get_next(s->tree,
6626 s->selected_entry);
6627 else
6628 te = got_object_tree_get_first_entry(s->tree);
6629 } else {
6630 if (s->selected_entry == NULL)
6631 te = got_object_tree_get_last_entry(s->tree);
6632 else
6633 te = got_tree_entry_get_prev(s->tree,
6634 s->selected_entry);
6636 } else {
6637 if (s->selected_entry)
6638 te = s->selected_entry;
6639 else if (view->searching == TOG_SEARCH_FORWARD)
6640 te = got_object_tree_get_first_entry(s->tree);
6641 else
6642 te = got_object_tree_get_last_entry(s->tree);
6645 while (1) {
6646 if (te == NULL) {
6647 if (s->matched_entry == NULL) {
6648 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6649 return NULL;
6651 if (view->searching == TOG_SEARCH_FORWARD)
6652 te = got_object_tree_get_first_entry(s->tree);
6653 else
6654 te = got_object_tree_get_last_entry(s->tree);
6657 if (match_tree_entry(te, &view->regex)) {
6658 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6659 s->matched_entry = te;
6660 break;
6663 if (view->searching == TOG_SEARCH_FORWARD)
6664 te = got_tree_entry_get_next(s->tree, te);
6665 else
6666 te = got_tree_entry_get_prev(s->tree, te);
6669 if (s->matched_entry) {
6670 s->first_displayed_entry = s->matched_entry;
6671 s->selected = 0;
6674 return NULL;
6677 static const struct got_error *
6678 show_tree_view(struct tog_view *view)
6680 const struct got_error *err = NULL;
6681 struct tog_tree_view_state *s = &view->state.tree;
6682 char *parent_path;
6684 err = tree_entry_path(&parent_path, &s->parents, NULL);
6685 if (err)
6686 return err;
6688 err = draw_tree_entries(view, parent_path);
6689 free(parent_path);
6691 view_border(view);
6692 return err;
6695 static const struct got_error *
6696 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6698 const struct got_error *err = NULL;
6699 struct tog_tree_view_state *s = &view->state.tree;
6700 struct tog_view *log_view, *ref_view;
6701 struct got_tree_entry *te;
6702 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6704 switch (ch) {
6705 case 'i':
6706 s->show_ids = !s->show_ids;
6707 view->count = 0;
6708 break;
6709 case 'l':
6710 view->count = 0;
6711 if (!s->selected_entry)
6712 break;
6713 if (view_is_parent_view(view))
6714 view_get_split(view, &begin_y, &begin_x);
6715 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6716 if (view_is_parent_view(view) &&
6717 view->mode == TOG_VIEW_SPLIT_HRZN) {
6718 err = view_init_hsplit(view, begin_y);
6719 if (err)
6720 break;
6722 view->focussed = 0;
6723 log_view->focussed = 1;
6724 log_view->mode = view->mode;
6725 log_view->nlines = view->lines - begin_y;
6726 if (view_is_parent_view(view)) {
6727 view_transfer_size(log_view, view);
6728 err = view_close_child(view);
6729 if (err)
6730 return err;
6731 err = view_set_child(view, log_view);
6732 if (err)
6733 return err;
6734 view->focus_child = 1;
6735 } else
6736 *new_view = log_view;
6737 break;
6738 case 'r':
6739 view->count = 0;
6740 if (view_is_parent_view(view))
6741 view_get_split(view, &begin_y, &begin_x);
6742 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6743 if (ref_view == NULL)
6744 return got_error_from_errno("view_open");
6745 err = open_ref_view(ref_view, s->repo);
6746 if (err) {
6747 view_close(ref_view);
6748 return err;
6750 if (view_is_parent_view(view) &&
6751 view->mode == TOG_VIEW_SPLIT_HRZN) {
6752 err = view_init_hsplit(view, begin_y);
6753 if (err)
6754 break;
6756 view->focussed = 0;
6757 ref_view->focussed = 1;
6758 ref_view->mode = view->mode;
6759 ref_view->nlines = view->lines - begin_y;
6760 if (view_is_parent_view(view)) {
6761 view_transfer_size(ref_view, view);
6762 err = view_close_child(view);
6763 if (err)
6764 return err;
6765 err = view_set_child(view, ref_view);
6766 if (err)
6767 return err;
6768 view->focus_child = 1;
6769 } else
6770 *new_view = ref_view;
6771 break;
6772 case 'g':
6773 case KEY_HOME:
6774 s->selected = 0;
6775 view->count = 0;
6776 if (s->tree == s->root)
6777 s->first_displayed_entry =
6778 got_object_tree_get_first_entry(s->tree);
6779 else
6780 s->first_displayed_entry = NULL;
6781 break;
6782 case 'G':
6783 case KEY_END: {
6784 int eos = view->nlines - 3;
6786 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6787 --eos; /* border */
6788 s->selected = 0;
6789 view->count = 0;
6790 te = got_object_tree_get_last_entry(s->tree);
6791 for (n = 0; n < eos; n++) {
6792 if (te == NULL) {
6793 if(s->tree != s->root) {
6794 s->first_displayed_entry = NULL;
6795 n++;
6797 break;
6799 s->first_displayed_entry = te;
6800 te = got_tree_entry_get_prev(s->tree, te);
6802 if (n > 0)
6803 s->selected = n - 1;
6804 break;
6806 case 'k':
6807 case KEY_UP:
6808 case CTRL('p'):
6809 if (s->selected > 0) {
6810 s->selected--;
6811 break;
6813 tree_scroll_up(s, 1);
6814 if (s->selected_entry == NULL ||
6815 (s->tree == s->root && s->selected_entry ==
6816 got_object_tree_get_first_entry(s->tree)))
6817 view->count = 0;
6818 break;
6819 case CTRL('u'):
6820 case 'u':
6821 nscroll /= 2;
6822 /* FALL THROUGH */
6823 case KEY_PPAGE:
6824 case CTRL('b'):
6825 case 'b':
6826 if (s->tree == s->root) {
6827 if (got_object_tree_get_first_entry(s->tree) ==
6828 s->first_displayed_entry)
6829 s->selected -= MIN(s->selected, nscroll);
6830 } else {
6831 if (s->first_displayed_entry == NULL)
6832 s->selected -= MIN(s->selected, nscroll);
6834 tree_scroll_up(s, MAX(0, nscroll));
6835 if (s->selected_entry == NULL ||
6836 (s->tree == s->root && s->selected_entry ==
6837 got_object_tree_get_first_entry(s->tree)))
6838 view->count = 0;
6839 break;
6840 case 'j':
6841 case KEY_DOWN:
6842 case CTRL('n'):
6843 if (s->selected < s->ndisplayed - 1) {
6844 s->selected++;
6845 break;
6847 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6848 == NULL) {
6849 /* can't scroll any further */
6850 view->count = 0;
6851 break;
6853 tree_scroll_down(view, 1);
6854 break;
6855 case CTRL('d'):
6856 case 'd':
6857 nscroll /= 2;
6858 /* FALL THROUGH */
6859 case KEY_NPAGE:
6860 case CTRL('f'):
6861 case 'f':
6862 case ' ':
6863 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6864 == NULL) {
6865 /* can't scroll any further; move cursor down */
6866 if (s->selected < s->ndisplayed - 1)
6867 s->selected += MIN(nscroll,
6868 s->ndisplayed - s->selected - 1);
6869 else
6870 view->count = 0;
6871 break;
6873 tree_scroll_down(view, nscroll);
6874 break;
6875 case KEY_ENTER:
6876 case '\r':
6877 case KEY_BACKSPACE:
6878 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6879 struct tog_parent_tree *parent;
6880 /* user selected '..' */
6881 if (s->tree == s->root) {
6882 view->count = 0;
6883 break;
6885 parent = TAILQ_FIRST(&s->parents);
6886 TAILQ_REMOVE(&s->parents, parent,
6887 entry);
6888 got_object_tree_close(s->tree);
6889 s->tree = parent->tree;
6890 s->first_displayed_entry =
6891 parent->first_displayed_entry;
6892 s->selected_entry =
6893 parent->selected_entry;
6894 s->selected = parent->selected;
6895 if (s->selected > view->nlines - 3) {
6896 err = offset_selection_down(view);
6897 if (err)
6898 break;
6900 free(parent);
6901 } else if (S_ISDIR(got_tree_entry_get_mode(
6902 s->selected_entry))) {
6903 struct got_tree_object *subtree;
6904 view->count = 0;
6905 err = got_object_open_as_tree(&subtree, s->repo,
6906 got_tree_entry_get_id(s->selected_entry));
6907 if (err)
6908 break;
6909 err = tree_view_visit_subtree(s, subtree);
6910 if (err) {
6911 got_object_tree_close(subtree);
6912 break;
6914 } else if (S_ISREG(got_tree_entry_get_mode(
6915 s->selected_entry))) {
6916 struct tog_view *blame_view;
6917 int begin_x = 0, begin_y = 0;
6919 if (view_is_parent_view(view))
6920 view_get_split(view, &begin_y, &begin_x);
6922 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6923 s->selected_entry, &s->parents,
6924 s->commit_id, s->repo);
6925 if (err)
6926 break;
6928 if (view_is_parent_view(view) &&
6929 view->mode == TOG_VIEW_SPLIT_HRZN) {
6930 err = view_init_hsplit(view, begin_y);
6931 if (err)
6932 break;
6935 view->count = 0;
6936 view->focussed = 0;
6937 blame_view->focussed = 1;
6938 blame_view->mode = view->mode;
6939 blame_view->nlines = view->lines - begin_y;
6940 if (view_is_parent_view(view)) {
6941 view_transfer_size(blame_view, view);
6942 err = view_close_child(view);
6943 if (err)
6944 return err;
6945 err = view_set_child(view, blame_view);
6946 if (err)
6947 return err;
6948 view->focus_child = 1;
6949 } else
6950 *new_view = blame_view;
6952 break;
6953 case KEY_RESIZE:
6954 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6955 s->selected = view->nlines - 4;
6956 view->count = 0;
6957 break;
6958 default:
6959 view->count = 0;
6960 break;
6963 return err;
6966 __dead static void
6967 usage_tree(void)
6969 endwin();
6970 fprintf(stderr,
6971 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6972 getprogname());
6973 exit(1);
6976 static const struct got_error *
6977 cmd_tree(int argc, char *argv[])
6979 const struct got_error *error;
6980 struct got_repository *repo = NULL;
6981 struct got_worktree *worktree = NULL;
6982 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6983 struct got_object_id *commit_id = NULL;
6984 struct got_commit_object *commit = NULL;
6985 const char *commit_id_arg = NULL;
6986 char *label = NULL;
6987 struct got_reference *ref = NULL;
6988 const char *head_ref_name = NULL;
6989 int ch;
6990 struct tog_view *view;
6991 int *pack_fds = NULL;
6993 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6994 switch (ch) {
6995 case 'c':
6996 commit_id_arg = optarg;
6997 break;
6998 case 'r':
6999 repo_path = realpath(optarg, NULL);
7000 if (repo_path == NULL)
7001 return got_error_from_errno2("realpath",
7002 optarg);
7003 break;
7004 default:
7005 usage_tree();
7006 /* NOTREACHED */
7010 argc -= optind;
7011 argv += optind;
7013 if (argc > 1)
7014 usage_tree();
7016 error = got_repo_pack_fds_open(&pack_fds);
7017 if (error != NULL)
7018 goto done;
7020 if (repo_path == NULL) {
7021 cwd = getcwd(NULL, 0);
7022 if (cwd == NULL)
7023 return got_error_from_errno("getcwd");
7024 error = got_worktree_open(&worktree, cwd);
7025 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7026 goto done;
7027 if (worktree)
7028 repo_path =
7029 strdup(got_worktree_get_repo_path(worktree));
7030 else
7031 repo_path = strdup(cwd);
7032 if (repo_path == NULL) {
7033 error = got_error_from_errno("strdup");
7034 goto done;
7038 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7039 if (error != NULL)
7040 goto done;
7042 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7043 repo, worktree);
7044 if (error)
7045 goto done;
7047 init_curses();
7049 error = apply_unveil(got_repo_get_path(repo), NULL);
7050 if (error)
7051 goto done;
7053 error = tog_load_refs(repo, 0);
7054 if (error)
7055 goto done;
7057 if (commit_id_arg == NULL) {
7058 error = got_repo_match_object_id(&commit_id, &label,
7059 worktree ? got_worktree_get_head_ref_name(worktree) :
7060 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7061 if (error)
7062 goto done;
7063 head_ref_name = label;
7064 } else {
7065 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7066 if (error == NULL)
7067 head_ref_name = got_ref_get_name(ref);
7068 else if (error->code != GOT_ERR_NOT_REF)
7069 goto done;
7070 error = got_repo_match_object_id(&commit_id, NULL,
7071 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7072 if (error)
7073 goto done;
7076 error = got_object_open_as_commit(&commit, repo, commit_id);
7077 if (error)
7078 goto done;
7080 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7081 if (view == NULL) {
7082 error = got_error_from_errno("view_open");
7083 goto done;
7085 error = open_tree_view(view, commit_id, head_ref_name, repo);
7086 if (error)
7087 goto done;
7088 if (!got_path_is_root_dir(in_repo_path)) {
7089 error = tree_view_walk_path(&view->state.tree, commit,
7090 in_repo_path);
7091 if (error)
7092 goto done;
7095 if (worktree) {
7096 /* Release work tree lock. */
7097 got_worktree_close(worktree);
7098 worktree = NULL;
7100 error = view_loop(view);
7101 done:
7102 free(repo_path);
7103 free(cwd);
7104 free(commit_id);
7105 free(label);
7106 if (ref)
7107 got_ref_close(ref);
7108 if (repo) {
7109 const struct got_error *close_err = got_repo_close(repo);
7110 if (error == NULL)
7111 error = close_err;
7113 if (pack_fds) {
7114 const struct got_error *pack_err =
7115 got_repo_pack_fds_close(pack_fds);
7116 if (error == NULL)
7117 error = pack_err;
7119 tog_free_refs();
7120 return error;
7123 static const struct got_error *
7124 ref_view_load_refs(struct tog_ref_view_state *s)
7126 struct got_reflist_entry *sre;
7127 struct tog_reflist_entry *re;
7129 s->nrefs = 0;
7130 TAILQ_FOREACH(sre, &tog_refs, entry) {
7131 if (strncmp(got_ref_get_name(sre->ref),
7132 "refs/got/", 9) == 0 &&
7133 strncmp(got_ref_get_name(sre->ref),
7134 "refs/got/backup/", 16) != 0)
7135 continue;
7137 re = malloc(sizeof(*re));
7138 if (re == NULL)
7139 return got_error_from_errno("malloc");
7141 re->ref = got_ref_dup(sre->ref);
7142 if (re->ref == NULL)
7143 return got_error_from_errno("got_ref_dup");
7144 re->idx = s->nrefs++;
7145 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7148 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7149 return NULL;
7152 static void
7153 ref_view_free_refs(struct tog_ref_view_state *s)
7155 struct tog_reflist_entry *re;
7157 while (!TAILQ_EMPTY(&s->refs)) {
7158 re = TAILQ_FIRST(&s->refs);
7159 TAILQ_REMOVE(&s->refs, re, entry);
7160 got_ref_close(re->ref);
7161 free(re);
7165 static const struct got_error *
7166 open_ref_view(struct tog_view *view, struct got_repository *repo)
7168 const struct got_error *err = NULL;
7169 struct tog_ref_view_state *s = &view->state.ref;
7171 s->selected_entry = 0;
7172 s->repo = repo;
7174 TAILQ_INIT(&s->refs);
7175 STAILQ_INIT(&s->colors);
7177 err = ref_view_load_refs(s);
7178 if (err)
7179 return err;
7181 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7182 err = add_color(&s->colors, "^refs/heads/",
7183 TOG_COLOR_REFS_HEADS,
7184 get_color_value("TOG_COLOR_REFS_HEADS"));
7185 if (err)
7186 goto done;
7188 err = add_color(&s->colors, "^refs/tags/",
7189 TOG_COLOR_REFS_TAGS,
7190 get_color_value("TOG_COLOR_REFS_TAGS"));
7191 if (err)
7192 goto done;
7194 err = add_color(&s->colors, "^refs/remotes/",
7195 TOG_COLOR_REFS_REMOTES,
7196 get_color_value("TOG_COLOR_REFS_REMOTES"));
7197 if (err)
7198 goto done;
7200 err = add_color(&s->colors, "^refs/got/backup/",
7201 TOG_COLOR_REFS_BACKUP,
7202 get_color_value("TOG_COLOR_REFS_BACKUP"));
7203 if (err)
7204 goto done;
7207 view->show = show_ref_view;
7208 view->input = input_ref_view;
7209 view->close = close_ref_view;
7210 view->search_start = search_start_ref_view;
7211 view->search_next = search_next_ref_view;
7212 done:
7213 if (err)
7214 free_colors(&s->colors);
7215 return err;
7218 static const struct got_error *
7219 close_ref_view(struct tog_view *view)
7221 struct tog_ref_view_state *s = &view->state.ref;
7223 ref_view_free_refs(s);
7224 free_colors(&s->colors);
7226 return NULL;
7229 static const struct got_error *
7230 resolve_reflist_entry(struct got_object_id **commit_id,
7231 struct tog_reflist_entry *re, struct got_repository *repo)
7233 const struct got_error *err = NULL;
7234 struct got_object_id *obj_id;
7235 struct got_tag_object *tag = NULL;
7236 int obj_type;
7238 *commit_id = NULL;
7240 err = got_ref_resolve(&obj_id, repo, re->ref);
7241 if (err)
7242 return err;
7244 err = got_object_get_type(&obj_type, repo, obj_id);
7245 if (err)
7246 goto done;
7248 switch (obj_type) {
7249 case GOT_OBJ_TYPE_COMMIT:
7250 *commit_id = obj_id;
7251 break;
7252 case GOT_OBJ_TYPE_TAG:
7253 err = got_object_open_as_tag(&tag, repo, obj_id);
7254 if (err)
7255 goto done;
7256 free(obj_id);
7257 err = got_object_get_type(&obj_type, repo,
7258 got_object_tag_get_object_id(tag));
7259 if (err)
7260 goto done;
7261 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7262 err = got_error(GOT_ERR_OBJ_TYPE);
7263 goto done;
7265 *commit_id = got_object_id_dup(
7266 got_object_tag_get_object_id(tag));
7267 if (*commit_id == NULL) {
7268 err = got_error_from_errno("got_object_id_dup");
7269 goto done;
7271 break;
7272 default:
7273 err = got_error(GOT_ERR_OBJ_TYPE);
7274 break;
7277 done:
7278 if (tag)
7279 got_object_tag_close(tag);
7280 if (err) {
7281 free(*commit_id);
7282 *commit_id = NULL;
7284 return err;
7287 static const struct got_error *
7288 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7289 struct tog_reflist_entry *re, struct got_repository *repo)
7291 struct tog_view *log_view;
7292 const struct got_error *err = NULL;
7293 struct got_object_id *commit_id = NULL;
7295 *new_view = NULL;
7297 err = resolve_reflist_entry(&commit_id, re, repo);
7298 if (err) {
7299 if (err->code != GOT_ERR_OBJ_TYPE)
7300 return err;
7301 else
7302 return NULL;
7305 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7306 if (log_view == NULL) {
7307 err = got_error_from_errno("view_open");
7308 goto done;
7311 err = open_log_view(log_view, commit_id, repo,
7312 got_ref_get_name(re->ref), "", 0);
7313 done:
7314 if (err)
7315 view_close(log_view);
7316 else
7317 *new_view = log_view;
7318 free(commit_id);
7319 return err;
7322 static void
7323 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7325 struct tog_reflist_entry *re;
7326 int i = 0;
7328 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7329 return;
7331 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7332 while (i++ < maxscroll) {
7333 if (re == NULL)
7334 break;
7335 s->first_displayed_entry = re;
7336 re = TAILQ_PREV(re, tog_reflist_head, entry);
7340 static const struct got_error *
7341 ref_scroll_down(struct tog_view *view, int maxscroll)
7343 struct tog_ref_view_state *s = &view->state.ref;
7344 struct tog_reflist_entry *next, *last;
7345 int n = 0;
7347 if (s->first_displayed_entry)
7348 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7349 else
7350 next = TAILQ_FIRST(&s->refs);
7352 last = s->last_displayed_entry;
7353 while (next && n++ < maxscroll) {
7354 if (last)
7355 last = TAILQ_NEXT(last, entry);
7356 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7357 s->first_displayed_entry = next;
7358 next = TAILQ_NEXT(next, entry);
7362 return NULL;
7365 static const struct got_error *
7366 search_start_ref_view(struct tog_view *view)
7368 struct tog_ref_view_state *s = &view->state.ref;
7370 s->matched_entry = NULL;
7371 return NULL;
7374 static int
7375 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7377 regmatch_t regmatch;
7379 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7380 0) == 0;
7383 static const struct got_error *
7384 search_next_ref_view(struct tog_view *view)
7386 struct tog_ref_view_state *s = &view->state.ref;
7387 struct tog_reflist_entry *re = NULL;
7389 if (!view->searching) {
7390 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7391 return NULL;
7394 if (s->matched_entry) {
7395 if (view->searching == TOG_SEARCH_FORWARD) {
7396 if (s->selected_entry)
7397 re = TAILQ_NEXT(s->selected_entry, entry);
7398 else
7399 re = TAILQ_PREV(s->selected_entry,
7400 tog_reflist_head, entry);
7401 } else {
7402 if (s->selected_entry == NULL)
7403 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7404 else
7405 re = TAILQ_PREV(s->selected_entry,
7406 tog_reflist_head, entry);
7408 } else {
7409 if (s->selected_entry)
7410 re = s->selected_entry;
7411 else if (view->searching == TOG_SEARCH_FORWARD)
7412 re = TAILQ_FIRST(&s->refs);
7413 else
7414 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7417 while (1) {
7418 if (re == NULL) {
7419 if (s->matched_entry == NULL) {
7420 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7421 return NULL;
7423 if (view->searching == TOG_SEARCH_FORWARD)
7424 re = TAILQ_FIRST(&s->refs);
7425 else
7426 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7429 if (match_reflist_entry(re, &view->regex)) {
7430 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7431 s->matched_entry = re;
7432 break;
7435 if (view->searching == TOG_SEARCH_FORWARD)
7436 re = TAILQ_NEXT(re, entry);
7437 else
7438 re = TAILQ_PREV(re, tog_reflist_head, entry);
7441 if (s->matched_entry) {
7442 s->first_displayed_entry = s->matched_entry;
7443 s->selected = 0;
7446 return NULL;
7449 static const struct got_error *
7450 show_ref_view(struct tog_view *view)
7452 const struct got_error *err = NULL;
7453 struct tog_ref_view_state *s = &view->state.ref;
7454 struct tog_reflist_entry *re;
7455 char *line = NULL;
7456 wchar_t *wline;
7457 struct tog_color *tc;
7458 int width, n;
7459 int limit = view->nlines;
7461 werase(view->window);
7463 s->ndisplayed = 0;
7464 if (view_is_hsplit_top(view))
7465 --limit; /* border */
7467 if (limit == 0)
7468 return NULL;
7470 re = s->first_displayed_entry;
7472 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7473 s->nrefs) == -1)
7474 return got_error_from_errno("asprintf");
7476 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7477 if (err) {
7478 free(line);
7479 return err;
7481 if (view_needs_focus_indication(view))
7482 wstandout(view->window);
7483 waddwstr(view->window, wline);
7484 if (view_needs_focus_indication(view))
7485 wstandend(view->window);
7486 free(wline);
7487 wline = NULL;
7488 free(line);
7489 line = NULL;
7490 if (width < view->ncols - 1)
7491 waddch(view->window, '\n');
7492 if (--limit <= 0)
7493 return NULL;
7495 n = 0;
7496 while (re && limit > 0) {
7497 char *line = NULL;
7498 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7500 if (s->show_date) {
7501 struct got_commit_object *ci;
7502 struct got_tag_object *tag;
7503 struct got_object_id *id;
7504 struct tm tm;
7505 time_t t;
7507 err = got_ref_resolve(&id, s->repo, re->ref);
7508 if (err)
7509 return err;
7510 err = got_object_open_as_tag(&tag, s->repo, id);
7511 if (err) {
7512 if (err->code != GOT_ERR_OBJ_TYPE) {
7513 free(id);
7514 return err;
7516 err = got_object_open_as_commit(&ci, s->repo,
7517 id);
7518 if (err) {
7519 free(id);
7520 return err;
7522 t = got_object_commit_get_committer_time(ci);
7523 got_object_commit_close(ci);
7524 } else {
7525 t = got_object_tag_get_tagger_time(tag);
7526 got_object_tag_close(tag);
7528 free(id);
7529 if (gmtime_r(&t, &tm) == NULL)
7530 return got_error_from_errno("gmtime_r");
7531 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7532 return got_error(GOT_ERR_NO_SPACE);
7534 if (got_ref_is_symbolic(re->ref)) {
7535 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7536 ymd : "", got_ref_get_name(re->ref),
7537 got_ref_get_symref_target(re->ref)) == -1)
7538 return got_error_from_errno("asprintf");
7539 } else if (s->show_ids) {
7540 struct got_object_id *id;
7541 char *id_str;
7542 err = got_ref_resolve(&id, s->repo, re->ref);
7543 if (err)
7544 return err;
7545 err = got_object_id_str(&id_str, id);
7546 if (err) {
7547 free(id);
7548 return err;
7550 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7551 got_ref_get_name(re->ref), id_str) == -1) {
7552 err = got_error_from_errno("asprintf");
7553 free(id);
7554 free(id_str);
7555 return err;
7557 free(id);
7558 free(id_str);
7559 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7560 got_ref_get_name(re->ref)) == -1)
7561 return got_error_from_errno("asprintf");
7563 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7564 0, 0);
7565 if (err) {
7566 free(line);
7567 return err;
7569 if (n == s->selected) {
7570 if (view->focussed)
7571 wstandout(view->window);
7572 s->selected_entry = re;
7574 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7575 if (tc)
7576 wattr_on(view->window,
7577 COLOR_PAIR(tc->colorpair), NULL);
7578 waddwstr(view->window, wline);
7579 if (tc)
7580 wattr_off(view->window,
7581 COLOR_PAIR(tc->colorpair), NULL);
7582 if (width < view->ncols - 1)
7583 waddch(view->window, '\n');
7584 if (n == s->selected && view->focussed)
7585 wstandend(view->window);
7586 free(line);
7587 free(wline);
7588 wline = NULL;
7589 n++;
7590 s->ndisplayed++;
7591 s->last_displayed_entry = re;
7593 limit--;
7594 re = TAILQ_NEXT(re, entry);
7597 view_border(view);
7598 return err;
7601 static const struct got_error *
7602 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7603 struct tog_reflist_entry *re, struct got_repository *repo)
7605 const struct got_error *err = NULL;
7606 struct got_object_id *commit_id = NULL;
7607 struct tog_view *tree_view;
7609 *new_view = NULL;
7611 err = resolve_reflist_entry(&commit_id, re, repo);
7612 if (err) {
7613 if (err->code != GOT_ERR_OBJ_TYPE)
7614 return err;
7615 else
7616 return NULL;
7620 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7621 if (tree_view == NULL) {
7622 err = got_error_from_errno("view_open");
7623 goto done;
7626 err = open_tree_view(tree_view, commit_id,
7627 got_ref_get_name(re->ref), repo);
7628 if (err)
7629 goto done;
7631 *new_view = tree_view;
7632 done:
7633 free(commit_id);
7634 return err;
7636 static const struct got_error *
7637 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7639 const struct got_error *err = NULL;
7640 struct tog_ref_view_state *s = &view->state.ref;
7641 struct tog_view *log_view, *tree_view;
7642 struct tog_reflist_entry *re;
7643 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7645 switch (ch) {
7646 case 'i':
7647 s->show_ids = !s->show_ids;
7648 view->count = 0;
7649 break;
7650 case 'm':
7651 s->show_date = !s->show_date;
7652 view->count = 0;
7653 break;
7654 case 'o':
7655 s->sort_by_date = !s->sort_by_date;
7656 view->count = 0;
7657 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7658 got_ref_cmp_by_commit_timestamp_descending :
7659 tog_ref_cmp_by_name, s->repo);
7660 if (err)
7661 break;
7662 got_reflist_object_id_map_free(tog_refs_idmap);
7663 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7664 &tog_refs, s->repo);
7665 if (err)
7666 break;
7667 ref_view_free_refs(s);
7668 err = ref_view_load_refs(s);
7669 break;
7670 case KEY_ENTER:
7671 case '\r':
7672 view->count = 0;
7673 if (!s->selected_entry)
7674 break;
7675 if (view_is_parent_view(view))
7676 view_get_split(view, &begin_y, &begin_x);
7678 err = log_ref_entry(&log_view, begin_y, begin_x,
7679 s->selected_entry, s->repo);
7680 if (err)
7681 break;
7683 if (view_is_parent_view(view) &&
7684 view->mode == TOG_VIEW_SPLIT_HRZN) {
7685 err = view_init_hsplit(view, begin_y);
7686 if (err)
7687 break;
7690 view->focussed = 0;
7691 log_view->focussed = 1;
7692 log_view->mode = view->mode;
7693 log_view->nlines = view->lines - begin_y;
7694 if (view_is_parent_view(view)) {
7695 view_transfer_size(log_view, view);
7696 err = view_close_child(view);
7697 if (err)
7698 return err;
7699 err = view_set_child(view, log_view);
7700 if (err)
7701 return err;
7702 view->focus_child = 1;
7703 } else
7704 *new_view = log_view;
7705 break;
7706 case 't':
7707 view->count = 0;
7708 if (!s->selected_entry)
7709 break;
7710 if (view_is_parent_view(view))
7711 view_get_split(view, &begin_y, &begin_x);
7712 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7713 s->selected_entry, s->repo);
7714 if (err || tree_view == NULL)
7715 break;
7716 if (view_is_parent_view(view) &&
7717 view->mode == TOG_VIEW_SPLIT_HRZN) {
7718 err = view_init_hsplit(view, begin_y);
7719 if (err)
7720 break;
7722 view->focussed = 0;
7723 tree_view->focussed = 1;
7724 tree_view->mode = view->mode;
7725 tree_view->nlines = view->lines - begin_y;
7726 if (view_is_parent_view(view)) {
7727 view_transfer_size(tree_view, view);
7728 err = view_close_child(view);
7729 if (err)
7730 return err;
7731 err = view_set_child(view, tree_view);
7732 if (err)
7733 return err;
7734 view->focus_child = 1;
7735 } else
7736 *new_view = tree_view;
7737 break;
7738 case 'g':
7739 case KEY_HOME:
7740 s->selected = 0;
7741 view->count = 0;
7742 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7743 break;
7744 case 'G':
7745 case KEY_END: {
7746 int eos = view->nlines - 1;
7748 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7749 --eos; /* border */
7750 s->selected = 0;
7751 view->count = 0;
7752 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7753 for (n = 0; n < eos; n++) {
7754 if (re == NULL)
7755 break;
7756 s->first_displayed_entry = re;
7757 re = TAILQ_PREV(re, tog_reflist_head, entry);
7759 if (n > 0)
7760 s->selected = n - 1;
7761 break;
7763 case 'k':
7764 case KEY_UP:
7765 case CTRL('p'):
7766 if (s->selected > 0) {
7767 s->selected--;
7768 break;
7770 ref_scroll_up(s, 1);
7771 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7772 view->count = 0;
7773 break;
7774 case CTRL('u'):
7775 case 'u':
7776 nscroll /= 2;
7777 /* FALL THROUGH */
7778 case KEY_PPAGE:
7779 case CTRL('b'):
7780 case 'b':
7781 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7782 s->selected -= MIN(nscroll, s->selected);
7783 ref_scroll_up(s, MAX(0, nscroll));
7784 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7785 view->count = 0;
7786 break;
7787 case 'j':
7788 case KEY_DOWN:
7789 case CTRL('n'):
7790 if (s->selected < s->ndisplayed - 1) {
7791 s->selected++;
7792 break;
7794 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7795 /* can't scroll any further */
7796 view->count = 0;
7797 break;
7799 ref_scroll_down(view, 1);
7800 break;
7801 case CTRL('d'):
7802 case 'd':
7803 nscroll /= 2;
7804 /* FALL THROUGH */
7805 case KEY_NPAGE:
7806 case CTRL('f'):
7807 case 'f':
7808 case ' ':
7809 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7810 /* can't scroll any further; move cursor down */
7811 if (s->selected < s->ndisplayed - 1)
7812 s->selected += MIN(nscroll,
7813 s->ndisplayed - s->selected - 1);
7814 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7815 s->selected += s->ndisplayed - s->selected - 1;
7816 view->count = 0;
7817 break;
7819 ref_scroll_down(view, nscroll);
7820 break;
7821 case CTRL('l'):
7822 view->count = 0;
7823 tog_free_refs();
7824 err = tog_load_refs(s->repo, s->sort_by_date);
7825 if (err)
7826 break;
7827 ref_view_free_refs(s);
7828 err = ref_view_load_refs(s);
7829 break;
7830 case KEY_RESIZE:
7831 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7832 s->selected = view->nlines - 2;
7833 break;
7834 default:
7835 view->count = 0;
7836 break;
7839 return err;
7842 __dead static void
7843 usage_ref(void)
7845 endwin();
7846 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7847 getprogname());
7848 exit(1);
7851 static const struct got_error *
7852 cmd_ref(int argc, char *argv[])
7854 const struct got_error *error;
7855 struct got_repository *repo = NULL;
7856 struct got_worktree *worktree = NULL;
7857 char *cwd = NULL, *repo_path = NULL;
7858 int ch;
7859 struct tog_view *view;
7860 int *pack_fds = NULL;
7862 while ((ch = getopt(argc, argv, "r:")) != -1) {
7863 switch (ch) {
7864 case 'r':
7865 repo_path = realpath(optarg, NULL);
7866 if (repo_path == NULL)
7867 return got_error_from_errno2("realpath",
7868 optarg);
7869 break;
7870 default:
7871 usage_ref();
7872 /* NOTREACHED */
7876 argc -= optind;
7877 argv += optind;
7879 if (argc > 1)
7880 usage_ref();
7882 error = got_repo_pack_fds_open(&pack_fds);
7883 if (error != NULL)
7884 goto done;
7886 if (repo_path == NULL) {
7887 cwd = getcwd(NULL, 0);
7888 if (cwd == NULL)
7889 return got_error_from_errno("getcwd");
7890 error = got_worktree_open(&worktree, cwd);
7891 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7892 goto done;
7893 if (worktree)
7894 repo_path =
7895 strdup(got_worktree_get_repo_path(worktree));
7896 else
7897 repo_path = strdup(cwd);
7898 if (repo_path == NULL) {
7899 error = got_error_from_errno("strdup");
7900 goto done;
7904 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7905 if (error != NULL)
7906 goto done;
7908 init_curses();
7910 error = apply_unveil(got_repo_get_path(repo), NULL);
7911 if (error)
7912 goto done;
7914 error = tog_load_refs(repo, 0);
7915 if (error)
7916 goto done;
7918 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7919 if (view == NULL) {
7920 error = got_error_from_errno("view_open");
7921 goto done;
7924 error = open_ref_view(view, repo);
7925 if (error)
7926 goto done;
7928 if (worktree) {
7929 /* Release work tree lock. */
7930 got_worktree_close(worktree);
7931 worktree = NULL;
7933 error = view_loop(view);
7934 done:
7935 free(repo_path);
7936 free(cwd);
7937 if (repo) {
7938 const struct got_error *close_err = got_repo_close(repo);
7939 if (close_err)
7940 error = close_err;
7942 if (pack_fds) {
7943 const struct got_error *pack_err =
7944 got_repo_pack_fds_close(pack_fds);
7945 if (error == NULL)
7946 error = pack_err;
7948 tog_free_refs();
7949 return error;
7953 * If view was scrolled down to move the selected line into view when opening a
7954 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7956 static void
7957 offset_selection_up(struct tog_view *view)
7959 switch (view->type) {
7960 case TOG_VIEW_BLAME: {
7961 struct tog_blame_view_state *s = &view->state.blame;
7962 if (s->first_displayed_line == 1) {
7963 s->selected_line = MAX(s->selected_line - view->offset,
7964 1);
7965 break;
7967 if (s->first_displayed_line > view->offset)
7968 s->first_displayed_line -= view->offset;
7969 else
7970 s->first_displayed_line = 1;
7971 s->selected_line += view->offset;
7972 break;
7974 case TOG_VIEW_LOG:
7975 log_scroll_up(&view->state.log, view->offset);
7976 view->state.log.selected += view->offset;
7977 break;
7978 case TOG_VIEW_REF:
7979 ref_scroll_up(&view->state.ref, view->offset);
7980 view->state.ref.selected += view->offset;
7981 break;
7982 case TOG_VIEW_TREE:
7983 tree_scroll_up(&view->state.tree, view->offset);
7984 view->state.tree.selected += view->offset;
7985 break;
7986 default:
7987 break;
7990 view->offset = 0;
7994 * If the selected line is in the section of screen covered by the bottom split,
7995 * scroll down offset lines to move it into view and index its new position.
7997 static const struct got_error *
7998 offset_selection_down(struct tog_view *view)
8000 const struct got_error *err = NULL;
8001 const struct got_error *(*scrolld)(struct tog_view *, int);
8002 int *selected = NULL;
8003 int header, offset;
8005 switch (view->type) {
8006 case TOG_VIEW_BLAME: {
8007 struct tog_blame_view_state *s = &view->state.blame;
8008 header = 3;
8009 scrolld = NULL;
8010 if (s->selected_line > view->nlines - header) {
8011 offset = abs(view->nlines - s->selected_line - header);
8012 s->first_displayed_line += offset;
8013 s->selected_line -= offset;
8014 view->offset = offset;
8016 break;
8018 case TOG_VIEW_LOG: {
8019 struct tog_log_view_state *s = &view->state.log;
8020 scrolld = &log_scroll_down;
8021 header = view_is_parent_view(view) ? 3 : 2;
8022 selected = &s->selected;
8023 break;
8025 case TOG_VIEW_REF: {
8026 struct tog_ref_view_state *s = &view->state.ref;
8027 scrolld = &ref_scroll_down;
8028 header = 3;
8029 selected = &s->selected;
8030 break;
8032 case TOG_VIEW_TREE: {
8033 struct tog_tree_view_state *s = &view->state.tree;
8034 scrolld = &tree_scroll_down;
8035 header = 5;
8036 selected = &s->selected;
8037 break;
8039 default:
8040 selected = NULL;
8041 scrolld = NULL;
8042 header = 0;
8043 break;
8046 if (selected && *selected > view->nlines - header) {
8047 offset = abs(view->nlines - *selected - header);
8048 view->offset = offset;
8049 if (scrolld && offset) {
8050 err = scrolld(view, offset);
8051 *selected -= offset;
8055 return err;
8058 static void
8059 list_commands(FILE *fp)
8061 size_t i;
8063 fprintf(fp, "commands:");
8064 for (i = 0; i < nitems(tog_commands); i++) {
8065 const struct tog_cmd *cmd = &tog_commands[i];
8066 fprintf(fp, " %s", cmd->name);
8068 fputc('\n', fp);
8071 __dead static void
8072 usage(int hflag, int status)
8074 FILE *fp = (status == 0) ? stdout : stderr;
8076 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8077 getprogname());
8078 if (hflag) {
8079 fprintf(fp, "lazy usage: %s path\n", getprogname());
8080 list_commands(fp);
8082 exit(status);
8085 static char **
8086 make_argv(int argc, ...)
8088 va_list ap;
8089 char **argv;
8090 int i;
8092 va_start(ap, argc);
8094 argv = calloc(argc, sizeof(char *));
8095 if (argv == NULL)
8096 err(1, "calloc");
8097 for (i = 0; i < argc; i++) {
8098 argv[i] = strdup(va_arg(ap, char *));
8099 if (argv[i] == NULL)
8100 err(1, "strdup");
8103 va_end(ap);
8104 return argv;
8108 * Try to convert 'tog path' into a 'tog log path' command.
8109 * The user could simply have mistyped the command rather than knowingly
8110 * provided a path. So check whether argv[0] can in fact be resolved
8111 * to a path in the HEAD commit and print a special error if not.
8112 * This hack is for mpi@ <3
8114 static const struct got_error *
8115 tog_log_with_path(int argc, char *argv[])
8117 const struct got_error *error = NULL, *close_err;
8118 const struct tog_cmd *cmd = NULL;
8119 struct got_repository *repo = NULL;
8120 struct got_worktree *worktree = NULL;
8121 struct got_object_id *commit_id = NULL, *id = NULL;
8122 struct got_commit_object *commit = NULL;
8123 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8124 char *commit_id_str = NULL, **cmd_argv = NULL;
8125 int *pack_fds = NULL;
8127 cwd = getcwd(NULL, 0);
8128 if (cwd == NULL)
8129 return got_error_from_errno("getcwd");
8131 error = got_repo_pack_fds_open(&pack_fds);
8132 if (error != NULL)
8133 goto done;
8135 error = got_worktree_open(&worktree, cwd);
8136 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8137 goto done;
8139 if (worktree)
8140 repo_path = strdup(got_worktree_get_repo_path(worktree));
8141 else
8142 repo_path = strdup(cwd);
8143 if (repo_path == NULL) {
8144 error = got_error_from_errno("strdup");
8145 goto done;
8148 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8149 if (error != NULL)
8150 goto done;
8152 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8153 repo, worktree);
8154 if (error)
8155 goto done;
8157 error = tog_load_refs(repo, 0);
8158 if (error)
8159 goto done;
8160 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8161 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8162 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8163 if (error)
8164 goto done;
8166 if (worktree) {
8167 got_worktree_close(worktree);
8168 worktree = NULL;
8171 error = got_object_open_as_commit(&commit, repo, commit_id);
8172 if (error)
8173 goto done;
8175 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8176 if (error) {
8177 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8178 goto done;
8179 fprintf(stderr, "%s: '%s' is no known command or path\n",
8180 getprogname(), argv[0]);
8181 usage(1, 1);
8182 /* not reached */
8185 close_err = got_repo_close(repo);
8186 if (error == NULL)
8187 error = close_err;
8188 repo = NULL;
8190 error = got_object_id_str(&commit_id_str, commit_id);
8191 if (error)
8192 goto done;
8194 cmd = &tog_commands[0]; /* log */
8195 argc = 4;
8196 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8197 error = cmd->cmd_main(argc, cmd_argv);
8198 done:
8199 if (repo) {
8200 close_err = got_repo_close(repo);
8201 if (error == NULL)
8202 error = close_err;
8204 if (commit)
8205 got_object_commit_close(commit);
8206 if (worktree)
8207 got_worktree_close(worktree);
8208 if (pack_fds) {
8209 const struct got_error *pack_err =
8210 got_repo_pack_fds_close(pack_fds);
8211 if (error == NULL)
8212 error = pack_err;
8214 free(id);
8215 free(commit_id_str);
8216 free(commit_id);
8217 free(cwd);
8218 free(repo_path);
8219 free(in_repo_path);
8220 if (cmd_argv) {
8221 int i;
8222 for (i = 0; i < argc; i++)
8223 free(cmd_argv[i]);
8224 free(cmd_argv);
8226 tog_free_refs();
8227 return error;
8230 int
8231 main(int argc, char *argv[])
8233 const struct got_error *error = NULL;
8234 const struct tog_cmd *cmd = NULL;
8235 int ch, hflag = 0, Vflag = 0;
8236 char **cmd_argv = NULL;
8237 static const struct option longopts[] = {
8238 { "version", no_argument, NULL, 'V' },
8239 { NULL, 0, NULL, 0}
8241 char *diff_algo_str = NULL;
8243 setlocale(LC_CTYPE, "");
8245 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8246 switch (ch) {
8247 case 'h':
8248 hflag = 1;
8249 break;
8250 case 'V':
8251 Vflag = 1;
8252 break;
8253 default:
8254 usage(hflag, 1);
8255 /* NOTREACHED */
8259 argc -= optind;
8260 argv += optind;
8261 optind = 1;
8262 optreset = 1;
8264 if (Vflag) {
8265 got_version_print_str();
8266 return 0;
8269 #ifndef PROFILE
8270 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8271 NULL) == -1)
8272 err(1, "pledge");
8273 #endif
8275 if (argc == 0) {
8276 if (hflag)
8277 usage(hflag, 0);
8278 /* Build an argument vector which runs a default command. */
8279 cmd = &tog_commands[0];
8280 argc = 1;
8281 cmd_argv = make_argv(argc, cmd->name);
8282 } else {
8283 size_t i;
8285 /* Did the user specify a command? */
8286 for (i = 0; i < nitems(tog_commands); i++) {
8287 if (strncmp(tog_commands[i].name, argv[0],
8288 strlen(argv[0])) == 0) {
8289 cmd = &tog_commands[i];
8290 break;
8295 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8296 if (diff_algo_str) {
8297 if (strcasecmp(diff_algo_str, "patience") == 0)
8298 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8299 if (strcasecmp(diff_algo_str, "myers") == 0)
8300 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8303 if (cmd == NULL) {
8304 if (argc != 1)
8305 usage(0, 1);
8306 /* No command specified; try log with a path */
8307 error = tog_log_with_path(argc, argv);
8308 } else {
8309 if (hflag)
8310 cmd->cmd_usage();
8311 else
8312 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8315 endwin();
8316 putchar('\n');
8317 if (cmd_argv) {
8318 int i;
8319 for (i = 0; i < argc; i++)
8320 free(cmd_argv[i]);
8321 free(cmd_argv);
8324 if (error && error->code != GOT_ERR_CANCELLED)
8325 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8326 return 0;