Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #if defined(__FreeBSD__) || defined(__APPLE__)
24 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 #endif
26 #include <curses.h>
27 #include <panel.h>
28 #include <locale.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <getopt.h>
34 #include <string.h>
35 #include <err.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <wchar.h>
39 #include <time.h>
40 #include <pthread.h>
41 #include <libgen.h>
42 #include <regex.h>
43 #include <sched.h>
45 #include "got_compat.h"
47 #include "got_version.h"
48 #include "got_error.h"
49 #include "got_object.h"
50 #include "got_reference.h"
51 #include "got_repository.h"
52 #include "got_diff.h"
53 #include "got_opentemp.h"
54 #include "got_utf8.h"
55 #include "got_cancel.h"
56 #include "got_commit_graph.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_path.h"
60 #include "got_worktree.h"
62 #ifndef MIN
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #endif
66 #ifndef MAX
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #endif
70 #ifndef CTRL
71 #define CTRL(x) ((x) & 0x1f)
72 #endif
74 #ifndef nitems
75 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 #endif
78 struct tog_cmd {
79 const char *name;
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 };
84 __dead static void usage(int, int);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_ref(void);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct tog_cmd tog_commands[] = {
98 { "log", cmd_log, usage_log },
99 { "diff", cmd_diff, usage_diff },
100 { "blame", cmd_blame, usage_blame },
101 { "tree", cmd_tree, usage_tree },
102 { "ref", cmd_ref, usage_ref },
103 };
105 enum tog_view_type {
106 TOG_VIEW_DIFF,
107 TOG_VIEW_LOG,
108 TOG_VIEW_BLAME,
109 TOG_VIEW_TREE,
110 TOG_VIEW_REF,
111 };
113 enum tog_view_mode {
114 TOG_VIEW_SPLIT_NONE,
115 TOG_VIEW_SPLIT_VERT,
116 TOG_VIEW_SPLIT_HRZN
117 };
119 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
121 #define TOG_EOF_STRING "(END)"
123 struct commit_queue_entry {
124 TAILQ_ENTRY(commit_queue_entry) entry;
125 struct got_object_id *id;
126 struct got_commit_object *commit;
127 int idx;
128 };
129 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
130 struct commit_queue {
131 int ncommits;
132 struct commit_queue_head head;
133 };
135 struct tog_color {
136 STAILQ_ENTRY(tog_color) entry;
137 regex_t regex;
138 short colorpair;
139 };
140 STAILQ_HEAD(tog_colors, tog_color);
142 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
143 static struct got_reflist_object_id_map *tog_refs_idmap;
144 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
146 static const struct got_error *
147 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
148 struct got_reference* re2)
150 const char *name1 = got_ref_get_name(re1);
151 const char *name2 = got_ref_get_name(re2);
152 int isbackup1, isbackup2;
154 /* Sort backup refs towards the bottom of the list. */
155 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
156 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
157 if (!isbackup1 && isbackup2) {
158 *cmp = -1;
159 return NULL;
160 } else if (isbackup1 && !isbackup2) {
161 *cmp = 1;
162 return NULL;
165 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
166 return NULL;
169 static const struct got_error *
170 tog_load_refs(struct got_repository *repo, int sort_by_date)
172 const struct got_error *err;
174 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
175 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
176 repo);
177 if (err)
178 return err;
180 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
181 repo);
184 static void
185 tog_free_refs(void)
187 if (tog_refs_idmap) {
188 got_reflist_object_id_map_free(tog_refs_idmap);
189 tog_refs_idmap = NULL;
191 got_ref_list_free(&tog_refs);
194 static const struct got_error *
195 add_color(struct tog_colors *colors, const char *pattern,
196 int idx, short color)
198 const struct got_error *err = NULL;
199 struct tog_color *tc;
200 int regerr = 0;
202 if (idx < 1 || idx > COLOR_PAIRS - 1)
203 return NULL;
205 init_pair(idx, color, -1);
207 tc = calloc(1, sizeof(*tc));
208 if (tc == NULL)
209 return got_error_from_errno("calloc");
210 regerr = regcomp(&tc->regex, pattern,
211 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
212 if (regerr) {
213 static char regerr_msg[512];
214 static char err_msg[512];
215 regerror(regerr, &tc->regex, regerr_msg,
216 sizeof(regerr_msg));
217 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
218 regerr_msg);
219 err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 free(tc);
221 return err;
223 tc->colorpair = idx;
224 STAILQ_INSERT_HEAD(colors, tc, entry);
225 return NULL;
228 static void
229 free_colors(struct tog_colors *colors)
231 struct tog_color *tc;
233 while (!STAILQ_EMPTY(colors)) {
234 tc = STAILQ_FIRST(colors);
235 STAILQ_REMOVE_HEAD(colors, entry);
236 regfree(&tc->regex);
237 free(tc);
241 static struct tog_color *
242 get_color(struct tog_colors *colors, int colorpair)
244 struct tog_color *tc = NULL;
246 STAILQ_FOREACH(tc, colors, entry) {
247 if (tc->colorpair == colorpair)
248 return tc;
251 return NULL;
254 static int
255 default_color_value(const char *envvar)
257 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
258 return COLOR_MAGENTA;
259 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
260 return COLOR_CYAN;
261 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
262 return COLOR_YELLOW;
263 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
264 return COLOR_GREEN;
265 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
266 return COLOR_MAGENTA;
267 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
270 return COLOR_CYAN;
271 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
272 return COLOR_GREEN;
273 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
274 return COLOR_GREEN;
275 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
276 return COLOR_CYAN;
277 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
278 return COLOR_YELLOW;
279 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
280 return COLOR_GREEN;
281 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
282 return COLOR_MAGENTA;
283 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
284 return COLOR_YELLOW;
285 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
286 return COLOR_CYAN;
288 return -1;
291 static int
292 get_color_value(const char *envvar)
294 const char *val = getenv(envvar);
296 if (val == NULL)
297 return default_color_value(envvar);
299 if (strcasecmp(val, "black") == 0)
300 return COLOR_BLACK;
301 if (strcasecmp(val, "red") == 0)
302 return COLOR_RED;
303 if (strcasecmp(val, "green") == 0)
304 return COLOR_GREEN;
305 if (strcasecmp(val, "yellow") == 0)
306 return COLOR_YELLOW;
307 if (strcasecmp(val, "blue") == 0)
308 return COLOR_BLUE;
309 if (strcasecmp(val, "magenta") == 0)
310 return COLOR_MAGENTA;
311 if (strcasecmp(val, "cyan") == 0)
312 return COLOR_CYAN;
313 if (strcasecmp(val, "white") == 0)
314 return COLOR_WHITE;
315 if (strcasecmp(val, "default") == 0)
316 return -1;
318 return default_color_value(envvar);
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 lineno;
327 int first_displayed_line;
328 int last_displayed_line;
329 int eof;
330 int diff_context;
331 int ignore_whitespace;
332 int force_text_diff;
333 struct got_repository *repo;
334 struct got_diff_line *lines;
335 size_t nlines;
336 int matched_line;
337 int selected_line;
339 /* passed from log or blame view; may be NULL */
340 struct tog_view *parent_view;
341 };
343 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 static volatile sig_atomic_t tog_thread_error;
346 struct tog_log_thread_args {
347 pthread_cond_t need_commits;
348 pthread_cond_t commit_loaded;
349 int commits_needed;
350 int load_all;
351 struct got_commit_graph *graph;
352 struct commit_queue *real_commits;
353 const char *in_repo_path;
354 struct got_object_id *start_id;
355 struct got_repository *repo;
356 int *pack_fds;
357 int log_complete;
358 sig_atomic_t *quit;
359 struct commit_queue_entry **first_displayed_entry;
360 struct commit_queue_entry **selected_entry;
361 int *searching;
362 int *search_next_done;
363 regex_t *regex;
364 int *limiting;
365 int limit_match;
366 regex_t *limit_regex;
367 struct commit_queue *limit_commits;
368 };
370 struct tog_log_view_state {
371 struct commit_queue *commits;
372 struct commit_queue_entry *first_displayed_entry;
373 struct commit_queue_entry *last_displayed_entry;
374 struct commit_queue_entry *selected_entry;
375 struct commit_queue real_commits;
376 int selected;
377 char *in_repo_path;
378 char *head_ref_name;
379 int log_branches;
380 struct got_repository *repo;
381 struct got_object_id *start_id;
382 sig_atomic_t quit;
383 pthread_t thread;
384 struct tog_log_thread_args thread_args;
385 struct commit_queue_entry *matched_entry;
386 struct commit_queue_entry *search_entry;
387 struct tog_colors colors;
388 int use_committer;
389 int limit_view;
390 regex_t limit_regex;
391 struct commit_queue limit_commits;
392 };
394 #define TOG_COLOR_DIFF_MINUS 1
395 #define TOG_COLOR_DIFF_PLUS 2
396 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
397 #define TOG_COLOR_DIFF_META 4
398 #define TOG_COLOR_TREE_SUBMODULE 5
399 #define TOG_COLOR_TREE_SYMLINK 6
400 #define TOG_COLOR_TREE_DIRECTORY 7
401 #define TOG_COLOR_TREE_EXECUTABLE 8
402 #define TOG_COLOR_COMMIT 9
403 #define TOG_COLOR_AUTHOR 10
404 #define TOG_COLOR_DATE 11
405 #define TOG_COLOR_REFS_HEADS 12
406 #define TOG_COLOR_REFS_TAGS 13
407 #define TOG_COLOR_REFS_REMOTES 14
408 #define TOG_COLOR_REFS_BACKUP 15
410 struct tog_blame_cb_args {
411 struct tog_blame_line *lines; /* one per line */
412 int nlines;
414 struct tog_view *view;
415 struct got_object_id *commit_id;
416 int *quit;
417 };
419 struct tog_blame_thread_args {
420 const char *path;
421 struct got_repository *repo;
422 struct tog_blame_cb_args *cb_args;
423 int *complete;
424 got_cancel_cb cancel_cb;
425 void *cancel_arg;
426 };
428 struct tog_blame {
429 FILE *f;
430 off_t filesize;
431 struct tog_blame_line *lines;
432 int nlines;
433 off_t *line_offsets;
434 pthread_t thread;
435 struct tog_blame_thread_args thread_args;
436 struct tog_blame_cb_args cb_args;
437 const char *path;
438 int *pack_fds;
439 };
441 struct tog_blame_view_state {
442 int first_displayed_line;
443 int last_displayed_line;
444 int selected_line;
445 int last_diffed_line;
446 int blame_complete;
447 int eof;
448 int done;
449 struct got_object_id_queue blamed_commits;
450 struct got_object_qid *blamed_commit;
451 char *path;
452 struct got_repository *repo;
453 struct got_object_id *commit_id;
454 struct got_object_id *id_to_log;
455 struct tog_blame blame;
456 int matched_line;
457 struct tog_colors colors;
458 };
460 struct tog_parent_tree {
461 TAILQ_ENTRY(tog_parent_tree) entry;
462 struct got_tree_object *tree;
463 struct got_tree_entry *first_displayed_entry;
464 struct got_tree_entry *selected_entry;
465 int selected;
466 };
468 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
470 struct tog_tree_view_state {
471 char *tree_label;
472 struct got_object_id *commit_id;/* commit which this tree belongs to */
473 struct got_tree_object *root; /* the commit's root tree entry */
474 struct got_tree_object *tree; /* currently displayed (sub-)tree */
475 struct got_tree_entry *first_displayed_entry;
476 struct got_tree_entry *last_displayed_entry;
477 struct got_tree_entry *selected_entry;
478 int ndisplayed, selected, show_ids;
479 struct tog_parent_trees parents; /* parent trees of current sub-tree */
480 char *head_ref_name;
481 struct got_repository *repo;
482 struct got_tree_entry *matched_entry;
483 struct tog_colors colors;
484 };
486 struct tog_reflist_entry {
487 TAILQ_ENTRY(tog_reflist_entry) entry;
488 struct got_reference *ref;
489 int idx;
490 };
492 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
494 struct tog_ref_view_state {
495 struct tog_reflist_head refs;
496 struct tog_reflist_entry *first_displayed_entry;
497 struct tog_reflist_entry *last_displayed_entry;
498 struct tog_reflist_entry *selected_entry;
499 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
500 struct got_repository *repo;
501 struct tog_reflist_entry *matched_entry;
502 struct tog_colors colors;
503 };
505 /*
506 * We implement two types of views: parent views and child views.
508 * The 'Tab' key switches focus between a parent view and its child view.
509 * Child views are shown side-by-side to their parent view, provided
510 * there is enough screen estate.
512 * When a new view is opened from within a parent view, this new view
513 * becomes a child view of the parent view, replacing any existing child.
515 * When a new view is opened from within a child view, this new view
516 * becomes a parent view which will obscure the views below until the
517 * user quits the new parent view by typing 'q'.
519 * This list of views contains parent views only.
520 * Child views are only pointed to by their parent view.
521 */
522 TAILQ_HEAD(tog_view_list_head, tog_view);
524 struct tog_view {
525 TAILQ_ENTRY(tog_view) entry;
526 WINDOW *window;
527 PANEL *panel;
528 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
529 int resized_y, resized_x; /* begin_y/x based on user resizing */
530 int maxx, x; /* max column and current start column */
531 int lines, cols; /* copies of LINES and COLS */
532 int nscrolled, offset; /* lines scrolled and hsplit line offset */
533 int gline, hiline; /* navigate to and highlight this nG line */
534 int ch, count; /* current keymap and count prefix */
535 int resized; /* set when in a resize event */
536 int focussed; /* Only set on one parent or child view at a time. */
537 int dying;
538 struct tog_view *parent;
539 struct tog_view *child;
541 /*
542 * This flag is initially set on parent views when a new child view
543 * is created. It gets toggled when the 'Tab' key switches focus
544 * between parent and child.
545 * The flag indicates whether focus should be passed on to our child
546 * view if this parent view gets picked for focus after another parent
547 * view was closed. This prevents child views from losing focus in such
548 * situations.
549 */
550 int focus_child;
552 enum tog_view_mode mode;
553 /* type-specific state */
554 enum tog_view_type type;
555 union {
556 struct tog_diff_view_state diff;
557 struct tog_log_view_state log;
558 struct tog_blame_view_state blame;
559 struct tog_tree_view_state tree;
560 struct tog_ref_view_state ref;
561 } state;
563 const struct got_error *(*show)(struct tog_view *);
564 const struct got_error *(*input)(struct tog_view **,
565 struct tog_view *, int);
566 const struct got_error *(*reset)(struct tog_view *);
567 const struct got_error *(*resize)(struct tog_view *, int);
568 const struct got_error *(*close)(struct tog_view *);
570 const struct got_error *(*search_start)(struct tog_view *);
571 const struct got_error *(*search_next)(struct tog_view *);
572 int search_started;
573 int searching;
574 #define TOG_SEARCH_FORWARD 1
575 #define TOG_SEARCH_BACKWARD 2
576 int search_next_done;
577 #define TOG_SEARCH_HAVE_MORE 1
578 #define TOG_SEARCH_NO_MORE 2
579 #define TOG_SEARCH_HAVE_NONE 3
580 regex_t regex;
581 regmatch_t regmatch;
582 };
584 static const struct got_error *open_diff_view(struct tog_view *,
585 struct got_object_id *, struct got_object_id *,
586 const char *, const char *, int, int, int, struct tog_view *,
587 struct got_repository *);
588 static const struct got_error *show_diff_view(struct tog_view *);
589 static const struct got_error *input_diff_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *reset_diff_view(struct tog_view *);
592 static const struct got_error* close_diff_view(struct tog_view *);
593 static const struct got_error *search_start_diff_view(struct tog_view *);
594 static const struct got_error *search_next_diff_view(struct tog_view *);
596 static const struct got_error *open_log_view(struct tog_view *,
597 struct got_object_id *, struct got_repository *,
598 const char *, const char *, int);
599 static const struct got_error * show_log_view(struct tog_view *);
600 static const struct got_error *input_log_view(struct tog_view **,
601 struct tog_view *, int);
602 static const struct got_error *resize_log_view(struct tog_view *, int);
603 static const struct got_error *close_log_view(struct tog_view *);
604 static const struct got_error *search_start_log_view(struct tog_view *);
605 static const struct got_error *search_next_log_view(struct tog_view *);
607 static const struct got_error *open_blame_view(struct tog_view *, char *,
608 struct got_object_id *, struct got_repository *);
609 static const struct got_error *show_blame_view(struct tog_view *);
610 static const struct got_error *input_blame_view(struct tog_view **,
611 struct tog_view *, int);
612 static const struct got_error *reset_blame_view(struct tog_view *);
613 static const struct got_error *close_blame_view(struct tog_view *);
614 static const struct got_error *search_start_blame_view(struct tog_view *);
615 static const struct got_error *search_next_blame_view(struct tog_view *);
617 static const struct got_error *open_tree_view(struct tog_view *,
618 struct got_object_id *, const char *, struct got_repository *);
619 static const struct got_error *show_tree_view(struct tog_view *);
620 static const struct got_error *input_tree_view(struct tog_view **,
621 struct tog_view *, int);
622 static const struct got_error *close_tree_view(struct tog_view *);
623 static const struct got_error *search_start_tree_view(struct tog_view *);
624 static const struct got_error *search_next_tree_view(struct tog_view *);
626 static const struct got_error *open_ref_view(struct tog_view *,
627 struct got_repository *);
628 static const struct got_error *show_ref_view(struct tog_view *);
629 static const struct got_error *input_ref_view(struct tog_view **,
630 struct tog_view *, int);
631 static const struct got_error *close_ref_view(struct tog_view *);
632 static const struct got_error *search_start_ref_view(struct tog_view *);
633 static const struct got_error *search_next_ref_view(struct tog_view *);
635 static volatile sig_atomic_t tog_sigwinch_received;
636 static volatile sig_atomic_t tog_sigpipe_received;
637 static volatile sig_atomic_t tog_sigcont_received;
638 static volatile sig_atomic_t tog_sigint_received;
639 static volatile sig_atomic_t tog_sigterm_received;
641 static void
642 tog_sigwinch(int signo)
644 tog_sigwinch_received = 1;
647 static void
648 tog_sigpipe(int signo)
650 tog_sigpipe_received = 1;
653 static void
654 tog_sigcont(int signo)
656 tog_sigcont_received = 1;
659 static void
660 tog_sigint(int signo)
662 tog_sigint_received = 1;
665 static void
666 tog_sigterm(int signo)
668 tog_sigterm_received = 1;
671 static int
672 tog_fatal_signal_received(void)
674 return (tog_sigpipe_received ||
675 tog_sigint_received || tog_sigint_received);
678 static const struct got_error *
679 view_close(struct tog_view *view)
681 const struct got_error *err = NULL, *child_err = NULL;
683 if (view->child) {
684 child_err = view_close(view->child);
685 view->child = NULL;
687 if (view->close)
688 err = view->close(view);
689 if (view->panel)
690 del_panel(view->panel);
691 if (view->window)
692 delwin(view->window);
693 free(view);
694 return err ? err : child_err;
697 static struct tog_view *
698 view_open(int nlines, int ncols, int begin_y, int begin_x,
699 enum tog_view_type type)
701 struct tog_view *view = calloc(1, sizeof(*view));
703 if (view == NULL)
704 return NULL;
706 view->type = type;
707 view->lines = LINES;
708 view->cols = COLS;
709 view->nlines = nlines ? nlines : LINES - begin_y;
710 view->ncols = ncols ? ncols : COLS - begin_x;
711 view->begin_y = begin_y;
712 view->begin_x = begin_x;
713 view->window = newwin(nlines, ncols, begin_y, begin_x);
714 if (view->window == NULL) {
715 view_close(view);
716 return NULL;
718 view->panel = new_panel(view->window);
719 if (view->panel == NULL ||
720 set_panel_userptr(view->panel, view) != OK) {
721 view_close(view);
722 return NULL;
725 keypad(view->window, TRUE);
726 return view;
729 static int
730 view_split_begin_x(int begin_x)
732 if (begin_x > 0 || COLS < 120)
733 return 0;
734 return (COLS - MAX(COLS / 2, 80));
737 /* XXX Stub till we decide what to do. */
738 static int
739 view_split_begin_y(int lines)
741 return lines * HSPLIT_SCALE;
744 static const struct got_error *view_resize(struct tog_view *);
746 static const struct got_error *
747 view_splitscreen(struct tog_view *view)
749 const struct got_error *err = NULL;
751 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
752 if (view->resized_y && view->resized_y < view->lines)
753 view->begin_y = view->resized_y;
754 else
755 view->begin_y = view_split_begin_y(view->nlines);
756 view->begin_x = 0;
757 } else if (!view->resized) {
758 if (view->resized_x && view->resized_x < view->cols - 1 &&
759 view->cols > 119)
760 view->begin_x = view->resized_x;
761 else
762 view->begin_x = view_split_begin_x(0);
763 view->begin_y = 0;
765 view->nlines = LINES - view->begin_y;
766 view->ncols = COLS - view->begin_x;
767 view->lines = LINES;
768 view->cols = COLS;
769 err = view_resize(view);
770 if (err)
771 return err;
773 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
774 view->parent->nlines = view->begin_y;
776 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 return got_error_from_errno("mvwin");
779 return NULL;
782 static const struct got_error *
783 view_fullscreen(struct tog_view *view)
785 const struct got_error *err = NULL;
787 view->begin_x = 0;
788 view->begin_y = view->resized ? view->begin_y : 0;
789 view->nlines = view->resized ? view->nlines : LINES;
790 view->ncols = COLS;
791 view->lines = LINES;
792 view->cols = COLS;
793 err = view_resize(view);
794 if (err)
795 return err;
797 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
798 return got_error_from_errno("mvwin");
800 return NULL;
803 static int
804 view_is_parent_view(struct tog_view *view)
806 return view->parent == NULL;
809 static int
810 view_is_splitscreen(struct tog_view *view)
812 return view->begin_x > 0 || view->begin_y > 0;
815 static int
816 view_is_fullscreen(struct tog_view *view)
818 return view->nlines == LINES && view->ncols == COLS;
821 static int
822 view_is_hsplit_top(struct tog_view *view)
824 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
825 view_is_splitscreen(view->child);
828 static void
829 view_border(struct tog_view *view)
831 PANEL *panel;
832 const struct tog_view *view_above;
834 if (view->parent)
835 return view_border(view->parent);
837 panel = panel_above(view->panel);
838 if (panel == NULL)
839 return;
841 view_above = panel_userptr(panel);
842 if (view->mode == TOG_VIEW_SPLIT_HRZN)
843 mvwhline(view->window, view_above->begin_y - 1,
844 view->begin_x, got_locale_is_utf8() ?
845 ACS_HLINE : '-', view->ncols);
846 else
847 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
848 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
851 static const struct got_error *view_init_hsplit(struct tog_view *, int);
852 static const struct got_error *request_log_commits(struct tog_view *);
853 static const struct got_error *offset_selection_down(struct tog_view *);
854 static void offset_selection_up(struct tog_view *);
855 static void view_get_split(struct tog_view *, int *, int *);
857 static const struct got_error *
858 view_resize(struct tog_view *view)
860 const struct got_error *err = NULL;
861 int dif, nlines, ncols;
863 dif = LINES - view->lines; /* line difference */
865 if (view->lines > LINES)
866 nlines = view->nlines - (view->lines - LINES);
867 else
868 nlines = view->nlines + (LINES - view->lines);
869 if (view->cols > COLS)
870 ncols = view->ncols - (view->cols - COLS);
871 else
872 ncols = view->ncols + (COLS - view->cols);
874 if (view->child) {
875 int hs = view->child->begin_y;
877 if (!view_is_fullscreen(view))
878 view->child->begin_x = view_split_begin_x(view->begin_x);
879 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
880 view->child->begin_x == 0) {
881 ncols = COLS;
883 view_fullscreen(view->child);
884 if (view->child->focussed)
885 show_panel(view->child->panel);
886 else
887 show_panel(view->panel);
888 } else {
889 ncols = view->child->begin_x;
891 view_splitscreen(view->child);
892 show_panel(view->child->panel);
894 /*
895 * XXX This is ugly and needs to be moved into the above
896 * logic but "works" for now and my attempts at moving it
897 * break either 'tab' or 'F' key maps in horizontal splits.
898 */
899 if (hs) {
900 err = view_splitscreen(view->child);
901 if (err)
902 return err;
903 if (dif < 0) { /* top split decreased */
904 err = offset_selection_down(view);
905 if (err)
906 return err;
908 view_border(view);
909 update_panels();
910 doupdate();
911 show_panel(view->child->panel);
912 nlines = view->nlines;
914 } else if (view->parent == NULL)
915 ncols = COLS;
917 if (view->resize && dif > 0) {
918 err = view->resize(view, dif);
919 if (err)
920 return err;
923 if (wresize(view->window, nlines, ncols) == ERR)
924 return got_error_from_errno("wresize");
925 if (replace_panel(view->panel, view->window) == ERR)
926 return got_error_from_errno("replace_panel");
927 wclear(view->window);
929 view->nlines = nlines;
930 view->ncols = ncols;
931 view->lines = LINES;
932 view->cols = COLS;
934 return NULL;
937 static const struct got_error *
938 resize_log_view(struct tog_view *view, int increase)
940 struct tog_log_view_state *s = &view->state.log;
941 const struct got_error *err = NULL;
942 int n = 0;
944 if (s->selected_entry)
945 n = s->selected_entry->idx + view->lines - s->selected;
947 /*
948 * Request commits to account for the increased
949 * height so we have enough to populate the view.
950 */
951 if (s->commits->ncommits < n) {
952 view->nscrolled = n - s->commits->ncommits + increase + 1;
953 err = request_log_commits(view);
956 return err;
959 static void
960 view_adjust_offset(struct tog_view *view, int n)
962 if (n == 0)
963 return;
965 if (view->parent && view->parent->offset) {
966 if (view->parent->offset + n >= 0)
967 view->parent->offset += n;
968 else
969 view->parent->offset = 0;
970 } else if (view->offset) {
971 if (view->offset - n >= 0)
972 view->offset -= n;
973 else
974 view->offset = 0;
978 static const struct got_error *
979 view_resize_split(struct tog_view *view, int resize)
981 const struct got_error *err = NULL;
982 struct tog_view *v = NULL;
984 if (view->parent)
985 v = view->parent;
986 else
987 v = view;
989 if (!v->child || !view_is_splitscreen(v->child))
990 return NULL;
992 v->resized = v->child->resized = resize; /* lock for resize event */
994 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
995 if (v->child->resized_y)
996 v->child->begin_y = v->child->resized_y;
997 if (view->parent)
998 v->child->begin_y -= resize;
999 else
1000 v->child->begin_y += resize;
1001 if (v->child->begin_y < 3) {
1002 view->count = 0;
1003 v->child->begin_y = 3;
1004 } else if (v->child->begin_y > LINES - 1) {
1005 view->count = 0;
1006 v->child->begin_y = LINES - 1;
1008 v->ncols = COLS;
1009 v->child->ncols = COLS;
1010 view_adjust_offset(view, resize);
1011 err = view_init_hsplit(v, v->child->begin_y);
1012 if (err)
1013 return err;
1014 v->child->resized_y = v->child->begin_y;
1015 } else {
1016 if (v->child->resized_x)
1017 v->child->begin_x = v->child->resized_x;
1018 if (view->parent)
1019 v->child->begin_x -= resize;
1020 else
1021 v->child->begin_x += resize;
1022 if (v->child->begin_x < 11) {
1023 view->count = 0;
1024 v->child->begin_x = 11;
1025 } else if (v->child->begin_x > COLS - 1) {
1026 view->count = 0;
1027 v->child->begin_x = COLS - 1;
1029 v->child->resized_x = v->child->begin_x;
1032 v->child->mode = v->mode;
1033 v->child->nlines = v->lines - v->child->begin_y;
1034 v->child->ncols = v->cols - v->child->begin_x;
1035 v->focus_child = 1;
1037 err = view_fullscreen(v);
1038 if (err)
1039 return err;
1040 err = view_splitscreen(v->child);
1041 if (err)
1042 return err;
1044 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1045 err = offset_selection_down(v->child);
1046 if (err)
1047 return err;
1050 if (v->resize)
1051 err = v->resize(v, 0);
1052 else if (v->child->resize)
1053 err = v->child->resize(v->child, 0);
1055 v->resized = v->child->resized = 0;
1057 return err;
1060 static void
1061 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1063 struct tog_view *v = src->child ? src->child : src;
1065 dst->resized_x = v->resized_x;
1066 dst->resized_y = v->resized_y;
1069 static const struct got_error *
1070 view_close_child(struct tog_view *view)
1072 const struct got_error *err = NULL;
1074 if (view->child == NULL)
1075 return NULL;
1077 err = view_close(view->child);
1078 view->child = NULL;
1079 return err;
1082 static const struct got_error *
1083 view_set_child(struct tog_view *view, struct tog_view *child)
1085 const struct got_error *err = NULL;
1087 view->child = child;
1088 child->parent = view;
1090 err = view_resize(view);
1091 if (err)
1092 return err;
1094 if (view->child->resized_x || view->child->resized_y)
1095 err = view_resize_split(view, 0);
1097 return err;
1100 static const struct got_error *view_dispatch_request(struct tog_view **,
1101 struct tog_view *, enum tog_view_type, int, int);
1103 static const struct got_error *
1104 view_request_new(struct tog_view **requested, struct tog_view *view,
1105 enum tog_view_type request)
1107 struct tog_view *new_view = NULL;
1108 const struct got_error *err;
1109 int y = 0, x = 0;
1111 *requested = NULL;
1113 if (view_is_parent_view(view))
1114 view_get_split(view, &y, &x);
1116 err = view_dispatch_request(&new_view, view, request, y, x);
1117 if (err)
1118 return err;
1120 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1121 err = view_init_hsplit(view, y);
1122 if (err)
1123 return err;
1126 view->focussed = 0;
1127 new_view->focussed = 1;
1128 new_view->mode = view->mode;
1129 new_view->nlines = view->lines - y;
1131 if (view_is_parent_view(view)) {
1132 view_transfer_size(new_view, view);
1133 err = view_close_child(view);
1134 if (err)
1135 return err;
1136 err = view_set_child(view, new_view);
1137 if (err)
1138 return err;
1139 view->focus_child = 1;
1140 } else
1141 *requested = new_view;
1143 return NULL;
1146 static void
1147 tog_resizeterm(void)
1149 int cols, lines;
1150 struct winsize size;
1152 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1153 cols = 80; /* Default */
1154 lines = 24;
1155 } else {
1156 cols = size.ws_col;
1157 lines = size.ws_row;
1159 resize_term(lines, cols);
1162 static const struct got_error *
1163 view_search_start(struct tog_view *view)
1165 const struct got_error *err = NULL;
1166 struct tog_view *v = view;
1167 char pattern[1024];
1168 int ret;
1170 if (view->search_started) {
1171 regfree(&view->regex);
1172 view->searching = 0;
1173 memset(&view->regmatch, 0, sizeof(view->regmatch));
1175 view->search_started = 0;
1177 if (view->nlines < 1)
1178 return NULL;
1180 if (view_is_hsplit_top(view))
1181 v = view->child;
1183 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1184 wclrtoeol(v->window);
1186 nodelay(view->window, FALSE); /* block for search term input */
1187 nocbreak();
1188 echo();
1189 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1190 wrefresh(v->window);
1191 cbreak();
1192 noecho();
1193 nodelay(view->window, TRUE);
1194 if (ret == ERR)
1195 return NULL;
1197 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1198 err = view->search_start(view);
1199 if (err) {
1200 regfree(&view->regex);
1201 return err;
1203 view->search_started = 1;
1204 view->searching = TOG_SEARCH_FORWARD;
1205 view->search_next_done = 0;
1206 view->search_next(view);
1209 return NULL;
1212 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1213 static const struct got_error *
1214 switch_split(struct tog_view *view)
1216 const struct got_error *err = NULL;
1217 struct tog_view *v = NULL;
1219 if (view->parent)
1220 v = view->parent;
1221 else
1222 v = view;
1224 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1225 v->mode = TOG_VIEW_SPLIT_VERT;
1226 else
1227 v->mode = TOG_VIEW_SPLIT_HRZN;
1229 if (!v->child)
1230 return NULL;
1231 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1232 v->mode = TOG_VIEW_SPLIT_NONE;
1234 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1235 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1236 v->child->begin_y = v->child->resized_y;
1237 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1238 v->child->begin_x = v->child->resized_x;
1241 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1242 v->ncols = COLS;
1243 v->child->ncols = COLS;
1244 v->child->nscrolled = LINES - v->child->nlines;
1246 err = view_init_hsplit(v, v->child->begin_y);
1247 if (err)
1248 return err;
1250 v->child->mode = v->mode;
1251 v->child->nlines = v->lines - v->child->begin_y;
1252 v->focus_child = 1;
1254 err = view_fullscreen(v);
1255 if (err)
1256 return err;
1257 err = view_splitscreen(v->child);
1258 if (err)
1259 return err;
1261 if (v->mode == TOG_VIEW_SPLIT_NONE)
1262 v->mode = TOG_VIEW_SPLIT_VERT;
1263 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1264 err = offset_selection_down(v);
1265 if (err)
1266 return err;
1267 err = offset_selection_down(v->child);
1268 if (err)
1269 return err;
1270 } else {
1271 offset_selection_up(v);
1272 offset_selection_up(v->child);
1274 if (v->resize)
1275 err = v->resize(v, 0);
1276 else if (v->child->resize)
1277 err = v->child->resize(v->child, 0);
1279 return err;
1283 * Compute view->count from numeric input. Assign total to view->count and
1284 * return first non-numeric key entered.
1286 static int
1287 get_compound_key(struct tog_view *view, int c)
1289 struct tog_view *v = view;
1290 int x, n = 0;
1292 if (view_is_hsplit_top(view))
1293 v = view->child;
1294 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1295 v = view->parent;
1297 view->count = 0;
1298 cbreak(); /* block for input */
1299 nodelay(view->window, FALSE);
1300 wmove(v->window, v->nlines - 1, 0);
1301 wclrtoeol(v->window);
1302 waddch(v->window, ':');
1304 do {
1305 x = getcurx(v->window);
1306 if (x != ERR && x < view->ncols) {
1307 waddch(v->window, c);
1308 wrefresh(v->window);
1312 * Don't overflow. Max valid request should be the greatest
1313 * between the longest and total lines; cap at 10 million.
1315 if (n >= 9999999)
1316 n = 9999999;
1317 else
1318 n = n * 10 + (c - '0');
1319 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1321 if (c == 'G' || c == 'g') { /* nG key map */
1322 view->gline = view->hiline = n;
1323 n = 0;
1324 c = 0;
1327 /* Massage excessive or inapplicable values at the input handler. */
1328 view->count = n;
1330 return c;
1333 static const struct got_error *
1334 view_input(struct tog_view **new, int *done, struct tog_view *view,
1335 struct tog_view_list_head *views)
1337 const struct got_error *err = NULL;
1338 struct tog_view *v;
1339 int ch, errcode;
1341 *new = NULL;
1343 /* Clear "no matches" indicator. */
1344 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1345 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1346 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1347 view->count = 0;
1350 if (view->searching && !view->search_next_done) {
1351 errcode = pthread_mutex_unlock(&tog_mutex);
1352 if (errcode)
1353 return got_error_set_errno(errcode,
1354 "pthread_mutex_unlock");
1355 sched_yield();
1356 errcode = pthread_mutex_lock(&tog_mutex);
1357 if (errcode)
1358 return got_error_set_errno(errcode,
1359 "pthread_mutex_lock");
1360 view->search_next(view);
1361 return NULL;
1364 /* Allow threads to make progress while we are waiting for input. */
1365 errcode = pthread_mutex_unlock(&tog_mutex);
1366 if (errcode)
1367 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1368 /* If we have an unfinished count, let C-g or backspace abort. */
1369 if (view->count && --view->count) {
1370 cbreak();
1371 nodelay(view->window, TRUE);
1372 ch = wgetch(view->window);
1373 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1374 view->count = 0;
1375 else
1376 ch = view->ch;
1377 } else {
1378 ch = wgetch(view->window);
1379 if (ch >= '1' && ch <= '9')
1380 view->ch = ch = get_compound_key(view, ch);
1382 if (view->hiline && ch != ERR && ch != 0)
1383 view->hiline = 0; /* key pressed, clear line highlight */
1384 nodelay(view->window, TRUE);
1385 errcode = pthread_mutex_lock(&tog_mutex);
1386 if (errcode)
1387 return got_error_set_errno(errcode, "pthread_mutex_lock");
1389 if (tog_sigwinch_received || tog_sigcont_received) {
1390 tog_resizeterm();
1391 tog_sigwinch_received = 0;
1392 tog_sigcont_received = 0;
1393 TAILQ_FOREACH(v, views, entry) {
1394 err = view_resize(v);
1395 if (err)
1396 return err;
1397 err = v->input(new, v, KEY_RESIZE);
1398 if (err)
1399 return err;
1400 if (v->child) {
1401 err = view_resize(v->child);
1402 if (err)
1403 return err;
1404 err = v->child->input(new, v->child,
1405 KEY_RESIZE);
1406 if (err)
1407 return err;
1408 if (v->child->resized_x || v->child->resized_y) {
1409 err = view_resize_split(v, 0);
1410 if (err)
1411 return err;
1417 switch (ch) {
1418 case '\t':
1419 view->count = 0;
1420 if (view->child) {
1421 view->focussed = 0;
1422 view->child->focussed = 1;
1423 view->focus_child = 1;
1424 } else if (view->parent) {
1425 view->focussed = 0;
1426 view->parent->focussed = 1;
1427 view->parent->focus_child = 0;
1428 if (!view_is_splitscreen(view)) {
1429 if (view->parent->resize) {
1430 err = view->parent->resize(view->parent,
1431 0);
1432 if (err)
1433 return err;
1435 offset_selection_up(view->parent);
1436 err = view_fullscreen(view->parent);
1437 if (err)
1438 return err;
1441 break;
1442 case 'q':
1443 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1444 if (view->parent->resize) {
1445 /* might need more commits to fill fullscreen */
1446 err = view->parent->resize(view->parent, 0);
1447 if (err)
1448 break;
1450 offset_selection_up(view->parent);
1452 err = view->input(new, view, ch);
1453 view->dying = 1;
1454 break;
1455 case 'Q':
1456 *done = 1;
1457 break;
1458 case 'F':
1459 view->count = 0;
1460 if (view_is_parent_view(view)) {
1461 if (view->child == NULL)
1462 break;
1463 if (view_is_splitscreen(view->child)) {
1464 view->focussed = 0;
1465 view->child->focussed = 1;
1466 err = view_fullscreen(view->child);
1467 } else {
1468 err = view_splitscreen(view->child);
1469 if (!err)
1470 err = view_resize_split(view, 0);
1472 if (err)
1473 break;
1474 err = view->child->input(new, view->child,
1475 KEY_RESIZE);
1476 } else {
1477 if (view_is_splitscreen(view)) {
1478 view->parent->focussed = 0;
1479 view->focussed = 1;
1480 err = view_fullscreen(view);
1481 } else {
1482 err = view_splitscreen(view);
1483 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1484 err = view_resize(view->parent);
1485 if (!err)
1486 err = view_resize_split(view, 0);
1488 if (err)
1489 break;
1490 err = view->input(new, view, KEY_RESIZE);
1492 if (err)
1493 break;
1494 if (view->resize) {
1495 err = view->resize(view, 0);
1496 if (err)
1497 break;
1499 if (view->parent)
1500 err = offset_selection_down(view->parent);
1501 if (!err)
1502 err = offset_selection_down(view);
1503 break;
1504 case 'S':
1505 view->count = 0;
1506 err = switch_split(view);
1507 break;
1508 case '-':
1509 err = view_resize_split(view, -1);
1510 break;
1511 case '+':
1512 err = view_resize_split(view, 1);
1513 break;
1514 case KEY_RESIZE:
1515 break;
1516 case '/':
1517 view->count = 0;
1518 if (view->search_start)
1519 view_search_start(view);
1520 else
1521 err = view->input(new, view, ch);
1522 break;
1523 case 'N':
1524 case 'n':
1525 if (view->search_started && view->search_next) {
1526 view->searching = (ch == 'n' ?
1527 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1528 view->search_next_done = 0;
1529 view->search_next(view);
1530 } else
1531 err = view->input(new, view, ch);
1532 break;
1533 case 'A':
1534 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1535 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1536 else
1537 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1538 TAILQ_FOREACH(v, views, entry) {
1539 if (v->reset) {
1540 err = v->reset(v);
1541 if (err)
1542 return err;
1544 if (v->child && v->child->reset) {
1545 err = v->child->reset(v->child);
1546 if (err)
1547 return err;
1550 break;
1551 default:
1552 err = view->input(new, view, ch);
1553 break;
1556 return err;
1559 static int
1560 view_needs_focus_indication(struct tog_view *view)
1562 if (view_is_parent_view(view)) {
1563 if (view->child == NULL || view->child->focussed)
1564 return 0;
1565 if (!view_is_splitscreen(view->child))
1566 return 0;
1567 } else if (!view_is_splitscreen(view))
1568 return 0;
1570 return view->focussed;
1573 static const struct got_error *
1574 view_loop(struct tog_view *view)
1576 const struct got_error *err = NULL;
1577 struct tog_view_list_head views;
1578 struct tog_view *new_view;
1579 char *mode;
1580 int fast_refresh = 10;
1581 int done = 0, errcode;
1583 mode = getenv("TOG_VIEW_SPLIT_MODE");
1584 if (!mode || !(*mode == 'h' || *mode == 'H'))
1585 view->mode = TOG_VIEW_SPLIT_VERT;
1586 else
1587 view->mode = TOG_VIEW_SPLIT_HRZN;
1589 errcode = pthread_mutex_lock(&tog_mutex);
1590 if (errcode)
1591 return got_error_set_errno(errcode, "pthread_mutex_lock");
1593 TAILQ_INIT(&views);
1594 TAILQ_INSERT_HEAD(&views, view, entry);
1596 view->focussed = 1;
1597 err = view->show(view);
1598 if (err)
1599 return err;
1600 update_panels();
1601 doupdate();
1602 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1603 !tog_fatal_signal_received()) {
1604 /* Refresh fast during initialization, then become slower. */
1605 if (fast_refresh && fast_refresh-- == 0)
1606 halfdelay(10); /* switch to once per second */
1608 err = view_input(&new_view, &done, view, &views);
1609 if (err)
1610 break;
1611 if (view->dying) {
1612 struct tog_view *v, *prev = NULL;
1614 if (view_is_parent_view(view))
1615 prev = TAILQ_PREV(view, tog_view_list_head,
1616 entry);
1617 else if (view->parent)
1618 prev = view->parent;
1620 if (view->parent) {
1621 view->parent->child = NULL;
1622 view->parent->focus_child = 0;
1623 /* Restore fullscreen line height. */
1624 view->parent->nlines = view->parent->lines;
1625 err = view_resize(view->parent);
1626 if (err)
1627 break;
1628 /* Make resized splits persist. */
1629 view_transfer_size(view->parent, view);
1630 } else
1631 TAILQ_REMOVE(&views, view, entry);
1633 err = view_close(view);
1634 if (err)
1635 goto done;
1637 view = NULL;
1638 TAILQ_FOREACH(v, &views, entry) {
1639 if (v->focussed)
1640 break;
1642 if (view == NULL && new_view == NULL) {
1643 /* No view has focus. Try to pick one. */
1644 if (prev)
1645 view = prev;
1646 else if (!TAILQ_EMPTY(&views)) {
1647 view = TAILQ_LAST(&views,
1648 tog_view_list_head);
1650 if (view) {
1651 if (view->focus_child) {
1652 view->child->focussed = 1;
1653 view = view->child;
1654 } else
1655 view->focussed = 1;
1659 if (new_view) {
1660 struct tog_view *v, *t;
1661 /* Only allow one parent view per type. */
1662 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1663 if (v->type != new_view->type)
1664 continue;
1665 TAILQ_REMOVE(&views, v, entry);
1666 err = view_close(v);
1667 if (err)
1668 goto done;
1669 break;
1671 TAILQ_INSERT_TAIL(&views, new_view, entry);
1672 view = new_view;
1674 if (view) {
1675 if (view_is_parent_view(view)) {
1676 if (view->child && view->child->focussed)
1677 view = view->child;
1678 } else {
1679 if (view->parent && view->parent->focussed)
1680 view = view->parent;
1682 show_panel(view->panel);
1683 if (view->child && view_is_splitscreen(view->child))
1684 show_panel(view->child->panel);
1685 if (view->parent && view_is_splitscreen(view)) {
1686 err = view->parent->show(view->parent);
1687 if (err)
1688 goto done;
1690 err = view->show(view);
1691 if (err)
1692 goto done;
1693 if (view->child) {
1694 err = view->child->show(view->child);
1695 if (err)
1696 goto done;
1698 update_panels();
1699 doupdate();
1702 done:
1703 while (!TAILQ_EMPTY(&views)) {
1704 const struct got_error *close_err;
1705 view = TAILQ_FIRST(&views);
1706 TAILQ_REMOVE(&views, view, entry);
1707 close_err = view_close(view);
1708 if (close_err && err == NULL)
1709 err = close_err;
1712 errcode = pthread_mutex_unlock(&tog_mutex);
1713 if (errcode && err == NULL)
1714 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1716 return err;
1719 __dead static void
1720 usage_log(void)
1722 endwin();
1723 fprintf(stderr,
1724 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1725 getprogname());
1726 exit(1);
1729 /* Create newly allocated wide-character string equivalent to a byte string. */
1730 static const struct got_error *
1731 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1733 char *vis = NULL;
1734 const struct got_error *err = NULL;
1736 *ws = NULL;
1737 *wlen = mbstowcs(NULL, s, 0);
1738 if (*wlen == (size_t)-1) {
1739 int vislen;
1740 if (errno != EILSEQ)
1741 return got_error_from_errno("mbstowcs");
1743 /* byte string invalid in current encoding; try to "fix" it */
1744 err = got_mbsavis(&vis, &vislen, s);
1745 if (err)
1746 return err;
1747 *wlen = mbstowcs(NULL, vis, 0);
1748 if (*wlen == (size_t)-1) {
1749 err = got_error_from_errno("mbstowcs"); /* give up */
1750 goto done;
1754 *ws = calloc(*wlen + 1, sizeof(**ws));
1755 if (*ws == NULL) {
1756 err = got_error_from_errno("calloc");
1757 goto done;
1760 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1761 err = got_error_from_errno("mbstowcs");
1762 done:
1763 free(vis);
1764 if (err) {
1765 free(*ws);
1766 *ws = NULL;
1767 *wlen = 0;
1769 return err;
1772 static const struct got_error *
1773 expand_tab(char **ptr, const char *src)
1775 char *dst;
1776 size_t len, n, idx = 0, sz = 0;
1778 *ptr = NULL;
1779 n = len = strlen(src);
1780 dst = malloc(n + 1);
1781 if (dst == NULL)
1782 return got_error_from_errno("malloc");
1784 while (idx < len && src[idx]) {
1785 const char c = src[idx];
1787 if (c == '\t') {
1788 size_t nb = TABSIZE - sz % TABSIZE;
1789 char *p;
1791 p = realloc(dst, n + nb);
1792 if (p == NULL) {
1793 free(dst);
1794 return got_error_from_errno("realloc");
1797 dst = p;
1798 n += nb;
1799 memset(dst + sz, ' ', nb);
1800 sz += nb;
1801 } else
1802 dst[sz++] = src[idx];
1803 ++idx;
1806 dst[sz] = '\0';
1807 *ptr = dst;
1808 return NULL;
1812 * Advance at most n columns from wline starting at offset off.
1813 * Return the index to the first character after the span operation.
1814 * Return the combined column width of all spanned wide character in
1815 * *rcol.
1817 static int
1818 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1820 int width, i, cols = 0;
1822 if (n == 0) {
1823 *rcol = cols;
1824 return off;
1827 for (i = off; wline[i] != L'\0'; ++i) {
1828 if (wline[i] == L'\t')
1829 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1830 else
1831 width = wcwidth(wline[i]);
1833 if (width == -1) {
1834 width = 1;
1835 wline[i] = L'.';
1838 if (cols + width > n)
1839 break;
1840 cols += width;
1843 *rcol = cols;
1844 return i;
1848 * Format a line for display, ensuring that it won't overflow a width limit.
1849 * With scrolling, the width returned refers to the scrolled version of the
1850 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1852 static const struct got_error *
1853 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1854 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1856 const struct got_error *err = NULL;
1857 int cols;
1858 wchar_t *wline = NULL;
1859 char *exstr = NULL;
1860 size_t wlen;
1861 int i, scrollx;
1863 *wlinep = NULL;
1864 *widthp = 0;
1866 if (expand) {
1867 err = expand_tab(&exstr, line);
1868 if (err)
1869 return err;
1872 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1873 free(exstr);
1874 if (err)
1875 return err;
1877 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1879 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1880 wline[wlen - 1] = L'\0';
1881 wlen--;
1883 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1884 wline[wlen - 1] = L'\0';
1885 wlen--;
1888 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1889 wline[i] = L'\0';
1891 if (widthp)
1892 *widthp = cols;
1893 if (scrollxp)
1894 *scrollxp = scrollx;
1895 if (err)
1896 free(wline);
1897 else
1898 *wlinep = wline;
1899 return err;
1902 static const struct got_error*
1903 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1904 struct got_object_id *id, struct got_repository *repo)
1906 static const struct got_error *err = NULL;
1907 struct got_reflist_entry *re;
1908 char *s;
1909 const char *name;
1911 *refs_str = NULL;
1913 TAILQ_FOREACH(re, refs, entry) {
1914 struct got_tag_object *tag = NULL;
1915 struct got_object_id *ref_id;
1916 int cmp;
1918 name = got_ref_get_name(re->ref);
1919 if (strcmp(name, GOT_REF_HEAD) == 0)
1920 continue;
1921 if (strncmp(name, "refs/", 5) == 0)
1922 name += 5;
1923 if (strncmp(name, "got/", 4) == 0 &&
1924 strncmp(name, "got/backup/", 11) != 0)
1925 continue;
1926 if (strncmp(name, "heads/", 6) == 0)
1927 name += 6;
1928 if (strncmp(name, "remotes/", 8) == 0) {
1929 name += 8;
1930 s = strstr(name, "/" GOT_REF_HEAD);
1931 if (s != NULL && s[strlen(s)] == '\0')
1932 continue;
1934 err = got_ref_resolve(&ref_id, repo, re->ref);
1935 if (err)
1936 break;
1937 if (strncmp(name, "tags/", 5) == 0) {
1938 err = got_object_open_as_tag(&tag, repo, ref_id);
1939 if (err) {
1940 if (err->code != GOT_ERR_OBJ_TYPE) {
1941 free(ref_id);
1942 break;
1944 /* Ref points at something other than a tag. */
1945 err = NULL;
1946 tag = NULL;
1949 cmp = got_object_id_cmp(tag ?
1950 got_object_tag_get_object_id(tag) : ref_id, id);
1951 free(ref_id);
1952 if (tag)
1953 got_object_tag_close(tag);
1954 if (cmp != 0)
1955 continue;
1956 s = *refs_str;
1957 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1958 s ? ", " : "", name) == -1) {
1959 err = got_error_from_errno("asprintf");
1960 free(s);
1961 *refs_str = NULL;
1962 break;
1964 free(s);
1967 return err;
1970 static const struct got_error *
1971 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1972 int col_tab_align)
1974 char *smallerthan;
1976 smallerthan = strchr(author, '<');
1977 if (smallerthan && smallerthan[1] != '\0')
1978 author = smallerthan + 1;
1979 author[strcspn(author, "@>")] = '\0';
1980 return format_line(wauthor, author_width, NULL, author, 0, limit,
1981 col_tab_align, 0);
1984 static const struct got_error *
1985 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1986 struct got_object_id *id, const size_t date_display_cols,
1987 int author_display_cols)
1989 struct tog_log_view_state *s = &view->state.log;
1990 const struct got_error *err = NULL;
1991 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1992 char *logmsg0 = NULL, *logmsg = NULL;
1993 char *author = NULL;
1994 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1995 int author_width, logmsg_width;
1996 char *newline, *line = NULL;
1997 int col, limit, scrollx;
1998 const int avail = view->ncols;
1999 struct tm tm;
2000 time_t committer_time;
2001 struct tog_color *tc;
2003 committer_time = got_object_commit_get_committer_time(commit);
2004 if (gmtime_r(&committer_time, &tm) == NULL)
2005 return got_error_from_errno("gmtime_r");
2006 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2007 return got_error(GOT_ERR_NO_SPACE);
2009 if (avail <= date_display_cols)
2010 limit = MIN(sizeof(datebuf) - 1, avail);
2011 else
2012 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2013 tc = get_color(&s->colors, TOG_COLOR_DATE);
2014 if (tc)
2015 wattr_on(view->window,
2016 COLOR_PAIR(tc->colorpair), NULL);
2017 waddnstr(view->window, datebuf, limit);
2018 if (tc)
2019 wattr_off(view->window,
2020 COLOR_PAIR(tc->colorpair), NULL);
2021 col = limit;
2022 if (col > avail)
2023 goto done;
2025 if (avail >= 120) {
2026 char *id_str;
2027 err = got_object_id_str(&id_str, id);
2028 if (err)
2029 goto done;
2030 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2031 if (tc)
2032 wattr_on(view->window,
2033 COLOR_PAIR(tc->colorpair), NULL);
2034 wprintw(view->window, "%.8s ", id_str);
2035 if (tc)
2036 wattr_off(view->window,
2037 COLOR_PAIR(tc->colorpair), NULL);
2038 free(id_str);
2039 col += 9;
2040 if (col > avail)
2041 goto done;
2044 if (s->use_committer)
2045 author = strdup(got_object_commit_get_committer(commit));
2046 else
2047 author = strdup(got_object_commit_get_author(commit));
2048 if (author == NULL) {
2049 err = got_error_from_errno("strdup");
2050 goto done;
2052 err = format_author(&wauthor, &author_width, author, avail - col, col);
2053 if (err)
2054 goto done;
2055 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2056 if (tc)
2057 wattr_on(view->window,
2058 COLOR_PAIR(tc->colorpair), NULL);
2059 waddwstr(view->window, wauthor);
2060 col += author_width;
2061 while (col < avail && author_width < author_display_cols + 2) {
2062 waddch(view->window, ' ');
2063 col++;
2064 author_width++;
2066 if (tc)
2067 wattr_off(view->window,
2068 COLOR_PAIR(tc->colorpair), NULL);
2069 if (col > avail)
2070 goto done;
2072 err = got_object_commit_get_logmsg(&logmsg0, commit);
2073 if (err)
2074 goto done;
2075 logmsg = logmsg0;
2076 while (*logmsg == '\n')
2077 logmsg++;
2078 newline = strchr(logmsg, '\n');
2079 if (newline)
2080 *newline = '\0';
2081 limit = avail - col;
2082 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2083 limit--; /* for the border */
2084 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2085 limit, col, 1);
2086 if (err)
2087 goto done;
2088 waddwstr(view->window, &wlogmsg[scrollx]);
2089 col += MAX(logmsg_width, 0);
2090 while (col < avail) {
2091 waddch(view->window, ' ');
2092 col++;
2094 done:
2095 free(logmsg0);
2096 free(wlogmsg);
2097 free(author);
2098 free(wauthor);
2099 free(line);
2100 return err;
2103 static struct commit_queue_entry *
2104 alloc_commit_queue_entry(struct got_commit_object *commit,
2105 struct got_object_id *id)
2107 struct commit_queue_entry *entry;
2108 struct got_object_id *dup;
2110 entry = calloc(1, sizeof(*entry));
2111 if (entry == NULL)
2112 return NULL;
2114 dup = got_object_id_dup(id);
2115 if (dup == NULL) {
2116 free(entry);
2117 return NULL;
2120 entry->id = dup;
2121 entry->commit = commit;
2122 return entry;
2125 static void
2126 pop_commit(struct commit_queue *commits)
2128 struct commit_queue_entry *entry;
2130 entry = TAILQ_FIRST(&commits->head);
2131 TAILQ_REMOVE(&commits->head, entry, entry);
2132 got_object_commit_close(entry->commit);
2133 commits->ncommits--;
2134 free(entry->id);
2135 free(entry);
2138 static void
2139 free_commits(struct commit_queue *commits)
2141 while (!TAILQ_EMPTY(&commits->head))
2142 pop_commit(commits);
2145 static const struct got_error *
2146 match_commit(int *have_match, struct got_object_id *id,
2147 struct got_commit_object *commit, regex_t *regex)
2149 const struct got_error *err = NULL;
2150 regmatch_t regmatch;
2151 char *id_str = NULL, *logmsg = NULL;
2153 *have_match = 0;
2155 err = got_object_id_str(&id_str, id);
2156 if (err)
2157 return err;
2159 err = got_object_commit_get_logmsg(&logmsg, commit);
2160 if (err)
2161 goto done;
2163 if (regexec(regex, got_object_commit_get_author(commit), 1,
2164 &regmatch, 0) == 0 ||
2165 regexec(regex, got_object_commit_get_committer(commit), 1,
2166 &regmatch, 0) == 0 ||
2167 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2168 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2169 *have_match = 1;
2170 done:
2171 free(id_str);
2172 free(logmsg);
2173 return err;
2176 static const struct got_error *
2177 queue_commits(struct tog_log_thread_args *a)
2179 const struct got_error *err = NULL;
2182 * We keep all commits open throughout the lifetime of the log
2183 * view in order to avoid having to re-fetch commits from disk
2184 * while updating the display.
2186 do {
2187 struct got_object_id id;
2188 struct got_commit_object *commit;
2189 struct commit_queue_entry *entry;
2190 int limit_match = 0;
2191 int errcode;
2193 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2194 NULL, NULL);
2195 if (err)
2196 break;
2198 err = got_object_open_as_commit(&commit, a->repo, &id);
2199 if (err)
2200 break;
2201 entry = alloc_commit_queue_entry(commit, &id);
2202 if (entry == NULL) {
2203 err = got_error_from_errno("alloc_commit_queue_entry");
2204 break;
2207 errcode = pthread_mutex_lock(&tog_mutex);
2208 if (errcode) {
2209 err = got_error_set_errno(errcode,
2210 "pthread_mutex_lock");
2211 break;
2214 entry->idx = a->real_commits->ncommits;
2215 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2216 a->real_commits->ncommits++;
2218 if (*a->limiting) {
2219 err = match_commit(&limit_match, &id, commit,
2220 a->limit_regex);
2221 if (err)
2222 break;
2224 if (limit_match) {
2225 struct commit_queue_entry *matched;
2227 matched = alloc_commit_queue_entry(
2228 entry->commit, entry->id);
2229 if (matched == NULL) {
2230 err = got_error_from_errno(
2231 "alloc_commit_queue_entry");
2232 break;
2235 err = got_object_commit_dup(&matched->commit,
2236 entry->commit);
2237 if (err)
2238 break;
2240 matched->idx = a->limit_commits->ncommits;
2241 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2242 matched, entry);
2243 a->limit_commits->ncommits++;
2247 * This is how we signal log_thread() that we
2248 * have found a match, and that it should be
2249 * counted as a new entry for the view.
2251 a->limit_match = limit_match;
2254 if (*a->searching == TOG_SEARCH_FORWARD &&
2255 !*a->search_next_done) {
2256 int have_match;
2257 err = match_commit(&have_match, &id, commit, a->regex);
2258 if (err)
2259 break;
2261 if (*a->limiting) {
2262 if (limit_match && have_match)
2263 *a->search_next_done =
2264 TOG_SEARCH_HAVE_MORE;
2265 } else if (have_match)
2266 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2269 errcode = pthread_mutex_unlock(&tog_mutex);
2270 if (errcode && err == NULL)
2271 err = got_error_set_errno(errcode,
2272 "pthread_mutex_unlock");
2273 if (err)
2274 break;
2275 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2277 return err;
2280 static void
2281 select_commit(struct tog_log_view_state *s)
2283 struct commit_queue_entry *entry;
2284 int ncommits = 0;
2286 entry = s->first_displayed_entry;
2287 while (entry) {
2288 if (ncommits == s->selected) {
2289 s->selected_entry = entry;
2290 break;
2292 entry = TAILQ_NEXT(entry, entry);
2293 ncommits++;
2297 static const struct got_error *
2298 draw_commits(struct tog_view *view)
2300 const struct got_error *err = NULL;
2301 struct tog_log_view_state *s = &view->state.log;
2302 struct commit_queue_entry *entry = s->selected_entry;
2303 int limit = view->nlines;
2304 int width;
2305 int ncommits, author_cols = 4;
2306 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2307 char *refs_str = NULL;
2308 wchar_t *wline;
2309 struct tog_color *tc;
2310 static const size_t date_display_cols = 12;
2312 if (view_is_hsplit_top(view))
2313 --limit; /* account for border */
2315 if (s->selected_entry &&
2316 !(view->searching && view->search_next_done == 0)) {
2317 struct got_reflist_head *refs;
2318 err = got_object_id_str(&id_str, s->selected_entry->id);
2319 if (err)
2320 return err;
2321 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2322 s->selected_entry->id);
2323 if (refs) {
2324 err = build_refs_str(&refs_str, refs,
2325 s->selected_entry->id, s->repo);
2326 if (err)
2327 goto done;
2331 if (s->thread_args.commits_needed == 0)
2332 halfdelay(10); /* disable fast refresh */
2334 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2335 if (asprintf(&ncommits_str, " [%d/%d] %s",
2336 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2337 (view->searching && !view->search_next_done) ?
2338 "searching..." : "loading...") == -1) {
2339 err = got_error_from_errno("asprintf");
2340 goto done;
2342 } else {
2343 const char *search_str = NULL;
2344 const char *limit_str = NULL;
2346 if (view->searching) {
2347 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2348 search_str = "no more matches";
2349 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2350 search_str = "no matches found";
2351 else if (!view->search_next_done)
2352 search_str = "searching...";
2355 if (s->limit_view && s->commits->ncommits == 0)
2356 limit_str = "no matches found";
2358 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2359 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2360 search_str ? search_str : (refs_str ? refs_str : ""),
2361 limit_str ? limit_str : "") == -1) {
2362 err = got_error_from_errno("asprintf");
2363 goto done;
2367 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2368 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2369 "........................................",
2370 s->in_repo_path, ncommits_str) == -1) {
2371 err = got_error_from_errno("asprintf");
2372 header = NULL;
2373 goto done;
2375 } else if (asprintf(&header, "commit %s%s",
2376 id_str ? id_str : "........................................",
2377 ncommits_str) == -1) {
2378 err = got_error_from_errno("asprintf");
2379 header = NULL;
2380 goto done;
2382 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2383 if (err)
2384 goto done;
2386 werase(view->window);
2388 if (view_needs_focus_indication(view))
2389 wstandout(view->window);
2390 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2391 if (tc)
2392 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2393 waddwstr(view->window, wline);
2394 while (width < view->ncols) {
2395 waddch(view->window, ' ');
2396 width++;
2398 if (tc)
2399 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2400 if (view_needs_focus_indication(view))
2401 wstandend(view->window);
2402 free(wline);
2403 if (limit <= 1)
2404 goto done;
2406 /* Grow author column size if necessary, and set view->maxx. */
2407 entry = s->first_displayed_entry;
2408 ncommits = 0;
2409 view->maxx = 0;
2410 while (entry) {
2411 struct got_commit_object *c = entry->commit;
2412 char *author, *eol, *msg, *msg0;
2413 wchar_t *wauthor, *wmsg;
2414 int width;
2415 if (ncommits >= limit - 1)
2416 break;
2417 if (s->use_committer)
2418 author = strdup(got_object_commit_get_committer(c));
2419 else
2420 author = strdup(got_object_commit_get_author(c));
2421 if (author == NULL) {
2422 err = got_error_from_errno("strdup");
2423 goto done;
2425 err = format_author(&wauthor, &width, author, COLS,
2426 date_display_cols);
2427 if (author_cols < width)
2428 author_cols = width;
2429 free(wauthor);
2430 free(author);
2431 if (err)
2432 goto done;
2433 err = got_object_commit_get_logmsg(&msg0, c);
2434 if (err)
2435 goto done;
2436 msg = msg0;
2437 while (*msg == '\n')
2438 ++msg;
2439 if ((eol = strchr(msg, '\n')))
2440 *eol = '\0';
2441 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2442 date_display_cols + author_cols, 0);
2443 if (err)
2444 goto done;
2445 view->maxx = MAX(view->maxx, width);
2446 free(msg0);
2447 free(wmsg);
2448 ncommits++;
2449 entry = TAILQ_NEXT(entry, entry);
2452 entry = s->first_displayed_entry;
2453 s->last_displayed_entry = s->first_displayed_entry;
2454 ncommits = 0;
2455 while (entry) {
2456 if (ncommits >= limit - 1)
2457 break;
2458 if (ncommits == s->selected)
2459 wstandout(view->window);
2460 err = draw_commit(view, entry->commit, entry->id,
2461 date_display_cols, author_cols);
2462 if (ncommits == s->selected)
2463 wstandend(view->window);
2464 if (err)
2465 goto done;
2466 ncommits++;
2467 s->last_displayed_entry = entry;
2468 entry = TAILQ_NEXT(entry, entry);
2471 view_border(view);
2472 done:
2473 free(id_str);
2474 free(refs_str);
2475 free(ncommits_str);
2476 free(header);
2477 return err;
2480 static void
2481 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2483 struct commit_queue_entry *entry;
2484 int nscrolled = 0;
2486 entry = TAILQ_FIRST(&s->commits->head);
2487 if (s->first_displayed_entry == entry)
2488 return;
2490 entry = s->first_displayed_entry;
2491 while (entry && nscrolled < maxscroll) {
2492 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2493 if (entry) {
2494 s->first_displayed_entry = entry;
2495 nscrolled++;
2500 static const struct got_error *
2501 trigger_log_thread(struct tog_view *view, int wait)
2503 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2504 int errcode;
2506 halfdelay(1); /* fast refresh while loading commits */
2508 while (!ta->log_complete && !tog_thread_error &&
2509 (ta->commits_needed > 0 || ta->load_all)) {
2510 /* Wake the log thread. */
2511 errcode = pthread_cond_signal(&ta->need_commits);
2512 if (errcode)
2513 return got_error_set_errno(errcode,
2514 "pthread_cond_signal");
2517 * The mutex will be released while the view loop waits
2518 * in wgetch(), at which time the log thread will run.
2520 if (!wait)
2521 break;
2523 /* Display progress update in log view. */
2524 show_log_view(view);
2525 update_panels();
2526 doupdate();
2528 /* Wait right here while next commit is being loaded. */
2529 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2530 if (errcode)
2531 return got_error_set_errno(errcode,
2532 "pthread_cond_wait");
2534 /* Display progress update in log view. */
2535 show_log_view(view);
2536 update_panels();
2537 doupdate();
2540 return NULL;
2543 static const struct got_error *
2544 request_log_commits(struct tog_view *view)
2546 struct tog_log_view_state *state = &view->state.log;
2547 const struct got_error *err = NULL;
2549 if (state->thread_args.log_complete)
2550 return NULL;
2552 state->thread_args.commits_needed += view->nscrolled;
2553 err = trigger_log_thread(view, 1);
2554 view->nscrolled = 0;
2556 return err;
2559 static const struct got_error *
2560 log_scroll_down(struct tog_view *view, int maxscroll)
2562 struct tog_log_view_state *s = &view->state.log;
2563 const struct got_error *err = NULL;
2564 struct commit_queue_entry *pentry;
2565 int nscrolled = 0, ncommits_needed;
2567 if (s->last_displayed_entry == NULL)
2568 return NULL;
2570 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2571 if (s->commits->ncommits < ncommits_needed &&
2572 !s->thread_args.log_complete) {
2574 * Ask the log thread for required amount of commits.
2576 s->thread_args.commits_needed +=
2577 ncommits_needed - s->commits->ncommits;
2578 err = trigger_log_thread(view, 1);
2579 if (err)
2580 return err;
2583 do {
2584 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2585 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2586 break;
2588 s->last_displayed_entry = pentry ?
2589 pentry : s->last_displayed_entry;;
2591 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2592 if (pentry == NULL)
2593 break;
2594 s->first_displayed_entry = pentry;
2595 } while (++nscrolled < maxscroll);
2597 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2598 view->nscrolled += nscrolled;
2599 else
2600 view->nscrolled = 0;
2602 return err;
2605 static const struct got_error *
2606 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2607 struct got_commit_object *commit, struct got_object_id *commit_id,
2608 struct tog_view *log_view, struct got_repository *repo)
2610 const struct got_error *err;
2611 struct got_object_qid *parent_id;
2612 struct tog_view *diff_view;
2614 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2615 if (diff_view == NULL)
2616 return got_error_from_errno("view_open");
2618 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2619 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2620 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2621 if (err == NULL)
2622 *new_view = diff_view;
2623 return err;
2626 static const struct got_error *
2627 tree_view_visit_subtree(struct tog_tree_view_state *s,
2628 struct got_tree_object *subtree)
2630 struct tog_parent_tree *parent;
2632 parent = calloc(1, sizeof(*parent));
2633 if (parent == NULL)
2634 return got_error_from_errno("calloc");
2636 parent->tree = s->tree;
2637 parent->first_displayed_entry = s->first_displayed_entry;
2638 parent->selected_entry = s->selected_entry;
2639 parent->selected = s->selected;
2640 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2641 s->tree = subtree;
2642 s->selected = 0;
2643 s->first_displayed_entry = NULL;
2644 return NULL;
2647 static const struct got_error *
2648 tree_view_walk_path(struct tog_tree_view_state *s,
2649 struct got_commit_object *commit, const char *path)
2651 const struct got_error *err = NULL;
2652 struct got_tree_object *tree = NULL;
2653 const char *p;
2654 char *slash, *subpath = NULL;
2656 /* Walk the path and open corresponding tree objects. */
2657 p = path;
2658 while (*p) {
2659 struct got_tree_entry *te;
2660 struct got_object_id *tree_id;
2661 char *te_name;
2663 while (p[0] == '/')
2664 p++;
2666 /* Ensure the correct subtree entry is selected. */
2667 slash = strchr(p, '/');
2668 if (slash == NULL)
2669 te_name = strdup(p);
2670 else
2671 te_name = strndup(p, slash - p);
2672 if (te_name == NULL) {
2673 err = got_error_from_errno("strndup");
2674 break;
2676 te = got_object_tree_find_entry(s->tree, te_name);
2677 if (te == NULL) {
2678 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2679 free(te_name);
2680 break;
2682 free(te_name);
2683 s->first_displayed_entry = s->selected_entry = te;
2685 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2686 break; /* jump to this file's entry */
2688 slash = strchr(p, '/');
2689 if (slash)
2690 subpath = strndup(path, slash - path);
2691 else
2692 subpath = strdup(path);
2693 if (subpath == NULL) {
2694 err = got_error_from_errno("strdup");
2695 break;
2698 err = got_object_id_by_path(&tree_id, s->repo, commit,
2699 subpath);
2700 if (err)
2701 break;
2703 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2704 free(tree_id);
2705 if (err)
2706 break;
2708 err = tree_view_visit_subtree(s, tree);
2709 if (err) {
2710 got_object_tree_close(tree);
2711 break;
2713 if (slash == NULL)
2714 break;
2715 free(subpath);
2716 subpath = NULL;
2717 p = slash;
2720 free(subpath);
2721 return err;
2724 static const struct got_error *
2725 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2726 struct commit_queue_entry *entry, const char *path,
2727 const char *head_ref_name, struct got_repository *repo)
2729 const struct got_error *err = NULL;
2730 struct tog_tree_view_state *s;
2731 struct tog_view *tree_view;
2733 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2734 if (tree_view == NULL)
2735 return got_error_from_errno("view_open");
2737 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2738 if (err)
2739 return err;
2740 s = &tree_view->state.tree;
2742 *new_view = tree_view;
2744 if (got_path_is_root_dir(path))
2745 return NULL;
2747 return tree_view_walk_path(s, entry->commit, path);
2750 static const struct got_error *
2751 block_signals_used_by_main_thread(void)
2753 sigset_t sigset;
2754 int errcode;
2756 if (sigemptyset(&sigset) == -1)
2757 return got_error_from_errno("sigemptyset");
2759 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2760 if (sigaddset(&sigset, SIGWINCH) == -1)
2761 return got_error_from_errno("sigaddset");
2762 if (sigaddset(&sigset, SIGCONT) == -1)
2763 return got_error_from_errno("sigaddset");
2764 if (sigaddset(&sigset, SIGINT) == -1)
2765 return got_error_from_errno("sigaddset");
2766 if (sigaddset(&sigset, SIGTERM) == -1)
2767 return got_error_from_errno("sigaddset");
2769 /* ncurses handles SIGTSTP */
2770 if (sigaddset(&sigset, SIGTSTP) == -1)
2771 return got_error_from_errno("sigaddset");
2773 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2774 if (errcode)
2775 return got_error_set_errno(errcode, "pthread_sigmask");
2777 return NULL;
2780 static void *
2781 log_thread(void *arg)
2783 const struct got_error *err = NULL;
2784 int errcode = 0;
2785 struct tog_log_thread_args *a = arg;
2786 int done = 0;
2789 * Sync startup with main thread such that we begin our
2790 * work once view_input() has released the mutex.
2792 errcode = pthread_mutex_lock(&tog_mutex);
2793 if (errcode) {
2794 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2795 return (void *)err;
2798 err = block_signals_used_by_main_thread();
2799 if (err) {
2800 pthread_mutex_unlock(&tog_mutex);
2801 goto done;
2804 while (!done && !err && !tog_fatal_signal_received()) {
2805 errcode = pthread_mutex_unlock(&tog_mutex);
2806 if (errcode) {
2807 err = got_error_set_errno(errcode,
2808 "pthread_mutex_unlock");
2809 goto done;
2811 err = queue_commits(a);
2812 if (err) {
2813 if (err->code != GOT_ERR_ITER_COMPLETED)
2814 goto done;
2815 err = NULL;
2816 done = 1;
2817 } else if (a->commits_needed > 0 && !a->load_all) {
2818 if (*a->limiting) {
2819 if (a->limit_match)
2820 a->commits_needed--;
2821 } else
2822 a->commits_needed--;
2825 errcode = pthread_mutex_lock(&tog_mutex);
2826 if (errcode) {
2827 err = got_error_set_errno(errcode,
2828 "pthread_mutex_lock");
2829 goto done;
2830 } else if (*a->quit)
2831 done = 1;
2832 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2833 *a->first_displayed_entry =
2834 TAILQ_FIRST(&a->limit_commits->head);
2835 *a->selected_entry = *a->first_displayed_entry;
2836 } else if (*a->first_displayed_entry == NULL) {
2837 *a->first_displayed_entry =
2838 TAILQ_FIRST(&a->real_commits->head);
2839 *a->selected_entry = *a->first_displayed_entry;
2842 errcode = pthread_cond_signal(&a->commit_loaded);
2843 if (errcode) {
2844 err = got_error_set_errno(errcode,
2845 "pthread_cond_signal");
2846 pthread_mutex_unlock(&tog_mutex);
2847 goto done;
2850 if (done)
2851 a->commits_needed = 0;
2852 else {
2853 if (a->commits_needed == 0 && !a->load_all) {
2854 errcode = pthread_cond_wait(&a->need_commits,
2855 &tog_mutex);
2856 if (errcode) {
2857 err = got_error_set_errno(errcode,
2858 "pthread_cond_wait");
2859 pthread_mutex_unlock(&tog_mutex);
2860 goto done;
2862 if (*a->quit)
2863 done = 1;
2867 a->log_complete = 1;
2868 errcode = pthread_mutex_unlock(&tog_mutex);
2869 if (errcode)
2870 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2871 done:
2872 if (err) {
2873 tog_thread_error = 1;
2874 pthread_cond_signal(&a->commit_loaded);
2876 return (void *)err;
2879 static const struct got_error *
2880 stop_log_thread(struct tog_log_view_state *s)
2882 const struct got_error *err = NULL, *thread_err = NULL;
2883 int errcode;
2885 if (s->thread) {
2886 s->quit = 1;
2887 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2888 if (errcode)
2889 return got_error_set_errno(errcode,
2890 "pthread_cond_signal");
2891 errcode = pthread_mutex_unlock(&tog_mutex);
2892 if (errcode)
2893 return got_error_set_errno(errcode,
2894 "pthread_mutex_unlock");
2895 errcode = pthread_join(s->thread, (void **)&thread_err);
2896 if (errcode)
2897 return got_error_set_errno(errcode, "pthread_join");
2898 errcode = pthread_mutex_lock(&tog_mutex);
2899 if (errcode)
2900 return got_error_set_errno(errcode,
2901 "pthread_mutex_lock");
2902 s->thread = 0; //NULL;
2905 if (s->thread_args.repo) {
2906 err = got_repo_close(s->thread_args.repo);
2907 s->thread_args.repo = NULL;
2910 if (s->thread_args.pack_fds) {
2911 const struct got_error *pack_err =
2912 got_repo_pack_fds_close(s->thread_args.pack_fds);
2913 if (err == NULL)
2914 err = pack_err;
2915 s->thread_args.pack_fds = NULL;
2918 if (s->thread_args.graph) {
2919 got_commit_graph_close(s->thread_args.graph);
2920 s->thread_args.graph = NULL;
2923 return err ? err : thread_err;
2926 static const struct got_error *
2927 close_log_view(struct tog_view *view)
2929 const struct got_error *err = NULL;
2930 struct tog_log_view_state *s = &view->state.log;
2931 int errcode;
2933 err = stop_log_thread(s);
2935 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2936 if (errcode && err == NULL)
2937 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2939 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2940 if (errcode && err == NULL)
2941 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2943 free_commits(&s->limit_commits);
2944 free_commits(&s->real_commits);
2945 free(s->in_repo_path);
2946 s->in_repo_path = NULL;
2947 free(s->start_id);
2948 s->start_id = NULL;
2949 free(s->head_ref_name);
2950 s->head_ref_name = NULL;
2951 return err;
2955 * We use two queues to implement the limit feature: first consists of
2956 * commits matching the current limit_regex; second is the real queue
2957 * of all known commits (real_commits). When the user starts limiting,
2958 * we swap queues such that all movement and displaying functionality
2959 * works with very slight change.
2961 static const struct got_error *
2962 limit_log_view(struct tog_view *view)
2964 struct tog_log_view_state *s = &view->state.log;
2965 struct commit_queue_entry *entry;
2966 struct tog_view *v = view;
2967 const struct got_error *err = NULL;
2968 char pattern[1024];
2969 int ret;
2971 if (view_is_hsplit_top(view))
2972 v = view->child;
2973 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2974 v = view->parent;
2976 /* Get the pattern */
2977 wmove(v->window, v->nlines - 1, 0);
2978 wclrtoeol(v->window);
2979 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2980 nodelay(v->window, FALSE);
2981 nocbreak();
2982 echo();
2983 ret = wgetnstr(v->window, pattern, sizeof(pattern));
2984 cbreak();
2985 noecho();
2986 nodelay(v->window, TRUE);
2987 if (ret == ERR)
2988 return NULL;
2990 if (*pattern == '\0') {
2992 * Safety measure for the situation where the user
2993 * resets limit without previously limiting anything.
2995 if (!s->limit_view)
2996 return NULL;
2999 * User could have pressed Ctrl+L, which refreshed the
3000 * commit queues, it means we can't save previously
3001 * (before limit took place) displayed entries,
3002 * because they would point to already free'ed memory,
3003 * so we are forced to always select first entry of
3004 * the queue.
3006 s->commits = &s->real_commits;
3007 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3008 s->selected_entry = s->first_displayed_entry;
3009 s->selected = 0;
3010 s->limit_view = 0;
3012 return NULL;
3015 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3016 return NULL;
3018 s->limit_view = 1;
3020 /* Clear the screen while loading limit view */
3021 s->first_displayed_entry = NULL;
3022 s->last_displayed_entry = NULL;
3023 s->selected_entry = NULL;
3024 s->commits = &s->limit_commits;
3026 /* Prepare limit queue for new search */
3027 free_commits(&s->limit_commits);
3028 s->limit_commits.ncommits = 0;
3030 /* First process commits, which are in queue already */
3031 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3032 int have_match = 0;
3034 err = match_commit(&have_match, entry->id,
3035 entry->commit, &s->limit_regex);
3036 if (err)
3037 return err;
3039 if (have_match) {
3040 struct commit_queue_entry *matched;
3042 matched = alloc_commit_queue_entry(entry->commit,
3043 entry->id);
3044 if (matched == NULL) {
3045 err = got_error_from_errno(
3046 "alloc_commit_queue_entry");
3047 break;
3050 err = got_object_commit_dup(&matched->commit,
3051 entry->commit);
3052 if (err) {
3053 free(matched);
3054 return err;
3057 matched->idx = s->limit_commits.ncommits;
3058 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3059 matched, entry);
3060 s->limit_commits.ncommits++;
3064 /* Second process all the commits, until we fill the screen */
3065 if (s->limit_commits.ncommits < view->nlines - 1 &&
3066 !s->thread_args.log_complete) {
3067 s->thread_args.commits_needed +=
3068 view->nlines - s->limit_commits.ncommits - 1;
3069 err = trigger_log_thread(view, 1);
3070 if (err)
3071 return err;
3074 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3075 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3076 s->selected = 0;
3078 return NULL;
3081 static const struct got_error *
3082 search_start_log_view(struct tog_view *view)
3084 struct tog_log_view_state *s = &view->state.log;
3086 s->matched_entry = NULL;
3087 s->search_entry = NULL;
3088 return NULL;
3091 static const struct got_error *
3092 search_next_log_view(struct tog_view *view)
3094 const struct got_error *err = NULL;
3095 struct tog_log_view_state *s = &view->state.log;
3096 struct commit_queue_entry *entry;
3098 /* Display progress update in log view. */
3099 show_log_view(view);
3100 update_panels();
3101 doupdate();
3103 if (s->search_entry) {
3104 int errcode, ch;
3105 errcode = pthread_mutex_unlock(&tog_mutex);
3106 if (errcode)
3107 return got_error_set_errno(errcode,
3108 "pthread_mutex_unlock");
3109 ch = wgetch(view->window);
3110 errcode = pthread_mutex_lock(&tog_mutex);
3111 if (errcode)
3112 return got_error_set_errno(errcode,
3113 "pthread_mutex_lock");
3114 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3115 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3116 return NULL;
3118 if (view->searching == TOG_SEARCH_FORWARD)
3119 entry = TAILQ_NEXT(s->search_entry, entry);
3120 else
3121 entry = TAILQ_PREV(s->search_entry,
3122 commit_queue_head, entry);
3123 } else if (s->matched_entry) {
3125 * If the user has moved the cursor after we hit a match,
3126 * the position from where we should continue searching
3127 * might have changed.
3129 if (view->searching == TOG_SEARCH_FORWARD)
3130 entry = TAILQ_NEXT(s->selected_entry, entry);
3131 else
3132 entry = TAILQ_PREV(s->selected_entry, commit_queue_head,
3133 entry);
3134 } else {
3135 entry = s->selected_entry;
3138 while (1) {
3139 int have_match = 0;
3141 if (entry == NULL) {
3142 if (s->thread_args.log_complete ||
3143 view->searching == TOG_SEARCH_BACKWARD) {
3144 view->search_next_done =
3145 (s->matched_entry == NULL ?
3146 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3147 s->search_entry = NULL;
3148 return NULL;
3151 * Poke the log thread for more commits and return,
3152 * allowing the main loop to make progress. Search
3153 * will resume at s->search_entry once we come back.
3155 s->thread_args.commits_needed++;
3156 return trigger_log_thread(view, 0);
3159 err = match_commit(&have_match, entry->id, entry->commit,
3160 &view->regex);
3161 if (err)
3162 break;
3163 if (have_match) {
3164 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3165 s->matched_entry = entry;
3166 break;
3169 s->search_entry = entry;
3170 if (view->searching == TOG_SEARCH_FORWARD)
3171 entry = TAILQ_NEXT(entry, entry);
3172 else
3173 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3176 if (s->matched_entry) {
3177 int cur = s->selected_entry->idx;
3178 while (cur < s->matched_entry->idx) {
3179 err = input_log_view(NULL, view, KEY_DOWN);
3180 if (err)
3181 return err;
3182 cur++;
3184 while (cur > s->matched_entry->idx) {
3185 err = input_log_view(NULL, view, KEY_UP);
3186 if (err)
3187 return err;
3188 cur--;
3192 s->search_entry = NULL;
3194 return NULL;
3197 static const struct got_error *
3198 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3199 struct got_repository *repo, const char *head_ref_name,
3200 const char *in_repo_path, int log_branches)
3202 const struct got_error *err = NULL;
3203 struct tog_log_view_state *s = &view->state.log;
3204 struct got_repository *thread_repo = NULL;
3205 struct got_commit_graph *thread_graph = NULL;
3206 int errcode;
3208 if (in_repo_path != s->in_repo_path) {
3209 free(s->in_repo_path);
3210 s->in_repo_path = strdup(in_repo_path);
3211 if (s->in_repo_path == NULL)
3212 return got_error_from_errno("strdup");
3215 /* The commit queue only contains commits being displayed. */
3216 TAILQ_INIT(&s->real_commits.head);
3217 s->real_commits.ncommits = 0;
3218 s->commits = &s->real_commits;
3220 TAILQ_INIT(&s->limit_commits.head);
3221 s->limit_view = 0;
3222 s->limit_commits.ncommits = 0;
3224 s->repo = repo;
3225 if (head_ref_name) {
3226 s->head_ref_name = strdup(head_ref_name);
3227 if (s->head_ref_name == NULL) {
3228 err = got_error_from_errno("strdup");
3229 goto done;
3232 s->start_id = got_object_id_dup(start_id);
3233 if (s->start_id == NULL) {
3234 err = got_error_from_errno("got_object_id_dup");
3235 goto done;
3237 s->log_branches = log_branches;
3239 STAILQ_INIT(&s->colors);
3240 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3241 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3242 get_color_value("TOG_COLOR_COMMIT"));
3243 if (err)
3244 goto done;
3245 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3246 get_color_value("TOG_COLOR_AUTHOR"));
3247 if (err) {
3248 free_colors(&s->colors);
3249 goto done;
3251 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3252 get_color_value("TOG_COLOR_DATE"));
3253 if (err) {
3254 free_colors(&s->colors);
3255 goto done;
3259 view->show = show_log_view;
3260 view->input = input_log_view;
3261 view->resize = resize_log_view;
3262 view->close = close_log_view;
3263 view->search_start = search_start_log_view;
3264 view->search_next = search_next_log_view;
3266 if (s->thread_args.pack_fds == NULL) {
3267 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3268 if (err)
3269 goto done;
3271 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3272 s->thread_args.pack_fds);
3273 if (err)
3274 goto done;
3275 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3276 !s->log_branches);
3277 if (err)
3278 goto done;
3279 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3280 s->repo, NULL, NULL);
3281 if (err)
3282 goto done;
3284 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3285 if (errcode) {
3286 err = got_error_set_errno(errcode, "pthread_cond_init");
3287 goto done;
3289 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3290 if (errcode) {
3291 err = got_error_set_errno(errcode, "pthread_cond_init");
3292 goto done;
3295 s->thread_args.commits_needed = view->nlines;
3296 s->thread_args.graph = thread_graph;
3297 s->thread_args.real_commits = &s->real_commits;
3298 s->thread_args.limit_commits = &s->limit_commits;
3299 s->thread_args.in_repo_path = s->in_repo_path;
3300 s->thread_args.start_id = s->start_id;
3301 s->thread_args.repo = thread_repo;
3302 s->thread_args.log_complete = 0;
3303 s->thread_args.quit = &s->quit;
3304 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3305 s->thread_args.selected_entry = &s->selected_entry;
3306 s->thread_args.searching = &view->searching;
3307 s->thread_args.search_next_done = &view->search_next_done;
3308 s->thread_args.regex = &view->regex;
3309 s->thread_args.limiting = &s->limit_view;
3310 s->thread_args.limit_regex = &s->limit_regex;
3311 s->thread_args.limit_commits = &s->limit_commits;
3312 done:
3313 if (err)
3314 close_log_view(view);
3315 return err;
3318 static const struct got_error *
3319 show_log_view(struct tog_view *view)
3321 const struct got_error *err;
3322 struct tog_log_view_state *s = &view->state.log;
3324 if (s->thread == 0) { //NULL) {
3325 int errcode = pthread_create(&s->thread, NULL, log_thread,
3326 &s->thread_args);
3327 if (errcode)
3328 return got_error_set_errno(errcode, "pthread_create");
3329 if (s->thread_args.commits_needed > 0) {
3330 err = trigger_log_thread(view, 1);
3331 if (err)
3332 return err;
3336 return draw_commits(view);
3339 static void
3340 log_move_cursor_up(struct tog_view *view, int page, int home)
3342 struct tog_log_view_state *s = &view->state.log;
3344 if (s->first_displayed_entry == NULL)
3345 return;
3346 if (s->selected_entry->idx == 0)
3347 view->count = 0;
3349 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3350 || home)
3351 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3353 if (!page && !home && s->selected > 0)
3354 --s->selected;
3355 else
3356 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3358 select_commit(s);
3359 return;
3362 static const struct got_error *
3363 log_move_cursor_down(struct tog_view *view, int page)
3365 struct tog_log_view_state *s = &view->state.log;
3366 const struct got_error *err = NULL;
3367 int eos = view->nlines - 2;
3369 if (s->first_displayed_entry == NULL)
3370 return NULL;
3372 if (s->thread_args.log_complete &&
3373 s->selected_entry->idx >= s->commits->ncommits - 1)
3374 return NULL;
3376 if (view_is_hsplit_top(view))
3377 --eos; /* border consumes the last line */
3379 if (!page) {
3380 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3381 ++s->selected;
3382 else
3383 err = log_scroll_down(view, 1);
3384 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3385 struct commit_queue_entry *entry;
3386 int n;
3388 s->selected = 0;
3389 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3390 s->last_displayed_entry = entry;
3391 for (n = 0; n <= eos; n++) {
3392 if (entry == NULL)
3393 break;
3394 s->first_displayed_entry = entry;
3395 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3397 if (n > 0)
3398 s->selected = n - 1;
3399 } else {
3400 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3401 s->thread_args.log_complete)
3402 s->selected += MIN(page,
3403 s->commits->ncommits - s->selected_entry->idx - 1);
3404 else
3405 err = log_scroll_down(view, page);
3407 if (err)
3408 return err;
3411 * We might necessarily overshoot in horizontal
3412 * splits; if so, select the last displayed commit.
3414 if (s->first_displayed_entry && s->last_displayed_entry) {
3415 s->selected = MIN(s->selected,
3416 s->last_displayed_entry->idx -
3417 s->first_displayed_entry->idx);
3420 select_commit(s);
3422 if (s->thread_args.log_complete &&
3423 s->selected_entry->idx == s->commits->ncommits - 1)
3424 view->count = 0;
3426 return NULL;
3429 static void
3430 view_get_split(struct tog_view *view, int *y, int *x)
3432 *x = 0;
3433 *y = 0;
3435 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3436 if (view->child && view->child->resized_y)
3437 *y = view->child->resized_y;
3438 else if (view->resized_y)
3439 *y = view->resized_y;
3440 else
3441 *y = view_split_begin_y(view->lines);
3442 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3443 if (view->child && view->child->resized_x)
3444 *x = view->child->resized_x;
3445 else if (view->resized_x)
3446 *x = view->resized_x;
3447 else
3448 *x = view_split_begin_x(view->begin_x);
3452 /* Split view horizontally at y and offset view->state->selected line. */
3453 static const struct got_error *
3454 view_init_hsplit(struct tog_view *view, int y)
3456 const struct got_error *err = NULL;
3458 view->nlines = y;
3459 view->ncols = COLS;
3460 err = view_resize(view);
3461 if (err)
3462 return err;
3464 err = offset_selection_down(view);
3466 return err;
3469 static const struct got_error *
3470 log_goto_line(struct tog_view *view, int nlines)
3472 const struct got_error *err = NULL;
3473 struct tog_log_view_state *s = &view->state.log;
3474 int g, idx = s->selected_entry->idx;
3476 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3477 return NULL;
3479 g = view->gline;
3480 view->gline = 0;
3482 if (g >= s->first_displayed_entry->idx + 1 &&
3483 g <= s->last_displayed_entry->idx + 1 &&
3484 g - s->first_displayed_entry->idx - 1 < nlines) {
3485 s->selected = g - s->first_displayed_entry->idx - 1;
3486 select_commit(s);
3487 return NULL;
3490 if (idx + 1 < g) {
3491 err = log_move_cursor_down(view, g - idx - 1);
3492 if (!err && g > s->selected_entry->idx + 1)
3493 err = log_move_cursor_down(view,
3494 g - s->first_displayed_entry->idx - 1);
3495 if (err)
3496 return err;
3497 } else if (idx + 1 > g)
3498 log_move_cursor_up(view, idx - g + 1, 0);
3500 if (g < nlines && s->first_displayed_entry->idx == 0)
3501 s->selected = g - 1;
3503 select_commit(s);
3504 return NULL;
3508 static const struct got_error *
3509 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3511 const struct got_error *err = NULL;
3512 struct tog_log_view_state *s = &view->state.log;
3513 int eos, nscroll;
3515 if (s->thread_args.load_all) {
3516 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3517 s->thread_args.load_all = 0;
3518 else if (s->thread_args.log_complete) {
3519 err = log_move_cursor_down(view, s->commits->ncommits);
3520 s->thread_args.load_all = 0;
3522 if (err)
3523 return err;
3526 eos = nscroll = view->nlines - 1;
3527 if (view_is_hsplit_top(view))
3528 --eos; /* border */
3530 if (view->gline)
3531 return log_goto_line(view, eos);
3533 switch (ch) {
3534 case '&':
3535 err = limit_log_view(view);
3536 break;
3537 case 'q':
3538 s->quit = 1;
3539 break;
3540 case '0':
3541 view->x = 0;
3542 break;
3543 case '$':
3544 view->x = MAX(view->maxx - view->ncols / 2, 0);
3545 view->count = 0;
3546 break;
3547 case KEY_RIGHT:
3548 case 'l':
3549 if (view->x + view->ncols / 2 < view->maxx)
3550 view->x += 2; /* move two columns right */
3551 else
3552 view->count = 0;
3553 break;
3554 case KEY_LEFT:
3555 case 'h':
3556 view->x -= MIN(view->x, 2); /* move two columns back */
3557 if (view->x <= 0)
3558 view->count = 0;
3559 break;
3560 case 'k':
3561 case KEY_UP:
3562 case '<':
3563 case ',':
3564 case CTRL('p'):
3565 log_move_cursor_up(view, 0, 0);
3566 break;
3567 case 'g':
3568 case KEY_HOME:
3569 log_move_cursor_up(view, 0, 1);
3570 view->count = 0;
3571 break;
3572 case CTRL('u'):
3573 case 'u':
3574 nscroll /= 2;
3575 /* FALL THROUGH */
3576 case KEY_PPAGE:
3577 case CTRL('b'):
3578 case 'b':
3579 log_move_cursor_up(view, nscroll, 0);
3580 break;
3581 case 'j':
3582 case KEY_DOWN:
3583 case '>':
3584 case '.':
3585 case CTRL('n'):
3586 err = log_move_cursor_down(view, 0);
3587 break;
3588 case '@':
3589 s->use_committer = !s->use_committer;
3590 break;
3591 case 'G':
3592 case KEY_END: {
3593 /* We don't know yet how many commits, so we're forced to
3594 * traverse them all. */
3595 view->count = 0;
3596 s->thread_args.load_all = 1;
3597 if (!s->thread_args.log_complete)
3598 return trigger_log_thread(view, 0);
3599 err = log_move_cursor_down(view, s->commits->ncommits);
3600 s->thread_args.load_all = 0;
3601 break;
3603 case CTRL('d'):
3604 case 'd':
3605 nscroll /= 2;
3606 /* FALL THROUGH */
3607 case KEY_NPAGE:
3608 case CTRL('f'):
3609 case 'f':
3610 case ' ':
3611 err = log_move_cursor_down(view, nscroll);
3612 break;
3613 case KEY_RESIZE:
3614 if (s->selected > view->nlines - 2)
3615 s->selected = view->nlines - 2;
3616 if (s->selected > s->commits->ncommits - 1)
3617 s->selected = s->commits->ncommits - 1;
3618 select_commit(s);
3619 if (s->commits->ncommits < view->nlines - 1 &&
3620 !s->thread_args.log_complete) {
3621 s->thread_args.commits_needed += (view->nlines - 1) -
3622 s->commits->ncommits;
3623 err = trigger_log_thread(view, 1);
3625 break;
3626 case KEY_ENTER:
3627 case '\r':
3628 view->count = 0;
3629 if (s->selected_entry == NULL)
3630 break;
3631 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3632 break;
3633 case 'T':
3634 view->count = 0;
3635 if (s->selected_entry == NULL)
3636 break;
3637 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3638 break;
3639 case KEY_BACKSPACE:
3640 case CTRL('l'):
3641 case 'B':
3642 view->count = 0;
3643 if (ch == KEY_BACKSPACE &&
3644 got_path_is_root_dir(s->in_repo_path))
3645 break;
3646 err = stop_log_thread(s);
3647 if (err)
3648 return err;
3649 if (ch == KEY_BACKSPACE) {
3650 char *parent_path;
3651 err = got_path_dirname(&parent_path, s->in_repo_path);
3652 if (err)
3653 return err;
3654 free(s->in_repo_path);
3655 s->in_repo_path = parent_path;
3656 s->thread_args.in_repo_path = s->in_repo_path;
3657 } else if (ch == CTRL('l')) {
3658 struct got_object_id *start_id;
3659 err = got_repo_match_object_id(&start_id, NULL,
3660 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3661 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3662 if (err)
3663 return err;
3664 free(s->start_id);
3665 s->start_id = start_id;
3666 s->thread_args.start_id = s->start_id;
3667 } else /* 'B' */
3668 s->log_branches = !s->log_branches;
3670 if (s->thread_args.pack_fds == NULL) {
3671 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3672 if (err)
3673 return err;
3675 err = got_repo_open(&s->thread_args.repo,
3676 got_repo_get_path(s->repo), NULL,
3677 s->thread_args.pack_fds);
3678 if (err)
3679 return err;
3680 tog_free_refs();
3681 err = tog_load_refs(s->repo, 0);
3682 if (err)
3683 return err;
3684 err = got_commit_graph_open(&s->thread_args.graph,
3685 s->in_repo_path, !s->log_branches);
3686 if (err)
3687 return err;
3688 err = got_commit_graph_iter_start(s->thread_args.graph,
3689 s->start_id, s->repo, NULL, NULL);
3690 if (err)
3691 return err;
3692 free_commits(&s->real_commits);
3693 free_commits(&s->limit_commits);
3694 s->first_displayed_entry = NULL;
3695 s->last_displayed_entry = NULL;
3696 s->selected_entry = NULL;
3697 s->selected = 0;
3698 s->thread_args.log_complete = 0;
3699 s->quit = 0;
3700 s->thread_args.commits_needed = view->lines;
3701 s->matched_entry = NULL;
3702 s->search_entry = NULL;
3703 view->offset = 0;
3704 break;
3705 case 'R':
3706 view->count = 0;
3707 err = view_request_new(new_view, view, TOG_VIEW_REF);
3708 break;
3709 default:
3710 view->count = 0;
3711 break;
3714 return err;
3717 static const struct got_error *
3718 apply_unveil(const char *repo_path, const char *worktree_path)
3720 const struct got_error *error;
3722 #ifdef PROFILE
3723 if (unveil("gmon.out", "rwc") != 0)
3724 return got_error_from_errno2("unveil", "gmon.out");
3725 #endif
3726 if (repo_path && unveil(repo_path, "r") != 0)
3727 return got_error_from_errno2("unveil", repo_path);
3729 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3730 return got_error_from_errno2("unveil", worktree_path);
3732 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3733 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3735 error = got_privsep_unveil_exec_helpers();
3736 if (error != NULL)
3737 return error;
3739 if (unveil(NULL, NULL) != 0)
3740 return got_error_from_errno("unveil");
3742 return NULL;
3745 static void
3746 init_curses(void)
3749 * Override default signal handlers before starting ncurses.
3750 * This should prevent ncurses from installing its own
3751 * broken cleanup() signal handler.
3753 signal(SIGWINCH, tog_sigwinch);
3754 signal(SIGPIPE, tog_sigpipe);
3755 signal(SIGCONT, tog_sigcont);
3756 signal(SIGINT, tog_sigint);
3757 signal(SIGTERM, tog_sigterm);
3759 initscr();
3760 cbreak();
3761 halfdelay(1); /* Do fast refresh while initial view is loading. */
3762 noecho();
3763 nonl();
3764 intrflush(stdscr, FALSE);
3765 keypad(stdscr, TRUE);
3766 curs_set(0);
3767 if (getenv("TOG_COLORS") != NULL) {
3768 start_color();
3769 use_default_colors();
3773 static const struct got_error *
3774 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3775 struct got_repository *repo, struct got_worktree *worktree)
3777 const struct got_error *err = NULL;
3779 if (argc == 0) {
3780 *in_repo_path = strdup("/");
3781 if (*in_repo_path == NULL)
3782 return got_error_from_errno("strdup");
3783 return NULL;
3786 if (worktree) {
3787 const char *prefix = got_worktree_get_path_prefix(worktree);
3788 char *p;
3790 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3791 if (err)
3792 return err;
3793 if (asprintf(in_repo_path, "%s%s%s", prefix,
3794 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3795 p) == -1) {
3796 err = got_error_from_errno("asprintf");
3797 *in_repo_path = NULL;
3799 free(p);
3800 } else
3801 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3803 return err;
3806 static const struct got_error *
3807 cmd_log(int argc, char *argv[])
3809 const struct got_error *error;
3810 struct got_repository *repo = NULL;
3811 struct got_worktree *worktree = NULL;
3812 struct got_object_id *start_id = NULL;
3813 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3814 char *start_commit = NULL, *label = NULL;
3815 struct got_reference *ref = NULL;
3816 const char *head_ref_name = NULL;
3817 int ch, log_branches = 0;
3818 struct tog_view *view;
3819 int *pack_fds = NULL;
3821 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3822 switch (ch) {
3823 case 'b':
3824 log_branches = 1;
3825 break;
3826 case 'c':
3827 start_commit = optarg;
3828 break;
3829 case 'r':
3830 repo_path = realpath(optarg, NULL);
3831 if (repo_path == NULL)
3832 return got_error_from_errno2("realpath",
3833 optarg);
3834 break;
3835 default:
3836 usage_log();
3837 /* NOTREACHED */
3841 argc -= optind;
3842 argv += optind;
3844 if (argc > 1)
3845 usage_log();
3847 error = got_repo_pack_fds_open(&pack_fds);
3848 if (error != NULL)
3849 goto done;
3851 if (repo_path == NULL) {
3852 cwd = getcwd(NULL, 0);
3853 if (cwd == NULL)
3854 return got_error_from_errno("getcwd");
3855 error = got_worktree_open(&worktree, cwd);
3856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3857 goto done;
3858 if (worktree)
3859 repo_path =
3860 strdup(got_worktree_get_repo_path(worktree));
3861 else
3862 repo_path = strdup(cwd);
3863 if (repo_path == NULL) {
3864 error = got_error_from_errno("strdup");
3865 goto done;
3869 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3870 if (error != NULL)
3871 goto done;
3873 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3874 repo, worktree);
3875 if (error)
3876 goto done;
3878 init_curses();
3880 error = apply_unveil(got_repo_get_path(repo),
3881 worktree ? got_worktree_get_root_path(worktree) : NULL);
3882 if (error)
3883 goto done;
3885 /* already loaded by tog_log_with_path()? */
3886 if (TAILQ_EMPTY(&tog_refs)) {
3887 error = tog_load_refs(repo, 0);
3888 if (error)
3889 goto done;
3892 if (start_commit == NULL) {
3893 error = got_repo_match_object_id(&start_id, &label,
3894 worktree ? got_worktree_get_head_ref_name(worktree) :
3895 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3896 if (error)
3897 goto done;
3898 head_ref_name = label;
3899 } else {
3900 error = got_ref_open(&ref, repo, start_commit, 0);
3901 if (error == NULL)
3902 head_ref_name = got_ref_get_name(ref);
3903 else if (error->code != GOT_ERR_NOT_REF)
3904 goto done;
3905 error = got_repo_match_object_id(&start_id, NULL,
3906 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3907 if (error)
3908 goto done;
3911 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3912 if (view == NULL) {
3913 error = got_error_from_errno("view_open");
3914 goto done;
3916 error = open_log_view(view, start_id, repo, head_ref_name,
3917 in_repo_path, log_branches);
3918 if (error)
3919 goto done;
3920 if (worktree) {
3921 /* Release work tree lock. */
3922 got_worktree_close(worktree);
3923 worktree = NULL;
3925 error = view_loop(view);
3926 done:
3927 free(in_repo_path);
3928 free(repo_path);
3929 free(cwd);
3930 free(start_id);
3931 free(label);
3932 if (ref)
3933 got_ref_close(ref);
3934 if (repo) {
3935 const struct got_error *close_err = got_repo_close(repo);
3936 if (error == NULL)
3937 error = close_err;
3939 if (worktree)
3940 got_worktree_close(worktree);
3941 if (pack_fds) {
3942 const struct got_error *pack_err =
3943 got_repo_pack_fds_close(pack_fds);
3944 if (error == NULL)
3945 error = pack_err;
3947 tog_free_refs();
3948 return error;
3951 __dead static void
3952 usage_diff(void)
3954 endwin();
3955 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3956 "object1 object2\n", getprogname());
3957 exit(1);
3960 static int
3961 match_line(const char *line, regex_t *regex, size_t nmatch,
3962 regmatch_t *regmatch)
3964 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3967 static struct tog_color *
3968 match_color(struct tog_colors *colors, const char *line)
3970 struct tog_color *tc = NULL;
3972 STAILQ_FOREACH(tc, colors, entry) {
3973 if (match_line(line, &tc->regex, 0, NULL))
3974 return tc;
3977 return NULL;
3980 static const struct got_error *
3981 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3982 WINDOW *window, int skipcol, regmatch_t *regmatch)
3984 const struct got_error *err = NULL;
3985 char *exstr = NULL;
3986 wchar_t *wline = NULL;
3987 int rme, rms, n, width, scrollx;
3988 int width0 = 0, width1 = 0, width2 = 0;
3989 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3991 *wtotal = 0;
3993 rms = regmatch->rm_so;
3994 rme = regmatch->rm_eo;
3996 err = expand_tab(&exstr, line);
3997 if (err)
3998 return err;
4000 /* Split the line into 3 segments, according to match offsets. */
4001 seg0 = strndup(exstr, rms);
4002 if (seg0 == NULL) {
4003 err = got_error_from_errno("strndup");
4004 goto done;
4006 seg1 = strndup(exstr + rms, rme - rms);
4007 if (seg1 == NULL) {
4008 err = got_error_from_errno("strndup");
4009 goto done;
4011 seg2 = strdup(exstr + rme);
4012 if (seg2 == NULL) {
4013 err = got_error_from_errno("strndup");
4014 goto done;
4017 /* draw up to matched token if we haven't scrolled past it */
4018 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4019 col_tab_align, 1);
4020 if (err)
4021 goto done;
4022 n = MAX(width0 - skipcol, 0);
4023 if (n) {
4024 free(wline);
4025 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4026 wlimit, col_tab_align, 1);
4027 if (err)
4028 goto done;
4029 waddwstr(window, &wline[scrollx]);
4030 wlimit -= width;
4031 *wtotal += width;
4034 if (wlimit > 0) {
4035 int i = 0, w = 0;
4036 size_t wlen;
4038 free(wline);
4039 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4040 col_tab_align, 1);
4041 if (err)
4042 goto done;
4043 wlen = wcslen(wline);
4044 while (i < wlen) {
4045 width = wcwidth(wline[i]);
4046 if (width == -1) {
4047 /* should not happen, tabs are expanded */
4048 err = got_error(GOT_ERR_RANGE);
4049 goto done;
4051 if (width0 + w + width > skipcol)
4052 break;
4053 w += width;
4054 i++;
4056 /* draw (visible part of) matched token (if scrolled into it) */
4057 if (width1 - w > 0) {
4058 wattron(window, A_STANDOUT);
4059 waddwstr(window, &wline[i]);
4060 wattroff(window, A_STANDOUT);
4061 wlimit -= (width1 - w);
4062 *wtotal += (width1 - w);
4066 if (wlimit > 0) { /* draw rest of line */
4067 free(wline);
4068 if (skipcol > width0 + width1) {
4069 err = format_line(&wline, &width2, &scrollx, seg2,
4070 skipcol - (width0 + width1), wlimit,
4071 col_tab_align, 1);
4072 if (err)
4073 goto done;
4074 waddwstr(window, &wline[scrollx]);
4075 } else {
4076 err = format_line(&wline, &width2, NULL, seg2, 0,
4077 wlimit, col_tab_align, 1);
4078 if (err)
4079 goto done;
4080 waddwstr(window, wline);
4082 *wtotal += width2;
4084 done:
4085 free(wline);
4086 free(exstr);
4087 free(seg0);
4088 free(seg1);
4089 free(seg2);
4090 return err;
4093 static int
4094 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4096 FILE *f = NULL;
4097 int *eof, *first, *selected;
4099 if (view->type == TOG_VIEW_DIFF) {
4100 struct tog_diff_view_state *s = &view->state.diff;
4102 first = &s->first_displayed_line;
4103 selected = first;
4104 eof = &s->eof;
4105 f = s->f;
4106 } else if (view->type == TOG_VIEW_BLAME) {
4107 struct tog_blame_view_state *s = &view->state.blame;
4109 first = &s->first_displayed_line;
4110 selected = &s->selected_line;
4111 eof = &s->eof;
4112 f = s->blame.f;
4113 } else
4114 return 0;
4116 /* Center gline in the middle of the page like vi(1). */
4117 if (*lineno < view->gline - (view->nlines - 3) / 2)
4118 return 0;
4119 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4120 rewind(f);
4121 *eof = 0;
4122 *first = 1;
4123 *lineno = 0;
4124 *nprinted = 0;
4125 return 0;
4128 *selected = view->gline <= (view->nlines - 3) / 2 ?
4129 view->gline : (view->nlines - 3) / 2 + 1;
4130 view->gline = 0;
4132 return 1;
4135 static const struct got_error *
4136 draw_file(struct tog_view *view, const char *header)
4138 struct tog_diff_view_state *s = &view->state.diff;
4139 regmatch_t *regmatch = &view->regmatch;
4140 const struct got_error *err;
4141 int nprinted = 0;
4142 char *line;
4143 size_t linesize = 0;
4144 ssize_t linelen;
4145 wchar_t *wline;
4146 int width;
4147 int max_lines = view->nlines;
4148 int nlines = s->nlines;
4149 off_t line_offset;
4151 s->lineno = s->first_displayed_line - 1;
4152 line_offset = s->lines[s->first_displayed_line - 1].offset;
4153 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4154 return got_error_from_errno("fseek");
4156 werase(view->window);
4158 if (view->gline > s->nlines - 1)
4159 view->gline = s->nlines - 1;
4161 if (header) {
4162 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4163 1 : view->gline - (view->nlines - 3) / 2 :
4164 s->lineno + s->selected_line;
4166 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4167 return got_error_from_errno("asprintf");
4168 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4169 0, 0);
4170 free(line);
4171 if (err)
4172 return err;
4174 if (view_needs_focus_indication(view))
4175 wstandout(view->window);
4176 waddwstr(view->window, wline);
4177 free(wline);
4178 wline = NULL;
4179 while (width++ < view->ncols)
4180 waddch(view->window, ' ');
4181 if (view_needs_focus_indication(view))
4182 wstandend(view->window);
4184 if (max_lines <= 1)
4185 return NULL;
4186 max_lines--;
4189 s->eof = 0;
4190 view->maxx = 0;
4191 line = NULL;
4192 while (max_lines > 0 && nprinted < max_lines) {
4193 enum got_diff_line_type linetype;
4194 attr_t attr = 0;
4196 linelen = getline(&line, &linesize, s->f);
4197 if (linelen == -1) {
4198 if (feof(s->f)) {
4199 s->eof = 1;
4200 break;
4202 free(line);
4203 return got_ferror(s->f, GOT_ERR_IO);
4206 if (++s->lineno < s->first_displayed_line)
4207 continue;
4208 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4209 continue;
4210 if (s->lineno == view->hiline)
4211 attr = A_STANDOUT;
4213 /* Set view->maxx based on full line length. */
4214 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4215 view->x ? 1 : 0);
4216 if (err) {
4217 free(line);
4218 return err;
4220 view->maxx = MAX(view->maxx, width);
4221 free(wline);
4222 wline = NULL;
4224 linetype = s->lines[s->lineno].type;
4225 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4226 linetype < GOT_DIFF_LINE_CONTEXT)
4227 attr |= COLOR_PAIR(linetype);
4228 if (attr)
4229 wattron(view->window, attr);
4230 if (s->first_displayed_line + nprinted == s->matched_line &&
4231 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4232 err = add_matched_line(&width, line, view->ncols, 0,
4233 view->window, view->x, regmatch);
4234 if (err) {
4235 free(line);
4236 return err;
4238 } else {
4239 int skip;
4240 err = format_line(&wline, &width, &skip, line,
4241 view->x, view->ncols, 0, view->x ? 1 : 0);
4242 if (err) {
4243 free(line);
4244 return err;
4246 waddwstr(view->window, &wline[skip]);
4247 free(wline);
4248 wline = NULL;
4250 if (s->lineno == view->hiline) {
4251 /* highlight full gline length */
4252 while (width++ < view->ncols)
4253 waddch(view->window, ' ');
4254 } else {
4255 if (width <= view->ncols - 1)
4256 waddch(view->window, '\n');
4258 if (attr)
4259 wattroff(view->window, attr);
4260 if (++nprinted == 1)
4261 s->first_displayed_line = s->lineno;
4263 free(line);
4264 if (nprinted >= 1)
4265 s->last_displayed_line = s->first_displayed_line +
4266 (nprinted - 1);
4267 else
4268 s->last_displayed_line = s->first_displayed_line;
4270 view_border(view);
4272 if (s->eof) {
4273 while (nprinted < view->nlines) {
4274 waddch(view->window, '\n');
4275 nprinted++;
4278 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4279 view->ncols, 0, 0);
4280 if (err) {
4281 return err;
4284 wstandout(view->window);
4285 waddwstr(view->window, wline);
4286 free(wline);
4287 wline = NULL;
4288 wstandend(view->window);
4291 return NULL;
4294 static char *
4295 get_datestr(time_t *time, char *datebuf)
4297 struct tm mytm, *tm;
4298 char *p, *s;
4300 tm = gmtime_r(time, &mytm);
4301 if (tm == NULL)
4302 return NULL;
4303 s = asctime_r(tm, datebuf);
4304 if (s == NULL)
4305 return NULL;
4306 p = strchr(s, '\n');
4307 if (p)
4308 *p = '\0';
4309 return s;
4312 static const struct got_error *
4313 get_changed_paths(struct got_pathlist_head *paths,
4314 struct got_commit_object *commit, struct got_repository *repo)
4316 const struct got_error *err = NULL;
4317 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4318 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4319 struct got_object_qid *qid;
4321 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4322 if (qid != NULL) {
4323 struct got_commit_object *pcommit;
4324 err = got_object_open_as_commit(&pcommit, repo,
4325 &qid->id);
4326 if (err)
4327 return err;
4329 tree_id1 = got_object_id_dup(
4330 got_object_commit_get_tree_id(pcommit));
4331 if (tree_id1 == NULL) {
4332 got_object_commit_close(pcommit);
4333 return got_error_from_errno("got_object_id_dup");
4335 got_object_commit_close(pcommit);
4339 if (tree_id1) {
4340 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4341 if (err)
4342 goto done;
4345 tree_id2 = got_object_commit_get_tree_id(commit);
4346 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4347 if (err)
4348 goto done;
4350 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4351 got_diff_tree_collect_changed_paths, paths, 0);
4352 done:
4353 if (tree1)
4354 got_object_tree_close(tree1);
4355 if (tree2)
4356 got_object_tree_close(tree2);
4357 free(tree_id1);
4358 return err;
4361 static const struct got_error *
4362 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4363 off_t off, uint8_t type)
4365 struct got_diff_line *p;
4367 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4368 if (p == NULL)
4369 return got_error_from_errno("reallocarray");
4370 *lines = p;
4371 (*lines)[*nlines].offset = off;
4372 (*lines)[*nlines].type = type;
4373 (*nlines)++;
4375 return NULL;
4378 static const struct got_error *
4379 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4380 struct got_object_id *commit_id, struct got_reflist_head *refs,
4381 struct got_repository *repo, FILE *outfile)
4383 const struct got_error *err = NULL;
4384 char datebuf[26], *datestr;
4385 struct got_commit_object *commit;
4386 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4387 time_t committer_time;
4388 const char *author, *committer;
4389 char *refs_str = NULL;
4390 struct got_pathlist_head changed_paths;
4391 struct got_pathlist_entry *pe;
4392 off_t outoff = 0;
4393 int n;
4395 TAILQ_INIT(&changed_paths);
4397 if (refs) {
4398 err = build_refs_str(&refs_str, refs, commit_id, repo);
4399 if (err)
4400 return err;
4403 err = got_object_open_as_commit(&commit, repo, commit_id);
4404 if (err)
4405 return err;
4407 err = got_object_id_str(&id_str, commit_id);
4408 if (err) {
4409 err = got_error_from_errno("got_object_id_str");
4410 goto done;
4413 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4414 if (err)
4415 goto done;
4417 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4418 refs_str ? refs_str : "", refs_str ? ")" : "");
4419 if (n < 0) {
4420 err = got_error_from_errno("fprintf");
4421 goto done;
4423 outoff += n;
4424 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4425 if (err)
4426 goto done;
4428 n = fprintf(outfile, "from: %s\n",
4429 got_object_commit_get_author(commit));
4430 if (n < 0) {
4431 err = got_error_from_errno("fprintf");
4432 goto done;
4434 outoff += n;
4435 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4436 if (err)
4437 goto done;
4439 committer_time = got_object_commit_get_committer_time(commit);
4440 datestr = get_datestr(&committer_time, datebuf);
4441 if (datestr) {
4442 n = fprintf(outfile, "date: %s UTC\n", datestr);
4443 if (n < 0) {
4444 err = got_error_from_errno("fprintf");
4445 goto done;
4447 outoff += n;
4448 err = add_line_metadata(lines, nlines, outoff,
4449 GOT_DIFF_LINE_DATE);
4450 if (err)
4451 goto done;
4453 author = got_object_commit_get_author(commit);
4454 committer = got_object_commit_get_committer(commit);
4455 if (strcmp(author, committer) != 0) {
4456 n = fprintf(outfile, "via: %s\n", committer);
4457 if (n < 0) {
4458 err = got_error_from_errno("fprintf");
4459 goto done;
4461 outoff += n;
4462 err = add_line_metadata(lines, nlines, outoff,
4463 GOT_DIFF_LINE_AUTHOR);
4464 if (err)
4465 goto done;
4467 if (got_object_commit_get_nparents(commit) > 1) {
4468 const struct got_object_id_queue *parent_ids;
4469 struct got_object_qid *qid;
4470 int pn = 1;
4471 parent_ids = got_object_commit_get_parent_ids(commit);
4472 STAILQ_FOREACH(qid, parent_ids, entry) {
4473 err = got_object_id_str(&id_str, &qid->id);
4474 if (err)
4475 goto done;
4476 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4477 if (n < 0) {
4478 err = got_error_from_errno("fprintf");
4479 goto done;
4481 outoff += n;
4482 err = add_line_metadata(lines, nlines, outoff,
4483 GOT_DIFF_LINE_META);
4484 if (err)
4485 goto done;
4486 free(id_str);
4487 id_str = NULL;
4491 err = got_object_commit_get_logmsg(&logmsg, commit);
4492 if (err)
4493 goto done;
4494 s = logmsg;
4495 while ((line = strsep(&s, "\n")) != NULL) {
4496 n = fprintf(outfile, "%s\n", line);
4497 if (n < 0) {
4498 err = got_error_from_errno("fprintf");
4499 goto done;
4501 outoff += n;
4502 err = add_line_metadata(lines, nlines, outoff,
4503 GOT_DIFF_LINE_LOGMSG);
4504 if (err)
4505 goto done;
4508 err = get_changed_paths(&changed_paths, commit, repo);
4509 if (err)
4510 goto done;
4511 TAILQ_FOREACH(pe, &changed_paths, entry) {
4512 struct got_diff_changed_path *cp = pe->data;
4513 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4514 if (n < 0) {
4515 err = got_error_from_errno("fprintf");
4516 goto done;
4518 outoff += n;
4519 err = add_line_metadata(lines, nlines, outoff,
4520 GOT_DIFF_LINE_CHANGES);
4521 if (err)
4522 goto done;
4523 free((char *)pe->path);
4524 free(pe->data);
4527 fputc('\n', outfile);
4528 outoff++;
4529 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4530 done:
4531 got_pathlist_free(&changed_paths);
4532 free(id_str);
4533 free(logmsg);
4534 free(refs_str);
4535 got_object_commit_close(commit);
4536 if (err) {
4537 free(*lines);
4538 *lines = NULL;
4539 *nlines = 0;
4541 return err;
4544 static const struct got_error *
4545 create_diff(struct tog_diff_view_state *s)
4547 const struct got_error *err = NULL;
4548 FILE *f = NULL;
4549 int obj_type;
4551 free(s->lines);
4552 s->lines = malloc(sizeof(*s->lines));
4553 if (s->lines == NULL)
4554 return got_error_from_errno("malloc");
4555 s->nlines = 0;
4557 f = got_opentemp();
4558 if (f == NULL) {
4559 err = got_error_from_errno("got_opentemp");
4560 goto done;
4562 if (s->f && fclose(s->f) == EOF) {
4563 err = got_error_from_errno("fclose");
4564 goto done;
4566 s->f = f;
4568 if (s->id1)
4569 err = got_object_get_type(&obj_type, s->repo, s->id1);
4570 else
4571 err = got_object_get_type(&obj_type, s->repo, s->id2);
4572 if (err)
4573 goto done;
4575 switch (obj_type) {
4576 case GOT_OBJ_TYPE_BLOB:
4577 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4578 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4579 s->label1, s->label2, tog_diff_algo, s->diff_context,
4580 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4581 break;
4582 case GOT_OBJ_TYPE_TREE:
4583 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4584 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4585 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4586 s->force_text_diff, s->repo, s->f);
4587 break;
4588 case GOT_OBJ_TYPE_COMMIT: {
4589 const struct got_object_id_queue *parent_ids;
4590 struct got_object_qid *pid;
4591 struct got_commit_object *commit2;
4592 struct got_reflist_head *refs;
4594 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4595 if (err)
4596 goto done;
4597 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4598 /* Show commit info if we're diffing to a parent/root commit. */
4599 if (s->id1 == NULL) {
4600 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4601 refs, s->repo, s->f);
4602 if (err)
4603 goto done;
4604 } else {
4605 parent_ids = got_object_commit_get_parent_ids(commit2);
4606 STAILQ_FOREACH(pid, parent_ids, entry) {
4607 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4608 err = write_commit_info(&s->lines,
4609 &s->nlines, s->id2, refs, s->repo,
4610 s->f);
4611 if (err)
4612 goto done;
4613 break;
4617 got_object_commit_close(commit2);
4619 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4620 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4621 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4622 s->force_text_diff, s->repo, s->f);
4623 break;
4625 default:
4626 err = got_error(GOT_ERR_OBJ_TYPE);
4627 break;
4629 done:
4630 if (s->f && fflush(s->f) != 0 && err == NULL)
4631 err = got_error_from_errno("fflush");
4632 return err;
4635 static void
4636 diff_view_indicate_progress(struct tog_view *view)
4638 mvwaddstr(view->window, 0, 0, "diffing...");
4639 update_panels();
4640 doupdate();
4643 static const struct got_error *
4644 search_start_diff_view(struct tog_view *view)
4646 struct tog_diff_view_state *s = &view->state.diff;
4648 s->matched_line = 0;
4649 return NULL;
4652 static const struct got_error *
4653 search_next_diff_view(struct tog_view *view)
4655 struct tog_diff_view_state *s = &view->state.diff;
4656 const struct got_error *err = NULL;
4657 int lineno;
4658 char *line = NULL;
4659 size_t linesize = 0;
4660 ssize_t linelen;
4662 if (!view->searching) {
4663 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4664 return NULL;
4667 if (s->matched_line) {
4668 if (view->searching == TOG_SEARCH_FORWARD)
4669 lineno = s->matched_line + 1;
4670 else
4671 lineno = s->matched_line - 1;
4672 } else
4673 lineno = s->first_displayed_line;
4675 while (1) {
4676 off_t offset;
4678 if (lineno <= 0 || lineno > s->nlines) {
4679 if (s->matched_line == 0) {
4680 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4681 break;
4684 if (view->searching == TOG_SEARCH_FORWARD)
4685 lineno = 1;
4686 else
4687 lineno = s->nlines;
4690 offset = s->lines[lineno - 1].offset;
4691 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4692 free(line);
4693 return got_error_from_errno("fseeko");
4695 linelen = getline(&line, &linesize, s->f);
4696 if (linelen != -1) {
4697 char *exstr;
4698 err = expand_tab(&exstr, line);
4699 if (err)
4700 break;
4701 if (match_line(exstr, &view->regex, 1,
4702 &view->regmatch)) {
4703 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4704 s->matched_line = lineno;
4705 free(exstr);
4706 break;
4708 free(exstr);
4710 if (view->searching == TOG_SEARCH_FORWARD)
4711 lineno++;
4712 else
4713 lineno--;
4715 free(line);
4717 if (s->matched_line) {
4718 s->first_displayed_line = s->matched_line;
4719 s->selected_line = 1;
4722 return err;
4725 static const struct got_error *
4726 close_diff_view(struct tog_view *view)
4728 const struct got_error *err = NULL;
4729 struct tog_diff_view_state *s = &view->state.diff;
4731 free(s->id1);
4732 s->id1 = NULL;
4733 free(s->id2);
4734 s->id2 = NULL;
4735 if (s->f && fclose(s->f) == EOF)
4736 err = got_error_from_errno("fclose");
4737 s->f = NULL;
4738 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4739 err = got_error_from_errno("fclose");
4740 s->f1 = NULL;
4741 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4742 err = got_error_from_errno("fclose");
4743 s->f2 = NULL;
4744 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4745 err = got_error_from_errno("close");
4746 s->fd1 = -1;
4747 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4748 err = got_error_from_errno("close");
4749 s->fd2 = -1;
4750 free(s->lines);
4751 s->lines = NULL;
4752 s->nlines = 0;
4753 return err;
4756 static const struct got_error *
4757 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4758 struct got_object_id *id2, const char *label1, const char *label2,
4759 int diff_context, int ignore_whitespace, int force_text_diff,
4760 struct tog_view *parent_view, struct got_repository *repo)
4762 const struct got_error *err;
4763 struct tog_diff_view_state *s = &view->state.diff;
4765 memset(s, 0, sizeof(*s));
4766 s->fd1 = -1;
4767 s->fd2 = -1;
4769 if (id1 != NULL && id2 != NULL) {
4770 int type1, type2;
4771 err = got_object_get_type(&type1, repo, id1);
4772 if (err)
4773 return err;
4774 err = got_object_get_type(&type2, repo, id2);
4775 if (err)
4776 return err;
4778 if (type1 != type2)
4779 return got_error(GOT_ERR_OBJ_TYPE);
4781 s->first_displayed_line = 1;
4782 s->last_displayed_line = view->nlines;
4783 s->selected_line = 1;
4784 s->repo = repo;
4785 s->id1 = id1;
4786 s->id2 = id2;
4787 s->label1 = label1;
4788 s->label2 = label2;
4790 if (id1) {
4791 s->id1 = got_object_id_dup(id1);
4792 if (s->id1 == NULL)
4793 return got_error_from_errno("got_object_id_dup");
4794 } else
4795 s->id1 = NULL;
4797 s->id2 = got_object_id_dup(id2);
4798 if (s->id2 == NULL) {
4799 err = got_error_from_errno("got_object_id_dup");
4800 goto done;
4803 s->f1 = got_opentemp();
4804 if (s->f1 == NULL) {
4805 err = got_error_from_errno("got_opentemp");
4806 goto done;
4809 s->f2 = got_opentemp();
4810 if (s->f2 == NULL) {
4811 err = got_error_from_errno("got_opentemp");
4812 goto done;
4815 s->fd1 = got_opentempfd();
4816 if (s->fd1 == -1) {
4817 err = got_error_from_errno("got_opentempfd");
4818 goto done;
4821 s->fd2 = got_opentempfd();
4822 if (s->fd2 == -1) {
4823 err = got_error_from_errno("got_opentempfd");
4824 goto done;
4827 s->first_displayed_line = 1;
4828 s->last_displayed_line = view->nlines;
4829 s->diff_context = diff_context;
4830 s->ignore_whitespace = ignore_whitespace;
4831 s->force_text_diff = force_text_diff;
4832 s->parent_view = parent_view;
4833 s->repo = repo;
4835 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4836 int rc;
4838 rc = init_pair(GOT_DIFF_LINE_MINUS,
4839 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4840 if (rc != ERR)
4841 rc = init_pair(GOT_DIFF_LINE_PLUS,
4842 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4843 if (rc != ERR)
4844 rc = init_pair(GOT_DIFF_LINE_HUNK,
4845 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4846 if (rc != ERR)
4847 rc = init_pair(GOT_DIFF_LINE_META,
4848 get_color_value("TOG_COLOR_DIFF_META"), -1);
4849 if (rc != ERR)
4850 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4851 get_color_value("TOG_COLOR_DIFF_META"), -1);
4852 if (rc != ERR)
4853 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4854 get_color_value("TOG_COLOR_DIFF_META"), -1);
4855 if (rc != ERR)
4856 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4857 get_color_value("TOG_COLOR_DIFF_META"), -1);
4858 if (rc != ERR)
4859 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4860 get_color_value("TOG_COLOR_AUTHOR"), -1);
4861 if (rc != ERR)
4862 rc = init_pair(GOT_DIFF_LINE_DATE,
4863 get_color_value("TOG_COLOR_DATE"), -1);
4864 if (rc == ERR) {
4865 err = got_error(GOT_ERR_RANGE);
4866 goto done;
4870 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4871 view_is_splitscreen(view))
4872 show_log_view(parent_view); /* draw border */
4873 diff_view_indicate_progress(view);
4875 err = create_diff(s);
4877 view->show = show_diff_view;
4878 view->input = input_diff_view;
4879 view->reset = reset_diff_view;
4880 view->close = close_diff_view;
4881 view->search_start = search_start_diff_view;
4882 view->search_next = search_next_diff_view;
4883 done:
4884 if (err)
4885 close_diff_view(view);
4886 return err;
4889 static const struct got_error *
4890 show_diff_view(struct tog_view *view)
4892 const struct got_error *err;
4893 struct tog_diff_view_state *s = &view->state.diff;
4894 char *id_str1 = NULL, *id_str2, *header;
4895 const char *label1, *label2;
4897 if (s->id1) {
4898 err = got_object_id_str(&id_str1, s->id1);
4899 if (err)
4900 return err;
4901 label1 = s->label1 ? s->label1 : id_str1;
4902 } else
4903 label1 = "/dev/null";
4905 err = got_object_id_str(&id_str2, s->id2);
4906 if (err)
4907 return err;
4908 label2 = s->label2 ? s->label2 : id_str2;
4910 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4911 err = got_error_from_errno("asprintf");
4912 free(id_str1);
4913 free(id_str2);
4914 return err;
4916 free(id_str1);
4917 free(id_str2);
4919 err = draw_file(view, header);
4920 free(header);
4921 return err;
4924 static const struct got_error *
4925 set_selected_commit(struct tog_diff_view_state *s,
4926 struct commit_queue_entry *entry)
4928 const struct got_error *err;
4929 const struct got_object_id_queue *parent_ids;
4930 struct got_commit_object *selected_commit;
4931 struct got_object_qid *pid;
4933 free(s->id2);
4934 s->id2 = got_object_id_dup(entry->id);
4935 if (s->id2 == NULL)
4936 return got_error_from_errno("got_object_id_dup");
4938 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4939 if (err)
4940 return err;
4941 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4942 free(s->id1);
4943 pid = STAILQ_FIRST(parent_ids);
4944 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4945 got_object_commit_close(selected_commit);
4946 return NULL;
4949 static const struct got_error *
4950 reset_diff_view(struct tog_view *view)
4952 struct tog_diff_view_state *s = &view->state.diff;
4954 view->count = 0;
4955 wclear(view->window);
4956 s->first_displayed_line = 1;
4957 s->last_displayed_line = view->nlines;
4958 s->matched_line = 0;
4959 diff_view_indicate_progress(view);
4960 return create_diff(s);
4963 static void
4964 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4966 int start, i;
4968 i = start = s->first_displayed_line - 1;
4970 while (s->lines[i].type != type) {
4971 if (i == 0)
4972 i = s->nlines - 1;
4973 if (--i == start)
4974 return; /* do nothing, requested type not in file */
4977 s->selected_line = 1;
4978 s->first_displayed_line = i;
4981 static void
4982 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4984 int start, i;
4986 i = start = s->first_displayed_line + 1;
4988 while (s->lines[i].type != type) {
4989 if (i == s->nlines - 1)
4990 i = 0;
4991 if (++i == start)
4992 return; /* do nothing, requested type not in file */
4995 s->selected_line = 1;
4996 s->first_displayed_line = i;
4999 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5000 int, int, int);
5001 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5002 int, int);
5004 static const struct got_error *
5005 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5007 const struct got_error *err = NULL;
5008 struct tog_diff_view_state *s = &view->state.diff;
5009 struct tog_log_view_state *ls;
5010 struct commit_queue_entry *old_selected_entry;
5011 char *line = NULL;
5012 size_t linesize = 0;
5013 ssize_t linelen;
5014 int i, nscroll = view->nlines - 1, up = 0;
5016 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5018 switch (ch) {
5019 case '0':
5020 view->x = 0;
5021 break;
5022 case '$':
5023 view->x = MAX(view->maxx - view->ncols / 3, 0);
5024 view->count = 0;
5025 break;
5026 case KEY_RIGHT:
5027 case 'l':
5028 if (view->x + view->ncols / 3 < view->maxx)
5029 view->x += 2; /* move two columns right */
5030 else
5031 view->count = 0;
5032 break;
5033 case KEY_LEFT:
5034 case 'h':
5035 view->x -= MIN(view->x, 2); /* move two columns back */
5036 if (view->x <= 0)
5037 view->count = 0;
5038 break;
5039 case 'a':
5040 case 'w':
5041 if (ch == 'a')
5042 s->force_text_diff = !s->force_text_diff;
5043 if (ch == 'w')
5044 s->ignore_whitespace = !s->ignore_whitespace;
5045 err = reset_diff_view(view);
5046 break;
5047 case 'g':
5048 case KEY_HOME:
5049 s->first_displayed_line = 1;
5050 view->count = 0;
5051 break;
5052 case 'G':
5053 case KEY_END:
5054 view->count = 0;
5055 if (s->eof)
5056 break;
5058 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5059 s->eof = 1;
5060 break;
5061 case 'k':
5062 case KEY_UP:
5063 case CTRL('p'):
5064 if (s->first_displayed_line > 1)
5065 s->first_displayed_line--;
5066 else
5067 view->count = 0;
5068 break;
5069 case CTRL('u'):
5070 case 'u':
5071 nscroll /= 2;
5072 /* FALL THROUGH */
5073 case KEY_PPAGE:
5074 case CTRL('b'):
5075 case 'b':
5076 if (s->first_displayed_line == 1) {
5077 view->count = 0;
5078 break;
5080 i = 0;
5081 while (i++ < nscroll && s->first_displayed_line > 1)
5082 s->first_displayed_line--;
5083 break;
5084 case 'j':
5085 case KEY_DOWN:
5086 case CTRL('n'):
5087 if (!s->eof)
5088 s->first_displayed_line++;
5089 else
5090 view->count = 0;
5091 break;
5092 case CTRL('d'):
5093 case 'd':
5094 nscroll /= 2;
5095 /* FALL THROUGH */
5096 case KEY_NPAGE:
5097 case CTRL('f'):
5098 case 'f':
5099 case ' ':
5100 if (s->eof) {
5101 view->count = 0;
5102 break;
5104 i = 0;
5105 while (!s->eof && i++ < nscroll) {
5106 linelen = getline(&line, &linesize, s->f);
5107 s->first_displayed_line++;
5108 if (linelen == -1) {
5109 if (feof(s->f)) {
5110 s->eof = 1;
5111 } else
5112 err = got_ferror(s->f, GOT_ERR_IO);
5113 break;
5116 free(line);
5117 break;
5118 case '(':
5119 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5120 break;
5121 case ')':
5122 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5123 break;
5124 case '{':
5125 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5126 break;
5127 case '}':
5128 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5129 break;
5130 case '[':
5131 if (s->diff_context > 0) {
5132 s->diff_context--;
5133 s->matched_line = 0;
5134 diff_view_indicate_progress(view);
5135 err = create_diff(s);
5136 if (s->first_displayed_line + view->nlines - 1 >
5137 s->nlines) {
5138 s->first_displayed_line = 1;
5139 s->last_displayed_line = view->nlines;
5141 } else
5142 view->count = 0;
5143 break;
5144 case ']':
5145 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5146 s->diff_context++;
5147 s->matched_line = 0;
5148 diff_view_indicate_progress(view);
5149 err = create_diff(s);
5150 } else
5151 view->count = 0;
5152 break;
5153 case '<':
5154 case ',':
5155 case 'K':
5156 up = 1;
5157 /* FALL THROUGH */
5158 case '>':
5159 case '.':
5160 case 'J':
5161 if (s->parent_view == NULL) {
5162 view->count = 0;
5163 break;
5165 s->parent_view->count = view->count;
5167 if (s->parent_view->type == TOG_VIEW_LOG) {
5168 ls = &s->parent_view->state.log;
5169 old_selected_entry = ls->selected_entry;
5171 err = input_log_view(NULL, s->parent_view,
5172 up ? KEY_UP : KEY_DOWN);
5173 if (err)
5174 break;
5175 view->count = s->parent_view->count;
5177 if (old_selected_entry == ls->selected_entry)
5178 break;
5180 err = set_selected_commit(s, ls->selected_entry);
5181 if (err)
5182 break;
5183 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5184 struct tog_blame_view_state *bs;
5185 struct got_object_id *id, *prev_id;
5187 bs = &s->parent_view->state.blame;
5188 prev_id = get_annotation_for_line(bs->blame.lines,
5189 bs->blame.nlines, bs->last_diffed_line);
5191 err = input_blame_view(&view, s->parent_view,
5192 up ? KEY_UP : KEY_DOWN);
5193 if (err)
5194 break;
5195 view->count = s->parent_view->count;
5197 if (prev_id == NULL)
5198 break;
5199 id = get_selected_commit_id(bs->blame.lines,
5200 bs->blame.nlines, bs->first_displayed_line,
5201 bs->selected_line);
5202 if (id == NULL)
5203 break;
5205 if (!got_object_id_cmp(prev_id, id))
5206 break;
5208 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5209 if (err)
5210 break;
5212 s->first_displayed_line = 1;
5213 s->last_displayed_line = view->nlines;
5214 s->matched_line = 0;
5215 view->x = 0;
5217 diff_view_indicate_progress(view);
5218 err = create_diff(s);
5219 break;
5220 default:
5221 view->count = 0;
5222 break;
5225 return err;
5228 static const struct got_error *
5229 cmd_diff(int argc, char *argv[])
5231 const struct got_error *error = NULL;
5232 struct got_repository *repo = NULL;
5233 struct got_worktree *worktree = NULL;
5234 struct got_object_id *id1 = NULL, *id2 = NULL;
5235 char *repo_path = NULL, *cwd = NULL;
5236 char *id_str1 = NULL, *id_str2 = NULL;
5237 char *label1 = NULL, *label2 = NULL;
5238 int diff_context = 3, ignore_whitespace = 0;
5239 int ch, force_text_diff = 0;
5240 const char *errstr;
5241 struct tog_view *view;
5242 int *pack_fds = NULL;
5244 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5245 switch (ch) {
5246 case 'a':
5247 force_text_diff = 1;
5248 break;
5249 case 'C':
5250 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5251 &errstr);
5252 if (errstr != NULL)
5253 errx(1, "number of context lines is %s: %s",
5254 errstr, errstr);
5255 break;
5256 case 'r':
5257 repo_path = realpath(optarg, NULL);
5258 if (repo_path == NULL)
5259 return got_error_from_errno2("realpath",
5260 optarg);
5261 got_path_strip_trailing_slashes(repo_path);
5262 break;
5263 case 'w':
5264 ignore_whitespace = 1;
5265 break;
5266 default:
5267 usage_diff();
5268 /* NOTREACHED */
5272 argc -= optind;
5273 argv += optind;
5275 if (argc == 0) {
5276 usage_diff(); /* TODO show local worktree changes */
5277 } else if (argc == 2) {
5278 id_str1 = argv[0];
5279 id_str2 = argv[1];
5280 } else
5281 usage_diff();
5283 error = got_repo_pack_fds_open(&pack_fds);
5284 if (error)
5285 goto done;
5287 if (repo_path == NULL) {
5288 cwd = getcwd(NULL, 0);
5289 if (cwd == NULL)
5290 return got_error_from_errno("getcwd");
5291 error = got_worktree_open(&worktree, cwd);
5292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5293 goto done;
5294 if (worktree)
5295 repo_path =
5296 strdup(got_worktree_get_repo_path(worktree));
5297 else
5298 repo_path = strdup(cwd);
5299 if (repo_path == NULL) {
5300 error = got_error_from_errno("strdup");
5301 goto done;
5305 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5306 if (error)
5307 goto done;
5309 init_curses();
5311 error = apply_unveil(got_repo_get_path(repo), NULL);
5312 if (error)
5313 goto done;
5315 error = tog_load_refs(repo, 0);
5316 if (error)
5317 goto done;
5319 error = got_repo_match_object_id(&id1, &label1, id_str1,
5320 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5321 if (error)
5322 goto done;
5324 error = got_repo_match_object_id(&id2, &label2, id_str2,
5325 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5326 if (error)
5327 goto done;
5329 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5330 if (view == NULL) {
5331 error = got_error_from_errno("view_open");
5332 goto done;
5334 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5335 ignore_whitespace, force_text_diff, NULL, repo);
5336 if (error)
5337 goto done;
5338 error = view_loop(view);
5339 done:
5340 free(label1);
5341 free(label2);
5342 free(repo_path);
5343 free(cwd);
5344 if (repo) {
5345 const struct got_error *close_err = got_repo_close(repo);
5346 if (error == NULL)
5347 error = close_err;
5349 if (worktree)
5350 got_worktree_close(worktree);
5351 if (pack_fds) {
5352 const struct got_error *pack_err =
5353 got_repo_pack_fds_close(pack_fds);
5354 if (error == NULL)
5355 error = pack_err;
5357 tog_free_refs();
5358 return error;
5361 __dead static void
5362 usage_blame(void)
5364 endwin();
5365 fprintf(stderr,
5366 "usage: %s blame [-c commit] [-r repository-path] path\n",
5367 getprogname());
5368 exit(1);
5371 struct tog_blame_line {
5372 int annotated;
5373 struct got_object_id *id;
5376 static const struct got_error *
5377 draw_blame(struct tog_view *view)
5379 struct tog_blame_view_state *s = &view->state.blame;
5380 struct tog_blame *blame = &s->blame;
5381 regmatch_t *regmatch = &view->regmatch;
5382 const struct got_error *err;
5383 int lineno = 0, nprinted = 0;
5384 char *line = NULL;
5385 size_t linesize = 0;
5386 ssize_t linelen;
5387 wchar_t *wline;
5388 int width;
5389 struct tog_blame_line *blame_line;
5390 struct got_object_id *prev_id = NULL;
5391 char *id_str;
5392 struct tog_color *tc;
5394 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5395 if (err)
5396 return err;
5398 rewind(blame->f);
5399 werase(view->window);
5401 if (asprintf(&line, "commit %s", id_str) == -1) {
5402 err = got_error_from_errno("asprintf");
5403 free(id_str);
5404 return err;
5407 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5408 free(line);
5409 line = NULL;
5410 if (err)
5411 return err;
5412 if (view_needs_focus_indication(view))
5413 wstandout(view->window);
5414 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5415 if (tc)
5416 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5417 waddwstr(view->window, wline);
5418 while (width++ < view->ncols)
5419 waddch(view->window, ' ');
5420 if (tc)
5421 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5422 if (view_needs_focus_indication(view))
5423 wstandend(view->window);
5424 free(wline);
5425 wline = NULL;
5427 if (view->gline > blame->nlines)
5428 view->gline = blame->nlines;
5430 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5431 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5432 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5433 free(id_str);
5434 return got_error_from_errno("asprintf");
5436 free(id_str);
5437 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5438 free(line);
5439 line = NULL;
5440 if (err)
5441 return err;
5442 waddwstr(view->window, wline);
5443 free(wline);
5444 wline = NULL;
5445 if (width < view->ncols - 1)
5446 waddch(view->window, '\n');
5448 s->eof = 0;
5449 view->maxx = 0;
5450 while (nprinted < view->nlines - 2) {
5451 linelen = getline(&line, &linesize, blame->f);
5452 if (linelen == -1) {
5453 if (feof(blame->f)) {
5454 s->eof = 1;
5455 break;
5457 free(line);
5458 return got_ferror(blame->f, GOT_ERR_IO);
5460 if (++lineno < s->first_displayed_line)
5461 continue;
5462 if (view->gline && !gotoline(view, &lineno, &nprinted))
5463 continue;
5465 /* Set view->maxx based on full line length. */
5466 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5467 if (err) {
5468 free(line);
5469 return err;
5471 free(wline);
5472 wline = NULL;
5473 view->maxx = MAX(view->maxx, width);
5475 if (nprinted == s->selected_line - 1)
5476 wstandout(view->window);
5478 if (blame->nlines > 0) {
5479 blame_line = &blame->lines[lineno - 1];
5480 if (blame_line->annotated && prev_id &&
5481 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5482 !(nprinted == s->selected_line - 1)) {
5483 waddstr(view->window, " ");
5484 } else if (blame_line->annotated) {
5485 char *id_str;
5486 err = got_object_id_str(&id_str,
5487 blame_line->id);
5488 if (err) {
5489 free(line);
5490 return err;
5492 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5493 if (tc)
5494 wattr_on(view->window,
5495 COLOR_PAIR(tc->colorpair), NULL);
5496 wprintw(view->window, "%.8s", id_str);
5497 if (tc)
5498 wattr_off(view->window,
5499 COLOR_PAIR(tc->colorpair), NULL);
5500 free(id_str);
5501 prev_id = blame_line->id;
5502 } else {
5503 waddstr(view->window, "........");
5504 prev_id = NULL;
5506 } else {
5507 waddstr(view->window, "........");
5508 prev_id = NULL;
5511 if (nprinted == s->selected_line - 1)
5512 wstandend(view->window);
5513 waddstr(view->window, " ");
5515 if (view->ncols <= 9) {
5516 width = 9;
5517 } else if (s->first_displayed_line + nprinted ==
5518 s->matched_line &&
5519 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5520 err = add_matched_line(&width, line, view->ncols - 9, 9,
5521 view->window, view->x, regmatch);
5522 if (err) {
5523 free(line);
5524 return err;
5526 width += 9;
5527 } else {
5528 int skip;
5529 err = format_line(&wline, &width, &skip, line,
5530 view->x, view->ncols - 9, 9, 1);
5531 if (err) {
5532 free(line);
5533 return err;
5535 waddwstr(view->window, &wline[skip]);
5536 width += 9;
5537 free(wline);
5538 wline = NULL;
5541 if (width <= view->ncols - 1)
5542 waddch(view->window, '\n');
5543 if (++nprinted == 1)
5544 s->first_displayed_line = lineno;
5546 free(line);
5547 s->last_displayed_line = lineno;
5549 view_border(view);
5551 return NULL;
5554 static const struct got_error *
5555 blame_cb(void *arg, int nlines, int lineno,
5556 struct got_commit_object *commit, struct got_object_id *id)
5558 const struct got_error *err = NULL;
5559 struct tog_blame_cb_args *a = arg;
5560 struct tog_blame_line *line;
5561 int errcode;
5563 if (nlines != a->nlines ||
5564 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5565 return got_error(GOT_ERR_RANGE);
5567 errcode = pthread_mutex_lock(&tog_mutex);
5568 if (errcode)
5569 return got_error_set_errno(errcode, "pthread_mutex_lock");
5571 if (*a->quit) { /* user has quit the blame view */
5572 err = got_error(GOT_ERR_ITER_COMPLETED);
5573 goto done;
5576 if (lineno == -1)
5577 goto done; /* no change in this commit */
5579 line = &a->lines[lineno - 1];
5580 if (line->annotated)
5581 goto done;
5583 line->id = got_object_id_dup(id);
5584 if (line->id == NULL) {
5585 err = got_error_from_errno("got_object_id_dup");
5586 goto done;
5588 line->annotated = 1;
5589 done:
5590 errcode = pthread_mutex_unlock(&tog_mutex);
5591 if (errcode)
5592 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5593 return err;
5596 static void *
5597 blame_thread(void *arg)
5599 const struct got_error *err, *close_err;
5600 struct tog_blame_thread_args *ta = arg;
5601 struct tog_blame_cb_args *a = ta->cb_args;
5602 int errcode, fd1 = -1, fd2 = -1;
5603 FILE *f1 = NULL, *f2 = NULL;
5605 fd1 = got_opentempfd();
5606 if (fd1 == -1)
5607 return (void *)got_error_from_errno("got_opentempfd");
5609 fd2 = got_opentempfd();
5610 if (fd2 == -1) {
5611 err = got_error_from_errno("got_opentempfd");
5612 goto done;
5615 f1 = got_opentemp();
5616 if (f1 == NULL) {
5617 err = (void *)got_error_from_errno("got_opentemp");
5618 goto done;
5620 f2 = got_opentemp();
5621 if (f2 == NULL) {
5622 err = (void *)got_error_from_errno("got_opentemp");
5623 goto done;
5626 err = block_signals_used_by_main_thread();
5627 if (err)
5628 goto done;
5630 err = got_blame(ta->path, a->commit_id, ta->repo,
5631 tog_diff_algo, blame_cb, ta->cb_args,
5632 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5633 if (err && err->code == GOT_ERR_CANCELLED)
5634 err = NULL;
5636 errcode = pthread_mutex_lock(&tog_mutex);
5637 if (errcode) {
5638 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5639 goto done;
5642 close_err = got_repo_close(ta->repo);
5643 if (err == NULL)
5644 err = close_err;
5645 ta->repo = NULL;
5646 *ta->complete = 1;
5648 errcode = pthread_mutex_unlock(&tog_mutex);
5649 if (errcode && err == NULL)
5650 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5652 done:
5653 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5654 err = got_error_from_errno("close");
5655 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5656 err = got_error_from_errno("close");
5657 if (f1 && fclose(f1) == EOF && err == NULL)
5658 err = got_error_from_errno("fclose");
5659 if (f2 && fclose(f2) == EOF && err == NULL)
5660 err = got_error_from_errno("fclose");
5662 return (void *)err;
5665 static struct got_object_id *
5666 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5667 int first_displayed_line, int selected_line)
5669 struct tog_blame_line *line;
5671 if (nlines <= 0)
5672 return NULL;
5674 line = &lines[first_displayed_line - 1 + selected_line - 1];
5675 if (!line->annotated)
5676 return NULL;
5678 return line->id;
5681 static struct got_object_id *
5682 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5683 int lineno)
5685 struct tog_blame_line *line;
5687 if (nlines <= 0 || lineno >= nlines)
5688 return NULL;
5690 line = &lines[lineno - 1];
5691 if (!line->annotated)
5692 return NULL;
5694 return line->id;
5697 static const struct got_error *
5698 stop_blame(struct tog_blame *blame)
5700 const struct got_error *err = NULL;
5701 int i;
5703 if (blame->thread) {
5704 int errcode;
5705 errcode = pthread_mutex_unlock(&tog_mutex);
5706 if (errcode)
5707 return got_error_set_errno(errcode,
5708 "pthread_mutex_unlock");
5709 errcode = pthread_join(blame->thread, (void **)&err);
5710 if (errcode)
5711 return got_error_set_errno(errcode, "pthread_join");
5712 errcode = pthread_mutex_lock(&tog_mutex);
5713 if (errcode)
5714 return got_error_set_errno(errcode,
5715 "pthread_mutex_lock");
5716 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5717 err = NULL;
5718 blame->thread = 0; //NULL;
5720 if (blame->thread_args.repo) {
5721 const struct got_error *close_err;
5722 close_err = got_repo_close(blame->thread_args.repo);
5723 if (err == NULL)
5724 err = close_err;
5725 blame->thread_args.repo = NULL;
5727 if (blame->f) {
5728 if (fclose(blame->f) == EOF && err == NULL)
5729 err = got_error_from_errno("fclose");
5730 blame->f = NULL;
5732 if (blame->lines) {
5733 for (i = 0; i < blame->nlines; i++)
5734 free(blame->lines[i].id);
5735 free(blame->lines);
5736 blame->lines = NULL;
5738 free(blame->cb_args.commit_id);
5739 blame->cb_args.commit_id = NULL;
5740 if (blame->pack_fds) {
5741 const struct got_error *pack_err =
5742 got_repo_pack_fds_close(blame->pack_fds);
5743 if (err == NULL)
5744 err = pack_err;
5745 blame->pack_fds = NULL;
5747 return err;
5750 static const struct got_error *
5751 cancel_blame_view(void *arg)
5753 const struct got_error *err = NULL;
5754 int *done = arg;
5755 int errcode;
5757 errcode = pthread_mutex_lock(&tog_mutex);
5758 if (errcode)
5759 return got_error_set_errno(errcode,
5760 "pthread_mutex_unlock");
5762 if (*done)
5763 err = got_error(GOT_ERR_CANCELLED);
5765 errcode = pthread_mutex_unlock(&tog_mutex);
5766 if (errcode)
5767 return got_error_set_errno(errcode,
5768 "pthread_mutex_lock");
5770 return err;
5773 static const struct got_error *
5774 run_blame(struct tog_view *view)
5776 struct tog_blame_view_state *s = &view->state.blame;
5777 struct tog_blame *blame = &s->blame;
5778 const struct got_error *err = NULL;
5779 struct got_commit_object *commit = NULL;
5780 struct got_blob_object *blob = NULL;
5781 struct got_repository *thread_repo = NULL;
5782 struct got_object_id *obj_id = NULL;
5783 int obj_type, fd = -1;
5784 int *pack_fds = NULL;
5786 err = got_object_open_as_commit(&commit, s->repo,
5787 &s->blamed_commit->id);
5788 if (err)
5789 return err;
5791 fd = got_opentempfd();
5792 if (fd == -1) {
5793 err = got_error_from_errno("got_opentempfd");
5794 goto done;
5797 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5798 if (err)
5799 goto done;
5801 err = got_object_get_type(&obj_type, s->repo, obj_id);
5802 if (err)
5803 goto done;
5805 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5806 err = got_error(GOT_ERR_OBJ_TYPE);
5807 goto done;
5810 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5811 if (err)
5812 goto done;
5813 blame->f = got_opentemp();
5814 if (blame->f == NULL) {
5815 err = got_error_from_errno("got_opentemp");
5816 goto done;
5818 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5819 &blame->line_offsets, blame->f, blob);
5820 if (err)
5821 goto done;
5822 if (blame->nlines == 0) {
5823 s->blame_complete = 1;
5824 goto done;
5827 /* Don't include \n at EOF in the blame line count. */
5828 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5829 blame->nlines--;
5831 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5832 if (blame->lines == NULL) {
5833 err = got_error_from_errno("calloc");
5834 goto done;
5837 err = got_repo_pack_fds_open(&pack_fds);
5838 if (err)
5839 goto done;
5840 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5841 pack_fds);
5842 if (err)
5843 goto done;
5845 blame->pack_fds = pack_fds;
5846 blame->cb_args.view = view;
5847 blame->cb_args.lines = blame->lines;
5848 blame->cb_args.nlines = blame->nlines;
5849 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5850 if (blame->cb_args.commit_id == NULL) {
5851 err = got_error_from_errno("got_object_id_dup");
5852 goto done;
5854 blame->cb_args.quit = &s->done;
5856 blame->thread_args.path = s->path;
5857 blame->thread_args.repo = thread_repo;
5858 blame->thread_args.cb_args = &blame->cb_args;
5859 blame->thread_args.complete = &s->blame_complete;
5860 blame->thread_args.cancel_cb = cancel_blame_view;
5861 blame->thread_args.cancel_arg = &s->done;
5862 s->blame_complete = 0;
5864 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5865 s->first_displayed_line = 1;
5866 s->last_displayed_line = view->nlines;
5867 s->selected_line = 1;
5869 s->matched_line = 0;
5871 done:
5872 if (commit)
5873 got_object_commit_close(commit);
5874 if (fd != -1 && close(fd) == -1 && err == NULL)
5875 err = got_error_from_errno("close");
5876 if (blob)
5877 got_object_blob_close(blob);
5878 free(obj_id);
5879 if (err)
5880 stop_blame(blame);
5881 return err;
5884 static const struct got_error *
5885 open_blame_view(struct tog_view *view, char *path,
5886 struct got_object_id *commit_id, struct got_repository *repo)
5888 const struct got_error *err = NULL;
5889 struct tog_blame_view_state *s = &view->state.blame;
5891 STAILQ_INIT(&s->blamed_commits);
5893 s->path = strdup(path);
5894 if (s->path == NULL)
5895 return got_error_from_errno("strdup");
5897 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5898 if (err) {
5899 free(s->path);
5900 return err;
5903 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5904 s->first_displayed_line = 1;
5905 s->last_displayed_line = view->nlines;
5906 s->selected_line = 1;
5907 s->blame_complete = 0;
5908 s->repo = repo;
5909 s->commit_id = commit_id;
5910 memset(&s->blame, 0, sizeof(s->blame));
5912 STAILQ_INIT(&s->colors);
5913 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5914 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5915 get_color_value("TOG_COLOR_COMMIT"));
5916 if (err)
5917 return err;
5920 view->show = show_blame_view;
5921 view->input = input_blame_view;
5922 view->reset = reset_blame_view;
5923 view->close = close_blame_view;
5924 view->search_start = search_start_blame_view;
5925 view->search_next = search_next_blame_view;
5927 return run_blame(view);
5930 static const struct got_error *
5931 close_blame_view(struct tog_view *view)
5933 const struct got_error *err = NULL;
5934 struct tog_blame_view_state *s = &view->state.blame;
5936 if (s->blame.thread)
5937 err = stop_blame(&s->blame);
5939 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5940 struct got_object_qid *blamed_commit;
5941 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5942 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5943 got_object_qid_free(blamed_commit);
5946 free(s->path);
5947 free_colors(&s->colors);
5948 return err;
5951 static const struct got_error *
5952 search_start_blame_view(struct tog_view *view)
5954 struct tog_blame_view_state *s = &view->state.blame;
5956 s->matched_line = 0;
5957 return NULL;
5960 static const struct got_error *
5961 search_next_blame_view(struct tog_view *view)
5963 struct tog_blame_view_state *s = &view->state.blame;
5964 const struct got_error *err = NULL;
5965 int lineno;
5966 char *line = NULL;
5967 size_t linesize = 0;
5968 ssize_t linelen;
5970 if (!view->searching) {
5971 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5972 return NULL;
5975 if (s->matched_line) {
5976 if (view->searching == TOG_SEARCH_FORWARD)
5977 lineno = s->matched_line + 1;
5978 else
5979 lineno = s->matched_line - 1;
5980 } else
5981 lineno = s->first_displayed_line - 1 + s->selected_line;
5983 while (1) {
5984 off_t offset;
5986 if (lineno <= 0 || lineno > s->blame.nlines) {
5987 if (s->matched_line == 0) {
5988 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5989 break;
5992 if (view->searching == TOG_SEARCH_FORWARD)
5993 lineno = 1;
5994 else
5995 lineno = s->blame.nlines;
5998 offset = s->blame.line_offsets[lineno - 1];
5999 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6000 free(line);
6001 return got_error_from_errno("fseeko");
6003 linelen = getline(&line, &linesize, s->blame.f);
6004 if (linelen != -1) {
6005 char *exstr;
6006 err = expand_tab(&exstr, line);
6007 if (err)
6008 break;
6009 if (match_line(exstr, &view->regex, 1,
6010 &view->regmatch)) {
6011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6012 s->matched_line = lineno;
6013 free(exstr);
6014 break;
6016 free(exstr);
6018 if (view->searching == TOG_SEARCH_FORWARD)
6019 lineno++;
6020 else
6021 lineno--;
6023 free(line);
6025 if (s->matched_line) {
6026 s->first_displayed_line = s->matched_line;
6027 s->selected_line = 1;
6030 return err;
6033 static const struct got_error *
6034 show_blame_view(struct tog_view *view)
6036 const struct got_error *err = NULL;
6037 struct tog_blame_view_state *s = &view->state.blame;
6038 int errcode;
6040 if (s->blame.thread == 0 && !s->blame_complete) {
6041 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6042 &s->blame.thread_args);
6043 if (errcode)
6044 return got_error_set_errno(errcode, "pthread_create");
6046 halfdelay(1); /* fast refresh while annotating */
6049 if (s->blame_complete)
6050 halfdelay(10); /* disable fast refresh */
6052 err = draw_blame(view);
6054 view_border(view);
6055 return err;
6058 static const struct got_error *
6059 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6060 struct got_repository *repo, struct got_object_id *id)
6062 struct tog_view *log_view;
6063 const struct got_error *err = NULL;
6065 *new_view = NULL;
6067 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6068 if (log_view == NULL)
6069 return got_error_from_errno("view_open");
6071 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6072 if (err)
6073 view_close(log_view);
6074 else
6075 *new_view = log_view;
6077 return err;
6080 static const struct got_error *
6081 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6083 const struct got_error *err = NULL, *thread_err = NULL;
6084 struct tog_view *diff_view;
6085 struct tog_blame_view_state *s = &view->state.blame;
6086 int eos, nscroll, begin_y = 0, begin_x = 0;
6088 eos = nscroll = view->nlines - 2;
6089 if (view_is_hsplit_top(view))
6090 --eos; /* border */
6092 switch (ch) {
6093 case '0':
6094 view->x = 0;
6095 break;
6096 case '$':
6097 view->x = MAX(view->maxx - view->ncols / 3, 0);
6098 view->count = 0;
6099 break;
6100 case KEY_RIGHT:
6101 case 'l':
6102 if (view->x + view->ncols / 3 < view->maxx)
6103 view->x += 2; /* move two columns right */
6104 else
6105 view->count = 0;
6106 break;
6107 case KEY_LEFT:
6108 case 'h':
6109 view->x -= MIN(view->x, 2); /* move two columns back */
6110 if (view->x <= 0)
6111 view->count = 0;
6112 break;
6113 case 'q':
6114 s->done = 1;
6115 break;
6116 case 'g':
6117 case KEY_HOME:
6118 s->selected_line = 1;
6119 s->first_displayed_line = 1;
6120 view->count = 0;
6121 break;
6122 case 'G':
6123 case KEY_END:
6124 if (s->blame.nlines < eos) {
6125 s->selected_line = s->blame.nlines;
6126 s->first_displayed_line = 1;
6127 } else {
6128 s->selected_line = eos;
6129 s->first_displayed_line = s->blame.nlines - (eos - 1);
6131 view->count = 0;
6132 break;
6133 case 'k':
6134 case KEY_UP:
6135 case CTRL('p'):
6136 if (s->selected_line > 1)
6137 s->selected_line--;
6138 else if (s->selected_line == 1 &&
6139 s->first_displayed_line > 1)
6140 s->first_displayed_line--;
6141 else
6142 view->count = 0;
6143 break;
6144 case CTRL('u'):
6145 case 'u':
6146 nscroll /= 2;
6147 /* FALL THROUGH */
6148 case KEY_PPAGE:
6149 case CTRL('b'):
6150 case 'b':
6151 if (s->first_displayed_line == 1) {
6152 if (view->count > 1)
6153 nscroll += nscroll;
6154 s->selected_line = MAX(1, s->selected_line - nscroll);
6155 view->count = 0;
6156 break;
6158 if (s->first_displayed_line > nscroll)
6159 s->first_displayed_line -= nscroll;
6160 else
6161 s->first_displayed_line = 1;
6162 break;
6163 case 'j':
6164 case KEY_DOWN:
6165 case CTRL('n'):
6166 if (s->selected_line < eos && s->first_displayed_line +
6167 s->selected_line <= s->blame.nlines)
6168 s->selected_line++;
6169 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6170 s->first_displayed_line++;
6171 else
6172 view->count = 0;
6173 break;
6174 case 'c':
6175 case 'p': {
6176 struct got_object_id *id = NULL;
6178 view->count = 0;
6179 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6180 s->first_displayed_line, s->selected_line);
6181 if (id == NULL)
6182 break;
6183 if (ch == 'p') {
6184 struct got_commit_object *commit, *pcommit;
6185 struct got_object_qid *pid;
6186 struct got_object_id *blob_id = NULL;
6187 int obj_type;
6188 err = got_object_open_as_commit(&commit,
6189 s->repo, id);
6190 if (err)
6191 break;
6192 pid = STAILQ_FIRST(
6193 got_object_commit_get_parent_ids(commit));
6194 if (pid == NULL) {
6195 got_object_commit_close(commit);
6196 break;
6198 /* Check if path history ends here. */
6199 err = got_object_open_as_commit(&pcommit,
6200 s->repo, &pid->id);
6201 if (err)
6202 break;
6203 err = got_object_id_by_path(&blob_id, s->repo,
6204 pcommit, s->path);
6205 got_object_commit_close(pcommit);
6206 if (err) {
6207 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6208 err = NULL;
6209 got_object_commit_close(commit);
6210 break;
6212 err = got_object_get_type(&obj_type, s->repo,
6213 blob_id);
6214 free(blob_id);
6215 /* Can't blame non-blob type objects. */
6216 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6217 got_object_commit_close(commit);
6218 break;
6220 err = got_object_qid_alloc(&s->blamed_commit,
6221 &pid->id);
6222 got_object_commit_close(commit);
6223 } else {
6224 if (got_object_id_cmp(id,
6225 &s->blamed_commit->id) == 0)
6226 break;
6227 err = got_object_qid_alloc(&s->blamed_commit,
6228 id);
6230 if (err)
6231 break;
6232 s->done = 1;
6233 thread_err = stop_blame(&s->blame);
6234 s->done = 0;
6235 if (thread_err)
6236 break;
6237 STAILQ_INSERT_HEAD(&s->blamed_commits,
6238 s->blamed_commit, entry);
6239 err = run_blame(view);
6240 if (err)
6241 break;
6242 break;
6244 case 'C': {
6245 struct got_object_qid *first;
6247 view->count = 0;
6248 first = STAILQ_FIRST(&s->blamed_commits);
6249 if (!got_object_id_cmp(&first->id, s->commit_id))
6250 break;
6251 s->done = 1;
6252 thread_err = stop_blame(&s->blame);
6253 s->done = 0;
6254 if (thread_err)
6255 break;
6256 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6257 got_object_qid_free(s->blamed_commit);
6258 s->blamed_commit =
6259 STAILQ_FIRST(&s->blamed_commits);
6260 err = run_blame(view);
6261 if (err)
6262 break;
6263 break;
6265 case 'L':
6266 view->count = 0;
6267 s->id_to_log = get_selected_commit_id(s->blame.lines,
6268 s->blame.nlines, s->first_displayed_line, s->selected_line);
6269 if (s->id_to_log)
6270 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6271 break;
6272 case KEY_ENTER:
6273 case '\r': {
6274 struct got_object_id *id = NULL;
6275 struct got_object_qid *pid;
6276 struct got_commit_object *commit = NULL;
6278 view->count = 0;
6279 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6280 s->first_displayed_line, s->selected_line);
6281 if (id == NULL)
6282 break;
6283 err = got_object_open_as_commit(&commit, s->repo, id);
6284 if (err)
6285 break;
6286 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6287 if (*new_view) {
6288 /* traversed from diff view, release diff resources */
6289 err = close_diff_view(*new_view);
6290 if (err)
6291 break;
6292 diff_view = *new_view;
6293 } else {
6294 if (view_is_parent_view(view))
6295 view_get_split(view, &begin_y, &begin_x);
6297 diff_view = view_open(0, 0, begin_y, begin_x,
6298 TOG_VIEW_DIFF);
6299 if (diff_view == NULL) {
6300 got_object_commit_close(commit);
6301 err = got_error_from_errno("view_open");
6302 break;
6305 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6306 id, NULL, NULL, 3, 0, 0, view, s->repo);
6307 got_object_commit_close(commit);
6308 if (err) {
6309 view_close(diff_view);
6310 break;
6312 s->last_diffed_line = s->first_displayed_line - 1 +
6313 s->selected_line;
6314 if (*new_view)
6315 break; /* still open from active diff view */
6316 if (view_is_parent_view(view) &&
6317 view->mode == TOG_VIEW_SPLIT_HRZN) {
6318 err = view_init_hsplit(view, begin_y);
6319 if (err)
6320 break;
6323 view->focussed = 0;
6324 diff_view->focussed = 1;
6325 diff_view->mode = view->mode;
6326 diff_view->nlines = view->lines - begin_y;
6327 if (view_is_parent_view(view)) {
6328 view_transfer_size(diff_view, view);
6329 err = view_close_child(view);
6330 if (err)
6331 break;
6332 err = view_set_child(view, diff_view);
6333 if (err)
6334 break;
6335 view->focus_child = 1;
6336 } else
6337 *new_view = diff_view;
6338 if (err)
6339 break;
6340 break;
6342 case CTRL('d'):
6343 case 'd':
6344 nscroll /= 2;
6345 /* FALL THROUGH */
6346 case KEY_NPAGE:
6347 case CTRL('f'):
6348 case 'f':
6349 case ' ':
6350 if (s->last_displayed_line >= s->blame.nlines &&
6351 s->selected_line >= MIN(s->blame.nlines,
6352 view->nlines - 2)) {
6353 view->count = 0;
6354 break;
6356 if (s->last_displayed_line >= s->blame.nlines &&
6357 s->selected_line < view->nlines - 2) {
6358 s->selected_line +=
6359 MIN(nscroll, s->last_displayed_line -
6360 s->first_displayed_line - s->selected_line + 1);
6362 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6363 s->first_displayed_line += nscroll;
6364 else
6365 s->first_displayed_line =
6366 s->blame.nlines - (view->nlines - 3);
6367 break;
6368 case KEY_RESIZE:
6369 if (s->selected_line > view->nlines - 2) {
6370 s->selected_line = MIN(s->blame.nlines,
6371 view->nlines - 2);
6373 break;
6374 default:
6375 view->count = 0;
6376 break;
6378 return thread_err ? thread_err : err;
6381 static const struct got_error *
6382 reset_blame_view(struct tog_view *view)
6384 const struct got_error *err;
6385 struct tog_blame_view_state *s = &view->state.blame;
6387 view->count = 0;
6388 s->done = 1;
6389 err = stop_blame(&s->blame);
6390 s->done = 0;
6391 if (err)
6392 return err;
6393 return run_blame(view);
6396 static const struct got_error *
6397 cmd_blame(int argc, char *argv[])
6399 const struct got_error *error;
6400 struct got_repository *repo = NULL;
6401 struct got_worktree *worktree = NULL;
6402 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6403 char *link_target = NULL;
6404 struct got_object_id *commit_id = NULL;
6405 struct got_commit_object *commit = NULL;
6406 char *commit_id_str = NULL;
6407 int ch;
6408 struct tog_view *view;
6409 int *pack_fds = NULL;
6411 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6412 switch (ch) {
6413 case 'c':
6414 commit_id_str = optarg;
6415 break;
6416 case 'r':
6417 repo_path = realpath(optarg, NULL);
6418 if (repo_path == NULL)
6419 return got_error_from_errno2("realpath",
6420 optarg);
6421 break;
6422 default:
6423 usage_blame();
6424 /* NOTREACHED */
6428 argc -= optind;
6429 argv += optind;
6431 if (argc != 1)
6432 usage_blame();
6434 error = got_repo_pack_fds_open(&pack_fds);
6435 if (error != NULL)
6436 goto done;
6438 if (repo_path == NULL) {
6439 cwd = getcwd(NULL, 0);
6440 if (cwd == NULL)
6441 return got_error_from_errno("getcwd");
6442 error = got_worktree_open(&worktree, cwd);
6443 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6444 goto done;
6445 if (worktree)
6446 repo_path =
6447 strdup(got_worktree_get_repo_path(worktree));
6448 else
6449 repo_path = strdup(cwd);
6450 if (repo_path == NULL) {
6451 error = got_error_from_errno("strdup");
6452 goto done;
6456 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6457 if (error != NULL)
6458 goto done;
6460 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6461 worktree);
6462 if (error)
6463 goto done;
6465 init_curses();
6467 error = apply_unveil(got_repo_get_path(repo), NULL);
6468 if (error)
6469 goto done;
6471 error = tog_load_refs(repo, 0);
6472 if (error)
6473 goto done;
6475 if (commit_id_str == NULL) {
6476 struct got_reference *head_ref;
6477 error = got_ref_open(&head_ref, repo, worktree ?
6478 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6479 if (error != NULL)
6480 goto done;
6481 error = got_ref_resolve(&commit_id, repo, head_ref);
6482 got_ref_close(head_ref);
6483 } else {
6484 error = got_repo_match_object_id(&commit_id, NULL,
6485 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6487 if (error != NULL)
6488 goto done;
6490 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6491 if (view == NULL) {
6492 error = got_error_from_errno("view_open");
6493 goto done;
6496 error = got_object_open_as_commit(&commit, repo, commit_id);
6497 if (error)
6498 goto done;
6500 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6501 commit, repo);
6502 if (error)
6503 goto done;
6505 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6506 commit_id, repo);
6507 if (error)
6508 goto done;
6509 if (worktree) {
6510 /* Release work tree lock. */
6511 got_worktree_close(worktree);
6512 worktree = NULL;
6514 error = view_loop(view);
6515 done:
6516 free(repo_path);
6517 free(in_repo_path);
6518 free(link_target);
6519 free(cwd);
6520 free(commit_id);
6521 if (commit)
6522 got_object_commit_close(commit);
6523 if (worktree)
6524 got_worktree_close(worktree);
6525 if (repo) {
6526 const struct got_error *close_err = got_repo_close(repo);
6527 if (error == NULL)
6528 error = close_err;
6530 if (pack_fds) {
6531 const struct got_error *pack_err =
6532 got_repo_pack_fds_close(pack_fds);
6533 if (error == NULL)
6534 error = pack_err;
6536 tog_free_refs();
6537 return error;
6540 static const struct got_error *
6541 draw_tree_entries(struct tog_view *view, const char *parent_path)
6543 struct tog_tree_view_state *s = &view->state.tree;
6544 const struct got_error *err = NULL;
6545 struct got_tree_entry *te;
6546 wchar_t *wline;
6547 char *index = NULL;
6548 struct tog_color *tc;
6549 int width, n, nentries, i = 1;
6550 int limit = view->nlines;
6552 s->ndisplayed = 0;
6553 if (view_is_hsplit_top(view))
6554 --limit; /* border */
6556 werase(view->window);
6558 if (limit == 0)
6559 return NULL;
6561 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6562 0, 0);
6563 if (err)
6564 return err;
6565 if (view_needs_focus_indication(view))
6566 wstandout(view->window);
6567 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6568 if (tc)
6569 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6570 waddwstr(view->window, wline);
6571 free(wline);
6572 wline = NULL;
6573 while (width++ < view->ncols)
6574 waddch(view->window, ' ');
6575 if (tc)
6576 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6577 if (view_needs_focus_indication(view))
6578 wstandend(view->window);
6579 if (--limit <= 0)
6580 return NULL;
6582 i += s->selected;
6583 if (s->first_displayed_entry) {
6584 i += got_tree_entry_get_index(s->first_displayed_entry);
6585 if (s->tree != s->root)
6586 ++i; /* account for ".." entry */
6588 nentries = got_object_tree_get_nentries(s->tree);
6589 if (asprintf(&index, "[%d/%d] %s",
6590 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6591 return got_error_from_errno("asprintf");
6592 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6593 free(index);
6594 if (err)
6595 return err;
6596 waddwstr(view->window, wline);
6597 free(wline);
6598 wline = NULL;
6599 if (width < view->ncols - 1)
6600 waddch(view->window, '\n');
6601 if (--limit <= 0)
6602 return NULL;
6603 waddch(view->window, '\n');
6604 if (--limit <= 0)
6605 return NULL;
6607 if (s->first_displayed_entry == NULL) {
6608 te = got_object_tree_get_first_entry(s->tree);
6609 if (s->selected == 0) {
6610 if (view->focussed)
6611 wstandout(view->window);
6612 s->selected_entry = NULL;
6614 waddstr(view->window, " ..\n"); /* parent directory */
6615 if (s->selected == 0 && view->focussed)
6616 wstandend(view->window);
6617 s->ndisplayed++;
6618 if (--limit <= 0)
6619 return NULL;
6620 n = 1;
6621 } else {
6622 n = 0;
6623 te = s->first_displayed_entry;
6626 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6627 char *line = NULL, *id_str = NULL, *link_target = NULL;
6628 const char *modestr = "";
6629 mode_t mode;
6631 te = got_object_tree_get_entry(s->tree, i);
6632 mode = got_tree_entry_get_mode(te);
6634 if (s->show_ids) {
6635 err = got_object_id_str(&id_str,
6636 got_tree_entry_get_id(te));
6637 if (err)
6638 return got_error_from_errno(
6639 "got_object_id_str");
6641 if (got_object_tree_entry_is_submodule(te))
6642 modestr = "$";
6643 else if (S_ISLNK(mode)) {
6644 int i;
6646 err = got_tree_entry_get_symlink_target(&link_target,
6647 te, s->repo);
6648 if (err) {
6649 free(id_str);
6650 return err;
6652 for (i = 0; i < strlen(link_target); i++) {
6653 if (!isprint((unsigned char)link_target[i]))
6654 link_target[i] = '?';
6656 modestr = "@";
6658 else if (S_ISDIR(mode))
6659 modestr = "/";
6660 else if (mode & S_IXUSR)
6661 modestr = "*";
6662 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6663 got_tree_entry_get_name(te), modestr,
6664 link_target ? " -> ": "",
6665 link_target ? link_target : "") == -1) {
6666 free(id_str);
6667 free(link_target);
6668 return got_error_from_errno("asprintf");
6670 free(id_str);
6671 free(link_target);
6672 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6673 0, 0);
6674 if (err) {
6675 free(line);
6676 break;
6678 if (n == s->selected) {
6679 if (view->focussed)
6680 wstandout(view->window);
6681 s->selected_entry = te;
6683 tc = match_color(&s->colors, line);
6684 if (tc)
6685 wattr_on(view->window,
6686 COLOR_PAIR(tc->colorpair), NULL);
6687 waddwstr(view->window, wline);
6688 if (tc)
6689 wattr_off(view->window,
6690 COLOR_PAIR(tc->colorpair), NULL);
6691 if (width < view->ncols - 1)
6692 waddch(view->window, '\n');
6693 if (n == s->selected && view->focussed)
6694 wstandend(view->window);
6695 free(line);
6696 free(wline);
6697 wline = NULL;
6698 n++;
6699 s->ndisplayed++;
6700 s->last_displayed_entry = te;
6701 if (--limit <= 0)
6702 break;
6705 return err;
6708 static void
6709 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6711 struct got_tree_entry *te;
6712 int isroot = s->tree == s->root;
6713 int i = 0;
6715 if (s->first_displayed_entry == NULL)
6716 return;
6718 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6719 while (i++ < maxscroll) {
6720 if (te == NULL) {
6721 if (!isroot)
6722 s->first_displayed_entry = NULL;
6723 break;
6725 s->first_displayed_entry = te;
6726 te = got_tree_entry_get_prev(s->tree, te);
6730 static const struct got_error *
6731 tree_scroll_down(struct tog_view *view, int maxscroll)
6733 struct tog_tree_view_state *s = &view->state.tree;
6734 struct got_tree_entry *next, *last;
6735 int n = 0;
6737 if (s->first_displayed_entry)
6738 next = got_tree_entry_get_next(s->tree,
6739 s->first_displayed_entry);
6740 else
6741 next = got_object_tree_get_first_entry(s->tree);
6743 last = s->last_displayed_entry;
6744 while (next && n++ < maxscroll) {
6745 if (last) {
6746 s->last_displayed_entry = last;
6747 last = got_tree_entry_get_next(s->tree, last);
6749 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6750 s->first_displayed_entry = next;
6751 next = got_tree_entry_get_next(s->tree, next);
6755 return NULL;
6758 static const struct got_error *
6759 tree_entry_path(char **path, struct tog_parent_trees *parents,
6760 struct got_tree_entry *te)
6762 const struct got_error *err = NULL;
6763 struct tog_parent_tree *pt;
6764 size_t len = 2; /* for leading slash and NUL */
6766 TAILQ_FOREACH(pt, parents, entry)
6767 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6768 + 1 /* slash */;
6769 if (te)
6770 len += strlen(got_tree_entry_get_name(te));
6772 *path = calloc(1, len);
6773 if (path == NULL)
6774 return got_error_from_errno("calloc");
6776 (*path)[0] = '/';
6777 pt = TAILQ_LAST(parents, tog_parent_trees);
6778 while (pt) {
6779 const char *name = got_tree_entry_get_name(pt->selected_entry);
6780 if (strlcat(*path, name, len) >= len) {
6781 err = got_error(GOT_ERR_NO_SPACE);
6782 goto done;
6784 if (strlcat(*path, "/", len) >= len) {
6785 err = got_error(GOT_ERR_NO_SPACE);
6786 goto done;
6788 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6790 if (te) {
6791 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6792 err = got_error(GOT_ERR_NO_SPACE);
6793 goto done;
6796 done:
6797 if (err) {
6798 free(*path);
6799 *path = NULL;
6801 return err;
6804 static const struct got_error *
6805 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6806 struct got_tree_entry *te, struct tog_parent_trees *parents,
6807 struct got_object_id *commit_id, struct got_repository *repo)
6809 const struct got_error *err = NULL;
6810 char *path;
6811 struct tog_view *blame_view;
6813 *new_view = NULL;
6815 err = tree_entry_path(&path, parents, te);
6816 if (err)
6817 return err;
6819 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6820 if (blame_view == NULL) {
6821 err = got_error_from_errno("view_open");
6822 goto done;
6825 err = open_blame_view(blame_view, path, commit_id, repo);
6826 if (err) {
6827 if (err->code == GOT_ERR_CANCELLED)
6828 err = NULL;
6829 view_close(blame_view);
6830 } else
6831 *new_view = blame_view;
6832 done:
6833 free(path);
6834 return err;
6837 static const struct got_error *
6838 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6839 struct tog_tree_view_state *s)
6841 struct tog_view *log_view;
6842 const struct got_error *err = NULL;
6843 char *path;
6845 *new_view = NULL;
6847 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6848 if (log_view == NULL)
6849 return got_error_from_errno("view_open");
6851 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6852 if (err)
6853 return err;
6855 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6856 path, 0);
6857 if (err)
6858 view_close(log_view);
6859 else
6860 *new_view = log_view;
6861 free(path);
6862 return err;
6865 static const struct got_error *
6866 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6867 const char *head_ref_name, struct got_repository *repo)
6869 const struct got_error *err = NULL;
6870 char *commit_id_str = NULL;
6871 struct tog_tree_view_state *s = &view->state.tree;
6872 struct got_commit_object *commit = NULL;
6874 TAILQ_INIT(&s->parents);
6875 STAILQ_INIT(&s->colors);
6877 s->commit_id = got_object_id_dup(commit_id);
6878 if (s->commit_id == NULL)
6879 return got_error_from_errno("got_object_id_dup");
6881 err = got_object_open_as_commit(&commit, repo, commit_id);
6882 if (err)
6883 goto done;
6886 * The root is opened here and will be closed when the view is closed.
6887 * Any visited subtrees and their path-wise parents are opened and
6888 * closed on demand.
6890 err = got_object_open_as_tree(&s->root, repo,
6891 got_object_commit_get_tree_id(commit));
6892 if (err)
6893 goto done;
6894 s->tree = s->root;
6896 err = got_object_id_str(&commit_id_str, commit_id);
6897 if (err != NULL)
6898 goto done;
6900 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6901 err = got_error_from_errno("asprintf");
6902 goto done;
6905 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6906 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6907 if (head_ref_name) {
6908 s->head_ref_name = strdup(head_ref_name);
6909 if (s->head_ref_name == NULL) {
6910 err = got_error_from_errno("strdup");
6911 goto done;
6914 s->repo = repo;
6916 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6917 err = add_color(&s->colors, "\\$$",
6918 TOG_COLOR_TREE_SUBMODULE,
6919 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6920 if (err)
6921 goto done;
6922 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6923 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6924 if (err)
6925 goto done;
6926 err = add_color(&s->colors, "/$",
6927 TOG_COLOR_TREE_DIRECTORY,
6928 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6929 if (err)
6930 goto done;
6932 err = add_color(&s->colors, "\\*$",
6933 TOG_COLOR_TREE_EXECUTABLE,
6934 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6935 if (err)
6936 goto done;
6938 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6939 get_color_value("TOG_COLOR_COMMIT"));
6940 if (err)
6941 goto done;
6944 view->show = show_tree_view;
6945 view->input = input_tree_view;
6946 view->close = close_tree_view;
6947 view->search_start = search_start_tree_view;
6948 view->search_next = search_next_tree_view;
6949 done:
6950 free(commit_id_str);
6951 if (commit)
6952 got_object_commit_close(commit);
6953 if (err)
6954 close_tree_view(view);
6955 return err;
6958 static const struct got_error *
6959 close_tree_view(struct tog_view *view)
6961 struct tog_tree_view_state *s = &view->state.tree;
6963 free_colors(&s->colors);
6964 free(s->tree_label);
6965 s->tree_label = NULL;
6966 free(s->commit_id);
6967 s->commit_id = NULL;
6968 free(s->head_ref_name);
6969 s->head_ref_name = NULL;
6970 while (!TAILQ_EMPTY(&s->parents)) {
6971 struct tog_parent_tree *parent;
6972 parent = TAILQ_FIRST(&s->parents);
6973 TAILQ_REMOVE(&s->parents, parent, entry);
6974 if (parent->tree != s->root)
6975 got_object_tree_close(parent->tree);
6976 free(parent);
6979 if (s->tree != NULL && s->tree != s->root)
6980 got_object_tree_close(s->tree);
6981 if (s->root)
6982 got_object_tree_close(s->root);
6983 return NULL;
6986 static const struct got_error *
6987 search_start_tree_view(struct tog_view *view)
6989 struct tog_tree_view_state *s = &view->state.tree;
6991 s->matched_entry = NULL;
6992 return NULL;
6995 static int
6996 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6998 regmatch_t regmatch;
7000 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7001 0) == 0;
7004 static const struct got_error *
7005 search_next_tree_view(struct tog_view *view)
7007 struct tog_tree_view_state *s = &view->state.tree;
7008 struct got_tree_entry *te = NULL;
7010 if (!view->searching) {
7011 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7012 return NULL;
7015 if (s->matched_entry) {
7016 if (view->searching == TOG_SEARCH_FORWARD) {
7017 if (s->selected_entry)
7018 te = got_tree_entry_get_next(s->tree,
7019 s->selected_entry);
7020 else
7021 te = got_object_tree_get_first_entry(s->tree);
7022 } else {
7023 if (s->selected_entry == NULL)
7024 te = got_object_tree_get_last_entry(s->tree);
7025 else
7026 te = got_tree_entry_get_prev(s->tree,
7027 s->selected_entry);
7029 } else {
7030 if (s->selected_entry)
7031 te = s->selected_entry;
7032 else if (view->searching == TOG_SEARCH_FORWARD)
7033 te = got_object_tree_get_first_entry(s->tree);
7034 else
7035 te = got_object_tree_get_last_entry(s->tree);
7038 while (1) {
7039 if (te == NULL) {
7040 if (s->matched_entry == NULL) {
7041 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7042 return NULL;
7044 if (view->searching == TOG_SEARCH_FORWARD)
7045 te = got_object_tree_get_first_entry(s->tree);
7046 else
7047 te = got_object_tree_get_last_entry(s->tree);
7050 if (match_tree_entry(te, &view->regex)) {
7051 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7052 s->matched_entry = te;
7053 break;
7056 if (view->searching == TOG_SEARCH_FORWARD)
7057 te = got_tree_entry_get_next(s->tree, te);
7058 else
7059 te = got_tree_entry_get_prev(s->tree, te);
7062 if (s->matched_entry) {
7063 s->first_displayed_entry = s->matched_entry;
7064 s->selected = 0;
7067 return NULL;
7070 static const struct got_error *
7071 show_tree_view(struct tog_view *view)
7073 const struct got_error *err = NULL;
7074 struct tog_tree_view_state *s = &view->state.tree;
7075 char *parent_path;
7077 err = tree_entry_path(&parent_path, &s->parents, NULL);
7078 if (err)
7079 return err;
7081 err = draw_tree_entries(view, parent_path);
7082 free(parent_path);
7084 view_border(view);
7085 return err;
7088 static const struct got_error *
7089 tree_goto_line(struct tog_view *view, int nlines)
7091 const struct got_error *err = NULL;
7092 struct tog_tree_view_state *s = &view->state.tree;
7093 struct got_tree_entry **fte, **lte, **ste;
7094 int g, last, first = 1, i = 1;
7095 int root = s->tree == s->root;
7096 int off = root ? 1 : 2;
7098 g = view->gline;
7099 view->gline = 0;
7101 if (g == 0)
7102 g = 1;
7103 else if (g > got_object_tree_get_nentries(s->tree))
7104 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7106 fte = &s->first_displayed_entry;
7107 lte = &s->last_displayed_entry;
7108 ste = &s->selected_entry;
7110 if (*fte != NULL) {
7111 first = got_tree_entry_get_index(*fte);
7112 first += off; /* account for ".." */
7114 last = got_tree_entry_get_index(*lte);
7115 last += off;
7117 if (g >= first && g <= last && g - first < nlines) {
7118 s->selected = g - first;
7119 return NULL; /* gline is on the current page */
7122 if (*ste != NULL) {
7123 i = got_tree_entry_get_index(*ste);
7124 i += off;
7127 if (i < g) {
7128 err = tree_scroll_down(view, g - i);
7129 if (err)
7130 return err;
7131 if (got_tree_entry_get_index(*lte) >=
7132 got_object_tree_get_nentries(s->tree) - 1 &&
7133 first + s->selected < g &&
7134 s->selected < s->ndisplayed - 1) {
7135 first = got_tree_entry_get_index(*fte);
7136 first += off;
7137 s->selected = g - first;
7139 } else if (i > g)
7140 tree_scroll_up(s, i - g);
7142 if (g < nlines &&
7143 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7144 s->selected = g - 1;
7146 return NULL;
7149 static const struct got_error *
7150 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7152 const struct got_error *err = NULL;
7153 struct tog_tree_view_state *s = &view->state.tree;
7154 struct got_tree_entry *te;
7155 int n, nscroll = view->nlines - 3;
7157 if (view->gline)
7158 return tree_goto_line(view, nscroll);
7160 switch (ch) {
7161 case 'i':
7162 s->show_ids = !s->show_ids;
7163 view->count = 0;
7164 break;
7165 case 'L':
7166 view->count = 0;
7167 if (!s->selected_entry)
7168 break;
7169 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7170 break;
7171 case 'R':
7172 view->count = 0;
7173 err = view_request_new(new_view, view, TOG_VIEW_REF);
7174 break;
7175 case 'g':
7176 case KEY_HOME:
7177 s->selected = 0;
7178 view->count = 0;
7179 if (s->tree == s->root)
7180 s->first_displayed_entry =
7181 got_object_tree_get_first_entry(s->tree);
7182 else
7183 s->first_displayed_entry = NULL;
7184 break;
7185 case 'G':
7186 case KEY_END: {
7187 int eos = view->nlines - 3;
7189 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7190 --eos; /* border */
7191 s->selected = 0;
7192 view->count = 0;
7193 te = got_object_tree_get_last_entry(s->tree);
7194 for (n = 0; n < eos; n++) {
7195 if (te == NULL) {
7196 if (s->tree != s->root) {
7197 s->first_displayed_entry = NULL;
7198 n++;
7200 break;
7202 s->first_displayed_entry = te;
7203 te = got_tree_entry_get_prev(s->tree, te);
7205 if (n > 0)
7206 s->selected = n - 1;
7207 break;
7209 case 'k':
7210 case KEY_UP:
7211 case CTRL('p'):
7212 if (s->selected > 0) {
7213 s->selected--;
7214 break;
7216 tree_scroll_up(s, 1);
7217 if (s->selected_entry == NULL ||
7218 (s->tree == s->root && s->selected_entry ==
7219 got_object_tree_get_first_entry(s->tree)))
7220 view->count = 0;
7221 break;
7222 case CTRL('u'):
7223 case 'u':
7224 nscroll /= 2;
7225 /* FALL THROUGH */
7226 case KEY_PPAGE:
7227 case CTRL('b'):
7228 case 'b':
7229 if (s->tree == s->root) {
7230 if (got_object_tree_get_first_entry(s->tree) ==
7231 s->first_displayed_entry)
7232 s->selected -= MIN(s->selected, nscroll);
7233 } else {
7234 if (s->first_displayed_entry == NULL)
7235 s->selected -= MIN(s->selected, nscroll);
7237 tree_scroll_up(s, MAX(0, nscroll));
7238 if (s->selected_entry == NULL ||
7239 (s->tree == s->root && s->selected_entry ==
7240 got_object_tree_get_first_entry(s->tree)))
7241 view->count = 0;
7242 break;
7243 case 'j':
7244 case KEY_DOWN:
7245 case CTRL('n'):
7246 if (s->selected < s->ndisplayed - 1) {
7247 s->selected++;
7248 break;
7250 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7251 == NULL) {
7252 /* can't scroll any further */
7253 view->count = 0;
7254 break;
7256 tree_scroll_down(view, 1);
7257 break;
7258 case CTRL('d'):
7259 case 'd':
7260 nscroll /= 2;
7261 /* FALL THROUGH */
7262 case KEY_NPAGE:
7263 case CTRL('f'):
7264 case 'f':
7265 case ' ':
7266 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7267 == NULL) {
7268 /* can't scroll any further; move cursor down */
7269 if (s->selected < s->ndisplayed - 1)
7270 s->selected += MIN(nscroll,
7271 s->ndisplayed - s->selected - 1);
7272 else
7273 view->count = 0;
7274 break;
7276 tree_scroll_down(view, nscroll);
7277 break;
7278 case KEY_ENTER:
7279 case '\r':
7280 case KEY_BACKSPACE:
7281 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7282 struct tog_parent_tree *parent;
7283 /* user selected '..' */
7284 if (s->tree == s->root) {
7285 view->count = 0;
7286 break;
7288 parent = TAILQ_FIRST(&s->parents);
7289 TAILQ_REMOVE(&s->parents, parent,
7290 entry);
7291 got_object_tree_close(s->tree);
7292 s->tree = parent->tree;
7293 s->first_displayed_entry =
7294 parent->first_displayed_entry;
7295 s->selected_entry =
7296 parent->selected_entry;
7297 s->selected = parent->selected;
7298 if (s->selected > view->nlines - 3) {
7299 err = offset_selection_down(view);
7300 if (err)
7301 break;
7303 free(parent);
7304 } else if (S_ISDIR(got_tree_entry_get_mode(
7305 s->selected_entry))) {
7306 struct got_tree_object *subtree;
7307 view->count = 0;
7308 err = got_object_open_as_tree(&subtree, s->repo,
7309 got_tree_entry_get_id(s->selected_entry));
7310 if (err)
7311 break;
7312 err = tree_view_visit_subtree(s, subtree);
7313 if (err) {
7314 got_object_tree_close(subtree);
7315 break;
7317 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7318 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7319 break;
7320 case KEY_RESIZE:
7321 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7322 s->selected = view->nlines - 4;
7323 view->count = 0;
7324 break;
7325 default:
7326 view->count = 0;
7327 break;
7330 return err;
7333 __dead static void
7334 usage_tree(void)
7336 endwin();
7337 fprintf(stderr,
7338 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7339 getprogname());
7340 exit(1);
7343 static const struct got_error *
7344 cmd_tree(int argc, char *argv[])
7346 const struct got_error *error;
7347 struct got_repository *repo = NULL;
7348 struct got_worktree *worktree = NULL;
7349 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7350 struct got_object_id *commit_id = NULL;
7351 struct got_commit_object *commit = NULL;
7352 const char *commit_id_arg = NULL;
7353 char *label = NULL;
7354 struct got_reference *ref = NULL;
7355 const char *head_ref_name = NULL;
7356 int ch;
7357 struct tog_view *view;
7358 int *pack_fds = NULL;
7360 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7361 switch (ch) {
7362 case 'c':
7363 commit_id_arg = optarg;
7364 break;
7365 case 'r':
7366 repo_path = realpath(optarg, NULL);
7367 if (repo_path == NULL)
7368 return got_error_from_errno2("realpath",
7369 optarg);
7370 break;
7371 default:
7372 usage_tree();
7373 /* NOTREACHED */
7377 argc -= optind;
7378 argv += optind;
7380 if (argc > 1)
7381 usage_tree();
7383 error = got_repo_pack_fds_open(&pack_fds);
7384 if (error != NULL)
7385 goto done;
7387 if (repo_path == NULL) {
7388 cwd = getcwd(NULL, 0);
7389 if (cwd == NULL)
7390 return got_error_from_errno("getcwd");
7391 error = got_worktree_open(&worktree, cwd);
7392 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7393 goto done;
7394 if (worktree)
7395 repo_path =
7396 strdup(got_worktree_get_repo_path(worktree));
7397 else
7398 repo_path = strdup(cwd);
7399 if (repo_path == NULL) {
7400 error = got_error_from_errno("strdup");
7401 goto done;
7405 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7406 if (error != NULL)
7407 goto done;
7409 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7410 repo, worktree);
7411 if (error)
7412 goto done;
7414 init_curses();
7416 error = apply_unveil(got_repo_get_path(repo), NULL);
7417 if (error)
7418 goto done;
7420 error = tog_load_refs(repo, 0);
7421 if (error)
7422 goto done;
7424 if (commit_id_arg == NULL) {
7425 error = got_repo_match_object_id(&commit_id, &label,
7426 worktree ? got_worktree_get_head_ref_name(worktree) :
7427 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7428 if (error)
7429 goto done;
7430 head_ref_name = label;
7431 } else {
7432 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7433 if (error == NULL)
7434 head_ref_name = got_ref_get_name(ref);
7435 else if (error->code != GOT_ERR_NOT_REF)
7436 goto done;
7437 error = got_repo_match_object_id(&commit_id, NULL,
7438 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7439 if (error)
7440 goto done;
7443 error = got_object_open_as_commit(&commit, repo, commit_id);
7444 if (error)
7445 goto done;
7447 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7448 if (view == NULL) {
7449 error = got_error_from_errno("view_open");
7450 goto done;
7452 error = open_tree_view(view, commit_id, head_ref_name, repo);
7453 if (error)
7454 goto done;
7455 if (!got_path_is_root_dir(in_repo_path)) {
7456 error = tree_view_walk_path(&view->state.tree, commit,
7457 in_repo_path);
7458 if (error)
7459 goto done;
7462 if (worktree) {
7463 /* Release work tree lock. */
7464 got_worktree_close(worktree);
7465 worktree = NULL;
7467 error = view_loop(view);
7468 done:
7469 free(repo_path);
7470 free(cwd);
7471 free(commit_id);
7472 free(label);
7473 if (ref)
7474 got_ref_close(ref);
7475 if (repo) {
7476 const struct got_error *close_err = got_repo_close(repo);
7477 if (error == NULL)
7478 error = close_err;
7480 if (pack_fds) {
7481 const struct got_error *pack_err =
7482 got_repo_pack_fds_close(pack_fds);
7483 if (error == NULL)
7484 error = pack_err;
7486 tog_free_refs();
7487 return error;
7490 static const struct got_error *
7491 ref_view_load_refs(struct tog_ref_view_state *s)
7493 struct got_reflist_entry *sre;
7494 struct tog_reflist_entry *re;
7496 s->nrefs = 0;
7497 TAILQ_FOREACH(sre, &tog_refs, entry) {
7498 if (strncmp(got_ref_get_name(sre->ref),
7499 "refs/got/", 9) == 0 &&
7500 strncmp(got_ref_get_name(sre->ref),
7501 "refs/got/backup/", 16) != 0)
7502 continue;
7504 re = malloc(sizeof(*re));
7505 if (re == NULL)
7506 return got_error_from_errno("malloc");
7508 re->ref = got_ref_dup(sre->ref);
7509 if (re->ref == NULL)
7510 return got_error_from_errno("got_ref_dup");
7511 re->idx = s->nrefs++;
7512 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7515 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7516 return NULL;
7519 static void
7520 ref_view_free_refs(struct tog_ref_view_state *s)
7522 struct tog_reflist_entry *re;
7524 while (!TAILQ_EMPTY(&s->refs)) {
7525 re = TAILQ_FIRST(&s->refs);
7526 TAILQ_REMOVE(&s->refs, re, entry);
7527 got_ref_close(re->ref);
7528 free(re);
7532 static const struct got_error *
7533 open_ref_view(struct tog_view *view, struct got_repository *repo)
7535 const struct got_error *err = NULL;
7536 struct tog_ref_view_state *s = &view->state.ref;
7538 s->selected_entry = 0;
7539 s->repo = repo;
7541 TAILQ_INIT(&s->refs);
7542 STAILQ_INIT(&s->colors);
7544 err = ref_view_load_refs(s);
7545 if (err)
7546 return err;
7548 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7549 err = add_color(&s->colors, "^refs/heads/",
7550 TOG_COLOR_REFS_HEADS,
7551 get_color_value("TOG_COLOR_REFS_HEADS"));
7552 if (err)
7553 goto done;
7555 err = add_color(&s->colors, "^refs/tags/",
7556 TOG_COLOR_REFS_TAGS,
7557 get_color_value("TOG_COLOR_REFS_TAGS"));
7558 if (err)
7559 goto done;
7561 err = add_color(&s->colors, "^refs/remotes/",
7562 TOG_COLOR_REFS_REMOTES,
7563 get_color_value("TOG_COLOR_REFS_REMOTES"));
7564 if (err)
7565 goto done;
7567 err = add_color(&s->colors, "^refs/got/backup/",
7568 TOG_COLOR_REFS_BACKUP,
7569 get_color_value("TOG_COLOR_REFS_BACKUP"));
7570 if (err)
7571 goto done;
7574 view->show = show_ref_view;
7575 view->input = input_ref_view;
7576 view->close = close_ref_view;
7577 view->search_start = search_start_ref_view;
7578 view->search_next = search_next_ref_view;
7579 done:
7580 if (err)
7581 free_colors(&s->colors);
7582 return err;
7585 static const struct got_error *
7586 close_ref_view(struct tog_view *view)
7588 struct tog_ref_view_state *s = &view->state.ref;
7590 ref_view_free_refs(s);
7591 free_colors(&s->colors);
7593 return NULL;
7596 static const struct got_error *
7597 resolve_reflist_entry(struct got_object_id **commit_id,
7598 struct tog_reflist_entry *re, struct got_repository *repo)
7600 const struct got_error *err = NULL;
7601 struct got_object_id *obj_id;
7602 struct got_tag_object *tag = NULL;
7603 int obj_type;
7605 *commit_id = NULL;
7607 err = got_ref_resolve(&obj_id, repo, re->ref);
7608 if (err)
7609 return err;
7611 err = got_object_get_type(&obj_type, repo, obj_id);
7612 if (err)
7613 goto done;
7615 switch (obj_type) {
7616 case GOT_OBJ_TYPE_COMMIT:
7617 *commit_id = obj_id;
7618 break;
7619 case GOT_OBJ_TYPE_TAG:
7620 err = got_object_open_as_tag(&tag, repo, obj_id);
7621 if (err)
7622 goto done;
7623 free(obj_id);
7624 err = got_object_get_type(&obj_type, repo,
7625 got_object_tag_get_object_id(tag));
7626 if (err)
7627 goto done;
7628 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7629 err = got_error(GOT_ERR_OBJ_TYPE);
7630 goto done;
7632 *commit_id = got_object_id_dup(
7633 got_object_tag_get_object_id(tag));
7634 if (*commit_id == NULL) {
7635 err = got_error_from_errno("got_object_id_dup");
7636 goto done;
7638 break;
7639 default:
7640 err = got_error(GOT_ERR_OBJ_TYPE);
7641 break;
7644 done:
7645 if (tag)
7646 got_object_tag_close(tag);
7647 if (err) {
7648 free(*commit_id);
7649 *commit_id = NULL;
7651 return err;
7654 static const struct got_error *
7655 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7656 struct tog_reflist_entry *re, struct got_repository *repo)
7658 struct tog_view *log_view;
7659 const struct got_error *err = NULL;
7660 struct got_object_id *commit_id = NULL;
7662 *new_view = NULL;
7664 err = resolve_reflist_entry(&commit_id, re, repo);
7665 if (err) {
7666 if (err->code != GOT_ERR_OBJ_TYPE)
7667 return err;
7668 else
7669 return NULL;
7672 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7673 if (log_view == NULL) {
7674 err = got_error_from_errno("view_open");
7675 goto done;
7678 err = open_log_view(log_view, commit_id, repo,
7679 got_ref_get_name(re->ref), "", 0);
7680 done:
7681 if (err)
7682 view_close(log_view);
7683 else
7684 *new_view = log_view;
7685 free(commit_id);
7686 return err;
7689 static void
7690 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7692 struct tog_reflist_entry *re;
7693 int i = 0;
7695 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7696 return;
7698 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7699 while (i++ < maxscroll) {
7700 if (re == NULL)
7701 break;
7702 s->first_displayed_entry = re;
7703 re = TAILQ_PREV(re, tog_reflist_head, entry);
7707 static const struct got_error *
7708 ref_scroll_down(struct tog_view *view, int maxscroll)
7710 struct tog_ref_view_state *s = &view->state.ref;
7711 struct tog_reflist_entry *next, *last;
7712 int n = 0;
7714 if (s->first_displayed_entry)
7715 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7716 else
7717 next = TAILQ_FIRST(&s->refs);
7719 last = s->last_displayed_entry;
7720 while (next && n++ < maxscroll) {
7721 if (last) {
7722 s->last_displayed_entry = last;
7723 last = TAILQ_NEXT(last, entry);
7725 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7726 s->first_displayed_entry = next;
7727 next = TAILQ_NEXT(next, entry);
7731 return NULL;
7734 static const struct got_error *
7735 search_start_ref_view(struct tog_view *view)
7737 struct tog_ref_view_state *s = &view->state.ref;
7739 s->matched_entry = NULL;
7740 return NULL;
7743 static int
7744 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7746 regmatch_t regmatch;
7748 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7749 0) == 0;
7752 static const struct got_error *
7753 search_next_ref_view(struct tog_view *view)
7755 struct tog_ref_view_state *s = &view->state.ref;
7756 struct tog_reflist_entry *re = NULL;
7758 if (!view->searching) {
7759 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7760 return NULL;
7763 if (s->matched_entry) {
7764 if (view->searching == TOG_SEARCH_FORWARD) {
7765 if (s->selected_entry)
7766 re = TAILQ_NEXT(s->selected_entry, entry);
7767 else
7768 re = TAILQ_PREV(s->selected_entry,
7769 tog_reflist_head, entry);
7770 } else {
7771 if (s->selected_entry == NULL)
7772 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7773 else
7774 re = TAILQ_PREV(s->selected_entry,
7775 tog_reflist_head, entry);
7777 } else {
7778 if (s->selected_entry)
7779 re = s->selected_entry;
7780 else if (view->searching == TOG_SEARCH_FORWARD)
7781 re = TAILQ_FIRST(&s->refs);
7782 else
7783 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7786 while (1) {
7787 if (re == NULL) {
7788 if (s->matched_entry == NULL) {
7789 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7790 return NULL;
7792 if (view->searching == TOG_SEARCH_FORWARD)
7793 re = TAILQ_FIRST(&s->refs);
7794 else
7795 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7798 if (match_reflist_entry(re, &view->regex)) {
7799 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7800 s->matched_entry = re;
7801 break;
7804 if (view->searching == TOG_SEARCH_FORWARD)
7805 re = TAILQ_NEXT(re, entry);
7806 else
7807 re = TAILQ_PREV(re, tog_reflist_head, entry);
7810 if (s->matched_entry) {
7811 s->first_displayed_entry = s->matched_entry;
7812 s->selected = 0;
7815 return NULL;
7818 static const struct got_error *
7819 show_ref_view(struct tog_view *view)
7821 const struct got_error *err = NULL;
7822 struct tog_ref_view_state *s = &view->state.ref;
7823 struct tog_reflist_entry *re;
7824 char *line = NULL;
7825 wchar_t *wline;
7826 struct tog_color *tc;
7827 int width, n;
7828 int limit = view->nlines;
7830 werase(view->window);
7832 s->ndisplayed = 0;
7833 if (view_is_hsplit_top(view))
7834 --limit; /* border */
7836 if (limit == 0)
7837 return NULL;
7839 re = s->first_displayed_entry;
7841 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7842 s->nrefs) == -1)
7843 return got_error_from_errno("asprintf");
7845 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7846 if (err) {
7847 free(line);
7848 return err;
7850 if (view_needs_focus_indication(view))
7851 wstandout(view->window);
7852 waddwstr(view->window, wline);
7853 while (width++ < view->ncols)
7854 waddch(view->window, ' ');
7855 if (view_needs_focus_indication(view))
7856 wstandend(view->window);
7857 free(wline);
7858 wline = NULL;
7859 free(line);
7860 line = NULL;
7861 if (--limit <= 0)
7862 return NULL;
7864 n = 0;
7865 while (re && limit > 0) {
7866 char *line = NULL;
7867 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7869 if (s->show_date) {
7870 struct got_commit_object *ci;
7871 struct got_tag_object *tag;
7872 struct got_object_id *id;
7873 struct tm tm;
7874 time_t t;
7876 err = got_ref_resolve(&id, s->repo, re->ref);
7877 if (err)
7878 return err;
7879 err = got_object_open_as_tag(&tag, s->repo, id);
7880 if (err) {
7881 if (err->code != GOT_ERR_OBJ_TYPE) {
7882 free(id);
7883 return err;
7885 err = got_object_open_as_commit(&ci, s->repo,
7886 id);
7887 if (err) {
7888 free(id);
7889 return err;
7891 t = got_object_commit_get_committer_time(ci);
7892 got_object_commit_close(ci);
7893 } else {
7894 t = got_object_tag_get_tagger_time(tag);
7895 got_object_tag_close(tag);
7897 free(id);
7898 if (gmtime_r(&t, &tm) == NULL)
7899 return got_error_from_errno("gmtime_r");
7900 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7901 return got_error(GOT_ERR_NO_SPACE);
7903 if (got_ref_is_symbolic(re->ref)) {
7904 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7905 ymd : "", got_ref_get_name(re->ref),
7906 got_ref_get_symref_target(re->ref)) == -1)
7907 return got_error_from_errno("asprintf");
7908 } else if (s->show_ids) {
7909 struct got_object_id *id;
7910 char *id_str;
7911 err = got_ref_resolve(&id, s->repo, re->ref);
7912 if (err)
7913 return err;
7914 err = got_object_id_str(&id_str, id);
7915 if (err) {
7916 free(id);
7917 return err;
7919 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7920 got_ref_get_name(re->ref), id_str) == -1) {
7921 err = got_error_from_errno("asprintf");
7922 free(id);
7923 free(id_str);
7924 return err;
7926 free(id);
7927 free(id_str);
7928 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7929 got_ref_get_name(re->ref)) == -1)
7930 return got_error_from_errno("asprintf");
7932 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7933 0, 0);
7934 if (err) {
7935 free(line);
7936 return err;
7938 if (n == s->selected) {
7939 if (view->focussed)
7940 wstandout(view->window);
7941 s->selected_entry = re;
7943 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7944 if (tc)
7945 wattr_on(view->window,
7946 COLOR_PAIR(tc->colorpair), NULL);
7947 waddwstr(view->window, wline);
7948 if (tc)
7949 wattr_off(view->window,
7950 COLOR_PAIR(tc->colorpair), NULL);
7951 if (width < view->ncols - 1)
7952 waddch(view->window, '\n');
7953 if (n == s->selected && view->focussed)
7954 wstandend(view->window);
7955 free(line);
7956 free(wline);
7957 wline = NULL;
7958 n++;
7959 s->ndisplayed++;
7960 s->last_displayed_entry = re;
7962 limit--;
7963 re = TAILQ_NEXT(re, entry);
7966 view_border(view);
7967 return err;
7970 static const struct got_error *
7971 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7972 struct tog_reflist_entry *re, struct got_repository *repo)
7974 const struct got_error *err = NULL;
7975 struct got_object_id *commit_id = NULL;
7976 struct tog_view *tree_view;
7978 *new_view = NULL;
7980 err = resolve_reflist_entry(&commit_id, re, repo);
7981 if (err) {
7982 if (err->code != GOT_ERR_OBJ_TYPE)
7983 return err;
7984 else
7985 return NULL;
7989 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7990 if (tree_view == NULL) {
7991 err = got_error_from_errno("view_open");
7992 goto done;
7995 err = open_tree_view(tree_view, commit_id,
7996 got_ref_get_name(re->ref), repo);
7997 if (err)
7998 goto done;
8000 *new_view = tree_view;
8001 done:
8002 free(commit_id);
8003 return err;
8006 static const struct got_error *
8007 ref_goto_line(struct tog_view *view, int nlines)
8009 const struct got_error *err = NULL;
8010 struct tog_ref_view_state *s = &view->state.ref;
8011 int g, idx = s->selected_entry->idx;
8013 g = view->gline;
8014 view->gline = 0;
8016 if (g == 0)
8017 g = 1;
8018 else if (g > s->nrefs)
8019 g = s->nrefs;
8021 if (g >= s->first_displayed_entry->idx + 1 &&
8022 g <= s->last_displayed_entry->idx + 1 &&
8023 g - s->first_displayed_entry->idx - 1 < nlines) {
8024 s->selected = g - s->first_displayed_entry->idx - 1;
8025 return NULL;
8028 if (idx + 1 < g) {
8029 err = ref_scroll_down(view, g - idx - 1);
8030 if (err)
8031 return err;
8032 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8033 s->first_displayed_entry->idx + s->selected < g &&
8034 s->selected < s->ndisplayed - 1)
8035 s->selected = g - s->first_displayed_entry->idx - 1;
8036 } else if (idx + 1 > g)
8037 ref_scroll_up(s, idx - g + 1);
8039 if (g < nlines && s->first_displayed_entry->idx == 0)
8040 s->selected = g - 1;
8042 return NULL;
8046 static const struct got_error *
8047 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8049 const struct got_error *err = NULL;
8050 struct tog_ref_view_state *s = &view->state.ref;
8051 struct tog_reflist_entry *re;
8052 int n, nscroll = view->nlines - 1;
8054 if (view->gline)
8055 return ref_goto_line(view, nscroll);
8057 switch (ch) {
8058 case 'i':
8059 s->show_ids = !s->show_ids;
8060 view->count = 0;
8061 break;
8062 case 'm':
8063 s->show_date = !s->show_date;
8064 view->count = 0;
8065 break;
8066 case 'o':
8067 s->sort_by_date = !s->sort_by_date;
8068 view->count = 0;
8069 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8070 got_ref_cmp_by_commit_timestamp_descending :
8071 tog_ref_cmp_by_name, s->repo);
8072 if (err)
8073 break;
8074 got_reflist_object_id_map_free(tog_refs_idmap);
8075 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8076 &tog_refs, s->repo);
8077 if (err)
8078 break;
8079 ref_view_free_refs(s);
8080 err = ref_view_load_refs(s);
8081 break;
8082 case KEY_ENTER:
8083 case '\r':
8084 view->count = 0;
8085 if (!s->selected_entry)
8086 break;
8087 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8088 break;
8089 case 'T':
8090 view->count = 0;
8091 if (!s->selected_entry)
8092 break;
8093 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8094 break;
8095 case 'g':
8096 case KEY_HOME:
8097 s->selected = 0;
8098 view->count = 0;
8099 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8100 break;
8101 case 'G':
8102 case KEY_END: {
8103 int eos = view->nlines - 1;
8105 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8106 --eos; /* border */
8107 s->selected = 0;
8108 view->count = 0;
8109 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8110 for (n = 0; n < eos; n++) {
8111 if (re == NULL)
8112 break;
8113 s->first_displayed_entry = re;
8114 re = TAILQ_PREV(re, tog_reflist_head, entry);
8116 if (n > 0)
8117 s->selected = n - 1;
8118 break;
8120 case 'k':
8121 case KEY_UP:
8122 case CTRL('p'):
8123 if (s->selected > 0) {
8124 s->selected--;
8125 break;
8127 ref_scroll_up(s, 1);
8128 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8129 view->count = 0;
8130 break;
8131 case CTRL('u'):
8132 case 'u':
8133 nscroll /= 2;
8134 /* FALL THROUGH */
8135 case KEY_PPAGE:
8136 case CTRL('b'):
8137 case 'b':
8138 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8139 s->selected -= MIN(nscroll, s->selected);
8140 ref_scroll_up(s, MAX(0, nscroll));
8141 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8142 view->count = 0;
8143 break;
8144 case 'j':
8145 case KEY_DOWN:
8146 case CTRL('n'):
8147 if (s->selected < s->ndisplayed - 1) {
8148 s->selected++;
8149 break;
8151 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8152 /* can't scroll any further */
8153 view->count = 0;
8154 break;
8156 ref_scroll_down(view, 1);
8157 break;
8158 case CTRL('d'):
8159 case 'd':
8160 nscroll /= 2;
8161 /* FALL THROUGH */
8162 case KEY_NPAGE:
8163 case CTRL('f'):
8164 case 'f':
8165 case ' ':
8166 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8167 /* can't scroll any further; move cursor down */
8168 if (s->selected < s->ndisplayed - 1)
8169 s->selected += MIN(nscroll,
8170 s->ndisplayed - s->selected - 1);
8171 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8172 s->selected += s->ndisplayed - s->selected - 1;
8173 view->count = 0;
8174 break;
8176 ref_scroll_down(view, nscroll);
8177 break;
8178 case CTRL('l'):
8179 view->count = 0;
8180 tog_free_refs();
8181 err = tog_load_refs(s->repo, s->sort_by_date);
8182 if (err)
8183 break;
8184 ref_view_free_refs(s);
8185 err = ref_view_load_refs(s);
8186 break;
8187 case KEY_RESIZE:
8188 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8189 s->selected = view->nlines - 2;
8190 break;
8191 default:
8192 view->count = 0;
8193 break;
8196 return err;
8199 __dead static void
8200 usage_ref(void)
8202 endwin();
8203 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8204 getprogname());
8205 exit(1);
8208 static const struct got_error *
8209 cmd_ref(int argc, char *argv[])
8211 const struct got_error *error;
8212 struct got_repository *repo = NULL;
8213 struct got_worktree *worktree = NULL;
8214 char *cwd = NULL, *repo_path = NULL;
8215 int ch;
8216 struct tog_view *view;
8217 int *pack_fds = NULL;
8219 while ((ch = getopt(argc, argv, "r:")) != -1) {
8220 switch (ch) {
8221 case 'r':
8222 repo_path = realpath(optarg, NULL);
8223 if (repo_path == NULL)
8224 return got_error_from_errno2("realpath",
8225 optarg);
8226 break;
8227 default:
8228 usage_ref();
8229 /* NOTREACHED */
8233 argc -= optind;
8234 argv += optind;
8236 if (argc > 1)
8237 usage_ref();
8239 error = got_repo_pack_fds_open(&pack_fds);
8240 if (error != NULL)
8241 goto done;
8243 if (repo_path == NULL) {
8244 cwd = getcwd(NULL, 0);
8245 if (cwd == NULL)
8246 return got_error_from_errno("getcwd");
8247 error = got_worktree_open(&worktree, cwd);
8248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8249 goto done;
8250 if (worktree)
8251 repo_path =
8252 strdup(got_worktree_get_repo_path(worktree));
8253 else
8254 repo_path = strdup(cwd);
8255 if (repo_path == NULL) {
8256 error = got_error_from_errno("strdup");
8257 goto done;
8261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8262 if (error != NULL)
8263 goto done;
8265 init_curses();
8267 error = apply_unveil(got_repo_get_path(repo), NULL);
8268 if (error)
8269 goto done;
8271 error = tog_load_refs(repo, 0);
8272 if (error)
8273 goto done;
8275 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8276 if (view == NULL) {
8277 error = got_error_from_errno("view_open");
8278 goto done;
8281 error = open_ref_view(view, repo);
8282 if (error)
8283 goto done;
8285 if (worktree) {
8286 /* Release work tree lock. */
8287 got_worktree_close(worktree);
8288 worktree = NULL;
8290 error = view_loop(view);
8291 done:
8292 free(repo_path);
8293 free(cwd);
8294 if (repo) {
8295 const struct got_error *close_err = got_repo_close(repo);
8296 if (close_err)
8297 error = close_err;
8299 if (pack_fds) {
8300 const struct got_error *pack_err =
8301 got_repo_pack_fds_close(pack_fds);
8302 if (error == NULL)
8303 error = pack_err;
8305 tog_free_refs();
8306 return error;
8309 static const struct got_error *
8310 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8311 enum tog_view_type request, int y, int x)
8313 const struct got_error *err = NULL;
8315 *new_view = NULL;
8317 switch (request) {
8318 case TOG_VIEW_DIFF:
8319 if (view->type == TOG_VIEW_LOG) {
8320 struct tog_log_view_state *s = &view->state.log;
8322 err = open_diff_view_for_commit(new_view, y, x,
8323 s->selected_entry->commit, s->selected_entry->id,
8324 view, s->repo);
8325 } else
8326 return got_error_msg(GOT_ERR_NOT_IMPL,
8327 "parent/child view pair not supported");
8328 break;
8329 case TOG_VIEW_BLAME:
8330 if (view->type == TOG_VIEW_TREE) {
8331 struct tog_tree_view_state *s = &view->state.tree;
8333 err = blame_tree_entry(new_view, y, x,
8334 s->selected_entry, &s->parents, s->commit_id,
8335 s->repo);
8336 } else
8337 return got_error_msg(GOT_ERR_NOT_IMPL,
8338 "parent/child view pair not supported");
8339 break;
8340 case TOG_VIEW_LOG:
8341 if (view->type == TOG_VIEW_BLAME)
8342 err = log_annotated_line(new_view, y, x,
8343 view->state.blame.repo, view->state.blame.id_to_log);
8344 else if (view->type == TOG_VIEW_TREE)
8345 err = log_selected_tree_entry(new_view, y, x,
8346 &view->state.tree);
8347 else if (view->type == TOG_VIEW_REF)
8348 err = log_ref_entry(new_view, y, x,
8349 view->state.ref.selected_entry,
8350 view->state.ref.repo);
8351 else
8352 return got_error_msg(GOT_ERR_NOT_IMPL,
8353 "parent/child view pair not supported");
8354 break;
8355 case TOG_VIEW_TREE:
8356 if (view->type == TOG_VIEW_LOG)
8357 err = browse_commit_tree(new_view, y, x,
8358 view->state.log.selected_entry,
8359 view->state.log.in_repo_path,
8360 view->state.log.head_ref_name,
8361 view->state.log.repo);
8362 else if (view->type == TOG_VIEW_REF)
8363 err = browse_ref_tree(new_view, y, x,
8364 view->state.ref.selected_entry,
8365 view->state.ref.repo);
8366 else
8367 return got_error_msg(GOT_ERR_NOT_IMPL,
8368 "parent/child view pair not supported");
8369 break;
8370 case TOG_VIEW_REF:
8371 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8372 if (*new_view == NULL)
8373 return got_error_from_errno("view_open");
8374 if (view->type == TOG_VIEW_LOG)
8375 err = open_ref_view(*new_view, view->state.log.repo);
8376 else if (view->type == TOG_VIEW_TREE)
8377 err = open_ref_view(*new_view, view->state.tree.repo);
8378 else
8379 err = got_error_msg(GOT_ERR_NOT_IMPL,
8380 "parent/child view pair not supported");
8381 if (err)
8382 view_close(*new_view);
8383 break;
8384 default:
8385 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8388 return err;
8392 * If view was scrolled down to move the selected line into view when opening a
8393 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8395 static void
8396 offset_selection_up(struct tog_view *view)
8398 switch (view->type) {
8399 case TOG_VIEW_BLAME: {
8400 struct tog_blame_view_state *s = &view->state.blame;
8401 if (s->first_displayed_line == 1) {
8402 s->selected_line = MAX(s->selected_line - view->offset,
8403 1);
8404 break;
8406 if (s->first_displayed_line > view->offset)
8407 s->first_displayed_line -= view->offset;
8408 else
8409 s->first_displayed_line = 1;
8410 s->selected_line += view->offset;
8411 break;
8413 case TOG_VIEW_LOG:
8414 log_scroll_up(&view->state.log, view->offset);
8415 view->state.log.selected += view->offset;
8416 break;
8417 case TOG_VIEW_REF:
8418 ref_scroll_up(&view->state.ref, view->offset);
8419 view->state.ref.selected += view->offset;
8420 break;
8421 case TOG_VIEW_TREE:
8422 tree_scroll_up(&view->state.tree, view->offset);
8423 view->state.tree.selected += view->offset;
8424 break;
8425 default:
8426 break;
8429 view->offset = 0;
8433 * If the selected line is in the section of screen covered by the bottom split,
8434 * scroll down offset lines to move it into view and index its new position.
8436 static const struct got_error *
8437 offset_selection_down(struct tog_view *view)
8439 const struct got_error *err = NULL;
8440 const struct got_error *(*scrolld)(struct tog_view *, int);
8441 int *selected = NULL;
8442 int header, offset;
8444 switch (view->type) {
8445 case TOG_VIEW_BLAME: {
8446 struct tog_blame_view_state *s = &view->state.blame;
8447 header = 3;
8448 scrolld = NULL;
8449 if (s->selected_line > view->nlines - header) {
8450 offset = abs(view->nlines - s->selected_line - header);
8451 s->first_displayed_line += offset;
8452 s->selected_line -= offset;
8453 view->offset = offset;
8455 break;
8457 case TOG_VIEW_LOG: {
8458 struct tog_log_view_state *s = &view->state.log;
8459 scrolld = &log_scroll_down;
8460 header = view_is_parent_view(view) ? 3 : 2;
8461 selected = &s->selected;
8462 break;
8464 case TOG_VIEW_REF: {
8465 struct tog_ref_view_state *s = &view->state.ref;
8466 scrolld = &ref_scroll_down;
8467 header = 3;
8468 selected = &s->selected;
8469 break;
8471 case TOG_VIEW_TREE: {
8472 struct tog_tree_view_state *s = &view->state.tree;
8473 scrolld = &tree_scroll_down;
8474 header = 5;
8475 selected = &s->selected;
8476 break;
8478 default:
8479 selected = NULL;
8480 scrolld = NULL;
8481 header = 0;
8482 break;
8485 if (selected && *selected > view->nlines - header) {
8486 offset = abs(view->nlines - *selected - header);
8487 view->offset = offset;
8488 if (scrolld && offset) {
8489 err = scrolld(view, offset);
8490 *selected -= offset;
8494 return err;
8497 static void
8498 list_commands(FILE *fp)
8500 size_t i;
8502 fprintf(fp, "commands:");
8503 for (i = 0; i < nitems(tog_commands); i++) {
8504 const struct tog_cmd *cmd = &tog_commands[i];
8505 fprintf(fp, " %s", cmd->name);
8507 fputc('\n', fp);
8510 __dead static void
8511 usage(int hflag, int status)
8513 FILE *fp = (status == 0) ? stdout : stderr;
8515 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8516 getprogname());
8517 if (hflag) {
8518 fprintf(fp, "lazy usage: %s path\n", getprogname());
8519 list_commands(fp);
8521 exit(status);
8524 static char **
8525 make_argv(int argc, ...)
8527 va_list ap;
8528 char **argv;
8529 int i;
8531 va_start(ap, argc);
8533 argv = calloc(argc, sizeof(char *));
8534 if (argv == NULL)
8535 err(1, "calloc");
8536 for (i = 0; i < argc; i++) {
8537 argv[i] = strdup(va_arg(ap, char *));
8538 if (argv[i] == NULL)
8539 err(1, "strdup");
8542 va_end(ap);
8543 return argv;
8547 * Try to convert 'tog path' into a 'tog log path' command.
8548 * The user could simply have mistyped the command rather than knowingly
8549 * provided a path. So check whether argv[0] can in fact be resolved
8550 * to a path in the HEAD commit and print a special error if not.
8551 * This hack is for mpi@ <3
8553 static const struct got_error *
8554 tog_log_with_path(int argc, char *argv[])
8556 const struct got_error *error = NULL, *close_err;
8557 const struct tog_cmd *cmd = NULL;
8558 struct got_repository *repo = NULL;
8559 struct got_worktree *worktree = NULL;
8560 struct got_object_id *commit_id = NULL, *id = NULL;
8561 struct got_commit_object *commit = NULL;
8562 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8563 char *commit_id_str = NULL, **cmd_argv = NULL;
8564 int *pack_fds = NULL;
8566 cwd = getcwd(NULL, 0);
8567 if (cwd == NULL)
8568 return got_error_from_errno("getcwd");
8570 error = got_repo_pack_fds_open(&pack_fds);
8571 if (error != NULL)
8572 goto done;
8574 error = got_worktree_open(&worktree, cwd);
8575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8576 goto done;
8578 if (worktree)
8579 repo_path = strdup(got_worktree_get_repo_path(worktree));
8580 else
8581 repo_path = strdup(cwd);
8582 if (repo_path == NULL) {
8583 error = got_error_from_errno("strdup");
8584 goto done;
8587 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8588 if (error != NULL)
8589 goto done;
8591 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8592 repo, worktree);
8593 if (error)
8594 goto done;
8596 error = tog_load_refs(repo, 0);
8597 if (error)
8598 goto done;
8599 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8600 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8601 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8602 if (error)
8603 goto done;
8605 if (worktree) {
8606 got_worktree_close(worktree);
8607 worktree = NULL;
8610 error = got_object_open_as_commit(&commit, repo, commit_id);
8611 if (error)
8612 goto done;
8614 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8615 if (error) {
8616 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8617 goto done;
8618 fprintf(stderr, "%s: '%s' is no known command or path\n",
8619 getprogname(), argv[0]);
8620 usage(1, 1);
8621 /* not reached */
8624 error = got_object_id_str(&commit_id_str, commit_id);
8625 if (error)
8626 goto done;
8628 cmd = &tog_commands[0]; /* log */
8629 argc = 4;
8630 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8631 error = cmd->cmd_main(argc, cmd_argv);
8632 done:
8633 if (repo) {
8634 close_err = got_repo_close(repo);
8635 if (error == NULL)
8636 error = close_err;
8638 if (commit)
8639 got_object_commit_close(commit);
8640 if (worktree)
8641 got_worktree_close(worktree);
8642 if (pack_fds) {
8643 const struct got_error *pack_err =
8644 got_repo_pack_fds_close(pack_fds);
8645 if (error == NULL)
8646 error = pack_err;
8648 free(id);
8649 free(commit_id_str);
8650 free(commit_id);
8651 free(cwd);
8652 free(repo_path);
8653 free(in_repo_path);
8654 if (cmd_argv) {
8655 int i;
8656 for (i = 0; i < argc; i++)
8657 free(cmd_argv[i]);
8658 free(cmd_argv);
8660 tog_free_refs();
8661 return error;
8664 int
8665 main(int argc, char *argv[])
8667 const struct got_error *error = NULL;
8668 const struct tog_cmd *cmd = NULL;
8669 int ch, hflag = 0, Vflag = 0;
8670 char **cmd_argv = NULL;
8671 static const struct option longopts[] = {
8672 { "version", no_argument, NULL, 'V' },
8673 { NULL, 0, NULL, 0}
8675 char *diff_algo_str = NULL;
8677 if (!isatty(STDIN_FILENO))
8678 errx(1, "standard input is not a tty");
8680 setlocale(LC_CTYPE, "");
8682 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8683 switch (ch) {
8684 case 'h':
8685 hflag = 1;
8686 break;
8687 case 'V':
8688 Vflag = 1;
8689 break;
8690 default:
8691 usage(hflag, 1);
8692 /* NOTREACHED */
8696 argc -= optind;
8697 argv += optind;
8698 optind = 1;
8699 optreset = 1;
8701 if (Vflag) {
8702 got_version_print_str();
8703 return 0;
8706 #ifndef PROFILE
8707 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8708 NULL) == -1)
8709 err(1, "pledge");
8710 #endif
8712 if (argc == 0) {
8713 if (hflag)
8714 usage(hflag, 0);
8715 /* Build an argument vector which runs a default command. */
8716 cmd = &tog_commands[0];
8717 argc = 1;
8718 cmd_argv = make_argv(argc, cmd->name);
8719 } else {
8720 size_t i;
8722 /* Did the user specify a command? */
8723 for (i = 0; i < nitems(tog_commands); i++) {
8724 if (strncmp(tog_commands[i].name, argv[0],
8725 strlen(argv[0])) == 0) {
8726 cmd = &tog_commands[i];
8727 break;
8732 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8733 if (diff_algo_str) {
8734 if (strcasecmp(diff_algo_str, "patience") == 0)
8735 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8736 if (strcasecmp(diff_algo_str, "myers") == 0)
8737 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8740 if (cmd == NULL) {
8741 if (argc != 1)
8742 usage(0, 1);
8743 /* No command specified; try log with a path */
8744 error = tog_log_with_path(argc, argv);
8745 } else {
8746 if (hflag)
8747 cmd->cmd_usage();
8748 else
8749 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8752 endwin();
8753 putchar('\n');
8754 if (cmd_argv) {
8755 int i;
8756 for (i = 0; i < argc; i++)
8757 free(cmd_argv[i]);
8758 free(cmd_argv);
8761 if (error && error->code != GOT_ERR_CANCELLED)
8762 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8763 return 0;