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 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int *pack_fds;
342 int log_complete;
343 sig_atomic_t *quit;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
346 int *searching;
347 int *search_next_done;
348 regex_t *regex;
349 };
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
356 int selected;
357 char *in_repo_path;
358 char *head_ref_name;
359 int log_branches;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
362 sig_atomic_t quit;
363 pthread_t thread;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
368 };
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
388 int nlines;
390 struct tog_view *view;
391 struct got_object_id *commit_id;
392 int *quit;
393 };
395 struct tog_blame_thread_args {
396 const char *path;
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
399 int *complete;
400 got_cancel_cb cancel_cb;
401 void *cancel_arg;
402 };
404 struct tog_blame {
405 FILE *f;
406 off_t filesize;
407 struct tog_blame_line *lines;
408 int nlines;
409 off_t *line_offsets;
410 pthread_t thread;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
413 const char *path;
414 int *pack_fds;
415 };
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
420 int selected_line;
421 int blame_complete;
422 int eof;
423 int done;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
426 char *path;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
430 int matched_line;
431 struct tog_colors colors;
432 };
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
439 int selected;
440 };
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
445 char *tree_label;
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 char *head_ref_name;
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
458 };
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
463 int idx;
464 };
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 /*
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
495 */
496 TAILQ_HEAD(tog_view_list_head, tog_view);
498 struct tog_view {
499 TAILQ_ENTRY(tog_view) entry;
500 WINDOW *window;
501 PANEL *panel;
502 int nlines, ncols, begin_y, begin_x;
503 int maxx, x; /* max column and current start column */
504 int lines, cols; /* copies of LINES and COLS */
505 int focussed; /* Only set on one parent or child view at a time. */
506 int dying;
507 struct tog_view *parent;
508 struct tog_view *child;
510 /*
511 * This flag is initially set on parent views when a new child view
512 * is created. It gets toggled when the 'Tab' key switches focus
513 * between parent and child.
514 * The flag indicates whether focus should be passed on to our child
515 * view if this parent view gets picked for focus after another parent
516 * view was closed. This prevents child views from losing focus in such
517 * situations.
518 */
519 int focus_child;
521 /* type-specific state */
522 enum tog_view_type type;
523 union {
524 struct tog_diff_view_state diff;
525 struct tog_log_view_state log;
526 struct tog_blame_view_state blame;
527 struct tog_tree_view_state tree;
528 struct tog_ref_view_state ref;
529 } state;
531 const struct got_error *(*show)(struct tog_view *);
532 const struct got_error *(*input)(struct tog_view **,
533 struct tog_view *, int);
534 const struct got_error *(*close)(struct tog_view *);
536 const struct got_error *(*search_start)(struct tog_view *);
537 const struct got_error *(*search_next)(struct tog_view *);
538 int search_started;
539 int searching;
540 #define TOG_SEARCH_FORWARD 1
541 #define TOG_SEARCH_BACKWARD 2
542 int search_next_done;
543 #define TOG_SEARCH_HAVE_MORE 1
544 #define TOG_SEARCH_NO_MORE 2
545 #define TOG_SEARCH_HAVE_NONE 3
546 regex_t regex;
547 regmatch_t regmatch;
548 };
550 static const struct got_error *open_diff_view(struct tog_view *,
551 struct got_object_id *, struct got_object_id *,
552 const char *, const char *, int, int, int, struct tog_view *,
553 struct got_repository *);
554 static const struct got_error *show_diff_view(struct tog_view *);
555 static const struct got_error *input_diff_view(struct tog_view **,
556 struct tog_view *, int);
557 static const struct got_error* close_diff_view(struct tog_view *);
558 static const struct got_error *search_start_diff_view(struct tog_view *);
559 static const struct got_error *search_next_diff_view(struct tog_view *);
561 static const struct got_error *open_log_view(struct tog_view *,
562 struct got_object_id *, struct got_repository *,
563 const char *, const char *, int);
564 static const struct got_error * show_log_view(struct tog_view *);
565 static const struct got_error *input_log_view(struct tog_view **,
566 struct tog_view *, int);
567 static const struct got_error *close_log_view(struct tog_view *);
568 static const struct got_error *search_start_log_view(struct tog_view *);
569 static const struct got_error *search_next_log_view(struct tog_view *);
571 static const struct got_error *open_blame_view(struct tog_view *, char *,
572 struct got_object_id *, struct got_repository *);
573 static const struct got_error *show_blame_view(struct tog_view *);
574 static const struct got_error *input_blame_view(struct tog_view **,
575 struct tog_view *, int);
576 static const struct got_error *close_blame_view(struct tog_view *);
577 static const struct got_error *search_start_blame_view(struct tog_view *);
578 static const struct got_error *search_next_blame_view(struct tog_view *);
580 static const struct got_error *open_tree_view(struct tog_view *,
581 struct got_object_id *, const char *, struct got_repository *);
582 static const struct got_error *show_tree_view(struct tog_view *);
583 static const struct got_error *input_tree_view(struct tog_view **,
584 struct tog_view *, int);
585 static const struct got_error *close_tree_view(struct tog_view *);
586 static const struct got_error *search_start_tree_view(struct tog_view *);
587 static const struct got_error *search_next_tree_view(struct tog_view *);
589 static const struct got_error *open_ref_view(struct tog_view *,
590 struct got_repository *);
591 static const struct got_error *show_ref_view(struct tog_view *);
592 static const struct got_error *input_ref_view(struct tog_view **,
593 struct tog_view *, int);
594 static const struct got_error *close_ref_view(struct tog_view *);
595 static const struct got_error *search_start_ref_view(struct tog_view *);
596 static const struct got_error *search_next_ref_view(struct tog_view *);
598 static volatile sig_atomic_t tog_sigwinch_received;
599 static volatile sig_atomic_t tog_sigpipe_received;
600 static volatile sig_atomic_t tog_sigcont_received;
601 static volatile sig_atomic_t tog_sigint_received;
602 static volatile sig_atomic_t tog_sigterm_received;
604 static void
605 tog_sigwinch(int signo)
607 tog_sigwinch_received = 1;
610 static void
611 tog_sigpipe(int signo)
613 tog_sigpipe_received = 1;
616 static void
617 tog_sigcont(int signo)
619 tog_sigcont_received = 1;
622 static void
623 tog_sigint(int signo)
625 tog_sigint_received = 1;
628 static void
629 tog_sigterm(int signo)
631 tog_sigterm_received = 1;
634 static int
635 tog_fatal_signal_received(void)
637 return (tog_sigpipe_received ||
638 tog_sigint_received || tog_sigint_received);
642 static const struct got_error *
643 view_close(struct tog_view *view)
645 const struct got_error *err = NULL;
647 if (view->child) {
648 view_close(view->child);
649 view->child = NULL;
651 if (view->close)
652 err = view->close(view);
653 if (view->panel)
654 del_panel(view->panel);
655 if (view->window)
656 delwin(view->window);
657 free(view);
658 return err;
661 static struct tog_view *
662 view_open(int nlines, int ncols, int begin_y, int begin_x,
663 enum tog_view_type type)
665 struct tog_view *view = calloc(1, sizeof(*view));
667 if (view == NULL)
668 return NULL;
670 view->type = type;
671 view->lines = LINES;
672 view->cols = COLS;
673 view->nlines = nlines ? nlines : LINES - begin_y;
674 view->ncols = ncols ? ncols : COLS - begin_x;
675 view->begin_y = begin_y;
676 view->begin_x = begin_x;
677 view->window = newwin(nlines, ncols, begin_y, begin_x);
678 if (view->window == NULL) {
679 view_close(view);
680 return NULL;
682 view->panel = new_panel(view->window);
683 if (view->panel == NULL ||
684 set_panel_userptr(view->panel, view) != OK) {
685 view_close(view);
686 return NULL;
689 keypad(view->window, TRUE);
690 return view;
693 static int
694 view_split_begin_x(int begin_x)
696 if (begin_x > 0 || COLS < 120)
697 return 0;
698 return (COLS - MAX(COLS / 2, 80));
701 static const struct got_error *view_resize(struct tog_view *);
703 static const struct got_error *
704 view_splitscreen(struct tog_view *view)
706 const struct got_error *err = NULL;
708 view->begin_y = 0;
709 view->begin_x = view_split_begin_x(0);
710 view->nlines = LINES;
711 view->ncols = COLS - view->begin_x;
712 view->lines = LINES;
713 view->cols = COLS;
714 err = view_resize(view);
715 if (err)
716 return err;
718 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
719 return got_error_from_errno("mvwin");
721 return NULL;
724 static const struct got_error *
725 view_fullscreen(struct tog_view *view)
727 const struct got_error *err = NULL;
729 view->begin_x = 0;
730 view->begin_y = 0;
731 view->nlines = LINES;
732 view->ncols = COLS;
733 view->lines = LINES;
734 view->cols = COLS;
735 err = view_resize(view);
736 if (err)
737 return err;
739 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
740 return got_error_from_errno("mvwin");
742 return NULL;
745 static int
746 view_is_parent_view(struct tog_view *view)
748 return view->parent == NULL;
751 static int
752 view_is_splitscreen(struct tog_view *view)
754 return view->begin_x > 0;
758 static const struct got_error *
759 view_resize(struct tog_view *view)
761 int nlines, ncols;
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
765 else
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
770 else
771 ncols = view->ncols + (COLS - view->cols);
773 if (view->child && view_is_splitscreen(view->child)) {
774 view->child->begin_x = view_split_begin_x(view->begin_x);
775 if (view->child->begin_x == 0) {
776 ncols = COLS;
778 view_fullscreen(view->child);
779 if (view->child->focussed)
780 show_panel(view->child->panel);
781 else
782 show_panel(view->panel);
783 } else {
784 ncols = view->child->begin_x;
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
789 } else if (view->parent == NULL)
790 ncols = COLS;
792 if (wresize(view->window, nlines, ncols) == ERR)
793 return got_error_from_errno("wresize");
794 if (replace_panel(view->panel, view->window) == ERR)
795 return got_error_from_errno("replace_panel");
796 wclear(view->window);
798 view->nlines = nlines;
799 view->ncols = ncols;
800 view->lines = LINES;
801 view->cols = COLS;
803 return NULL;
806 static const struct got_error *
807 view_close_child(struct tog_view *view)
809 const struct got_error *err = NULL;
811 if (view->child == NULL)
812 return NULL;
814 err = view_close(view->child);
815 view->child = NULL;
816 return err;
819 static const struct got_error *
820 view_set_child(struct tog_view *view, struct tog_view *child)
822 view->child = child;
823 child->parent = view;
825 return view_resize(view);
828 static void
829 tog_resizeterm(void)
831 int cols, lines;
832 struct winsize size;
834 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
835 cols = 80; /* Default */
836 lines = 24;
837 } else {
838 cols = size.ws_col;
839 lines = size.ws_row;
841 resize_term(lines, cols);
844 static const struct got_error *
845 view_search_start(struct tog_view *view)
847 const struct got_error *err = NULL;
848 char pattern[1024];
849 int ret;
851 if (view->search_started) {
852 regfree(&view->regex);
853 view->searching = 0;
854 memset(&view->regmatch, 0, sizeof(view->regmatch));
856 view->search_started = 0;
858 if (view->nlines < 1)
859 return NULL;
861 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
862 wclrtoeol(view->window);
864 nocbreak();
865 echo();
866 ret = wgetnstr(view->window, pattern, sizeof(pattern));
867 cbreak();
868 noecho();
869 if (ret == ERR)
870 return NULL;
872 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
873 err = view->search_start(view);
874 if (err) {
875 regfree(&view->regex);
876 return err;
878 view->search_started = 1;
879 view->searching = TOG_SEARCH_FORWARD;
880 view->search_next_done = 0;
881 view->search_next(view);
884 return NULL;
887 static const struct got_error *
888 view_input(struct tog_view **new, int *done, struct tog_view *view,
889 struct tog_view_list_head *views)
891 const struct got_error *err = NULL;
892 struct tog_view *v;
893 int ch, errcode;
895 *new = NULL;
897 /* Clear "no matches" indicator. */
898 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
899 view->search_next_done == TOG_SEARCH_HAVE_NONE)
900 view->search_next_done = TOG_SEARCH_HAVE_MORE;
902 if (view->searching && !view->search_next_done) {
903 errcode = pthread_mutex_unlock(&tog_mutex);
904 if (errcode)
905 return got_error_set_errno(errcode,
906 "pthread_mutex_unlock");
907 sched_yield();
908 errcode = pthread_mutex_lock(&tog_mutex);
909 if (errcode)
910 return got_error_set_errno(errcode,
911 "pthread_mutex_lock");
912 view->search_next(view);
913 return NULL;
916 nodelay(stdscr, FALSE);
917 /* Allow threads to make progress while we are waiting for input. */
918 errcode = pthread_mutex_unlock(&tog_mutex);
919 if (errcode)
920 return got_error_set_errno(errcode, "pthread_mutex_unlock");
921 ch = wgetch(view->window);
922 errcode = pthread_mutex_lock(&tog_mutex);
923 if (errcode)
924 return got_error_set_errno(errcode, "pthread_mutex_lock");
925 nodelay(stdscr, TRUE);
927 if (tog_sigwinch_received || tog_sigcont_received) {
928 tog_resizeterm();
929 tog_sigwinch_received = 0;
930 tog_sigcont_received = 0;
931 TAILQ_FOREACH(v, views, entry) {
932 err = view_resize(v);
933 if (err)
934 return err;
935 err = v->input(new, v, KEY_RESIZE);
936 if (err)
937 return err;
938 if (v->child) {
939 err = view_resize(v->child);
940 if (err)
941 return err;
942 err = v->child->input(new, v->child,
943 KEY_RESIZE);
944 if (err)
945 return err;
950 switch (ch) {
951 case '\t':
952 if (view->child) {
953 view->focussed = 0;
954 view->child->focussed = 1;
955 view->focus_child = 1;
956 } else if (view->parent) {
957 view->focussed = 0;
958 view->parent->focussed = 1;
959 view->parent->focus_child = 0;
960 if (!view_is_splitscreen(view))
961 err = view_fullscreen(view->parent);
963 break;
964 case 'q':
965 err = view->input(new, view, ch);
966 view->dying = 1;
967 break;
968 case 'Q':
969 *done = 1;
970 break;
971 case 'F':
972 if (view_is_parent_view(view)) {
973 if (view->child == NULL)
974 break;
975 if (view_is_splitscreen(view->child)) {
976 view->focussed = 0;
977 view->child->focussed = 1;
978 err = view_fullscreen(view->child);
979 } else
980 err = view_splitscreen(view->child);
981 if (err)
982 break;
983 err = view->child->input(new, view->child,
984 KEY_RESIZE);
985 } else {
986 if (view_is_splitscreen(view)) {
987 view->parent->focussed = 0;
988 view->focussed = 1;
989 err = view_fullscreen(view);
990 } else {
991 err = view_splitscreen(view);
992 if (!err)
993 err = view_resize(view->parent);
995 if (err)
996 break;
997 err = view->input(new, view, KEY_RESIZE);
999 break;
1000 case KEY_RESIZE:
1001 break;
1002 case '/':
1003 if (view->search_start)
1004 view_search_start(view);
1005 else
1006 err = view->input(new, view, ch);
1007 break;
1008 case 'N':
1009 case 'n':
1010 if (view->search_started && view->search_next) {
1011 view->searching = (ch == 'n' ?
1012 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1013 view->search_next_done = 0;
1014 view->search_next(view);
1015 } else
1016 err = view->input(new, view, ch);
1017 break;
1018 default:
1019 err = view->input(new, view, ch);
1020 break;
1023 return err;
1026 void
1027 view_vborder(struct tog_view *view)
1029 PANEL *panel;
1030 const struct tog_view *view_above;
1032 if (view->parent)
1033 return view_vborder(view->parent);
1035 panel = panel_above(view->panel);
1036 if (panel == NULL)
1037 return;
1039 view_above = panel_userptr(panel);
1040 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1041 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1044 int
1045 view_needs_focus_indication(struct tog_view *view)
1047 if (view_is_parent_view(view)) {
1048 if (view->child == NULL || view->child->focussed)
1049 return 0;
1050 if (!view_is_splitscreen(view->child))
1051 return 0;
1052 } else if (!view_is_splitscreen(view))
1053 return 0;
1055 return view->focussed;
1058 static const struct got_error *
1059 view_loop(struct tog_view *view)
1061 const struct got_error *err = NULL;
1062 struct tog_view_list_head views;
1063 struct tog_view *new_view;
1064 int fast_refresh = 10;
1065 int done = 0, errcode;
1067 errcode = pthread_mutex_lock(&tog_mutex);
1068 if (errcode)
1069 return got_error_set_errno(errcode, "pthread_mutex_lock");
1071 TAILQ_INIT(&views);
1072 TAILQ_INSERT_HEAD(&views, view, entry);
1074 view->focussed = 1;
1075 err = view->show(view);
1076 if (err)
1077 return err;
1078 update_panels();
1079 doupdate();
1080 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1081 /* Refresh fast during initialization, then become slower. */
1082 if (fast_refresh && fast_refresh-- == 0)
1083 halfdelay(10); /* switch to once per second */
1085 err = view_input(&new_view, &done, view, &views);
1086 if (err)
1087 break;
1088 if (view->dying) {
1089 struct tog_view *v, *prev = NULL;
1091 if (view_is_parent_view(view))
1092 prev = TAILQ_PREV(view, tog_view_list_head,
1093 entry);
1094 else if (view->parent)
1095 prev = view->parent;
1097 if (view->parent) {
1098 view->parent->child = NULL;
1099 view->parent->focus_child = 0;
1101 err = view_resize(view->parent);
1102 if (err)
1103 break;
1104 } else
1105 TAILQ_REMOVE(&views, view, entry);
1107 err = view_close(view);
1108 if (err)
1109 goto done;
1111 view = NULL;
1112 TAILQ_FOREACH(v, &views, entry) {
1113 if (v->focussed)
1114 break;
1116 if (view == NULL && new_view == NULL) {
1117 /* No view has focus. Try to pick one. */
1118 if (prev)
1119 view = prev;
1120 else if (!TAILQ_EMPTY(&views)) {
1121 view = TAILQ_LAST(&views,
1122 tog_view_list_head);
1124 if (view) {
1125 if (view->focus_child) {
1126 view->child->focussed = 1;
1127 view = view->child;
1128 } else
1129 view->focussed = 1;
1133 if (new_view) {
1134 struct tog_view *v, *t;
1135 /* Only allow one parent view per type. */
1136 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1137 if (v->type != new_view->type)
1138 continue;
1139 TAILQ_REMOVE(&views, v, entry);
1140 err = view_close(v);
1141 if (err)
1142 goto done;
1143 break;
1145 TAILQ_INSERT_TAIL(&views, new_view, entry);
1146 view = new_view;
1148 if (view) {
1149 if (view_is_parent_view(view)) {
1150 if (view->child && view->child->focussed)
1151 view = view->child;
1152 } else {
1153 if (view->parent && view->parent->focussed)
1154 view = view->parent;
1156 show_panel(view->panel);
1157 if (view->child && view_is_splitscreen(view->child))
1158 show_panel(view->child->panel);
1159 if (view->parent && view_is_splitscreen(view)) {
1160 err = view->parent->show(view->parent);
1161 if (err)
1162 goto done;
1164 err = view->show(view);
1165 if (err)
1166 goto done;
1167 if (view->child) {
1168 err = view->child->show(view->child);
1169 if (err)
1170 goto done;
1172 update_panels();
1173 doupdate();
1176 done:
1177 while (!TAILQ_EMPTY(&views)) {
1178 view = TAILQ_FIRST(&views);
1179 TAILQ_REMOVE(&views, view, entry);
1180 view_close(view);
1183 errcode = pthread_mutex_unlock(&tog_mutex);
1184 if (errcode && err == NULL)
1185 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1187 return err;
1190 __dead static void
1191 usage_log(void)
1193 endwin();
1194 fprintf(stderr,
1195 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1196 getprogname());
1197 exit(1);
1200 /* Create newly allocated wide-character string equivalent to a byte string. */
1201 static const struct got_error *
1202 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1204 char *vis = NULL;
1205 const struct got_error *err = NULL;
1207 *ws = NULL;
1208 *wlen = mbstowcs(NULL, s, 0);
1209 if (*wlen == (size_t)-1) {
1210 int vislen;
1211 if (errno != EILSEQ)
1212 return got_error_from_errno("mbstowcs");
1214 /* byte string invalid in current encoding; try to "fix" it */
1215 err = got_mbsavis(&vis, &vislen, s);
1216 if (err)
1217 return err;
1218 *wlen = mbstowcs(NULL, vis, 0);
1219 if (*wlen == (size_t)-1) {
1220 err = got_error_from_errno("mbstowcs"); /* give up */
1221 goto done;
1225 *ws = calloc(*wlen + 1, sizeof(**ws));
1226 if (*ws == NULL) {
1227 err = got_error_from_errno("calloc");
1228 goto done;
1231 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1232 err = got_error_from_errno("mbstowcs");
1233 done:
1234 free(vis);
1235 if (err) {
1236 free(*ws);
1237 *ws = NULL;
1238 *wlen = 0;
1240 return err;
1243 static const struct got_error *
1244 expand_tab(char **ptr, const char *src)
1246 char *dst;
1247 size_t len, n, idx = 0, sz = 0;
1249 *ptr = NULL;
1250 n = len = strlen(src);
1251 dst = malloc(n + 1);
1252 if (dst == NULL)
1253 return got_error_from_errno("malloc");
1255 while (idx < len && src[idx]) {
1256 const char c = src[idx];
1258 if (c == '\t') {
1259 size_t nb = TABSIZE - sz % TABSIZE;
1260 char *p;
1262 p = realloc(dst, n + nb);
1263 if (p == NULL) {
1264 free(dst);
1265 return got_error_from_errno("realloc");
1268 dst = p;
1269 n += nb;
1270 memset(dst + sz, ' ', nb);
1271 sz += nb;
1272 } else
1273 dst[sz++] = src[idx];
1274 ++idx;
1277 dst[sz] = '\0';
1278 *ptr = dst;
1279 return NULL;
1283 * Advance at most n columns from wline starting at offset off.
1284 * Return the index to the first character after the span operation.
1285 * Return the combined column width of all spanned wide character in
1286 * *rcol.
1288 static int
1289 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1291 int width, i, cols = 0;
1293 if (n == 0) {
1294 *rcol = cols;
1295 return off;
1298 for (i = off; wline[i] != L'\0'; ++i) {
1299 if (wline[i] == L'\t')
1300 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1301 else
1302 width = wcwidth(wline[i]);
1304 if (width == -1) {
1305 width = 1;
1306 wline[i] = L'.';
1309 if (cols + width > n)
1310 break;
1311 cols += width;
1314 *rcol = cols;
1315 return i;
1319 * Format a line for display, ensuring that it won't overflow a width limit.
1320 * With scrolling, the width returned refers to the scrolled version of the
1321 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1323 static const struct got_error *
1324 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1325 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1327 const struct got_error *err = NULL;
1328 int cols;
1329 wchar_t *wline = NULL;
1330 char *exstr = NULL;
1331 size_t wlen;
1332 int i, scrollx;
1334 *wlinep = NULL;
1335 *widthp = 0;
1337 if (expand) {
1338 err = expand_tab(&exstr, line);
1339 if (err)
1340 return err;
1343 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1344 free(exstr);
1345 if (err)
1346 return err;
1348 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1350 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1351 wline[wlen - 1] = L'\0';
1352 wlen--;
1354 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1355 wline[wlen - 1] = L'\0';
1356 wlen--;
1359 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1360 wline[i] = L'\0';
1362 if (widthp)
1363 *widthp = cols;
1364 if (scrollxp)
1365 *scrollxp = scrollx;
1366 if (err)
1367 free(wline);
1368 else
1369 *wlinep = wline;
1370 return err;
1373 static const struct got_error*
1374 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1375 struct got_object_id *id, struct got_repository *repo)
1377 static const struct got_error *err = NULL;
1378 struct got_reflist_entry *re;
1379 char *s;
1380 const char *name;
1382 *refs_str = NULL;
1384 TAILQ_FOREACH(re, refs, entry) {
1385 struct got_tag_object *tag = NULL;
1386 struct got_object_id *ref_id;
1387 int cmp;
1389 name = got_ref_get_name(re->ref);
1390 if (strcmp(name, GOT_REF_HEAD) == 0)
1391 continue;
1392 if (strncmp(name, "refs/", 5) == 0)
1393 name += 5;
1394 if (strncmp(name, "got/", 4) == 0 &&
1395 strncmp(name, "got/backup/", 11) != 0)
1396 continue;
1397 if (strncmp(name, "heads/", 6) == 0)
1398 name += 6;
1399 if (strncmp(name, "remotes/", 8) == 0) {
1400 name += 8;
1401 s = strstr(name, "/" GOT_REF_HEAD);
1402 if (s != NULL && s[strlen(s)] == '\0')
1403 continue;
1405 err = got_ref_resolve(&ref_id, repo, re->ref);
1406 if (err)
1407 break;
1408 if (strncmp(name, "tags/", 5) == 0) {
1409 err = got_object_open_as_tag(&tag, repo, ref_id);
1410 if (err) {
1411 if (err->code != GOT_ERR_OBJ_TYPE) {
1412 free(ref_id);
1413 break;
1415 /* Ref points at something other than a tag. */
1416 err = NULL;
1417 tag = NULL;
1420 cmp = got_object_id_cmp(tag ?
1421 got_object_tag_get_object_id(tag) : ref_id, id);
1422 free(ref_id);
1423 if (tag)
1424 got_object_tag_close(tag);
1425 if (cmp != 0)
1426 continue;
1427 s = *refs_str;
1428 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1429 s ? ", " : "", name) == -1) {
1430 err = got_error_from_errno("asprintf");
1431 free(s);
1432 *refs_str = NULL;
1433 break;
1435 free(s);
1438 return err;
1441 static const struct got_error *
1442 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1443 int col_tab_align)
1445 char *smallerthan;
1447 smallerthan = strchr(author, '<');
1448 if (smallerthan && smallerthan[1] != '\0')
1449 author = smallerthan + 1;
1450 author[strcspn(author, "@>")] = '\0';
1451 return format_line(wauthor, author_width, NULL, author, 0, limit,
1452 col_tab_align, 0);
1455 static const struct got_error *
1456 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1457 struct got_object_id *id, const size_t date_display_cols,
1458 int author_display_cols)
1460 struct tog_log_view_state *s = &view->state.log;
1461 const struct got_error *err = NULL;
1462 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1463 char *logmsg0 = NULL, *logmsg = NULL;
1464 char *author = NULL;
1465 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1466 int author_width, logmsg_width;
1467 char *newline, *line = NULL;
1468 int col, limit, scrollx;
1469 const int avail = view->ncols;
1470 struct tm tm;
1471 time_t committer_time;
1472 struct tog_color *tc;
1474 committer_time = got_object_commit_get_committer_time(commit);
1475 if (gmtime_r(&committer_time, &tm) == NULL)
1476 return got_error_from_errno("gmtime_r");
1477 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1478 return got_error(GOT_ERR_NO_SPACE);
1480 if (avail <= date_display_cols)
1481 limit = MIN(sizeof(datebuf) - 1, avail);
1482 else
1483 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1484 tc = get_color(&s->colors, TOG_COLOR_DATE);
1485 if (tc)
1486 wattr_on(view->window,
1487 COLOR_PAIR(tc->colorpair), NULL);
1488 waddnstr(view->window, datebuf, limit);
1489 if (tc)
1490 wattr_off(view->window,
1491 COLOR_PAIR(tc->colorpair), NULL);
1492 col = limit;
1493 if (col > avail)
1494 goto done;
1496 if (avail >= 120) {
1497 char *id_str;
1498 err = got_object_id_str(&id_str, id);
1499 if (err)
1500 goto done;
1501 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1502 if (tc)
1503 wattr_on(view->window,
1504 COLOR_PAIR(tc->colorpair), NULL);
1505 wprintw(view->window, "%.8s ", id_str);
1506 if (tc)
1507 wattr_off(view->window,
1508 COLOR_PAIR(tc->colorpair), NULL);
1509 free(id_str);
1510 col += 9;
1511 if (col > avail)
1512 goto done;
1515 author = strdup(got_object_commit_get_author(commit));
1516 if (author == NULL) {
1517 err = got_error_from_errno("strdup");
1518 goto done;
1520 err = format_author(&wauthor, &author_width, author, avail - col, col);
1521 if (err)
1522 goto done;
1523 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1524 if (tc)
1525 wattr_on(view->window,
1526 COLOR_PAIR(tc->colorpair), NULL);
1527 waddwstr(view->window, wauthor);
1528 if (tc)
1529 wattr_off(view->window,
1530 COLOR_PAIR(tc->colorpair), NULL);
1531 col += author_width;
1532 while (col < avail && author_width < author_display_cols + 2) {
1533 waddch(view->window, ' ');
1534 col++;
1535 author_width++;
1537 if (col > avail)
1538 goto done;
1540 err = got_object_commit_get_logmsg(&logmsg0, commit);
1541 if (err)
1542 goto done;
1543 logmsg = logmsg0;
1544 while (*logmsg == '\n')
1545 logmsg++;
1546 newline = strchr(logmsg, '\n');
1547 if (newline)
1548 *newline = '\0';
1549 limit = avail - col;
1550 if (view->child && limit > 0)
1551 limit--; /* for the border */
1552 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1553 limit, col, 1);
1554 if (err)
1555 goto done;
1556 waddwstr(view->window, &wlogmsg[scrollx]);
1557 col += MAX(logmsg_width, 0);
1558 while (col < avail) {
1559 waddch(view->window, ' ');
1560 col++;
1562 done:
1563 free(logmsg0);
1564 free(wlogmsg);
1565 free(author);
1566 free(wauthor);
1567 free(line);
1568 return err;
1571 static struct commit_queue_entry *
1572 alloc_commit_queue_entry(struct got_commit_object *commit,
1573 struct got_object_id *id)
1575 struct commit_queue_entry *entry;
1577 entry = calloc(1, sizeof(*entry));
1578 if (entry == NULL)
1579 return NULL;
1581 entry->id = id;
1582 entry->commit = commit;
1583 return entry;
1586 static void
1587 pop_commit(struct commit_queue *commits)
1589 struct commit_queue_entry *entry;
1591 entry = TAILQ_FIRST(&commits->head);
1592 TAILQ_REMOVE(&commits->head, entry, entry);
1593 got_object_commit_close(entry->commit);
1594 commits->ncommits--;
1595 /* Don't free entry->id! It is owned by the commit graph. */
1596 free(entry);
1599 static void
1600 free_commits(struct commit_queue *commits)
1602 while (!TAILQ_EMPTY(&commits->head))
1603 pop_commit(commits);
1606 static const struct got_error *
1607 match_commit(int *have_match, struct got_object_id *id,
1608 struct got_commit_object *commit, regex_t *regex)
1610 const struct got_error *err = NULL;
1611 regmatch_t regmatch;
1612 char *id_str = NULL, *logmsg = NULL;
1614 *have_match = 0;
1616 err = got_object_id_str(&id_str, id);
1617 if (err)
1618 return err;
1620 err = got_object_commit_get_logmsg(&logmsg, commit);
1621 if (err)
1622 goto done;
1624 if (regexec(regex, got_object_commit_get_author(commit), 1,
1625 &regmatch, 0) == 0 ||
1626 regexec(regex, got_object_commit_get_committer(commit), 1,
1627 &regmatch, 0) == 0 ||
1628 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1629 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1630 *have_match = 1;
1631 done:
1632 free(id_str);
1633 free(logmsg);
1634 return err;
1637 static const struct got_error *
1638 queue_commits(struct tog_log_thread_args *a)
1640 const struct got_error *err = NULL;
1643 * We keep all commits open throughout the lifetime of the log
1644 * view in order to avoid having to re-fetch commits from disk
1645 * while updating the display.
1647 do {
1648 struct got_object_id *id;
1649 struct got_commit_object *commit;
1650 struct commit_queue_entry *entry;
1651 int errcode;
1653 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1654 NULL, NULL);
1655 if (err || id == NULL)
1656 break;
1658 err = got_object_open_as_commit(&commit, a->repo, id);
1659 if (err)
1660 break;
1661 entry = alloc_commit_queue_entry(commit, id);
1662 if (entry == NULL) {
1663 err = got_error_from_errno("alloc_commit_queue_entry");
1664 break;
1667 errcode = pthread_mutex_lock(&tog_mutex);
1668 if (errcode) {
1669 err = got_error_set_errno(errcode,
1670 "pthread_mutex_lock");
1671 break;
1674 entry->idx = a->commits->ncommits;
1675 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1676 a->commits->ncommits++;
1678 if (*a->searching == TOG_SEARCH_FORWARD &&
1679 !*a->search_next_done) {
1680 int have_match;
1681 err = match_commit(&have_match, id, commit, a->regex);
1682 if (err)
1683 break;
1684 if (have_match)
1685 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1688 errcode = pthread_mutex_unlock(&tog_mutex);
1689 if (errcode && err == NULL)
1690 err = got_error_set_errno(errcode,
1691 "pthread_mutex_unlock");
1692 if (err)
1693 break;
1694 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1696 return err;
1699 static void
1700 select_commit(struct tog_log_view_state *s)
1702 struct commit_queue_entry *entry;
1703 int ncommits = 0;
1705 entry = s->first_displayed_entry;
1706 while (entry) {
1707 if (ncommits == s->selected) {
1708 s->selected_entry = entry;
1709 break;
1711 entry = TAILQ_NEXT(entry, entry);
1712 ncommits++;
1716 static const struct got_error *
1717 draw_commits(struct tog_view *view)
1719 const struct got_error *err = NULL;
1720 struct tog_log_view_state *s = &view->state.log;
1721 struct commit_queue_entry *entry = s->selected_entry;
1722 const int limit = view->nlines;
1723 int width;
1724 int ncommits, author_cols = 4;
1725 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1726 char *refs_str = NULL;
1727 wchar_t *wline;
1728 struct tog_color *tc;
1729 static const size_t date_display_cols = 12;
1731 if (s->selected_entry &&
1732 !(view->searching && view->search_next_done == 0)) {
1733 struct got_reflist_head *refs;
1734 err = got_object_id_str(&id_str, s->selected_entry->id);
1735 if (err)
1736 return err;
1737 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1738 s->selected_entry->id);
1739 if (refs) {
1740 err = build_refs_str(&refs_str, refs,
1741 s->selected_entry->id, s->repo);
1742 if (err)
1743 goto done;
1747 if (s->thread_args.commits_needed == 0)
1748 halfdelay(10); /* disable fast refresh */
1750 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1751 if (asprintf(&ncommits_str, " [%d/%d] %s",
1752 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1753 (view->searching && !view->search_next_done) ?
1754 "searching..." : "loading...") == -1) {
1755 err = got_error_from_errno("asprintf");
1756 goto done;
1758 } else {
1759 const char *search_str = NULL;
1761 if (view->searching) {
1762 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1763 search_str = "no more matches";
1764 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1765 search_str = "no matches found";
1766 else if (!view->search_next_done)
1767 search_str = "searching...";
1770 if (asprintf(&ncommits_str, " [%d/%d] %s",
1771 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1772 search_str ? search_str :
1773 (refs_str ? refs_str : "")) == -1) {
1774 err = got_error_from_errno("asprintf");
1775 goto done;
1779 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1780 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1781 "........................................",
1782 s->in_repo_path, ncommits_str) == -1) {
1783 err = got_error_from_errno("asprintf");
1784 header = NULL;
1785 goto done;
1787 } else if (asprintf(&header, "commit %s%s",
1788 id_str ? id_str : "........................................",
1789 ncommits_str) == -1) {
1790 err = got_error_from_errno("asprintf");
1791 header = NULL;
1792 goto done;
1794 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1795 if (err)
1796 goto done;
1798 werase(view->window);
1800 if (view_needs_focus_indication(view))
1801 wstandout(view->window);
1802 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1803 if (tc)
1804 wattr_on(view->window,
1805 COLOR_PAIR(tc->colorpair), NULL);
1806 waddwstr(view->window, wline);
1807 if (tc)
1808 wattr_off(view->window,
1809 COLOR_PAIR(tc->colorpair), NULL);
1810 while (width < view->ncols) {
1811 waddch(view->window, ' ');
1812 width++;
1814 if (view_needs_focus_indication(view))
1815 wstandend(view->window);
1816 free(wline);
1817 if (limit <= 1)
1818 goto done;
1820 /* Grow author column size if necessary, and set view->maxx. */
1821 entry = s->first_displayed_entry;
1822 ncommits = 0;
1823 view->maxx = 0;
1824 while (entry) {
1825 char *author, *eol, *msg, *msg0;
1826 wchar_t *wauthor, *wmsg;
1827 int width;
1828 if (ncommits >= limit - 1)
1829 break;
1830 author = strdup(got_object_commit_get_author(entry->commit));
1831 if (author == NULL) {
1832 err = got_error_from_errno("strdup");
1833 goto done;
1835 err = format_author(&wauthor, &width, author, COLS,
1836 date_display_cols);
1837 if (author_cols < width)
1838 author_cols = width;
1839 free(wauthor);
1840 free(author);
1841 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1842 if (err)
1843 goto done;
1844 msg = msg0;
1845 while (*msg == '\n')
1846 ++msg;
1847 if ((eol = strchr(msg, '\n')))
1848 *eol = '\0';
1849 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1850 date_display_cols + author_cols, 0);
1851 if (err)
1852 goto done;
1853 view->maxx = MAX(view->maxx, width);
1854 free(msg0);
1855 free(wmsg);
1856 ncommits++;
1857 entry = TAILQ_NEXT(entry, entry);
1860 entry = s->first_displayed_entry;
1861 s->last_displayed_entry = s->first_displayed_entry;
1862 ncommits = 0;
1863 while (entry) {
1864 if (ncommits >= limit - 1)
1865 break;
1866 if (ncommits == s->selected)
1867 wstandout(view->window);
1868 err = draw_commit(view, entry->commit, entry->id,
1869 date_display_cols, author_cols);
1870 if (ncommits == s->selected)
1871 wstandend(view->window);
1872 if (err)
1873 goto done;
1874 ncommits++;
1875 s->last_displayed_entry = entry;
1876 entry = TAILQ_NEXT(entry, entry);
1879 view_vborder(view);
1880 done:
1881 free(id_str);
1882 free(refs_str);
1883 free(ncommits_str);
1884 free(header);
1885 return err;
1888 static void
1889 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1891 struct commit_queue_entry *entry;
1892 int nscrolled = 0;
1894 entry = TAILQ_FIRST(&s->commits.head);
1895 if (s->first_displayed_entry == entry)
1896 return;
1898 entry = s->first_displayed_entry;
1899 while (entry && nscrolled < maxscroll) {
1900 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1901 if (entry) {
1902 s->first_displayed_entry = entry;
1903 nscrolled++;
1908 static const struct got_error *
1909 trigger_log_thread(struct tog_view *view, int wait)
1911 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1912 int errcode;
1914 halfdelay(1); /* fast refresh while loading commits */
1916 while (ta->commits_needed > 0 || ta->load_all) {
1917 if (ta->log_complete)
1918 break;
1920 /* Wake the log thread. */
1921 errcode = pthread_cond_signal(&ta->need_commits);
1922 if (errcode)
1923 return got_error_set_errno(errcode,
1924 "pthread_cond_signal");
1927 * The mutex will be released while the view loop waits
1928 * in wgetch(), at which time the log thread will run.
1930 if (!wait)
1931 break;
1933 /* Display progress update in log view. */
1934 show_log_view(view);
1935 update_panels();
1936 doupdate();
1938 /* Wait right here while next commit is being loaded. */
1939 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1940 if (errcode)
1941 return got_error_set_errno(errcode,
1942 "pthread_cond_wait");
1944 /* Display progress update in log view. */
1945 show_log_view(view);
1946 update_panels();
1947 doupdate();
1950 return NULL;
1953 static const struct got_error *
1954 log_scroll_down(struct tog_view *view, int maxscroll)
1956 struct tog_log_view_state *s = &view->state.log;
1957 const struct got_error *err = NULL;
1958 struct commit_queue_entry *pentry;
1959 int nscrolled = 0, ncommits_needed;
1961 if (s->last_displayed_entry == NULL)
1962 return NULL;
1964 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1965 if (s->commits.ncommits < ncommits_needed &&
1966 !s->thread_args.log_complete) {
1968 * Ask the log thread for required amount of commits.
1970 s->thread_args.commits_needed += maxscroll;
1971 err = trigger_log_thread(view, 1);
1972 if (err)
1973 return err;
1976 do {
1977 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1978 if (pentry == NULL)
1979 break;
1981 s->last_displayed_entry = pentry;
1983 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1984 if (pentry == NULL)
1985 break;
1986 s->first_displayed_entry = pentry;
1987 } while (++nscrolled < maxscroll);
1989 return err;
1992 static const struct got_error *
1993 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1994 struct got_commit_object *commit, struct got_object_id *commit_id,
1995 struct tog_view *log_view, struct got_repository *repo)
1997 const struct got_error *err;
1998 struct got_object_qid *parent_id;
1999 struct tog_view *diff_view;
2001 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2002 if (diff_view == NULL)
2003 return got_error_from_errno("view_open");
2005 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2006 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2007 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2008 if (err == NULL)
2009 *new_view = diff_view;
2010 return err;
2013 static const struct got_error *
2014 tree_view_visit_subtree(struct tog_tree_view_state *s,
2015 struct got_tree_object *subtree)
2017 struct tog_parent_tree *parent;
2019 parent = calloc(1, sizeof(*parent));
2020 if (parent == NULL)
2021 return got_error_from_errno("calloc");
2023 parent->tree = s->tree;
2024 parent->first_displayed_entry = s->first_displayed_entry;
2025 parent->selected_entry = s->selected_entry;
2026 parent->selected = s->selected;
2027 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2028 s->tree = subtree;
2029 s->selected = 0;
2030 s->first_displayed_entry = NULL;
2031 return NULL;
2034 static const struct got_error *
2035 tree_view_walk_path(struct tog_tree_view_state *s,
2036 struct got_commit_object *commit, const char *path)
2038 const struct got_error *err = NULL;
2039 struct got_tree_object *tree = NULL;
2040 const char *p;
2041 char *slash, *subpath = NULL;
2043 /* Walk the path and open corresponding tree objects. */
2044 p = path;
2045 while (*p) {
2046 struct got_tree_entry *te;
2047 struct got_object_id *tree_id;
2048 char *te_name;
2050 while (p[0] == '/')
2051 p++;
2053 /* Ensure the correct subtree entry is selected. */
2054 slash = strchr(p, '/');
2055 if (slash == NULL)
2056 te_name = strdup(p);
2057 else
2058 te_name = strndup(p, slash - p);
2059 if (te_name == NULL) {
2060 err = got_error_from_errno("strndup");
2061 break;
2063 te = got_object_tree_find_entry(s->tree, te_name);
2064 if (te == NULL) {
2065 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2066 free(te_name);
2067 break;
2069 free(te_name);
2070 s->first_displayed_entry = s->selected_entry = te;
2072 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2073 break; /* jump to this file's entry */
2075 slash = strchr(p, '/');
2076 if (slash)
2077 subpath = strndup(path, slash - path);
2078 else
2079 subpath = strdup(path);
2080 if (subpath == NULL) {
2081 err = got_error_from_errno("strdup");
2082 break;
2085 err = got_object_id_by_path(&tree_id, s->repo, commit,
2086 subpath);
2087 if (err)
2088 break;
2090 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2091 free(tree_id);
2092 if (err)
2093 break;
2095 err = tree_view_visit_subtree(s, tree);
2096 if (err) {
2097 got_object_tree_close(tree);
2098 break;
2100 if (slash == NULL)
2101 break;
2102 free(subpath);
2103 subpath = NULL;
2104 p = slash;
2107 free(subpath);
2108 return err;
2111 static const struct got_error *
2112 browse_commit_tree(struct tog_view **new_view, int begin_x,
2113 struct commit_queue_entry *entry, const char *path,
2114 const char *head_ref_name, struct got_repository *repo)
2116 const struct got_error *err = NULL;
2117 struct tog_tree_view_state *s;
2118 struct tog_view *tree_view;
2120 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2121 if (tree_view == NULL)
2122 return got_error_from_errno("view_open");
2124 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2125 if (err)
2126 return err;
2127 s = &tree_view->state.tree;
2129 *new_view = tree_view;
2131 if (got_path_is_root_dir(path))
2132 return NULL;
2134 return tree_view_walk_path(s, entry->commit, path);
2137 static const struct got_error *
2138 block_signals_used_by_main_thread(void)
2140 sigset_t sigset;
2141 int errcode;
2143 if (sigemptyset(&sigset) == -1)
2144 return got_error_from_errno("sigemptyset");
2146 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2147 if (sigaddset(&sigset, SIGWINCH) == -1)
2148 return got_error_from_errno("sigaddset");
2149 if (sigaddset(&sigset, SIGCONT) == -1)
2150 return got_error_from_errno("sigaddset");
2151 if (sigaddset(&sigset, SIGINT) == -1)
2152 return got_error_from_errno("sigaddset");
2153 if (sigaddset(&sigset, SIGTERM) == -1)
2154 return got_error_from_errno("sigaddset");
2156 /* ncurses handles SIGTSTP */
2157 if (sigaddset(&sigset, SIGTSTP) == -1)
2158 return got_error_from_errno("sigaddset");
2160 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2161 if (errcode)
2162 return got_error_set_errno(errcode, "pthread_sigmask");
2164 return NULL;
2167 static void *
2168 log_thread(void *arg)
2170 const struct got_error *err = NULL;
2171 int errcode = 0;
2172 struct tog_log_thread_args *a = arg;
2173 int done = 0;
2175 err = block_signals_used_by_main_thread();
2176 if (err)
2177 return (void *)err;
2179 while (!done && !err && !tog_fatal_signal_received()) {
2180 err = queue_commits(a);
2181 if (err) {
2182 if (err->code != GOT_ERR_ITER_COMPLETED)
2183 return (void *)err;
2184 err = NULL;
2185 done = 1;
2186 } else if (a->commits_needed > 0 && !a->load_all)
2187 a->commits_needed--;
2189 errcode = pthread_mutex_lock(&tog_mutex);
2190 if (errcode) {
2191 err = got_error_set_errno(errcode,
2192 "pthread_mutex_lock");
2193 break;
2194 } else if (*a->quit)
2195 done = 1;
2196 else if (*a->first_displayed_entry == NULL) {
2197 *a->first_displayed_entry =
2198 TAILQ_FIRST(&a->commits->head);
2199 *a->selected_entry = *a->first_displayed_entry;
2202 errcode = pthread_cond_signal(&a->commit_loaded);
2203 if (errcode) {
2204 err = got_error_set_errno(errcode,
2205 "pthread_cond_signal");
2206 pthread_mutex_unlock(&tog_mutex);
2207 break;
2210 if (done)
2211 a->commits_needed = 0;
2212 else {
2213 if (a->commits_needed == 0 && !a->load_all) {
2214 errcode = pthread_cond_wait(&a->need_commits,
2215 &tog_mutex);
2216 if (errcode)
2217 err = got_error_set_errno(errcode,
2218 "pthread_cond_wait");
2219 if (*a->quit)
2220 done = 1;
2224 errcode = pthread_mutex_unlock(&tog_mutex);
2225 if (errcode && err == NULL)
2226 err = got_error_set_errno(errcode,
2227 "pthread_mutex_unlock");
2229 a->log_complete = 1;
2230 return (void *)err;
2233 static const struct got_error *
2234 stop_log_thread(struct tog_log_view_state *s)
2236 const struct got_error *err = NULL;
2237 int errcode;
2239 if (s->thread) {
2240 s->quit = 1;
2241 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2242 if (errcode)
2243 return got_error_set_errno(errcode,
2244 "pthread_cond_signal");
2245 errcode = pthread_mutex_unlock(&tog_mutex);
2246 if (errcode)
2247 return got_error_set_errno(errcode,
2248 "pthread_mutex_unlock");
2249 errcode = pthread_join(s->thread, (void **)&err);
2250 if (errcode)
2251 return got_error_set_errno(errcode, "pthread_join");
2252 errcode = pthread_mutex_lock(&tog_mutex);
2253 if (errcode)
2254 return got_error_set_errno(errcode,
2255 "pthread_mutex_lock");
2256 s->thread = NULL;
2259 if (s->thread_args.repo) {
2260 err = got_repo_close(s->thread_args.repo);
2261 s->thread_args.repo = NULL;
2264 if (s->thread_args.pack_fds) {
2265 const struct got_error *pack_err =
2266 got_repo_pack_fds_close(s->thread_args.pack_fds);
2267 if (err == NULL)
2268 err = pack_err;
2269 s->thread_args.pack_fds = NULL;
2272 if (s->thread_args.graph) {
2273 got_commit_graph_close(s->thread_args.graph);
2274 s->thread_args.graph = NULL;
2277 return err;
2280 static const struct got_error *
2281 close_log_view(struct tog_view *view)
2283 const struct got_error *err = NULL;
2284 struct tog_log_view_state *s = &view->state.log;
2285 int errcode;
2287 err = stop_log_thread(s);
2289 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2290 if (errcode && err == NULL)
2291 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2293 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2294 if (errcode && err == NULL)
2295 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2297 free_commits(&s->commits);
2298 free(s->in_repo_path);
2299 s->in_repo_path = NULL;
2300 free(s->start_id);
2301 s->start_id = NULL;
2302 free(s->head_ref_name);
2303 s->head_ref_name = NULL;
2304 return err;
2307 static const struct got_error *
2308 search_start_log_view(struct tog_view *view)
2310 struct tog_log_view_state *s = &view->state.log;
2312 s->matched_entry = NULL;
2313 s->search_entry = NULL;
2314 return NULL;
2317 static const struct got_error *
2318 search_next_log_view(struct tog_view *view)
2320 const struct got_error *err = NULL;
2321 struct tog_log_view_state *s = &view->state.log;
2322 struct commit_queue_entry *entry;
2324 /* Display progress update in log view. */
2325 show_log_view(view);
2326 update_panels();
2327 doupdate();
2329 if (s->search_entry) {
2330 int errcode, ch;
2331 errcode = pthread_mutex_unlock(&tog_mutex);
2332 if (errcode)
2333 return got_error_set_errno(errcode,
2334 "pthread_mutex_unlock");
2335 ch = wgetch(view->window);
2336 errcode = pthread_mutex_lock(&tog_mutex);
2337 if (errcode)
2338 return got_error_set_errno(errcode,
2339 "pthread_mutex_lock");
2340 if (ch == KEY_BACKSPACE) {
2341 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2342 return NULL;
2344 if (view->searching == TOG_SEARCH_FORWARD)
2345 entry = TAILQ_NEXT(s->search_entry, entry);
2346 else
2347 entry = TAILQ_PREV(s->search_entry,
2348 commit_queue_head, entry);
2349 } else if (s->matched_entry) {
2350 int matched_idx = s->matched_entry->idx;
2351 int selected_idx = s->selected_entry->idx;
2354 * If the user has moved the cursor after we hit a match,
2355 * the position from where we should continue searching
2356 * might have changed.
2358 if (view->searching == TOG_SEARCH_FORWARD) {
2359 if (matched_idx > selected_idx)
2360 entry = TAILQ_NEXT(s->selected_entry, entry);
2361 else
2362 entry = TAILQ_NEXT(s->matched_entry, entry);
2363 } else {
2364 if (matched_idx < selected_idx)
2365 entry = TAILQ_PREV(s->selected_entry,
2366 commit_queue_head, entry);
2367 else
2368 entry = TAILQ_PREV(s->matched_entry,
2369 commit_queue_head, entry);
2371 } else {
2372 entry = s->selected_entry;
2375 while (1) {
2376 int have_match = 0;
2378 if (entry == NULL) {
2379 if (s->thread_args.log_complete ||
2380 view->searching == TOG_SEARCH_BACKWARD) {
2381 view->search_next_done =
2382 (s->matched_entry == NULL ?
2383 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2384 s->search_entry = NULL;
2385 return NULL;
2388 * Poke the log thread for more commits and return,
2389 * allowing the main loop to make progress. Search
2390 * will resume at s->search_entry once we come back.
2392 s->thread_args.commits_needed++;
2393 return trigger_log_thread(view, 0);
2396 err = match_commit(&have_match, entry->id, entry->commit,
2397 &view->regex);
2398 if (err)
2399 break;
2400 if (have_match) {
2401 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2402 s->matched_entry = entry;
2403 break;
2406 s->search_entry = entry;
2407 if (view->searching == TOG_SEARCH_FORWARD)
2408 entry = TAILQ_NEXT(entry, entry);
2409 else
2410 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2413 if (s->matched_entry) {
2414 int cur = s->selected_entry->idx;
2415 while (cur < s->matched_entry->idx) {
2416 err = input_log_view(NULL, view, KEY_DOWN);
2417 if (err)
2418 return err;
2419 cur++;
2421 while (cur > s->matched_entry->idx) {
2422 err = input_log_view(NULL, view, KEY_UP);
2423 if (err)
2424 return err;
2425 cur--;
2429 s->search_entry = NULL;
2431 return NULL;
2434 static const struct got_error *
2435 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2436 struct got_repository *repo, const char *head_ref_name,
2437 const char *in_repo_path, int log_branches)
2439 const struct got_error *err = NULL;
2440 struct tog_log_view_state *s = &view->state.log;
2441 struct got_repository *thread_repo = NULL;
2442 struct got_commit_graph *thread_graph = NULL;
2443 int errcode;
2445 if (in_repo_path != s->in_repo_path) {
2446 free(s->in_repo_path);
2447 s->in_repo_path = strdup(in_repo_path);
2448 if (s->in_repo_path == NULL)
2449 return got_error_from_errno("strdup");
2452 /* The commit queue only contains commits being displayed. */
2453 TAILQ_INIT(&s->commits.head);
2454 s->commits.ncommits = 0;
2456 s->repo = repo;
2457 if (head_ref_name) {
2458 s->head_ref_name = strdup(head_ref_name);
2459 if (s->head_ref_name == NULL) {
2460 err = got_error_from_errno("strdup");
2461 goto done;
2464 s->start_id = got_object_id_dup(start_id);
2465 if (s->start_id == NULL) {
2466 err = got_error_from_errno("got_object_id_dup");
2467 goto done;
2469 s->log_branches = log_branches;
2471 STAILQ_INIT(&s->colors);
2472 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2473 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2474 get_color_value("TOG_COLOR_COMMIT"));
2475 if (err)
2476 goto done;
2477 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2478 get_color_value("TOG_COLOR_AUTHOR"));
2479 if (err) {
2480 free_colors(&s->colors);
2481 goto done;
2483 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2484 get_color_value("TOG_COLOR_DATE"));
2485 if (err) {
2486 free_colors(&s->colors);
2487 goto done;
2491 view->show = show_log_view;
2492 view->input = input_log_view;
2493 view->close = close_log_view;
2494 view->search_start = search_start_log_view;
2495 view->search_next = search_next_log_view;
2497 if (s->thread_args.pack_fds == NULL) {
2498 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2499 if (err)
2500 goto done;
2502 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2503 s->thread_args.pack_fds);
2504 if (err)
2505 goto done;
2506 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2507 !s->log_branches);
2508 if (err)
2509 goto done;
2510 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2511 s->repo, NULL, NULL);
2512 if (err)
2513 goto done;
2515 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2516 if (errcode) {
2517 err = got_error_set_errno(errcode, "pthread_cond_init");
2518 goto done;
2520 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2521 if (errcode) {
2522 err = got_error_set_errno(errcode, "pthread_cond_init");
2523 goto done;
2526 s->thread_args.commits_needed = view->nlines;
2527 s->thread_args.graph = thread_graph;
2528 s->thread_args.commits = &s->commits;
2529 s->thread_args.in_repo_path = s->in_repo_path;
2530 s->thread_args.start_id = s->start_id;
2531 s->thread_args.repo = thread_repo;
2532 s->thread_args.log_complete = 0;
2533 s->thread_args.quit = &s->quit;
2534 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2535 s->thread_args.selected_entry = &s->selected_entry;
2536 s->thread_args.searching = &view->searching;
2537 s->thread_args.search_next_done = &view->search_next_done;
2538 s->thread_args.regex = &view->regex;
2539 done:
2540 if (err)
2541 close_log_view(view);
2542 return err;
2545 static const struct got_error *
2546 show_log_view(struct tog_view *view)
2548 const struct got_error *err;
2549 struct tog_log_view_state *s = &view->state.log;
2551 if (s->thread == NULL) {
2552 int errcode = pthread_create(&s->thread, NULL, log_thread,
2553 &s->thread_args);
2554 if (errcode)
2555 return got_error_set_errno(errcode, "pthread_create");
2556 if (s->thread_args.commits_needed > 0) {
2557 err = trigger_log_thread(view, 1);
2558 if (err)
2559 return err;
2563 return draw_commits(view);
2566 static const struct got_error *
2567 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2569 const struct got_error *err = NULL;
2570 struct tog_log_view_state *s = &view->state.log;
2571 struct tog_view *diff_view = NULL, *tree_view = NULL;
2572 struct tog_view *ref_view = NULL;
2573 struct commit_queue_entry *entry;
2574 int begin_x = 0, n, nscroll = view->nlines - 1;
2576 if (s->thread_args.load_all) {
2577 if (ch == KEY_BACKSPACE)
2578 s->thread_args.load_all = 0;
2579 else if (s->thread_args.log_complete) {
2580 s->thread_args.load_all = 0;
2581 log_scroll_down(view, s->commits.ncommits);
2582 s->selected = MIN(view->nlines - 2,
2583 s->commits.ncommits - 1);
2584 select_commit(s);
2586 return NULL;
2589 switch (ch) {
2590 case 'q':
2591 s->quit = 1;
2592 break;
2593 case '0':
2594 view->x = 0;
2595 break;
2596 case '$':
2597 view->x = MAX(view->maxx - view->ncols / 2, 0);
2598 break;
2599 case KEY_RIGHT:
2600 case 'l':
2601 if (view->x + view->ncols / 2 < view->maxx)
2602 view->x += 2; /* move two columns right */
2603 break;
2604 case KEY_LEFT:
2605 case 'h':
2606 view->x -= MIN(view->x, 2); /* move two columns back */
2607 break;
2608 case 'k':
2609 case KEY_UP:
2610 case '<':
2611 case ',':
2612 case CTRL('p'):
2613 if (s->first_displayed_entry == NULL)
2614 break;
2615 if (s->selected > 0)
2616 s->selected--;
2617 else
2618 log_scroll_up(s, 1);
2619 select_commit(s);
2620 break;
2621 case 'g':
2622 case KEY_HOME:
2623 s->selected = 0;
2624 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2625 select_commit(s);
2626 break;
2627 case CTRL('u'):
2628 case 'u':
2629 nscroll /= 2;
2630 /* FALL THROUGH */
2631 case KEY_PPAGE:
2632 case CTRL('b'):
2633 case 'b':
2634 if (s->first_displayed_entry == NULL)
2635 break;
2636 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2637 s->selected = MAX(0, s->selected - nscroll - 1);
2638 else
2639 log_scroll_up(s, nscroll);
2640 select_commit(s);
2641 break;
2642 case 'j':
2643 case KEY_DOWN:
2644 case '>':
2645 case '.':
2646 case CTRL('n'):
2647 if (s->first_displayed_entry == NULL)
2648 break;
2649 if (s->selected < MIN(view->nlines - 2,
2650 s->commits.ncommits - 1))
2651 s->selected++;
2652 else {
2653 err = log_scroll_down(view, 1);
2654 if (err)
2655 break;
2657 select_commit(s);
2658 break;
2659 case 'G':
2660 case KEY_END: {
2661 /* We don't know yet how many commits, so we're forced to
2662 * traverse them all. */
2663 if (!s->thread_args.log_complete) {
2664 s->thread_args.load_all = 1;
2665 return trigger_log_thread(view, 0);
2668 s->selected = 0;
2669 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2670 for (n = 0; n < view->nlines - 1; n++) {
2671 if (entry == NULL)
2672 break;
2673 s->first_displayed_entry = entry;
2674 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2676 if (n > 0)
2677 s->selected = n - 1;
2678 select_commit(s);
2679 break;
2681 case CTRL('d'):
2682 case 'd':
2683 nscroll /= 2;
2684 /* FALL THROUGH */
2685 case KEY_NPAGE:
2686 case CTRL('f'):
2687 case 'f': {
2688 struct commit_queue_entry *first;
2689 first = s->first_displayed_entry;
2690 if (first == NULL)
2691 break;
2692 err = log_scroll_down(view, nscroll);
2693 if (err)
2694 break;
2695 if (first == s->first_displayed_entry &&
2696 s->selected < MIN(view->nlines - 2,
2697 s->commits.ncommits - 1)) {
2698 /* can't scroll further down */
2699 s->selected += MIN(s->last_displayed_entry->idx -
2700 s->selected_entry->idx, nscroll + 1);
2702 select_commit(s);
2703 break;
2705 case KEY_RESIZE:
2706 if (s->selected > view->nlines - 2)
2707 s->selected = view->nlines - 2;
2708 if (s->selected > s->commits.ncommits - 1)
2709 s->selected = s->commits.ncommits - 1;
2710 select_commit(s);
2711 if (s->commits.ncommits < view->nlines - 1 &&
2712 !s->thread_args.log_complete) {
2713 s->thread_args.commits_needed += (view->nlines - 1) -
2714 s->commits.ncommits;
2715 err = trigger_log_thread(view, 1);
2717 break;
2718 case KEY_ENTER:
2719 case ' ':
2720 case '\r':
2721 if (s->selected_entry == NULL)
2722 break;
2723 if (view_is_parent_view(view))
2724 begin_x = view_split_begin_x(view->begin_x);
2725 err = open_diff_view_for_commit(&diff_view, begin_x,
2726 s->selected_entry->commit, s->selected_entry->id,
2727 view, s->repo);
2728 if (err)
2729 break;
2730 view->focussed = 0;
2731 diff_view->focussed = 1;
2732 if (view_is_parent_view(view)) {
2733 err = view_close_child(view);
2734 if (err)
2735 return err;
2736 err = view_set_child(view, diff_view);
2737 if (err)
2738 return err;
2739 view->focus_child = 1;
2740 } else
2741 *new_view = diff_view;
2742 break;
2743 case 't':
2744 if (s->selected_entry == NULL)
2745 break;
2746 if (view_is_parent_view(view))
2747 begin_x = view_split_begin_x(view->begin_x);
2748 err = browse_commit_tree(&tree_view, begin_x,
2749 s->selected_entry, s->in_repo_path, s->head_ref_name,
2750 s->repo);
2751 if (err)
2752 break;
2753 view->focussed = 0;
2754 tree_view->focussed = 1;
2755 if (view_is_parent_view(view)) {
2756 err = view_close_child(view);
2757 if (err)
2758 return err;
2759 err = view_set_child(view, tree_view);
2760 if (err)
2761 return err;
2762 view->focus_child = 1;
2763 } else
2764 *new_view = tree_view;
2765 break;
2766 case KEY_BACKSPACE:
2767 case CTRL('l'):
2768 case 'B':
2769 if (ch == KEY_BACKSPACE &&
2770 got_path_is_root_dir(s->in_repo_path))
2771 break;
2772 err = stop_log_thread(s);
2773 if (err)
2774 return err;
2775 if (ch == KEY_BACKSPACE) {
2776 char *parent_path;
2777 err = got_path_dirname(&parent_path, s->in_repo_path);
2778 if (err)
2779 return err;
2780 free(s->in_repo_path);
2781 s->in_repo_path = parent_path;
2782 s->thread_args.in_repo_path = s->in_repo_path;
2783 } else if (ch == CTRL('l')) {
2784 struct got_object_id *start_id;
2785 err = got_repo_match_object_id(&start_id, NULL,
2786 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2787 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2788 if (err)
2789 return err;
2790 free(s->start_id);
2791 s->start_id = start_id;
2792 s->thread_args.start_id = s->start_id;
2793 } else /* 'B' */
2794 s->log_branches = !s->log_branches;
2796 if (s->thread_args.pack_fds == NULL) {
2797 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2798 if (err)
2799 return err;
2801 err = got_repo_open(&s->thread_args.repo,
2802 got_repo_get_path(s->repo), NULL,
2803 s->thread_args.pack_fds);
2804 if (err)
2805 return err;
2806 tog_free_refs();
2807 err = tog_load_refs(s->repo, 0);
2808 if (err)
2809 return err;
2810 err = got_commit_graph_open(&s->thread_args.graph,
2811 s->in_repo_path, !s->log_branches);
2812 if (err)
2813 return err;
2814 err = got_commit_graph_iter_start(s->thread_args.graph,
2815 s->start_id, s->repo, NULL, NULL);
2816 if (err)
2817 return err;
2818 free_commits(&s->commits);
2819 s->first_displayed_entry = NULL;
2820 s->last_displayed_entry = NULL;
2821 s->selected_entry = NULL;
2822 s->selected = 0;
2823 s->thread_args.log_complete = 0;
2824 s->quit = 0;
2825 s->thread_args.commits_needed = view->nlines;
2826 s->matched_entry = NULL;
2827 s->search_entry = NULL;
2828 break;
2829 case 'r':
2830 if (view_is_parent_view(view))
2831 begin_x = view_split_begin_x(view->begin_x);
2832 ref_view = view_open(view->nlines, view->ncols,
2833 view->begin_y, begin_x, TOG_VIEW_REF);
2834 if (ref_view == NULL)
2835 return got_error_from_errno("view_open");
2836 err = open_ref_view(ref_view, s->repo);
2837 if (err) {
2838 view_close(ref_view);
2839 return err;
2841 view->focussed = 0;
2842 ref_view->focussed = 1;
2843 if (view_is_parent_view(view)) {
2844 err = view_close_child(view);
2845 if (err)
2846 return err;
2847 err = view_set_child(view, ref_view);
2848 if (err)
2849 return err;
2850 view->focus_child = 1;
2851 } else
2852 *new_view = ref_view;
2853 break;
2854 default:
2855 break;
2858 return err;
2861 static const struct got_error *
2862 apply_unveil(const char *repo_path, const char *worktree_path)
2864 const struct got_error *error;
2866 #ifdef PROFILE
2867 if (unveil("gmon.out", "rwc") != 0)
2868 return got_error_from_errno2("unveil", "gmon.out");
2869 #endif
2870 if (repo_path && unveil(repo_path, "r") != 0)
2871 return got_error_from_errno2("unveil", repo_path);
2873 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2874 return got_error_from_errno2("unveil", worktree_path);
2876 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2877 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2879 error = got_privsep_unveil_exec_helpers();
2880 if (error != NULL)
2881 return error;
2883 if (unveil(NULL, NULL) != 0)
2884 return got_error_from_errno("unveil");
2886 return NULL;
2889 static void
2890 init_curses(void)
2893 * Override default signal handlers before starting ncurses.
2894 * This should prevent ncurses from installing its own
2895 * broken cleanup() signal handler.
2897 signal(SIGWINCH, tog_sigwinch);
2898 signal(SIGPIPE, tog_sigpipe);
2899 signal(SIGCONT, tog_sigcont);
2900 signal(SIGINT, tog_sigint);
2901 signal(SIGTERM, tog_sigterm);
2903 initscr();
2904 cbreak();
2905 halfdelay(1); /* Do fast refresh while initial view is loading. */
2906 noecho();
2907 nonl();
2908 intrflush(stdscr, FALSE);
2909 keypad(stdscr, TRUE);
2910 curs_set(0);
2911 if (getenv("TOG_COLORS") != NULL) {
2912 start_color();
2913 use_default_colors();
2917 static const struct got_error *
2918 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2919 struct got_repository *repo, struct got_worktree *worktree)
2921 const struct got_error *err = NULL;
2923 if (argc == 0) {
2924 *in_repo_path = strdup("/");
2925 if (*in_repo_path == NULL)
2926 return got_error_from_errno("strdup");
2927 return NULL;
2930 if (worktree) {
2931 const char *prefix = got_worktree_get_path_prefix(worktree);
2932 char *p;
2934 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2935 if (err)
2936 return err;
2937 if (asprintf(in_repo_path, "%s%s%s", prefix,
2938 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2939 p) == -1) {
2940 err = got_error_from_errno("asprintf");
2941 *in_repo_path = NULL;
2943 free(p);
2944 } else
2945 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2947 return err;
2950 static const struct got_error *
2951 cmd_log(int argc, char *argv[])
2953 const struct got_error *error;
2954 struct got_repository *repo = NULL;
2955 struct got_worktree *worktree = NULL;
2956 struct got_object_id *start_id = NULL;
2957 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2958 char *start_commit = NULL, *label = NULL;
2959 struct got_reference *ref = NULL;
2960 const char *head_ref_name = NULL;
2961 int ch, log_branches = 0;
2962 struct tog_view *view;
2963 int *pack_fds = NULL;
2965 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2966 switch (ch) {
2967 case 'b':
2968 log_branches = 1;
2969 break;
2970 case 'c':
2971 start_commit = optarg;
2972 break;
2973 case 'r':
2974 repo_path = realpath(optarg, NULL);
2975 if (repo_path == NULL)
2976 return got_error_from_errno2("realpath",
2977 optarg);
2978 break;
2979 default:
2980 usage_log();
2981 /* NOTREACHED */
2985 argc -= optind;
2986 argv += optind;
2988 if (argc > 1)
2989 usage_log();
2991 error = got_repo_pack_fds_open(&pack_fds);
2992 if (error != NULL)
2993 goto done;
2995 if (repo_path == NULL) {
2996 cwd = getcwd(NULL, 0);
2997 if (cwd == NULL)
2998 return got_error_from_errno("getcwd");
2999 error = got_worktree_open(&worktree, cwd);
3000 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3001 goto done;
3002 if (worktree)
3003 repo_path =
3004 strdup(got_worktree_get_repo_path(worktree));
3005 else
3006 repo_path = strdup(cwd);
3007 if (repo_path == NULL) {
3008 error = got_error_from_errno("strdup");
3009 goto done;
3013 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3014 if (error != NULL)
3015 goto done;
3017 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3018 repo, worktree);
3019 if (error)
3020 goto done;
3022 init_curses();
3024 error = apply_unveil(got_repo_get_path(repo),
3025 worktree ? got_worktree_get_root_path(worktree) : NULL);
3026 if (error)
3027 goto done;
3029 /* already loaded by tog_log_with_path()? */
3030 if (TAILQ_EMPTY(&tog_refs)) {
3031 error = tog_load_refs(repo, 0);
3032 if (error)
3033 goto done;
3036 if (start_commit == NULL) {
3037 error = got_repo_match_object_id(&start_id, &label,
3038 worktree ? got_worktree_get_head_ref_name(worktree) :
3039 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3040 if (error)
3041 goto done;
3042 head_ref_name = label;
3043 } else {
3044 error = got_ref_open(&ref, repo, start_commit, 0);
3045 if (error == NULL)
3046 head_ref_name = got_ref_get_name(ref);
3047 else if (error->code != GOT_ERR_NOT_REF)
3048 goto done;
3049 error = got_repo_match_object_id(&start_id, NULL,
3050 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3051 if (error)
3052 goto done;
3055 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3056 if (view == NULL) {
3057 error = got_error_from_errno("view_open");
3058 goto done;
3060 error = open_log_view(view, start_id, repo, head_ref_name,
3061 in_repo_path, log_branches);
3062 if (error)
3063 goto done;
3064 if (worktree) {
3065 /* Release work tree lock. */
3066 got_worktree_close(worktree);
3067 worktree = NULL;
3069 error = view_loop(view);
3070 done:
3071 free(in_repo_path);
3072 free(repo_path);
3073 free(cwd);
3074 free(start_id);
3075 free(label);
3076 if (ref)
3077 got_ref_close(ref);
3078 if (repo) {
3079 const struct got_error *close_err = got_repo_close(repo);
3080 if (error == NULL)
3081 error = close_err;
3083 if (worktree)
3084 got_worktree_close(worktree);
3085 if (pack_fds) {
3086 const struct got_error *pack_err =
3087 got_repo_pack_fds_close(pack_fds);
3088 if (error == NULL)
3089 error = pack_err;
3091 tog_free_refs();
3092 return error;
3095 __dead static void
3096 usage_diff(void)
3098 endwin();
3099 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3100 "[-w] object1 object2\n", getprogname());
3101 exit(1);
3104 static int
3105 match_line(const char *line, regex_t *regex, size_t nmatch,
3106 regmatch_t *regmatch)
3108 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3111 struct tog_color *
3112 match_color(struct tog_colors *colors, const char *line)
3114 struct tog_color *tc = NULL;
3116 STAILQ_FOREACH(tc, colors, entry) {
3117 if (match_line(line, &tc->regex, 0, NULL))
3118 return tc;
3121 return NULL;
3124 static const struct got_error *
3125 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3126 WINDOW *window, int skipcol, regmatch_t *regmatch)
3128 const struct got_error *err = NULL;
3129 char *exstr = NULL;
3130 wchar_t *wline = NULL;
3131 int rme, rms, n, width, scrollx;
3132 int width0 = 0, width1 = 0, width2 = 0;
3133 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3135 *wtotal = 0;
3137 rms = regmatch->rm_so;
3138 rme = regmatch->rm_eo;
3140 err = expand_tab(&exstr, line);
3141 if (err)
3142 return err;
3144 /* Split the line into 3 segments, according to match offsets. */
3145 seg0 = strndup(exstr, rms);
3146 if (seg0 == NULL) {
3147 err = got_error_from_errno("strndup");
3148 goto done;
3150 seg1 = strndup(exstr + rms, rme - rms);
3151 if (seg1 == NULL) {
3152 err = got_error_from_errno("strndup");
3153 goto done;
3155 seg2 = strdup(exstr + rme);
3156 if (seg2 == NULL) {
3157 err = got_error_from_errno("strndup");
3158 goto done;
3161 /* draw up to matched token if we haven't scrolled past it */
3162 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3163 col_tab_align, 1);
3164 if (err)
3165 goto done;
3166 n = MAX(width0 - skipcol, 0);
3167 if (n) {
3168 free(wline);
3169 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3170 wlimit, col_tab_align, 1);
3171 if (err)
3172 goto done;
3173 waddwstr(window, &wline[scrollx]);
3174 wlimit -= width;
3175 *wtotal += width;
3178 if (wlimit > 0) {
3179 int i = 0, w = 0;
3180 size_t wlen;
3182 free(wline);
3183 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3184 col_tab_align, 1);
3185 if (err)
3186 goto done;
3187 wlen = wcslen(wline);
3188 while (i < wlen) {
3189 width = wcwidth(wline[i]);
3190 if (width == -1) {
3191 /* should not happen, tabs are expanded */
3192 err = got_error(GOT_ERR_RANGE);
3193 goto done;
3195 if (width0 + w + width > skipcol)
3196 break;
3197 w += width;
3198 i++;
3200 /* draw (visible part of) matched token (if scrolled into it) */
3201 if (width1 - w > 0) {
3202 wattron(window, A_STANDOUT);
3203 waddwstr(window, &wline[i]);
3204 wattroff(window, A_STANDOUT);
3205 wlimit -= (width1 - w);
3206 *wtotal += (width1 - w);
3210 if (wlimit > 0) { /* draw rest of line */
3211 free(wline);
3212 if (skipcol > width0 + width1) {
3213 err = format_line(&wline, &width2, &scrollx, seg2,
3214 skipcol - (width0 + width1), wlimit,
3215 col_tab_align, 1);
3216 if (err)
3217 goto done;
3218 waddwstr(window, &wline[scrollx]);
3219 } else {
3220 err = format_line(&wline, &width2, NULL, seg2, 0,
3221 wlimit, col_tab_align, 1);
3222 if (err)
3223 goto done;
3224 waddwstr(window, wline);
3226 *wtotal += width2;
3228 done:
3229 free(wline);
3230 free(exstr);
3231 free(seg0);
3232 free(seg1);
3233 free(seg2);
3234 return err;
3237 static const struct got_error *
3238 draw_file(struct tog_view *view, const char *header)
3240 struct tog_diff_view_state *s = &view->state.diff;
3241 regmatch_t *regmatch = &view->regmatch;
3242 const struct got_error *err;
3243 int nprinted = 0;
3244 char *line;
3245 size_t linesize = 0;
3246 ssize_t linelen;
3247 struct tog_color *tc;
3248 wchar_t *wline;
3249 int width;
3250 int max_lines = view->nlines;
3251 int nlines = s->nlines;
3252 off_t line_offset;
3254 line_offset = s->line_offsets[s->first_displayed_line - 1];
3255 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3256 return got_error_from_errno("fseek");
3258 werase(view->window);
3260 if (header) {
3261 if (asprintf(&line, "[%d/%d] %s",
3262 s->first_displayed_line - 1 + s->selected_line, nlines,
3263 header) == -1)
3264 return got_error_from_errno("asprintf");
3265 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3266 0, 0);
3267 free(line);
3268 if (err)
3269 return err;
3271 if (view_needs_focus_indication(view))
3272 wstandout(view->window);
3273 waddwstr(view->window, wline);
3274 free(wline);
3275 wline = NULL;
3276 if (view_needs_focus_indication(view))
3277 wstandend(view->window);
3278 if (width <= view->ncols - 1)
3279 waddch(view->window, '\n');
3281 if (max_lines <= 1)
3282 return NULL;
3283 max_lines--;
3286 s->eof = 0;
3287 view->maxx = 0;
3288 line = NULL;
3289 while (max_lines > 0 && nprinted < max_lines) {
3290 linelen = getline(&line, &linesize, s->f);
3291 if (linelen == -1) {
3292 if (feof(s->f)) {
3293 s->eof = 1;
3294 break;
3296 free(line);
3297 return got_ferror(s->f, GOT_ERR_IO);
3300 /* Set view->maxx based on full line length. */
3301 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3302 view->x ? 1 : 0);
3303 if (err) {
3304 free(line);
3305 return err;
3307 view->maxx = MAX(view->maxx, width);
3308 free(wline);
3309 wline = NULL;
3311 tc = match_color(&s->colors, line);
3312 if (tc)
3313 wattr_on(view->window,
3314 COLOR_PAIR(tc->colorpair), NULL);
3315 if (s->first_displayed_line + nprinted == s->matched_line &&
3316 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3317 err = add_matched_line(&width, line, view->ncols, 0,
3318 view->window, view->x, regmatch);
3319 if (err) {
3320 free(line);
3321 return err;
3323 } else {
3324 int skip;
3325 err = format_line(&wline, &width, &skip, line,
3326 view->x, view->ncols, 0, view->x ? 1 : 0);
3327 if (err) {
3328 free(line);
3329 return err;
3331 waddwstr(view->window, &wline[skip]);
3332 free(wline);
3333 wline = NULL;
3335 if (tc)
3336 wattr_off(view->window,
3337 COLOR_PAIR(tc->colorpair), NULL);
3338 if (width <= view->ncols - 1)
3339 waddch(view->window, '\n');
3340 nprinted++;
3342 free(line);
3343 if (nprinted >= 1)
3344 s->last_displayed_line = s->first_displayed_line +
3345 (nprinted - 1);
3346 else
3347 s->last_displayed_line = s->first_displayed_line;
3349 view_vborder(view);
3351 if (s->eof) {
3352 while (nprinted < view->nlines) {
3353 waddch(view->window, '\n');
3354 nprinted++;
3357 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3358 view->ncols, 0, 0);
3359 if (err) {
3360 return err;
3363 wstandout(view->window);
3364 waddwstr(view->window, wline);
3365 free(wline);
3366 wline = NULL;
3367 wstandend(view->window);
3370 return NULL;
3373 static char *
3374 get_datestr(time_t *time, char *datebuf)
3376 struct tm mytm, *tm;
3377 char *p, *s;
3379 tm = gmtime_r(time, &mytm);
3380 if (tm == NULL)
3381 return NULL;
3382 s = asctime_r(tm, datebuf);
3383 if (s == NULL)
3384 return NULL;
3385 p = strchr(s, '\n');
3386 if (p)
3387 *p = '\0';
3388 return s;
3391 static const struct got_error *
3392 get_changed_paths(struct got_pathlist_head *paths,
3393 struct got_commit_object *commit, struct got_repository *repo)
3395 const struct got_error *err = NULL;
3396 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3397 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3398 struct got_object_qid *qid;
3400 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3401 if (qid != NULL) {
3402 struct got_commit_object *pcommit;
3403 err = got_object_open_as_commit(&pcommit, repo,
3404 &qid->id);
3405 if (err)
3406 return err;
3408 tree_id1 = got_object_id_dup(
3409 got_object_commit_get_tree_id(pcommit));
3410 if (tree_id1 == NULL) {
3411 got_object_commit_close(pcommit);
3412 return got_error_from_errno("got_object_id_dup");
3414 got_object_commit_close(pcommit);
3418 if (tree_id1) {
3419 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3420 if (err)
3421 goto done;
3424 tree_id2 = got_object_commit_get_tree_id(commit);
3425 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3426 if (err)
3427 goto done;
3429 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3430 got_diff_tree_collect_changed_paths, paths, 0);
3431 done:
3432 if (tree1)
3433 got_object_tree_close(tree1);
3434 if (tree2)
3435 got_object_tree_close(tree2);
3436 free(tree_id1);
3437 return err;
3440 static const struct got_error *
3441 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3443 off_t *p;
3445 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3446 if (p == NULL)
3447 return got_error_from_errno("reallocarray");
3448 *line_offsets = p;
3449 (*line_offsets)[*nlines] = off;
3450 (*nlines)++;
3451 return NULL;
3454 static const struct got_error *
3455 write_commit_info(off_t **line_offsets, size_t *nlines,
3456 struct got_object_id *commit_id, struct got_reflist_head *refs,
3457 struct got_repository *repo, FILE *outfile)
3459 const struct got_error *err = NULL;
3460 char datebuf[26], *datestr;
3461 struct got_commit_object *commit;
3462 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3463 time_t committer_time;
3464 const char *author, *committer;
3465 char *refs_str = NULL;
3466 struct got_pathlist_head changed_paths;
3467 struct got_pathlist_entry *pe;
3468 off_t outoff = 0;
3469 int n;
3471 TAILQ_INIT(&changed_paths);
3473 if (refs) {
3474 err = build_refs_str(&refs_str, refs, commit_id, repo);
3475 if (err)
3476 return err;
3479 err = got_object_open_as_commit(&commit, repo, commit_id);
3480 if (err)
3481 return err;
3483 err = got_object_id_str(&id_str, commit_id);
3484 if (err) {
3485 err = got_error_from_errno("got_object_id_str");
3486 goto done;
3489 err = add_line_offset(line_offsets, nlines, 0);
3490 if (err)
3491 goto done;
3493 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3494 refs_str ? refs_str : "", refs_str ? ")" : "");
3495 if (n < 0) {
3496 err = got_error_from_errno("fprintf");
3497 goto done;
3499 outoff += n;
3500 err = add_line_offset(line_offsets, nlines, outoff);
3501 if (err)
3502 goto done;
3504 n = fprintf(outfile, "from: %s\n",
3505 got_object_commit_get_author(commit));
3506 if (n < 0) {
3507 err = got_error_from_errno("fprintf");
3508 goto done;
3510 outoff += n;
3511 err = add_line_offset(line_offsets, nlines, outoff);
3512 if (err)
3513 goto done;
3515 committer_time = got_object_commit_get_committer_time(commit);
3516 datestr = get_datestr(&committer_time, datebuf);
3517 if (datestr) {
3518 n = fprintf(outfile, "date: %s UTC\n", datestr);
3519 if (n < 0) {
3520 err = got_error_from_errno("fprintf");
3521 goto done;
3523 outoff += n;
3524 err = add_line_offset(line_offsets, nlines, outoff);
3525 if (err)
3526 goto done;
3528 author = got_object_commit_get_author(commit);
3529 committer = got_object_commit_get_committer(commit);
3530 if (strcmp(author, committer) != 0) {
3531 n = fprintf(outfile, "via: %s\n", committer);
3532 if (n < 0) {
3533 err = got_error_from_errno("fprintf");
3534 goto done;
3536 outoff += n;
3537 err = add_line_offset(line_offsets, nlines, outoff);
3538 if (err)
3539 goto done;
3541 if (got_object_commit_get_nparents(commit) > 1) {
3542 const struct got_object_id_queue *parent_ids;
3543 struct got_object_qid *qid;
3544 int pn = 1;
3545 parent_ids = got_object_commit_get_parent_ids(commit);
3546 STAILQ_FOREACH(qid, parent_ids, entry) {
3547 err = got_object_id_str(&id_str, &qid->id);
3548 if (err)
3549 goto done;
3550 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3551 if (n < 0) {
3552 err = got_error_from_errno("fprintf");
3553 goto done;
3555 outoff += n;
3556 err = add_line_offset(line_offsets, nlines, outoff);
3557 if (err)
3558 goto done;
3559 free(id_str);
3560 id_str = NULL;
3564 err = got_object_commit_get_logmsg(&logmsg, commit);
3565 if (err)
3566 goto done;
3567 s = logmsg;
3568 while ((line = strsep(&s, "\n")) != NULL) {
3569 n = fprintf(outfile, "%s\n", line);
3570 if (n < 0) {
3571 err = got_error_from_errno("fprintf");
3572 goto done;
3574 outoff += n;
3575 err = add_line_offset(line_offsets, nlines, outoff);
3576 if (err)
3577 goto done;
3580 err = get_changed_paths(&changed_paths, commit, repo);
3581 if (err)
3582 goto done;
3583 TAILQ_FOREACH(pe, &changed_paths, entry) {
3584 struct got_diff_changed_path *cp = pe->data;
3585 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3586 if (n < 0) {
3587 err = got_error_from_errno("fprintf");
3588 goto done;
3590 outoff += n;
3591 err = add_line_offset(line_offsets, nlines, outoff);
3592 if (err)
3593 goto done;
3594 free((char *)pe->path);
3595 free(pe->data);
3598 fputc('\n', outfile);
3599 outoff++;
3600 err = add_line_offset(line_offsets, nlines, outoff);
3601 done:
3602 got_pathlist_free(&changed_paths);
3603 free(id_str);
3604 free(logmsg);
3605 free(refs_str);
3606 got_object_commit_close(commit);
3607 if (err) {
3608 free(*line_offsets);
3609 *line_offsets = NULL;
3610 *nlines = 0;
3612 return err;
3615 static const struct got_error *
3616 create_diff(struct tog_diff_view_state *s)
3618 const struct got_error *err = NULL;
3619 FILE *f = NULL;
3620 int obj_type;
3622 free(s->line_offsets);
3623 s->line_offsets = malloc(sizeof(off_t));
3624 if (s->line_offsets == NULL)
3625 return got_error_from_errno("malloc");
3626 s->nlines = 0;
3628 f = got_opentemp();
3629 if (f == NULL) {
3630 err = got_error_from_errno("got_opentemp");
3631 goto done;
3633 if (s->f && fclose(s->f) == EOF) {
3634 err = got_error_from_errno("fclose");
3635 goto done;
3637 s->f = f;
3639 if (s->id1)
3640 err = got_object_get_type(&obj_type, s->repo, s->id1);
3641 else
3642 err = got_object_get_type(&obj_type, s->repo, s->id2);
3643 if (err)
3644 goto done;
3646 switch (obj_type) {
3647 case GOT_OBJ_TYPE_BLOB:
3648 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3649 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3650 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3651 s->repo, s->f);
3652 break;
3653 case GOT_OBJ_TYPE_TREE:
3654 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3655 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3656 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3657 break;
3658 case GOT_OBJ_TYPE_COMMIT: {
3659 const struct got_object_id_queue *parent_ids;
3660 struct got_object_qid *pid;
3661 struct got_commit_object *commit2;
3662 struct got_reflist_head *refs;
3664 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3665 if (err)
3666 goto done;
3667 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3668 /* Show commit info if we're diffing to a parent/root commit. */
3669 if (s->id1 == NULL) {
3670 err = write_commit_info(&s->line_offsets, &s->nlines,
3671 s->id2, refs, s->repo, s->f);
3672 if (err)
3673 goto done;
3674 } else {
3675 parent_ids = got_object_commit_get_parent_ids(commit2);
3676 STAILQ_FOREACH(pid, parent_ids, entry) {
3677 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3678 err = write_commit_info(
3679 &s->line_offsets, &s->nlines,
3680 s->id2, refs, s->repo, s->f);
3681 if (err)
3682 goto done;
3683 break;
3687 got_object_commit_close(commit2);
3689 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3690 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3691 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3692 break;
3694 default:
3695 err = got_error(GOT_ERR_OBJ_TYPE);
3696 break;
3698 if (err)
3699 goto done;
3700 done:
3701 if (s->f && fflush(s->f) != 0 && err == NULL)
3702 err = got_error_from_errno("fflush");
3703 return err;
3706 static void
3707 diff_view_indicate_progress(struct tog_view *view)
3709 mvwaddstr(view->window, 0, 0, "diffing...");
3710 update_panels();
3711 doupdate();
3714 static const struct got_error *
3715 search_start_diff_view(struct tog_view *view)
3717 struct tog_diff_view_state *s = &view->state.diff;
3719 s->matched_line = 0;
3720 return NULL;
3723 static const struct got_error *
3724 search_next_diff_view(struct tog_view *view)
3726 struct tog_diff_view_state *s = &view->state.diff;
3727 const struct got_error *err = NULL;
3728 int lineno;
3729 char *line = NULL;
3730 size_t linesize = 0;
3731 ssize_t linelen;
3733 if (!view->searching) {
3734 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3735 return NULL;
3738 if (s->matched_line) {
3739 if (view->searching == TOG_SEARCH_FORWARD)
3740 lineno = s->matched_line + 1;
3741 else
3742 lineno = s->matched_line - 1;
3743 } else
3744 lineno = s->first_displayed_line;
3746 while (1) {
3747 off_t offset;
3749 if (lineno <= 0 || lineno > s->nlines) {
3750 if (s->matched_line == 0) {
3751 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3752 break;
3755 if (view->searching == TOG_SEARCH_FORWARD)
3756 lineno = 1;
3757 else
3758 lineno = s->nlines;
3761 offset = s->line_offsets[lineno - 1];
3762 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3763 free(line);
3764 return got_error_from_errno("fseeko");
3766 linelen = getline(&line, &linesize, s->f);
3767 if (linelen != -1) {
3768 char *exstr;
3769 err = expand_tab(&exstr, line);
3770 if (err)
3771 break;
3772 if (match_line(exstr, &view->regex, 1,
3773 &view->regmatch)) {
3774 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3775 s->matched_line = lineno;
3776 free(exstr);
3777 break;
3779 free(exstr);
3781 if (view->searching == TOG_SEARCH_FORWARD)
3782 lineno++;
3783 else
3784 lineno--;
3786 free(line);
3788 if (s->matched_line) {
3789 s->first_displayed_line = s->matched_line;
3790 s->selected_line = 1;
3793 return err;
3796 static const struct got_error *
3797 close_diff_view(struct tog_view *view)
3799 const struct got_error *err = NULL;
3800 struct tog_diff_view_state *s = &view->state.diff;
3802 free(s->id1);
3803 s->id1 = NULL;
3804 free(s->id2);
3805 s->id2 = NULL;
3806 if (s->f && fclose(s->f) == EOF)
3807 err = got_error_from_errno("fclose");
3808 s->f = NULL;
3809 if (s->f1 && fclose(s->f1) == EOF)
3810 err = got_error_from_errno("fclose");
3811 s->f1 = NULL;
3812 if (s->f2 && fclose(s->f2) == EOF)
3813 err = got_error_from_errno("fclose");
3814 s->f2 = NULL;
3815 free_colors(&s->colors);
3816 free(s->line_offsets);
3817 s->line_offsets = NULL;
3818 s->nlines = 0;
3819 return err;
3822 static const struct got_error *
3823 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3824 struct got_object_id *id2, const char *label1, const char *label2,
3825 int diff_context, int ignore_whitespace, int force_text_diff,
3826 struct tog_view *log_view, struct got_repository *repo)
3828 const struct got_error *err;
3829 struct tog_diff_view_state *s = &view->state.diff;
3831 memset(s, 0, sizeof(*s));
3833 if (id1 != NULL && id2 != NULL) {
3834 int type1, type2;
3835 err = got_object_get_type(&type1, repo, id1);
3836 if (err)
3837 return err;
3838 err = got_object_get_type(&type2, repo, id2);
3839 if (err)
3840 return err;
3842 if (type1 != type2)
3843 return got_error(GOT_ERR_OBJ_TYPE);
3845 s->first_displayed_line = 1;
3846 s->last_displayed_line = view->nlines;
3847 s->selected_line = 1;
3848 s->repo = repo;
3849 s->id1 = id1;
3850 s->id2 = id2;
3851 s->label1 = label1;
3852 s->label2 = label2;
3854 if (id1) {
3855 s->id1 = got_object_id_dup(id1);
3856 if (s->id1 == NULL)
3857 return got_error_from_errno("got_object_id_dup");
3858 } else
3859 s->id1 = NULL;
3861 s->id2 = got_object_id_dup(id2);
3862 if (s->id2 == NULL) {
3863 err = got_error_from_errno("got_object_id_dup");
3864 goto done;
3867 s->f1 = got_opentemp();
3868 if (s->f1 == NULL) {
3869 err = got_error_from_errno("got_opentemp");
3870 goto done;
3873 s->f2 = got_opentemp();
3874 if (s->f2 == NULL) {
3875 err = got_error_from_errno("got_opentemp");
3876 goto done;
3879 s->first_displayed_line = 1;
3880 s->last_displayed_line = view->nlines;
3881 s->diff_context = diff_context;
3882 s->ignore_whitespace = ignore_whitespace;
3883 s->force_text_diff = force_text_diff;
3884 s->log_view = log_view;
3885 s->repo = repo;
3887 STAILQ_INIT(&s->colors);
3888 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3889 err = add_color(&s->colors,
3890 "^-", TOG_COLOR_DIFF_MINUS,
3891 get_color_value("TOG_COLOR_DIFF_MINUS"));
3892 if (err)
3893 goto done;
3894 err = add_color(&s->colors, "^\\+",
3895 TOG_COLOR_DIFF_PLUS,
3896 get_color_value("TOG_COLOR_DIFF_PLUS"));
3897 if (err)
3898 goto done;
3899 err = add_color(&s->colors,
3900 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3901 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3902 if (err)
3903 goto done;
3905 err = add_color(&s->colors,
3906 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3907 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3908 get_color_value("TOG_COLOR_DIFF_META"));
3909 if (err)
3910 goto done;
3912 err = add_color(&s->colors,
3913 "^(from|via): ", TOG_COLOR_AUTHOR,
3914 get_color_value("TOG_COLOR_AUTHOR"));
3915 if (err)
3916 goto done;
3918 err = add_color(&s->colors,
3919 "^date: ", TOG_COLOR_DATE,
3920 get_color_value("TOG_COLOR_DATE"));
3921 if (err)
3922 goto done;
3925 if (log_view && view_is_splitscreen(view))
3926 show_log_view(log_view); /* draw vborder */
3927 diff_view_indicate_progress(view);
3929 err = create_diff(s);
3931 view->show = show_diff_view;
3932 view->input = input_diff_view;
3933 view->close = close_diff_view;
3934 view->search_start = search_start_diff_view;
3935 view->search_next = search_next_diff_view;
3936 done:
3937 if (err)
3938 close_diff_view(view);
3939 return err;
3942 static const struct got_error *
3943 show_diff_view(struct tog_view *view)
3945 const struct got_error *err;
3946 struct tog_diff_view_state *s = &view->state.diff;
3947 char *id_str1 = NULL, *id_str2, *header;
3948 const char *label1, *label2;
3950 if (s->id1) {
3951 err = got_object_id_str(&id_str1, s->id1);
3952 if (err)
3953 return err;
3954 label1 = s->label1 ? : id_str1;
3955 } else
3956 label1 = "/dev/null";
3958 err = got_object_id_str(&id_str2, s->id2);
3959 if (err)
3960 return err;
3961 label2 = s->label2 ? : id_str2;
3963 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3964 err = got_error_from_errno("asprintf");
3965 free(id_str1);
3966 free(id_str2);
3967 return err;
3969 free(id_str1);
3970 free(id_str2);
3972 err = draw_file(view, header);
3973 free(header);
3974 return err;
3977 static const struct got_error *
3978 set_selected_commit(struct tog_diff_view_state *s,
3979 struct commit_queue_entry *entry)
3981 const struct got_error *err;
3982 const struct got_object_id_queue *parent_ids;
3983 struct got_commit_object *selected_commit;
3984 struct got_object_qid *pid;
3986 free(s->id2);
3987 s->id2 = got_object_id_dup(entry->id);
3988 if (s->id2 == NULL)
3989 return got_error_from_errno("got_object_id_dup");
3991 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3992 if (err)
3993 return err;
3994 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3995 free(s->id1);
3996 pid = STAILQ_FIRST(parent_ids);
3997 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3998 got_object_commit_close(selected_commit);
3999 return NULL;
4002 static const struct got_error *
4003 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4005 const struct got_error *err = NULL;
4006 struct tog_diff_view_state *s = &view->state.diff;
4007 struct tog_log_view_state *ls;
4008 struct commit_queue_entry *old_selected_entry;
4009 char *line = NULL;
4010 size_t linesize = 0;
4011 ssize_t linelen;
4012 int i, nscroll = view->nlines - 1;
4014 switch (ch) {
4015 case '0':
4016 view->x = 0;
4017 break;
4018 case '$':
4019 view->x = MAX(view->maxx - view->ncols / 3, 0);
4020 break;
4021 case KEY_RIGHT:
4022 case 'l':
4023 if (view->x + view->ncols / 3 < view->maxx)
4024 view->x += 2; /* move two columns right */
4025 break;
4026 case KEY_LEFT:
4027 case 'h':
4028 view->x -= MIN(view->x, 2); /* move two columns back */
4029 break;
4030 case 'a':
4031 case 'w':
4032 if (ch == 'a')
4033 s->force_text_diff = !s->force_text_diff;
4034 if (ch == 'w')
4035 s->ignore_whitespace = !s->ignore_whitespace;
4036 wclear(view->window);
4037 s->first_displayed_line = 1;
4038 s->last_displayed_line = view->nlines;
4039 s->matched_line = 0;
4040 diff_view_indicate_progress(view);
4041 err = create_diff(s);
4042 break;
4043 case 'g':
4044 case KEY_HOME:
4045 s->first_displayed_line = 1;
4046 break;
4047 case 'G':
4048 case KEY_END:
4049 if (s->eof)
4050 break;
4052 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4053 s->eof = 1;
4054 break;
4055 case 'k':
4056 case KEY_UP:
4057 case CTRL('p'):
4058 if (s->first_displayed_line > 1)
4059 s->first_displayed_line--;
4060 break;
4061 case CTRL('u'):
4062 case 'u':
4063 nscroll /= 2;
4064 /* FALL THROUGH */
4065 case KEY_PPAGE:
4066 case CTRL('b'):
4067 case 'b':
4068 if (s->first_displayed_line == 1)
4069 break;
4070 i = 0;
4071 while (i++ < nscroll && s->first_displayed_line > 1)
4072 s->first_displayed_line--;
4073 break;
4074 case 'j':
4075 case KEY_DOWN:
4076 case CTRL('n'):
4077 if (!s->eof)
4078 s->first_displayed_line++;
4079 break;
4080 case CTRL('d'):
4081 case 'd':
4082 nscroll /= 2;
4083 /* FALL THROUGH */
4084 case KEY_NPAGE:
4085 case CTRL('f'):
4086 case 'f':
4087 case ' ':
4088 if (s->eof)
4089 break;
4090 i = 0;
4091 while (!s->eof && i++ < nscroll) {
4092 linelen = getline(&line, &linesize, s->f);
4093 s->first_displayed_line++;
4094 if (linelen == -1) {
4095 if (feof(s->f)) {
4096 s->eof = 1;
4097 } else
4098 err = got_ferror(s->f, GOT_ERR_IO);
4099 break;
4102 free(line);
4103 break;
4104 case '[':
4105 if (s->diff_context > 0) {
4106 s->diff_context--;
4107 s->matched_line = 0;
4108 diff_view_indicate_progress(view);
4109 err = create_diff(s);
4110 if (s->first_displayed_line + view->nlines - 1 >
4111 s->nlines) {
4112 s->first_displayed_line = 1;
4113 s->last_displayed_line = view->nlines;
4116 break;
4117 case ']':
4118 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4119 s->diff_context++;
4120 s->matched_line = 0;
4121 diff_view_indicate_progress(view);
4122 err = create_diff(s);
4124 break;
4125 case '<':
4126 case ',':
4127 if (s->log_view == NULL)
4128 break;
4129 ls = &s->log_view->state.log;
4130 old_selected_entry = ls->selected_entry;
4132 err = input_log_view(NULL, s->log_view, KEY_UP);
4133 if (err)
4134 break;
4136 if (old_selected_entry == ls->selected_entry)
4137 break;
4139 err = set_selected_commit(s, ls->selected_entry);
4140 if (err)
4141 break;
4143 s->first_displayed_line = 1;
4144 s->last_displayed_line = view->nlines;
4145 s->matched_line = 0;
4146 view->x = 0;
4148 diff_view_indicate_progress(view);
4149 err = create_diff(s);
4150 break;
4151 case '>':
4152 case '.':
4153 if (s->log_view == NULL)
4154 break;
4155 ls = &s->log_view->state.log;
4156 old_selected_entry = ls->selected_entry;
4158 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4159 if (err)
4160 break;
4162 if (old_selected_entry == ls->selected_entry)
4163 break;
4165 err = set_selected_commit(s, ls->selected_entry);
4166 if (err)
4167 break;
4169 s->first_displayed_line = 1;
4170 s->last_displayed_line = view->nlines;
4171 s->matched_line = 0;
4172 view->x = 0;
4174 diff_view_indicate_progress(view);
4175 err = create_diff(s);
4176 break;
4177 default:
4178 break;
4181 return err;
4184 static const struct got_error *
4185 cmd_diff(int argc, char *argv[])
4187 const struct got_error *error = NULL;
4188 struct got_repository *repo = NULL;
4189 struct got_worktree *worktree = NULL;
4190 struct got_object_id *id1 = NULL, *id2 = NULL;
4191 char *repo_path = NULL, *cwd = NULL;
4192 char *id_str1 = NULL, *id_str2 = NULL;
4193 char *label1 = NULL, *label2 = NULL;
4194 int diff_context = 3, ignore_whitespace = 0;
4195 int ch, force_text_diff = 0;
4196 const char *errstr;
4197 struct tog_view *view;
4198 int *pack_fds = NULL;
4200 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4201 switch (ch) {
4202 case 'a':
4203 force_text_diff = 1;
4204 break;
4205 case 'C':
4206 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4207 &errstr);
4208 if (errstr != NULL)
4209 errx(1, "number of context lines is %s: %s",
4210 errstr, errstr);
4211 break;
4212 case 'r':
4213 repo_path = realpath(optarg, NULL);
4214 if (repo_path == NULL)
4215 return got_error_from_errno2("realpath",
4216 optarg);
4217 got_path_strip_trailing_slashes(repo_path);
4218 break;
4219 case 'w':
4220 ignore_whitespace = 1;
4221 break;
4222 default:
4223 usage_diff();
4224 /* NOTREACHED */
4228 argc -= optind;
4229 argv += optind;
4231 if (argc == 0) {
4232 usage_diff(); /* TODO show local worktree changes */
4233 } else if (argc == 2) {
4234 id_str1 = argv[0];
4235 id_str2 = argv[1];
4236 } else
4237 usage_diff();
4239 error = got_repo_pack_fds_open(&pack_fds);
4240 if (error)
4241 goto done;
4243 if (repo_path == NULL) {
4244 cwd = getcwd(NULL, 0);
4245 if (cwd == NULL)
4246 return got_error_from_errno("getcwd");
4247 error = got_worktree_open(&worktree, cwd);
4248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4249 goto done;
4250 if (worktree)
4251 repo_path =
4252 strdup(got_worktree_get_repo_path(worktree));
4253 else
4254 repo_path = strdup(cwd);
4255 if (repo_path == NULL) {
4256 error = got_error_from_errno("strdup");
4257 goto done;
4261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4262 if (error)
4263 goto done;
4265 init_curses();
4267 error = apply_unveil(got_repo_get_path(repo), NULL);
4268 if (error)
4269 goto done;
4271 error = tog_load_refs(repo, 0);
4272 if (error)
4273 goto done;
4275 error = got_repo_match_object_id(&id1, &label1, id_str1,
4276 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4277 if (error)
4278 goto done;
4280 error = got_repo_match_object_id(&id2, &label2, id_str2,
4281 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4282 if (error)
4283 goto done;
4285 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4286 if (view == NULL) {
4287 error = got_error_from_errno("view_open");
4288 goto done;
4290 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4291 ignore_whitespace, force_text_diff, NULL, repo);
4292 if (error)
4293 goto done;
4294 error = view_loop(view);
4295 done:
4296 free(label1);
4297 free(label2);
4298 free(repo_path);
4299 free(cwd);
4300 if (repo) {
4301 const struct got_error *close_err = got_repo_close(repo);
4302 if (error == NULL)
4303 error = close_err;
4305 if (worktree)
4306 got_worktree_close(worktree);
4307 if (pack_fds) {
4308 const struct got_error *pack_err =
4309 got_repo_pack_fds_close(pack_fds);
4310 if (error == NULL)
4311 error = pack_err;
4313 tog_free_refs();
4314 return error;
4317 __dead static void
4318 usage_blame(void)
4320 endwin();
4321 fprintf(stderr,
4322 "usage: %s blame [-c commit] [-r repository-path] path\n",
4323 getprogname());
4324 exit(1);
4327 struct tog_blame_line {
4328 int annotated;
4329 struct got_object_id *id;
4332 static const struct got_error *
4333 draw_blame(struct tog_view *view)
4335 struct tog_blame_view_state *s = &view->state.blame;
4336 struct tog_blame *blame = &s->blame;
4337 regmatch_t *regmatch = &view->regmatch;
4338 const struct got_error *err;
4339 int lineno = 0, nprinted = 0;
4340 char *line = NULL;
4341 size_t linesize = 0;
4342 ssize_t linelen;
4343 wchar_t *wline;
4344 int width;
4345 struct tog_blame_line *blame_line;
4346 struct got_object_id *prev_id = NULL;
4347 char *id_str;
4348 struct tog_color *tc;
4350 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4351 if (err)
4352 return err;
4354 rewind(blame->f);
4355 werase(view->window);
4357 if (asprintf(&line, "commit %s", id_str) == -1) {
4358 err = got_error_from_errno("asprintf");
4359 free(id_str);
4360 return err;
4363 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4364 free(line);
4365 line = NULL;
4366 if (err)
4367 return err;
4368 if (view_needs_focus_indication(view))
4369 wstandout(view->window);
4370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4371 if (tc)
4372 wattr_on(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 waddwstr(view->window, wline);
4375 if (tc)
4376 wattr_off(view->window,
4377 COLOR_PAIR(tc->colorpair), NULL);
4378 if (view_needs_focus_indication(view))
4379 wstandend(view->window);
4380 free(wline);
4381 wline = NULL;
4382 if (width < view->ncols - 1)
4383 waddch(view->window, '\n');
4385 if (asprintf(&line, "[%d/%d] %s%s",
4386 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4387 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4388 free(id_str);
4389 return got_error_from_errno("asprintf");
4391 free(id_str);
4392 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4393 free(line);
4394 line = NULL;
4395 if (err)
4396 return err;
4397 waddwstr(view->window, wline);
4398 free(wline);
4399 wline = NULL;
4400 if (width < view->ncols - 1)
4401 waddch(view->window, '\n');
4403 s->eof = 0;
4404 view->maxx = 0;
4405 while (nprinted < view->nlines - 2) {
4406 linelen = getline(&line, &linesize, blame->f);
4407 if (linelen == -1) {
4408 if (feof(blame->f)) {
4409 s->eof = 1;
4410 break;
4412 free(line);
4413 return got_ferror(blame->f, GOT_ERR_IO);
4415 if (++lineno < s->first_displayed_line)
4416 continue;
4418 /* Set view->maxx based on full line length. */
4419 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4420 if (err) {
4421 free(line);
4422 return err;
4424 free(wline);
4425 wline = NULL;
4426 view->maxx = MAX(view->maxx, width);
4428 if (view->focussed && nprinted == s->selected_line - 1)
4429 wstandout(view->window);
4431 if (blame->nlines > 0) {
4432 blame_line = &blame->lines[lineno - 1];
4433 if (blame_line->annotated && prev_id &&
4434 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4435 !(view->focussed &&
4436 nprinted == s->selected_line - 1)) {
4437 waddstr(view->window, " ");
4438 } else if (blame_line->annotated) {
4439 char *id_str;
4440 err = got_object_id_str(&id_str,
4441 blame_line->id);
4442 if (err) {
4443 free(line);
4444 return err;
4446 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4447 if (tc)
4448 wattr_on(view->window,
4449 COLOR_PAIR(tc->colorpair), NULL);
4450 wprintw(view->window, "%.8s", id_str);
4451 if (tc)
4452 wattr_off(view->window,
4453 COLOR_PAIR(tc->colorpair), NULL);
4454 free(id_str);
4455 prev_id = blame_line->id;
4456 } else {
4457 waddstr(view->window, "........");
4458 prev_id = NULL;
4460 } else {
4461 waddstr(view->window, "........");
4462 prev_id = NULL;
4465 if (view->focussed && nprinted == s->selected_line - 1)
4466 wstandend(view->window);
4467 waddstr(view->window, " ");
4469 if (view->ncols <= 9) {
4470 width = 9;
4471 } else if (s->first_displayed_line + nprinted ==
4472 s->matched_line &&
4473 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4474 err = add_matched_line(&width, line, view->ncols - 9, 9,
4475 view->window, view->x, regmatch);
4476 if (err) {
4477 free(line);
4478 return err;
4480 width += 9;
4481 } else {
4482 int skip;
4483 err = format_line(&wline, &width, &skip, line,
4484 view->x, view->ncols - 9, 9, 1);
4485 if (err) {
4486 free(line);
4487 return err;
4489 waddwstr(view->window, &wline[skip]);
4490 width += 9;
4491 free(wline);
4492 wline = NULL;
4495 if (width <= view->ncols - 1)
4496 waddch(view->window, '\n');
4497 if (++nprinted == 1)
4498 s->first_displayed_line = lineno;
4500 free(line);
4501 s->last_displayed_line = lineno;
4503 view_vborder(view);
4505 return NULL;
4508 static const struct got_error *
4509 blame_cb(void *arg, int nlines, int lineno,
4510 struct got_commit_object *commit, struct got_object_id *id)
4512 const struct got_error *err = NULL;
4513 struct tog_blame_cb_args *a = arg;
4514 struct tog_blame_line *line;
4515 int errcode;
4517 if (nlines != a->nlines ||
4518 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4519 return got_error(GOT_ERR_RANGE);
4521 errcode = pthread_mutex_lock(&tog_mutex);
4522 if (errcode)
4523 return got_error_set_errno(errcode, "pthread_mutex_lock");
4525 if (*a->quit) { /* user has quit the blame view */
4526 err = got_error(GOT_ERR_ITER_COMPLETED);
4527 goto done;
4530 if (lineno == -1)
4531 goto done; /* no change in this commit */
4533 line = &a->lines[lineno - 1];
4534 if (line->annotated)
4535 goto done;
4537 line->id = got_object_id_dup(id);
4538 if (line->id == NULL) {
4539 err = got_error_from_errno("got_object_id_dup");
4540 goto done;
4542 line->annotated = 1;
4543 done:
4544 errcode = pthread_mutex_unlock(&tog_mutex);
4545 if (errcode)
4546 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4547 return err;
4550 static void *
4551 blame_thread(void *arg)
4553 const struct got_error *err, *close_err;
4554 struct tog_blame_thread_args *ta = arg;
4555 struct tog_blame_cb_args *a = ta->cb_args;
4556 int errcode;
4558 err = block_signals_used_by_main_thread();
4559 if (err)
4560 return (void *)err;
4562 err = got_blame(ta->path, a->commit_id, ta->repo,
4563 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4564 if (err && err->code == GOT_ERR_CANCELLED)
4565 err = NULL;
4567 errcode = pthread_mutex_lock(&tog_mutex);
4568 if (errcode)
4569 return (void *)got_error_set_errno(errcode,
4570 "pthread_mutex_lock");
4572 close_err = got_repo_close(ta->repo);
4573 if (err == NULL)
4574 err = close_err;
4575 ta->repo = NULL;
4576 *ta->complete = 1;
4578 errcode = pthread_mutex_unlock(&tog_mutex);
4579 if (errcode && err == NULL)
4580 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4582 return (void *)err;
4585 static struct got_object_id *
4586 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4587 int first_displayed_line, int selected_line)
4589 struct tog_blame_line *line;
4591 if (nlines <= 0)
4592 return NULL;
4594 line = &lines[first_displayed_line - 1 + selected_line - 1];
4595 if (!line->annotated)
4596 return NULL;
4598 return line->id;
4601 static const struct got_error *
4602 stop_blame(struct tog_blame *blame)
4604 const struct got_error *err = NULL;
4605 int i;
4607 if (blame->thread) {
4608 int errcode;
4609 errcode = pthread_mutex_unlock(&tog_mutex);
4610 if (errcode)
4611 return got_error_set_errno(errcode,
4612 "pthread_mutex_unlock");
4613 errcode = pthread_join(blame->thread, (void **)&err);
4614 if (errcode)
4615 return got_error_set_errno(errcode, "pthread_join");
4616 errcode = pthread_mutex_lock(&tog_mutex);
4617 if (errcode)
4618 return got_error_set_errno(errcode,
4619 "pthread_mutex_lock");
4620 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4621 err = NULL;
4622 blame->thread = NULL;
4624 if (blame->thread_args.repo) {
4625 const struct got_error *close_err;
4626 close_err = got_repo_close(blame->thread_args.repo);
4627 if (err == NULL)
4628 err = close_err;
4629 blame->thread_args.repo = NULL;
4631 if (blame->f) {
4632 if (fclose(blame->f) == EOF && err == NULL)
4633 err = got_error_from_errno("fclose");
4634 blame->f = NULL;
4636 if (blame->lines) {
4637 for (i = 0; i < blame->nlines; i++)
4638 free(blame->lines[i].id);
4639 free(blame->lines);
4640 blame->lines = NULL;
4642 free(blame->cb_args.commit_id);
4643 blame->cb_args.commit_id = NULL;
4644 if (blame->pack_fds) {
4645 const struct got_error *pack_err =
4646 got_repo_pack_fds_close(blame->pack_fds);
4647 if (err == NULL)
4648 err = pack_err;
4649 blame->pack_fds = NULL;
4651 return err;
4654 static const struct got_error *
4655 cancel_blame_view(void *arg)
4657 const struct got_error *err = NULL;
4658 int *done = arg;
4659 int errcode;
4661 errcode = pthread_mutex_lock(&tog_mutex);
4662 if (errcode)
4663 return got_error_set_errno(errcode,
4664 "pthread_mutex_unlock");
4666 if (*done)
4667 err = got_error(GOT_ERR_CANCELLED);
4669 errcode = pthread_mutex_unlock(&tog_mutex);
4670 if (errcode)
4671 return got_error_set_errno(errcode,
4672 "pthread_mutex_lock");
4674 return err;
4677 static const struct got_error *
4678 run_blame(struct tog_view *view)
4680 struct tog_blame_view_state *s = &view->state.blame;
4681 struct tog_blame *blame = &s->blame;
4682 const struct got_error *err = NULL;
4683 struct got_commit_object *commit = NULL;
4684 struct got_blob_object *blob = NULL;
4685 struct got_repository *thread_repo = NULL;
4686 struct got_object_id *obj_id = NULL;
4687 int obj_type;
4688 int *pack_fds = NULL;
4690 err = got_object_open_as_commit(&commit, s->repo,
4691 &s->blamed_commit->id);
4692 if (err)
4693 return err;
4695 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4696 if (err)
4697 goto done;
4699 err = got_object_get_type(&obj_type, s->repo, obj_id);
4700 if (err)
4701 goto done;
4703 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4704 err = got_error(GOT_ERR_OBJ_TYPE);
4705 goto done;
4708 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4709 if (err)
4710 goto done;
4711 blame->f = got_opentemp();
4712 if (blame->f == NULL) {
4713 err = got_error_from_errno("got_opentemp");
4714 goto done;
4716 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4717 &blame->line_offsets, blame->f, blob);
4718 if (err)
4719 goto done;
4720 if (blame->nlines == 0) {
4721 s->blame_complete = 1;
4722 goto done;
4725 /* Don't include \n at EOF in the blame line count. */
4726 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4727 blame->nlines--;
4729 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4730 if (blame->lines == NULL) {
4731 err = got_error_from_errno("calloc");
4732 goto done;
4735 err = got_repo_pack_fds_open(&pack_fds);
4736 if (err)
4737 goto done;
4738 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4739 pack_fds);
4740 if (err)
4741 goto done;
4743 blame->pack_fds = pack_fds;
4744 blame->cb_args.view = view;
4745 blame->cb_args.lines = blame->lines;
4746 blame->cb_args.nlines = blame->nlines;
4747 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4748 if (blame->cb_args.commit_id == NULL) {
4749 err = got_error_from_errno("got_object_id_dup");
4750 goto done;
4752 blame->cb_args.quit = &s->done;
4754 blame->thread_args.path = s->path;
4755 blame->thread_args.repo = thread_repo;
4756 blame->thread_args.cb_args = &blame->cb_args;
4757 blame->thread_args.complete = &s->blame_complete;
4758 blame->thread_args.cancel_cb = cancel_blame_view;
4759 blame->thread_args.cancel_arg = &s->done;
4760 s->blame_complete = 0;
4762 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4763 s->first_displayed_line = 1;
4764 s->last_displayed_line = view->nlines;
4765 s->selected_line = 1;
4767 s->matched_line = 0;
4769 done:
4770 if (commit)
4771 got_object_commit_close(commit);
4772 if (blob)
4773 got_object_blob_close(blob);
4774 free(obj_id);
4775 if (err)
4776 stop_blame(blame);
4777 return err;
4780 static const struct got_error *
4781 open_blame_view(struct tog_view *view, char *path,
4782 struct got_object_id *commit_id, struct got_repository *repo)
4784 const struct got_error *err = NULL;
4785 struct tog_blame_view_state *s = &view->state.blame;
4787 STAILQ_INIT(&s->blamed_commits);
4789 s->path = strdup(path);
4790 if (s->path == NULL)
4791 return got_error_from_errno("strdup");
4793 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4794 if (err) {
4795 free(s->path);
4796 return err;
4799 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4800 s->first_displayed_line = 1;
4801 s->last_displayed_line = view->nlines;
4802 s->selected_line = 1;
4803 s->blame_complete = 0;
4804 s->repo = repo;
4805 s->commit_id = commit_id;
4806 memset(&s->blame, 0, sizeof(s->blame));
4808 STAILQ_INIT(&s->colors);
4809 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4810 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4811 get_color_value("TOG_COLOR_COMMIT"));
4812 if (err)
4813 return err;
4816 view->show = show_blame_view;
4817 view->input = input_blame_view;
4818 view->close = close_blame_view;
4819 view->search_start = search_start_blame_view;
4820 view->search_next = search_next_blame_view;
4822 return run_blame(view);
4825 static const struct got_error *
4826 close_blame_view(struct tog_view *view)
4828 const struct got_error *err = NULL;
4829 struct tog_blame_view_state *s = &view->state.blame;
4831 if (s->blame.thread)
4832 err = stop_blame(&s->blame);
4834 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4835 struct got_object_qid *blamed_commit;
4836 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4837 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4838 got_object_qid_free(blamed_commit);
4841 free(s->path);
4842 free_colors(&s->colors);
4843 return err;
4846 static const struct got_error *
4847 search_start_blame_view(struct tog_view *view)
4849 struct tog_blame_view_state *s = &view->state.blame;
4851 s->matched_line = 0;
4852 return NULL;
4855 static const struct got_error *
4856 search_next_blame_view(struct tog_view *view)
4858 struct tog_blame_view_state *s = &view->state.blame;
4859 const struct got_error *err = NULL;
4860 int lineno;
4861 char *line = NULL;
4862 size_t linesize = 0;
4863 ssize_t linelen;
4865 if (!view->searching) {
4866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4867 return NULL;
4870 if (s->matched_line) {
4871 if (view->searching == TOG_SEARCH_FORWARD)
4872 lineno = s->matched_line + 1;
4873 else
4874 lineno = s->matched_line - 1;
4875 } else
4876 lineno = s->first_displayed_line - 1 + s->selected_line;
4878 while (1) {
4879 off_t offset;
4881 if (lineno <= 0 || lineno > s->blame.nlines) {
4882 if (s->matched_line == 0) {
4883 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4884 break;
4887 if (view->searching == TOG_SEARCH_FORWARD)
4888 lineno = 1;
4889 else
4890 lineno = s->blame.nlines;
4893 offset = s->blame.line_offsets[lineno - 1];
4894 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4895 free(line);
4896 return got_error_from_errno("fseeko");
4898 linelen = getline(&line, &linesize, s->blame.f);
4899 if (linelen != -1) {
4900 char *exstr;
4901 err = expand_tab(&exstr, line);
4902 if (err)
4903 break;
4904 if (match_line(exstr, &view->regex, 1,
4905 &view->regmatch)) {
4906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4907 s->matched_line = lineno;
4908 free(exstr);
4909 break;
4911 free(exstr);
4913 if (view->searching == TOG_SEARCH_FORWARD)
4914 lineno++;
4915 else
4916 lineno--;
4918 free(line);
4920 if (s->matched_line) {
4921 s->first_displayed_line = s->matched_line;
4922 s->selected_line = 1;
4925 return err;
4928 static const struct got_error *
4929 show_blame_view(struct tog_view *view)
4931 const struct got_error *err = NULL;
4932 struct tog_blame_view_state *s = &view->state.blame;
4933 int errcode;
4935 if (s->blame.thread == NULL && !s->blame_complete) {
4936 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4937 &s->blame.thread_args);
4938 if (errcode)
4939 return got_error_set_errno(errcode, "pthread_create");
4941 halfdelay(1); /* fast refresh while annotating */
4944 if (s->blame_complete)
4945 halfdelay(10); /* disable fast refresh */
4947 err = draw_blame(view);
4949 view_vborder(view);
4950 return err;
4953 static const struct got_error *
4954 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4956 const struct got_error *err = NULL, *thread_err = NULL;
4957 struct tog_view *diff_view;
4958 struct tog_blame_view_state *s = &view->state.blame;
4959 int begin_x = 0, nscroll = view->nlines - 2;
4961 switch (ch) {
4962 case '0':
4963 view->x = 0;
4964 break;
4965 case '$':
4966 view->x = MAX(view->maxx - view->ncols / 3, 0);
4967 break;
4968 case KEY_RIGHT:
4969 case 'l':
4970 if (view->x + view->ncols / 3 < view->maxx)
4971 view->x += 2; /* move two columns right */
4972 break;
4973 case KEY_LEFT:
4974 case 'h':
4975 view->x -= MIN(view->x, 2); /* move two columns back */
4976 break;
4977 case 'q':
4978 s->done = 1;
4979 break;
4980 case 'g':
4981 case KEY_HOME:
4982 s->selected_line = 1;
4983 s->first_displayed_line = 1;
4984 break;
4985 case 'G':
4986 case KEY_END:
4987 if (s->blame.nlines < view->nlines - 2) {
4988 s->selected_line = s->blame.nlines;
4989 s->first_displayed_line = 1;
4990 } else {
4991 s->selected_line = view->nlines - 2;
4992 s->first_displayed_line = s->blame.nlines -
4993 (view->nlines - 3);
4995 break;
4996 case 'k':
4997 case KEY_UP:
4998 case CTRL('p'):
4999 if (s->selected_line > 1)
5000 s->selected_line--;
5001 else if (s->selected_line == 1 &&
5002 s->first_displayed_line > 1)
5003 s->first_displayed_line--;
5004 break;
5005 case CTRL('u'):
5006 case 'u':
5007 nscroll /= 2;
5008 /* FALL THROUGH */
5009 case KEY_PPAGE:
5010 case CTRL('b'):
5011 case 'b':
5012 if (s->first_displayed_line == 1) {
5013 s->selected_line = MAX(1, s->selected_line - nscroll);
5014 break;
5016 if (s->first_displayed_line > nscroll)
5017 s->first_displayed_line -= nscroll;
5018 else
5019 s->first_displayed_line = 1;
5020 break;
5021 case 'j':
5022 case KEY_DOWN:
5023 case CTRL('n'):
5024 if (s->selected_line < view->nlines - 2 &&
5025 s->first_displayed_line +
5026 s->selected_line <= s->blame.nlines)
5027 s->selected_line++;
5028 else if (s->last_displayed_line <
5029 s->blame.nlines)
5030 s->first_displayed_line++;
5031 break;
5032 case 'c':
5033 case 'p': {
5034 struct got_object_id *id = NULL;
5035 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5036 s->first_displayed_line, s->selected_line);
5037 if (id == NULL)
5038 break;
5039 if (ch == 'p') {
5040 struct got_commit_object *commit, *pcommit;
5041 struct got_object_qid *pid;
5042 struct got_object_id *blob_id = NULL;
5043 int obj_type;
5044 err = got_object_open_as_commit(&commit,
5045 s->repo, id);
5046 if (err)
5047 break;
5048 pid = STAILQ_FIRST(
5049 got_object_commit_get_parent_ids(commit));
5050 if (pid == NULL) {
5051 got_object_commit_close(commit);
5052 break;
5054 /* Check if path history ends here. */
5055 err = got_object_open_as_commit(&pcommit,
5056 s->repo, &pid->id);
5057 if (err)
5058 break;
5059 err = got_object_id_by_path(&blob_id, s->repo,
5060 pcommit, s->path);
5061 got_object_commit_close(pcommit);
5062 if (err) {
5063 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5064 err = NULL;
5065 got_object_commit_close(commit);
5066 break;
5068 err = got_object_get_type(&obj_type, s->repo,
5069 blob_id);
5070 free(blob_id);
5071 /* Can't blame non-blob type objects. */
5072 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5073 got_object_commit_close(commit);
5074 break;
5076 err = got_object_qid_alloc(&s->blamed_commit,
5077 &pid->id);
5078 got_object_commit_close(commit);
5079 } else {
5080 if (got_object_id_cmp(id,
5081 &s->blamed_commit->id) == 0)
5082 break;
5083 err = got_object_qid_alloc(&s->blamed_commit,
5084 id);
5086 if (err)
5087 break;
5088 s->done = 1;
5089 thread_err = stop_blame(&s->blame);
5090 s->done = 0;
5091 if (thread_err)
5092 break;
5093 STAILQ_INSERT_HEAD(&s->blamed_commits,
5094 s->blamed_commit, entry);
5095 err = run_blame(view);
5096 if (err)
5097 break;
5098 break;
5100 case 'C': {
5101 struct got_object_qid *first;
5102 first = STAILQ_FIRST(&s->blamed_commits);
5103 if (!got_object_id_cmp(&first->id, s->commit_id))
5104 break;
5105 s->done = 1;
5106 thread_err = stop_blame(&s->blame);
5107 s->done = 0;
5108 if (thread_err)
5109 break;
5110 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5111 got_object_qid_free(s->blamed_commit);
5112 s->blamed_commit =
5113 STAILQ_FIRST(&s->blamed_commits);
5114 err = run_blame(view);
5115 if (err)
5116 break;
5117 break;
5119 case KEY_ENTER:
5120 case '\r': {
5121 struct got_object_id *id = NULL;
5122 struct got_object_qid *pid;
5123 struct got_commit_object *commit = NULL;
5124 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5125 s->first_displayed_line, s->selected_line);
5126 if (id == NULL)
5127 break;
5128 err = got_object_open_as_commit(&commit, s->repo, id);
5129 if (err)
5130 break;
5131 pid = STAILQ_FIRST(
5132 got_object_commit_get_parent_ids(commit));
5133 if (view_is_parent_view(view))
5134 begin_x = view_split_begin_x(view->begin_x);
5135 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5136 if (diff_view == NULL) {
5137 got_object_commit_close(commit);
5138 err = got_error_from_errno("view_open");
5139 break;
5141 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5142 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5143 got_object_commit_close(commit);
5144 if (err) {
5145 view_close(diff_view);
5146 break;
5148 view->focussed = 0;
5149 diff_view->focussed = 1;
5150 if (view_is_parent_view(view)) {
5151 err = view_close_child(view);
5152 if (err)
5153 break;
5154 err = view_set_child(view, diff_view);
5155 if (err)
5156 break;
5157 view->focus_child = 1;
5158 } else
5159 *new_view = diff_view;
5160 if (err)
5161 break;
5162 break;
5164 case CTRL('d'):
5165 case 'd':
5166 nscroll /= 2;
5167 /* FALL THROUGH */
5168 case KEY_NPAGE:
5169 case CTRL('f'):
5170 case 'f':
5171 case ' ':
5172 if (s->last_displayed_line >= s->blame.nlines &&
5173 s->selected_line >= MIN(s->blame.nlines,
5174 view->nlines - 2)) {
5175 break;
5177 if (s->last_displayed_line >= s->blame.nlines &&
5178 s->selected_line < view->nlines - 2) {
5179 s->selected_line +=
5180 MIN(nscroll, s->last_displayed_line -
5181 s->first_displayed_line - s->selected_line + 1);
5183 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5184 s->first_displayed_line += nscroll;
5185 else
5186 s->first_displayed_line =
5187 s->blame.nlines - (view->nlines - 3);
5188 break;
5189 case KEY_RESIZE:
5190 if (s->selected_line > view->nlines - 2) {
5191 s->selected_line = MIN(s->blame.nlines,
5192 view->nlines - 2);
5194 break;
5195 default:
5196 break;
5198 return thread_err ? thread_err : err;
5201 static const struct got_error *
5202 cmd_blame(int argc, char *argv[])
5204 const struct got_error *error;
5205 struct got_repository *repo = NULL;
5206 struct got_worktree *worktree = NULL;
5207 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5208 char *link_target = NULL;
5209 struct got_object_id *commit_id = NULL;
5210 struct got_commit_object *commit = NULL;
5211 char *commit_id_str = NULL;
5212 int ch;
5213 struct tog_view *view;
5214 int *pack_fds = NULL;
5216 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5217 switch (ch) {
5218 case 'c':
5219 commit_id_str = optarg;
5220 break;
5221 case 'r':
5222 repo_path = realpath(optarg, NULL);
5223 if (repo_path == NULL)
5224 return got_error_from_errno2("realpath",
5225 optarg);
5226 break;
5227 default:
5228 usage_blame();
5229 /* NOTREACHED */
5233 argc -= optind;
5234 argv += optind;
5236 if (argc != 1)
5237 usage_blame();
5239 error = got_repo_pack_fds_open(&pack_fds);
5240 if (error != NULL)
5241 goto done;
5243 if (repo_path == NULL) {
5244 cwd = getcwd(NULL, 0);
5245 if (cwd == NULL)
5246 return got_error_from_errno("getcwd");
5247 error = got_worktree_open(&worktree, cwd);
5248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5249 goto done;
5250 if (worktree)
5251 repo_path =
5252 strdup(got_worktree_get_repo_path(worktree));
5253 else
5254 repo_path = strdup(cwd);
5255 if (repo_path == NULL) {
5256 error = got_error_from_errno("strdup");
5257 goto done;
5261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5262 if (error != NULL)
5263 goto done;
5265 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5266 worktree);
5267 if (error)
5268 goto done;
5270 init_curses();
5272 error = apply_unveil(got_repo_get_path(repo), NULL);
5273 if (error)
5274 goto done;
5276 error = tog_load_refs(repo, 0);
5277 if (error)
5278 goto done;
5280 if (commit_id_str == NULL) {
5281 struct got_reference *head_ref;
5282 error = got_ref_open(&head_ref, repo, worktree ?
5283 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5284 if (error != NULL)
5285 goto done;
5286 error = got_ref_resolve(&commit_id, repo, head_ref);
5287 got_ref_close(head_ref);
5288 } else {
5289 error = got_repo_match_object_id(&commit_id, NULL,
5290 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5292 if (error != NULL)
5293 goto done;
5295 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5296 if (view == NULL) {
5297 error = got_error_from_errno("view_open");
5298 goto done;
5301 error = got_object_open_as_commit(&commit, repo, commit_id);
5302 if (error)
5303 goto done;
5305 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5306 commit, repo);
5307 if (error)
5308 goto done;
5310 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5311 commit_id, repo);
5312 if (error)
5313 goto done;
5314 if (worktree) {
5315 /* Release work tree lock. */
5316 got_worktree_close(worktree);
5317 worktree = NULL;
5319 error = view_loop(view);
5320 done:
5321 free(repo_path);
5322 free(in_repo_path);
5323 free(link_target);
5324 free(cwd);
5325 free(commit_id);
5326 if (commit)
5327 got_object_commit_close(commit);
5328 if (worktree)
5329 got_worktree_close(worktree);
5330 if (repo) {
5331 const struct got_error *close_err = got_repo_close(repo);
5332 if (error == NULL)
5333 error = close_err;
5335 if (pack_fds) {
5336 const struct got_error *pack_err =
5337 got_repo_pack_fds_close(pack_fds);
5338 if (error == NULL)
5339 error = pack_err;
5341 tog_free_refs();
5342 return error;
5345 static const struct got_error *
5346 draw_tree_entries(struct tog_view *view, const char *parent_path)
5348 struct tog_tree_view_state *s = &view->state.tree;
5349 const struct got_error *err = NULL;
5350 struct got_tree_entry *te;
5351 wchar_t *wline;
5352 struct tog_color *tc;
5353 int width, n, i, nentries;
5354 int limit = view->nlines;
5356 s->ndisplayed = 0;
5358 werase(view->window);
5360 if (limit == 0)
5361 return NULL;
5363 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5364 0, 0);
5365 if (err)
5366 return err;
5367 if (view_needs_focus_indication(view))
5368 wstandout(view->window);
5369 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5370 if (tc)
5371 wattr_on(view->window,
5372 COLOR_PAIR(tc->colorpair), NULL);
5373 waddwstr(view->window, wline);
5374 if (tc)
5375 wattr_off(view->window,
5376 COLOR_PAIR(tc->colorpair), NULL);
5377 if (view_needs_focus_indication(view))
5378 wstandend(view->window);
5379 free(wline);
5380 wline = NULL;
5381 if (width < view->ncols - 1)
5382 waddch(view->window, '\n');
5383 if (--limit <= 0)
5384 return NULL;
5385 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5386 0, 0);
5387 if (err)
5388 return err;
5389 waddwstr(view->window, wline);
5390 free(wline);
5391 wline = NULL;
5392 if (width < view->ncols - 1)
5393 waddch(view->window, '\n');
5394 if (--limit <= 0)
5395 return NULL;
5396 waddch(view->window, '\n');
5397 if (--limit <= 0)
5398 return NULL;
5400 if (s->first_displayed_entry == NULL) {
5401 te = got_object_tree_get_first_entry(s->tree);
5402 if (s->selected == 0) {
5403 if (view->focussed)
5404 wstandout(view->window);
5405 s->selected_entry = NULL;
5407 waddstr(view->window, " ..\n"); /* parent directory */
5408 if (s->selected == 0 && view->focussed)
5409 wstandend(view->window);
5410 s->ndisplayed++;
5411 if (--limit <= 0)
5412 return NULL;
5413 n = 1;
5414 } else {
5415 n = 0;
5416 te = s->first_displayed_entry;
5419 nentries = got_object_tree_get_nentries(s->tree);
5420 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5421 char *line = NULL, *id_str = NULL, *link_target = NULL;
5422 const char *modestr = "";
5423 mode_t mode;
5425 te = got_object_tree_get_entry(s->tree, i);
5426 mode = got_tree_entry_get_mode(te);
5428 if (s->show_ids) {
5429 err = got_object_id_str(&id_str,
5430 got_tree_entry_get_id(te));
5431 if (err)
5432 return got_error_from_errno(
5433 "got_object_id_str");
5435 if (got_object_tree_entry_is_submodule(te))
5436 modestr = "$";
5437 else if (S_ISLNK(mode)) {
5438 int i;
5440 err = got_tree_entry_get_symlink_target(&link_target,
5441 te, s->repo);
5442 if (err) {
5443 free(id_str);
5444 return err;
5446 for (i = 0; i < strlen(link_target); i++) {
5447 if (!isprint((unsigned char)link_target[i]))
5448 link_target[i] = '?';
5450 modestr = "@";
5452 else if (S_ISDIR(mode))
5453 modestr = "/";
5454 else if (mode & S_IXUSR)
5455 modestr = "*";
5456 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5457 got_tree_entry_get_name(te), modestr,
5458 link_target ? " -> ": "",
5459 link_target ? link_target : "") == -1) {
5460 free(id_str);
5461 free(link_target);
5462 return got_error_from_errno("asprintf");
5464 free(id_str);
5465 free(link_target);
5466 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5467 0, 0);
5468 if (err) {
5469 free(line);
5470 break;
5472 if (n == s->selected) {
5473 if (view->focussed)
5474 wstandout(view->window);
5475 s->selected_entry = te;
5477 tc = match_color(&s->colors, line);
5478 if (tc)
5479 wattr_on(view->window,
5480 COLOR_PAIR(tc->colorpair), NULL);
5481 waddwstr(view->window, wline);
5482 if (tc)
5483 wattr_off(view->window,
5484 COLOR_PAIR(tc->colorpair), NULL);
5485 if (width < view->ncols - 1)
5486 waddch(view->window, '\n');
5487 if (n == s->selected && view->focussed)
5488 wstandend(view->window);
5489 free(line);
5490 free(wline);
5491 wline = NULL;
5492 n++;
5493 s->ndisplayed++;
5494 s->last_displayed_entry = te;
5495 if (--limit <= 0)
5496 break;
5499 return err;
5502 static void
5503 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5505 struct got_tree_entry *te;
5506 int isroot = s->tree == s->root;
5507 int i = 0;
5509 if (s->first_displayed_entry == NULL)
5510 return;
5512 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5513 while (i++ < maxscroll) {
5514 if (te == NULL) {
5515 if (!isroot)
5516 s->first_displayed_entry = NULL;
5517 break;
5519 s->first_displayed_entry = te;
5520 te = got_tree_entry_get_prev(s->tree, te);
5524 static void
5525 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5527 struct got_tree_entry *next, *last;
5528 int n = 0;
5530 if (s->first_displayed_entry)
5531 next = got_tree_entry_get_next(s->tree,
5532 s->first_displayed_entry);
5533 else
5534 next = got_object_tree_get_first_entry(s->tree);
5536 last = s->last_displayed_entry;
5537 while (next && last && n++ < maxscroll) {
5538 last = got_tree_entry_get_next(s->tree, last);
5539 if (last) {
5540 s->first_displayed_entry = next;
5541 next = got_tree_entry_get_next(s->tree, next);
5546 static const struct got_error *
5547 tree_entry_path(char **path, struct tog_parent_trees *parents,
5548 struct got_tree_entry *te)
5550 const struct got_error *err = NULL;
5551 struct tog_parent_tree *pt;
5552 size_t len = 2; /* for leading slash and NUL */
5554 TAILQ_FOREACH(pt, parents, entry)
5555 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5556 + 1 /* slash */;
5557 if (te)
5558 len += strlen(got_tree_entry_get_name(te));
5560 *path = calloc(1, len);
5561 if (path == NULL)
5562 return got_error_from_errno("calloc");
5564 (*path)[0] = '/';
5565 pt = TAILQ_LAST(parents, tog_parent_trees);
5566 while (pt) {
5567 const char *name = got_tree_entry_get_name(pt->selected_entry);
5568 if (strlcat(*path, name, len) >= len) {
5569 err = got_error(GOT_ERR_NO_SPACE);
5570 goto done;
5572 if (strlcat(*path, "/", len) >= len) {
5573 err = got_error(GOT_ERR_NO_SPACE);
5574 goto done;
5576 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5578 if (te) {
5579 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5580 err = got_error(GOT_ERR_NO_SPACE);
5581 goto done;
5584 done:
5585 if (err) {
5586 free(*path);
5587 *path = NULL;
5589 return err;
5592 static const struct got_error *
5593 blame_tree_entry(struct tog_view **new_view, int begin_x,
5594 struct got_tree_entry *te, struct tog_parent_trees *parents,
5595 struct got_object_id *commit_id, struct got_repository *repo)
5597 const struct got_error *err = NULL;
5598 char *path;
5599 struct tog_view *blame_view;
5601 *new_view = NULL;
5603 err = tree_entry_path(&path, parents, te);
5604 if (err)
5605 return err;
5607 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5608 if (blame_view == NULL) {
5609 err = got_error_from_errno("view_open");
5610 goto done;
5613 err = open_blame_view(blame_view, path, commit_id, repo);
5614 if (err) {
5615 if (err->code == GOT_ERR_CANCELLED)
5616 err = NULL;
5617 view_close(blame_view);
5618 } else
5619 *new_view = blame_view;
5620 done:
5621 free(path);
5622 return err;
5625 static const struct got_error *
5626 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5627 struct tog_tree_view_state *s)
5629 struct tog_view *log_view;
5630 const struct got_error *err = NULL;
5631 char *path;
5633 *new_view = NULL;
5635 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5636 if (log_view == NULL)
5637 return got_error_from_errno("view_open");
5639 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5640 if (err)
5641 return err;
5643 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5644 path, 0);
5645 if (err)
5646 view_close(log_view);
5647 else
5648 *new_view = log_view;
5649 free(path);
5650 return err;
5653 static const struct got_error *
5654 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5655 const char *head_ref_name, struct got_repository *repo)
5657 const struct got_error *err = NULL;
5658 char *commit_id_str = NULL;
5659 struct tog_tree_view_state *s = &view->state.tree;
5660 struct got_commit_object *commit = NULL;
5662 TAILQ_INIT(&s->parents);
5663 STAILQ_INIT(&s->colors);
5665 s->commit_id = got_object_id_dup(commit_id);
5666 if (s->commit_id == NULL)
5667 return got_error_from_errno("got_object_id_dup");
5669 err = got_object_open_as_commit(&commit, repo, commit_id);
5670 if (err)
5671 goto done;
5674 * The root is opened here and will be closed when the view is closed.
5675 * Any visited subtrees and their path-wise parents are opened and
5676 * closed on demand.
5678 err = got_object_open_as_tree(&s->root, repo,
5679 got_object_commit_get_tree_id(commit));
5680 if (err)
5681 goto done;
5682 s->tree = s->root;
5684 err = got_object_id_str(&commit_id_str, commit_id);
5685 if (err != NULL)
5686 goto done;
5688 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5689 err = got_error_from_errno("asprintf");
5690 goto done;
5693 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5694 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5695 if (head_ref_name) {
5696 s->head_ref_name = strdup(head_ref_name);
5697 if (s->head_ref_name == NULL) {
5698 err = got_error_from_errno("strdup");
5699 goto done;
5702 s->repo = repo;
5704 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5705 err = add_color(&s->colors, "\\$$",
5706 TOG_COLOR_TREE_SUBMODULE,
5707 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5708 if (err)
5709 goto done;
5710 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5711 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5712 if (err)
5713 goto done;
5714 err = add_color(&s->colors, "/$",
5715 TOG_COLOR_TREE_DIRECTORY,
5716 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5717 if (err)
5718 goto done;
5720 err = add_color(&s->colors, "\\*$",
5721 TOG_COLOR_TREE_EXECUTABLE,
5722 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5723 if (err)
5724 goto done;
5726 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5727 get_color_value("TOG_COLOR_COMMIT"));
5728 if (err)
5729 goto done;
5732 view->show = show_tree_view;
5733 view->input = input_tree_view;
5734 view->close = close_tree_view;
5735 view->search_start = search_start_tree_view;
5736 view->search_next = search_next_tree_view;
5737 done:
5738 free(commit_id_str);
5739 if (commit)
5740 got_object_commit_close(commit);
5741 if (err)
5742 close_tree_view(view);
5743 return err;
5746 static const struct got_error *
5747 close_tree_view(struct tog_view *view)
5749 struct tog_tree_view_state *s = &view->state.tree;
5751 free_colors(&s->colors);
5752 free(s->tree_label);
5753 s->tree_label = NULL;
5754 free(s->commit_id);
5755 s->commit_id = NULL;
5756 free(s->head_ref_name);
5757 s->head_ref_name = NULL;
5758 while (!TAILQ_EMPTY(&s->parents)) {
5759 struct tog_parent_tree *parent;
5760 parent = TAILQ_FIRST(&s->parents);
5761 TAILQ_REMOVE(&s->parents, parent, entry);
5762 if (parent->tree != s->root)
5763 got_object_tree_close(parent->tree);
5764 free(parent);
5767 if (s->tree != NULL && s->tree != s->root)
5768 got_object_tree_close(s->tree);
5769 if (s->root)
5770 got_object_tree_close(s->root);
5771 return NULL;
5774 static const struct got_error *
5775 search_start_tree_view(struct tog_view *view)
5777 struct tog_tree_view_state *s = &view->state.tree;
5779 s->matched_entry = NULL;
5780 return NULL;
5783 static int
5784 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5786 regmatch_t regmatch;
5788 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5789 0) == 0;
5792 static const struct got_error *
5793 search_next_tree_view(struct tog_view *view)
5795 struct tog_tree_view_state *s = &view->state.tree;
5796 struct got_tree_entry *te = NULL;
5798 if (!view->searching) {
5799 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5800 return NULL;
5803 if (s->matched_entry) {
5804 if (view->searching == TOG_SEARCH_FORWARD) {
5805 if (s->selected_entry)
5806 te = got_tree_entry_get_next(s->tree,
5807 s->selected_entry);
5808 else
5809 te = got_object_tree_get_first_entry(s->tree);
5810 } else {
5811 if (s->selected_entry == NULL)
5812 te = got_object_tree_get_last_entry(s->tree);
5813 else
5814 te = got_tree_entry_get_prev(s->tree,
5815 s->selected_entry);
5817 } else {
5818 if (s->selected_entry)
5819 te = s->selected_entry;
5820 else if (view->searching == TOG_SEARCH_FORWARD)
5821 te = got_object_tree_get_first_entry(s->tree);
5822 else
5823 te = got_object_tree_get_last_entry(s->tree);
5826 while (1) {
5827 if (te == NULL) {
5828 if (s->matched_entry == NULL) {
5829 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5830 return NULL;
5832 if (view->searching == TOG_SEARCH_FORWARD)
5833 te = got_object_tree_get_first_entry(s->tree);
5834 else
5835 te = got_object_tree_get_last_entry(s->tree);
5838 if (match_tree_entry(te, &view->regex)) {
5839 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5840 s->matched_entry = te;
5841 break;
5844 if (view->searching == TOG_SEARCH_FORWARD)
5845 te = got_tree_entry_get_next(s->tree, te);
5846 else
5847 te = got_tree_entry_get_prev(s->tree, te);
5850 if (s->matched_entry) {
5851 s->first_displayed_entry = s->matched_entry;
5852 s->selected = 0;
5855 return NULL;
5858 static const struct got_error *
5859 show_tree_view(struct tog_view *view)
5861 const struct got_error *err = NULL;
5862 struct tog_tree_view_state *s = &view->state.tree;
5863 char *parent_path;
5865 err = tree_entry_path(&parent_path, &s->parents, NULL);
5866 if (err)
5867 return err;
5869 err = draw_tree_entries(view, parent_path);
5870 free(parent_path);
5872 view_vborder(view);
5873 return err;
5876 static const struct got_error *
5877 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5879 const struct got_error *err = NULL;
5880 struct tog_tree_view_state *s = &view->state.tree;
5881 struct tog_view *log_view, *ref_view;
5882 struct got_tree_entry *te;
5883 int begin_x = 0, n, nscroll = view->nlines - 3;
5885 switch (ch) {
5886 case 'i':
5887 s->show_ids = !s->show_ids;
5888 break;
5889 case 'l':
5890 if (!s->selected_entry)
5891 break;
5892 if (view_is_parent_view(view))
5893 begin_x = view_split_begin_x(view->begin_x);
5894 err = log_selected_tree_entry(&log_view, begin_x, s);
5895 view->focussed = 0;
5896 log_view->focussed = 1;
5897 if (view_is_parent_view(view)) {
5898 err = view_close_child(view);
5899 if (err)
5900 return err;
5901 err = view_set_child(view, log_view);
5902 if (err)
5903 return err;
5904 view->focus_child = 1;
5905 } else
5906 *new_view = log_view;
5907 break;
5908 case 'r':
5909 if (view_is_parent_view(view))
5910 begin_x = view_split_begin_x(view->begin_x);
5911 ref_view = view_open(view->nlines, view->ncols,
5912 view->begin_y, begin_x, TOG_VIEW_REF);
5913 if (ref_view == NULL)
5914 return got_error_from_errno("view_open");
5915 err = open_ref_view(ref_view, s->repo);
5916 if (err) {
5917 view_close(ref_view);
5918 return err;
5920 view->focussed = 0;
5921 ref_view->focussed = 1;
5922 if (view_is_parent_view(view)) {
5923 err = view_close_child(view);
5924 if (err)
5925 return err;
5926 err = view_set_child(view, ref_view);
5927 if (err)
5928 return err;
5929 view->focus_child = 1;
5930 } else
5931 *new_view = ref_view;
5932 break;
5933 case 'g':
5934 case KEY_HOME:
5935 s->selected = 0;
5936 if (s->tree == s->root)
5937 s->first_displayed_entry =
5938 got_object_tree_get_first_entry(s->tree);
5939 else
5940 s->first_displayed_entry = NULL;
5941 break;
5942 case 'G':
5943 case KEY_END:
5944 s->selected = 0;
5945 te = got_object_tree_get_last_entry(s->tree);
5946 for (n = 0; n < view->nlines - 3; n++) {
5947 if (te == NULL) {
5948 if(s->tree != s->root) {
5949 s->first_displayed_entry = NULL;
5950 n++;
5952 break;
5954 s->first_displayed_entry = te;
5955 te = got_tree_entry_get_prev(s->tree, te);
5957 if (n > 0)
5958 s->selected = n - 1;
5959 break;
5960 case 'k':
5961 case KEY_UP:
5962 case CTRL('p'):
5963 if (s->selected > 0) {
5964 s->selected--;
5965 break;
5967 tree_scroll_up(s, 1);
5968 break;
5969 case CTRL('u'):
5970 case 'u':
5971 nscroll /= 2;
5972 /* FALL THROUGH */
5973 case KEY_PPAGE:
5974 case CTRL('b'):
5975 case 'b':
5976 if (s->tree == s->root) {
5977 if (got_object_tree_get_first_entry(s->tree) ==
5978 s->first_displayed_entry)
5979 s->selected -= MIN(s->selected, nscroll);
5980 } else {
5981 if (s->first_displayed_entry == NULL)
5982 s->selected -= MIN(s->selected, nscroll);
5984 tree_scroll_up(s, MAX(0, nscroll));
5985 break;
5986 case 'j':
5987 case KEY_DOWN:
5988 case CTRL('n'):
5989 if (s->selected < s->ndisplayed - 1) {
5990 s->selected++;
5991 break;
5993 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5994 == NULL)
5995 /* can't scroll any further */
5996 break;
5997 tree_scroll_down(s, 1);
5998 break;
5999 case CTRL('d'):
6000 case 'd':
6001 nscroll /= 2;
6002 /* FALL THROUGH */
6003 case KEY_NPAGE:
6004 case CTRL('f'):
6005 case 'f':
6006 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6007 == NULL) {
6008 /* can't scroll any further; move cursor down */
6009 if (s->selected < s->ndisplayed - 1)
6010 s->selected += MIN(nscroll,
6011 s->ndisplayed - s->selected - 1);
6012 break;
6014 tree_scroll_down(s, nscroll);
6015 break;
6016 case KEY_ENTER:
6017 case '\r':
6018 case KEY_BACKSPACE:
6019 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6020 struct tog_parent_tree *parent;
6021 /* user selected '..' */
6022 if (s->tree == s->root)
6023 break;
6024 parent = TAILQ_FIRST(&s->parents);
6025 TAILQ_REMOVE(&s->parents, parent,
6026 entry);
6027 got_object_tree_close(s->tree);
6028 s->tree = parent->tree;
6029 s->first_displayed_entry =
6030 parent->first_displayed_entry;
6031 s->selected_entry =
6032 parent->selected_entry;
6033 s->selected = parent->selected;
6034 free(parent);
6035 } else if (S_ISDIR(got_tree_entry_get_mode(
6036 s->selected_entry))) {
6037 struct got_tree_object *subtree;
6038 err = got_object_open_as_tree(&subtree, s->repo,
6039 got_tree_entry_get_id(s->selected_entry));
6040 if (err)
6041 break;
6042 err = tree_view_visit_subtree(s, subtree);
6043 if (err) {
6044 got_object_tree_close(subtree);
6045 break;
6047 } else if (S_ISREG(got_tree_entry_get_mode(
6048 s->selected_entry))) {
6049 struct tog_view *blame_view;
6050 int begin_x = view_is_parent_view(view) ?
6051 view_split_begin_x(view->begin_x) : 0;
6053 err = blame_tree_entry(&blame_view, begin_x,
6054 s->selected_entry, &s->parents,
6055 s->commit_id, s->repo);
6056 if (err)
6057 break;
6058 view->focussed = 0;
6059 blame_view->focussed = 1;
6060 if (view_is_parent_view(view)) {
6061 err = view_close_child(view);
6062 if (err)
6063 return err;
6064 err = view_set_child(view, blame_view);
6065 if (err)
6066 return err;
6067 view->focus_child = 1;
6068 } else
6069 *new_view = blame_view;
6071 break;
6072 case KEY_RESIZE:
6073 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6074 s->selected = view->nlines - 4;
6075 break;
6076 default:
6077 break;
6080 return err;
6083 __dead static void
6084 usage_tree(void)
6086 endwin();
6087 fprintf(stderr,
6088 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6089 getprogname());
6090 exit(1);
6093 static const struct got_error *
6094 cmd_tree(int argc, char *argv[])
6096 const struct got_error *error;
6097 struct got_repository *repo = NULL;
6098 struct got_worktree *worktree = NULL;
6099 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6100 struct got_object_id *commit_id = NULL;
6101 struct got_commit_object *commit = NULL;
6102 const char *commit_id_arg = NULL;
6103 char *label = NULL;
6104 struct got_reference *ref = NULL;
6105 const char *head_ref_name = NULL;
6106 int ch;
6107 struct tog_view *view;
6108 int *pack_fds = NULL;
6110 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6111 switch (ch) {
6112 case 'c':
6113 commit_id_arg = optarg;
6114 break;
6115 case 'r':
6116 repo_path = realpath(optarg, NULL);
6117 if (repo_path == NULL)
6118 return got_error_from_errno2("realpath",
6119 optarg);
6120 break;
6121 default:
6122 usage_tree();
6123 /* NOTREACHED */
6127 argc -= optind;
6128 argv += optind;
6130 if (argc > 1)
6131 usage_tree();
6133 error = got_repo_pack_fds_open(&pack_fds);
6134 if (error != NULL)
6135 goto done;
6137 if (repo_path == NULL) {
6138 cwd = getcwd(NULL, 0);
6139 if (cwd == NULL)
6140 return got_error_from_errno("getcwd");
6141 error = got_worktree_open(&worktree, cwd);
6142 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6143 goto done;
6144 if (worktree)
6145 repo_path =
6146 strdup(got_worktree_get_repo_path(worktree));
6147 else
6148 repo_path = strdup(cwd);
6149 if (repo_path == NULL) {
6150 error = got_error_from_errno("strdup");
6151 goto done;
6155 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6156 if (error != NULL)
6157 goto done;
6159 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6160 repo, worktree);
6161 if (error)
6162 goto done;
6164 init_curses();
6166 error = apply_unveil(got_repo_get_path(repo), NULL);
6167 if (error)
6168 goto done;
6170 error = tog_load_refs(repo, 0);
6171 if (error)
6172 goto done;
6174 if (commit_id_arg == NULL) {
6175 error = got_repo_match_object_id(&commit_id, &label,
6176 worktree ? got_worktree_get_head_ref_name(worktree) :
6177 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6178 if (error)
6179 goto done;
6180 head_ref_name = label;
6181 } else {
6182 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6183 if (error == NULL)
6184 head_ref_name = got_ref_get_name(ref);
6185 else if (error->code != GOT_ERR_NOT_REF)
6186 goto done;
6187 error = got_repo_match_object_id(&commit_id, NULL,
6188 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6189 if (error)
6190 goto done;
6193 error = got_object_open_as_commit(&commit, repo, commit_id);
6194 if (error)
6195 goto done;
6197 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6198 if (view == NULL) {
6199 error = got_error_from_errno("view_open");
6200 goto done;
6202 error = open_tree_view(view, commit_id, head_ref_name, repo);
6203 if (error)
6204 goto done;
6205 if (!got_path_is_root_dir(in_repo_path)) {
6206 error = tree_view_walk_path(&view->state.tree, commit,
6207 in_repo_path);
6208 if (error)
6209 goto done;
6212 if (worktree) {
6213 /* Release work tree lock. */
6214 got_worktree_close(worktree);
6215 worktree = NULL;
6217 error = view_loop(view);
6218 done:
6219 free(repo_path);
6220 free(cwd);
6221 free(commit_id);
6222 free(label);
6223 if (ref)
6224 got_ref_close(ref);
6225 if (repo) {
6226 const struct got_error *close_err = got_repo_close(repo);
6227 if (error == NULL)
6228 error = close_err;
6230 if (pack_fds) {
6231 const struct got_error *pack_err =
6232 got_repo_pack_fds_close(pack_fds);
6233 if (error == NULL)
6234 error = pack_err;
6236 tog_free_refs();
6237 return error;
6240 static const struct got_error *
6241 ref_view_load_refs(struct tog_ref_view_state *s)
6243 struct got_reflist_entry *sre;
6244 struct tog_reflist_entry *re;
6246 s->nrefs = 0;
6247 TAILQ_FOREACH(sre, &tog_refs, entry) {
6248 if (strncmp(got_ref_get_name(sre->ref),
6249 "refs/got/", 9) == 0 &&
6250 strncmp(got_ref_get_name(sre->ref),
6251 "refs/got/backup/", 16) != 0)
6252 continue;
6254 re = malloc(sizeof(*re));
6255 if (re == NULL)
6256 return got_error_from_errno("malloc");
6258 re->ref = got_ref_dup(sre->ref);
6259 if (re->ref == NULL)
6260 return got_error_from_errno("got_ref_dup");
6261 re->idx = s->nrefs++;
6262 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6265 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6266 return NULL;
6269 void
6270 ref_view_free_refs(struct tog_ref_view_state *s)
6272 struct tog_reflist_entry *re;
6274 while (!TAILQ_EMPTY(&s->refs)) {
6275 re = TAILQ_FIRST(&s->refs);
6276 TAILQ_REMOVE(&s->refs, re, entry);
6277 got_ref_close(re->ref);
6278 free(re);
6282 static const struct got_error *
6283 open_ref_view(struct tog_view *view, struct got_repository *repo)
6285 const struct got_error *err = NULL;
6286 struct tog_ref_view_state *s = &view->state.ref;
6288 s->selected_entry = 0;
6289 s->repo = repo;
6291 TAILQ_INIT(&s->refs);
6292 STAILQ_INIT(&s->colors);
6294 err = ref_view_load_refs(s);
6295 if (err)
6296 return err;
6298 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6299 err = add_color(&s->colors, "^refs/heads/",
6300 TOG_COLOR_REFS_HEADS,
6301 get_color_value("TOG_COLOR_REFS_HEADS"));
6302 if (err)
6303 goto done;
6305 err = add_color(&s->colors, "^refs/tags/",
6306 TOG_COLOR_REFS_TAGS,
6307 get_color_value("TOG_COLOR_REFS_TAGS"));
6308 if (err)
6309 goto done;
6311 err = add_color(&s->colors, "^refs/remotes/",
6312 TOG_COLOR_REFS_REMOTES,
6313 get_color_value("TOG_COLOR_REFS_REMOTES"));
6314 if (err)
6315 goto done;
6317 err = add_color(&s->colors, "^refs/got/backup/",
6318 TOG_COLOR_REFS_BACKUP,
6319 get_color_value("TOG_COLOR_REFS_BACKUP"));
6320 if (err)
6321 goto done;
6324 view->show = show_ref_view;
6325 view->input = input_ref_view;
6326 view->close = close_ref_view;
6327 view->search_start = search_start_ref_view;
6328 view->search_next = search_next_ref_view;
6329 done:
6330 if (err)
6331 free_colors(&s->colors);
6332 return err;
6335 static const struct got_error *
6336 close_ref_view(struct tog_view *view)
6338 struct tog_ref_view_state *s = &view->state.ref;
6340 ref_view_free_refs(s);
6341 free_colors(&s->colors);
6343 return NULL;
6346 static const struct got_error *
6347 resolve_reflist_entry(struct got_object_id **commit_id,
6348 struct tog_reflist_entry *re, struct got_repository *repo)
6350 const struct got_error *err = NULL;
6351 struct got_object_id *obj_id;
6352 struct got_tag_object *tag = NULL;
6353 int obj_type;
6355 *commit_id = NULL;
6357 err = got_ref_resolve(&obj_id, repo, re->ref);
6358 if (err)
6359 return err;
6361 err = got_object_get_type(&obj_type, repo, obj_id);
6362 if (err)
6363 goto done;
6365 switch (obj_type) {
6366 case GOT_OBJ_TYPE_COMMIT:
6367 *commit_id = obj_id;
6368 break;
6369 case GOT_OBJ_TYPE_TAG:
6370 err = got_object_open_as_tag(&tag, repo, obj_id);
6371 if (err)
6372 goto done;
6373 free(obj_id);
6374 err = got_object_get_type(&obj_type, repo,
6375 got_object_tag_get_object_id(tag));
6376 if (err)
6377 goto done;
6378 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6379 err = got_error(GOT_ERR_OBJ_TYPE);
6380 goto done;
6382 *commit_id = got_object_id_dup(
6383 got_object_tag_get_object_id(tag));
6384 if (*commit_id == NULL) {
6385 err = got_error_from_errno("got_object_id_dup");
6386 goto done;
6388 break;
6389 default:
6390 err = got_error(GOT_ERR_OBJ_TYPE);
6391 break;
6394 done:
6395 if (tag)
6396 got_object_tag_close(tag);
6397 if (err) {
6398 free(*commit_id);
6399 *commit_id = NULL;
6401 return err;
6404 static const struct got_error *
6405 log_ref_entry(struct tog_view **new_view, int begin_x,
6406 struct tog_reflist_entry *re, struct got_repository *repo)
6408 struct tog_view *log_view;
6409 const struct got_error *err = NULL;
6410 struct got_object_id *commit_id = NULL;
6412 *new_view = NULL;
6414 err = resolve_reflist_entry(&commit_id, re, repo);
6415 if (err) {
6416 if (err->code != GOT_ERR_OBJ_TYPE)
6417 return err;
6418 else
6419 return NULL;
6422 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6423 if (log_view == NULL) {
6424 err = got_error_from_errno("view_open");
6425 goto done;
6428 err = open_log_view(log_view, commit_id, repo,
6429 got_ref_get_name(re->ref), "", 0);
6430 done:
6431 if (err)
6432 view_close(log_view);
6433 else
6434 *new_view = log_view;
6435 free(commit_id);
6436 return err;
6439 static void
6440 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6442 struct tog_reflist_entry *re;
6443 int i = 0;
6445 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6446 return;
6448 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6449 while (i++ < maxscroll) {
6450 if (re == NULL)
6451 break;
6452 s->first_displayed_entry = re;
6453 re = TAILQ_PREV(re, tog_reflist_head, entry);
6457 static void
6458 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6460 struct tog_reflist_entry *next, *last;
6461 int n = 0;
6463 if (s->first_displayed_entry)
6464 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6465 else
6466 next = TAILQ_FIRST(&s->refs);
6468 last = s->last_displayed_entry;
6469 while (next && last && n++ < maxscroll) {
6470 last = TAILQ_NEXT(last, entry);
6471 if (last) {
6472 s->first_displayed_entry = next;
6473 next = TAILQ_NEXT(next, entry);
6478 static const struct got_error *
6479 search_start_ref_view(struct tog_view *view)
6481 struct tog_ref_view_state *s = &view->state.ref;
6483 s->matched_entry = NULL;
6484 return NULL;
6487 static int
6488 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6490 regmatch_t regmatch;
6492 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6493 0) == 0;
6496 static const struct got_error *
6497 search_next_ref_view(struct tog_view *view)
6499 struct tog_ref_view_state *s = &view->state.ref;
6500 struct tog_reflist_entry *re = NULL;
6502 if (!view->searching) {
6503 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6504 return NULL;
6507 if (s->matched_entry) {
6508 if (view->searching == TOG_SEARCH_FORWARD) {
6509 if (s->selected_entry)
6510 re = TAILQ_NEXT(s->selected_entry, entry);
6511 else
6512 re = TAILQ_PREV(s->selected_entry,
6513 tog_reflist_head, entry);
6514 } else {
6515 if (s->selected_entry == NULL)
6516 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6517 else
6518 re = TAILQ_PREV(s->selected_entry,
6519 tog_reflist_head, entry);
6521 } else {
6522 if (s->selected_entry)
6523 re = s->selected_entry;
6524 else if (view->searching == TOG_SEARCH_FORWARD)
6525 re = TAILQ_FIRST(&s->refs);
6526 else
6527 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6530 while (1) {
6531 if (re == NULL) {
6532 if (s->matched_entry == NULL) {
6533 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6534 return NULL;
6536 if (view->searching == TOG_SEARCH_FORWARD)
6537 re = TAILQ_FIRST(&s->refs);
6538 else
6539 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6542 if (match_reflist_entry(re, &view->regex)) {
6543 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6544 s->matched_entry = re;
6545 break;
6548 if (view->searching == TOG_SEARCH_FORWARD)
6549 re = TAILQ_NEXT(re, entry);
6550 else
6551 re = TAILQ_PREV(re, tog_reflist_head, entry);
6554 if (s->matched_entry) {
6555 s->first_displayed_entry = s->matched_entry;
6556 s->selected = 0;
6559 return NULL;
6562 static const struct got_error *
6563 show_ref_view(struct tog_view *view)
6565 const struct got_error *err = NULL;
6566 struct tog_ref_view_state *s = &view->state.ref;
6567 struct tog_reflist_entry *re;
6568 char *line = NULL;
6569 wchar_t *wline;
6570 struct tog_color *tc;
6571 int width, n;
6572 int limit = view->nlines;
6574 werase(view->window);
6576 s->ndisplayed = 0;
6578 if (limit == 0)
6579 return NULL;
6581 re = s->first_displayed_entry;
6583 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6584 s->nrefs) == -1)
6585 return got_error_from_errno("asprintf");
6587 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6588 if (err) {
6589 free(line);
6590 return err;
6592 if (view_needs_focus_indication(view))
6593 wstandout(view->window);
6594 waddwstr(view->window, wline);
6595 if (view_needs_focus_indication(view))
6596 wstandend(view->window);
6597 free(wline);
6598 wline = NULL;
6599 free(line);
6600 line = NULL;
6601 if (width < view->ncols - 1)
6602 waddch(view->window, '\n');
6603 if (--limit <= 0)
6604 return NULL;
6606 n = 0;
6607 while (re && limit > 0) {
6608 char *line = NULL;
6609 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6611 if (s->show_date) {
6612 struct got_commit_object *ci;
6613 struct got_tag_object *tag;
6614 struct got_object_id *id;
6615 struct tm tm;
6616 time_t t;
6618 err = got_ref_resolve(&id, s->repo, re->ref);
6619 if (err)
6620 return err;
6621 err = got_object_open_as_tag(&tag, s->repo, id);
6622 if (err) {
6623 if (err->code != GOT_ERR_OBJ_TYPE) {
6624 free(id);
6625 return err;
6627 err = got_object_open_as_commit(&ci, s->repo,
6628 id);
6629 if (err) {
6630 free(id);
6631 return err;
6633 t = got_object_commit_get_committer_time(ci);
6634 got_object_commit_close(ci);
6635 } else {
6636 t = got_object_tag_get_tagger_time(tag);
6637 got_object_tag_close(tag);
6639 free(id);
6640 if (gmtime_r(&t, &tm) == NULL)
6641 return got_error_from_errno("gmtime_r");
6642 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6643 return got_error(GOT_ERR_NO_SPACE);
6645 if (got_ref_is_symbolic(re->ref)) {
6646 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6647 ymd : "", got_ref_get_name(re->ref),
6648 got_ref_get_symref_target(re->ref)) == -1)
6649 return got_error_from_errno("asprintf");
6650 } else if (s->show_ids) {
6651 struct got_object_id *id;
6652 char *id_str;
6653 err = got_ref_resolve(&id, s->repo, re->ref);
6654 if (err)
6655 return err;
6656 err = got_object_id_str(&id_str, id);
6657 if (err) {
6658 free(id);
6659 return err;
6661 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6662 got_ref_get_name(re->ref), id_str) == -1) {
6663 err = got_error_from_errno("asprintf");
6664 free(id);
6665 free(id_str);
6666 return err;
6668 free(id);
6669 free(id_str);
6670 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6671 got_ref_get_name(re->ref)) == -1)
6672 return got_error_from_errno("asprintf");
6674 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6675 0, 0);
6676 if (err) {
6677 free(line);
6678 return err;
6680 if (n == s->selected) {
6681 if (view->focussed)
6682 wstandout(view->window);
6683 s->selected_entry = re;
6685 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6686 if (tc)
6687 wattr_on(view->window,
6688 COLOR_PAIR(tc->colorpair), NULL);
6689 waddwstr(view->window, wline);
6690 if (tc)
6691 wattr_off(view->window,
6692 COLOR_PAIR(tc->colorpair), NULL);
6693 if (width < view->ncols - 1)
6694 waddch(view->window, '\n');
6695 if (n == s->selected && view->focussed)
6696 wstandend(view->window);
6697 free(line);
6698 free(wline);
6699 wline = NULL;
6700 n++;
6701 s->ndisplayed++;
6702 s->last_displayed_entry = re;
6704 limit--;
6705 re = TAILQ_NEXT(re, entry);
6708 view_vborder(view);
6709 return err;
6712 static const struct got_error *
6713 browse_ref_tree(struct tog_view **new_view, int begin_x,
6714 struct tog_reflist_entry *re, struct got_repository *repo)
6716 const struct got_error *err = NULL;
6717 struct got_object_id *commit_id = NULL;
6718 struct tog_view *tree_view;
6720 *new_view = NULL;
6722 err = resolve_reflist_entry(&commit_id, re, repo);
6723 if (err) {
6724 if (err->code != GOT_ERR_OBJ_TYPE)
6725 return err;
6726 else
6727 return NULL;
6731 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6732 if (tree_view == NULL) {
6733 err = got_error_from_errno("view_open");
6734 goto done;
6737 err = open_tree_view(tree_view, commit_id,
6738 got_ref_get_name(re->ref), repo);
6739 if (err)
6740 goto done;
6742 *new_view = tree_view;
6743 done:
6744 free(commit_id);
6745 return err;
6747 static const struct got_error *
6748 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6750 const struct got_error *err = NULL;
6751 struct tog_ref_view_state *s = &view->state.ref;
6752 struct tog_view *log_view, *tree_view;
6753 struct tog_reflist_entry *re;
6754 int begin_x = 0, n, nscroll = view->nlines - 1;
6756 switch (ch) {
6757 case 'i':
6758 s->show_ids = !s->show_ids;
6759 break;
6760 case 'm':
6761 s->show_date = !s->show_date;
6762 break;
6763 case 'o':
6764 s->sort_by_date = !s->sort_by_date;
6765 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6766 got_ref_cmp_by_commit_timestamp_descending :
6767 tog_ref_cmp_by_name, s->repo);
6768 if (err)
6769 break;
6770 got_reflist_object_id_map_free(tog_refs_idmap);
6771 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6772 &tog_refs, s->repo);
6773 if (err)
6774 break;
6775 ref_view_free_refs(s);
6776 err = ref_view_load_refs(s);
6777 break;
6778 case KEY_ENTER:
6779 case '\r':
6780 if (!s->selected_entry)
6781 break;
6782 if (view_is_parent_view(view))
6783 begin_x = view_split_begin_x(view->begin_x);
6784 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6785 s->repo);
6786 view->focussed = 0;
6787 log_view->focussed = 1;
6788 if (view_is_parent_view(view)) {
6789 err = view_close_child(view);
6790 if (err)
6791 return err;
6792 err = view_set_child(view, log_view);
6793 if (err)
6794 return err;
6795 view->focus_child = 1;
6796 } else
6797 *new_view = log_view;
6798 break;
6799 case 't':
6800 if (!s->selected_entry)
6801 break;
6802 if (view_is_parent_view(view))
6803 begin_x = view_split_begin_x(view->begin_x);
6804 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6805 s->repo);
6806 if (err || tree_view == NULL)
6807 break;
6808 view->focussed = 0;
6809 tree_view->focussed = 1;
6810 if (view_is_parent_view(view)) {
6811 err = view_close_child(view);
6812 if (err)
6813 return err;
6814 err = view_set_child(view, tree_view);
6815 if (err)
6816 return err;
6817 view->focus_child = 1;
6818 } else
6819 *new_view = tree_view;
6820 break;
6821 case 'g':
6822 case KEY_HOME:
6823 s->selected = 0;
6824 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6825 break;
6826 case 'G':
6827 case KEY_END:
6828 s->selected = 0;
6829 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6830 for (n = 0; n < view->nlines - 1; n++) {
6831 if (re == NULL)
6832 break;
6833 s->first_displayed_entry = re;
6834 re = TAILQ_PREV(re, tog_reflist_head, entry);
6836 if (n > 0)
6837 s->selected = n - 1;
6838 break;
6839 case 'k':
6840 case KEY_UP:
6841 case CTRL('p'):
6842 if (s->selected > 0) {
6843 s->selected--;
6844 break;
6846 ref_scroll_up(s, 1);
6847 break;
6848 case CTRL('u'):
6849 case 'u':
6850 nscroll /= 2;
6851 /* FALL THROUGH */
6852 case KEY_PPAGE:
6853 case CTRL('b'):
6854 case 'b':
6855 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6856 s->selected -= MIN(nscroll, s->selected);
6857 ref_scroll_up(s, MAX(0, nscroll));
6858 break;
6859 case 'j':
6860 case KEY_DOWN:
6861 case CTRL('n'):
6862 if (s->selected < s->ndisplayed - 1) {
6863 s->selected++;
6864 break;
6866 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6867 /* can't scroll any further */
6868 break;
6869 ref_scroll_down(s, 1);
6870 break;
6871 case CTRL('d'):
6872 case 'd':
6873 nscroll /= 2;
6874 /* FALL THROUGH */
6875 case KEY_NPAGE:
6876 case CTRL('f'):
6877 case 'f':
6878 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6879 /* can't scroll any further; move cursor down */
6880 if (s->selected < s->ndisplayed - 1)
6881 s->selected += MIN(nscroll,
6882 s->ndisplayed - s->selected - 1);
6883 break;
6885 ref_scroll_down(s, nscroll);
6886 break;
6887 case CTRL('l'):
6888 tog_free_refs();
6889 err = tog_load_refs(s->repo, s->sort_by_date);
6890 if (err)
6891 break;
6892 ref_view_free_refs(s);
6893 err = ref_view_load_refs(s);
6894 break;
6895 case KEY_RESIZE:
6896 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6897 s->selected = view->nlines - 2;
6898 break;
6899 default:
6900 break;
6903 return err;
6906 __dead static void
6907 usage_ref(void)
6909 endwin();
6910 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6911 getprogname());
6912 exit(1);
6915 static const struct got_error *
6916 cmd_ref(int argc, char *argv[])
6918 const struct got_error *error;
6919 struct got_repository *repo = NULL;
6920 struct got_worktree *worktree = NULL;
6921 char *cwd = NULL, *repo_path = NULL;
6922 int ch;
6923 struct tog_view *view;
6924 int *pack_fds = NULL;
6926 while ((ch = getopt(argc, argv, "r:")) != -1) {
6927 switch (ch) {
6928 case 'r':
6929 repo_path = realpath(optarg, NULL);
6930 if (repo_path == NULL)
6931 return got_error_from_errno2("realpath",
6932 optarg);
6933 break;
6934 default:
6935 usage_ref();
6936 /* NOTREACHED */
6940 argc -= optind;
6941 argv += optind;
6943 if (argc > 1)
6944 usage_ref();
6946 error = got_repo_pack_fds_open(&pack_fds);
6947 if (error != NULL)
6948 goto done;
6950 if (repo_path == NULL) {
6951 cwd = getcwd(NULL, 0);
6952 if (cwd == NULL)
6953 return got_error_from_errno("getcwd");
6954 error = got_worktree_open(&worktree, cwd);
6955 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6956 goto done;
6957 if (worktree)
6958 repo_path =
6959 strdup(got_worktree_get_repo_path(worktree));
6960 else
6961 repo_path = strdup(cwd);
6962 if (repo_path == NULL) {
6963 error = got_error_from_errno("strdup");
6964 goto done;
6968 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6969 if (error != NULL)
6970 goto done;
6972 init_curses();
6974 error = apply_unveil(got_repo_get_path(repo), NULL);
6975 if (error)
6976 goto done;
6978 error = tog_load_refs(repo, 0);
6979 if (error)
6980 goto done;
6982 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6983 if (view == NULL) {
6984 error = got_error_from_errno("view_open");
6985 goto done;
6988 error = open_ref_view(view, repo);
6989 if (error)
6990 goto done;
6992 if (worktree) {
6993 /* Release work tree lock. */
6994 got_worktree_close(worktree);
6995 worktree = NULL;
6997 error = view_loop(view);
6998 done:
6999 free(repo_path);
7000 free(cwd);
7001 if (repo) {
7002 const struct got_error *close_err = got_repo_close(repo);
7003 if (close_err)
7004 error = close_err;
7006 if (pack_fds) {
7007 const struct got_error *pack_err =
7008 got_repo_pack_fds_close(pack_fds);
7009 if (error == NULL)
7010 error = pack_err;
7012 tog_free_refs();
7013 return error;
7016 static void
7017 list_commands(FILE *fp)
7019 size_t i;
7021 fprintf(fp, "commands:");
7022 for (i = 0; i < nitems(tog_commands); i++) {
7023 const struct tog_cmd *cmd = &tog_commands[i];
7024 fprintf(fp, " %s", cmd->name);
7026 fputc('\n', fp);
7029 __dead static void
7030 usage(int hflag, int status)
7032 FILE *fp = (status == 0) ? stdout : stderr;
7034 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7035 getprogname());
7036 if (hflag) {
7037 fprintf(fp, "lazy usage: %s path\n", getprogname());
7038 list_commands(fp);
7040 exit(status);
7043 static char **
7044 make_argv(int argc, ...)
7046 va_list ap;
7047 char **argv;
7048 int i;
7050 va_start(ap, argc);
7052 argv = calloc(argc, sizeof(char *));
7053 if (argv == NULL)
7054 err(1, "calloc");
7055 for (i = 0; i < argc; i++) {
7056 argv[i] = strdup(va_arg(ap, char *));
7057 if (argv[i] == NULL)
7058 err(1, "strdup");
7061 va_end(ap);
7062 return argv;
7066 * Try to convert 'tog path' into a 'tog log path' command.
7067 * The user could simply have mistyped the command rather than knowingly
7068 * provided a path. So check whether argv[0] can in fact be resolved
7069 * to a path in the HEAD commit and print a special error if not.
7070 * This hack is for mpi@ <3
7072 static const struct got_error *
7073 tog_log_with_path(int argc, char *argv[])
7075 const struct got_error *error = NULL, *close_err;
7076 const struct tog_cmd *cmd = NULL;
7077 struct got_repository *repo = NULL;
7078 struct got_worktree *worktree = NULL;
7079 struct got_object_id *commit_id = NULL, *id = NULL;
7080 struct got_commit_object *commit = NULL;
7081 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7082 char *commit_id_str = NULL, **cmd_argv = NULL;
7083 int *pack_fds = NULL;
7085 cwd = getcwd(NULL, 0);
7086 if (cwd == NULL)
7087 return got_error_from_errno("getcwd");
7089 error = got_repo_pack_fds_open(&pack_fds);
7090 if (error != NULL)
7091 goto done;
7093 error = got_worktree_open(&worktree, cwd);
7094 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7095 goto done;
7097 if (worktree)
7098 repo_path = strdup(got_worktree_get_repo_path(worktree));
7099 else
7100 repo_path = strdup(cwd);
7101 if (repo_path == NULL) {
7102 error = got_error_from_errno("strdup");
7103 goto done;
7106 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7107 if (error != NULL)
7108 goto done;
7110 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7111 repo, worktree);
7112 if (error)
7113 goto done;
7115 error = tog_load_refs(repo, 0);
7116 if (error)
7117 goto done;
7118 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7119 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7120 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7121 if (error)
7122 goto done;
7124 if (worktree) {
7125 got_worktree_close(worktree);
7126 worktree = NULL;
7129 error = got_object_open_as_commit(&commit, repo, commit_id);
7130 if (error)
7131 goto done;
7133 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7134 if (error) {
7135 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7136 goto done;
7137 fprintf(stderr, "%s: '%s' is no known command or path\n",
7138 getprogname(), argv[0]);
7139 usage(1, 1);
7140 /* not reached */
7143 close_err = got_repo_close(repo);
7144 if (error == NULL)
7145 error = close_err;
7146 repo = NULL;
7148 error = got_object_id_str(&commit_id_str, commit_id);
7149 if (error)
7150 goto done;
7152 cmd = &tog_commands[0]; /* log */
7153 argc = 4;
7154 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7155 error = cmd->cmd_main(argc, cmd_argv);
7156 done:
7157 if (repo) {
7158 close_err = got_repo_close(repo);
7159 if (error == NULL)
7160 error = close_err;
7162 if (commit)
7163 got_object_commit_close(commit);
7164 if (worktree)
7165 got_worktree_close(worktree);
7166 if (pack_fds) {
7167 const struct got_error *pack_err =
7168 got_repo_pack_fds_close(pack_fds);
7169 if (error == NULL)
7170 error = pack_err;
7172 free(id);
7173 free(commit_id_str);
7174 free(commit_id);
7175 free(cwd);
7176 free(repo_path);
7177 free(in_repo_path);
7178 if (cmd_argv) {
7179 int i;
7180 for (i = 0; i < argc; i++)
7181 free(cmd_argv[i]);
7182 free(cmd_argv);
7184 tog_free_refs();
7185 return error;
7188 int
7189 main(int argc, char *argv[])
7191 const struct got_error *error = NULL;
7192 const struct tog_cmd *cmd = NULL;
7193 int ch, hflag = 0, Vflag = 0;
7194 char **cmd_argv = NULL;
7195 static const struct option longopts[] = {
7196 { "version", no_argument, NULL, 'V' },
7197 { NULL, 0, NULL, 0}
7200 setlocale(LC_CTYPE, "");
7202 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7203 switch (ch) {
7204 case 'h':
7205 hflag = 1;
7206 break;
7207 case 'V':
7208 Vflag = 1;
7209 break;
7210 default:
7211 usage(hflag, 1);
7212 /* NOTREACHED */
7216 argc -= optind;
7217 argv += optind;
7218 optind = 1;
7219 optreset = 1;
7221 if (Vflag) {
7222 got_version_print_str();
7223 return 0;
7226 #ifndef PROFILE
7227 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7228 NULL) == -1)
7229 err(1, "pledge");
7230 #endif
7232 if (argc == 0) {
7233 if (hflag)
7234 usage(hflag, 0);
7235 /* Build an argument vector which runs a default command. */
7236 cmd = &tog_commands[0];
7237 argc = 1;
7238 cmd_argv = make_argv(argc, cmd->name);
7239 } else {
7240 size_t i;
7242 /* Did the user specify a command? */
7243 for (i = 0; i < nitems(tog_commands); i++) {
7244 if (strncmp(tog_commands[i].name, argv[0],
7245 strlen(argv[0])) == 0) {
7246 cmd = &tog_commands[i];
7247 break;
7252 if (cmd == NULL) {
7253 if (argc != 1)
7254 usage(0, 1);
7255 /* No command specified; try log with a path */
7256 error = tog_log_with_path(argc, argv);
7257 } else {
7258 if (hflag)
7259 cmd->cmd_usage();
7260 else
7261 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7264 endwin();
7265 putchar('\n');
7266 if (cmd_argv) {
7267 int i;
7268 for (i = 0; i < argc; i++)
7269 free(cmd_argv[i]);
7270 free(cmd_argv);
7273 if (error && error->code != GOT_ERR_CANCELLED)
7274 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7275 return 0;