Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 enum tog_view_mode {
109 TOG_VIEW_SPLIT_NONE,
110 TOG_VIEW_SPLIT_VERT,
111 TOG_VIEW_SPLIT_HRZN
112 };
114 #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
116 #define TOG_EOF_STRING "(END)"
118 struct commit_queue_entry {
119 TAILQ_ENTRY(commit_queue_entry) entry;
120 struct got_object_id *id;
121 struct got_commit_object *commit;
122 int idx;
123 };
124 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 struct commit_queue {
126 int ncommits;
127 struct commit_queue_head head;
128 };
130 struct tog_color {
131 STAILQ_ENTRY(tog_color) entry;
132 regex_t regex;
133 short colorpair;
134 };
135 STAILQ_HEAD(tog_colors, tog_color);
137 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
141 static const struct got_error *
142 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
143 struct got_reference* re2)
145 const char *name1 = got_ref_get_name(re1);
146 const char *name2 = got_ref_get_name(re2);
147 int isbackup1, isbackup2;
149 /* Sort backup refs towards the bottom of the list. */
150 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
151 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
152 if (!isbackup1 && isbackup2) {
153 *cmp = -1;
154 return NULL;
155 } else if (isbackup1 && !isbackup2) {
156 *cmp = 1;
157 return NULL;
160 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
161 return NULL;
164 static const struct got_error *
165 tog_load_refs(struct got_repository *repo, int sort_by_date)
167 const struct got_error *err;
169 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
170 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 repo);
172 if (err)
173 return err;
175 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
176 repo);
179 static void
180 tog_free_refs(void)
182 if (tog_refs_idmap) {
183 got_reflist_object_id_map_free(tog_refs_idmap);
184 tog_refs_idmap = NULL;
186 got_ref_list_free(&tog_refs);
189 static const struct got_error *
190 add_color(struct tog_colors *colors, const char *pattern,
191 int idx, short color)
193 const struct got_error *err = NULL;
194 struct tog_color *tc;
195 int regerr = 0;
197 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 return NULL;
200 init_pair(idx, color, -1);
202 tc = calloc(1, sizeof(*tc));
203 if (tc == NULL)
204 return got_error_from_errno("calloc");
205 regerr = regcomp(&tc->regex, pattern,
206 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
207 if (regerr) {
208 static char regerr_msg[512];
209 static char err_msg[512];
210 regerror(regerr, &tc->regex, regerr_msg,
211 sizeof(regerr_msg));
212 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
213 regerr_msg);
214 err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 free(tc);
216 return err;
218 tc->colorpair = idx;
219 STAILQ_INSERT_HEAD(colors, tc, entry);
220 return NULL;
223 static void
224 free_colors(struct tog_colors *colors)
226 struct tog_color *tc;
228 while (!STAILQ_EMPTY(colors)) {
229 tc = STAILQ_FIRST(colors);
230 STAILQ_REMOVE_HEAD(colors, entry);
231 regfree(&tc->regex);
232 free(tc);
236 static struct tog_color *
237 get_color(struct tog_colors *colors, int colorpair)
239 struct tog_color *tc = NULL;
241 STAILQ_FOREACH(tc, colors, entry) {
242 if (tc->colorpair == colorpair)
243 return tc;
246 return NULL;
249 static int
250 default_color_value(const char *envvar)
252 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
253 return COLOR_MAGENTA;
254 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
255 return COLOR_CYAN;
256 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
257 return COLOR_YELLOW;
258 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
259 return COLOR_GREEN;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
263 return COLOR_MAGENTA;
264 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
265 return COLOR_CYAN;
266 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
267 return COLOR_GREEN;
268 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
269 return COLOR_GREEN;
270 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
271 return COLOR_CYAN;
272 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
273 return COLOR_YELLOW;
274 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
275 return COLOR_GREEN;
276 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
277 return COLOR_MAGENTA;
278 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
279 return COLOR_YELLOW;
280 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
281 return COLOR_CYAN;
283 return -1;
286 static int
287 get_color_value(const char *envvar)
289 const char *val = getenv(envvar);
291 if (val == NULL)
292 return default_color_value(envvar);
294 if (strcasecmp(val, "black") == 0)
295 return COLOR_BLACK;
296 if (strcasecmp(val, "red") == 0)
297 return COLOR_RED;
298 if (strcasecmp(val, "green") == 0)
299 return COLOR_GREEN;
300 if (strcasecmp(val, "yellow") == 0)
301 return COLOR_YELLOW;
302 if (strcasecmp(val, "blue") == 0)
303 return COLOR_BLUE;
304 if (strcasecmp(val, "magenta") == 0)
305 return COLOR_MAGENTA;
306 if (strcasecmp(val, "cyan") == 0)
307 return COLOR_CYAN;
308 if (strcasecmp(val, "white") == 0)
309 return COLOR_WHITE;
310 if (strcasecmp(val, "default") == 0)
311 return -1;
313 return default_color_value(envvar);
316 struct tog_diff_view_state {
317 struct got_object_id *id1, *id2;
318 const char *label1, *label2;
319 FILE *f, *f1, *f2;
320 int fd1, fd2;
321 int lineno;
322 int first_displayed_line;
323 int last_displayed_line;
324 int eof;
325 int diff_context;
326 int ignore_whitespace;
327 int force_text_diff;
328 struct got_repository *repo;
329 struct got_diff_line *lines;
330 size_t nlines;
331 int matched_line;
332 int selected_line;
334 /* passed from log or blame view; may be NULL */
335 struct tog_view *parent_view;
336 };
338 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 static volatile sig_atomic_t tog_thread_error;
341 struct tog_log_thread_args {
342 pthread_cond_t need_commits;
343 pthread_cond_t commit_loaded;
344 int commits_needed;
345 int load_all;
346 struct got_commit_graph *graph;
347 struct commit_queue *real_commits;
348 const char *in_repo_path;
349 struct got_object_id *start_id;
350 struct got_repository *repo;
351 int *pack_fds;
352 int log_complete;
353 sig_atomic_t *quit;
354 struct commit_queue_entry **first_displayed_entry;
355 struct commit_queue_entry **selected_entry;
356 int *searching;
357 int *search_next_done;
358 regex_t *regex;
359 int *limiting;
360 int limit_match;
361 regex_t *limit_regex;
362 struct commit_queue *limit_commits;
363 };
365 struct tog_log_view_state {
366 struct commit_queue *commits;
367 struct commit_queue_entry *first_displayed_entry;
368 struct commit_queue_entry *last_displayed_entry;
369 struct commit_queue_entry *selected_entry;
370 struct commit_queue real_commits;
371 int selected;
372 char *in_repo_path;
373 char *head_ref_name;
374 int log_branches;
375 struct got_repository *repo;
376 struct got_object_id *start_id;
377 sig_atomic_t quit;
378 pthread_t thread;
379 struct tog_log_thread_args thread_args;
380 struct commit_queue_entry *matched_entry;
381 struct commit_queue_entry *search_entry;
382 struct tog_colors colors;
383 int use_committer;
384 int limit_view;
385 regex_t limit_regex;
386 struct commit_queue limit_commits;
387 };
389 #define TOG_COLOR_DIFF_MINUS 1
390 #define TOG_COLOR_DIFF_PLUS 2
391 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
392 #define TOG_COLOR_DIFF_META 4
393 #define TOG_COLOR_TREE_SUBMODULE 5
394 #define TOG_COLOR_TREE_SYMLINK 6
395 #define TOG_COLOR_TREE_DIRECTORY 7
396 #define TOG_COLOR_TREE_EXECUTABLE 8
397 #define TOG_COLOR_COMMIT 9
398 #define TOG_COLOR_AUTHOR 10
399 #define TOG_COLOR_DATE 11
400 #define TOG_COLOR_REFS_HEADS 12
401 #define TOG_COLOR_REFS_TAGS 13
402 #define TOG_COLOR_REFS_REMOTES 14
403 #define TOG_COLOR_REFS_BACKUP 15
405 struct tog_blame_cb_args {
406 struct tog_blame_line *lines; /* one per line */
407 int nlines;
409 struct tog_view *view;
410 struct got_object_id *commit_id;
411 int *quit;
412 };
414 struct tog_blame_thread_args {
415 const char *path;
416 struct got_repository *repo;
417 struct tog_blame_cb_args *cb_args;
418 int *complete;
419 got_cancel_cb cancel_cb;
420 void *cancel_arg;
421 };
423 struct tog_blame {
424 FILE *f;
425 off_t filesize;
426 struct tog_blame_line *lines;
427 int nlines;
428 off_t *line_offsets;
429 pthread_t thread;
430 struct tog_blame_thread_args thread_args;
431 struct tog_blame_cb_args cb_args;
432 const char *path;
433 int *pack_fds;
434 };
436 struct tog_blame_view_state {
437 int first_displayed_line;
438 int last_displayed_line;
439 int selected_line;
440 int last_diffed_line;
441 int blame_complete;
442 int eof;
443 int done;
444 struct got_object_id_queue blamed_commits;
445 struct got_object_qid *blamed_commit;
446 char *path;
447 struct got_repository *repo;
448 struct got_object_id *commit_id;
449 struct got_object_id *id_to_log;
450 struct tog_blame blame;
451 int matched_line;
452 struct tog_colors colors;
453 };
455 struct tog_parent_tree {
456 TAILQ_ENTRY(tog_parent_tree) entry;
457 struct got_tree_object *tree;
458 struct got_tree_entry *first_displayed_entry;
459 struct got_tree_entry *selected_entry;
460 int selected;
461 };
463 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
465 struct tog_tree_view_state {
466 char *tree_label;
467 struct got_object_id *commit_id;/* commit which this tree belongs to */
468 struct got_tree_object *root; /* the commit's root tree entry */
469 struct got_tree_object *tree; /* currently displayed (sub-)tree */
470 struct got_tree_entry *first_displayed_entry;
471 struct got_tree_entry *last_displayed_entry;
472 struct got_tree_entry *selected_entry;
473 int ndisplayed, selected, show_ids;
474 struct tog_parent_trees parents; /* parent trees of current sub-tree */
475 char *head_ref_name;
476 struct got_repository *repo;
477 struct got_tree_entry *matched_entry;
478 struct tog_colors colors;
479 };
481 struct tog_reflist_entry {
482 TAILQ_ENTRY(tog_reflist_entry) entry;
483 struct got_reference *ref;
484 int idx;
485 };
487 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
489 struct tog_ref_view_state {
490 struct tog_reflist_head refs;
491 struct tog_reflist_entry *first_displayed_entry;
492 struct tog_reflist_entry *last_displayed_entry;
493 struct tog_reflist_entry *selected_entry;
494 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
495 struct got_repository *repo;
496 struct tog_reflist_entry *matched_entry;
497 struct tog_colors colors;
498 };
500 /*
501 * We implement two types of views: parent views and child views.
503 * The 'Tab' key switches focus between a parent view and its child view.
504 * Child views are shown side-by-side to their parent view, provided
505 * there is enough screen estate.
507 * When a new view is opened from within a parent view, this new view
508 * becomes a child view of the parent view, replacing any existing child.
510 * When a new view is opened from within a child view, this new view
511 * becomes a parent view which will obscure the views below until the
512 * user quits the new parent view by typing 'q'.
514 * This list of views contains parent views only.
515 * Child views are only pointed to by their parent view.
516 */
517 TAILQ_HEAD(tog_view_list_head, tog_view);
519 struct tog_view {
520 TAILQ_ENTRY(tog_view) entry;
521 WINDOW *window;
522 PANEL *panel;
523 int nlines, ncols, begin_y, begin_x; /* based on split height/width */
524 int resized_y, resized_x; /* begin_y/x based on user resizing */
525 int maxx, x; /* max column and current start column */
526 int lines, cols; /* copies of LINES and COLS */
527 int nscrolled, offset; /* lines scrolled and hsplit line offset */
528 int gline, hiline; /* navigate to and highlight this nG line */
529 int ch, count; /* current keymap and count prefix */
530 int resized; /* set when in a resize event */
531 int focussed; /* Only set on one parent or child view at a time. */
532 int dying;
533 struct tog_view *parent;
534 struct tog_view *child;
536 /*
537 * This flag is initially set on parent views when a new child view
538 * is created. It gets toggled when the 'Tab' key switches focus
539 * between parent and child.
540 * The flag indicates whether focus should be passed on to our child
541 * view if this parent view gets picked for focus after another parent
542 * view was closed. This prevents child views from losing focus in such
543 * situations.
544 */
545 int focus_child;
547 enum tog_view_mode mode;
548 /* type-specific state */
549 enum tog_view_type type;
550 union {
551 struct tog_diff_view_state diff;
552 struct tog_log_view_state log;
553 struct tog_blame_view_state blame;
554 struct tog_tree_view_state tree;
555 struct tog_ref_view_state ref;
556 } state;
558 const struct got_error *(*show)(struct tog_view *);
559 const struct got_error *(*input)(struct tog_view **,
560 struct tog_view *, int);
561 const struct got_error *(*reset)(struct tog_view *);
562 const struct got_error *(*resize)(struct tog_view *, int);
563 const struct got_error *(*close)(struct tog_view *);
565 const struct got_error *(*search_start)(struct tog_view *);
566 const struct got_error *(*search_next)(struct tog_view *);
567 int search_started;
568 int searching;
569 #define TOG_SEARCH_FORWARD 1
570 #define TOG_SEARCH_BACKWARD 2
571 int search_next_done;
572 #define TOG_SEARCH_HAVE_MORE 1
573 #define TOG_SEARCH_NO_MORE 2
574 #define TOG_SEARCH_HAVE_NONE 3
575 regex_t regex;
576 regmatch_t regmatch;
577 };
579 static const struct got_error *open_diff_view(struct tog_view *,
580 struct got_object_id *, struct got_object_id *,
581 const char *, const char *, int, int, int, struct tog_view *,
582 struct got_repository *);
583 static const struct got_error *show_diff_view(struct tog_view *);
584 static const struct got_error *input_diff_view(struct tog_view **,
585 struct tog_view *, int);
586 static const struct got_error *reset_diff_view(struct tog_view *);
587 static const struct got_error* close_diff_view(struct tog_view *);
588 static const struct got_error *search_start_diff_view(struct tog_view *);
589 static const struct got_error *search_next_diff_view(struct tog_view *);
591 static const struct got_error *open_log_view(struct tog_view *,
592 struct got_object_id *, struct got_repository *,
593 const char *, const char *, int);
594 static const struct got_error * show_log_view(struct tog_view *);
595 static const struct got_error *input_log_view(struct tog_view **,
596 struct tog_view *, int);
597 static const struct got_error *resize_log_view(struct tog_view *, int);
598 static const struct got_error *close_log_view(struct tog_view *);
599 static const struct got_error *search_start_log_view(struct tog_view *);
600 static const struct got_error *search_next_log_view(struct tog_view *);
602 static const struct got_error *open_blame_view(struct tog_view *, char *,
603 struct got_object_id *, struct got_repository *);
604 static const struct got_error *show_blame_view(struct tog_view *);
605 static const struct got_error *input_blame_view(struct tog_view **,
606 struct tog_view *, int);
607 static const struct got_error *reset_blame_view(struct tog_view *);
608 static const struct got_error *close_blame_view(struct tog_view *);
609 static const struct got_error *search_start_blame_view(struct tog_view *);
610 static const struct got_error *search_next_blame_view(struct tog_view *);
612 static const struct got_error *open_tree_view(struct tog_view *,
613 struct got_object_id *, const char *, struct got_repository *);
614 static const struct got_error *show_tree_view(struct tog_view *);
615 static const struct got_error *input_tree_view(struct tog_view **,
616 struct tog_view *, int);
617 static const struct got_error *close_tree_view(struct tog_view *);
618 static const struct got_error *search_start_tree_view(struct tog_view *);
619 static const struct got_error *search_next_tree_view(struct tog_view *);
621 static const struct got_error *open_ref_view(struct tog_view *,
622 struct got_repository *);
623 static const struct got_error *show_ref_view(struct tog_view *);
624 static const struct got_error *input_ref_view(struct tog_view **,
625 struct tog_view *, int);
626 static const struct got_error *close_ref_view(struct tog_view *);
627 static const struct got_error *search_start_ref_view(struct tog_view *);
628 static const struct got_error *search_next_ref_view(struct tog_view *);
630 static volatile sig_atomic_t tog_sigwinch_received;
631 static volatile sig_atomic_t tog_sigpipe_received;
632 static volatile sig_atomic_t tog_sigcont_received;
633 static volatile sig_atomic_t tog_sigint_received;
634 static volatile sig_atomic_t tog_sigterm_received;
636 static void
637 tog_sigwinch(int signo)
639 tog_sigwinch_received = 1;
642 static void
643 tog_sigpipe(int signo)
645 tog_sigpipe_received = 1;
648 static void
649 tog_sigcont(int signo)
651 tog_sigcont_received = 1;
654 static void
655 tog_sigint(int signo)
657 tog_sigint_received = 1;
660 static void
661 tog_sigterm(int signo)
663 tog_sigterm_received = 1;
666 static int
667 tog_fatal_signal_received(void)
669 return (tog_sigpipe_received ||
670 tog_sigint_received || tog_sigint_received);
673 static const struct got_error *
674 view_close(struct tog_view *view)
676 const struct got_error *err = NULL, *child_err = NULL;
678 if (view->child) {
679 child_err = view_close(view->child);
680 view->child = NULL;
682 if (view->close)
683 err = view->close(view);
684 if (view->panel)
685 del_panel(view->panel);
686 if (view->window)
687 delwin(view->window);
688 free(view);
689 return err ? err : child_err;
692 static struct tog_view *
693 view_open(int nlines, int ncols, int begin_y, int begin_x,
694 enum tog_view_type type)
696 struct tog_view *view = calloc(1, sizeof(*view));
698 if (view == NULL)
699 return NULL;
701 view->type = type;
702 view->lines = LINES;
703 view->cols = COLS;
704 view->nlines = nlines ? nlines : LINES - begin_y;
705 view->ncols = ncols ? ncols : COLS - begin_x;
706 view->begin_y = begin_y;
707 view->begin_x = begin_x;
708 view->window = newwin(nlines, ncols, begin_y, begin_x);
709 if (view->window == NULL) {
710 view_close(view);
711 return NULL;
713 view->panel = new_panel(view->window);
714 if (view->panel == NULL ||
715 set_panel_userptr(view->panel, view) != OK) {
716 view_close(view);
717 return NULL;
720 keypad(view->window, TRUE);
721 return view;
724 static int
725 view_split_begin_x(int begin_x)
727 if (begin_x > 0 || COLS < 120)
728 return 0;
729 return (COLS - MAX(COLS / 2, 80));
732 /* XXX Stub till we decide what to do. */
733 static int
734 view_split_begin_y(int lines)
736 return lines * HSPLIT_SCALE;
739 static const struct got_error *view_resize(struct tog_view *);
741 static const struct got_error *
742 view_splitscreen(struct tog_view *view)
744 const struct got_error *err = NULL;
746 if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
747 if (view->resized_y && view->resized_y < view->lines)
748 view->begin_y = view->resized_y;
749 else
750 view->begin_y = view_split_begin_y(view->nlines);
751 view->begin_x = 0;
752 } else if (!view->resized) {
753 if (view->resized_x && view->resized_x < view->cols - 1 &&
754 view->cols > 119)
755 view->begin_x = view->resized_x;
756 else
757 view->begin_x = view_split_begin_x(0);
758 view->begin_y = 0;
760 view->nlines = LINES - view->begin_y;
761 view->ncols = COLS - view->begin_x;
762 view->lines = LINES;
763 view->cols = COLS;
764 err = view_resize(view);
765 if (err)
766 return err;
768 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
769 view->parent->nlines = view->begin_y;
771 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
772 return got_error_from_errno("mvwin");
774 return NULL;
777 static const struct got_error *
778 view_fullscreen(struct tog_view *view)
780 const struct got_error *err = NULL;
782 view->begin_x = 0;
783 view->begin_y = view->resized ? view->begin_y : 0;
784 view->nlines = view->resized ? view->nlines : LINES;
785 view->ncols = COLS;
786 view->lines = LINES;
787 view->cols = COLS;
788 err = view_resize(view);
789 if (err)
790 return err;
792 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
793 return got_error_from_errno("mvwin");
795 return NULL;
798 static int
799 view_is_parent_view(struct tog_view *view)
801 return view->parent == NULL;
804 static int
805 view_is_splitscreen(struct tog_view *view)
807 return view->begin_x > 0 || view->begin_y > 0;
810 static int
811 view_is_fullscreen(struct tog_view *view)
813 return view->nlines == LINES && view->ncols == COLS;
816 static int
817 view_is_hsplit_top(struct tog_view *view)
819 return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
820 view_is_splitscreen(view->child);
823 static void
824 view_border(struct tog_view *view)
826 PANEL *panel;
827 const struct tog_view *view_above;
829 if (view->parent)
830 return view_border(view->parent);
832 panel = panel_above(view->panel);
833 if (panel == NULL)
834 return;
836 view_above = panel_userptr(panel);
837 if (view->mode == TOG_VIEW_SPLIT_HRZN)
838 mvwhline(view->window, view_above->begin_y - 1,
839 view->begin_x, got_locale_is_utf8() ?
840 ACS_HLINE : '-', view->ncols);
841 else
842 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
843 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
846 static const struct got_error *view_init_hsplit(struct tog_view *, int);
847 static const struct got_error *request_log_commits(struct tog_view *);
848 static const struct got_error *offset_selection_down(struct tog_view *);
849 static void offset_selection_up(struct tog_view *);
850 static void view_get_split(struct tog_view *, int *, int *);
852 static const struct got_error *
853 view_resize(struct tog_view *view)
855 const struct got_error *err = NULL;
856 int dif, nlines, ncols;
858 dif = LINES - view->lines; /* line difference */
860 if (view->lines > LINES)
861 nlines = view->nlines - (view->lines - LINES);
862 else
863 nlines = view->nlines + (LINES - view->lines);
864 if (view->cols > COLS)
865 ncols = view->ncols - (view->cols - COLS);
866 else
867 ncols = view->ncols + (COLS - view->cols);
869 if (view->child) {
870 int hs = view->child->begin_y;
872 if (!view_is_fullscreen(view))
873 view->child->begin_x = view_split_begin_x(view->begin_x);
874 if (view->mode == TOG_VIEW_SPLIT_HRZN ||
875 view->child->begin_x == 0) {
876 ncols = COLS;
878 view_fullscreen(view->child);
879 if (view->child->focussed)
880 show_panel(view->child->panel);
881 else
882 show_panel(view->panel);
883 } else {
884 ncols = view->child->begin_x;
886 view_splitscreen(view->child);
887 show_panel(view->child->panel);
889 /*
890 * XXX This is ugly and needs to be moved into the above
891 * logic but "works" for now and my attempts at moving it
892 * break either 'tab' or 'F' key maps in horizontal splits.
893 */
894 if (hs) {
895 err = view_splitscreen(view->child);
896 if (err)
897 return err;
898 if (dif < 0) { /* top split decreased */
899 err = offset_selection_down(view);
900 if (err)
901 return err;
903 view_border(view);
904 update_panels();
905 doupdate();
906 show_panel(view->child->panel);
907 nlines = view->nlines;
909 } else if (view->parent == NULL)
910 ncols = COLS;
912 if (view->resize && dif > 0) {
913 err = view->resize(view, dif);
914 if (err)
915 return err;
918 if (wresize(view->window, nlines, ncols) == ERR)
919 return got_error_from_errno("wresize");
920 if (replace_panel(view->panel, view->window) == ERR)
921 return got_error_from_errno("replace_panel");
922 wclear(view->window);
924 view->nlines = nlines;
925 view->ncols = ncols;
926 view->lines = LINES;
927 view->cols = COLS;
929 return NULL;
932 static const struct got_error *
933 resize_log_view(struct tog_view *view, int increase)
935 struct tog_log_view_state *s = &view->state.log;
936 const struct got_error *err = NULL;
937 int n = 0;
939 if (s->selected_entry)
940 n = s->selected_entry->idx + view->lines - s->selected;
942 /*
943 * Request commits to account for the increased
944 * height so we have enough to populate the view.
945 */
946 if (s->commits->ncommits < n) {
947 view->nscrolled = n - s->commits->ncommits + increase + 1;
948 err = request_log_commits(view);
951 return err;
954 static void
955 view_adjust_offset(struct tog_view *view, int n)
957 if (n == 0)
958 return;
960 if (view->parent && view->parent->offset) {
961 if (view->parent->offset + n >= 0)
962 view->parent->offset += n;
963 else
964 view->parent->offset = 0;
965 } else if (view->offset) {
966 if (view->offset - n >= 0)
967 view->offset -= n;
968 else
969 view->offset = 0;
973 static const struct got_error *
974 view_resize_split(struct tog_view *view, int resize)
976 const struct got_error *err = NULL;
977 struct tog_view *v = NULL;
979 if (view->parent)
980 v = view->parent;
981 else
982 v = view;
984 if (!v->child || !view_is_splitscreen(v->child))
985 return NULL;
987 v->resized = v->child->resized = resize; /* lock for resize event */
989 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
990 if (v->child->resized_y)
991 v->child->begin_y = v->child->resized_y;
992 if (view->parent)
993 v->child->begin_y -= resize;
994 else
995 v->child->begin_y += resize;
996 if (v->child->begin_y < 3) {
997 view->count = 0;
998 v->child->begin_y = 3;
999 } else if (v->child->begin_y > LINES - 1) {
1000 view->count = 0;
1001 v->child->begin_y = LINES - 1;
1003 v->ncols = COLS;
1004 v->child->ncols = COLS;
1005 view_adjust_offset(view, resize);
1006 err = view_init_hsplit(v, v->child->begin_y);
1007 if (err)
1008 return err;
1009 v->child->resized_y = v->child->begin_y;
1010 } else {
1011 if (v->child->resized_x)
1012 v->child->begin_x = v->child->resized_x;
1013 if (view->parent)
1014 v->child->begin_x -= resize;
1015 else
1016 v->child->begin_x += resize;
1017 if (v->child->begin_x < 11) {
1018 view->count = 0;
1019 v->child->begin_x = 11;
1020 } else if (v->child->begin_x > COLS - 1) {
1021 view->count = 0;
1022 v->child->begin_x = COLS - 1;
1024 v->child->resized_x = v->child->begin_x;
1027 v->child->mode = v->mode;
1028 v->child->nlines = v->lines - v->child->begin_y;
1029 v->child->ncols = v->cols - v->child->begin_x;
1030 v->focus_child = 1;
1032 err = view_fullscreen(v);
1033 if (err)
1034 return err;
1035 err = view_splitscreen(v->child);
1036 if (err)
1037 return err;
1039 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1040 err = offset_selection_down(v->child);
1041 if (err)
1042 return err;
1045 if (v->resize)
1046 err = v->resize(v, 0);
1047 else if (v->child->resize)
1048 err = v->child->resize(v->child, 0);
1050 v->resized = v->child->resized = 0;
1052 return err;
1055 static void
1056 view_transfer_size(struct tog_view *dst, struct tog_view *src)
1058 struct tog_view *v = src->child ? src->child : src;
1060 dst->resized_x = v->resized_x;
1061 dst->resized_y = v->resized_y;
1064 static const struct got_error *
1065 view_close_child(struct tog_view *view)
1067 const struct got_error *err = NULL;
1069 if (view->child == NULL)
1070 return NULL;
1072 err = view_close(view->child);
1073 view->child = NULL;
1074 return err;
1077 static const struct got_error *
1078 view_set_child(struct tog_view *view, struct tog_view *child)
1080 const struct got_error *err = NULL;
1082 view->child = child;
1083 child->parent = view;
1085 err = view_resize(view);
1086 if (err)
1087 return err;
1089 if (view->child->resized_x || view->child->resized_y)
1090 err = view_resize_split(view, 0);
1092 return err;
1095 static const struct got_error *view_dispatch_request(struct tog_view **,
1096 struct tog_view *, enum tog_view_type, int, int);
1098 static const struct got_error *
1099 view_request_new(struct tog_view **requested, struct tog_view *view,
1100 enum tog_view_type request)
1102 struct tog_view *new_view = NULL;
1103 const struct got_error *err;
1104 int y = 0, x = 0;
1106 *requested = NULL;
1108 if (view_is_parent_view(view))
1109 view_get_split(view, &y, &x);
1111 err = view_dispatch_request(&new_view, view, request, y, x);
1112 if (err)
1113 return err;
1115 if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1116 err = view_init_hsplit(view, y);
1117 if (err)
1118 return err;
1121 view->focussed = 0;
1122 new_view->focussed = 1;
1123 new_view->mode = view->mode;
1124 new_view->nlines = view->lines - y;
1126 if (view_is_parent_view(view)) {
1127 view_transfer_size(new_view, view);
1128 err = view_close_child(view);
1129 if (err)
1130 return err;
1131 err = view_set_child(view, new_view);
1132 if (err)
1133 return err;
1134 view->focus_child = 1;
1135 } else
1136 *requested = new_view;
1138 return NULL;
1141 static void
1142 tog_resizeterm(void)
1144 int cols, lines;
1145 struct winsize size;
1147 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1148 cols = 80; /* Default */
1149 lines = 24;
1150 } else {
1151 cols = size.ws_col;
1152 lines = size.ws_row;
1154 resize_term(lines, cols);
1157 static const struct got_error *
1158 view_search_start(struct tog_view *view)
1160 const struct got_error *err = NULL;
1161 struct tog_view *v = view;
1162 char pattern[1024];
1163 int ret;
1165 if (view->search_started) {
1166 regfree(&view->regex);
1167 view->searching = 0;
1168 memset(&view->regmatch, 0, sizeof(view->regmatch));
1170 view->search_started = 0;
1172 if (view->nlines < 1)
1173 return NULL;
1175 if (view_is_hsplit_top(view))
1176 v = view->child;
1178 mvwaddstr(v->window, v->nlines - 1, 0, "/");
1179 wclrtoeol(v->window);
1181 nodelay(view->window, FALSE); /* block for search term input */
1182 nocbreak();
1183 echo();
1184 ret = wgetnstr(v->window, pattern, sizeof(pattern));
1185 wrefresh(v->window);
1186 cbreak();
1187 noecho();
1188 nodelay(view->window, TRUE);
1189 if (ret == ERR)
1190 return NULL;
1192 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1193 err = view->search_start(view);
1194 if (err) {
1195 regfree(&view->regex);
1196 return err;
1198 view->search_started = 1;
1199 view->searching = TOG_SEARCH_FORWARD;
1200 view->search_next_done = 0;
1201 view->search_next(view);
1204 return NULL;
1207 /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1208 static const struct got_error *
1209 switch_split(struct tog_view *view)
1211 const struct got_error *err = NULL;
1212 struct tog_view *v = NULL;
1214 if (view->parent)
1215 v = view->parent;
1216 else
1217 v = view;
1219 if (v->mode == TOG_VIEW_SPLIT_HRZN)
1220 v->mode = TOG_VIEW_SPLIT_VERT;
1221 else
1222 v->mode = TOG_VIEW_SPLIT_HRZN;
1224 if (!v->child)
1225 return NULL;
1226 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1227 v->mode = TOG_VIEW_SPLIT_NONE;
1229 view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1230 if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1231 v->child->begin_y = v->child->resized_y;
1232 else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1233 v->child->begin_x = v->child->resized_x;
1236 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1237 v->ncols = COLS;
1238 v->child->ncols = COLS;
1239 v->child->nscrolled = LINES - v->child->nlines;
1241 err = view_init_hsplit(v, v->child->begin_y);
1242 if (err)
1243 return err;
1245 v->child->mode = v->mode;
1246 v->child->nlines = v->lines - v->child->begin_y;
1247 v->focus_child = 1;
1249 err = view_fullscreen(v);
1250 if (err)
1251 return err;
1252 err = view_splitscreen(v->child);
1253 if (err)
1254 return err;
1256 if (v->mode == TOG_VIEW_SPLIT_NONE)
1257 v->mode = TOG_VIEW_SPLIT_VERT;
1258 if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1259 err = offset_selection_down(v);
1260 if (err)
1261 return err;
1262 err = offset_selection_down(v->child);
1263 if (err)
1264 return err;
1265 } else {
1266 offset_selection_up(v);
1267 offset_selection_up(v->child);
1269 if (v->resize)
1270 err = v->resize(v, 0);
1271 else if (v->child->resize)
1272 err = v->child->resize(v->child, 0);
1274 return err;
1278 * Compute view->count from numeric input. Assign total to view->count and
1279 * return first non-numeric key entered.
1281 static int
1282 get_compound_key(struct tog_view *view, int c)
1284 struct tog_view *v = view;
1285 int x, n = 0;
1287 if (view_is_hsplit_top(view))
1288 v = view->child;
1289 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1290 v = view->parent;
1292 view->count = 0;
1293 cbreak(); /* block for input */
1294 nodelay(view->window, FALSE);
1295 wmove(v->window, v->nlines - 1, 0);
1296 wclrtoeol(v->window);
1297 waddch(v->window, ':');
1299 do {
1300 x = getcurx(v->window);
1301 if (x != ERR && x < view->ncols) {
1302 waddch(v->window, c);
1303 wrefresh(v->window);
1307 * Don't overflow. Max valid request should be the greatest
1308 * between the longest and total lines; cap at 10 million.
1310 if (n >= 9999999)
1311 n = 9999999;
1312 else
1313 n = n * 10 + (c - '0');
1314 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1316 if (c == 'G' || c == 'g') { /* nG key map */
1317 view->gline = view->hiline = n;
1318 n = 0;
1319 c = 0;
1322 /* Massage excessive or inapplicable values at the input handler. */
1323 view->count = n;
1325 return c;
1328 static const struct got_error *
1329 view_input(struct tog_view **new, int *done, struct tog_view *view,
1330 struct tog_view_list_head *views)
1332 const struct got_error *err = NULL;
1333 struct tog_view *v;
1334 int ch, errcode;
1336 *new = NULL;
1338 /* Clear "no matches" indicator. */
1339 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1340 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
1342 view->count = 0;
1345 if (view->searching && !view->search_next_done) {
1346 errcode = pthread_mutex_unlock(&tog_mutex);
1347 if (errcode)
1348 return got_error_set_errno(errcode,
1349 "pthread_mutex_unlock");
1350 sched_yield();
1351 errcode = pthread_mutex_lock(&tog_mutex);
1352 if (errcode)
1353 return got_error_set_errno(errcode,
1354 "pthread_mutex_lock");
1355 view->search_next(view);
1356 return NULL;
1359 /* Allow threads to make progress while we are waiting for input. */
1360 errcode = pthread_mutex_unlock(&tog_mutex);
1361 if (errcode)
1362 return got_error_set_errno(errcode, "pthread_mutex_unlock");
1363 /* If we have an unfinished count, let C-g or backspace abort. */
1364 if (view->count && --view->count) {
1365 cbreak();
1366 nodelay(view->window, TRUE);
1367 ch = wgetch(view->window);
1368 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1369 view->count = 0;
1370 else
1371 ch = view->ch;
1372 } else {
1373 ch = wgetch(view->window);
1374 if (ch >= '1' && ch <= '9')
1375 view->ch = ch = get_compound_key(view, ch);
1377 if (view->hiline && ch != ERR && ch != 0)
1378 view->hiline = 0; /* key pressed, clear line highlight */
1379 nodelay(view->window, TRUE);
1380 errcode = pthread_mutex_lock(&tog_mutex);
1381 if (errcode)
1382 return got_error_set_errno(errcode, "pthread_mutex_lock");
1384 if (tog_sigwinch_received || tog_sigcont_received) {
1385 tog_resizeterm();
1386 tog_sigwinch_received = 0;
1387 tog_sigcont_received = 0;
1388 TAILQ_FOREACH(v, views, entry) {
1389 err = view_resize(v);
1390 if (err)
1391 return err;
1392 err = v->input(new, v, KEY_RESIZE);
1393 if (err)
1394 return err;
1395 if (v->child) {
1396 err = view_resize(v->child);
1397 if (err)
1398 return err;
1399 err = v->child->input(new, v->child,
1400 KEY_RESIZE);
1401 if (err)
1402 return err;
1403 if (v->child->resized_x || v->child->resized_y) {
1404 err = view_resize_split(v, 0);
1405 if (err)
1406 return err;
1412 switch (ch) {
1413 case '\t':
1414 view->count = 0;
1415 if (view->child) {
1416 view->focussed = 0;
1417 view->child->focussed = 1;
1418 view->focus_child = 1;
1419 } else if (view->parent) {
1420 view->focussed = 0;
1421 view->parent->focussed = 1;
1422 view->parent->focus_child = 0;
1423 if (!view_is_splitscreen(view)) {
1424 if (view->parent->resize) {
1425 err = view->parent->resize(view->parent,
1426 0);
1427 if (err)
1428 return err;
1430 offset_selection_up(view->parent);
1431 err = view_fullscreen(view->parent);
1432 if (err)
1433 return err;
1436 break;
1437 case 'q':
1438 if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1439 if (view->parent->resize) {
1440 /* might need more commits to fill fullscreen */
1441 err = view->parent->resize(view->parent, 0);
1442 if (err)
1443 break;
1445 offset_selection_up(view->parent);
1447 err = view->input(new, view, ch);
1448 view->dying = 1;
1449 break;
1450 case 'Q':
1451 *done = 1;
1452 break;
1453 case 'F':
1454 view->count = 0;
1455 if (view_is_parent_view(view)) {
1456 if (view->child == NULL)
1457 break;
1458 if (view_is_splitscreen(view->child)) {
1459 view->focussed = 0;
1460 view->child->focussed = 1;
1461 err = view_fullscreen(view->child);
1462 } else {
1463 err = view_splitscreen(view->child);
1464 if (!err)
1465 err = view_resize_split(view, 0);
1467 if (err)
1468 break;
1469 err = view->child->input(new, view->child,
1470 KEY_RESIZE);
1471 } else {
1472 if (view_is_splitscreen(view)) {
1473 view->parent->focussed = 0;
1474 view->focussed = 1;
1475 err = view_fullscreen(view);
1476 } else {
1477 err = view_splitscreen(view);
1478 if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1479 err = view_resize(view->parent);
1480 if (!err)
1481 err = view_resize_split(view, 0);
1483 if (err)
1484 break;
1485 err = view->input(new, view, KEY_RESIZE);
1487 if (err)
1488 break;
1489 if (view->resize) {
1490 err = view->resize(view, 0);
1491 if (err)
1492 break;
1494 if (view->parent)
1495 err = offset_selection_down(view->parent);
1496 if (!err)
1497 err = offset_selection_down(view);
1498 break;
1499 case 'S':
1500 view->count = 0;
1501 err = switch_split(view);
1502 break;
1503 case '-':
1504 err = view_resize_split(view, -1);
1505 break;
1506 case '+':
1507 err = view_resize_split(view, 1);
1508 break;
1509 case KEY_RESIZE:
1510 break;
1511 case '/':
1512 view->count = 0;
1513 if (view->search_start)
1514 view_search_start(view);
1515 else
1516 err = view->input(new, view, ch);
1517 break;
1518 case 'N':
1519 case 'n':
1520 if (view->search_started && view->search_next) {
1521 view->searching = (ch == 'n' ?
1522 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1523 view->search_next_done = 0;
1524 view->search_next(view);
1525 } else
1526 err = view->input(new, view, ch);
1527 break;
1528 case 'A':
1529 if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1530 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1531 else
1532 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1533 TAILQ_FOREACH(v, views, entry) {
1534 if (v->reset) {
1535 err = v->reset(v);
1536 if (err)
1537 return err;
1539 if (v->child && v->child->reset) {
1540 err = v->child->reset(v->child);
1541 if (err)
1542 return err;
1545 break;
1546 default:
1547 err = view->input(new, view, ch);
1548 break;
1551 return err;
1554 static int
1555 view_needs_focus_indication(struct tog_view *view)
1557 if (view_is_parent_view(view)) {
1558 if (view->child == NULL || view->child->focussed)
1559 return 0;
1560 if (!view_is_splitscreen(view->child))
1561 return 0;
1562 } else if (!view_is_splitscreen(view))
1563 return 0;
1565 return view->focussed;
1568 static const struct got_error *
1569 view_loop(struct tog_view *view)
1571 const struct got_error *err = NULL;
1572 struct tog_view_list_head views;
1573 struct tog_view *new_view;
1574 char *mode;
1575 int fast_refresh = 10;
1576 int done = 0, errcode;
1578 mode = getenv("TOG_VIEW_SPLIT_MODE");
1579 if (!mode || !(*mode == 'h' || *mode == 'H'))
1580 view->mode = TOG_VIEW_SPLIT_VERT;
1581 else
1582 view->mode = TOG_VIEW_SPLIT_HRZN;
1584 errcode = pthread_mutex_lock(&tog_mutex);
1585 if (errcode)
1586 return got_error_set_errno(errcode, "pthread_mutex_lock");
1588 TAILQ_INIT(&views);
1589 TAILQ_INSERT_HEAD(&views, view, entry);
1591 view->focussed = 1;
1592 err = view->show(view);
1593 if (err)
1594 return err;
1595 update_panels();
1596 doupdate();
1597 while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1598 !tog_fatal_signal_received()) {
1599 /* Refresh fast during initialization, then become slower. */
1600 if (fast_refresh && fast_refresh-- == 0)
1601 halfdelay(10); /* switch to once per second */
1603 err = view_input(&new_view, &done, view, &views);
1604 if (err)
1605 break;
1606 if (view->dying) {
1607 struct tog_view *v, *prev = NULL;
1609 if (view_is_parent_view(view))
1610 prev = TAILQ_PREV(view, tog_view_list_head,
1611 entry);
1612 else if (view->parent)
1613 prev = view->parent;
1615 if (view->parent) {
1616 view->parent->child = NULL;
1617 view->parent->focus_child = 0;
1618 /* Restore fullscreen line height. */
1619 view->parent->nlines = view->parent->lines;
1620 err = view_resize(view->parent);
1621 if (err)
1622 break;
1623 /* Make resized splits persist. */
1624 view_transfer_size(view->parent, view);
1625 } else
1626 TAILQ_REMOVE(&views, view, entry);
1628 err = view_close(view);
1629 if (err)
1630 goto done;
1632 view = NULL;
1633 TAILQ_FOREACH(v, &views, entry) {
1634 if (v->focussed)
1635 break;
1637 if (view == NULL && new_view == NULL) {
1638 /* No view has focus. Try to pick one. */
1639 if (prev)
1640 view = prev;
1641 else if (!TAILQ_EMPTY(&views)) {
1642 view = TAILQ_LAST(&views,
1643 tog_view_list_head);
1645 if (view) {
1646 if (view->focus_child) {
1647 view->child->focussed = 1;
1648 view = view->child;
1649 } else
1650 view->focussed = 1;
1654 if (new_view) {
1655 struct tog_view *v, *t;
1656 /* Only allow one parent view per type. */
1657 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1658 if (v->type != new_view->type)
1659 continue;
1660 TAILQ_REMOVE(&views, v, entry);
1661 err = view_close(v);
1662 if (err)
1663 goto done;
1664 break;
1666 TAILQ_INSERT_TAIL(&views, new_view, entry);
1667 view = new_view;
1669 if (view) {
1670 if (view_is_parent_view(view)) {
1671 if (view->child && view->child->focussed)
1672 view = view->child;
1673 } else {
1674 if (view->parent && view->parent->focussed)
1675 view = view->parent;
1677 show_panel(view->panel);
1678 if (view->child && view_is_splitscreen(view->child))
1679 show_panel(view->child->panel);
1680 if (view->parent && view_is_splitscreen(view)) {
1681 err = view->parent->show(view->parent);
1682 if (err)
1683 goto done;
1685 err = view->show(view);
1686 if (err)
1687 goto done;
1688 if (view->child) {
1689 err = view->child->show(view->child);
1690 if (err)
1691 goto done;
1693 update_panels();
1694 doupdate();
1697 done:
1698 while (!TAILQ_EMPTY(&views)) {
1699 const struct got_error *close_err;
1700 view = TAILQ_FIRST(&views);
1701 TAILQ_REMOVE(&views, view, entry);
1702 close_err = view_close(view);
1703 if (close_err && err == NULL)
1704 err = close_err;
1707 errcode = pthread_mutex_unlock(&tog_mutex);
1708 if (errcode && err == NULL)
1709 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1711 return err;
1714 __dead static void
1715 usage_log(void)
1717 endwin();
1718 fprintf(stderr,
1719 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1720 getprogname());
1721 exit(1);
1724 /* Create newly allocated wide-character string equivalent to a byte string. */
1725 static const struct got_error *
1726 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1728 char *vis = NULL;
1729 const struct got_error *err = NULL;
1731 *ws = NULL;
1732 *wlen = mbstowcs(NULL, s, 0);
1733 if (*wlen == (size_t)-1) {
1734 int vislen;
1735 if (errno != EILSEQ)
1736 return got_error_from_errno("mbstowcs");
1738 /* byte string invalid in current encoding; try to "fix" it */
1739 err = got_mbsavis(&vis, &vislen, s);
1740 if (err)
1741 return err;
1742 *wlen = mbstowcs(NULL, vis, 0);
1743 if (*wlen == (size_t)-1) {
1744 err = got_error_from_errno("mbstowcs"); /* give up */
1745 goto done;
1749 *ws = calloc(*wlen + 1, sizeof(**ws));
1750 if (*ws == NULL) {
1751 err = got_error_from_errno("calloc");
1752 goto done;
1755 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1756 err = got_error_from_errno("mbstowcs");
1757 done:
1758 free(vis);
1759 if (err) {
1760 free(*ws);
1761 *ws = NULL;
1762 *wlen = 0;
1764 return err;
1767 static const struct got_error *
1768 expand_tab(char **ptr, const char *src)
1770 char *dst;
1771 size_t len, n, idx = 0, sz = 0;
1773 *ptr = NULL;
1774 n = len = strlen(src);
1775 dst = malloc(n + 1);
1776 if (dst == NULL)
1777 return got_error_from_errno("malloc");
1779 while (idx < len && src[idx]) {
1780 const char c = src[idx];
1782 if (c == '\t') {
1783 size_t nb = TABSIZE - sz % TABSIZE;
1784 char *p;
1786 p = realloc(dst, n + nb);
1787 if (p == NULL) {
1788 free(dst);
1789 return got_error_from_errno("realloc");
1792 dst = p;
1793 n += nb;
1794 memset(dst + sz, ' ', nb);
1795 sz += nb;
1796 } else
1797 dst[sz++] = src[idx];
1798 ++idx;
1801 dst[sz] = '\0';
1802 *ptr = dst;
1803 return NULL;
1807 * Advance at most n columns from wline starting at offset off.
1808 * Return the index to the first character after the span operation.
1809 * Return the combined column width of all spanned wide character in
1810 * *rcol.
1812 static int
1813 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1815 int width, i, cols = 0;
1817 if (n == 0) {
1818 *rcol = cols;
1819 return off;
1822 for (i = off; wline[i] != L'\0'; ++i) {
1823 if (wline[i] == L'\t')
1824 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1825 else
1826 width = wcwidth(wline[i]);
1828 if (width == -1) {
1829 width = 1;
1830 wline[i] = L'.';
1833 if (cols + width > n)
1834 break;
1835 cols += width;
1838 *rcol = cols;
1839 return i;
1843 * Format a line for display, ensuring that it won't overflow a width limit.
1844 * With scrolling, the width returned refers to the scrolled version of the
1845 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1847 static const struct got_error *
1848 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1849 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1851 const struct got_error *err = NULL;
1852 int cols;
1853 wchar_t *wline = NULL;
1854 char *exstr = NULL;
1855 size_t wlen;
1856 int i, scrollx;
1858 *wlinep = NULL;
1859 *widthp = 0;
1861 if (expand) {
1862 err = expand_tab(&exstr, line);
1863 if (err)
1864 return err;
1867 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1868 free(exstr);
1869 if (err)
1870 return err;
1872 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1874 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1875 wline[wlen - 1] = L'\0';
1876 wlen--;
1878 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1879 wline[wlen - 1] = L'\0';
1880 wlen--;
1883 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1884 wline[i] = L'\0';
1886 if (widthp)
1887 *widthp = cols;
1888 if (scrollxp)
1889 *scrollxp = scrollx;
1890 if (err)
1891 free(wline);
1892 else
1893 *wlinep = wline;
1894 return err;
1897 static const struct got_error*
1898 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1899 struct got_object_id *id, struct got_repository *repo)
1901 static const struct got_error *err = NULL;
1902 struct got_reflist_entry *re;
1903 char *s;
1904 const char *name;
1906 *refs_str = NULL;
1908 TAILQ_FOREACH(re, refs, entry) {
1909 struct got_tag_object *tag = NULL;
1910 struct got_object_id *ref_id;
1911 int cmp;
1913 name = got_ref_get_name(re->ref);
1914 if (strcmp(name, GOT_REF_HEAD) == 0)
1915 continue;
1916 if (strncmp(name, "refs/", 5) == 0)
1917 name += 5;
1918 if (strncmp(name, "got/", 4) == 0 &&
1919 strncmp(name, "got/backup/", 11) != 0)
1920 continue;
1921 if (strncmp(name, "heads/", 6) == 0)
1922 name += 6;
1923 if (strncmp(name, "remotes/", 8) == 0) {
1924 name += 8;
1925 s = strstr(name, "/" GOT_REF_HEAD);
1926 if (s != NULL && s[strlen(s)] == '\0')
1927 continue;
1929 err = got_ref_resolve(&ref_id, repo, re->ref);
1930 if (err)
1931 break;
1932 if (strncmp(name, "tags/", 5) == 0) {
1933 err = got_object_open_as_tag(&tag, repo, ref_id);
1934 if (err) {
1935 if (err->code != GOT_ERR_OBJ_TYPE) {
1936 free(ref_id);
1937 break;
1939 /* Ref points at something other than a tag. */
1940 err = NULL;
1941 tag = NULL;
1944 cmp = got_object_id_cmp(tag ?
1945 got_object_tag_get_object_id(tag) : ref_id, id);
1946 free(ref_id);
1947 if (tag)
1948 got_object_tag_close(tag);
1949 if (cmp != 0)
1950 continue;
1951 s = *refs_str;
1952 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1953 s ? ", " : "", name) == -1) {
1954 err = got_error_from_errno("asprintf");
1955 free(s);
1956 *refs_str = NULL;
1957 break;
1959 free(s);
1962 return err;
1965 static const struct got_error *
1966 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1967 int col_tab_align)
1969 char *smallerthan;
1971 smallerthan = strchr(author, '<');
1972 if (smallerthan && smallerthan[1] != '\0')
1973 author = smallerthan + 1;
1974 author[strcspn(author, "@>")] = '\0';
1975 return format_line(wauthor, author_width, NULL, author, 0, limit,
1976 col_tab_align, 0);
1979 static const struct got_error *
1980 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1981 struct got_object_id *id, const size_t date_display_cols,
1982 int author_display_cols)
1984 struct tog_log_view_state *s = &view->state.log;
1985 const struct got_error *err = NULL;
1986 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1987 char *logmsg0 = NULL, *logmsg = NULL;
1988 char *author = NULL;
1989 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1990 int author_width, logmsg_width;
1991 char *newline, *line = NULL;
1992 int col, limit, scrollx;
1993 const int avail = view->ncols;
1994 struct tm tm;
1995 time_t committer_time;
1996 struct tog_color *tc;
1998 committer_time = got_object_commit_get_committer_time(commit);
1999 if (gmtime_r(&committer_time, &tm) == NULL)
2000 return got_error_from_errno("gmtime_r");
2001 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2002 return got_error(GOT_ERR_NO_SPACE);
2004 if (avail <= date_display_cols)
2005 limit = MIN(sizeof(datebuf) - 1, avail);
2006 else
2007 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2008 tc = get_color(&s->colors, TOG_COLOR_DATE);
2009 if (tc)
2010 wattr_on(view->window,
2011 COLOR_PAIR(tc->colorpair), NULL);
2012 waddnstr(view->window, datebuf, limit);
2013 if (tc)
2014 wattr_off(view->window,
2015 COLOR_PAIR(tc->colorpair), NULL);
2016 col = limit;
2017 if (col > avail)
2018 goto done;
2020 if (avail >= 120) {
2021 char *id_str;
2022 err = got_object_id_str(&id_str, id);
2023 if (err)
2024 goto done;
2025 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2026 if (tc)
2027 wattr_on(view->window,
2028 COLOR_PAIR(tc->colorpair), NULL);
2029 wprintw(view->window, "%.8s ", id_str);
2030 if (tc)
2031 wattr_off(view->window,
2032 COLOR_PAIR(tc->colorpair), NULL);
2033 free(id_str);
2034 col += 9;
2035 if (col > avail)
2036 goto done;
2039 if (s->use_committer)
2040 author = strdup(got_object_commit_get_committer(commit));
2041 else
2042 author = strdup(got_object_commit_get_author(commit));
2043 if (author == NULL) {
2044 err = got_error_from_errno("strdup");
2045 goto done;
2047 err = format_author(&wauthor, &author_width, author, avail - col, col);
2048 if (err)
2049 goto done;
2050 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2051 if (tc)
2052 wattr_on(view->window,
2053 COLOR_PAIR(tc->colorpair), NULL);
2054 waddwstr(view->window, wauthor);
2055 col += author_width;
2056 while (col < avail && author_width < author_display_cols + 2) {
2057 waddch(view->window, ' ');
2058 col++;
2059 author_width++;
2061 if (tc)
2062 wattr_off(view->window,
2063 COLOR_PAIR(tc->colorpair), NULL);
2064 if (col > avail)
2065 goto done;
2067 err = got_object_commit_get_logmsg(&logmsg0, commit);
2068 if (err)
2069 goto done;
2070 logmsg = logmsg0;
2071 while (*logmsg == '\n')
2072 logmsg++;
2073 newline = strchr(logmsg, '\n');
2074 if (newline)
2075 *newline = '\0';
2076 limit = avail - col;
2077 if (view->child && !view_is_hsplit_top(view) && limit > 0)
2078 limit--; /* for the border */
2079 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2080 limit, col, 1);
2081 if (err)
2082 goto done;
2083 waddwstr(view->window, &wlogmsg[scrollx]);
2084 col += MAX(logmsg_width, 0);
2085 while (col < avail) {
2086 waddch(view->window, ' ');
2087 col++;
2089 done:
2090 free(logmsg0);
2091 free(wlogmsg);
2092 free(author);
2093 free(wauthor);
2094 free(line);
2095 return err;
2098 static struct commit_queue_entry *
2099 alloc_commit_queue_entry(struct got_commit_object *commit,
2100 struct got_object_id *id)
2102 struct commit_queue_entry *entry;
2103 struct got_object_id *dup;
2105 entry = calloc(1, sizeof(*entry));
2106 if (entry == NULL)
2107 return NULL;
2109 dup = got_object_id_dup(id);
2110 if (dup == NULL) {
2111 free(entry);
2112 return NULL;
2115 entry->id = dup;
2116 entry->commit = commit;
2117 return entry;
2120 static void
2121 pop_commit(struct commit_queue *commits)
2123 struct commit_queue_entry *entry;
2125 entry = TAILQ_FIRST(&commits->head);
2126 TAILQ_REMOVE(&commits->head, entry, entry);
2127 got_object_commit_close(entry->commit);
2128 commits->ncommits--;
2129 free(entry->id);
2130 free(entry);
2133 static void
2134 free_commits(struct commit_queue *commits)
2136 while (!TAILQ_EMPTY(&commits->head))
2137 pop_commit(commits);
2140 static const struct got_error *
2141 match_commit(int *have_match, struct got_object_id *id,
2142 struct got_commit_object *commit, regex_t *regex)
2144 const struct got_error *err = NULL;
2145 regmatch_t regmatch;
2146 char *id_str = NULL, *logmsg = NULL;
2148 *have_match = 0;
2150 err = got_object_id_str(&id_str, id);
2151 if (err)
2152 return err;
2154 err = got_object_commit_get_logmsg(&logmsg, commit);
2155 if (err)
2156 goto done;
2158 if (regexec(regex, got_object_commit_get_author(commit), 1,
2159 &regmatch, 0) == 0 ||
2160 regexec(regex, got_object_commit_get_committer(commit), 1,
2161 &regmatch, 0) == 0 ||
2162 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
2163 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2164 *have_match = 1;
2165 done:
2166 free(id_str);
2167 free(logmsg);
2168 return err;
2171 static const struct got_error *
2172 queue_commits(struct tog_log_thread_args *a)
2174 const struct got_error *err = NULL;
2177 * We keep all commits open throughout the lifetime of the log
2178 * view in order to avoid having to re-fetch commits from disk
2179 * while updating the display.
2181 do {
2182 struct got_object_id id;
2183 struct got_commit_object *commit;
2184 struct commit_queue_entry *entry;
2185 int limit_match = 0;
2186 int errcode;
2188 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2189 NULL, NULL);
2190 if (err)
2191 break;
2193 err = got_object_open_as_commit(&commit, a->repo, &id);
2194 if (err)
2195 break;
2196 entry = alloc_commit_queue_entry(commit, &id);
2197 if (entry == NULL) {
2198 err = got_error_from_errno("alloc_commit_queue_entry");
2199 break;
2202 errcode = pthread_mutex_lock(&tog_mutex);
2203 if (errcode) {
2204 err = got_error_set_errno(errcode,
2205 "pthread_mutex_lock");
2206 break;
2209 entry->idx = a->real_commits->ncommits;
2210 TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2211 a->real_commits->ncommits++;
2213 if (*a->limiting) {
2214 err = match_commit(&limit_match, &id, commit,
2215 a->limit_regex);
2216 if (err)
2217 break;
2219 if (limit_match) {
2220 struct commit_queue_entry *matched;
2222 matched = alloc_commit_queue_entry(
2223 entry->commit, entry->id);
2224 if (matched == NULL) {
2225 err = got_error_from_errno(
2226 "alloc_commit_queue_entry");
2227 break;
2230 err = got_object_commit_dup(&matched->commit,
2231 entry->commit);
2232 if (err)
2233 break;
2235 matched->idx = a->limit_commits->ncommits;
2236 TAILQ_INSERT_TAIL(&a->limit_commits->head,
2237 matched, entry);
2238 a->limit_commits->ncommits++;
2242 * This is how we signal log_thread() that we
2243 * have found a match, and that it should be
2244 * counted as a new entry for the view.
2246 a->limit_match = limit_match;
2249 if (*a->searching == TOG_SEARCH_FORWARD &&
2250 !*a->search_next_done) {
2251 int have_match;
2252 err = match_commit(&have_match, &id, commit, a->regex);
2253 if (err)
2254 break;
2256 if (*a->limiting) {
2257 if (limit_match && have_match)
2258 *a->search_next_done =
2259 TOG_SEARCH_HAVE_MORE;
2260 } else if (have_match)
2261 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2264 errcode = pthread_mutex_unlock(&tog_mutex);
2265 if (errcode && err == NULL)
2266 err = got_error_set_errno(errcode,
2267 "pthread_mutex_unlock");
2268 if (err)
2269 break;
2270 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2272 return err;
2275 static void
2276 select_commit(struct tog_log_view_state *s)
2278 struct commit_queue_entry *entry;
2279 int ncommits = 0;
2281 entry = s->first_displayed_entry;
2282 while (entry) {
2283 if (ncommits == s->selected) {
2284 s->selected_entry = entry;
2285 break;
2287 entry = TAILQ_NEXT(entry, entry);
2288 ncommits++;
2292 static const struct got_error *
2293 draw_commits(struct tog_view *view)
2295 const struct got_error *err = NULL;
2296 struct tog_log_view_state *s = &view->state.log;
2297 struct commit_queue_entry *entry = s->selected_entry;
2298 int limit = view->nlines;
2299 int width;
2300 int ncommits, author_cols = 4;
2301 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2302 char *refs_str = NULL;
2303 wchar_t *wline;
2304 struct tog_color *tc;
2305 static const size_t date_display_cols = 12;
2307 if (view_is_hsplit_top(view))
2308 --limit; /* account for border */
2310 if (s->selected_entry &&
2311 !(view->searching && view->search_next_done == 0)) {
2312 struct got_reflist_head *refs;
2313 err = got_object_id_str(&id_str, s->selected_entry->id);
2314 if (err)
2315 return err;
2316 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2317 s->selected_entry->id);
2318 if (refs) {
2319 err = build_refs_str(&refs_str, refs,
2320 s->selected_entry->id, s->repo);
2321 if (err)
2322 goto done;
2326 if (s->thread_args.commits_needed == 0)
2327 halfdelay(10); /* disable fast refresh */
2329 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2330 if (asprintf(&ncommits_str, " [%d/%d] %s",
2331 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2332 (view->searching && !view->search_next_done) ?
2333 "searching..." : "loading...") == -1) {
2334 err = got_error_from_errno("asprintf");
2335 goto done;
2337 } else {
2338 const char *search_str = NULL;
2339 const char *limit_str = NULL;
2341 if (view->searching) {
2342 if (view->search_next_done == TOG_SEARCH_NO_MORE)
2343 search_str = "no more matches";
2344 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2345 search_str = "no matches found";
2346 else if (!view->search_next_done)
2347 search_str = "searching...";
2350 if (s->limit_view && s->commits->ncommits == 0)
2351 limit_str = "no matches found";
2353 if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2354 entry ? entry->idx + 1 : 0, s->commits->ncommits,
2355 search_str ? search_str : (refs_str ? refs_str : ""),
2356 limit_str ? limit_str : "") == -1) {
2357 err = got_error_from_errno("asprintf");
2358 goto done;
2362 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2363 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2364 "........................................",
2365 s->in_repo_path, ncommits_str) == -1) {
2366 err = got_error_from_errno("asprintf");
2367 header = NULL;
2368 goto done;
2370 } else if (asprintf(&header, "commit %s%s",
2371 id_str ? id_str : "........................................",
2372 ncommits_str) == -1) {
2373 err = got_error_from_errno("asprintf");
2374 header = NULL;
2375 goto done;
2377 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2378 if (err)
2379 goto done;
2381 werase(view->window);
2383 if (view_needs_focus_indication(view))
2384 wstandout(view->window);
2385 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2386 if (tc)
2387 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2388 waddwstr(view->window, wline);
2389 while (width < view->ncols) {
2390 waddch(view->window, ' ');
2391 width++;
2393 if (tc)
2394 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2395 if (view_needs_focus_indication(view))
2396 wstandend(view->window);
2397 free(wline);
2398 if (limit <= 1)
2399 goto done;
2401 /* Grow author column size if necessary, and set view->maxx. */
2402 entry = s->first_displayed_entry;
2403 ncommits = 0;
2404 view->maxx = 0;
2405 while (entry) {
2406 struct got_commit_object *c = entry->commit;
2407 char *author, *eol, *msg, *msg0;
2408 wchar_t *wauthor, *wmsg;
2409 int width;
2410 if (ncommits >= limit - 1)
2411 break;
2412 if (s->use_committer)
2413 author = strdup(got_object_commit_get_committer(c));
2414 else
2415 author = strdup(got_object_commit_get_author(c));
2416 if (author == NULL) {
2417 err = got_error_from_errno("strdup");
2418 goto done;
2420 err = format_author(&wauthor, &width, author, COLS,
2421 date_display_cols);
2422 if (author_cols < width)
2423 author_cols = width;
2424 free(wauthor);
2425 free(author);
2426 if (err)
2427 goto done;
2428 err = got_object_commit_get_logmsg(&msg0, c);
2429 if (err)
2430 goto done;
2431 msg = msg0;
2432 while (*msg == '\n')
2433 ++msg;
2434 if ((eol = strchr(msg, '\n')))
2435 *eol = '\0';
2436 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2437 date_display_cols + author_cols, 0);
2438 if (err)
2439 goto done;
2440 view->maxx = MAX(view->maxx, width);
2441 free(msg0);
2442 free(wmsg);
2443 ncommits++;
2444 entry = TAILQ_NEXT(entry, entry);
2447 entry = s->first_displayed_entry;
2448 s->last_displayed_entry = s->first_displayed_entry;
2449 ncommits = 0;
2450 while (entry) {
2451 if (ncommits >= limit - 1)
2452 break;
2453 if (ncommits == s->selected)
2454 wstandout(view->window);
2455 err = draw_commit(view, entry->commit, entry->id,
2456 date_display_cols, author_cols);
2457 if (ncommits == s->selected)
2458 wstandend(view->window);
2459 if (err)
2460 goto done;
2461 ncommits++;
2462 s->last_displayed_entry = entry;
2463 entry = TAILQ_NEXT(entry, entry);
2466 view_border(view);
2467 done:
2468 free(id_str);
2469 free(refs_str);
2470 free(ncommits_str);
2471 free(header);
2472 return err;
2475 static void
2476 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2478 struct commit_queue_entry *entry;
2479 int nscrolled = 0;
2481 entry = TAILQ_FIRST(&s->commits->head);
2482 if (s->first_displayed_entry == entry)
2483 return;
2485 entry = s->first_displayed_entry;
2486 while (entry && nscrolled < maxscroll) {
2487 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2488 if (entry) {
2489 s->first_displayed_entry = entry;
2490 nscrolled++;
2495 static const struct got_error *
2496 trigger_log_thread(struct tog_view *view, int wait)
2498 struct tog_log_thread_args *ta = &view->state.log.thread_args;
2499 int errcode;
2501 halfdelay(1); /* fast refresh while loading commits */
2503 while (!ta->log_complete && !tog_thread_error &&
2504 (ta->commits_needed > 0 || ta->load_all)) {
2505 /* Wake the log thread. */
2506 errcode = pthread_cond_signal(&ta->need_commits);
2507 if (errcode)
2508 return got_error_set_errno(errcode,
2509 "pthread_cond_signal");
2512 * The mutex will be released while the view loop waits
2513 * in wgetch(), at which time the log thread will run.
2515 if (!wait)
2516 break;
2518 /* Display progress update in log view. */
2519 show_log_view(view);
2520 update_panels();
2521 doupdate();
2523 /* Wait right here while next commit is being loaded. */
2524 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2525 if (errcode)
2526 return got_error_set_errno(errcode,
2527 "pthread_cond_wait");
2529 /* Display progress update in log view. */
2530 show_log_view(view);
2531 update_panels();
2532 doupdate();
2535 return NULL;
2538 static const struct got_error *
2539 request_log_commits(struct tog_view *view)
2541 struct tog_log_view_state *state = &view->state.log;
2542 const struct got_error *err = NULL;
2544 if (state->thread_args.log_complete)
2545 return NULL;
2547 state->thread_args.commits_needed += view->nscrolled;
2548 err = trigger_log_thread(view, 1);
2549 view->nscrolled = 0;
2551 return err;
2554 static const struct got_error *
2555 log_scroll_down(struct tog_view *view, int maxscroll)
2557 struct tog_log_view_state *s = &view->state.log;
2558 const struct got_error *err = NULL;
2559 struct commit_queue_entry *pentry;
2560 int nscrolled = 0, ncommits_needed;
2562 if (s->last_displayed_entry == NULL)
2563 return NULL;
2565 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2566 if (s->commits->ncommits < ncommits_needed &&
2567 !s->thread_args.log_complete) {
2569 * Ask the log thread for required amount of commits.
2571 s->thread_args.commits_needed +=
2572 ncommits_needed - s->commits->ncommits;
2573 err = trigger_log_thread(view, 1);
2574 if (err)
2575 return err;
2578 do {
2579 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2580 if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2581 break;
2583 s->last_displayed_entry = pentry ?
2584 pentry : s->last_displayed_entry;;
2586 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2587 if (pentry == NULL)
2588 break;
2589 s->first_displayed_entry = pentry;
2590 } while (++nscrolled < maxscroll);
2592 if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2593 view->nscrolled += nscrolled;
2594 else
2595 view->nscrolled = 0;
2597 return err;
2600 static const struct got_error *
2601 open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2602 struct got_commit_object *commit, struct got_object_id *commit_id,
2603 struct tog_view *log_view, struct got_repository *repo)
2605 const struct got_error *err;
2606 struct got_object_qid *parent_id;
2607 struct tog_view *diff_view;
2609 diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2610 if (diff_view == NULL)
2611 return got_error_from_errno("view_open");
2613 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2614 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2615 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2616 if (err == NULL)
2617 *new_view = diff_view;
2618 return err;
2621 static const struct got_error *
2622 tree_view_visit_subtree(struct tog_tree_view_state *s,
2623 struct got_tree_object *subtree)
2625 struct tog_parent_tree *parent;
2627 parent = calloc(1, sizeof(*parent));
2628 if (parent == NULL)
2629 return got_error_from_errno("calloc");
2631 parent->tree = s->tree;
2632 parent->first_displayed_entry = s->first_displayed_entry;
2633 parent->selected_entry = s->selected_entry;
2634 parent->selected = s->selected;
2635 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2636 s->tree = subtree;
2637 s->selected = 0;
2638 s->first_displayed_entry = NULL;
2639 return NULL;
2642 static const struct got_error *
2643 tree_view_walk_path(struct tog_tree_view_state *s,
2644 struct got_commit_object *commit, const char *path)
2646 const struct got_error *err = NULL;
2647 struct got_tree_object *tree = NULL;
2648 const char *p;
2649 char *slash, *subpath = NULL;
2651 /* Walk the path and open corresponding tree objects. */
2652 p = path;
2653 while (*p) {
2654 struct got_tree_entry *te;
2655 struct got_object_id *tree_id;
2656 char *te_name;
2658 while (p[0] == '/')
2659 p++;
2661 /* Ensure the correct subtree entry is selected. */
2662 slash = strchr(p, '/');
2663 if (slash == NULL)
2664 te_name = strdup(p);
2665 else
2666 te_name = strndup(p, slash - p);
2667 if (te_name == NULL) {
2668 err = got_error_from_errno("strndup");
2669 break;
2671 te = got_object_tree_find_entry(s->tree, te_name);
2672 if (te == NULL) {
2673 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2674 free(te_name);
2675 break;
2677 free(te_name);
2678 s->first_displayed_entry = s->selected_entry = te;
2680 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2681 break; /* jump to this file's entry */
2683 slash = strchr(p, '/');
2684 if (slash)
2685 subpath = strndup(path, slash - path);
2686 else
2687 subpath = strdup(path);
2688 if (subpath == NULL) {
2689 err = got_error_from_errno("strdup");
2690 break;
2693 err = got_object_id_by_path(&tree_id, s->repo, commit,
2694 subpath);
2695 if (err)
2696 break;
2698 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2699 free(tree_id);
2700 if (err)
2701 break;
2703 err = tree_view_visit_subtree(s, tree);
2704 if (err) {
2705 got_object_tree_close(tree);
2706 break;
2708 if (slash == NULL)
2709 break;
2710 free(subpath);
2711 subpath = NULL;
2712 p = slash;
2715 free(subpath);
2716 return err;
2719 static const struct got_error *
2720 browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2721 struct commit_queue_entry *entry, const char *path,
2722 const char *head_ref_name, struct got_repository *repo)
2724 const struct got_error *err = NULL;
2725 struct tog_tree_view_state *s;
2726 struct tog_view *tree_view;
2728 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2729 if (tree_view == NULL)
2730 return got_error_from_errno("view_open");
2732 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2733 if (err)
2734 return err;
2735 s = &tree_view->state.tree;
2737 *new_view = tree_view;
2739 if (got_path_is_root_dir(path))
2740 return NULL;
2742 return tree_view_walk_path(s, entry->commit, path);
2745 static const struct got_error *
2746 block_signals_used_by_main_thread(void)
2748 sigset_t sigset;
2749 int errcode;
2751 if (sigemptyset(&sigset) == -1)
2752 return got_error_from_errno("sigemptyset");
2754 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2755 if (sigaddset(&sigset, SIGWINCH) == -1)
2756 return got_error_from_errno("sigaddset");
2757 if (sigaddset(&sigset, SIGCONT) == -1)
2758 return got_error_from_errno("sigaddset");
2759 if (sigaddset(&sigset, SIGINT) == -1)
2760 return got_error_from_errno("sigaddset");
2761 if (sigaddset(&sigset, SIGTERM) == -1)
2762 return got_error_from_errno("sigaddset");
2764 /* ncurses handles SIGTSTP */
2765 if (sigaddset(&sigset, SIGTSTP) == -1)
2766 return got_error_from_errno("sigaddset");
2768 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2769 if (errcode)
2770 return got_error_set_errno(errcode, "pthread_sigmask");
2772 return NULL;
2775 static void *
2776 log_thread(void *arg)
2778 const struct got_error *err = NULL;
2779 int errcode = 0;
2780 struct tog_log_thread_args *a = arg;
2781 int done = 0;
2784 * Sync startup with main thread such that we begin our
2785 * work once view_input() has released the mutex.
2787 errcode = pthread_mutex_lock(&tog_mutex);
2788 if (errcode) {
2789 err = got_error_set_errno(errcode, "pthread_mutex_lock");
2790 return (void *)err;
2793 err = block_signals_used_by_main_thread();
2794 if (err) {
2795 pthread_mutex_unlock(&tog_mutex);
2796 goto done;
2799 while (!done && !err && !tog_fatal_signal_received()) {
2800 errcode = pthread_mutex_unlock(&tog_mutex);
2801 if (errcode) {
2802 err = got_error_set_errno(errcode,
2803 "pthread_mutex_unlock");
2804 goto done;
2806 err = queue_commits(a);
2807 if (err) {
2808 if (err->code != GOT_ERR_ITER_COMPLETED)
2809 goto done;
2810 err = NULL;
2811 done = 1;
2812 } else if (a->commits_needed > 0 && !a->load_all) {
2813 if (*a->limiting) {
2814 if (a->limit_match)
2815 a->commits_needed--;
2816 } else
2817 a->commits_needed--;
2820 errcode = pthread_mutex_lock(&tog_mutex);
2821 if (errcode) {
2822 err = got_error_set_errno(errcode,
2823 "pthread_mutex_lock");
2824 goto done;
2825 } else if (*a->quit)
2826 done = 1;
2827 else if (*a->limiting && *a->first_displayed_entry == NULL) {
2828 *a->first_displayed_entry =
2829 TAILQ_FIRST(&a->limit_commits->head);
2830 *a->selected_entry = *a->first_displayed_entry;
2831 } else if (*a->first_displayed_entry == NULL) {
2832 *a->first_displayed_entry =
2833 TAILQ_FIRST(&a->real_commits->head);
2834 *a->selected_entry = *a->first_displayed_entry;
2837 errcode = pthread_cond_signal(&a->commit_loaded);
2838 if (errcode) {
2839 err = got_error_set_errno(errcode,
2840 "pthread_cond_signal");
2841 pthread_mutex_unlock(&tog_mutex);
2842 goto done;
2845 if (done)
2846 a->commits_needed = 0;
2847 else {
2848 if (a->commits_needed == 0 && !a->load_all) {
2849 errcode = pthread_cond_wait(&a->need_commits,
2850 &tog_mutex);
2851 if (errcode) {
2852 err = got_error_set_errno(errcode,
2853 "pthread_cond_wait");
2854 pthread_mutex_unlock(&tog_mutex);
2855 goto done;
2857 if (*a->quit)
2858 done = 1;
2862 a->log_complete = 1;
2863 errcode = pthread_mutex_unlock(&tog_mutex);
2864 if (errcode)
2865 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2866 done:
2867 if (err) {
2868 tog_thread_error = 1;
2869 pthread_cond_signal(&a->commit_loaded);
2871 return (void *)err;
2874 static const struct got_error *
2875 stop_log_thread(struct tog_log_view_state *s)
2877 const struct got_error *err = NULL, *thread_err = NULL;
2878 int errcode;
2880 if (s->thread) {
2881 s->quit = 1;
2882 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2883 if (errcode)
2884 return got_error_set_errno(errcode,
2885 "pthread_cond_signal");
2886 errcode = pthread_mutex_unlock(&tog_mutex);
2887 if (errcode)
2888 return got_error_set_errno(errcode,
2889 "pthread_mutex_unlock");
2890 errcode = pthread_join(s->thread, (void **)&thread_err);
2891 if (errcode)
2892 return got_error_set_errno(errcode, "pthread_join");
2893 errcode = pthread_mutex_lock(&tog_mutex);
2894 if (errcode)
2895 return got_error_set_errno(errcode,
2896 "pthread_mutex_lock");
2897 s->thread = NULL;
2900 if (s->thread_args.repo) {
2901 err = got_repo_close(s->thread_args.repo);
2902 s->thread_args.repo = NULL;
2905 if (s->thread_args.pack_fds) {
2906 const struct got_error *pack_err =
2907 got_repo_pack_fds_close(s->thread_args.pack_fds);
2908 if (err == NULL)
2909 err = pack_err;
2910 s->thread_args.pack_fds = NULL;
2913 if (s->thread_args.graph) {
2914 got_commit_graph_close(s->thread_args.graph);
2915 s->thread_args.graph = NULL;
2918 return err ? err : thread_err;
2921 static const struct got_error *
2922 close_log_view(struct tog_view *view)
2924 const struct got_error *err = NULL;
2925 struct tog_log_view_state *s = &view->state.log;
2926 int errcode;
2928 err = stop_log_thread(s);
2930 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2931 if (errcode && err == NULL)
2932 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2934 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2935 if (errcode && err == NULL)
2936 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2938 free_commits(&s->limit_commits);
2939 free_commits(&s->real_commits);
2940 free(s->in_repo_path);
2941 s->in_repo_path = NULL;
2942 free(s->start_id);
2943 s->start_id = NULL;
2944 free(s->head_ref_name);
2945 s->head_ref_name = NULL;
2946 return err;
2950 * We use two queues to implement the limit feature: first consists of
2951 * commits matching the current limit_regex; second is the real queue
2952 * of all known commits (real_commits). When the user starts limiting,
2953 * we swap queues such that all movement and displaying functionality
2954 * works with very slight change.
2956 static const struct got_error *
2957 limit_log_view(struct tog_view *view)
2959 struct tog_log_view_state *s = &view->state.log;
2960 struct commit_queue_entry *entry;
2961 struct tog_view *v = view;
2962 const struct got_error *err = NULL;
2963 char pattern[1024];
2964 int ret;
2966 if (view_is_hsplit_top(view))
2967 v = view->child;
2968 else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2969 v = view->parent;
2971 /* Get the pattern */
2972 wmove(v->window, v->nlines - 1, 0);
2973 wclrtoeol(v->window);
2974 mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2975 nodelay(v->window, FALSE);
2976 nocbreak();
2977 echo();
2978 ret = wgetnstr(v->window, pattern, sizeof(pattern));
2979 cbreak();
2980 noecho();
2981 nodelay(v->window, TRUE);
2982 if (ret == ERR)
2983 return NULL;
2985 if (*pattern == '\0') {
2987 * Safety measure for the situation where the user
2988 * resets limit without previously limiting anything.
2990 if (!s->limit_view)
2991 return NULL;
2994 * User could have pressed Ctrl+L, which refreshed the
2995 * commit queues, it means we can't save previously
2996 * (before limit took place) displayed entries,
2997 * because they would point to already free'ed memory,
2998 * so we are forced to always select first entry of
2999 * the queue.
3001 s->commits = &s->real_commits;
3002 s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3003 s->selected_entry = s->first_displayed_entry;
3004 s->selected = 0;
3005 s->limit_view = 0;
3007 return NULL;
3010 if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3011 return NULL;
3013 s->limit_view = 1;
3015 /* Clear the screen while loading limit view */
3016 s->first_displayed_entry = NULL;
3017 s->last_displayed_entry = NULL;
3018 s->selected_entry = NULL;
3019 s->commits = &s->limit_commits;
3021 /* Prepare limit queue for new search */
3022 free_commits(&s->limit_commits);
3023 s->limit_commits.ncommits = 0;
3025 /* First process commits, which are in queue already */
3026 TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3027 int have_match = 0;
3029 err = match_commit(&have_match, entry->id,
3030 entry->commit, &s->limit_regex);
3031 if (err)
3032 return err;
3034 if (have_match) {
3035 struct commit_queue_entry *matched;
3037 matched = alloc_commit_queue_entry(entry->commit,
3038 entry->id);
3039 if (matched == NULL) {
3040 err = got_error_from_errno(
3041 "alloc_commit_queue_entry");
3042 break;
3045 err = got_object_commit_dup(&matched->commit,
3046 entry->commit);
3047 if (err) {
3048 free(matched);
3049 return err;
3052 matched->idx = s->limit_commits.ncommits;
3053 TAILQ_INSERT_TAIL(&s->limit_commits.head,
3054 matched, entry);
3055 s->limit_commits.ncommits++;
3059 /* Second process all the commits, until we fill the screen */
3060 if (s->limit_commits.ncommits < view->nlines - 1 &&
3061 !s->thread_args.log_complete) {
3062 s->thread_args.commits_needed +=
3063 view->nlines - s->limit_commits.ncommits - 1;
3064 err = trigger_log_thread(view, 1);
3065 if (err)
3066 return err;
3069 s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3070 s->selected_entry = TAILQ_FIRST(&s->commits->head);
3071 s->selected = 0;
3073 return NULL;
3076 static const struct got_error *
3077 search_start_log_view(struct tog_view *view)
3079 struct tog_log_view_state *s = &view->state.log;
3081 s->matched_entry = NULL;
3082 s->search_entry = NULL;
3083 return NULL;
3086 static const struct got_error *
3087 search_next_log_view(struct tog_view *view)
3089 const struct got_error *err = NULL;
3090 struct tog_log_view_state *s = &view->state.log;
3091 struct commit_queue_entry *entry;
3093 /* Display progress update in log view. */
3094 show_log_view(view);
3095 update_panels();
3096 doupdate();
3098 if (s->search_entry) {
3099 int errcode, ch;
3100 errcode = pthread_mutex_unlock(&tog_mutex);
3101 if (errcode)
3102 return got_error_set_errno(errcode,
3103 "pthread_mutex_unlock");
3104 ch = wgetch(view->window);
3105 errcode = pthread_mutex_lock(&tog_mutex);
3106 if (errcode)
3107 return got_error_set_errno(errcode,
3108 "pthread_mutex_lock");
3109 if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3110 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3111 return NULL;
3113 if (view->searching == TOG_SEARCH_FORWARD)
3114 entry = TAILQ_NEXT(s->search_entry, entry);
3115 else
3116 entry = TAILQ_PREV(s->search_entry,
3117 commit_queue_head, entry);
3118 } else if (s->matched_entry) {
3119 int matched_idx = s->matched_entry->idx;
3120 int selected_idx = s->selected_entry->idx;
3123 * If the user has moved the cursor after we hit a match,
3124 * the position from where we should continue searching
3125 * might have changed.
3127 if (view->searching == TOG_SEARCH_FORWARD) {
3128 if (matched_idx > selected_idx)
3129 entry = TAILQ_NEXT(s->selected_entry, entry);
3130 else
3131 entry = TAILQ_NEXT(s->matched_entry, entry);
3132 } else {
3133 if (matched_idx < selected_idx)
3134 entry = TAILQ_PREV(s->selected_entry,
3135 commit_queue_head, entry);
3136 else
3137 entry = TAILQ_PREV(s->matched_entry,
3138 commit_queue_head, entry);
3140 } else {
3141 entry = s->selected_entry;
3144 while (1) {
3145 int have_match = 0;
3147 if (entry == NULL) {
3148 if (s->thread_args.log_complete ||
3149 view->searching == TOG_SEARCH_BACKWARD) {
3150 view->search_next_done =
3151 (s->matched_entry == NULL ?
3152 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3153 s->search_entry = NULL;
3154 return NULL;
3157 * Poke the log thread for more commits and return,
3158 * allowing the main loop to make progress. Search
3159 * will resume at s->search_entry once we come back.
3161 s->thread_args.commits_needed++;
3162 return trigger_log_thread(view, 0);
3165 err = match_commit(&have_match, entry->id, entry->commit,
3166 &view->regex);
3167 if (err)
3168 break;
3169 if (have_match) {
3170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3171 s->matched_entry = entry;
3172 break;
3175 s->search_entry = entry;
3176 if (view->searching == TOG_SEARCH_FORWARD)
3177 entry = TAILQ_NEXT(entry, entry);
3178 else
3179 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3182 if (s->matched_entry) {
3183 int cur = s->selected_entry->idx;
3184 while (cur < s->matched_entry->idx) {
3185 err = input_log_view(NULL, view, KEY_DOWN);
3186 if (err)
3187 return err;
3188 cur++;
3190 while (cur > s->matched_entry->idx) {
3191 err = input_log_view(NULL, view, KEY_UP);
3192 if (err)
3193 return err;
3194 cur--;
3198 s->search_entry = NULL;
3200 return NULL;
3203 static const struct got_error *
3204 open_log_view(struct tog_view *view, struct got_object_id *start_id,
3205 struct got_repository *repo, const char *head_ref_name,
3206 const char *in_repo_path, int log_branches)
3208 const struct got_error *err = NULL;
3209 struct tog_log_view_state *s = &view->state.log;
3210 struct got_repository *thread_repo = NULL;
3211 struct got_commit_graph *thread_graph = NULL;
3212 int errcode;
3214 if (in_repo_path != s->in_repo_path) {
3215 free(s->in_repo_path);
3216 s->in_repo_path = strdup(in_repo_path);
3217 if (s->in_repo_path == NULL)
3218 return got_error_from_errno("strdup");
3221 /* The commit queue only contains commits being displayed. */
3222 TAILQ_INIT(&s->real_commits.head);
3223 s->real_commits.ncommits = 0;
3224 s->commits = &s->real_commits;
3226 TAILQ_INIT(&s->limit_commits.head);
3227 s->limit_view = 0;
3228 s->limit_commits.ncommits = 0;
3230 s->repo = repo;
3231 if (head_ref_name) {
3232 s->head_ref_name = strdup(head_ref_name);
3233 if (s->head_ref_name == NULL) {
3234 err = got_error_from_errno("strdup");
3235 goto done;
3238 s->start_id = got_object_id_dup(start_id);
3239 if (s->start_id == NULL) {
3240 err = got_error_from_errno("got_object_id_dup");
3241 goto done;
3243 s->log_branches = log_branches;
3245 STAILQ_INIT(&s->colors);
3246 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3247 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3248 get_color_value("TOG_COLOR_COMMIT"));
3249 if (err)
3250 goto done;
3251 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3252 get_color_value("TOG_COLOR_AUTHOR"));
3253 if (err) {
3254 free_colors(&s->colors);
3255 goto done;
3257 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3258 get_color_value("TOG_COLOR_DATE"));
3259 if (err) {
3260 free_colors(&s->colors);
3261 goto done;
3265 view->show = show_log_view;
3266 view->input = input_log_view;
3267 view->resize = resize_log_view;
3268 view->close = close_log_view;
3269 view->search_start = search_start_log_view;
3270 view->search_next = search_next_log_view;
3272 if (s->thread_args.pack_fds == NULL) {
3273 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3274 if (err)
3275 goto done;
3277 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3278 s->thread_args.pack_fds);
3279 if (err)
3280 goto done;
3281 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3282 !s->log_branches);
3283 if (err)
3284 goto done;
3285 err = got_commit_graph_iter_start(thread_graph, s->start_id,
3286 s->repo, NULL, NULL);
3287 if (err)
3288 goto done;
3290 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3291 if (errcode) {
3292 err = got_error_set_errno(errcode, "pthread_cond_init");
3293 goto done;
3295 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3296 if (errcode) {
3297 err = got_error_set_errno(errcode, "pthread_cond_init");
3298 goto done;
3301 s->thread_args.commits_needed = view->nlines;
3302 s->thread_args.graph = thread_graph;
3303 s->thread_args.real_commits = &s->real_commits;
3304 s->thread_args.limit_commits = &s->limit_commits;
3305 s->thread_args.in_repo_path = s->in_repo_path;
3306 s->thread_args.start_id = s->start_id;
3307 s->thread_args.repo = thread_repo;
3308 s->thread_args.log_complete = 0;
3309 s->thread_args.quit = &s->quit;
3310 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3311 s->thread_args.selected_entry = &s->selected_entry;
3312 s->thread_args.searching = &view->searching;
3313 s->thread_args.search_next_done = &view->search_next_done;
3314 s->thread_args.regex = &view->regex;
3315 s->thread_args.limiting = &s->limit_view;
3316 s->thread_args.limit_regex = &s->limit_regex;
3317 s->thread_args.limit_commits = &s->limit_commits;
3318 done:
3319 if (err)
3320 close_log_view(view);
3321 return err;
3324 static const struct got_error *
3325 show_log_view(struct tog_view *view)
3327 const struct got_error *err;
3328 struct tog_log_view_state *s = &view->state.log;
3330 if (s->thread == NULL) {
3331 int errcode = pthread_create(&s->thread, NULL, log_thread,
3332 &s->thread_args);
3333 if (errcode)
3334 return got_error_set_errno(errcode, "pthread_create");
3335 if (s->thread_args.commits_needed > 0) {
3336 err = trigger_log_thread(view, 1);
3337 if (err)
3338 return err;
3342 return draw_commits(view);
3345 static void
3346 log_move_cursor_up(struct tog_view *view, int page, int home)
3348 struct tog_log_view_state *s = &view->state.log;
3350 if (s->first_displayed_entry == NULL)
3351 return;
3352 if (s->selected_entry->idx == 0)
3353 view->count = 0;
3355 if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3356 || home)
3357 s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3359 if (!page && !home && s->selected > 0)
3360 --s->selected;
3361 else
3362 log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3364 select_commit(s);
3365 return;
3368 static const struct got_error *
3369 log_move_cursor_down(struct tog_view *view, int page)
3371 struct tog_log_view_state *s = &view->state.log;
3372 const struct got_error *err = NULL;
3373 int eos = view->nlines - 2;
3375 if (s->first_displayed_entry == NULL)
3376 return NULL;
3378 if (s->thread_args.log_complete &&
3379 s->selected_entry->idx >= s->commits->ncommits - 1)
3380 return NULL;
3382 if (view_is_hsplit_top(view))
3383 --eos; /* border consumes the last line */
3385 if (!page) {
3386 if (s->selected < MIN(eos, s->commits->ncommits - 1))
3387 ++s->selected;
3388 else
3389 err = log_scroll_down(view, 1);
3390 } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3391 struct commit_queue_entry *entry;
3392 int n;
3394 s->selected = 0;
3395 entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3396 s->last_displayed_entry = entry;
3397 for (n = 0; n <= eos; n++) {
3398 if (entry == NULL)
3399 break;
3400 s->first_displayed_entry = entry;
3401 entry = TAILQ_PREV(entry, commit_queue_head, entry);
3403 if (n > 0)
3404 s->selected = n - 1;
3405 } else {
3406 if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3407 s->thread_args.log_complete)
3408 s->selected += MIN(page,
3409 s->commits->ncommits - s->selected_entry->idx - 1);
3410 else
3411 err = log_scroll_down(view, page);
3413 if (err)
3414 return err;
3417 * We might necessarily overshoot in horizontal
3418 * splits; if so, select the last displayed commit.
3420 if (s->first_displayed_entry && s->last_displayed_entry) {
3421 s->selected = MIN(s->selected,
3422 s->last_displayed_entry->idx -
3423 s->first_displayed_entry->idx);
3426 select_commit(s);
3428 if (s->thread_args.log_complete &&
3429 s->selected_entry->idx == s->commits->ncommits - 1)
3430 view->count = 0;
3432 return NULL;
3435 static void
3436 view_get_split(struct tog_view *view, int *y, int *x)
3438 *x = 0;
3439 *y = 0;
3441 if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3442 if (view->child && view->child->resized_y)
3443 *y = view->child->resized_y;
3444 else if (view->resized_y)
3445 *y = view->resized_y;
3446 else
3447 *y = view_split_begin_y(view->lines);
3448 } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3449 if (view->child && view->child->resized_x)
3450 *x = view->child->resized_x;
3451 else if (view->resized_x)
3452 *x = view->resized_x;
3453 else
3454 *x = view_split_begin_x(view->begin_x);
3458 /* Split view horizontally at y and offset view->state->selected line. */
3459 static const struct got_error *
3460 view_init_hsplit(struct tog_view *view, int y)
3462 const struct got_error *err = NULL;
3464 view->nlines = y;
3465 view->ncols = COLS;
3466 err = view_resize(view);
3467 if (err)
3468 return err;
3470 err = offset_selection_down(view);
3472 return err;
3475 static const struct got_error *
3476 log_goto_line(struct tog_view *view, int nlines)
3478 const struct got_error *err = NULL;
3479 struct tog_log_view_state *s = &view->state.log;
3480 int g, idx = s->selected_entry->idx;
3482 if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3483 return NULL;
3485 g = view->gline;
3486 view->gline = 0;
3488 if (g >= s->first_displayed_entry->idx + 1 &&
3489 g <= s->last_displayed_entry->idx + 1 &&
3490 g - s->first_displayed_entry->idx - 1 < nlines) {
3491 s->selected = g - s->first_displayed_entry->idx - 1;
3492 select_commit(s);
3493 return NULL;
3496 if (idx + 1 < g) {
3497 err = log_move_cursor_down(view, g - idx - 1);
3498 if (!err && g > s->selected_entry->idx + 1)
3499 err = log_move_cursor_down(view,
3500 g - s->first_displayed_entry->idx - 1);
3501 if (err)
3502 return err;
3503 } else if (idx + 1 > g)
3504 log_move_cursor_up(view, idx - g + 1, 0);
3506 if (g < nlines && s->first_displayed_entry->idx == 0)
3507 s->selected = g - 1;
3509 select_commit(s);
3510 return NULL;
3514 static const struct got_error *
3515 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3517 const struct got_error *err = NULL;
3518 struct tog_log_view_state *s = &view->state.log;
3519 int eos, nscroll;
3521 if (s->thread_args.load_all) {
3522 if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3523 s->thread_args.load_all = 0;
3524 else if (s->thread_args.log_complete) {
3525 err = log_move_cursor_down(view, s->commits->ncommits);
3526 s->thread_args.load_all = 0;
3528 if (err)
3529 return err;
3532 eos = nscroll = view->nlines - 1;
3533 if (view_is_hsplit_top(view))
3534 --eos; /* border */
3536 if (view->gline)
3537 return log_goto_line(view, eos);
3539 switch (ch) {
3540 case '&':
3541 err = limit_log_view(view);
3542 break;
3543 case 'q':
3544 s->quit = 1;
3545 break;
3546 case '0':
3547 view->x = 0;
3548 break;
3549 case '$':
3550 view->x = MAX(view->maxx - view->ncols / 2, 0);
3551 view->count = 0;
3552 break;
3553 case KEY_RIGHT:
3554 case 'l':
3555 if (view->x + view->ncols / 2 < view->maxx)
3556 view->x += 2; /* move two columns right */
3557 else
3558 view->count = 0;
3559 break;
3560 case KEY_LEFT:
3561 case 'h':
3562 view->x -= MIN(view->x, 2); /* move two columns back */
3563 if (view->x <= 0)
3564 view->count = 0;
3565 break;
3566 case 'k':
3567 case KEY_UP:
3568 case '<':
3569 case ',':
3570 case CTRL('p'):
3571 log_move_cursor_up(view, 0, 0);
3572 break;
3573 case 'g':
3574 case KEY_HOME:
3575 log_move_cursor_up(view, 0, 1);
3576 view->count = 0;
3577 break;
3578 case CTRL('u'):
3579 case 'u':
3580 nscroll /= 2;
3581 /* FALL THROUGH */
3582 case KEY_PPAGE:
3583 case CTRL('b'):
3584 case 'b':
3585 log_move_cursor_up(view, nscroll, 0);
3586 break;
3587 case 'j':
3588 case KEY_DOWN:
3589 case '>':
3590 case '.':
3591 case CTRL('n'):
3592 err = log_move_cursor_down(view, 0);
3593 break;
3594 case '@':
3595 s->use_committer = !s->use_committer;
3596 break;
3597 case 'G':
3598 case KEY_END: {
3599 /* We don't know yet how many commits, so we're forced to
3600 * traverse them all. */
3601 view->count = 0;
3602 s->thread_args.load_all = 1;
3603 if (!s->thread_args.log_complete)
3604 return trigger_log_thread(view, 0);
3605 err = log_move_cursor_down(view, s->commits->ncommits);
3606 s->thread_args.load_all = 0;
3607 break;
3609 case CTRL('d'):
3610 case 'd':
3611 nscroll /= 2;
3612 /* FALL THROUGH */
3613 case KEY_NPAGE:
3614 case CTRL('f'):
3615 case 'f':
3616 case ' ':
3617 err = log_move_cursor_down(view, nscroll);
3618 break;
3619 case KEY_RESIZE:
3620 if (s->selected > view->nlines - 2)
3621 s->selected = view->nlines - 2;
3622 if (s->selected > s->commits->ncommits - 1)
3623 s->selected = s->commits->ncommits - 1;
3624 select_commit(s);
3625 if (s->commits->ncommits < view->nlines - 1 &&
3626 !s->thread_args.log_complete) {
3627 s->thread_args.commits_needed += (view->nlines - 1) -
3628 s->commits->ncommits;
3629 err = trigger_log_thread(view, 1);
3631 break;
3632 case KEY_ENTER:
3633 case '\r':
3634 view->count = 0;
3635 if (s->selected_entry == NULL)
3636 break;
3637 err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3638 break;
3639 case 'T':
3640 view->count = 0;
3641 if (s->selected_entry == NULL)
3642 break;
3643 err = view_request_new(new_view, view, TOG_VIEW_TREE);
3644 break;
3645 case KEY_BACKSPACE:
3646 case CTRL('l'):
3647 case 'B':
3648 view->count = 0;
3649 if (ch == KEY_BACKSPACE &&
3650 got_path_is_root_dir(s->in_repo_path))
3651 break;
3652 err = stop_log_thread(s);
3653 if (err)
3654 return err;
3655 if (ch == KEY_BACKSPACE) {
3656 char *parent_path;
3657 err = got_path_dirname(&parent_path, s->in_repo_path);
3658 if (err)
3659 return err;
3660 free(s->in_repo_path);
3661 s->in_repo_path = parent_path;
3662 s->thread_args.in_repo_path = s->in_repo_path;
3663 } else if (ch == CTRL('l')) {
3664 struct got_object_id *start_id;
3665 err = got_repo_match_object_id(&start_id, NULL,
3666 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3667 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3668 if (err)
3669 return err;
3670 free(s->start_id);
3671 s->start_id = start_id;
3672 s->thread_args.start_id = s->start_id;
3673 } else /* 'B' */
3674 s->log_branches = !s->log_branches;
3676 if (s->thread_args.pack_fds == NULL) {
3677 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3678 if (err)
3679 return err;
3681 err = got_repo_open(&s->thread_args.repo,
3682 got_repo_get_path(s->repo), NULL,
3683 s->thread_args.pack_fds);
3684 if (err)
3685 return err;
3686 tog_free_refs();
3687 err = tog_load_refs(s->repo, 0);
3688 if (err)
3689 return err;
3690 err = got_commit_graph_open(&s->thread_args.graph,
3691 s->in_repo_path, !s->log_branches);
3692 if (err)
3693 return err;
3694 err = got_commit_graph_iter_start(s->thread_args.graph,
3695 s->start_id, s->repo, NULL, NULL);
3696 if (err)
3697 return err;
3698 free_commits(&s->real_commits);
3699 free_commits(&s->limit_commits);
3700 s->first_displayed_entry = NULL;
3701 s->last_displayed_entry = NULL;
3702 s->selected_entry = NULL;
3703 s->selected = 0;
3704 s->thread_args.log_complete = 0;
3705 s->quit = 0;
3706 s->thread_args.commits_needed = view->lines;
3707 s->matched_entry = NULL;
3708 s->search_entry = NULL;
3709 view->offset = 0;
3710 break;
3711 case 'R':
3712 view->count = 0;
3713 err = view_request_new(new_view, view, TOG_VIEW_REF);
3714 break;
3715 default:
3716 view->count = 0;
3717 break;
3720 return err;
3723 static const struct got_error *
3724 apply_unveil(const char *repo_path, const char *worktree_path)
3726 const struct got_error *error;
3728 #ifdef PROFILE
3729 if (unveil("gmon.out", "rwc") != 0)
3730 return got_error_from_errno2("unveil", "gmon.out");
3731 #endif
3732 if (repo_path && unveil(repo_path, "r") != 0)
3733 return got_error_from_errno2("unveil", repo_path);
3735 if (worktree_path && unveil(worktree_path, "rwc") != 0)
3736 return got_error_from_errno2("unveil", worktree_path);
3738 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3739 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3741 error = got_privsep_unveil_exec_helpers();
3742 if (error != NULL)
3743 return error;
3745 if (unveil(NULL, NULL) != 0)
3746 return got_error_from_errno("unveil");
3748 return NULL;
3751 static void
3752 init_curses(void)
3755 * Override default signal handlers before starting ncurses.
3756 * This should prevent ncurses from installing its own
3757 * broken cleanup() signal handler.
3759 signal(SIGWINCH, tog_sigwinch);
3760 signal(SIGPIPE, tog_sigpipe);
3761 signal(SIGCONT, tog_sigcont);
3762 signal(SIGINT, tog_sigint);
3763 signal(SIGTERM, tog_sigterm);
3765 initscr();
3766 cbreak();
3767 halfdelay(1); /* Do fast refresh while initial view is loading. */
3768 noecho();
3769 nonl();
3770 intrflush(stdscr, FALSE);
3771 keypad(stdscr, TRUE);
3772 curs_set(0);
3773 if (getenv("TOG_COLORS") != NULL) {
3774 start_color();
3775 use_default_colors();
3779 static const struct got_error *
3780 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3781 struct got_repository *repo, struct got_worktree *worktree)
3783 const struct got_error *err = NULL;
3785 if (argc == 0) {
3786 *in_repo_path = strdup("/");
3787 if (*in_repo_path == NULL)
3788 return got_error_from_errno("strdup");
3789 return NULL;
3792 if (worktree) {
3793 const char *prefix = got_worktree_get_path_prefix(worktree);
3794 char *p;
3796 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3797 if (err)
3798 return err;
3799 if (asprintf(in_repo_path, "%s%s%s", prefix,
3800 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3801 p) == -1) {
3802 err = got_error_from_errno("asprintf");
3803 *in_repo_path = NULL;
3805 free(p);
3806 } else
3807 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3809 return err;
3812 static const struct got_error *
3813 cmd_log(int argc, char *argv[])
3815 const struct got_error *error;
3816 struct got_repository *repo = NULL;
3817 struct got_worktree *worktree = NULL;
3818 struct got_object_id *start_id = NULL;
3819 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3820 char *start_commit = NULL, *label = NULL;
3821 struct got_reference *ref = NULL;
3822 const char *head_ref_name = NULL;
3823 int ch, log_branches = 0;
3824 struct tog_view *view;
3825 int *pack_fds = NULL;
3827 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3828 switch (ch) {
3829 case 'b':
3830 log_branches = 1;
3831 break;
3832 case 'c':
3833 start_commit = optarg;
3834 break;
3835 case 'r':
3836 repo_path = realpath(optarg, NULL);
3837 if (repo_path == NULL)
3838 return got_error_from_errno2("realpath",
3839 optarg);
3840 break;
3841 default:
3842 usage_log();
3843 /* NOTREACHED */
3847 argc -= optind;
3848 argv += optind;
3850 if (argc > 1)
3851 usage_log();
3853 error = got_repo_pack_fds_open(&pack_fds);
3854 if (error != NULL)
3855 goto done;
3857 if (repo_path == NULL) {
3858 cwd = getcwd(NULL, 0);
3859 if (cwd == NULL)
3860 return got_error_from_errno("getcwd");
3861 error = got_worktree_open(&worktree, cwd);
3862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3863 goto done;
3864 if (worktree)
3865 repo_path =
3866 strdup(got_worktree_get_repo_path(worktree));
3867 else
3868 repo_path = strdup(cwd);
3869 if (repo_path == NULL) {
3870 error = got_error_from_errno("strdup");
3871 goto done;
3875 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3876 if (error != NULL)
3877 goto done;
3879 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3880 repo, worktree);
3881 if (error)
3882 goto done;
3884 init_curses();
3886 error = apply_unveil(got_repo_get_path(repo),
3887 worktree ? got_worktree_get_root_path(worktree) : NULL);
3888 if (error)
3889 goto done;
3891 /* already loaded by tog_log_with_path()? */
3892 if (TAILQ_EMPTY(&tog_refs)) {
3893 error = tog_load_refs(repo, 0);
3894 if (error)
3895 goto done;
3898 if (start_commit == NULL) {
3899 error = got_repo_match_object_id(&start_id, &label,
3900 worktree ? got_worktree_get_head_ref_name(worktree) :
3901 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3902 if (error)
3903 goto done;
3904 head_ref_name = label;
3905 } else {
3906 error = got_ref_open(&ref, repo, start_commit, 0);
3907 if (error == NULL)
3908 head_ref_name = got_ref_get_name(ref);
3909 else if (error->code != GOT_ERR_NOT_REF)
3910 goto done;
3911 error = got_repo_match_object_id(&start_id, NULL,
3912 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3913 if (error)
3914 goto done;
3917 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3918 if (view == NULL) {
3919 error = got_error_from_errno("view_open");
3920 goto done;
3922 error = open_log_view(view, start_id, repo, head_ref_name,
3923 in_repo_path, log_branches);
3924 if (error)
3925 goto done;
3926 if (worktree) {
3927 /* Release work tree lock. */
3928 got_worktree_close(worktree);
3929 worktree = NULL;
3931 error = view_loop(view);
3932 done:
3933 free(in_repo_path);
3934 free(repo_path);
3935 free(cwd);
3936 free(start_id);
3937 free(label);
3938 if (ref)
3939 got_ref_close(ref);
3940 if (repo) {
3941 const struct got_error *close_err = got_repo_close(repo);
3942 if (error == NULL)
3943 error = close_err;
3945 if (worktree)
3946 got_worktree_close(worktree);
3947 if (pack_fds) {
3948 const struct got_error *pack_err =
3949 got_repo_pack_fds_close(pack_fds);
3950 if (error == NULL)
3951 error = pack_err;
3953 tog_free_refs();
3954 return error;
3957 __dead static void
3958 usage_diff(void)
3960 endwin();
3961 fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3962 "object1 object2\n", getprogname());
3963 exit(1);
3966 static int
3967 match_line(const char *line, regex_t *regex, size_t nmatch,
3968 regmatch_t *regmatch)
3970 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3973 static struct tog_color *
3974 match_color(struct tog_colors *colors, const char *line)
3976 struct tog_color *tc = NULL;
3978 STAILQ_FOREACH(tc, colors, entry) {
3979 if (match_line(line, &tc->regex, 0, NULL))
3980 return tc;
3983 return NULL;
3986 static const struct got_error *
3987 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3988 WINDOW *window, int skipcol, regmatch_t *regmatch)
3990 const struct got_error *err = NULL;
3991 char *exstr = NULL;
3992 wchar_t *wline = NULL;
3993 int rme, rms, n, width, scrollx;
3994 int width0 = 0, width1 = 0, width2 = 0;
3995 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3997 *wtotal = 0;
3999 rms = regmatch->rm_so;
4000 rme = regmatch->rm_eo;
4002 err = expand_tab(&exstr, line);
4003 if (err)
4004 return err;
4006 /* Split the line into 3 segments, according to match offsets. */
4007 seg0 = strndup(exstr, rms);
4008 if (seg0 == NULL) {
4009 err = got_error_from_errno("strndup");
4010 goto done;
4012 seg1 = strndup(exstr + rms, rme - rms);
4013 if (seg1 == NULL) {
4014 err = got_error_from_errno("strndup");
4015 goto done;
4017 seg2 = strdup(exstr + rme);
4018 if (seg2 == NULL) {
4019 err = got_error_from_errno("strndup");
4020 goto done;
4023 /* draw up to matched token if we haven't scrolled past it */
4024 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4025 col_tab_align, 1);
4026 if (err)
4027 goto done;
4028 n = MAX(width0 - skipcol, 0);
4029 if (n) {
4030 free(wline);
4031 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4032 wlimit, col_tab_align, 1);
4033 if (err)
4034 goto done;
4035 waddwstr(window, &wline[scrollx]);
4036 wlimit -= width;
4037 *wtotal += width;
4040 if (wlimit > 0) {
4041 int i = 0, w = 0;
4042 size_t wlen;
4044 free(wline);
4045 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4046 col_tab_align, 1);
4047 if (err)
4048 goto done;
4049 wlen = wcslen(wline);
4050 while (i < wlen) {
4051 width = wcwidth(wline[i]);
4052 if (width == -1) {
4053 /* should not happen, tabs are expanded */
4054 err = got_error(GOT_ERR_RANGE);
4055 goto done;
4057 if (width0 + w + width > skipcol)
4058 break;
4059 w += width;
4060 i++;
4062 /* draw (visible part of) matched token (if scrolled into it) */
4063 if (width1 - w > 0) {
4064 wattron(window, A_STANDOUT);
4065 waddwstr(window, &wline[i]);
4066 wattroff(window, A_STANDOUT);
4067 wlimit -= (width1 - w);
4068 *wtotal += (width1 - w);
4072 if (wlimit > 0) { /* draw rest of line */
4073 free(wline);
4074 if (skipcol > width0 + width1) {
4075 err = format_line(&wline, &width2, &scrollx, seg2,
4076 skipcol - (width0 + width1), wlimit,
4077 col_tab_align, 1);
4078 if (err)
4079 goto done;
4080 waddwstr(window, &wline[scrollx]);
4081 } else {
4082 err = format_line(&wline, &width2, NULL, seg2, 0,
4083 wlimit, col_tab_align, 1);
4084 if (err)
4085 goto done;
4086 waddwstr(window, wline);
4088 *wtotal += width2;
4090 done:
4091 free(wline);
4092 free(exstr);
4093 free(seg0);
4094 free(seg1);
4095 free(seg2);
4096 return err;
4099 static int
4100 gotoline(struct tog_view *view, int *lineno, int *nprinted)
4102 FILE *f = NULL;
4103 int *eof, *first, *selected;
4105 if (view->type == TOG_VIEW_DIFF) {
4106 struct tog_diff_view_state *s = &view->state.diff;
4108 first = &s->first_displayed_line;
4109 selected = first;
4110 eof = &s->eof;
4111 f = s->f;
4112 } else if (view->type == TOG_VIEW_BLAME) {
4113 struct tog_blame_view_state *s = &view->state.blame;
4115 first = &s->first_displayed_line;
4116 selected = &s->selected_line;
4117 eof = &s->eof;
4118 f = s->blame.f;
4119 } else
4120 return 0;
4122 /* Center gline in the middle of the page like vi(1). */
4123 if (*lineno < view->gline - (view->nlines - 3) / 2)
4124 return 0;
4125 if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4126 rewind(f);
4127 *eof = 0;
4128 *first = 1;
4129 *lineno = 0;
4130 *nprinted = 0;
4131 return 0;
4134 *selected = view->gline <= (view->nlines - 3) / 2 ?
4135 view->gline : (view->nlines - 3) / 2 + 1;
4136 view->gline = 0;
4138 return 1;
4141 static const struct got_error *
4142 draw_file(struct tog_view *view, const char *header)
4144 struct tog_diff_view_state *s = &view->state.diff;
4145 regmatch_t *regmatch = &view->regmatch;
4146 const struct got_error *err;
4147 int nprinted = 0;
4148 char *line;
4149 size_t linesize = 0;
4150 ssize_t linelen;
4151 wchar_t *wline;
4152 int width;
4153 int max_lines = view->nlines;
4154 int nlines = s->nlines;
4155 off_t line_offset;
4157 s->lineno = s->first_displayed_line - 1;
4158 line_offset = s->lines[s->first_displayed_line - 1].offset;
4159 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4160 return got_error_from_errno("fseek");
4162 werase(view->window);
4164 if (view->gline > s->nlines - 1)
4165 view->gline = s->nlines - 1;
4167 if (header) {
4168 int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4169 1 : view->gline - (view->nlines - 3) / 2 :
4170 s->lineno + s->selected_line;
4172 if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4173 return got_error_from_errno("asprintf");
4174 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4175 0, 0);
4176 free(line);
4177 if (err)
4178 return err;
4180 if (view_needs_focus_indication(view))
4181 wstandout(view->window);
4182 waddwstr(view->window, wline);
4183 free(wline);
4184 wline = NULL;
4185 while (width++ < view->ncols)
4186 waddch(view->window, ' ');
4187 if (view_needs_focus_indication(view))
4188 wstandend(view->window);
4190 if (max_lines <= 1)
4191 return NULL;
4192 max_lines--;
4195 s->eof = 0;
4196 view->maxx = 0;
4197 line = NULL;
4198 while (max_lines > 0 && nprinted < max_lines) {
4199 enum got_diff_line_type linetype;
4200 attr_t attr = 0;
4202 linelen = getline(&line, &linesize, s->f);
4203 if (linelen == -1) {
4204 if (feof(s->f)) {
4205 s->eof = 1;
4206 break;
4208 free(line);
4209 return got_ferror(s->f, GOT_ERR_IO);
4212 if (++s->lineno < s->first_displayed_line)
4213 continue;
4214 if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4215 continue;
4216 if (s->lineno == view->hiline)
4217 attr = A_STANDOUT;
4219 /* Set view->maxx based on full line length. */
4220 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4221 view->x ? 1 : 0);
4222 if (err) {
4223 free(line);
4224 return err;
4226 view->maxx = MAX(view->maxx, width);
4227 free(wline);
4228 wline = NULL;
4230 linetype = s->lines[s->lineno].type;
4231 if (linetype > GOT_DIFF_LINE_LOGMSG &&
4232 linetype < GOT_DIFF_LINE_CONTEXT)
4233 attr |= COLOR_PAIR(linetype);
4234 if (attr)
4235 wattron(view->window, attr);
4236 if (s->first_displayed_line + nprinted == s->matched_line &&
4237 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4238 err = add_matched_line(&width, line, view->ncols, 0,
4239 view->window, view->x, regmatch);
4240 if (err) {
4241 free(line);
4242 return err;
4244 } else {
4245 int skip;
4246 err = format_line(&wline, &width, &skip, line,
4247 view->x, view->ncols, 0, view->x ? 1 : 0);
4248 if (err) {
4249 free(line);
4250 return err;
4252 waddwstr(view->window, &wline[skip]);
4253 free(wline);
4254 wline = NULL;
4256 if (s->lineno == view->hiline) {
4257 /* highlight full gline length */
4258 while (width++ < view->ncols)
4259 waddch(view->window, ' ');
4260 } else {
4261 if (width <= view->ncols - 1)
4262 waddch(view->window, '\n');
4264 if (attr)
4265 wattroff(view->window, attr);
4266 if (++nprinted == 1)
4267 s->first_displayed_line = s->lineno;
4269 free(line);
4270 if (nprinted >= 1)
4271 s->last_displayed_line = s->first_displayed_line +
4272 (nprinted - 1);
4273 else
4274 s->last_displayed_line = s->first_displayed_line;
4276 view_border(view);
4278 if (s->eof) {
4279 while (nprinted < view->nlines) {
4280 waddch(view->window, '\n');
4281 nprinted++;
4284 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4285 view->ncols, 0, 0);
4286 if (err) {
4287 return err;
4290 wstandout(view->window);
4291 waddwstr(view->window, wline);
4292 free(wline);
4293 wline = NULL;
4294 wstandend(view->window);
4297 return NULL;
4300 static char *
4301 get_datestr(time_t *time, char *datebuf)
4303 struct tm mytm, *tm;
4304 char *p, *s;
4306 tm = gmtime_r(time, &mytm);
4307 if (tm == NULL)
4308 return NULL;
4309 s = asctime_r(tm, datebuf);
4310 if (s == NULL)
4311 return NULL;
4312 p = strchr(s, '\n');
4313 if (p)
4314 *p = '\0';
4315 return s;
4318 static const struct got_error *
4319 get_changed_paths(struct got_pathlist_head *paths,
4320 struct got_commit_object *commit, struct got_repository *repo)
4322 const struct got_error *err = NULL;
4323 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4324 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4325 struct got_object_qid *qid;
4327 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4328 if (qid != NULL) {
4329 struct got_commit_object *pcommit;
4330 err = got_object_open_as_commit(&pcommit, repo,
4331 &qid->id);
4332 if (err)
4333 return err;
4335 tree_id1 = got_object_id_dup(
4336 got_object_commit_get_tree_id(pcommit));
4337 if (tree_id1 == NULL) {
4338 got_object_commit_close(pcommit);
4339 return got_error_from_errno("got_object_id_dup");
4341 got_object_commit_close(pcommit);
4345 if (tree_id1) {
4346 err = got_object_open_as_tree(&tree1, repo, tree_id1);
4347 if (err)
4348 goto done;
4351 tree_id2 = got_object_commit_get_tree_id(commit);
4352 err = got_object_open_as_tree(&tree2, repo, tree_id2);
4353 if (err)
4354 goto done;
4356 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4357 got_diff_tree_collect_changed_paths, paths, 0);
4358 done:
4359 if (tree1)
4360 got_object_tree_close(tree1);
4361 if (tree2)
4362 got_object_tree_close(tree2);
4363 free(tree_id1);
4364 return err;
4367 static const struct got_error *
4368 add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4369 off_t off, uint8_t type)
4371 struct got_diff_line *p;
4373 p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4374 if (p == NULL)
4375 return got_error_from_errno("reallocarray");
4376 *lines = p;
4377 (*lines)[*nlines].offset = off;
4378 (*lines)[*nlines].type = type;
4379 (*nlines)++;
4381 return NULL;
4384 static const struct got_error *
4385 write_commit_info(struct got_diff_line **lines, size_t *nlines,
4386 struct got_object_id *commit_id, struct got_reflist_head *refs,
4387 struct got_repository *repo, FILE *outfile)
4389 const struct got_error *err = NULL;
4390 char datebuf[26], *datestr;
4391 struct got_commit_object *commit;
4392 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4393 time_t committer_time;
4394 const char *author, *committer;
4395 char *refs_str = NULL;
4396 struct got_pathlist_head changed_paths;
4397 struct got_pathlist_entry *pe;
4398 off_t outoff = 0;
4399 int n;
4401 TAILQ_INIT(&changed_paths);
4403 if (refs) {
4404 err = build_refs_str(&refs_str, refs, commit_id, repo);
4405 if (err)
4406 return err;
4409 err = got_object_open_as_commit(&commit, repo, commit_id);
4410 if (err)
4411 return err;
4413 err = got_object_id_str(&id_str, commit_id);
4414 if (err) {
4415 err = got_error_from_errno("got_object_id_str");
4416 goto done;
4419 err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4420 if (err)
4421 goto done;
4423 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4424 refs_str ? refs_str : "", refs_str ? ")" : "");
4425 if (n < 0) {
4426 err = got_error_from_errno("fprintf");
4427 goto done;
4429 outoff += n;
4430 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4431 if (err)
4432 goto done;
4434 n = fprintf(outfile, "from: %s\n",
4435 got_object_commit_get_author(commit));
4436 if (n < 0) {
4437 err = got_error_from_errno("fprintf");
4438 goto done;
4440 outoff += n;
4441 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4442 if (err)
4443 goto done;
4445 committer_time = got_object_commit_get_committer_time(commit);
4446 datestr = get_datestr(&committer_time, datebuf);
4447 if (datestr) {
4448 n = fprintf(outfile, "date: %s UTC\n", datestr);
4449 if (n < 0) {
4450 err = got_error_from_errno("fprintf");
4451 goto done;
4453 outoff += n;
4454 err = add_line_metadata(lines, nlines, outoff,
4455 GOT_DIFF_LINE_DATE);
4456 if (err)
4457 goto done;
4459 author = got_object_commit_get_author(commit);
4460 committer = got_object_commit_get_committer(commit);
4461 if (strcmp(author, committer) != 0) {
4462 n = fprintf(outfile, "via: %s\n", committer);
4463 if (n < 0) {
4464 err = got_error_from_errno("fprintf");
4465 goto done;
4467 outoff += n;
4468 err = add_line_metadata(lines, nlines, outoff,
4469 GOT_DIFF_LINE_AUTHOR);
4470 if (err)
4471 goto done;
4473 if (got_object_commit_get_nparents(commit) > 1) {
4474 const struct got_object_id_queue *parent_ids;
4475 struct got_object_qid *qid;
4476 int pn = 1;
4477 parent_ids = got_object_commit_get_parent_ids(commit);
4478 STAILQ_FOREACH(qid, parent_ids, entry) {
4479 err = got_object_id_str(&id_str, &qid->id);
4480 if (err)
4481 goto done;
4482 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4483 if (n < 0) {
4484 err = got_error_from_errno("fprintf");
4485 goto done;
4487 outoff += n;
4488 err = add_line_metadata(lines, nlines, outoff,
4489 GOT_DIFF_LINE_META);
4490 if (err)
4491 goto done;
4492 free(id_str);
4493 id_str = NULL;
4497 err = got_object_commit_get_logmsg(&logmsg, commit);
4498 if (err)
4499 goto done;
4500 s = logmsg;
4501 while ((line = strsep(&s, "\n")) != NULL) {
4502 n = fprintf(outfile, "%s\n", line);
4503 if (n < 0) {
4504 err = got_error_from_errno("fprintf");
4505 goto done;
4507 outoff += n;
4508 err = add_line_metadata(lines, nlines, outoff,
4509 GOT_DIFF_LINE_LOGMSG);
4510 if (err)
4511 goto done;
4514 err = get_changed_paths(&changed_paths, commit, repo);
4515 if (err)
4516 goto done;
4517 TAILQ_FOREACH(pe, &changed_paths, entry) {
4518 struct got_diff_changed_path *cp = pe->data;
4519 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4520 if (n < 0) {
4521 err = got_error_from_errno("fprintf");
4522 goto done;
4524 outoff += n;
4525 err = add_line_metadata(lines, nlines, outoff,
4526 GOT_DIFF_LINE_CHANGES);
4527 if (err)
4528 goto done;
4529 free((char *)pe->path);
4530 free(pe->data);
4533 fputc('\n', outfile);
4534 outoff++;
4535 err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4536 done:
4537 got_pathlist_free(&changed_paths);
4538 free(id_str);
4539 free(logmsg);
4540 free(refs_str);
4541 got_object_commit_close(commit);
4542 if (err) {
4543 free(*lines);
4544 *lines = NULL;
4545 *nlines = 0;
4547 return err;
4550 static const struct got_error *
4551 create_diff(struct tog_diff_view_state *s)
4553 const struct got_error *err = NULL;
4554 FILE *f = NULL;
4555 int obj_type;
4557 free(s->lines);
4558 s->lines = malloc(sizeof(*s->lines));
4559 if (s->lines == NULL)
4560 return got_error_from_errno("malloc");
4561 s->nlines = 0;
4563 f = got_opentemp();
4564 if (f == NULL) {
4565 err = got_error_from_errno("got_opentemp");
4566 goto done;
4568 if (s->f && fclose(s->f) == EOF) {
4569 err = got_error_from_errno("fclose");
4570 goto done;
4572 s->f = f;
4574 if (s->id1)
4575 err = got_object_get_type(&obj_type, s->repo, s->id1);
4576 else
4577 err = got_object_get_type(&obj_type, s->repo, s->id2);
4578 if (err)
4579 goto done;
4581 switch (obj_type) {
4582 case GOT_OBJ_TYPE_BLOB:
4583 err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4584 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4585 s->label1, s->label2, tog_diff_algo, s->diff_context,
4586 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4587 break;
4588 case GOT_OBJ_TYPE_TREE:
4589 err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4590 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4591 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4592 s->force_text_diff, s->repo, s->f);
4593 break;
4594 case GOT_OBJ_TYPE_COMMIT: {
4595 const struct got_object_id_queue *parent_ids;
4596 struct got_object_qid *pid;
4597 struct got_commit_object *commit2;
4598 struct got_reflist_head *refs;
4600 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4601 if (err)
4602 goto done;
4603 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4604 /* Show commit info if we're diffing to a parent/root commit. */
4605 if (s->id1 == NULL) {
4606 err = write_commit_info(&s->lines, &s->nlines, s->id2,
4607 refs, s->repo, s->f);
4608 if (err)
4609 goto done;
4610 } else {
4611 parent_ids = got_object_commit_get_parent_ids(commit2);
4612 STAILQ_FOREACH(pid, parent_ids, entry) {
4613 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4614 err = write_commit_info(&s->lines,
4615 &s->nlines, s->id2, refs, s->repo,
4616 s->f);
4617 if (err)
4618 goto done;
4619 break;
4623 got_object_commit_close(commit2);
4625 err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4626 s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4627 tog_diff_algo, s->diff_context, s->ignore_whitespace,
4628 s->force_text_diff, s->repo, s->f);
4629 break;
4631 default:
4632 err = got_error(GOT_ERR_OBJ_TYPE);
4633 break;
4635 done:
4636 if (s->f && fflush(s->f) != 0 && err == NULL)
4637 err = got_error_from_errno("fflush");
4638 return err;
4641 static void
4642 diff_view_indicate_progress(struct tog_view *view)
4644 mvwaddstr(view->window, 0, 0, "diffing...");
4645 update_panels();
4646 doupdate();
4649 static const struct got_error *
4650 search_start_diff_view(struct tog_view *view)
4652 struct tog_diff_view_state *s = &view->state.diff;
4654 s->matched_line = 0;
4655 return NULL;
4658 static const struct got_error *
4659 search_next_diff_view(struct tog_view *view)
4661 struct tog_diff_view_state *s = &view->state.diff;
4662 const struct got_error *err = NULL;
4663 int lineno;
4664 char *line = NULL;
4665 size_t linesize = 0;
4666 ssize_t linelen;
4668 if (!view->searching) {
4669 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4670 return NULL;
4673 if (s->matched_line) {
4674 if (view->searching == TOG_SEARCH_FORWARD)
4675 lineno = s->matched_line + 1;
4676 else
4677 lineno = s->matched_line - 1;
4678 } else
4679 lineno = s->first_displayed_line;
4681 while (1) {
4682 off_t offset;
4684 if (lineno <= 0 || lineno > s->nlines) {
4685 if (s->matched_line == 0) {
4686 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4687 break;
4690 if (view->searching == TOG_SEARCH_FORWARD)
4691 lineno = 1;
4692 else
4693 lineno = s->nlines;
4696 offset = s->lines[lineno - 1].offset;
4697 if (fseeko(s->f, offset, SEEK_SET) != 0) {
4698 free(line);
4699 return got_error_from_errno("fseeko");
4701 linelen = getline(&line, &linesize, s->f);
4702 if (linelen != -1) {
4703 char *exstr;
4704 err = expand_tab(&exstr, line);
4705 if (err)
4706 break;
4707 if (match_line(exstr, &view->regex, 1,
4708 &view->regmatch)) {
4709 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4710 s->matched_line = lineno;
4711 free(exstr);
4712 break;
4714 free(exstr);
4716 if (view->searching == TOG_SEARCH_FORWARD)
4717 lineno++;
4718 else
4719 lineno--;
4721 free(line);
4723 if (s->matched_line) {
4724 s->first_displayed_line = s->matched_line;
4725 s->selected_line = 1;
4728 return err;
4731 static const struct got_error *
4732 close_diff_view(struct tog_view *view)
4734 const struct got_error *err = NULL;
4735 struct tog_diff_view_state *s = &view->state.diff;
4737 free(s->id1);
4738 s->id1 = NULL;
4739 free(s->id2);
4740 s->id2 = NULL;
4741 if (s->f && fclose(s->f) == EOF)
4742 err = got_error_from_errno("fclose");
4743 s->f = NULL;
4744 if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4745 err = got_error_from_errno("fclose");
4746 s->f1 = NULL;
4747 if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4748 err = got_error_from_errno("fclose");
4749 s->f2 = NULL;
4750 if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4751 err = got_error_from_errno("close");
4752 s->fd1 = -1;
4753 if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4754 err = got_error_from_errno("close");
4755 s->fd2 = -1;
4756 free(s->lines);
4757 s->lines = NULL;
4758 s->nlines = 0;
4759 return err;
4762 static const struct got_error *
4763 open_diff_view(struct tog_view *view, struct got_object_id *id1,
4764 struct got_object_id *id2, const char *label1, const char *label2,
4765 int diff_context, int ignore_whitespace, int force_text_diff,
4766 struct tog_view *parent_view, struct got_repository *repo)
4768 const struct got_error *err;
4769 struct tog_diff_view_state *s = &view->state.diff;
4771 memset(s, 0, sizeof(*s));
4772 s->fd1 = -1;
4773 s->fd2 = -1;
4775 if (id1 != NULL && id2 != NULL) {
4776 int type1, type2;
4777 err = got_object_get_type(&type1, repo, id1);
4778 if (err)
4779 return err;
4780 err = got_object_get_type(&type2, repo, id2);
4781 if (err)
4782 return err;
4784 if (type1 != type2)
4785 return got_error(GOT_ERR_OBJ_TYPE);
4787 s->first_displayed_line = 1;
4788 s->last_displayed_line = view->nlines;
4789 s->selected_line = 1;
4790 s->repo = repo;
4791 s->id1 = id1;
4792 s->id2 = id2;
4793 s->label1 = label1;
4794 s->label2 = label2;
4796 if (id1) {
4797 s->id1 = got_object_id_dup(id1);
4798 if (s->id1 == NULL)
4799 return got_error_from_errno("got_object_id_dup");
4800 } else
4801 s->id1 = NULL;
4803 s->id2 = got_object_id_dup(id2);
4804 if (s->id2 == NULL) {
4805 err = got_error_from_errno("got_object_id_dup");
4806 goto done;
4809 s->f1 = got_opentemp();
4810 if (s->f1 == NULL) {
4811 err = got_error_from_errno("got_opentemp");
4812 goto done;
4815 s->f2 = got_opentemp();
4816 if (s->f2 == NULL) {
4817 err = got_error_from_errno("got_opentemp");
4818 goto done;
4821 s->fd1 = got_opentempfd();
4822 if (s->fd1 == -1) {
4823 err = got_error_from_errno("got_opentempfd");
4824 goto done;
4827 s->fd2 = got_opentempfd();
4828 if (s->fd2 == -1) {
4829 err = got_error_from_errno("got_opentempfd");
4830 goto done;
4833 s->first_displayed_line = 1;
4834 s->last_displayed_line = view->nlines;
4835 s->diff_context = diff_context;
4836 s->ignore_whitespace = ignore_whitespace;
4837 s->force_text_diff = force_text_diff;
4838 s->parent_view = parent_view;
4839 s->repo = repo;
4841 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4842 int rc;
4844 rc = init_pair(GOT_DIFF_LINE_MINUS,
4845 get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4846 if (rc != ERR)
4847 rc = init_pair(GOT_DIFF_LINE_PLUS,
4848 get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4849 if (rc != ERR)
4850 rc = init_pair(GOT_DIFF_LINE_HUNK,
4851 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4852 if (rc != ERR)
4853 rc = init_pair(GOT_DIFF_LINE_META,
4854 get_color_value("TOG_COLOR_DIFF_META"), -1);
4855 if (rc != ERR)
4856 rc = init_pair(GOT_DIFF_LINE_CHANGES,
4857 get_color_value("TOG_COLOR_DIFF_META"), -1);
4858 if (rc != ERR)
4859 rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4860 get_color_value("TOG_COLOR_DIFF_META"), -1);
4861 if (rc != ERR)
4862 rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4863 get_color_value("TOG_COLOR_DIFF_META"), -1);
4864 if (rc != ERR)
4865 rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4866 get_color_value("TOG_COLOR_AUTHOR"), -1);
4867 if (rc != ERR)
4868 rc = init_pair(GOT_DIFF_LINE_DATE,
4869 get_color_value("TOG_COLOR_DATE"), -1);
4870 if (rc == ERR) {
4871 err = got_error(GOT_ERR_RANGE);
4872 goto done;
4876 if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4877 view_is_splitscreen(view))
4878 show_log_view(parent_view); /* draw border */
4879 diff_view_indicate_progress(view);
4881 err = create_diff(s);
4883 view->show = show_diff_view;
4884 view->input = input_diff_view;
4885 view->reset = reset_diff_view;
4886 view->close = close_diff_view;
4887 view->search_start = search_start_diff_view;
4888 view->search_next = search_next_diff_view;
4889 done:
4890 if (err)
4891 close_diff_view(view);
4892 return err;
4895 static const struct got_error *
4896 show_diff_view(struct tog_view *view)
4898 const struct got_error *err;
4899 struct tog_diff_view_state *s = &view->state.diff;
4900 char *id_str1 = NULL, *id_str2, *header;
4901 const char *label1, *label2;
4903 if (s->id1) {
4904 err = got_object_id_str(&id_str1, s->id1);
4905 if (err)
4906 return err;
4907 label1 = s->label1 ? s->label1 : id_str1;
4908 } else
4909 label1 = "/dev/null";
4911 err = got_object_id_str(&id_str2, s->id2);
4912 if (err)
4913 return err;
4914 label2 = s->label2 ? s->label2 : id_str2;
4916 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4917 err = got_error_from_errno("asprintf");
4918 free(id_str1);
4919 free(id_str2);
4920 return err;
4922 free(id_str1);
4923 free(id_str2);
4925 err = draw_file(view, header);
4926 free(header);
4927 return err;
4930 static const struct got_error *
4931 set_selected_commit(struct tog_diff_view_state *s,
4932 struct commit_queue_entry *entry)
4934 const struct got_error *err;
4935 const struct got_object_id_queue *parent_ids;
4936 struct got_commit_object *selected_commit;
4937 struct got_object_qid *pid;
4939 free(s->id2);
4940 s->id2 = got_object_id_dup(entry->id);
4941 if (s->id2 == NULL)
4942 return got_error_from_errno("got_object_id_dup");
4944 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4945 if (err)
4946 return err;
4947 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4948 free(s->id1);
4949 pid = STAILQ_FIRST(parent_ids);
4950 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4951 got_object_commit_close(selected_commit);
4952 return NULL;
4955 static const struct got_error *
4956 reset_diff_view(struct tog_view *view)
4958 struct tog_diff_view_state *s = &view->state.diff;
4960 view->count = 0;
4961 wclear(view->window);
4962 s->first_displayed_line = 1;
4963 s->last_displayed_line = view->nlines;
4964 s->matched_line = 0;
4965 diff_view_indicate_progress(view);
4966 return create_diff(s);
4969 static void
4970 diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4972 int start, i;
4974 i = start = s->first_displayed_line - 1;
4976 while (s->lines[i].type != type) {
4977 if (i == 0)
4978 i = s->nlines - 1;
4979 if (--i == start)
4980 return; /* do nothing, requested type not in file */
4983 s->selected_line = 1;
4984 s->first_displayed_line = i;
4987 static void
4988 diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4990 int start, i;
4992 i = start = s->first_displayed_line + 1;
4994 while (s->lines[i].type != type) {
4995 if (i == s->nlines - 1)
4996 i = 0;
4997 if (++i == start)
4998 return; /* do nothing, requested type not in file */
5001 s->selected_line = 1;
5002 s->first_displayed_line = i;
5005 static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5006 int, int, int);
5007 static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5008 int, int);
5010 static const struct got_error *
5011 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5013 const struct got_error *err = NULL;
5014 struct tog_diff_view_state *s = &view->state.diff;
5015 struct tog_log_view_state *ls;
5016 struct commit_queue_entry *old_selected_entry;
5017 char *line = NULL;
5018 size_t linesize = 0;
5019 ssize_t linelen;
5020 int i, nscroll = view->nlines - 1, up = 0;
5022 s->lineno = s->first_displayed_line - 1 + s->selected_line;
5024 switch (ch) {
5025 case '0':
5026 view->x = 0;
5027 break;
5028 case '$':
5029 view->x = MAX(view->maxx - view->ncols / 3, 0);
5030 view->count = 0;
5031 break;
5032 case KEY_RIGHT:
5033 case 'l':
5034 if (view->x + view->ncols / 3 < view->maxx)
5035 view->x += 2; /* move two columns right */
5036 else
5037 view->count = 0;
5038 break;
5039 case KEY_LEFT:
5040 case 'h':
5041 view->x -= MIN(view->x, 2); /* move two columns back */
5042 if (view->x <= 0)
5043 view->count = 0;
5044 break;
5045 case 'a':
5046 case 'w':
5047 if (ch == 'a')
5048 s->force_text_diff = !s->force_text_diff;
5049 if (ch == 'w')
5050 s->ignore_whitespace = !s->ignore_whitespace;
5051 err = reset_diff_view(view);
5052 break;
5053 case 'g':
5054 case KEY_HOME:
5055 s->first_displayed_line = 1;
5056 view->count = 0;
5057 break;
5058 case 'G':
5059 case KEY_END:
5060 view->count = 0;
5061 if (s->eof)
5062 break;
5064 s->first_displayed_line = (s->nlines - view->nlines) + 2;
5065 s->eof = 1;
5066 break;
5067 case 'k':
5068 case KEY_UP:
5069 case CTRL('p'):
5070 if (s->first_displayed_line > 1)
5071 s->first_displayed_line--;
5072 else
5073 view->count = 0;
5074 break;
5075 case CTRL('u'):
5076 case 'u':
5077 nscroll /= 2;
5078 /* FALL THROUGH */
5079 case KEY_PPAGE:
5080 case CTRL('b'):
5081 case 'b':
5082 if (s->first_displayed_line == 1) {
5083 view->count = 0;
5084 break;
5086 i = 0;
5087 while (i++ < nscroll && s->first_displayed_line > 1)
5088 s->first_displayed_line--;
5089 break;
5090 case 'j':
5091 case KEY_DOWN:
5092 case CTRL('n'):
5093 if (!s->eof)
5094 s->first_displayed_line++;
5095 else
5096 view->count = 0;
5097 break;
5098 case CTRL('d'):
5099 case 'd':
5100 nscroll /= 2;
5101 /* FALL THROUGH */
5102 case KEY_NPAGE:
5103 case CTRL('f'):
5104 case 'f':
5105 case ' ':
5106 if (s->eof) {
5107 view->count = 0;
5108 break;
5110 i = 0;
5111 while (!s->eof && i++ < nscroll) {
5112 linelen = getline(&line, &linesize, s->f);
5113 s->first_displayed_line++;
5114 if (linelen == -1) {
5115 if (feof(s->f)) {
5116 s->eof = 1;
5117 } else
5118 err = got_ferror(s->f, GOT_ERR_IO);
5119 break;
5122 free(line);
5123 break;
5124 case '(':
5125 diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5126 break;
5127 case ')':
5128 diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5129 break;
5130 case '{':
5131 diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5132 break;
5133 case '}':
5134 diff_next_index(s, GOT_DIFF_LINE_HUNK);
5135 break;
5136 case '[':
5137 if (s->diff_context > 0) {
5138 s->diff_context--;
5139 s->matched_line = 0;
5140 diff_view_indicate_progress(view);
5141 err = create_diff(s);
5142 if (s->first_displayed_line + view->nlines - 1 >
5143 s->nlines) {
5144 s->first_displayed_line = 1;
5145 s->last_displayed_line = view->nlines;
5147 } else
5148 view->count = 0;
5149 break;
5150 case ']':
5151 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5152 s->diff_context++;
5153 s->matched_line = 0;
5154 diff_view_indicate_progress(view);
5155 err = create_diff(s);
5156 } else
5157 view->count = 0;
5158 break;
5159 case '<':
5160 case ',':
5161 case 'K':
5162 up = 1;
5163 /* FALL THROUGH */
5164 case '>':
5165 case '.':
5166 case 'J':
5167 if (s->parent_view == NULL) {
5168 view->count = 0;
5169 break;
5171 s->parent_view->count = view->count;
5173 if (s->parent_view->type == TOG_VIEW_LOG) {
5174 ls = &s->parent_view->state.log;
5175 old_selected_entry = ls->selected_entry;
5177 err = input_log_view(NULL, s->parent_view,
5178 up ? KEY_UP : KEY_DOWN);
5179 if (err)
5180 break;
5181 view->count = s->parent_view->count;
5183 if (old_selected_entry == ls->selected_entry)
5184 break;
5186 err = set_selected_commit(s, ls->selected_entry);
5187 if (err)
5188 break;
5189 } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5190 struct tog_blame_view_state *bs;
5191 struct got_object_id *id, *prev_id;
5193 bs = &s->parent_view->state.blame;
5194 prev_id = get_annotation_for_line(bs->blame.lines,
5195 bs->blame.nlines, bs->last_diffed_line);
5197 err = input_blame_view(&view, s->parent_view,
5198 up ? KEY_UP : KEY_DOWN);
5199 if (err)
5200 break;
5201 view->count = s->parent_view->count;
5203 if (prev_id == NULL)
5204 break;
5205 id = get_selected_commit_id(bs->blame.lines,
5206 bs->blame.nlines, bs->first_displayed_line,
5207 bs->selected_line);
5208 if (id == NULL)
5209 break;
5211 if (!got_object_id_cmp(prev_id, id))
5212 break;
5214 err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5215 if (err)
5216 break;
5218 s->first_displayed_line = 1;
5219 s->last_displayed_line = view->nlines;
5220 s->matched_line = 0;
5221 view->x = 0;
5223 diff_view_indicate_progress(view);
5224 err = create_diff(s);
5225 break;
5226 default:
5227 view->count = 0;
5228 break;
5231 return err;
5234 static const struct got_error *
5235 cmd_diff(int argc, char *argv[])
5237 const struct got_error *error = NULL;
5238 struct got_repository *repo = NULL;
5239 struct got_worktree *worktree = NULL;
5240 struct got_object_id *id1 = NULL, *id2 = NULL;
5241 char *repo_path = NULL, *cwd = NULL;
5242 char *id_str1 = NULL, *id_str2 = NULL;
5243 char *label1 = NULL, *label2 = NULL;
5244 int diff_context = 3, ignore_whitespace = 0;
5245 int ch, force_text_diff = 0;
5246 const char *errstr;
5247 struct tog_view *view;
5248 int *pack_fds = NULL;
5250 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5251 switch (ch) {
5252 case 'a':
5253 force_text_diff = 1;
5254 break;
5255 case 'C':
5256 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5257 &errstr);
5258 if (errstr != NULL)
5259 errx(1, "number of context lines is %s: %s",
5260 errstr, errstr);
5261 break;
5262 case 'r':
5263 repo_path = realpath(optarg, NULL);
5264 if (repo_path == NULL)
5265 return got_error_from_errno2("realpath",
5266 optarg);
5267 got_path_strip_trailing_slashes(repo_path);
5268 break;
5269 case 'w':
5270 ignore_whitespace = 1;
5271 break;
5272 default:
5273 usage_diff();
5274 /* NOTREACHED */
5278 argc -= optind;
5279 argv += optind;
5281 if (argc == 0) {
5282 usage_diff(); /* TODO show local worktree changes */
5283 } else if (argc == 2) {
5284 id_str1 = argv[0];
5285 id_str2 = argv[1];
5286 } else
5287 usage_diff();
5289 error = got_repo_pack_fds_open(&pack_fds);
5290 if (error)
5291 goto done;
5293 if (repo_path == NULL) {
5294 cwd = getcwd(NULL, 0);
5295 if (cwd == NULL)
5296 return got_error_from_errno("getcwd");
5297 error = got_worktree_open(&worktree, cwd);
5298 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5299 goto done;
5300 if (worktree)
5301 repo_path =
5302 strdup(got_worktree_get_repo_path(worktree));
5303 else
5304 repo_path = strdup(cwd);
5305 if (repo_path == NULL) {
5306 error = got_error_from_errno("strdup");
5307 goto done;
5311 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5312 if (error)
5313 goto done;
5315 init_curses();
5317 error = apply_unveil(got_repo_get_path(repo), NULL);
5318 if (error)
5319 goto done;
5321 error = tog_load_refs(repo, 0);
5322 if (error)
5323 goto done;
5325 error = got_repo_match_object_id(&id1, &label1, id_str1,
5326 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5327 if (error)
5328 goto done;
5330 error = got_repo_match_object_id(&id2, &label2, id_str2,
5331 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5332 if (error)
5333 goto done;
5335 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5336 if (view == NULL) {
5337 error = got_error_from_errno("view_open");
5338 goto done;
5340 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5341 ignore_whitespace, force_text_diff, NULL, repo);
5342 if (error)
5343 goto done;
5344 error = view_loop(view);
5345 done:
5346 free(label1);
5347 free(label2);
5348 free(repo_path);
5349 free(cwd);
5350 if (repo) {
5351 const struct got_error *close_err = got_repo_close(repo);
5352 if (error == NULL)
5353 error = close_err;
5355 if (worktree)
5356 got_worktree_close(worktree);
5357 if (pack_fds) {
5358 const struct got_error *pack_err =
5359 got_repo_pack_fds_close(pack_fds);
5360 if (error == NULL)
5361 error = pack_err;
5363 tog_free_refs();
5364 return error;
5367 __dead static void
5368 usage_blame(void)
5370 endwin();
5371 fprintf(stderr,
5372 "usage: %s blame [-c commit] [-r repository-path] path\n",
5373 getprogname());
5374 exit(1);
5377 struct tog_blame_line {
5378 int annotated;
5379 struct got_object_id *id;
5382 static const struct got_error *
5383 draw_blame(struct tog_view *view)
5385 struct tog_blame_view_state *s = &view->state.blame;
5386 struct tog_blame *blame = &s->blame;
5387 regmatch_t *regmatch = &view->regmatch;
5388 const struct got_error *err;
5389 int lineno = 0, nprinted = 0;
5390 char *line = NULL;
5391 size_t linesize = 0;
5392 ssize_t linelen;
5393 wchar_t *wline;
5394 int width;
5395 struct tog_blame_line *blame_line;
5396 struct got_object_id *prev_id = NULL;
5397 char *id_str;
5398 struct tog_color *tc;
5400 err = got_object_id_str(&id_str, &s->blamed_commit->id);
5401 if (err)
5402 return err;
5404 rewind(blame->f);
5405 werase(view->window);
5407 if (asprintf(&line, "commit %s", id_str) == -1) {
5408 err = got_error_from_errno("asprintf");
5409 free(id_str);
5410 return err;
5413 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5414 free(line);
5415 line = NULL;
5416 if (err)
5417 return err;
5418 if (view_needs_focus_indication(view))
5419 wstandout(view->window);
5420 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5421 if (tc)
5422 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5423 waddwstr(view->window, wline);
5424 while (width++ < view->ncols)
5425 waddch(view->window, ' ');
5426 if (tc)
5427 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5428 if (view_needs_focus_indication(view))
5429 wstandend(view->window);
5430 free(wline);
5431 wline = NULL;
5433 if (view->gline > blame->nlines)
5434 view->gline = blame->nlines;
5436 if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5437 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5438 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5439 free(id_str);
5440 return got_error_from_errno("asprintf");
5442 free(id_str);
5443 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5444 free(line);
5445 line = NULL;
5446 if (err)
5447 return err;
5448 waddwstr(view->window, wline);
5449 free(wline);
5450 wline = NULL;
5451 if (width < view->ncols - 1)
5452 waddch(view->window, '\n');
5454 s->eof = 0;
5455 view->maxx = 0;
5456 while (nprinted < view->nlines - 2) {
5457 linelen = getline(&line, &linesize, blame->f);
5458 if (linelen == -1) {
5459 if (feof(blame->f)) {
5460 s->eof = 1;
5461 break;
5463 free(line);
5464 return got_ferror(blame->f, GOT_ERR_IO);
5466 if (++lineno < s->first_displayed_line)
5467 continue;
5468 if (view->gline && !gotoline(view, &lineno, &nprinted))
5469 continue;
5471 /* Set view->maxx based on full line length. */
5472 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5473 if (err) {
5474 free(line);
5475 return err;
5477 free(wline);
5478 wline = NULL;
5479 view->maxx = MAX(view->maxx, width);
5481 if (nprinted == s->selected_line - 1)
5482 wstandout(view->window);
5484 if (blame->nlines > 0) {
5485 blame_line = &blame->lines[lineno - 1];
5486 if (blame_line->annotated && prev_id &&
5487 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5488 !(nprinted == s->selected_line - 1)) {
5489 waddstr(view->window, " ");
5490 } else if (blame_line->annotated) {
5491 char *id_str;
5492 err = got_object_id_str(&id_str,
5493 blame_line->id);
5494 if (err) {
5495 free(line);
5496 return err;
5498 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5499 if (tc)
5500 wattr_on(view->window,
5501 COLOR_PAIR(tc->colorpair), NULL);
5502 wprintw(view->window, "%.8s", id_str);
5503 if (tc)
5504 wattr_off(view->window,
5505 COLOR_PAIR(tc->colorpair), NULL);
5506 free(id_str);
5507 prev_id = blame_line->id;
5508 } else {
5509 waddstr(view->window, "........");
5510 prev_id = NULL;
5512 } else {
5513 waddstr(view->window, "........");
5514 prev_id = NULL;
5517 if (nprinted == s->selected_line - 1)
5518 wstandend(view->window);
5519 waddstr(view->window, " ");
5521 if (view->ncols <= 9) {
5522 width = 9;
5523 } else if (s->first_displayed_line + nprinted ==
5524 s->matched_line &&
5525 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5526 err = add_matched_line(&width, line, view->ncols - 9, 9,
5527 view->window, view->x, regmatch);
5528 if (err) {
5529 free(line);
5530 return err;
5532 width += 9;
5533 } else {
5534 int skip;
5535 err = format_line(&wline, &width, &skip, line,
5536 view->x, view->ncols - 9, 9, 1);
5537 if (err) {
5538 free(line);
5539 return err;
5541 waddwstr(view->window, &wline[skip]);
5542 width += 9;
5543 free(wline);
5544 wline = NULL;
5547 if (width <= view->ncols - 1)
5548 waddch(view->window, '\n');
5549 if (++nprinted == 1)
5550 s->first_displayed_line = lineno;
5552 free(line);
5553 s->last_displayed_line = lineno;
5555 view_border(view);
5557 return NULL;
5560 static const struct got_error *
5561 blame_cb(void *arg, int nlines, int lineno,
5562 struct got_commit_object *commit, struct got_object_id *id)
5564 const struct got_error *err = NULL;
5565 struct tog_blame_cb_args *a = arg;
5566 struct tog_blame_line *line;
5567 int errcode;
5569 if (nlines != a->nlines ||
5570 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5571 return got_error(GOT_ERR_RANGE);
5573 errcode = pthread_mutex_lock(&tog_mutex);
5574 if (errcode)
5575 return got_error_set_errno(errcode, "pthread_mutex_lock");
5577 if (*a->quit) { /* user has quit the blame view */
5578 err = got_error(GOT_ERR_ITER_COMPLETED);
5579 goto done;
5582 if (lineno == -1)
5583 goto done; /* no change in this commit */
5585 line = &a->lines[lineno - 1];
5586 if (line->annotated)
5587 goto done;
5589 line->id = got_object_id_dup(id);
5590 if (line->id == NULL) {
5591 err = got_error_from_errno("got_object_id_dup");
5592 goto done;
5594 line->annotated = 1;
5595 done:
5596 errcode = pthread_mutex_unlock(&tog_mutex);
5597 if (errcode)
5598 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5599 return err;
5602 static void *
5603 blame_thread(void *arg)
5605 const struct got_error *err, *close_err;
5606 struct tog_blame_thread_args *ta = arg;
5607 struct tog_blame_cb_args *a = ta->cb_args;
5608 int errcode, fd1 = -1, fd2 = -1;
5609 FILE *f1 = NULL, *f2 = NULL;
5611 fd1 = got_opentempfd();
5612 if (fd1 == -1)
5613 return (void *)got_error_from_errno("got_opentempfd");
5615 fd2 = got_opentempfd();
5616 if (fd2 == -1) {
5617 err = got_error_from_errno("got_opentempfd");
5618 goto done;
5621 f1 = got_opentemp();
5622 if (f1 == NULL) {
5623 err = (void *)got_error_from_errno("got_opentemp");
5624 goto done;
5626 f2 = got_opentemp();
5627 if (f2 == NULL) {
5628 err = (void *)got_error_from_errno("got_opentemp");
5629 goto done;
5632 err = block_signals_used_by_main_thread();
5633 if (err)
5634 goto done;
5636 err = got_blame(ta->path, a->commit_id, ta->repo,
5637 tog_diff_algo, blame_cb, ta->cb_args,
5638 ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5639 if (err && err->code == GOT_ERR_CANCELLED)
5640 err = NULL;
5642 errcode = pthread_mutex_lock(&tog_mutex);
5643 if (errcode) {
5644 err = got_error_set_errno(errcode, "pthread_mutex_lock");
5645 goto done;
5648 close_err = got_repo_close(ta->repo);
5649 if (err == NULL)
5650 err = close_err;
5651 ta->repo = NULL;
5652 *ta->complete = 1;
5654 errcode = pthread_mutex_unlock(&tog_mutex);
5655 if (errcode && err == NULL)
5656 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5658 done:
5659 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5660 err = got_error_from_errno("close");
5661 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5662 err = got_error_from_errno("close");
5663 if (f1 && fclose(f1) == EOF && err == NULL)
5664 err = got_error_from_errno("fclose");
5665 if (f2 && fclose(f2) == EOF && err == NULL)
5666 err = got_error_from_errno("fclose");
5668 return (void *)err;
5671 static struct got_object_id *
5672 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5673 int first_displayed_line, int selected_line)
5675 struct tog_blame_line *line;
5677 if (nlines <= 0)
5678 return NULL;
5680 line = &lines[first_displayed_line - 1 + selected_line - 1];
5681 if (!line->annotated)
5682 return NULL;
5684 return line->id;
5687 static struct got_object_id *
5688 get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5689 int lineno)
5691 struct tog_blame_line *line;
5693 if (nlines <= 0 || lineno >= nlines)
5694 return NULL;
5696 line = &lines[lineno - 1];
5697 if (!line->annotated)
5698 return NULL;
5700 return line->id;
5703 static const struct got_error *
5704 stop_blame(struct tog_blame *blame)
5706 const struct got_error *err = NULL;
5707 int i;
5709 if (blame->thread) {
5710 int errcode;
5711 errcode = pthread_mutex_unlock(&tog_mutex);
5712 if (errcode)
5713 return got_error_set_errno(errcode,
5714 "pthread_mutex_unlock");
5715 errcode = pthread_join(blame->thread, (void **)&err);
5716 if (errcode)
5717 return got_error_set_errno(errcode, "pthread_join");
5718 errcode = pthread_mutex_lock(&tog_mutex);
5719 if (errcode)
5720 return got_error_set_errno(errcode,
5721 "pthread_mutex_lock");
5722 if (err && err->code == GOT_ERR_ITER_COMPLETED)
5723 err = NULL;
5724 blame->thread = NULL;
5726 if (blame->thread_args.repo) {
5727 const struct got_error *close_err;
5728 close_err = got_repo_close(blame->thread_args.repo);
5729 if (err == NULL)
5730 err = close_err;
5731 blame->thread_args.repo = NULL;
5733 if (blame->f) {
5734 if (fclose(blame->f) == EOF && err == NULL)
5735 err = got_error_from_errno("fclose");
5736 blame->f = NULL;
5738 if (blame->lines) {
5739 for (i = 0; i < blame->nlines; i++)
5740 free(blame->lines[i].id);
5741 free(blame->lines);
5742 blame->lines = NULL;
5744 free(blame->cb_args.commit_id);
5745 blame->cb_args.commit_id = NULL;
5746 if (blame->pack_fds) {
5747 const struct got_error *pack_err =
5748 got_repo_pack_fds_close(blame->pack_fds);
5749 if (err == NULL)
5750 err = pack_err;
5751 blame->pack_fds = NULL;
5753 return err;
5756 static const struct got_error *
5757 cancel_blame_view(void *arg)
5759 const struct got_error *err = NULL;
5760 int *done = arg;
5761 int errcode;
5763 errcode = pthread_mutex_lock(&tog_mutex);
5764 if (errcode)
5765 return got_error_set_errno(errcode,
5766 "pthread_mutex_unlock");
5768 if (*done)
5769 err = got_error(GOT_ERR_CANCELLED);
5771 errcode = pthread_mutex_unlock(&tog_mutex);
5772 if (errcode)
5773 return got_error_set_errno(errcode,
5774 "pthread_mutex_lock");
5776 return err;
5779 static const struct got_error *
5780 run_blame(struct tog_view *view)
5782 struct tog_blame_view_state *s = &view->state.blame;
5783 struct tog_blame *blame = &s->blame;
5784 const struct got_error *err = NULL;
5785 struct got_commit_object *commit = NULL;
5786 struct got_blob_object *blob = NULL;
5787 struct got_repository *thread_repo = NULL;
5788 struct got_object_id *obj_id = NULL;
5789 int obj_type, fd = -1;
5790 int *pack_fds = NULL;
5792 err = got_object_open_as_commit(&commit, s->repo,
5793 &s->blamed_commit->id);
5794 if (err)
5795 return err;
5797 fd = got_opentempfd();
5798 if (fd == -1) {
5799 err = got_error_from_errno("got_opentempfd");
5800 goto done;
5803 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5804 if (err)
5805 goto done;
5807 err = got_object_get_type(&obj_type, s->repo, obj_id);
5808 if (err)
5809 goto done;
5811 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5812 err = got_error(GOT_ERR_OBJ_TYPE);
5813 goto done;
5816 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5817 if (err)
5818 goto done;
5819 blame->f = got_opentemp();
5820 if (blame->f == NULL) {
5821 err = got_error_from_errno("got_opentemp");
5822 goto done;
5824 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5825 &blame->line_offsets, blame->f, blob);
5826 if (err)
5827 goto done;
5828 if (blame->nlines == 0) {
5829 s->blame_complete = 1;
5830 goto done;
5833 /* Don't include \n at EOF in the blame line count. */
5834 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5835 blame->nlines--;
5837 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5838 if (blame->lines == NULL) {
5839 err = got_error_from_errno("calloc");
5840 goto done;
5843 err = got_repo_pack_fds_open(&pack_fds);
5844 if (err)
5845 goto done;
5846 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5847 pack_fds);
5848 if (err)
5849 goto done;
5851 blame->pack_fds = pack_fds;
5852 blame->cb_args.view = view;
5853 blame->cb_args.lines = blame->lines;
5854 blame->cb_args.nlines = blame->nlines;
5855 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5856 if (blame->cb_args.commit_id == NULL) {
5857 err = got_error_from_errno("got_object_id_dup");
5858 goto done;
5860 blame->cb_args.quit = &s->done;
5862 blame->thread_args.path = s->path;
5863 blame->thread_args.repo = thread_repo;
5864 blame->thread_args.cb_args = &blame->cb_args;
5865 blame->thread_args.complete = &s->blame_complete;
5866 blame->thread_args.cancel_cb = cancel_blame_view;
5867 blame->thread_args.cancel_arg = &s->done;
5868 s->blame_complete = 0;
5870 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5871 s->first_displayed_line = 1;
5872 s->last_displayed_line = view->nlines;
5873 s->selected_line = 1;
5875 s->matched_line = 0;
5877 done:
5878 if (commit)
5879 got_object_commit_close(commit);
5880 if (fd != -1 && close(fd) == -1 && err == NULL)
5881 err = got_error_from_errno("close");
5882 if (blob)
5883 got_object_blob_close(blob);
5884 free(obj_id);
5885 if (err)
5886 stop_blame(blame);
5887 return err;
5890 static const struct got_error *
5891 open_blame_view(struct tog_view *view, char *path,
5892 struct got_object_id *commit_id, struct got_repository *repo)
5894 const struct got_error *err = NULL;
5895 struct tog_blame_view_state *s = &view->state.blame;
5897 STAILQ_INIT(&s->blamed_commits);
5899 s->path = strdup(path);
5900 if (s->path == NULL)
5901 return got_error_from_errno("strdup");
5903 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5904 if (err) {
5905 free(s->path);
5906 return err;
5909 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5910 s->first_displayed_line = 1;
5911 s->last_displayed_line = view->nlines;
5912 s->selected_line = 1;
5913 s->blame_complete = 0;
5914 s->repo = repo;
5915 s->commit_id = commit_id;
5916 memset(&s->blame, 0, sizeof(s->blame));
5918 STAILQ_INIT(&s->colors);
5919 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5920 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5921 get_color_value("TOG_COLOR_COMMIT"));
5922 if (err)
5923 return err;
5926 view->show = show_blame_view;
5927 view->input = input_blame_view;
5928 view->reset = reset_blame_view;
5929 view->close = close_blame_view;
5930 view->search_start = search_start_blame_view;
5931 view->search_next = search_next_blame_view;
5933 return run_blame(view);
5936 static const struct got_error *
5937 close_blame_view(struct tog_view *view)
5939 const struct got_error *err = NULL;
5940 struct tog_blame_view_state *s = &view->state.blame;
5942 if (s->blame.thread)
5943 err = stop_blame(&s->blame);
5945 while (!STAILQ_EMPTY(&s->blamed_commits)) {
5946 struct got_object_qid *blamed_commit;
5947 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5948 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5949 got_object_qid_free(blamed_commit);
5952 free(s->path);
5953 free_colors(&s->colors);
5954 return err;
5957 static const struct got_error *
5958 search_start_blame_view(struct tog_view *view)
5960 struct tog_blame_view_state *s = &view->state.blame;
5962 s->matched_line = 0;
5963 return NULL;
5966 static const struct got_error *
5967 search_next_blame_view(struct tog_view *view)
5969 struct tog_blame_view_state *s = &view->state.blame;
5970 const struct got_error *err = NULL;
5971 int lineno;
5972 char *line = NULL;
5973 size_t linesize = 0;
5974 ssize_t linelen;
5976 if (!view->searching) {
5977 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5978 return NULL;
5981 if (s->matched_line) {
5982 if (view->searching == TOG_SEARCH_FORWARD)
5983 lineno = s->matched_line + 1;
5984 else
5985 lineno = s->matched_line - 1;
5986 } else
5987 lineno = s->first_displayed_line - 1 + s->selected_line;
5989 while (1) {
5990 off_t offset;
5992 if (lineno <= 0 || lineno > s->blame.nlines) {
5993 if (s->matched_line == 0) {
5994 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5995 break;
5998 if (view->searching == TOG_SEARCH_FORWARD)
5999 lineno = 1;
6000 else
6001 lineno = s->blame.nlines;
6004 offset = s->blame.line_offsets[lineno - 1];
6005 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6006 free(line);
6007 return got_error_from_errno("fseeko");
6009 linelen = getline(&line, &linesize, s->blame.f);
6010 if (linelen != -1) {
6011 char *exstr;
6012 err = expand_tab(&exstr, line);
6013 if (err)
6014 break;
6015 if (match_line(exstr, &view->regex, 1,
6016 &view->regmatch)) {
6017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6018 s->matched_line = lineno;
6019 free(exstr);
6020 break;
6022 free(exstr);
6024 if (view->searching == TOG_SEARCH_FORWARD)
6025 lineno++;
6026 else
6027 lineno--;
6029 free(line);
6031 if (s->matched_line) {
6032 s->first_displayed_line = s->matched_line;
6033 s->selected_line = 1;
6036 return err;
6039 static const struct got_error *
6040 show_blame_view(struct tog_view *view)
6042 const struct got_error *err = NULL;
6043 struct tog_blame_view_state *s = &view->state.blame;
6044 int errcode;
6046 if (s->blame.thread == NULL && !s->blame_complete) {
6047 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6048 &s->blame.thread_args);
6049 if (errcode)
6050 return got_error_set_errno(errcode, "pthread_create");
6052 halfdelay(1); /* fast refresh while annotating */
6055 if (s->blame_complete)
6056 halfdelay(10); /* disable fast refresh */
6058 err = draw_blame(view);
6060 view_border(view);
6061 return err;
6064 static const struct got_error *
6065 log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6066 struct got_repository *repo, struct got_object_id *id)
6068 struct tog_view *log_view;
6069 const struct got_error *err = NULL;
6071 *new_view = NULL;
6073 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6074 if (log_view == NULL)
6075 return got_error_from_errno("view_open");
6077 err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6078 if (err)
6079 view_close(log_view);
6080 else
6081 *new_view = log_view;
6083 return err;
6086 static const struct got_error *
6087 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6089 const struct got_error *err = NULL, *thread_err = NULL;
6090 struct tog_view *diff_view;
6091 struct tog_blame_view_state *s = &view->state.blame;
6092 int eos, nscroll, begin_y = 0, begin_x = 0;
6094 eos = nscroll = view->nlines - 2;
6095 if (view_is_hsplit_top(view))
6096 --eos; /* border */
6098 switch (ch) {
6099 case '0':
6100 view->x = 0;
6101 break;
6102 case '$':
6103 view->x = MAX(view->maxx - view->ncols / 3, 0);
6104 view->count = 0;
6105 break;
6106 case KEY_RIGHT:
6107 case 'l':
6108 if (view->x + view->ncols / 3 < view->maxx)
6109 view->x += 2; /* move two columns right */
6110 else
6111 view->count = 0;
6112 break;
6113 case KEY_LEFT:
6114 case 'h':
6115 view->x -= MIN(view->x, 2); /* move two columns back */
6116 if (view->x <= 0)
6117 view->count = 0;
6118 break;
6119 case 'q':
6120 s->done = 1;
6121 break;
6122 case 'g':
6123 case KEY_HOME:
6124 s->selected_line = 1;
6125 s->first_displayed_line = 1;
6126 view->count = 0;
6127 break;
6128 case 'G':
6129 case KEY_END:
6130 if (s->blame.nlines < eos) {
6131 s->selected_line = s->blame.nlines;
6132 s->first_displayed_line = 1;
6133 } else {
6134 s->selected_line = eos;
6135 s->first_displayed_line = s->blame.nlines - (eos - 1);
6137 view->count = 0;
6138 break;
6139 case 'k':
6140 case KEY_UP:
6141 case CTRL('p'):
6142 if (s->selected_line > 1)
6143 s->selected_line--;
6144 else if (s->selected_line == 1 &&
6145 s->first_displayed_line > 1)
6146 s->first_displayed_line--;
6147 else
6148 view->count = 0;
6149 break;
6150 case CTRL('u'):
6151 case 'u':
6152 nscroll /= 2;
6153 /* FALL THROUGH */
6154 case KEY_PPAGE:
6155 case CTRL('b'):
6156 case 'b':
6157 if (s->first_displayed_line == 1) {
6158 if (view->count > 1)
6159 nscroll += nscroll;
6160 s->selected_line = MAX(1, s->selected_line - nscroll);
6161 view->count = 0;
6162 break;
6164 if (s->first_displayed_line > nscroll)
6165 s->first_displayed_line -= nscroll;
6166 else
6167 s->first_displayed_line = 1;
6168 break;
6169 case 'j':
6170 case KEY_DOWN:
6171 case CTRL('n'):
6172 if (s->selected_line < eos && s->first_displayed_line +
6173 s->selected_line <= s->blame.nlines)
6174 s->selected_line++;
6175 else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6176 s->first_displayed_line++;
6177 else
6178 view->count = 0;
6179 break;
6180 case 'c':
6181 case 'p': {
6182 struct got_object_id *id = NULL;
6184 view->count = 0;
6185 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6186 s->first_displayed_line, s->selected_line);
6187 if (id == NULL)
6188 break;
6189 if (ch == 'p') {
6190 struct got_commit_object *commit, *pcommit;
6191 struct got_object_qid *pid;
6192 struct got_object_id *blob_id = NULL;
6193 int obj_type;
6194 err = got_object_open_as_commit(&commit,
6195 s->repo, id);
6196 if (err)
6197 break;
6198 pid = STAILQ_FIRST(
6199 got_object_commit_get_parent_ids(commit));
6200 if (pid == NULL) {
6201 got_object_commit_close(commit);
6202 break;
6204 /* Check if path history ends here. */
6205 err = got_object_open_as_commit(&pcommit,
6206 s->repo, &pid->id);
6207 if (err)
6208 break;
6209 err = got_object_id_by_path(&blob_id, s->repo,
6210 pcommit, s->path);
6211 got_object_commit_close(pcommit);
6212 if (err) {
6213 if (err->code == GOT_ERR_NO_TREE_ENTRY)
6214 err = NULL;
6215 got_object_commit_close(commit);
6216 break;
6218 err = got_object_get_type(&obj_type, s->repo,
6219 blob_id);
6220 free(blob_id);
6221 /* Can't blame non-blob type objects. */
6222 if (obj_type != GOT_OBJ_TYPE_BLOB) {
6223 got_object_commit_close(commit);
6224 break;
6226 err = got_object_qid_alloc(&s->blamed_commit,
6227 &pid->id);
6228 got_object_commit_close(commit);
6229 } else {
6230 if (got_object_id_cmp(id,
6231 &s->blamed_commit->id) == 0)
6232 break;
6233 err = got_object_qid_alloc(&s->blamed_commit,
6234 id);
6236 if (err)
6237 break;
6238 s->done = 1;
6239 thread_err = stop_blame(&s->blame);
6240 s->done = 0;
6241 if (thread_err)
6242 break;
6243 STAILQ_INSERT_HEAD(&s->blamed_commits,
6244 s->blamed_commit, entry);
6245 err = run_blame(view);
6246 if (err)
6247 break;
6248 break;
6250 case 'C': {
6251 struct got_object_qid *first;
6253 view->count = 0;
6254 first = STAILQ_FIRST(&s->blamed_commits);
6255 if (!got_object_id_cmp(&first->id, s->commit_id))
6256 break;
6257 s->done = 1;
6258 thread_err = stop_blame(&s->blame);
6259 s->done = 0;
6260 if (thread_err)
6261 break;
6262 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6263 got_object_qid_free(s->blamed_commit);
6264 s->blamed_commit =
6265 STAILQ_FIRST(&s->blamed_commits);
6266 err = run_blame(view);
6267 if (err)
6268 break;
6269 break;
6271 case 'L':
6272 view->count = 0;
6273 s->id_to_log = get_selected_commit_id(s->blame.lines,
6274 s->blame.nlines, s->first_displayed_line, s->selected_line);
6275 if (s->id_to_log)
6276 err = view_request_new(new_view, view, TOG_VIEW_LOG);
6277 break;
6278 case KEY_ENTER:
6279 case '\r': {
6280 struct got_object_id *id = NULL;
6281 struct got_object_qid *pid;
6282 struct got_commit_object *commit = NULL;
6284 view->count = 0;
6285 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6286 s->first_displayed_line, s->selected_line);
6287 if (id == NULL)
6288 break;
6289 err = got_object_open_as_commit(&commit, s->repo, id);
6290 if (err)
6291 break;
6292 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6293 if (*new_view) {
6294 /* traversed from diff view, release diff resources */
6295 err = close_diff_view(*new_view);
6296 if (err)
6297 break;
6298 diff_view = *new_view;
6299 } else {
6300 if (view_is_parent_view(view))
6301 view_get_split(view, &begin_y, &begin_x);
6303 diff_view = view_open(0, 0, begin_y, begin_x,
6304 TOG_VIEW_DIFF);
6305 if (diff_view == NULL) {
6306 got_object_commit_close(commit);
6307 err = got_error_from_errno("view_open");
6308 break;
6311 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6312 id, NULL, NULL, 3, 0, 0, view, s->repo);
6313 got_object_commit_close(commit);
6314 if (err) {
6315 view_close(diff_view);
6316 break;
6318 s->last_diffed_line = s->first_displayed_line - 1 +
6319 s->selected_line;
6320 if (*new_view)
6321 break; /* still open from active diff view */
6322 if (view_is_parent_view(view) &&
6323 view->mode == TOG_VIEW_SPLIT_HRZN) {
6324 err = view_init_hsplit(view, begin_y);
6325 if (err)
6326 break;
6329 view->focussed = 0;
6330 diff_view->focussed = 1;
6331 diff_view->mode = view->mode;
6332 diff_view->nlines = view->lines - begin_y;
6333 if (view_is_parent_view(view)) {
6334 view_transfer_size(diff_view, view);
6335 err = view_close_child(view);
6336 if (err)
6337 break;
6338 err = view_set_child(view, diff_view);
6339 if (err)
6340 break;
6341 view->focus_child = 1;
6342 } else
6343 *new_view = diff_view;
6344 if (err)
6345 break;
6346 break;
6348 case CTRL('d'):
6349 case 'd':
6350 nscroll /= 2;
6351 /* FALL THROUGH */
6352 case KEY_NPAGE:
6353 case CTRL('f'):
6354 case 'f':
6355 case ' ':
6356 if (s->last_displayed_line >= s->blame.nlines &&
6357 s->selected_line >= MIN(s->blame.nlines,
6358 view->nlines - 2)) {
6359 view->count = 0;
6360 break;
6362 if (s->last_displayed_line >= s->blame.nlines &&
6363 s->selected_line < view->nlines - 2) {
6364 s->selected_line +=
6365 MIN(nscroll, s->last_displayed_line -
6366 s->first_displayed_line - s->selected_line + 1);
6368 if (s->last_displayed_line + nscroll <= s->blame.nlines)
6369 s->first_displayed_line += nscroll;
6370 else
6371 s->first_displayed_line =
6372 s->blame.nlines - (view->nlines - 3);
6373 break;
6374 case KEY_RESIZE:
6375 if (s->selected_line > view->nlines - 2) {
6376 s->selected_line = MIN(s->blame.nlines,
6377 view->nlines - 2);
6379 break;
6380 default:
6381 view->count = 0;
6382 break;
6384 return thread_err ? thread_err : err;
6387 static const struct got_error *
6388 reset_blame_view(struct tog_view *view)
6390 const struct got_error *err;
6391 struct tog_blame_view_state *s = &view->state.blame;
6393 view->count = 0;
6394 s->done = 1;
6395 err = stop_blame(&s->blame);
6396 s->done = 0;
6397 if (err)
6398 return err;
6399 return run_blame(view);
6402 static const struct got_error *
6403 cmd_blame(int argc, char *argv[])
6405 const struct got_error *error;
6406 struct got_repository *repo = NULL;
6407 struct got_worktree *worktree = NULL;
6408 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6409 char *link_target = NULL;
6410 struct got_object_id *commit_id = NULL;
6411 struct got_commit_object *commit = NULL;
6412 char *commit_id_str = NULL;
6413 int ch;
6414 struct tog_view *view;
6415 int *pack_fds = NULL;
6417 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6418 switch (ch) {
6419 case 'c':
6420 commit_id_str = optarg;
6421 break;
6422 case 'r':
6423 repo_path = realpath(optarg, NULL);
6424 if (repo_path == NULL)
6425 return got_error_from_errno2("realpath",
6426 optarg);
6427 break;
6428 default:
6429 usage_blame();
6430 /* NOTREACHED */
6434 argc -= optind;
6435 argv += optind;
6437 if (argc != 1)
6438 usage_blame();
6440 error = got_repo_pack_fds_open(&pack_fds);
6441 if (error != NULL)
6442 goto done;
6444 if (repo_path == NULL) {
6445 cwd = getcwd(NULL, 0);
6446 if (cwd == NULL)
6447 return got_error_from_errno("getcwd");
6448 error = got_worktree_open(&worktree, cwd);
6449 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6450 goto done;
6451 if (worktree)
6452 repo_path =
6453 strdup(got_worktree_get_repo_path(worktree));
6454 else
6455 repo_path = strdup(cwd);
6456 if (repo_path == NULL) {
6457 error = got_error_from_errno("strdup");
6458 goto done;
6462 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6463 if (error != NULL)
6464 goto done;
6466 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6467 worktree);
6468 if (error)
6469 goto done;
6471 init_curses();
6473 error = apply_unveil(got_repo_get_path(repo), NULL);
6474 if (error)
6475 goto done;
6477 error = tog_load_refs(repo, 0);
6478 if (error)
6479 goto done;
6481 if (commit_id_str == NULL) {
6482 struct got_reference *head_ref;
6483 error = got_ref_open(&head_ref, repo, worktree ?
6484 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6485 if (error != NULL)
6486 goto done;
6487 error = got_ref_resolve(&commit_id, repo, head_ref);
6488 got_ref_close(head_ref);
6489 } else {
6490 error = got_repo_match_object_id(&commit_id, NULL,
6491 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6493 if (error != NULL)
6494 goto done;
6496 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6497 if (view == NULL) {
6498 error = got_error_from_errno("view_open");
6499 goto done;
6502 error = got_object_open_as_commit(&commit, repo, commit_id);
6503 if (error)
6504 goto done;
6506 error = got_object_resolve_symlinks(&link_target, in_repo_path,
6507 commit, repo);
6508 if (error)
6509 goto done;
6511 error = open_blame_view(view, link_target ? link_target : in_repo_path,
6512 commit_id, repo);
6513 if (error)
6514 goto done;
6515 if (worktree) {
6516 /* Release work tree lock. */
6517 got_worktree_close(worktree);
6518 worktree = NULL;
6520 error = view_loop(view);
6521 done:
6522 free(repo_path);
6523 free(in_repo_path);
6524 free(link_target);
6525 free(cwd);
6526 free(commit_id);
6527 if (commit)
6528 got_object_commit_close(commit);
6529 if (worktree)
6530 got_worktree_close(worktree);
6531 if (repo) {
6532 const struct got_error *close_err = got_repo_close(repo);
6533 if (error == NULL)
6534 error = close_err;
6536 if (pack_fds) {
6537 const struct got_error *pack_err =
6538 got_repo_pack_fds_close(pack_fds);
6539 if (error == NULL)
6540 error = pack_err;
6542 tog_free_refs();
6543 return error;
6546 static const struct got_error *
6547 draw_tree_entries(struct tog_view *view, const char *parent_path)
6549 struct tog_tree_view_state *s = &view->state.tree;
6550 const struct got_error *err = NULL;
6551 struct got_tree_entry *te;
6552 wchar_t *wline;
6553 char *index = NULL;
6554 struct tog_color *tc;
6555 int width, n, nentries, i = 1;
6556 int limit = view->nlines;
6558 s->ndisplayed = 0;
6559 if (view_is_hsplit_top(view))
6560 --limit; /* border */
6562 werase(view->window);
6564 if (limit == 0)
6565 return NULL;
6567 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6568 0, 0);
6569 if (err)
6570 return err;
6571 if (view_needs_focus_indication(view))
6572 wstandout(view->window);
6573 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6574 if (tc)
6575 wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6576 waddwstr(view->window, wline);
6577 free(wline);
6578 wline = NULL;
6579 while (width++ < view->ncols)
6580 waddch(view->window, ' ');
6581 if (tc)
6582 wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6583 if (view_needs_focus_indication(view))
6584 wstandend(view->window);
6585 if (--limit <= 0)
6586 return NULL;
6588 i += s->selected;
6589 if (s->first_displayed_entry) {
6590 i += got_tree_entry_get_index(s->first_displayed_entry);
6591 if (s->tree != s->root)
6592 ++i; /* account for ".." entry */
6594 nentries = got_object_tree_get_nentries(s->tree);
6595 if (asprintf(&index, "[%d/%d] %s",
6596 i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6597 return got_error_from_errno("asprintf");
6598 err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6599 free(index);
6600 if (err)
6601 return err;
6602 waddwstr(view->window, wline);
6603 free(wline);
6604 wline = NULL;
6605 if (width < view->ncols - 1)
6606 waddch(view->window, '\n');
6607 if (--limit <= 0)
6608 return NULL;
6609 waddch(view->window, '\n');
6610 if (--limit <= 0)
6611 return NULL;
6613 if (s->first_displayed_entry == NULL) {
6614 te = got_object_tree_get_first_entry(s->tree);
6615 if (s->selected == 0) {
6616 if (view->focussed)
6617 wstandout(view->window);
6618 s->selected_entry = NULL;
6620 waddstr(view->window, " ..\n"); /* parent directory */
6621 if (s->selected == 0 && view->focussed)
6622 wstandend(view->window);
6623 s->ndisplayed++;
6624 if (--limit <= 0)
6625 return NULL;
6626 n = 1;
6627 } else {
6628 n = 0;
6629 te = s->first_displayed_entry;
6632 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6633 char *line = NULL, *id_str = NULL, *link_target = NULL;
6634 const char *modestr = "";
6635 mode_t mode;
6637 te = got_object_tree_get_entry(s->tree, i);
6638 mode = got_tree_entry_get_mode(te);
6640 if (s->show_ids) {
6641 err = got_object_id_str(&id_str,
6642 got_tree_entry_get_id(te));
6643 if (err)
6644 return got_error_from_errno(
6645 "got_object_id_str");
6647 if (got_object_tree_entry_is_submodule(te))
6648 modestr = "$";
6649 else if (S_ISLNK(mode)) {
6650 int i;
6652 err = got_tree_entry_get_symlink_target(&link_target,
6653 te, s->repo);
6654 if (err) {
6655 free(id_str);
6656 return err;
6658 for (i = 0; i < strlen(link_target); i++) {
6659 if (!isprint((unsigned char)link_target[i]))
6660 link_target[i] = '?';
6662 modestr = "@";
6664 else if (S_ISDIR(mode))
6665 modestr = "/";
6666 else if (mode & S_IXUSR)
6667 modestr = "*";
6668 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6669 got_tree_entry_get_name(te), modestr,
6670 link_target ? " -> ": "",
6671 link_target ? link_target : "") == -1) {
6672 free(id_str);
6673 free(link_target);
6674 return got_error_from_errno("asprintf");
6676 free(id_str);
6677 free(link_target);
6678 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6679 0, 0);
6680 if (err) {
6681 free(line);
6682 break;
6684 if (n == s->selected) {
6685 if (view->focussed)
6686 wstandout(view->window);
6687 s->selected_entry = te;
6689 tc = match_color(&s->colors, line);
6690 if (tc)
6691 wattr_on(view->window,
6692 COLOR_PAIR(tc->colorpair), NULL);
6693 waddwstr(view->window, wline);
6694 if (tc)
6695 wattr_off(view->window,
6696 COLOR_PAIR(tc->colorpair), NULL);
6697 if (width < view->ncols - 1)
6698 waddch(view->window, '\n');
6699 if (n == s->selected && view->focussed)
6700 wstandend(view->window);
6701 free(line);
6702 free(wline);
6703 wline = NULL;
6704 n++;
6705 s->ndisplayed++;
6706 s->last_displayed_entry = te;
6707 if (--limit <= 0)
6708 break;
6711 return err;
6714 static void
6715 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6717 struct got_tree_entry *te;
6718 int isroot = s->tree == s->root;
6719 int i = 0;
6721 if (s->first_displayed_entry == NULL)
6722 return;
6724 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6725 while (i++ < maxscroll) {
6726 if (te == NULL) {
6727 if (!isroot)
6728 s->first_displayed_entry = NULL;
6729 break;
6731 s->first_displayed_entry = te;
6732 te = got_tree_entry_get_prev(s->tree, te);
6736 static const struct got_error *
6737 tree_scroll_down(struct tog_view *view, int maxscroll)
6739 struct tog_tree_view_state *s = &view->state.tree;
6740 struct got_tree_entry *next, *last;
6741 int n = 0;
6743 if (s->first_displayed_entry)
6744 next = got_tree_entry_get_next(s->tree,
6745 s->first_displayed_entry);
6746 else
6747 next = got_object_tree_get_first_entry(s->tree);
6749 last = s->last_displayed_entry;
6750 while (next && n++ < maxscroll) {
6751 if (last) {
6752 s->last_displayed_entry = last;
6753 last = got_tree_entry_get_next(s->tree, last);
6755 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6756 s->first_displayed_entry = next;
6757 next = got_tree_entry_get_next(s->tree, next);
6761 return NULL;
6764 static const struct got_error *
6765 tree_entry_path(char **path, struct tog_parent_trees *parents,
6766 struct got_tree_entry *te)
6768 const struct got_error *err = NULL;
6769 struct tog_parent_tree *pt;
6770 size_t len = 2; /* for leading slash and NUL */
6772 TAILQ_FOREACH(pt, parents, entry)
6773 len += strlen(got_tree_entry_get_name(pt->selected_entry))
6774 + 1 /* slash */;
6775 if (te)
6776 len += strlen(got_tree_entry_get_name(te));
6778 *path = calloc(1, len);
6779 if (path == NULL)
6780 return got_error_from_errno("calloc");
6782 (*path)[0] = '/';
6783 pt = TAILQ_LAST(parents, tog_parent_trees);
6784 while (pt) {
6785 const char *name = got_tree_entry_get_name(pt->selected_entry);
6786 if (strlcat(*path, name, len) >= len) {
6787 err = got_error(GOT_ERR_NO_SPACE);
6788 goto done;
6790 if (strlcat(*path, "/", len) >= len) {
6791 err = got_error(GOT_ERR_NO_SPACE);
6792 goto done;
6794 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6796 if (te) {
6797 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6798 err = got_error(GOT_ERR_NO_SPACE);
6799 goto done;
6802 done:
6803 if (err) {
6804 free(*path);
6805 *path = NULL;
6807 return err;
6810 static const struct got_error *
6811 blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6812 struct got_tree_entry *te, struct tog_parent_trees *parents,
6813 struct got_object_id *commit_id, struct got_repository *repo)
6815 const struct got_error *err = NULL;
6816 char *path;
6817 struct tog_view *blame_view;
6819 *new_view = NULL;
6821 err = tree_entry_path(&path, parents, te);
6822 if (err)
6823 return err;
6825 blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6826 if (blame_view == NULL) {
6827 err = got_error_from_errno("view_open");
6828 goto done;
6831 err = open_blame_view(blame_view, path, commit_id, repo);
6832 if (err) {
6833 if (err->code == GOT_ERR_CANCELLED)
6834 err = NULL;
6835 view_close(blame_view);
6836 } else
6837 *new_view = blame_view;
6838 done:
6839 free(path);
6840 return err;
6843 static const struct got_error *
6844 log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6845 struct tog_tree_view_state *s)
6847 struct tog_view *log_view;
6848 const struct got_error *err = NULL;
6849 char *path;
6851 *new_view = NULL;
6853 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6854 if (log_view == NULL)
6855 return got_error_from_errno("view_open");
6857 err = tree_entry_path(&path, &s->parents, s->selected_entry);
6858 if (err)
6859 return err;
6861 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6862 path, 0);
6863 if (err)
6864 view_close(log_view);
6865 else
6866 *new_view = log_view;
6867 free(path);
6868 return err;
6871 static const struct got_error *
6872 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6873 const char *head_ref_name, struct got_repository *repo)
6875 const struct got_error *err = NULL;
6876 char *commit_id_str = NULL;
6877 struct tog_tree_view_state *s = &view->state.tree;
6878 struct got_commit_object *commit = NULL;
6880 TAILQ_INIT(&s->parents);
6881 STAILQ_INIT(&s->colors);
6883 s->commit_id = got_object_id_dup(commit_id);
6884 if (s->commit_id == NULL)
6885 return got_error_from_errno("got_object_id_dup");
6887 err = got_object_open_as_commit(&commit, repo, commit_id);
6888 if (err)
6889 goto done;
6892 * The root is opened here and will be closed when the view is closed.
6893 * Any visited subtrees and their path-wise parents are opened and
6894 * closed on demand.
6896 err = got_object_open_as_tree(&s->root, repo,
6897 got_object_commit_get_tree_id(commit));
6898 if (err)
6899 goto done;
6900 s->tree = s->root;
6902 err = got_object_id_str(&commit_id_str, commit_id);
6903 if (err != NULL)
6904 goto done;
6906 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6907 err = got_error_from_errno("asprintf");
6908 goto done;
6911 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6912 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6913 if (head_ref_name) {
6914 s->head_ref_name = strdup(head_ref_name);
6915 if (s->head_ref_name == NULL) {
6916 err = got_error_from_errno("strdup");
6917 goto done;
6920 s->repo = repo;
6922 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6923 err = add_color(&s->colors, "\\$$",
6924 TOG_COLOR_TREE_SUBMODULE,
6925 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6926 if (err)
6927 goto done;
6928 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6929 get_color_value("TOG_COLOR_TREE_SYMLINK"));
6930 if (err)
6931 goto done;
6932 err = add_color(&s->colors, "/$",
6933 TOG_COLOR_TREE_DIRECTORY,
6934 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6935 if (err)
6936 goto done;
6938 err = add_color(&s->colors, "\\*$",
6939 TOG_COLOR_TREE_EXECUTABLE,
6940 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6941 if (err)
6942 goto done;
6944 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6945 get_color_value("TOG_COLOR_COMMIT"));
6946 if (err)
6947 goto done;
6950 view->show = show_tree_view;
6951 view->input = input_tree_view;
6952 view->close = close_tree_view;
6953 view->search_start = search_start_tree_view;
6954 view->search_next = search_next_tree_view;
6955 done:
6956 free(commit_id_str);
6957 if (commit)
6958 got_object_commit_close(commit);
6959 if (err)
6960 close_tree_view(view);
6961 return err;
6964 static const struct got_error *
6965 close_tree_view(struct tog_view *view)
6967 struct tog_tree_view_state *s = &view->state.tree;
6969 free_colors(&s->colors);
6970 free(s->tree_label);
6971 s->tree_label = NULL;
6972 free(s->commit_id);
6973 s->commit_id = NULL;
6974 free(s->head_ref_name);
6975 s->head_ref_name = NULL;
6976 while (!TAILQ_EMPTY(&s->parents)) {
6977 struct tog_parent_tree *parent;
6978 parent = TAILQ_FIRST(&s->parents);
6979 TAILQ_REMOVE(&s->parents, parent, entry);
6980 if (parent->tree != s->root)
6981 got_object_tree_close(parent->tree);
6982 free(parent);
6985 if (s->tree != NULL && s->tree != s->root)
6986 got_object_tree_close(s->tree);
6987 if (s->root)
6988 got_object_tree_close(s->root);
6989 return NULL;
6992 static const struct got_error *
6993 search_start_tree_view(struct tog_view *view)
6995 struct tog_tree_view_state *s = &view->state.tree;
6997 s->matched_entry = NULL;
6998 return NULL;
7001 static int
7002 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7004 regmatch_t regmatch;
7006 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
7007 0) == 0;
7010 static const struct got_error *
7011 search_next_tree_view(struct tog_view *view)
7013 struct tog_tree_view_state *s = &view->state.tree;
7014 struct got_tree_entry *te = NULL;
7016 if (!view->searching) {
7017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7018 return NULL;
7021 if (s->matched_entry) {
7022 if (view->searching == TOG_SEARCH_FORWARD) {
7023 if (s->selected_entry)
7024 te = got_tree_entry_get_next(s->tree,
7025 s->selected_entry);
7026 else
7027 te = got_object_tree_get_first_entry(s->tree);
7028 } else {
7029 if (s->selected_entry == NULL)
7030 te = got_object_tree_get_last_entry(s->tree);
7031 else
7032 te = got_tree_entry_get_prev(s->tree,
7033 s->selected_entry);
7035 } else {
7036 if (s->selected_entry)
7037 te = s->selected_entry;
7038 else if (view->searching == TOG_SEARCH_FORWARD)
7039 te = got_object_tree_get_first_entry(s->tree);
7040 else
7041 te = got_object_tree_get_last_entry(s->tree);
7044 while (1) {
7045 if (te == NULL) {
7046 if (s->matched_entry == NULL) {
7047 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7048 return NULL;
7050 if (view->searching == TOG_SEARCH_FORWARD)
7051 te = got_object_tree_get_first_entry(s->tree);
7052 else
7053 te = got_object_tree_get_last_entry(s->tree);
7056 if (match_tree_entry(te, &view->regex)) {
7057 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7058 s->matched_entry = te;
7059 break;
7062 if (view->searching == TOG_SEARCH_FORWARD)
7063 te = got_tree_entry_get_next(s->tree, te);
7064 else
7065 te = got_tree_entry_get_prev(s->tree, te);
7068 if (s->matched_entry) {
7069 s->first_displayed_entry = s->matched_entry;
7070 s->selected = 0;
7073 return NULL;
7076 static const struct got_error *
7077 show_tree_view(struct tog_view *view)
7079 const struct got_error *err = NULL;
7080 struct tog_tree_view_state *s = &view->state.tree;
7081 char *parent_path;
7083 err = tree_entry_path(&parent_path, &s->parents, NULL);
7084 if (err)
7085 return err;
7087 err = draw_tree_entries(view, parent_path);
7088 free(parent_path);
7090 view_border(view);
7091 return err;
7094 static const struct got_error *
7095 tree_goto_line(struct tog_view *view, int nlines)
7097 const struct got_error *err = NULL;
7098 struct tog_tree_view_state *s = &view->state.tree;
7099 struct got_tree_entry **fte, **lte, **ste;
7100 int g, last, first = 1, i = 1;
7101 int root = s->tree == s->root;
7102 int off = root ? 1 : 2;
7104 g = view->gline;
7105 view->gline = 0;
7107 if (g == 0)
7108 g = 1;
7109 else if (g > got_object_tree_get_nentries(s->tree))
7110 g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7112 fte = &s->first_displayed_entry;
7113 lte = &s->last_displayed_entry;
7114 ste = &s->selected_entry;
7116 if (*fte != NULL) {
7117 first = got_tree_entry_get_index(*fte);
7118 first += off; /* account for ".." */
7120 last = got_tree_entry_get_index(*lte);
7121 last += off;
7123 if (g >= first && g <= last && g - first < nlines) {
7124 s->selected = g - first;
7125 return NULL; /* gline is on the current page */
7128 if (*ste != NULL) {
7129 i = got_tree_entry_get_index(*ste);
7130 i += off;
7133 if (i < g) {
7134 err = tree_scroll_down(view, g - i);
7135 if (err)
7136 return err;
7137 if (got_tree_entry_get_index(*lte) >=
7138 got_object_tree_get_nentries(s->tree) - 1 &&
7139 first + s->selected < g &&
7140 s->selected < s->ndisplayed - 1) {
7141 first = got_tree_entry_get_index(*fte);
7142 first += off;
7143 s->selected = g - first;
7145 } else if (i > g)
7146 tree_scroll_up(s, i - g);
7148 if (g < nlines &&
7149 (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7150 s->selected = g - 1;
7152 return NULL;
7155 static const struct got_error *
7156 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7158 const struct got_error *err = NULL;
7159 struct tog_tree_view_state *s = &view->state.tree;
7160 struct got_tree_entry *te;
7161 int n, nscroll = view->nlines - 3;
7163 if (view->gline)
7164 return tree_goto_line(view, nscroll);
7166 switch (ch) {
7167 case 'i':
7168 s->show_ids = !s->show_ids;
7169 view->count = 0;
7170 break;
7171 case 'L':
7172 view->count = 0;
7173 if (!s->selected_entry)
7174 break;
7175 err = view_request_new(new_view, view, TOG_VIEW_LOG);
7176 break;
7177 case 'R':
7178 view->count = 0;
7179 err = view_request_new(new_view, view, TOG_VIEW_REF);
7180 break;
7181 case 'g':
7182 case KEY_HOME:
7183 s->selected = 0;
7184 view->count = 0;
7185 if (s->tree == s->root)
7186 s->first_displayed_entry =
7187 got_object_tree_get_first_entry(s->tree);
7188 else
7189 s->first_displayed_entry = NULL;
7190 break;
7191 case 'G':
7192 case KEY_END: {
7193 int eos = view->nlines - 3;
7195 if (view->mode == TOG_VIEW_SPLIT_HRZN)
7196 --eos; /* border */
7197 s->selected = 0;
7198 view->count = 0;
7199 te = got_object_tree_get_last_entry(s->tree);
7200 for (n = 0; n < eos; n++) {
7201 if (te == NULL) {
7202 if (s->tree != s->root) {
7203 s->first_displayed_entry = NULL;
7204 n++;
7206 break;
7208 s->first_displayed_entry = te;
7209 te = got_tree_entry_get_prev(s->tree, te);
7211 if (n > 0)
7212 s->selected = n - 1;
7213 break;
7215 case 'k':
7216 case KEY_UP:
7217 case CTRL('p'):
7218 if (s->selected > 0) {
7219 s->selected--;
7220 break;
7222 tree_scroll_up(s, 1);
7223 if (s->selected_entry == NULL ||
7224 (s->tree == s->root && s->selected_entry ==
7225 got_object_tree_get_first_entry(s->tree)))
7226 view->count = 0;
7227 break;
7228 case CTRL('u'):
7229 case 'u':
7230 nscroll /= 2;
7231 /* FALL THROUGH */
7232 case KEY_PPAGE:
7233 case CTRL('b'):
7234 case 'b':
7235 if (s->tree == s->root) {
7236 if (got_object_tree_get_first_entry(s->tree) ==
7237 s->first_displayed_entry)
7238 s->selected -= MIN(s->selected, nscroll);
7239 } else {
7240 if (s->first_displayed_entry == NULL)
7241 s->selected -= MIN(s->selected, nscroll);
7243 tree_scroll_up(s, MAX(0, nscroll));
7244 if (s->selected_entry == NULL ||
7245 (s->tree == s->root && s->selected_entry ==
7246 got_object_tree_get_first_entry(s->tree)))
7247 view->count = 0;
7248 break;
7249 case 'j':
7250 case KEY_DOWN:
7251 case CTRL('n'):
7252 if (s->selected < s->ndisplayed - 1) {
7253 s->selected++;
7254 break;
7256 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7257 == NULL) {
7258 /* can't scroll any further */
7259 view->count = 0;
7260 break;
7262 tree_scroll_down(view, 1);
7263 break;
7264 case CTRL('d'):
7265 case 'd':
7266 nscroll /= 2;
7267 /* FALL THROUGH */
7268 case KEY_NPAGE:
7269 case CTRL('f'):
7270 case 'f':
7271 case ' ':
7272 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7273 == NULL) {
7274 /* can't scroll any further; move cursor down */
7275 if (s->selected < s->ndisplayed - 1)
7276 s->selected += MIN(nscroll,
7277 s->ndisplayed - s->selected - 1);
7278 else
7279 view->count = 0;
7280 break;
7282 tree_scroll_down(view, nscroll);
7283 break;
7284 case KEY_ENTER:
7285 case '\r':
7286 case KEY_BACKSPACE:
7287 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7288 struct tog_parent_tree *parent;
7289 /* user selected '..' */
7290 if (s->tree == s->root) {
7291 view->count = 0;
7292 break;
7294 parent = TAILQ_FIRST(&s->parents);
7295 TAILQ_REMOVE(&s->parents, parent,
7296 entry);
7297 got_object_tree_close(s->tree);
7298 s->tree = parent->tree;
7299 s->first_displayed_entry =
7300 parent->first_displayed_entry;
7301 s->selected_entry =
7302 parent->selected_entry;
7303 s->selected = parent->selected;
7304 if (s->selected > view->nlines - 3) {
7305 err = offset_selection_down(view);
7306 if (err)
7307 break;
7309 free(parent);
7310 } else if (S_ISDIR(got_tree_entry_get_mode(
7311 s->selected_entry))) {
7312 struct got_tree_object *subtree;
7313 view->count = 0;
7314 err = got_object_open_as_tree(&subtree, s->repo,
7315 got_tree_entry_get_id(s->selected_entry));
7316 if (err)
7317 break;
7318 err = tree_view_visit_subtree(s, subtree);
7319 if (err) {
7320 got_object_tree_close(subtree);
7321 break;
7323 } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7324 err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7325 break;
7326 case KEY_RESIZE:
7327 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7328 s->selected = view->nlines - 4;
7329 view->count = 0;
7330 break;
7331 default:
7332 view->count = 0;
7333 break;
7336 return err;
7339 __dead static void
7340 usage_tree(void)
7342 endwin();
7343 fprintf(stderr,
7344 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7345 getprogname());
7346 exit(1);
7349 static const struct got_error *
7350 cmd_tree(int argc, char *argv[])
7352 const struct got_error *error;
7353 struct got_repository *repo = NULL;
7354 struct got_worktree *worktree = NULL;
7355 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7356 struct got_object_id *commit_id = NULL;
7357 struct got_commit_object *commit = NULL;
7358 const char *commit_id_arg = NULL;
7359 char *label = NULL;
7360 struct got_reference *ref = NULL;
7361 const char *head_ref_name = NULL;
7362 int ch;
7363 struct tog_view *view;
7364 int *pack_fds = NULL;
7366 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7367 switch (ch) {
7368 case 'c':
7369 commit_id_arg = optarg;
7370 break;
7371 case 'r':
7372 repo_path = realpath(optarg, NULL);
7373 if (repo_path == NULL)
7374 return got_error_from_errno2("realpath",
7375 optarg);
7376 break;
7377 default:
7378 usage_tree();
7379 /* NOTREACHED */
7383 argc -= optind;
7384 argv += optind;
7386 if (argc > 1)
7387 usage_tree();
7389 error = got_repo_pack_fds_open(&pack_fds);
7390 if (error != NULL)
7391 goto done;
7393 if (repo_path == NULL) {
7394 cwd = getcwd(NULL, 0);
7395 if (cwd == NULL)
7396 return got_error_from_errno("getcwd");
7397 error = got_worktree_open(&worktree, cwd);
7398 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7399 goto done;
7400 if (worktree)
7401 repo_path =
7402 strdup(got_worktree_get_repo_path(worktree));
7403 else
7404 repo_path = strdup(cwd);
7405 if (repo_path == NULL) {
7406 error = got_error_from_errno("strdup");
7407 goto done;
7411 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7412 if (error != NULL)
7413 goto done;
7415 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7416 repo, worktree);
7417 if (error)
7418 goto done;
7420 init_curses();
7422 error = apply_unveil(got_repo_get_path(repo), NULL);
7423 if (error)
7424 goto done;
7426 error = tog_load_refs(repo, 0);
7427 if (error)
7428 goto done;
7430 if (commit_id_arg == NULL) {
7431 error = got_repo_match_object_id(&commit_id, &label,
7432 worktree ? got_worktree_get_head_ref_name(worktree) :
7433 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7434 if (error)
7435 goto done;
7436 head_ref_name = label;
7437 } else {
7438 error = got_ref_open(&ref, repo, commit_id_arg, 0);
7439 if (error == NULL)
7440 head_ref_name = got_ref_get_name(ref);
7441 else if (error->code != GOT_ERR_NOT_REF)
7442 goto done;
7443 error = got_repo_match_object_id(&commit_id, NULL,
7444 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7445 if (error)
7446 goto done;
7449 error = got_object_open_as_commit(&commit, repo, commit_id);
7450 if (error)
7451 goto done;
7453 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7454 if (view == NULL) {
7455 error = got_error_from_errno("view_open");
7456 goto done;
7458 error = open_tree_view(view, commit_id, head_ref_name, repo);
7459 if (error)
7460 goto done;
7461 if (!got_path_is_root_dir(in_repo_path)) {
7462 error = tree_view_walk_path(&view->state.tree, commit,
7463 in_repo_path);
7464 if (error)
7465 goto done;
7468 if (worktree) {
7469 /* Release work tree lock. */
7470 got_worktree_close(worktree);
7471 worktree = NULL;
7473 error = view_loop(view);
7474 done:
7475 free(repo_path);
7476 free(cwd);
7477 free(commit_id);
7478 free(label);
7479 if (ref)
7480 got_ref_close(ref);
7481 if (repo) {
7482 const struct got_error *close_err = got_repo_close(repo);
7483 if (error == NULL)
7484 error = close_err;
7486 if (pack_fds) {
7487 const struct got_error *pack_err =
7488 got_repo_pack_fds_close(pack_fds);
7489 if (error == NULL)
7490 error = pack_err;
7492 tog_free_refs();
7493 return error;
7496 static const struct got_error *
7497 ref_view_load_refs(struct tog_ref_view_state *s)
7499 struct got_reflist_entry *sre;
7500 struct tog_reflist_entry *re;
7502 s->nrefs = 0;
7503 TAILQ_FOREACH(sre, &tog_refs, entry) {
7504 if (strncmp(got_ref_get_name(sre->ref),
7505 "refs/got/", 9) == 0 &&
7506 strncmp(got_ref_get_name(sre->ref),
7507 "refs/got/backup/", 16) != 0)
7508 continue;
7510 re = malloc(sizeof(*re));
7511 if (re == NULL)
7512 return got_error_from_errno("malloc");
7514 re->ref = got_ref_dup(sre->ref);
7515 if (re->ref == NULL)
7516 return got_error_from_errno("got_ref_dup");
7517 re->idx = s->nrefs++;
7518 TAILQ_INSERT_TAIL(&s->refs, re, entry);
7521 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7522 return NULL;
7525 static void
7526 ref_view_free_refs(struct tog_ref_view_state *s)
7528 struct tog_reflist_entry *re;
7530 while (!TAILQ_EMPTY(&s->refs)) {
7531 re = TAILQ_FIRST(&s->refs);
7532 TAILQ_REMOVE(&s->refs, re, entry);
7533 got_ref_close(re->ref);
7534 free(re);
7538 static const struct got_error *
7539 open_ref_view(struct tog_view *view, struct got_repository *repo)
7541 const struct got_error *err = NULL;
7542 struct tog_ref_view_state *s = &view->state.ref;
7544 s->selected_entry = 0;
7545 s->repo = repo;
7547 TAILQ_INIT(&s->refs);
7548 STAILQ_INIT(&s->colors);
7550 err = ref_view_load_refs(s);
7551 if (err)
7552 return err;
7554 if (has_colors() && getenv("TOG_COLORS") != NULL) {
7555 err = add_color(&s->colors, "^refs/heads/",
7556 TOG_COLOR_REFS_HEADS,
7557 get_color_value("TOG_COLOR_REFS_HEADS"));
7558 if (err)
7559 goto done;
7561 err = add_color(&s->colors, "^refs/tags/",
7562 TOG_COLOR_REFS_TAGS,
7563 get_color_value("TOG_COLOR_REFS_TAGS"));
7564 if (err)
7565 goto done;
7567 err = add_color(&s->colors, "^refs/remotes/",
7568 TOG_COLOR_REFS_REMOTES,
7569 get_color_value("TOG_COLOR_REFS_REMOTES"));
7570 if (err)
7571 goto done;
7573 err = add_color(&s->colors, "^refs/got/backup/",
7574 TOG_COLOR_REFS_BACKUP,
7575 get_color_value("TOG_COLOR_REFS_BACKUP"));
7576 if (err)
7577 goto done;
7580 view->show = show_ref_view;
7581 view->input = input_ref_view;
7582 view->close = close_ref_view;
7583 view->search_start = search_start_ref_view;
7584 view->search_next = search_next_ref_view;
7585 done:
7586 if (err)
7587 free_colors(&s->colors);
7588 return err;
7591 static const struct got_error *
7592 close_ref_view(struct tog_view *view)
7594 struct tog_ref_view_state *s = &view->state.ref;
7596 ref_view_free_refs(s);
7597 free_colors(&s->colors);
7599 return NULL;
7602 static const struct got_error *
7603 resolve_reflist_entry(struct got_object_id **commit_id,
7604 struct tog_reflist_entry *re, struct got_repository *repo)
7606 const struct got_error *err = NULL;
7607 struct got_object_id *obj_id;
7608 struct got_tag_object *tag = NULL;
7609 int obj_type;
7611 *commit_id = NULL;
7613 err = got_ref_resolve(&obj_id, repo, re->ref);
7614 if (err)
7615 return err;
7617 err = got_object_get_type(&obj_type, repo, obj_id);
7618 if (err)
7619 goto done;
7621 switch (obj_type) {
7622 case GOT_OBJ_TYPE_COMMIT:
7623 *commit_id = obj_id;
7624 break;
7625 case GOT_OBJ_TYPE_TAG:
7626 err = got_object_open_as_tag(&tag, repo, obj_id);
7627 if (err)
7628 goto done;
7629 free(obj_id);
7630 err = got_object_get_type(&obj_type, repo,
7631 got_object_tag_get_object_id(tag));
7632 if (err)
7633 goto done;
7634 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7635 err = got_error(GOT_ERR_OBJ_TYPE);
7636 goto done;
7638 *commit_id = got_object_id_dup(
7639 got_object_tag_get_object_id(tag));
7640 if (*commit_id == NULL) {
7641 err = got_error_from_errno("got_object_id_dup");
7642 goto done;
7644 break;
7645 default:
7646 err = got_error(GOT_ERR_OBJ_TYPE);
7647 break;
7650 done:
7651 if (tag)
7652 got_object_tag_close(tag);
7653 if (err) {
7654 free(*commit_id);
7655 *commit_id = NULL;
7657 return err;
7660 static const struct got_error *
7661 log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7662 struct tog_reflist_entry *re, struct got_repository *repo)
7664 struct tog_view *log_view;
7665 const struct got_error *err = NULL;
7666 struct got_object_id *commit_id = NULL;
7668 *new_view = NULL;
7670 err = resolve_reflist_entry(&commit_id, re, repo);
7671 if (err) {
7672 if (err->code != GOT_ERR_OBJ_TYPE)
7673 return err;
7674 else
7675 return NULL;
7678 log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7679 if (log_view == NULL) {
7680 err = got_error_from_errno("view_open");
7681 goto done;
7684 err = open_log_view(log_view, commit_id, repo,
7685 got_ref_get_name(re->ref), "", 0);
7686 done:
7687 if (err)
7688 view_close(log_view);
7689 else
7690 *new_view = log_view;
7691 free(commit_id);
7692 return err;
7695 static void
7696 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7698 struct tog_reflist_entry *re;
7699 int i = 0;
7701 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7702 return;
7704 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7705 while (i++ < maxscroll) {
7706 if (re == NULL)
7707 break;
7708 s->first_displayed_entry = re;
7709 re = TAILQ_PREV(re, tog_reflist_head, entry);
7713 static const struct got_error *
7714 ref_scroll_down(struct tog_view *view, int maxscroll)
7716 struct tog_ref_view_state *s = &view->state.ref;
7717 struct tog_reflist_entry *next, *last;
7718 int n = 0;
7720 if (s->first_displayed_entry)
7721 next = TAILQ_NEXT(s->first_displayed_entry, entry);
7722 else
7723 next = TAILQ_FIRST(&s->refs);
7725 last = s->last_displayed_entry;
7726 while (next && n++ < maxscroll) {
7727 if (last) {
7728 s->last_displayed_entry = last;
7729 last = TAILQ_NEXT(last, entry);
7731 if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7732 s->first_displayed_entry = next;
7733 next = TAILQ_NEXT(next, entry);
7737 return NULL;
7740 static const struct got_error *
7741 search_start_ref_view(struct tog_view *view)
7743 struct tog_ref_view_state *s = &view->state.ref;
7745 s->matched_entry = NULL;
7746 return NULL;
7749 static int
7750 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7752 regmatch_t regmatch;
7754 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
7755 0) == 0;
7758 static const struct got_error *
7759 search_next_ref_view(struct tog_view *view)
7761 struct tog_ref_view_state *s = &view->state.ref;
7762 struct tog_reflist_entry *re = NULL;
7764 if (!view->searching) {
7765 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7766 return NULL;
7769 if (s->matched_entry) {
7770 if (view->searching == TOG_SEARCH_FORWARD) {
7771 if (s->selected_entry)
7772 re = TAILQ_NEXT(s->selected_entry, entry);
7773 else
7774 re = TAILQ_PREV(s->selected_entry,
7775 tog_reflist_head, entry);
7776 } else {
7777 if (s->selected_entry == NULL)
7778 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7779 else
7780 re = TAILQ_PREV(s->selected_entry,
7781 tog_reflist_head, entry);
7783 } else {
7784 if (s->selected_entry)
7785 re = s->selected_entry;
7786 else if (view->searching == TOG_SEARCH_FORWARD)
7787 re = TAILQ_FIRST(&s->refs);
7788 else
7789 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7792 while (1) {
7793 if (re == NULL) {
7794 if (s->matched_entry == NULL) {
7795 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7796 return NULL;
7798 if (view->searching == TOG_SEARCH_FORWARD)
7799 re = TAILQ_FIRST(&s->refs);
7800 else
7801 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7804 if (match_reflist_entry(re, &view->regex)) {
7805 view->search_next_done = TOG_SEARCH_HAVE_MORE;
7806 s->matched_entry = re;
7807 break;
7810 if (view->searching == TOG_SEARCH_FORWARD)
7811 re = TAILQ_NEXT(re, entry);
7812 else
7813 re = TAILQ_PREV(re, tog_reflist_head, entry);
7816 if (s->matched_entry) {
7817 s->first_displayed_entry = s->matched_entry;
7818 s->selected = 0;
7821 return NULL;
7824 static const struct got_error *
7825 show_ref_view(struct tog_view *view)
7827 const struct got_error *err = NULL;
7828 struct tog_ref_view_state *s = &view->state.ref;
7829 struct tog_reflist_entry *re;
7830 char *line = NULL;
7831 wchar_t *wline;
7832 struct tog_color *tc;
7833 int width, n;
7834 int limit = view->nlines;
7836 werase(view->window);
7838 s->ndisplayed = 0;
7839 if (view_is_hsplit_top(view))
7840 --limit; /* border */
7842 if (limit == 0)
7843 return NULL;
7845 re = s->first_displayed_entry;
7847 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7848 s->nrefs) == -1)
7849 return got_error_from_errno("asprintf");
7851 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7852 if (err) {
7853 free(line);
7854 return err;
7856 if (view_needs_focus_indication(view))
7857 wstandout(view->window);
7858 waddwstr(view->window, wline);
7859 while (width++ < view->ncols)
7860 waddch(view->window, ' ');
7861 if (view_needs_focus_indication(view))
7862 wstandend(view->window);
7863 free(wline);
7864 wline = NULL;
7865 free(line);
7866 line = NULL;
7867 if (--limit <= 0)
7868 return NULL;
7870 n = 0;
7871 while (re && limit > 0) {
7872 char *line = NULL;
7873 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7875 if (s->show_date) {
7876 struct got_commit_object *ci;
7877 struct got_tag_object *tag;
7878 struct got_object_id *id;
7879 struct tm tm;
7880 time_t t;
7882 err = got_ref_resolve(&id, s->repo, re->ref);
7883 if (err)
7884 return err;
7885 err = got_object_open_as_tag(&tag, s->repo, id);
7886 if (err) {
7887 if (err->code != GOT_ERR_OBJ_TYPE) {
7888 free(id);
7889 return err;
7891 err = got_object_open_as_commit(&ci, s->repo,
7892 id);
7893 if (err) {
7894 free(id);
7895 return err;
7897 t = got_object_commit_get_committer_time(ci);
7898 got_object_commit_close(ci);
7899 } else {
7900 t = got_object_tag_get_tagger_time(tag);
7901 got_object_tag_close(tag);
7903 free(id);
7904 if (gmtime_r(&t, &tm) == NULL)
7905 return got_error_from_errno("gmtime_r");
7906 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7907 return got_error(GOT_ERR_NO_SPACE);
7909 if (got_ref_is_symbolic(re->ref)) {
7910 if (asprintf(&line, "%s%s -> %s", s->show_date ?
7911 ymd : "", got_ref_get_name(re->ref),
7912 got_ref_get_symref_target(re->ref)) == -1)
7913 return got_error_from_errno("asprintf");
7914 } else if (s->show_ids) {
7915 struct got_object_id *id;
7916 char *id_str;
7917 err = got_ref_resolve(&id, s->repo, re->ref);
7918 if (err)
7919 return err;
7920 err = got_object_id_str(&id_str, id);
7921 if (err) {
7922 free(id);
7923 return err;
7925 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7926 got_ref_get_name(re->ref), id_str) == -1) {
7927 err = got_error_from_errno("asprintf");
7928 free(id);
7929 free(id_str);
7930 return err;
7932 free(id);
7933 free(id_str);
7934 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7935 got_ref_get_name(re->ref)) == -1)
7936 return got_error_from_errno("asprintf");
7938 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7939 0, 0);
7940 if (err) {
7941 free(line);
7942 return err;
7944 if (n == s->selected) {
7945 if (view->focussed)
7946 wstandout(view->window);
7947 s->selected_entry = re;
7949 tc = match_color(&s->colors, got_ref_get_name(re->ref));
7950 if (tc)
7951 wattr_on(view->window,
7952 COLOR_PAIR(tc->colorpair), NULL);
7953 waddwstr(view->window, wline);
7954 if (tc)
7955 wattr_off(view->window,
7956 COLOR_PAIR(tc->colorpair), NULL);
7957 if (width < view->ncols - 1)
7958 waddch(view->window, '\n');
7959 if (n == s->selected && view->focussed)
7960 wstandend(view->window);
7961 free(line);
7962 free(wline);
7963 wline = NULL;
7964 n++;
7965 s->ndisplayed++;
7966 s->last_displayed_entry = re;
7968 limit--;
7969 re = TAILQ_NEXT(re, entry);
7972 view_border(view);
7973 return err;
7976 static const struct got_error *
7977 browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7978 struct tog_reflist_entry *re, struct got_repository *repo)
7980 const struct got_error *err = NULL;
7981 struct got_object_id *commit_id = NULL;
7982 struct tog_view *tree_view;
7984 *new_view = NULL;
7986 err = resolve_reflist_entry(&commit_id, re, repo);
7987 if (err) {
7988 if (err->code != GOT_ERR_OBJ_TYPE)
7989 return err;
7990 else
7991 return NULL;
7995 tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7996 if (tree_view == NULL) {
7997 err = got_error_from_errno("view_open");
7998 goto done;
8001 err = open_tree_view(tree_view, commit_id,
8002 got_ref_get_name(re->ref), repo);
8003 if (err)
8004 goto done;
8006 *new_view = tree_view;
8007 done:
8008 free(commit_id);
8009 return err;
8012 static const struct got_error *
8013 ref_goto_line(struct tog_view *view, int nlines)
8015 const struct got_error *err = NULL;
8016 struct tog_ref_view_state *s = &view->state.ref;
8017 int g, idx = s->selected_entry->idx;
8019 g = view->gline;
8020 view->gline = 0;
8022 if (g == 0)
8023 g = 1;
8024 else if (g > s->nrefs)
8025 g = s->nrefs;
8027 if (g >= s->first_displayed_entry->idx + 1 &&
8028 g <= s->last_displayed_entry->idx + 1 &&
8029 g - s->first_displayed_entry->idx - 1 < nlines) {
8030 s->selected = g - s->first_displayed_entry->idx - 1;
8031 return NULL;
8034 if (idx + 1 < g) {
8035 err = ref_scroll_down(view, g - idx - 1);
8036 if (err)
8037 return err;
8038 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8039 s->first_displayed_entry->idx + s->selected < g &&
8040 s->selected < s->ndisplayed - 1)
8041 s->selected = g - s->first_displayed_entry->idx - 1;
8042 } else if (idx + 1 > g)
8043 ref_scroll_up(s, idx - g + 1);
8045 if (g < nlines && s->first_displayed_entry->idx == 0)
8046 s->selected = g - 1;
8048 return NULL;
8052 static const struct got_error *
8053 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8055 const struct got_error *err = NULL;
8056 struct tog_ref_view_state *s = &view->state.ref;
8057 struct tog_reflist_entry *re;
8058 int n, nscroll = view->nlines - 1;
8060 if (view->gline)
8061 return ref_goto_line(view, nscroll);
8063 switch (ch) {
8064 case 'i':
8065 s->show_ids = !s->show_ids;
8066 view->count = 0;
8067 break;
8068 case 'm':
8069 s->show_date = !s->show_date;
8070 view->count = 0;
8071 break;
8072 case 'o':
8073 s->sort_by_date = !s->sort_by_date;
8074 view->count = 0;
8075 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8076 got_ref_cmp_by_commit_timestamp_descending :
8077 tog_ref_cmp_by_name, s->repo);
8078 if (err)
8079 break;
8080 got_reflist_object_id_map_free(tog_refs_idmap);
8081 err = got_reflist_object_id_map_create(&tog_refs_idmap,
8082 &tog_refs, s->repo);
8083 if (err)
8084 break;
8085 ref_view_free_refs(s);
8086 err = ref_view_load_refs(s);
8087 break;
8088 case KEY_ENTER:
8089 case '\r':
8090 view->count = 0;
8091 if (!s->selected_entry)
8092 break;
8093 err = view_request_new(new_view, view, TOG_VIEW_LOG);
8094 break;
8095 case 'T':
8096 view->count = 0;
8097 if (!s->selected_entry)
8098 break;
8099 err = view_request_new(new_view, view, TOG_VIEW_TREE);
8100 break;
8101 case 'g':
8102 case KEY_HOME:
8103 s->selected = 0;
8104 view->count = 0;
8105 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8106 break;
8107 case 'G':
8108 case KEY_END: {
8109 int eos = view->nlines - 1;
8111 if (view->mode == TOG_VIEW_SPLIT_HRZN)
8112 --eos; /* border */
8113 s->selected = 0;
8114 view->count = 0;
8115 re = TAILQ_LAST(&s->refs, tog_reflist_head);
8116 for (n = 0; n < eos; n++) {
8117 if (re == NULL)
8118 break;
8119 s->first_displayed_entry = re;
8120 re = TAILQ_PREV(re, tog_reflist_head, entry);
8122 if (n > 0)
8123 s->selected = n - 1;
8124 break;
8126 case 'k':
8127 case KEY_UP:
8128 case CTRL('p'):
8129 if (s->selected > 0) {
8130 s->selected--;
8131 break;
8133 ref_scroll_up(s, 1);
8134 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8135 view->count = 0;
8136 break;
8137 case CTRL('u'):
8138 case 'u':
8139 nscroll /= 2;
8140 /* FALL THROUGH */
8141 case KEY_PPAGE:
8142 case CTRL('b'):
8143 case 'b':
8144 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8145 s->selected -= MIN(nscroll, s->selected);
8146 ref_scroll_up(s, MAX(0, nscroll));
8147 if (s->selected_entry == TAILQ_FIRST(&s->refs))
8148 view->count = 0;
8149 break;
8150 case 'j':
8151 case KEY_DOWN:
8152 case CTRL('n'):
8153 if (s->selected < s->ndisplayed - 1) {
8154 s->selected++;
8155 break;
8157 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8158 /* can't scroll any further */
8159 view->count = 0;
8160 break;
8162 ref_scroll_down(view, 1);
8163 break;
8164 case CTRL('d'):
8165 case 'd':
8166 nscroll /= 2;
8167 /* FALL THROUGH */
8168 case KEY_NPAGE:
8169 case CTRL('f'):
8170 case 'f':
8171 case ' ':
8172 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8173 /* can't scroll any further; move cursor down */
8174 if (s->selected < s->ndisplayed - 1)
8175 s->selected += MIN(nscroll,
8176 s->ndisplayed - s->selected - 1);
8177 if (view->count > 1 && s->selected < s->ndisplayed - 1)
8178 s->selected += s->ndisplayed - s->selected - 1;
8179 view->count = 0;
8180 break;
8182 ref_scroll_down(view, nscroll);
8183 break;
8184 case CTRL('l'):
8185 view->count = 0;
8186 tog_free_refs();
8187 err = tog_load_refs(s->repo, s->sort_by_date);
8188 if (err)
8189 break;
8190 ref_view_free_refs(s);
8191 err = ref_view_load_refs(s);
8192 break;
8193 case KEY_RESIZE:
8194 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8195 s->selected = view->nlines - 2;
8196 break;
8197 default:
8198 view->count = 0;
8199 break;
8202 return err;
8205 __dead static void
8206 usage_ref(void)
8208 endwin();
8209 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8210 getprogname());
8211 exit(1);
8214 static const struct got_error *
8215 cmd_ref(int argc, char *argv[])
8217 const struct got_error *error;
8218 struct got_repository *repo = NULL;
8219 struct got_worktree *worktree = NULL;
8220 char *cwd = NULL, *repo_path = NULL;
8221 int ch;
8222 struct tog_view *view;
8223 int *pack_fds = NULL;
8225 while ((ch = getopt(argc, argv, "r:")) != -1) {
8226 switch (ch) {
8227 case 'r':
8228 repo_path = realpath(optarg, NULL);
8229 if (repo_path == NULL)
8230 return got_error_from_errno2("realpath",
8231 optarg);
8232 break;
8233 default:
8234 usage_ref();
8235 /* NOTREACHED */
8239 argc -= optind;
8240 argv += optind;
8242 if (argc > 1)
8243 usage_ref();
8245 error = got_repo_pack_fds_open(&pack_fds);
8246 if (error != NULL)
8247 goto done;
8249 if (repo_path == NULL) {
8250 cwd = getcwd(NULL, 0);
8251 if (cwd == NULL)
8252 return got_error_from_errno("getcwd");
8253 error = got_worktree_open(&worktree, cwd);
8254 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8255 goto done;
8256 if (worktree)
8257 repo_path =
8258 strdup(got_worktree_get_repo_path(worktree));
8259 else
8260 repo_path = strdup(cwd);
8261 if (repo_path == NULL) {
8262 error = got_error_from_errno("strdup");
8263 goto done;
8267 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8268 if (error != NULL)
8269 goto done;
8271 init_curses();
8273 error = apply_unveil(got_repo_get_path(repo), NULL);
8274 if (error)
8275 goto done;
8277 error = tog_load_refs(repo, 0);
8278 if (error)
8279 goto done;
8281 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8282 if (view == NULL) {
8283 error = got_error_from_errno("view_open");
8284 goto done;
8287 error = open_ref_view(view, repo);
8288 if (error)
8289 goto done;
8291 if (worktree) {
8292 /* Release work tree lock. */
8293 got_worktree_close(worktree);
8294 worktree = NULL;
8296 error = view_loop(view);
8297 done:
8298 free(repo_path);
8299 free(cwd);
8300 if (repo) {
8301 const struct got_error *close_err = got_repo_close(repo);
8302 if (close_err)
8303 error = close_err;
8305 if (pack_fds) {
8306 const struct got_error *pack_err =
8307 got_repo_pack_fds_close(pack_fds);
8308 if (error == NULL)
8309 error = pack_err;
8311 tog_free_refs();
8312 return error;
8315 static const struct got_error *
8316 view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8317 enum tog_view_type request, int y, int x)
8319 const struct got_error *err = NULL;
8321 *new_view = NULL;
8323 switch (request) {
8324 case TOG_VIEW_DIFF:
8325 if (view->type == TOG_VIEW_LOG) {
8326 struct tog_log_view_state *s = &view->state.log;
8328 err = open_diff_view_for_commit(new_view, y, x,
8329 s->selected_entry->commit, s->selected_entry->id,
8330 view, s->repo);
8331 } else
8332 return got_error_msg(GOT_ERR_NOT_IMPL,
8333 "parent/child view pair not supported");
8334 break;
8335 case TOG_VIEW_BLAME:
8336 if (view->type == TOG_VIEW_TREE) {
8337 struct tog_tree_view_state *s = &view->state.tree;
8339 err = blame_tree_entry(new_view, y, x,
8340 s->selected_entry, &s->parents, s->commit_id,
8341 s->repo);
8342 } else
8343 return got_error_msg(GOT_ERR_NOT_IMPL,
8344 "parent/child view pair not supported");
8345 break;
8346 case TOG_VIEW_LOG:
8347 if (view->type == TOG_VIEW_BLAME)
8348 err = log_annotated_line(new_view, y, x,
8349 view->state.blame.repo, view->state.blame.id_to_log);
8350 else if (view->type == TOG_VIEW_TREE)
8351 err = log_selected_tree_entry(new_view, y, x,
8352 &view->state.tree);
8353 else if (view->type == TOG_VIEW_REF)
8354 err = log_ref_entry(new_view, y, x,
8355 view->state.ref.selected_entry,
8356 view->state.ref.repo);
8357 else
8358 return got_error_msg(GOT_ERR_NOT_IMPL,
8359 "parent/child view pair not supported");
8360 break;
8361 case TOG_VIEW_TREE:
8362 if (view->type == TOG_VIEW_LOG)
8363 err = browse_commit_tree(new_view, y, x,
8364 view->state.log.selected_entry,
8365 view->state.log.in_repo_path,
8366 view->state.log.head_ref_name,
8367 view->state.log.repo);
8368 else if (view->type == TOG_VIEW_REF)
8369 err = browse_ref_tree(new_view, y, x,
8370 view->state.ref.selected_entry,
8371 view->state.ref.repo);
8372 else
8373 return got_error_msg(GOT_ERR_NOT_IMPL,
8374 "parent/child view pair not supported");
8375 break;
8376 case TOG_VIEW_REF:
8377 *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8378 if (*new_view == NULL)
8379 return got_error_from_errno("view_open");
8380 if (view->type == TOG_VIEW_LOG)
8381 err = open_ref_view(*new_view, view->state.log.repo);
8382 else if (view->type == TOG_VIEW_TREE)
8383 err = open_ref_view(*new_view, view->state.tree.repo);
8384 else
8385 err = got_error_msg(GOT_ERR_NOT_IMPL,
8386 "parent/child view pair not supported");
8387 if (err)
8388 view_close(*new_view);
8389 break;
8390 default:
8391 return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8394 return err;
8398 * If view was scrolled down to move the selected line into view when opening a
8399 * horizontal split, scroll back up when closing the split/toggling fullscreen.
8401 static void
8402 offset_selection_up(struct tog_view *view)
8404 switch (view->type) {
8405 case TOG_VIEW_BLAME: {
8406 struct tog_blame_view_state *s = &view->state.blame;
8407 if (s->first_displayed_line == 1) {
8408 s->selected_line = MAX(s->selected_line - view->offset,
8409 1);
8410 break;
8412 if (s->first_displayed_line > view->offset)
8413 s->first_displayed_line -= view->offset;
8414 else
8415 s->first_displayed_line = 1;
8416 s->selected_line += view->offset;
8417 break;
8419 case TOG_VIEW_LOG:
8420 log_scroll_up(&view->state.log, view->offset);
8421 view->state.log.selected += view->offset;
8422 break;
8423 case TOG_VIEW_REF:
8424 ref_scroll_up(&view->state.ref, view->offset);
8425 view->state.ref.selected += view->offset;
8426 break;
8427 case TOG_VIEW_TREE:
8428 tree_scroll_up(&view->state.tree, view->offset);
8429 view->state.tree.selected += view->offset;
8430 break;
8431 default:
8432 break;
8435 view->offset = 0;
8439 * If the selected line is in the section of screen covered by the bottom split,
8440 * scroll down offset lines to move it into view and index its new position.
8442 static const struct got_error *
8443 offset_selection_down(struct tog_view *view)
8445 const struct got_error *err = NULL;
8446 const struct got_error *(*scrolld)(struct tog_view *, int);
8447 int *selected = NULL;
8448 int header, offset;
8450 switch (view->type) {
8451 case TOG_VIEW_BLAME: {
8452 struct tog_blame_view_state *s = &view->state.blame;
8453 header = 3;
8454 scrolld = NULL;
8455 if (s->selected_line > view->nlines - header) {
8456 offset = abs(view->nlines - s->selected_line - header);
8457 s->first_displayed_line += offset;
8458 s->selected_line -= offset;
8459 view->offset = offset;
8461 break;
8463 case TOG_VIEW_LOG: {
8464 struct tog_log_view_state *s = &view->state.log;
8465 scrolld = &log_scroll_down;
8466 header = view_is_parent_view(view) ? 3 : 2;
8467 selected = &s->selected;
8468 break;
8470 case TOG_VIEW_REF: {
8471 struct tog_ref_view_state *s = &view->state.ref;
8472 scrolld = &ref_scroll_down;
8473 header = 3;
8474 selected = &s->selected;
8475 break;
8477 case TOG_VIEW_TREE: {
8478 struct tog_tree_view_state *s = &view->state.tree;
8479 scrolld = &tree_scroll_down;
8480 header = 5;
8481 selected = &s->selected;
8482 break;
8484 default:
8485 selected = NULL;
8486 scrolld = NULL;
8487 header = 0;
8488 break;
8491 if (selected && *selected > view->nlines - header) {
8492 offset = abs(view->nlines - *selected - header);
8493 view->offset = offset;
8494 if (scrolld && offset) {
8495 err = scrolld(view, offset);
8496 *selected -= offset;
8500 return err;
8503 static void
8504 list_commands(FILE *fp)
8506 size_t i;
8508 fprintf(fp, "commands:");
8509 for (i = 0; i < nitems(tog_commands); i++) {
8510 const struct tog_cmd *cmd = &tog_commands[i];
8511 fprintf(fp, " %s", cmd->name);
8513 fputc('\n', fp);
8516 __dead static void
8517 usage(int hflag, int status)
8519 FILE *fp = (status == 0) ? stdout : stderr;
8521 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8522 getprogname());
8523 if (hflag) {
8524 fprintf(fp, "lazy usage: %s path\n", getprogname());
8525 list_commands(fp);
8527 exit(status);
8530 static char **
8531 make_argv(int argc, ...)
8533 va_list ap;
8534 char **argv;
8535 int i;
8537 va_start(ap, argc);
8539 argv = calloc(argc, sizeof(char *));
8540 if (argv == NULL)
8541 err(1, "calloc");
8542 for (i = 0; i < argc; i++) {
8543 argv[i] = strdup(va_arg(ap, char *));
8544 if (argv[i] == NULL)
8545 err(1, "strdup");
8548 va_end(ap);
8549 return argv;
8553 * Try to convert 'tog path' into a 'tog log path' command.
8554 * The user could simply have mistyped the command rather than knowingly
8555 * provided a path. So check whether argv[0] can in fact be resolved
8556 * to a path in the HEAD commit and print a special error if not.
8557 * This hack is for mpi@ <3
8559 static const struct got_error *
8560 tog_log_with_path(int argc, char *argv[])
8562 const struct got_error *error = NULL, *close_err;
8563 const struct tog_cmd *cmd = NULL;
8564 struct got_repository *repo = NULL;
8565 struct got_worktree *worktree = NULL;
8566 struct got_object_id *commit_id = NULL, *id = NULL;
8567 struct got_commit_object *commit = NULL;
8568 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8569 char *commit_id_str = NULL, **cmd_argv = NULL;
8570 int *pack_fds = NULL;
8572 cwd = getcwd(NULL, 0);
8573 if (cwd == NULL)
8574 return got_error_from_errno("getcwd");
8576 error = got_repo_pack_fds_open(&pack_fds);
8577 if (error != NULL)
8578 goto done;
8580 error = got_worktree_open(&worktree, cwd);
8581 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8582 goto done;
8584 if (worktree)
8585 repo_path = strdup(got_worktree_get_repo_path(worktree));
8586 else
8587 repo_path = strdup(cwd);
8588 if (repo_path == NULL) {
8589 error = got_error_from_errno("strdup");
8590 goto done;
8593 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8594 if (error != NULL)
8595 goto done;
8597 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8598 repo, worktree);
8599 if (error)
8600 goto done;
8602 error = tog_load_refs(repo, 0);
8603 if (error)
8604 goto done;
8605 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8606 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8607 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8608 if (error)
8609 goto done;
8611 if (worktree) {
8612 got_worktree_close(worktree);
8613 worktree = NULL;
8616 error = got_object_open_as_commit(&commit, repo, commit_id);
8617 if (error)
8618 goto done;
8620 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8621 if (error) {
8622 if (error->code != GOT_ERR_NO_TREE_ENTRY)
8623 goto done;
8624 fprintf(stderr, "%s: '%s' is no known command or path\n",
8625 getprogname(), argv[0]);
8626 usage(1, 1);
8627 /* not reached */
8630 error = got_object_id_str(&commit_id_str, commit_id);
8631 if (error)
8632 goto done;
8634 cmd = &tog_commands[0]; /* log */
8635 argc = 4;
8636 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8637 error = cmd->cmd_main(argc, cmd_argv);
8638 done:
8639 if (repo) {
8640 close_err = got_repo_close(repo);
8641 if (error == NULL)
8642 error = close_err;
8644 if (commit)
8645 got_object_commit_close(commit);
8646 if (worktree)
8647 got_worktree_close(worktree);
8648 if (pack_fds) {
8649 const struct got_error *pack_err =
8650 got_repo_pack_fds_close(pack_fds);
8651 if (error == NULL)
8652 error = pack_err;
8654 free(id);
8655 free(commit_id_str);
8656 free(commit_id);
8657 free(cwd);
8658 free(repo_path);
8659 free(in_repo_path);
8660 if (cmd_argv) {
8661 int i;
8662 for (i = 0; i < argc; i++)
8663 free(cmd_argv[i]);
8664 free(cmd_argv);
8666 tog_free_refs();
8667 return error;
8670 int
8671 main(int argc, char *argv[])
8673 const struct got_error *error = NULL;
8674 const struct tog_cmd *cmd = NULL;
8675 int ch, hflag = 0, Vflag = 0;
8676 char **cmd_argv = NULL;
8677 static const struct option longopts[] = {
8678 { "version", no_argument, NULL, 'V' },
8679 { NULL, 0, NULL, 0}
8681 char *diff_algo_str = NULL;
8683 if (!isatty(STDIN_FILENO))
8684 errx(1, "standard input is not a tty");
8686 setlocale(LC_CTYPE, "");
8688 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8689 switch (ch) {
8690 case 'h':
8691 hflag = 1;
8692 break;
8693 case 'V':
8694 Vflag = 1;
8695 break;
8696 default:
8697 usage(hflag, 1);
8698 /* NOTREACHED */
8702 argc -= optind;
8703 argv += optind;
8704 optind = 1;
8705 optreset = 1;
8707 if (Vflag) {
8708 got_version_print_str();
8709 return 0;
8712 #ifndef PROFILE
8713 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8714 NULL) == -1)
8715 err(1, "pledge");
8716 #endif
8718 if (argc == 0) {
8719 if (hflag)
8720 usage(hflag, 0);
8721 /* Build an argument vector which runs a default command. */
8722 cmd = &tog_commands[0];
8723 argc = 1;
8724 cmd_argv = make_argv(argc, cmd->name);
8725 } else {
8726 size_t i;
8728 /* Did the user specify a command? */
8729 for (i = 0; i < nitems(tog_commands); i++) {
8730 if (strncmp(tog_commands[i].name, argv[0],
8731 strlen(argv[0])) == 0) {
8732 cmd = &tog_commands[i];
8733 break;
8738 diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8739 if (diff_algo_str) {
8740 if (strcasecmp(diff_algo_str, "patience") == 0)
8741 tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8742 if (strcasecmp(diff_algo_str, "myers") == 0)
8743 tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8746 if (cmd == NULL) {
8747 if (argc != 1)
8748 usage(0, 1);
8749 /* No command specified; try log with a path */
8750 error = tog_log_with_path(argc, argv);
8751 } else {
8752 if (hflag)
8753 cmd->cmd_usage();
8754 else
8755 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8758 endwin();
8759 putchar('\n');
8760 if (cmd_argv) {
8761 int i;
8762 for (i = 0; i < argc; i++)
8763 free(cmd_argv[i]);
8764 free(cmd_argv);
8767 if (error && error->code != GOT_ERR_CANCELLED)
8768 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8769 return 0;