Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #endif
25 #include <curses.h>
26 #include <panel.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
51 #include "got_diff.h"
52 #include "got_opentemp.h"
53 #include "got_utf8.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_path.h"
59 #include "got_worktree.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #ifndef MAX
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #endif
69 #ifndef CTRL
70 #define CTRL(x) ((x) & 0x1f)
71 #endif
73 #ifndef nitems
74 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 #endif
77 struct tog_cmd {
78 const char *name;
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 };
83 __dead static void usage(int, int);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_ref(void);
90 static const struct got_error* cmd_log(int, char *[]);
91 static const struct got_error* cmd_diff(int, char *[]);
92 static const struct got_error* cmd_blame(int, char *[]);
93 static const struct got_error* cmd_tree(int, char *[]);
94 static const struct got_error* cmd_ref(int, char *[]);
96 static const struct tog_cmd tog_commands[] = {
97 { "log", cmd_log, usage_log },
98 { "diff", cmd_diff, usage_diff },
99 { "blame", cmd_blame, usage_blame },
100 { "tree", cmd_tree, usage_tree },
101 { "ref", cmd_ref, usage_ref },
102 };
104 enum tog_view_type {
105 TOG_VIEW_DIFF,
106 TOG_VIEW_LOG,
107 TOG_VIEW_BLAME,
108 TOG_VIEW_TREE,
109 TOG_VIEW_REF,
110 };
112 enum tog_view_mode {
113 TOG_VIEW_SPLIT_NONE,
114 TOG_VIEW_SPLIT_VERT,
115 TOG_VIEW_SPLIT_HRZN
116 };
118 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 #define TOG_EOF_STRING "(END)"
122 struct commit_queue_entry {
123 TAILQ_ENTRY(commit_queue_entry) entry;
124 struct got_object_id *id;
125 struct got_commit_object *commit;
126 int idx;
127 };
128 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 struct commit_queue {
130 int ncommits;
131 struct commit_queue_head head;
132 };
134 struct tog_color {
135 STAILQ_ENTRY(tog_color) entry;
136 regex_t regex;
137 short colorpair;
138 };
139 STAILQ_HEAD(tog_colors, tog_color);
141 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 static struct got_reflist_object_id_map *tog_refs_idmap;
143 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 static const struct got_error *
146 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 struct got_reference* re2)
149 const char *name1 = got_ref_get_name(re1);
150 const char *name2 = got_ref_get_name(re2);
151 int isbackup1, isbackup2;
153 /* Sort backup refs towards the bottom of the list. */
154 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 if (!isbackup1 && isbackup2) {
157 *cmp = -1;
158 return NULL;
159 } else if (isbackup1 && !isbackup2) {
160 *cmp = 1;
161 return NULL;
164 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
165 return NULL;
168 static const struct got_error *
169 tog_load_refs(struct got_repository *repo, int sort_by_date)
171 const struct got_error *err;
173 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
175 repo);
176 if (err)
177 return err;
179 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
180 repo);
183 static void
184 tog_free_refs(void)
186 if (tog_refs_idmap) {
187 got_reflist_object_id_map_free(tog_refs_idmap);
188 tog_refs_idmap = NULL;
190 got_ref_list_free(&tog_refs);
193 static const struct got_error *
194 add_color(struct tog_colors *colors, const char *pattern,
195 int idx, short color)
197 const struct got_error *err = NULL;
198 struct tog_color *tc;
199 int regerr = 0;
201 if (idx < 1 || idx > COLOR_PAIRS - 1)
202 return NULL;
204 init_pair(idx, color, -1);
206 tc = calloc(1, sizeof(*tc));
207 if (tc == NULL)
208 return got_error_from_errno("calloc");
209 regerr = regcomp(&tc->regex, pattern,
210 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
211 if (regerr) {
212 static char regerr_msg[512];
213 static char err_msg[512];
214 regerror(regerr, &tc->regex, regerr_msg,
215 sizeof(regerr_msg));
216 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
217 regerr_msg);
218 err = got_error_msg(GOT_ERR_REGEX, err_msg);
219 free(tc);
220 return err;
222 tc->colorpair = idx;
223 STAILQ_INSERT_HEAD(colors, tc, entry);
224 return NULL;
227 static void
228 free_colors(struct tog_colors *colors)
230 struct tog_color *tc;
232 while (!STAILQ_EMPTY(colors)) {
233 tc = STAILQ_FIRST(colors);
234 STAILQ_REMOVE_HEAD(colors, entry);
235 regfree(&tc->regex);
236 free(tc);
240 static struct tog_color *
241 get_color(struct tog_colors *colors, int colorpair)
243 struct tog_color *tc = NULL;
245 STAILQ_FOREACH(tc, colors, entry) {
246 if (tc->colorpair == colorpair)
247 return tc;
250 return NULL;
253 static int
254 default_color_value(const char *envvar)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 return COLOR_MAGENTA;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
259 return COLOR_CYAN;
260 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
261 return COLOR_YELLOW;
262 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
263 return COLOR_GREEN;
264 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 return COLOR_MAGENTA;
266 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 return COLOR_MAGENTA;
268 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
269 return COLOR_CYAN;
270 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
271 return COLOR_GREEN;
272 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
273 return COLOR_GREEN;
274 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
275 return COLOR_CYAN;
276 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
277 return COLOR_YELLOW;
278 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
279 return COLOR_GREEN;
280 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 return COLOR_MAGENTA;
282 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
283 return COLOR_YELLOW;
284 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 return COLOR_CYAN;
287 return -1;
290 static int
291 get_color_value(const char *envvar)
293 const char *val = getenv(envvar);
295 if (val == NULL)
296 return default_color_value(envvar);
298 if (strcasecmp(val, "black") == 0)
299 return COLOR_BLACK;
300 if (strcasecmp(val, "red") == 0)
301 return COLOR_RED;
302 if (strcasecmp(val, "green") == 0)
303 return COLOR_GREEN;
304 if (strcasecmp(val, "yellow") == 0)
305 return COLOR_YELLOW;
306 if (strcasecmp(val, "blue") == 0)
307 return COLOR_BLUE;
308 if (strcasecmp(val, "magenta") == 0)
309 return COLOR_MAGENTA;
310 if (strcasecmp(val, "cyan") == 0)
311 return COLOR_CYAN;
312 if (strcasecmp(val, "white") == 0)
313 return COLOR_WHITE;
314 if (strcasecmp(val, "default") == 0)
315 return -1;
317 return default_color_value(envvar);
321 struct tog_diff_view_state {
322 struct got_object_id *id1, *id2;
323 const char *label1, *label2;
324 FILE *f, *f1, *f2;
325 int fd1, fd2;
326 int first_displayed_line;
327 int last_displayed_line;
328 int eof;
329 int diff_context;
330 int ignore_whitespace;
331 int force_text_diff;
332 struct got_repository *repo;
333 struct tog_colors colors;
334 size_t nlines;
335 off_t *line_offsets;
336 int matched_line;
337 int selected_line;
339 /* passed from log view; may be NULL */
340 struct tog_view *log_view;
341 };
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
345 struct tog_log_thread_args {
346 pthread_cond_t need_commits;
347 pthread_cond_t commit_loaded;
348 int commits_needed;
349 int load_all;
350 struct got_commit_graph *graph;
351 struct commit_queue *commits;
352 const char *in_repo_path;
353 struct got_object_id *start_id;
354 struct got_repository *repo;
355 int *pack_fds;
356 int log_complete;
357 sig_atomic_t *quit;
358 struct commit_queue_entry **first_displayed_entry;
359 struct commit_queue_entry **selected_entry;
360 int *searching;
361 int *search_next_done;
362 regex_t *regex;
363 };
365 struct tog_log_view_state {
366 struct commit_queue commits;
367 struct commit_queue_entry *first_displayed_entry;
368 struct commit_queue_entry *last_displayed_entry;
369 struct commit_queue_entry *selected_entry;
370 int selected;
371 char *in_repo_path;
372 char *head_ref_name;
373 int log_branches;
374 struct got_repository *repo;
375 struct got_object_id *start_id;
376 sig_atomic_t quit;
377 pthread_t thread;
378 struct tog_log_thread_args thread_args;
379 struct commit_queue_entry *matched_entry;
380 struct commit_queue_entry *search_entry;
381 struct tog_colors colors;
382 };
384 #define TOG_COLOR_DIFF_MINUS 1
385 #define TOG_COLOR_DIFF_PLUS 2
386 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
387 #define TOG_COLOR_DIFF_META 4
388 #define TOG_COLOR_TREE_SUBMODULE 5
389 #define TOG_COLOR_TREE_SYMLINK 6
390 #define TOG_COLOR_TREE_DIRECTORY 7
391 #define TOG_COLOR_TREE_EXECUTABLE 8
392 #define TOG_COLOR_COMMIT 9
393 #define TOG_COLOR_AUTHOR 10
394 #define TOG_COLOR_DATE 11
395 #define TOG_COLOR_REFS_HEADS 12
396 #define TOG_COLOR_REFS_TAGS 13
397 #define TOG_COLOR_REFS_REMOTES 14
398 #define TOG_COLOR_REFS_BACKUP 15
400 struct tog_blame_cb_args {
401 struct tog_blame_line *lines; /* one per line */
402 int nlines;
404 struct tog_view *view;
405 struct got_object_id *commit_id;
406 int *quit;
407 };
409 struct tog_blame_thread_args {
410 const char *path;
411 struct got_repository *repo;
412 struct tog_blame_cb_args *cb_args;
413 int *complete;
414 got_cancel_cb cancel_cb;
415 void *cancel_arg;
416 };
418 struct tog_blame {
419 FILE *f;
420 off_t filesize;
421 struct tog_blame_line *lines;
422 int nlines;
423 off_t *line_offsets;
424 pthread_t thread;
425 struct tog_blame_thread_args thread_args;
426 struct tog_blame_cb_args cb_args;
427 const char *path;
428 int *pack_fds;
429 };
431 struct tog_blame_view_state {
432 int first_displayed_line;
433 int last_displayed_line;
434 int selected_line;
435 int blame_complete;
436 int eof;
437 int done;
438 struct got_object_id_queue blamed_commits;
439 struct got_object_qid *blamed_commit;
440 char *path;
441 struct got_repository *repo;
442 struct got_object_id *commit_id;
443 struct tog_blame blame;
444 int matched_line;
445 struct tog_colors colors;
446 };
448 struct tog_parent_tree {
449 TAILQ_ENTRY(tog_parent_tree) entry;
450 struct got_tree_object *tree;
451 struct got_tree_entry *first_displayed_entry;
452 struct got_tree_entry *selected_entry;
453 int selected;
454 };
456 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
458 struct tog_tree_view_state {
459 char *tree_label;
460 struct got_object_id *commit_id;/* commit which this tree belongs to */
461 struct got_tree_object *root; /* the commit's root tree entry */
462 struct got_tree_object *tree; /* currently displayed (sub-)tree */
463 struct got_tree_entry *first_displayed_entry;
464 struct got_tree_entry *last_displayed_entry;
465 struct got_tree_entry *selected_entry;
466 int ndisplayed, selected, show_ids;
467 struct tog_parent_trees parents; /* parent trees of current sub-tree */
468 char *head_ref_name;
469 struct got_repository *repo;
470 struct got_tree_entry *matched_entry;
471 struct tog_colors colors;
472 };
474 struct tog_reflist_entry {
475 TAILQ_ENTRY(tog_reflist_entry) entry;
476 struct got_reference *ref;
477 int idx;
478 };
480 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
482 struct tog_ref_view_state {
483 struct tog_reflist_head refs;
484 struct tog_reflist_entry *first_displayed_entry;
485 struct tog_reflist_entry *last_displayed_entry;
486 struct tog_reflist_entry *selected_entry;
487 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
488 struct got_repository *repo;
489 struct tog_reflist_entry *matched_entry;
490 struct tog_colors colors;
491 };
493 /*
494 * We implement two types of views: parent views and child views.
496 * The 'Tab' key switches focus between a parent view and its child view.
497 * Child views are shown side-by-side to their parent view, provided
498 * there is enough screen estate.
500 * When a new view is opened from within a parent view, this new view
501 * becomes a child view of the parent view, replacing any existing child.
503 * When a new view is opened from within a child view, this new view
504 * becomes a parent view which will obscure the views below until the
505 * user quits the new parent view by typing 'q'.
507 * This list of views contains parent views only.
508 * Child views are only pointed to by their parent view.
509 */
510 TAILQ_HEAD(tog_view_list_head, tog_view);
512 struct tog_view {
513 TAILQ_ENTRY(tog_view) entry;
514 WINDOW *window;
515 PANEL *panel;
516 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
517 int resized_y, resized_x; /* begin_y/x based on user resizing */
518 int maxx, x; /* max column and current start column */
519 int lines, cols; /* copies of LINES and COLS */
520 int nscrolled, offset; /* lines scrolled and hsplit line offset */
521 int ch, count; /* current keymap and count prefix */
522 int resize; /* set when in a resize event */
523 int focussed; /* Only set on one parent or child view at a time. */
524 int dying;
525 struct tog_view *parent;
526 struct tog_view *child;
528 /*
529 * This flag is initially set on parent views when a new child view
530 * is created. It gets toggled when the 'Tab' key switches focus
531 * between parent and child.
532 * The flag indicates whether focus should be passed on to our child
533 * view if this parent view gets picked for focus after another parent
534 * view was closed. This prevents child views from losing focus in such
535 * situations.
536 */
537 int focus_child;
539 enum tog_view_mode mode;
540 /* type-specific state */
541 enum tog_view_type type;
542 union {
543 struct tog_diff_view_state diff;
544 struct tog_log_view_state log;
545 struct tog_blame_view_state blame;
546 struct tog_tree_view_state tree;
547 struct tog_ref_view_state ref;
548 } state;
550 const struct got_error *(*show)(struct tog_view *);
551 const struct got_error *(*input)(struct tog_view **,
552 struct tog_view *, int);
553 const struct got_error *(*reset)(struct tog_view *);
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 *close_log_view(struct tog_view *);
589 static const struct got_error *search_start_log_view(struct tog_view *);
590 static const struct got_error *search_next_log_view(struct tog_view *);
592 static const struct got_error *open_blame_view(struct tog_view *, char *,
593 struct got_object_id *, struct got_repository *);
594 static const struct got_error *show_blame_view(struct tog_view *);
595 static const struct got_error *input_blame_view(struct tog_view **,
596 struct tog_view *, int);
597 static const struct got_error *reset_blame_view(struct tog_view *);
598 static const struct got_error *close_blame_view(struct tog_view *);
599 static const struct got_error *search_start_blame_view(struct tog_view *);
600 static const struct got_error *search_next_blame_view(struct tog_view *);
602 static const struct got_error *open_tree_view(struct tog_view *,
603 struct got_object_id *, const char *, struct got_repository *);
604 static const struct got_error *show_tree_view(struct tog_view *);
605 static const struct got_error *input_tree_view(struct tog_view **,
606 struct tog_view *, int);
607 static const struct got_error *close_tree_view(struct tog_view *);
608 static const struct got_error *search_start_tree_view(struct tog_view *);
609 static const struct got_error *search_next_tree_view(struct tog_view *);
611 static const struct got_error *open_ref_view(struct tog_view *,
612 struct got_repository *);
613 static const struct got_error *show_ref_view(struct tog_view *);
614 static const struct got_error *input_ref_view(struct tog_view **,
615 struct tog_view *, int);
616 static const struct got_error *close_ref_view(struct tog_view *);
617 static const struct got_error *search_start_ref_view(struct tog_view *);
618 static const struct got_error *search_next_ref_view(struct tog_view *);
620 static volatile sig_atomic_t tog_sigwinch_received;
621 static volatile sig_atomic_t tog_sigpipe_received;
622 static volatile sig_atomic_t tog_sigcont_received;
623 static volatile sig_atomic_t tog_sigint_received;
624 static volatile sig_atomic_t tog_sigterm_received;
626 static void
627 tog_sigwinch(int signo)
629 tog_sigwinch_received = 1;
632 static void
633 tog_sigpipe(int signo)
635 tog_sigpipe_received = 1;
638 static void
639 tog_sigcont(int signo)
641 tog_sigcont_received = 1;
644 static void
645 tog_sigint(int signo)
647 tog_sigint_received = 1;
650 static void
651 tog_sigterm(int signo)
653 tog_sigterm_received = 1;
656 static int
657 tog_fatal_signal_received(void)
659 return (tog_sigpipe_received ||
660 tog_sigint_received || tog_sigint_received);
663 static const struct got_error *
664 view_close(struct tog_view *view)
666 const struct got_error *err = NULL;
668 if (view->child) {
669 view_close(view->child);
670 view->child = NULL;
672 if (view->close)
673 err = view->close(view);
674 if (view->panel)
675 del_panel(view->panel);
676 if (view->window)
677 delwin(view->window);
678 free(view);
679 return err;
682 static struct tog_view *
683 view_open(int nlines, int ncols, int begin_y, int begin_x,
684 enum tog_view_type type)
686 struct tog_view *view = calloc(1, sizeof(*view));
688 if (view == NULL)
689 return NULL;
691 view->type = type;
692 view->lines = LINES;
693 view->cols = COLS;
694 view->nlines = nlines ? nlines : LINES - begin_y;
695 view->ncols = ncols ? ncols : COLS - begin_x;
696 view->begin_y = begin_y;
697 view->begin_x = begin_x;
698 view->window = newwin(nlines, ncols, begin_y, begin_x);
699 if (view->window == NULL) {
700 view_close(view);
701 return NULL;
703 view->panel = new_panel(view->window);
704 if (view->panel == NULL ||
705 set_panel_userptr(view->panel, view) != OK) {
706 view_close(view);
707 return NULL;
710 keypad(view->window, TRUE);
711 return view;
714 static int
715 view_split_begin_x(int begin_x)
717 if (begin_x > 0 || COLS < 120)
718 return 0;
719 return (COLS - MAX(COLS / 2, 80));
722 /* XXX Stub till we decide what to do. */
723 static int
724 view_split_begin_y(int lines)
726 return lines * HSPLIT_SCALE;
729 static const struct got_error *view_resize(struct tog_view *);
731 static const struct got_error *
732 view_splitscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
736 if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
737 if (view->resized_y && view->resized_y < view->lines)
738 view->begin_y = view->resized_y;
739 else
740 view->begin_y = view_split_begin_y(view->nlines);
741 view->begin_x = 0;
742 } else if (!view->resize) {
743 if (view->resized_x && view->resized_x < view->cols - 1 &&
744 view->cols > 119)
745 view->begin_x = view->resized_x;
746 else
747 view->begin_x = view_split_begin_x(0);
748 view->begin_y = 0;
750 view->nlines = LINES - view->begin_y;
751 view->ncols = COLS - view->begin_x;
752 view->lines = LINES;
753 view->cols = COLS;
754 err = view_resize(view);
755 if (err)
756 return err;
758 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
759 view->parent->nlines = view->begin_y;
761 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
762 return got_error_from_errno("mvwin");
764 return NULL;
767 static const struct got_error *
768 view_fullscreen(struct tog_view *view)
770 const struct got_error *err = NULL;
772 view->begin_x = 0;
773 view->begin_y = view->resize ? view->begin_y : 0;
774 view->nlines = view->resize ? view->nlines : LINES;
775 view->ncols = COLS;
776 view->lines = LINES;
777 view->cols = COLS;
778 err = view_resize(view);
779 if (err)
780 return err;
782 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
783 return got_error_from_errno("mvwin");
785 return NULL;
788 static int
789 view_is_parent_view(struct tog_view *view)
791 return view->parent == NULL;
794 static int
795 view_is_splitscreen(struct tog_view *view)
797 return view->begin_x > 0 || view->begin_y > 0;
800 static int
801 view_is_fullscreen(struct tog_view *view)
803 return view->nlines == LINES && view->ncols == COLS;
806 static int
807 view_is_hsplit_top(struct tog_view *view)
809 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
810 view_is_splitscreen(view->child);
813 static void
814 view_border(struct tog_view *view)
816 PANEL *panel;
817 const struct tog_view *view_above;
819 if (view->parent)
820 return view_border(view->parent);
822 panel = panel_above(view->panel);
823 if (panel == NULL)
824 return;
826 view_above = panel_userptr(panel);
827 if (view->mode == TOG_VIEW_SPLIT_HRZN)
828 mvwhline(view->window, view_above->begin_y - 1,
829 view->begin_x, got_locale_is_utf8() ?
830 ACS_HLINE : '-', view->ncols);
831 else
832 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
833 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
836 static const struct got_error *view_init_hsplit(struct tog_view *, int);
837 static const struct got_error *request_log_commits(struct tog_view *);
838 static const struct got_error *offset_selection_down(struct tog_view *);
839 static void offset_selection_up(struct tog_view *);
840 static void view_get_split(struct tog_view *, int *, int *);
842 static const struct got_error *
843 view_resize(struct tog_view *view)
845 const struct got_error *err = NULL;
846 int dif, nlines, ncols;
848 dif = LINES - view->lines; /* line difference */
850 if (view->lines > LINES)
851 nlines = view->nlines - (view->lines - LINES);
852 else
853 nlines = view->nlines + (LINES - view->lines);
854 if (view->cols > COLS)
855 ncols = view->ncols - (view->cols - COLS);
856 else
857 ncols = view->ncols + (COLS - view->cols);
859 if (view->child) {
860 int hs = view->child->begin_y;
862 if (!view_is_fullscreen(view))
863 view->child->begin_x = view_split_begin_x(view->begin_x);
864 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
865 view->child->begin_x == 0) {
866 ncols = COLS;
868 view_fullscreen(view->child);
869 if (view->child->focussed)
870 show_panel(view->child->panel);
871 else
872 show_panel(view->panel);
873 } else {
874 ncols = view->child->begin_x;
876 view_splitscreen(view->child);
877 show_panel(view->child->panel);
879 /*
880 * Request commits if terminal height was increased in a log
881 * view so we have enough commits loaded to populate the view.
882 */
883 if (view->type == TOG_VIEW_LOG && dif > 0) {
884 struct tog_log_view_state *ts = &view->state.log;
886 if (ts->commits.ncommits < ts->selected_entry->idx +
887 view->lines - ts->selected) {
888 view->nscrolled = ts->selected_entry->idx +
889 view->lines - ts->selected -
890 ts->commits.ncommits + dif;
891 err = request_log_commits(view);
892 if (err)
893 return err;
897 /*
898 * XXX This is ugly and needs to be moved into the above
899 * logic but "works" for now and my attempts at moving it
900 * break either 'tab' or 'F' key maps in horizontal splits.
901 */
902 if (hs) {
903 err = view_splitscreen(view->child);
904 if (err)
905 return err;
906 if (dif < 0) { /* top split decreased */
907 err = offset_selection_down(view);
908 if (err)
909 return err;
911 view_border(view);
912 update_panels();
913 doupdate();
914 show_panel(view->child->panel);
915 nlines = view->nlines;
917 } else if (view->parent == NULL)
918 ncols = COLS;
920 if (wresize(view->window, nlines, ncols) == ERR)
921 return got_error_from_errno("wresize");
922 if (replace_panel(view->panel, view->window) == ERR)
923 return got_error_from_errno("replace_panel");
924 wclear(view->window);
926 view->nlines = nlines;
927 view->ncols = ncols;
928 view->lines = LINES;
929 view->cols = COLS;
931 return NULL;
934 static const struct got_error *
935 view_resize_split(struct tog_view *view, int resize)
937 const struct got_error *err = NULL;
938 struct tog_view *v = NULL;
940 if (view->parent)
941 v = view->parent;
942 else
943 v = view;
945 if (!v->child || !view_is_splitscreen(v->child))
946 return NULL;
948 v->resize = v->child->resize = resize; /* lock for resize event */
950 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
951 if (v->child->resized_y)
952 v->child->begin_y = v->child->resized_y;
953 if (view->parent)
954 v->child->begin_y -= resize;
955 else
956 v->child->begin_y += resize;
957 if (v->child->begin_y < 3) {
958 view->count = 0;
959 v->child->begin_y = 3;
960 } else if (v->child->begin_y > LINES - 1) {
961 view->count = 0;
962 v->child->begin_y = LINES - 1;
964 v->ncols = COLS;
965 v->child->ncols = COLS;
966 err = view_init_hsplit(v, v->child->begin_y);
967 if (err)
968 return err;
969 v->child->resized_y = v->child->begin_y;
970 } else {
971 if (v->child->resized_x)
972 v->child->begin_x = v->child->resized_x;
973 if (view->parent)
974 v->child->begin_x -= resize;
975 else
976 v->child->begin_x += resize;
977 if (v->child->begin_x < 11) {
978 view->count = 0;
979 v->child->begin_x = 11;
980 } else if (v->child->begin_x > COLS - 1) {
981 view->count = 0;
982 v->child->begin_x = COLS - 1;
984 v->child->resized_x = v->child->begin_x;
987 v->child->mode = v->mode;
988 v->child->nlines = v->lines - v->child->begin_y;
989 v->child->ncols = v->cols - v->child->begin_x;
990 v->focus_child = 1;
992 err = view_fullscreen(v);
993 if (err)
994 return err;
995 err = view_splitscreen(v->child);
996 if (err)
997 return err;
999 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1000 err = offset_selection_down(v->child);
1001 if (err)
1002 return err;
1005 if (v->type == TOG_VIEW_LOG)
1006 err = request_log_commits(v);
1007 else if (v->child->type == TOG_VIEW_LOG)
1008 err = request_log_commits(v->child);
1010 v->resize = v->child->resize = 0;
1012 return err;
1015 static void
1016 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1018 struct tog_view *v = src->child ? src->child : src;
1020 dst->resized_x = v->resized_x;
1021 dst->resized_y = v->resized_y;
1024 static const struct got_error *
1025 view_close_child(struct tog_view *view)
1027 const struct got_error *err = NULL;
1029 if (view->child == NULL)
1030 return NULL;
1032 err = view_close(view->child);
1033 view->child = NULL;
1034 return err;
1037 static const struct got_error *
1038 view_set_child(struct tog_view *view, struct tog_view *child)
1040 const struct got_error *err = NULL;
1042 view->child = child;
1043 child->parent = view;
1045 err = view_resize(view);
1046 if (err)
1047 return err;
1049 if (view->child->resized_x || view->child->resized_y)
1050 err = view_resize_split(view, 0);
1052 return err;
1055 static void
1056 tog_resizeterm(void)
1058 int cols, lines;
1059 struct winsize size;
1061 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1062 cols = 80; /* Default */
1063 lines = 24;
1064 } else {
1065 cols = size.ws_col;
1066 lines = size.ws_row;
1068 resize_term(lines, cols);
1071 static const struct got_error *
1072 view_search_start(struct tog_view *view)
1074 const struct got_error *err = NULL;
1075 struct tog_view *v = view;
1076 char pattern[1024];
1077 int ret;
1079 if (view->search_started) {
1080 regfree(&view->regex);
1081 view->searching = 0;
1082 memset(&view->regmatch, 0, sizeof(view->regmatch));
1084 view->search_started = 0;
1086 if (view->nlines < 1)
1087 return NULL;
1089 if (view_is_hsplit_top(view))
1090 v = view->child;
1092 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1093 wclrtoeol(v->window);
1095 nodelay(view->window, FALSE); /* block for search term input */
1096 nocbreak();
1097 echo();
1098 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1099 wrefresh(v->window);
1100 cbreak();
1101 noecho();
1102 nodelay(view->window, TRUE);
1103 if (ret == ERR)
1104 return NULL;
1106 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1107 err = view->search_start(view);
1108 if (err) {
1109 regfree(&view->regex);
1110 return err;
1112 view->search_started = 1;
1113 view->searching = TOG_SEARCH_FORWARD;
1114 view->search_next_done = 0;
1115 view->search_next(view);
1118 return NULL;
1122 * If view is a parent or child view and is currently in a splitscreen, switch
1123 * to the alternate split. If in a hsplit and LINES < 120, don't vsplit.
1125 static const struct got_error *
1126 switch_split(struct tog_view *view)
1128 const struct got_error *err = NULL;
1129 struct tog_view *v = NULL;
1131 if (view->parent)
1132 v = view->parent;
1133 else
1134 v = view;
1136 if (!v->child || !view_is_splitscreen(v->child))
1137 return NULL;
1138 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->cols < 120)
1139 return NULL;
1141 if (!v->mode || v->mode == TOG_VIEW_SPLIT_HRZN) {
1142 v->child->nscrolled = LINES - v->child->nlines;
1143 v->mode = TOG_VIEW_SPLIT_VERT;
1144 } else if (v->mode == TOG_VIEW_SPLIT_VERT)
1145 v->mode = TOG_VIEW_SPLIT_HRZN;
1147 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1148 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1149 v->child->begin_y = v->child->resized_y;
1150 if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1151 v->child->begin_x = v->child->resized_x;
1153 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1154 v->ncols = COLS;
1155 v->child->ncols = COLS;
1157 err = view_init_hsplit(v, v->child->begin_y);
1158 if (err)
1159 return err;
1161 v->child->mode = v->mode;
1162 v->child->nlines = v->lines - v->child->begin_y;
1163 v->focus_child = 1;
1165 err = view_fullscreen(v);
1166 if (err)
1167 return err;
1168 err = view_splitscreen(v->child);
1169 if (err)
1170 return err;
1172 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1173 err = offset_selection_down(v->child);
1174 else if (v->mode == TOG_VIEW_SPLIT_VERT) {
1175 if (v->type == TOG_VIEW_LOG)
1176 err = request_log_commits(v);
1177 else if (v->child->type == TOG_VIEW_LOG)
1178 err = request_log_commits(v->child);
1181 return err;
1185 * Compute view->count from numeric input. Assign total to view->count and
1186 * return first non-numeric key entered.
1188 static int
1189 get_compound_key(struct tog_view *view, int c)
1191 struct tog_view *v = view;
1192 int x, n = 0;
1194 if (view_is_hsplit_top(view))
1195 v = view->child;
1196 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1197 v = view->parent;
1199 view->count = 0;
1200 cbreak(); /* block for input */
1201 wmove(v->window, v->nlines - 1, 0);
1202 wclrtoeol(v->window);
1203 waddch(v->window, ':');
1205 do {
1206 x = getcurx(v->window);
1207 if (x != ERR && x < view->ncols) {
1208 waddch(v->window, c);
1209 wrefresh(v->window);
1213 * Don't overflow. Max valid request should be the greatest
1214 * between the longest and total lines; cap at 10 million.
1216 if (n >= 9999999)
1217 n = 9999999;
1218 else
1219 n = n * 10 + (c - '0');
1220 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1222 /* Massage excessive or inapplicable values at the input handler. */
1223 view->count = n;
1225 return c;
1228 static const struct got_error *
1229 view_input(struct tog_view **new, int *done, struct tog_view *view,
1230 struct tog_view_list_head *views)
1232 const struct got_error *err = NULL;
1233 struct tog_view *v;
1234 int ch, errcode;
1236 *new = NULL;
1238 /* Clear "no matches" indicator. */
1239 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1240 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1241 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1242 view->count = 0;
1245 if (view->searching && !view->search_next_done) {
1246 errcode = pthread_mutex_unlock(&tog_mutex);
1247 if (errcode)
1248 return got_error_set_errno(errcode,
1249 "pthread_mutex_unlock");
1250 sched_yield();
1251 errcode = pthread_mutex_lock(&tog_mutex);
1252 if (errcode)
1253 return got_error_set_errno(errcode,
1254 "pthread_mutex_lock");
1255 view->search_next(view);
1256 return NULL;
1259 nodelay(view->window, FALSE);
1260 /* Allow threads to make progress while we are waiting for input. */
1261 errcode = pthread_mutex_unlock(&tog_mutex);
1262 if (errcode)
1263 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1264 /* If we have an unfinished count, let C-g or backspace abort. */
1265 if (view->count && --view->count) {
1266 cbreak();
1267 nodelay(view->window, TRUE);
1268 ch = wgetch(view->window);
1269 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1270 view->count = 0;
1271 else
1272 ch = view->ch;
1273 } else {
1274 ch = wgetch(view->window);
1275 if (ch >= '1' && ch <= '9')
1276 view->ch = ch = get_compound_key(view, ch);
1278 errcode = pthread_mutex_lock(&tog_mutex);
1279 if (errcode)
1280 return got_error_set_errno(errcode, "pthread_mutex_lock");
1281 nodelay(view->window, TRUE);
1283 if (tog_sigwinch_received || tog_sigcont_received) {
1284 tog_resizeterm();
1285 tog_sigwinch_received = 0;
1286 tog_sigcont_received = 0;
1287 TAILQ_FOREACH(v, views, entry) {
1288 err = view_resize(v);
1289 if (err)
1290 return err;
1291 err = v->input(new, v, KEY_RESIZE);
1292 if (err)
1293 return err;
1294 if (v->child) {
1295 err = view_resize(v->child);
1296 if (err)
1297 return err;
1298 err = v->child->input(new, v->child,
1299 KEY_RESIZE);
1300 if (err)
1301 return err;
1302 if (v->child->resized_x || v->child->resized_y) {
1303 err = view_resize_split(v, 0);
1304 if (err)
1305 return err;
1311 switch (ch) {
1312 case '\t':
1313 view->count = 0;
1314 if (view->child) {
1315 view->focussed = 0;
1316 view->child->focussed = 1;
1317 view->focus_child = 1;
1318 } else if (view->parent) {
1319 view->focussed = 0;
1320 view->parent->focussed = 1;
1321 view->parent->focus_child = 0;
1322 if (!view_is_splitscreen(view)) {
1323 if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1324 view->parent->type == TOG_VIEW_LOG) {
1325 err = request_log_commits(view->parent);
1326 if (err)
1327 return err;
1329 offset_selection_up(view->parent);
1330 err = view_fullscreen(view->parent);
1331 if (err)
1332 return err;
1335 break;
1336 case 'q':
1337 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1338 if (view->parent->type == TOG_VIEW_LOG) {
1339 /* might need more commits to fill fullscreen */
1340 err = request_log_commits(view->parent);
1341 if (err)
1342 break;
1344 offset_selection_up(view->parent);
1346 err = view->input(new, view, ch);
1347 view->dying = 1;
1348 break;
1349 case 'Q':
1350 *done = 1;
1351 break;
1352 case 'F':
1353 view->count = 0;
1354 if (view_is_parent_view(view)) {
1355 if (view->child == NULL)
1356 break;
1357 if (view_is_splitscreen(view->child)) {
1358 view->focussed = 0;
1359 view->child->focussed = 1;
1360 err = view_fullscreen(view->child);
1361 } else {
1362 err = view_splitscreen(view->child);
1363 if (!err)
1364 err = view_resize_split(view, 0);
1366 if (err)
1367 break;
1368 err = view->child->input(new, view->child,
1369 KEY_RESIZE);
1370 } else {
1371 if (view_is_splitscreen(view)) {
1372 view->parent->focussed = 0;
1373 view->focussed = 1;
1374 err = view_fullscreen(view);
1375 } else {
1376 err = view_splitscreen(view);
1377 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1378 err = view_resize(view->parent);
1379 if (!err)
1380 err = view_resize_split(view, 0);
1382 if (err)
1383 break;
1384 err = view->input(new, view, KEY_RESIZE);
1386 if (err)
1387 break;
1388 if (view->type == TOG_VIEW_LOG) {
1389 err = request_log_commits(view);
1390 if (err)
1391 break;
1393 if (view->parent)
1394 err = offset_selection_down(view->parent);
1395 if (!err)
1396 err = offset_selection_down(view);
1397 break;
1398 case 'S':
1399 view->count = 0;
1400 err = switch_split(view);
1401 break;
1402 case '-':
1403 err = view_resize_split(view, -1);
1404 break;
1405 case '+':
1406 err = view_resize_split(view, 1);
1407 break;
1408 case KEY_RESIZE:
1409 break;
1410 case '/':
1411 view->count = 0;
1412 if (view->search_start)
1413 view_search_start(view);
1414 else
1415 err = view->input(new, view, ch);
1416 break;
1417 case 'N':
1418 case 'n':
1419 if (view->search_started && view->search_next) {
1420 view->searching = (ch == 'n' ?
1421 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1422 view->search_next_done = 0;
1423 view->search_next(view);
1424 } else
1425 err = view->input(new, view, ch);
1426 break;
1427 case 'A':
1428 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1429 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1430 else
1431 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1432 TAILQ_FOREACH(v, views, entry) {
1433 if (v->reset) {
1434 err = v->reset(v);
1435 if (err)
1436 return err;
1438 if (v->child && v->child->reset) {
1439 err = v->child->reset(v->child);
1440 if (err)
1441 return err;
1444 break;
1445 default:
1446 err = view->input(new, view, ch);
1447 break;
1450 return err;
1453 static int
1454 view_needs_focus_indication(struct tog_view *view)
1456 if (view_is_parent_view(view)) {
1457 if (view->child == NULL || view->child->focussed)
1458 return 0;
1459 if (!view_is_splitscreen(view->child))
1460 return 0;
1461 } else if (!view_is_splitscreen(view))
1462 return 0;
1464 return view->focussed;
1467 static const struct got_error *
1468 view_loop(struct tog_view *view)
1470 const struct got_error *err = NULL;
1471 struct tog_view_list_head views;
1472 struct tog_view *new_view;
1473 char *mode;
1474 int fast_refresh = 10;
1475 int done = 0, errcode;
1477 mode = getenv("TOG_VIEW_SPLIT_MODE");
1478 if (!mode || !(*mode == 'h' || *mode == 'H'))
1479 view->mode = TOG_VIEW_SPLIT_VERT;
1480 else
1481 view->mode = TOG_VIEW_SPLIT_HRZN;
1483 errcode = pthread_mutex_lock(&tog_mutex);
1484 if (errcode)
1485 return got_error_set_errno(errcode, "pthread_mutex_lock");
1487 TAILQ_INIT(&views);
1488 TAILQ_INSERT_HEAD(&views, view, entry);
1490 view->focussed = 1;
1491 err = view->show(view);
1492 if (err)
1493 return err;
1494 update_panels();
1495 doupdate();
1496 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1497 /* Refresh fast during initialization, then become slower. */
1498 if (fast_refresh && fast_refresh-- == 0)
1499 halfdelay(10); /* switch to once per second */
1501 err = view_input(&new_view, &done, view, &views);
1502 if (err)
1503 break;
1504 if (view->dying) {
1505 struct tog_view *v, *prev = NULL;
1507 if (view_is_parent_view(view))
1508 prev = TAILQ_PREV(view, tog_view_list_head,
1509 entry);
1510 else if (view->parent)
1511 prev = view->parent;
1513 if (view->parent) {
1514 view->parent->child = NULL;
1515 view->parent->focus_child = 0;
1516 /* Restore fullscreen line height. */
1517 view->parent->nlines = view->parent->lines;
1518 err = view_resize(view->parent);
1519 if (err)
1520 break;
1521 /* Make resized splits persist. */
1522 view_transfer_size(view->parent, view);
1523 } else
1524 TAILQ_REMOVE(&views, view, entry);
1526 err = view_close(view);
1527 if (err)
1528 goto done;
1530 view = NULL;
1531 TAILQ_FOREACH(v, &views, entry) {
1532 if (v->focussed)
1533 break;
1535 if (view == NULL && new_view == NULL) {
1536 /* No view has focus. Try to pick one. */
1537 if (prev)
1538 view = prev;
1539 else if (!TAILQ_EMPTY(&views)) {
1540 view = TAILQ_LAST(&views,
1541 tog_view_list_head);
1543 if (view) {
1544 if (view->focus_child) {
1545 view->child->focussed = 1;
1546 view = view->child;
1547 } else
1548 view->focussed = 1;
1552 if (new_view) {
1553 struct tog_view *v, *t;
1554 /* Only allow one parent view per type. */
1555 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1556 if (v->type != new_view->type)
1557 continue;
1558 TAILQ_REMOVE(&views, v, entry);
1559 err = view_close(v);
1560 if (err)
1561 goto done;
1562 break;
1564 TAILQ_INSERT_TAIL(&views, new_view, entry);
1565 view = new_view;
1567 if (view) {
1568 if (view_is_parent_view(view)) {
1569 if (view->child && view->child->focussed)
1570 view = view->child;
1571 } else {
1572 if (view->parent && view->parent->focussed)
1573 view = view->parent;
1575 show_panel(view->panel);
1576 if (view->child && view_is_splitscreen(view->child))
1577 show_panel(view->child->panel);
1578 if (view->parent && view_is_splitscreen(view)) {
1579 err = view->parent->show(view->parent);
1580 if (err)
1581 goto done;
1583 err = view->show(view);
1584 if (err)
1585 goto done;
1586 if (view->child) {
1587 err = view->child->show(view->child);
1588 if (err)
1589 goto done;
1591 update_panels();
1592 doupdate();
1595 done:
1596 while (!TAILQ_EMPTY(&views)) {
1597 view = TAILQ_FIRST(&views);
1598 TAILQ_REMOVE(&views, view, entry);
1599 view_close(view);
1602 errcode = pthread_mutex_unlock(&tog_mutex);
1603 if (errcode && err == NULL)
1604 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1606 return err;
1609 __dead static void
1610 usage_log(void)
1612 endwin();
1613 fprintf(stderr,
1614 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1615 getprogname());
1616 exit(1);
1619 /* Create newly allocated wide-character string equivalent to a byte string. */
1620 static const struct got_error *
1621 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1623 char *vis = NULL;
1624 const struct got_error *err = NULL;
1626 *ws = NULL;
1627 *wlen = mbstowcs(NULL, s, 0);
1628 if (*wlen == (size_t)-1) {
1629 int vislen;
1630 if (errno != EILSEQ)
1631 return got_error_from_errno("mbstowcs");
1633 /* byte string invalid in current encoding; try to "fix" it */
1634 err = got_mbsavis(&vis, &vislen, s);
1635 if (err)
1636 return err;
1637 *wlen = mbstowcs(NULL, vis, 0);
1638 if (*wlen == (size_t)-1) {
1639 err = got_error_from_errno("mbstowcs"); /* give up */
1640 goto done;
1644 *ws = calloc(*wlen + 1, sizeof(**ws));
1645 if (*ws == NULL) {
1646 err = got_error_from_errno("calloc");
1647 goto done;
1650 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1651 err = got_error_from_errno("mbstowcs");
1652 done:
1653 free(vis);
1654 if (err) {
1655 free(*ws);
1656 *ws = NULL;
1657 *wlen = 0;
1659 return err;
1662 static const struct got_error *
1663 expand_tab(char **ptr, const char *src)
1665 char *dst;
1666 size_t len, n, idx = 0, sz = 0;
1668 *ptr = NULL;
1669 n = len = strlen(src);
1670 dst = malloc(n + 1);
1671 if (dst == NULL)
1672 return got_error_from_errno("malloc");
1674 while (idx < len && src[idx]) {
1675 const char c = src[idx];
1677 if (c == '\t') {
1678 size_t nb = TABSIZE - sz % TABSIZE;
1679 char *p;
1681 p = realloc(dst, n + nb);
1682 if (p == NULL) {
1683 free(dst);
1684 return got_error_from_errno("realloc");
1687 dst = p;
1688 n += nb;
1689 memset(dst + sz, ' ', nb);
1690 sz += nb;
1691 } else
1692 dst[sz++] = src[idx];
1693 ++idx;
1696 dst[sz] = '\0';
1697 *ptr = dst;
1698 return NULL;
1702 * Advance at most n columns from wline starting at offset off.
1703 * Return the index to the first character after the span operation.
1704 * Return the combined column width of all spanned wide character in
1705 * *rcol.
1707 static int
1708 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1710 int width, i, cols = 0;
1712 if (n == 0) {
1713 *rcol = cols;
1714 return off;
1717 for (i = off; wline[i] != L'\0'; ++i) {
1718 if (wline[i] == L'\t')
1719 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1720 else
1721 width = wcwidth(wline[i]);
1723 if (width == -1) {
1724 width = 1;
1725 wline[i] = L'.';
1728 if (cols + width > n)
1729 break;
1730 cols += width;
1733 *rcol = cols;
1734 return i;
1738 * Format a line for display, ensuring that it won't overflow a width limit.
1739 * With scrolling, the width returned refers to the scrolled version of the
1740 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1742 static const struct got_error *
1743 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1744 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1746 const struct got_error *err = NULL;
1747 int cols;
1748 wchar_t *wline = NULL;
1749 char *exstr = NULL;
1750 size_t wlen;
1751 int i, scrollx;
1753 *wlinep = NULL;
1754 *widthp = 0;
1756 if (expand) {
1757 err = expand_tab(&exstr, line);
1758 if (err)
1759 return err;
1762 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1763 free(exstr);
1764 if (err)
1765 return err;
1767 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1769 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1770 wline[wlen - 1] = L'\0';
1771 wlen--;
1773 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1774 wline[wlen - 1] = L'\0';
1775 wlen--;
1778 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1779 wline[i] = L'\0';
1781 if (widthp)
1782 *widthp = cols;
1783 if (scrollxp)
1784 *scrollxp = scrollx;
1785 if (err)
1786 free(wline);
1787 else
1788 *wlinep = wline;
1789 return err;
1792 static const struct got_error*
1793 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1794 struct got_object_id *id, struct got_repository *repo)
1796 static const struct got_error *err = NULL;
1797 struct got_reflist_entry *re;
1798 char *s;
1799 const char *name;
1801 *refs_str = NULL;
1803 TAILQ_FOREACH(re, refs, entry) {
1804 struct got_tag_object *tag = NULL;
1805 struct got_object_id *ref_id;
1806 int cmp;
1808 name = got_ref_get_name(re->ref);
1809 if (strcmp(name, GOT_REF_HEAD) == 0)
1810 continue;
1811 if (strncmp(name, "refs/", 5) == 0)
1812 name += 5;
1813 if (strncmp(name, "got/", 4) == 0 &&
1814 strncmp(name, "got/backup/", 11) != 0)
1815 continue;
1816 if (strncmp(name, "heads/", 6) == 0)
1817 name += 6;
1818 if (strncmp(name, "remotes/", 8) == 0) {
1819 name += 8;
1820 s = strstr(name, "/" GOT_REF_HEAD);
1821 if (s != NULL && s[strlen(s)] == '\0')
1822 continue;
1824 err = got_ref_resolve(&ref_id, repo, re->ref);
1825 if (err)
1826 break;
1827 if (strncmp(name, "tags/", 5) == 0) {
1828 err = got_object_open_as_tag(&tag, repo, ref_id);
1829 if (err) {
1830 if (err->code != GOT_ERR_OBJ_TYPE) {
1831 free(ref_id);
1832 break;
1834 /* Ref points at something other than a tag. */
1835 err = NULL;
1836 tag = NULL;
1839 cmp = got_object_id_cmp(tag ?
1840 got_object_tag_get_object_id(tag) : ref_id, id);
1841 free(ref_id);
1842 if (tag)
1843 got_object_tag_close(tag);
1844 if (cmp != 0)
1845 continue;
1846 s = *refs_str;
1847 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1848 s ? ", " : "", name) == -1) {
1849 err = got_error_from_errno("asprintf");
1850 free(s);
1851 *refs_str = NULL;
1852 break;
1854 free(s);
1857 return err;
1860 static const struct got_error *
1861 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1862 int col_tab_align)
1864 char *smallerthan;
1866 smallerthan = strchr(author, '<');
1867 if (smallerthan && smallerthan[1] != '\0')
1868 author = smallerthan + 1;
1869 author[strcspn(author, "@>")] = '\0';
1870 return format_line(wauthor, author_width, NULL, author, 0, limit,
1871 col_tab_align, 0);
1874 static const struct got_error *
1875 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1876 struct got_object_id *id, const size_t date_display_cols,
1877 int author_display_cols)
1879 struct tog_log_view_state *s = &view->state.log;
1880 const struct got_error *err = NULL;
1881 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1882 char *logmsg0 = NULL, *logmsg = NULL;
1883 char *author = NULL;
1884 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1885 int author_width, logmsg_width;
1886 char *newline, *line = NULL;
1887 int col, limit, scrollx;
1888 const int avail = view->ncols;
1889 struct tm tm;
1890 time_t committer_time;
1891 struct tog_color *tc;
1893 committer_time = got_object_commit_get_committer_time(commit);
1894 if (gmtime_r(&committer_time, &tm) == NULL)
1895 return got_error_from_errno("gmtime_r");
1896 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1897 return got_error(GOT_ERR_NO_SPACE);
1899 if (avail <= date_display_cols)
1900 limit = MIN(sizeof(datebuf) - 1, avail);
1901 else
1902 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1903 tc = get_color(&s->colors, TOG_COLOR_DATE);
1904 if (tc)
1905 wattr_on(view->window,
1906 COLOR_PAIR(tc->colorpair), NULL);
1907 waddnstr(view->window, datebuf, limit);
1908 if (tc)
1909 wattr_off(view->window,
1910 COLOR_PAIR(tc->colorpair), NULL);
1911 col = limit;
1912 if (col > avail)
1913 goto done;
1915 if (avail >= 120) {
1916 char *id_str;
1917 err = got_object_id_str(&id_str, id);
1918 if (err)
1919 goto done;
1920 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1921 if (tc)
1922 wattr_on(view->window,
1923 COLOR_PAIR(tc->colorpair), NULL);
1924 wprintw(view->window, "%.8s ", id_str);
1925 if (tc)
1926 wattr_off(view->window,
1927 COLOR_PAIR(tc->colorpair), NULL);
1928 free(id_str);
1929 col += 9;
1930 if (col > avail)
1931 goto done;
1934 author = strdup(got_object_commit_get_author(commit));
1935 if (author == NULL) {
1936 err = got_error_from_errno("strdup");
1937 goto done;
1939 err = format_author(&wauthor, &author_width, author, avail - col, col);
1940 if (err)
1941 goto done;
1942 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1943 if (tc)
1944 wattr_on(view->window,
1945 COLOR_PAIR(tc->colorpair), NULL);
1946 waddwstr(view->window, wauthor);
1947 if (tc)
1948 wattr_off(view->window,
1949 COLOR_PAIR(tc->colorpair), NULL);
1950 col += author_width;
1951 while (col < avail && author_width < author_display_cols + 2) {
1952 waddch(view->window, ' ');
1953 col++;
1954 author_width++;
1956 if (col > avail)
1957 goto done;
1959 err = got_object_commit_get_logmsg(&logmsg0, commit);
1960 if (err)
1961 goto done;
1962 logmsg = logmsg0;
1963 while (*logmsg == '\n')
1964 logmsg++;
1965 newline = strchr(logmsg, '\n');
1966 if (newline)
1967 *newline = '\0';
1968 limit = avail - col;
1969 if (view->child && !view_is_hsplit_top(view) && limit > 0)
1970 limit--; /* for the border */
1971 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1972 limit, col, 1);
1973 if (err)
1974 goto done;
1975 waddwstr(view->window, &wlogmsg[scrollx]);
1976 col += MAX(logmsg_width, 0);
1977 while (col < avail) {
1978 waddch(view->window, ' ');
1979 col++;
1981 done:
1982 free(logmsg0);
1983 free(wlogmsg);
1984 free(author);
1985 free(wauthor);
1986 free(line);
1987 return err;
1990 static struct commit_queue_entry *
1991 alloc_commit_queue_entry(struct got_commit_object *commit,
1992 struct got_object_id *id)
1994 struct commit_queue_entry *entry;
1996 entry = calloc(1, sizeof(*entry));
1997 if (entry == NULL)
1998 return NULL;
2000 entry->id = id;
2001 entry->commit = commit;
2002 return entry;
2005 static void
2006 pop_commit(struct commit_queue *commits)
2008 struct commit_queue_entry *entry;
2010 entry = TAILQ_FIRST(&commits->head);
2011 TAILQ_REMOVE(&commits->head, entry, entry);
2012 got_object_commit_close(entry->commit);
2013 commits->ncommits--;
2014 /* Don't free entry->id! It is owned by the commit graph. */
2015 free(entry);
2018 static void
2019 free_commits(struct commit_queue *commits)
2021 while (!TAILQ_EMPTY(&commits->head))
2022 pop_commit(commits);
2025 static const struct got_error *
2026 match_commit(int *have_match, struct got_object_id *id,
2027 struct got_commit_object *commit, regex_t *regex)
2029 const struct got_error *err = NULL;
2030 regmatch_t regmatch;
2031 char *id_str = NULL, *logmsg = NULL;
2033 *have_match = 0;
2035 err = got_object_id_str(&id_str, id);
2036 if (err)
2037 return err;
2039 err = got_object_commit_get_logmsg(&logmsg, commit);
2040 if (err)
2041 goto done;
2043 if (regexec(regex, got_object_commit_get_author(commit), 1,
2044 &regmatch, 0) == 0 ||
2045 regexec(regex, got_object_commit_get_committer(commit), 1,
2046 &regmatch, 0) == 0 ||
2047 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2048 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2049 *have_match = 1;
2050 done:
2051 free(id_str);
2052 free(logmsg);
2053 return err;
2056 static const struct got_error *
2057 queue_commits(struct tog_log_thread_args *a)
2059 const struct got_error *err = NULL;
2062 * We keep all commits open throughout the lifetime of the log
2063 * view in order to avoid having to re-fetch commits from disk
2064 * while updating the display.
2066 do {
2067 struct got_object_id *id;
2068 struct got_commit_object *commit;
2069 struct commit_queue_entry *entry;
2070 int errcode;
2072 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2073 NULL, NULL);
2074 if (err || id == NULL)
2075 break;
2077 err = got_object_open_as_commit(&commit, a->repo, id);
2078 if (err)
2079 break;
2080 entry = alloc_commit_queue_entry(commit, id);
2081 if (entry == NULL) {
2082 err = got_error_from_errno("alloc_commit_queue_entry");
2083 break;
2086 errcode = pthread_mutex_lock(&tog_mutex);
2087 if (errcode) {
2088 err = got_error_set_errno(errcode,
2089 "pthread_mutex_lock");
2090 break;
2093 entry->idx = a->commits->ncommits;
2094 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2095 a->commits->ncommits++;
2097 if (*a->searching == TOG_SEARCH_FORWARD &&
2098 !*a->search_next_done) {
2099 int have_match;
2100 err = match_commit(&have_match, id, commit, a->regex);
2101 if (err)
2102 break;
2103 if (have_match)
2104 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2107 errcode = pthread_mutex_unlock(&tog_mutex);
2108 if (errcode && err == NULL)
2109 err = got_error_set_errno(errcode,
2110 "pthread_mutex_unlock");
2111 if (err)
2112 break;
2113 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2115 return err;
2118 static void
2119 select_commit(struct tog_log_view_state *s)
2121 struct commit_queue_entry *entry;
2122 int ncommits = 0;
2124 entry = s->first_displayed_entry;
2125 while (entry) {
2126 if (ncommits == s->selected) {
2127 s->selected_entry = entry;
2128 break;
2130 entry = TAILQ_NEXT(entry, entry);
2131 ncommits++;
2135 static const struct got_error *
2136 draw_commits(struct tog_view *view)
2138 const struct got_error *err = NULL;
2139 struct tog_log_view_state *s = &view->state.log;
2140 struct commit_queue_entry *entry = s->selected_entry;
2141 const int limit = view->nlines;
2142 int width;
2143 int ncommits, author_cols = 4;
2144 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2145 char *refs_str = NULL;
2146 wchar_t *wline;
2147 struct tog_color *tc;
2148 static const size_t date_display_cols = 12;
2150 if (s->selected_entry &&
2151 !(view->searching && view->search_next_done == 0)) {
2152 struct got_reflist_head *refs;
2153 err = got_object_id_str(&id_str, s->selected_entry->id);
2154 if (err)
2155 return err;
2156 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2157 s->selected_entry->id);
2158 if (refs) {
2159 err = build_refs_str(&refs_str, refs,
2160 s->selected_entry->id, s->repo);
2161 if (err)
2162 goto done;
2166 if (s->thread_args.commits_needed == 0)
2167 halfdelay(10); /* disable fast refresh */
2169 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2170 if (asprintf(&ncommits_str, " [%d/%d] %s",
2171 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2172 (view->searching && !view->search_next_done) ?
2173 "searching..." : "loading...") == -1) {
2174 err = got_error_from_errno("asprintf");
2175 goto done;
2177 } else {
2178 const char *search_str = NULL;
2180 if (view->searching) {
2181 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2182 search_str = "no more matches";
2183 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2184 search_str = "no matches found";
2185 else if (!view->search_next_done)
2186 search_str = "searching...";
2189 if (asprintf(&ncommits_str, " [%d/%d] %s",
2190 entry ? entry->idx + 1 : 0, s->commits.ncommits,
2191 search_str ? search_str :
2192 (refs_str ? refs_str : "")) == -1) {
2193 err = got_error_from_errno("asprintf");
2194 goto done;
2198 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2199 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2200 "........................................",
2201 s->in_repo_path, ncommits_str) == -1) {
2202 err = got_error_from_errno("asprintf");
2203 header = NULL;
2204 goto done;
2206 } else if (asprintf(&header, "commit %s%s",
2207 id_str ? id_str : "........................................",
2208 ncommits_str) == -1) {
2209 err = got_error_from_errno("asprintf");
2210 header = NULL;
2211 goto done;
2213 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2214 if (err)
2215 goto done;
2217 werase(view->window);
2219 if (view_needs_focus_indication(view))
2220 wstandout(view->window);
2221 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2222 if (tc)
2223 wattr_on(view->window,
2224 COLOR_PAIR(tc->colorpair), NULL);
2225 waddwstr(view->window, wline);
2226 if (tc)
2227 wattr_off(view->window,
2228 COLOR_PAIR(tc->colorpair), NULL);
2229 while (width < view->ncols) {
2230 waddch(view->window, ' ');
2231 width++;
2233 if (view_needs_focus_indication(view))
2234 wstandend(view->window);
2235 free(wline);
2236 if (limit <= 1)
2237 goto done;
2239 /* Grow author column size if necessary, and set view->maxx. */
2240 entry = s->first_displayed_entry;
2241 ncommits = 0;
2242 view->maxx = 0;
2243 while (entry) {
2244 char *author, *eol, *msg, *msg0;
2245 wchar_t *wauthor, *wmsg;
2246 int width;
2247 if (ncommits >= limit - 1)
2248 break;
2249 author = strdup(got_object_commit_get_author(entry->commit));
2250 if (author == NULL) {
2251 err = got_error_from_errno("strdup");
2252 goto done;
2254 err = format_author(&wauthor, &width, author, COLS,
2255 date_display_cols);
2256 if (author_cols < width)
2257 author_cols = width;
2258 free(wauthor);
2259 free(author);
2260 err = got_object_commit_get_logmsg(&msg0, entry->commit);
2261 if (err)
2262 goto done;
2263 msg = msg0;
2264 while (*msg == '\n')
2265 ++msg;
2266 if ((eol = strchr(msg, '\n')))
2267 *eol = '\0';
2268 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2269 date_display_cols + author_cols, 0);
2270 if (err)
2271 goto done;
2272 view->maxx = MAX(view->maxx, width);
2273 free(msg0);
2274 free(wmsg);
2275 ncommits++;
2276 entry = TAILQ_NEXT(entry, entry);
2279 entry = s->first_displayed_entry;
2280 s->last_displayed_entry = s->first_displayed_entry;
2281 ncommits = 0;
2282 while (entry) {
2283 if (ncommits >= limit - 1)
2284 break;
2285 if (ncommits == s->selected)
2286 wstandout(view->window);
2287 err = draw_commit(view, entry->commit, entry->id,
2288 date_display_cols, author_cols);
2289 if (ncommits == s->selected)
2290 wstandend(view->window);
2291 if (err)
2292 goto done;
2293 ncommits++;
2294 s->last_displayed_entry = entry;
2295 entry = TAILQ_NEXT(entry, entry);
2298 view_border(view);
2299 done:
2300 free(id_str);
2301 free(refs_str);
2302 free(ncommits_str);
2303 free(header);
2304 return err;
2307 static void
2308 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2310 struct commit_queue_entry *entry;
2311 int nscrolled = 0;
2313 entry = TAILQ_FIRST(&s->commits.head);
2314 if (s->first_displayed_entry == entry)
2315 return;
2317 entry = s->first_displayed_entry;
2318 while (entry && nscrolled < maxscroll) {
2319 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2320 if (entry) {
2321 s->first_displayed_entry = entry;
2322 nscrolled++;
2327 static const struct got_error *
2328 trigger_log_thread(struct tog_view *view, int wait)
2330 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2331 int errcode;
2333 halfdelay(1); /* fast refresh while loading commits */
2335 while (ta->commits_needed > 0 || ta->load_all) {
2336 if (ta->log_complete)
2337 break;
2339 /* Wake the log thread. */
2340 errcode = pthread_cond_signal(&ta->need_commits);
2341 if (errcode)
2342 return got_error_set_errno(errcode,
2343 "pthread_cond_signal");
2346 * The mutex will be released while the view loop waits
2347 * in wgetch(), at which time the log thread will run.
2349 if (!wait)
2350 break;
2352 /* Display progress update in log view. */
2353 show_log_view(view);
2354 update_panels();
2355 doupdate();
2357 /* Wait right here while next commit is being loaded. */
2358 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2359 if (errcode)
2360 return got_error_set_errno(errcode,
2361 "pthread_cond_wait");
2363 /* Display progress update in log view. */
2364 show_log_view(view);
2365 update_panels();
2366 doupdate();
2369 return NULL;
2372 static const struct got_error *
2373 request_log_commits(struct tog_view *view)
2375 struct tog_log_view_state *state = &view->state.log;
2376 const struct got_error *err = NULL;
2378 state->thread_args.commits_needed = view->nscrolled;
2379 err = trigger_log_thread(view, 1);
2380 view->nscrolled = 0;
2382 return err;
2385 static const struct got_error *
2386 log_scroll_down(struct tog_view *view, int maxscroll)
2388 struct tog_log_view_state *s = &view->state.log;
2389 const struct got_error *err = NULL;
2390 struct commit_queue_entry *pentry;
2391 int nscrolled = 0, ncommits_needed;
2393 if (s->last_displayed_entry == NULL)
2394 return NULL;
2396 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2397 if (s->commits.ncommits < ncommits_needed &&
2398 !s->thread_args.log_complete) {
2400 * Ask the log thread for required amount of commits.
2402 s->thread_args.commits_needed += maxscroll;
2403 err = trigger_log_thread(view, 1);
2404 if (err)
2405 return err;
2408 do {
2409 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2410 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2411 break;
2413 s->last_displayed_entry = pentry ?
2414 pentry : s->last_displayed_entry;;
2416 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2417 if (pentry == NULL)
2418 break;
2419 s->first_displayed_entry = pentry;
2420 } while (++nscrolled < maxscroll);
2422 if (view->mode == TOG_VIEW_SPLIT_HRZN)
2423 view->nscrolled += nscrolled;
2424 else
2425 view->nscrolled = 0;
2427 return err;
2430 static const struct got_error *
2431 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2432 struct got_commit_object *commit, struct got_object_id *commit_id,
2433 struct tog_view *log_view, struct got_repository *repo)
2435 const struct got_error *err;
2436 struct got_object_qid *parent_id;
2437 struct tog_view *diff_view;
2439 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2440 if (diff_view == NULL)
2441 return got_error_from_errno("view_open");
2443 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2444 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2445 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2446 if (err == NULL)
2447 *new_view = diff_view;
2448 return err;
2451 static const struct got_error *
2452 tree_view_visit_subtree(struct tog_tree_view_state *s,
2453 struct got_tree_object *subtree)
2455 struct tog_parent_tree *parent;
2457 parent = calloc(1, sizeof(*parent));
2458 if (parent == NULL)
2459 return got_error_from_errno("calloc");
2461 parent->tree = s->tree;
2462 parent->first_displayed_entry = s->first_displayed_entry;
2463 parent->selected_entry = s->selected_entry;
2464 parent->selected = s->selected;
2465 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2466 s->tree = subtree;
2467 s->selected = 0;
2468 s->first_displayed_entry = NULL;
2469 return NULL;
2472 static const struct got_error *
2473 tree_view_walk_path(struct tog_tree_view_state *s,
2474 struct got_commit_object *commit, const char *path)
2476 const struct got_error *err = NULL;
2477 struct got_tree_object *tree = NULL;
2478 const char *p;
2479 char *slash, *subpath = NULL;
2481 /* Walk the path and open corresponding tree objects. */
2482 p = path;
2483 while (*p) {
2484 struct got_tree_entry *te;
2485 struct got_object_id *tree_id;
2486 char *te_name;
2488 while (p[0] == '/')
2489 p++;
2491 /* Ensure the correct subtree entry is selected. */
2492 slash = strchr(p, '/');
2493 if (slash == NULL)
2494 te_name = strdup(p);
2495 else
2496 te_name = strndup(p, slash - p);
2497 if (te_name == NULL) {
2498 err = got_error_from_errno("strndup");
2499 break;
2501 te = got_object_tree_find_entry(s->tree, te_name);
2502 if (te == NULL) {
2503 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2504 free(te_name);
2505 break;
2507 free(te_name);
2508 s->first_displayed_entry = s->selected_entry = te;
2510 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2511 break; /* jump to this file's entry */
2513 slash = strchr(p, '/');
2514 if (slash)
2515 subpath = strndup(path, slash - path);
2516 else
2517 subpath = strdup(path);
2518 if (subpath == NULL) {
2519 err = got_error_from_errno("strdup");
2520 break;
2523 err = got_object_id_by_path(&tree_id, s->repo, commit,
2524 subpath);
2525 if (err)
2526 break;
2528 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2529 free(tree_id);
2530 if (err)
2531 break;
2533 err = tree_view_visit_subtree(s, tree);
2534 if (err) {
2535 got_object_tree_close(tree);
2536 break;
2538 if (slash == NULL)
2539 break;
2540 free(subpath);
2541 subpath = NULL;
2542 p = slash;
2545 free(subpath);
2546 return err;
2549 static const struct got_error *
2550 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2551 struct commit_queue_entry *entry, const char *path,
2552 const char *head_ref_name, struct got_repository *repo)
2554 const struct got_error *err = NULL;
2555 struct tog_tree_view_state *s;
2556 struct tog_view *tree_view;
2558 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2559 if (tree_view == NULL)
2560 return got_error_from_errno("view_open");
2562 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2563 if (err)
2564 return err;
2565 s = &tree_view->state.tree;
2567 *new_view = tree_view;
2569 if (got_path_is_root_dir(path))
2570 return NULL;
2572 return tree_view_walk_path(s, entry->commit, path);
2575 static const struct got_error *
2576 block_signals_used_by_main_thread(void)
2578 sigset_t sigset;
2579 int errcode;
2581 if (sigemptyset(&sigset) == -1)
2582 return got_error_from_errno("sigemptyset");
2584 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2585 if (sigaddset(&sigset, SIGWINCH) == -1)
2586 return got_error_from_errno("sigaddset");
2587 if (sigaddset(&sigset, SIGCONT) == -1)
2588 return got_error_from_errno("sigaddset");
2589 if (sigaddset(&sigset, SIGINT) == -1)
2590 return got_error_from_errno("sigaddset");
2591 if (sigaddset(&sigset, SIGTERM) == -1)
2592 return got_error_from_errno("sigaddset");
2594 /* ncurses handles SIGTSTP */
2595 if (sigaddset(&sigset, SIGTSTP) == -1)
2596 return got_error_from_errno("sigaddset");
2598 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2599 if (errcode)
2600 return got_error_set_errno(errcode, "pthread_sigmask");
2602 return NULL;
2605 static void *
2606 log_thread(void *arg)
2608 const struct got_error *err = NULL;
2609 int errcode = 0;
2610 struct tog_log_thread_args *a = arg;
2611 int done = 0;
2613 err = block_signals_used_by_main_thread();
2614 if (err)
2615 return (void *)err;
2617 while (!done && !err && !tog_fatal_signal_received()) {
2618 err = queue_commits(a);
2619 if (err) {
2620 if (err->code != GOT_ERR_ITER_COMPLETED)
2621 return (void *)err;
2622 err = NULL;
2623 done = 1;
2624 } else if (a->commits_needed > 0 && !a->load_all)
2625 a->commits_needed--;
2627 errcode = pthread_mutex_lock(&tog_mutex);
2628 if (errcode) {
2629 err = got_error_set_errno(errcode,
2630 "pthread_mutex_lock");
2631 break;
2632 } else if (*a->quit)
2633 done = 1;
2634 else if (*a->first_displayed_entry == NULL) {
2635 *a->first_displayed_entry =
2636 TAILQ_FIRST(&a->commits->head);
2637 *a->selected_entry = *a->first_displayed_entry;
2640 errcode = pthread_cond_signal(&a->commit_loaded);
2641 if (errcode) {
2642 err = got_error_set_errno(errcode,
2643 "pthread_cond_signal");
2644 pthread_mutex_unlock(&tog_mutex);
2645 break;
2648 if (done)
2649 a->commits_needed = 0;
2650 else {
2651 if (a->commits_needed == 0 && !a->load_all) {
2652 errcode = pthread_cond_wait(&a->need_commits,
2653 &tog_mutex);
2654 if (errcode)
2655 err = got_error_set_errno(errcode,
2656 "pthread_cond_wait");
2657 if (*a->quit)
2658 done = 1;
2662 errcode = pthread_mutex_unlock(&tog_mutex);
2663 if (errcode && err == NULL)
2664 err = got_error_set_errno(errcode,
2665 "pthread_mutex_unlock");
2667 a->log_complete = 1;
2668 return (void *)err;
2671 static const struct got_error *
2672 stop_log_thread(struct tog_log_view_state *s)
2674 const struct got_error *err = NULL;
2675 int errcode;
2677 if (s->thread) {
2678 s->quit = 1;
2679 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2680 if (errcode)
2681 return got_error_set_errno(errcode,
2682 "pthread_cond_signal");
2683 errcode = pthread_mutex_unlock(&tog_mutex);
2684 if (errcode)
2685 return got_error_set_errno(errcode,
2686 "pthread_mutex_unlock");
2687 errcode = pthread_join(s->thread, (void **)&err);
2688 if (errcode)
2689 return got_error_set_errno(errcode, "pthread_join");
2690 errcode = pthread_mutex_lock(&tog_mutex);
2691 if (errcode)
2692 return got_error_set_errno(errcode,
2693 "pthread_mutex_lock");
2694 s->thread = 0; //NULL;
2697 if (s->thread_args.repo) {
2698 err = got_repo_close(s->thread_args.repo);
2699 s->thread_args.repo = NULL;
2702 if (s->thread_args.pack_fds) {
2703 const struct got_error *pack_err =
2704 got_repo_pack_fds_close(s->thread_args.pack_fds);
2705 if (err == NULL)
2706 err = pack_err;
2707 s->thread_args.pack_fds = NULL;
2710 if (s->thread_args.graph) {
2711 got_commit_graph_close(s->thread_args.graph);
2712 s->thread_args.graph = NULL;
2715 return err;
2718 static const struct got_error *
2719 close_log_view(struct tog_view *view)
2721 const struct got_error *err = NULL;
2722 struct tog_log_view_state *s = &view->state.log;
2723 int errcode;
2725 err = stop_log_thread(s);
2727 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2728 if (errcode && err == NULL)
2729 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2731 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2732 if (errcode && err == NULL)
2733 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2735 free_commits(&s->commits);
2736 free(s->in_repo_path);
2737 s->in_repo_path = NULL;
2738 free(s->start_id);
2739 s->start_id = NULL;
2740 free(s->head_ref_name);
2741 s->head_ref_name = NULL;
2742 return err;
2745 static const struct got_error *
2746 search_start_log_view(struct tog_view *view)
2748 struct tog_log_view_state *s = &view->state.log;
2750 s->matched_entry = NULL;
2751 s->search_entry = NULL;
2752 return NULL;
2755 static const struct got_error *
2756 search_next_log_view(struct tog_view *view)
2758 const struct got_error *err = NULL;
2759 struct tog_log_view_state *s = &view->state.log;
2760 struct commit_queue_entry *entry;
2762 /* Display progress update in log view. */
2763 show_log_view(view);
2764 update_panels();
2765 doupdate();
2767 if (s->search_entry) {
2768 int errcode, ch;
2769 errcode = pthread_mutex_unlock(&tog_mutex);
2770 if (errcode)
2771 return got_error_set_errno(errcode,
2772 "pthread_mutex_unlock");
2773 ch = wgetch(view->window);
2774 errcode = pthread_mutex_lock(&tog_mutex);
2775 if (errcode)
2776 return got_error_set_errno(errcode,
2777 "pthread_mutex_lock");
2778 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2779 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2780 return NULL;
2782 if (view->searching == TOG_SEARCH_FORWARD)
2783 entry = TAILQ_NEXT(s->search_entry, entry);
2784 else
2785 entry = TAILQ_PREV(s->search_entry,
2786 commit_queue_head, entry);
2787 } else if (s->matched_entry) {
2788 int matched_idx = s->matched_entry->idx;
2789 int selected_idx = s->selected_entry->idx;
2792 * If the user has moved the cursor after we hit a match,
2793 * the position from where we should continue searching
2794 * might have changed.
2796 if (view->searching == TOG_SEARCH_FORWARD) {
2797 if (matched_idx > selected_idx)
2798 entry = TAILQ_NEXT(s->selected_entry, entry);
2799 else
2800 entry = TAILQ_NEXT(s->matched_entry, entry);
2801 } else {
2802 if (matched_idx < selected_idx)
2803 entry = TAILQ_PREV(s->selected_entry,
2804 commit_queue_head, entry);
2805 else
2806 entry = TAILQ_PREV(s->matched_entry,
2807 commit_queue_head, entry);
2809 } else {
2810 entry = s->selected_entry;
2813 while (1) {
2814 int have_match = 0;
2816 if (entry == NULL) {
2817 if (s->thread_args.log_complete ||
2818 view->searching == TOG_SEARCH_BACKWARD) {
2819 view->search_next_done =
2820 (s->matched_entry == NULL ?
2821 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2822 s->search_entry = NULL;
2823 return NULL;
2826 * Poke the log thread for more commits and return,
2827 * allowing the main loop to make progress. Search
2828 * will resume at s->search_entry once we come back.
2830 s->thread_args.commits_needed++;
2831 return trigger_log_thread(view, 0);
2834 err = match_commit(&have_match, entry->id, entry->commit,
2835 &view->regex);
2836 if (err)
2837 break;
2838 if (have_match) {
2839 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2840 s->matched_entry = entry;
2841 break;
2844 s->search_entry = entry;
2845 if (view->searching == TOG_SEARCH_FORWARD)
2846 entry = TAILQ_NEXT(entry, entry);
2847 else
2848 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2851 if (s->matched_entry) {
2852 int cur = s->selected_entry->idx;
2853 while (cur < s->matched_entry->idx) {
2854 err = input_log_view(NULL, view, KEY_DOWN);
2855 if (err)
2856 return err;
2857 cur++;
2859 while (cur > s->matched_entry->idx) {
2860 err = input_log_view(NULL, view, KEY_UP);
2861 if (err)
2862 return err;
2863 cur--;
2867 s->search_entry = NULL;
2869 return NULL;
2872 static const struct got_error *
2873 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2874 struct got_repository *repo, const char *head_ref_name,
2875 const char *in_repo_path, int log_branches)
2877 const struct got_error *err = NULL;
2878 struct tog_log_view_state *s = &view->state.log;
2879 struct got_repository *thread_repo = NULL;
2880 struct got_commit_graph *thread_graph = NULL;
2881 int errcode;
2883 if (in_repo_path != s->in_repo_path) {
2884 free(s->in_repo_path);
2885 s->in_repo_path = strdup(in_repo_path);
2886 if (s->in_repo_path == NULL)
2887 return got_error_from_errno("strdup");
2890 /* The commit queue only contains commits being displayed. */
2891 TAILQ_INIT(&s->commits.head);
2892 s->commits.ncommits = 0;
2894 s->repo = repo;
2895 if (head_ref_name) {
2896 s->head_ref_name = strdup(head_ref_name);
2897 if (s->head_ref_name == NULL) {
2898 err = got_error_from_errno("strdup");
2899 goto done;
2902 s->start_id = got_object_id_dup(start_id);
2903 if (s->start_id == NULL) {
2904 err = got_error_from_errno("got_object_id_dup");
2905 goto done;
2907 s->log_branches = log_branches;
2909 STAILQ_INIT(&s->colors);
2910 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2911 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2912 get_color_value("TOG_COLOR_COMMIT"));
2913 if (err)
2914 goto done;
2915 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2916 get_color_value("TOG_COLOR_AUTHOR"));
2917 if (err) {
2918 free_colors(&s->colors);
2919 goto done;
2921 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2922 get_color_value("TOG_COLOR_DATE"));
2923 if (err) {
2924 free_colors(&s->colors);
2925 goto done;
2929 view->show = show_log_view;
2930 view->input = input_log_view;
2931 view->close = close_log_view;
2932 view->search_start = search_start_log_view;
2933 view->search_next = search_next_log_view;
2935 if (s->thread_args.pack_fds == NULL) {
2936 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2937 if (err)
2938 goto done;
2940 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2941 s->thread_args.pack_fds);
2942 if (err)
2943 goto done;
2944 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2945 !s->log_branches);
2946 if (err)
2947 goto done;
2948 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2949 s->repo, NULL, NULL);
2950 if (err)
2951 goto done;
2953 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2954 if (errcode) {
2955 err = got_error_set_errno(errcode, "pthread_cond_init");
2956 goto done;
2958 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2959 if (errcode) {
2960 err = got_error_set_errno(errcode, "pthread_cond_init");
2961 goto done;
2964 s->thread_args.commits_needed = view->nlines;
2965 s->thread_args.graph = thread_graph;
2966 s->thread_args.commits = &s->commits;
2967 s->thread_args.in_repo_path = s->in_repo_path;
2968 s->thread_args.start_id = s->start_id;
2969 s->thread_args.repo = thread_repo;
2970 s->thread_args.log_complete = 0;
2971 s->thread_args.quit = &s->quit;
2972 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2973 s->thread_args.selected_entry = &s->selected_entry;
2974 s->thread_args.searching = &view->searching;
2975 s->thread_args.search_next_done = &view->search_next_done;
2976 s->thread_args.regex = &view->regex;
2977 done:
2978 if (err)
2979 close_log_view(view);
2980 return err;
2983 static const struct got_error *
2984 show_log_view(struct tog_view *view)
2986 const struct got_error *err;
2987 struct tog_log_view_state *s = &view->state.log;
2989 if (s->thread == 0) { //NULL) {
2990 int errcode = pthread_create(&s->thread, NULL, log_thread,
2991 &s->thread_args);
2992 if (errcode)
2993 return got_error_set_errno(errcode, "pthread_create");
2994 if (s->thread_args.commits_needed > 0) {
2995 err = trigger_log_thread(view, 1);
2996 if (err)
2997 return err;
3001 return draw_commits(view);
3004 static void
3005 log_move_cursor_up(struct tog_view *view, int page, int home)
3007 struct tog_log_view_state *s = &view->state.log;
3009 if (s->selected_entry->idx == 0)
3010 view->count = 0;
3011 if (s->first_displayed_entry == NULL)
3012 return;
3014 if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3015 || home)
3016 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3018 if (!page && !home && s->selected > 0)
3019 --s->selected;
3020 else
3021 log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3023 select_commit(s);
3024 return;
3027 static const struct got_error *
3028 log_move_cursor_down(struct tog_view *view, int page)
3030 struct tog_log_view_state *s = &view->state.log;
3031 struct commit_queue_entry *first;
3032 const struct got_error *err = NULL;
3034 first = s->first_displayed_entry;
3035 if (first == NULL) {
3036 view->count = 0;
3037 return NULL;
3040 if (s->thread_args.log_complete &&
3041 s->selected_entry->idx >= s->commits.ncommits - 1)
3042 return NULL;
3044 if (!page) {
3045 int eos = view->nlines - 2;
3047 if (view_is_hsplit_top(view))
3048 --eos; /* border consumes the last line */
3049 if (s->selected < MIN(eos, s->commits.ncommits - 1))
3050 ++s->selected;
3051 else
3052 err = log_scroll_down(view, 1);
3053 } else if (s->thread_args.load_all) {
3054 if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3055 s->selected += MIN(s->last_displayed_entry->idx -
3056 s->selected_entry->idx, page + 1);
3057 else
3058 err = log_scroll_down(view, MIN(page,
3059 s->commits.ncommits - s->selected_entry->idx - 1));
3060 s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3061 } else {
3062 err = log_scroll_down(view, page);
3063 if (err)
3064 return err;
3065 if (first == s->first_displayed_entry && s->selected <
3066 MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3067 s->selected = MIN(s->commits.ncommits - 1, page);
3070 if (err)
3071 return err;
3074 * We might necessarily overshoot in horizontal
3075 * splits; if so, select the last displayed commit.
3077 s->selected = MIN(s->selected,
3078 s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3080 select_commit(s);
3082 if (s->thread_args.log_complete &&
3083 s->selected_entry->idx == s->commits.ncommits - 1)
3084 view->count = 0;
3086 return NULL;
3089 static void
3090 view_get_split(struct tog_view *view, int *y, int *x)
3092 *x = 0;
3093 *y = 0;
3095 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3096 if (view->child && view->child->resized_y)
3097 *y = view->child->resized_y;
3098 else
3099 *y = view_split_begin_y(view->lines);
3100 } else {
3101 if (view->child && view->child->resized_x)
3102 *x = view->child->resized_x;
3103 else
3104 *x = view_split_begin_x(view->begin_x);
3108 /* Split view horizontally at y and offset view->state->selected line. */
3109 static const struct got_error *
3110 view_init_hsplit(struct tog_view *view, int y)
3112 const struct got_error *err = NULL;
3114 view->nlines = y;
3115 view->ncols = COLS;
3116 err = view_resize(view);
3117 if (err)
3118 return err;
3120 err = offset_selection_down(view);
3122 return err;
3125 static const struct got_error *
3126 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3128 const struct got_error *err = NULL;
3129 struct tog_log_view_state *s = &view->state.log;
3130 struct tog_view *diff_view = NULL, *tree_view = NULL;
3131 struct tog_view *ref_view = NULL;
3132 struct commit_queue_entry *entry;
3133 int begin_x = 0, begin_y = 0, eos, n, nscroll;
3135 if (s->thread_args.load_all) {
3136 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3137 s->thread_args.load_all = 0;
3138 else if (s->thread_args.log_complete) {
3139 err = log_move_cursor_down(view, s->commits.ncommits);
3140 s->thread_args.load_all = 0;
3142 return err;
3145 eos = nscroll = view->nlines - 1;
3146 if (view_is_hsplit_top(view))
3147 --eos; /* border */
3149 switch (ch) {
3150 case 'q':
3151 s->quit = 1;
3152 break;
3153 case '0':
3154 view->x = 0;
3155 break;
3156 case '$':
3157 view->x = MAX(view->maxx - view->ncols / 2, 0);
3158 view->count = 0;
3159 break;
3160 case KEY_RIGHT:
3161 case 'l':
3162 if (view->x + view->ncols / 2 < view->maxx)
3163 view->x += 2; /* move two columns right */
3164 else
3165 view->count = 0;
3166 break;
3167 case KEY_LEFT:
3168 case 'h':
3169 view->x -= MIN(view->x, 2); /* move two columns back */
3170 if (view->x <= 0)
3171 view->count = 0;
3172 break;
3173 case 'k':
3174 case KEY_UP:
3175 case '<':
3176 case ',':
3177 case CTRL('p'):
3178 log_move_cursor_up(view, 0, 0);
3179 break;
3180 case 'g':
3181 case KEY_HOME:
3182 log_move_cursor_up(view, 0, 1);
3183 view->count = 0;
3184 break;
3185 case CTRL('u'):
3186 case 'u':
3187 nscroll /= 2;
3188 /* FALL THROUGH */
3189 case KEY_PPAGE:
3190 case CTRL('b'):
3191 case 'b':
3192 log_move_cursor_up(view, nscroll, 0);
3193 break;
3194 case 'j':
3195 case KEY_DOWN:
3196 case '>':
3197 case '.':
3198 case CTRL('n'):
3199 err = log_move_cursor_down(view, 0);
3200 break;
3201 case 'G':
3202 case KEY_END: {
3203 /* We don't know yet how many commits, so we're forced to
3204 * traverse them all. */
3205 view->count = 0;
3206 if (!s->thread_args.log_complete) {
3207 s->thread_args.load_all = 1;
3208 return trigger_log_thread(view, 0);
3211 s->selected = 0;
3212 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3213 for (n = 0; n < eos; n++) {
3214 if (entry == NULL)
3215 break;
3216 s->first_displayed_entry = entry;
3217 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3219 if (n > 0)
3220 s->selected = n - 1;
3221 select_commit(s);
3222 break;
3224 case CTRL('d'):
3225 case 'd':
3226 nscroll /= 2;
3227 /* FALL THROUGH */
3228 case KEY_NPAGE:
3229 case CTRL('f'):
3230 case 'f':
3231 case ' ':
3232 err = log_move_cursor_down(view, nscroll);
3233 break;
3234 case KEY_RESIZE:
3235 if (s->selected > view->nlines - 2)
3236 s->selected = view->nlines - 2;
3237 if (s->selected > s->commits.ncommits - 1)
3238 s->selected = s->commits.ncommits - 1;
3239 select_commit(s);
3240 if (s->commits.ncommits < view->nlines - 1 &&
3241 !s->thread_args.log_complete) {
3242 s->thread_args.commits_needed += (view->nlines - 1) -
3243 s->commits.ncommits;
3244 err = trigger_log_thread(view, 1);
3246 break;
3247 case KEY_ENTER:
3248 case '\r':
3249 view->count = 0;
3250 if (s->selected_entry == NULL)
3251 break;
3253 /* get dimensions--don't split till initialisation succeeds */
3254 if (view_is_parent_view(view))
3255 view_get_split(view, &begin_y, &begin_x);
3257 err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3258 s->selected_entry->commit, s->selected_entry->id,
3259 view, s->repo);
3260 if (err)
3261 break;
3263 if (view_is_parent_view(view) &&
3264 view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3265 err = view_init_hsplit(view, begin_y);
3266 if (err)
3267 break;
3270 view->focussed = 0;
3271 diff_view->focussed = 1;
3272 diff_view->mode = view->mode;
3273 diff_view->nlines = view->lines - begin_y;
3275 if (view_is_parent_view(view)) {
3276 view_transfer_size(diff_view, view);
3277 err = view_close_child(view);
3278 if (err)
3279 return err;
3280 err = view_set_child(view, diff_view);
3281 if (err)
3282 return err;
3283 view->focus_child = 1;
3284 } else
3285 *new_view = diff_view;
3286 break;
3287 case 't':
3288 view->count = 0;
3289 if (s->selected_entry == NULL)
3290 break;
3291 if (view_is_parent_view(view))
3292 view_get_split(view, &begin_y, &begin_x);
3293 err = browse_commit_tree(&tree_view, begin_y, begin_x,
3294 s->selected_entry, s->in_repo_path, s->head_ref_name,
3295 s->repo);
3296 if (err)
3297 break;
3298 if (view_is_parent_view(view) &&
3299 view->mode == TOG_VIEW_SPLIT_HRZN) {
3300 err = view_init_hsplit(view, begin_y);
3301 if (err)
3302 break;
3304 view->focussed = 0;
3305 tree_view->focussed = 1;
3306 tree_view->mode = view->mode;
3307 tree_view->nlines = view->lines - begin_y;
3308 if (view_is_parent_view(view)) {
3309 view_transfer_size(tree_view, view);
3310 err = view_close_child(view);
3311 if (err)
3312 return err;
3313 err = view_set_child(view, tree_view);
3314 if (err)
3315 return err;
3316 view->focus_child = 1;
3317 } else
3318 *new_view = tree_view;
3319 break;
3320 case KEY_BACKSPACE:
3321 case CTRL('l'):
3322 case 'B':
3323 view->count = 0;
3324 if (ch == KEY_BACKSPACE &&
3325 got_path_is_root_dir(s->in_repo_path))
3326 break;
3327 err = stop_log_thread(s);
3328 if (err)
3329 return err;
3330 if (ch == KEY_BACKSPACE) {
3331 char *parent_path;
3332 err = got_path_dirname(&parent_path, s->in_repo_path);
3333 if (err)
3334 return err;
3335 free(s->in_repo_path);
3336 s->in_repo_path = parent_path;
3337 s->thread_args.in_repo_path = s->in_repo_path;
3338 } else if (ch == CTRL('l')) {
3339 struct got_object_id *start_id;
3340 err = got_repo_match_object_id(&start_id, NULL,
3341 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3342 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3343 if (err)
3344 return err;
3345 free(s->start_id);
3346 s->start_id = start_id;
3347 s->thread_args.start_id = s->start_id;
3348 } else /* 'B' */
3349 s->log_branches = !s->log_branches;
3351 if (s->thread_args.pack_fds == NULL) {
3352 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3353 if (err)
3354 return err;
3356 err = got_repo_open(&s->thread_args.repo,
3357 got_repo_get_path(s->repo), NULL,
3358 s->thread_args.pack_fds);
3359 if (err)
3360 return err;
3361 tog_free_refs();
3362 err = tog_load_refs(s->repo, 0);
3363 if (err)
3364 return err;
3365 err = got_commit_graph_open(&s->thread_args.graph,
3366 s->in_repo_path, !s->log_branches);
3367 if (err)
3368 return err;
3369 err = got_commit_graph_iter_start(s->thread_args.graph,
3370 s->start_id, s->repo, NULL, NULL);
3371 if (err)
3372 return err;
3373 free_commits(&s->commits);
3374 s->first_displayed_entry = NULL;
3375 s->last_displayed_entry = NULL;
3376 s->selected_entry = NULL;
3377 s->selected = 0;
3378 s->thread_args.log_complete = 0;
3379 s->quit = 0;
3380 s->thread_args.commits_needed = view->lines;
3381 s->matched_entry = NULL;
3382 s->search_entry = NULL;
3383 break;
3384 case 'r':
3385 view->count = 0;
3386 if (view_is_parent_view(view))
3387 view_get_split(view, &begin_y, &begin_x);
3388 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3389 if (ref_view == NULL)
3390 return got_error_from_errno("view_open");
3391 err = open_ref_view(ref_view, s->repo);
3392 if (err) {
3393 view_close(ref_view);
3394 return err;
3396 if (view_is_parent_view(view) &&
3397 view->mode == TOG_VIEW_SPLIT_HRZN) {
3398 err = view_init_hsplit(view, begin_y);
3399 if (err)
3400 break;
3402 view->focussed = 0;
3403 ref_view->focussed = 1;
3404 ref_view->mode = view->mode;
3405 ref_view->nlines = view->lines - begin_y;
3406 if (view_is_parent_view(view)) {
3407 view_transfer_size(ref_view, view);
3408 err = view_close_child(view);
3409 if (err)
3410 return err;
3411 err = view_set_child(view, ref_view);
3412 if (err)
3413 return err;
3414 view->focus_child = 1;
3415 } else
3416 *new_view = ref_view;
3417 break;
3418 default:
3419 view->count = 0;
3420 break;
3423 return err;
3426 static const struct got_error *
3427 apply_unveil(const char *repo_path, const char *worktree_path)
3429 const struct got_error *error;
3431 #ifdef PROFILE
3432 if (unveil("gmon.out", "rwc") != 0)
3433 return got_error_from_errno2("unveil", "gmon.out");
3434 #endif
3435 if (repo_path && unveil(repo_path, "r") != 0)
3436 return got_error_from_errno2("unveil", repo_path);
3438 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3439 return got_error_from_errno2("unveil", worktree_path);
3441 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3442 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3444 error = got_privsep_unveil_exec_helpers();
3445 if (error != NULL)
3446 return error;
3448 if (unveil(NULL, NULL) != 0)
3449 return got_error_from_errno("unveil");
3451 return NULL;
3454 static void
3455 init_curses(void)
3458 * Override default signal handlers before starting ncurses.
3459 * This should prevent ncurses from installing its own
3460 * broken cleanup() signal handler.
3462 signal(SIGWINCH, tog_sigwinch);
3463 signal(SIGPIPE, tog_sigpipe);
3464 signal(SIGCONT, tog_sigcont);
3465 signal(SIGINT, tog_sigint);
3466 signal(SIGTERM, tog_sigterm);
3468 initscr();
3469 cbreak();
3470 halfdelay(1); /* Do fast refresh while initial view is loading. */
3471 noecho();
3472 nonl();
3473 intrflush(stdscr, FALSE);
3474 keypad(stdscr, TRUE);
3475 curs_set(0);
3476 if (getenv("TOG_COLORS") != NULL) {
3477 start_color();
3478 use_default_colors();
3482 static const struct got_error *
3483 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3484 struct got_repository *repo, struct got_worktree *worktree)
3486 const struct got_error *err = NULL;
3488 if (argc == 0) {
3489 *in_repo_path = strdup("/");
3490 if (*in_repo_path == NULL)
3491 return got_error_from_errno("strdup");
3492 return NULL;
3495 if (worktree) {
3496 const char *prefix = got_worktree_get_path_prefix(worktree);
3497 char *p;
3499 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3500 if (err)
3501 return err;
3502 if (asprintf(in_repo_path, "%s%s%s", prefix,
3503 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3504 p) == -1) {
3505 err = got_error_from_errno("asprintf");
3506 *in_repo_path = NULL;
3508 free(p);
3509 } else
3510 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3512 return err;
3515 static const struct got_error *
3516 cmd_log(int argc, char *argv[])
3518 const struct got_error *error;
3519 struct got_repository *repo = NULL;
3520 struct got_worktree *worktree = NULL;
3521 struct got_object_id *start_id = NULL;
3522 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3523 char *start_commit = NULL, *label = NULL;
3524 struct got_reference *ref = NULL;
3525 const char *head_ref_name = NULL;
3526 int ch, log_branches = 0;
3527 struct tog_view *view;
3528 int *pack_fds = NULL;
3530 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3531 switch (ch) {
3532 case 'b':
3533 log_branches = 1;
3534 break;
3535 case 'c':
3536 start_commit = optarg;
3537 break;
3538 case 'r':
3539 repo_path = realpath(optarg, NULL);
3540 if (repo_path == NULL)
3541 return got_error_from_errno2("realpath",
3542 optarg);
3543 break;
3544 default:
3545 usage_log();
3546 /* NOTREACHED */
3550 argc -= optind;
3551 argv += optind;
3553 if (argc > 1)
3554 usage_log();
3556 error = got_repo_pack_fds_open(&pack_fds);
3557 if (error != NULL)
3558 goto done;
3560 if (repo_path == NULL) {
3561 cwd = getcwd(NULL, 0);
3562 if (cwd == NULL)
3563 return got_error_from_errno("getcwd");
3564 error = got_worktree_open(&worktree, cwd);
3565 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3566 goto done;
3567 if (worktree)
3568 repo_path =
3569 strdup(got_worktree_get_repo_path(worktree));
3570 else
3571 repo_path = strdup(cwd);
3572 if (repo_path == NULL) {
3573 error = got_error_from_errno("strdup");
3574 goto done;
3578 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3579 if (error != NULL)
3580 goto done;
3582 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3583 repo, worktree);
3584 if (error)
3585 goto done;
3587 init_curses();
3589 error = apply_unveil(got_repo_get_path(repo),
3590 worktree ? got_worktree_get_root_path(worktree) : NULL);
3591 if (error)
3592 goto done;
3594 /* already loaded by tog_log_with_path()? */
3595 if (TAILQ_EMPTY(&tog_refs)) {
3596 error = tog_load_refs(repo, 0);
3597 if (error)
3598 goto done;
3601 if (start_commit == NULL) {
3602 error = got_repo_match_object_id(&start_id, &label,
3603 worktree ? got_worktree_get_head_ref_name(worktree) :
3604 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3605 if (error)
3606 goto done;
3607 head_ref_name = label;
3608 } else {
3609 error = got_ref_open(&ref, repo, start_commit, 0);
3610 if (error == NULL)
3611 head_ref_name = got_ref_get_name(ref);
3612 else if (error->code != GOT_ERR_NOT_REF)
3613 goto done;
3614 error = got_repo_match_object_id(&start_id, NULL,
3615 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3616 if (error)
3617 goto done;
3620 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3621 if (view == NULL) {
3622 error = got_error_from_errno("view_open");
3623 goto done;
3625 error = open_log_view(view, start_id, repo, head_ref_name,
3626 in_repo_path, log_branches);
3627 if (error)
3628 goto done;
3629 if (worktree) {
3630 /* Release work tree lock. */
3631 got_worktree_close(worktree);
3632 worktree = NULL;
3634 error = view_loop(view);
3635 done:
3636 free(in_repo_path);
3637 free(repo_path);
3638 free(cwd);
3639 free(start_id);
3640 free(label);
3641 if (ref)
3642 got_ref_close(ref);
3643 if (repo) {
3644 const struct got_error *close_err = got_repo_close(repo);
3645 if (error == NULL)
3646 error = close_err;
3648 if (worktree)
3649 got_worktree_close(worktree);
3650 if (pack_fds) {
3651 const struct got_error *pack_err =
3652 got_repo_pack_fds_close(pack_fds);
3653 if (error == NULL)
3654 error = pack_err;
3656 tog_free_refs();
3657 return error;
3660 __dead static void
3661 usage_diff(void)
3663 endwin();
3664 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3665 "[-w] object1 object2\n", getprogname());
3666 exit(1);
3669 static int
3670 match_line(const char *line, regex_t *regex, size_t nmatch,
3671 regmatch_t *regmatch)
3673 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3676 static struct tog_color *
3677 match_color(struct tog_colors *colors, const char *line)
3679 struct tog_color *tc = NULL;
3681 STAILQ_FOREACH(tc, colors, entry) {
3682 if (match_line(line, &tc->regex, 0, NULL))
3683 return tc;
3686 return NULL;
3689 static const struct got_error *
3690 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3691 WINDOW *window, int skipcol, regmatch_t *regmatch)
3693 const struct got_error *err = NULL;
3694 char *exstr = NULL;
3695 wchar_t *wline = NULL;
3696 int rme, rms, n, width, scrollx;
3697 int width0 = 0, width1 = 0, width2 = 0;
3698 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3700 *wtotal = 0;
3702 rms = regmatch->rm_so;
3703 rme = regmatch->rm_eo;
3705 err = expand_tab(&exstr, line);
3706 if (err)
3707 return err;
3709 /* Split the line into 3 segments, according to match offsets. */
3710 seg0 = strndup(exstr, rms);
3711 if (seg0 == NULL) {
3712 err = got_error_from_errno("strndup");
3713 goto done;
3715 seg1 = strndup(exstr + rms, rme - rms);
3716 if (seg1 == NULL) {
3717 err = got_error_from_errno("strndup");
3718 goto done;
3720 seg2 = strdup(exstr + rme);
3721 if (seg2 == NULL) {
3722 err = got_error_from_errno("strndup");
3723 goto done;
3726 /* draw up to matched token if we haven't scrolled past it */
3727 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3728 col_tab_align, 1);
3729 if (err)
3730 goto done;
3731 n = MAX(width0 - skipcol, 0);
3732 if (n) {
3733 free(wline);
3734 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3735 wlimit, col_tab_align, 1);
3736 if (err)
3737 goto done;
3738 waddwstr(window, &wline[scrollx]);
3739 wlimit -= width;
3740 *wtotal += width;
3743 if (wlimit > 0) {
3744 int i = 0, w = 0;
3745 size_t wlen;
3747 free(wline);
3748 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3749 col_tab_align, 1);
3750 if (err)
3751 goto done;
3752 wlen = wcslen(wline);
3753 while (i < wlen) {
3754 width = wcwidth(wline[i]);
3755 if (width == -1) {
3756 /* should not happen, tabs are expanded */
3757 err = got_error(GOT_ERR_RANGE);
3758 goto done;
3760 if (width0 + w + width > skipcol)
3761 break;
3762 w += width;
3763 i++;
3765 /* draw (visible part of) matched token (if scrolled into it) */
3766 if (width1 - w > 0) {
3767 wattron(window, A_STANDOUT);
3768 waddwstr(window, &wline[i]);
3769 wattroff(window, A_STANDOUT);
3770 wlimit -= (width1 - w);
3771 *wtotal += (width1 - w);
3775 if (wlimit > 0) { /* draw rest of line */
3776 free(wline);
3777 if (skipcol > width0 + width1) {
3778 err = format_line(&wline, &width2, &scrollx, seg2,
3779 skipcol - (width0 + width1), wlimit,
3780 col_tab_align, 1);
3781 if (err)
3782 goto done;
3783 waddwstr(window, &wline[scrollx]);
3784 } else {
3785 err = format_line(&wline, &width2, NULL, seg2, 0,
3786 wlimit, col_tab_align, 1);
3787 if (err)
3788 goto done;
3789 waddwstr(window, wline);
3791 *wtotal += width2;
3793 done:
3794 free(wline);
3795 free(exstr);
3796 free(seg0);
3797 free(seg1);
3798 free(seg2);
3799 return err;
3802 static const struct got_error *
3803 draw_file(struct tog_view *view, const char *header)
3805 struct tog_diff_view_state *s = &view->state.diff;
3806 regmatch_t *regmatch = &view->regmatch;
3807 const struct got_error *err;
3808 int nprinted = 0;
3809 char *line;
3810 size_t linesize = 0;
3811 ssize_t linelen;
3812 struct tog_color *tc;
3813 wchar_t *wline;
3814 int width;
3815 int max_lines = view->nlines;
3816 int nlines = s->nlines;
3817 off_t line_offset;
3819 line_offset = s->line_offsets[s->first_displayed_line - 1];
3820 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3821 return got_error_from_errno("fseek");
3823 werase(view->window);
3825 if (header) {
3826 if (asprintf(&line, "[%d/%d] %s",
3827 s->first_displayed_line - 1 + s->selected_line, nlines,
3828 header) == -1)
3829 return got_error_from_errno("asprintf");
3830 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3831 0, 0);
3832 free(line);
3833 if (err)
3834 return err;
3836 if (view_needs_focus_indication(view))
3837 wstandout(view->window);
3838 waddwstr(view->window, wline);
3839 free(wline);
3840 wline = NULL;
3841 if (view_needs_focus_indication(view))
3842 wstandend(view->window);
3843 if (width <= view->ncols - 1)
3844 waddch(view->window, '\n');
3846 if (max_lines <= 1)
3847 return NULL;
3848 max_lines--;
3851 s->eof = 0;
3852 view->maxx = 0;
3853 line = NULL;
3854 while (max_lines > 0 && nprinted < max_lines) {
3855 linelen = getline(&line, &linesize, s->f);
3856 if (linelen == -1) {
3857 if (feof(s->f)) {
3858 s->eof = 1;
3859 break;
3861 free(line);
3862 return got_ferror(s->f, GOT_ERR_IO);
3865 /* Set view->maxx based on full line length. */
3866 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3867 view->x ? 1 : 0);
3868 if (err) {
3869 free(line);
3870 return err;
3872 view->maxx = MAX(view->maxx, width);
3873 free(wline);
3874 wline = NULL;
3876 tc = match_color(&s->colors, line);
3877 if (tc)
3878 wattr_on(view->window,
3879 COLOR_PAIR(tc->colorpair), NULL);
3880 if (s->first_displayed_line + nprinted == s->matched_line &&
3881 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3882 err = add_matched_line(&width, line, view->ncols, 0,
3883 view->window, view->x, regmatch);
3884 if (err) {
3885 free(line);
3886 return err;
3888 } else {
3889 int skip;
3890 err = format_line(&wline, &width, &skip, line,
3891 view->x, view->ncols, 0, view->x ? 1 : 0);
3892 if (err) {
3893 free(line);
3894 return err;
3896 waddwstr(view->window, &wline[skip]);
3897 free(wline);
3898 wline = NULL;
3900 if (tc)
3901 wattr_off(view->window,
3902 COLOR_PAIR(tc->colorpair), NULL);
3903 if (width <= view->ncols - 1)
3904 waddch(view->window, '\n');
3905 nprinted++;
3907 free(line);
3908 if (nprinted >= 1)
3909 s->last_displayed_line = s->first_displayed_line +
3910 (nprinted - 1);
3911 else
3912 s->last_displayed_line = s->first_displayed_line;
3914 view_border(view);
3916 if (s->eof) {
3917 while (nprinted < view->nlines) {
3918 waddch(view->window, '\n');
3919 nprinted++;
3922 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3923 view->ncols, 0, 0);
3924 if (err) {
3925 return err;
3928 wstandout(view->window);
3929 waddwstr(view->window, wline);
3930 free(wline);
3931 wline = NULL;
3932 wstandend(view->window);
3935 return NULL;
3938 static char *
3939 get_datestr(time_t *time, char *datebuf)
3941 struct tm mytm, *tm;
3942 char *p, *s;
3944 tm = gmtime_r(time, &mytm);
3945 if (tm == NULL)
3946 return NULL;
3947 s = asctime_r(tm, datebuf);
3948 if (s == NULL)
3949 return NULL;
3950 p = strchr(s, '\n');
3951 if (p)
3952 *p = '\0';
3953 return s;
3956 static const struct got_error *
3957 get_changed_paths(struct got_pathlist_head *paths,
3958 struct got_commit_object *commit, struct got_repository *repo)
3960 const struct got_error *err = NULL;
3961 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3962 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3963 struct got_object_qid *qid;
3965 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3966 if (qid != NULL) {
3967 struct got_commit_object *pcommit;
3968 err = got_object_open_as_commit(&pcommit, repo,
3969 &qid->id);
3970 if (err)
3971 return err;
3973 tree_id1 = got_object_id_dup(
3974 got_object_commit_get_tree_id(pcommit));
3975 if (tree_id1 == NULL) {
3976 got_object_commit_close(pcommit);
3977 return got_error_from_errno("got_object_id_dup");
3979 got_object_commit_close(pcommit);
3983 if (tree_id1) {
3984 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3985 if (err)
3986 goto done;
3989 tree_id2 = got_object_commit_get_tree_id(commit);
3990 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3991 if (err)
3992 goto done;
3994 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3995 got_diff_tree_collect_changed_paths, paths, 0);
3996 done:
3997 if (tree1)
3998 got_object_tree_close(tree1);
3999 if (tree2)
4000 got_object_tree_close(tree2);
4001 free(tree_id1);
4002 return err;
4005 static const struct got_error *
4006 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4008 off_t *p;
4010 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4011 if (p == NULL)
4012 return got_error_from_errno("reallocarray");
4013 *line_offsets = p;
4014 (*line_offsets)[*nlines] = off;
4015 (*nlines)++;
4016 return NULL;
4019 static const struct got_error *
4020 write_commit_info(off_t **line_offsets, size_t *nlines,
4021 struct got_object_id *commit_id, struct got_reflist_head *refs,
4022 struct got_repository *repo, FILE *outfile)
4024 const struct got_error *err = NULL;
4025 char datebuf[26], *datestr;
4026 struct got_commit_object *commit;
4027 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4028 time_t committer_time;
4029 const char *author, *committer;
4030 char *refs_str = NULL;
4031 struct got_pathlist_head changed_paths;
4032 struct got_pathlist_entry *pe;
4033 off_t outoff = 0;
4034 int n;
4036 TAILQ_INIT(&changed_paths);
4038 if (refs) {
4039 err = build_refs_str(&refs_str, refs, commit_id, repo);
4040 if (err)
4041 return err;
4044 err = got_object_open_as_commit(&commit, repo, commit_id);
4045 if (err)
4046 return err;
4048 err = got_object_id_str(&id_str, commit_id);
4049 if (err) {
4050 err = got_error_from_errno("got_object_id_str");
4051 goto done;
4054 err = add_line_offset(line_offsets, nlines, 0);
4055 if (err)
4056 goto done;
4058 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4059 refs_str ? refs_str : "", refs_str ? ")" : "");
4060 if (n < 0) {
4061 err = got_error_from_errno("fprintf");
4062 goto done;
4064 outoff += n;
4065 err = add_line_offset(line_offsets, nlines, outoff);
4066 if (err)
4067 goto done;
4069 n = fprintf(outfile, "from: %s\n",
4070 got_object_commit_get_author(commit));
4071 if (n < 0) {
4072 err = got_error_from_errno("fprintf");
4073 goto done;
4075 outoff += n;
4076 err = add_line_offset(line_offsets, nlines, outoff);
4077 if (err)
4078 goto done;
4080 committer_time = got_object_commit_get_committer_time(commit);
4081 datestr = get_datestr(&committer_time, datebuf);
4082 if (datestr) {
4083 n = fprintf(outfile, "date: %s UTC\n", datestr);
4084 if (n < 0) {
4085 err = got_error_from_errno("fprintf");
4086 goto done;
4088 outoff += n;
4089 err = add_line_offset(line_offsets, nlines, outoff);
4090 if (err)
4091 goto done;
4093 author = got_object_commit_get_author(commit);
4094 committer = got_object_commit_get_committer(commit);
4095 if (strcmp(author, committer) != 0) {
4096 n = fprintf(outfile, "via: %s\n", committer);
4097 if (n < 0) {
4098 err = got_error_from_errno("fprintf");
4099 goto done;
4101 outoff += n;
4102 err = add_line_offset(line_offsets, nlines, outoff);
4103 if (err)
4104 goto done;
4106 if (got_object_commit_get_nparents(commit) > 1) {
4107 const struct got_object_id_queue *parent_ids;
4108 struct got_object_qid *qid;
4109 int pn = 1;
4110 parent_ids = got_object_commit_get_parent_ids(commit);
4111 STAILQ_FOREACH(qid, parent_ids, entry) {
4112 err = got_object_id_str(&id_str, &qid->id);
4113 if (err)
4114 goto done;
4115 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4116 if (n < 0) {
4117 err = got_error_from_errno("fprintf");
4118 goto done;
4120 outoff += n;
4121 err = add_line_offset(line_offsets, nlines, outoff);
4122 if (err)
4123 goto done;
4124 free(id_str);
4125 id_str = NULL;
4129 err = got_object_commit_get_logmsg(&logmsg, commit);
4130 if (err)
4131 goto done;
4132 s = logmsg;
4133 while ((line = strsep(&s, "\n")) != NULL) {
4134 n = fprintf(outfile, "%s\n", line);
4135 if (n < 0) {
4136 err = got_error_from_errno("fprintf");
4137 goto done;
4139 outoff += n;
4140 err = add_line_offset(line_offsets, nlines, outoff);
4141 if (err)
4142 goto done;
4145 err = get_changed_paths(&changed_paths, commit, repo);
4146 if (err)
4147 goto done;
4148 TAILQ_FOREACH(pe, &changed_paths, entry) {
4149 struct got_diff_changed_path *cp = pe->data;
4150 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4151 if (n < 0) {
4152 err = got_error_from_errno("fprintf");
4153 goto done;
4155 outoff += n;
4156 err = add_line_offset(line_offsets, nlines, outoff);
4157 if (err)
4158 goto done;
4159 free((char *)pe->path);
4160 free(pe->data);
4163 fputc('\n', outfile);
4164 outoff++;
4165 err = add_line_offset(line_offsets, nlines, outoff);
4166 done:
4167 got_pathlist_free(&changed_paths);
4168 free(id_str);
4169 free(logmsg);
4170 free(refs_str);
4171 got_object_commit_close(commit);
4172 if (err) {
4173 free(*line_offsets);
4174 *line_offsets = NULL;
4175 *nlines = 0;
4177 return err;
4180 static const struct got_error *
4181 create_diff(struct tog_diff_view_state *s)
4183 const struct got_error *err = NULL;
4184 FILE *f = NULL;
4185 int obj_type;
4187 free(s->line_offsets);
4188 s->line_offsets = malloc(sizeof(off_t));
4189 if (s->line_offsets == NULL)
4190 return got_error_from_errno("malloc");
4191 s->nlines = 0;
4193 f = got_opentemp();
4194 if (f == NULL) {
4195 err = got_error_from_errno("got_opentemp");
4196 goto done;
4198 if (s->f && fclose(s->f) == EOF) {
4199 err = got_error_from_errno("fclose");
4200 goto done;
4202 s->f = f;
4204 if (s->id1)
4205 err = got_object_get_type(&obj_type, s->repo, s->id1);
4206 else
4207 err = got_object_get_type(&obj_type, s->repo, s->id2);
4208 if (err)
4209 goto done;
4211 switch (obj_type) {
4212 case GOT_OBJ_TYPE_BLOB:
4213 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4214 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4215 s->label1, s->label2, tog_diff_algo, s->diff_context,
4216 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4217 break;
4218 case GOT_OBJ_TYPE_TREE:
4219 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4220 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4221 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4222 s->force_text_diff, s->repo, s->f);
4223 break;
4224 case GOT_OBJ_TYPE_COMMIT: {
4225 const struct got_object_id_queue *parent_ids;
4226 struct got_object_qid *pid;
4227 struct got_commit_object *commit2;
4228 struct got_reflist_head *refs;
4230 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4231 if (err)
4232 goto done;
4233 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4234 /* Show commit info if we're diffing to a parent/root commit. */
4235 if (s->id1 == NULL) {
4236 err = write_commit_info(&s->line_offsets, &s->nlines,
4237 s->id2, refs, s->repo, s->f);
4238 if (err)
4239 goto done;
4240 } else {
4241 parent_ids = got_object_commit_get_parent_ids(commit2);
4242 STAILQ_FOREACH(pid, parent_ids, entry) {
4243 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4244 err = write_commit_info(
4245 &s->line_offsets, &s->nlines,
4246 s->id2, refs, s->repo, s->f);
4247 if (err)
4248 goto done;
4249 break;
4253 got_object_commit_close(commit2);
4255 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4256 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4257 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4258 s->force_text_diff, s->repo, s->f);
4259 break;
4261 default:
4262 err = got_error(GOT_ERR_OBJ_TYPE);
4263 break;
4265 if (err)
4266 goto done;
4267 done:
4268 if (s->f && fflush(s->f) != 0 && err == NULL)
4269 err = got_error_from_errno("fflush");
4270 return err;
4273 static void
4274 diff_view_indicate_progress(struct tog_view *view)
4276 mvwaddstr(view->window, 0, 0, "diffing...");
4277 update_panels();
4278 doupdate();
4281 static const struct got_error *
4282 search_start_diff_view(struct tog_view *view)
4284 struct tog_diff_view_state *s = &view->state.diff;
4286 s->matched_line = 0;
4287 return NULL;
4290 static const struct got_error *
4291 search_next_diff_view(struct tog_view *view)
4293 struct tog_diff_view_state *s = &view->state.diff;
4294 const struct got_error *err = NULL;
4295 int lineno;
4296 char *line = NULL;
4297 size_t linesize = 0;
4298 ssize_t linelen;
4300 if (!view->searching) {
4301 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4302 return NULL;
4305 if (s->matched_line) {
4306 if (view->searching == TOG_SEARCH_FORWARD)
4307 lineno = s->matched_line + 1;
4308 else
4309 lineno = s->matched_line - 1;
4310 } else
4311 lineno = s->first_displayed_line;
4313 while (1) {
4314 off_t offset;
4316 if (lineno <= 0 || lineno > s->nlines) {
4317 if (s->matched_line == 0) {
4318 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4319 break;
4322 if (view->searching == TOG_SEARCH_FORWARD)
4323 lineno = 1;
4324 else
4325 lineno = s->nlines;
4328 offset = s->line_offsets[lineno - 1];
4329 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4330 free(line);
4331 return got_error_from_errno("fseeko");
4333 linelen = getline(&line, &linesize, s->f);
4334 if (linelen != -1) {
4335 char *exstr;
4336 err = expand_tab(&exstr, line);
4337 if (err)
4338 break;
4339 if (match_line(exstr, &view->regex, 1,
4340 &view->regmatch)) {
4341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4342 s->matched_line = lineno;
4343 free(exstr);
4344 break;
4346 free(exstr);
4348 if (view->searching == TOG_SEARCH_FORWARD)
4349 lineno++;
4350 else
4351 lineno--;
4353 free(line);
4355 if (s->matched_line) {
4356 s->first_displayed_line = s->matched_line;
4357 s->selected_line = 1;
4360 return err;
4363 static const struct got_error *
4364 close_diff_view(struct tog_view *view)
4366 const struct got_error *err = NULL;
4367 struct tog_diff_view_state *s = &view->state.diff;
4369 free(s->id1);
4370 s->id1 = NULL;
4371 free(s->id2);
4372 s->id2 = NULL;
4373 if (s->f && fclose(s->f) == EOF)
4374 err = got_error_from_errno("fclose");
4375 s->f = NULL;
4376 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4377 err = got_error_from_errno("fclose");
4378 s->f1 = NULL;
4379 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4380 err = got_error_from_errno("fclose");
4381 s->f2 = NULL;
4382 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4383 err = got_error_from_errno("close");
4384 s->fd1 = -1;
4385 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4386 err = got_error_from_errno("close");
4387 s->fd2 = -1;
4388 free_colors(&s->colors);
4389 free(s->line_offsets);
4390 s->line_offsets = NULL;
4391 s->nlines = 0;
4392 return err;
4395 static const struct got_error *
4396 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4397 struct got_object_id *id2, const char *label1, const char *label2,
4398 int diff_context, int ignore_whitespace, int force_text_diff,
4399 struct tog_view *log_view, struct got_repository *repo)
4401 const struct got_error *err;
4402 struct tog_diff_view_state *s = &view->state.diff;
4404 memset(s, 0, sizeof(*s));
4405 s->fd1 = -1;
4406 s->fd2 = -1;
4408 if (id1 != NULL && id2 != NULL) {
4409 int type1, type2;
4410 err = got_object_get_type(&type1, repo, id1);
4411 if (err)
4412 return err;
4413 err = got_object_get_type(&type2, repo, id2);
4414 if (err)
4415 return err;
4417 if (type1 != type2)
4418 return got_error(GOT_ERR_OBJ_TYPE);
4420 s->first_displayed_line = 1;
4421 s->last_displayed_line = view->nlines;
4422 s->selected_line = 1;
4423 s->repo = repo;
4424 s->id1 = id1;
4425 s->id2 = id2;
4426 s->label1 = label1;
4427 s->label2 = label2;
4429 if (id1) {
4430 s->id1 = got_object_id_dup(id1);
4431 if (s->id1 == NULL)
4432 return got_error_from_errno("got_object_id_dup");
4433 } else
4434 s->id1 = NULL;
4436 s->id2 = got_object_id_dup(id2);
4437 if (s->id2 == NULL) {
4438 err = got_error_from_errno("got_object_id_dup");
4439 goto done;
4442 s->f1 = got_opentemp();
4443 if (s->f1 == NULL) {
4444 err = got_error_from_errno("got_opentemp");
4445 goto done;
4448 s->f2 = got_opentemp();
4449 if (s->f2 == NULL) {
4450 err = got_error_from_errno("got_opentemp");
4451 goto done;
4454 s->fd1 = got_opentempfd();
4455 if (s->fd1 == -1) {
4456 err = got_error_from_errno("got_opentempfd");
4457 goto done;
4460 s->fd2 = got_opentempfd();
4461 if (s->fd2 == -1) {
4462 err = got_error_from_errno("got_opentempfd");
4463 goto done;
4466 s->first_displayed_line = 1;
4467 s->last_displayed_line = view->nlines;
4468 s->diff_context = diff_context;
4469 s->ignore_whitespace = ignore_whitespace;
4470 s->force_text_diff = force_text_diff;
4471 s->log_view = log_view;
4472 s->repo = repo;
4474 STAILQ_INIT(&s->colors);
4475 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4476 err = add_color(&s->colors,
4477 "^-", TOG_COLOR_DIFF_MINUS,
4478 get_color_value("TOG_COLOR_DIFF_MINUS"));
4479 if (err)
4480 goto done;
4481 err = add_color(&s->colors, "^\\+",
4482 TOG_COLOR_DIFF_PLUS,
4483 get_color_value("TOG_COLOR_DIFF_PLUS"));
4484 if (err)
4485 goto done;
4486 err = add_color(&s->colors,
4487 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4488 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4489 if (err)
4490 goto done;
4492 err = add_color(&s->colors,
4493 "^(commit [0-9a-f]|parent [0-9]|"
4494 "(blob|file|tree|commit) [-+] |"
4495 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4496 get_color_value("TOG_COLOR_DIFF_META"));
4497 if (err)
4498 goto done;
4500 err = add_color(&s->colors,
4501 "^(from|via): ", TOG_COLOR_AUTHOR,
4502 get_color_value("TOG_COLOR_AUTHOR"));
4503 if (err)
4504 goto done;
4506 err = add_color(&s->colors,
4507 "^date: ", TOG_COLOR_DATE,
4508 get_color_value("TOG_COLOR_DATE"));
4509 if (err)
4510 goto done;
4513 if (log_view && view_is_splitscreen(view))
4514 show_log_view(log_view); /* draw vborder */
4515 diff_view_indicate_progress(view);
4517 err = create_diff(s);
4519 view->show = show_diff_view;
4520 view->input = input_diff_view;
4521 view->reset = reset_diff_view;
4522 view->close = close_diff_view;
4523 view->search_start = search_start_diff_view;
4524 view->search_next = search_next_diff_view;
4525 done:
4526 if (err)
4527 close_diff_view(view);
4528 return err;
4531 static const struct got_error *
4532 show_diff_view(struct tog_view *view)
4534 const struct got_error *err;
4535 struct tog_diff_view_state *s = &view->state.diff;
4536 char *id_str1 = NULL, *id_str2, *header;
4537 const char *label1, *label2;
4539 if (s->id1) {
4540 err = got_object_id_str(&id_str1, s->id1);
4541 if (err)
4542 return err;
4543 label1 = s->label1 ? : id_str1;
4544 } else
4545 label1 = "/dev/null";
4547 err = got_object_id_str(&id_str2, s->id2);
4548 if (err)
4549 return err;
4550 label2 = s->label2 ? : id_str2;
4552 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4553 err = got_error_from_errno("asprintf");
4554 free(id_str1);
4555 free(id_str2);
4556 return err;
4558 free(id_str1);
4559 free(id_str2);
4561 err = draw_file(view, header);
4562 free(header);
4563 return err;
4566 static const struct got_error *
4567 set_selected_commit(struct tog_diff_view_state *s,
4568 struct commit_queue_entry *entry)
4570 const struct got_error *err;
4571 const struct got_object_id_queue *parent_ids;
4572 struct got_commit_object *selected_commit;
4573 struct got_object_qid *pid;
4575 free(s->id2);
4576 s->id2 = got_object_id_dup(entry->id);
4577 if (s->id2 == NULL)
4578 return got_error_from_errno("got_object_id_dup");
4580 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4581 if (err)
4582 return err;
4583 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4584 free(s->id1);
4585 pid = STAILQ_FIRST(parent_ids);
4586 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4587 got_object_commit_close(selected_commit);
4588 return NULL;
4591 static const struct got_error *
4592 reset_diff_view(struct tog_view *view)
4594 struct tog_diff_view_state *s = &view->state.diff;
4596 view->count = 0;
4597 wclear(view->window);
4598 s->first_displayed_line = 1;
4599 s->last_displayed_line = view->nlines;
4600 s->matched_line = 0;
4601 diff_view_indicate_progress(view);
4602 return create_diff(s);
4605 static const struct got_error *
4606 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4608 const struct got_error *err = NULL;
4609 struct tog_diff_view_state *s = &view->state.diff;
4610 struct tog_log_view_state *ls;
4611 struct commit_queue_entry *old_selected_entry;
4612 char *line = NULL;
4613 size_t linesize = 0;
4614 ssize_t linelen;
4615 int i, nscroll = view->nlines - 1;
4617 switch (ch) {
4618 case '0':
4619 view->x = 0;
4620 break;
4621 case '$':
4622 view->x = MAX(view->maxx - view->ncols / 3, 0);
4623 view->count = 0;
4624 break;
4625 case KEY_RIGHT:
4626 case 'l':
4627 if (view->x + view->ncols / 3 < view->maxx)
4628 view->x += 2; /* move two columns right */
4629 else
4630 view->count = 0;
4631 break;
4632 case KEY_LEFT:
4633 case 'h':
4634 view->x -= MIN(view->x, 2); /* move two columns back */
4635 if (view->x <= 0)
4636 view->count = 0;
4637 break;
4638 case 'a':
4639 case 'w':
4640 if (ch == 'a')
4641 s->force_text_diff = !s->force_text_diff;
4642 if (ch == 'w')
4643 s->ignore_whitespace = !s->ignore_whitespace;
4644 err = reset_diff_view(view);
4645 break;
4646 case 'g':
4647 case KEY_HOME:
4648 s->first_displayed_line = 1;
4649 view->count = 0;
4650 break;
4651 case 'G':
4652 case KEY_END:
4653 view->count = 0;
4654 if (s->eof)
4655 break;
4657 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4658 s->eof = 1;
4659 break;
4660 case 'k':
4661 case KEY_UP:
4662 case CTRL('p'):
4663 if (s->first_displayed_line > 1)
4664 s->first_displayed_line--;
4665 else
4666 view->count = 0;
4667 break;
4668 case CTRL('u'):
4669 case 'u':
4670 nscroll /= 2;
4671 /* FALL THROUGH */
4672 case KEY_PPAGE:
4673 case CTRL('b'):
4674 case 'b':
4675 if (s->first_displayed_line == 1) {
4676 view->count = 0;
4677 break;
4679 i = 0;
4680 while (i++ < nscroll && s->first_displayed_line > 1)
4681 s->first_displayed_line--;
4682 break;
4683 case 'j':
4684 case KEY_DOWN:
4685 case CTRL('n'):
4686 if (!s->eof)
4687 s->first_displayed_line++;
4688 else
4689 view->count = 0;
4690 break;
4691 case CTRL('d'):
4692 case 'd':
4693 nscroll /= 2;
4694 /* FALL THROUGH */
4695 case KEY_NPAGE:
4696 case CTRL('f'):
4697 case 'f':
4698 case ' ':
4699 if (s->eof) {
4700 view->count = 0;
4701 break;
4703 i = 0;
4704 while (!s->eof && i++ < nscroll) {
4705 linelen = getline(&line, &linesize, s->f);
4706 s->first_displayed_line++;
4707 if (linelen == -1) {
4708 if (feof(s->f)) {
4709 s->eof = 1;
4710 } else
4711 err = got_ferror(s->f, GOT_ERR_IO);
4712 break;
4715 free(line);
4716 break;
4717 case '[':
4718 if (s->diff_context > 0) {
4719 s->diff_context--;
4720 s->matched_line = 0;
4721 diff_view_indicate_progress(view);
4722 err = create_diff(s);
4723 if (s->first_displayed_line + view->nlines - 1 >
4724 s->nlines) {
4725 s->first_displayed_line = 1;
4726 s->last_displayed_line = view->nlines;
4728 } else
4729 view->count = 0;
4730 break;
4731 case ']':
4732 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4733 s->diff_context++;
4734 s->matched_line = 0;
4735 diff_view_indicate_progress(view);
4736 err = create_diff(s);
4737 } else
4738 view->count = 0;
4739 break;
4740 case '<':
4741 case ',':
4742 if (s->log_view == NULL) {
4743 view->count = 0;
4744 break;
4746 ls = &s->log_view->state.log;
4747 old_selected_entry = ls->selected_entry;
4749 /* view->count handled in input_log_view() */
4750 err = input_log_view(NULL, s->log_view, KEY_UP);
4751 if (err)
4752 break;
4754 if (old_selected_entry == ls->selected_entry)
4755 break;
4757 err = set_selected_commit(s, ls->selected_entry);
4758 if (err)
4759 break;
4761 s->first_displayed_line = 1;
4762 s->last_displayed_line = view->nlines;
4763 s->matched_line = 0;
4764 view->x = 0;
4766 diff_view_indicate_progress(view);
4767 err = create_diff(s);
4768 break;
4769 case '>':
4770 case '.':
4771 if (s->log_view == NULL) {
4772 view->count = 0;
4773 break;
4775 ls = &s->log_view->state.log;
4776 old_selected_entry = ls->selected_entry;
4778 /* view->count handled in input_log_view() */
4779 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4780 if (err)
4781 break;
4783 if (old_selected_entry == ls->selected_entry)
4784 break;
4786 err = set_selected_commit(s, ls->selected_entry);
4787 if (err)
4788 break;
4790 s->first_displayed_line = 1;
4791 s->last_displayed_line = view->nlines;
4792 s->matched_line = 0;
4793 view->x = 0;
4795 diff_view_indicate_progress(view);
4796 err = create_diff(s);
4797 break;
4798 default:
4799 view->count = 0;
4800 break;
4803 return err;
4806 static const struct got_error *
4807 cmd_diff(int argc, char *argv[])
4809 const struct got_error *error = NULL;
4810 struct got_repository *repo = NULL;
4811 struct got_worktree *worktree = NULL;
4812 struct got_object_id *id1 = NULL, *id2 = NULL;
4813 char *repo_path = NULL, *cwd = NULL;
4814 char *id_str1 = NULL, *id_str2 = NULL;
4815 char *label1 = NULL, *label2 = NULL;
4816 int diff_context = 3, ignore_whitespace = 0;
4817 int ch, force_text_diff = 0;
4818 const char *errstr;
4819 struct tog_view *view;
4820 int *pack_fds = NULL;
4822 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4823 switch (ch) {
4824 case 'a':
4825 force_text_diff = 1;
4826 break;
4827 case 'C':
4828 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4829 &errstr);
4830 if (errstr != NULL)
4831 errx(1, "number of context lines is %s: %s",
4832 errstr, errstr);
4833 break;
4834 case 'r':
4835 repo_path = realpath(optarg, NULL);
4836 if (repo_path == NULL)
4837 return got_error_from_errno2("realpath",
4838 optarg);
4839 got_path_strip_trailing_slashes(repo_path);
4840 break;
4841 case 'w':
4842 ignore_whitespace = 1;
4843 break;
4844 default:
4845 usage_diff();
4846 /* NOTREACHED */
4850 argc -= optind;
4851 argv += optind;
4853 if (argc == 0) {
4854 usage_diff(); /* TODO show local worktree changes */
4855 } else if (argc == 2) {
4856 id_str1 = argv[0];
4857 id_str2 = argv[1];
4858 } else
4859 usage_diff();
4861 error = got_repo_pack_fds_open(&pack_fds);
4862 if (error)
4863 goto done;
4865 if (repo_path == NULL) {
4866 cwd = getcwd(NULL, 0);
4867 if (cwd == NULL)
4868 return got_error_from_errno("getcwd");
4869 error = got_worktree_open(&worktree, cwd);
4870 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4871 goto done;
4872 if (worktree)
4873 repo_path =
4874 strdup(got_worktree_get_repo_path(worktree));
4875 else
4876 repo_path = strdup(cwd);
4877 if (repo_path == NULL) {
4878 error = got_error_from_errno("strdup");
4879 goto done;
4883 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4884 if (error)
4885 goto done;
4887 init_curses();
4889 error = apply_unveil(got_repo_get_path(repo), NULL);
4890 if (error)
4891 goto done;
4893 error = tog_load_refs(repo, 0);
4894 if (error)
4895 goto done;
4897 error = got_repo_match_object_id(&id1, &label1, id_str1,
4898 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4899 if (error)
4900 goto done;
4902 error = got_repo_match_object_id(&id2, &label2, id_str2,
4903 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4904 if (error)
4905 goto done;
4907 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4908 if (view == NULL) {
4909 error = got_error_from_errno("view_open");
4910 goto done;
4912 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4913 ignore_whitespace, force_text_diff, NULL, repo);
4914 if (error)
4915 goto done;
4916 error = view_loop(view);
4917 done:
4918 free(label1);
4919 free(label2);
4920 free(repo_path);
4921 free(cwd);
4922 if (repo) {
4923 const struct got_error *close_err = got_repo_close(repo);
4924 if (error == NULL)
4925 error = close_err;
4927 if (worktree)
4928 got_worktree_close(worktree);
4929 if (pack_fds) {
4930 const struct got_error *pack_err =
4931 got_repo_pack_fds_close(pack_fds);
4932 if (error == NULL)
4933 error = pack_err;
4935 tog_free_refs();
4936 return error;
4939 __dead static void
4940 usage_blame(void)
4942 endwin();
4943 fprintf(stderr,
4944 "usage: %s blame [-c commit] [-r repository-path] path\n",
4945 getprogname());
4946 exit(1);
4949 struct tog_blame_line {
4950 int annotated;
4951 struct got_object_id *id;
4954 static const struct got_error *
4955 draw_blame(struct tog_view *view)
4957 struct tog_blame_view_state *s = &view->state.blame;
4958 struct tog_blame *blame = &s->blame;
4959 regmatch_t *regmatch = &view->regmatch;
4960 const struct got_error *err;
4961 int lineno = 0, nprinted = 0;
4962 char *line = NULL;
4963 size_t linesize = 0;
4964 ssize_t linelen;
4965 wchar_t *wline;
4966 int width;
4967 struct tog_blame_line *blame_line;
4968 struct got_object_id *prev_id = NULL;
4969 char *id_str;
4970 struct tog_color *tc;
4972 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4973 if (err)
4974 return err;
4976 rewind(blame->f);
4977 werase(view->window);
4979 if (asprintf(&line, "commit %s", id_str) == -1) {
4980 err = got_error_from_errno("asprintf");
4981 free(id_str);
4982 return err;
4985 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4986 free(line);
4987 line = NULL;
4988 if (err)
4989 return err;
4990 if (view_needs_focus_indication(view))
4991 wstandout(view->window);
4992 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4993 if (tc)
4994 wattr_on(view->window,
4995 COLOR_PAIR(tc->colorpair), NULL);
4996 waddwstr(view->window, wline);
4997 if (tc)
4998 wattr_off(view->window,
4999 COLOR_PAIR(tc->colorpair), NULL);
5000 if (view_needs_focus_indication(view))
5001 wstandend(view->window);
5002 free(wline);
5003 wline = NULL;
5004 if (width < view->ncols - 1)
5005 waddch(view->window, '\n');
5007 if (asprintf(&line, "[%d/%d] %s%s",
5008 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5009 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5010 free(id_str);
5011 return got_error_from_errno("asprintf");
5013 free(id_str);
5014 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5015 free(line);
5016 line = NULL;
5017 if (err)
5018 return err;
5019 waddwstr(view->window, wline);
5020 free(wline);
5021 wline = NULL;
5022 if (width < view->ncols - 1)
5023 waddch(view->window, '\n');
5025 s->eof = 0;
5026 view->maxx = 0;
5027 while (nprinted < view->nlines - 2) {
5028 linelen = getline(&line, &linesize, blame->f);
5029 if (linelen == -1) {
5030 if (feof(blame->f)) {
5031 s->eof = 1;
5032 break;
5034 free(line);
5035 return got_ferror(blame->f, GOT_ERR_IO);
5037 if (++lineno < s->first_displayed_line)
5038 continue;
5040 /* Set view->maxx based on full line length. */
5041 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5042 if (err) {
5043 free(line);
5044 return err;
5046 free(wline);
5047 wline = NULL;
5048 view->maxx = MAX(view->maxx, width);
5050 if (view->focussed && nprinted == s->selected_line - 1)
5051 wstandout(view->window);
5053 if (blame->nlines > 0) {
5054 blame_line = &blame->lines[lineno - 1];
5055 if (blame_line->annotated && prev_id &&
5056 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5057 !(view->focussed &&
5058 nprinted == s->selected_line - 1)) {
5059 waddstr(view->window, " ");
5060 } else if (blame_line->annotated) {
5061 char *id_str;
5062 err = got_object_id_str(&id_str,
5063 blame_line->id);
5064 if (err) {
5065 free(line);
5066 return err;
5068 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5069 if (tc)
5070 wattr_on(view->window,
5071 COLOR_PAIR(tc->colorpair), NULL);
5072 wprintw(view->window, "%.8s", id_str);
5073 if (tc)
5074 wattr_off(view->window,
5075 COLOR_PAIR(tc->colorpair), NULL);
5076 free(id_str);
5077 prev_id = blame_line->id;
5078 } else {
5079 waddstr(view->window, "........");
5080 prev_id = NULL;
5082 } else {
5083 waddstr(view->window, "........");
5084 prev_id = NULL;
5087 if (view->focussed && nprinted == s->selected_line - 1)
5088 wstandend(view->window);
5089 waddstr(view->window, " ");
5091 if (view->ncols <= 9) {
5092 width = 9;
5093 } else if (s->first_displayed_line + nprinted ==
5094 s->matched_line &&
5095 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5096 err = add_matched_line(&width, line, view->ncols - 9, 9,
5097 view->window, view->x, regmatch);
5098 if (err) {
5099 free(line);
5100 return err;
5102 width += 9;
5103 } else {
5104 int skip;
5105 err = format_line(&wline, &width, &skip, line,
5106 view->x, view->ncols - 9, 9, 1);
5107 if (err) {
5108 free(line);
5109 return err;
5111 waddwstr(view->window, &wline[skip]);
5112 width += 9;
5113 free(wline);
5114 wline = NULL;
5117 if (width <= view->ncols - 1)
5118 waddch(view->window, '\n');
5119 if (++nprinted == 1)
5120 s->first_displayed_line = lineno;
5122 free(line);
5123 s->last_displayed_line = lineno;
5125 view_border(view);
5127 return NULL;
5130 static const struct got_error *
5131 blame_cb(void *arg, int nlines, int lineno,
5132 struct got_commit_object *commit, struct got_object_id *id)
5134 const struct got_error *err = NULL;
5135 struct tog_blame_cb_args *a = arg;
5136 struct tog_blame_line *line;
5137 int errcode;
5139 if (nlines != a->nlines ||
5140 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5141 return got_error(GOT_ERR_RANGE);
5143 errcode = pthread_mutex_lock(&tog_mutex);
5144 if (errcode)
5145 return got_error_set_errno(errcode, "pthread_mutex_lock");
5147 if (*a->quit) { /* user has quit the blame view */
5148 err = got_error(GOT_ERR_ITER_COMPLETED);
5149 goto done;
5152 if (lineno == -1)
5153 goto done; /* no change in this commit */
5155 line = &a->lines[lineno - 1];
5156 if (line->annotated)
5157 goto done;
5159 line->id = got_object_id_dup(id);
5160 if (line->id == NULL) {
5161 err = got_error_from_errno("got_object_id_dup");
5162 goto done;
5164 line->annotated = 1;
5165 done:
5166 errcode = pthread_mutex_unlock(&tog_mutex);
5167 if (errcode)
5168 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5169 return err;
5172 static void *
5173 blame_thread(void *arg)
5175 const struct got_error *err, *close_err;
5176 struct tog_blame_thread_args *ta = arg;
5177 struct tog_blame_cb_args *a = ta->cb_args;
5178 int errcode, fd1 = -1, fd2 = -1;
5179 FILE *f1 = NULL, *f2 = NULL;
5181 fd1 = got_opentempfd();
5182 if (fd1 == -1)
5183 return (void *)got_error_from_errno("got_opentempfd");
5185 fd2 = got_opentempfd();
5186 if (fd2 == -1) {
5187 err = got_error_from_errno("got_opentempfd");
5188 goto done;
5191 f1 = got_opentemp();
5192 if (f1 == NULL) {
5193 err = (void *)got_error_from_errno("got_opentemp");
5194 goto done;
5196 f2 = got_opentemp();
5197 if (f2 == NULL) {
5198 err = (void *)got_error_from_errno("got_opentemp");
5199 goto done;
5202 err = block_signals_used_by_main_thread();
5203 if (err)
5204 goto done;
5206 err = got_blame(ta->path, a->commit_id, ta->repo,
5207 tog_diff_algo, blame_cb, ta->cb_args,
5208 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5209 if (err && err->code == GOT_ERR_CANCELLED)
5210 err = NULL;
5212 errcode = pthread_mutex_lock(&tog_mutex);
5213 if (errcode) {
5214 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5215 goto done;
5218 close_err = got_repo_close(ta->repo);
5219 if (err == NULL)
5220 err = close_err;
5221 ta->repo = NULL;
5222 *ta->complete = 1;
5224 errcode = pthread_mutex_unlock(&tog_mutex);
5225 if (errcode && err == NULL)
5226 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5228 done:
5229 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5230 err = got_error_from_errno("close");
5231 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5232 err = got_error_from_errno("close");
5233 if (f1 && fclose(f1) == EOF && err == NULL)
5234 err = got_error_from_errno("fclose");
5235 if (f2 && fclose(f2) == EOF && err == NULL)
5236 err = got_error_from_errno("fclose");
5238 return (void *)err;
5241 static struct got_object_id *
5242 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5243 int first_displayed_line, int selected_line)
5245 struct tog_blame_line *line;
5247 if (nlines <= 0)
5248 return NULL;
5250 line = &lines[first_displayed_line - 1 + selected_line - 1];
5251 if (!line->annotated)
5252 return NULL;
5254 return line->id;
5257 static const struct got_error *
5258 stop_blame(struct tog_blame *blame)
5260 const struct got_error *err = NULL;
5261 int i;
5263 if (blame->thread) {
5264 int errcode;
5265 errcode = pthread_mutex_unlock(&tog_mutex);
5266 if (errcode)
5267 return got_error_set_errno(errcode,
5268 "pthread_mutex_unlock");
5269 errcode = pthread_join(blame->thread, (void **)&err);
5270 if (errcode)
5271 return got_error_set_errno(errcode, "pthread_join");
5272 errcode = pthread_mutex_lock(&tog_mutex);
5273 if (errcode)
5274 return got_error_set_errno(errcode,
5275 "pthread_mutex_lock");
5276 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5277 err = NULL;
5278 blame->thread = 0; //NULL;
5280 if (blame->thread_args.repo) {
5281 const struct got_error *close_err;
5282 close_err = got_repo_close(blame->thread_args.repo);
5283 if (err == NULL)
5284 err = close_err;
5285 blame->thread_args.repo = NULL;
5287 if (blame->f) {
5288 if (fclose(blame->f) == EOF && err == NULL)
5289 err = got_error_from_errno("fclose");
5290 blame->f = NULL;
5292 if (blame->lines) {
5293 for (i = 0; i < blame->nlines; i++)
5294 free(blame->lines[i].id);
5295 free(blame->lines);
5296 blame->lines = NULL;
5298 free(blame->cb_args.commit_id);
5299 blame->cb_args.commit_id = NULL;
5300 if (blame->pack_fds) {
5301 const struct got_error *pack_err =
5302 got_repo_pack_fds_close(blame->pack_fds);
5303 if (err == NULL)
5304 err = pack_err;
5305 blame->pack_fds = NULL;
5307 return err;
5310 static const struct got_error *
5311 cancel_blame_view(void *arg)
5313 const struct got_error *err = NULL;
5314 int *done = arg;
5315 int errcode;
5317 errcode = pthread_mutex_lock(&tog_mutex);
5318 if (errcode)
5319 return got_error_set_errno(errcode,
5320 "pthread_mutex_unlock");
5322 if (*done)
5323 err = got_error(GOT_ERR_CANCELLED);
5325 errcode = pthread_mutex_unlock(&tog_mutex);
5326 if (errcode)
5327 return got_error_set_errno(errcode,
5328 "pthread_mutex_lock");
5330 return err;
5333 static const struct got_error *
5334 run_blame(struct tog_view *view)
5336 struct tog_blame_view_state *s = &view->state.blame;
5337 struct tog_blame *blame = &s->blame;
5338 const struct got_error *err = NULL;
5339 struct got_commit_object *commit = NULL;
5340 struct got_blob_object *blob = NULL;
5341 struct got_repository *thread_repo = NULL;
5342 struct got_object_id *obj_id = NULL;
5343 int obj_type, fd = -1;
5344 int *pack_fds = NULL;
5346 err = got_object_open_as_commit(&commit, s->repo,
5347 &s->blamed_commit->id);
5348 if (err)
5349 return err;
5351 fd = got_opentempfd();
5352 if (fd == -1) {
5353 err = got_error_from_errno("got_opentempfd");
5354 goto done;
5357 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5358 if (err)
5359 goto done;
5361 err = got_object_get_type(&obj_type, s->repo, obj_id);
5362 if (err)
5363 goto done;
5365 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5366 err = got_error(GOT_ERR_OBJ_TYPE);
5367 goto done;
5370 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5371 if (err)
5372 goto done;
5373 blame->f = got_opentemp();
5374 if (blame->f == NULL) {
5375 err = got_error_from_errno("got_opentemp");
5376 goto done;
5378 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5379 &blame->line_offsets, blame->f, blob);
5380 if (err)
5381 goto done;
5382 if (blame->nlines == 0) {
5383 s->blame_complete = 1;
5384 goto done;
5387 /* Don't include \n at EOF in the blame line count. */
5388 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5389 blame->nlines--;
5391 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5392 if (blame->lines == NULL) {
5393 err = got_error_from_errno("calloc");
5394 goto done;
5397 err = got_repo_pack_fds_open(&pack_fds);
5398 if (err)
5399 goto done;
5400 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5401 pack_fds);
5402 if (err)
5403 goto done;
5405 blame->pack_fds = pack_fds;
5406 blame->cb_args.view = view;
5407 blame->cb_args.lines = blame->lines;
5408 blame->cb_args.nlines = blame->nlines;
5409 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5410 if (blame->cb_args.commit_id == NULL) {
5411 err = got_error_from_errno("got_object_id_dup");
5412 goto done;
5414 blame->cb_args.quit = &s->done;
5416 blame->thread_args.path = s->path;
5417 blame->thread_args.repo = thread_repo;
5418 blame->thread_args.cb_args = &blame->cb_args;
5419 blame->thread_args.complete = &s->blame_complete;
5420 blame->thread_args.cancel_cb = cancel_blame_view;
5421 blame->thread_args.cancel_arg = &s->done;
5422 s->blame_complete = 0;
5424 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5425 s->first_displayed_line = 1;
5426 s->last_displayed_line = view->nlines;
5427 s->selected_line = 1;
5429 s->matched_line = 0;
5431 done:
5432 if (commit)
5433 got_object_commit_close(commit);
5434 if (fd != -1 && close(fd) == -1 && err == NULL)
5435 err = got_error_from_errno("close");
5436 if (blob)
5437 got_object_blob_close(blob);
5438 free(obj_id);
5439 if (err)
5440 stop_blame(blame);
5441 return err;
5444 static const struct got_error *
5445 open_blame_view(struct tog_view *view, char *path,
5446 struct got_object_id *commit_id, struct got_repository *repo)
5448 const struct got_error *err = NULL;
5449 struct tog_blame_view_state *s = &view->state.blame;
5451 STAILQ_INIT(&s->blamed_commits);
5453 s->path = strdup(path);
5454 if (s->path == NULL)
5455 return got_error_from_errno("strdup");
5457 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5458 if (err) {
5459 free(s->path);
5460 return err;
5463 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5464 s->first_displayed_line = 1;
5465 s->last_displayed_line = view->nlines;
5466 s->selected_line = 1;
5467 s->blame_complete = 0;
5468 s->repo = repo;
5469 s->commit_id = commit_id;
5470 memset(&s->blame, 0, sizeof(s->blame));
5472 STAILQ_INIT(&s->colors);
5473 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5474 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5475 get_color_value("TOG_COLOR_COMMIT"));
5476 if (err)
5477 return err;
5480 view->show = show_blame_view;
5481 view->input = input_blame_view;
5482 view->reset = reset_blame_view;
5483 view->close = close_blame_view;
5484 view->search_start = search_start_blame_view;
5485 view->search_next = search_next_blame_view;
5487 return run_blame(view);
5490 static const struct got_error *
5491 close_blame_view(struct tog_view *view)
5493 const struct got_error *err = NULL;
5494 struct tog_blame_view_state *s = &view->state.blame;
5496 if (s->blame.thread)
5497 err = stop_blame(&s->blame);
5499 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5500 struct got_object_qid *blamed_commit;
5501 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5502 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5503 got_object_qid_free(blamed_commit);
5506 free(s->path);
5507 free_colors(&s->colors);
5508 return err;
5511 static const struct got_error *
5512 search_start_blame_view(struct tog_view *view)
5514 struct tog_blame_view_state *s = &view->state.blame;
5516 s->matched_line = 0;
5517 return NULL;
5520 static const struct got_error *
5521 search_next_blame_view(struct tog_view *view)
5523 struct tog_blame_view_state *s = &view->state.blame;
5524 const struct got_error *err = NULL;
5525 int lineno;
5526 char *line = NULL;
5527 size_t linesize = 0;
5528 ssize_t linelen;
5530 if (!view->searching) {
5531 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5532 return NULL;
5535 if (s->matched_line) {
5536 if (view->searching == TOG_SEARCH_FORWARD)
5537 lineno = s->matched_line + 1;
5538 else
5539 lineno = s->matched_line - 1;
5540 } else
5541 lineno = s->first_displayed_line - 1 + s->selected_line;
5543 while (1) {
5544 off_t offset;
5546 if (lineno <= 0 || lineno > s->blame.nlines) {
5547 if (s->matched_line == 0) {
5548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5549 break;
5552 if (view->searching == TOG_SEARCH_FORWARD)
5553 lineno = 1;
5554 else
5555 lineno = s->blame.nlines;
5558 offset = s->blame.line_offsets[lineno - 1];
5559 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5560 free(line);
5561 return got_error_from_errno("fseeko");
5563 linelen = getline(&line, &linesize, s->blame.f);
5564 if (linelen != -1) {
5565 char *exstr;
5566 err = expand_tab(&exstr, line);
5567 if (err)
5568 break;
5569 if (match_line(exstr, &view->regex, 1,
5570 &view->regmatch)) {
5571 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5572 s->matched_line = lineno;
5573 free(exstr);
5574 break;
5576 free(exstr);
5578 if (view->searching == TOG_SEARCH_FORWARD)
5579 lineno++;
5580 else
5581 lineno--;
5583 free(line);
5585 if (s->matched_line) {
5586 s->first_displayed_line = s->matched_line;
5587 s->selected_line = 1;
5590 return err;
5593 static const struct got_error *
5594 show_blame_view(struct tog_view *view)
5596 const struct got_error *err = NULL;
5597 struct tog_blame_view_state *s = &view->state.blame;
5598 int errcode;
5600 if (s->blame.thread == 0 && !s->blame_complete) {
5601 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5602 &s->blame.thread_args);
5603 if (errcode)
5604 return got_error_set_errno(errcode, "pthread_create");
5606 halfdelay(1); /* fast refresh while annotating */
5609 if (s->blame_complete)
5610 halfdelay(10); /* disable fast refresh */
5612 err = draw_blame(view);
5614 view_border(view);
5615 return err;
5618 static const struct got_error *
5619 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5621 const struct got_error *err = NULL, *thread_err = NULL;
5622 struct tog_view *diff_view;
5623 struct tog_blame_view_state *s = &view->state.blame;
5624 int eos, nscroll, begin_y = 0, begin_x = 0;
5626 eos = nscroll = view->nlines - 2;
5627 if (view_is_hsplit_top(view))
5628 --eos; /* border */
5630 switch (ch) {
5631 case '0':
5632 view->x = 0;
5633 break;
5634 case '$':
5635 view->x = MAX(view->maxx - view->ncols / 3, 0);
5636 view->count = 0;
5637 break;
5638 case KEY_RIGHT:
5639 case 'l':
5640 if (view->x + view->ncols / 3 < view->maxx)
5641 view->x += 2; /* move two columns right */
5642 else
5643 view->count = 0;
5644 break;
5645 case KEY_LEFT:
5646 case 'h':
5647 view->x -= MIN(view->x, 2); /* move two columns back */
5648 if (view->x <= 0)
5649 view->count = 0;
5650 break;
5651 case 'q':
5652 s->done = 1;
5653 break;
5654 case 'g':
5655 case KEY_HOME:
5656 s->selected_line = 1;
5657 s->first_displayed_line = 1;
5658 view->count = 0;
5659 break;
5660 case 'G':
5661 case KEY_END:
5662 if (s->blame.nlines < eos) {
5663 s->selected_line = s->blame.nlines;
5664 s->first_displayed_line = 1;
5665 } else {
5666 s->selected_line = eos;
5667 s->first_displayed_line = s->blame.nlines - (eos - 1);
5669 view->count = 0;
5670 break;
5671 case 'k':
5672 case KEY_UP:
5673 case CTRL('p'):
5674 if (s->selected_line > 1)
5675 s->selected_line--;
5676 else if (s->selected_line == 1 &&
5677 s->first_displayed_line > 1)
5678 s->first_displayed_line--;
5679 else
5680 view->count = 0;
5681 break;
5682 case CTRL('u'):
5683 case 'u':
5684 nscroll /= 2;
5685 /* FALL THROUGH */
5686 case KEY_PPAGE:
5687 case CTRL('b'):
5688 case 'b':
5689 if (s->first_displayed_line == 1) {
5690 if (view->count > 1)
5691 nscroll += nscroll;
5692 s->selected_line = MAX(1, s->selected_line - nscroll);
5693 view->count = 0;
5694 break;
5696 if (s->first_displayed_line > nscroll)
5697 s->first_displayed_line -= nscroll;
5698 else
5699 s->first_displayed_line = 1;
5700 break;
5701 case 'j':
5702 case KEY_DOWN:
5703 case CTRL('n'):
5704 if (s->selected_line < eos && s->first_displayed_line +
5705 s->selected_line <= s->blame.nlines)
5706 s->selected_line++;
5707 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5708 s->first_displayed_line++;
5709 else
5710 view->count = 0;
5711 break;
5712 case 'c':
5713 case 'p': {
5714 struct got_object_id *id = NULL;
5716 view->count = 0;
5717 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5718 s->first_displayed_line, s->selected_line);
5719 if (id == NULL)
5720 break;
5721 if (ch == 'p') {
5722 struct got_commit_object *commit, *pcommit;
5723 struct got_object_qid *pid;
5724 struct got_object_id *blob_id = NULL;
5725 int obj_type;
5726 err = got_object_open_as_commit(&commit,
5727 s->repo, id);
5728 if (err)
5729 break;
5730 pid = STAILQ_FIRST(
5731 got_object_commit_get_parent_ids(commit));
5732 if (pid == NULL) {
5733 got_object_commit_close(commit);
5734 break;
5736 /* Check if path history ends here. */
5737 err = got_object_open_as_commit(&pcommit,
5738 s->repo, &pid->id);
5739 if (err)
5740 break;
5741 err = got_object_id_by_path(&blob_id, s->repo,
5742 pcommit, s->path);
5743 got_object_commit_close(pcommit);
5744 if (err) {
5745 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5746 err = NULL;
5747 got_object_commit_close(commit);
5748 break;
5750 err = got_object_get_type(&obj_type, s->repo,
5751 blob_id);
5752 free(blob_id);
5753 /* Can't blame non-blob type objects. */
5754 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5755 got_object_commit_close(commit);
5756 break;
5758 err = got_object_qid_alloc(&s->blamed_commit,
5759 &pid->id);
5760 got_object_commit_close(commit);
5761 } else {
5762 if (got_object_id_cmp(id,
5763 &s->blamed_commit->id) == 0)
5764 break;
5765 err = got_object_qid_alloc(&s->blamed_commit,
5766 id);
5768 if (err)
5769 break;
5770 s->done = 1;
5771 thread_err = stop_blame(&s->blame);
5772 s->done = 0;
5773 if (thread_err)
5774 break;
5775 STAILQ_INSERT_HEAD(&s->blamed_commits,
5776 s->blamed_commit, entry);
5777 err = run_blame(view);
5778 if (err)
5779 break;
5780 break;
5782 case 'C': {
5783 struct got_object_qid *first;
5785 view->count = 0;
5786 first = STAILQ_FIRST(&s->blamed_commits);
5787 if (!got_object_id_cmp(&first->id, s->commit_id))
5788 break;
5789 s->done = 1;
5790 thread_err = stop_blame(&s->blame);
5791 s->done = 0;
5792 if (thread_err)
5793 break;
5794 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5795 got_object_qid_free(s->blamed_commit);
5796 s->blamed_commit =
5797 STAILQ_FIRST(&s->blamed_commits);
5798 err = run_blame(view);
5799 if (err)
5800 break;
5801 break;
5803 case KEY_ENTER:
5804 case '\r': {
5805 struct got_object_id *id = NULL;
5806 struct got_object_qid *pid;
5807 struct got_commit_object *commit = NULL;
5809 view->count = 0;
5810 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5811 s->first_displayed_line, s->selected_line);
5812 if (id == NULL)
5813 break;
5814 err = got_object_open_as_commit(&commit, s->repo, id);
5815 if (err)
5816 break;
5817 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5818 if (view_is_parent_view(view))
5819 view_get_split(view, &begin_y, &begin_x);
5821 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5822 if (diff_view == NULL) {
5823 got_object_commit_close(commit);
5824 err = got_error_from_errno("view_open");
5825 break;
5827 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5828 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5829 got_object_commit_close(commit);
5830 if (err) {
5831 view_close(diff_view);
5832 break;
5834 if (view_is_parent_view(view) &&
5835 view->mode == TOG_VIEW_SPLIT_HRZN) {
5836 err = view_init_hsplit(view, begin_y);
5837 if (err)
5838 break;
5841 view->focussed = 0;
5842 diff_view->focussed = 1;
5843 diff_view->mode = view->mode;
5844 diff_view->nlines = view->lines - begin_y;
5845 if (view_is_parent_view(view)) {
5846 view_transfer_size(diff_view, view);
5847 err = view_close_child(view);
5848 if (err)
5849 break;
5850 err = view_set_child(view, diff_view);
5851 if (err)
5852 break;
5853 view->focus_child = 1;
5854 } else
5855 *new_view = diff_view;
5856 if (err)
5857 break;
5858 break;
5860 case CTRL('d'):
5861 case 'd':
5862 nscroll /= 2;
5863 /* FALL THROUGH */
5864 case KEY_NPAGE:
5865 case CTRL('f'):
5866 case 'f':
5867 case ' ':
5868 if (s->last_displayed_line >= s->blame.nlines &&
5869 s->selected_line >= MIN(s->blame.nlines,
5870 view->nlines - 2)) {
5871 view->count = 0;
5872 break;
5874 if (s->last_displayed_line >= s->blame.nlines &&
5875 s->selected_line < view->nlines - 2) {
5876 s->selected_line +=
5877 MIN(nscroll, s->last_displayed_line -
5878 s->first_displayed_line - s->selected_line + 1);
5880 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5881 s->first_displayed_line += nscroll;
5882 else
5883 s->first_displayed_line =
5884 s->blame.nlines - (view->nlines - 3);
5885 break;
5886 case KEY_RESIZE:
5887 if (s->selected_line > view->nlines - 2) {
5888 s->selected_line = MIN(s->blame.nlines,
5889 view->nlines - 2);
5891 break;
5892 default:
5893 view->count = 0;
5894 break;
5896 return thread_err ? thread_err : err;
5899 static const struct got_error *
5900 reset_blame_view(struct tog_view *view)
5902 const struct got_error *err;
5903 struct tog_blame_view_state *s = &view->state.blame;
5905 view->count = 0;
5906 s->done = 1;
5907 err = stop_blame(&s->blame);
5908 s->done = 0;
5909 if (err)
5910 return err;
5911 return run_blame(view);
5914 static const struct got_error *
5915 cmd_blame(int argc, char *argv[])
5917 const struct got_error *error;
5918 struct got_repository *repo = NULL;
5919 struct got_worktree *worktree = NULL;
5920 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5921 char *link_target = NULL;
5922 struct got_object_id *commit_id = NULL;
5923 struct got_commit_object *commit = NULL;
5924 char *commit_id_str = NULL;
5925 int ch;
5926 struct tog_view *view;
5927 int *pack_fds = NULL;
5929 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5930 switch (ch) {
5931 case 'c':
5932 commit_id_str = optarg;
5933 break;
5934 case 'r':
5935 repo_path = realpath(optarg, NULL);
5936 if (repo_path == NULL)
5937 return got_error_from_errno2("realpath",
5938 optarg);
5939 break;
5940 default:
5941 usage_blame();
5942 /* NOTREACHED */
5946 argc -= optind;
5947 argv += optind;
5949 if (argc != 1)
5950 usage_blame();
5952 error = got_repo_pack_fds_open(&pack_fds);
5953 if (error != NULL)
5954 goto done;
5956 if (repo_path == NULL) {
5957 cwd = getcwd(NULL, 0);
5958 if (cwd == NULL)
5959 return got_error_from_errno("getcwd");
5960 error = got_worktree_open(&worktree, cwd);
5961 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5962 goto done;
5963 if (worktree)
5964 repo_path =
5965 strdup(got_worktree_get_repo_path(worktree));
5966 else
5967 repo_path = strdup(cwd);
5968 if (repo_path == NULL) {
5969 error = got_error_from_errno("strdup");
5970 goto done;
5974 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5975 if (error != NULL)
5976 goto done;
5978 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5979 worktree);
5980 if (error)
5981 goto done;
5983 init_curses();
5985 error = apply_unveil(got_repo_get_path(repo), NULL);
5986 if (error)
5987 goto done;
5989 error = tog_load_refs(repo, 0);
5990 if (error)
5991 goto done;
5993 if (commit_id_str == NULL) {
5994 struct got_reference *head_ref;
5995 error = got_ref_open(&head_ref, repo, worktree ?
5996 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5997 if (error != NULL)
5998 goto done;
5999 error = got_ref_resolve(&commit_id, repo, head_ref);
6000 got_ref_close(head_ref);
6001 } else {
6002 error = got_repo_match_object_id(&commit_id, NULL,
6003 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6005 if (error != NULL)
6006 goto done;
6008 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6009 if (view == NULL) {
6010 error = got_error_from_errno("view_open");
6011 goto done;
6014 error = got_object_open_as_commit(&commit, repo, commit_id);
6015 if (error)
6016 goto done;
6018 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6019 commit, repo);
6020 if (error)
6021 goto done;
6023 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6024 commit_id, repo);
6025 if (error)
6026 goto done;
6027 if (worktree) {
6028 /* Release work tree lock. */
6029 got_worktree_close(worktree);
6030 worktree = NULL;
6032 error = view_loop(view);
6033 done:
6034 free(repo_path);
6035 free(in_repo_path);
6036 free(link_target);
6037 free(cwd);
6038 free(commit_id);
6039 if (commit)
6040 got_object_commit_close(commit);
6041 if (worktree)
6042 got_worktree_close(worktree);
6043 if (repo) {
6044 const struct got_error *close_err = got_repo_close(repo);
6045 if (error == NULL)
6046 error = close_err;
6048 if (pack_fds) {
6049 const struct got_error *pack_err =
6050 got_repo_pack_fds_close(pack_fds);
6051 if (error == NULL)
6052 error = pack_err;
6054 tog_free_refs();
6055 return error;
6058 static const struct got_error *
6059 draw_tree_entries(struct tog_view *view, const char *parent_path)
6061 struct tog_tree_view_state *s = &view->state.tree;
6062 const struct got_error *err = NULL;
6063 struct got_tree_entry *te;
6064 wchar_t *wline;
6065 struct tog_color *tc;
6066 int width, n, i, nentries;
6067 int limit = view->nlines;
6069 s->ndisplayed = 0;
6070 if (view_is_hsplit_top(view))
6071 --limit; /* border */
6073 werase(view->window);
6075 if (limit == 0)
6076 return NULL;
6078 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6079 0, 0);
6080 if (err)
6081 return err;
6082 if (view_needs_focus_indication(view))
6083 wstandout(view->window);
6084 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6085 if (tc)
6086 wattr_on(view->window,
6087 COLOR_PAIR(tc->colorpair), NULL);
6088 waddwstr(view->window, wline);
6089 if (tc)
6090 wattr_off(view->window,
6091 COLOR_PAIR(tc->colorpair), NULL);
6092 if (view_needs_focus_indication(view))
6093 wstandend(view->window);
6094 free(wline);
6095 wline = NULL;
6096 if (width < view->ncols - 1)
6097 waddch(view->window, '\n');
6098 if (--limit <= 0)
6099 return NULL;
6100 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6101 0, 0);
6102 if (err)
6103 return err;
6104 waddwstr(view->window, wline);
6105 free(wline);
6106 wline = NULL;
6107 if (width < view->ncols - 1)
6108 waddch(view->window, '\n');
6109 if (--limit <= 0)
6110 return NULL;
6111 waddch(view->window, '\n');
6112 if (--limit <= 0)
6113 return NULL;
6115 if (s->first_displayed_entry == NULL) {
6116 te = got_object_tree_get_first_entry(s->tree);
6117 if (s->selected == 0) {
6118 if (view->focussed)
6119 wstandout(view->window);
6120 s->selected_entry = NULL;
6122 waddstr(view->window, " ..\n"); /* parent directory */
6123 if (s->selected == 0 && view->focussed)
6124 wstandend(view->window);
6125 s->ndisplayed++;
6126 if (--limit <= 0)
6127 return NULL;
6128 n = 1;
6129 } else {
6130 n = 0;
6131 te = s->first_displayed_entry;
6134 nentries = got_object_tree_get_nentries(s->tree);
6135 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6136 char *line = NULL, *id_str = NULL, *link_target = NULL;
6137 const char *modestr = "";
6138 mode_t mode;
6140 te = got_object_tree_get_entry(s->tree, i);
6141 mode = got_tree_entry_get_mode(te);
6143 if (s->show_ids) {
6144 err = got_object_id_str(&id_str,
6145 got_tree_entry_get_id(te));
6146 if (err)
6147 return got_error_from_errno(
6148 "got_object_id_str");
6150 if (got_object_tree_entry_is_submodule(te))
6151 modestr = "$";
6152 else if (S_ISLNK(mode)) {
6153 int i;
6155 err = got_tree_entry_get_symlink_target(&link_target,
6156 te, s->repo);
6157 if (err) {
6158 free(id_str);
6159 return err;
6161 for (i = 0; i < strlen(link_target); i++) {
6162 if (!isprint((unsigned char)link_target[i]))
6163 link_target[i] = '?';
6165 modestr = "@";
6167 else if (S_ISDIR(mode))
6168 modestr = "/";
6169 else if (mode & S_IXUSR)
6170 modestr = "*";
6171 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6172 got_tree_entry_get_name(te), modestr,
6173 link_target ? " -> ": "",
6174 link_target ? link_target : "") == -1) {
6175 free(id_str);
6176 free(link_target);
6177 return got_error_from_errno("asprintf");
6179 free(id_str);
6180 free(link_target);
6181 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6182 0, 0);
6183 if (err) {
6184 free(line);
6185 break;
6187 if (n == s->selected) {
6188 if (view->focussed)
6189 wstandout(view->window);
6190 s->selected_entry = te;
6192 tc = match_color(&s->colors, line);
6193 if (tc)
6194 wattr_on(view->window,
6195 COLOR_PAIR(tc->colorpair), NULL);
6196 waddwstr(view->window, wline);
6197 if (tc)
6198 wattr_off(view->window,
6199 COLOR_PAIR(tc->colorpair), NULL);
6200 if (width < view->ncols - 1)
6201 waddch(view->window, '\n');
6202 if (n == s->selected && view->focussed)
6203 wstandend(view->window);
6204 free(line);
6205 free(wline);
6206 wline = NULL;
6207 n++;
6208 s->ndisplayed++;
6209 s->last_displayed_entry = te;
6210 if (--limit <= 0)
6211 break;
6214 return err;
6217 static void
6218 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6220 struct got_tree_entry *te;
6221 int isroot = s->tree == s->root;
6222 int i = 0;
6224 if (s->first_displayed_entry == NULL)
6225 return;
6227 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6228 while (i++ < maxscroll) {
6229 if (te == NULL) {
6230 if (!isroot)
6231 s->first_displayed_entry = NULL;
6232 break;
6234 s->first_displayed_entry = te;
6235 te = got_tree_entry_get_prev(s->tree, te);
6239 static const struct got_error *
6240 tree_scroll_down(struct tog_view *view, int maxscroll)
6242 struct tog_tree_view_state *s = &view->state.tree;
6243 struct got_tree_entry *next, *last;
6244 int n = 0;
6246 if (s->first_displayed_entry)
6247 next = got_tree_entry_get_next(s->tree,
6248 s->first_displayed_entry);
6249 else
6250 next = got_object_tree_get_first_entry(s->tree);
6252 last = s->last_displayed_entry;
6253 while (next && n++ < maxscroll) {
6254 if (last)
6255 last = got_tree_entry_get_next(s->tree, last);
6256 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6257 s->first_displayed_entry = next;
6258 next = got_tree_entry_get_next(s->tree, next);
6262 return NULL;
6265 static const struct got_error *
6266 tree_entry_path(char **path, struct tog_parent_trees *parents,
6267 struct got_tree_entry *te)
6269 const struct got_error *err = NULL;
6270 struct tog_parent_tree *pt;
6271 size_t len = 2; /* for leading slash and NUL */
6273 TAILQ_FOREACH(pt, parents, entry)
6274 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6275 + 1 /* slash */;
6276 if (te)
6277 len += strlen(got_tree_entry_get_name(te));
6279 *path = calloc(1, len);
6280 if (path == NULL)
6281 return got_error_from_errno("calloc");
6283 (*path)[0] = '/';
6284 pt = TAILQ_LAST(parents, tog_parent_trees);
6285 while (pt) {
6286 const char *name = got_tree_entry_get_name(pt->selected_entry);
6287 if (strlcat(*path, name, len) >= len) {
6288 err = got_error(GOT_ERR_NO_SPACE);
6289 goto done;
6291 if (strlcat(*path, "/", len) >= len) {
6292 err = got_error(GOT_ERR_NO_SPACE);
6293 goto done;
6295 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6297 if (te) {
6298 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6299 err = got_error(GOT_ERR_NO_SPACE);
6300 goto done;
6303 done:
6304 if (err) {
6305 free(*path);
6306 *path = NULL;
6308 return err;
6311 static const struct got_error *
6312 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6313 struct got_tree_entry *te, struct tog_parent_trees *parents,
6314 struct got_object_id *commit_id, struct got_repository *repo)
6316 const struct got_error *err = NULL;
6317 char *path;
6318 struct tog_view *blame_view;
6320 *new_view = NULL;
6322 err = tree_entry_path(&path, parents, te);
6323 if (err)
6324 return err;
6326 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6327 if (blame_view == NULL) {
6328 err = got_error_from_errno("view_open");
6329 goto done;
6332 err = open_blame_view(blame_view, path, commit_id, repo);
6333 if (err) {
6334 if (err->code == GOT_ERR_CANCELLED)
6335 err = NULL;
6336 view_close(blame_view);
6337 } else
6338 *new_view = blame_view;
6339 done:
6340 free(path);
6341 return err;
6344 static const struct got_error *
6345 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6346 struct tog_tree_view_state *s)
6348 struct tog_view *log_view;
6349 const struct got_error *err = NULL;
6350 char *path;
6352 *new_view = NULL;
6354 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6355 if (log_view == NULL)
6356 return got_error_from_errno("view_open");
6358 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6359 if (err)
6360 return err;
6362 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6363 path, 0);
6364 if (err)
6365 view_close(log_view);
6366 else
6367 *new_view = log_view;
6368 free(path);
6369 return err;
6372 static const struct got_error *
6373 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6374 const char *head_ref_name, struct got_repository *repo)
6376 const struct got_error *err = NULL;
6377 char *commit_id_str = NULL;
6378 struct tog_tree_view_state *s = &view->state.tree;
6379 struct got_commit_object *commit = NULL;
6381 TAILQ_INIT(&s->parents);
6382 STAILQ_INIT(&s->colors);
6384 s->commit_id = got_object_id_dup(commit_id);
6385 if (s->commit_id == NULL)
6386 return got_error_from_errno("got_object_id_dup");
6388 err = got_object_open_as_commit(&commit, repo, commit_id);
6389 if (err)
6390 goto done;
6393 * The root is opened here and will be closed when the view is closed.
6394 * Any visited subtrees and their path-wise parents are opened and
6395 * closed on demand.
6397 err = got_object_open_as_tree(&s->root, repo,
6398 got_object_commit_get_tree_id(commit));
6399 if (err)
6400 goto done;
6401 s->tree = s->root;
6403 err = got_object_id_str(&commit_id_str, commit_id);
6404 if (err != NULL)
6405 goto done;
6407 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6408 err = got_error_from_errno("asprintf");
6409 goto done;
6412 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6413 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6414 if (head_ref_name) {
6415 s->head_ref_name = strdup(head_ref_name);
6416 if (s->head_ref_name == NULL) {
6417 err = got_error_from_errno("strdup");
6418 goto done;
6421 s->repo = repo;
6423 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6424 err = add_color(&s->colors, "\\$$",
6425 TOG_COLOR_TREE_SUBMODULE,
6426 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6427 if (err)
6428 goto done;
6429 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6430 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6431 if (err)
6432 goto done;
6433 err = add_color(&s->colors, "/$",
6434 TOG_COLOR_TREE_DIRECTORY,
6435 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6436 if (err)
6437 goto done;
6439 err = add_color(&s->colors, "\\*$",
6440 TOG_COLOR_TREE_EXECUTABLE,
6441 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6442 if (err)
6443 goto done;
6445 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6446 get_color_value("TOG_COLOR_COMMIT"));
6447 if (err)
6448 goto done;
6451 view->show = show_tree_view;
6452 view->input = input_tree_view;
6453 view->close = close_tree_view;
6454 view->search_start = search_start_tree_view;
6455 view->search_next = search_next_tree_view;
6456 done:
6457 free(commit_id_str);
6458 if (commit)
6459 got_object_commit_close(commit);
6460 if (err)
6461 close_tree_view(view);
6462 return err;
6465 static const struct got_error *
6466 close_tree_view(struct tog_view *view)
6468 struct tog_tree_view_state *s = &view->state.tree;
6470 free_colors(&s->colors);
6471 free(s->tree_label);
6472 s->tree_label = NULL;
6473 free(s->commit_id);
6474 s->commit_id = NULL;
6475 free(s->head_ref_name);
6476 s->head_ref_name = NULL;
6477 while (!TAILQ_EMPTY(&s->parents)) {
6478 struct tog_parent_tree *parent;
6479 parent = TAILQ_FIRST(&s->parents);
6480 TAILQ_REMOVE(&s->parents, parent, entry);
6481 if (parent->tree != s->root)
6482 got_object_tree_close(parent->tree);
6483 free(parent);
6486 if (s->tree != NULL && s->tree != s->root)
6487 got_object_tree_close(s->tree);
6488 if (s->root)
6489 got_object_tree_close(s->root);
6490 return NULL;
6493 static const struct got_error *
6494 search_start_tree_view(struct tog_view *view)
6496 struct tog_tree_view_state *s = &view->state.tree;
6498 s->matched_entry = NULL;
6499 return NULL;
6502 static int
6503 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6505 regmatch_t regmatch;
6507 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
6508 0) == 0;
6511 static const struct got_error *
6512 search_next_tree_view(struct tog_view *view)
6514 struct tog_tree_view_state *s = &view->state.tree;
6515 struct got_tree_entry *te = NULL;
6517 if (!view->searching) {
6518 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6519 return NULL;
6522 if (s->matched_entry) {
6523 if (view->searching == TOG_SEARCH_FORWARD) {
6524 if (s->selected_entry)
6525 te = got_tree_entry_get_next(s->tree,
6526 s->selected_entry);
6527 else
6528 te = got_object_tree_get_first_entry(s->tree);
6529 } else {
6530 if (s->selected_entry == NULL)
6531 te = got_object_tree_get_last_entry(s->tree);
6532 else
6533 te = got_tree_entry_get_prev(s->tree,
6534 s->selected_entry);
6536 } else {
6537 if (s->selected_entry)
6538 te = s->selected_entry;
6539 else if (view->searching == TOG_SEARCH_FORWARD)
6540 te = got_object_tree_get_first_entry(s->tree);
6541 else
6542 te = got_object_tree_get_last_entry(s->tree);
6545 while (1) {
6546 if (te == NULL) {
6547 if (s->matched_entry == NULL) {
6548 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6549 return NULL;
6551 if (view->searching == TOG_SEARCH_FORWARD)
6552 te = got_object_tree_get_first_entry(s->tree);
6553 else
6554 te = got_object_tree_get_last_entry(s->tree);
6557 if (match_tree_entry(te, &view->regex)) {
6558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6559 s->matched_entry = te;
6560 break;
6563 if (view->searching == TOG_SEARCH_FORWARD)
6564 te = got_tree_entry_get_next(s->tree, te);
6565 else
6566 te = got_tree_entry_get_prev(s->tree, te);
6569 if (s->matched_entry) {
6570 s->first_displayed_entry = s->matched_entry;
6571 s->selected = 0;
6574 return NULL;
6577 static const struct got_error *
6578 show_tree_view(struct tog_view *view)
6580 const struct got_error *err = NULL;
6581 struct tog_tree_view_state *s = &view->state.tree;
6582 char *parent_path;
6584 err = tree_entry_path(&parent_path, &s->parents, NULL);
6585 if (err)
6586 return err;
6588 err = draw_tree_entries(view, parent_path);
6589 free(parent_path);
6591 view_border(view);
6592 return err;
6595 static const struct got_error *
6596 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6598 const struct got_error *err = NULL;
6599 struct tog_tree_view_state *s = &view->state.tree;
6600 struct tog_view *log_view, *ref_view;
6601 struct got_tree_entry *te;
6602 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6604 switch (ch) {
6605 case 'i':
6606 s->show_ids = !s->show_ids;
6607 view->count = 0;
6608 break;
6609 case 'l':
6610 view->count = 0;
6611 if (!s->selected_entry)
6612 break;
6613 if (view_is_parent_view(view))
6614 view_get_split(view, &begin_y, &begin_x);
6615 err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6616 if (view_is_parent_view(view) &&
6617 view->mode == TOG_VIEW_SPLIT_HRZN) {
6618 err = view_init_hsplit(view, begin_y);
6619 if (err)
6620 break;
6622 view->focussed = 0;
6623 log_view->focussed = 1;
6624 log_view->mode = view->mode;
6625 log_view->nlines = view->lines - begin_y;
6626 if (view_is_parent_view(view)) {
6627 view_transfer_size(log_view, view);
6628 err = view_close_child(view);
6629 if (err)
6630 return err;
6631 err = view_set_child(view, log_view);
6632 if (err)
6633 return err;
6634 view->focus_child = 1;
6635 } else
6636 *new_view = log_view;
6637 break;
6638 case 'r':
6639 view->count = 0;
6640 if (view_is_parent_view(view))
6641 view_get_split(view, &begin_y, &begin_x);
6642 ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6643 if (ref_view == NULL)
6644 return got_error_from_errno("view_open");
6645 err = open_ref_view(ref_view, s->repo);
6646 if (err) {
6647 view_close(ref_view);
6648 return err;
6650 if (view_is_parent_view(view) &&
6651 view->mode == TOG_VIEW_SPLIT_HRZN) {
6652 err = view_init_hsplit(view, begin_y);
6653 if (err)
6654 break;
6656 view->focussed = 0;
6657 ref_view->focussed = 1;
6658 ref_view->mode = view->mode;
6659 ref_view->nlines = view->lines - begin_y;
6660 if (view_is_parent_view(view)) {
6661 view_transfer_size(ref_view, view);
6662 err = view_close_child(view);
6663 if (err)
6664 return err;
6665 err = view_set_child(view, ref_view);
6666 if (err)
6667 return err;
6668 view->focus_child = 1;
6669 } else
6670 *new_view = ref_view;
6671 break;
6672 case 'g':
6673 case KEY_HOME:
6674 s->selected = 0;
6675 view->count = 0;
6676 if (s->tree == s->root)
6677 s->first_displayed_entry =
6678 got_object_tree_get_first_entry(s->tree);
6679 else
6680 s->first_displayed_entry = NULL;
6681 break;
6682 case 'G':
6683 case KEY_END: {
6684 int eos = view->nlines - 3;
6686 if (view->mode == TOG_VIEW_SPLIT_HRZN)
6687 --eos; /* border */
6688 s->selected = 0;
6689 view->count = 0;
6690 te = got_object_tree_get_last_entry(s->tree);
6691 for (n = 0; n < eos; n++) {
6692 if (te == NULL) {
6693 if(s->tree != s->root) {
6694 s->first_displayed_entry = NULL;
6695 n++;
6697 break;
6699 s->first_displayed_entry = te;
6700 te = got_tree_entry_get_prev(s->tree, te);
6702 if (n > 0)
6703 s->selected = n - 1;
6704 break;
6706 case 'k':
6707 case KEY_UP:
6708 case CTRL('p'):
6709 if (s->selected > 0) {
6710 s->selected--;
6711 break;
6713 tree_scroll_up(s, 1);
6714 if (s->selected_entry == NULL ||
6715 (s->tree == s->root && s->selected_entry ==
6716 got_object_tree_get_first_entry(s->tree)))
6717 view->count = 0;
6718 break;
6719 case CTRL('u'):
6720 case 'u':
6721 nscroll /= 2;
6722 /* FALL THROUGH */
6723 case KEY_PPAGE:
6724 case CTRL('b'):
6725 case 'b':
6726 if (s->tree == s->root) {
6727 if (got_object_tree_get_first_entry(s->tree) ==
6728 s->first_displayed_entry)
6729 s->selected -= MIN(s->selected, nscroll);
6730 } else {
6731 if (s->first_displayed_entry == NULL)
6732 s->selected -= MIN(s->selected, nscroll);
6734 tree_scroll_up(s, MAX(0, nscroll));
6735 if (s->selected_entry == NULL ||
6736 (s->tree == s->root && s->selected_entry ==
6737 got_object_tree_get_first_entry(s->tree)))
6738 view->count = 0;
6739 break;
6740 case 'j':
6741 case KEY_DOWN:
6742 case CTRL('n'):
6743 if (s->selected < s->ndisplayed - 1) {
6744 s->selected++;
6745 break;
6747 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6748 == NULL) {
6749 /* can't scroll any further */
6750 view->count = 0;
6751 break;
6753 tree_scroll_down(view, 1);
6754 break;
6755 case CTRL('d'):
6756 case 'd':
6757 nscroll /= 2;
6758 /* FALL THROUGH */
6759 case KEY_NPAGE:
6760 case CTRL('f'):
6761 case 'f':
6762 case ' ':
6763 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6764 == NULL) {
6765 /* can't scroll any further; move cursor down */
6766 if (s->selected < s->ndisplayed - 1)
6767 s->selected += MIN(nscroll,
6768 s->ndisplayed - s->selected - 1);
6769 else
6770 view->count = 0;
6771 break;
6773 tree_scroll_down(view, nscroll);
6774 break;
6775 case KEY_ENTER:
6776 case '\r':
6777 case KEY_BACKSPACE:
6778 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6779 struct tog_parent_tree *parent;
6780 /* user selected '..' */
6781 if (s->tree == s->root) {
6782 view->count = 0;
6783 break;
6785 parent = TAILQ_FIRST(&s->parents);
6786 TAILQ_REMOVE(&s->parents, parent,
6787 entry);
6788 got_object_tree_close(s->tree);
6789 s->tree = parent->tree;
6790 s->first_displayed_entry =
6791 parent->first_displayed_entry;
6792 s->selected_entry =
6793 parent->selected_entry;
6794 s->selected = parent->selected;
6795 if (s->selected > view->nlines - 3) {
6796 err = offset_selection_down(view);
6797 if (err)
6798 break;
6800 free(parent);
6801 } else if (S_ISDIR(got_tree_entry_get_mode(
6802 s->selected_entry))) {
6803 struct got_tree_object *subtree;
6804 view->count = 0;
6805 err = got_object_open_as_tree(&subtree, s->repo,
6806 got_tree_entry_get_id(s->selected_entry));
6807 if (err)
6808 break;
6809 err = tree_view_visit_subtree(s, subtree);
6810 if (err) {
6811 got_object_tree_close(subtree);
6812 break;
6814 } else if (S_ISREG(got_tree_entry_get_mode(
6815 s->selected_entry))) {
6816 struct tog_view *blame_view;
6817 int begin_x = 0, begin_y = 0;
6819 if (view_is_parent_view(view))
6820 view_get_split(view, &begin_y, &begin_x);
6822 err = blame_tree_entry(&blame_view, begin_y, begin_x,
6823 s->selected_entry, &s->parents,
6824 s->commit_id, s->repo);
6825 if (err)
6826 break;
6828 if (view_is_parent_view(view) &&
6829 view->mode == TOG_VIEW_SPLIT_HRZN) {
6830 err = view_init_hsplit(view, begin_y);
6831 if (err)
6832 break;
6835 view->count = 0;
6836 view->focussed = 0;
6837 blame_view->focussed = 1;
6838 blame_view->mode = view->mode;
6839 blame_view->nlines = view->lines - begin_y;
6840 if (view_is_parent_view(view)) {
6841 view_transfer_size(blame_view, view);
6842 err = view_close_child(view);
6843 if (err)
6844 return err;
6845 err = view_set_child(view, blame_view);
6846 if (err)
6847 return err;
6848 view->focus_child = 1;
6849 } else
6850 *new_view = blame_view;
6852 break;
6853 case KEY_RESIZE:
6854 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6855 s->selected = view->nlines - 4;
6856 view->count = 0;
6857 break;
6858 default:
6859 view->count = 0;
6860 break;
6863 return err;
6866 __dead static void
6867 usage_tree(void)
6869 endwin();
6870 fprintf(stderr,
6871 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6872 getprogname());
6873 exit(1);
6876 static const struct got_error *
6877 cmd_tree(int argc, char *argv[])
6879 const struct got_error *error;
6880 struct got_repository *repo = NULL;
6881 struct got_worktree *worktree = NULL;
6882 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6883 struct got_object_id *commit_id = NULL;
6884 struct got_commit_object *commit = NULL;
6885 const char *commit_id_arg = NULL;
6886 char *label = NULL;
6887 struct got_reference *ref = NULL;
6888 const char *head_ref_name = NULL;
6889 int ch;
6890 struct tog_view *view;
6891 int *pack_fds = NULL;
6893 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6894 switch (ch) {
6895 case 'c':
6896 commit_id_arg = optarg;
6897 break;
6898 case 'r':
6899 repo_path = realpath(optarg, NULL);
6900 if (repo_path == NULL)
6901 return got_error_from_errno2("realpath",
6902 optarg);
6903 break;
6904 default:
6905 usage_tree();
6906 /* NOTREACHED */
6910 argc -= optind;
6911 argv += optind;
6913 if (argc > 1)
6914 usage_tree();
6916 error = got_repo_pack_fds_open(&pack_fds);
6917 if (error != NULL)
6918 goto done;
6920 if (repo_path == NULL) {
6921 cwd = getcwd(NULL, 0);
6922 if (cwd == NULL)
6923 return got_error_from_errno("getcwd");
6924 error = got_worktree_open(&worktree, cwd);
6925 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6926 goto done;
6927 if (worktree)
6928 repo_path =
6929 strdup(got_worktree_get_repo_path(worktree));
6930 else
6931 repo_path = strdup(cwd);
6932 if (repo_path == NULL) {
6933 error = got_error_from_errno("strdup");
6934 goto done;
6938 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6939 if (error != NULL)
6940 goto done;
6942 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6943 repo, worktree);
6944 if (error)
6945 goto done;
6947 init_curses();
6949 error = apply_unveil(got_repo_get_path(repo), NULL);
6950 if (error)
6951 goto done;
6953 error = tog_load_refs(repo, 0);
6954 if (error)
6955 goto done;
6957 if (commit_id_arg == NULL) {
6958 error = got_repo_match_object_id(&commit_id, &label,
6959 worktree ? got_worktree_get_head_ref_name(worktree) :
6960 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6961 if (error)
6962 goto done;
6963 head_ref_name = label;
6964 } else {
6965 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6966 if (error == NULL)
6967 head_ref_name = got_ref_get_name(ref);
6968 else if (error->code != GOT_ERR_NOT_REF)
6969 goto done;
6970 error = got_repo_match_object_id(&commit_id, NULL,
6971 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6972 if (error)
6973 goto done;
6976 error = got_object_open_as_commit(&commit, repo, commit_id);
6977 if (error)
6978 goto done;
6980 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6981 if (view == NULL) {
6982 error = got_error_from_errno("view_open");
6983 goto done;
6985 error = open_tree_view(view, commit_id, head_ref_name, repo);
6986 if (error)
6987 goto done;
6988 if (!got_path_is_root_dir(in_repo_path)) {
6989 error = tree_view_walk_path(&view->state.tree, commit,
6990 in_repo_path);
6991 if (error)
6992 goto done;
6995 if (worktree) {
6996 /* Release work tree lock. */
6997 got_worktree_close(worktree);
6998 worktree = NULL;
7000 error = view_loop(view);
7001 done:
7002 free(repo_path);
7003 free(cwd);
7004 free(commit_id);
7005 free(label);
7006 if (ref)
7007 got_ref_close(ref);
7008 if (repo) {
7009 const struct got_error *close_err = got_repo_close(repo);
7010 if (error == NULL)
7011 error = close_err;
7013 if (pack_fds) {
7014 const struct got_error *pack_err =
7015 got_repo_pack_fds_close(pack_fds);
7016 if (error == NULL)
7017 error = pack_err;
7019 tog_free_refs();
7020 return error;
7023 static const struct got_error *
7024 ref_view_load_refs(struct tog_ref_view_state *s)
7026 struct got_reflist_entry *sre;
7027 struct tog_reflist_entry *re;
7029 s->nrefs = 0;
7030 TAILQ_FOREACH(sre, &tog_refs, entry) {
7031 if (strncmp(got_ref_get_name(sre->ref),
7032 "refs/got/", 9) == 0 &&
7033 strncmp(got_ref_get_name(sre->ref),
7034 "refs/got/backup/", 16) != 0)
7035 continue;
7037 re = malloc(sizeof(*re));
7038 if (re == NULL)
7039 return got_error_from_errno("malloc");
7041 re->ref = got_ref_dup(sre->ref);
7042 if (re->ref == NULL)
7043 return got_error_from_errno("got_ref_dup");
7044 re->idx = s->nrefs++;
7045 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7048 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7049 return NULL;
7052 static void
7053 ref_view_free_refs(struct tog_ref_view_state *s)
7055 struct tog_reflist_entry *re;
7057 while (!TAILQ_EMPTY(&s->refs)) {
7058 re = TAILQ_FIRST(&s->refs);
7059 TAILQ_REMOVE(&s->refs, re, entry);
7060 got_ref_close(re->ref);
7061 free(re);
7065 static const struct got_error *
7066 open_ref_view(struct tog_view *view, struct got_repository *repo)
7068 const struct got_error *err = NULL;
7069 struct tog_ref_view_state *s = &view->state.ref;
7071 s->selected_entry = 0;
7072 s->repo = repo;
7074 TAILQ_INIT(&s->refs);
7075 STAILQ_INIT(&s->colors);
7077 err = ref_view_load_refs(s);
7078 if (err)
7079 return err;
7081 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7082 err = add_color(&s->colors, "^refs/heads/",
7083 TOG_COLOR_REFS_HEADS,
7084 get_color_value("TOG_COLOR_REFS_HEADS"));
7085 if (err)
7086 goto done;
7088 err = add_color(&s->colors, "^refs/tags/",
7089 TOG_COLOR_REFS_TAGS,
7090 get_color_value("TOG_COLOR_REFS_TAGS"));
7091 if (err)
7092 goto done;
7094 err = add_color(&s->colors, "^refs/remotes/",
7095 TOG_COLOR_REFS_REMOTES,
7096 get_color_value("TOG_COLOR_REFS_REMOTES"));
7097 if (err)
7098 goto done;
7100 err = add_color(&s->colors, "^refs/got/backup/",
7101 TOG_COLOR_REFS_BACKUP,
7102 get_color_value("TOG_COLOR_REFS_BACKUP"));
7103 if (err)
7104 goto done;
7107 view->show = show_ref_view;
7108 view->input = input_ref_view;
7109 view->close = close_ref_view;
7110 view->search_start = search_start_ref_view;
7111 view->search_next = search_next_ref_view;
7112 done:
7113 if (err)
7114 free_colors(&s->colors);
7115 return err;
7118 static const struct got_error *
7119 close_ref_view(struct tog_view *view)
7121 struct tog_ref_view_state *s = &view->state.ref;
7123 ref_view_free_refs(s);
7124 free_colors(&s->colors);
7126 return NULL;
7129 static const struct got_error *
7130 resolve_reflist_entry(struct got_object_id **commit_id,
7131 struct tog_reflist_entry *re, struct got_repository *repo)
7133 const struct got_error *err = NULL;
7134 struct got_object_id *obj_id;
7135 struct got_tag_object *tag = NULL;
7136 int obj_type;
7138 *commit_id = NULL;
7140 err = got_ref_resolve(&obj_id, repo, re->ref);
7141 if (err)
7142 return err;
7144 err = got_object_get_type(&obj_type, repo, obj_id);
7145 if (err)
7146 goto done;
7148 switch (obj_type) {
7149 case GOT_OBJ_TYPE_COMMIT:
7150 *commit_id = obj_id;
7151 break;
7152 case GOT_OBJ_TYPE_TAG:
7153 err = got_object_open_as_tag(&tag, repo, obj_id);
7154 if (err)
7155 goto done;
7156 free(obj_id);
7157 err = got_object_get_type(&obj_type, repo,
7158 got_object_tag_get_object_id(tag));
7159 if (err)
7160 goto done;
7161 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7162 err = got_error(GOT_ERR_OBJ_TYPE);
7163 goto done;
7165 *commit_id = got_object_id_dup(
7166 got_object_tag_get_object_id(tag));
7167 if (*commit_id == NULL) {
7168 err = got_error_from_errno("got_object_id_dup");
7169 goto done;
7171 break;
7172 default:
7173 err = got_error(GOT_ERR_OBJ_TYPE);
7174 break;
7177 done:
7178 if (tag)
7179 got_object_tag_close(tag);
7180 if (err) {
7181 free(*commit_id);
7182 *commit_id = NULL;
7184 return err;
7187 static const struct got_error *
7188 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7189 struct tog_reflist_entry *re, struct got_repository *repo)
7191 struct tog_view *log_view;
7192 const struct got_error *err = NULL;
7193 struct got_object_id *commit_id = NULL;
7195 *new_view = NULL;
7197 err = resolve_reflist_entry(&commit_id, re, repo);
7198 if (err) {
7199 if (err->code != GOT_ERR_OBJ_TYPE)
7200 return err;
7201 else
7202 return NULL;
7205 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7206 if (log_view == NULL) {
7207 err = got_error_from_errno("view_open");
7208 goto done;
7211 err = open_log_view(log_view, commit_id, repo,
7212 got_ref_get_name(re->ref), "", 0);
7213 done:
7214 if (err)
7215 view_close(log_view);
7216 else
7217 *new_view = log_view;
7218 free(commit_id);
7219 return err;
7222 static void
7223 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7225 struct tog_reflist_entry *re;
7226 int i = 0;
7228 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7229 return;
7231 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7232 while (i++ < maxscroll) {
7233 if (re == NULL)
7234 break;
7235 s->first_displayed_entry = re;
7236 re = TAILQ_PREV(re, tog_reflist_head, entry);
7240 static const struct got_error *
7241 ref_scroll_down(struct tog_view *view, int maxscroll)
7243 struct tog_ref_view_state *s = &view->state.ref;
7244 struct tog_reflist_entry *next, *last;
7245 int n = 0;
7247 if (s->first_displayed_entry)
7248 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7249 else
7250 next = TAILQ_FIRST(&s->refs);
7252 last = s->last_displayed_entry;
7253 while (next && n++ < maxscroll) {
7254 if (last)
7255 last = TAILQ_NEXT(last, entry);
7256 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7257 s->first_displayed_entry = next;
7258 next = TAILQ_NEXT(next, entry);
7262 return NULL;
7265 static const struct got_error *
7266 search_start_ref_view(struct tog_view *view)
7268 struct tog_ref_view_state *s = &view->state.ref;
7270 s->matched_entry = NULL;
7271 return NULL;
7274 static int
7275 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7277 regmatch_t regmatch;
7279 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7280 0) == 0;
7283 static const struct got_error *
7284 search_next_ref_view(struct tog_view *view)
7286 struct tog_ref_view_state *s = &view->state.ref;
7287 struct tog_reflist_entry *re = NULL;
7289 if (!view->searching) {
7290 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7291 return NULL;
7294 if (s->matched_entry) {
7295 if (view->searching == TOG_SEARCH_FORWARD) {
7296 if (s->selected_entry)
7297 re = TAILQ_NEXT(s->selected_entry, entry);
7298 else
7299 re = TAILQ_PREV(s->selected_entry,
7300 tog_reflist_head, entry);
7301 } else {
7302 if (s->selected_entry == NULL)
7303 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7304 else
7305 re = TAILQ_PREV(s->selected_entry,
7306 tog_reflist_head, entry);
7308 } else {
7309 if (s->selected_entry)
7310 re = s->selected_entry;
7311 else if (view->searching == TOG_SEARCH_FORWARD)
7312 re = TAILQ_FIRST(&s->refs);
7313 else
7314 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7317 while (1) {
7318 if (re == NULL) {
7319 if (s->matched_entry == NULL) {
7320 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7321 return NULL;
7323 if (view->searching == TOG_SEARCH_FORWARD)
7324 re = TAILQ_FIRST(&s->refs);
7325 else
7326 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7329 if (match_reflist_entry(re, &view->regex)) {
7330 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7331 s->matched_entry = re;
7332 break;
7335 if (view->searching == TOG_SEARCH_FORWARD)
7336 re = TAILQ_NEXT(re, entry);
7337 else
7338 re = TAILQ_PREV(re, tog_reflist_head, entry);
7341 if (s->matched_entry) {
7342 s->first_displayed_entry = s->matched_entry;
7343 s->selected = 0;
7346 return NULL;
7349 static const struct got_error *
7350 show_ref_view(struct tog_view *view)
7352 const struct got_error *err = NULL;
7353 struct tog_ref_view_state *s = &view->state.ref;
7354 struct tog_reflist_entry *re;
7355 char *line = NULL;
7356 wchar_t *wline;
7357 struct tog_color *tc;
7358 int width, n;
7359 int limit = view->nlines;
7361 werase(view->window);
7363 s->ndisplayed = 0;
7364 if (view_is_hsplit_top(view))
7365 --limit; /* border */
7367 if (limit == 0)
7368 return NULL;
7370 re = s->first_displayed_entry;
7372 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7373 s->nrefs) == -1)
7374 return got_error_from_errno("asprintf");
7376 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7377 if (err) {
7378 free(line);
7379 return err;
7381 if (view_needs_focus_indication(view))
7382 wstandout(view->window);
7383 waddwstr(view->window, wline);
7384 if (view_needs_focus_indication(view))
7385 wstandend(view->window);
7386 free(wline);
7387 wline = NULL;
7388 free(line);
7389 line = NULL;
7390 if (width < view->ncols - 1)
7391 waddch(view->window, '\n');
7392 if (--limit <= 0)
7393 return NULL;
7395 n = 0;
7396 while (re && limit > 0) {
7397 char *line = NULL;
7398 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7400 if (s->show_date) {
7401 struct got_commit_object *ci;
7402 struct got_tag_object *tag;
7403 struct got_object_id *id;
7404 struct tm tm;
7405 time_t t;
7407 err = got_ref_resolve(&id, s->repo, re->ref);
7408 if (err)
7409 return err;
7410 err = got_object_open_as_tag(&tag, s->repo, id);
7411 if (err) {
7412 if (err->code != GOT_ERR_OBJ_TYPE) {
7413 free(id);
7414 return err;
7416 err = got_object_open_as_commit(&ci, s->repo,
7417 id);
7418 if (err) {
7419 free(id);
7420 return err;
7422 t = got_object_commit_get_committer_time(ci);
7423 got_object_commit_close(ci);
7424 } else {
7425 t = got_object_tag_get_tagger_time(tag);
7426 got_object_tag_close(tag);
7428 free(id);
7429 if (gmtime_r(&t, &tm) == NULL)
7430 return got_error_from_errno("gmtime_r");
7431 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7432 return got_error(GOT_ERR_NO_SPACE);
7434 if (got_ref_is_symbolic(re->ref)) {
7435 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7436 ymd : "", got_ref_get_name(re->ref),
7437 got_ref_get_symref_target(re->ref)) == -1)
7438 return got_error_from_errno("asprintf");
7439 } else if (s->show_ids) {
7440 struct got_object_id *id;
7441 char *id_str;
7442 err = got_ref_resolve(&id, s->repo, re->ref);
7443 if (err)
7444 return err;
7445 err = got_object_id_str(&id_str, id);
7446 if (err) {
7447 free(id);
7448 return err;
7450 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7451 got_ref_get_name(re->ref), id_str) == -1) {
7452 err = got_error_from_errno("asprintf");
7453 free(id);
7454 free(id_str);
7455 return err;
7457 free(id);
7458 free(id_str);
7459 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7460 got_ref_get_name(re->ref)) == -1)
7461 return got_error_from_errno("asprintf");
7463 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7464 0, 0);
7465 if (err) {
7466 free(line);
7467 return err;
7469 if (n == s->selected) {
7470 if (view->focussed)
7471 wstandout(view->window);
7472 s->selected_entry = re;
7474 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7475 if (tc)
7476 wattr_on(view->window,
7477 COLOR_PAIR(tc->colorpair), NULL);
7478 waddwstr(view->window, wline);
7479 if (tc)
7480 wattr_off(view->window,
7481 COLOR_PAIR(tc->colorpair), NULL);
7482 if (width < view->ncols - 1)
7483 waddch(view->window, '\n');
7484 if (n == s->selected && view->focussed)
7485 wstandend(view->window);
7486 free(line);
7487 free(wline);
7488 wline = NULL;
7489 n++;
7490 s->ndisplayed++;
7491 s->last_displayed_entry = re;
7493 limit--;
7494 re = TAILQ_NEXT(re, entry);
7497 view_border(view);
7498 return err;
7501 static const struct got_error *
7502 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7503 struct tog_reflist_entry *re, struct got_repository *repo)
7505 const struct got_error *err = NULL;
7506 struct got_object_id *commit_id = NULL;
7507 struct tog_view *tree_view;
7509 *new_view = NULL;
7511 err = resolve_reflist_entry(&commit_id, re, repo);
7512 if (err) {
7513 if (err->code != GOT_ERR_OBJ_TYPE)
7514 return err;
7515 else
7516 return NULL;
7520 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7521 if (tree_view == NULL) {
7522 err = got_error_from_errno("view_open");
7523 goto done;
7526 err = open_tree_view(tree_view, commit_id,
7527 got_ref_get_name(re->ref), repo);
7528 if (err)
7529 goto done;
7531 *new_view = tree_view;
7532 done:
7533 free(commit_id);
7534 return err;
7536 static const struct got_error *
7537 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7539 const struct got_error *err = NULL;
7540 struct tog_ref_view_state *s = &view->state.ref;
7541 struct tog_view *log_view, *tree_view;
7542 struct tog_reflist_entry *re;
7543 int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7545 switch (ch) {
7546 case 'i':
7547 s->show_ids = !s->show_ids;
7548 view->count = 0;
7549 break;
7550 case 'm':
7551 s->show_date = !s->show_date;
7552 view->count = 0;
7553 break;
7554 case 'o':
7555 s->sort_by_date = !s->sort_by_date;
7556 view->count = 0;
7557 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7558 got_ref_cmp_by_commit_timestamp_descending :
7559 tog_ref_cmp_by_name, s->repo);
7560 if (err)
7561 break;
7562 got_reflist_object_id_map_free(tog_refs_idmap);
7563 err = got_reflist_object_id_map_create(&tog_refs_idmap,
7564 &tog_refs, s->repo);
7565 if (err)
7566 break;
7567 ref_view_free_refs(s);
7568 err = ref_view_load_refs(s);
7569 break;
7570 case KEY_ENTER:
7571 case '\r':
7572 view->count = 0;
7573 if (!s->selected_entry)
7574 break;
7575 if (view_is_parent_view(view))
7576 view_get_split(view, &begin_y, &begin_x);
7578 err = log_ref_entry(&log_view, begin_y, begin_x,
7579 s->selected_entry, s->repo);
7580 if (err)
7581 break;
7583 if (view_is_parent_view(view) &&
7584 view->mode == TOG_VIEW_SPLIT_HRZN) {
7585 err = view_init_hsplit(view, begin_y);
7586 if (err)
7587 break;
7590 view->focussed = 0;
7591 log_view->focussed = 1;
7592 log_view->mode = view->mode;
7593 log_view->nlines = view->lines - begin_y;
7594 if (view_is_parent_view(view)) {
7595 view_transfer_size(log_view, view);
7596 err = view_close_child(view);
7597 if (err)
7598 return err;
7599 err = view_set_child(view, log_view);
7600 if (err)
7601 return err;
7602 view->focus_child = 1;
7603 } else
7604 *new_view = log_view;
7605 break;
7606 case 't':
7607 view->count = 0;
7608 if (!s->selected_entry)
7609 break;
7610 if (view_is_parent_view(view))
7611 view_get_split(view, &begin_y, &begin_x);
7612 err = browse_ref_tree(&tree_view, begin_y, begin_x,
7613 s->selected_entry, s->repo);
7614 if (err || tree_view == NULL)
7615 break;
7616 if (view_is_parent_view(view) &&
7617 view->mode == TOG_VIEW_SPLIT_HRZN) {
7618 err = view_init_hsplit(view, begin_y);
7619 if (err)
7620 break;
7622 view->focussed = 0;
7623 tree_view->focussed = 1;
7624 tree_view->mode = view->mode;
7625 tree_view->nlines = view->lines - begin_y;
7626 if (view_is_parent_view(view)) {
7627 view_transfer_size(tree_view, view);
7628 err = view_close_child(view);
7629 if (err)
7630 return err;
7631 err = view_set_child(view, tree_view);
7632 if (err)
7633 return err;
7634 view->focus_child = 1;
7635 } else
7636 *new_view = tree_view;
7637 break;
7638 case 'g':
7639 case KEY_HOME:
7640 s->selected = 0;
7641 view->count = 0;
7642 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7643 break;
7644 case 'G':
7645 case KEY_END: {
7646 int eos = view->nlines - 1;
7648 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7649 --eos; /* border */
7650 s->selected = 0;
7651 view->count = 0;
7652 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7653 for (n = 0; n < eos; n++) {
7654 if (re == NULL)
7655 break;
7656 s->first_displayed_entry = re;
7657 re = TAILQ_PREV(re, tog_reflist_head, entry);
7659 if (n > 0)
7660 s->selected = n - 1;
7661 break;
7663 case 'k':
7664 case KEY_UP:
7665 case CTRL('p'):
7666 if (s->selected > 0) {
7667 s->selected--;
7668 break;
7670 ref_scroll_up(s, 1);
7671 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7672 view->count = 0;
7673 break;
7674 case CTRL('u'):
7675 case 'u':
7676 nscroll /= 2;
7677 /* FALL THROUGH */
7678 case KEY_PPAGE:
7679 case CTRL('b'):
7680 case 'b':
7681 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7682 s->selected -= MIN(nscroll, s->selected);
7683 ref_scroll_up(s, MAX(0, nscroll));
7684 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7685 view->count = 0;
7686 break;
7687 case 'j':
7688 case KEY_DOWN:
7689 case CTRL('n'):
7690 if (s->selected < s->ndisplayed - 1) {
7691 s->selected++;
7692 break;
7694 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7695 /* can't scroll any further */
7696 view->count = 0;
7697 break;
7699 ref_scroll_down(view, 1);
7700 break;
7701 case CTRL('d'):
7702 case 'd':
7703 nscroll /= 2;
7704 /* FALL THROUGH */
7705 case KEY_NPAGE:
7706 case CTRL('f'):
7707 case 'f':
7708 case ' ':
7709 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7710 /* can't scroll any further; move cursor down */
7711 if (s->selected < s->ndisplayed - 1)
7712 s->selected += MIN(nscroll,
7713 s->ndisplayed - s->selected - 1);
7714 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7715 s->selected += s->ndisplayed - s->selected - 1;
7716 view->count = 0;
7717 break;
7719 ref_scroll_down(view, nscroll);
7720 break;
7721 case CTRL('l'):
7722 view->count = 0;
7723 tog_free_refs();
7724 err = tog_load_refs(s->repo, s->sort_by_date);
7725 if (err)
7726 break;
7727 ref_view_free_refs(s);
7728 err = ref_view_load_refs(s);
7729 break;
7730 case KEY_RESIZE:
7731 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7732 s->selected = view->nlines - 2;
7733 break;
7734 default:
7735 view->count = 0;
7736 break;
7739 return err;
7742 __dead static void
7743 usage_ref(void)
7745 endwin();
7746 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7747 getprogname());
7748 exit(1);
7751 static const struct got_error *
7752 cmd_ref(int argc, char *argv[])
7754 const struct got_error *error;
7755 struct got_repository *repo = NULL;
7756 struct got_worktree *worktree = NULL;
7757 char *cwd = NULL, *repo_path = NULL;
7758 int ch;
7759 struct tog_view *view;
7760 int *pack_fds = NULL;
7762 while ((ch = getopt(argc, argv, "r:")) != -1) {
7763 switch (ch) {
7764 case 'r':
7765 repo_path = realpath(optarg, NULL);
7766 if (repo_path == NULL)
7767 return got_error_from_errno2("realpath",
7768 optarg);
7769 break;
7770 default:
7771 usage_ref();
7772 /* NOTREACHED */
7776 argc -= optind;
7777 argv += optind;
7779 if (argc > 1)
7780 usage_ref();
7782 error = got_repo_pack_fds_open(&pack_fds);
7783 if (error != NULL)
7784 goto done;
7786 if (repo_path == NULL) {
7787 cwd = getcwd(NULL, 0);
7788 if (cwd == NULL)
7789 return got_error_from_errno("getcwd");
7790 error = got_worktree_open(&worktree, cwd);
7791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7792 goto done;
7793 if (worktree)
7794 repo_path =
7795 strdup(got_worktree_get_repo_path(worktree));
7796 else
7797 repo_path = strdup(cwd);
7798 if (repo_path == NULL) {
7799 error = got_error_from_errno("strdup");
7800 goto done;
7804 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7805 if (error != NULL)
7806 goto done;
7808 init_curses();
7810 error = apply_unveil(got_repo_get_path(repo), NULL);
7811 if (error)
7812 goto done;
7814 error = tog_load_refs(repo, 0);
7815 if (error)
7816 goto done;
7818 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7819 if (view == NULL) {
7820 error = got_error_from_errno("view_open");
7821 goto done;
7824 error = open_ref_view(view, repo);
7825 if (error)
7826 goto done;
7828 if (worktree) {
7829 /* Release work tree lock. */
7830 got_worktree_close(worktree);
7831 worktree = NULL;
7833 error = view_loop(view);
7834 done:
7835 free(repo_path);
7836 free(cwd);
7837 if (repo) {
7838 const struct got_error *close_err = got_repo_close(repo);
7839 if (close_err)
7840 error = close_err;
7842 if (pack_fds) {
7843 const struct got_error *pack_err =
7844 got_repo_pack_fds_close(pack_fds);
7845 if (error == NULL)
7846 error = pack_err;
7848 tog_free_refs();
7849 return error;
7853 * If view was scrolled down to move the selected line into view when opening a
7854 * horizontal split, scroll back up when closing the split/toggling fullscreen.
7856 static void
7857 offset_selection_up(struct tog_view *view)
7859 switch (view->type) {
7860 case TOG_VIEW_BLAME: {
7861 struct tog_blame_view_state *s = &view->state.blame;
7862 if (s->first_displayed_line == 1) {
7863 s->selected_line = MAX(s->selected_line - view->offset,
7864 1);
7865 break;
7867 if (s->first_displayed_line > view->offset)
7868 s->first_displayed_line -= view->offset;
7869 else
7870 s->first_displayed_line = 1;
7871 s->selected_line += view->offset;
7872 break;
7874 case TOG_VIEW_LOG:
7875 log_scroll_up(&view->state.log, view->offset);
7876 view->state.log.selected += view->offset;
7877 break;
7878 case TOG_VIEW_REF:
7879 ref_scroll_up(&view->state.ref, view->offset);
7880 view->state.ref.selected += view->offset;
7881 break;
7882 case TOG_VIEW_TREE:
7883 tree_scroll_up(&view->state.tree, view->offset);
7884 view->state.tree.selected += view->offset;
7885 break;
7886 default:
7887 break;
7890 view->offset = 0;
7894 * If the selected line is in the section of screen covered by the bottom split,
7895 * scroll down offset lines to move it into view and index its new position.
7897 static const struct got_error *
7898 offset_selection_down(struct tog_view *view)
7900 const struct got_error *err = NULL;
7901 const struct got_error *(*scrolld)(struct tog_view *, int);
7902 int *selected = NULL;
7903 int header, offset;
7905 switch (view->type) {
7906 case TOG_VIEW_BLAME: {
7907 struct tog_blame_view_state *s = &view->state.blame;
7908 header = 3;
7909 scrolld = NULL;
7910 if (s->selected_line > view->nlines - header) {
7911 offset = abs(view->nlines - s->selected_line - header);
7912 s->first_displayed_line += offset;
7913 s->selected_line -= offset;
7914 view->offset = offset;
7916 break;
7918 case TOG_VIEW_LOG: {
7919 struct tog_log_view_state *s = &view->state.log;
7920 scrolld = &log_scroll_down;
7921 header = view_is_parent_view(view) ? 3 : 2;
7922 selected = &s->selected;
7923 break;
7925 case TOG_VIEW_REF: {
7926 struct tog_ref_view_state *s = &view->state.ref;
7927 scrolld = &ref_scroll_down;
7928 header = 3;
7929 selected = &s->selected;
7930 break;
7932 case TOG_VIEW_TREE: {
7933 struct tog_tree_view_state *s = &view->state.tree;
7934 scrolld = &tree_scroll_down;
7935 header = 5;
7936 selected = &s->selected;
7937 break;
7939 default:
7940 selected = NULL;
7941 scrolld = NULL;
7942 header = 0;
7943 break;
7946 if (selected && *selected > view->nlines - header) {
7947 offset = abs(view->nlines - *selected - header);
7948 view->offset = offset;
7949 if (scrolld && offset) {
7950 err = scrolld(view, offset);
7951 *selected -= offset;
7955 return err;
7958 static void
7959 list_commands(FILE *fp)
7961 size_t i;
7963 fprintf(fp, "commands:");
7964 for (i = 0; i < nitems(tog_commands); i++) {
7965 const struct tog_cmd *cmd = &tog_commands[i];
7966 fprintf(fp, " %s", cmd->name);
7968 fputc('\n', fp);
7971 __dead static void
7972 usage(int hflag, int status)
7974 FILE *fp = (status == 0) ? stdout : stderr;
7976 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7977 getprogname());
7978 if (hflag) {
7979 fprintf(fp, "lazy usage: %s path\n", getprogname());
7980 list_commands(fp);
7982 exit(status);
7985 static char **
7986 make_argv(int argc, ...)
7988 va_list ap;
7989 char **argv;
7990 int i;
7992 va_start(ap, argc);
7994 argv = calloc(argc, sizeof(char *));
7995 if (argv == NULL)
7996 err(1, "calloc");
7997 for (i = 0; i < argc; i++) {
7998 argv[i] = strdup(va_arg(ap, char *));
7999 if (argv[i] == NULL)
8000 err(1, "strdup");
8003 va_end(ap);
8004 return argv;
8008 * Try to convert 'tog path' into a 'tog log path' command.
8009 * The user could simply have mistyped the command rather than knowingly
8010 * provided a path. So check whether argv[0] can in fact be resolved
8011 * to a path in the HEAD commit and print a special error if not.
8012 * This hack is for mpi@ <3
8014 static const struct got_error *
8015 tog_log_with_path(int argc, char *argv[])
8017 const struct got_error *error = NULL, *close_err;
8018 const struct tog_cmd *cmd = NULL;
8019 struct got_repository *repo = NULL;
8020 struct got_worktree *worktree = NULL;
8021 struct got_object_id *commit_id = NULL, *id = NULL;
8022 struct got_commit_object *commit = NULL;
8023 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8024 char *commit_id_str = NULL, **cmd_argv = NULL;
8025 int *pack_fds = NULL;
8027 cwd = getcwd(NULL, 0);
8028 if (cwd == NULL)
8029 return got_error_from_errno("getcwd");
8031 error = got_repo_pack_fds_open(&pack_fds);
8032 if (error != NULL)
8033 goto done;
8035 error = got_worktree_open(&worktree, cwd);
8036 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8037 goto done;
8039 if (worktree)
8040 repo_path = strdup(got_worktree_get_repo_path(worktree));
8041 else
8042 repo_path = strdup(cwd);
8043 if (repo_path == NULL) {
8044 error = got_error_from_errno("strdup");
8045 goto done;
8048 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8049 if (error != NULL)
8050 goto done;
8052 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8053 repo, worktree);
8054 if (error)
8055 goto done;
8057 error = tog_load_refs(repo, 0);
8058 if (error)
8059 goto done;
8060 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8061 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8062 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8063 if (error)
8064 goto done;
8066 if (worktree) {
8067 got_worktree_close(worktree);
8068 worktree = NULL;
8071 error = got_object_open_as_commit(&commit, repo, commit_id);
8072 if (error)
8073 goto done;
8075 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8076 if (error) {
8077 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8078 goto done;
8079 fprintf(stderr, "%s: '%s' is no known command or path\n",
8080 getprogname(), argv[0]);
8081 usage(1, 1);
8082 /* not reached */
8085 close_err = got_repo_close(repo);
8086 if (error == NULL)
8087 error = close_err;
8088 repo = NULL;
8090 error = got_object_id_str(&commit_id_str, commit_id);
8091 if (error)
8092 goto done;
8094 cmd = &tog_commands[0]; /* log */
8095 argc = 4;
8096 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8097 error = cmd->cmd_main(argc, cmd_argv);
8098 done:
8099 if (repo) {
8100 close_err = got_repo_close(repo);
8101 if (error == NULL)
8102 error = close_err;
8104 if (commit)
8105 got_object_commit_close(commit);
8106 if (worktree)
8107 got_worktree_close(worktree);
8108 if (pack_fds) {
8109 const struct got_error *pack_err =
8110 got_repo_pack_fds_close(pack_fds);
8111 if (error == NULL)
8112 error = pack_err;
8114 free(id);
8115 free(commit_id_str);
8116 free(commit_id);
8117 free(cwd);
8118 free(repo_path);
8119 free(in_repo_path);
8120 if (cmd_argv) {
8121 int i;
8122 for (i = 0; i < argc; i++)
8123 free(cmd_argv[i]);
8124 free(cmd_argv);
8126 tog_free_refs();
8127 return error;
8130 int
8131 main(int argc, char *argv[])
8133 const struct got_error *error = NULL;
8134 const struct tog_cmd *cmd = NULL;
8135 int ch, hflag = 0, Vflag = 0;
8136 char **cmd_argv = NULL;
8137 static const struct option longopts[] = {
8138 { "version", no_argument, NULL, 'V' },
8139 { NULL, 0, NULL, 0}
8141 char *diff_algo_str = NULL;
8143 setlocale(LC_CTYPE, "");
8145 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8146 switch (ch) {
8147 case 'h':
8148 hflag = 1;
8149 break;
8150 case 'V':
8151 Vflag = 1;
8152 break;
8153 default:
8154 usage(hflag, 1);
8155 /* NOTREACHED */
8159 argc -= optind;
8160 argv += optind;
8161 optind = 1;
8162 optreset = 1;
8164 if (Vflag) {
8165 got_version_print_str();
8166 return 0;
8169 #ifndef PROFILE
8170 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8171 NULL) == -1)
8172 err(1, "pledge");
8173 #endif
8175 if (argc == 0) {
8176 if (hflag)
8177 usage(hflag, 0);
8178 /* Build an argument vector which runs a default command. */
8179 cmd = &tog_commands[0];
8180 argc = 1;
8181 cmd_argv = make_argv(argc, cmd->name);
8182 } else {
8183 size_t i;
8185 /* Did the user specify a command? */
8186 for (i = 0; i < nitems(tog_commands); i++) {
8187 if (strncmp(tog_commands[i].name, argv[0],
8188 strlen(argv[0])) == 0) {
8189 cmd = &tog_commands[i];
8190 break;
8195 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8196 if (diff_algo_str) {
8197 if (strcasecmp(diff_algo_str, "patience") == 0)
8198 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8199 if (strcasecmp(diff_algo_str, "myers") == 0)
8200 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8203 if (cmd == NULL) {
8204 if (argc != 1)
8205 usage(0, 1);
8206 /* No command specified; try log with a path */
8207 error = tog_log_with_path(argc, argv);
8208 } else {
8209 if (hflag)
8210 cmd->cmd_usage();
8211 else
8212 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8215 endwin();
8216 putchar('\n');
8217 if (cmd_argv) {
8218 int i;
8219 for (i = 0; i < argc; i++)
8220 free(cmd_argv[i]);
8221 free(cmd_argv);
8224 if (error && error->code != GOT_ERR_CANCELLED)
8225 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8226 return 0;