Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 enum tog_view_mode {
109 TOG_VIEW_SPLIT_NONE,
110 TOG_VIEW_SPLIT_VERT,
111 TOG_VIEW_SPLIT_HRZN
112 };
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
122 int idx;
123 };
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
126 int ncommits;
127 struct commit_queue_head head;
128 };
130 struct tog_color {
131 STAILQ_ENTRY(tog_color) entry;
132 regex_t regex;
133 short colorpair;
134 };
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
153 *cmp = -1;
154 return NULL;
155 } else if (isbackup1 && !isbackup2) {
156 *cmp = 1;
157 return NULL;
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 return NULL;
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 repo);
172 if (err)
173 return err;
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 repo);
179 static void
180 tog_free_refs(void)
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
195 int regerr = 0;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 return NULL;
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
203 if (tc == NULL)
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 if (regerr) {
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
211 sizeof(regerr_msg));
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 regerr_msg);
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 free(tc);
216 return err;
218 tc->colorpair = idx;
219 STAILQ_INSERT_HEAD(colors, tc, entry);
220 return NULL;
223 static void
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
231 regfree(&tc->regex);
232 free(tc);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
243 return tc;
246 return NULL;
249 static int
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 return COLOR_CYAN;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 return COLOR_YELLOW;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 return COLOR_GREEN;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 return COLOR_CYAN;
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 return COLOR_GREEN;
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 return COLOR_CYAN;
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 return COLOR_YELLOW;
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 return COLOR_GREEN;
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 return COLOR_YELLOW;
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 return COLOR_CYAN;
283 return -1;
286 static int
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
291 if (val == NULL)
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
295 return COLOR_BLACK;
296 if (strcasecmp(val, "red") == 0)
297 return COLOR_RED;
298 if (strcasecmp(val, "green") == 0)
299 return COLOR_GREEN;
300 if (strcasecmp(val, "yellow") == 0)
301 return COLOR_YELLOW;
302 if (strcasecmp(val, "blue") == 0)
303 return COLOR_BLUE;
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
307 return COLOR_CYAN;
308 if (strcasecmp(val, "white") == 0)
309 return COLOR_WHITE;
310 if (strcasecmp(val, "default") == 0)
311 return -1;
313 return default_color_value(envvar);
317 struct tog_diff_view_state {
318 struct got_object_id *id1, *id2;
319 const char *label1, *label2;
320 FILE *f, *f1, *f2;
321 int fd1, fd2;
322 int first_displayed_line;
323 int last_displayed_line;
324 int eof;
325 int diff_context;
326 int ignore_whitespace;
327 int force_text_diff;
328 struct got_repository *repo;
329 struct tog_colors colors;
330 size_t nlines;
331 off_t *line_offsets;
332 int matched_line;
333 int selected_line;
335 /* passed from log or blame view; may be NULL */
336 struct tog_view *parent_view;
337 };
339 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
340 static volatile sig_atomic_t tog_thread_error;
342 struct tog_log_thread_args {
343 pthread_cond_t need_commits;
344 pthread_cond_t commit_loaded;
345 int commits_needed;
346 int load_all;
347 struct got_commit_graph *graph;
348 struct commit_queue *commits;
349 const char *in_repo_path;
350 struct got_object_id *start_id;
351 struct got_repository *repo;
352 int *pack_fds;
353 int log_complete;
354 sig_atomic_t *quit;
355 struct commit_queue_entry **first_displayed_entry;
356 struct commit_queue_entry **selected_entry;
357 int *searching;
358 int *search_next_done;
359 regex_t *regex;
360 };
362 struct tog_log_view_state {
363 struct commit_queue commits;
364 struct commit_queue_entry *first_displayed_entry;
365 struct commit_queue_entry *last_displayed_entry;
366 struct commit_queue_entry *selected_entry;
367 int selected;
368 char *in_repo_path;
369 char *head_ref_name;
370 int log_branches;
371 struct got_repository *repo;
372 struct got_object_id *start_id;
373 sig_atomic_t quit;
374 pthread_t thread;
375 struct tog_log_thread_args thread_args;
376 struct commit_queue_entry *matched_entry;
377 struct commit_queue_entry *search_entry;
378 struct tog_colors colors;
379 int use_committer;
380 };
382 #define TOG_COLOR_DIFF_MINUS 1
383 #define TOG_COLOR_DIFF_PLUS 2
384 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
385 #define TOG_COLOR_DIFF_META 4
386 #define TOG_COLOR_TREE_SUBMODULE 5
387 #define TOG_COLOR_TREE_SYMLINK 6
388 #define TOG_COLOR_TREE_DIRECTORY 7
389 #define TOG_COLOR_TREE_EXECUTABLE 8
390 #define TOG_COLOR_COMMIT 9
391 #define TOG_COLOR_AUTHOR 10
392 #define TOG_COLOR_DATE 11
393 #define TOG_COLOR_REFS_HEADS 12
394 #define TOG_COLOR_REFS_TAGS 13
395 #define TOG_COLOR_REFS_REMOTES 14
396 #define TOG_COLOR_REFS_BACKUP 15
398 struct tog_blame_cb_args {
399 struct tog_blame_line *lines; /* one per line */
400 int nlines;
402 struct tog_view *view;
403 struct got_object_id *commit_id;
404 int *quit;
405 };
407 struct tog_blame_thread_args {
408 const char *path;
409 struct got_repository *repo;
410 struct tog_blame_cb_args *cb_args;
411 int *complete;
412 got_cancel_cb cancel_cb;
413 void *cancel_arg;
414 };
416 struct tog_blame {
417 FILE *f;
418 off_t filesize;
419 struct tog_blame_line *lines;
420 int nlines;
421 off_t *line_offsets;
422 pthread_t thread;
423 struct tog_blame_thread_args thread_args;
424 struct tog_blame_cb_args cb_args;
425 const char *path;
426 int *pack_fds;
427 };
429 struct tog_blame_view_state {
430 int first_displayed_line;
431 int last_displayed_line;
432 int selected_line;
433 int last_diffed_line;
434 int blame_complete;
435 int eof;
436 int done;
437 struct got_object_id_queue blamed_commits;
438 struct got_object_qid *blamed_commit;
439 char *path;
440 struct got_repository *repo;
441 struct got_object_id *commit_id;
442 struct tog_blame blame;
443 int matched_line;
444 struct tog_colors colors;
445 };
447 struct tog_parent_tree {
448 TAILQ_ENTRY(tog_parent_tree) entry;
449 struct got_tree_object *tree;
450 struct got_tree_entry *first_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int selected;
453 };
455 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
457 struct tog_tree_view_state {
458 char *tree_label;
459 struct got_object_id *commit_id;/* commit which this tree belongs to */
460 struct got_tree_object *root; /* the commit's root tree entry */
461 struct got_tree_object *tree; /* currently displayed (sub-)tree */
462 struct got_tree_entry *first_displayed_entry;
463 struct got_tree_entry *last_displayed_entry;
464 struct got_tree_entry *selected_entry;
465 int ndisplayed, selected, show_ids;
466 struct tog_parent_trees parents; /* parent trees of current sub-tree */
467 char *head_ref_name;
468 struct got_repository *repo;
469 struct got_tree_entry *matched_entry;
470 struct tog_colors colors;
471 };
473 struct tog_reflist_entry {
474 TAILQ_ENTRY(tog_reflist_entry) entry;
475 struct got_reference *ref;
476 int idx;
477 };
479 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
481 struct tog_ref_view_state {
482 struct tog_reflist_head refs;
483 struct tog_reflist_entry *first_displayed_entry;
484 struct tog_reflist_entry *last_displayed_entry;
485 struct tog_reflist_entry *selected_entry;
486 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
487 struct got_repository *repo;
488 struct tog_reflist_entry *matched_entry;
489 struct tog_colors colors;
490 };
492 /*
493 * We implement two types of views: parent views and child views.
495 * The 'Tab' key switches focus between a parent view and its child view.
496 * Child views are shown side-by-side to their parent view, provided
497 * there is enough screen estate.
499 * When a new view is opened from within a parent view, this new view
500 * becomes a child view of the parent view, replacing any existing child.
502 * When a new view is opened from within a child view, this new view
503 * becomes a parent view which will obscure the views below until the
504 * user quits the new parent view by typing 'q'.
506 * This list of views contains parent views only.
507 * Child views are only pointed to by their parent view.
508 */
509 TAILQ_HEAD(tog_view_list_head, tog_view);
511 struct tog_view {
512 TAILQ_ENTRY(tog_view) entry;
513 WINDOW *window;
514 PANEL *panel;
515 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
516 int resized_y, resized_x; /* begin_y/x based on user resizing */
517 int maxx, x; /* max column and current start column */
518 int lines, cols; /* copies of LINES and COLS */
519 int nscrolled, offset; /* lines scrolled and hsplit line offset */
520 int ch, count; /* current keymap and count prefix */
521 int resized; /* set when in a resize event */
522 int focussed; /* Only set on one parent or child view at a time. */
523 int dying;
524 struct tog_view *parent;
525 struct tog_view *child;
527 /*
528 * This flag is initially set on parent views when a new child view
529 * is created. It gets toggled when the 'Tab' key switches focus
530 * between parent and child.
531 * The flag indicates whether focus should be passed on to our child
532 * view if this parent view gets picked for focus after another parent
533 * view was closed. This prevents child views from losing focus in such
534 * situations.
535 */
536 int focus_child;
538 enum tog_view_mode mode;
539 /* type-specific state */
540 enum tog_view_type type;
541 union {
542 struct tog_diff_view_state diff;
543 struct tog_log_view_state log;
544 struct tog_blame_view_state blame;
545 struct tog_tree_view_state tree;
546 struct tog_ref_view_state ref;
547 } state;
549 const struct got_error *(*show)(struct tog_view *);
550 const struct got_error *(*input)(struct tog_view **,
551 struct tog_view *, int);
552 const struct got_error *(*reset)(struct tog_view *);
553 const struct got_error *(*resize)(struct tog_view *, int);
554 const struct got_error *(*close)(struct tog_view *);
556 const struct got_error *(*search_start)(struct tog_view *);
557 const struct got_error *(*search_next)(struct tog_view *);
558 int search_started;
559 int searching;
560 #define TOG_SEARCH_FORWARD 1
561 #define TOG_SEARCH_BACKWARD 2
562 int search_next_done;
563 #define TOG_SEARCH_HAVE_MORE 1
564 #define TOG_SEARCH_NO_MORE 2
565 #define TOG_SEARCH_HAVE_NONE 3
566 regex_t regex;
567 regmatch_t regmatch;
568 };
570 static const struct got_error *open_diff_view(struct tog_view *,
571 struct got_object_id *, struct got_object_id *,
572 const char *, const char *, int, int, int, struct tog_view *,
573 struct got_repository *);
574 static const struct got_error *show_diff_view(struct tog_view *);
575 static const struct got_error *input_diff_view(struct tog_view **,
576 struct tog_view *, int);
577 static const struct got_error *reset_diff_view(struct tog_view *);
578 static const struct got_error* close_diff_view(struct tog_view *);
579 static const struct got_error *search_start_diff_view(struct tog_view *);
580 static const struct got_error *search_next_diff_view(struct tog_view *);
582 static const struct got_error *open_log_view(struct tog_view *,
583 struct got_object_id *, struct got_repository *,
584 const char *, const char *, int);
585 static const struct got_error * show_log_view(struct tog_view *);
586 static const struct got_error *input_log_view(struct tog_view **,
587 struct tog_view *, int);
588 static const struct got_error *resize_log_view(struct tog_view *, int);
589 static const struct got_error *close_log_view(struct tog_view *);
590 static const struct got_error *search_start_log_view(struct tog_view *);
591 static const struct got_error *search_next_log_view(struct tog_view *);
593 static const struct got_error *open_blame_view(struct tog_view *, char *,
594 struct got_object_id *, struct got_repository *);
595 static const struct got_error *show_blame_view(struct tog_view *);
596 static const struct got_error *input_blame_view(struct tog_view **,
597 struct tog_view *, int);
598 static const struct got_error *reset_blame_view(struct tog_view *);
599 static const struct got_error *close_blame_view(struct tog_view *);
600 static const struct got_error *search_start_blame_view(struct tog_view *);
601 static const struct got_error *search_next_blame_view(struct tog_view *);
603 static const struct got_error *open_tree_view(struct tog_view *,
604 struct got_object_id *, const char *, struct got_repository *);
605 static const struct got_error *show_tree_view(struct tog_view *);
606 static const struct got_error *input_tree_view(struct tog_view **,
607 struct tog_view *, int);
608 static const struct got_error *close_tree_view(struct tog_view *);
609 static const struct got_error *search_start_tree_view(struct tog_view *);
610 static const struct got_error *search_next_tree_view(struct tog_view *);
612 static const struct got_error *open_ref_view(struct tog_view *,
613 struct got_repository *);
614 static const struct got_error *show_ref_view(struct tog_view *);
615 static const struct got_error *input_ref_view(struct tog_view **,
616 struct tog_view *, int);
617 static const struct got_error *close_ref_view(struct tog_view *);
618 static const struct got_error *search_start_ref_view(struct tog_view *);
619 static const struct got_error *search_next_ref_view(struct tog_view *);
621 static volatile sig_atomic_t tog_sigwinch_received;
622 static volatile sig_atomic_t tog_sigpipe_received;
623 static volatile sig_atomic_t tog_sigcont_received;
624 static volatile sig_atomic_t tog_sigint_received;
625 static volatile sig_atomic_t tog_sigterm_received;
627 static void
628 tog_sigwinch(int signo)
630 tog_sigwinch_received = 1;
633 static void
634 tog_sigpipe(int signo)
636 tog_sigpipe_received = 1;
639 static void
640 tog_sigcont(int signo)
642 tog_sigcont_received = 1;
645 static void
646 tog_sigint(int signo)
648 tog_sigint_received = 1;
651 static void
652 tog_sigterm(int signo)
654 tog_sigterm_received = 1;
657 static int
658 tog_fatal_signal_received(void)
660 return (tog_sigpipe_received ||
661 tog_sigint_received || tog_sigint_received);
664 static const struct got_error *
665 view_close(struct tog_view *view)
667 const struct got_error *err = NULL, *child_err = NULL;
669 if (view->child) {
670 child_err = view_close(view->child);
671 view->child = NULL;
673 if (view->close)
674 err = view->close(view);
675 if (view->panel)
676 del_panel(view->panel);
677 if (view->window)
678 delwin(view->window);
679 free(view);
680 return err ? err : child_err;
683 static struct tog_view *
684 view_open(int nlines, int ncols, int begin_y, int begin_x,
685 enum tog_view_type type)
687 struct tog_view *view = calloc(1, sizeof(*view));
689 if (view == NULL)
690 return NULL;
692 view->type = type;
693 view->lines = LINES;
694 view->cols = COLS;
695 view->nlines = nlines ? nlines : LINES - begin_y;
696 view->ncols = ncols ? ncols : COLS - begin_x;
697 view->begin_y = begin_y;
698 view->begin_x = begin_x;
699 view->window = newwin(nlines, ncols, begin_y, begin_x);
700 if (view->window == NULL) {
701 view_close(view);
702 return NULL;
704 view->panel = new_panel(view->window);
705 if (view->panel == NULL ||
706 set_panel_userptr(view->panel, view) != OK) {
707 view_close(view);
708 return NULL;
711 keypad(view->window, TRUE);
712 return view;
715 static int
716 view_split_begin_x(int begin_x)
718 if (begin_x > 0 || COLS < 120)
719 return 0;
720 return (COLS - MAX(COLS / 2, 80));
723 /* XXX Stub till we decide what to do. */
724 static int
725 view_split_begin_y(int lines)
727 return lines * HSPLIT_SCALE;
730 static const struct got_error *view_resize(struct tog_view *);
732 static const struct got_error *
733 view_splitscreen(struct tog_view *view)
735 const struct got_error *err = NULL;
737 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
738 if (view->resized_y && view->resized_y < view->lines)
739 view->begin_y = view->resized_y;
740 else
741 view->begin_y = view_split_begin_y(view->nlines);
742 view->begin_x = 0;
743 } else if (!view->resized) {
744 if (view->resized_x && view->resized_x < view->cols - 1 &&
745 view->cols > 119)
746 view->begin_x = view->resized_x;
747 else
748 view->begin_x = view_split_begin_x(0);
749 view->begin_y = 0;
751 view->nlines = LINES - view->begin_y;
752 view->ncols = COLS - view->begin_x;
753 view->lines = LINES;
754 view->cols = COLS;
755 err = view_resize(view);
756 if (err)
757 return err;
759 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
760 view->parent->nlines = view->begin_y;
762 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
763 return got_error_from_errno("mvwin");
765 return NULL;
768 static const struct got_error *
769 view_fullscreen(struct tog_view *view)
771 const struct got_error *err = NULL;
773 view->begin_x = 0;
774 view->begin_y = view->resized ? view->begin_y : 0;
775 view->nlines = view->resized ? view->nlines : LINES;
776 view->ncols = COLS;
777 view->lines = LINES;
778 view->cols = COLS;
779 err = view_resize(view);
780 if (err)
781 return err;
783 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
784 return got_error_from_errno("mvwin");
786 return NULL;
789 static int
790 view_is_parent_view(struct tog_view *view)
792 return view->parent == NULL;
795 static int
796 view_is_splitscreen(struct tog_view *view)
798 return view->begin_x > 0 || view->begin_y > 0;
801 static int
802 view_is_fullscreen(struct tog_view *view)
804 return view->nlines == LINES && view->ncols == COLS;
807 static int
808 view_is_hsplit_top(struct tog_view *view)
810 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
811 view_is_splitscreen(view->child);
814 static void
815 view_border(struct tog_view *view)
817 PANEL *panel;
818 const struct tog_view *view_above;
820 if (view->parent)
821 return view_border(view->parent);
823 panel = panel_above(view->panel);
824 if (panel == NULL)
825 return;
827 view_above = panel_userptr(panel);
828 if (view->mode == TOG_VIEW_SPLIT_HRZN)
829 mvwhline(view->window, view_above->begin_y - 1,
830 view->begin_x, got_locale_is_utf8() ?
831 ACS_HLINE : '-', view->ncols);
832 else
833 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
834 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
837 static const struct got_error *view_init_hsplit(struct tog_view *, int);
838 static const struct got_error *request_log_commits(struct tog_view *);
839 static const struct got_error *offset_selection_down(struct tog_view *);
840 static void offset_selection_up(struct tog_view *);
841 static void view_get_split(struct tog_view *, int *, int *);
843 static const struct got_error *
844 view_resize(struct tog_view *view)
846 const struct got_error *err = NULL;
847 int dif, nlines, ncols;
849 dif = LINES - view->lines; /* line difference */
851 if (view->lines > LINES)
852 nlines = view->nlines - (view->lines - LINES);
853 else
854 nlines = view->nlines + (LINES - view->lines);
855 if (view->cols > COLS)
856 ncols = view->ncols - (view->cols - COLS);
857 else
858 ncols = view->ncols + (COLS - view->cols);
860 if (view->child) {
861 int hs = view->child->begin_y;
863 if (!view_is_fullscreen(view))
864 view->child->begin_x = view_split_begin_x(view->begin_x);
865 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
866 view->child->begin_x == 0) {
867 ncols = COLS;
869 view_fullscreen(view->child);
870 if (view->child->focussed)
871 show_panel(view->child->panel);
872 else
873 show_panel(view->panel);
874 } else {
875 ncols = view->child->begin_x;
877 view_splitscreen(view->child);
878 show_panel(view->child->panel);
880 /*
881 * XXX This is ugly and needs to be moved into the above
882 * logic but "works" for now and my attempts at moving it
883 * break either 'tab' or 'F' key maps in horizontal splits.
884 */
885 if (hs) {
886 err = view_splitscreen(view->child);
887 if (err)
888 return err;
889 if (dif < 0) { /* top split decreased */
890 err = offset_selection_down(view);
891 if (err)
892 return err;
894 view_border(view);
895 update_panels();
896 doupdate();
897 show_panel(view->child->panel);
898 nlines = view->nlines;
900 } else if (view->parent == NULL)
901 ncols = COLS;
903 if (view->resize && dif > 0) {
904 err = view->resize(view, dif);
905 if (err)
906 return err;
909 if (wresize(view->window, nlines, ncols) == ERR)
910 return got_error_from_errno("wresize");
911 if (replace_panel(view->panel, view->window) == ERR)
912 return got_error_from_errno("replace_panel");
913 wclear(view->window);
915 view->nlines = nlines;
916 view->ncols = ncols;
917 view->lines = LINES;
918 view->cols = COLS;
920 return NULL;
923 static const struct got_error *
924 resize_log_view(struct tog_view *view, int increase)
926 struct tog_log_view_state *s = &view->state.log;
927 const struct got_error *err = NULL;
928 int n = s->selected_entry->idx + view->lines - s->selected;
930 /*
931 * Request commits to account for the increased
932 * height so we have enough to populate the view.
933 */
934 if (s->commits.ncommits < n) {
935 view->nscrolled = n - s->commits.ncommits + increase + 1;
936 err = request_log_commits(view);
939 return err;
942 static void
943 view_adjust_offset(struct tog_view *view, int n)
945 if (n == 0)
946 return;
948 if (view->parent && view->parent->offset) {
949 if (view->parent->offset + n >= 0)
950 view->parent->offset += n;
951 else
952 view->parent->offset = 0;
953 } else if (view->offset) {
954 if (view->offset - n >= 0)
955 view->offset -= n;
956 else
957 view->offset = 0;
961 static const struct got_error *
962 view_resize_split(struct tog_view *view, int resize)
964 const struct got_error *err = NULL;
965 struct tog_view *v = NULL;
967 if (view->parent)
968 v = view->parent;
969 else
970 v = view;
972 if (!v->child || !view_is_splitscreen(v->child))
973 return NULL;
975 v->resized = v->child->resized = resize; /* lock for resize event */
977 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
978 int y = v->child->begin_y;
980 if (v->child->resized_y)
981 v->child->begin_y = v->child->resized_y;
982 if (view->parent)
983 v->child->begin_y -= resize;
984 else
985 v->child->begin_y += resize;
986 if (v->child->begin_y < 3) {
987 view->count = 0;
988 v->child->begin_y = 3;
989 } else if (v->child->begin_y > LINES - 1) {
990 view->count = 0;
991 v->child->begin_y = LINES - 1;
993 v->ncols = COLS;
994 v->child->ncols = COLS;
995 view_adjust_offset(view, resize);
996 err = view_init_hsplit(v, v->child->begin_y);
997 if (err)
998 return err;
999 v->child->resized_y = v->child->begin_y;
1000 if (y > v->child->begin_y && v->child->type == TOG_VIEW_LOG)
1001 v->child->nscrolled = y - v->child->begin_y;
1002 else if (y < v->child->begin_y && v->type == TOG_VIEW_LOG)
1003 v->nscrolled = v->child->begin_y - y;
1004 } else {
1005 if (v->child->resized_x)
1006 v->child->begin_x = v->child->resized_x;
1007 if (view->parent)
1008 v->child->begin_x -= resize;
1009 else
1010 v->child->begin_x += resize;
1011 if (v->child->begin_x < 11) {
1012 view->count = 0;
1013 v->child->begin_x = 11;
1014 } else if (v->child->begin_x > COLS - 1) {
1015 view->count = 0;
1016 v->child->begin_x = COLS - 1;
1018 v->child->resized_x = v->child->begin_x;
1021 v->child->mode = v->mode;
1022 v->child->nlines = v->lines - v->child->begin_y;
1023 v->child->ncols = v->cols - v->child->begin_x;
1024 v->focus_child = 1;
1026 err = view_fullscreen(v);
1027 if (err)
1028 return err;
1029 err = view_splitscreen(v->child);
1030 if (err)
1031 return err;
1033 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1034 err = offset_selection_down(v->child);
1035 if (err)
1036 return err;
1039 if (v->nscrolled)
1040 err = request_log_commits(v);
1041 else if (v->child->nscrolled)
1042 err = request_log_commits(v->child);
1044 v->resized = v->child->resized = 0;
1046 return err;
1049 static void
1050 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1052 struct tog_view *v = src->child ? src->child : src;
1054 dst->resized_x = v->resized_x;
1055 dst->resized_y = v->resized_y;
1058 static const struct got_error *
1059 view_close_child(struct tog_view *view)
1061 const struct got_error *err = NULL;
1063 if (view->child == NULL)
1064 return NULL;
1066 err = view_close(view->child);
1067 view->child = NULL;
1068 return err;
1071 static const struct got_error *
1072 view_set_child(struct tog_view *view, struct tog_view *child)
1074 const struct got_error *err = NULL;
1076 view->child = child;
1077 child->parent = view;
1079 err = view_resize(view);
1080 if (err)
1081 return err;
1083 if (view->child->resized_x || view->child->resized_y)
1084 err = view_resize_split(view, 0);
1086 return err;
1089 static void
1090 tog_resizeterm(void)
1092 int cols, lines;
1093 struct winsize size;
1095 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1096 cols = 80; /* Default */
1097 lines = 24;
1098 } else {
1099 cols = size.ws_col;
1100 lines = size.ws_row;
1102 resize_term(lines, cols);
1105 static const struct got_error *
1106 view_search_start(struct tog_view *view)
1108 const struct got_error *err = NULL;
1109 struct tog_view *v = view;
1110 char pattern[1024];
1111 int ret;
1113 if (view->search_started) {
1114 regfree(&view->regex);
1115 view->searching = 0;
1116 memset(&view->regmatch, 0, sizeof(view->regmatch));
1118 view->search_started = 0;
1120 if (view->nlines < 1)
1121 return NULL;
1123 if (view_is_hsplit_top(view))
1124 v = view->child;
1126 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1127 wclrtoeol(v->window);
1129 nodelay(view->window, FALSE); /* block for search term input */
1130 nocbreak();
1131 echo();
1132 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1133 wrefresh(v->window);
1134 cbreak();
1135 noecho();
1136 nodelay(view->window, TRUE);
1137 if (ret == ERR)
1138 return NULL;
1140 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1141 err = view->search_start(view);
1142 if (err) {
1143 regfree(&view->regex);
1144 return err;
1146 view->search_started = 1;
1147 view->searching = TOG_SEARCH_FORWARD;
1148 view->search_next_done = 0;
1149 view->search_next(view);
1152 return NULL;
1155 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1156 static const struct got_error *
1157 switch_split(struct tog_view *view)
1159 const struct got_error *err = NULL;
1160 struct tog_view *v = NULL;
1162 if (view->parent)
1163 v = view->parent;
1164 else
1165 v = view;
1167 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1168 v->mode = TOG_VIEW_SPLIT_VERT;
1169 else
1170 v->mode = TOG_VIEW_SPLIT_HRZN;
1172 if (!v->child)
1173 return NULL;
1174 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1175 v->mode = TOG_VIEW_SPLIT_NONE;
1177 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1178 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1179 v->child->begin_y = v->child->resized_y;
1180 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1181 v->child->begin_x = v->child->resized_x;
1184 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1185 v->ncols = COLS;
1186 v->child->ncols = COLS;
1187 v->child->nscrolled = LINES - v->child->nlines;
1189 err = view_init_hsplit(v, v->child->begin_y);
1190 if (err)
1191 return err;
1193 v->child->mode = v->mode;
1194 v->child->nlines = v->lines - v->child->begin_y;
1195 v->focus_child = 1;
1197 err = view_fullscreen(v);
1198 if (err)
1199 return err;
1200 err = view_splitscreen(v->child);
1201 if (err)
1202 return err;
1204 if (v->mode == TOG_VIEW_SPLIT_NONE)
1205 v->mode = TOG_VIEW_SPLIT_VERT;
1206 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1207 err = offset_selection_down(v);
1208 err = offset_selection_down(v->child);
1209 } else {
1210 offset_selection_up(v);
1211 offset_selection_up(v->child);
1213 if (v->type == TOG_VIEW_LOG && v->nscrolled)
1214 err = request_log_commits(v);
1215 else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1216 err = request_log_commits(v->child);
1218 return err;
1222 * Compute view->count from numeric input. Assign total to view->count and
1223 * return first non-numeric key entered.
1225 static int
1226 get_compound_key(struct tog_view *view, int c)
1228 struct tog_view *v = view;
1229 int x, n = 0;
1231 if (view_is_hsplit_top(view))
1232 v = view->child;
1233 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1234 v = view->parent;
1236 view->count = 0;
1237 cbreak(); /* block for input */
1238 wmove(v->window, v->nlines - 1, 0);
1239 wclrtoeol(v->window);
1240 waddch(v->window, ':');
1242 do {
1243 x = getcurx(v->window);
1244 if (x != ERR && x < view->ncols) {
1245 waddch(v->window, c);
1246 wrefresh(v->window);
1250 * Don't overflow. Max valid request should be the greatest
1251 * between the longest and total lines; cap at 10 million.
1253 if (n >= 9999999)
1254 n = 9999999;
1255 else
1256 n = n * 10 + (c - '0');
1257 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1259 /* Massage excessive or inapplicable values at the input handler. */
1260 view->count = n;
1262 return c;
1265 static const struct got_error *
1266 view_input(struct tog_view **new, int *done, struct tog_view *view,
1267 struct tog_view_list_head *views)
1269 const struct got_error *err = NULL;
1270 struct tog_view *v;
1271 int ch, errcode;
1273 *new = NULL;
1275 /* Clear "no matches" indicator. */
1276 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1277 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1278 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1279 view->count = 0;
1282 if (view->searching && !view->search_next_done) {
1283 errcode = pthread_mutex_unlock(&tog_mutex);
1284 if (errcode)
1285 return got_error_set_errno(errcode,
1286 "pthread_mutex_unlock");
1287 sched_yield();
1288 errcode = pthread_mutex_lock(&tog_mutex);
1289 if (errcode)
1290 return got_error_set_errno(errcode,
1291 "pthread_mutex_lock");
1292 view->search_next(view);
1293 return NULL;
1296 nodelay(view->window, FALSE);
1297 /* Allow threads to make progress while we are waiting for input. */
1298 errcode = pthread_mutex_unlock(&tog_mutex);
1299 if (errcode)
1300 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1301 /* If we have an unfinished count, let C-g or backspace abort. */
1302 if (view->count && --view->count) {
1303 cbreak();
1304 nodelay(view->window, TRUE);
1305 ch = wgetch(view->window);
1306 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1307 view->count = 0;
1308 else
1309 ch = view->ch;
1310 } else {
1311 ch = wgetch(view->window);
1312 if (ch >= '1' && ch <= '9')
1313 view->ch = ch = get_compound_key(view, ch);
1315 errcode = pthread_mutex_lock(&tog_mutex);
1316 if (errcode)
1317 return got_error_set_errno(errcode, "pthread_mutex_lock");
1318 nodelay(view->window, TRUE);
1320 if (tog_sigwinch_received || tog_sigcont_received) {
1321 tog_resizeterm();
1322 tog_sigwinch_received = 0;
1323 tog_sigcont_received = 0;
1324 TAILQ_FOREACH(v, views, entry) {
1325 err = view_resize(v);
1326 if (err)
1327 return err;
1328 err = v->input(new, v, KEY_RESIZE);
1329 if (err)
1330 return err;
1331 if (v->child) {
1332 err = view_resize(v->child);
1333 if (err)
1334 return err;
1335 err = v->child->input(new, v->child,
1336 KEY_RESIZE);
1337 if (err)
1338 return err;
1339 if (v->child->resized_x || v->child->resized_y) {
1340 err = view_resize_split(v, 0);
1341 if (err)
1342 return err;
1348 switch (ch) {
1349 case '\t':
1350 view->count = 0;
1351 if (view->child) {
1352 view->focussed = 0;
1353 view->child->focussed = 1;
1354 view->focus_child = 1;
1355 } else if (view->parent) {
1356 view->focussed = 0;
1357 view->parent->focussed = 1;
1358 view->parent->focus_child = 0;
1359 if (!view_is_splitscreen(view)) {
1360 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1361 view->parent->type == TOG_VIEW_LOG) {
1362 err = request_log_commits(view->parent);
1363 if (err)
1364 return err;
1366 offset_selection_up(view->parent);
1367 err = view_fullscreen(view->parent);
1368 if (err)
1369 return err;
1372 break;
1373 case 'q':
1374 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1375 if (view->parent->type == TOG_VIEW_LOG) {
1376 /* might need more commits to fill fullscreen */
1377 err = request_log_commits(view->parent);
1378 if (err)
1379 break;
1381 offset_selection_up(view->parent);
1383 err = view->input(new, view, ch);
1384 view->dying = 1;
1385 break;
1386 case 'Q':
1387 *done = 1;
1388 break;
1389 case 'F':
1390 view->count = 0;
1391 if (view_is_parent_view(view)) {
1392 if (view->child == NULL)
1393 break;
1394 if (view_is_splitscreen(view->child)) {
1395 view->focussed = 0;
1396 view->child->focussed = 1;
1397 err = view_fullscreen(view->child);
1398 } else {
1399 err = view_splitscreen(view->child);
1400 if (!err)
1401 err = view_resize_split(view, 0);
1403 if (err)
1404 break;
1405 err = view->child->input(new, view->child,
1406 KEY_RESIZE);
1407 } else {
1408 if (view_is_splitscreen(view)) {
1409 view->parent->focussed = 0;
1410 view->focussed = 1;
1411 err = view_fullscreen(view);
1412 } else {
1413 err = view_splitscreen(view);
1414 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1415 err = view_resize(view->parent);
1416 if (!err)
1417 err = view_resize_split(view, 0);
1419 if (err)
1420 break;
1421 err = view->input(new, view, KEY_RESIZE);
1423 if (err)
1424 break;
1425 if (view->type == TOG_VIEW_LOG) {
1426 err = request_log_commits(view);
1427 if (err)
1428 break;
1430 if (view->parent)
1431 err = offset_selection_down(view->parent);
1432 if (!err)
1433 err = offset_selection_down(view);
1434 break;
1435 case 'S':
1436 view->count = 0;
1437 err = switch_split(view);
1438 break;
1439 case '-':
1440 err = view_resize_split(view, -1);
1441 break;
1442 case '+':
1443 err = view_resize_split(view, 1);
1444 break;
1445 case KEY_RESIZE:
1446 break;
1447 case '/':
1448 view->count = 0;
1449 if (view->search_start)
1450 view_search_start(view);
1451 else
1452 err = view->input(new, view, ch);
1453 break;
1454 case 'N':
1455 case 'n':
1456 if (view->search_started && view->search_next) {
1457 view->searching = (ch == 'n' ?
1458 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1459 view->search_next_done = 0;
1460 view->search_next(view);
1461 } else
1462 err = view->input(new, view, ch);
1463 break;
1464 case 'A':
1465 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1466 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1467 else
1468 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1469 TAILQ_FOREACH(v, views, entry) {
1470 if (v->reset) {
1471 err = v->reset(v);
1472 if (err)
1473 return err;
1475 if (v->child && v->child->reset) {
1476 err = v->child->reset(v->child);
1477 if (err)
1478 return err;
1481 break;
1482 default:
1483 err = view->input(new, view, ch);
1484 break;
1487 return err;
1490 static int
1491 view_needs_focus_indication(struct tog_view *view)
1493 if (view_is_parent_view(view)) {
1494 if (view->child == NULL || view->child->focussed)
1495 return 0;
1496 if (!view_is_splitscreen(view->child))
1497 return 0;
1498 } else if (!view_is_splitscreen(view))
1499 return 0;
1501 return view->focussed;
1504 static const struct got_error *
1505 view_loop(struct tog_view *view)
1507 const struct got_error *err = NULL;
1508 struct tog_view_list_head views;
1509 struct tog_view *new_view;
1510 char *mode;
1511 int fast_refresh = 10;
1512 int done = 0, errcode;
1514 mode = getenv("TOG_VIEW_SPLIT_MODE");
1515 if (!mode || !(*mode == 'h' || *mode == 'H'))
1516 view->mode = TOG_VIEW_SPLIT_VERT;
1517 else
1518 view->mode = TOG_VIEW_SPLIT_HRZN;
1520 errcode = pthread_mutex_lock(&tog_mutex);
1521 if (errcode)
1522 return got_error_set_errno(errcode, "pthread_mutex_lock");
1524 TAILQ_INIT(&views);
1525 TAILQ_INSERT_HEAD(&views, view, entry);
1527 view->focussed = 1;
1528 err = view->show(view);
1529 if (err)
1530 return err;
1531 update_panels();
1532 doupdate();
1533 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1534 !tog_fatal_signal_received()) {
1535 /* Refresh fast during initialization, then become slower. */
1536 if (fast_refresh && fast_refresh-- == 0)
1537 halfdelay(10); /* switch to once per second */
1539 err = view_input(&new_view, &done, view, &views);
1540 if (err)
1541 break;
1542 if (view->dying) {
1543 struct tog_view *v, *prev = NULL;
1545 if (view_is_parent_view(view))
1546 prev = TAILQ_PREV(view, tog_view_list_head,
1547 entry);
1548 else if (view->parent)
1549 prev = view->parent;
1551 if (view->parent) {
1552 view->parent->child = NULL;
1553 view->parent->focus_child = 0;
1554 /* Restore fullscreen line height. */
1555 view->parent->nlines = view->parent->lines;
1556 err = view_resize(view->parent);
1557 if (err)
1558 break;
1559 /* Make resized splits persist. */
1560 view_transfer_size(view->parent, view);
1561 } else
1562 TAILQ_REMOVE(&views, view, entry);
1564 err = view_close(view);
1565 if (err)
1566 goto done;
1568 view = NULL;
1569 TAILQ_FOREACH(v, &views, entry) {
1570 if (v->focussed)
1571 break;
1573 if (view == NULL && new_view == NULL) {
1574 /* No view has focus. Try to pick one. */
1575 if (prev)
1576 view = prev;
1577 else if (!TAILQ_EMPTY(&views)) {
1578 view = TAILQ_LAST(&views,
1579 tog_view_list_head);
1581 if (view) {
1582 if (view->focus_child) {
1583 view->child->focussed = 1;
1584 view = view->child;
1585 } else
1586 view->focussed = 1;
1590 if (new_view) {
1591 struct tog_view *v, *t;
1592 /* Only allow one parent view per type. */
1593 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1594 if (v->type != new_view->type)
1595 continue;
1596 TAILQ_REMOVE(&views, v, entry);
1597 err = view_close(v);
1598 if (err)
1599 goto done;
1600 break;
1602 TAILQ_INSERT_TAIL(&views, new_view, entry);
1603 view = new_view;
1605 if (view) {
1606 if (view_is_parent_view(view)) {
1607 if (view->child && view->child->focussed)
1608 view = view->child;
1609 } else {
1610 if (view->parent && view->parent->focussed)
1611 view = view->parent;
1613 show_panel(view->panel);
1614 if (view->child && view_is_splitscreen(view->child))
1615 show_panel(view->child->panel);
1616 if (view->parent && view_is_splitscreen(view)) {
1617 err = view->parent->show(view->parent);
1618 if (err)
1619 goto done;
1621 err = view->show(view);
1622 if (err)
1623 goto done;
1624 if (view->child) {
1625 err = view->child->show(view->child);
1626 if (err)
1627 goto done;
1629 update_panels();
1630 doupdate();
1633 done:
1634 while (!TAILQ_EMPTY(&views)) {
1635 const struct got_error *close_err;
1636 view = TAILQ_FIRST(&views);
1637 TAILQ_REMOVE(&views, view, entry);
1638 close_err = view_close(view);
1639 if (close_err && err == NULL)
1640 err = close_err;
1643 errcode = pthread_mutex_unlock(&tog_mutex);
1644 if (errcode && err == NULL)
1645 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1647 return err;
1650 __dead static void
1651 usage_log(void)
1653 endwin();
1654 fprintf(stderr,
1655 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1656 getprogname());
1657 exit(1);
1660 /* Create newly allocated wide-character string equivalent to a byte string. */
1661 static const struct got_error *
1662 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1664 char *vis = NULL;
1665 const struct got_error *err = NULL;
1667 *ws = NULL;
1668 *wlen = mbstowcs(NULL, s, 0);
1669 if (*wlen == (size_t)-1) {
1670 int vislen;
1671 if (errno != EILSEQ)
1672 return got_error_from_errno("mbstowcs");
1674 /* byte string invalid in current encoding; try to "fix" it */
1675 err = got_mbsavis(&vis, &vislen, s);
1676 if (err)
1677 return err;
1678 *wlen = mbstowcs(NULL, vis, 0);
1679 if (*wlen == (size_t)-1) {
1680 err = got_error_from_errno("mbstowcs"); /* give up */
1681 goto done;
1685 *ws = calloc(*wlen + 1, sizeof(**ws));
1686 if (*ws == NULL) {
1687 err = got_error_from_errno("calloc");
1688 goto done;
1691 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1692 err = got_error_from_errno("mbstowcs");
1693 done:
1694 free(vis);
1695 if (err) {
1696 free(*ws);
1697 *ws = NULL;
1698 *wlen = 0;
1700 return err;
1703 static const struct got_error *
1704 expand_tab(char **ptr, const char *src)
1706 char *dst;
1707 size_t len, n, idx = 0, sz = 0;
1709 *ptr = NULL;
1710 n = len = strlen(src);
1711 dst = malloc(n + 1);
1712 if (dst == NULL)
1713 return got_error_from_errno("malloc");
1715 while (idx < len && src[idx]) {
1716 const char c = src[idx];
1718 if (c == '\t') {
1719 size_t nb = TABSIZE - sz % TABSIZE;
1720 char *p;
1722 p = realloc(dst, n + nb);
1723 if (p == NULL) {
1724 free(dst);
1725 return got_error_from_errno("realloc");
1728 dst = p;
1729 n += nb;
1730 memset(dst + sz, ' ', nb);
1731 sz += nb;
1732 } else
1733 dst[sz++] = src[idx];
1734 ++idx;
1737 dst[sz] = '\0';
1738 *ptr = dst;
1739 return NULL;
1743 * Advance at most n columns from wline starting at offset off.
1744 * Return the index to the first character after the span operation.
1745 * Return the combined column width of all spanned wide character in
1746 * *rcol.
1748 static int
1749 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1751 int width, i, cols = 0;
1753 if (n == 0) {
1754 *rcol = cols;
1755 return off;
1758 for (i = off; wline[i] != L'\0'; ++i) {
1759 if (wline[i] == L'\t')
1760 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1761 else
1762 width = wcwidth(wline[i]);
1764 if (width == -1) {
1765 width = 1;
1766 wline[i] = L'.';
1769 if (cols + width > n)
1770 break;
1771 cols += width;
1774 *rcol = cols;
1775 return i;
1779 * Format a line for display, ensuring that it won't overflow a width limit.
1780 * With scrolling, the width returned refers to the scrolled version of the
1781 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1783 static const struct got_error *
1784 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1785 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1787 const struct got_error *err = NULL;
1788 int cols;
1789 wchar_t *wline = NULL;
1790 char *exstr = NULL;
1791 size_t wlen;
1792 int i, scrollx;
1794 *wlinep = NULL;
1795 *widthp = 0;
1797 if (expand) {
1798 err = expand_tab(&exstr, line);
1799 if (err)
1800 return err;
1803 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1804 free(exstr);
1805 if (err)
1806 return err;
1808 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1810 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1811 wline[wlen - 1] = L'\0';
1812 wlen--;
1814 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1815 wline[wlen - 1] = L'\0';
1816 wlen--;
1819 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1820 wline[i] = L'\0';
1822 if (widthp)
1823 *widthp = cols;
1824 if (scrollxp)
1825 *scrollxp = scrollx;
1826 if (err)
1827 free(wline);
1828 else
1829 *wlinep = wline;
1830 return err;
1833 static const struct got_error*
1834 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1835 struct got_object_id *id, struct got_repository *repo)
1837 static const struct got_error *err = NULL;
1838 struct got_reflist_entry *re;
1839 char *s;
1840 const char *name;
1842 *refs_str = NULL;
1844 TAILQ_FOREACH(re, refs, entry) {
1845 struct got_tag_object *tag = NULL;
1846 struct got_object_id *ref_id;
1847 int cmp;
1849 name = got_ref_get_name(re->ref);
1850 if (strcmp(name, GOT_REF_HEAD) == 0)
1851 continue;
1852 if (strncmp(name, "refs/", 5) == 0)
1853 name += 5;
1854 if (strncmp(name, "got/", 4) == 0 &&
1855 strncmp(name, "got/backup/", 11) != 0)
1856 continue;
1857 if (strncmp(name, "heads/", 6) == 0)
1858 name += 6;
1859 if (strncmp(name, "remotes/", 8) == 0) {
1860 name += 8;
1861 s = strstr(name, "/" GOT_REF_HEAD);
1862 if (s != NULL && s[strlen(s)] == '\0')
1863 continue;
1865 err = got_ref_resolve(&ref_id, repo, re->ref);
1866 if (err)
1867 break;
1868 if (strncmp(name, "tags/", 5) == 0) {
1869 err = got_object_open_as_tag(&tag, repo, ref_id);
1870 if (err) {
1871 if (err->code != GOT_ERR_OBJ_TYPE) {
1872 free(ref_id);
1873 break;
1875 /* Ref points at something other than a tag. */
1876 err = NULL;
1877 tag = NULL;
1880 cmp = got_object_id_cmp(tag ?
1881 got_object_tag_get_object_id(tag) : ref_id, id);
1882 free(ref_id);
1883 if (tag)
1884 got_object_tag_close(tag);
1885 if (cmp != 0)
1886 continue;
1887 s = *refs_str;
1888 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1889 s ? ", " : "", name) == -1) {
1890 err = got_error_from_errno("asprintf");
1891 free(s);
1892 *refs_str = NULL;
1893 break;
1895 free(s);
1898 return err;
1901 static const struct got_error *
1902 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1903 int col_tab_align)
1905 char *smallerthan;
1907 smallerthan = strchr(author, '<');
1908 if (smallerthan && smallerthan[1] != '\0')
1909 author = smallerthan + 1;
1910 author[strcspn(author, "@>")] = '\0';
1911 return format_line(wauthor, author_width, NULL, author, 0, limit,
1912 col_tab_align, 0);
1915 static const struct got_error *
1916 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1917 struct got_object_id *id, const size_t date_display_cols,
1918 int author_display_cols)
1920 struct tog_log_view_state *s = &view->state.log;
1921 const struct got_error *err = NULL;
1922 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1923 char *logmsg0 = NULL, *logmsg = NULL;
1924 char *author = NULL;
1925 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1926 int author_width, logmsg_width;
1927 char *newline, *line = NULL;
1928 int col, limit, scrollx;
1929 const int avail = view->ncols;
1930 struct tm tm;
1931 time_t committer_time;
1932 struct tog_color *tc;
1934 committer_time = got_object_commit_get_committer_time(commit);
1935 if (gmtime_r(&committer_time, &tm) == NULL)
1936 return got_error_from_errno("gmtime_r");
1937 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1938 return got_error(GOT_ERR_NO_SPACE);
1940 if (avail <= date_display_cols)
1941 limit = MIN(sizeof(datebuf) - 1, avail);
1942 else
1943 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1944 tc = get_color(&s->colors, TOG_COLOR_DATE);
1945 if (tc)
1946 wattr_on(view->window,
1947 COLOR_PAIR(tc->colorpair), NULL);
1948 waddnstr(view->window, datebuf, limit);
1949 if (tc)
1950 wattr_off(view->window,
1951 COLOR_PAIR(tc->colorpair), NULL);
1952 col = limit;
1953 if (col > avail)
1954 goto done;
1956 if (avail >= 120) {
1957 char *id_str;
1958 err = got_object_id_str(&id_str, id);
1959 if (err)
1960 goto done;
1961 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1962 if (tc)
1963 wattr_on(view->window,
1964 COLOR_PAIR(tc->colorpair), NULL);
1965 wprintw(view->window, "%.8s ", id_str);
1966 if (tc)
1967 wattr_off(view->window,
1968 COLOR_PAIR(tc->colorpair), NULL);
1969 free(id_str);
1970 col += 9;
1971 if (col > avail)
1972 goto done;
1975 if (s->use_committer)
1976 author = strdup(got_object_commit_get_committer(commit));
1977 else
1978 author = strdup(got_object_commit_get_author(commit));
1979 if (author == NULL) {
1980 err = got_error_from_errno("strdup");
1981 goto done;
1983 err = format_author(&wauthor, &author_width, author, avail - col, col);
1984 if (err)
1985 goto done;
1986 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1987 if (tc)
1988 wattr_on(view->window,
1989 COLOR_PAIR(tc->colorpair), NULL);
1990 waddwstr(view->window, wauthor);
1991 if (tc)
1992 wattr_off(view->window,
1993 COLOR_PAIR(tc->colorpair), NULL);
1994 col += author_width;
1995 while (col < avail && author_width < author_display_cols + 2) {
1996 waddch(view->window, ' ');
1997 col++;
1998 author_width++;
2000 if (col > avail)
2001 goto done;
2003 err = got_object_commit_get_logmsg(&logmsg0, commit);
2004 if (err)
2005 goto done;
2006 logmsg = logmsg0;
2007 while (*logmsg == '\n')
2008 logmsg++;
2009 newline = strchr(logmsg, '\n');
2010 if (newline)
2011 *newline = '\0';
2012 limit = avail - col;
2013 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2014 limit--; /* for the border */
2015 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2016 limit, col, 1);
2017 if (err)
2018 goto done;
2019 waddwstr(view->window, &wlogmsg[scrollx]);
2020 col += MAX(logmsg_width, 0);
2021 while (col < avail) {
2022 waddch(view->window, ' ');
2023 col++;
2025 done:
2026 free(logmsg0);
2027 free(wlogmsg);
2028 free(author);
2029 free(wauthor);
2030 free(line);
2031 return err;
2034 static struct commit_queue_entry *
2035 alloc_commit_queue_entry(struct got_commit_object *commit,
2036 struct got_object_id *id)
2038 struct commit_queue_entry *entry;
2040 entry = calloc(1, sizeof(*entry));
2041 if (entry == NULL)
2042 return NULL;
2044 entry->id = id;
2045 entry->commit = commit;
2046 return entry;
2049 static void
2050 pop_commit(struct commit_queue *commits)
2052 struct commit_queue_entry *entry;
2054 entry = TAILQ_FIRST(&commits->head);
2055 TAILQ_REMOVE(&commits->head, entry, entry);
2056 got_object_commit_close(entry->commit);
2057 commits->ncommits--;
2058 /* Don't free entry->id! It is owned by the commit graph. */
2059 free(entry);
2062 static void
2063 free_commits(struct commit_queue *commits)
2065 while (!TAILQ_EMPTY(&commits->head))
2066 pop_commit(commits);
2069 static const struct got_error *
2070 match_commit(int *have_match, struct got_object_id *id,
2071 struct got_commit_object *commit, regex_t *regex)
2073 const struct got_error *err = NULL;
2074 regmatch_t regmatch;
2075 char *id_str = NULL, *logmsg = NULL;
2077 *have_match = 0;
2079 err = got_object_id_str(&id_str, id);
2080 if (err)
2081 return err;
2083 err = got_object_commit_get_logmsg(&logmsg, commit);
2084 if (err)
2085 goto done;
2087 if (regexec(regex, got_object_commit_get_author(commit), 1,
2088 &regmatch, 0) == 0 ||
2089 regexec(regex, got_object_commit_get_committer(commit), 1,
2090 &regmatch, 0) == 0 ||
2091 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2092 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2093 *have_match = 1;
2094 done:
2095 free(id_str);
2096 free(logmsg);
2097 return err;
2100 static const struct got_error *
2101 queue_commits(struct tog_log_thread_args *a)
2103 const struct got_error *err = NULL;
2106 * We keep all commits open throughout the lifetime of the log
2107 * view in order to avoid having to re-fetch commits from disk
2108 * while updating the display.
2110 do {
2111 struct got_object_id *id;
2112 struct got_commit_object *commit;
2113 struct commit_queue_entry *entry;
2114 int errcode;
2116 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2117 NULL, NULL);
2118 if (err || id == NULL)
2119 break;
2121 err = got_object_open_as_commit(&commit, a->repo, id);
2122 if (err)
2123 break;
2124 entry = alloc_commit_queue_entry(commit, id);
2125 if (entry == NULL) {
2126 err = got_error_from_errno("alloc_commit_queue_entry");
2127 break;
2130 errcode = pthread_mutex_lock(&tog_mutex);
2131 if (errcode) {
2132 err = got_error_set_errno(errcode,
2133 "pthread_mutex_lock");
2134 break;
2137 entry->idx = a->commits->ncommits;
2138 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2139 a->commits->ncommits++;
2141 if (*a->searching == TOG_SEARCH_FORWARD &&
2142 !*a->search_next_done) {
2143 int have_match;
2144 err = match_commit(&have_match, id, commit, a->regex);
2145 if (err)
2146 break;
2147 if (have_match)
2148 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2151 errcode = pthread_mutex_unlock(&tog_mutex);
2152 if (errcode && err == NULL)
2153 err = got_error_set_errno(errcode,
2154 "pthread_mutex_unlock");
2155 if (err)
2156 break;
2157 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2159 return err;
2162 static void
2163 select_commit(struct tog_log_view_state *s)
2165 struct commit_queue_entry *entry;
2166 int ncommits = 0;
2168 entry = s->first_displayed_entry;
2169 while (entry) {
2170 if (ncommits == s->selected) {
2171 s->selected_entry = entry;
2172 break;
2174 entry = TAILQ_NEXT(entry, entry);
2175 ncommits++;
2179 static const struct got_error *
2180 draw_commits(struct tog_view *view)
2182 const struct got_error *err = NULL;
2183 struct tog_log_view_state *s = &view->state.log;
2184 struct commit_queue_entry *entry = s->selected_entry;
2185 const int limit = view->nlines;
2186 int width;
2187 int ncommits, author_cols = 4;
2188 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2189 char *refs_str = NULL;
2190 wchar_t *wline;
2191 struct tog_color *tc;
2192 static const size_t date_display_cols = 12;
2194 if (s->selected_entry &&
2195 !(view->searching && view->search_next_done == 0)) {
2196 struct got_reflist_head *refs;
2197 err = got_object_id_str(&id_str, s->selected_entry->id);
2198 if (err)
2199 return err;
2200 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2201 s->selected_entry->id);
2202 if (refs) {
2203 err = build_refs_str(&refs_str, refs,
2204 s->selected_entry->id, s->repo);
2205 if (err)
2206 goto done;
2210 if (s->thread_args.commits_needed == 0)
2211 halfdelay(10); /* disable fast refresh */
2213 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2214 if (asprintf(&ncommits_str, " [%d/%d] %s",
2215 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2216 (view->searching && !view->search_next_done) ?
2217 "searching..." : "loading...") == -1) {
2218 err = got_error_from_errno("asprintf");
2219 goto done;
2221 } else {
2222 const char *search_str = NULL;
2224 if (view->searching) {
2225 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2226 search_str = "no more matches";
2227 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2228 search_str = "no matches found";
2229 else if (!view->search_next_done)
2230 search_str = "searching...";
2233 if (asprintf(&ncommits_str, " [%d/%d] %s",
2234 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2235 search_str ? search_str :
2236 (refs_str ? refs_str : "")) == -1) {
2237 err = got_error_from_errno("asprintf");
2238 goto done;
2242 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2243 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2244 "........................................",
2245 s->in_repo_path, ncommits_str) == -1) {
2246 err = got_error_from_errno("asprintf");
2247 header = NULL;
2248 goto done;
2250 } else if (asprintf(&header, "commit %s%s",
2251 id_str ? id_str : "........................................",
2252 ncommits_str) == -1) {
2253 err = got_error_from_errno("asprintf");
2254 header = NULL;
2255 goto done;
2257 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2258 if (err)
2259 goto done;
2261 werase(view->window);
2263 if (view_needs_focus_indication(view))
2264 wstandout(view->window);
2265 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2266 if (tc)
2267 wattr_on(view->window,
2268 COLOR_PAIR(tc->colorpair), NULL);
2269 waddwstr(view->window, wline);
2270 if (tc)
2271 wattr_off(view->window,
2272 COLOR_PAIR(tc->colorpair), NULL);
2273 while (width < view->ncols) {
2274 waddch(view->window, ' ');
2275 width++;
2277 if (view_needs_focus_indication(view))
2278 wstandend(view->window);
2279 free(wline);
2280 if (limit <= 1)
2281 goto done;
2283 /* Grow author column size if necessary, and set view->maxx. */
2284 entry = s->first_displayed_entry;
2285 ncommits = 0;
2286 view->maxx = 0;
2287 while (entry) {
2288 struct got_commit_object *c = entry->commit;
2289 char *author, *eol, *msg, *msg0;
2290 wchar_t *wauthor, *wmsg;
2291 int width;
2292 if (ncommits >= limit - 1)
2293 break;
2294 if (s->use_committer)
2295 author = strdup(got_object_commit_get_committer(c));
2296 else
2297 author = strdup(got_object_commit_get_author(c));
2298 if (author == NULL) {
2299 err = got_error_from_errno("strdup");
2300 goto done;
2302 err = format_author(&wauthor, &width, author, COLS,
2303 date_display_cols);
2304 if (author_cols < width)
2305 author_cols = width;
2306 free(wauthor);
2307 free(author);
2308 err = got_object_commit_get_logmsg(&msg0, c);
2309 if (err)
2310 goto done;
2311 msg = msg0;
2312 while (*msg == '\n')
2313 ++msg;
2314 if ((eol = strchr(msg, '\n')))
2315 *eol = '\0';
2316 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2317 date_display_cols + author_cols, 0);
2318 if (err)
2319 goto done;
2320 view->maxx = MAX(view->maxx, width);
2321 free(msg0);
2322 free(wmsg);
2323 ncommits++;
2324 entry = TAILQ_NEXT(entry, entry);
2327 entry = s->first_displayed_entry;
2328 s->last_displayed_entry = s->first_displayed_entry;
2329 ncommits = 0;
2330 while (entry) {
2331 if (ncommits >= limit - 1)
2332 break;
2333 if (ncommits == s->selected)
2334 wstandout(view->window);
2335 err = draw_commit(view, entry->commit, entry->id,
2336 date_display_cols, author_cols);
2337 if (ncommits == s->selected)
2338 wstandend(view->window);
2339 if (err)
2340 goto done;
2341 ncommits++;
2342 s->last_displayed_entry = entry;
2343 entry = TAILQ_NEXT(entry, entry);
2346 view_border(view);
2347 done:
2348 free(id_str);
2349 free(refs_str);
2350 free(ncommits_str);
2351 free(header);
2352 return err;
2355 static void
2356 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2358 struct commit_queue_entry *entry;
2359 int nscrolled = 0;
2361 entry = TAILQ_FIRST(&s->commits.head);
2362 if (s->first_displayed_entry == entry)
2363 return;
2365 entry = s->first_displayed_entry;
2366 while (entry && nscrolled < maxscroll) {
2367 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2368 if (entry) {
2369 s->first_displayed_entry = entry;
2370 nscrolled++;
2375 static const struct got_error *
2376 trigger_log_thread(struct tog_view *view, int wait)
2378 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2379 int errcode;
2381 halfdelay(1); /* fast refresh while loading commits */
2383 while (!ta->log_complete && !tog_thread_error &&
2384 (ta->commits_needed > 0 || ta->load_all)) {
2385 /* Wake the log thread. */
2386 errcode = pthread_cond_signal(&ta->need_commits);
2387 if (errcode)
2388 return got_error_set_errno(errcode,
2389 "pthread_cond_signal");
2392 * The mutex will be released while the view loop waits
2393 * in wgetch(), at which time the log thread will run.
2395 if (!wait)
2396 break;
2398 /* Display progress update in log view. */
2399 show_log_view(view);
2400 update_panels();
2401 doupdate();
2403 /* Wait right here while next commit is being loaded. */
2404 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2405 if (errcode)
2406 return got_error_set_errno(errcode,
2407 "pthread_cond_wait");
2409 /* Display progress update in log view. */
2410 show_log_view(view);
2411 update_panels();
2412 doupdate();
2415 return NULL;
2418 static const struct got_error *
2419 request_log_commits(struct tog_view *view)
2421 struct tog_log_view_state *state = &view->state.log;
2422 const struct got_error *err = NULL;
2424 if (state->thread_args.log_complete)
2425 return NULL;
2427 state->thread_args.commits_needed += view->nscrolled;
2428 err = trigger_log_thread(view, 1);
2429 view->nscrolled = 0;
2431 return err;
2434 static const struct got_error *
2435 log_scroll_down(struct tog_view *view, int maxscroll)
2437 struct tog_log_view_state *s = &view->state.log;
2438 const struct got_error *err = NULL;
2439 struct commit_queue_entry *pentry;
2440 int nscrolled = 0, ncommits_needed;
2442 if (s->last_displayed_entry == NULL)
2443 return NULL;
2445 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2446 if (s->commits.ncommits < ncommits_needed &&
2447 !s->thread_args.log_complete) {
2449 * Ask the log thread for required amount of commits.
2451 s->thread_args.commits_needed += maxscroll;
2452 err = trigger_log_thread(view, 1);
2453 if (err)
2454 return err;
2457 do {
2458 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2459 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2460 break;
2462 s->last_displayed_entry = pentry ?
2463 pentry : s->last_displayed_entry;;
2465 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2466 if (pentry == NULL)
2467 break;
2468 s->first_displayed_entry = pentry;
2469 } while (++nscrolled < maxscroll);
2471 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2472 view->nscrolled += nscrolled;
2473 else
2474 view->nscrolled = 0;
2476 return err;
2479 static const struct got_error *
2480 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2481 struct got_commit_object *commit, struct got_object_id *commit_id,
2482 struct tog_view *log_view, struct got_repository *repo)
2484 const struct got_error *err;
2485 struct got_object_qid *parent_id;
2486 struct tog_view *diff_view;
2488 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2489 if (diff_view == NULL)
2490 return got_error_from_errno("view_open");
2492 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2493 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2494 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2495 if (err == NULL)
2496 *new_view = diff_view;
2497 return err;
2500 static const struct got_error *
2501 tree_view_visit_subtree(struct tog_tree_view_state *s,
2502 struct got_tree_object *subtree)
2504 struct tog_parent_tree *parent;
2506 parent = calloc(1, sizeof(*parent));
2507 if (parent == NULL)
2508 return got_error_from_errno("calloc");
2510 parent->tree = s->tree;
2511 parent->first_displayed_entry = s->first_displayed_entry;
2512 parent->selected_entry = s->selected_entry;
2513 parent->selected = s->selected;
2514 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2515 s->tree = subtree;
2516 s->selected = 0;
2517 s->first_displayed_entry = NULL;
2518 return NULL;
2521 static const struct got_error *
2522 tree_view_walk_path(struct tog_tree_view_state *s,
2523 struct got_commit_object *commit, const char *path)
2525 const struct got_error *err = NULL;
2526 struct got_tree_object *tree = NULL;
2527 const char *p;
2528 char *slash, *subpath = NULL;
2530 /* Walk the path and open corresponding tree objects. */
2531 p = path;
2532 while (*p) {
2533 struct got_tree_entry *te;
2534 struct got_object_id *tree_id;
2535 char *te_name;
2537 while (p[0] == '/')
2538 p++;
2540 /* Ensure the correct subtree entry is selected. */
2541 slash = strchr(p, '/');
2542 if (slash == NULL)
2543 te_name = strdup(p);
2544 else
2545 te_name = strndup(p, slash - p);
2546 if (te_name == NULL) {
2547 err = got_error_from_errno("strndup");
2548 break;
2550 te = got_object_tree_find_entry(s->tree, te_name);
2551 if (te == NULL) {
2552 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2553 free(te_name);
2554 break;
2556 free(te_name);
2557 s->first_displayed_entry = s->selected_entry = te;
2559 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2560 break; /* jump to this file's entry */
2562 slash = strchr(p, '/');
2563 if (slash)
2564 subpath = strndup(path, slash - path);
2565 else
2566 subpath = strdup(path);
2567 if (subpath == NULL) {
2568 err = got_error_from_errno("strdup");
2569 break;
2572 err = got_object_id_by_path(&tree_id, s->repo, commit,
2573 subpath);
2574 if (err)
2575 break;
2577 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2578 free(tree_id);
2579 if (err)
2580 break;
2582 err = tree_view_visit_subtree(s, tree);
2583 if (err) {
2584 got_object_tree_close(tree);
2585 break;
2587 if (slash == NULL)
2588 break;
2589 free(subpath);
2590 subpath = NULL;
2591 p = slash;
2594 free(subpath);
2595 return err;
2598 static const struct got_error *
2599 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2600 struct commit_queue_entry *entry, const char *path,
2601 const char *head_ref_name, struct got_repository *repo)
2603 const struct got_error *err = NULL;
2604 struct tog_tree_view_state *s;
2605 struct tog_view *tree_view;
2607 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2608 if (tree_view == NULL)
2609 return got_error_from_errno("view_open");
2611 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2612 if (err)
2613 return err;
2614 s = &tree_view->state.tree;
2616 *new_view = tree_view;
2618 if (got_path_is_root_dir(path))
2619 return NULL;
2621 return tree_view_walk_path(s, entry->commit, path);
2624 static const struct got_error *
2625 block_signals_used_by_main_thread(void)
2627 sigset_t sigset;
2628 int errcode;
2630 if (sigemptyset(&sigset) == -1)
2631 return got_error_from_errno("sigemptyset");
2633 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2634 if (sigaddset(&sigset, SIGWINCH) == -1)
2635 return got_error_from_errno("sigaddset");
2636 if (sigaddset(&sigset, SIGCONT) == -1)
2637 return got_error_from_errno("sigaddset");
2638 if (sigaddset(&sigset, SIGINT) == -1)
2639 return got_error_from_errno("sigaddset");
2640 if (sigaddset(&sigset, SIGTERM) == -1)
2641 return got_error_from_errno("sigaddset");
2643 /* ncurses handles SIGTSTP */
2644 if (sigaddset(&sigset, SIGTSTP) == -1)
2645 return got_error_from_errno("sigaddset");
2647 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2648 if (errcode)
2649 return got_error_set_errno(errcode, "pthread_sigmask");
2651 return NULL;
2654 static void *
2655 log_thread(void *arg)
2657 const struct got_error *err = NULL;
2658 int errcode = 0;
2659 struct tog_log_thread_args *a = arg;
2660 int done = 0;
2663 * Sync startup with main thread such that we begin our
2664 * work once view_input() has released the mutex.
2666 errcode = pthread_mutex_lock(&tog_mutex);
2667 if (errcode) {
2668 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2669 return (void *)err;
2672 err = block_signals_used_by_main_thread();
2673 if (err) {
2674 pthread_mutex_unlock(&tog_mutex);
2675 goto done;
2678 while (!done && !err && !tog_fatal_signal_received()) {
2679 errcode = pthread_mutex_unlock(&tog_mutex);
2680 if (errcode) {
2681 err = got_error_set_errno(errcode,
2682 "pthread_mutex_unlock");
2683 goto done;
2685 err = queue_commits(a);
2686 if (err) {
2687 if (err->code != GOT_ERR_ITER_COMPLETED)
2688 goto done;
2689 err = NULL;
2690 done = 1;
2691 } else if (a->commits_needed > 0 && !a->load_all)
2692 a->commits_needed--;
2694 errcode = pthread_mutex_lock(&tog_mutex);
2695 if (errcode) {
2696 err = got_error_set_errno(errcode,
2697 "pthread_mutex_lock");
2698 goto done;
2699 } else if (*a->quit)
2700 done = 1;
2701 else if (*a->first_displayed_entry == NULL) {
2702 *a->first_displayed_entry =
2703 TAILQ_FIRST(&a->commits->head);
2704 *a->selected_entry = *a->first_displayed_entry;
2707 errcode = pthread_cond_signal(&a->commit_loaded);
2708 if (errcode) {
2709 err = got_error_set_errno(errcode,
2710 "pthread_cond_signal");
2711 pthread_mutex_unlock(&tog_mutex);
2712 goto done;
2715 if (done)
2716 a->commits_needed = 0;
2717 else {
2718 if (a->commits_needed == 0 && !a->load_all) {
2719 errcode = pthread_cond_wait(&a->need_commits,
2720 &tog_mutex);
2721 if (errcode) {
2722 err = got_error_set_errno(errcode,
2723 "pthread_cond_wait");
2724 pthread_mutex_unlock(&tog_mutex);
2725 goto done;
2727 if (*a->quit)
2728 done = 1;
2732 a->log_complete = 1;
2733 errcode = pthread_mutex_unlock(&tog_mutex);
2734 if (errcode)
2735 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2736 done:
2737 if (err) {
2738 tog_thread_error = 1;
2739 pthread_cond_signal(&a->commit_loaded);
2741 return (void *)err;
2744 static const struct got_error *
2745 stop_log_thread(struct tog_log_view_state *s)
2747 const struct got_error *err = NULL, *thread_err = NULL;
2748 int errcode;
2750 if (s->thread) {
2751 s->quit = 1;
2752 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2753 if (errcode)
2754 return got_error_set_errno(errcode,
2755 "pthread_cond_signal");
2756 errcode = pthread_mutex_unlock(&tog_mutex);
2757 if (errcode)
2758 return got_error_set_errno(errcode,
2759 "pthread_mutex_unlock");
2760 errcode = pthread_join(s->thread, (void **)&thread_err);
2761 if (errcode)
2762 return got_error_set_errno(errcode, "pthread_join");
2763 errcode = pthread_mutex_lock(&tog_mutex);
2764 if (errcode)
2765 return got_error_set_errno(errcode,
2766 "pthread_mutex_lock");
2767 s->thread = NULL;
2770 if (s->thread_args.repo) {
2771 err = got_repo_close(s->thread_args.repo);
2772 s->thread_args.repo = NULL;
2775 if (s->thread_args.pack_fds) {
2776 const struct got_error *pack_err =
2777 got_repo_pack_fds_close(s->thread_args.pack_fds);
2778 if (err == NULL)
2779 err = pack_err;
2780 s->thread_args.pack_fds = NULL;
2783 if (s->thread_args.graph) {
2784 got_commit_graph_close(s->thread_args.graph);
2785 s->thread_args.graph = NULL;
2788 return err ? err : thread_err;
2791 static const struct got_error *
2792 close_log_view(struct tog_view *view)
2794 const struct got_error *err = NULL;
2795 struct tog_log_view_state *s = &view->state.log;
2796 int errcode;
2798 err = stop_log_thread(s);
2800 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2801 if (errcode && err == NULL)
2802 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2804 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2805 if (errcode && err == NULL)
2806 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2808 free_commits(&s->commits);
2809 free(s->in_repo_path);
2810 s->in_repo_path = NULL;
2811 free(s->start_id);
2812 s->start_id = NULL;
2813 free(s->head_ref_name);
2814 s->head_ref_name = NULL;
2815 return err;
2818 static const struct got_error *
2819 search_start_log_view(struct tog_view *view)
2821 struct tog_log_view_state *s = &view->state.log;
2823 s->matched_entry = NULL;
2824 s->search_entry = NULL;
2825 return NULL;
2828 static const struct got_error *
2829 search_next_log_view(struct tog_view *view)
2831 const struct got_error *err = NULL;
2832 struct tog_log_view_state *s = &view->state.log;
2833 struct commit_queue_entry *entry;
2835 /* Display progress update in log view. */
2836 show_log_view(view);
2837 update_panels();
2838 doupdate();
2840 if (s->search_entry) {
2841 int errcode, ch;
2842 errcode = pthread_mutex_unlock(&tog_mutex);
2843 if (errcode)
2844 return got_error_set_errno(errcode,
2845 "pthread_mutex_unlock");
2846 ch = wgetch(view->window);
2847 errcode = pthread_mutex_lock(&tog_mutex);
2848 if (errcode)
2849 return got_error_set_errno(errcode,
2850 "pthread_mutex_lock");
2851 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2852 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2853 return NULL;
2855 if (view->searching == TOG_SEARCH_FORWARD)
2856 entry = TAILQ_NEXT(s->search_entry, entry);
2857 else
2858 entry = TAILQ_PREV(s->search_entry,
2859 commit_queue_head, entry);
2860 } else if (s->matched_entry) {
2861 int matched_idx = s->matched_entry->idx;
2862 int selected_idx = s->selected_entry->idx;
2865 * If the user has moved the cursor after we hit a match,
2866 * the position from where we should continue searching
2867 * might have changed.
2869 if (view->searching == TOG_SEARCH_FORWARD) {
2870 if (matched_idx > selected_idx)
2871 entry = TAILQ_NEXT(s->selected_entry, entry);
2872 else
2873 entry = TAILQ_NEXT(s->matched_entry, entry);
2874 } else {
2875 if (matched_idx < selected_idx)
2876 entry = TAILQ_PREV(s->selected_entry,
2877 commit_queue_head, entry);
2878 else
2879 entry = TAILQ_PREV(s->matched_entry,
2880 commit_queue_head, entry);
2882 } else {
2883 entry = s->selected_entry;
2886 while (1) {
2887 int have_match = 0;
2889 if (entry == NULL) {
2890 if (s->thread_args.log_complete ||
2891 view->searching == TOG_SEARCH_BACKWARD) {
2892 view->search_next_done =
2893 (s->matched_entry == NULL ?
2894 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2895 s->search_entry = NULL;
2896 return NULL;
2899 * Poke the log thread for more commits and return,
2900 * allowing the main loop to make progress. Search
2901 * will resume at s->search_entry once we come back.
2903 s->thread_args.commits_needed++;
2904 return trigger_log_thread(view, 0);
2907 err = match_commit(&have_match, entry->id, entry->commit,
2908 &view->regex);
2909 if (err)
2910 break;
2911 if (have_match) {
2912 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2913 s->matched_entry = entry;
2914 break;
2917 s->search_entry = entry;
2918 if (view->searching == TOG_SEARCH_FORWARD)
2919 entry = TAILQ_NEXT(entry, entry);
2920 else
2921 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2924 if (s->matched_entry) {
2925 int cur = s->selected_entry->idx;
2926 while (cur < s->matched_entry->idx) {
2927 err = input_log_view(NULL, view, KEY_DOWN);
2928 if (err)
2929 return err;
2930 cur++;
2932 while (cur > s->matched_entry->idx) {
2933 err = input_log_view(NULL, view, KEY_UP);
2934 if (err)
2935 return err;
2936 cur--;
2940 s->search_entry = NULL;
2942 return NULL;
2945 static const struct got_error *
2946 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2947 struct got_repository *repo, const char *head_ref_name,
2948 const char *in_repo_path, int log_branches)
2950 const struct got_error *err = NULL;
2951 struct tog_log_view_state *s = &view->state.log;
2952 struct got_repository *thread_repo = NULL;
2953 struct got_commit_graph *thread_graph = NULL;
2954 int errcode;
2956 if (in_repo_path != s->in_repo_path) {
2957 free(s->in_repo_path);
2958 s->in_repo_path = strdup(in_repo_path);
2959 if (s->in_repo_path == NULL)
2960 return got_error_from_errno("strdup");
2963 /* The commit queue only contains commits being displayed. */
2964 TAILQ_INIT(&s->commits.head);
2965 s->commits.ncommits = 0;
2967 s->repo = repo;
2968 if (head_ref_name) {
2969 s->head_ref_name = strdup(head_ref_name);
2970 if (s->head_ref_name == NULL) {
2971 err = got_error_from_errno("strdup");
2972 goto done;
2975 s->start_id = got_object_id_dup(start_id);
2976 if (s->start_id == NULL) {
2977 err = got_error_from_errno("got_object_id_dup");
2978 goto done;
2980 s->log_branches = log_branches;
2982 STAILQ_INIT(&s->colors);
2983 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2984 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2985 get_color_value("TOG_COLOR_COMMIT"));
2986 if (err)
2987 goto done;
2988 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2989 get_color_value("TOG_COLOR_AUTHOR"));
2990 if (err) {
2991 free_colors(&s->colors);
2992 goto done;
2994 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2995 get_color_value("TOG_COLOR_DATE"));
2996 if (err) {
2997 free_colors(&s->colors);
2998 goto done;
3002 view->show = show_log_view;
3003 view->input = input_log_view;
3004 view->resize = resize_log_view;
3005 view->close = close_log_view;
3006 view->search_start = search_start_log_view;
3007 view->search_next = search_next_log_view;
3009 if (s->thread_args.pack_fds == NULL) {
3010 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3011 if (err)
3012 goto done;
3014 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3015 s->thread_args.pack_fds);
3016 if (err)
3017 goto done;
3018 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3019 !s->log_branches);
3020 if (err)
3021 goto done;
3022 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3023 s->repo, NULL, NULL);
3024 if (err)
3025 goto done;
3027 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3028 if (errcode) {
3029 err = got_error_set_errno(errcode, "pthread_cond_init");
3030 goto done;
3032 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3033 if (errcode) {
3034 err = got_error_set_errno(errcode, "pthread_cond_init");
3035 goto done;
3038 s->thread_args.commits_needed = view->nlines;
3039 s->thread_args.graph = thread_graph;
3040 s->thread_args.commits = &s->commits;
3041 s->thread_args.in_repo_path = s->in_repo_path;
3042 s->thread_args.start_id = s->start_id;
3043 s->thread_args.repo = thread_repo;
3044 s->thread_args.log_complete = 0;
3045 s->thread_args.quit = &s->quit;
3046 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3047 s->thread_args.selected_entry = &s->selected_entry;
3048 s->thread_args.searching = &view->searching;
3049 s->thread_args.search_next_done = &view->search_next_done;
3050 s->thread_args.regex = &view->regex;
3051 done:
3052 if (err)
3053 close_log_view(view);
3054 return err;
3057 static const struct got_error *
3058 show_log_view(struct tog_view *view)
3060 const struct got_error *err;
3061 struct tog_log_view_state *s = &view->state.log;
3063 if (s->thread == NULL) {
3064 int errcode = pthread_create(&s->thread, NULL, log_thread,
3065 &s->thread_args);
3066 if (errcode)
3067 return got_error_set_errno(errcode, "pthread_create");
3068 if (s->thread_args.commits_needed > 0) {
3069 err = trigger_log_thread(view, 1);
3070 if (err)
3071 return err;
3075 return draw_commits(view);
3078 static void
3079 log_move_cursor_up(struct tog_view *view, int page, int home)
3081 struct tog_log_view_state *s = &view->state.log;
3083 if (s->selected_entry->idx == 0)
3084 view->count = 0;
3085 if (s->first_displayed_entry == NULL)
3086 return;
3088 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3089 || home)
3090 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3092 if (!page && !home && s->selected > 0)
3093 --s->selected;
3094 else
3095 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3097 select_commit(s);
3098 return;
3101 static const struct got_error *
3102 log_move_cursor_down(struct tog_view *view, int page)
3104 struct tog_log_view_state *s = &view->state.log;
3105 struct commit_queue_entry *first;
3106 const struct got_error *err = NULL;
3108 first = s->first_displayed_entry;
3109 if (first == NULL) {
3110 view->count = 0;
3111 return NULL;
3114 if (s->thread_args.log_complete &&
3115 s->selected_entry->idx >= s->commits.ncommits - 1)
3116 return NULL;
3118 if (!page) {
3119 int eos = view->nlines - 2;
3121 if (view_is_hsplit_top(view))
3122 --eos; /* border consumes the last line */
3123 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3124 ++s->selected;
3125 else
3126 err = log_scroll_down(view, 1);
3127 } else if (s->thread_args.load_all) {
3128 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3129 s->selected += MIN(s->last_displayed_entry->idx -
3130 s->selected_entry->idx, page + 1);
3131 else
3132 err = log_scroll_down(view, MIN(page,
3133 s->commits.ncommits - s->selected_entry->idx - 1));
3134 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3135 } else {
3136 err = log_scroll_down(view, page);
3137 if (err)
3138 return err;
3139 if (first == s->first_displayed_entry && s->selected <
3140 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3141 s->selected = MIN(s->commits.ncommits - 1, page);
3144 if (err)
3145 return err;
3148 * We might necessarily overshoot in horizontal
3149 * splits; if so, select the last displayed commit.
3151 s->selected = MIN(s->selected,
3152 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3154 select_commit(s);
3156 if (s->thread_args.log_complete &&
3157 s->selected_entry->idx == s->commits.ncommits - 1)
3158 view->count = 0;
3160 return NULL;
3163 static void
3164 view_get_split(struct tog_view *view, int *y, int *x)
3166 *x = 0;
3167 *y = 0;
3169 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3170 if (view->child && view->child->resized_y)
3171 *y = view->child->resized_y;
3172 else if (view->resized_y)
3173 *y = view->resized_y;
3174 else
3175 *y = view_split_begin_y(view->lines);
3176 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3177 if (view->child && view->child->resized_x)
3178 *x = view->child->resized_x;
3179 else if (view->resized_x)
3180 *x = view->resized_x;
3181 else
3182 *x = view_split_begin_x(view->begin_x);
3186 /* Split view horizontally at y and offset view->state->selected line. */
3187 static const struct got_error *
3188 view_init_hsplit(struct tog_view *view, int y)
3190 const struct got_error *err = NULL;
3192 view->nlines = y;
3193 view->ncols = COLS;
3194 err = view_resize(view);
3195 if (err)
3196 return err;
3198 err = offset_selection_down(view);
3200 return err;
3203 static const struct got_error *
3204 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3206 const struct got_error *err = NULL;
3207 struct tog_log_view_state *s = &view->state.log;
3208 struct tog_view *diff_view = NULL, *tree_view = NULL;
3209 struct tog_view *ref_view = NULL;
3210 struct commit_queue_entry *entry;
3211 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3213 if (s->thread_args.load_all) {
3214 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3215 s->thread_args.load_all = 0;
3216 else if (s->thread_args.log_complete) {
3217 err = log_move_cursor_down(view, s->commits.ncommits);
3218 s->thread_args.load_all = 0;
3220 return err;
3223 eos = nscroll = view->nlines - 1;
3224 if (view_is_hsplit_top(view))
3225 --eos; /* border */
3227 switch (ch) {
3228 case 'q':
3229 s->quit = 1;
3230 break;
3231 case '0':
3232 view->x = 0;
3233 break;
3234 case '$':
3235 view->x = MAX(view->maxx - view->ncols / 2, 0);
3236 view->count = 0;
3237 break;
3238 case KEY_RIGHT:
3239 case 'l':
3240 if (view->x + view->ncols / 2 < view->maxx)
3241 view->x += 2; /* move two columns right */
3242 else
3243 view->count = 0;
3244 break;
3245 case KEY_LEFT:
3246 case 'h':
3247 view->x -= MIN(view->x, 2); /* move two columns back */
3248 if (view->x <= 0)
3249 view->count = 0;
3250 break;
3251 case 'k':
3252 case KEY_UP:
3253 case '<':
3254 case ',':
3255 case CTRL('p'):
3256 log_move_cursor_up(view, 0, 0);
3257 break;
3258 case 'g':
3259 case KEY_HOME:
3260 log_move_cursor_up(view, 0, 1);
3261 view->count = 0;
3262 break;
3263 case CTRL('u'):
3264 case 'u':
3265 nscroll /= 2;
3266 /* FALL THROUGH */
3267 case KEY_PPAGE:
3268 case CTRL('b'):
3269 case 'b':
3270 log_move_cursor_up(view, nscroll, 0);
3271 break;
3272 case 'j':
3273 case KEY_DOWN:
3274 case '>':
3275 case '.':
3276 case CTRL('n'):
3277 err = log_move_cursor_down(view, 0);
3278 break;
3279 case '@':
3280 s->use_committer = !s->use_committer;
3281 break;
3282 case 'G':
3283 case KEY_END: {
3284 /* We don't know yet how many commits, so we're forced to
3285 * traverse them all. */
3286 view->count = 0;
3287 if (!s->thread_args.log_complete) {
3288 s->thread_args.load_all = 1;
3289 return trigger_log_thread(view, 0);
3292 s->selected = 0;
3293 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3294 for (n = 0; n < eos; n++) {
3295 if (entry == NULL)
3296 break;
3297 s->first_displayed_entry = entry;
3298 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3300 if (n > 0)
3301 s->selected = n - 1;
3302 select_commit(s);
3303 break;
3305 case CTRL('d'):
3306 case 'd':
3307 nscroll /= 2;
3308 /* FALL THROUGH */
3309 case KEY_NPAGE:
3310 case CTRL('f'):
3311 case 'f':
3312 case ' ':
3313 err = log_move_cursor_down(view, nscroll);
3314 break;
3315 case KEY_RESIZE:
3316 if (s->selected > view->nlines - 2)
3317 s->selected = view->nlines - 2;
3318 if (s->selected > s->commits.ncommits - 1)
3319 s->selected = s->commits.ncommits - 1;
3320 select_commit(s);
3321 if (s->commits.ncommits < view->nlines - 1 &&
3322 !s->thread_args.log_complete) {
3323 s->thread_args.commits_needed += (view->nlines - 1) -
3324 s->commits.ncommits;
3325 err = trigger_log_thread(view, 1);
3327 break;
3328 case KEY_ENTER:
3329 case '\r':
3330 view->count = 0;
3331 if (s->selected_entry == NULL)
3332 break;
3334 /* get dimensions--don't split till initialisation succeeds */
3335 if (view_is_parent_view(view))
3336 view_get_split(view, &begin_y, &begin_x);
3338 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3339 s->selected_entry->commit, s->selected_entry->id,
3340 view, s->repo);
3341 if (err)
3342 break;
3344 if (view_is_parent_view(view) &&
3345 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3346 err = view_init_hsplit(view, begin_y);
3347 if (err)
3348 break;
3351 view->focussed = 0;
3352 diff_view->focussed = 1;
3353 diff_view->mode = view->mode;
3354 diff_view->nlines = view->lines - begin_y;
3356 if (view_is_parent_view(view)) {
3357 view_transfer_size(diff_view, view);
3358 err = view_close_child(view);
3359 if (err)
3360 return err;
3361 err = view_set_child(view, diff_view);
3362 if (err)
3363 return err;
3364 view->focus_child = 1;
3365 } else
3366 *new_view = diff_view;
3367 break;
3368 case 't':
3369 view->count = 0;
3370 if (s->selected_entry == NULL)
3371 break;
3372 if (view_is_parent_view(view))
3373 view_get_split(view, &begin_y, &begin_x);
3374 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3375 s->selected_entry, s->in_repo_path, s->head_ref_name,
3376 s->repo);
3377 if (err)
3378 break;
3379 if (view_is_parent_view(view) &&
3380 view->mode == TOG_VIEW_SPLIT_HRZN) {
3381 err = view_init_hsplit(view, begin_y);
3382 if (err)
3383 break;
3385 view->focussed = 0;
3386 tree_view->focussed = 1;
3387 tree_view->mode = view->mode;
3388 tree_view->nlines = view->lines - begin_y;
3389 if (view_is_parent_view(view)) {
3390 view_transfer_size(tree_view, view);
3391 err = view_close_child(view);
3392 if (err)
3393 return err;
3394 err = view_set_child(view, tree_view);
3395 if (err)
3396 return err;
3397 view->focus_child = 1;
3398 } else
3399 *new_view = tree_view;
3400 break;
3401 case KEY_BACKSPACE:
3402 case CTRL('l'):
3403 case 'B':
3404 view->count = 0;
3405 if (ch == KEY_BACKSPACE &&
3406 got_path_is_root_dir(s->in_repo_path))
3407 break;
3408 err = stop_log_thread(s);
3409 if (err)
3410 return err;
3411 if (ch == KEY_BACKSPACE) {
3412 char *parent_path;
3413 err = got_path_dirname(&parent_path, s->in_repo_path);
3414 if (err)
3415 return err;
3416 free(s->in_repo_path);
3417 s->in_repo_path = parent_path;
3418 s->thread_args.in_repo_path = s->in_repo_path;
3419 } else if (ch == CTRL('l')) {
3420 struct got_object_id *start_id;
3421 err = got_repo_match_object_id(&start_id, NULL,
3422 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3423 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3424 if (err)
3425 return err;
3426 free(s->start_id);
3427 s->start_id = start_id;
3428 s->thread_args.start_id = s->start_id;
3429 } else /* 'B' */
3430 s->log_branches = !s->log_branches;
3432 if (s->thread_args.pack_fds == NULL) {
3433 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3434 if (err)
3435 return err;
3437 err = got_repo_open(&s->thread_args.repo,
3438 got_repo_get_path(s->repo), NULL,
3439 s->thread_args.pack_fds);
3440 if (err)
3441 return err;
3442 tog_free_refs();
3443 err = tog_load_refs(s->repo, 0);
3444 if (err)
3445 return err;
3446 err = got_commit_graph_open(&s->thread_args.graph,
3447 s->in_repo_path, !s->log_branches);
3448 if (err)
3449 return err;
3450 err = got_commit_graph_iter_start(s->thread_args.graph,
3451 s->start_id, s->repo, NULL, NULL);
3452 if (err)
3453 return err;
3454 free_commits(&s->commits);
3455 s->first_displayed_entry = NULL;
3456 s->last_displayed_entry = NULL;
3457 s->selected_entry = NULL;
3458 s->selected = 0;
3459 s->thread_args.log_complete = 0;
3460 s->quit = 0;
3461 s->thread_args.commits_needed = view->lines;
3462 s->matched_entry = NULL;
3463 s->search_entry = NULL;
3464 break;
3465 case 'r':
3466 view->count = 0;
3467 if (view_is_parent_view(view))
3468 view_get_split(view, &begin_y, &begin_x);
3469 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3470 if (ref_view == NULL)
3471 return got_error_from_errno("view_open");
3472 err = open_ref_view(ref_view, s->repo);
3473 if (err) {
3474 view_close(ref_view);
3475 return err;
3477 if (view_is_parent_view(view) &&
3478 view->mode == TOG_VIEW_SPLIT_HRZN) {
3479 err = view_init_hsplit(view, begin_y);
3480 if (err)
3481 break;
3483 view->focussed = 0;
3484 ref_view->focussed = 1;
3485 ref_view->mode = view->mode;
3486 ref_view->nlines = view->lines - begin_y;
3487 if (view_is_parent_view(view)) {
3488 view_transfer_size(ref_view, view);
3489 err = view_close_child(view);
3490 if (err)
3491 return err;
3492 err = view_set_child(view, ref_view);
3493 if (err)
3494 return err;
3495 view->focus_child = 1;
3496 } else
3497 *new_view = ref_view;
3498 break;
3499 default:
3500 view->count = 0;
3501 break;
3504 return err;
3507 static const struct got_error *
3508 apply_unveil(const char *repo_path, const char *worktree_path)
3510 const struct got_error *error;
3512 #ifdef PROFILE
3513 if (unveil("gmon.out", "rwc") != 0)
3514 return got_error_from_errno2("unveil", "gmon.out");
3515 #endif
3516 if (repo_path && unveil(repo_path, "r") != 0)
3517 return got_error_from_errno2("unveil", repo_path);
3519 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3520 return got_error_from_errno2("unveil", worktree_path);
3522 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3523 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3525 error = got_privsep_unveil_exec_helpers();
3526 if (error != NULL)
3527 return error;
3529 if (unveil(NULL, NULL) != 0)
3530 return got_error_from_errno("unveil");
3532 return NULL;
3535 static void
3536 init_curses(void)
3539 * Override default signal handlers before starting ncurses.
3540 * This should prevent ncurses from installing its own
3541 * broken cleanup() signal handler.
3543 signal(SIGWINCH, tog_sigwinch);
3544 signal(SIGPIPE, tog_sigpipe);
3545 signal(SIGCONT, tog_sigcont);
3546 signal(SIGINT, tog_sigint);
3547 signal(SIGTERM, tog_sigterm);
3549 initscr();
3550 cbreak();
3551 halfdelay(1); /* Do fast refresh while initial view is loading. */
3552 noecho();
3553 nonl();
3554 intrflush(stdscr, FALSE);
3555 keypad(stdscr, TRUE);
3556 curs_set(0);
3557 if (getenv("TOG_COLORS") != NULL) {
3558 start_color();
3559 use_default_colors();
3563 static const struct got_error *
3564 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3565 struct got_repository *repo, struct got_worktree *worktree)
3567 const struct got_error *err = NULL;
3569 if (argc == 0) {
3570 *in_repo_path = strdup("/");
3571 if (*in_repo_path == NULL)
3572 return got_error_from_errno("strdup");
3573 return NULL;
3576 if (worktree) {
3577 const char *prefix = got_worktree_get_path_prefix(worktree);
3578 char *p;
3580 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3581 if (err)
3582 return err;
3583 if (asprintf(in_repo_path, "%s%s%s", prefix,
3584 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3585 p) == -1) {
3586 err = got_error_from_errno("asprintf");
3587 *in_repo_path = NULL;
3589 free(p);
3590 } else
3591 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3593 return err;
3596 static const struct got_error *
3597 cmd_log(int argc, char *argv[])
3599 const struct got_error *error;
3600 struct got_repository *repo = NULL;
3601 struct got_worktree *worktree = NULL;
3602 struct got_object_id *start_id = NULL;
3603 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3604 char *start_commit = NULL, *label = NULL;
3605 struct got_reference *ref = NULL;
3606 const char *head_ref_name = NULL;
3607 int ch, log_branches = 0;
3608 struct tog_view *view;
3609 int *pack_fds = NULL;
3611 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3612 switch (ch) {
3613 case 'b':
3614 log_branches = 1;
3615 break;
3616 case 'c':
3617 start_commit = optarg;
3618 break;
3619 case 'r':
3620 repo_path = realpath(optarg, NULL);
3621 if (repo_path == NULL)
3622 return got_error_from_errno2("realpath",
3623 optarg);
3624 break;
3625 default:
3626 usage_log();
3627 /* NOTREACHED */
3631 argc -= optind;
3632 argv += optind;
3634 if (argc > 1)
3635 usage_log();
3637 error = got_repo_pack_fds_open(&pack_fds);
3638 if (error != NULL)
3639 goto done;
3641 if (repo_path == NULL) {
3642 cwd = getcwd(NULL, 0);
3643 if (cwd == NULL)
3644 return got_error_from_errno("getcwd");
3645 error = got_worktree_open(&worktree, cwd);
3646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3647 goto done;
3648 if (worktree)
3649 repo_path =
3650 strdup(got_worktree_get_repo_path(worktree));
3651 else
3652 repo_path = strdup(cwd);
3653 if (repo_path == NULL) {
3654 error = got_error_from_errno("strdup");
3655 goto done;
3659 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3660 if (error != NULL)
3661 goto done;
3663 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3664 repo, worktree);
3665 if (error)
3666 goto done;
3668 init_curses();
3670 error = apply_unveil(got_repo_get_path(repo),
3671 worktree ? got_worktree_get_root_path(worktree) : NULL);
3672 if (error)
3673 goto done;
3675 /* already loaded by tog_log_with_path()? */
3676 if (TAILQ_EMPTY(&tog_refs)) {
3677 error = tog_load_refs(repo, 0);
3678 if (error)
3679 goto done;
3682 if (start_commit == NULL) {
3683 error = got_repo_match_object_id(&start_id, &label,
3684 worktree ? got_worktree_get_head_ref_name(worktree) :
3685 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3686 if (error)
3687 goto done;
3688 head_ref_name = label;
3689 } else {
3690 error = got_ref_open(&ref, repo, start_commit, 0);
3691 if (error == NULL)
3692 head_ref_name = got_ref_get_name(ref);
3693 else if (error->code != GOT_ERR_NOT_REF)
3694 goto done;
3695 error = got_repo_match_object_id(&start_id, NULL,
3696 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3697 if (error)
3698 goto done;
3701 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3702 if (view == NULL) {
3703 error = got_error_from_errno("view_open");
3704 goto done;
3706 error = open_log_view(view, start_id, repo, head_ref_name,
3707 in_repo_path, log_branches);
3708 if (error)
3709 goto done;
3710 if (worktree) {
3711 /* Release work tree lock. */
3712 got_worktree_close(worktree);
3713 worktree = NULL;
3715 error = view_loop(view);
3716 done:
3717 free(in_repo_path);
3718 free(repo_path);
3719 free(cwd);
3720 free(start_id);
3721 free(label);
3722 if (ref)
3723 got_ref_close(ref);
3724 if (repo) {
3725 const struct got_error *close_err = got_repo_close(repo);
3726 if (error == NULL)
3727 error = close_err;
3729 if (worktree)
3730 got_worktree_close(worktree);
3731 if (pack_fds) {
3732 const struct got_error *pack_err =
3733 got_repo_pack_fds_close(pack_fds);
3734 if (error == NULL)
3735 error = pack_err;
3737 tog_free_refs();
3738 return error;
3741 __dead static void
3742 usage_diff(void)
3744 endwin();
3745 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3746 "[-w] object1 object2\n", getprogname());
3747 exit(1);
3750 static int
3751 match_line(const char *line, regex_t *regex, size_t nmatch,
3752 regmatch_t *regmatch)
3754 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3757 static struct tog_color *
3758 match_color(struct tog_colors *colors, const char *line)
3760 struct tog_color *tc = NULL;
3762 STAILQ_FOREACH(tc, colors, entry) {
3763 if (match_line(line, &tc->regex, 0, NULL))
3764 return tc;
3767 return NULL;
3770 static const struct got_error *
3771 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3772 WINDOW *window, int skipcol, regmatch_t *regmatch)
3774 const struct got_error *err = NULL;
3775 char *exstr = NULL;
3776 wchar_t *wline = NULL;
3777 int rme, rms, n, width, scrollx;
3778 int width0 = 0, width1 = 0, width2 = 0;
3779 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3781 *wtotal = 0;
3783 rms = regmatch->rm_so;
3784 rme = regmatch->rm_eo;
3786 err = expand_tab(&exstr, line);
3787 if (err)
3788 return err;
3790 /* Split the line into 3 segments, according to match offsets. */
3791 seg0 = strndup(exstr, rms);
3792 if (seg0 == NULL) {
3793 err = got_error_from_errno("strndup");
3794 goto done;
3796 seg1 = strndup(exstr + rms, rme - rms);
3797 if (seg1 == NULL) {
3798 err = got_error_from_errno("strndup");
3799 goto done;
3801 seg2 = strdup(exstr + rme);
3802 if (seg2 == NULL) {
3803 err = got_error_from_errno("strndup");
3804 goto done;
3807 /* draw up to matched token if we haven't scrolled past it */
3808 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3809 col_tab_align, 1);
3810 if (err)
3811 goto done;
3812 n = MAX(width0 - skipcol, 0);
3813 if (n) {
3814 free(wline);
3815 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3816 wlimit, col_tab_align, 1);
3817 if (err)
3818 goto done;
3819 waddwstr(window, &wline[scrollx]);
3820 wlimit -= width;
3821 *wtotal += width;
3824 if (wlimit > 0) {
3825 int i = 0, w = 0;
3826 size_t wlen;
3828 free(wline);
3829 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3830 col_tab_align, 1);
3831 if (err)
3832 goto done;
3833 wlen = wcslen(wline);
3834 while (i < wlen) {
3835 width = wcwidth(wline[i]);
3836 if (width == -1) {
3837 /* should not happen, tabs are expanded */
3838 err = got_error(GOT_ERR_RANGE);
3839 goto done;
3841 if (width0 + w + width > skipcol)
3842 break;
3843 w += width;
3844 i++;
3846 /* draw (visible part of) matched token (if scrolled into it) */
3847 if (width1 - w > 0) {
3848 wattron(window, A_STANDOUT);
3849 waddwstr(window, &wline[i]);
3850 wattroff(window, A_STANDOUT);
3851 wlimit -= (width1 - w);
3852 *wtotal += (width1 - w);
3856 if (wlimit > 0) { /* draw rest of line */
3857 free(wline);
3858 if (skipcol > width0 + width1) {
3859 err = format_line(&wline, &width2, &scrollx, seg2,
3860 skipcol - (width0 + width1), wlimit,
3861 col_tab_align, 1);
3862 if (err)
3863 goto done;
3864 waddwstr(window, &wline[scrollx]);
3865 } else {
3866 err = format_line(&wline, &width2, NULL, seg2, 0,
3867 wlimit, col_tab_align, 1);
3868 if (err)
3869 goto done;
3870 waddwstr(window, wline);
3872 *wtotal += width2;
3874 done:
3875 free(wline);
3876 free(exstr);
3877 free(seg0);
3878 free(seg1);
3879 free(seg2);
3880 return err;
3883 static const struct got_error *
3884 draw_file(struct tog_view *view, const char *header)
3886 struct tog_diff_view_state *s = &view->state.diff;
3887 regmatch_t *regmatch = &view->regmatch;
3888 const struct got_error *err;
3889 int nprinted = 0;
3890 char *line;
3891 size_t linesize = 0;
3892 ssize_t linelen;
3893 struct tog_color *tc;
3894 wchar_t *wline;
3895 int width;
3896 int max_lines = view->nlines;
3897 int nlines = s->nlines;
3898 off_t line_offset;
3900 line_offset = s->line_offsets[s->first_displayed_line - 1];
3901 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3902 return got_error_from_errno("fseek");
3904 werase(view->window);
3906 if (header) {
3907 if (asprintf(&line, "[%d/%d] %s",
3908 s->first_displayed_line - 1 + s->selected_line, nlines,
3909 header) == -1)
3910 return got_error_from_errno("asprintf");
3911 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3912 0, 0);
3913 free(line);
3914 if (err)
3915 return err;
3917 if (view_needs_focus_indication(view))
3918 wstandout(view->window);
3919 waddwstr(view->window, wline);
3920 free(wline);
3921 wline = NULL;
3922 if (view_needs_focus_indication(view))
3923 wstandend(view->window);
3924 if (width <= view->ncols - 1)
3925 waddch(view->window, '\n');
3927 if (max_lines <= 1)
3928 return NULL;
3929 max_lines--;
3932 s->eof = 0;
3933 view->maxx = 0;
3934 line = NULL;
3935 while (max_lines > 0 && nprinted < max_lines) {
3936 linelen = getline(&line, &linesize, s->f);
3937 if (linelen == -1) {
3938 if (feof(s->f)) {
3939 s->eof = 1;
3940 break;
3942 free(line);
3943 return got_ferror(s->f, GOT_ERR_IO);
3946 /* Set view->maxx based on full line length. */
3947 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3948 view->x ? 1 : 0);
3949 if (err) {
3950 free(line);
3951 return err;
3953 view->maxx = MAX(view->maxx, width);
3954 free(wline);
3955 wline = NULL;
3957 tc = match_color(&s->colors, line);
3958 if (tc)
3959 wattr_on(view->window,
3960 COLOR_PAIR(tc->colorpair), NULL);
3961 if (s->first_displayed_line + nprinted == s->matched_line &&
3962 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3963 err = add_matched_line(&width, line, view->ncols, 0,
3964 view->window, view->x, regmatch);
3965 if (err) {
3966 free(line);
3967 return err;
3969 } else {
3970 int skip;
3971 err = format_line(&wline, &width, &skip, line,
3972 view->x, view->ncols, 0, view->x ? 1 : 0);
3973 if (err) {
3974 free(line);
3975 return err;
3977 waddwstr(view->window, &wline[skip]);
3978 free(wline);
3979 wline = NULL;
3981 if (tc)
3982 wattr_off(view->window,
3983 COLOR_PAIR(tc->colorpair), NULL);
3984 if (width <= view->ncols - 1)
3985 waddch(view->window, '\n');
3986 nprinted++;
3988 free(line);
3989 if (nprinted >= 1)
3990 s->last_displayed_line = s->first_displayed_line +
3991 (nprinted - 1);
3992 else
3993 s->last_displayed_line = s->first_displayed_line;
3995 view_border(view);
3997 if (s->eof) {
3998 while (nprinted < view->nlines) {
3999 waddch(view->window, '\n');
4000 nprinted++;
4003 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4004 view->ncols, 0, 0);
4005 if (err) {
4006 return err;
4009 wstandout(view->window);
4010 waddwstr(view->window, wline);
4011 free(wline);
4012 wline = NULL;
4013 wstandend(view->window);
4016 return NULL;
4019 static char *
4020 get_datestr(time_t *time, char *datebuf)
4022 struct tm mytm, *tm;
4023 char *p, *s;
4025 tm = gmtime_r(time, &mytm);
4026 if (tm == NULL)
4027 return NULL;
4028 s = asctime_r(tm, datebuf);
4029 if (s == NULL)
4030 return NULL;
4031 p = strchr(s, '\n');
4032 if (p)
4033 *p = '\0';
4034 return s;
4037 static const struct got_error *
4038 get_changed_paths(struct got_pathlist_head *paths,
4039 struct got_commit_object *commit, struct got_repository *repo)
4041 const struct got_error *err = NULL;
4042 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4043 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4044 struct got_object_qid *qid;
4046 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4047 if (qid != NULL) {
4048 struct got_commit_object *pcommit;
4049 err = got_object_open_as_commit(&pcommit, repo,
4050 &qid->id);
4051 if (err)
4052 return err;
4054 tree_id1 = got_object_id_dup(
4055 got_object_commit_get_tree_id(pcommit));
4056 if (tree_id1 == NULL) {
4057 got_object_commit_close(pcommit);
4058 return got_error_from_errno("got_object_id_dup");
4060 got_object_commit_close(pcommit);
4064 if (tree_id1) {
4065 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4066 if (err)
4067 goto done;
4070 tree_id2 = got_object_commit_get_tree_id(commit);
4071 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4072 if (err)
4073 goto done;
4075 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4076 got_diff_tree_collect_changed_paths, paths, 0);
4077 done:
4078 if (tree1)
4079 got_object_tree_close(tree1);
4080 if (tree2)
4081 got_object_tree_close(tree2);
4082 free(tree_id1);
4083 return err;
4086 static const struct got_error *
4087 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4089 off_t *p;
4091 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4092 if (p == NULL)
4093 return got_error_from_errno("reallocarray");
4094 *line_offsets = p;
4095 (*line_offsets)[*nlines] = off;
4096 (*nlines)++;
4097 return NULL;
4100 static const struct got_error *
4101 write_commit_info(off_t **line_offsets, size_t *nlines,
4102 struct got_object_id *commit_id, struct got_reflist_head *refs,
4103 struct got_repository *repo, FILE *outfile)
4105 const struct got_error *err = NULL;
4106 char datebuf[26], *datestr;
4107 struct got_commit_object *commit;
4108 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4109 time_t committer_time;
4110 const char *author, *committer;
4111 char *refs_str = NULL;
4112 struct got_pathlist_head changed_paths;
4113 struct got_pathlist_entry *pe;
4114 off_t outoff = 0;
4115 int n;
4117 TAILQ_INIT(&changed_paths);
4119 if (refs) {
4120 err = build_refs_str(&refs_str, refs, commit_id, repo);
4121 if (err)
4122 return err;
4125 err = got_object_open_as_commit(&commit, repo, commit_id);
4126 if (err)
4127 return err;
4129 err = got_object_id_str(&id_str, commit_id);
4130 if (err) {
4131 err = got_error_from_errno("got_object_id_str");
4132 goto done;
4135 err = add_line_offset(line_offsets, nlines, 0);
4136 if (err)
4137 goto done;
4139 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4140 refs_str ? refs_str : "", refs_str ? ")" : "");
4141 if (n < 0) {
4142 err = got_error_from_errno("fprintf");
4143 goto done;
4145 outoff += n;
4146 err = add_line_offset(line_offsets, nlines, outoff);
4147 if (err)
4148 goto done;
4150 n = fprintf(outfile, "from: %s\n",
4151 got_object_commit_get_author(commit));
4152 if (n < 0) {
4153 err = got_error_from_errno("fprintf");
4154 goto done;
4156 outoff += n;
4157 err = add_line_offset(line_offsets, nlines, outoff);
4158 if (err)
4159 goto done;
4161 committer_time = got_object_commit_get_committer_time(commit);
4162 datestr = get_datestr(&committer_time, datebuf);
4163 if (datestr) {
4164 n = fprintf(outfile, "date: %s UTC\n", datestr);
4165 if (n < 0) {
4166 err = got_error_from_errno("fprintf");
4167 goto done;
4169 outoff += n;
4170 err = add_line_offset(line_offsets, nlines, outoff);
4171 if (err)
4172 goto done;
4174 author = got_object_commit_get_author(commit);
4175 committer = got_object_commit_get_committer(commit);
4176 if (strcmp(author, committer) != 0) {
4177 n = fprintf(outfile, "via: %s\n", committer);
4178 if (n < 0) {
4179 err = got_error_from_errno("fprintf");
4180 goto done;
4182 outoff += n;
4183 err = add_line_offset(line_offsets, nlines, outoff);
4184 if (err)
4185 goto done;
4187 if (got_object_commit_get_nparents(commit) > 1) {
4188 const struct got_object_id_queue *parent_ids;
4189 struct got_object_qid *qid;
4190 int pn = 1;
4191 parent_ids = got_object_commit_get_parent_ids(commit);
4192 STAILQ_FOREACH(qid, parent_ids, entry) {
4193 err = got_object_id_str(&id_str, &qid->id);
4194 if (err)
4195 goto done;
4196 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4197 if (n < 0) {
4198 err = got_error_from_errno("fprintf");
4199 goto done;
4201 outoff += n;
4202 err = add_line_offset(line_offsets, nlines, outoff);
4203 if (err)
4204 goto done;
4205 free(id_str);
4206 id_str = NULL;
4210 err = got_object_commit_get_logmsg(&logmsg, commit);
4211 if (err)
4212 goto done;
4213 s = logmsg;
4214 while ((line = strsep(&s, "\n")) != NULL) {
4215 n = fprintf(outfile, "%s\n", line);
4216 if (n < 0) {
4217 err = got_error_from_errno("fprintf");
4218 goto done;
4220 outoff += n;
4221 err = add_line_offset(line_offsets, nlines, outoff);
4222 if (err)
4223 goto done;
4226 err = get_changed_paths(&changed_paths, commit, repo);
4227 if (err)
4228 goto done;
4229 TAILQ_FOREACH(pe, &changed_paths, entry) {
4230 struct got_diff_changed_path *cp = pe->data;
4231 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4232 if (n < 0) {
4233 err = got_error_from_errno("fprintf");
4234 goto done;
4236 outoff += n;
4237 err = add_line_offset(line_offsets, nlines, outoff);
4238 if (err)
4239 goto done;
4240 free((char *)pe->path);
4241 free(pe->data);
4244 fputc('\n', outfile);
4245 outoff++;
4246 err = add_line_offset(line_offsets, nlines, outoff);
4247 done:
4248 got_pathlist_free(&changed_paths);
4249 free(id_str);
4250 free(logmsg);
4251 free(refs_str);
4252 got_object_commit_close(commit);
4253 if (err) {
4254 free(*line_offsets);
4255 *line_offsets = NULL;
4256 *nlines = 0;
4258 return err;
4261 static const struct got_error *
4262 create_diff(struct tog_diff_view_state *s)
4264 const struct got_error *err = NULL;
4265 FILE *f = NULL;
4266 int obj_type;
4268 free(s->line_offsets);
4269 s->line_offsets = malloc(sizeof(off_t));
4270 if (s->line_offsets == NULL)
4271 return got_error_from_errno("malloc");
4272 s->nlines = 0;
4274 f = got_opentemp();
4275 if (f == NULL) {
4276 err = got_error_from_errno("got_opentemp");
4277 goto done;
4279 if (s->f && fclose(s->f) == EOF) {
4280 err = got_error_from_errno("fclose");
4281 goto done;
4283 s->f = f;
4285 if (s->id1)
4286 err = got_object_get_type(&obj_type, s->repo, s->id1);
4287 else
4288 err = got_object_get_type(&obj_type, s->repo, s->id2);
4289 if (err)
4290 goto done;
4292 switch (obj_type) {
4293 case GOT_OBJ_TYPE_BLOB:
4294 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4295 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4296 s->label1, s->label2, tog_diff_algo, s->diff_context,
4297 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4298 break;
4299 case GOT_OBJ_TYPE_TREE:
4300 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4301 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4302 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4303 s->force_text_diff, s->repo, s->f);
4304 break;
4305 case GOT_OBJ_TYPE_COMMIT: {
4306 const struct got_object_id_queue *parent_ids;
4307 struct got_object_qid *pid;
4308 struct got_commit_object *commit2;
4309 struct got_reflist_head *refs;
4311 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4312 if (err)
4313 goto done;
4314 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4315 /* Show commit info if we're diffing to a parent/root commit. */
4316 if (s->id1 == NULL) {
4317 err = write_commit_info(&s->line_offsets, &s->nlines,
4318 s->id2, refs, s->repo, s->f);
4319 if (err)
4320 goto done;
4321 } else {
4322 parent_ids = got_object_commit_get_parent_ids(commit2);
4323 STAILQ_FOREACH(pid, parent_ids, entry) {
4324 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4325 err = write_commit_info(
4326 &s->line_offsets, &s->nlines,
4327 s->id2, refs, s->repo, s->f);
4328 if (err)
4329 goto done;
4330 break;
4334 got_object_commit_close(commit2);
4336 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4337 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4338 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4339 s->force_text_diff, s->repo, s->f);
4340 break;
4342 default:
4343 err = got_error(GOT_ERR_OBJ_TYPE);
4344 break;
4346 if (err)
4347 goto done;
4348 done:
4349 if (s->f && fflush(s->f) != 0 && err == NULL)
4350 err = got_error_from_errno("fflush");
4351 return err;
4354 static void
4355 diff_view_indicate_progress(struct tog_view *view)
4357 mvwaddstr(view->window, 0, 0, "diffing...");
4358 update_panels();
4359 doupdate();
4362 static const struct got_error *
4363 search_start_diff_view(struct tog_view *view)
4365 struct tog_diff_view_state *s = &view->state.diff;
4367 s->matched_line = 0;
4368 return NULL;
4371 static const struct got_error *
4372 search_next_diff_view(struct tog_view *view)
4374 struct tog_diff_view_state *s = &view->state.diff;
4375 const struct got_error *err = NULL;
4376 int lineno;
4377 char *line = NULL;
4378 size_t linesize = 0;
4379 ssize_t linelen;
4381 if (!view->searching) {
4382 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4383 return NULL;
4386 if (s->matched_line) {
4387 if (view->searching == TOG_SEARCH_FORWARD)
4388 lineno = s->matched_line + 1;
4389 else
4390 lineno = s->matched_line - 1;
4391 } else
4392 lineno = s->first_displayed_line;
4394 while (1) {
4395 off_t offset;
4397 if (lineno <= 0 || lineno > s->nlines) {
4398 if (s->matched_line == 0) {
4399 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4400 break;
4403 if (view->searching == TOG_SEARCH_FORWARD)
4404 lineno = 1;
4405 else
4406 lineno = s->nlines;
4409 offset = s->line_offsets[lineno - 1];
4410 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4411 free(line);
4412 return got_error_from_errno("fseeko");
4414 linelen = getline(&line, &linesize, s->f);
4415 if (linelen != -1) {
4416 char *exstr;
4417 err = expand_tab(&exstr, line);
4418 if (err)
4419 break;
4420 if (match_line(exstr, &view->regex, 1,
4421 &view->regmatch)) {
4422 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4423 s->matched_line = lineno;
4424 free(exstr);
4425 break;
4427 free(exstr);
4429 if (view->searching == TOG_SEARCH_FORWARD)
4430 lineno++;
4431 else
4432 lineno--;
4434 free(line);
4436 if (s->matched_line) {
4437 s->first_displayed_line = s->matched_line;
4438 s->selected_line = 1;
4441 return err;
4444 static const struct got_error *
4445 close_diff_view(struct tog_view *view)
4447 const struct got_error *err = NULL;
4448 struct tog_diff_view_state *s = &view->state.diff;
4450 free(s->id1);
4451 s->id1 = NULL;
4452 free(s->id2);
4453 s->id2 = NULL;
4454 if (s->f && fclose(s->f) == EOF)
4455 err = got_error_from_errno("fclose");
4456 s->f = NULL;
4457 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4458 err = got_error_from_errno("fclose");
4459 s->f1 = NULL;
4460 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4461 err = got_error_from_errno("fclose");
4462 s->f2 = NULL;
4463 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4464 err = got_error_from_errno("close");
4465 s->fd1 = -1;
4466 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4467 err = got_error_from_errno("close");
4468 s->fd2 = -1;
4469 free_colors(&s->colors);
4470 free(s->line_offsets);
4471 s->line_offsets = NULL;
4472 s->nlines = 0;
4473 return err;
4476 static const struct got_error *
4477 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4478 struct got_object_id *id2, const char *label1, const char *label2,
4479 int diff_context, int ignore_whitespace, int force_text_diff,
4480 struct tog_view *parent_view, struct got_repository *repo)
4482 const struct got_error *err;
4483 struct tog_diff_view_state *s = &view->state.diff;
4485 memset(s, 0, sizeof(*s));
4486 s->fd1 = -1;
4487 s->fd2 = -1;
4489 if (id1 != NULL && id2 != NULL) {
4490 int type1, type2;
4491 err = got_object_get_type(&type1, repo, id1);
4492 if (err)
4493 return err;
4494 err = got_object_get_type(&type2, repo, id2);
4495 if (err)
4496 return err;
4498 if (type1 != type2)
4499 return got_error(GOT_ERR_OBJ_TYPE);
4501 s->first_displayed_line = 1;
4502 s->last_displayed_line = view->nlines;
4503 s->selected_line = 1;
4504 s->repo = repo;
4505 s->id1 = id1;
4506 s->id2 = id2;
4507 s->label1 = label1;
4508 s->label2 = label2;
4510 if (id1) {
4511 s->id1 = got_object_id_dup(id1);
4512 if (s->id1 == NULL)
4513 return got_error_from_errno("got_object_id_dup");
4514 } else
4515 s->id1 = NULL;
4517 s->id2 = got_object_id_dup(id2);
4518 if (s->id2 == NULL) {
4519 err = got_error_from_errno("got_object_id_dup");
4520 goto done;
4523 s->f1 = got_opentemp();
4524 if (s->f1 == NULL) {
4525 err = got_error_from_errno("got_opentemp");
4526 goto done;
4529 s->f2 = got_opentemp();
4530 if (s->f2 == NULL) {
4531 err = got_error_from_errno("got_opentemp");
4532 goto done;
4535 s->fd1 = got_opentempfd();
4536 if (s->fd1 == -1) {
4537 err = got_error_from_errno("got_opentempfd");
4538 goto done;
4541 s->fd2 = got_opentempfd();
4542 if (s->fd2 == -1) {
4543 err = got_error_from_errno("got_opentempfd");
4544 goto done;
4547 s->first_displayed_line = 1;
4548 s->last_displayed_line = view->nlines;
4549 s->diff_context = diff_context;
4550 s->ignore_whitespace = ignore_whitespace;
4551 s->force_text_diff = force_text_diff;
4552 s->parent_view = parent_view;
4553 s->repo = repo;
4555 STAILQ_INIT(&s->colors);
4556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 err = add_color(&s->colors,
4558 "^-", TOG_COLOR_DIFF_MINUS,
4559 get_color_value("TOG_COLOR_DIFF_MINUS"));
4560 if (err)
4561 goto done;
4562 err = add_color(&s->colors, "^\\+",
4563 TOG_COLOR_DIFF_PLUS,
4564 get_color_value("TOG_COLOR_DIFF_PLUS"));
4565 if (err)
4566 goto done;
4567 err = add_color(&s->colors,
4568 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4569 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4570 if (err)
4571 goto done;
4573 err = add_color(&s->colors,
4574 "^(commit [0-9a-f]|parent [0-9]|"
4575 "(blob|file|tree|commit) [-+] |"
4576 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4577 get_color_value("TOG_COLOR_DIFF_META"));
4578 if (err)
4579 goto done;
4581 err = add_color(&s->colors,
4582 "^(from|via): ", TOG_COLOR_AUTHOR,
4583 get_color_value("TOG_COLOR_AUTHOR"));
4584 if (err)
4585 goto done;
4587 err = add_color(&s->colors,
4588 "^date: ", TOG_COLOR_DATE,
4589 get_color_value("TOG_COLOR_DATE"));
4590 if (err)
4591 goto done;
4594 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4595 view_is_splitscreen(view))
4596 show_log_view(parent_view); /* draw border */
4597 diff_view_indicate_progress(view);
4599 err = create_diff(s);
4601 view->show = show_diff_view;
4602 view->input = input_diff_view;
4603 view->reset = reset_diff_view;
4604 view->close = close_diff_view;
4605 view->search_start = search_start_diff_view;
4606 view->search_next = search_next_diff_view;
4607 done:
4608 if (err)
4609 close_diff_view(view);
4610 return err;
4613 static const struct got_error *
4614 show_diff_view(struct tog_view *view)
4616 const struct got_error *err;
4617 struct tog_diff_view_state *s = &view->state.diff;
4618 char *id_str1 = NULL, *id_str2, *header;
4619 const char *label1, *label2;
4621 if (s->id1) {
4622 err = got_object_id_str(&id_str1, s->id1);
4623 if (err)
4624 return err;
4625 label1 = s->label1 ? : id_str1;
4626 } else
4627 label1 = "/dev/null";
4629 err = got_object_id_str(&id_str2, s->id2);
4630 if (err)
4631 return err;
4632 label2 = s->label2 ? : id_str2;
4634 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4635 err = got_error_from_errno("asprintf");
4636 free(id_str1);
4637 free(id_str2);
4638 return err;
4640 free(id_str1);
4641 free(id_str2);
4643 err = draw_file(view, header);
4644 free(header);
4645 return err;
4648 static const struct got_error *
4649 set_selected_commit(struct tog_diff_view_state *s,
4650 struct commit_queue_entry *entry)
4652 const struct got_error *err;
4653 const struct got_object_id_queue *parent_ids;
4654 struct got_commit_object *selected_commit;
4655 struct got_object_qid *pid;
4657 free(s->id2);
4658 s->id2 = got_object_id_dup(entry->id);
4659 if (s->id2 == NULL)
4660 return got_error_from_errno("got_object_id_dup");
4662 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4663 if (err)
4664 return err;
4665 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4666 free(s->id1);
4667 pid = STAILQ_FIRST(parent_ids);
4668 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4669 got_object_commit_close(selected_commit);
4670 return NULL;
4673 static const struct got_error *
4674 reset_diff_view(struct tog_view *view)
4676 struct tog_diff_view_state *s = &view->state.diff;
4678 view->count = 0;
4679 wclear(view->window);
4680 s->first_displayed_line = 1;
4681 s->last_displayed_line = view->nlines;
4682 s->matched_line = 0;
4683 diff_view_indicate_progress(view);
4684 return create_diff(s);
4687 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4688 int, int, int);
4689 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4690 int, int);
4692 static const struct got_error *
4693 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 const struct got_error *err = NULL;
4696 struct tog_diff_view_state *s = &view->state.diff;
4697 struct tog_log_view_state *ls;
4698 struct commit_queue_entry *old_selected_entry;
4699 char *line = NULL;
4700 size_t linesize = 0;
4701 ssize_t linelen;
4702 int i, nscroll = view->nlines - 1, up = 0;
4704 switch (ch) {
4705 case '0':
4706 view->x = 0;
4707 break;
4708 case '$':
4709 view->x = MAX(view->maxx - view->ncols / 3, 0);
4710 view->count = 0;
4711 break;
4712 case KEY_RIGHT:
4713 case 'l':
4714 if (view->x + view->ncols / 3 < view->maxx)
4715 view->x += 2; /* move two columns right */
4716 else
4717 view->count = 0;
4718 break;
4719 case KEY_LEFT:
4720 case 'h':
4721 view->x -= MIN(view->x, 2); /* move two columns back */
4722 if (view->x <= 0)
4723 view->count = 0;
4724 break;
4725 case 'a':
4726 case 'w':
4727 if (ch == 'a')
4728 s->force_text_diff = !s->force_text_diff;
4729 if (ch == 'w')
4730 s->ignore_whitespace = !s->ignore_whitespace;
4731 err = reset_diff_view(view);
4732 break;
4733 case 'g':
4734 case KEY_HOME:
4735 s->first_displayed_line = 1;
4736 view->count = 0;
4737 break;
4738 case 'G':
4739 case KEY_END:
4740 view->count = 0;
4741 if (s->eof)
4742 break;
4744 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4745 s->eof = 1;
4746 break;
4747 case 'k':
4748 case KEY_UP:
4749 case CTRL('p'):
4750 if (s->first_displayed_line > 1)
4751 s->first_displayed_line--;
4752 else
4753 view->count = 0;
4754 break;
4755 case CTRL('u'):
4756 case 'u':
4757 nscroll /= 2;
4758 /* FALL THROUGH */
4759 case KEY_PPAGE:
4760 case CTRL('b'):
4761 case 'b':
4762 if (s->first_displayed_line == 1) {
4763 view->count = 0;
4764 break;
4766 i = 0;
4767 while (i++ < nscroll && s->first_displayed_line > 1)
4768 s->first_displayed_line--;
4769 break;
4770 case 'j':
4771 case KEY_DOWN:
4772 case CTRL('n'):
4773 if (!s->eof)
4774 s->first_displayed_line++;
4775 else
4776 view->count = 0;
4777 break;
4778 case CTRL('d'):
4779 case 'd':
4780 nscroll /= 2;
4781 /* FALL THROUGH */
4782 case KEY_NPAGE:
4783 case CTRL('f'):
4784 case 'f':
4785 case ' ':
4786 if (s->eof) {
4787 view->count = 0;
4788 break;
4790 i = 0;
4791 while (!s->eof && i++ < nscroll) {
4792 linelen = getline(&line, &linesize, s->f);
4793 s->first_displayed_line++;
4794 if (linelen == -1) {
4795 if (feof(s->f)) {
4796 s->eof = 1;
4797 } else
4798 err = got_ferror(s->f, GOT_ERR_IO);
4799 break;
4802 free(line);
4803 break;
4804 case '[':
4805 if (s->diff_context > 0) {
4806 s->diff_context--;
4807 s->matched_line = 0;
4808 diff_view_indicate_progress(view);
4809 err = create_diff(s);
4810 if (s->first_displayed_line + view->nlines - 1 >
4811 s->nlines) {
4812 s->first_displayed_line = 1;
4813 s->last_displayed_line = view->nlines;
4815 } else
4816 view->count = 0;
4817 break;
4818 case ']':
4819 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4820 s->diff_context++;
4821 s->matched_line = 0;
4822 diff_view_indicate_progress(view);
4823 err = create_diff(s);
4824 } else
4825 view->count = 0;
4826 break;
4827 case '<':
4828 case ',':
4829 up = 1;
4830 /* FALL THROUGH */
4831 case '>':
4832 case '.':
4833 if (s->parent_view == NULL) {
4834 view->count = 0;
4835 break;
4837 s->parent_view->count = view->count;
4839 if (s->parent_view->type == TOG_VIEW_LOG) {
4840 ls = &s->parent_view->state.log;
4841 old_selected_entry = ls->selected_entry;
4843 err = input_log_view(NULL, s->parent_view,
4844 up ? KEY_UP : KEY_DOWN);
4845 if (err)
4846 break;
4847 view->count = s->parent_view->count;
4849 if (old_selected_entry == ls->selected_entry)
4850 break;
4852 err = set_selected_commit(s, ls->selected_entry);
4853 if (err)
4854 break;
4855 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4856 struct tog_blame_view_state *bs;
4857 struct got_object_id *id, *prev_id;
4859 bs = &s->parent_view->state.blame;
4860 prev_id = get_annotation_for_line(bs->blame.lines,
4861 bs->blame.nlines, bs->last_diffed_line);
4863 err = input_blame_view(&view, s->parent_view,
4864 up ? KEY_UP : KEY_DOWN);
4865 if (err)
4866 break;
4867 view->count = s->parent_view->count;
4869 if (prev_id == NULL)
4870 break;
4871 id = get_selected_commit_id(bs->blame.lines,
4872 bs->blame.nlines, bs->first_displayed_line,
4873 bs->selected_line);
4874 if (id == NULL)
4875 break;
4877 if (!got_object_id_cmp(prev_id, id))
4878 break;
4880 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4881 if (err)
4882 break;
4884 s->first_displayed_line = 1;
4885 s->last_displayed_line = view->nlines;
4886 s->matched_line = 0;
4887 view->x = 0;
4889 diff_view_indicate_progress(view);
4890 err = create_diff(s);
4891 break;
4892 default:
4893 view->count = 0;
4894 break;
4897 return err;
4900 static const struct got_error *
4901 cmd_diff(int argc, char *argv[])
4903 const struct got_error *error = NULL;
4904 struct got_repository *repo = NULL;
4905 struct got_worktree *worktree = NULL;
4906 struct got_object_id *id1 = NULL, *id2 = NULL;
4907 char *repo_path = NULL, *cwd = NULL;
4908 char *id_str1 = NULL, *id_str2 = NULL;
4909 char *label1 = NULL, *label2 = NULL;
4910 int diff_context = 3, ignore_whitespace = 0;
4911 int ch, force_text_diff = 0;
4912 const char *errstr;
4913 struct tog_view *view;
4914 int *pack_fds = NULL;
4916 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4917 switch (ch) {
4918 case 'a':
4919 force_text_diff = 1;
4920 break;
4921 case 'C':
4922 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4923 &errstr);
4924 if (errstr != NULL)
4925 errx(1, "number of context lines is %s: %s",
4926 errstr, errstr);
4927 break;
4928 case 'r':
4929 repo_path = realpath(optarg, NULL);
4930 if (repo_path == NULL)
4931 return got_error_from_errno2("realpath",
4932 optarg);
4933 got_path_strip_trailing_slashes(repo_path);
4934 break;
4935 case 'w':
4936 ignore_whitespace = 1;
4937 break;
4938 default:
4939 usage_diff();
4940 /* NOTREACHED */
4944 argc -= optind;
4945 argv += optind;
4947 if (argc == 0) {
4948 usage_diff(); /* TODO show local worktree changes */
4949 } else if (argc == 2) {
4950 id_str1 = argv[0];
4951 id_str2 = argv[1];
4952 } else
4953 usage_diff();
4955 error = got_repo_pack_fds_open(&pack_fds);
4956 if (error)
4957 goto done;
4959 if (repo_path == NULL) {
4960 cwd = getcwd(NULL, 0);
4961 if (cwd == NULL)
4962 return got_error_from_errno("getcwd");
4963 error = got_worktree_open(&worktree, cwd);
4964 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4965 goto done;
4966 if (worktree)
4967 repo_path =
4968 strdup(got_worktree_get_repo_path(worktree));
4969 else
4970 repo_path = strdup(cwd);
4971 if (repo_path == NULL) {
4972 error = got_error_from_errno("strdup");
4973 goto done;
4977 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4978 if (error)
4979 goto done;
4981 init_curses();
4983 error = apply_unveil(got_repo_get_path(repo), NULL);
4984 if (error)
4985 goto done;
4987 error = tog_load_refs(repo, 0);
4988 if (error)
4989 goto done;
4991 error = got_repo_match_object_id(&id1, &label1, id_str1,
4992 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4993 if (error)
4994 goto done;
4996 error = got_repo_match_object_id(&id2, &label2, id_str2,
4997 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4998 if (error)
4999 goto done;
5001 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5002 if (view == NULL) {
5003 error = got_error_from_errno("view_open");
5004 goto done;
5006 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5007 ignore_whitespace, force_text_diff, NULL, repo);
5008 if (error)
5009 goto done;
5010 error = view_loop(view);
5011 done:
5012 free(label1);
5013 free(label2);
5014 free(repo_path);
5015 free(cwd);
5016 if (repo) {
5017 const struct got_error *close_err = got_repo_close(repo);
5018 if (error == NULL)
5019 error = close_err;
5021 if (worktree)
5022 got_worktree_close(worktree);
5023 if (pack_fds) {
5024 const struct got_error *pack_err =
5025 got_repo_pack_fds_close(pack_fds);
5026 if (error == NULL)
5027 error = pack_err;
5029 tog_free_refs();
5030 return error;
5033 __dead static void
5034 usage_blame(void)
5036 endwin();
5037 fprintf(stderr,
5038 "usage: %s blame [-c commit] [-r repository-path] path\n",
5039 getprogname());
5040 exit(1);
5043 struct tog_blame_line {
5044 int annotated;
5045 struct got_object_id *id;
5048 static const struct got_error *
5049 draw_blame(struct tog_view *view)
5051 struct tog_blame_view_state *s = &view->state.blame;
5052 struct tog_blame *blame = &s->blame;
5053 regmatch_t *regmatch = &view->regmatch;
5054 const struct got_error *err;
5055 int lineno = 0, nprinted = 0;
5056 char *line = NULL;
5057 size_t linesize = 0;
5058 ssize_t linelen;
5059 wchar_t *wline;
5060 int width;
5061 struct tog_blame_line *blame_line;
5062 struct got_object_id *prev_id = NULL;
5063 char *id_str;
5064 struct tog_color *tc;
5066 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5067 if (err)
5068 return err;
5070 rewind(blame->f);
5071 werase(view->window);
5073 if (asprintf(&line, "commit %s", id_str) == -1) {
5074 err = got_error_from_errno("asprintf");
5075 free(id_str);
5076 return err;
5079 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5080 free(line);
5081 line = NULL;
5082 if (err)
5083 return err;
5084 if (view_needs_focus_indication(view))
5085 wstandout(view->window);
5086 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5087 if (tc)
5088 wattr_on(view->window,
5089 COLOR_PAIR(tc->colorpair), NULL);
5090 waddwstr(view->window, wline);
5091 if (tc)
5092 wattr_off(view->window,
5093 COLOR_PAIR(tc->colorpair), NULL);
5094 if (view_needs_focus_indication(view))
5095 wstandend(view->window);
5096 free(wline);
5097 wline = NULL;
5098 if (width < view->ncols - 1)
5099 waddch(view->window, '\n');
5101 if (asprintf(&line, "[%d/%d] %s%s",
5102 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5103 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5104 free(id_str);
5105 return got_error_from_errno("asprintf");
5107 free(id_str);
5108 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5109 free(line);
5110 line = NULL;
5111 if (err)
5112 return err;
5113 waddwstr(view->window, wline);
5114 free(wline);
5115 wline = NULL;
5116 if (width < view->ncols - 1)
5117 waddch(view->window, '\n');
5119 s->eof = 0;
5120 view->maxx = 0;
5121 while (nprinted < view->nlines - 2) {
5122 linelen = getline(&line, &linesize, blame->f);
5123 if (linelen == -1) {
5124 if (feof(blame->f)) {
5125 s->eof = 1;
5126 break;
5128 free(line);
5129 return got_ferror(blame->f, GOT_ERR_IO);
5131 if (++lineno < s->first_displayed_line)
5132 continue;
5134 /* Set view->maxx based on full line length. */
5135 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5136 if (err) {
5137 free(line);
5138 return err;
5140 free(wline);
5141 wline = NULL;
5142 view->maxx = MAX(view->maxx, width);
5144 if (nprinted == s->selected_line - 1)
5145 wstandout(view->window);
5147 if (blame->nlines > 0) {
5148 blame_line = &blame->lines[lineno - 1];
5149 if (blame_line->annotated && prev_id &&
5150 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5151 !(nprinted == s->selected_line - 1)) {
5152 waddstr(view->window, " ");
5153 } else if (blame_line->annotated) {
5154 char *id_str;
5155 err = got_object_id_str(&id_str,
5156 blame_line->id);
5157 if (err) {
5158 free(line);
5159 return err;
5161 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5162 if (tc)
5163 wattr_on(view->window,
5164 COLOR_PAIR(tc->colorpair), NULL);
5165 wprintw(view->window, "%.8s", id_str);
5166 if (tc)
5167 wattr_off(view->window,
5168 COLOR_PAIR(tc->colorpair), NULL);
5169 free(id_str);
5170 prev_id = blame_line->id;
5171 } else {
5172 waddstr(view->window, "........");
5173 prev_id = NULL;
5175 } else {
5176 waddstr(view->window, "........");
5177 prev_id = NULL;
5180 if (nprinted == s->selected_line - 1)
5181 wstandend(view->window);
5182 waddstr(view->window, " ");
5184 if (view->ncols <= 9) {
5185 width = 9;
5186 } else if (s->first_displayed_line + nprinted ==
5187 s->matched_line &&
5188 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5189 err = add_matched_line(&width, line, view->ncols - 9, 9,
5190 view->window, view->x, regmatch);
5191 if (err) {
5192 free(line);
5193 return err;
5195 width += 9;
5196 } else {
5197 int skip;
5198 err = format_line(&wline, &width, &skip, line,
5199 view->x, view->ncols - 9, 9, 1);
5200 if (err) {
5201 free(line);
5202 return err;
5204 waddwstr(view->window, &wline[skip]);
5205 width += 9;
5206 free(wline);
5207 wline = NULL;
5210 if (width <= view->ncols - 1)
5211 waddch(view->window, '\n');
5212 if (++nprinted == 1)
5213 s->first_displayed_line = lineno;
5215 free(line);
5216 s->last_displayed_line = lineno;
5218 view_border(view);
5220 return NULL;
5223 static const struct got_error *
5224 blame_cb(void *arg, int nlines, int lineno,
5225 struct got_commit_object *commit, struct got_object_id *id)
5227 const struct got_error *err = NULL;
5228 struct tog_blame_cb_args *a = arg;
5229 struct tog_blame_line *line;
5230 int errcode;
5232 if (nlines != a->nlines ||
5233 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5234 return got_error(GOT_ERR_RANGE);
5236 errcode = pthread_mutex_lock(&tog_mutex);
5237 if (errcode)
5238 return got_error_set_errno(errcode, "pthread_mutex_lock");
5240 if (*a->quit) { /* user has quit the blame view */
5241 err = got_error(GOT_ERR_ITER_COMPLETED);
5242 goto done;
5245 if (lineno == -1)
5246 goto done; /* no change in this commit */
5248 line = &a->lines[lineno - 1];
5249 if (line->annotated)
5250 goto done;
5252 line->id = got_object_id_dup(id);
5253 if (line->id == NULL) {
5254 err = got_error_from_errno("got_object_id_dup");
5255 goto done;
5257 line->annotated = 1;
5258 done:
5259 errcode = pthread_mutex_unlock(&tog_mutex);
5260 if (errcode)
5261 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5262 return err;
5265 static void *
5266 blame_thread(void *arg)
5268 const struct got_error *err, *close_err;
5269 struct tog_blame_thread_args *ta = arg;
5270 struct tog_blame_cb_args *a = ta->cb_args;
5271 int errcode, fd1 = -1, fd2 = -1;
5272 FILE *f1 = NULL, *f2 = NULL;
5274 fd1 = got_opentempfd();
5275 if (fd1 == -1)
5276 return (void *)got_error_from_errno("got_opentempfd");
5278 fd2 = got_opentempfd();
5279 if (fd2 == -1) {
5280 err = got_error_from_errno("got_opentempfd");
5281 goto done;
5284 f1 = got_opentemp();
5285 if (f1 == NULL) {
5286 err = (void *)got_error_from_errno("got_opentemp");
5287 goto done;
5289 f2 = got_opentemp();
5290 if (f2 == NULL) {
5291 err = (void *)got_error_from_errno("got_opentemp");
5292 goto done;
5295 err = block_signals_used_by_main_thread();
5296 if (err)
5297 goto done;
5299 err = got_blame(ta->path, a->commit_id, ta->repo,
5300 tog_diff_algo, blame_cb, ta->cb_args,
5301 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5302 if (err && err->code == GOT_ERR_CANCELLED)
5303 err = NULL;
5305 errcode = pthread_mutex_lock(&tog_mutex);
5306 if (errcode) {
5307 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5308 goto done;
5311 close_err = got_repo_close(ta->repo);
5312 if (err == NULL)
5313 err = close_err;
5314 ta->repo = NULL;
5315 *ta->complete = 1;
5317 errcode = pthread_mutex_unlock(&tog_mutex);
5318 if (errcode && err == NULL)
5319 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5321 done:
5322 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5323 err = got_error_from_errno("close");
5324 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5325 err = got_error_from_errno("close");
5326 if (f1 && fclose(f1) == EOF && err == NULL)
5327 err = got_error_from_errno("fclose");
5328 if (f2 && fclose(f2) == EOF && err == NULL)
5329 err = got_error_from_errno("fclose");
5331 return (void *)err;
5334 static struct got_object_id *
5335 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5336 int first_displayed_line, int selected_line)
5338 struct tog_blame_line *line;
5340 if (nlines <= 0)
5341 return NULL;
5343 line = &lines[first_displayed_line - 1 + selected_line - 1];
5344 if (!line->annotated)
5345 return NULL;
5347 return line->id;
5350 static struct got_object_id *
5351 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5352 int lineno)
5354 struct tog_blame_line *line;
5356 if (nlines <= 0 || lineno >= nlines)
5357 return NULL;
5359 line = &lines[lineno - 1];
5360 if (!line->annotated)
5361 return NULL;
5363 return line->id;
5366 static const struct got_error *
5367 stop_blame(struct tog_blame *blame)
5369 const struct got_error *err = NULL;
5370 int i;
5372 if (blame->thread) {
5373 int errcode;
5374 errcode = pthread_mutex_unlock(&tog_mutex);
5375 if (errcode)
5376 return got_error_set_errno(errcode,
5377 "pthread_mutex_unlock");
5378 errcode = pthread_join(blame->thread, (void **)&err);
5379 if (errcode)
5380 return got_error_set_errno(errcode, "pthread_join");
5381 errcode = pthread_mutex_lock(&tog_mutex);
5382 if (errcode)
5383 return got_error_set_errno(errcode,
5384 "pthread_mutex_lock");
5385 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5386 err = NULL;
5387 blame->thread = NULL;
5389 if (blame->thread_args.repo) {
5390 const struct got_error *close_err;
5391 close_err = got_repo_close(blame->thread_args.repo);
5392 if (err == NULL)
5393 err = close_err;
5394 blame->thread_args.repo = NULL;
5396 if (blame->f) {
5397 if (fclose(blame->f) == EOF && err == NULL)
5398 err = got_error_from_errno("fclose");
5399 blame->f = NULL;
5401 if (blame->lines) {
5402 for (i = 0; i < blame->nlines; i++)
5403 free(blame->lines[i].id);
5404 free(blame->lines);
5405 blame->lines = NULL;
5407 free(blame->cb_args.commit_id);
5408 blame->cb_args.commit_id = NULL;
5409 if (blame->pack_fds) {
5410 const struct got_error *pack_err =
5411 got_repo_pack_fds_close(blame->pack_fds);
5412 if (err == NULL)
5413 err = pack_err;
5414 blame->pack_fds = NULL;
5416 return err;
5419 static const struct got_error *
5420 cancel_blame_view(void *arg)
5422 const struct got_error *err = NULL;
5423 int *done = arg;
5424 int errcode;
5426 errcode = pthread_mutex_lock(&tog_mutex);
5427 if (errcode)
5428 return got_error_set_errno(errcode,
5429 "pthread_mutex_unlock");
5431 if (*done)
5432 err = got_error(GOT_ERR_CANCELLED);
5434 errcode = pthread_mutex_unlock(&tog_mutex);
5435 if (errcode)
5436 return got_error_set_errno(errcode,
5437 "pthread_mutex_lock");
5439 return err;
5442 static const struct got_error *
5443 run_blame(struct tog_view *view)
5445 struct tog_blame_view_state *s = &view->state.blame;
5446 struct tog_blame *blame = &s->blame;
5447 const struct got_error *err = NULL;
5448 struct got_commit_object *commit = NULL;
5449 struct got_blob_object *blob = NULL;
5450 struct got_repository *thread_repo = NULL;
5451 struct got_object_id *obj_id = NULL;
5452 int obj_type, fd = -1;
5453 int *pack_fds = NULL;
5455 err = got_object_open_as_commit(&commit, s->repo,
5456 &s->blamed_commit->id);
5457 if (err)
5458 return err;
5460 fd = got_opentempfd();
5461 if (fd == -1) {
5462 err = got_error_from_errno("got_opentempfd");
5463 goto done;
5466 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5467 if (err)
5468 goto done;
5470 err = got_object_get_type(&obj_type, s->repo, obj_id);
5471 if (err)
5472 goto done;
5474 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5475 err = got_error(GOT_ERR_OBJ_TYPE);
5476 goto done;
5479 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5480 if (err)
5481 goto done;
5482 blame->f = got_opentemp();
5483 if (blame->f == NULL) {
5484 err = got_error_from_errno("got_opentemp");
5485 goto done;
5487 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5488 &blame->line_offsets, blame->f, blob);
5489 if (err)
5490 goto done;
5491 if (blame->nlines == 0) {
5492 s->blame_complete = 1;
5493 goto done;
5496 /* Don't include \n at EOF in the blame line count. */
5497 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5498 blame->nlines--;
5500 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5501 if (blame->lines == NULL) {
5502 err = got_error_from_errno("calloc");
5503 goto done;
5506 err = got_repo_pack_fds_open(&pack_fds);
5507 if (err)
5508 goto done;
5509 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5510 pack_fds);
5511 if (err)
5512 goto done;
5514 blame->pack_fds = pack_fds;
5515 blame->cb_args.view = view;
5516 blame->cb_args.lines = blame->lines;
5517 blame->cb_args.nlines = blame->nlines;
5518 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5519 if (blame->cb_args.commit_id == NULL) {
5520 err = got_error_from_errno("got_object_id_dup");
5521 goto done;
5523 blame->cb_args.quit = &s->done;
5525 blame->thread_args.path = s->path;
5526 blame->thread_args.repo = thread_repo;
5527 blame->thread_args.cb_args = &blame->cb_args;
5528 blame->thread_args.complete = &s->blame_complete;
5529 blame->thread_args.cancel_cb = cancel_blame_view;
5530 blame->thread_args.cancel_arg = &s->done;
5531 s->blame_complete = 0;
5533 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5534 s->first_displayed_line = 1;
5535 s->last_displayed_line = view->nlines;
5536 s->selected_line = 1;
5538 s->matched_line = 0;
5540 done:
5541 if (commit)
5542 got_object_commit_close(commit);
5543 if (fd != -1 && close(fd) == -1 && err == NULL)
5544 err = got_error_from_errno("close");
5545 if (blob)
5546 got_object_blob_close(blob);
5547 free(obj_id);
5548 if (err)
5549 stop_blame(blame);
5550 return err;
5553 static const struct got_error *
5554 open_blame_view(struct tog_view *view, char *path,
5555 struct got_object_id *commit_id, struct got_repository *repo)
5557 const struct got_error *err = NULL;
5558 struct tog_blame_view_state *s = &view->state.blame;
5560 STAILQ_INIT(&s->blamed_commits);
5562 s->path = strdup(path);
5563 if (s->path == NULL)
5564 return got_error_from_errno("strdup");
5566 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5567 if (err) {
5568 free(s->path);
5569 return err;
5572 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5573 s->first_displayed_line = 1;
5574 s->last_displayed_line = view->nlines;
5575 s->selected_line = 1;
5576 s->blame_complete = 0;
5577 s->repo = repo;
5578 s->commit_id = commit_id;
5579 memset(&s->blame, 0, sizeof(s->blame));
5581 STAILQ_INIT(&s->colors);
5582 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5583 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5584 get_color_value("TOG_COLOR_COMMIT"));
5585 if (err)
5586 return err;
5589 view->show = show_blame_view;
5590 view->input = input_blame_view;
5591 view->reset = reset_blame_view;
5592 view->close = close_blame_view;
5593 view->search_start = search_start_blame_view;
5594 view->search_next = search_next_blame_view;
5596 return run_blame(view);
5599 static const struct got_error *
5600 close_blame_view(struct tog_view *view)
5602 const struct got_error *err = NULL;
5603 struct tog_blame_view_state *s = &view->state.blame;
5605 if (s->blame.thread)
5606 err = stop_blame(&s->blame);
5608 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5609 struct got_object_qid *blamed_commit;
5610 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5611 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5612 got_object_qid_free(blamed_commit);
5615 free(s->path);
5616 free_colors(&s->colors);
5617 return err;
5620 static const struct got_error *
5621 search_start_blame_view(struct tog_view *view)
5623 struct tog_blame_view_state *s = &view->state.blame;
5625 s->matched_line = 0;
5626 return NULL;
5629 static const struct got_error *
5630 search_next_blame_view(struct tog_view *view)
5632 struct tog_blame_view_state *s = &view->state.blame;
5633 const struct got_error *err = NULL;
5634 int lineno;
5635 char *line = NULL;
5636 size_t linesize = 0;
5637 ssize_t linelen;
5639 if (!view->searching) {
5640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5641 return NULL;
5644 if (s->matched_line) {
5645 if (view->searching == TOG_SEARCH_FORWARD)
5646 lineno = s->matched_line + 1;
5647 else
5648 lineno = s->matched_line - 1;
5649 } else
5650 lineno = s->first_displayed_line - 1 + s->selected_line;
5652 while (1) {
5653 off_t offset;
5655 if (lineno <= 0 || lineno > s->blame.nlines) {
5656 if (s->matched_line == 0) {
5657 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5658 break;
5661 if (view->searching == TOG_SEARCH_FORWARD)
5662 lineno = 1;
5663 else
5664 lineno = s->blame.nlines;
5667 offset = s->blame.line_offsets[lineno - 1];
5668 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5669 free(line);
5670 return got_error_from_errno("fseeko");
5672 linelen = getline(&line, &linesize, s->blame.f);
5673 if (linelen != -1) {
5674 char *exstr;
5675 err = expand_tab(&exstr, line);
5676 if (err)
5677 break;
5678 if (match_line(exstr, &view->regex, 1,
5679 &view->regmatch)) {
5680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5681 s->matched_line = lineno;
5682 free(exstr);
5683 break;
5685 free(exstr);
5687 if (view->searching == TOG_SEARCH_FORWARD)
5688 lineno++;
5689 else
5690 lineno--;
5692 free(line);
5694 if (s->matched_line) {
5695 s->first_displayed_line = s->matched_line;
5696 s->selected_line = 1;
5699 return err;
5702 static const struct got_error *
5703 show_blame_view(struct tog_view *view)
5705 const struct got_error *err = NULL;
5706 struct tog_blame_view_state *s = &view->state.blame;
5707 int errcode;
5709 if (s->blame.thread == NULL && !s->blame_complete) {
5710 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5711 &s->blame.thread_args);
5712 if (errcode)
5713 return got_error_set_errno(errcode, "pthread_create");
5715 halfdelay(1); /* fast refresh while annotating */
5718 if (s->blame_complete)
5719 halfdelay(10); /* disable fast refresh */
5721 err = draw_blame(view);
5723 view_border(view);
5724 return err;
5727 static const struct got_error *
5728 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5730 const struct got_error *err = NULL, *thread_err = NULL;
5731 struct tog_view *diff_view;
5732 struct tog_blame_view_state *s = &view->state.blame;
5733 int eos, nscroll, begin_y = 0, begin_x = 0;
5735 eos = nscroll = view->nlines - 2;
5736 if (view_is_hsplit_top(view))
5737 --eos; /* border */
5739 switch (ch) {
5740 case '0':
5741 view->x = 0;
5742 break;
5743 case '$':
5744 view->x = MAX(view->maxx - view->ncols / 3, 0);
5745 view->count = 0;
5746 break;
5747 case KEY_RIGHT:
5748 case 'l':
5749 if (view->x + view->ncols / 3 < view->maxx)
5750 view->x += 2; /* move two columns right */
5751 else
5752 view->count = 0;
5753 break;
5754 case KEY_LEFT:
5755 case 'h':
5756 view->x -= MIN(view->x, 2); /* move two columns back */
5757 if (view->x <= 0)
5758 view->count = 0;
5759 break;
5760 case 'q':
5761 s->done = 1;
5762 break;
5763 case 'g':
5764 case KEY_HOME:
5765 s->selected_line = 1;
5766 s->first_displayed_line = 1;
5767 view->count = 0;
5768 break;
5769 case 'G':
5770 case KEY_END:
5771 if (s->blame.nlines < eos) {
5772 s->selected_line = s->blame.nlines;
5773 s->first_displayed_line = 1;
5774 } else {
5775 s->selected_line = eos;
5776 s->first_displayed_line = s->blame.nlines - (eos - 1);
5778 view->count = 0;
5779 break;
5780 case 'k':
5781 case KEY_UP:
5782 case CTRL('p'):
5783 if (s->selected_line > 1)
5784 s->selected_line--;
5785 else if (s->selected_line == 1 &&
5786 s->first_displayed_line > 1)
5787 s->first_displayed_line--;
5788 else
5789 view->count = 0;
5790 break;
5791 case CTRL('u'):
5792 case 'u':
5793 nscroll /= 2;
5794 /* FALL THROUGH */
5795 case KEY_PPAGE:
5796 case CTRL('b'):
5797 case 'b':
5798 if (s->first_displayed_line == 1) {
5799 if (view->count > 1)
5800 nscroll += nscroll;
5801 s->selected_line = MAX(1, s->selected_line - nscroll);
5802 view->count = 0;
5803 break;
5805 if (s->first_displayed_line > nscroll)
5806 s->first_displayed_line -= nscroll;
5807 else
5808 s->first_displayed_line = 1;
5809 break;
5810 case 'j':
5811 case KEY_DOWN:
5812 case CTRL('n'):
5813 if (s->selected_line < eos && s->first_displayed_line +
5814 s->selected_line <= s->blame.nlines)
5815 s->selected_line++;
5816 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5817 s->first_displayed_line++;
5818 else
5819 view->count = 0;
5820 break;
5821 case 'c':
5822 case 'p': {
5823 struct got_object_id *id = NULL;
5825 view->count = 0;
5826 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5827 s->first_displayed_line, s->selected_line);
5828 if (id == NULL)
5829 break;
5830 if (ch == 'p') {
5831 struct got_commit_object *commit, *pcommit;
5832 struct got_object_qid *pid;
5833 struct got_object_id *blob_id = NULL;
5834 int obj_type;
5835 err = got_object_open_as_commit(&commit,
5836 s->repo, id);
5837 if (err)
5838 break;
5839 pid = STAILQ_FIRST(
5840 got_object_commit_get_parent_ids(commit));
5841 if (pid == NULL) {
5842 got_object_commit_close(commit);
5843 break;
5845 /* Check if path history ends here. */
5846 err = got_object_open_as_commit(&pcommit,
5847 s->repo, &pid->id);
5848 if (err)
5849 break;
5850 err = got_object_id_by_path(&blob_id, s->repo,
5851 pcommit, s->path);
5852 got_object_commit_close(pcommit);
5853 if (err) {
5854 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5855 err = NULL;
5856 got_object_commit_close(commit);
5857 break;
5859 err = got_object_get_type(&obj_type, s->repo,
5860 blob_id);
5861 free(blob_id);
5862 /* Can't blame non-blob type objects. */
5863 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5864 got_object_commit_close(commit);
5865 break;
5867 err = got_object_qid_alloc(&s->blamed_commit,
5868 &pid->id);
5869 got_object_commit_close(commit);
5870 } else {
5871 if (got_object_id_cmp(id,
5872 &s->blamed_commit->id) == 0)
5873 break;
5874 err = got_object_qid_alloc(&s->blamed_commit,
5875 id);
5877 if (err)
5878 break;
5879 s->done = 1;
5880 thread_err = stop_blame(&s->blame);
5881 s->done = 0;
5882 if (thread_err)
5883 break;
5884 STAILQ_INSERT_HEAD(&s->blamed_commits,
5885 s->blamed_commit, entry);
5886 err = run_blame(view);
5887 if (err)
5888 break;
5889 break;
5891 case 'C': {
5892 struct got_object_qid *first;
5894 view->count = 0;
5895 first = STAILQ_FIRST(&s->blamed_commits);
5896 if (!got_object_id_cmp(&first->id, s->commit_id))
5897 break;
5898 s->done = 1;
5899 thread_err = stop_blame(&s->blame);
5900 s->done = 0;
5901 if (thread_err)
5902 break;
5903 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5904 got_object_qid_free(s->blamed_commit);
5905 s->blamed_commit =
5906 STAILQ_FIRST(&s->blamed_commits);
5907 err = run_blame(view);
5908 if (err)
5909 break;
5910 break;
5912 case KEY_ENTER:
5913 case '\r': {
5914 struct got_object_id *id = NULL;
5915 struct got_object_qid *pid;
5916 struct got_commit_object *commit = NULL;
5918 view->count = 0;
5919 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5920 s->first_displayed_line, s->selected_line);
5921 if (id == NULL)
5922 break;
5923 err = got_object_open_as_commit(&commit, s->repo, id);
5924 if (err)
5925 break;
5926 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5927 if (*new_view) {
5928 /* traversed from diff view, release diff resources */
5929 err = close_diff_view(*new_view);
5930 if (err)
5931 break;
5932 diff_view = *new_view;
5933 } else {
5934 if (view_is_parent_view(view))
5935 view_get_split(view, &begin_y, &begin_x);
5937 diff_view = view_open(0, 0, begin_y, begin_x,
5938 TOG_VIEW_DIFF);
5939 if (diff_view == NULL) {
5940 got_object_commit_close(commit);
5941 err = got_error_from_errno("view_open");
5942 break;
5945 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5946 id, NULL, NULL, 3, 0, 0, view, s->repo);
5947 got_object_commit_close(commit);
5948 if (err) {
5949 view_close(diff_view);
5950 break;
5952 s->last_diffed_line = s->first_displayed_line - 1 +
5953 s->selected_line;
5954 if (*new_view)
5955 break; /* still open from active diff view */
5956 if (view_is_parent_view(view) &&
5957 view->mode == TOG_VIEW_SPLIT_HRZN) {
5958 err = view_init_hsplit(view, begin_y);
5959 if (err)
5960 break;
5963 view->focussed = 0;
5964 diff_view->focussed = 1;
5965 diff_view->mode = view->mode;
5966 diff_view->nlines = view->lines - begin_y;
5967 if (view_is_parent_view(view)) {
5968 view_transfer_size(diff_view, view);
5969 err = view_close_child(view);
5970 if (err)
5971 break;
5972 err = view_set_child(view, diff_view);
5973 if (err)
5974 break;
5975 view->focus_child = 1;
5976 } else
5977 *new_view = diff_view;
5978 if (err)
5979 break;
5980 break;
5982 case CTRL('d'):
5983 case 'd':
5984 nscroll /= 2;
5985 /* FALL THROUGH */
5986 case KEY_NPAGE:
5987 case CTRL('f'):
5988 case 'f':
5989 case ' ':
5990 if (s->last_displayed_line >= s->blame.nlines &&
5991 s->selected_line >= MIN(s->blame.nlines,
5992 view->nlines - 2)) {
5993 view->count = 0;
5994 break;
5996 if (s->last_displayed_line >= s->blame.nlines &&
5997 s->selected_line < view->nlines - 2) {
5998 s->selected_line +=
5999 MIN(nscroll, s->last_displayed_line -
6000 s->first_displayed_line - s->selected_line + 1);
6002 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6003 s->first_displayed_line += nscroll;
6004 else
6005 s->first_displayed_line =
6006 s->blame.nlines - (view->nlines - 3);
6007 break;
6008 case KEY_RESIZE:
6009 if (s->selected_line > view->nlines - 2) {
6010 s->selected_line = MIN(s->blame.nlines,
6011 view->nlines - 2);
6013 break;
6014 default:
6015 view->count = 0;
6016 break;
6018 return thread_err ? thread_err : err;
6021 static const struct got_error *
6022 reset_blame_view(struct tog_view *view)
6024 const struct got_error *err;
6025 struct tog_blame_view_state *s = &view->state.blame;
6027 view->count = 0;
6028 s->done = 1;
6029 err = stop_blame(&s->blame);
6030 s->done = 0;
6031 if (err)
6032 return err;
6033 return run_blame(view);
6036 static const struct got_error *
6037 cmd_blame(int argc, char *argv[])
6039 const struct got_error *error;
6040 struct got_repository *repo = NULL;
6041 struct got_worktree *worktree = NULL;
6042 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6043 char *link_target = NULL;
6044 struct got_object_id *commit_id = NULL;
6045 struct got_commit_object *commit = NULL;
6046 char *commit_id_str = NULL;
6047 int ch;
6048 struct tog_view *view;
6049 int *pack_fds = NULL;
6051 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6052 switch (ch) {
6053 case 'c':
6054 commit_id_str = optarg;
6055 break;
6056 case 'r':
6057 repo_path = realpath(optarg, NULL);
6058 if (repo_path == NULL)
6059 return got_error_from_errno2("realpath",
6060 optarg);
6061 break;
6062 default:
6063 usage_blame();
6064 /* NOTREACHED */
6068 argc -= optind;
6069 argv += optind;
6071 if (argc != 1)
6072 usage_blame();
6074 error = got_repo_pack_fds_open(&pack_fds);
6075 if (error != NULL)
6076 goto done;
6078 if (repo_path == NULL) {
6079 cwd = getcwd(NULL, 0);
6080 if (cwd == NULL)
6081 return got_error_from_errno("getcwd");
6082 error = got_worktree_open(&worktree, cwd);
6083 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6084 goto done;
6085 if (worktree)
6086 repo_path =
6087 strdup(got_worktree_get_repo_path(worktree));
6088 else
6089 repo_path = strdup(cwd);
6090 if (repo_path == NULL) {
6091 error = got_error_from_errno("strdup");
6092 goto done;
6096 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6097 if (error != NULL)
6098 goto done;
6100 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6101 worktree);
6102 if (error)
6103 goto done;
6105 init_curses();
6107 error = apply_unveil(got_repo_get_path(repo), NULL);
6108 if (error)
6109 goto done;
6111 error = tog_load_refs(repo, 0);
6112 if (error)
6113 goto done;
6115 if (commit_id_str == NULL) {
6116 struct got_reference *head_ref;
6117 error = got_ref_open(&head_ref, repo, worktree ?
6118 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6119 if (error != NULL)
6120 goto done;
6121 error = got_ref_resolve(&commit_id, repo, head_ref);
6122 got_ref_close(head_ref);
6123 } else {
6124 error = got_repo_match_object_id(&commit_id, NULL,
6125 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6127 if (error != NULL)
6128 goto done;
6130 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6131 if (view == NULL) {
6132 error = got_error_from_errno("view_open");
6133 goto done;
6136 error = got_object_open_as_commit(&commit, repo, commit_id);
6137 if (error)
6138 goto done;
6140 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6141 commit, repo);
6142 if (error)
6143 goto done;
6145 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6146 commit_id, repo);
6147 if (error)
6148 goto done;
6149 if (worktree) {
6150 /* Release work tree lock. */
6151 got_worktree_close(worktree);
6152 worktree = NULL;
6154 error = view_loop(view);
6155 done:
6156 free(repo_path);
6157 free(in_repo_path);
6158 free(link_target);
6159 free(cwd);
6160 free(commit_id);
6161 if (commit)
6162 got_object_commit_close(commit);
6163 if (worktree)
6164 got_worktree_close(worktree);
6165 if (repo) {
6166 const struct got_error *close_err = got_repo_close(repo);
6167 if (error == NULL)
6168 error = close_err;
6170 if (pack_fds) {
6171 const struct got_error *pack_err =
6172 got_repo_pack_fds_close(pack_fds);
6173 if (error == NULL)
6174 error = pack_err;
6176 tog_free_refs();
6177 return error;
6180 static const struct got_error *
6181 draw_tree_entries(struct tog_view *view, const char *parent_path)
6183 struct tog_tree_view_state *s = &view->state.tree;
6184 const struct got_error *err = NULL;
6185 struct got_tree_entry *te;
6186 wchar_t *wline;
6187 struct tog_color *tc;
6188 int width, n, i, nentries;
6189 int limit = view->nlines;
6191 s->ndisplayed = 0;
6192 if (view_is_hsplit_top(view))
6193 --limit; /* border */
6195 werase(view->window);
6197 if (limit == 0)
6198 return NULL;
6200 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6201 0, 0);
6202 if (err)
6203 return err;
6204 if (view_needs_focus_indication(view))
6205 wstandout(view->window);
6206 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6207 if (tc)
6208 wattr_on(view->window,
6209 COLOR_PAIR(tc->colorpair), NULL);
6210 waddwstr(view->window, wline);
6211 if (tc)
6212 wattr_off(view->window,
6213 COLOR_PAIR(tc->colorpair), NULL);
6214 if (view_needs_focus_indication(view))
6215 wstandend(view->window);
6216 free(wline);
6217 wline = NULL;
6218 if (width < view->ncols - 1)
6219 waddch(view->window, '\n');
6220 if (--limit <= 0)
6221 return NULL;
6222 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6223 0, 0);
6224 if (err)
6225 return err;
6226 waddwstr(view->window, wline);
6227 free(wline);
6228 wline = NULL;
6229 if (width < view->ncols - 1)
6230 waddch(view->window, '\n');
6231 if (--limit <= 0)
6232 return NULL;
6233 waddch(view->window, '\n');
6234 if (--limit <= 0)
6235 return NULL;
6237 if (s->first_displayed_entry == NULL) {
6238 te = got_object_tree_get_first_entry(s->tree);
6239 if (s->selected == 0) {
6240 if (view->focussed)
6241 wstandout(view->window);
6242 s->selected_entry = NULL;
6244 waddstr(view->window, " ..\n"); /* parent directory */
6245 if (s->selected == 0 && view->focussed)
6246 wstandend(view->window);
6247 s->ndisplayed++;
6248 if (--limit <= 0)
6249 return NULL;
6250 n = 1;
6251 } else {
6252 n = 0;
6253 te = s->first_displayed_entry;
6256 nentries = got_object_tree_get_nentries(s->tree);
6257 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6258 char *line = NULL, *id_str = NULL, *link_target = NULL;
6259 const char *modestr = "";
6260 mode_t mode;
6262 te = got_object_tree_get_entry(s->tree, i);
6263 mode = got_tree_entry_get_mode(te);
6265 if (s->show_ids) {
6266 err = got_object_id_str(&id_str,
6267 got_tree_entry_get_id(te));
6268 if (err)
6269 return got_error_from_errno(
6270 "got_object_id_str");
6272 if (got_object_tree_entry_is_submodule(te))
6273 modestr = "$";
6274 else if (S_ISLNK(mode)) {
6275 int i;
6277 err = got_tree_entry_get_symlink_target(&link_target,
6278 te, s->repo);
6279 if (err) {
6280 free(id_str);
6281 return err;
6283 for (i = 0; i < strlen(link_target); i++) {
6284 if (!isprint((unsigned char)link_target[i]))
6285 link_target[i] = '?';
6287 modestr = "@";
6289 else if (S_ISDIR(mode))
6290 modestr = "/";
6291 else if (mode & S_IXUSR)
6292 modestr = "*";
6293 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6294 got_tree_entry_get_name(te), modestr,
6295 link_target ? " -> ": "",
6296 link_target ? link_target : "") == -1) {
6297 free(id_str);
6298 free(link_target);
6299 return got_error_from_errno("asprintf");
6301 free(id_str);
6302 free(link_target);
6303 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6304 0, 0);
6305 if (err) {
6306 free(line);
6307 break;
6309 if (n == s->selected) {
6310 if (view->focussed)
6311 wstandout(view->window);
6312 s->selected_entry = te;
6314 tc = match_color(&s->colors, line);
6315 if (tc)
6316 wattr_on(view->window,
6317 COLOR_PAIR(tc->colorpair), NULL);
6318 waddwstr(view->window, wline);
6319 if (tc)
6320 wattr_off(view->window,
6321 COLOR_PAIR(tc->colorpair), NULL);
6322 if (width < view->ncols - 1)
6323 waddch(view->window, '\n');
6324 if (n == s->selected && view->focussed)
6325 wstandend(view->window);
6326 free(line);
6327 free(wline);
6328 wline = NULL;
6329 n++;
6330 s->ndisplayed++;
6331 s->last_displayed_entry = te;
6332 if (--limit <= 0)
6333 break;
6336 return err;
6339 static void
6340 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6342 struct got_tree_entry *te;
6343 int isroot = s->tree == s->root;
6344 int i = 0;
6346 if (s->first_displayed_entry == NULL)
6347 return;
6349 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6350 while (i++ < maxscroll) {
6351 if (te == NULL) {
6352 if (!isroot)
6353 s->first_displayed_entry = NULL;
6354 break;
6356 s->first_displayed_entry = te;
6357 te = got_tree_entry_get_prev(s->tree, te);
6361 static const struct got_error *
6362 tree_scroll_down(struct tog_view *view, int maxscroll)
6364 struct tog_tree_view_state *s = &view->state.tree;
6365 struct got_tree_entry *next, *last;
6366 int n = 0;
6368 if (s->first_displayed_entry)
6369 next = got_tree_entry_get_next(s->tree,
6370 s->first_displayed_entry);
6371 else
6372 next = got_object_tree_get_first_entry(s->tree);
6374 last = s->last_displayed_entry;
6375 while (next && n++ < maxscroll) {
6376 if (last)
6377 last = got_tree_entry_get_next(s->tree, last);
6378 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6379 s->first_displayed_entry = next;
6380 next = got_tree_entry_get_next(s->tree, next);
6384 return NULL;
6387 static const struct got_error *
6388 tree_entry_path(char **path, struct tog_parent_trees *parents,
6389 struct got_tree_entry *te)
6391 const struct got_error *err = NULL;
6392 struct tog_parent_tree *pt;
6393 size_t len = 2; /* for leading slash and NUL */
6395 TAILQ_FOREACH(pt, parents, entry)
6396 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6397 + 1 /* slash */;
6398 if (te)
6399 len += strlen(got_tree_entry_get_name(te));
6401 *path = calloc(1, len);
6402 if (path == NULL)
6403 return got_error_from_errno("calloc");
6405 (*path)[0] = '/';
6406 pt = TAILQ_LAST(parents, tog_parent_trees);
6407 while (pt) {
6408 const char *name = got_tree_entry_get_name(pt->selected_entry);
6409 if (strlcat(*path, name, len) >= len) {
6410 err = got_error(GOT_ERR_NO_SPACE);
6411 goto done;
6413 if (strlcat(*path, "/", len) >= len) {
6414 err = got_error(GOT_ERR_NO_SPACE);
6415 goto done;
6417 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6419 if (te) {
6420 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6421 err = got_error(GOT_ERR_NO_SPACE);
6422 goto done;
6425 done:
6426 if (err) {
6427 free(*path);
6428 *path = NULL;
6430 return err;
6433 static const struct got_error *
6434 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6435 struct got_tree_entry *te, struct tog_parent_trees *parents,
6436 struct got_object_id *commit_id, struct got_repository *repo)
6438 const struct got_error *err = NULL;
6439 char *path;
6440 struct tog_view *blame_view;
6442 *new_view = NULL;
6444 err = tree_entry_path(&path, parents, te);
6445 if (err)
6446 return err;
6448 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6449 if (blame_view == NULL) {
6450 err = got_error_from_errno("view_open");
6451 goto done;
6454 err = open_blame_view(blame_view, path, commit_id, repo);
6455 if (err) {
6456 if (err->code == GOT_ERR_CANCELLED)
6457 err = NULL;
6458 view_close(blame_view);
6459 } else
6460 *new_view = blame_view;
6461 done:
6462 free(path);
6463 return err;
6466 static const struct got_error *
6467 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6468 struct tog_tree_view_state *s)
6470 struct tog_view *log_view;
6471 const struct got_error *err = NULL;
6472 char *path;
6474 *new_view = NULL;
6476 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6477 if (log_view == NULL)
6478 return got_error_from_errno("view_open");
6480 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6481 if (err)
6482 return err;
6484 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6485 path, 0);
6486 if (err)
6487 view_close(log_view);
6488 else
6489 *new_view = log_view;
6490 free(path);
6491 return err;
6494 static const struct got_error *
6495 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6496 const char *head_ref_name, struct got_repository *repo)
6498 const struct got_error *err = NULL;
6499 char *commit_id_str = NULL;
6500 struct tog_tree_view_state *s = &view->state.tree;
6501 struct got_commit_object *commit = NULL;
6503 TAILQ_INIT(&s->parents);
6504 STAILQ_INIT(&s->colors);
6506 s->commit_id = got_object_id_dup(commit_id);
6507 if (s->commit_id == NULL)
6508 return got_error_from_errno("got_object_id_dup");
6510 err = got_object_open_as_commit(&commit, repo, commit_id);
6511 if (err)
6512 goto done;
6515 * The root is opened here and will be closed when the view is closed.
6516 * Any visited subtrees and their path-wise parents are opened and
6517 * closed on demand.
6519 err = got_object_open_as_tree(&s->root, repo,
6520 got_object_commit_get_tree_id(commit));
6521 if (err)
6522 goto done;
6523 s->tree = s->root;
6525 err = got_object_id_str(&commit_id_str, commit_id);
6526 if (err != NULL)
6527 goto done;
6529 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6530 err = got_error_from_errno("asprintf");
6531 goto done;
6534 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6535 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6536 if (head_ref_name) {
6537 s->head_ref_name = strdup(head_ref_name);
6538 if (s->head_ref_name == NULL) {
6539 err = got_error_from_errno("strdup");
6540 goto done;
6543 s->repo = repo;
6545 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6546 err = add_color(&s->colors, "\\$$",
6547 TOG_COLOR_TREE_SUBMODULE,
6548 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6549 if (err)
6550 goto done;
6551 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6552 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6553 if (err)
6554 goto done;
6555 err = add_color(&s->colors, "/$",
6556 TOG_COLOR_TREE_DIRECTORY,
6557 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6558 if (err)
6559 goto done;
6561 err = add_color(&s->colors, "\\*$",
6562 TOG_COLOR_TREE_EXECUTABLE,
6563 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6564 if (err)
6565 goto done;
6567 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6568 get_color_value("TOG_COLOR_COMMIT"));
6569 if (err)
6570 goto done;
6573 view->show = show_tree_view;
6574 view->input = input_tree_view;
6575 view->close = close_tree_view;
6576 view->search_start = search_start_tree_view;
6577 view->search_next = search_next_tree_view;
6578 done:
6579 free(commit_id_str);
6580 if (commit)
6581 got_object_commit_close(commit);
6582 if (err)
6583 close_tree_view(view);
6584 return err;
6587 static const struct got_error *
6588 close_tree_view(struct tog_view *view)
6590 struct tog_tree_view_state *s = &view->state.tree;
6592 free_colors(&s->colors);
6593 free(s->tree_label);
6594 s->tree_label = NULL;
6595 free(s->commit_id);
6596 s->commit_id = NULL;
6597 free(s->head_ref_name);
6598 s->head_ref_name = NULL;
6599 while (!TAILQ_EMPTY(&s->parents)) {
6600 struct tog_parent_tree *parent;
6601 parent = TAILQ_FIRST(&s->parents);
6602 TAILQ_REMOVE(&s->parents, parent, entry);
6603 if (parent->tree != s->root)
6604 got_object_tree_close(parent->tree);
6605 free(parent);
6608 if (s->tree != NULL && s->tree != s->root)
6609 got_object_tree_close(s->tree);
6610 if (s->root)
6611 got_object_tree_close(s->root);
6612 return NULL;
6615 static const struct got_error *
6616 search_start_tree_view(struct tog_view *view)
6618 struct tog_tree_view_state *s = &view->state.tree;
6620 s->matched_entry = NULL;
6621 return NULL;
6624 static int
6625 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6627 regmatch_t regmatch;
6629 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6630 0) == 0;
6633 static const struct got_error *
6634 search_next_tree_view(struct tog_view *view)
6636 struct tog_tree_view_state *s = &view->state.tree;
6637 struct got_tree_entry *te = NULL;
6639 if (!view->searching) {
6640 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6641 return NULL;
6644 if (s->matched_entry) {
6645 if (view->searching == TOG_SEARCH_FORWARD) {
6646 if (s->selected_entry)
6647 te = got_tree_entry_get_next(s->tree,
6648 s->selected_entry);
6649 else
6650 te = got_object_tree_get_first_entry(s->tree);
6651 } else {
6652 if (s->selected_entry == NULL)
6653 te = got_object_tree_get_last_entry(s->tree);
6654 else
6655 te = got_tree_entry_get_prev(s->tree,
6656 s->selected_entry);
6658 } else {
6659 if (s->selected_entry)
6660 te = s->selected_entry;
6661 else if (view->searching == TOG_SEARCH_FORWARD)
6662 te = got_object_tree_get_first_entry(s->tree);
6663 else
6664 te = got_object_tree_get_last_entry(s->tree);
6667 while (1) {
6668 if (te == NULL) {
6669 if (s->matched_entry == NULL) {
6670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6671 return NULL;
6673 if (view->searching == TOG_SEARCH_FORWARD)
6674 te = got_object_tree_get_first_entry(s->tree);
6675 else
6676 te = got_object_tree_get_last_entry(s->tree);
6679 if (match_tree_entry(te, &view->regex)) {
6680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6681 s->matched_entry = te;
6682 break;
6685 if (view->searching == TOG_SEARCH_FORWARD)
6686 te = got_tree_entry_get_next(s->tree, te);
6687 else
6688 te = got_tree_entry_get_prev(s->tree, te);
6691 if (s->matched_entry) {
6692 s->first_displayed_entry = s->matched_entry;
6693 s->selected = 0;
6696 return NULL;
6699 static const struct got_error *
6700 show_tree_view(struct tog_view *view)
6702 const struct got_error *err = NULL;
6703 struct tog_tree_view_state *s = &view->state.tree;
6704 char *parent_path;
6706 err = tree_entry_path(&parent_path, &s->parents, NULL);
6707 if (err)
6708 return err;
6710 err = draw_tree_entries(view, parent_path);
6711 free(parent_path);
6713 view_border(view);
6714 return err;
6717 static const struct got_error *
6718 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6720 const struct got_error *err = NULL;
6721 struct tog_tree_view_state *s = &view->state.tree;
6722 struct tog_view *log_view, *ref_view;
6723 struct got_tree_entry *te;
6724 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6726 switch (ch) {
6727 case 'i':
6728 s->show_ids = !s->show_ids;
6729 view->count = 0;
6730 break;
6731 case 'l':
6732 view->count = 0;
6733 if (!s->selected_entry)
6734 break;
6735 if (view_is_parent_view(view))
6736 view_get_split(view, &begin_y, &begin_x);
6737 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6738 if (view_is_parent_view(view) &&
6739 view->mode == TOG_VIEW_SPLIT_HRZN) {
6740 err = view_init_hsplit(view, begin_y);
6741 if (err)
6742 break;
6744 view->focussed = 0;
6745 log_view->focussed = 1;
6746 log_view->mode = view->mode;
6747 log_view->nlines = view->lines - begin_y;
6748 if (view_is_parent_view(view)) {
6749 view_transfer_size(log_view, view);
6750 err = view_close_child(view);
6751 if (err)
6752 return err;
6753 err = view_set_child(view, log_view);
6754 if (err)
6755 return err;
6756 view->focus_child = 1;
6757 } else
6758 *new_view = log_view;
6759 break;
6760 case 'r':
6761 view->count = 0;
6762 if (view_is_parent_view(view))
6763 view_get_split(view, &begin_y, &begin_x);
6764 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6765 if (ref_view == NULL)
6766 return got_error_from_errno("view_open");
6767 err = open_ref_view(ref_view, s->repo);
6768 if (err) {
6769 view_close(ref_view);
6770 return err;
6772 if (view_is_parent_view(view) &&
6773 view->mode == TOG_VIEW_SPLIT_HRZN) {
6774 err = view_init_hsplit(view, begin_y);
6775 if (err)
6776 break;
6778 view->focussed = 0;
6779 ref_view->focussed = 1;
6780 ref_view->mode = view->mode;
6781 ref_view->nlines = view->lines - begin_y;
6782 if (view_is_parent_view(view)) {
6783 view_transfer_size(ref_view, view);
6784 err = view_close_child(view);
6785 if (err)
6786 return err;
6787 err = view_set_child(view, ref_view);
6788 if (err)
6789 return err;
6790 view->focus_child = 1;
6791 } else
6792 *new_view = ref_view;
6793 break;
6794 case 'g':
6795 case KEY_HOME:
6796 s->selected = 0;
6797 view->count = 0;
6798 if (s->tree == s->root)
6799 s->first_displayed_entry =
6800 got_object_tree_get_first_entry(s->tree);
6801 else
6802 s->first_displayed_entry = NULL;
6803 break;
6804 case 'G':
6805 case KEY_END: {
6806 int eos = view->nlines - 3;
6808 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6809 --eos; /* border */
6810 s->selected = 0;
6811 view->count = 0;
6812 te = got_object_tree_get_last_entry(s->tree);
6813 for (n = 0; n < eos; n++) {
6814 if (te == NULL) {
6815 if (s->tree != s->root) {
6816 s->first_displayed_entry = NULL;
6817 n++;
6819 break;
6821 s->first_displayed_entry = te;
6822 te = got_tree_entry_get_prev(s->tree, te);
6824 if (n > 0)
6825 s->selected = n - 1;
6826 break;
6828 case 'k':
6829 case KEY_UP:
6830 case CTRL('p'):
6831 if (s->selected > 0) {
6832 s->selected--;
6833 break;
6835 tree_scroll_up(s, 1);
6836 if (s->selected_entry == NULL ||
6837 (s->tree == s->root && s->selected_entry ==
6838 got_object_tree_get_first_entry(s->tree)))
6839 view->count = 0;
6840 break;
6841 case CTRL('u'):
6842 case 'u':
6843 nscroll /= 2;
6844 /* FALL THROUGH */
6845 case KEY_PPAGE:
6846 case CTRL('b'):
6847 case 'b':
6848 if (s->tree == s->root) {
6849 if (got_object_tree_get_first_entry(s->tree) ==
6850 s->first_displayed_entry)
6851 s->selected -= MIN(s->selected, nscroll);
6852 } else {
6853 if (s->first_displayed_entry == NULL)
6854 s->selected -= MIN(s->selected, nscroll);
6856 tree_scroll_up(s, MAX(0, nscroll));
6857 if (s->selected_entry == NULL ||
6858 (s->tree == s->root && s->selected_entry ==
6859 got_object_tree_get_first_entry(s->tree)))
6860 view->count = 0;
6861 break;
6862 case 'j':
6863 case KEY_DOWN:
6864 case CTRL('n'):
6865 if (s->selected < s->ndisplayed - 1) {
6866 s->selected++;
6867 break;
6869 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6870 == NULL) {
6871 /* can't scroll any further */
6872 view->count = 0;
6873 break;
6875 tree_scroll_down(view, 1);
6876 break;
6877 case CTRL('d'):
6878 case 'd':
6879 nscroll /= 2;
6880 /* FALL THROUGH */
6881 case KEY_NPAGE:
6882 case CTRL('f'):
6883 case 'f':
6884 case ' ':
6885 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6886 == NULL) {
6887 /* can't scroll any further; move cursor down */
6888 if (s->selected < s->ndisplayed - 1)
6889 s->selected += MIN(nscroll,
6890 s->ndisplayed - s->selected - 1);
6891 else
6892 view->count = 0;
6893 break;
6895 tree_scroll_down(view, nscroll);
6896 break;
6897 case KEY_ENTER:
6898 case '\r':
6899 case KEY_BACKSPACE:
6900 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6901 struct tog_parent_tree *parent;
6902 /* user selected '..' */
6903 if (s->tree == s->root) {
6904 view->count = 0;
6905 break;
6907 parent = TAILQ_FIRST(&s->parents);
6908 TAILQ_REMOVE(&s->parents, parent,
6909 entry);
6910 got_object_tree_close(s->tree);
6911 s->tree = parent->tree;
6912 s->first_displayed_entry =
6913 parent->first_displayed_entry;
6914 s->selected_entry =
6915 parent->selected_entry;
6916 s->selected = parent->selected;
6917 if (s->selected > view->nlines - 3) {
6918 err = offset_selection_down(view);
6919 if (err)
6920 break;
6922 free(parent);
6923 } else if (S_ISDIR(got_tree_entry_get_mode(
6924 s->selected_entry))) {
6925 struct got_tree_object *subtree;
6926 view->count = 0;
6927 err = got_object_open_as_tree(&subtree, s->repo,
6928 got_tree_entry_get_id(s->selected_entry));
6929 if (err)
6930 break;
6931 err = tree_view_visit_subtree(s, subtree);
6932 if (err) {
6933 got_object_tree_close(subtree);
6934 break;
6936 } else if (S_ISREG(got_tree_entry_get_mode(
6937 s->selected_entry))) {
6938 struct tog_view *blame_view;
6939 int begin_x = 0, begin_y = 0;
6941 if (view_is_parent_view(view))
6942 view_get_split(view, &begin_y, &begin_x);
6944 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6945 s->selected_entry, &s->parents,
6946 s->commit_id, s->repo);
6947 if (err)
6948 break;
6950 if (view_is_parent_view(view) &&
6951 view->mode == TOG_VIEW_SPLIT_HRZN) {
6952 err = view_init_hsplit(view, begin_y);
6953 if (err)
6954 break;
6957 view->count = 0;
6958 view->focussed = 0;
6959 blame_view->focussed = 1;
6960 blame_view->mode = view->mode;
6961 blame_view->nlines = view->lines - begin_y;
6962 if (view_is_parent_view(view)) {
6963 view_transfer_size(blame_view, view);
6964 err = view_close_child(view);
6965 if (err)
6966 return err;
6967 err = view_set_child(view, blame_view);
6968 if (err)
6969 return err;
6970 view->focus_child = 1;
6971 } else
6972 *new_view = blame_view;
6974 break;
6975 case KEY_RESIZE:
6976 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6977 s->selected = view->nlines - 4;
6978 view->count = 0;
6979 break;
6980 default:
6981 view->count = 0;
6982 break;
6985 return err;
6988 __dead static void
6989 usage_tree(void)
6991 endwin();
6992 fprintf(stderr,
6993 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6994 getprogname());
6995 exit(1);
6998 static const struct got_error *
6999 cmd_tree(int argc, char *argv[])
7001 const struct got_error *error;
7002 struct got_repository *repo = NULL;
7003 struct got_worktree *worktree = NULL;
7004 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7005 struct got_object_id *commit_id = NULL;
7006 struct got_commit_object *commit = NULL;
7007 const char *commit_id_arg = NULL;
7008 char *label = NULL;
7009 struct got_reference *ref = NULL;
7010 const char *head_ref_name = NULL;
7011 int ch;
7012 struct tog_view *view;
7013 int *pack_fds = NULL;
7015 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7016 switch (ch) {
7017 case 'c':
7018 commit_id_arg = optarg;
7019 break;
7020 case 'r':
7021 repo_path = realpath(optarg, NULL);
7022 if (repo_path == NULL)
7023 return got_error_from_errno2("realpath",
7024 optarg);
7025 break;
7026 default:
7027 usage_tree();
7028 /* NOTREACHED */
7032 argc -= optind;
7033 argv += optind;
7035 if (argc > 1)
7036 usage_tree();
7038 error = got_repo_pack_fds_open(&pack_fds);
7039 if (error != NULL)
7040 goto done;
7042 if (repo_path == NULL) {
7043 cwd = getcwd(NULL, 0);
7044 if (cwd == NULL)
7045 return got_error_from_errno("getcwd");
7046 error = got_worktree_open(&worktree, cwd);
7047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7048 goto done;
7049 if (worktree)
7050 repo_path =
7051 strdup(got_worktree_get_repo_path(worktree));
7052 else
7053 repo_path = strdup(cwd);
7054 if (repo_path == NULL) {
7055 error = got_error_from_errno("strdup");
7056 goto done;
7060 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7061 if (error != NULL)
7062 goto done;
7064 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7065 repo, worktree);
7066 if (error)
7067 goto done;
7069 init_curses();
7071 error = apply_unveil(got_repo_get_path(repo), NULL);
7072 if (error)
7073 goto done;
7075 error = tog_load_refs(repo, 0);
7076 if (error)
7077 goto done;
7079 if (commit_id_arg == NULL) {
7080 error = got_repo_match_object_id(&commit_id, &label,
7081 worktree ? got_worktree_get_head_ref_name(worktree) :
7082 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7083 if (error)
7084 goto done;
7085 head_ref_name = label;
7086 } else {
7087 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7088 if (error == NULL)
7089 head_ref_name = got_ref_get_name(ref);
7090 else if (error->code != GOT_ERR_NOT_REF)
7091 goto done;
7092 error = got_repo_match_object_id(&commit_id, NULL,
7093 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7094 if (error)
7095 goto done;
7098 error = got_object_open_as_commit(&commit, repo, commit_id);
7099 if (error)
7100 goto done;
7102 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7103 if (view == NULL) {
7104 error = got_error_from_errno("view_open");
7105 goto done;
7107 error = open_tree_view(view, commit_id, head_ref_name, repo);
7108 if (error)
7109 goto done;
7110 if (!got_path_is_root_dir(in_repo_path)) {
7111 error = tree_view_walk_path(&view->state.tree, commit,
7112 in_repo_path);
7113 if (error)
7114 goto done;
7117 if (worktree) {
7118 /* Release work tree lock. */
7119 got_worktree_close(worktree);
7120 worktree = NULL;
7122 error = view_loop(view);
7123 done:
7124 free(repo_path);
7125 free(cwd);
7126 free(commit_id);
7127 free(label);
7128 if (ref)
7129 got_ref_close(ref);
7130 if (repo) {
7131 const struct got_error *close_err = got_repo_close(repo);
7132 if (error == NULL)
7133 error = close_err;
7135 if (pack_fds) {
7136 const struct got_error *pack_err =
7137 got_repo_pack_fds_close(pack_fds);
7138 if (error == NULL)
7139 error = pack_err;
7141 tog_free_refs();
7142 return error;
7145 static const struct got_error *
7146 ref_view_load_refs(struct tog_ref_view_state *s)
7148 struct got_reflist_entry *sre;
7149 struct tog_reflist_entry *re;
7151 s->nrefs = 0;
7152 TAILQ_FOREACH(sre, &tog_refs, entry) {
7153 if (strncmp(got_ref_get_name(sre->ref),
7154 "refs/got/", 9) == 0 &&
7155 strncmp(got_ref_get_name(sre->ref),
7156 "refs/got/backup/", 16) != 0)
7157 continue;
7159 re = malloc(sizeof(*re));
7160 if (re == NULL)
7161 return got_error_from_errno("malloc");
7163 re->ref = got_ref_dup(sre->ref);
7164 if (re->ref == NULL)
7165 return got_error_from_errno("got_ref_dup");
7166 re->idx = s->nrefs++;
7167 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7170 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7171 return NULL;
7174 static void
7175 ref_view_free_refs(struct tog_ref_view_state *s)
7177 struct tog_reflist_entry *re;
7179 while (!TAILQ_EMPTY(&s->refs)) {
7180 re = TAILQ_FIRST(&s->refs);
7181 TAILQ_REMOVE(&s->refs, re, entry);
7182 got_ref_close(re->ref);
7183 free(re);
7187 static const struct got_error *
7188 open_ref_view(struct tog_view *view, struct got_repository *repo)
7190 const struct got_error *err = NULL;
7191 struct tog_ref_view_state *s = &view->state.ref;
7193 s->selected_entry = 0;
7194 s->repo = repo;
7196 TAILQ_INIT(&s->refs);
7197 STAILQ_INIT(&s->colors);
7199 err = ref_view_load_refs(s);
7200 if (err)
7201 return err;
7203 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7204 err = add_color(&s->colors, "^refs/heads/",
7205 TOG_COLOR_REFS_HEADS,
7206 get_color_value("TOG_COLOR_REFS_HEADS"));
7207 if (err)
7208 goto done;
7210 err = add_color(&s->colors, "^refs/tags/",
7211 TOG_COLOR_REFS_TAGS,
7212 get_color_value("TOG_COLOR_REFS_TAGS"));
7213 if (err)
7214 goto done;
7216 err = add_color(&s->colors, "^refs/remotes/",
7217 TOG_COLOR_REFS_REMOTES,
7218 get_color_value("TOG_COLOR_REFS_REMOTES"));
7219 if (err)
7220 goto done;
7222 err = add_color(&s->colors, "^refs/got/backup/",
7223 TOG_COLOR_REFS_BACKUP,
7224 get_color_value("TOG_COLOR_REFS_BACKUP"));
7225 if (err)
7226 goto done;
7229 view->show = show_ref_view;
7230 view->input = input_ref_view;
7231 view->close = close_ref_view;
7232 view->search_start = search_start_ref_view;
7233 view->search_next = search_next_ref_view;
7234 done:
7235 if (err)
7236 free_colors(&s->colors);
7237 return err;
7240 static const struct got_error *
7241 close_ref_view(struct tog_view *view)
7243 struct tog_ref_view_state *s = &view->state.ref;
7245 ref_view_free_refs(s);
7246 free_colors(&s->colors);
7248 return NULL;
7251 static const struct got_error *
7252 resolve_reflist_entry(struct got_object_id **commit_id,
7253 struct tog_reflist_entry *re, struct got_repository *repo)
7255 const struct got_error *err = NULL;
7256 struct got_object_id *obj_id;
7257 struct got_tag_object *tag = NULL;
7258 int obj_type;
7260 *commit_id = NULL;
7262 err = got_ref_resolve(&obj_id, repo, re->ref);
7263 if (err)
7264 return err;
7266 err = got_object_get_type(&obj_type, repo, obj_id);
7267 if (err)
7268 goto done;
7270 switch (obj_type) {
7271 case GOT_OBJ_TYPE_COMMIT:
7272 *commit_id = obj_id;
7273 break;
7274 case GOT_OBJ_TYPE_TAG:
7275 err = got_object_open_as_tag(&tag, repo, obj_id);
7276 if (err)
7277 goto done;
7278 free(obj_id);
7279 err = got_object_get_type(&obj_type, repo,
7280 got_object_tag_get_object_id(tag));
7281 if (err)
7282 goto done;
7283 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7284 err = got_error(GOT_ERR_OBJ_TYPE);
7285 goto done;
7287 *commit_id = got_object_id_dup(
7288 got_object_tag_get_object_id(tag));
7289 if (*commit_id == NULL) {
7290 err = got_error_from_errno("got_object_id_dup");
7291 goto done;
7293 break;
7294 default:
7295 err = got_error(GOT_ERR_OBJ_TYPE);
7296 break;
7299 done:
7300 if (tag)
7301 got_object_tag_close(tag);
7302 if (err) {
7303 free(*commit_id);
7304 *commit_id = NULL;
7306 return err;
7309 static const struct got_error *
7310 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7311 struct tog_reflist_entry *re, struct got_repository *repo)
7313 struct tog_view *log_view;
7314 const struct got_error *err = NULL;
7315 struct got_object_id *commit_id = NULL;
7317 *new_view = NULL;
7319 err = resolve_reflist_entry(&commit_id, re, repo);
7320 if (err) {
7321 if (err->code != GOT_ERR_OBJ_TYPE)
7322 return err;
7323 else
7324 return NULL;
7327 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7328 if (log_view == NULL) {
7329 err = got_error_from_errno("view_open");
7330 goto done;
7333 err = open_log_view(log_view, commit_id, repo,
7334 got_ref_get_name(re->ref), "", 0);
7335 done:
7336 if (err)
7337 view_close(log_view);
7338 else
7339 *new_view = log_view;
7340 free(commit_id);
7341 return err;
7344 static void
7345 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7347 struct tog_reflist_entry *re;
7348 int i = 0;
7350 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7351 return;
7353 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7354 while (i++ < maxscroll) {
7355 if (re == NULL)
7356 break;
7357 s->first_displayed_entry = re;
7358 re = TAILQ_PREV(re, tog_reflist_head, entry);
7362 static const struct got_error *
7363 ref_scroll_down(struct tog_view *view, int maxscroll)
7365 struct tog_ref_view_state *s = &view->state.ref;
7366 struct tog_reflist_entry *next, *last;
7367 int n = 0;
7369 if (s->first_displayed_entry)
7370 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7371 else
7372 next = TAILQ_FIRST(&s->refs);
7374 last = s->last_displayed_entry;
7375 while (next && n++ < maxscroll) {
7376 if (last)
7377 last = TAILQ_NEXT(last, entry);
7378 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7379 s->first_displayed_entry = next;
7380 next = TAILQ_NEXT(next, entry);
7384 return NULL;
7387 static const struct got_error *
7388 search_start_ref_view(struct tog_view *view)
7390 struct tog_ref_view_state *s = &view->state.ref;
7392 s->matched_entry = NULL;
7393 return NULL;
7396 static int
7397 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7399 regmatch_t regmatch;
7401 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7402 0) == 0;
7405 static const struct got_error *
7406 search_next_ref_view(struct tog_view *view)
7408 struct tog_ref_view_state *s = &view->state.ref;
7409 struct tog_reflist_entry *re = NULL;
7411 if (!view->searching) {
7412 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7413 return NULL;
7416 if (s->matched_entry) {
7417 if (view->searching == TOG_SEARCH_FORWARD) {
7418 if (s->selected_entry)
7419 re = TAILQ_NEXT(s->selected_entry, entry);
7420 else
7421 re = TAILQ_PREV(s->selected_entry,
7422 tog_reflist_head, entry);
7423 } else {
7424 if (s->selected_entry == NULL)
7425 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7426 else
7427 re = TAILQ_PREV(s->selected_entry,
7428 tog_reflist_head, entry);
7430 } else {
7431 if (s->selected_entry)
7432 re = s->selected_entry;
7433 else if (view->searching == TOG_SEARCH_FORWARD)
7434 re = TAILQ_FIRST(&s->refs);
7435 else
7436 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7439 while (1) {
7440 if (re == NULL) {
7441 if (s->matched_entry == NULL) {
7442 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7443 return NULL;
7445 if (view->searching == TOG_SEARCH_FORWARD)
7446 re = TAILQ_FIRST(&s->refs);
7447 else
7448 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7451 if (match_reflist_entry(re, &view->regex)) {
7452 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7453 s->matched_entry = re;
7454 break;
7457 if (view->searching == TOG_SEARCH_FORWARD)
7458 re = TAILQ_NEXT(re, entry);
7459 else
7460 re = TAILQ_PREV(re, tog_reflist_head, entry);
7463 if (s->matched_entry) {
7464 s->first_displayed_entry = s->matched_entry;
7465 s->selected = 0;
7468 return NULL;
7471 static const struct got_error *
7472 show_ref_view(struct tog_view *view)
7474 const struct got_error *err = NULL;
7475 struct tog_ref_view_state *s = &view->state.ref;
7476 struct tog_reflist_entry *re;
7477 char *line = NULL;
7478 wchar_t *wline;
7479 struct tog_color *tc;
7480 int width, n;
7481 int limit = view->nlines;
7483 werase(view->window);
7485 s->ndisplayed = 0;
7486 if (view_is_hsplit_top(view))
7487 --limit; /* border */
7489 if (limit == 0)
7490 return NULL;
7492 re = s->first_displayed_entry;
7494 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7495 s->nrefs) == -1)
7496 return got_error_from_errno("asprintf");
7498 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7499 if (err) {
7500 free(line);
7501 return err;
7503 if (view_needs_focus_indication(view))
7504 wstandout(view->window);
7505 waddwstr(view->window, wline);
7506 if (view_needs_focus_indication(view))
7507 wstandend(view->window);
7508 free(wline);
7509 wline = NULL;
7510 free(line);
7511 line = NULL;
7512 if (width < view->ncols - 1)
7513 waddch(view->window, '\n');
7514 if (--limit <= 0)
7515 return NULL;
7517 n = 0;
7518 while (re && limit > 0) {
7519 char *line = NULL;
7520 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7522 if (s->show_date) {
7523 struct got_commit_object *ci;
7524 struct got_tag_object *tag;
7525 struct got_object_id *id;
7526 struct tm tm;
7527 time_t t;
7529 err = got_ref_resolve(&id, s->repo, re->ref);
7530 if (err)
7531 return err;
7532 err = got_object_open_as_tag(&tag, s->repo, id);
7533 if (err) {
7534 if (err->code != GOT_ERR_OBJ_TYPE) {
7535 free(id);
7536 return err;
7538 err = got_object_open_as_commit(&ci, s->repo,
7539 id);
7540 if (err) {
7541 free(id);
7542 return err;
7544 t = got_object_commit_get_committer_time(ci);
7545 got_object_commit_close(ci);
7546 } else {
7547 t = got_object_tag_get_tagger_time(tag);
7548 got_object_tag_close(tag);
7550 free(id);
7551 if (gmtime_r(&t, &tm) == NULL)
7552 return got_error_from_errno("gmtime_r");
7553 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7554 return got_error(GOT_ERR_NO_SPACE);
7556 if (got_ref_is_symbolic(re->ref)) {
7557 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7558 ymd : "", got_ref_get_name(re->ref),
7559 got_ref_get_symref_target(re->ref)) == -1)
7560 return got_error_from_errno("asprintf");
7561 } else if (s->show_ids) {
7562 struct got_object_id *id;
7563 char *id_str;
7564 err = got_ref_resolve(&id, s->repo, re->ref);
7565 if (err)
7566 return err;
7567 err = got_object_id_str(&id_str, id);
7568 if (err) {
7569 free(id);
7570 return err;
7572 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7573 got_ref_get_name(re->ref), id_str) == -1) {
7574 err = got_error_from_errno("asprintf");
7575 free(id);
7576 free(id_str);
7577 return err;
7579 free(id);
7580 free(id_str);
7581 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7582 got_ref_get_name(re->ref)) == -1)
7583 return got_error_from_errno("asprintf");
7585 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7586 0, 0);
7587 if (err) {
7588 free(line);
7589 return err;
7591 if (n == s->selected) {
7592 if (view->focussed)
7593 wstandout(view->window);
7594 s->selected_entry = re;
7596 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7597 if (tc)
7598 wattr_on(view->window,
7599 COLOR_PAIR(tc->colorpair), NULL);
7600 waddwstr(view->window, wline);
7601 if (tc)
7602 wattr_off(view->window,
7603 COLOR_PAIR(tc->colorpair), NULL);
7604 if (width < view->ncols - 1)
7605 waddch(view->window, '\n');
7606 if (n == s->selected && view->focussed)
7607 wstandend(view->window);
7608 free(line);
7609 free(wline);
7610 wline = NULL;
7611 n++;
7612 s->ndisplayed++;
7613 s->last_displayed_entry = re;
7615 limit--;
7616 re = TAILQ_NEXT(re, entry);
7619 view_border(view);
7620 return err;
7623 static const struct got_error *
7624 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7625 struct tog_reflist_entry *re, struct got_repository *repo)
7627 const struct got_error *err = NULL;
7628 struct got_object_id *commit_id = NULL;
7629 struct tog_view *tree_view;
7631 *new_view = NULL;
7633 err = resolve_reflist_entry(&commit_id, re, repo);
7634 if (err) {
7635 if (err->code != GOT_ERR_OBJ_TYPE)
7636 return err;
7637 else
7638 return NULL;
7642 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7643 if (tree_view == NULL) {
7644 err = got_error_from_errno("view_open");
7645 goto done;
7648 err = open_tree_view(tree_view, commit_id,
7649 got_ref_get_name(re->ref), repo);
7650 if (err)
7651 goto done;
7653 *new_view = tree_view;
7654 done:
7655 free(commit_id);
7656 return err;
7658 static const struct got_error *
7659 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7661 const struct got_error *err = NULL;
7662 struct tog_ref_view_state *s = &view->state.ref;
7663 struct tog_view *log_view, *tree_view;
7664 struct tog_reflist_entry *re;
7665 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7667 switch (ch) {
7668 case 'i':
7669 s->show_ids = !s->show_ids;
7670 view->count = 0;
7671 break;
7672 case 'm':
7673 s->show_date = !s->show_date;
7674 view->count = 0;
7675 break;
7676 case 'o':
7677 s->sort_by_date = !s->sort_by_date;
7678 view->count = 0;
7679 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7680 got_ref_cmp_by_commit_timestamp_descending :
7681 tog_ref_cmp_by_name, s->repo);
7682 if (err)
7683 break;
7684 got_reflist_object_id_map_free(tog_refs_idmap);
7685 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7686 &tog_refs, s->repo);
7687 if (err)
7688 break;
7689 ref_view_free_refs(s);
7690 err = ref_view_load_refs(s);
7691 break;
7692 case KEY_ENTER:
7693 case '\r':
7694 view->count = 0;
7695 if (!s->selected_entry)
7696 break;
7697 if (view_is_parent_view(view))
7698 view_get_split(view, &begin_y, &begin_x);
7700 err = log_ref_entry(&log_view, begin_y, begin_x,
7701 s->selected_entry, s->repo);
7702 if (err)
7703 break;
7705 if (view_is_parent_view(view) &&
7706 view->mode == TOG_VIEW_SPLIT_HRZN) {
7707 err = view_init_hsplit(view, begin_y);
7708 if (err)
7709 break;
7712 view->focussed = 0;
7713 log_view->focussed = 1;
7714 log_view->mode = view->mode;
7715 log_view->nlines = view->lines - begin_y;
7716 if (view_is_parent_view(view)) {
7717 view_transfer_size(log_view, view);
7718 err = view_close_child(view);
7719 if (err)
7720 return err;
7721 err = view_set_child(view, log_view);
7722 if (err)
7723 return err;
7724 view->focus_child = 1;
7725 } else
7726 *new_view = log_view;
7727 break;
7728 case 't':
7729 view->count = 0;
7730 if (!s->selected_entry)
7731 break;
7732 if (view_is_parent_view(view))
7733 view_get_split(view, &begin_y, &begin_x);
7734 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7735 s->selected_entry, s->repo);
7736 if (err || tree_view == NULL)
7737 break;
7738 if (view_is_parent_view(view) &&
7739 view->mode == TOG_VIEW_SPLIT_HRZN) {
7740 err = view_init_hsplit(view, begin_y);
7741 if (err)
7742 break;
7744 view->focussed = 0;
7745 tree_view->focussed = 1;
7746 tree_view->mode = view->mode;
7747 tree_view->nlines = view->lines - begin_y;
7748 if (view_is_parent_view(view)) {
7749 view_transfer_size(tree_view, view);
7750 err = view_close_child(view);
7751 if (err)
7752 return err;
7753 err = view_set_child(view, tree_view);
7754 if (err)
7755 return err;
7756 view->focus_child = 1;
7757 } else
7758 *new_view = tree_view;
7759 break;
7760 case 'g':
7761 case KEY_HOME:
7762 s->selected = 0;
7763 view->count = 0;
7764 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7765 break;
7766 case 'G':
7767 case KEY_END: {
7768 int eos = view->nlines - 1;
7770 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7771 --eos; /* border */
7772 s->selected = 0;
7773 view->count = 0;
7774 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7775 for (n = 0; n < eos; n++) {
7776 if (re == NULL)
7777 break;
7778 s->first_displayed_entry = re;
7779 re = TAILQ_PREV(re, tog_reflist_head, entry);
7781 if (n > 0)
7782 s->selected = n - 1;
7783 break;
7785 case 'k':
7786 case KEY_UP:
7787 case CTRL('p'):
7788 if (s->selected > 0) {
7789 s->selected--;
7790 break;
7792 ref_scroll_up(s, 1);
7793 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7794 view->count = 0;
7795 break;
7796 case CTRL('u'):
7797 case 'u':
7798 nscroll /= 2;
7799 /* FALL THROUGH */
7800 case KEY_PPAGE:
7801 case CTRL('b'):
7802 case 'b':
7803 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7804 s->selected -= MIN(nscroll, s->selected);
7805 ref_scroll_up(s, MAX(0, nscroll));
7806 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7807 view->count = 0;
7808 break;
7809 case 'j':
7810 case KEY_DOWN:
7811 case CTRL('n'):
7812 if (s->selected < s->ndisplayed - 1) {
7813 s->selected++;
7814 break;
7816 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7817 /* can't scroll any further */
7818 view->count = 0;
7819 break;
7821 ref_scroll_down(view, 1);
7822 break;
7823 case CTRL('d'):
7824 case 'd':
7825 nscroll /= 2;
7826 /* FALL THROUGH */
7827 case KEY_NPAGE:
7828 case CTRL('f'):
7829 case 'f':
7830 case ' ':
7831 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7832 /* can't scroll any further; move cursor down */
7833 if (s->selected < s->ndisplayed - 1)
7834 s->selected += MIN(nscroll,
7835 s->ndisplayed - s->selected - 1);
7836 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7837 s->selected += s->ndisplayed - s->selected - 1;
7838 view->count = 0;
7839 break;
7841 ref_scroll_down(view, nscroll);
7842 break;
7843 case CTRL('l'):
7844 view->count = 0;
7845 tog_free_refs();
7846 err = tog_load_refs(s->repo, s->sort_by_date);
7847 if (err)
7848 break;
7849 ref_view_free_refs(s);
7850 err = ref_view_load_refs(s);
7851 break;
7852 case KEY_RESIZE:
7853 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7854 s->selected = view->nlines - 2;
7855 break;
7856 default:
7857 view->count = 0;
7858 break;
7861 return err;
7864 __dead static void
7865 usage_ref(void)
7867 endwin();
7868 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7869 getprogname());
7870 exit(1);
7873 static const struct got_error *
7874 cmd_ref(int argc, char *argv[])
7876 const struct got_error *error;
7877 struct got_repository *repo = NULL;
7878 struct got_worktree *worktree = NULL;
7879 char *cwd = NULL, *repo_path = NULL;
7880 int ch;
7881 struct tog_view *view;
7882 int *pack_fds = NULL;
7884 while ((ch = getopt(argc, argv, "r:")) != -1) {
7885 switch (ch) {
7886 case 'r':
7887 repo_path = realpath(optarg, NULL);
7888 if (repo_path == NULL)
7889 return got_error_from_errno2("realpath",
7890 optarg);
7891 break;
7892 default:
7893 usage_ref();
7894 /* NOTREACHED */
7898 argc -= optind;
7899 argv += optind;
7901 if (argc > 1)
7902 usage_ref();
7904 error = got_repo_pack_fds_open(&pack_fds);
7905 if (error != NULL)
7906 goto done;
7908 if (repo_path == NULL) {
7909 cwd = getcwd(NULL, 0);
7910 if (cwd == NULL)
7911 return got_error_from_errno("getcwd");
7912 error = got_worktree_open(&worktree, cwd);
7913 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7914 goto done;
7915 if (worktree)
7916 repo_path =
7917 strdup(got_worktree_get_repo_path(worktree));
7918 else
7919 repo_path = strdup(cwd);
7920 if (repo_path == NULL) {
7921 error = got_error_from_errno("strdup");
7922 goto done;
7926 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7927 if (error != NULL)
7928 goto done;
7930 init_curses();
7932 error = apply_unveil(got_repo_get_path(repo), NULL);
7933 if (error)
7934 goto done;
7936 error = tog_load_refs(repo, 0);
7937 if (error)
7938 goto done;
7940 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7941 if (view == NULL) {
7942 error = got_error_from_errno("view_open");
7943 goto done;
7946 error = open_ref_view(view, repo);
7947 if (error)
7948 goto done;
7950 if (worktree) {
7951 /* Release work tree lock. */
7952 got_worktree_close(worktree);
7953 worktree = NULL;
7955 error = view_loop(view);
7956 done:
7957 free(repo_path);
7958 free(cwd);
7959 if (repo) {
7960 const struct got_error *close_err = got_repo_close(repo);
7961 if (close_err)
7962 error = close_err;
7964 if (pack_fds) {
7965 const struct got_error *pack_err =
7966 got_repo_pack_fds_close(pack_fds);
7967 if (error == NULL)
7968 error = pack_err;
7970 tog_free_refs();
7971 return error;
7975 * If view was scrolled down to move the selected line into view when opening a
7976 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7978 static void
7979 offset_selection_up(struct tog_view *view)
7981 switch (view->type) {
7982 case TOG_VIEW_BLAME: {
7983 struct tog_blame_view_state *s = &view->state.blame;
7984 if (s->first_displayed_line == 1) {
7985 s->selected_line = MAX(s->selected_line - view->offset,
7986 1);
7987 break;
7989 if (s->first_displayed_line > view->offset)
7990 s->first_displayed_line -= view->offset;
7991 else
7992 s->first_displayed_line = 1;
7993 s->selected_line += view->offset;
7994 break;
7996 case TOG_VIEW_LOG:
7997 log_scroll_up(&view->state.log, view->offset);
7998 view->state.log.selected += view->offset;
7999 break;
8000 case TOG_VIEW_REF:
8001 ref_scroll_up(&view->state.ref, view->offset);
8002 view->state.ref.selected += view->offset;
8003 break;
8004 case TOG_VIEW_TREE:
8005 tree_scroll_up(&view->state.tree, view->offset);
8006 view->state.tree.selected += view->offset;
8007 break;
8008 default:
8009 break;
8012 view->offset = 0;
8016 * If the selected line is in the section of screen covered by the bottom split,
8017 * scroll down offset lines to move it into view and index its new position.
8019 static const struct got_error *
8020 offset_selection_down(struct tog_view *view)
8022 const struct got_error *err = NULL;
8023 const struct got_error *(*scrolld)(struct tog_view *, int);
8024 int *selected = NULL;
8025 int header, offset;
8027 switch (view->type) {
8028 case TOG_VIEW_BLAME: {
8029 struct tog_blame_view_state *s = &view->state.blame;
8030 header = 3;
8031 scrolld = NULL;
8032 if (s->selected_line > view->nlines - header) {
8033 offset = abs(view->nlines - s->selected_line - header);
8034 s->first_displayed_line += offset;
8035 s->selected_line -= offset;
8036 view->offset = offset;
8038 break;
8040 case TOG_VIEW_LOG: {
8041 struct tog_log_view_state *s = &view->state.log;
8042 scrolld = &log_scroll_down;
8043 header = view_is_parent_view(view) ? 3 : 2;
8044 selected = &s->selected;
8045 break;
8047 case TOG_VIEW_REF: {
8048 struct tog_ref_view_state *s = &view->state.ref;
8049 scrolld = &ref_scroll_down;
8050 header = 3;
8051 selected = &s->selected;
8052 break;
8054 case TOG_VIEW_TREE: {
8055 struct tog_tree_view_state *s = &view->state.tree;
8056 scrolld = &tree_scroll_down;
8057 header = 5;
8058 selected = &s->selected;
8059 break;
8061 default:
8062 selected = NULL;
8063 scrolld = NULL;
8064 header = 0;
8065 break;
8068 if (selected && *selected > view->nlines - header) {
8069 offset = abs(view->nlines - *selected - header);
8070 view->offset = offset;
8071 if (scrolld && offset) {
8072 err = scrolld(view, offset);
8073 *selected -= offset;
8077 return err;
8080 static void
8081 list_commands(FILE *fp)
8083 size_t i;
8085 fprintf(fp, "commands:");
8086 for (i = 0; i < nitems(tog_commands); i++) {
8087 const struct tog_cmd *cmd = &tog_commands[i];
8088 fprintf(fp, " %s", cmd->name);
8090 fputc('\n', fp);
8093 __dead static void
8094 usage(int hflag, int status)
8096 FILE *fp = (status == 0) ? stdout : stderr;
8098 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8099 getprogname());
8100 if (hflag) {
8101 fprintf(fp, "lazy usage: %s path\n", getprogname());
8102 list_commands(fp);
8104 exit(status);
8107 static char **
8108 make_argv(int argc, ...)
8110 va_list ap;
8111 char **argv;
8112 int i;
8114 va_start(ap, argc);
8116 argv = calloc(argc, sizeof(char *));
8117 if (argv == NULL)
8118 err(1, "calloc");
8119 for (i = 0; i < argc; i++) {
8120 argv[i] = strdup(va_arg(ap, char *));
8121 if (argv[i] == NULL)
8122 err(1, "strdup");
8125 va_end(ap);
8126 return argv;
8130 * Try to convert 'tog path' into a 'tog log path' command.
8131 * The user could simply have mistyped the command rather than knowingly
8132 * provided a path. So check whether argv[0] can in fact be resolved
8133 * to a path in the HEAD commit and print a special error if not.
8134 * This hack is for mpi@ <3
8136 static const struct got_error *
8137 tog_log_with_path(int argc, char *argv[])
8139 const struct got_error *error = NULL, *close_err;
8140 const struct tog_cmd *cmd = NULL;
8141 struct got_repository *repo = NULL;
8142 struct got_worktree *worktree = NULL;
8143 struct got_object_id *commit_id = NULL, *id = NULL;
8144 struct got_commit_object *commit = NULL;
8145 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8146 char *commit_id_str = NULL, **cmd_argv = NULL;
8147 int *pack_fds = NULL;
8149 cwd = getcwd(NULL, 0);
8150 if (cwd == NULL)
8151 return got_error_from_errno("getcwd");
8153 error = got_repo_pack_fds_open(&pack_fds);
8154 if (error != NULL)
8155 goto done;
8157 error = got_worktree_open(&worktree, cwd);
8158 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8159 goto done;
8161 if (worktree)
8162 repo_path = strdup(got_worktree_get_repo_path(worktree));
8163 else
8164 repo_path = strdup(cwd);
8165 if (repo_path == NULL) {
8166 error = got_error_from_errno("strdup");
8167 goto done;
8170 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8171 if (error != NULL)
8172 goto done;
8174 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8175 repo, worktree);
8176 if (error)
8177 goto done;
8179 error = tog_load_refs(repo, 0);
8180 if (error)
8181 goto done;
8182 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8183 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8184 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8185 if (error)
8186 goto done;
8188 if (worktree) {
8189 got_worktree_close(worktree);
8190 worktree = NULL;
8193 error = got_object_open_as_commit(&commit, repo, commit_id);
8194 if (error)
8195 goto done;
8197 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8198 if (error) {
8199 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8200 goto done;
8201 fprintf(stderr, "%s: '%s' is no known command or path\n",
8202 getprogname(), argv[0]);
8203 usage(1, 1);
8204 /* not reached */
8207 close_err = got_repo_close(repo);
8208 if (error == NULL)
8209 error = close_err;
8210 repo = NULL;
8212 error = got_object_id_str(&commit_id_str, commit_id);
8213 if (error)
8214 goto done;
8216 cmd = &tog_commands[0]; /* log */
8217 argc = 4;
8218 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8219 error = cmd->cmd_main(argc, cmd_argv);
8220 done:
8221 if (repo) {
8222 close_err = got_repo_close(repo);
8223 if (error == NULL)
8224 error = close_err;
8226 if (commit)
8227 got_object_commit_close(commit);
8228 if (worktree)
8229 got_worktree_close(worktree);
8230 if (pack_fds) {
8231 const struct got_error *pack_err =
8232 got_repo_pack_fds_close(pack_fds);
8233 if (error == NULL)
8234 error = pack_err;
8236 free(id);
8237 free(commit_id_str);
8238 free(commit_id);
8239 free(cwd);
8240 free(repo_path);
8241 free(in_repo_path);
8242 if (cmd_argv) {
8243 int i;
8244 for (i = 0; i < argc; i++)
8245 free(cmd_argv[i]);
8246 free(cmd_argv);
8248 tog_free_refs();
8249 return error;
8252 int
8253 main(int argc, char *argv[])
8255 const struct got_error *error = NULL;
8256 const struct tog_cmd *cmd = NULL;
8257 int ch, hflag = 0, Vflag = 0;
8258 char **cmd_argv = NULL;
8259 static const struct option longopts[] = {
8260 { "version", no_argument, NULL, 'V' },
8261 { NULL, 0, NULL, 0}
8263 char *diff_algo_str = NULL;
8265 setlocale(LC_CTYPE, "");
8267 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8268 switch (ch) {
8269 case 'h':
8270 hflag = 1;
8271 break;
8272 case 'V':
8273 Vflag = 1;
8274 break;
8275 default:
8276 usage(hflag, 1);
8277 /* NOTREACHED */
8281 argc -= optind;
8282 argv += optind;
8283 optind = 1;
8284 optreset = 1;
8286 if (Vflag) {
8287 got_version_print_str();
8288 return 0;
8291 #ifndef PROFILE
8292 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8293 NULL) == -1)
8294 err(1, "pledge");
8295 #endif
8297 if (argc == 0) {
8298 if (hflag)
8299 usage(hflag, 0);
8300 /* Build an argument vector which runs a default command. */
8301 cmd = &tog_commands[0];
8302 argc = 1;
8303 cmd_argv = make_argv(argc, cmd->name);
8304 } else {
8305 size_t i;
8307 /* Did the user specify a command? */
8308 for (i = 0; i < nitems(tog_commands); i++) {
8309 if (strncmp(tog_commands[i].name, argv[0],
8310 strlen(argv[0])) == 0) {
8311 cmd = &tog_commands[i];
8312 break;
8317 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8318 if (diff_algo_str) {
8319 if (strcasecmp(diff_algo_str, "patience") == 0)
8320 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8321 if (strcasecmp(diff_algo_str, "myers") == 0)
8322 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8325 if (cmd == NULL) {
8326 if (argc != 1)
8327 usage(0, 1);
8328 /* No command specified; try log with a path */
8329 error = tog_log_with_path(argc, argv);
8330 } else {
8331 if (hflag)
8332 cmd->cmd_usage();
8333 else
8334 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8337 endwin();
8338 putchar('\n');
8339 if (cmd_argv) {
8340 int i;
8341 for (i = 0; i < argc; i++)
8342 free(cmd_argv[i]);
8343 free(cmd_argv);
8346 if (error && error->code != GOT_ERR_CANCELLED)
8347 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8348 return 0;