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 case 'K':
4830 up = 1;
4831 /* FALL THROUGH */
4832 case '>':
4833 case '.':
4834 case 'J':
4835 if (s->parent_view == NULL) {
4836 view->count = 0;
4837 break;
4839 s->parent_view->count = view->count;
4841 if (s->parent_view->type == TOG_VIEW_LOG) {
4842 ls = &s->parent_view->state.log;
4843 old_selected_entry = ls->selected_entry;
4845 err = input_log_view(NULL, s->parent_view,
4846 up ? KEY_UP : KEY_DOWN);
4847 if (err)
4848 break;
4849 view->count = s->parent_view->count;
4851 if (old_selected_entry == ls->selected_entry)
4852 break;
4854 err = set_selected_commit(s, ls->selected_entry);
4855 if (err)
4856 break;
4857 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4858 struct tog_blame_view_state *bs;
4859 struct got_object_id *id, *prev_id;
4861 bs = &s->parent_view->state.blame;
4862 prev_id = get_annotation_for_line(bs->blame.lines,
4863 bs->blame.nlines, bs->last_diffed_line);
4865 err = input_blame_view(&view, s->parent_view,
4866 up ? KEY_UP : KEY_DOWN);
4867 if (err)
4868 break;
4869 view->count = s->parent_view->count;
4871 if (prev_id == NULL)
4872 break;
4873 id = get_selected_commit_id(bs->blame.lines,
4874 bs->blame.nlines, bs->first_displayed_line,
4875 bs->selected_line);
4876 if (id == NULL)
4877 break;
4879 if (!got_object_id_cmp(prev_id, id))
4880 break;
4882 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4883 if (err)
4884 break;
4886 s->first_displayed_line = 1;
4887 s->last_displayed_line = view->nlines;
4888 s->matched_line = 0;
4889 view->x = 0;
4891 diff_view_indicate_progress(view);
4892 err = create_diff(s);
4893 break;
4894 default:
4895 view->count = 0;
4896 break;
4899 return err;
4902 static const struct got_error *
4903 cmd_diff(int argc, char *argv[])
4905 const struct got_error *error = NULL;
4906 struct got_repository *repo = NULL;
4907 struct got_worktree *worktree = NULL;
4908 struct got_object_id *id1 = NULL, *id2 = NULL;
4909 char *repo_path = NULL, *cwd = NULL;
4910 char *id_str1 = NULL, *id_str2 = NULL;
4911 char *label1 = NULL, *label2 = NULL;
4912 int diff_context = 3, ignore_whitespace = 0;
4913 int ch, force_text_diff = 0;
4914 const char *errstr;
4915 struct tog_view *view;
4916 int *pack_fds = NULL;
4918 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4919 switch (ch) {
4920 case 'a':
4921 force_text_diff = 1;
4922 break;
4923 case 'C':
4924 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4925 &errstr);
4926 if (errstr != NULL)
4927 errx(1, "number of context lines is %s: %s",
4928 errstr, errstr);
4929 break;
4930 case 'r':
4931 repo_path = realpath(optarg, NULL);
4932 if (repo_path == NULL)
4933 return got_error_from_errno2("realpath",
4934 optarg);
4935 got_path_strip_trailing_slashes(repo_path);
4936 break;
4937 case 'w':
4938 ignore_whitespace = 1;
4939 break;
4940 default:
4941 usage_diff();
4942 /* NOTREACHED */
4946 argc -= optind;
4947 argv += optind;
4949 if (argc == 0) {
4950 usage_diff(); /* TODO show local worktree changes */
4951 } else if (argc == 2) {
4952 id_str1 = argv[0];
4953 id_str2 = argv[1];
4954 } else
4955 usage_diff();
4957 error = got_repo_pack_fds_open(&pack_fds);
4958 if (error)
4959 goto done;
4961 if (repo_path == NULL) {
4962 cwd = getcwd(NULL, 0);
4963 if (cwd == NULL)
4964 return got_error_from_errno("getcwd");
4965 error = got_worktree_open(&worktree, cwd);
4966 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4967 goto done;
4968 if (worktree)
4969 repo_path =
4970 strdup(got_worktree_get_repo_path(worktree));
4971 else
4972 repo_path = strdup(cwd);
4973 if (repo_path == NULL) {
4974 error = got_error_from_errno("strdup");
4975 goto done;
4979 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4980 if (error)
4981 goto done;
4983 init_curses();
4985 error = apply_unveil(got_repo_get_path(repo), NULL);
4986 if (error)
4987 goto done;
4989 error = tog_load_refs(repo, 0);
4990 if (error)
4991 goto done;
4993 error = got_repo_match_object_id(&id1, &label1, id_str1,
4994 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4995 if (error)
4996 goto done;
4998 error = got_repo_match_object_id(&id2, &label2, id_str2,
4999 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5000 if (error)
5001 goto done;
5003 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5004 if (view == NULL) {
5005 error = got_error_from_errno("view_open");
5006 goto done;
5008 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5009 ignore_whitespace, force_text_diff, NULL, repo);
5010 if (error)
5011 goto done;
5012 error = view_loop(view);
5013 done:
5014 free(label1);
5015 free(label2);
5016 free(repo_path);
5017 free(cwd);
5018 if (repo) {
5019 const struct got_error *close_err = got_repo_close(repo);
5020 if (error == NULL)
5021 error = close_err;
5023 if (worktree)
5024 got_worktree_close(worktree);
5025 if (pack_fds) {
5026 const struct got_error *pack_err =
5027 got_repo_pack_fds_close(pack_fds);
5028 if (error == NULL)
5029 error = pack_err;
5031 tog_free_refs();
5032 return error;
5035 __dead static void
5036 usage_blame(void)
5038 endwin();
5039 fprintf(stderr,
5040 "usage: %s blame [-c commit] [-r repository-path] path\n",
5041 getprogname());
5042 exit(1);
5045 struct tog_blame_line {
5046 int annotated;
5047 struct got_object_id *id;
5050 static const struct got_error *
5051 draw_blame(struct tog_view *view)
5053 struct tog_blame_view_state *s = &view->state.blame;
5054 struct tog_blame *blame = &s->blame;
5055 regmatch_t *regmatch = &view->regmatch;
5056 const struct got_error *err;
5057 int lineno = 0, nprinted = 0;
5058 char *line = NULL;
5059 size_t linesize = 0;
5060 ssize_t linelen;
5061 wchar_t *wline;
5062 int width;
5063 struct tog_blame_line *blame_line;
5064 struct got_object_id *prev_id = NULL;
5065 char *id_str;
5066 struct tog_color *tc;
5068 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5069 if (err)
5070 return err;
5072 rewind(blame->f);
5073 werase(view->window);
5075 if (asprintf(&line, "commit %s", id_str) == -1) {
5076 err = got_error_from_errno("asprintf");
5077 free(id_str);
5078 return err;
5081 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5082 free(line);
5083 line = NULL;
5084 if (err)
5085 return err;
5086 if (view_needs_focus_indication(view))
5087 wstandout(view->window);
5088 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5089 if (tc)
5090 wattr_on(view->window,
5091 COLOR_PAIR(tc->colorpair), NULL);
5092 waddwstr(view->window, wline);
5093 if (tc)
5094 wattr_off(view->window,
5095 COLOR_PAIR(tc->colorpair), NULL);
5096 if (view_needs_focus_indication(view))
5097 wstandend(view->window);
5098 free(wline);
5099 wline = NULL;
5100 if (width < view->ncols - 1)
5101 waddch(view->window, '\n');
5103 if (asprintf(&line, "[%d/%d] %s%s",
5104 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5105 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5106 free(id_str);
5107 return got_error_from_errno("asprintf");
5109 free(id_str);
5110 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5111 free(line);
5112 line = NULL;
5113 if (err)
5114 return err;
5115 waddwstr(view->window, wline);
5116 free(wline);
5117 wline = NULL;
5118 if (width < view->ncols - 1)
5119 waddch(view->window, '\n');
5121 s->eof = 0;
5122 view->maxx = 0;
5123 while (nprinted < view->nlines - 2) {
5124 linelen = getline(&line, &linesize, blame->f);
5125 if (linelen == -1) {
5126 if (feof(blame->f)) {
5127 s->eof = 1;
5128 break;
5130 free(line);
5131 return got_ferror(blame->f, GOT_ERR_IO);
5133 if (++lineno < s->first_displayed_line)
5134 continue;
5136 /* Set view->maxx based on full line length. */
5137 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5138 if (err) {
5139 free(line);
5140 return err;
5142 free(wline);
5143 wline = NULL;
5144 view->maxx = MAX(view->maxx, width);
5146 if (nprinted == s->selected_line - 1)
5147 wstandout(view->window);
5149 if (blame->nlines > 0) {
5150 blame_line = &blame->lines[lineno - 1];
5151 if (blame_line->annotated && prev_id &&
5152 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5153 !(nprinted == s->selected_line - 1)) {
5154 waddstr(view->window, " ");
5155 } else if (blame_line->annotated) {
5156 char *id_str;
5157 err = got_object_id_str(&id_str,
5158 blame_line->id);
5159 if (err) {
5160 free(line);
5161 return err;
5163 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5164 if (tc)
5165 wattr_on(view->window,
5166 COLOR_PAIR(tc->colorpair), NULL);
5167 wprintw(view->window, "%.8s", id_str);
5168 if (tc)
5169 wattr_off(view->window,
5170 COLOR_PAIR(tc->colorpair), NULL);
5171 free(id_str);
5172 prev_id = blame_line->id;
5173 } else {
5174 waddstr(view->window, "........");
5175 prev_id = NULL;
5177 } else {
5178 waddstr(view->window, "........");
5179 prev_id = NULL;
5182 if (nprinted == s->selected_line - 1)
5183 wstandend(view->window);
5184 waddstr(view->window, " ");
5186 if (view->ncols <= 9) {
5187 width = 9;
5188 } else if (s->first_displayed_line + nprinted ==
5189 s->matched_line &&
5190 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5191 err = add_matched_line(&width, line, view->ncols - 9, 9,
5192 view->window, view->x, regmatch);
5193 if (err) {
5194 free(line);
5195 return err;
5197 width += 9;
5198 } else {
5199 int skip;
5200 err = format_line(&wline, &width, &skip, line,
5201 view->x, view->ncols - 9, 9, 1);
5202 if (err) {
5203 free(line);
5204 return err;
5206 waddwstr(view->window, &wline[skip]);
5207 width += 9;
5208 free(wline);
5209 wline = NULL;
5212 if (width <= view->ncols - 1)
5213 waddch(view->window, '\n');
5214 if (++nprinted == 1)
5215 s->first_displayed_line = lineno;
5217 free(line);
5218 s->last_displayed_line = lineno;
5220 view_border(view);
5222 return NULL;
5225 static const struct got_error *
5226 blame_cb(void *arg, int nlines, int lineno,
5227 struct got_commit_object *commit, struct got_object_id *id)
5229 const struct got_error *err = NULL;
5230 struct tog_blame_cb_args *a = arg;
5231 struct tog_blame_line *line;
5232 int errcode;
5234 if (nlines != a->nlines ||
5235 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5236 return got_error(GOT_ERR_RANGE);
5238 errcode = pthread_mutex_lock(&tog_mutex);
5239 if (errcode)
5240 return got_error_set_errno(errcode, "pthread_mutex_lock");
5242 if (*a->quit) { /* user has quit the blame view */
5243 err = got_error(GOT_ERR_ITER_COMPLETED);
5244 goto done;
5247 if (lineno == -1)
5248 goto done; /* no change in this commit */
5250 line = &a->lines[lineno - 1];
5251 if (line->annotated)
5252 goto done;
5254 line->id = got_object_id_dup(id);
5255 if (line->id == NULL) {
5256 err = got_error_from_errno("got_object_id_dup");
5257 goto done;
5259 line->annotated = 1;
5260 done:
5261 errcode = pthread_mutex_unlock(&tog_mutex);
5262 if (errcode)
5263 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5264 return err;
5267 static void *
5268 blame_thread(void *arg)
5270 const struct got_error *err, *close_err;
5271 struct tog_blame_thread_args *ta = arg;
5272 struct tog_blame_cb_args *a = ta->cb_args;
5273 int errcode, fd1 = -1, fd2 = -1;
5274 FILE *f1 = NULL, *f2 = NULL;
5276 fd1 = got_opentempfd();
5277 if (fd1 == -1)
5278 return (void *)got_error_from_errno("got_opentempfd");
5280 fd2 = got_opentempfd();
5281 if (fd2 == -1) {
5282 err = got_error_from_errno("got_opentempfd");
5283 goto done;
5286 f1 = got_opentemp();
5287 if (f1 == NULL) {
5288 err = (void *)got_error_from_errno("got_opentemp");
5289 goto done;
5291 f2 = got_opentemp();
5292 if (f2 == NULL) {
5293 err = (void *)got_error_from_errno("got_opentemp");
5294 goto done;
5297 err = block_signals_used_by_main_thread();
5298 if (err)
5299 goto done;
5301 err = got_blame(ta->path, a->commit_id, ta->repo,
5302 tog_diff_algo, blame_cb, ta->cb_args,
5303 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5304 if (err && err->code == GOT_ERR_CANCELLED)
5305 err = NULL;
5307 errcode = pthread_mutex_lock(&tog_mutex);
5308 if (errcode) {
5309 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5310 goto done;
5313 close_err = got_repo_close(ta->repo);
5314 if (err == NULL)
5315 err = close_err;
5316 ta->repo = NULL;
5317 *ta->complete = 1;
5319 errcode = pthread_mutex_unlock(&tog_mutex);
5320 if (errcode && err == NULL)
5321 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5323 done:
5324 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5325 err = got_error_from_errno("close");
5326 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5327 err = got_error_from_errno("close");
5328 if (f1 && fclose(f1) == EOF && err == NULL)
5329 err = got_error_from_errno("fclose");
5330 if (f2 && fclose(f2) == EOF && err == NULL)
5331 err = got_error_from_errno("fclose");
5333 return (void *)err;
5336 static struct got_object_id *
5337 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5338 int first_displayed_line, int selected_line)
5340 struct tog_blame_line *line;
5342 if (nlines <= 0)
5343 return NULL;
5345 line = &lines[first_displayed_line - 1 + selected_line - 1];
5346 if (!line->annotated)
5347 return NULL;
5349 return line->id;
5352 static struct got_object_id *
5353 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5354 int lineno)
5356 struct tog_blame_line *line;
5358 if (nlines <= 0 || lineno >= nlines)
5359 return NULL;
5361 line = &lines[lineno - 1];
5362 if (!line->annotated)
5363 return NULL;
5365 return line->id;
5368 static const struct got_error *
5369 stop_blame(struct tog_blame *blame)
5371 const struct got_error *err = NULL;
5372 int i;
5374 if (blame->thread) {
5375 int errcode;
5376 errcode = pthread_mutex_unlock(&tog_mutex);
5377 if (errcode)
5378 return got_error_set_errno(errcode,
5379 "pthread_mutex_unlock");
5380 errcode = pthread_join(blame->thread, (void **)&err);
5381 if (errcode)
5382 return got_error_set_errno(errcode, "pthread_join");
5383 errcode = pthread_mutex_lock(&tog_mutex);
5384 if (errcode)
5385 return got_error_set_errno(errcode,
5386 "pthread_mutex_lock");
5387 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5388 err = NULL;
5389 blame->thread = NULL;
5391 if (blame->thread_args.repo) {
5392 const struct got_error *close_err;
5393 close_err = got_repo_close(blame->thread_args.repo);
5394 if (err == NULL)
5395 err = close_err;
5396 blame->thread_args.repo = NULL;
5398 if (blame->f) {
5399 if (fclose(blame->f) == EOF && err == NULL)
5400 err = got_error_from_errno("fclose");
5401 blame->f = NULL;
5403 if (blame->lines) {
5404 for (i = 0; i < blame->nlines; i++)
5405 free(blame->lines[i].id);
5406 free(blame->lines);
5407 blame->lines = NULL;
5409 free(blame->cb_args.commit_id);
5410 blame->cb_args.commit_id = NULL;
5411 if (blame->pack_fds) {
5412 const struct got_error *pack_err =
5413 got_repo_pack_fds_close(blame->pack_fds);
5414 if (err == NULL)
5415 err = pack_err;
5416 blame->pack_fds = NULL;
5418 return err;
5421 static const struct got_error *
5422 cancel_blame_view(void *arg)
5424 const struct got_error *err = NULL;
5425 int *done = arg;
5426 int errcode;
5428 errcode = pthread_mutex_lock(&tog_mutex);
5429 if (errcode)
5430 return got_error_set_errno(errcode,
5431 "pthread_mutex_unlock");
5433 if (*done)
5434 err = got_error(GOT_ERR_CANCELLED);
5436 errcode = pthread_mutex_unlock(&tog_mutex);
5437 if (errcode)
5438 return got_error_set_errno(errcode,
5439 "pthread_mutex_lock");
5441 return err;
5444 static const struct got_error *
5445 run_blame(struct tog_view *view)
5447 struct tog_blame_view_state *s = &view->state.blame;
5448 struct tog_blame *blame = &s->blame;
5449 const struct got_error *err = NULL;
5450 struct got_commit_object *commit = NULL;
5451 struct got_blob_object *blob = NULL;
5452 struct got_repository *thread_repo = NULL;
5453 struct got_object_id *obj_id = NULL;
5454 int obj_type, fd = -1;
5455 int *pack_fds = NULL;
5457 err = got_object_open_as_commit(&commit, s->repo,
5458 &s->blamed_commit->id);
5459 if (err)
5460 return err;
5462 fd = got_opentempfd();
5463 if (fd == -1) {
5464 err = got_error_from_errno("got_opentempfd");
5465 goto done;
5468 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5469 if (err)
5470 goto done;
5472 err = got_object_get_type(&obj_type, s->repo, obj_id);
5473 if (err)
5474 goto done;
5476 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5477 err = got_error(GOT_ERR_OBJ_TYPE);
5478 goto done;
5481 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5482 if (err)
5483 goto done;
5484 blame->f = got_opentemp();
5485 if (blame->f == NULL) {
5486 err = got_error_from_errno("got_opentemp");
5487 goto done;
5489 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5490 &blame->line_offsets, blame->f, blob);
5491 if (err)
5492 goto done;
5493 if (blame->nlines == 0) {
5494 s->blame_complete = 1;
5495 goto done;
5498 /* Don't include \n at EOF in the blame line count. */
5499 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5500 blame->nlines--;
5502 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5503 if (blame->lines == NULL) {
5504 err = got_error_from_errno("calloc");
5505 goto done;
5508 err = got_repo_pack_fds_open(&pack_fds);
5509 if (err)
5510 goto done;
5511 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5512 pack_fds);
5513 if (err)
5514 goto done;
5516 blame->pack_fds = pack_fds;
5517 blame->cb_args.view = view;
5518 blame->cb_args.lines = blame->lines;
5519 blame->cb_args.nlines = blame->nlines;
5520 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5521 if (blame->cb_args.commit_id == NULL) {
5522 err = got_error_from_errno("got_object_id_dup");
5523 goto done;
5525 blame->cb_args.quit = &s->done;
5527 blame->thread_args.path = s->path;
5528 blame->thread_args.repo = thread_repo;
5529 blame->thread_args.cb_args = &blame->cb_args;
5530 blame->thread_args.complete = &s->blame_complete;
5531 blame->thread_args.cancel_cb = cancel_blame_view;
5532 blame->thread_args.cancel_arg = &s->done;
5533 s->blame_complete = 0;
5535 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5536 s->first_displayed_line = 1;
5537 s->last_displayed_line = view->nlines;
5538 s->selected_line = 1;
5540 s->matched_line = 0;
5542 done:
5543 if (commit)
5544 got_object_commit_close(commit);
5545 if (fd != -1 && close(fd) == -1 && err == NULL)
5546 err = got_error_from_errno("close");
5547 if (blob)
5548 got_object_blob_close(blob);
5549 free(obj_id);
5550 if (err)
5551 stop_blame(blame);
5552 return err;
5555 static const struct got_error *
5556 open_blame_view(struct tog_view *view, char *path,
5557 struct got_object_id *commit_id, struct got_repository *repo)
5559 const struct got_error *err = NULL;
5560 struct tog_blame_view_state *s = &view->state.blame;
5562 STAILQ_INIT(&s->blamed_commits);
5564 s->path = strdup(path);
5565 if (s->path == NULL)
5566 return got_error_from_errno("strdup");
5568 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5569 if (err) {
5570 free(s->path);
5571 return err;
5574 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5575 s->first_displayed_line = 1;
5576 s->last_displayed_line = view->nlines;
5577 s->selected_line = 1;
5578 s->blame_complete = 0;
5579 s->repo = repo;
5580 s->commit_id = commit_id;
5581 memset(&s->blame, 0, sizeof(s->blame));
5583 STAILQ_INIT(&s->colors);
5584 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5585 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5586 get_color_value("TOG_COLOR_COMMIT"));
5587 if (err)
5588 return err;
5591 view->show = show_blame_view;
5592 view->input = input_blame_view;
5593 view->reset = reset_blame_view;
5594 view->close = close_blame_view;
5595 view->search_start = search_start_blame_view;
5596 view->search_next = search_next_blame_view;
5598 return run_blame(view);
5601 static const struct got_error *
5602 close_blame_view(struct tog_view *view)
5604 const struct got_error *err = NULL;
5605 struct tog_blame_view_state *s = &view->state.blame;
5607 if (s->blame.thread)
5608 err = stop_blame(&s->blame);
5610 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5611 struct got_object_qid *blamed_commit;
5612 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5613 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5614 got_object_qid_free(blamed_commit);
5617 free(s->path);
5618 free_colors(&s->colors);
5619 return err;
5622 static const struct got_error *
5623 search_start_blame_view(struct tog_view *view)
5625 struct tog_blame_view_state *s = &view->state.blame;
5627 s->matched_line = 0;
5628 return NULL;
5631 static const struct got_error *
5632 search_next_blame_view(struct tog_view *view)
5634 struct tog_blame_view_state *s = &view->state.blame;
5635 const struct got_error *err = NULL;
5636 int lineno;
5637 char *line = NULL;
5638 size_t linesize = 0;
5639 ssize_t linelen;
5641 if (!view->searching) {
5642 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5643 return NULL;
5646 if (s->matched_line) {
5647 if (view->searching == TOG_SEARCH_FORWARD)
5648 lineno = s->matched_line + 1;
5649 else
5650 lineno = s->matched_line - 1;
5651 } else
5652 lineno = s->first_displayed_line - 1 + s->selected_line;
5654 while (1) {
5655 off_t offset;
5657 if (lineno <= 0 || lineno > s->blame.nlines) {
5658 if (s->matched_line == 0) {
5659 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5660 break;
5663 if (view->searching == TOG_SEARCH_FORWARD)
5664 lineno = 1;
5665 else
5666 lineno = s->blame.nlines;
5669 offset = s->blame.line_offsets[lineno - 1];
5670 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5671 free(line);
5672 return got_error_from_errno("fseeko");
5674 linelen = getline(&line, &linesize, s->blame.f);
5675 if (linelen != -1) {
5676 char *exstr;
5677 err = expand_tab(&exstr, line);
5678 if (err)
5679 break;
5680 if (match_line(exstr, &view->regex, 1,
5681 &view->regmatch)) {
5682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5683 s->matched_line = lineno;
5684 free(exstr);
5685 break;
5687 free(exstr);
5689 if (view->searching == TOG_SEARCH_FORWARD)
5690 lineno++;
5691 else
5692 lineno--;
5694 free(line);
5696 if (s->matched_line) {
5697 s->first_displayed_line = s->matched_line;
5698 s->selected_line = 1;
5701 return err;
5704 static const struct got_error *
5705 show_blame_view(struct tog_view *view)
5707 const struct got_error *err = NULL;
5708 struct tog_blame_view_state *s = &view->state.blame;
5709 int errcode;
5711 if (s->blame.thread == NULL && !s->blame_complete) {
5712 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5713 &s->blame.thread_args);
5714 if (errcode)
5715 return got_error_set_errno(errcode, "pthread_create");
5717 halfdelay(1); /* fast refresh while annotating */
5720 if (s->blame_complete)
5721 halfdelay(10); /* disable fast refresh */
5723 err = draw_blame(view);
5725 view_border(view);
5726 return err;
5729 static const struct got_error *
5730 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5732 const struct got_error *err = NULL, *thread_err = NULL;
5733 struct tog_view *diff_view;
5734 struct tog_blame_view_state *s = &view->state.blame;
5735 int eos, nscroll, begin_y = 0, begin_x = 0;
5737 eos = nscroll = view->nlines - 2;
5738 if (view_is_hsplit_top(view))
5739 --eos; /* border */
5741 switch (ch) {
5742 case '0':
5743 view->x = 0;
5744 break;
5745 case '$':
5746 view->x = MAX(view->maxx - view->ncols / 3, 0);
5747 view->count = 0;
5748 break;
5749 case KEY_RIGHT:
5750 case 'l':
5751 if (view->x + view->ncols / 3 < view->maxx)
5752 view->x += 2; /* move two columns right */
5753 else
5754 view->count = 0;
5755 break;
5756 case KEY_LEFT:
5757 case 'h':
5758 view->x -= MIN(view->x, 2); /* move two columns back */
5759 if (view->x <= 0)
5760 view->count = 0;
5761 break;
5762 case 'q':
5763 s->done = 1;
5764 break;
5765 case 'g':
5766 case KEY_HOME:
5767 s->selected_line = 1;
5768 s->first_displayed_line = 1;
5769 view->count = 0;
5770 break;
5771 case 'G':
5772 case KEY_END:
5773 if (s->blame.nlines < eos) {
5774 s->selected_line = s->blame.nlines;
5775 s->first_displayed_line = 1;
5776 } else {
5777 s->selected_line = eos;
5778 s->first_displayed_line = s->blame.nlines - (eos - 1);
5780 view->count = 0;
5781 break;
5782 case 'k':
5783 case KEY_UP:
5784 case CTRL('p'):
5785 if (s->selected_line > 1)
5786 s->selected_line--;
5787 else if (s->selected_line == 1 &&
5788 s->first_displayed_line > 1)
5789 s->first_displayed_line--;
5790 else
5791 view->count = 0;
5792 break;
5793 case CTRL('u'):
5794 case 'u':
5795 nscroll /= 2;
5796 /* FALL THROUGH */
5797 case KEY_PPAGE:
5798 case CTRL('b'):
5799 case 'b':
5800 if (s->first_displayed_line == 1) {
5801 if (view->count > 1)
5802 nscroll += nscroll;
5803 s->selected_line = MAX(1, s->selected_line - nscroll);
5804 view->count = 0;
5805 break;
5807 if (s->first_displayed_line > nscroll)
5808 s->first_displayed_line -= nscroll;
5809 else
5810 s->first_displayed_line = 1;
5811 break;
5812 case 'j':
5813 case KEY_DOWN:
5814 case CTRL('n'):
5815 if (s->selected_line < eos && s->first_displayed_line +
5816 s->selected_line <= s->blame.nlines)
5817 s->selected_line++;
5818 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5819 s->first_displayed_line++;
5820 else
5821 view->count = 0;
5822 break;
5823 case 'c':
5824 case 'p': {
5825 struct got_object_id *id = NULL;
5827 view->count = 0;
5828 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5829 s->first_displayed_line, s->selected_line);
5830 if (id == NULL)
5831 break;
5832 if (ch == 'p') {
5833 struct got_commit_object *commit, *pcommit;
5834 struct got_object_qid *pid;
5835 struct got_object_id *blob_id = NULL;
5836 int obj_type;
5837 err = got_object_open_as_commit(&commit,
5838 s->repo, id);
5839 if (err)
5840 break;
5841 pid = STAILQ_FIRST(
5842 got_object_commit_get_parent_ids(commit));
5843 if (pid == NULL) {
5844 got_object_commit_close(commit);
5845 break;
5847 /* Check if path history ends here. */
5848 err = got_object_open_as_commit(&pcommit,
5849 s->repo, &pid->id);
5850 if (err)
5851 break;
5852 err = got_object_id_by_path(&blob_id, s->repo,
5853 pcommit, s->path);
5854 got_object_commit_close(pcommit);
5855 if (err) {
5856 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5857 err = NULL;
5858 got_object_commit_close(commit);
5859 break;
5861 err = got_object_get_type(&obj_type, s->repo,
5862 blob_id);
5863 free(blob_id);
5864 /* Can't blame non-blob type objects. */
5865 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5866 got_object_commit_close(commit);
5867 break;
5869 err = got_object_qid_alloc(&s->blamed_commit,
5870 &pid->id);
5871 got_object_commit_close(commit);
5872 } else {
5873 if (got_object_id_cmp(id,
5874 &s->blamed_commit->id) == 0)
5875 break;
5876 err = got_object_qid_alloc(&s->blamed_commit,
5877 id);
5879 if (err)
5880 break;
5881 s->done = 1;
5882 thread_err = stop_blame(&s->blame);
5883 s->done = 0;
5884 if (thread_err)
5885 break;
5886 STAILQ_INSERT_HEAD(&s->blamed_commits,
5887 s->blamed_commit, entry);
5888 err = run_blame(view);
5889 if (err)
5890 break;
5891 break;
5893 case 'C': {
5894 struct got_object_qid *first;
5896 view->count = 0;
5897 first = STAILQ_FIRST(&s->blamed_commits);
5898 if (!got_object_id_cmp(&first->id, s->commit_id))
5899 break;
5900 s->done = 1;
5901 thread_err = stop_blame(&s->blame);
5902 s->done = 0;
5903 if (thread_err)
5904 break;
5905 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5906 got_object_qid_free(s->blamed_commit);
5907 s->blamed_commit =
5908 STAILQ_FIRST(&s->blamed_commits);
5909 err = run_blame(view);
5910 if (err)
5911 break;
5912 break;
5914 case KEY_ENTER:
5915 case '\r': {
5916 struct got_object_id *id = NULL;
5917 struct got_object_qid *pid;
5918 struct got_commit_object *commit = NULL;
5920 view->count = 0;
5921 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5922 s->first_displayed_line, s->selected_line);
5923 if (id == NULL)
5924 break;
5925 err = got_object_open_as_commit(&commit, s->repo, id);
5926 if (err)
5927 break;
5928 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5929 if (*new_view) {
5930 /* traversed from diff view, release diff resources */
5931 err = close_diff_view(*new_view);
5932 if (err)
5933 break;
5934 diff_view = *new_view;
5935 } else {
5936 if (view_is_parent_view(view))
5937 view_get_split(view, &begin_y, &begin_x);
5939 diff_view = view_open(0, 0, begin_y, begin_x,
5940 TOG_VIEW_DIFF);
5941 if (diff_view == NULL) {
5942 got_object_commit_close(commit);
5943 err = got_error_from_errno("view_open");
5944 break;
5947 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5948 id, NULL, NULL, 3, 0, 0, view, s->repo);
5949 got_object_commit_close(commit);
5950 if (err) {
5951 view_close(diff_view);
5952 break;
5954 s->last_diffed_line = s->first_displayed_line - 1 +
5955 s->selected_line;
5956 if (*new_view)
5957 break; /* still open from active diff view */
5958 if (view_is_parent_view(view) &&
5959 view->mode == TOG_VIEW_SPLIT_HRZN) {
5960 err = view_init_hsplit(view, begin_y);
5961 if (err)
5962 break;
5965 view->focussed = 0;
5966 diff_view->focussed = 1;
5967 diff_view->mode = view->mode;
5968 diff_view->nlines = view->lines - begin_y;
5969 if (view_is_parent_view(view)) {
5970 view_transfer_size(diff_view, view);
5971 err = view_close_child(view);
5972 if (err)
5973 break;
5974 err = view_set_child(view, diff_view);
5975 if (err)
5976 break;
5977 view->focus_child = 1;
5978 } else
5979 *new_view = diff_view;
5980 if (err)
5981 break;
5982 break;
5984 case CTRL('d'):
5985 case 'd':
5986 nscroll /= 2;
5987 /* FALL THROUGH */
5988 case KEY_NPAGE:
5989 case CTRL('f'):
5990 case 'f':
5991 case ' ':
5992 if (s->last_displayed_line >= s->blame.nlines &&
5993 s->selected_line >= MIN(s->blame.nlines,
5994 view->nlines - 2)) {
5995 view->count = 0;
5996 break;
5998 if (s->last_displayed_line >= s->blame.nlines &&
5999 s->selected_line < view->nlines - 2) {
6000 s->selected_line +=
6001 MIN(nscroll, s->last_displayed_line -
6002 s->first_displayed_line - s->selected_line + 1);
6004 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6005 s->first_displayed_line += nscroll;
6006 else
6007 s->first_displayed_line =
6008 s->blame.nlines - (view->nlines - 3);
6009 break;
6010 case KEY_RESIZE:
6011 if (s->selected_line > view->nlines - 2) {
6012 s->selected_line = MIN(s->blame.nlines,
6013 view->nlines - 2);
6015 break;
6016 default:
6017 view->count = 0;
6018 break;
6020 return thread_err ? thread_err : err;
6023 static const struct got_error *
6024 reset_blame_view(struct tog_view *view)
6026 const struct got_error *err;
6027 struct tog_blame_view_state *s = &view->state.blame;
6029 view->count = 0;
6030 s->done = 1;
6031 err = stop_blame(&s->blame);
6032 s->done = 0;
6033 if (err)
6034 return err;
6035 return run_blame(view);
6038 static const struct got_error *
6039 cmd_blame(int argc, char *argv[])
6041 const struct got_error *error;
6042 struct got_repository *repo = NULL;
6043 struct got_worktree *worktree = NULL;
6044 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6045 char *link_target = NULL;
6046 struct got_object_id *commit_id = NULL;
6047 struct got_commit_object *commit = NULL;
6048 char *commit_id_str = NULL;
6049 int ch;
6050 struct tog_view *view;
6051 int *pack_fds = NULL;
6053 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6054 switch (ch) {
6055 case 'c':
6056 commit_id_str = optarg;
6057 break;
6058 case 'r':
6059 repo_path = realpath(optarg, NULL);
6060 if (repo_path == NULL)
6061 return got_error_from_errno2("realpath",
6062 optarg);
6063 break;
6064 default:
6065 usage_blame();
6066 /* NOTREACHED */
6070 argc -= optind;
6071 argv += optind;
6073 if (argc != 1)
6074 usage_blame();
6076 error = got_repo_pack_fds_open(&pack_fds);
6077 if (error != NULL)
6078 goto done;
6080 if (repo_path == NULL) {
6081 cwd = getcwd(NULL, 0);
6082 if (cwd == NULL)
6083 return got_error_from_errno("getcwd");
6084 error = got_worktree_open(&worktree, cwd);
6085 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6086 goto done;
6087 if (worktree)
6088 repo_path =
6089 strdup(got_worktree_get_repo_path(worktree));
6090 else
6091 repo_path = strdup(cwd);
6092 if (repo_path == NULL) {
6093 error = got_error_from_errno("strdup");
6094 goto done;
6098 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6099 if (error != NULL)
6100 goto done;
6102 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6103 worktree);
6104 if (error)
6105 goto done;
6107 init_curses();
6109 error = apply_unveil(got_repo_get_path(repo), NULL);
6110 if (error)
6111 goto done;
6113 error = tog_load_refs(repo, 0);
6114 if (error)
6115 goto done;
6117 if (commit_id_str == NULL) {
6118 struct got_reference *head_ref;
6119 error = got_ref_open(&head_ref, repo, worktree ?
6120 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6121 if (error != NULL)
6122 goto done;
6123 error = got_ref_resolve(&commit_id, repo, head_ref);
6124 got_ref_close(head_ref);
6125 } else {
6126 error = got_repo_match_object_id(&commit_id, NULL,
6127 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6129 if (error != NULL)
6130 goto done;
6132 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6133 if (view == NULL) {
6134 error = got_error_from_errno("view_open");
6135 goto done;
6138 error = got_object_open_as_commit(&commit, repo, commit_id);
6139 if (error)
6140 goto done;
6142 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6143 commit, repo);
6144 if (error)
6145 goto done;
6147 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6148 commit_id, repo);
6149 if (error)
6150 goto done;
6151 if (worktree) {
6152 /* Release work tree lock. */
6153 got_worktree_close(worktree);
6154 worktree = NULL;
6156 error = view_loop(view);
6157 done:
6158 free(repo_path);
6159 free(in_repo_path);
6160 free(link_target);
6161 free(cwd);
6162 free(commit_id);
6163 if (commit)
6164 got_object_commit_close(commit);
6165 if (worktree)
6166 got_worktree_close(worktree);
6167 if (repo) {
6168 const struct got_error *close_err = got_repo_close(repo);
6169 if (error == NULL)
6170 error = close_err;
6172 if (pack_fds) {
6173 const struct got_error *pack_err =
6174 got_repo_pack_fds_close(pack_fds);
6175 if (error == NULL)
6176 error = pack_err;
6178 tog_free_refs();
6179 return error;
6182 static const struct got_error *
6183 draw_tree_entries(struct tog_view *view, const char *parent_path)
6185 struct tog_tree_view_state *s = &view->state.tree;
6186 const struct got_error *err = NULL;
6187 struct got_tree_entry *te;
6188 wchar_t *wline;
6189 struct tog_color *tc;
6190 int width, n, i, nentries;
6191 int limit = view->nlines;
6193 s->ndisplayed = 0;
6194 if (view_is_hsplit_top(view))
6195 --limit; /* border */
6197 werase(view->window);
6199 if (limit == 0)
6200 return NULL;
6202 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6203 0, 0);
6204 if (err)
6205 return err;
6206 if (view_needs_focus_indication(view))
6207 wstandout(view->window);
6208 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6209 if (tc)
6210 wattr_on(view->window,
6211 COLOR_PAIR(tc->colorpair), NULL);
6212 waddwstr(view->window, wline);
6213 if (tc)
6214 wattr_off(view->window,
6215 COLOR_PAIR(tc->colorpair), NULL);
6216 if (view_needs_focus_indication(view))
6217 wstandend(view->window);
6218 free(wline);
6219 wline = NULL;
6220 if (width < view->ncols - 1)
6221 waddch(view->window, '\n');
6222 if (--limit <= 0)
6223 return NULL;
6224 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6225 0, 0);
6226 if (err)
6227 return err;
6228 waddwstr(view->window, wline);
6229 free(wline);
6230 wline = NULL;
6231 if (width < view->ncols - 1)
6232 waddch(view->window, '\n');
6233 if (--limit <= 0)
6234 return NULL;
6235 waddch(view->window, '\n');
6236 if (--limit <= 0)
6237 return NULL;
6239 if (s->first_displayed_entry == NULL) {
6240 te = got_object_tree_get_first_entry(s->tree);
6241 if (s->selected == 0) {
6242 if (view->focussed)
6243 wstandout(view->window);
6244 s->selected_entry = NULL;
6246 waddstr(view->window, " ..\n"); /* parent directory */
6247 if (s->selected == 0 && view->focussed)
6248 wstandend(view->window);
6249 s->ndisplayed++;
6250 if (--limit <= 0)
6251 return NULL;
6252 n = 1;
6253 } else {
6254 n = 0;
6255 te = s->first_displayed_entry;
6258 nentries = got_object_tree_get_nentries(s->tree);
6259 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6260 char *line = NULL, *id_str = NULL, *link_target = NULL;
6261 const char *modestr = "";
6262 mode_t mode;
6264 te = got_object_tree_get_entry(s->tree, i);
6265 mode = got_tree_entry_get_mode(te);
6267 if (s->show_ids) {
6268 err = got_object_id_str(&id_str,
6269 got_tree_entry_get_id(te));
6270 if (err)
6271 return got_error_from_errno(
6272 "got_object_id_str");
6274 if (got_object_tree_entry_is_submodule(te))
6275 modestr = "$";
6276 else if (S_ISLNK(mode)) {
6277 int i;
6279 err = got_tree_entry_get_symlink_target(&link_target,
6280 te, s->repo);
6281 if (err) {
6282 free(id_str);
6283 return err;
6285 for (i = 0; i < strlen(link_target); i++) {
6286 if (!isprint((unsigned char)link_target[i]))
6287 link_target[i] = '?';
6289 modestr = "@";
6291 else if (S_ISDIR(mode))
6292 modestr = "/";
6293 else if (mode & S_IXUSR)
6294 modestr = "*";
6295 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6296 got_tree_entry_get_name(te), modestr,
6297 link_target ? " -> ": "",
6298 link_target ? link_target : "") == -1) {
6299 free(id_str);
6300 free(link_target);
6301 return got_error_from_errno("asprintf");
6303 free(id_str);
6304 free(link_target);
6305 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6306 0, 0);
6307 if (err) {
6308 free(line);
6309 break;
6311 if (n == s->selected) {
6312 if (view->focussed)
6313 wstandout(view->window);
6314 s->selected_entry = te;
6316 tc = match_color(&s->colors, line);
6317 if (tc)
6318 wattr_on(view->window,
6319 COLOR_PAIR(tc->colorpair), NULL);
6320 waddwstr(view->window, wline);
6321 if (tc)
6322 wattr_off(view->window,
6323 COLOR_PAIR(tc->colorpair), NULL);
6324 if (width < view->ncols - 1)
6325 waddch(view->window, '\n');
6326 if (n == s->selected && view->focussed)
6327 wstandend(view->window);
6328 free(line);
6329 free(wline);
6330 wline = NULL;
6331 n++;
6332 s->ndisplayed++;
6333 s->last_displayed_entry = te;
6334 if (--limit <= 0)
6335 break;
6338 return err;
6341 static void
6342 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6344 struct got_tree_entry *te;
6345 int isroot = s->tree == s->root;
6346 int i = 0;
6348 if (s->first_displayed_entry == NULL)
6349 return;
6351 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6352 while (i++ < maxscroll) {
6353 if (te == NULL) {
6354 if (!isroot)
6355 s->first_displayed_entry = NULL;
6356 break;
6358 s->first_displayed_entry = te;
6359 te = got_tree_entry_get_prev(s->tree, te);
6363 static const struct got_error *
6364 tree_scroll_down(struct tog_view *view, int maxscroll)
6366 struct tog_tree_view_state *s = &view->state.tree;
6367 struct got_tree_entry *next, *last;
6368 int n = 0;
6370 if (s->first_displayed_entry)
6371 next = got_tree_entry_get_next(s->tree,
6372 s->first_displayed_entry);
6373 else
6374 next = got_object_tree_get_first_entry(s->tree);
6376 last = s->last_displayed_entry;
6377 while (next && n++ < maxscroll) {
6378 if (last)
6379 last = got_tree_entry_get_next(s->tree, last);
6380 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6381 s->first_displayed_entry = next;
6382 next = got_tree_entry_get_next(s->tree, next);
6386 return NULL;
6389 static const struct got_error *
6390 tree_entry_path(char **path, struct tog_parent_trees *parents,
6391 struct got_tree_entry *te)
6393 const struct got_error *err = NULL;
6394 struct tog_parent_tree *pt;
6395 size_t len = 2; /* for leading slash and NUL */
6397 TAILQ_FOREACH(pt, parents, entry)
6398 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6399 + 1 /* slash */;
6400 if (te)
6401 len += strlen(got_tree_entry_get_name(te));
6403 *path = calloc(1, len);
6404 if (path == NULL)
6405 return got_error_from_errno("calloc");
6407 (*path)[0] = '/';
6408 pt = TAILQ_LAST(parents, tog_parent_trees);
6409 while (pt) {
6410 const char *name = got_tree_entry_get_name(pt->selected_entry);
6411 if (strlcat(*path, name, len) >= len) {
6412 err = got_error(GOT_ERR_NO_SPACE);
6413 goto done;
6415 if (strlcat(*path, "/", len) >= len) {
6416 err = got_error(GOT_ERR_NO_SPACE);
6417 goto done;
6419 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6421 if (te) {
6422 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6423 err = got_error(GOT_ERR_NO_SPACE);
6424 goto done;
6427 done:
6428 if (err) {
6429 free(*path);
6430 *path = NULL;
6432 return err;
6435 static const struct got_error *
6436 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6437 struct got_tree_entry *te, struct tog_parent_trees *parents,
6438 struct got_object_id *commit_id, struct got_repository *repo)
6440 const struct got_error *err = NULL;
6441 char *path;
6442 struct tog_view *blame_view;
6444 *new_view = NULL;
6446 err = tree_entry_path(&path, parents, te);
6447 if (err)
6448 return err;
6450 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6451 if (blame_view == NULL) {
6452 err = got_error_from_errno("view_open");
6453 goto done;
6456 err = open_blame_view(blame_view, path, commit_id, repo);
6457 if (err) {
6458 if (err->code == GOT_ERR_CANCELLED)
6459 err = NULL;
6460 view_close(blame_view);
6461 } else
6462 *new_view = blame_view;
6463 done:
6464 free(path);
6465 return err;
6468 static const struct got_error *
6469 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6470 struct tog_tree_view_state *s)
6472 struct tog_view *log_view;
6473 const struct got_error *err = NULL;
6474 char *path;
6476 *new_view = NULL;
6478 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6479 if (log_view == NULL)
6480 return got_error_from_errno("view_open");
6482 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6483 if (err)
6484 return err;
6486 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6487 path, 0);
6488 if (err)
6489 view_close(log_view);
6490 else
6491 *new_view = log_view;
6492 free(path);
6493 return err;
6496 static const struct got_error *
6497 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6498 const char *head_ref_name, struct got_repository *repo)
6500 const struct got_error *err = NULL;
6501 char *commit_id_str = NULL;
6502 struct tog_tree_view_state *s = &view->state.tree;
6503 struct got_commit_object *commit = NULL;
6505 TAILQ_INIT(&s->parents);
6506 STAILQ_INIT(&s->colors);
6508 s->commit_id = got_object_id_dup(commit_id);
6509 if (s->commit_id == NULL)
6510 return got_error_from_errno("got_object_id_dup");
6512 err = got_object_open_as_commit(&commit, repo, commit_id);
6513 if (err)
6514 goto done;
6517 * The root is opened here and will be closed when the view is closed.
6518 * Any visited subtrees and their path-wise parents are opened and
6519 * closed on demand.
6521 err = got_object_open_as_tree(&s->root, repo,
6522 got_object_commit_get_tree_id(commit));
6523 if (err)
6524 goto done;
6525 s->tree = s->root;
6527 err = got_object_id_str(&commit_id_str, commit_id);
6528 if (err != NULL)
6529 goto done;
6531 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6532 err = got_error_from_errno("asprintf");
6533 goto done;
6536 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6537 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6538 if (head_ref_name) {
6539 s->head_ref_name = strdup(head_ref_name);
6540 if (s->head_ref_name == NULL) {
6541 err = got_error_from_errno("strdup");
6542 goto done;
6545 s->repo = repo;
6547 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6548 err = add_color(&s->colors, "\\$$",
6549 TOG_COLOR_TREE_SUBMODULE,
6550 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6551 if (err)
6552 goto done;
6553 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6554 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6555 if (err)
6556 goto done;
6557 err = add_color(&s->colors, "/$",
6558 TOG_COLOR_TREE_DIRECTORY,
6559 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6560 if (err)
6561 goto done;
6563 err = add_color(&s->colors, "\\*$",
6564 TOG_COLOR_TREE_EXECUTABLE,
6565 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6566 if (err)
6567 goto done;
6569 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6570 get_color_value("TOG_COLOR_COMMIT"));
6571 if (err)
6572 goto done;
6575 view->show = show_tree_view;
6576 view->input = input_tree_view;
6577 view->close = close_tree_view;
6578 view->search_start = search_start_tree_view;
6579 view->search_next = search_next_tree_view;
6580 done:
6581 free(commit_id_str);
6582 if (commit)
6583 got_object_commit_close(commit);
6584 if (err)
6585 close_tree_view(view);
6586 return err;
6589 static const struct got_error *
6590 close_tree_view(struct tog_view *view)
6592 struct tog_tree_view_state *s = &view->state.tree;
6594 free_colors(&s->colors);
6595 free(s->tree_label);
6596 s->tree_label = NULL;
6597 free(s->commit_id);
6598 s->commit_id = NULL;
6599 free(s->head_ref_name);
6600 s->head_ref_name = NULL;
6601 while (!TAILQ_EMPTY(&s->parents)) {
6602 struct tog_parent_tree *parent;
6603 parent = TAILQ_FIRST(&s->parents);
6604 TAILQ_REMOVE(&s->parents, parent, entry);
6605 if (parent->tree != s->root)
6606 got_object_tree_close(parent->tree);
6607 free(parent);
6610 if (s->tree != NULL && s->tree != s->root)
6611 got_object_tree_close(s->tree);
6612 if (s->root)
6613 got_object_tree_close(s->root);
6614 return NULL;
6617 static const struct got_error *
6618 search_start_tree_view(struct tog_view *view)
6620 struct tog_tree_view_state *s = &view->state.tree;
6622 s->matched_entry = NULL;
6623 return NULL;
6626 static int
6627 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6629 regmatch_t regmatch;
6631 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6632 0) == 0;
6635 static const struct got_error *
6636 search_next_tree_view(struct tog_view *view)
6638 struct tog_tree_view_state *s = &view->state.tree;
6639 struct got_tree_entry *te = NULL;
6641 if (!view->searching) {
6642 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6643 return NULL;
6646 if (s->matched_entry) {
6647 if (view->searching == TOG_SEARCH_FORWARD) {
6648 if (s->selected_entry)
6649 te = got_tree_entry_get_next(s->tree,
6650 s->selected_entry);
6651 else
6652 te = got_object_tree_get_first_entry(s->tree);
6653 } else {
6654 if (s->selected_entry == NULL)
6655 te = got_object_tree_get_last_entry(s->tree);
6656 else
6657 te = got_tree_entry_get_prev(s->tree,
6658 s->selected_entry);
6660 } else {
6661 if (s->selected_entry)
6662 te = s->selected_entry;
6663 else if (view->searching == TOG_SEARCH_FORWARD)
6664 te = got_object_tree_get_first_entry(s->tree);
6665 else
6666 te = got_object_tree_get_last_entry(s->tree);
6669 while (1) {
6670 if (te == NULL) {
6671 if (s->matched_entry == NULL) {
6672 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6673 return NULL;
6675 if (view->searching == TOG_SEARCH_FORWARD)
6676 te = got_object_tree_get_first_entry(s->tree);
6677 else
6678 te = got_object_tree_get_last_entry(s->tree);
6681 if (match_tree_entry(te, &view->regex)) {
6682 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6683 s->matched_entry = te;
6684 break;
6687 if (view->searching == TOG_SEARCH_FORWARD)
6688 te = got_tree_entry_get_next(s->tree, te);
6689 else
6690 te = got_tree_entry_get_prev(s->tree, te);
6693 if (s->matched_entry) {
6694 s->first_displayed_entry = s->matched_entry;
6695 s->selected = 0;
6698 return NULL;
6701 static const struct got_error *
6702 show_tree_view(struct tog_view *view)
6704 const struct got_error *err = NULL;
6705 struct tog_tree_view_state *s = &view->state.tree;
6706 char *parent_path;
6708 err = tree_entry_path(&parent_path, &s->parents, NULL);
6709 if (err)
6710 return err;
6712 err = draw_tree_entries(view, parent_path);
6713 free(parent_path);
6715 view_border(view);
6716 return err;
6719 static const struct got_error *
6720 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6722 const struct got_error *err = NULL;
6723 struct tog_tree_view_state *s = &view->state.tree;
6724 struct tog_view *log_view, *ref_view;
6725 struct got_tree_entry *te;
6726 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6728 switch (ch) {
6729 case 'i':
6730 s->show_ids = !s->show_ids;
6731 view->count = 0;
6732 break;
6733 case 'l':
6734 view->count = 0;
6735 if (!s->selected_entry)
6736 break;
6737 if (view_is_parent_view(view))
6738 view_get_split(view, &begin_y, &begin_x);
6739 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6740 if (view_is_parent_view(view) &&
6741 view->mode == TOG_VIEW_SPLIT_HRZN) {
6742 err = view_init_hsplit(view, begin_y);
6743 if (err)
6744 break;
6746 view->focussed = 0;
6747 log_view->focussed = 1;
6748 log_view->mode = view->mode;
6749 log_view->nlines = view->lines - begin_y;
6750 if (view_is_parent_view(view)) {
6751 view_transfer_size(log_view, view);
6752 err = view_close_child(view);
6753 if (err)
6754 return err;
6755 err = view_set_child(view, log_view);
6756 if (err)
6757 return err;
6758 view->focus_child = 1;
6759 } else
6760 *new_view = log_view;
6761 break;
6762 case 'r':
6763 view->count = 0;
6764 if (view_is_parent_view(view))
6765 view_get_split(view, &begin_y, &begin_x);
6766 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6767 if (ref_view == NULL)
6768 return got_error_from_errno("view_open");
6769 err = open_ref_view(ref_view, s->repo);
6770 if (err) {
6771 view_close(ref_view);
6772 return err;
6774 if (view_is_parent_view(view) &&
6775 view->mode == TOG_VIEW_SPLIT_HRZN) {
6776 err = view_init_hsplit(view, begin_y);
6777 if (err)
6778 break;
6780 view->focussed = 0;
6781 ref_view->focussed = 1;
6782 ref_view->mode = view->mode;
6783 ref_view->nlines = view->lines - begin_y;
6784 if (view_is_parent_view(view)) {
6785 view_transfer_size(ref_view, view);
6786 err = view_close_child(view);
6787 if (err)
6788 return err;
6789 err = view_set_child(view, ref_view);
6790 if (err)
6791 return err;
6792 view->focus_child = 1;
6793 } else
6794 *new_view = ref_view;
6795 break;
6796 case 'g':
6797 case KEY_HOME:
6798 s->selected = 0;
6799 view->count = 0;
6800 if (s->tree == s->root)
6801 s->first_displayed_entry =
6802 got_object_tree_get_first_entry(s->tree);
6803 else
6804 s->first_displayed_entry = NULL;
6805 break;
6806 case 'G':
6807 case KEY_END: {
6808 int eos = view->nlines - 3;
6810 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6811 --eos; /* border */
6812 s->selected = 0;
6813 view->count = 0;
6814 te = got_object_tree_get_last_entry(s->tree);
6815 for (n = 0; n < eos; n++) {
6816 if (te == NULL) {
6817 if (s->tree != s->root) {
6818 s->first_displayed_entry = NULL;
6819 n++;
6821 break;
6823 s->first_displayed_entry = te;
6824 te = got_tree_entry_get_prev(s->tree, te);
6826 if (n > 0)
6827 s->selected = n - 1;
6828 break;
6830 case 'k':
6831 case KEY_UP:
6832 case CTRL('p'):
6833 if (s->selected > 0) {
6834 s->selected--;
6835 break;
6837 tree_scroll_up(s, 1);
6838 if (s->selected_entry == NULL ||
6839 (s->tree == s->root && s->selected_entry ==
6840 got_object_tree_get_first_entry(s->tree)))
6841 view->count = 0;
6842 break;
6843 case CTRL('u'):
6844 case 'u':
6845 nscroll /= 2;
6846 /* FALL THROUGH */
6847 case KEY_PPAGE:
6848 case CTRL('b'):
6849 case 'b':
6850 if (s->tree == s->root) {
6851 if (got_object_tree_get_first_entry(s->tree) ==
6852 s->first_displayed_entry)
6853 s->selected -= MIN(s->selected, nscroll);
6854 } else {
6855 if (s->first_displayed_entry == NULL)
6856 s->selected -= MIN(s->selected, nscroll);
6858 tree_scroll_up(s, MAX(0, nscroll));
6859 if (s->selected_entry == NULL ||
6860 (s->tree == s->root && s->selected_entry ==
6861 got_object_tree_get_first_entry(s->tree)))
6862 view->count = 0;
6863 break;
6864 case 'j':
6865 case KEY_DOWN:
6866 case CTRL('n'):
6867 if (s->selected < s->ndisplayed - 1) {
6868 s->selected++;
6869 break;
6871 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6872 == NULL) {
6873 /* can't scroll any further */
6874 view->count = 0;
6875 break;
6877 tree_scroll_down(view, 1);
6878 break;
6879 case CTRL('d'):
6880 case 'd':
6881 nscroll /= 2;
6882 /* FALL THROUGH */
6883 case KEY_NPAGE:
6884 case CTRL('f'):
6885 case 'f':
6886 case ' ':
6887 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6888 == NULL) {
6889 /* can't scroll any further; move cursor down */
6890 if (s->selected < s->ndisplayed - 1)
6891 s->selected += MIN(nscroll,
6892 s->ndisplayed - s->selected - 1);
6893 else
6894 view->count = 0;
6895 break;
6897 tree_scroll_down(view, nscroll);
6898 break;
6899 case KEY_ENTER:
6900 case '\r':
6901 case KEY_BACKSPACE:
6902 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6903 struct tog_parent_tree *parent;
6904 /* user selected '..' */
6905 if (s->tree == s->root) {
6906 view->count = 0;
6907 break;
6909 parent = TAILQ_FIRST(&s->parents);
6910 TAILQ_REMOVE(&s->parents, parent,
6911 entry);
6912 got_object_tree_close(s->tree);
6913 s->tree = parent->tree;
6914 s->first_displayed_entry =
6915 parent->first_displayed_entry;
6916 s->selected_entry =
6917 parent->selected_entry;
6918 s->selected = parent->selected;
6919 if (s->selected > view->nlines - 3) {
6920 err = offset_selection_down(view);
6921 if (err)
6922 break;
6924 free(parent);
6925 } else if (S_ISDIR(got_tree_entry_get_mode(
6926 s->selected_entry))) {
6927 struct got_tree_object *subtree;
6928 view->count = 0;
6929 err = got_object_open_as_tree(&subtree, s->repo,
6930 got_tree_entry_get_id(s->selected_entry));
6931 if (err)
6932 break;
6933 err = tree_view_visit_subtree(s, subtree);
6934 if (err) {
6935 got_object_tree_close(subtree);
6936 break;
6938 } else if (S_ISREG(got_tree_entry_get_mode(
6939 s->selected_entry))) {
6940 struct tog_view *blame_view;
6941 int begin_x = 0, begin_y = 0;
6943 if (view_is_parent_view(view))
6944 view_get_split(view, &begin_y, &begin_x);
6946 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6947 s->selected_entry, &s->parents,
6948 s->commit_id, s->repo);
6949 if (err)
6950 break;
6952 if (view_is_parent_view(view) &&
6953 view->mode == TOG_VIEW_SPLIT_HRZN) {
6954 err = view_init_hsplit(view, begin_y);
6955 if (err)
6956 break;
6959 view->count = 0;
6960 view->focussed = 0;
6961 blame_view->focussed = 1;
6962 blame_view->mode = view->mode;
6963 blame_view->nlines = view->lines - begin_y;
6964 if (view_is_parent_view(view)) {
6965 view_transfer_size(blame_view, view);
6966 err = view_close_child(view);
6967 if (err)
6968 return err;
6969 err = view_set_child(view, blame_view);
6970 if (err)
6971 return err;
6972 view->focus_child = 1;
6973 } else
6974 *new_view = blame_view;
6976 break;
6977 case KEY_RESIZE:
6978 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6979 s->selected = view->nlines - 4;
6980 view->count = 0;
6981 break;
6982 default:
6983 view->count = 0;
6984 break;
6987 return err;
6990 __dead static void
6991 usage_tree(void)
6993 endwin();
6994 fprintf(stderr,
6995 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6996 getprogname());
6997 exit(1);
7000 static const struct got_error *
7001 cmd_tree(int argc, char *argv[])
7003 const struct got_error *error;
7004 struct got_repository *repo = NULL;
7005 struct got_worktree *worktree = NULL;
7006 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7007 struct got_object_id *commit_id = NULL;
7008 struct got_commit_object *commit = NULL;
7009 const char *commit_id_arg = NULL;
7010 char *label = NULL;
7011 struct got_reference *ref = NULL;
7012 const char *head_ref_name = NULL;
7013 int ch;
7014 struct tog_view *view;
7015 int *pack_fds = NULL;
7017 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7018 switch (ch) {
7019 case 'c':
7020 commit_id_arg = optarg;
7021 break;
7022 case 'r':
7023 repo_path = realpath(optarg, NULL);
7024 if (repo_path == NULL)
7025 return got_error_from_errno2("realpath",
7026 optarg);
7027 break;
7028 default:
7029 usage_tree();
7030 /* NOTREACHED */
7034 argc -= optind;
7035 argv += optind;
7037 if (argc > 1)
7038 usage_tree();
7040 error = got_repo_pack_fds_open(&pack_fds);
7041 if (error != NULL)
7042 goto done;
7044 if (repo_path == NULL) {
7045 cwd = getcwd(NULL, 0);
7046 if (cwd == NULL)
7047 return got_error_from_errno("getcwd");
7048 error = got_worktree_open(&worktree, cwd);
7049 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7050 goto done;
7051 if (worktree)
7052 repo_path =
7053 strdup(got_worktree_get_repo_path(worktree));
7054 else
7055 repo_path = strdup(cwd);
7056 if (repo_path == NULL) {
7057 error = got_error_from_errno("strdup");
7058 goto done;
7062 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7063 if (error != NULL)
7064 goto done;
7066 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7067 repo, worktree);
7068 if (error)
7069 goto done;
7071 init_curses();
7073 error = apply_unveil(got_repo_get_path(repo), NULL);
7074 if (error)
7075 goto done;
7077 error = tog_load_refs(repo, 0);
7078 if (error)
7079 goto done;
7081 if (commit_id_arg == NULL) {
7082 error = got_repo_match_object_id(&commit_id, &label,
7083 worktree ? got_worktree_get_head_ref_name(worktree) :
7084 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7085 if (error)
7086 goto done;
7087 head_ref_name = label;
7088 } else {
7089 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7090 if (error == NULL)
7091 head_ref_name = got_ref_get_name(ref);
7092 else if (error->code != GOT_ERR_NOT_REF)
7093 goto done;
7094 error = got_repo_match_object_id(&commit_id, NULL,
7095 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7096 if (error)
7097 goto done;
7100 error = got_object_open_as_commit(&commit, repo, commit_id);
7101 if (error)
7102 goto done;
7104 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7105 if (view == NULL) {
7106 error = got_error_from_errno("view_open");
7107 goto done;
7109 error = open_tree_view(view, commit_id, head_ref_name, repo);
7110 if (error)
7111 goto done;
7112 if (!got_path_is_root_dir(in_repo_path)) {
7113 error = tree_view_walk_path(&view->state.tree, commit,
7114 in_repo_path);
7115 if (error)
7116 goto done;
7119 if (worktree) {
7120 /* Release work tree lock. */
7121 got_worktree_close(worktree);
7122 worktree = NULL;
7124 error = view_loop(view);
7125 done:
7126 free(repo_path);
7127 free(cwd);
7128 free(commit_id);
7129 free(label);
7130 if (ref)
7131 got_ref_close(ref);
7132 if (repo) {
7133 const struct got_error *close_err = got_repo_close(repo);
7134 if (error == NULL)
7135 error = close_err;
7137 if (pack_fds) {
7138 const struct got_error *pack_err =
7139 got_repo_pack_fds_close(pack_fds);
7140 if (error == NULL)
7141 error = pack_err;
7143 tog_free_refs();
7144 return error;
7147 static const struct got_error *
7148 ref_view_load_refs(struct tog_ref_view_state *s)
7150 struct got_reflist_entry *sre;
7151 struct tog_reflist_entry *re;
7153 s->nrefs = 0;
7154 TAILQ_FOREACH(sre, &tog_refs, entry) {
7155 if (strncmp(got_ref_get_name(sre->ref),
7156 "refs/got/", 9) == 0 &&
7157 strncmp(got_ref_get_name(sre->ref),
7158 "refs/got/backup/", 16) != 0)
7159 continue;
7161 re = malloc(sizeof(*re));
7162 if (re == NULL)
7163 return got_error_from_errno("malloc");
7165 re->ref = got_ref_dup(sre->ref);
7166 if (re->ref == NULL)
7167 return got_error_from_errno("got_ref_dup");
7168 re->idx = s->nrefs++;
7169 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7172 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7173 return NULL;
7176 static void
7177 ref_view_free_refs(struct tog_ref_view_state *s)
7179 struct tog_reflist_entry *re;
7181 while (!TAILQ_EMPTY(&s->refs)) {
7182 re = TAILQ_FIRST(&s->refs);
7183 TAILQ_REMOVE(&s->refs, re, entry);
7184 got_ref_close(re->ref);
7185 free(re);
7189 static const struct got_error *
7190 open_ref_view(struct tog_view *view, struct got_repository *repo)
7192 const struct got_error *err = NULL;
7193 struct tog_ref_view_state *s = &view->state.ref;
7195 s->selected_entry = 0;
7196 s->repo = repo;
7198 TAILQ_INIT(&s->refs);
7199 STAILQ_INIT(&s->colors);
7201 err = ref_view_load_refs(s);
7202 if (err)
7203 return err;
7205 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7206 err = add_color(&s->colors, "^refs/heads/",
7207 TOG_COLOR_REFS_HEADS,
7208 get_color_value("TOG_COLOR_REFS_HEADS"));
7209 if (err)
7210 goto done;
7212 err = add_color(&s->colors, "^refs/tags/",
7213 TOG_COLOR_REFS_TAGS,
7214 get_color_value("TOG_COLOR_REFS_TAGS"));
7215 if (err)
7216 goto done;
7218 err = add_color(&s->colors, "^refs/remotes/",
7219 TOG_COLOR_REFS_REMOTES,
7220 get_color_value("TOG_COLOR_REFS_REMOTES"));
7221 if (err)
7222 goto done;
7224 err = add_color(&s->colors, "^refs/got/backup/",
7225 TOG_COLOR_REFS_BACKUP,
7226 get_color_value("TOG_COLOR_REFS_BACKUP"));
7227 if (err)
7228 goto done;
7231 view->show = show_ref_view;
7232 view->input = input_ref_view;
7233 view->close = close_ref_view;
7234 view->search_start = search_start_ref_view;
7235 view->search_next = search_next_ref_view;
7236 done:
7237 if (err)
7238 free_colors(&s->colors);
7239 return err;
7242 static const struct got_error *
7243 close_ref_view(struct tog_view *view)
7245 struct tog_ref_view_state *s = &view->state.ref;
7247 ref_view_free_refs(s);
7248 free_colors(&s->colors);
7250 return NULL;
7253 static const struct got_error *
7254 resolve_reflist_entry(struct got_object_id **commit_id,
7255 struct tog_reflist_entry *re, struct got_repository *repo)
7257 const struct got_error *err = NULL;
7258 struct got_object_id *obj_id;
7259 struct got_tag_object *tag = NULL;
7260 int obj_type;
7262 *commit_id = NULL;
7264 err = got_ref_resolve(&obj_id, repo, re->ref);
7265 if (err)
7266 return err;
7268 err = got_object_get_type(&obj_type, repo, obj_id);
7269 if (err)
7270 goto done;
7272 switch (obj_type) {
7273 case GOT_OBJ_TYPE_COMMIT:
7274 *commit_id = obj_id;
7275 break;
7276 case GOT_OBJ_TYPE_TAG:
7277 err = got_object_open_as_tag(&tag, repo, obj_id);
7278 if (err)
7279 goto done;
7280 free(obj_id);
7281 err = got_object_get_type(&obj_type, repo,
7282 got_object_tag_get_object_id(tag));
7283 if (err)
7284 goto done;
7285 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7286 err = got_error(GOT_ERR_OBJ_TYPE);
7287 goto done;
7289 *commit_id = got_object_id_dup(
7290 got_object_tag_get_object_id(tag));
7291 if (*commit_id == NULL) {
7292 err = got_error_from_errno("got_object_id_dup");
7293 goto done;
7295 break;
7296 default:
7297 err = got_error(GOT_ERR_OBJ_TYPE);
7298 break;
7301 done:
7302 if (tag)
7303 got_object_tag_close(tag);
7304 if (err) {
7305 free(*commit_id);
7306 *commit_id = NULL;
7308 return err;
7311 static const struct got_error *
7312 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7313 struct tog_reflist_entry *re, struct got_repository *repo)
7315 struct tog_view *log_view;
7316 const struct got_error *err = NULL;
7317 struct got_object_id *commit_id = NULL;
7319 *new_view = NULL;
7321 err = resolve_reflist_entry(&commit_id, re, repo);
7322 if (err) {
7323 if (err->code != GOT_ERR_OBJ_TYPE)
7324 return err;
7325 else
7326 return NULL;
7329 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7330 if (log_view == NULL) {
7331 err = got_error_from_errno("view_open");
7332 goto done;
7335 err = open_log_view(log_view, commit_id, repo,
7336 got_ref_get_name(re->ref), "", 0);
7337 done:
7338 if (err)
7339 view_close(log_view);
7340 else
7341 *new_view = log_view;
7342 free(commit_id);
7343 return err;
7346 static void
7347 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7349 struct tog_reflist_entry *re;
7350 int i = 0;
7352 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7353 return;
7355 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7356 while (i++ < maxscroll) {
7357 if (re == NULL)
7358 break;
7359 s->first_displayed_entry = re;
7360 re = TAILQ_PREV(re, tog_reflist_head, entry);
7364 static const struct got_error *
7365 ref_scroll_down(struct tog_view *view, int maxscroll)
7367 struct tog_ref_view_state *s = &view->state.ref;
7368 struct tog_reflist_entry *next, *last;
7369 int n = 0;
7371 if (s->first_displayed_entry)
7372 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7373 else
7374 next = TAILQ_FIRST(&s->refs);
7376 last = s->last_displayed_entry;
7377 while (next && n++ < maxscroll) {
7378 if (last)
7379 last = TAILQ_NEXT(last, entry);
7380 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7381 s->first_displayed_entry = next;
7382 next = TAILQ_NEXT(next, entry);
7386 return NULL;
7389 static const struct got_error *
7390 search_start_ref_view(struct tog_view *view)
7392 struct tog_ref_view_state *s = &view->state.ref;
7394 s->matched_entry = NULL;
7395 return NULL;
7398 static int
7399 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7401 regmatch_t regmatch;
7403 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7404 0) == 0;
7407 static const struct got_error *
7408 search_next_ref_view(struct tog_view *view)
7410 struct tog_ref_view_state *s = &view->state.ref;
7411 struct tog_reflist_entry *re = NULL;
7413 if (!view->searching) {
7414 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7415 return NULL;
7418 if (s->matched_entry) {
7419 if (view->searching == TOG_SEARCH_FORWARD) {
7420 if (s->selected_entry)
7421 re = TAILQ_NEXT(s->selected_entry, entry);
7422 else
7423 re = TAILQ_PREV(s->selected_entry,
7424 tog_reflist_head, entry);
7425 } else {
7426 if (s->selected_entry == NULL)
7427 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7428 else
7429 re = TAILQ_PREV(s->selected_entry,
7430 tog_reflist_head, entry);
7432 } else {
7433 if (s->selected_entry)
7434 re = s->selected_entry;
7435 else if (view->searching == TOG_SEARCH_FORWARD)
7436 re = TAILQ_FIRST(&s->refs);
7437 else
7438 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7441 while (1) {
7442 if (re == NULL) {
7443 if (s->matched_entry == NULL) {
7444 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7445 return NULL;
7447 if (view->searching == TOG_SEARCH_FORWARD)
7448 re = TAILQ_FIRST(&s->refs);
7449 else
7450 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7453 if (match_reflist_entry(re, &view->regex)) {
7454 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7455 s->matched_entry = re;
7456 break;
7459 if (view->searching == TOG_SEARCH_FORWARD)
7460 re = TAILQ_NEXT(re, entry);
7461 else
7462 re = TAILQ_PREV(re, tog_reflist_head, entry);
7465 if (s->matched_entry) {
7466 s->first_displayed_entry = s->matched_entry;
7467 s->selected = 0;
7470 return NULL;
7473 static const struct got_error *
7474 show_ref_view(struct tog_view *view)
7476 const struct got_error *err = NULL;
7477 struct tog_ref_view_state *s = &view->state.ref;
7478 struct tog_reflist_entry *re;
7479 char *line = NULL;
7480 wchar_t *wline;
7481 struct tog_color *tc;
7482 int width, n;
7483 int limit = view->nlines;
7485 werase(view->window);
7487 s->ndisplayed = 0;
7488 if (view_is_hsplit_top(view))
7489 --limit; /* border */
7491 if (limit == 0)
7492 return NULL;
7494 re = s->first_displayed_entry;
7496 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7497 s->nrefs) == -1)
7498 return got_error_from_errno("asprintf");
7500 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7501 if (err) {
7502 free(line);
7503 return err;
7505 if (view_needs_focus_indication(view))
7506 wstandout(view->window);
7507 waddwstr(view->window, wline);
7508 if (view_needs_focus_indication(view))
7509 wstandend(view->window);
7510 free(wline);
7511 wline = NULL;
7512 free(line);
7513 line = NULL;
7514 if (width < view->ncols - 1)
7515 waddch(view->window, '\n');
7516 if (--limit <= 0)
7517 return NULL;
7519 n = 0;
7520 while (re && limit > 0) {
7521 char *line = NULL;
7522 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7524 if (s->show_date) {
7525 struct got_commit_object *ci;
7526 struct got_tag_object *tag;
7527 struct got_object_id *id;
7528 struct tm tm;
7529 time_t t;
7531 err = got_ref_resolve(&id, s->repo, re->ref);
7532 if (err)
7533 return err;
7534 err = got_object_open_as_tag(&tag, s->repo, id);
7535 if (err) {
7536 if (err->code != GOT_ERR_OBJ_TYPE) {
7537 free(id);
7538 return err;
7540 err = got_object_open_as_commit(&ci, s->repo,
7541 id);
7542 if (err) {
7543 free(id);
7544 return err;
7546 t = got_object_commit_get_committer_time(ci);
7547 got_object_commit_close(ci);
7548 } else {
7549 t = got_object_tag_get_tagger_time(tag);
7550 got_object_tag_close(tag);
7552 free(id);
7553 if (gmtime_r(&t, &tm) == NULL)
7554 return got_error_from_errno("gmtime_r");
7555 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7556 return got_error(GOT_ERR_NO_SPACE);
7558 if (got_ref_is_symbolic(re->ref)) {
7559 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7560 ymd : "", got_ref_get_name(re->ref),
7561 got_ref_get_symref_target(re->ref)) == -1)
7562 return got_error_from_errno("asprintf");
7563 } else if (s->show_ids) {
7564 struct got_object_id *id;
7565 char *id_str;
7566 err = got_ref_resolve(&id, s->repo, re->ref);
7567 if (err)
7568 return err;
7569 err = got_object_id_str(&id_str, id);
7570 if (err) {
7571 free(id);
7572 return err;
7574 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7575 got_ref_get_name(re->ref), id_str) == -1) {
7576 err = got_error_from_errno("asprintf");
7577 free(id);
7578 free(id_str);
7579 return err;
7581 free(id);
7582 free(id_str);
7583 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7584 got_ref_get_name(re->ref)) == -1)
7585 return got_error_from_errno("asprintf");
7587 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7588 0, 0);
7589 if (err) {
7590 free(line);
7591 return err;
7593 if (n == s->selected) {
7594 if (view->focussed)
7595 wstandout(view->window);
7596 s->selected_entry = re;
7598 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7599 if (tc)
7600 wattr_on(view->window,
7601 COLOR_PAIR(tc->colorpair), NULL);
7602 waddwstr(view->window, wline);
7603 if (tc)
7604 wattr_off(view->window,
7605 COLOR_PAIR(tc->colorpair), NULL);
7606 if (width < view->ncols - 1)
7607 waddch(view->window, '\n');
7608 if (n == s->selected && view->focussed)
7609 wstandend(view->window);
7610 free(line);
7611 free(wline);
7612 wline = NULL;
7613 n++;
7614 s->ndisplayed++;
7615 s->last_displayed_entry = re;
7617 limit--;
7618 re = TAILQ_NEXT(re, entry);
7621 view_border(view);
7622 return err;
7625 static const struct got_error *
7626 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7627 struct tog_reflist_entry *re, struct got_repository *repo)
7629 const struct got_error *err = NULL;
7630 struct got_object_id *commit_id = NULL;
7631 struct tog_view *tree_view;
7633 *new_view = NULL;
7635 err = resolve_reflist_entry(&commit_id, re, repo);
7636 if (err) {
7637 if (err->code != GOT_ERR_OBJ_TYPE)
7638 return err;
7639 else
7640 return NULL;
7644 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7645 if (tree_view == NULL) {
7646 err = got_error_from_errno("view_open");
7647 goto done;
7650 err = open_tree_view(tree_view, commit_id,
7651 got_ref_get_name(re->ref), repo);
7652 if (err)
7653 goto done;
7655 *new_view = tree_view;
7656 done:
7657 free(commit_id);
7658 return err;
7660 static const struct got_error *
7661 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7663 const struct got_error *err = NULL;
7664 struct tog_ref_view_state *s = &view->state.ref;
7665 struct tog_view *log_view, *tree_view;
7666 struct tog_reflist_entry *re;
7667 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7669 switch (ch) {
7670 case 'i':
7671 s->show_ids = !s->show_ids;
7672 view->count = 0;
7673 break;
7674 case 'm':
7675 s->show_date = !s->show_date;
7676 view->count = 0;
7677 break;
7678 case 'o':
7679 s->sort_by_date = !s->sort_by_date;
7680 view->count = 0;
7681 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7682 got_ref_cmp_by_commit_timestamp_descending :
7683 tog_ref_cmp_by_name, s->repo);
7684 if (err)
7685 break;
7686 got_reflist_object_id_map_free(tog_refs_idmap);
7687 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7688 &tog_refs, s->repo);
7689 if (err)
7690 break;
7691 ref_view_free_refs(s);
7692 err = ref_view_load_refs(s);
7693 break;
7694 case KEY_ENTER:
7695 case '\r':
7696 view->count = 0;
7697 if (!s->selected_entry)
7698 break;
7699 if (view_is_parent_view(view))
7700 view_get_split(view, &begin_y, &begin_x);
7702 err = log_ref_entry(&log_view, begin_y, begin_x,
7703 s->selected_entry, s->repo);
7704 if (err)
7705 break;
7707 if (view_is_parent_view(view) &&
7708 view->mode == TOG_VIEW_SPLIT_HRZN) {
7709 err = view_init_hsplit(view, begin_y);
7710 if (err)
7711 break;
7714 view->focussed = 0;
7715 log_view->focussed = 1;
7716 log_view->mode = view->mode;
7717 log_view->nlines = view->lines - begin_y;
7718 if (view_is_parent_view(view)) {
7719 view_transfer_size(log_view, view);
7720 err = view_close_child(view);
7721 if (err)
7722 return err;
7723 err = view_set_child(view, log_view);
7724 if (err)
7725 return err;
7726 view->focus_child = 1;
7727 } else
7728 *new_view = log_view;
7729 break;
7730 case 't':
7731 view->count = 0;
7732 if (!s->selected_entry)
7733 break;
7734 if (view_is_parent_view(view))
7735 view_get_split(view, &begin_y, &begin_x);
7736 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7737 s->selected_entry, s->repo);
7738 if (err || tree_view == NULL)
7739 break;
7740 if (view_is_parent_view(view) &&
7741 view->mode == TOG_VIEW_SPLIT_HRZN) {
7742 err = view_init_hsplit(view, begin_y);
7743 if (err)
7744 break;
7746 view->focussed = 0;
7747 tree_view->focussed = 1;
7748 tree_view->mode = view->mode;
7749 tree_view->nlines = view->lines - begin_y;
7750 if (view_is_parent_view(view)) {
7751 view_transfer_size(tree_view, view);
7752 err = view_close_child(view);
7753 if (err)
7754 return err;
7755 err = view_set_child(view, tree_view);
7756 if (err)
7757 return err;
7758 view->focus_child = 1;
7759 } else
7760 *new_view = tree_view;
7761 break;
7762 case 'g':
7763 case KEY_HOME:
7764 s->selected = 0;
7765 view->count = 0;
7766 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7767 break;
7768 case 'G':
7769 case KEY_END: {
7770 int eos = view->nlines - 1;
7772 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7773 --eos; /* border */
7774 s->selected = 0;
7775 view->count = 0;
7776 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7777 for (n = 0; n < eos; n++) {
7778 if (re == NULL)
7779 break;
7780 s->first_displayed_entry = re;
7781 re = TAILQ_PREV(re, tog_reflist_head, entry);
7783 if (n > 0)
7784 s->selected = n - 1;
7785 break;
7787 case 'k':
7788 case KEY_UP:
7789 case CTRL('p'):
7790 if (s->selected > 0) {
7791 s->selected--;
7792 break;
7794 ref_scroll_up(s, 1);
7795 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7796 view->count = 0;
7797 break;
7798 case CTRL('u'):
7799 case 'u':
7800 nscroll /= 2;
7801 /* FALL THROUGH */
7802 case KEY_PPAGE:
7803 case CTRL('b'):
7804 case 'b':
7805 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7806 s->selected -= MIN(nscroll, s->selected);
7807 ref_scroll_up(s, MAX(0, nscroll));
7808 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7809 view->count = 0;
7810 break;
7811 case 'j':
7812 case KEY_DOWN:
7813 case CTRL('n'):
7814 if (s->selected < s->ndisplayed - 1) {
7815 s->selected++;
7816 break;
7818 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7819 /* can't scroll any further */
7820 view->count = 0;
7821 break;
7823 ref_scroll_down(view, 1);
7824 break;
7825 case CTRL('d'):
7826 case 'd':
7827 nscroll /= 2;
7828 /* FALL THROUGH */
7829 case KEY_NPAGE:
7830 case CTRL('f'):
7831 case 'f':
7832 case ' ':
7833 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7834 /* can't scroll any further; move cursor down */
7835 if (s->selected < s->ndisplayed - 1)
7836 s->selected += MIN(nscroll,
7837 s->ndisplayed - s->selected - 1);
7838 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7839 s->selected += s->ndisplayed - s->selected - 1;
7840 view->count = 0;
7841 break;
7843 ref_scroll_down(view, nscroll);
7844 break;
7845 case CTRL('l'):
7846 view->count = 0;
7847 tog_free_refs();
7848 err = tog_load_refs(s->repo, s->sort_by_date);
7849 if (err)
7850 break;
7851 ref_view_free_refs(s);
7852 err = ref_view_load_refs(s);
7853 break;
7854 case KEY_RESIZE:
7855 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7856 s->selected = view->nlines - 2;
7857 break;
7858 default:
7859 view->count = 0;
7860 break;
7863 return err;
7866 __dead static void
7867 usage_ref(void)
7869 endwin();
7870 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7871 getprogname());
7872 exit(1);
7875 static const struct got_error *
7876 cmd_ref(int argc, char *argv[])
7878 const struct got_error *error;
7879 struct got_repository *repo = NULL;
7880 struct got_worktree *worktree = NULL;
7881 char *cwd = NULL, *repo_path = NULL;
7882 int ch;
7883 struct tog_view *view;
7884 int *pack_fds = NULL;
7886 while ((ch = getopt(argc, argv, "r:")) != -1) {
7887 switch (ch) {
7888 case 'r':
7889 repo_path = realpath(optarg, NULL);
7890 if (repo_path == NULL)
7891 return got_error_from_errno2("realpath",
7892 optarg);
7893 break;
7894 default:
7895 usage_ref();
7896 /* NOTREACHED */
7900 argc -= optind;
7901 argv += optind;
7903 if (argc > 1)
7904 usage_ref();
7906 error = got_repo_pack_fds_open(&pack_fds);
7907 if (error != NULL)
7908 goto done;
7910 if (repo_path == NULL) {
7911 cwd = getcwd(NULL, 0);
7912 if (cwd == NULL)
7913 return got_error_from_errno("getcwd");
7914 error = got_worktree_open(&worktree, cwd);
7915 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7916 goto done;
7917 if (worktree)
7918 repo_path =
7919 strdup(got_worktree_get_repo_path(worktree));
7920 else
7921 repo_path = strdup(cwd);
7922 if (repo_path == NULL) {
7923 error = got_error_from_errno("strdup");
7924 goto done;
7928 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7929 if (error != NULL)
7930 goto done;
7932 init_curses();
7934 error = apply_unveil(got_repo_get_path(repo), NULL);
7935 if (error)
7936 goto done;
7938 error = tog_load_refs(repo, 0);
7939 if (error)
7940 goto done;
7942 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7943 if (view == NULL) {
7944 error = got_error_from_errno("view_open");
7945 goto done;
7948 error = open_ref_view(view, repo);
7949 if (error)
7950 goto done;
7952 if (worktree) {
7953 /* Release work tree lock. */
7954 got_worktree_close(worktree);
7955 worktree = NULL;
7957 error = view_loop(view);
7958 done:
7959 free(repo_path);
7960 free(cwd);
7961 if (repo) {
7962 const struct got_error *close_err = got_repo_close(repo);
7963 if (close_err)
7964 error = close_err;
7966 if (pack_fds) {
7967 const struct got_error *pack_err =
7968 got_repo_pack_fds_close(pack_fds);
7969 if (error == NULL)
7970 error = pack_err;
7972 tog_free_refs();
7973 return error;
7977 * If view was scrolled down to move the selected line into view when opening a
7978 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7980 static void
7981 offset_selection_up(struct tog_view *view)
7983 switch (view->type) {
7984 case TOG_VIEW_BLAME: {
7985 struct tog_blame_view_state *s = &view->state.blame;
7986 if (s->first_displayed_line == 1) {
7987 s->selected_line = MAX(s->selected_line - view->offset,
7988 1);
7989 break;
7991 if (s->first_displayed_line > view->offset)
7992 s->first_displayed_line -= view->offset;
7993 else
7994 s->first_displayed_line = 1;
7995 s->selected_line += view->offset;
7996 break;
7998 case TOG_VIEW_LOG:
7999 log_scroll_up(&view->state.log, view->offset);
8000 view->state.log.selected += view->offset;
8001 break;
8002 case TOG_VIEW_REF:
8003 ref_scroll_up(&view->state.ref, view->offset);
8004 view->state.ref.selected += view->offset;
8005 break;
8006 case TOG_VIEW_TREE:
8007 tree_scroll_up(&view->state.tree, view->offset);
8008 view->state.tree.selected += view->offset;
8009 break;
8010 default:
8011 break;
8014 view->offset = 0;
8018 * If the selected line is in the section of screen covered by the bottom split,
8019 * scroll down offset lines to move it into view and index its new position.
8021 static const struct got_error *
8022 offset_selection_down(struct tog_view *view)
8024 const struct got_error *err = NULL;
8025 const struct got_error *(*scrolld)(struct tog_view *, int);
8026 int *selected = NULL;
8027 int header, offset;
8029 switch (view->type) {
8030 case TOG_VIEW_BLAME: {
8031 struct tog_blame_view_state *s = &view->state.blame;
8032 header = 3;
8033 scrolld = NULL;
8034 if (s->selected_line > view->nlines - header) {
8035 offset = abs(view->nlines - s->selected_line - header);
8036 s->first_displayed_line += offset;
8037 s->selected_line -= offset;
8038 view->offset = offset;
8040 break;
8042 case TOG_VIEW_LOG: {
8043 struct tog_log_view_state *s = &view->state.log;
8044 scrolld = &log_scroll_down;
8045 header = view_is_parent_view(view) ? 3 : 2;
8046 selected = &s->selected;
8047 break;
8049 case TOG_VIEW_REF: {
8050 struct tog_ref_view_state *s = &view->state.ref;
8051 scrolld = &ref_scroll_down;
8052 header = 3;
8053 selected = &s->selected;
8054 break;
8056 case TOG_VIEW_TREE: {
8057 struct tog_tree_view_state *s = &view->state.tree;
8058 scrolld = &tree_scroll_down;
8059 header = 5;
8060 selected = &s->selected;
8061 break;
8063 default:
8064 selected = NULL;
8065 scrolld = NULL;
8066 header = 0;
8067 break;
8070 if (selected && *selected > view->nlines - header) {
8071 offset = abs(view->nlines - *selected - header);
8072 view->offset = offset;
8073 if (scrolld && offset) {
8074 err = scrolld(view, offset);
8075 *selected -= offset;
8079 return err;
8082 static void
8083 list_commands(FILE *fp)
8085 size_t i;
8087 fprintf(fp, "commands:");
8088 for (i = 0; i < nitems(tog_commands); i++) {
8089 const struct tog_cmd *cmd = &tog_commands[i];
8090 fprintf(fp, " %s", cmd->name);
8092 fputc('\n', fp);
8095 __dead static void
8096 usage(int hflag, int status)
8098 FILE *fp = (status == 0) ? stdout : stderr;
8100 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8101 getprogname());
8102 if (hflag) {
8103 fprintf(fp, "lazy usage: %s path\n", getprogname());
8104 list_commands(fp);
8106 exit(status);
8109 static char **
8110 make_argv(int argc, ...)
8112 va_list ap;
8113 char **argv;
8114 int i;
8116 va_start(ap, argc);
8118 argv = calloc(argc, sizeof(char *));
8119 if (argv == NULL)
8120 err(1, "calloc");
8121 for (i = 0; i < argc; i++) {
8122 argv[i] = strdup(va_arg(ap, char *));
8123 if (argv[i] == NULL)
8124 err(1, "strdup");
8127 va_end(ap);
8128 return argv;
8132 * Try to convert 'tog path' into a 'tog log path' command.
8133 * The user could simply have mistyped the command rather than knowingly
8134 * provided a path. So check whether argv[0] can in fact be resolved
8135 * to a path in the HEAD commit and print a special error if not.
8136 * This hack is for mpi@ <3
8138 static const struct got_error *
8139 tog_log_with_path(int argc, char *argv[])
8141 const struct got_error *error = NULL, *close_err;
8142 const struct tog_cmd *cmd = NULL;
8143 struct got_repository *repo = NULL;
8144 struct got_worktree *worktree = NULL;
8145 struct got_object_id *commit_id = NULL, *id = NULL;
8146 struct got_commit_object *commit = NULL;
8147 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8148 char *commit_id_str = NULL, **cmd_argv = NULL;
8149 int *pack_fds = NULL;
8151 cwd = getcwd(NULL, 0);
8152 if (cwd == NULL)
8153 return got_error_from_errno("getcwd");
8155 error = got_repo_pack_fds_open(&pack_fds);
8156 if (error != NULL)
8157 goto done;
8159 error = got_worktree_open(&worktree, cwd);
8160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8161 goto done;
8163 if (worktree)
8164 repo_path = strdup(got_worktree_get_repo_path(worktree));
8165 else
8166 repo_path = strdup(cwd);
8167 if (repo_path == NULL) {
8168 error = got_error_from_errno("strdup");
8169 goto done;
8172 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8173 if (error != NULL)
8174 goto done;
8176 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8177 repo, worktree);
8178 if (error)
8179 goto done;
8181 error = tog_load_refs(repo, 0);
8182 if (error)
8183 goto done;
8184 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8185 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8186 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8187 if (error)
8188 goto done;
8190 if (worktree) {
8191 got_worktree_close(worktree);
8192 worktree = NULL;
8195 error = got_object_open_as_commit(&commit, repo, commit_id);
8196 if (error)
8197 goto done;
8199 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8200 if (error) {
8201 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8202 goto done;
8203 fprintf(stderr, "%s: '%s' is no known command or path\n",
8204 getprogname(), argv[0]);
8205 usage(1, 1);
8206 /* not reached */
8209 close_err = got_repo_close(repo);
8210 if (error == NULL)
8211 error = close_err;
8212 repo = NULL;
8214 error = got_object_id_str(&commit_id_str, commit_id);
8215 if (error)
8216 goto done;
8218 cmd = &tog_commands[0]; /* log */
8219 argc = 4;
8220 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8221 error = cmd->cmd_main(argc, cmd_argv);
8222 done:
8223 if (repo) {
8224 close_err = got_repo_close(repo);
8225 if (error == NULL)
8226 error = close_err;
8228 if (commit)
8229 got_object_commit_close(commit);
8230 if (worktree)
8231 got_worktree_close(worktree);
8232 if (pack_fds) {
8233 const struct got_error *pack_err =
8234 got_repo_pack_fds_close(pack_fds);
8235 if (error == NULL)
8236 error = pack_err;
8238 free(id);
8239 free(commit_id_str);
8240 free(commit_id);
8241 free(cwd);
8242 free(repo_path);
8243 free(in_repo_path);
8244 if (cmd_argv) {
8245 int i;
8246 for (i = 0; i < argc; i++)
8247 free(cmd_argv[i]);
8248 free(cmd_argv);
8250 tog_free_refs();
8251 return error;
8254 int
8255 main(int argc, char *argv[])
8257 const struct got_error *error = NULL;
8258 const struct tog_cmd *cmd = NULL;
8259 int ch, hflag = 0, Vflag = 0;
8260 char **cmd_argv = NULL;
8261 static const struct option longopts[] = {
8262 { "version", no_argument, NULL, 'V' },
8263 { NULL, 0, NULL, 0}
8265 char *diff_algo_str = NULL;
8267 setlocale(LC_CTYPE, "");
8269 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8270 switch (ch) {
8271 case 'h':
8272 hflag = 1;
8273 break;
8274 case 'V':
8275 Vflag = 1;
8276 break;
8277 default:
8278 usage(hflag, 1);
8279 /* NOTREACHED */
8283 argc -= optind;
8284 argv += optind;
8285 optind = 1;
8286 optreset = 1;
8288 if (Vflag) {
8289 got_version_print_str();
8290 return 0;
8293 #ifndef PROFILE
8294 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8295 NULL) == -1)
8296 err(1, "pledge");
8297 #endif
8299 if (argc == 0) {
8300 if (hflag)
8301 usage(hflag, 0);
8302 /* Build an argument vector which runs a default command. */
8303 cmd = &tog_commands[0];
8304 argc = 1;
8305 cmd_argv = make_argv(argc, cmd->name);
8306 } else {
8307 size_t i;
8309 /* Did the user specify a command? */
8310 for (i = 0; i < nitems(tog_commands); i++) {
8311 if (strncmp(tog_commands[i].name, argv[0],
8312 strlen(argv[0])) == 0) {
8313 cmd = &tog_commands[i];
8314 break;
8319 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8320 if (diff_algo_str) {
8321 if (strcasecmp(diff_algo_str, "patience") == 0)
8322 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8323 if (strcasecmp(diff_algo_str, "myers") == 0)
8324 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8327 if (cmd == NULL) {
8328 if (argc != 1)
8329 usage(0, 1);
8330 /* No command specified; try log with a path */
8331 error = tog_log_with_path(argc, argv);
8332 } else {
8333 if (hflag)
8334 cmd->cmd_usage();
8335 else
8336 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8339 endwin();
8340 putchar('\n');
8341 if (cmd_argv) {
8342 int i;
8343 for (i = 0; i < argc; i++)
8344 free(cmd_argv[i]);
8345 free(cmd_argv);
8348 if (error && error->code != GOT_ERR_CANCELLED)
8349 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8350 return 0;