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 log_complete;
342 sig_atomic_t *quit;
343 struct commit_queue_entry **first_displayed_entry;
344 struct commit_queue_entry **selected_entry;
345 int *searching;
346 int *search_next_done;
347 regex_t *regex;
348 };
350 struct tog_log_view_state {
351 struct commit_queue commits;
352 struct commit_queue_entry *first_displayed_entry;
353 struct commit_queue_entry *last_displayed_entry;
354 struct commit_queue_entry *selected_entry;
355 int selected;
356 char *in_repo_path;
357 char *head_ref_name;
358 int log_branches;
359 struct got_repository *repo;
360 struct got_object_id *start_id;
361 sig_atomic_t quit;
362 pthread_t thread;
363 struct tog_log_thread_args thread_args;
364 struct commit_queue_entry *matched_entry;
365 struct commit_queue_entry *search_entry;
366 struct tog_colors colors;
367 };
369 #define TOG_COLOR_DIFF_MINUS 1
370 #define TOG_COLOR_DIFF_PLUS 2
371 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
372 #define TOG_COLOR_DIFF_META 4
373 #define TOG_COLOR_TREE_SUBMODULE 5
374 #define TOG_COLOR_TREE_SYMLINK 6
375 #define TOG_COLOR_TREE_DIRECTORY 7
376 #define TOG_COLOR_TREE_EXECUTABLE 8
377 #define TOG_COLOR_COMMIT 9
378 #define TOG_COLOR_AUTHOR 10
379 #define TOG_COLOR_DATE 11
380 #define TOG_COLOR_REFS_HEADS 12
381 #define TOG_COLOR_REFS_TAGS 13
382 #define TOG_COLOR_REFS_REMOTES 14
383 #define TOG_COLOR_REFS_BACKUP 15
385 struct tog_blame_cb_args {
386 struct tog_blame_line *lines; /* one per line */
387 int nlines;
389 struct tog_view *view;
390 struct got_object_id *commit_id;
391 int *quit;
392 };
394 struct tog_blame_thread_args {
395 const char *path;
396 struct got_repository *repo;
397 struct tog_blame_cb_args *cb_args;
398 int *complete;
399 got_cancel_cb cancel_cb;
400 void *cancel_arg;
401 };
403 struct tog_blame {
404 FILE *f;
405 off_t filesize;
406 struct tog_blame_line *lines;
407 int nlines;
408 off_t *line_offsets;
409 pthread_t thread;
410 struct tog_blame_thread_args thread_args;
411 struct tog_blame_cb_args cb_args;
412 const char *path;
413 int *pack_fds;
414 };
416 struct tog_blame_view_state {
417 int first_displayed_line;
418 int last_displayed_line;
419 int selected_line;
420 int blame_complete;
421 int eof;
422 int done;
423 struct got_object_id_queue blamed_commits;
424 struct got_object_qid *blamed_commit;
425 char *path;
426 struct got_repository *repo;
427 struct got_object_id *commit_id;
428 struct tog_blame blame;
429 int matched_line;
430 struct tog_colors colors;
431 };
433 struct tog_parent_tree {
434 TAILQ_ENTRY(tog_parent_tree) entry;
435 struct got_tree_object *tree;
436 struct got_tree_entry *first_displayed_entry;
437 struct got_tree_entry *selected_entry;
438 int selected;
439 };
441 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
443 struct tog_tree_view_state {
444 char *tree_label;
445 struct got_object_id *commit_id;/* commit which this tree belongs to */
446 struct got_tree_object *root; /* the commit's root tree entry */
447 struct got_tree_object *tree; /* currently displayed (sub-)tree */
448 struct got_tree_entry *first_displayed_entry;
449 struct got_tree_entry *last_displayed_entry;
450 struct got_tree_entry *selected_entry;
451 int ndisplayed, selected, show_ids;
452 struct tog_parent_trees parents; /* parent trees of current sub-tree */
453 char *head_ref_name;
454 struct got_repository *repo;
455 struct got_tree_entry *matched_entry;
456 struct tog_colors colors;
457 };
459 struct tog_reflist_entry {
460 TAILQ_ENTRY(tog_reflist_entry) entry;
461 struct got_reference *ref;
462 int idx;
463 };
465 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
467 struct tog_ref_view_state {
468 struct tog_reflist_head refs;
469 struct tog_reflist_entry *first_displayed_entry;
470 struct tog_reflist_entry *last_displayed_entry;
471 struct tog_reflist_entry *selected_entry;
472 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
473 struct got_repository *repo;
474 struct tog_reflist_entry *matched_entry;
475 struct tog_colors colors;
476 };
478 /*
479 * We implement two types of views: parent views and child views.
481 * The 'Tab' key switches focus between a parent view and its child view.
482 * Child views are shown side-by-side to their parent view, provided
483 * there is enough screen estate.
485 * When a new view is opened from within a parent view, this new view
486 * becomes a child view of the parent view, replacing any existing child.
488 * When a new view is opened from within a child view, this new view
489 * becomes a parent view which will obscure the views below until the
490 * user quits the new parent view by typing 'q'.
492 * This list of views contains parent views only.
493 * Child views are only pointed to by their parent view.
494 */
495 TAILQ_HEAD(tog_view_list_head, tog_view);
497 struct tog_view {
498 TAILQ_ENTRY(tog_view) entry;
499 WINDOW *window;
500 PANEL *panel;
501 int nlines, ncols, begin_y, begin_x;
502 int lines, cols; /* copies of LINES and COLS */
503 int focussed; /* Only set on one parent or child view at a time. */
504 int dying;
505 struct tog_view *parent;
506 struct tog_view *child;
508 /*
509 * This flag is initially set on parent views when a new child view
510 * is created. It gets toggled when the 'Tab' key switches focus
511 * between parent and child.
512 * The flag indicates whether focus should be passed on to our child
513 * view if this parent view gets picked for focus after another parent
514 * view was closed. This prevents child views from losing focus in such
515 * situations.
516 */
517 int focus_child;
519 /* type-specific state */
520 enum tog_view_type type;
521 union {
522 struct tog_diff_view_state diff;
523 struct tog_log_view_state log;
524 struct tog_blame_view_state blame;
525 struct tog_tree_view_state tree;
526 struct tog_ref_view_state ref;
527 } state;
529 const struct got_error *(*show)(struct tog_view *);
530 const struct got_error *(*input)(struct tog_view **,
531 struct tog_view *, int);
532 const struct got_error *(*close)(struct tog_view *);
534 const struct got_error *(*search_start)(struct tog_view *);
535 const struct got_error *(*search_next)(struct tog_view *);
536 int search_started;
537 int searching;
538 #define TOG_SEARCH_FORWARD 1
539 #define TOG_SEARCH_BACKWARD 2
540 int search_next_done;
541 #define TOG_SEARCH_HAVE_MORE 1
542 #define TOG_SEARCH_NO_MORE 2
543 #define TOG_SEARCH_HAVE_NONE 3
544 regex_t regex;
545 regmatch_t regmatch;
546 };
548 static const struct got_error *open_diff_view(struct tog_view *,
549 struct got_object_id *, struct got_object_id *,
550 const char *, const char *, int, int, int, struct tog_view *,
551 struct got_repository *);
552 static const struct got_error *show_diff_view(struct tog_view *);
553 static const struct got_error *input_diff_view(struct tog_view **,
554 struct tog_view *, int);
555 static const struct got_error* close_diff_view(struct tog_view *);
556 static const struct got_error *search_start_diff_view(struct tog_view *);
557 static const struct got_error *search_next_diff_view(struct tog_view *);
559 static const struct got_error *open_log_view(struct tog_view *,
560 struct got_object_id *, struct got_repository *,
561 const char *, const char *, int);
562 static const struct got_error * show_log_view(struct tog_view *);
563 static const struct got_error *input_log_view(struct tog_view **,
564 struct tog_view *, int);
565 static const struct got_error *close_log_view(struct tog_view *);
566 static const struct got_error *search_start_log_view(struct tog_view *);
567 static const struct got_error *search_next_log_view(struct tog_view *);
569 static const struct got_error *open_blame_view(struct tog_view *, char *,
570 struct got_object_id *, struct got_repository *);
571 static const struct got_error *show_blame_view(struct tog_view *);
572 static const struct got_error *input_blame_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_blame_view(struct tog_view *);
575 static const struct got_error *search_start_blame_view(struct tog_view *);
576 static const struct got_error *search_next_blame_view(struct tog_view *);
578 static const struct got_error *open_tree_view(struct tog_view *,
579 struct got_object_id *, const char *, struct got_repository *);
580 static const struct got_error *show_tree_view(struct tog_view *);
581 static const struct got_error *input_tree_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_tree_view(struct tog_view *);
584 static const struct got_error *search_start_tree_view(struct tog_view *);
585 static const struct got_error *search_next_tree_view(struct tog_view *);
587 static const struct got_error *open_ref_view(struct tog_view *,
588 struct got_repository *);
589 static const struct got_error *show_ref_view(struct tog_view *);
590 static const struct got_error *input_ref_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_ref_view(struct tog_view *);
593 static const struct got_error *search_start_ref_view(struct tog_view *);
594 static const struct got_error *search_next_ref_view(struct tog_view *);
596 static volatile sig_atomic_t tog_sigwinch_received;
597 static volatile sig_atomic_t tog_sigpipe_received;
598 static volatile sig_atomic_t tog_sigcont_received;
599 static volatile sig_atomic_t tog_sigint_received;
600 static volatile sig_atomic_t tog_sigterm_received;
602 static void
603 tog_sigwinch(int signo)
605 tog_sigwinch_received = 1;
608 static void
609 tog_sigpipe(int signo)
611 tog_sigpipe_received = 1;
614 static void
615 tog_sigcont(int signo)
617 tog_sigcont_received = 1;
620 static void
621 tog_sigint(int signo)
623 tog_sigint_received = 1;
626 static void
627 tog_sigterm(int signo)
629 tog_sigterm_received = 1;
632 static int
633 tog_fatal_signal_received()
635 return (tog_sigpipe_received ||
636 tog_sigint_received || tog_sigint_received);
640 static const struct got_error *
641 view_close(struct tog_view *view)
643 const struct got_error *err = NULL;
645 if (view->child) {
646 view_close(view->child);
647 view->child = NULL;
649 if (view->close)
650 err = view->close(view);
651 if (view->panel)
652 del_panel(view->panel);
653 if (view->window)
654 delwin(view->window);
655 free(view);
656 return err;
659 static struct tog_view *
660 view_open(int nlines, int ncols, int begin_y, int begin_x,
661 enum tog_view_type type)
663 struct tog_view *view = calloc(1, sizeof(*view));
665 if (view == NULL)
666 return NULL;
668 view->type = type;
669 view->lines = LINES;
670 view->cols = COLS;
671 view->nlines = nlines ? nlines : LINES - begin_y;
672 view->ncols = ncols ? ncols : COLS - begin_x;
673 view->begin_y = begin_y;
674 view->begin_x = begin_x;
675 view->window = newwin(nlines, ncols, begin_y, begin_x);
676 if (view->window == NULL) {
677 view_close(view);
678 return NULL;
680 view->panel = new_panel(view->window);
681 if (view->panel == NULL ||
682 set_panel_userptr(view->panel, view) != OK) {
683 view_close(view);
684 return NULL;
687 keypad(view->window, TRUE);
688 return view;
691 static int
692 view_split_begin_x(int begin_x)
694 if (begin_x > 0 || COLS < 120)
695 return 0;
696 return (COLS - MAX(COLS / 2, 80));
699 static const struct got_error *view_resize(struct tog_view *);
701 static const struct got_error *
702 view_splitscreen(struct tog_view *view)
704 const struct got_error *err = NULL;
706 view->begin_y = 0;
707 view->begin_x = view_split_begin_x(0);
708 view->nlines = LINES;
709 view->ncols = COLS - view->begin_x;
710 view->lines = LINES;
711 view->cols = COLS;
712 err = view_resize(view);
713 if (err)
714 return err;
716 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
717 return got_error_from_errno("mvwin");
719 return NULL;
722 static const struct got_error *
723 view_fullscreen(struct tog_view *view)
725 const struct got_error *err = NULL;
727 view->begin_x = 0;
728 view->begin_y = 0;
729 view->nlines = LINES;
730 view->ncols = COLS;
731 view->lines = LINES;
732 view->cols = COLS;
733 err = view_resize(view);
734 if (err)
735 return err;
737 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
738 return got_error_from_errno("mvwin");
740 return NULL;
743 static int
744 view_is_parent_view(struct tog_view *view)
746 return view->parent == NULL;
749 static const struct got_error *
750 view_resize(struct tog_view *view)
752 int nlines, ncols;
754 if (view->lines > LINES)
755 nlines = view->nlines - (view->lines - LINES);
756 else
757 nlines = view->nlines + (LINES - view->lines);
759 if (view->cols > COLS)
760 ncols = view->ncols - (view->cols - COLS);
761 else
762 ncols = view->ncols + (COLS - view->cols);
764 if (wresize(view->window, nlines, ncols) == ERR)
765 return got_error_from_errno("wresize");
766 if (replace_panel(view->panel, view->window) == ERR)
767 return got_error_from_errno("replace_panel");
768 wclear(view->window);
770 view->nlines = nlines;
771 view->ncols = ncols;
772 view->lines = LINES;
773 view->cols = COLS;
775 if (view->child) {
776 view->child->begin_x = view_split_begin_x(view->begin_x);
777 if (view->child->begin_x == 0) {
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 view_splitscreen(view->child);
785 show_panel(view->child->panel);
789 return NULL;
792 static const struct got_error *
793 view_close_child(struct tog_view *view)
795 const struct got_error *err = NULL;
797 if (view->child == NULL)
798 return NULL;
800 err = view_close(view->child);
801 view->child = NULL;
802 return err;
805 static void
806 view_set_child(struct tog_view *view, struct tog_view *child)
808 view->child = child;
809 child->parent = view;
812 static int
813 view_is_splitscreen(struct tog_view *view)
815 return view->begin_x > 0;
818 static void
819 tog_resizeterm(void)
821 int cols, lines;
822 struct winsize size;
824 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
825 cols = 80; /* Default */
826 lines = 24;
827 } else {
828 cols = size.ws_col;
829 lines = size.ws_row;
831 resize_term(lines, cols);
834 static const struct got_error *
835 view_search_start(struct tog_view *view)
837 const struct got_error *err = NULL;
838 char pattern[1024];
839 int ret;
841 if (view->search_started) {
842 regfree(&view->regex);
843 view->searching = 0;
844 memset(&view->regmatch, 0, sizeof(view->regmatch));
846 view->search_started = 0;
848 if (view->nlines < 1)
849 return NULL;
851 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
852 wclrtoeol(view->window);
854 nocbreak();
855 echo();
856 ret = wgetnstr(view->window, pattern, sizeof(pattern));
857 cbreak();
858 noecho();
859 if (ret == ERR)
860 return NULL;
862 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
863 err = view->search_start(view);
864 if (err) {
865 regfree(&view->regex);
866 return err;
868 view->search_started = 1;
869 view->searching = TOG_SEARCH_FORWARD;
870 view->search_next_done = 0;
871 view->search_next(view);
874 return NULL;
877 static const struct got_error *
878 view_input(struct tog_view **new, int *done, struct tog_view *view,
879 struct tog_view_list_head *views)
881 const struct got_error *err = NULL;
882 struct tog_view *v;
883 int ch, errcode;
885 *new = NULL;
887 /* Clear "no matches" indicator. */
888 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
889 view->search_next_done == TOG_SEARCH_HAVE_NONE)
890 view->search_next_done = TOG_SEARCH_HAVE_MORE;
892 if (view->searching && !view->search_next_done) {
893 errcode = pthread_mutex_unlock(&tog_mutex);
894 if (errcode)
895 return got_error_set_errno(errcode,
896 "pthread_mutex_unlock");
897 sched_yield();
898 errcode = pthread_mutex_lock(&tog_mutex);
899 if (errcode)
900 return got_error_set_errno(errcode,
901 "pthread_mutex_lock");
902 view->search_next(view);
903 return NULL;
906 nodelay(stdscr, FALSE);
907 /* Allow threads to make progress while we are waiting for input. */
908 errcode = pthread_mutex_unlock(&tog_mutex);
909 if (errcode)
910 return got_error_set_errno(errcode, "pthread_mutex_unlock");
911 ch = wgetch(view->window);
912 errcode = pthread_mutex_lock(&tog_mutex);
913 if (errcode)
914 return got_error_set_errno(errcode, "pthread_mutex_lock");
915 nodelay(stdscr, TRUE);
917 if (tog_sigwinch_received || tog_sigcont_received) {
918 tog_resizeterm();
919 tog_sigwinch_received = 0;
920 tog_sigcont_received = 0;
921 TAILQ_FOREACH(v, views, entry) {
922 err = view_resize(v);
923 if (err)
924 return err;
925 err = v->input(new, v, KEY_RESIZE);
926 if (err)
927 return err;
928 if (v->child) {
929 err = view_resize(v->child);
930 if (err)
931 return err;
932 err = v->child->input(new, v->child,
933 KEY_RESIZE);
934 if (err)
935 return err;
940 switch (ch) {
941 case '\t':
942 if (view->child) {
943 view->focussed = 0;
944 view->child->focussed = 1;
945 view->focus_child = 1;
946 } else if (view->parent) {
947 view->focussed = 0;
948 view->parent->focussed = 1;
949 view->parent->focus_child = 0;
951 break;
952 case 'q':
953 err = view->input(new, view, ch);
954 view->dying = 1;
955 break;
956 case 'Q':
957 *done = 1;
958 break;
959 case 'f':
960 if (view_is_parent_view(view)) {
961 if (view->child == NULL)
962 break;
963 if (view_is_splitscreen(view->child)) {
964 view->focussed = 0;
965 view->child->focussed = 1;
966 err = view_fullscreen(view->child);
967 } else
968 err = view_splitscreen(view->child);
969 if (err)
970 break;
971 err = view->child->input(new, view->child,
972 KEY_RESIZE);
973 } else {
974 if (view_is_splitscreen(view)) {
975 view->parent->focussed = 0;
976 view->focussed = 1;
977 err = view_fullscreen(view);
978 } else {
979 err = view_splitscreen(view);
981 if (err)
982 break;
983 err = view->input(new, view, KEY_RESIZE);
985 break;
986 case KEY_RESIZE:
987 break;
988 case '/':
989 if (view->search_start)
990 view_search_start(view);
991 else
992 err = view->input(new, view, ch);
993 break;
994 case 'N':
995 case 'n':
996 if (view->search_started && view->search_next) {
997 view->searching = (ch == 'n' ?
998 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
999 view->search_next_done = 0;
1000 view->search_next(view);
1001 } else
1002 err = view->input(new, view, ch);
1003 break;
1004 default:
1005 err = view->input(new, view, ch);
1006 break;
1009 return err;
1012 void
1013 view_vborder(struct tog_view *view)
1015 PANEL *panel;
1016 const struct tog_view *view_above;
1018 if (view->parent)
1019 return view_vborder(view->parent);
1021 panel = panel_above(view->panel);
1022 if (panel == NULL)
1023 return;
1025 view_above = panel_userptr(panel);
1026 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1027 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1030 int
1031 view_needs_focus_indication(struct tog_view *view)
1033 if (view_is_parent_view(view)) {
1034 if (view->child == NULL || view->child->focussed)
1035 return 0;
1036 if (!view_is_splitscreen(view->child))
1037 return 0;
1038 } else if (!view_is_splitscreen(view))
1039 return 0;
1041 return view->focussed;
1044 static const struct got_error *
1045 view_loop(struct tog_view *view)
1047 const struct got_error *err = NULL;
1048 struct tog_view_list_head views;
1049 struct tog_view *new_view;
1050 int fast_refresh = 10;
1051 int done = 0, errcode;
1053 errcode = pthread_mutex_lock(&tog_mutex);
1054 if (errcode)
1055 return got_error_set_errno(errcode, "pthread_mutex_lock");
1057 TAILQ_INIT(&views);
1058 TAILQ_INSERT_HEAD(&views, view, entry);
1060 view->focussed = 1;
1061 err = view->show(view);
1062 if (err)
1063 return err;
1064 update_panels();
1065 doupdate();
1066 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1067 /* Refresh fast during initialization, then become slower. */
1068 if (fast_refresh && fast_refresh-- == 0)
1069 halfdelay(10); /* switch to once per second */
1071 err = view_input(&new_view, &done, view, &views);
1072 if (err)
1073 break;
1074 if (view->dying) {
1075 struct tog_view *v, *prev = NULL;
1077 if (view_is_parent_view(view))
1078 prev = TAILQ_PREV(view, tog_view_list_head,
1079 entry);
1080 else if (view->parent)
1081 prev = view->parent;
1083 if (view->parent) {
1084 view->parent->child = NULL;
1085 view->parent->focus_child = 0;
1086 } else
1087 TAILQ_REMOVE(&views, view, entry);
1089 err = view_close(view);
1090 if (err)
1091 goto done;
1093 view = NULL;
1094 TAILQ_FOREACH(v, &views, entry) {
1095 if (v->focussed)
1096 break;
1098 if (view == NULL && new_view == NULL) {
1099 /* No view has focus. Try to pick one. */
1100 if (prev)
1101 view = prev;
1102 else if (!TAILQ_EMPTY(&views)) {
1103 view = TAILQ_LAST(&views,
1104 tog_view_list_head);
1106 if (view) {
1107 if (view->focus_child) {
1108 view->child->focussed = 1;
1109 view = view->child;
1110 } else
1111 view->focussed = 1;
1115 if (new_view) {
1116 struct tog_view *v, *t;
1117 /* Only allow one parent view per type. */
1118 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1119 if (v->type != new_view->type)
1120 continue;
1121 TAILQ_REMOVE(&views, v, entry);
1122 err = view_close(v);
1123 if (err)
1124 goto done;
1125 break;
1127 TAILQ_INSERT_TAIL(&views, new_view, entry);
1128 view = new_view;
1130 if (view) {
1131 if (view_is_parent_view(view)) {
1132 if (view->child && view->child->focussed)
1133 view = view->child;
1134 } else {
1135 if (view->parent && view->parent->focussed)
1136 view = view->parent;
1138 show_panel(view->panel);
1139 if (view->child && view_is_splitscreen(view->child))
1140 show_panel(view->child->panel);
1141 if (view->parent && view_is_splitscreen(view)) {
1142 err = view->parent->show(view->parent);
1143 if (err)
1144 goto done;
1146 err = view->show(view);
1147 if (err)
1148 goto done;
1149 if (view->child) {
1150 err = view->child->show(view->child);
1151 if (err)
1152 goto done;
1154 update_panels();
1155 doupdate();
1158 done:
1159 while (!TAILQ_EMPTY(&views)) {
1160 view = TAILQ_FIRST(&views);
1161 TAILQ_REMOVE(&views, view, entry);
1162 view_close(view);
1165 errcode = pthread_mutex_unlock(&tog_mutex);
1166 if (errcode && err == NULL)
1167 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1169 return err;
1172 __dead static void
1173 usage_log(void)
1175 endwin();
1176 fprintf(stderr,
1177 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1178 getprogname());
1179 exit(1);
1182 /* Create newly allocated wide-character string equivalent to a byte string. */
1183 static const struct got_error *
1184 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1186 char *vis = NULL;
1187 const struct got_error *err = NULL;
1189 *ws = NULL;
1190 *wlen = mbstowcs(NULL, s, 0);
1191 if (*wlen == (size_t)-1) {
1192 int vislen;
1193 if (errno != EILSEQ)
1194 return got_error_from_errno("mbstowcs");
1196 /* byte string invalid in current encoding; try to "fix" it */
1197 err = got_mbsavis(&vis, &vislen, s);
1198 if (err)
1199 return err;
1200 *wlen = mbstowcs(NULL, vis, 0);
1201 if (*wlen == (size_t)-1) {
1202 err = got_error_from_errno("mbstowcs"); /* give up */
1203 goto done;
1207 *ws = calloc(*wlen + 1, sizeof(**ws));
1208 if (*ws == NULL) {
1209 err = got_error_from_errno("calloc");
1210 goto done;
1213 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1214 err = got_error_from_errno("mbstowcs");
1215 done:
1216 free(vis);
1217 if (err) {
1218 free(*ws);
1219 *ws = NULL;
1220 *wlen = 0;
1222 return err;
1225 /* Format a line for display, ensuring that it won't overflow a width limit. */
1226 static const struct got_error *
1227 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1228 int col_tab_align)
1230 const struct got_error *err = NULL;
1231 int cols = 0;
1232 wchar_t *wline = NULL;
1233 size_t wlen;
1234 int i;
1236 *wlinep = NULL;
1237 *widthp = 0;
1239 err = mbs2ws(&wline, &wlen, line);
1240 if (err)
1241 return err;
1243 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1244 wline[wlen - 1] = L'\0';
1245 wlen--;
1247 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1248 wline[wlen - 1] = L'\0';
1249 wlen--;
1252 i = 0;
1253 while (i < wlen) {
1254 int width = wcwidth(wline[i]);
1256 if (width == 0) {
1257 i++;
1258 continue;
1261 if (width == 1 || width == 2) {
1262 if (cols + width > wlimit)
1263 break;
1264 cols += width;
1265 i++;
1266 } else if (width == -1) {
1267 if (wline[i] == L'\t') {
1268 width = TABSIZE -
1269 ((cols + col_tab_align) % TABSIZE);
1270 } else {
1271 width = 1;
1272 wline[i] = L'.';
1274 if (cols + width > wlimit)
1275 break;
1276 cols += width;
1277 i++;
1278 } else {
1279 err = got_error_from_errno("wcwidth");
1280 goto done;
1283 wline[i] = L'\0';
1284 if (widthp)
1285 *widthp = cols;
1286 done:
1287 if (err)
1288 free(wline);
1289 else
1290 *wlinep = wline;
1291 return err;
1294 static const struct got_error*
1295 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1296 struct got_object_id *id, struct got_repository *repo)
1298 static const struct got_error *err = NULL;
1299 struct got_reflist_entry *re;
1300 char *s;
1301 const char *name;
1303 *refs_str = NULL;
1305 TAILQ_FOREACH(re, refs, entry) {
1306 struct got_tag_object *tag = NULL;
1307 struct got_object_id *ref_id;
1308 int cmp;
1310 name = got_ref_get_name(re->ref);
1311 if (strcmp(name, GOT_REF_HEAD) == 0)
1312 continue;
1313 if (strncmp(name, "refs/", 5) == 0)
1314 name += 5;
1315 if (strncmp(name, "got/", 4) == 0 &&
1316 strncmp(name, "got/backup/", 11) != 0)
1317 continue;
1318 if (strncmp(name, "heads/", 6) == 0)
1319 name += 6;
1320 if (strncmp(name, "remotes/", 8) == 0) {
1321 name += 8;
1322 s = strstr(name, "/" GOT_REF_HEAD);
1323 if (s != NULL && s[strlen(s)] == '\0')
1324 continue;
1326 err = got_ref_resolve(&ref_id, repo, re->ref);
1327 if (err)
1328 break;
1329 if (strncmp(name, "tags/", 5) == 0) {
1330 err = got_object_open_as_tag(&tag, repo, ref_id);
1331 if (err) {
1332 if (err->code != GOT_ERR_OBJ_TYPE) {
1333 free(ref_id);
1334 break;
1336 /* Ref points at something other than a tag. */
1337 err = NULL;
1338 tag = NULL;
1341 cmp = got_object_id_cmp(tag ?
1342 got_object_tag_get_object_id(tag) : ref_id, id);
1343 free(ref_id);
1344 if (tag)
1345 got_object_tag_close(tag);
1346 if (cmp != 0)
1347 continue;
1348 s = *refs_str;
1349 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1350 s ? ", " : "", name) == -1) {
1351 err = got_error_from_errno("asprintf");
1352 free(s);
1353 *refs_str = NULL;
1354 break;
1356 free(s);
1359 return err;
1362 static const struct got_error *
1363 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1364 int col_tab_align)
1366 char *smallerthan;
1368 smallerthan = strchr(author, '<');
1369 if (smallerthan && smallerthan[1] != '\0')
1370 author = smallerthan + 1;
1371 author[strcspn(author, "@>")] = '\0';
1372 return format_line(wauthor, author_width, author, limit, col_tab_align);
1375 static const struct got_error *
1376 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1377 struct got_object_id *id, const size_t date_display_cols,
1378 int author_display_cols)
1380 struct tog_log_view_state *s = &view->state.log;
1381 const struct got_error *err = NULL;
1382 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1383 char *logmsg0 = NULL, *logmsg = NULL;
1384 char *author = NULL;
1385 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1386 int author_width, logmsg_width;
1387 char *newline, *line = NULL;
1388 int col, limit;
1389 const int avail = view->ncols;
1390 struct tm tm;
1391 time_t committer_time;
1392 struct tog_color *tc;
1394 committer_time = got_object_commit_get_committer_time(commit);
1395 if (gmtime_r(&committer_time, &tm) == NULL)
1396 return got_error_from_errno("gmtime_r");
1397 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1398 return got_error(GOT_ERR_NO_SPACE);
1400 if (avail <= date_display_cols)
1401 limit = MIN(sizeof(datebuf) - 1, avail);
1402 else
1403 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1404 tc = get_color(&s->colors, TOG_COLOR_DATE);
1405 if (tc)
1406 wattr_on(view->window,
1407 COLOR_PAIR(tc->colorpair), NULL);
1408 waddnstr(view->window, datebuf, limit);
1409 if (tc)
1410 wattr_off(view->window,
1411 COLOR_PAIR(tc->colorpair), NULL);
1412 col = limit;
1413 if (col > avail)
1414 goto done;
1416 if (avail >= 120) {
1417 char *id_str;
1418 err = got_object_id_str(&id_str, id);
1419 if (err)
1420 goto done;
1421 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1422 if (tc)
1423 wattr_on(view->window,
1424 COLOR_PAIR(tc->colorpair), NULL);
1425 wprintw(view->window, "%.8s ", id_str);
1426 if (tc)
1427 wattr_off(view->window,
1428 COLOR_PAIR(tc->colorpair), NULL);
1429 free(id_str);
1430 col += 9;
1431 if (col > avail)
1432 goto done;
1435 author = strdup(got_object_commit_get_author(commit));
1436 if (author == NULL) {
1437 err = got_error_from_errno("strdup");
1438 goto done;
1440 err = format_author(&wauthor, &author_width, author, avail - col, col);
1441 if (err)
1442 goto done;
1443 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1444 if (tc)
1445 wattr_on(view->window,
1446 COLOR_PAIR(tc->colorpair), NULL);
1447 waddwstr(view->window, wauthor);
1448 if (tc)
1449 wattr_off(view->window,
1450 COLOR_PAIR(tc->colorpair), NULL);
1451 col += author_width;
1452 while (col < avail && author_width < author_display_cols + 2) {
1453 waddch(view->window, ' ');
1454 col++;
1455 author_width++;
1457 if (col > avail)
1458 goto done;
1460 err = got_object_commit_get_logmsg(&logmsg0, commit);
1461 if (err)
1462 goto done;
1463 logmsg = logmsg0;
1464 while (*logmsg == '\n')
1465 logmsg++;
1466 newline = strchr(logmsg, '\n');
1467 if (newline)
1468 *newline = '\0';
1469 limit = avail - col;
1470 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1471 if (err)
1472 goto done;
1473 waddwstr(view->window, wlogmsg);
1474 col += logmsg_width;
1475 while (col < avail) {
1476 waddch(view->window, ' ');
1477 col++;
1479 done:
1480 free(logmsg0);
1481 free(wlogmsg);
1482 free(author);
1483 free(wauthor);
1484 free(line);
1485 return err;
1488 static struct commit_queue_entry *
1489 alloc_commit_queue_entry(struct got_commit_object *commit,
1490 struct got_object_id *id)
1492 struct commit_queue_entry *entry;
1494 entry = calloc(1, sizeof(*entry));
1495 if (entry == NULL)
1496 return NULL;
1498 entry->id = id;
1499 entry->commit = commit;
1500 return entry;
1503 static void
1504 pop_commit(struct commit_queue *commits)
1506 struct commit_queue_entry *entry;
1508 entry = TAILQ_FIRST(&commits->head);
1509 TAILQ_REMOVE(&commits->head, entry, entry);
1510 got_object_commit_close(entry->commit);
1511 commits->ncommits--;
1512 /* Don't free entry->id! It is owned by the commit graph. */
1513 free(entry);
1516 static void
1517 free_commits(struct commit_queue *commits)
1519 while (!TAILQ_EMPTY(&commits->head))
1520 pop_commit(commits);
1523 static const struct got_error *
1524 match_commit(int *have_match, struct got_object_id *id,
1525 struct got_commit_object *commit, regex_t *regex)
1527 const struct got_error *err = NULL;
1528 regmatch_t regmatch;
1529 char *id_str = NULL, *logmsg = NULL;
1531 *have_match = 0;
1533 err = got_object_id_str(&id_str, id);
1534 if (err)
1535 return err;
1537 err = got_object_commit_get_logmsg(&logmsg, commit);
1538 if (err)
1539 goto done;
1541 if (regexec(regex, got_object_commit_get_author(commit), 1,
1542 &regmatch, 0) == 0 ||
1543 regexec(regex, got_object_commit_get_committer(commit), 1,
1544 &regmatch, 0) == 0 ||
1545 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1546 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1547 *have_match = 1;
1548 done:
1549 free(id_str);
1550 free(logmsg);
1551 return err;
1554 static const struct got_error *
1555 queue_commits(struct tog_log_thread_args *a)
1557 const struct got_error *err = NULL;
1560 * We keep all commits open throughout the lifetime of the log
1561 * view in order to avoid having to re-fetch commits from disk
1562 * while updating the display.
1564 do {
1565 struct got_object_id *id;
1566 struct got_commit_object *commit;
1567 struct commit_queue_entry *entry;
1568 int errcode;
1570 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1571 NULL, NULL);
1572 if (err || id == NULL)
1573 break;
1575 err = got_object_open_as_commit(&commit, a->repo, id);
1576 if (err)
1577 break;
1578 entry = alloc_commit_queue_entry(commit, id);
1579 if (entry == NULL) {
1580 err = got_error_from_errno("alloc_commit_queue_entry");
1581 break;
1584 errcode = pthread_mutex_lock(&tog_mutex);
1585 if (errcode) {
1586 err = got_error_set_errno(errcode,
1587 "pthread_mutex_lock");
1588 break;
1591 entry->idx = a->commits->ncommits;
1592 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1593 a->commits->ncommits++;
1595 if (*a->searching == TOG_SEARCH_FORWARD &&
1596 !*a->search_next_done) {
1597 int have_match;
1598 err = match_commit(&have_match, id, commit, a->regex);
1599 if (err)
1600 break;
1601 if (have_match)
1602 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1605 errcode = pthread_mutex_unlock(&tog_mutex);
1606 if (errcode && err == NULL)
1607 err = got_error_set_errno(errcode,
1608 "pthread_mutex_unlock");
1609 if (err)
1610 break;
1611 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1613 return err;
1616 static void
1617 select_commit(struct tog_log_view_state *s)
1619 struct commit_queue_entry *entry;
1620 int ncommits = 0;
1622 entry = s->first_displayed_entry;
1623 while (entry) {
1624 if (ncommits == s->selected) {
1625 s->selected_entry = entry;
1626 break;
1628 entry = TAILQ_NEXT(entry, entry);
1629 ncommits++;
1633 static const struct got_error *
1634 draw_commits(struct tog_view *view)
1636 const struct got_error *err = NULL;
1637 struct tog_log_view_state *s = &view->state.log;
1638 struct commit_queue_entry *entry = s->selected_entry;
1639 const int limit = view->nlines;
1640 int width;
1641 int ncommits, author_cols = 4;
1642 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1643 char *refs_str = NULL;
1644 wchar_t *wline;
1645 struct tog_color *tc;
1646 static const size_t date_display_cols = 12;
1648 if (s->selected_entry &&
1649 !(view->searching && view->search_next_done == 0)) {
1650 struct got_reflist_head *refs;
1651 err = got_object_id_str(&id_str, s->selected_entry->id);
1652 if (err)
1653 return err;
1654 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1655 s->selected_entry->id);
1656 if (refs) {
1657 err = build_refs_str(&refs_str, refs,
1658 s->selected_entry->id, s->repo);
1659 if (err)
1660 goto done;
1664 if (s->thread_args.commits_needed == 0)
1665 halfdelay(10); /* disable fast refresh */
1667 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1668 if (asprintf(&ncommits_str, " [%d/%d] %s",
1669 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1670 (view->searching && !view->search_next_done) ?
1671 "searching..." : "loading...") == -1) {
1672 err = got_error_from_errno("asprintf");
1673 goto done;
1675 } else {
1676 const char *search_str = NULL;
1678 if (view->searching) {
1679 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1680 search_str = "no more matches";
1681 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1682 search_str = "no matches found";
1683 else if (!view->search_next_done)
1684 search_str = "searching...";
1687 if (asprintf(&ncommits_str, " [%d/%d] %s",
1688 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1689 search_str ? search_str :
1690 (refs_str ? refs_str : "")) == -1) {
1691 err = got_error_from_errno("asprintf");
1692 goto done;
1696 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1697 if (asprintf(&header, "commit %s %s%s",
1698 id_str ? id_str : "........................................",
1699 s->in_repo_path, ncommits_str) == -1) {
1700 err = got_error_from_errno("asprintf");
1701 header = NULL;
1702 goto done;
1704 } else if (asprintf(&header, "commit %s%s",
1705 id_str ? id_str : "........................................",
1706 ncommits_str) == -1) {
1707 err = got_error_from_errno("asprintf");
1708 header = NULL;
1709 goto done;
1711 err = format_line(&wline, &width, header, view->ncols, 0);
1712 if (err)
1713 goto done;
1715 werase(view->window);
1717 if (view_needs_focus_indication(view))
1718 wstandout(view->window);
1719 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1720 if (tc)
1721 wattr_on(view->window,
1722 COLOR_PAIR(tc->colorpair), NULL);
1723 waddwstr(view->window, wline);
1724 if (tc)
1725 wattr_off(view->window,
1726 COLOR_PAIR(tc->colorpair), NULL);
1727 while (width < view->ncols) {
1728 waddch(view->window, ' ');
1729 width++;
1731 if (view_needs_focus_indication(view))
1732 wstandend(view->window);
1733 free(wline);
1734 if (limit <= 1)
1735 goto done;
1737 /* Grow author column size if necessary. */
1738 entry = s->first_displayed_entry;
1739 ncommits = 0;
1740 while (entry) {
1741 char *author;
1742 wchar_t *wauthor;
1743 int width;
1744 if (ncommits >= limit - 1)
1745 break;
1746 author = strdup(got_object_commit_get_author(entry->commit));
1747 if (author == NULL) {
1748 err = got_error_from_errno("strdup");
1749 goto done;
1751 err = format_author(&wauthor, &width, author, COLS,
1752 date_display_cols);
1753 if (author_cols < width)
1754 author_cols = width;
1755 free(wauthor);
1756 free(author);
1757 ncommits++;
1758 entry = TAILQ_NEXT(entry, entry);
1761 entry = s->first_displayed_entry;
1762 s->last_displayed_entry = s->first_displayed_entry;
1763 ncommits = 0;
1764 while (entry) {
1765 if (ncommits >= limit - 1)
1766 break;
1767 if (ncommits == s->selected)
1768 wstandout(view->window);
1769 err = draw_commit(view, entry->commit, entry->id,
1770 date_display_cols, author_cols);
1771 if (ncommits == s->selected)
1772 wstandend(view->window);
1773 if (err)
1774 goto done;
1775 ncommits++;
1776 s->last_displayed_entry = entry;
1777 entry = TAILQ_NEXT(entry, entry);
1780 view_vborder(view);
1781 done:
1782 free(id_str);
1783 free(refs_str);
1784 free(ncommits_str);
1785 free(header);
1786 return err;
1789 static void
1790 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1792 struct commit_queue_entry *entry;
1793 int nscrolled = 0;
1795 entry = TAILQ_FIRST(&s->commits.head);
1796 if (s->first_displayed_entry == entry)
1797 return;
1799 entry = s->first_displayed_entry;
1800 while (entry && nscrolled < maxscroll) {
1801 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1802 if (entry) {
1803 s->first_displayed_entry = entry;
1804 nscrolled++;
1809 static const struct got_error *
1810 trigger_log_thread(struct tog_view *view, int wait)
1812 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1813 int errcode;
1815 halfdelay(1); /* fast refresh while loading commits */
1817 while (ta->commits_needed > 0 || ta->load_all) {
1818 if (ta->log_complete)
1819 break;
1821 /* Wake the log thread. */
1822 errcode = pthread_cond_signal(&ta->need_commits);
1823 if (errcode)
1824 return got_error_set_errno(errcode,
1825 "pthread_cond_signal");
1828 * The mutex will be released while the view loop waits
1829 * in wgetch(), at which time the log thread will run.
1831 if (!wait)
1832 break;
1834 /* Display progress update in log view. */
1835 show_log_view(view);
1836 update_panels();
1837 doupdate();
1839 /* Wait right here while next commit is being loaded. */
1840 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1841 if (errcode)
1842 return got_error_set_errno(errcode,
1843 "pthread_cond_wait");
1845 /* Display progress update in log view. */
1846 show_log_view(view);
1847 update_panels();
1848 doupdate();
1851 return NULL;
1854 static const struct got_error *
1855 log_scroll_down(struct tog_view *view, int maxscroll)
1857 struct tog_log_view_state *s = &view->state.log;
1858 const struct got_error *err = NULL;
1859 struct commit_queue_entry *pentry;
1860 int nscrolled = 0, ncommits_needed;
1862 if (s->last_displayed_entry == NULL)
1863 return NULL;
1865 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1866 if (s->commits.ncommits < ncommits_needed &&
1867 !s->thread_args.log_complete) {
1869 * Ask the log thread for required amount of commits.
1871 s->thread_args.commits_needed += maxscroll;
1872 err = trigger_log_thread(view, 1);
1873 if (err)
1874 return err;
1877 do {
1878 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1879 if (pentry == NULL)
1880 break;
1882 s->last_displayed_entry = pentry;
1884 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1885 if (pentry == NULL)
1886 break;
1887 s->first_displayed_entry = pentry;
1888 } while (++nscrolled < maxscroll);
1890 return err;
1893 static const struct got_error *
1894 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1895 struct got_commit_object *commit, struct got_object_id *commit_id,
1896 struct tog_view *log_view, struct got_repository *repo)
1898 const struct got_error *err;
1899 struct got_object_qid *parent_id;
1900 struct tog_view *diff_view;
1902 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1903 if (diff_view == NULL)
1904 return got_error_from_errno("view_open");
1906 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1907 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1908 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1909 if (err == NULL)
1910 *new_view = diff_view;
1911 return err;
1914 static const struct got_error *
1915 tree_view_visit_subtree(struct tog_tree_view_state *s,
1916 struct got_tree_object *subtree)
1918 struct tog_parent_tree *parent;
1920 parent = calloc(1, sizeof(*parent));
1921 if (parent == NULL)
1922 return got_error_from_errno("calloc");
1924 parent->tree = s->tree;
1925 parent->first_displayed_entry = s->first_displayed_entry;
1926 parent->selected_entry = s->selected_entry;
1927 parent->selected = s->selected;
1928 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1929 s->tree = subtree;
1930 s->selected = 0;
1931 s->first_displayed_entry = NULL;
1932 return NULL;
1935 static const struct got_error *
1936 tree_view_walk_path(struct tog_tree_view_state *s,
1937 struct got_commit_object *commit, const char *path)
1939 const struct got_error *err = NULL;
1940 struct got_tree_object *tree = NULL;
1941 const char *p;
1942 char *slash, *subpath = NULL;
1944 /* Walk the path and open corresponding tree objects. */
1945 p = path;
1946 while (*p) {
1947 struct got_tree_entry *te;
1948 struct got_object_id *tree_id;
1949 char *te_name;
1951 while (p[0] == '/')
1952 p++;
1954 /* Ensure the correct subtree entry is selected. */
1955 slash = strchr(p, '/');
1956 if (slash == NULL)
1957 te_name = strdup(p);
1958 else
1959 te_name = strndup(p, slash - p);
1960 if (te_name == NULL) {
1961 err = got_error_from_errno("strndup");
1962 break;
1964 te = got_object_tree_find_entry(s->tree, te_name);
1965 if (te == NULL) {
1966 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1967 free(te_name);
1968 break;
1970 free(te_name);
1971 s->first_displayed_entry = s->selected_entry = te;
1973 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1974 break; /* jump to this file's entry */
1976 slash = strchr(p, '/');
1977 if (slash)
1978 subpath = strndup(path, slash - path);
1979 else
1980 subpath = strdup(path);
1981 if (subpath == NULL) {
1982 err = got_error_from_errno("strdup");
1983 break;
1986 err = got_object_id_by_path(&tree_id, s->repo, commit,
1987 subpath);
1988 if (err)
1989 break;
1991 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1992 free(tree_id);
1993 if (err)
1994 break;
1996 err = tree_view_visit_subtree(s, tree);
1997 if (err) {
1998 got_object_tree_close(tree);
1999 break;
2001 if (slash == NULL)
2002 break;
2003 free(subpath);
2004 subpath = NULL;
2005 p = slash;
2008 free(subpath);
2009 return err;
2012 static const struct got_error *
2013 browse_commit_tree(struct tog_view **new_view, int begin_x,
2014 struct commit_queue_entry *entry, const char *path,
2015 const char *head_ref_name, struct got_repository *repo)
2017 const struct got_error *err = NULL;
2018 struct tog_tree_view_state *s;
2019 struct tog_view *tree_view;
2021 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2022 if (tree_view == NULL)
2023 return got_error_from_errno("view_open");
2025 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2026 if (err)
2027 return err;
2028 s = &tree_view->state.tree;
2030 *new_view = tree_view;
2032 if (got_path_is_root_dir(path))
2033 return NULL;
2035 return tree_view_walk_path(s, entry->commit, path);
2038 static const struct got_error *
2039 block_signals_used_by_main_thread(void)
2041 sigset_t sigset;
2042 int errcode;
2044 if (sigemptyset(&sigset) == -1)
2045 return got_error_from_errno("sigemptyset");
2047 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2048 if (sigaddset(&sigset, SIGWINCH) == -1)
2049 return got_error_from_errno("sigaddset");
2050 if (sigaddset(&sigset, SIGCONT) == -1)
2051 return got_error_from_errno("sigaddset");
2052 if (sigaddset(&sigset, SIGINT) == -1)
2053 return got_error_from_errno("sigaddset");
2054 if (sigaddset(&sigset, SIGTERM) == -1)
2055 return got_error_from_errno("sigaddset");
2057 /* ncurses handles SIGTSTP */
2058 if (sigaddset(&sigset, SIGTSTP) == -1)
2059 return got_error_from_errno("sigaddset");
2061 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2062 if (errcode)
2063 return got_error_set_errno(errcode, "pthread_sigmask");
2065 return NULL;
2068 static void *
2069 log_thread(void *arg)
2071 const struct got_error *err = NULL;
2072 int errcode = 0;
2073 struct tog_log_thread_args *a = arg;
2074 int done = 0;
2076 err = block_signals_used_by_main_thread();
2077 if (err)
2078 return (void *)err;
2080 while (!done && !err && !tog_fatal_signal_received()) {
2081 err = queue_commits(a);
2082 if (err) {
2083 if (err->code != GOT_ERR_ITER_COMPLETED)
2084 return (void *)err;
2085 err = NULL;
2086 done = 1;
2087 } else if (a->commits_needed > 0 && !a->load_all)
2088 a->commits_needed--;
2090 errcode = pthread_mutex_lock(&tog_mutex);
2091 if (errcode) {
2092 err = got_error_set_errno(errcode,
2093 "pthread_mutex_lock");
2094 break;
2095 } else if (*a->quit)
2096 done = 1;
2097 else if (*a->first_displayed_entry == NULL) {
2098 *a->first_displayed_entry =
2099 TAILQ_FIRST(&a->commits->head);
2100 *a->selected_entry = *a->first_displayed_entry;
2103 errcode = pthread_cond_signal(&a->commit_loaded);
2104 if (errcode) {
2105 err = got_error_set_errno(errcode,
2106 "pthread_cond_signal");
2107 pthread_mutex_unlock(&tog_mutex);
2108 break;
2111 if (done)
2112 a->commits_needed = 0;
2113 else {
2114 if (a->commits_needed == 0 && !a->load_all) {
2115 errcode = pthread_cond_wait(&a->need_commits,
2116 &tog_mutex);
2117 if (errcode)
2118 err = got_error_set_errno(errcode,
2119 "pthread_cond_wait");
2120 if (*a->quit)
2121 done = 1;
2125 errcode = pthread_mutex_unlock(&tog_mutex);
2126 if (errcode && err == NULL)
2127 err = got_error_set_errno(errcode,
2128 "pthread_mutex_unlock");
2130 a->log_complete = 1;
2131 return (void *)err;
2134 static const struct got_error *
2135 stop_log_thread(struct tog_log_view_state *s)
2137 const struct got_error *err = NULL;
2138 int errcode;
2140 if (s->thread) {
2141 s->quit = 1;
2142 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2143 if (errcode)
2144 return got_error_set_errno(errcode,
2145 "pthread_cond_signal");
2146 errcode = pthread_mutex_unlock(&tog_mutex);
2147 if (errcode)
2148 return got_error_set_errno(errcode,
2149 "pthread_mutex_unlock");
2150 errcode = pthread_join(s->thread, (void **)&err);
2151 if (errcode)
2152 return got_error_set_errno(errcode, "pthread_join");
2153 errcode = pthread_mutex_lock(&tog_mutex);
2154 if (errcode)
2155 return got_error_set_errno(errcode,
2156 "pthread_mutex_lock");
2157 s->thread = NULL;
2160 if (s->thread_args.repo) {
2161 err = got_repo_close(s->thread_args.repo);
2162 s->thread_args.repo = NULL;
2165 if (s->thread_args.graph) {
2166 got_commit_graph_close(s->thread_args.graph);
2167 s->thread_args.graph = NULL;
2170 return err;
2173 static const struct got_error *
2174 close_log_view(struct tog_view *view)
2176 const struct got_error *err = NULL;
2177 struct tog_log_view_state *s = &view->state.log;
2178 int errcode;
2180 err = stop_log_thread(s);
2182 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2183 if (errcode && err == NULL)
2184 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2186 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2187 if (errcode && err == NULL)
2188 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2190 free_commits(&s->commits);
2191 free(s->in_repo_path);
2192 s->in_repo_path = NULL;
2193 free(s->start_id);
2194 s->start_id = NULL;
2195 free(s->head_ref_name);
2196 s->head_ref_name = NULL;
2197 return err;
2200 static const struct got_error *
2201 search_start_log_view(struct tog_view *view)
2203 struct tog_log_view_state *s = &view->state.log;
2205 s->matched_entry = NULL;
2206 s->search_entry = NULL;
2207 return NULL;
2210 static const struct got_error *
2211 search_next_log_view(struct tog_view *view)
2213 const struct got_error *err = NULL;
2214 struct tog_log_view_state *s = &view->state.log;
2215 struct commit_queue_entry *entry;
2217 /* Display progress update in log view. */
2218 show_log_view(view);
2219 update_panels();
2220 doupdate();
2222 if (s->search_entry) {
2223 int errcode, ch;
2224 errcode = pthread_mutex_unlock(&tog_mutex);
2225 if (errcode)
2226 return got_error_set_errno(errcode,
2227 "pthread_mutex_unlock");
2228 ch = wgetch(view->window);
2229 errcode = pthread_mutex_lock(&tog_mutex);
2230 if (errcode)
2231 return got_error_set_errno(errcode,
2232 "pthread_mutex_lock");
2233 if (ch == KEY_BACKSPACE) {
2234 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2235 return NULL;
2237 if (view->searching == TOG_SEARCH_FORWARD)
2238 entry = TAILQ_NEXT(s->search_entry, entry);
2239 else
2240 entry = TAILQ_PREV(s->search_entry,
2241 commit_queue_head, entry);
2242 } else if (s->matched_entry) {
2243 if (view->searching == TOG_SEARCH_FORWARD)
2244 entry = TAILQ_NEXT(s->matched_entry, entry);
2245 else
2246 entry = TAILQ_PREV(s->matched_entry,
2247 commit_queue_head, entry);
2248 } else {
2249 entry = s->selected_entry;
2252 while (1) {
2253 int have_match = 0;
2255 if (entry == NULL) {
2256 if (s->thread_args.log_complete ||
2257 view->searching == TOG_SEARCH_BACKWARD) {
2258 view->search_next_done =
2259 (s->matched_entry == NULL ?
2260 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2261 s->search_entry = NULL;
2262 return NULL;
2265 * Poke the log thread for more commits and return,
2266 * allowing the main loop to make progress. Search
2267 * will resume at s->search_entry once we come back.
2269 s->thread_args.commits_needed++;
2270 return trigger_log_thread(view, 0);
2273 err = match_commit(&have_match, entry->id, entry->commit,
2274 &view->regex);
2275 if (err)
2276 break;
2277 if (have_match) {
2278 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2279 s->matched_entry = entry;
2280 break;
2283 s->search_entry = entry;
2284 if (view->searching == TOG_SEARCH_FORWARD)
2285 entry = TAILQ_NEXT(entry, entry);
2286 else
2287 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2290 if (s->matched_entry) {
2291 int cur = s->selected_entry->idx;
2292 while (cur < s->matched_entry->idx) {
2293 err = input_log_view(NULL, view, KEY_DOWN);
2294 if (err)
2295 return err;
2296 cur++;
2298 while (cur > s->matched_entry->idx) {
2299 err = input_log_view(NULL, view, KEY_UP);
2300 if (err)
2301 return err;
2302 cur--;
2306 s->search_entry = NULL;
2308 return NULL;
2311 static const struct got_error *
2312 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2313 struct got_repository *repo, const char *head_ref_name,
2314 const char *in_repo_path, int log_branches)
2316 const struct got_error *err = NULL;
2317 struct tog_log_view_state *s = &view->state.log;
2318 struct got_repository *thread_repo = NULL;
2319 struct got_commit_graph *thread_graph = NULL;
2320 int errcode;
2321 int *pack_fds = NULL;
2323 if (in_repo_path != s->in_repo_path) {
2324 free(s->in_repo_path);
2325 s->in_repo_path = strdup(in_repo_path);
2326 if (s->in_repo_path == NULL)
2327 return got_error_from_errno("strdup");
2330 /* The commit queue only contains commits being displayed. */
2331 TAILQ_INIT(&s->commits.head);
2332 s->commits.ncommits = 0;
2334 s->repo = repo;
2335 if (head_ref_name) {
2336 s->head_ref_name = strdup(head_ref_name);
2337 if (s->head_ref_name == NULL) {
2338 err = got_error_from_errno("strdup");
2339 goto done;
2342 s->start_id = got_object_id_dup(start_id);
2343 if (s->start_id == NULL) {
2344 err = got_error_from_errno("got_object_id_dup");
2345 goto done;
2347 s->log_branches = log_branches;
2349 STAILQ_INIT(&s->colors);
2350 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2351 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2352 get_color_value("TOG_COLOR_COMMIT"));
2353 if (err)
2354 goto done;
2355 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2356 get_color_value("TOG_COLOR_AUTHOR"));
2357 if (err) {
2358 free_colors(&s->colors);
2359 goto done;
2361 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2362 get_color_value("TOG_COLOR_DATE"));
2363 if (err) {
2364 free_colors(&s->colors);
2365 goto done;
2369 view->show = show_log_view;
2370 view->input = input_log_view;
2371 view->close = close_log_view;
2372 view->search_start = search_start_log_view;
2373 view->search_next = search_next_log_view;
2375 err = got_repo_pack_fds_open(&pack_fds);
2376 if (err)
2377 goto done;
2378 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2379 pack_fds);
2380 if (err)
2381 goto done;
2382 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2383 !s->log_branches);
2384 if (err)
2385 goto done;
2386 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2387 s->repo, NULL, NULL);
2388 if (err)
2389 goto done;
2391 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2392 if (errcode) {
2393 err = got_error_set_errno(errcode, "pthread_cond_init");
2394 goto done;
2396 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2397 if (errcode) {
2398 err = got_error_set_errno(errcode, "pthread_cond_init");
2399 goto done;
2402 s->thread_args.commits_needed = view->nlines;
2403 s->thread_args.graph = thread_graph;
2404 s->thread_args.commits = &s->commits;
2405 s->thread_args.in_repo_path = s->in_repo_path;
2406 s->thread_args.start_id = s->start_id;
2407 s->thread_args.repo = thread_repo;
2408 s->thread_args.log_complete = 0;
2409 s->thread_args.quit = &s->quit;
2410 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2411 s->thread_args.selected_entry = &s->selected_entry;
2412 s->thread_args.searching = &view->searching;
2413 s->thread_args.search_next_done = &view->search_next_done;
2414 s->thread_args.regex = &view->regex;
2415 done:
2416 if (pack_fds) {
2417 const struct got_error *pack_err =
2418 got_repo_pack_fds_close(pack_fds);
2419 if (err == NULL)
2420 err = pack_err;
2422 if (err)
2423 close_log_view(view);
2424 return err;
2427 static const struct got_error *
2428 show_log_view(struct tog_view *view)
2430 const struct got_error *err;
2431 struct tog_log_view_state *s = &view->state.log;
2433 if (s->thread == NULL) {
2434 int errcode = pthread_create(&s->thread, NULL, log_thread,
2435 &s->thread_args);
2436 if (errcode)
2437 return got_error_set_errno(errcode, "pthread_create");
2438 if (s->thread_args.commits_needed > 0) {
2439 err = trigger_log_thread(view, 1);
2440 if (err)
2441 return err;
2445 return draw_commits(view);
2448 static const struct got_error *
2449 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2451 const struct got_error *err = NULL;
2452 struct tog_log_view_state *s = &view->state.log;
2453 struct tog_view *diff_view = NULL, *tree_view = NULL;
2454 struct tog_view *ref_view = NULL;
2455 struct commit_queue_entry *entry;
2456 int begin_x = 0, n, nscroll = view->nlines - 1;
2457 int *pack_fds = NULL;
2459 if (s->thread_args.load_all) {
2460 if (ch == KEY_BACKSPACE)
2461 s->thread_args.load_all = 0;
2462 else if (s->thread_args.log_complete) {
2463 s->thread_args.load_all = 0;
2464 log_scroll_down(view, s->commits.ncommits);
2465 s->selected = MIN(view->nlines - 2,
2466 s->commits.ncommits - 1);
2467 select_commit(s);
2469 return NULL;
2472 switch (ch) {
2473 case 'q':
2474 s->quit = 1;
2475 break;
2476 case 'k':
2477 case KEY_UP:
2478 case '<':
2479 case ',':
2480 case CTRL('p'):
2481 if (s->first_displayed_entry == NULL)
2482 break;
2483 if (s->selected > 0)
2484 s->selected--;
2485 else
2486 log_scroll_up(s, 1);
2487 select_commit(s);
2488 break;
2489 case 'g':
2490 case KEY_HOME:
2491 s->selected = 0;
2492 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2493 select_commit(s);
2494 break;
2495 case CTRL('u'):
2496 case 'u':
2497 nscroll /= 2;
2498 /* FALL THROUGH */
2499 case KEY_PPAGE:
2500 case CTRL('b'):
2501 if (s->first_displayed_entry == NULL)
2502 break;
2503 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2504 s->selected = MAX(0, s->selected - nscroll - 1);
2505 else
2506 log_scroll_up(s, nscroll);
2507 select_commit(s);
2508 break;
2509 case 'j':
2510 case KEY_DOWN:
2511 case '>':
2512 case '.':
2513 case CTRL('n'):
2514 if (s->first_displayed_entry == NULL)
2515 break;
2516 if (s->selected < MIN(view->nlines - 2,
2517 s->commits.ncommits - 1))
2518 s->selected++;
2519 else {
2520 err = log_scroll_down(view, 1);
2521 if (err)
2522 break;
2524 select_commit(s);
2525 break;
2526 case 'G':
2527 case KEY_END: {
2528 /* We don't know yet how many commits, so we're forced to
2529 * traverse them all. */
2530 if (!s->thread_args.log_complete) {
2531 s->thread_args.load_all = 1;
2532 return trigger_log_thread(view, 0);
2535 s->selected = 0;
2536 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2537 for (n = 0; n < view->nlines - 1; n++) {
2538 if (entry == NULL)
2539 break;
2540 s->first_displayed_entry = entry;
2541 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2543 if (n > 0)
2544 s->selected = n - 1;
2545 select_commit(s);
2546 break;
2548 case CTRL('d'):
2549 case 'd':
2550 nscroll /= 2;
2551 /* FALL THROUGH */
2552 case KEY_NPAGE:
2553 case CTRL('f'): {
2554 struct commit_queue_entry *first;
2555 first = s->first_displayed_entry;
2556 if (first == NULL)
2557 break;
2558 err = log_scroll_down(view, nscroll);
2559 if (err)
2560 break;
2561 if (first == s->first_displayed_entry &&
2562 s->selected < MIN(view->nlines - 2,
2563 s->commits.ncommits - 1)) {
2564 /* can't scroll further down */
2565 s->selected += MIN(s->last_displayed_entry->idx -
2566 s->selected_entry->idx, nscroll + 1);
2568 select_commit(s);
2569 break;
2571 case KEY_RESIZE:
2572 if (s->selected > view->nlines - 2)
2573 s->selected = view->nlines - 2;
2574 if (s->selected > s->commits.ncommits - 1)
2575 s->selected = s->commits.ncommits - 1;
2576 select_commit(s);
2577 if (s->commits.ncommits < view->nlines - 1 &&
2578 !s->thread_args.log_complete) {
2579 s->thread_args.commits_needed += (view->nlines - 1) -
2580 s->commits.ncommits;
2581 err = trigger_log_thread(view, 1);
2583 break;
2584 case KEY_ENTER:
2585 case ' ':
2586 case '\r':
2587 if (s->selected_entry == NULL)
2588 break;
2589 if (view_is_parent_view(view))
2590 begin_x = view_split_begin_x(view->begin_x);
2591 err = open_diff_view_for_commit(&diff_view, begin_x,
2592 s->selected_entry->commit, s->selected_entry->id,
2593 view, s->repo);
2594 if (err)
2595 break;
2596 view->focussed = 0;
2597 diff_view->focussed = 1;
2598 if (view_is_parent_view(view)) {
2599 err = view_close_child(view);
2600 if (err)
2601 return err;
2602 view_set_child(view, diff_view);
2603 view->focus_child = 1;
2604 } else
2605 *new_view = diff_view;
2606 break;
2607 case 't':
2608 if (s->selected_entry == NULL)
2609 break;
2610 if (view_is_parent_view(view))
2611 begin_x = view_split_begin_x(view->begin_x);
2612 err = browse_commit_tree(&tree_view, begin_x,
2613 s->selected_entry, s->in_repo_path, s->head_ref_name,
2614 s->repo);
2615 if (err)
2616 break;
2617 view->focussed = 0;
2618 tree_view->focussed = 1;
2619 if (view_is_parent_view(view)) {
2620 err = view_close_child(view);
2621 if (err)
2622 return err;
2623 view_set_child(view, tree_view);
2624 view->focus_child = 1;
2625 } else
2626 *new_view = tree_view;
2627 break;
2628 case KEY_BACKSPACE:
2629 case CTRL('l'):
2630 case 'B':
2631 if (ch == KEY_BACKSPACE &&
2632 got_path_is_root_dir(s->in_repo_path))
2633 break;
2634 err = stop_log_thread(s);
2635 if (err)
2636 return err;
2637 if (ch == KEY_BACKSPACE) {
2638 char *parent_path;
2639 err = got_path_dirname(&parent_path, s->in_repo_path);
2640 if (err)
2641 return err;
2642 free(s->in_repo_path);
2643 s->in_repo_path = parent_path;
2644 s->thread_args.in_repo_path = s->in_repo_path;
2645 } else if (ch == CTRL('l')) {
2646 struct got_object_id *start_id;
2647 err = got_repo_match_object_id(&start_id, NULL,
2648 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2649 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2650 if (err)
2651 return err;
2652 free(s->start_id);
2653 s->start_id = start_id;
2654 s->thread_args.start_id = s->start_id;
2655 } else /* 'B' */
2656 s->log_branches = !s->log_branches;
2658 err = got_repo_pack_fds_open(&pack_fds);
2659 if (err)
2660 return err;
2661 err = got_repo_open(&s->thread_args.repo,
2662 got_repo_get_path(s->repo), NULL, pack_fds);
2663 if (err)
2664 return err;
2665 err = got_repo_pack_fds_close(pack_fds);
2666 if (err)
2667 return err;
2668 tog_free_refs();
2669 err = tog_load_refs(s->repo, 0);
2670 if (err)
2671 return err;
2672 err = got_commit_graph_open(&s->thread_args.graph,
2673 s->in_repo_path, !s->log_branches);
2674 if (err)
2675 return err;
2676 err = got_commit_graph_iter_start(s->thread_args.graph,
2677 s->start_id, s->repo, NULL, NULL);
2678 if (err)
2679 return err;
2680 free_commits(&s->commits);
2681 s->first_displayed_entry = NULL;
2682 s->last_displayed_entry = NULL;
2683 s->selected_entry = NULL;
2684 s->selected = 0;
2685 s->thread_args.log_complete = 0;
2686 s->quit = 0;
2687 s->thread_args.commits_needed = view->nlines;
2688 break;
2689 case 'r':
2690 if (view_is_parent_view(view))
2691 begin_x = view_split_begin_x(view->begin_x);
2692 ref_view = view_open(view->nlines, view->ncols,
2693 view->begin_y, begin_x, TOG_VIEW_REF);
2694 if (ref_view == NULL)
2695 return got_error_from_errno("view_open");
2696 err = open_ref_view(ref_view, s->repo);
2697 if (err) {
2698 view_close(ref_view);
2699 return err;
2701 view->focussed = 0;
2702 ref_view->focussed = 1;
2703 if (view_is_parent_view(view)) {
2704 err = view_close_child(view);
2705 if (err)
2706 return err;
2707 view_set_child(view, ref_view);
2708 view->focus_child = 1;
2709 } else
2710 *new_view = ref_view;
2711 break;
2712 default:
2713 break;
2716 return err;
2719 static const struct got_error *
2720 apply_unveil(const char *repo_path, const char *worktree_path)
2722 const struct got_error *error;
2724 #ifdef PROFILE
2725 if (unveil("gmon.out", "rwc") != 0)
2726 return got_error_from_errno2("unveil", "gmon.out");
2727 #endif
2728 if (repo_path && unveil(repo_path, "r") != 0)
2729 return got_error_from_errno2("unveil", repo_path);
2731 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2732 return got_error_from_errno2("unveil", worktree_path);
2734 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2735 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2737 error = got_privsep_unveil_exec_helpers();
2738 if (error != NULL)
2739 return error;
2741 if (unveil(NULL, NULL) != 0)
2742 return got_error_from_errno("unveil");
2744 return NULL;
2747 static void
2748 init_curses(void)
2751 * Override default signal handlers before starting ncurses.
2752 * This should prevent ncurses from installing its own
2753 * broken cleanup() signal handler.
2755 signal(SIGWINCH, tog_sigwinch);
2756 signal(SIGPIPE, tog_sigpipe);
2757 signal(SIGCONT, tog_sigcont);
2758 signal(SIGINT, tog_sigint);
2759 signal(SIGTERM, tog_sigterm);
2761 initscr();
2762 cbreak();
2763 halfdelay(1); /* Do fast refresh while initial view is loading. */
2764 noecho();
2765 nonl();
2766 intrflush(stdscr, FALSE);
2767 keypad(stdscr, TRUE);
2768 curs_set(0);
2769 if (getenv("TOG_COLORS") != NULL) {
2770 start_color();
2771 use_default_colors();
2775 static const struct got_error *
2776 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2777 struct got_repository *repo, struct got_worktree *worktree)
2779 const struct got_error *err = NULL;
2781 if (argc == 0) {
2782 *in_repo_path = strdup("/");
2783 if (*in_repo_path == NULL)
2784 return got_error_from_errno("strdup");
2785 return NULL;
2788 if (worktree) {
2789 const char *prefix = got_worktree_get_path_prefix(worktree);
2790 char *p;
2792 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2793 if (err)
2794 return err;
2795 if (asprintf(in_repo_path, "%s%s%s", prefix,
2796 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2797 p) == -1) {
2798 err = got_error_from_errno("asprintf");
2799 *in_repo_path = NULL;
2801 free(p);
2802 } else
2803 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2805 return err;
2808 static const struct got_error *
2809 cmd_log(int argc, char *argv[])
2811 const struct got_error *error;
2812 struct got_repository *repo = NULL;
2813 struct got_worktree *worktree = NULL;
2814 struct got_object_id *start_id = NULL;
2815 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2816 char *start_commit = NULL, *label = NULL;
2817 struct got_reference *ref = NULL;
2818 const char *head_ref_name = NULL;
2819 int ch, log_branches = 0;
2820 struct tog_view *view;
2821 int *pack_fds = NULL;
2823 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2824 switch (ch) {
2825 case 'b':
2826 log_branches = 1;
2827 break;
2828 case 'c':
2829 start_commit = optarg;
2830 break;
2831 case 'r':
2832 repo_path = realpath(optarg, NULL);
2833 if (repo_path == NULL)
2834 return got_error_from_errno2("realpath",
2835 optarg);
2836 break;
2837 default:
2838 usage_log();
2839 /* NOTREACHED */
2843 argc -= optind;
2844 argv += optind;
2846 if (argc > 1)
2847 usage_log();
2849 error = got_repo_pack_fds_open(&pack_fds);
2850 if (error != NULL)
2851 goto done;
2853 if (repo_path == NULL) {
2854 cwd = getcwd(NULL, 0);
2855 if (cwd == NULL)
2856 return got_error_from_errno("getcwd");
2857 error = got_worktree_open(&worktree, cwd);
2858 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2859 goto done;
2860 if (worktree)
2861 repo_path =
2862 strdup(got_worktree_get_repo_path(worktree));
2863 else
2864 repo_path = strdup(cwd);
2865 if (repo_path == NULL) {
2866 error = got_error_from_errno("strdup");
2867 goto done;
2871 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2872 if (error != NULL)
2873 goto done;
2875 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2876 repo, worktree);
2877 if (error)
2878 goto done;
2880 init_curses();
2882 error = apply_unveil(got_repo_get_path(repo),
2883 worktree ? got_worktree_get_root_path(worktree) : NULL);
2884 if (error)
2885 goto done;
2887 /* already loaded by tog_log_with_path()? */
2888 if (TAILQ_EMPTY(&tog_refs)) {
2889 error = tog_load_refs(repo, 0);
2890 if (error)
2891 goto done;
2894 if (start_commit == NULL) {
2895 error = got_repo_match_object_id(&start_id, &label,
2896 worktree ? got_worktree_get_head_ref_name(worktree) :
2897 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2898 if (error)
2899 goto done;
2900 head_ref_name = label;
2901 } else {
2902 error = got_ref_open(&ref, repo, start_commit, 0);
2903 if (error == NULL)
2904 head_ref_name = got_ref_get_name(ref);
2905 else if (error->code != GOT_ERR_NOT_REF)
2906 goto done;
2907 error = got_repo_match_object_id(&start_id, NULL,
2908 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2909 if (error)
2910 goto done;
2913 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2914 if (view == NULL) {
2915 error = got_error_from_errno("view_open");
2916 goto done;
2918 error = open_log_view(view, start_id, repo, head_ref_name,
2919 in_repo_path, log_branches);
2920 if (error)
2921 goto done;
2922 if (worktree) {
2923 /* Release work tree lock. */
2924 got_worktree_close(worktree);
2925 worktree = NULL;
2927 error = view_loop(view);
2928 done:
2929 free(in_repo_path);
2930 free(repo_path);
2931 free(cwd);
2932 free(start_id);
2933 free(label);
2934 if (ref)
2935 got_ref_close(ref);
2936 if (repo) {
2937 const struct got_error *close_err = got_repo_close(repo);
2938 if (error == NULL)
2939 error = close_err;
2941 if (worktree)
2942 got_worktree_close(worktree);
2943 if (pack_fds) {
2944 const struct got_error *pack_err =
2945 got_repo_pack_fds_close(pack_fds);
2946 if (error == NULL)
2947 error = pack_err;
2949 tog_free_refs();
2950 return error;
2953 __dead static void
2954 usage_diff(void)
2956 endwin();
2957 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2958 "[-w] object1 object2\n", getprogname());
2959 exit(1);
2962 static int
2963 match_line(const char *line, regex_t *regex, size_t nmatch,
2964 regmatch_t *regmatch)
2966 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2969 struct tog_color *
2970 match_color(struct tog_colors *colors, const char *line)
2972 struct tog_color *tc = NULL;
2974 STAILQ_FOREACH(tc, colors, entry) {
2975 if (match_line(line, &tc->regex, 0, NULL))
2976 return tc;
2979 return NULL;
2982 static const struct got_error *
2983 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2984 WINDOW *window, regmatch_t *regmatch)
2986 const struct got_error *err = NULL;
2987 wchar_t *wline;
2988 int width;
2989 char *s;
2991 *wtotal = 0;
2993 s = strndup(line, regmatch->rm_so);
2994 if (s == NULL)
2995 return got_error_from_errno("strndup");
2997 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2998 if (err) {
2999 free(s);
3000 return err;
3002 waddwstr(window, wline);
3003 free(wline);
3004 free(s);
3005 wlimit -= width;
3006 *wtotal += width;
3008 if (wlimit > 0) {
3009 s = strndup(line + regmatch->rm_so,
3010 regmatch->rm_eo - regmatch->rm_so);
3011 if (s == NULL) {
3012 err = got_error_from_errno("strndup");
3013 free(s);
3014 return err;
3016 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3017 if (err) {
3018 free(s);
3019 return err;
3021 wattr_on(window, A_STANDOUT, NULL);
3022 waddwstr(window, wline);
3023 wattr_off(window, A_STANDOUT, NULL);
3024 free(wline);
3025 free(s);
3026 wlimit -= width;
3027 *wtotal += width;
3030 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3031 err = format_line(&wline, &width,
3032 line + regmatch->rm_eo, wlimit, col_tab_align);
3033 if (err)
3034 return err;
3035 waddwstr(window, wline);
3036 free(wline);
3037 *wtotal += width;
3040 return NULL;
3043 static const struct got_error *
3044 draw_file(struct tog_view *view, const char *header)
3046 struct tog_diff_view_state *s = &view->state.diff;
3047 regmatch_t *regmatch = &view->regmatch;
3048 const struct got_error *err;
3049 int nprinted = 0;
3050 char *line;
3051 size_t linesize = 0;
3052 ssize_t linelen;
3053 struct tog_color *tc;
3054 wchar_t *wline;
3055 int width;
3056 int max_lines = view->nlines;
3057 int nlines = s->nlines;
3058 off_t line_offset;
3060 line_offset = s->line_offsets[s->first_displayed_line - 1];
3061 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3062 return got_error_from_errno("fseek");
3064 werase(view->window);
3066 if (header) {
3067 if (asprintf(&line, "[%d/%d] %s",
3068 s->first_displayed_line - 1 + s->selected_line, nlines,
3069 header) == -1)
3070 return got_error_from_errno("asprintf");
3071 err = format_line(&wline, &width, line, view->ncols, 0);
3072 free(line);
3073 if (err)
3074 return err;
3076 if (view_needs_focus_indication(view))
3077 wstandout(view->window);
3078 waddwstr(view->window, wline);
3079 free(wline);
3080 wline = NULL;
3081 if (view_needs_focus_indication(view))
3082 wstandend(view->window);
3083 if (width <= view->ncols - 1)
3084 waddch(view->window, '\n');
3086 if (max_lines <= 1)
3087 return NULL;
3088 max_lines--;
3091 s->eof = 0;
3092 line = NULL;
3093 while (max_lines > 0 && nprinted < max_lines) {
3094 linelen = getline(&line, &linesize, s->f);
3095 if (linelen == -1) {
3096 if (feof(s->f)) {
3097 s->eof = 1;
3098 break;
3100 free(line);
3101 return got_ferror(s->f, GOT_ERR_IO);
3104 tc = match_color(&s->colors, line);
3105 if (tc)
3106 wattr_on(view->window,
3107 COLOR_PAIR(tc->colorpair), NULL);
3108 if (s->first_displayed_line + nprinted == s->matched_line &&
3109 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3110 err = add_matched_line(&width, line, view->ncols, 0,
3111 view->window, regmatch);
3112 if (err) {
3113 free(line);
3114 return err;
3116 } else {
3117 err = format_line(&wline, &width, line, view->ncols, 0);
3118 if (err) {
3119 free(line);
3120 return err;
3122 waddwstr(view->window, wline);
3123 free(wline);
3124 wline = NULL;
3126 if (tc)
3127 wattr_off(view->window,
3128 COLOR_PAIR(tc->colorpair), NULL);
3129 if (width <= view->ncols - 1)
3130 waddch(view->window, '\n');
3131 nprinted++;
3133 free(line);
3134 if (nprinted >= 1)
3135 s->last_displayed_line = s->first_displayed_line +
3136 (nprinted - 1);
3137 else
3138 s->last_displayed_line = s->first_displayed_line;
3140 view_vborder(view);
3142 if (s->eof) {
3143 while (nprinted < view->nlines) {
3144 waddch(view->window, '\n');
3145 nprinted++;
3148 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3149 if (err) {
3150 return err;
3153 wstandout(view->window);
3154 waddwstr(view->window, wline);
3155 free(wline);
3156 wline = NULL;
3157 wstandend(view->window);
3160 return NULL;
3163 static char *
3164 get_datestr(time_t *time, char *datebuf)
3166 struct tm mytm, *tm;
3167 char *p, *s;
3169 tm = gmtime_r(time, &mytm);
3170 if (tm == NULL)
3171 return NULL;
3172 s = asctime_r(tm, datebuf);
3173 if (s == NULL)
3174 return NULL;
3175 p = strchr(s, '\n');
3176 if (p)
3177 *p = '\0';
3178 return s;
3181 static const struct got_error *
3182 get_changed_paths(struct got_pathlist_head *paths,
3183 struct got_commit_object *commit, struct got_repository *repo)
3185 const struct got_error *err = NULL;
3186 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3187 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3188 struct got_object_qid *qid;
3190 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3191 if (qid != NULL) {
3192 struct got_commit_object *pcommit;
3193 err = got_object_open_as_commit(&pcommit, repo,
3194 &qid->id);
3195 if (err)
3196 return err;
3198 tree_id1 = got_object_id_dup(
3199 got_object_commit_get_tree_id(pcommit));
3200 if (tree_id1 == NULL) {
3201 got_object_commit_close(pcommit);
3202 return got_error_from_errno("got_object_id_dup");
3204 got_object_commit_close(pcommit);
3208 if (tree_id1) {
3209 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3210 if (err)
3211 goto done;
3214 tree_id2 = got_object_commit_get_tree_id(commit);
3215 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3216 if (err)
3217 goto done;
3219 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3220 got_diff_tree_collect_changed_paths, paths, 0);
3221 done:
3222 if (tree1)
3223 got_object_tree_close(tree1);
3224 if (tree2)
3225 got_object_tree_close(tree2);
3226 free(tree_id1);
3227 return err;
3230 static const struct got_error *
3231 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3233 off_t *p;
3235 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3236 if (p == NULL)
3237 return got_error_from_errno("reallocarray");
3238 *line_offsets = p;
3239 (*line_offsets)[*nlines] = off;
3240 (*nlines)++;
3241 return NULL;
3244 static const struct got_error *
3245 write_commit_info(off_t **line_offsets, size_t *nlines,
3246 struct got_object_id *commit_id, struct got_reflist_head *refs,
3247 struct got_repository *repo, FILE *outfile)
3249 const struct got_error *err = NULL;
3250 char datebuf[26], *datestr;
3251 struct got_commit_object *commit;
3252 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3253 time_t committer_time;
3254 const char *author, *committer;
3255 char *refs_str = NULL;
3256 struct got_pathlist_head changed_paths;
3257 struct got_pathlist_entry *pe;
3258 off_t outoff = 0;
3259 int n;
3261 TAILQ_INIT(&changed_paths);
3263 if (refs) {
3264 err = build_refs_str(&refs_str, refs, commit_id, repo);
3265 if (err)
3266 return err;
3269 err = got_object_open_as_commit(&commit, repo, commit_id);
3270 if (err)
3271 return err;
3273 err = got_object_id_str(&id_str, commit_id);
3274 if (err) {
3275 err = got_error_from_errno("got_object_id_str");
3276 goto done;
3279 err = add_line_offset(line_offsets, nlines, 0);
3280 if (err)
3281 goto done;
3283 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3284 refs_str ? refs_str : "", refs_str ? ")" : "");
3285 if (n < 0) {
3286 err = got_error_from_errno("fprintf");
3287 goto done;
3289 outoff += n;
3290 err = add_line_offset(line_offsets, nlines, outoff);
3291 if (err)
3292 goto done;
3294 n = fprintf(outfile, "from: %s\n",
3295 got_object_commit_get_author(commit));
3296 if (n < 0) {
3297 err = got_error_from_errno("fprintf");
3298 goto done;
3300 outoff += n;
3301 err = add_line_offset(line_offsets, nlines, outoff);
3302 if (err)
3303 goto done;
3305 committer_time = got_object_commit_get_committer_time(commit);
3306 datestr = get_datestr(&committer_time, datebuf);
3307 if (datestr) {
3308 n = fprintf(outfile, "date: %s UTC\n", datestr);
3309 if (n < 0) {
3310 err = got_error_from_errno("fprintf");
3311 goto done;
3313 outoff += n;
3314 err = add_line_offset(line_offsets, nlines, outoff);
3315 if (err)
3316 goto done;
3318 author = got_object_commit_get_author(commit);
3319 committer = got_object_commit_get_committer(commit);
3320 if (strcmp(author, committer) != 0) {
3321 n = fprintf(outfile, "via: %s\n", committer);
3322 if (n < 0) {
3323 err = got_error_from_errno("fprintf");
3324 goto done;
3326 outoff += n;
3327 err = add_line_offset(line_offsets, nlines, outoff);
3328 if (err)
3329 goto done;
3331 if (got_object_commit_get_nparents(commit) > 1) {
3332 const struct got_object_id_queue *parent_ids;
3333 struct got_object_qid *qid;
3334 int pn = 1;
3335 parent_ids = got_object_commit_get_parent_ids(commit);
3336 STAILQ_FOREACH(qid, parent_ids, entry) {
3337 err = got_object_id_str(&id_str, &qid->id);
3338 if (err)
3339 goto done;
3340 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3341 if (n < 0) {
3342 err = got_error_from_errno("fprintf");
3343 goto done;
3345 outoff += n;
3346 err = add_line_offset(line_offsets, nlines, outoff);
3347 if (err)
3348 goto done;
3349 free(id_str);
3350 id_str = NULL;
3354 err = got_object_commit_get_logmsg(&logmsg, commit);
3355 if (err)
3356 goto done;
3357 s = logmsg;
3358 while ((line = strsep(&s, "\n")) != NULL) {
3359 n = fprintf(outfile, "%s\n", line);
3360 if (n < 0) {
3361 err = got_error_from_errno("fprintf");
3362 goto done;
3364 outoff += n;
3365 err = add_line_offset(line_offsets, nlines, outoff);
3366 if (err)
3367 goto done;
3370 err = get_changed_paths(&changed_paths, commit, repo);
3371 if (err)
3372 goto done;
3373 TAILQ_FOREACH(pe, &changed_paths, entry) {
3374 struct got_diff_changed_path *cp = pe->data;
3375 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3376 if (n < 0) {
3377 err = got_error_from_errno("fprintf");
3378 goto done;
3380 outoff += n;
3381 err = add_line_offset(line_offsets, nlines, outoff);
3382 if (err)
3383 goto done;
3384 free((char *)pe->path);
3385 free(pe->data);
3388 fputc('\n', outfile);
3389 outoff++;
3390 err = add_line_offset(line_offsets, nlines, outoff);
3391 done:
3392 got_pathlist_free(&changed_paths);
3393 free(id_str);
3394 free(logmsg);
3395 free(refs_str);
3396 got_object_commit_close(commit);
3397 if (err) {
3398 free(*line_offsets);
3399 *line_offsets = NULL;
3400 *nlines = 0;
3402 return err;
3405 static const struct got_error *
3406 create_diff(struct tog_diff_view_state *s)
3408 const struct got_error *err = NULL;
3409 FILE *f = NULL;
3410 int obj_type;
3412 free(s->line_offsets);
3413 s->line_offsets = malloc(sizeof(off_t));
3414 if (s->line_offsets == NULL)
3415 return got_error_from_errno("malloc");
3416 s->nlines = 0;
3418 f = got_opentemp();
3419 if (f == NULL) {
3420 err = got_error_from_errno("got_opentemp");
3421 goto done;
3423 if (s->f && fclose(s->f) == EOF) {
3424 err = got_error_from_errno("fclose");
3425 goto done;
3427 s->f = f;
3429 if (s->id1)
3430 err = got_object_get_type(&obj_type, s->repo, s->id1);
3431 else
3432 err = got_object_get_type(&obj_type, s->repo, s->id2);
3433 if (err)
3434 goto done;
3436 switch (obj_type) {
3437 case GOT_OBJ_TYPE_BLOB:
3438 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3439 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3440 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3441 s->repo, s->f);
3442 break;
3443 case GOT_OBJ_TYPE_TREE:
3444 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3445 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3446 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3447 break;
3448 case GOT_OBJ_TYPE_COMMIT: {
3449 const struct got_object_id_queue *parent_ids;
3450 struct got_object_qid *pid;
3451 struct got_commit_object *commit2;
3452 struct got_reflist_head *refs;
3454 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3455 if (err)
3456 goto done;
3457 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3458 /* Show commit info if we're diffing to a parent/root commit. */
3459 if (s->id1 == NULL) {
3460 err = write_commit_info(&s->line_offsets, &s->nlines,
3461 s->id2, refs, s->repo, s->f);
3462 if (err)
3463 goto done;
3464 } else {
3465 parent_ids = got_object_commit_get_parent_ids(commit2);
3466 STAILQ_FOREACH(pid, parent_ids, entry) {
3467 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3468 err = write_commit_info(
3469 &s->line_offsets, &s->nlines,
3470 s->id2, refs, s->repo, s->f);
3471 if (err)
3472 goto done;
3473 break;
3477 got_object_commit_close(commit2);
3479 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3480 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3481 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3482 break;
3484 default:
3485 err = got_error(GOT_ERR_OBJ_TYPE);
3486 break;
3488 if (err)
3489 goto done;
3490 done:
3491 if (s->f && fflush(s->f) != 0 && err == NULL)
3492 err = got_error_from_errno("fflush");
3493 return err;
3496 static void
3497 diff_view_indicate_progress(struct tog_view *view)
3499 mvwaddstr(view->window, 0, 0, "diffing...");
3500 update_panels();
3501 doupdate();
3504 static const struct got_error *
3505 search_start_diff_view(struct tog_view *view)
3507 struct tog_diff_view_state *s = &view->state.diff;
3509 s->matched_line = 0;
3510 return NULL;
3513 static const struct got_error *
3514 search_next_diff_view(struct tog_view *view)
3516 struct tog_diff_view_state *s = &view->state.diff;
3517 int lineno;
3518 char *line = NULL;
3519 size_t linesize = 0;
3520 ssize_t linelen;
3522 if (!view->searching) {
3523 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3524 return NULL;
3527 if (s->matched_line) {
3528 if (view->searching == TOG_SEARCH_FORWARD)
3529 lineno = s->matched_line + 1;
3530 else
3531 lineno = s->matched_line - 1;
3532 } else
3533 lineno = s->first_displayed_line;
3535 while (1) {
3536 off_t offset;
3538 if (lineno <= 0 || lineno > s->nlines) {
3539 if (s->matched_line == 0) {
3540 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3541 break;
3544 if (view->searching == TOG_SEARCH_FORWARD)
3545 lineno = 1;
3546 else
3547 lineno = s->nlines;
3550 offset = s->line_offsets[lineno - 1];
3551 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3552 free(line);
3553 return got_error_from_errno("fseeko");
3555 linelen = getline(&line, &linesize, s->f);
3556 if (linelen != -1 &&
3557 match_line(line, &view->regex, 1, &view->regmatch)) {
3558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3559 s->matched_line = lineno;
3560 break;
3562 if (view->searching == TOG_SEARCH_FORWARD)
3563 lineno++;
3564 else
3565 lineno--;
3567 free(line);
3569 if (s->matched_line) {
3570 s->first_displayed_line = s->matched_line;
3571 s->selected_line = 1;
3574 return NULL;
3577 static const struct got_error *
3578 close_diff_view(struct tog_view *view)
3580 const struct got_error *err = NULL;
3581 struct tog_diff_view_state *s = &view->state.diff;
3583 free(s->id1);
3584 s->id1 = NULL;
3585 free(s->id2);
3586 s->id2 = NULL;
3587 if (s->f && fclose(s->f) == EOF)
3588 err = got_error_from_errno("fclose");
3589 s->f = NULL;
3590 if (s->f1 && fclose(s->f1) == EOF)
3591 err = got_error_from_errno("fclose");
3592 s->f1 = NULL;
3593 if (s->f2 && fclose(s->f2) == EOF)
3594 err = got_error_from_errno("fclose");
3595 s->f2 = NULL;
3596 free_colors(&s->colors);
3597 free(s->line_offsets);
3598 s->line_offsets = NULL;
3599 s->nlines = 0;
3600 return err;
3603 static const struct got_error *
3604 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3605 struct got_object_id *id2, const char *label1, const char *label2,
3606 int diff_context, int ignore_whitespace, int force_text_diff,
3607 struct tog_view *log_view, struct got_repository *repo)
3609 const struct got_error *err;
3610 struct tog_diff_view_state *s = &view->state.diff;
3612 memset(s, 0, sizeof(*s));
3614 if (id1 != NULL && id2 != NULL) {
3615 int type1, type2;
3616 err = got_object_get_type(&type1, repo, id1);
3617 if (err)
3618 return err;
3619 err = got_object_get_type(&type2, repo, id2);
3620 if (err)
3621 return err;
3623 if (type1 != type2)
3624 return got_error(GOT_ERR_OBJ_TYPE);
3626 s->first_displayed_line = 1;
3627 s->last_displayed_line = view->nlines;
3628 s->selected_line = 1;
3629 s->repo = repo;
3630 s->id1 = id1;
3631 s->id2 = id2;
3632 s->label1 = label1;
3633 s->label2 = label2;
3635 if (id1) {
3636 s->id1 = got_object_id_dup(id1);
3637 if (s->id1 == NULL)
3638 return got_error_from_errno("got_object_id_dup");
3639 s->f1 = got_opentemp();
3640 if (s->f1 == NULL) {
3641 err = got_error_from_errno("got_opentemp");
3642 goto done;
3644 } else
3645 s->id1 = NULL;
3647 s->id2 = got_object_id_dup(id2);
3648 if (s->id2 == NULL) {
3649 err = got_error_from_errno("got_object_id_dup");
3650 goto done;
3653 s->f2 = got_opentemp();
3654 if (s->f2 == NULL) {
3655 err = got_error_from_errno("got_opentemp");
3656 goto done;
3659 s->first_displayed_line = 1;
3660 s->last_displayed_line = view->nlines;
3661 s->diff_context = diff_context;
3662 s->ignore_whitespace = ignore_whitespace;
3663 s->force_text_diff = force_text_diff;
3664 s->log_view = log_view;
3665 s->repo = repo;
3667 STAILQ_INIT(&s->colors);
3668 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3669 err = add_color(&s->colors,
3670 "^-", TOG_COLOR_DIFF_MINUS,
3671 get_color_value("TOG_COLOR_DIFF_MINUS"));
3672 if (err)
3673 goto done;
3674 err = add_color(&s->colors, "^\\+",
3675 TOG_COLOR_DIFF_PLUS,
3676 get_color_value("TOG_COLOR_DIFF_PLUS"));
3677 if (err)
3678 goto done;
3679 err = add_color(&s->colors,
3680 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3681 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3682 if (err)
3683 goto done;
3685 err = add_color(&s->colors,
3686 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3687 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3688 get_color_value("TOG_COLOR_DIFF_META"));
3689 if (err)
3690 goto done;
3692 err = add_color(&s->colors,
3693 "^(from|via): ", TOG_COLOR_AUTHOR,
3694 get_color_value("TOG_COLOR_AUTHOR"));
3695 if (err)
3696 goto done;
3698 err = add_color(&s->colors,
3699 "^date: ", TOG_COLOR_DATE,
3700 get_color_value("TOG_COLOR_DATE"));
3701 if (err)
3702 goto done;
3705 if (log_view && view_is_splitscreen(view))
3706 show_log_view(log_view); /* draw vborder */
3707 diff_view_indicate_progress(view);
3709 err = create_diff(s);
3711 view->show = show_diff_view;
3712 view->input = input_diff_view;
3713 view->close = close_diff_view;
3714 view->search_start = search_start_diff_view;
3715 view->search_next = search_next_diff_view;
3716 done:
3717 if (err)
3718 close_diff_view(view);
3719 return err;
3722 static const struct got_error *
3723 show_diff_view(struct tog_view *view)
3725 const struct got_error *err;
3726 struct tog_diff_view_state *s = &view->state.diff;
3727 char *id_str1 = NULL, *id_str2, *header;
3728 const char *label1, *label2;
3730 if (s->id1) {
3731 err = got_object_id_str(&id_str1, s->id1);
3732 if (err)
3733 return err;
3734 label1 = s->label1 ? : id_str1;
3735 } else
3736 label1 = "/dev/null";
3738 err = got_object_id_str(&id_str2, s->id2);
3739 if (err)
3740 return err;
3741 label2 = s->label2 ? : id_str2;
3743 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3744 err = got_error_from_errno("asprintf");
3745 free(id_str1);
3746 free(id_str2);
3747 return err;
3749 free(id_str1);
3750 free(id_str2);
3752 err = draw_file(view, header);
3753 free(header);
3754 return err;
3757 static const struct got_error *
3758 set_selected_commit(struct tog_diff_view_state *s,
3759 struct commit_queue_entry *entry)
3761 const struct got_error *err;
3762 const struct got_object_id_queue *parent_ids;
3763 struct got_commit_object *selected_commit;
3764 struct got_object_qid *pid;
3766 free(s->id2);
3767 s->id2 = got_object_id_dup(entry->id);
3768 if (s->id2 == NULL)
3769 return got_error_from_errno("got_object_id_dup");
3771 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3772 if (err)
3773 return err;
3774 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3775 free(s->id1);
3776 pid = STAILQ_FIRST(parent_ids);
3777 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3778 got_object_commit_close(selected_commit);
3779 return NULL;
3782 static const struct got_error *
3783 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3785 const struct got_error *err = NULL;
3786 struct tog_diff_view_state *s = &view->state.diff;
3787 struct tog_log_view_state *ls;
3788 struct commit_queue_entry *old_selected_entry;
3789 char *line = NULL;
3790 size_t linesize = 0;
3791 ssize_t linelen;
3792 int i, nscroll = view->nlines - 1;
3794 switch (ch) {
3795 case 'a':
3796 case 'w':
3797 if (ch == 'a')
3798 s->force_text_diff = !s->force_text_diff;
3799 if (ch == 'w')
3800 s->ignore_whitespace = !s->ignore_whitespace;
3801 wclear(view->window);
3802 s->first_displayed_line = 1;
3803 s->last_displayed_line = view->nlines;
3804 s->matched_line = 0;
3805 diff_view_indicate_progress(view);
3806 err = create_diff(s);
3807 break;
3808 case 'g':
3809 case KEY_HOME:
3810 s->first_displayed_line = 1;
3811 break;
3812 case 'G':
3813 case KEY_END:
3814 if (s->eof)
3815 break;
3817 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3818 s->eof = 1;
3819 break;
3820 case 'k':
3821 case KEY_UP:
3822 case CTRL('p'):
3823 if (s->first_displayed_line > 1)
3824 s->first_displayed_line--;
3825 break;
3826 case CTRL('u'):
3827 case 'u':
3828 nscroll /= 2;
3829 /* FALL THROUGH */
3830 case KEY_PPAGE:
3831 case CTRL('b'):
3832 if (s->first_displayed_line == 1)
3833 break;
3834 i = 0;
3835 while (i++ < nscroll && s->first_displayed_line > 1)
3836 s->first_displayed_line--;
3837 break;
3838 case 'j':
3839 case KEY_DOWN:
3840 case CTRL('n'):
3841 if (!s->eof)
3842 s->first_displayed_line++;
3843 break;
3844 case CTRL('d'):
3845 case 'd':
3846 nscroll /= 2;
3847 /* FALL THROUGH */
3848 case KEY_NPAGE:
3849 case CTRL('f'):
3850 case ' ':
3851 if (s->eof)
3852 break;
3853 i = 0;
3854 while (!s->eof && i++ < nscroll) {
3855 linelen = getline(&line, &linesize, s->f);
3856 s->first_displayed_line++;
3857 if (linelen == -1) {
3858 if (feof(s->f)) {
3859 s->eof = 1;
3860 } else
3861 err = got_ferror(s->f, GOT_ERR_IO);
3862 break;
3865 free(line);
3866 break;
3867 case '[':
3868 if (s->diff_context > 0) {
3869 s->diff_context--;
3870 s->matched_line = 0;
3871 diff_view_indicate_progress(view);
3872 err = create_diff(s);
3873 if (s->first_displayed_line + view->nlines - 1 >
3874 s->nlines) {
3875 s->first_displayed_line = 1;
3876 s->last_displayed_line = view->nlines;
3879 break;
3880 case ']':
3881 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3882 s->diff_context++;
3883 s->matched_line = 0;
3884 diff_view_indicate_progress(view);
3885 err = create_diff(s);
3887 break;
3888 case '<':
3889 case ',':
3890 if (s->log_view == NULL)
3891 break;
3892 ls = &s->log_view->state.log;
3893 old_selected_entry = ls->selected_entry;
3895 err = input_log_view(NULL, s->log_view, KEY_UP);
3896 if (err)
3897 break;
3899 if (old_selected_entry == ls->selected_entry)
3900 break;
3902 err = set_selected_commit(s, ls->selected_entry);
3903 if (err)
3904 break;
3906 s->first_displayed_line = 1;
3907 s->last_displayed_line = view->nlines;
3908 s->matched_line = 0;
3910 diff_view_indicate_progress(view);
3911 err = create_diff(s);
3912 break;
3913 case '>':
3914 case '.':
3915 if (s->log_view == NULL)
3916 break;
3917 ls = &s->log_view->state.log;
3918 old_selected_entry = ls->selected_entry;
3920 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3921 if (err)
3922 break;
3924 if (old_selected_entry == ls->selected_entry)
3925 break;
3927 err = set_selected_commit(s, ls->selected_entry);
3928 if (err)
3929 break;
3931 s->first_displayed_line = 1;
3932 s->last_displayed_line = view->nlines;
3933 s->matched_line = 0;
3935 diff_view_indicate_progress(view);
3936 err = create_diff(s);
3937 break;
3938 default:
3939 break;
3942 return err;
3945 static const struct got_error *
3946 cmd_diff(int argc, char *argv[])
3948 const struct got_error *error = NULL;
3949 struct got_repository *repo = NULL;
3950 struct got_worktree *worktree = NULL;
3951 struct got_object_id *id1 = NULL, *id2 = NULL;
3952 char *repo_path = NULL, *cwd = NULL;
3953 char *id_str1 = NULL, *id_str2 = NULL;
3954 char *label1 = NULL, *label2 = NULL;
3955 int diff_context = 3, ignore_whitespace = 0;
3956 int ch, force_text_diff = 0;
3957 const char *errstr;
3958 struct tog_view *view;
3959 int *pack_fds = NULL;
3961 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3962 switch (ch) {
3963 case 'a':
3964 force_text_diff = 1;
3965 break;
3966 case 'C':
3967 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3968 &errstr);
3969 if (errstr != NULL)
3970 errx(1, "number of context lines is %s: %s",
3971 errstr, errstr);
3972 break;
3973 case 'r':
3974 repo_path = realpath(optarg, NULL);
3975 if (repo_path == NULL)
3976 return got_error_from_errno2("realpath",
3977 optarg);
3978 got_path_strip_trailing_slashes(repo_path);
3979 break;
3980 case 'w':
3981 ignore_whitespace = 1;
3982 break;
3983 default:
3984 usage_diff();
3985 /* NOTREACHED */
3989 argc -= optind;
3990 argv += optind;
3992 if (argc == 0) {
3993 usage_diff(); /* TODO show local worktree changes */
3994 } else if (argc == 2) {
3995 id_str1 = argv[0];
3996 id_str2 = argv[1];
3997 } else
3998 usage_diff();
4000 error = got_repo_pack_fds_open(&pack_fds);
4001 if (error)
4002 goto done;
4004 if (repo_path == NULL) {
4005 cwd = getcwd(NULL, 0);
4006 if (cwd == NULL)
4007 return got_error_from_errno("getcwd");
4008 error = got_worktree_open(&worktree, cwd);
4009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4010 goto done;
4011 if (worktree)
4012 repo_path =
4013 strdup(got_worktree_get_repo_path(worktree));
4014 else
4015 repo_path = strdup(cwd);
4016 if (repo_path == NULL) {
4017 error = got_error_from_errno("strdup");
4018 goto done;
4022 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4023 if (error)
4024 goto done;
4026 init_curses();
4028 error = apply_unveil(got_repo_get_path(repo), NULL);
4029 if (error)
4030 goto done;
4032 error = tog_load_refs(repo, 0);
4033 if (error)
4034 goto done;
4036 error = got_repo_match_object_id(&id1, &label1, id_str1,
4037 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4038 if (error)
4039 goto done;
4041 error = got_repo_match_object_id(&id2, &label2, id_str2,
4042 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4043 if (error)
4044 goto done;
4046 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4047 if (view == NULL) {
4048 error = got_error_from_errno("view_open");
4049 goto done;
4051 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4052 ignore_whitespace, force_text_diff, NULL, repo);
4053 if (error)
4054 goto done;
4055 error = view_loop(view);
4056 done:
4057 free(label1);
4058 free(label2);
4059 free(repo_path);
4060 free(cwd);
4061 if (repo) {
4062 const struct got_error *close_err = got_repo_close(repo);
4063 if (error == NULL)
4064 error = close_err;
4066 if (worktree)
4067 got_worktree_close(worktree);
4068 if (pack_fds) {
4069 const struct got_error *pack_err =
4070 got_repo_pack_fds_close(pack_fds);
4071 if (error == NULL)
4072 error = pack_err;
4074 tog_free_refs();
4075 return error;
4078 __dead static void
4079 usage_blame(void)
4081 endwin();
4082 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4083 getprogname());
4084 exit(1);
4087 struct tog_blame_line {
4088 int annotated;
4089 struct got_object_id *id;
4092 static const struct got_error *
4093 draw_blame(struct tog_view *view)
4095 struct tog_blame_view_state *s = &view->state.blame;
4096 struct tog_blame *blame = &s->blame;
4097 regmatch_t *regmatch = &view->regmatch;
4098 const struct got_error *err;
4099 int lineno = 0, nprinted = 0;
4100 char *line = NULL;
4101 size_t linesize = 0;
4102 ssize_t linelen;
4103 wchar_t *wline;
4104 int width;
4105 struct tog_blame_line *blame_line;
4106 struct got_object_id *prev_id = NULL;
4107 char *id_str;
4108 struct tog_color *tc;
4110 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4111 if (err)
4112 return err;
4114 rewind(blame->f);
4115 werase(view->window);
4117 if (asprintf(&line, "commit %s", id_str) == -1) {
4118 err = got_error_from_errno("asprintf");
4119 free(id_str);
4120 return err;
4123 err = format_line(&wline, &width, line, view->ncols, 0);
4124 free(line);
4125 line = NULL;
4126 if (err)
4127 return err;
4128 if (view_needs_focus_indication(view))
4129 wstandout(view->window);
4130 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4131 if (tc)
4132 wattr_on(view->window,
4133 COLOR_PAIR(tc->colorpair), NULL);
4134 waddwstr(view->window, wline);
4135 if (tc)
4136 wattr_off(view->window,
4137 COLOR_PAIR(tc->colorpair), NULL);
4138 if (view_needs_focus_indication(view))
4139 wstandend(view->window);
4140 free(wline);
4141 wline = NULL;
4142 if (width < view->ncols - 1)
4143 waddch(view->window, '\n');
4145 if (asprintf(&line, "[%d/%d] %s%s",
4146 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4147 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4148 free(id_str);
4149 return got_error_from_errno("asprintf");
4151 free(id_str);
4152 err = format_line(&wline, &width, line, view->ncols, 0);
4153 free(line);
4154 line = NULL;
4155 if (err)
4156 return err;
4157 waddwstr(view->window, wline);
4158 free(wline);
4159 wline = NULL;
4160 if (width < view->ncols - 1)
4161 waddch(view->window, '\n');
4163 s->eof = 0;
4164 while (nprinted < view->nlines - 2) {
4165 linelen = getline(&line, &linesize, blame->f);
4166 if (linelen == -1) {
4167 if (feof(blame->f)) {
4168 s->eof = 1;
4169 break;
4171 free(line);
4172 return got_ferror(blame->f, GOT_ERR_IO);
4174 if (++lineno < s->first_displayed_line)
4175 continue;
4177 if (view->focussed && nprinted == s->selected_line - 1)
4178 wstandout(view->window);
4180 if (blame->nlines > 0) {
4181 blame_line = &blame->lines[lineno - 1];
4182 if (blame_line->annotated && prev_id &&
4183 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4184 !(view->focussed &&
4185 nprinted == s->selected_line - 1)) {
4186 waddstr(view->window, " ");
4187 } else if (blame_line->annotated) {
4188 char *id_str;
4189 err = got_object_id_str(&id_str, blame_line->id);
4190 if (err) {
4191 free(line);
4192 return err;
4194 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4195 if (tc)
4196 wattr_on(view->window,
4197 COLOR_PAIR(tc->colorpair), NULL);
4198 wprintw(view->window, "%.8s", id_str);
4199 if (tc)
4200 wattr_off(view->window,
4201 COLOR_PAIR(tc->colorpair), NULL);
4202 free(id_str);
4203 prev_id = blame_line->id;
4204 } else {
4205 waddstr(view->window, "........");
4206 prev_id = NULL;
4208 } else {
4209 waddstr(view->window, "........");
4210 prev_id = NULL;
4213 if (view->focussed && nprinted == s->selected_line - 1)
4214 wstandend(view->window);
4215 waddstr(view->window, " ");
4217 if (view->ncols <= 9) {
4218 width = 9;
4219 wline = wcsdup(L"");
4220 if (wline == NULL) {
4221 err = got_error_from_errno("wcsdup");
4222 free(line);
4223 return err;
4225 } else if (s->first_displayed_line + nprinted ==
4226 s->matched_line &&
4227 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4228 err = add_matched_line(&width, line, view->ncols - 9, 9,
4229 view->window, regmatch);
4230 if (err) {
4231 free(line);
4232 return err;
4234 width += 9;
4235 } else {
4236 err = format_line(&wline, &width, line,
4237 view->ncols - 9, 9);
4238 waddwstr(view->window, wline);
4239 free(wline);
4240 wline = NULL;
4241 width += 9;
4244 if (width <= view->ncols - 1)
4245 waddch(view->window, '\n');
4246 if (++nprinted == 1)
4247 s->first_displayed_line = lineno;
4249 free(line);
4250 s->last_displayed_line = lineno;
4252 view_vborder(view);
4254 return NULL;
4257 static const struct got_error *
4258 blame_cb(void *arg, int nlines, int lineno,
4259 struct got_commit_object *commit, struct got_object_id *id)
4261 const struct got_error *err = NULL;
4262 struct tog_blame_cb_args *a = arg;
4263 struct tog_blame_line *line;
4264 int errcode;
4266 if (nlines != a->nlines ||
4267 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4268 return got_error(GOT_ERR_RANGE);
4270 errcode = pthread_mutex_lock(&tog_mutex);
4271 if (errcode)
4272 return got_error_set_errno(errcode, "pthread_mutex_lock");
4274 if (*a->quit) { /* user has quit the blame view */
4275 err = got_error(GOT_ERR_ITER_COMPLETED);
4276 goto done;
4279 if (lineno == -1)
4280 goto done; /* no change in this commit */
4282 line = &a->lines[lineno - 1];
4283 if (line->annotated)
4284 goto done;
4286 line->id = got_object_id_dup(id);
4287 if (line->id == NULL) {
4288 err = got_error_from_errno("got_object_id_dup");
4289 goto done;
4291 line->annotated = 1;
4292 done:
4293 errcode = pthread_mutex_unlock(&tog_mutex);
4294 if (errcode)
4295 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4296 return err;
4299 static void *
4300 blame_thread(void *arg)
4302 const struct got_error *err, *close_err;
4303 struct tog_blame_thread_args *ta = arg;
4304 struct tog_blame_cb_args *a = ta->cb_args;
4305 int errcode;
4307 err = block_signals_used_by_main_thread();
4308 if (err)
4309 return (void *)err;
4311 err = got_blame(ta->path, a->commit_id, ta->repo,
4312 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4313 if (err && err->code == GOT_ERR_CANCELLED)
4314 err = NULL;
4316 errcode = pthread_mutex_lock(&tog_mutex);
4317 if (errcode)
4318 return (void *)got_error_set_errno(errcode,
4319 "pthread_mutex_lock");
4321 close_err = got_repo_close(ta->repo);
4322 if (err == NULL)
4323 err = close_err;
4324 ta->repo = NULL;
4325 *ta->complete = 1;
4327 errcode = pthread_mutex_unlock(&tog_mutex);
4328 if (errcode && err == NULL)
4329 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4331 return (void *)err;
4334 static struct got_object_id *
4335 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4336 int first_displayed_line, int selected_line)
4338 struct tog_blame_line *line;
4340 if (nlines <= 0)
4341 return NULL;
4343 line = &lines[first_displayed_line - 1 + selected_line - 1];
4344 if (!line->annotated)
4345 return NULL;
4347 return line->id;
4350 static const struct got_error *
4351 stop_blame(struct tog_blame *blame)
4353 const struct got_error *err = NULL;
4354 int i;
4356 if (blame->thread) {
4357 int errcode;
4358 errcode = pthread_mutex_unlock(&tog_mutex);
4359 if (errcode)
4360 return got_error_set_errno(errcode,
4361 "pthread_mutex_unlock");
4362 errcode = pthread_join(blame->thread, (void **)&err);
4363 if (errcode)
4364 return got_error_set_errno(errcode, "pthread_join");
4365 errcode = pthread_mutex_lock(&tog_mutex);
4366 if (errcode)
4367 return got_error_set_errno(errcode,
4368 "pthread_mutex_lock");
4369 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4370 err = NULL;
4371 blame->thread = NULL;
4373 if (blame->thread_args.repo) {
4374 const struct got_error *close_err;
4375 close_err = got_repo_close(blame->thread_args.repo);
4376 if (err == NULL)
4377 err = close_err;
4378 blame->thread_args.repo = NULL;
4380 if (blame->f) {
4381 if (fclose(blame->f) == EOF && err == NULL)
4382 err = got_error_from_errno("fclose");
4383 blame->f = NULL;
4385 if (blame->lines) {
4386 for (i = 0; i < blame->nlines; i++)
4387 free(blame->lines[i].id);
4388 free(blame->lines);
4389 blame->lines = NULL;
4391 free(blame->cb_args.commit_id);
4392 blame->cb_args.commit_id = NULL;
4393 if (blame->pack_fds) {
4394 const struct got_error *pack_err =
4395 got_repo_pack_fds_close(blame->pack_fds);
4396 if (err == NULL)
4397 err = pack_err;
4399 return err;
4402 static const struct got_error *
4403 cancel_blame_view(void *arg)
4405 const struct got_error *err = NULL;
4406 int *done = arg;
4407 int errcode;
4409 errcode = pthread_mutex_lock(&tog_mutex);
4410 if (errcode)
4411 return got_error_set_errno(errcode,
4412 "pthread_mutex_unlock");
4414 if (*done)
4415 err = got_error(GOT_ERR_CANCELLED);
4417 errcode = pthread_mutex_unlock(&tog_mutex);
4418 if (errcode)
4419 return got_error_set_errno(errcode,
4420 "pthread_mutex_lock");
4422 return err;
4425 static const struct got_error *
4426 run_blame(struct tog_view *view)
4428 struct tog_blame_view_state *s = &view->state.blame;
4429 struct tog_blame *blame = &s->blame;
4430 const struct got_error *err = NULL;
4431 struct got_commit_object *commit = NULL;
4432 struct got_blob_object *blob = NULL;
4433 struct got_repository *thread_repo = NULL;
4434 struct got_object_id *obj_id = NULL;
4435 int obj_type;
4436 int *pack_fds = NULL;
4438 err = got_object_open_as_commit(&commit, s->repo,
4439 &s->blamed_commit->id);
4440 if (err)
4441 return err;
4443 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4444 if (err)
4445 goto done;
4447 err = got_object_get_type(&obj_type, s->repo, obj_id);
4448 if (err)
4449 goto done;
4451 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4452 err = got_error(GOT_ERR_OBJ_TYPE);
4453 goto done;
4456 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4457 if (err)
4458 goto done;
4459 blame->f = got_opentemp();
4460 if (blame->f == NULL) {
4461 err = got_error_from_errno("got_opentemp");
4462 goto done;
4464 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4465 &blame->line_offsets, blame->f, blob);
4466 if (err)
4467 goto done;
4468 if (blame->nlines == 0) {
4469 s->blame_complete = 1;
4470 goto done;
4473 /* Don't include \n at EOF in the blame line count. */
4474 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4475 blame->nlines--;
4477 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4478 if (blame->lines == NULL) {
4479 err = got_error_from_errno("calloc");
4480 goto done;
4483 err = got_repo_pack_fds_open(&pack_fds);
4484 if (err)
4485 goto done;
4486 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4487 pack_fds);
4488 if (err)
4489 goto done;
4491 blame->pack_fds = pack_fds;
4492 blame->cb_args.view = view;
4493 blame->cb_args.lines = blame->lines;
4494 blame->cb_args.nlines = blame->nlines;
4495 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4496 if (blame->cb_args.commit_id == NULL) {
4497 err = got_error_from_errno("got_object_id_dup");
4498 goto done;
4500 blame->cb_args.quit = &s->done;
4502 blame->thread_args.path = s->path;
4503 blame->thread_args.repo = thread_repo;
4504 blame->thread_args.cb_args = &blame->cb_args;
4505 blame->thread_args.complete = &s->blame_complete;
4506 blame->thread_args.cancel_cb = cancel_blame_view;
4507 blame->thread_args.cancel_arg = &s->done;
4508 s->blame_complete = 0;
4510 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4511 s->first_displayed_line = 1;
4512 s->last_displayed_line = view->nlines;
4513 s->selected_line = 1;
4515 s->matched_line = 0;
4517 done:
4518 if (commit)
4519 got_object_commit_close(commit);
4520 if (blob)
4521 got_object_blob_close(blob);
4522 free(obj_id);
4523 if (err)
4524 stop_blame(blame);
4525 return err;
4528 static const struct got_error *
4529 open_blame_view(struct tog_view *view, char *path,
4530 struct got_object_id *commit_id, struct got_repository *repo)
4532 const struct got_error *err = NULL;
4533 struct tog_blame_view_state *s = &view->state.blame;
4535 STAILQ_INIT(&s->blamed_commits);
4537 s->path = strdup(path);
4538 if (s->path == NULL)
4539 return got_error_from_errno("strdup");
4541 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4542 if (err) {
4543 free(s->path);
4544 return err;
4547 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4548 s->first_displayed_line = 1;
4549 s->last_displayed_line = view->nlines;
4550 s->selected_line = 1;
4551 s->blame_complete = 0;
4552 s->repo = repo;
4553 s->commit_id = commit_id;
4554 memset(&s->blame, 0, sizeof(s->blame));
4556 STAILQ_INIT(&s->colors);
4557 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4558 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4559 get_color_value("TOG_COLOR_COMMIT"));
4560 if (err)
4561 return err;
4564 view->show = show_blame_view;
4565 view->input = input_blame_view;
4566 view->close = close_blame_view;
4567 view->search_start = search_start_blame_view;
4568 view->search_next = search_next_blame_view;
4570 return run_blame(view);
4573 static const struct got_error *
4574 close_blame_view(struct tog_view *view)
4576 const struct got_error *err = NULL;
4577 struct tog_blame_view_state *s = &view->state.blame;
4579 if (s->blame.thread)
4580 err = stop_blame(&s->blame);
4582 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4583 struct got_object_qid *blamed_commit;
4584 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4585 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4586 got_object_qid_free(blamed_commit);
4589 free(s->path);
4590 free_colors(&s->colors);
4591 return err;
4594 static const struct got_error *
4595 search_start_blame_view(struct tog_view *view)
4597 struct tog_blame_view_state *s = &view->state.blame;
4599 s->matched_line = 0;
4600 return NULL;
4603 static const struct got_error *
4604 search_next_blame_view(struct tog_view *view)
4606 struct tog_blame_view_state *s = &view->state.blame;
4607 int lineno;
4608 char *line = NULL;
4609 size_t linesize = 0;
4610 ssize_t linelen;
4612 if (!view->searching) {
4613 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4614 return NULL;
4617 if (s->matched_line) {
4618 if (view->searching == TOG_SEARCH_FORWARD)
4619 lineno = s->matched_line + 1;
4620 else
4621 lineno = s->matched_line - 1;
4622 } else
4623 lineno = s->first_displayed_line - 1 + s->selected_line;
4625 while (1) {
4626 off_t offset;
4628 if (lineno <= 0 || lineno > s->blame.nlines) {
4629 if (s->matched_line == 0) {
4630 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4631 break;
4634 if (view->searching == TOG_SEARCH_FORWARD)
4635 lineno = 1;
4636 else
4637 lineno = s->blame.nlines;
4640 offset = s->blame.line_offsets[lineno - 1];
4641 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4642 free(line);
4643 return got_error_from_errno("fseeko");
4645 linelen = getline(&line, &linesize, s->blame.f);
4646 if (linelen != -1 &&
4647 match_line(line, &view->regex, 1, &view->regmatch)) {
4648 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4649 s->matched_line = lineno;
4650 break;
4652 if (view->searching == TOG_SEARCH_FORWARD)
4653 lineno++;
4654 else
4655 lineno--;
4657 free(line);
4659 if (s->matched_line) {
4660 s->first_displayed_line = s->matched_line;
4661 s->selected_line = 1;
4664 return NULL;
4667 static const struct got_error *
4668 show_blame_view(struct tog_view *view)
4670 const struct got_error *err = NULL;
4671 struct tog_blame_view_state *s = &view->state.blame;
4672 int errcode;
4674 if (s->blame.thread == NULL && !s->blame_complete) {
4675 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4676 &s->blame.thread_args);
4677 if (errcode)
4678 return got_error_set_errno(errcode, "pthread_create");
4680 halfdelay(1); /* fast refresh while annotating */
4683 if (s->blame_complete)
4684 halfdelay(10); /* disable fast refresh */
4686 err = draw_blame(view);
4688 view_vborder(view);
4689 return err;
4692 static const struct got_error *
4693 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4695 const struct got_error *err = NULL, *thread_err = NULL;
4696 struct tog_view *diff_view;
4697 struct tog_blame_view_state *s = &view->state.blame;
4698 int begin_x = 0, nscroll = view->nlines - 2;
4700 switch (ch) {
4701 case 'q':
4702 s->done = 1;
4703 break;
4704 case 'g':
4705 case KEY_HOME:
4706 s->selected_line = 1;
4707 s->first_displayed_line = 1;
4708 break;
4709 case 'G':
4710 case KEY_END:
4711 if (s->blame.nlines < view->nlines - 2) {
4712 s->selected_line = s->blame.nlines;
4713 s->first_displayed_line = 1;
4714 } else {
4715 s->selected_line = view->nlines - 2;
4716 s->first_displayed_line = s->blame.nlines -
4717 (view->nlines - 3);
4719 break;
4720 case 'k':
4721 case KEY_UP:
4722 case CTRL('p'):
4723 if (s->selected_line > 1)
4724 s->selected_line--;
4725 else if (s->selected_line == 1 &&
4726 s->first_displayed_line > 1)
4727 s->first_displayed_line--;
4728 break;
4729 case CTRL('u'):
4730 case 'u':
4731 nscroll /= 2;
4732 /* FALL THROUGH */
4733 case KEY_PPAGE:
4734 case CTRL('b'):
4735 if (s->first_displayed_line == 1) {
4736 s->selected_line = MAX(1, s->selected_line - nscroll);
4737 break;
4739 if (s->first_displayed_line > nscroll)
4740 s->first_displayed_line -= nscroll;
4741 else
4742 s->first_displayed_line = 1;
4743 break;
4744 case 'j':
4745 case KEY_DOWN:
4746 case CTRL('n'):
4747 if (s->selected_line < view->nlines - 2 &&
4748 s->first_displayed_line +
4749 s->selected_line <= s->blame.nlines)
4750 s->selected_line++;
4751 else if (s->last_displayed_line <
4752 s->blame.nlines)
4753 s->first_displayed_line++;
4754 break;
4755 case 'b':
4756 case 'p': {
4757 struct got_object_id *id = NULL;
4758 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4759 s->first_displayed_line, s->selected_line);
4760 if (id == NULL)
4761 break;
4762 if (ch == 'p') {
4763 struct got_commit_object *commit, *pcommit;
4764 struct got_object_qid *pid;
4765 struct got_object_id *blob_id = NULL;
4766 int obj_type;
4767 err = got_object_open_as_commit(&commit,
4768 s->repo, id);
4769 if (err)
4770 break;
4771 pid = STAILQ_FIRST(
4772 got_object_commit_get_parent_ids(commit));
4773 if (pid == NULL) {
4774 got_object_commit_close(commit);
4775 break;
4777 /* Check if path history ends here. */
4778 err = got_object_open_as_commit(&pcommit,
4779 s->repo, &pid->id);
4780 if (err)
4781 break;
4782 err = got_object_id_by_path(&blob_id, s->repo,
4783 pcommit, s->path);
4784 got_object_commit_close(pcommit);
4785 if (err) {
4786 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4787 err = NULL;
4788 got_object_commit_close(commit);
4789 break;
4791 err = got_object_get_type(&obj_type, s->repo,
4792 blob_id);
4793 free(blob_id);
4794 /* Can't blame non-blob type objects. */
4795 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4796 got_object_commit_close(commit);
4797 break;
4799 err = got_object_qid_alloc(&s->blamed_commit,
4800 &pid->id);
4801 got_object_commit_close(commit);
4802 } else {
4803 if (got_object_id_cmp(id,
4804 &s->blamed_commit->id) == 0)
4805 break;
4806 err = got_object_qid_alloc(&s->blamed_commit,
4807 id);
4809 if (err)
4810 break;
4811 s->done = 1;
4812 thread_err = stop_blame(&s->blame);
4813 s->done = 0;
4814 if (thread_err)
4815 break;
4816 STAILQ_INSERT_HEAD(&s->blamed_commits,
4817 s->blamed_commit, entry);
4818 err = run_blame(view);
4819 if (err)
4820 break;
4821 break;
4823 case 'B': {
4824 struct got_object_qid *first;
4825 first = STAILQ_FIRST(&s->blamed_commits);
4826 if (!got_object_id_cmp(&first->id, s->commit_id))
4827 break;
4828 s->done = 1;
4829 thread_err = stop_blame(&s->blame);
4830 s->done = 0;
4831 if (thread_err)
4832 break;
4833 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4834 got_object_qid_free(s->blamed_commit);
4835 s->blamed_commit =
4836 STAILQ_FIRST(&s->blamed_commits);
4837 err = run_blame(view);
4838 if (err)
4839 break;
4840 break;
4842 case KEY_ENTER:
4843 case '\r': {
4844 struct got_object_id *id = NULL;
4845 struct got_object_qid *pid;
4846 struct got_commit_object *commit = NULL;
4847 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4848 s->first_displayed_line, s->selected_line);
4849 if (id == NULL)
4850 break;
4851 err = got_object_open_as_commit(&commit, s->repo, id);
4852 if (err)
4853 break;
4854 pid = STAILQ_FIRST(
4855 got_object_commit_get_parent_ids(commit));
4856 if (view_is_parent_view(view))
4857 begin_x = view_split_begin_x(view->begin_x);
4858 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4859 if (diff_view == NULL) {
4860 got_object_commit_close(commit);
4861 err = got_error_from_errno("view_open");
4862 break;
4864 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4865 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4866 got_object_commit_close(commit);
4867 if (err) {
4868 view_close(diff_view);
4869 break;
4871 view->focussed = 0;
4872 diff_view->focussed = 1;
4873 if (view_is_parent_view(view)) {
4874 err = view_close_child(view);
4875 if (err)
4876 break;
4877 view_set_child(view, diff_view);
4878 view->focus_child = 1;
4879 } else
4880 *new_view = diff_view;
4881 if (err)
4882 break;
4883 break;
4885 case CTRL('d'):
4886 case 'd':
4887 nscroll /= 2;
4888 /* FALL THROUGH */
4889 case KEY_NPAGE:
4890 case CTRL('f'):
4891 case ' ':
4892 if (s->last_displayed_line >= s->blame.nlines &&
4893 s->selected_line >= MIN(s->blame.nlines,
4894 view->nlines - 2)) {
4895 break;
4897 if (s->last_displayed_line >= s->blame.nlines &&
4898 s->selected_line < view->nlines - 2) {
4899 s->selected_line +=
4900 MIN(nscroll, s->last_displayed_line -
4901 s->first_displayed_line - s->selected_line + 1);
4903 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4904 s->first_displayed_line += nscroll;
4905 else
4906 s->first_displayed_line =
4907 s->blame.nlines - (view->nlines - 3);
4908 break;
4909 case KEY_RESIZE:
4910 if (s->selected_line > view->nlines - 2) {
4911 s->selected_line = MIN(s->blame.nlines,
4912 view->nlines - 2);
4914 break;
4915 default:
4916 break;
4918 return thread_err ? thread_err : err;
4921 static const struct got_error *
4922 cmd_blame(int argc, char *argv[])
4924 const struct got_error *error;
4925 struct got_repository *repo = NULL;
4926 struct got_worktree *worktree = NULL;
4927 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4928 char *link_target = NULL;
4929 struct got_object_id *commit_id = NULL;
4930 struct got_commit_object *commit = NULL;
4931 char *commit_id_str = NULL;
4932 int ch;
4933 struct tog_view *view;
4934 int *pack_fds = NULL;
4936 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4937 switch (ch) {
4938 case 'c':
4939 commit_id_str = optarg;
4940 break;
4941 case 'r':
4942 repo_path = realpath(optarg, NULL);
4943 if (repo_path == NULL)
4944 return got_error_from_errno2("realpath",
4945 optarg);
4946 break;
4947 default:
4948 usage_blame();
4949 /* NOTREACHED */
4953 argc -= optind;
4954 argv += optind;
4956 if (argc != 1)
4957 usage_blame();
4959 error = got_repo_pack_fds_open(&pack_fds);
4960 if (error != NULL)
4961 goto done;
4963 if (repo_path == NULL) {
4964 cwd = getcwd(NULL, 0);
4965 if (cwd == NULL)
4966 return got_error_from_errno("getcwd");
4967 error = got_worktree_open(&worktree, cwd);
4968 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4969 goto done;
4970 if (worktree)
4971 repo_path =
4972 strdup(got_worktree_get_repo_path(worktree));
4973 else
4974 repo_path = strdup(cwd);
4975 if (repo_path == NULL) {
4976 error = got_error_from_errno("strdup");
4977 goto done;
4981 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4982 if (error != NULL)
4983 goto done;
4985 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4986 worktree);
4987 if (error)
4988 goto done;
4990 init_curses();
4992 error = apply_unveil(got_repo_get_path(repo), NULL);
4993 if (error)
4994 goto done;
4996 error = tog_load_refs(repo, 0);
4997 if (error)
4998 goto done;
5000 if (commit_id_str == NULL) {
5001 struct got_reference *head_ref;
5002 error = got_ref_open(&head_ref, repo, worktree ?
5003 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5004 if (error != NULL)
5005 goto done;
5006 error = got_ref_resolve(&commit_id, repo, head_ref);
5007 got_ref_close(head_ref);
5008 } else {
5009 error = got_repo_match_object_id(&commit_id, NULL,
5010 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5012 if (error != NULL)
5013 goto done;
5015 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5016 if (view == NULL) {
5017 error = got_error_from_errno("view_open");
5018 goto done;
5021 error = got_object_open_as_commit(&commit, repo, commit_id);
5022 if (error)
5023 goto done;
5025 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5026 commit, repo);
5027 if (error)
5028 goto done;
5030 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5031 commit_id, repo);
5032 if (error)
5033 goto done;
5034 if (worktree) {
5035 /* Release work tree lock. */
5036 got_worktree_close(worktree);
5037 worktree = NULL;
5039 error = view_loop(view);
5040 done:
5041 free(repo_path);
5042 free(in_repo_path);
5043 free(link_target);
5044 free(cwd);
5045 free(commit_id);
5046 if (commit)
5047 got_object_commit_close(commit);
5048 if (worktree)
5049 got_worktree_close(worktree);
5050 if (repo) {
5051 const struct got_error *close_err = got_repo_close(repo);
5052 if (error == NULL)
5053 error = close_err;
5055 if (pack_fds) {
5056 const struct got_error *pack_err =
5057 got_repo_pack_fds_close(pack_fds);
5058 if (error == NULL)
5059 error = pack_err;
5061 tog_free_refs();
5062 return error;
5065 static const struct got_error *
5066 draw_tree_entries(struct tog_view *view, const char *parent_path)
5068 struct tog_tree_view_state *s = &view->state.tree;
5069 const struct got_error *err = NULL;
5070 struct got_tree_entry *te;
5071 wchar_t *wline;
5072 struct tog_color *tc;
5073 int width, n, i, nentries;
5074 int limit = view->nlines;
5076 s->ndisplayed = 0;
5078 werase(view->window);
5080 if (limit == 0)
5081 return NULL;
5083 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5084 if (err)
5085 return err;
5086 if (view_needs_focus_indication(view))
5087 wstandout(view->window);
5088 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5089 if (tc)
5090 wattr_on(view->window,
5091 COLOR_PAIR(tc->colorpair), NULL);
5092 waddwstr(view->window, wline);
5093 if (tc)
5094 wattr_off(view->window,
5095 COLOR_PAIR(tc->colorpair), NULL);
5096 if (view_needs_focus_indication(view))
5097 wstandend(view->window);
5098 free(wline);
5099 wline = NULL;
5100 if (width < view->ncols - 1)
5101 waddch(view->window, '\n');
5102 if (--limit <= 0)
5103 return NULL;
5104 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5105 if (err)
5106 return err;
5107 waddwstr(view->window, wline);
5108 free(wline);
5109 wline = NULL;
5110 if (width < view->ncols - 1)
5111 waddch(view->window, '\n');
5112 if (--limit <= 0)
5113 return NULL;
5114 waddch(view->window, '\n');
5115 if (--limit <= 0)
5116 return NULL;
5118 if (s->first_displayed_entry == NULL) {
5119 te = got_object_tree_get_first_entry(s->tree);
5120 if (s->selected == 0) {
5121 if (view->focussed)
5122 wstandout(view->window);
5123 s->selected_entry = NULL;
5125 waddstr(view->window, " ..\n"); /* parent directory */
5126 if (s->selected == 0 && view->focussed)
5127 wstandend(view->window);
5128 s->ndisplayed++;
5129 if (--limit <= 0)
5130 return NULL;
5131 n = 1;
5132 } else {
5133 n = 0;
5134 te = s->first_displayed_entry;
5137 nentries = got_object_tree_get_nentries(s->tree);
5138 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5139 char *line = NULL, *id_str = NULL, *link_target = NULL;
5140 const char *modestr = "";
5141 mode_t mode;
5143 te = got_object_tree_get_entry(s->tree, i);
5144 mode = got_tree_entry_get_mode(te);
5146 if (s->show_ids) {
5147 err = got_object_id_str(&id_str,
5148 got_tree_entry_get_id(te));
5149 if (err)
5150 return got_error_from_errno(
5151 "got_object_id_str");
5153 if (got_object_tree_entry_is_submodule(te))
5154 modestr = "$";
5155 else if (S_ISLNK(mode)) {
5156 int i;
5158 err = got_tree_entry_get_symlink_target(&link_target,
5159 te, s->repo);
5160 if (err) {
5161 free(id_str);
5162 return err;
5164 for (i = 0; i < strlen(link_target); i++) {
5165 if (!isprint((unsigned char)link_target[i]))
5166 link_target[i] = '?';
5168 modestr = "@";
5170 else if (S_ISDIR(mode))
5171 modestr = "/";
5172 else if (mode & S_IXUSR)
5173 modestr = "*";
5174 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5175 got_tree_entry_get_name(te), modestr,
5176 link_target ? " -> ": "",
5177 link_target ? link_target : "") == -1) {
5178 free(id_str);
5179 free(link_target);
5180 return got_error_from_errno("asprintf");
5182 free(id_str);
5183 free(link_target);
5184 err = format_line(&wline, &width, line, view->ncols, 0);
5185 if (err) {
5186 free(line);
5187 break;
5189 if (n == s->selected) {
5190 if (view->focussed)
5191 wstandout(view->window);
5192 s->selected_entry = te;
5194 tc = match_color(&s->colors, line);
5195 if (tc)
5196 wattr_on(view->window,
5197 COLOR_PAIR(tc->colorpair), NULL);
5198 waddwstr(view->window, wline);
5199 if (tc)
5200 wattr_off(view->window,
5201 COLOR_PAIR(tc->colorpair), NULL);
5202 if (width < view->ncols - 1)
5203 waddch(view->window, '\n');
5204 if (n == s->selected && view->focussed)
5205 wstandend(view->window);
5206 free(line);
5207 free(wline);
5208 wline = NULL;
5209 n++;
5210 s->ndisplayed++;
5211 s->last_displayed_entry = te;
5212 if (--limit <= 0)
5213 break;
5216 return err;
5219 static void
5220 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5222 struct got_tree_entry *te;
5223 int isroot = s->tree == s->root;
5224 int i = 0;
5226 if (s->first_displayed_entry == NULL)
5227 return;
5229 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5230 while (i++ < maxscroll) {
5231 if (te == NULL) {
5232 if (!isroot)
5233 s->first_displayed_entry = NULL;
5234 break;
5236 s->first_displayed_entry = te;
5237 te = got_tree_entry_get_prev(s->tree, te);
5241 static void
5242 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5244 struct got_tree_entry *next, *last;
5245 int n = 0;
5247 if (s->first_displayed_entry)
5248 next = got_tree_entry_get_next(s->tree,
5249 s->first_displayed_entry);
5250 else
5251 next = got_object_tree_get_first_entry(s->tree);
5253 last = s->last_displayed_entry;
5254 while (next && last && n++ < maxscroll) {
5255 last = got_tree_entry_get_next(s->tree, last);
5256 if (last) {
5257 s->first_displayed_entry = next;
5258 next = got_tree_entry_get_next(s->tree, next);
5263 static const struct got_error *
5264 tree_entry_path(char **path, struct tog_parent_trees *parents,
5265 struct got_tree_entry *te)
5267 const struct got_error *err = NULL;
5268 struct tog_parent_tree *pt;
5269 size_t len = 2; /* for leading slash and NUL */
5271 TAILQ_FOREACH(pt, parents, entry)
5272 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5273 + 1 /* slash */;
5274 if (te)
5275 len += strlen(got_tree_entry_get_name(te));
5277 *path = calloc(1, len);
5278 if (path == NULL)
5279 return got_error_from_errno("calloc");
5281 (*path)[0] = '/';
5282 pt = TAILQ_LAST(parents, tog_parent_trees);
5283 while (pt) {
5284 const char *name = got_tree_entry_get_name(pt->selected_entry);
5285 if (strlcat(*path, name, len) >= len) {
5286 err = got_error(GOT_ERR_NO_SPACE);
5287 goto done;
5289 if (strlcat(*path, "/", len) >= len) {
5290 err = got_error(GOT_ERR_NO_SPACE);
5291 goto done;
5293 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5295 if (te) {
5296 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5297 err = got_error(GOT_ERR_NO_SPACE);
5298 goto done;
5301 done:
5302 if (err) {
5303 free(*path);
5304 *path = NULL;
5306 return err;
5309 static const struct got_error *
5310 blame_tree_entry(struct tog_view **new_view, int begin_x,
5311 struct got_tree_entry *te, struct tog_parent_trees *parents,
5312 struct got_object_id *commit_id, struct got_repository *repo)
5314 const struct got_error *err = NULL;
5315 char *path;
5316 struct tog_view *blame_view;
5318 *new_view = NULL;
5320 err = tree_entry_path(&path, parents, te);
5321 if (err)
5322 return err;
5324 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5325 if (blame_view == NULL) {
5326 err = got_error_from_errno("view_open");
5327 goto done;
5330 err = open_blame_view(blame_view, path, commit_id, repo);
5331 if (err) {
5332 if (err->code == GOT_ERR_CANCELLED)
5333 err = NULL;
5334 view_close(blame_view);
5335 } else
5336 *new_view = blame_view;
5337 done:
5338 free(path);
5339 return err;
5342 static const struct got_error *
5343 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5344 struct tog_tree_view_state *s)
5346 struct tog_view *log_view;
5347 const struct got_error *err = NULL;
5348 char *path;
5350 *new_view = NULL;
5352 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5353 if (log_view == NULL)
5354 return got_error_from_errno("view_open");
5356 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5357 if (err)
5358 return err;
5360 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5361 path, 0);
5362 if (err)
5363 view_close(log_view);
5364 else
5365 *new_view = log_view;
5366 free(path);
5367 return err;
5370 static const struct got_error *
5371 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5372 const char *head_ref_name, struct got_repository *repo)
5374 const struct got_error *err = NULL;
5375 char *commit_id_str = NULL;
5376 struct tog_tree_view_state *s = &view->state.tree;
5377 struct got_commit_object *commit = NULL;
5379 TAILQ_INIT(&s->parents);
5380 STAILQ_INIT(&s->colors);
5382 s->commit_id = got_object_id_dup(commit_id);
5383 if (s->commit_id == NULL)
5384 return got_error_from_errno("got_object_id_dup");
5386 err = got_object_open_as_commit(&commit, repo, commit_id);
5387 if (err)
5388 goto done;
5391 * The root is opened here and will be closed when the view is closed.
5392 * Any visited subtrees and their path-wise parents are opened and
5393 * closed on demand.
5395 err = got_object_open_as_tree(&s->root, repo,
5396 got_object_commit_get_tree_id(commit));
5397 if (err)
5398 goto done;
5399 s->tree = s->root;
5401 err = got_object_id_str(&commit_id_str, commit_id);
5402 if (err != NULL)
5403 goto done;
5405 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5406 err = got_error_from_errno("asprintf");
5407 goto done;
5410 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5411 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5412 if (head_ref_name) {
5413 s->head_ref_name = strdup(head_ref_name);
5414 if (s->head_ref_name == NULL) {
5415 err = got_error_from_errno("strdup");
5416 goto done;
5419 s->repo = repo;
5421 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5422 err = add_color(&s->colors, "\\$$",
5423 TOG_COLOR_TREE_SUBMODULE,
5424 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5425 if (err)
5426 goto done;
5427 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5428 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5429 if (err)
5430 goto done;
5431 err = add_color(&s->colors, "/$",
5432 TOG_COLOR_TREE_DIRECTORY,
5433 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5434 if (err)
5435 goto done;
5437 err = add_color(&s->colors, "\\*$",
5438 TOG_COLOR_TREE_EXECUTABLE,
5439 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5440 if (err)
5441 goto done;
5443 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5444 get_color_value("TOG_COLOR_COMMIT"));
5445 if (err)
5446 goto done;
5449 view->show = show_tree_view;
5450 view->input = input_tree_view;
5451 view->close = close_tree_view;
5452 view->search_start = search_start_tree_view;
5453 view->search_next = search_next_tree_view;
5454 done:
5455 free(commit_id_str);
5456 if (commit)
5457 got_object_commit_close(commit);
5458 if (err)
5459 close_tree_view(view);
5460 return err;
5463 static const struct got_error *
5464 close_tree_view(struct tog_view *view)
5466 struct tog_tree_view_state *s = &view->state.tree;
5468 free_colors(&s->colors);
5469 free(s->tree_label);
5470 s->tree_label = NULL;
5471 free(s->commit_id);
5472 s->commit_id = NULL;
5473 free(s->head_ref_name);
5474 s->head_ref_name = NULL;
5475 while (!TAILQ_EMPTY(&s->parents)) {
5476 struct tog_parent_tree *parent;
5477 parent = TAILQ_FIRST(&s->parents);
5478 TAILQ_REMOVE(&s->parents, parent, entry);
5479 if (parent->tree != s->root)
5480 got_object_tree_close(parent->tree);
5481 free(parent);
5484 if (s->tree != NULL && s->tree != s->root)
5485 got_object_tree_close(s->tree);
5486 if (s->root)
5487 got_object_tree_close(s->root);
5488 return NULL;
5491 static const struct got_error *
5492 search_start_tree_view(struct tog_view *view)
5494 struct tog_tree_view_state *s = &view->state.tree;
5496 s->matched_entry = NULL;
5497 return NULL;
5500 static int
5501 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5503 regmatch_t regmatch;
5505 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5506 0) == 0;
5509 static const struct got_error *
5510 search_next_tree_view(struct tog_view *view)
5512 struct tog_tree_view_state *s = &view->state.tree;
5513 struct got_tree_entry *te = NULL;
5515 if (!view->searching) {
5516 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5517 return NULL;
5520 if (s->matched_entry) {
5521 if (view->searching == TOG_SEARCH_FORWARD) {
5522 if (s->selected_entry)
5523 te = got_tree_entry_get_next(s->tree,
5524 s->selected_entry);
5525 else
5526 te = got_object_tree_get_first_entry(s->tree);
5527 } else {
5528 if (s->selected_entry == NULL)
5529 te = got_object_tree_get_last_entry(s->tree);
5530 else
5531 te = got_tree_entry_get_prev(s->tree,
5532 s->selected_entry);
5534 } else {
5535 if (s->selected_entry)
5536 te = s->selected_entry;
5537 else if (view->searching == TOG_SEARCH_FORWARD)
5538 te = got_object_tree_get_first_entry(s->tree);
5539 else
5540 te = got_object_tree_get_last_entry(s->tree);
5543 while (1) {
5544 if (te == NULL) {
5545 if (s->matched_entry == NULL) {
5546 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5547 return NULL;
5549 if (view->searching == TOG_SEARCH_FORWARD)
5550 te = got_object_tree_get_first_entry(s->tree);
5551 else
5552 te = got_object_tree_get_last_entry(s->tree);
5555 if (match_tree_entry(te, &view->regex)) {
5556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5557 s->matched_entry = te;
5558 break;
5561 if (view->searching == TOG_SEARCH_FORWARD)
5562 te = got_tree_entry_get_next(s->tree, te);
5563 else
5564 te = got_tree_entry_get_prev(s->tree, te);
5567 if (s->matched_entry) {
5568 s->first_displayed_entry = s->matched_entry;
5569 s->selected = 0;
5572 return NULL;
5575 static const struct got_error *
5576 show_tree_view(struct tog_view *view)
5578 const struct got_error *err = NULL;
5579 struct tog_tree_view_state *s = &view->state.tree;
5580 char *parent_path;
5582 err = tree_entry_path(&parent_path, &s->parents, NULL);
5583 if (err)
5584 return err;
5586 err = draw_tree_entries(view, parent_path);
5587 free(parent_path);
5589 view_vborder(view);
5590 return err;
5593 static const struct got_error *
5594 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5596 const struct got_error *err = NULL;
5597 struct tog_tree_view_state *s = &view->state.tree;
5598 struct tog_view *log_view, *ref_view;
5599 struct got_tree_entry *te;
5600 int begin_x = 0, n, nscroll = view->nlines - 3;
5602 switch (ch) {
5603 case 'i':
5604 s->show_ids = !s->show_ids;
5605 break;
5606 case 'l':
5607 if (!s->selected_entry)
5608 break;
5609 if (view_is_parent_view(view))
5610 begin_x = view_split_begin_x(view->begin_x);
5611 err = log_selected_tree_entry(&log_view, begin_x, s);
5612 view->focussed = 0;
5613 log_view->focussed = 1;
5614 if (view_is_parent_view(view)) {
5615 err = view_close_child(view);
5616 if (err)
5617 return err;
5618 view_set_child(view, log_view);
5619 view->focus_child = 1;
5620 } else
5621 *new_view = log_view;
5622 break;
5623 case 'r':
5624 if (view_is_parent_view(view))
5625 begin_x = view_split_begin_x(view->begin_x);
5626 ref_view = view_open(view->nlines, view->ncols,
5627 view->begin_y, begin_x, TOG_VIEW_REF);
5628 if (ref_view == NULL)
5629 return got_error_from_errno("view_open");
5630 err = open_ref_view(ref_view, s->repo);
5631 if (err) {
5632 view_close(ref_view);
5633 return err;
5635 view->focussed = 0;
5636 ref_view->focussed = 1;
5637 if (view_is_parent_view(view)) {
5638 err = view_close_child(view);
5639 if (err)
5640 return err;
5641 view_set_child(view, ref_view);
5642 view->focus_child = 1;
5643 } else
5644 *new_view = ref_view;
5645 break;
5646 case 'g':
5647 case KEY_HOME:
5648 s->selected = 0;
5649 if (s->tree == s->root)
5650 s->first_displayed_entry =
5651 got_object_tree_get_first_entry(s->tree);
5652 else
5653 s->first_displayed_entry = NULL;
5654 break;
5655 case 'G':
5656 case KEY_END:
5657 s->selected = 0;
5658 te = got_object_tree_get_last_entry(s->tree);
5659 for (n = 0; n < view->nlines - 3; n++) {
5660 if (te == NULL) {
5661 if(s->tree != s->root) {
5662 s->first_displayed_entry = NULL;
5663 n++;
5665 break;
5667 s->first_displayed_entry = te;
5668 te = got_tree_entry_get_prev(s->tree, te);
5670 if (n > 0)
5671 s->selected = n - 1;
5672 break;
5673 case 'k':
5674 case KEY_UP:
5675 case CTRL('p'):
5676 if (s->selected > 0) {
5677 s->selected--;
5678 break;
5680 tree_scroll_up(s, 1);
5681 break;
5682 case CTRL('u'):
5683 case 'u':
5684 nscroll /= 2;
5685 /* FALL THROUGH */
5686 case KEY_PPAGE:
5687 case CTRL('b'):
5688 if (s->tree == s->root) {
5689 if (got_object_tree_get_first_entry(s->tree) ==
5690 s->first_displayed_entry)
5691 s->selected -= MIN(s->selected, nscroll);
5692 } else {
5693 if (s->first_displayed_entry == NULL)
5694 s->selected -= MIN(s->selected, nscroll);
5696 tree_scroll_up(s, MAX(0, nscroll));
5697 break;
5698 case 'j':
5699 case KEY_DOWN:
5700 case CTRL('n'):
5701 if (s->selected < s->ndisplayed - 1) {
5702 s->selected++;
5703 break;
5705 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5706 == NULL)
5707 /* can't scroll any further */
5708 break;
5709 tree_scroll_down(s, 1);
5710 break;
5711 case CTRL('d'):
5712 case 'd':
5713 nscroll /= 2;
5714 /* FALL THROUGH */
5715 case KEY_NPAGE:
5716 case CTRL('f'):
5717 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5718 == NULL) {
5719 /* can't scroll any further; move cursor down */
5720 if (s->selected < s->ndisplayed - 1)
5721 s->selected += MIN(nscroll,
5722 s->ndisplayed - s->selected - 1);
5723 break;
5725 tree_scroll_down(s, nscroll);
5726 break;
5727 case KEY_ENTER:
5728 case '\r':
5729 case KEY_BACKSPACE:
5730 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5731 struct tog_parent_tree *parent;
5732 /* user selected '..' */
5733 if (s->tree == s->root)
5734 break;
5735 parent = TAILQ_FIRST(&s->parents);
5736 TAILQ_REMOVE(&s->parents, parent,
5737 entry);
5738 got_object_tree_close(s->tree);
5739 s->tree = parent->tree;
5740 s->first_displayed_entry =
5741 parent->first_displayed_entry;
5742 s->selected_entry =
5743 parent->selected_entry;
5744 s->selected = parent->selected;
5745 free(parent);
5746 } else if (S_ISDIR(got_tree_entry_get_mode(
5747 s->selected_entry))) {
5748 struct got_tree_object *subtree;
5749 err = got_object_open_as_tree(&subtree, s->repo,
5750 got_tree_entry_get_id(s->selected_entry));
5751 if (err)
5752 break;
5753 err = tree_view_visit_subtree(s, subtree);
5754 if (err) {
5755 got_object_tree_close(subtree);
5756 break;
5758 } else if (S_ISREG(got_tree_entry_get_mode(
5759 s->selected_entry))) {
5760 struct tog_view *blame_view;
5761 int begin_x = view_is_parent_view(view) ?
5762 view_split_begin_x(view->begin_x) : 0;
5764 err = blame_tree_entry(&blame_view, begin_x,
5765 s->selected_entry, &s->parents,
5766 s->commit_id, s->repo);
5767 if (err)
5768 break;
5769 view->focussed = 0;
5770 blame_view->focussed = 1;
5771 if (view_is_parent_view(view)) {
5772 err = view_close_child(view);
5773 if (err)
5774 return err;
5775 view_set_child(view, blame_view);
5776 view->focus_child = 1;
5777 } else
5778 *new_view = blame_view;
5780 break;
5781 case KEY_RESIZE:
5782 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5783 s->selected = view->nlines - 4;
5784 break;
5785 default:
5786 break;
5789 return err;
5792 __dead static void
5793 usage_tree(void)
5795 endwin();
5796 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5797 getprogname());
5798 exit(1);
5801 static const struct got_error *
5802 cmd_tree(int argc, char *argv[])
5804 const struct got_error *error;
5805 struct got_repository *repo = NULL;
5806 struct got_worktree *worktree = NULL;
5807 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5808 struct got_object_id *commit_id = NULL;
5809 struct got_commit_object *commit = NULL;
5810 const char *commit_id_arg = NULL;
5811 char *label = NULL;
5812 struct got_reference *ref = NULL;
5813 const char *head_ref_name = NULL;
5814 int ch;
5815 struct tog_view *view;
5816 int *pack_fds = NULL;
5818 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5819 switch (ch) {
5820 case 'c':
5821 commit_id_arg = optarg;
5822 break;
5823 case 'r':
5824 repo_path = realpath(optarg, NULL);
5825 if (repo_path == NULL)
5826 return got_error_from_errno2("realpath",
5827 optarg);
5828 break;
5829 default:
5830 usage_tree();
5831 /* NOTREACHED */
5835 argc -= optind;
5836 argv += optind;
5838 if (argc > 1)
5839 usage_tree();
5841 error = got_repo_pack_fds_open(&pack_fds);
5842 if (error != NULL)
5843 goto done;
5845 if (repo_path == NULL) {
5846 cwd = getcwd(NULL, 0);
5847 if (cwd == NULL)
5848 return got_error_from_errno("getcwd");
5849 error = got_worktree_open(&worktree, cwd);
5850 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5851 goto done;
5852 if (worktree)
5853 repo_path =
5854 strdup(got_worktree_get_repo_path(worktree));
5855 else
5856 repo_path = strdup(cwd);
5857 if (repo_path == NULL) {
5858 error = got_error_from_errno("strdup");
5859 goto done;
5863 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5864 if (error != NULL)
5865 goto done;
5867 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5868 repo, worktree);
5869 if (error)
5870 goto done;
5872 init_curses();
5874 error = apply_unveil(got_repo_get_path(repo), NULL);
5875 if (error)
5876 goto done;
5878 error = tog_load_refs(repo, 0);
5879 if (error)
5880 goto done;
5882 if (commit_id_arg == NULL) {
5883 error = got_repo_match_object_id(&commit_id, &label,
5884 worktree ? got_worktree_get_head_ref_name(worktree) :
5885 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5886 if (error)
5887 goto done;
5888 head_ref_name = label;
5889 } else {
5890 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5891 if (error == NULL)
5892 head_ref_name = got_ref_get_name(ref);
5893 else if (error->code != GOT_ERR_NOT_REF)
5894 goto done;
5895 error = got_repo_match_object_id(&commit_id, NULL,
5896 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5897 if (error)
5898 goto done;
5901 error = got_object_open_as_commit(&commit, repo, commit_id);
5902 if (error)
5903 goto done;
5905 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5906 if (view == NULL) {
5907 error = got_error_from_errno("view_open");
5908 goto done;
5910 error = open_tree_view(view, commit_id, head_ref_name, repo);
5911 if (error)
5912 goto done;
5913 if (!got_path_is_root_dir(in_repo_path)) {
5914 error = tree_view_walk_path(&view->state.tree, commit,
5915 in_repo_path);
5916 if (error)
5917 goto done;
5920 if (worktree) {
5921 /* Release work tree lock. */
5922 got_worktree_close(worktree);
5923 worktree = NULL;
5925 error = view_loop(view);
5926 done:
5927 free(repo_path);
5928 free(cwd);
5929 free(commit_id);
5930 free(label);
5931 if (ref)
5932 got_ref_close(ref);
5933 if (repo) {
5934 const struct got_error *close_err = got_repo_close(repo);
5935 if (error == NULL)
5936 error = close_err;
5938 if (pack_fds) {
5939 const struct got_error *pack_err =
5940 got_repo_pack_fds_close(pack_fds);
5941 if (error == NULL)
5942 error = pack_err;
5944 tog_free_refs();
5945 return error;
5948 static const struct got_error *
5949 ref_view_load_refs(struct tog_ref_view_state *s)
5951 struct got_reflist_entry *sre;
5952 struct tog_reflist_entry *re;
5954 s->nrefs = 0;
5955 TAILQ_FOREACH(sre, &tog_refs, entry) {
5956 if (strncmp(got_ref_get_name(sre->ref),
5957 "refs/got/", 9) == 0 &&
5958 strncmp(got_ref_get_name(sre->ref),
5959 "refs/got/backup/", 16) != 0)
5960 continue;
5962 re = malloc(sizeof(*re));
5963 if (re == NULL)
5964 return got_error_from_errno("malloc");
5966 re->ref = got_ref_dup(sre->ref);
5967 if (re->ref == NULL)
5968 return got_error_from_errno("got_ref_dup");
5969 re->idx = s->nrefs++;
5970 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5973 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5974 return NULL;
5977 void
5978 ref_view_free_refs(struct tog_ref_view_state *s)
5980 struct tog_reflist_entry *re;
5982 while (!TAILQ_EMPTY(&s->refs)) {
5983 re = TAILQ_FIRST(&s->refs);
5984 TAILQ_REMOVE(&s->refs, re, entry);
5985 got_ref_close(re->ref);
5986 free(re);
5990 static const struct got_error *
5991 open_ref_view(struct tog_view *view, struct got_repository *repo)
5993 const struct got_error *err = NULL;
5994 struct tog_ref_view_state *s = &view->state.ref;
5996 s->selected_entry = 0;
5997 s->repo = repo;
5999 TAILQ_INIT(&s->refs);
6000 STAILQ_INIT(&s->colors);
6002 err = ref_view_load_refs(s);
6003 if (err)
6004 return err;
6006 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6007 err = add_color(&s->colors, "^refs/heads/",
6008 TOG_COLOR_REFS_HEADS,
6009 get_color_value("TOG_COLOR_REFS_HEADS"));
6010 if (err)
6011 goto done;
6013 err = add_color(&s->colors, "^refs/tags/",
6014 TOG_COLOR_REFS_TAGS,
6015 get_color_value("TOG_COLOR_REFS_TAGS"));
6016 if (err)
6017 goto done;
6019 err = add_color(&s->colors, "^refs/remotes/",
6020 TOG_COLOR_REFS_REMOTES,
6021 get_color_value("TOG_COLOR_REFS_REMOTES"));
6022 if (err)
6023 goto done;
6025 err = add_color(&s->colors, "^refs/got/backup/",
6026 TOG_COLOR_REFS_BACKUP,
6027 get_color_value("TOG_COLOR_REFS_BACKUP"));
6028 if (err)
6029 goto done;
6032 view->show = show_ref_view;
6033 view->input = input_ref_view;
6034 view->close = close_ref_view;
6035 view->search_start = search_start_ref_view;
6036 view->search_next = search_next_ref_view;
6037 done:
6038 if (err)
6039 free_colors(&s->colors);
6040 return err;
6043 static const struct got_error *
6044 close_ref_view(struct tog_view *view)
6046 struct tog_ref_view_state *s = &view->state.ref;
6048 ref_view_free_refs(s);
6049 free_colors(&s->colors);
6051 return NULL;
6054 static const struct got_error *
6055 resolve_reflist_entry(struct got_object_id **commit_id,
6056 struct tog_reflist_entry *re, struct got_repository *repo)
6058 const struct got_error *err = NULL;
6059 struct got_object_id *obj_id;
6060 struct got_tag_object *tag = NULL;
6061 int obj_type;
6063 *commit_id = NULL;
6065 err = got_ref_resolve(&obj_id, repo, re->ref);
6066 if (err)
6067 return err;
6069 err = got_object_get_type(&obj_type, repo, obj_id);
6070 if (err)
6071 goto done;
6073 switch (obj_type) {
6074 case GOT_OBJ_TYPE_COMMIT:
6075 *commit_id = obj_id;
6076 break;
6077 case GOT_OBJ_TYPE_TAG:
6078 err = got_object_open_as_tag(&tag, repo, obj_id);
6079 if (err)
6080 goto done;
6081 free(obj_id);
6082 err = got_object_get_type(&obj_type, repo,
6083 got_object_tag_get_object_id(tag));
6084 if (err)
6085 goto done;
6086 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6087 err = got_error(GOT_ERR_OBJ_TYPE);
6088 goto done;
6090 *commit_id = got_object_id_dup(
6091 got_object_tag_get_object_id(tag));
6092 if (*commit_id == NULL) {
6093 err = got_error_from_errno("got_object_id_dup");
6094 goto done;
6096 break;
6097 default:
6098 err = got_error(GOT_ERR_OBJ_TYPE);
6099 break;
6102 done:
6103 if (tag)
6104 got_object_tag_close(tag);
6105 if (err) {
6106 free(*commit_id);
6107 *commit_id = NULL;
6109 return err;
6112 static const struct got_error *
6113 log_ref_entry(struct tog_view **new_view, int begin_x,
6114 struct tog_reflist_entry *re, struct got_repository *repo)
6116 struct tog_view *log_view;
6117 const struct got_error *err = NULL;
6118 struct got_object_id *commit_id = NULL;
6120 *new_view = NULL;
6122 err = resolve_reflist_entry(&commit_id, re, repo);
6123 if (err) {
6124 if (err->code != GOT_ERR_OBJ_TYPE)
6125 return err;
6126 else
6127 return NULL;
6130 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6131 if (log_view == NULL) {
6132 err = got_error_from_errno("view_open");
6133 goto done;
6136 err = open_log_view(log_view, commit_id, repo,
6137 got_ref_get_name(re->ref), "", 0);
6138 done:
6139 if (err)
6140 view_close(log_view);
6141 else
6142 *new_view = log_view;
6143 free(commit_id);
6144 return err;
6147 static void
6148 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6150 struct tog_reflist_entry *re;
6151 int i = 0;
6153 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6154 return;
6156 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6157 while (i++ < maxscroll) {
6158 if (re == NULL)
6159 break;
6160 s->first_displayed_entry = re;
6161 re = TAILQ_PREV(re, tog_reflist_head, entry);
6165 static void
6166 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6168 struct tog_reflist_entry *next, *last;
6169 int n = 0;
6171 if (s->first_displayed_entry)
6172 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6173 else
6174 next = TAILQ_FIRST(&s->refs);
6176 last = s->last_displayed_entry;
6177 while (next && last && n++ < maxscroll) {
6178 last = TAILQ_NEXT(last, entry);
6179 if (last) {
6180 s->first_displayed_entry = next;
6181 next = TAILQ_NEXT(next, entry);
6186 static const struct got_error *
6187 search_start_ref_view(struct tog_view *view)
6189 struct tog_ref_view_state *s = &view->state.ref;
6191 s->matched_entry = NULL;
6192 return NULL;
6195 static int
6196 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6198 regmatch_t regmatch;
6200 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6201 0) == 0;
6204 static const struct got_error *
6205 search_next_ref_view(struct tog_view *view)
6207 struct tog_ref_view_state *s = &view->state.ref;
6208 struct tog_reflist_entry *re = NULL;
6210 if (!view->searching) {
6211 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6212 return NULL;
6215 if (s->matched_entry) {
6216 if (view->searching == TOG_SEARCH_FORWARD) {
6217 if (s->selected_entry)
6218 re = TAILQ_NEXT(s->selected_entry, entry);
6219 else
6220 re = TAILQ_PREV(s->selected_entry,
6221 tog_reflist_head, entry);
6222 } else {
6223 if (s->selected_entry == NULL)
6224 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6225 else
6226 re = TAILQ_PREV(s->selected_entry,
6227 tog_reflist_head, entry);
6229 } else {
6230 if (s->selected_entry)
6231 re = s->selected_entry;
6232 else if (view->searching == TOG_SEARCH_FORWARD)
6233 re = TAILQ_FIRST(&s->refs);
6234 else
6235 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6238 while (1) {
6239 if (re == NULL) {
6240 if (s->matched_entry == NULL) {
6241 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6242 return NULL;
6244 if (view->searching == TOG_SEARCH_FORWARD)
6245 re = TAILQ_FIRST(&s->refs);
6246 else
6247 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6250 if (match_reflist_entry(re, &view->regex)) {
6251 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6252 s->matched_entry = re;
6253 break;
6256 if (view->searching == TOG_SEARCH_FORWARD)
6257 re = TAILQ_NEXT(re, entry);
6258 else
6259 re = TAILQ_PREV(re, tog_reflist_head, entry);
6262 if (s->matched_entry) {
6263 s->first_displayed_entry = s->matched_entry;
6264 s->selected = 0;
6267 return NULL;
6270 static const struct got_error *
6271 show_ref_view(struct tog_view *view)
6273 const struct got_error *err = NULL;
6274 struct tog_ref_view_state *s = &view->state.ref;
6275 struct tog_reflist_entry *re;
6276 char *line = NULL;
6277 wchar_t *wline;
6278 struct tog_color *tc;
6279 int width, n;
6280 int limit = view->nlines;
6282 werase(view->window);
6284 s->ndisplayed = 0;
6286 if (limit == 0)
6287 return NULL;
6289 re = s->first_displayed_entry;
6291 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6292 s->nrefs) == -1)
6293 return got_error_from_errno("asprintf");
6295 err = format_line(&wline, &width, line, view->ncols, 0);
6296 if (err) {
6297 free(line);
6298 return err;
6300 if (view_needs_focus_indication(view))
6301 wstandout(view->window);
6302 waddwstr(view->window, wline);
6303 if (view_needs_focus_indication(view))
6304 wstandend(view->window);
6305 free(wline);
6306 wline = NULL;
6307 free(line);
6308 line = NULL;
6309 if (width < view->ncols - 1)
6310 waddch(view->window, '\n');
6311 if (--limit <= 0)
6312 return NULL;
6314 n = 0;
6315 while (re && limit > 0) {
6316 char *line = NULL;
6318 if (got_ref_is_symbolic(re->ref)) {
6319 if (asprintf(&line, "%s -> %s",
6320 got_ref_get_name(re->ref),
6321 got_ref_get_symref_target(re->ref)) == -1)
6322 return got_error_from_errno("asprintf");
6323 } else if (s->show_ids) {
6324 struct got_object_id *id;
6325 char *id_str;
6326 err = got_ref_resolve(&id, s->repo, re->ref);
6327 if (err)
6328 return err;
6329 err = got_object_id_str(&id_str, id);
6330 if (err) {
6331 free(id);
6332 return err;
6334 if (asprintf(&line, "%s: %s",
6335 got_ref_get_name(re->ref), id_str) == -1) {
6336 err = got_error_from_errno("asprintf");
6337 free(id);
6338 free(id_str);
6339 return err;
6341 free(id);
6342 free(id_str);
6343 } else {
6344 line = strdup(got_ref_get_name(re->ref));
6345 if (line == NULL)
6346 return got_error_from_errno("strdup");
6349 err = format_line(&wline, &width, line, view->ncols, 0);
6350 if (err) {
6351 free(line);
6352 return err;
6354 if (n == s->selected) {
6355 if (view->focussed)
6356 wstandout(view->window);
6357 s->selected_entry = re;
6359 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6360 if (tc)
6361 wattr_on(view->window,
6362 COLOR_PAIR(tc->colorpair), NULL);
6363 waddwstr(view->window, wline);
6364 if (tc)
6365 wattr_off(view->window,
6366 COLOR_PAIR(tc->colorpair), NULL);
6367 if (width < view->ncols - 1)
6368 waddch(view->window, '\n');
6369 if (n == s->selected && view->focussed)
6370 wstandend(view->window);
6371 free(line);
6372 free(wline);
6373 wline = NULL;
6374 n++;
6375 s->ndisplayed++;
6376 s->last_displayed_entry = re;
6378 limit--;
6379 re = TAILQ_NEXT(re, entry);
6382 view_vborder(view);
6383 return err;
6386 static const struct got_error *
6387 browse_ref_tree(struct tog_view **new_view, int begin_x,
6388 struct tog_reflist_entry *re, struct got_repository *repo)
6390 const struct got_error *err = NULL;
6391 struct got_object_id *commit_id = NULL;
6392 struct tog_view *tree_view;
6394 *new_view = NULL;
6396 err = resolve_reflist_entry(&commit_id, re, repo);
6397 if (err) {
6398 if (err->code != GOT_ERR_OBJ_TYPE)
6399 return err;
6400 else
6401 return NULL;
6405 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6406 if (tree_view == NULL) {
6407 err = got_error_from_errno("view_open");
6408 goto done;
6411 err = open_tree_view(tree_view, commit_id,
6412 got_ref_get_name(re->ref), repo);
6413 if (err)
6414 goto done;
6416 *new_view = tree_view;
6417 done:
6418 free(commit_id);
6419 return err;
6421 static const struct got_error *
6422 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6424 const struct got_error *err = NULL;
6425 struct tog_ref_view_state *s = &view->state.ref;
6426 struct tog_view *log_view, *tree_view;
6427 struct tog_reflist_entry *re;
6428 int begin_x = 0, n, nscroll = view->nlines - 1;
6430 switch (ch) {
6431 case 'i':
6432 s->show_ids = !s->show_ids;
6433 break;
6434 case 'o':
6435 s->sort_by_date = !s->sort_by_date;
6436 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6437 got_ref_cmp_by_commit_timestamp_descending :
6438 tog_ref_cmp_by_name, s->repo);
6439 if (err)
6440 break;
6441 got_reflist_object_id_map_free(tog_refs_idmap);
6442 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6443 &tog_refs, s->repo);
6444 if (err)
6445 break;
6446 ref_view_free_refs(s);
6447 err = ref_view_load_refs(s);
6448 break;
6449 case KEY_ENTER:
6450 case '\r':
6451 if (!s->selected_entry)
6452 break;
6453 if (view_is_parent_view(view))
6454 begin_x = view_split_begin_x(view->begin_x);
6455 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6456 s->repo);
6457 view->focussed = 0;
6458 log_view->focussed = 1;
6459 if (view_is_parent_view(view)) {
6460 err = view_close_child(view);
6461 if (err)
6462 return err;
6463 view_set_child(view, log_view);
6464 view->focus_child = 1;
6465 } else
6466 *new_view = log_view;
6467 break;
6468 case 't':
6469 if (!s->selected_entry)
6470 break;
6471 if (view_is_parent_view(view))
6472 begin_x = view_split_begin_x(view->begin_x);
6473 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6474 s->repo);
6475 if (err || tree_view == NULL)
6476 break;
6477 view->focussed = 0;
6478 tree_view->focussed = 1;
6479 if (view_is_parent_view(view)) {
6480 err = view_close_child(view);
6481 if (err)
6482 return err;
6483 view_set_child(view, tree_view);
6484 view->focus_child = 1;
6485 } else
6486 *new_view = tree_view;
6487 break;
6488 case 'g':
6489 case KEY_HOME:
6490 s->selected = 0;
6491 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6492 break;
6493 case 'G':
6494 case KEY_END:
6495 s->selected = 0;
6496 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6497 for (n = 0; n < view->nlines - 1; n++) {
6498 if (re == NULL)
6499 break;
6500 s->first_displayed_entry = re;
6501 re = TAILQ_PREV(re, tog_reflist_head, entry);
6503 if (n > 0)
6504 s->selected = n - 1;
6505 break;
6506 case 'k':
6507 case KEY_UP:
6508 case CTRL('p'):
6509 if (s->selected > 0) {
6510 s->selected--;
6511 break;
6513 ref_scroll_up(s, 1);
6514 break;
6515 case CTRL('u'):
6516 case 'u':
6517 nscroll /= 2;
6518 /* FALL THROUGH */
6519 case KEY_PPAGE:
6520 case CTRL('b'):
6521 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6522 s->selected -= MIN(nscroll, s->selected);
6523 ref_scroll_up(s, MAX(0, nscroll));
6524 break;
6525 case 'j':
6526 case KEY_DOWN:
6527 case CTRL('n'):
6528 if (s->selected < s->ndisplayed - 1) {
6529 s->selected++;
6530 break;
6532 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6533 /* can't scroll any further */
6534 break;
6535 ref_scroll_down(s, 1);
6536 break;
6537 case CTRL('d'):
6538 case 'd':
6539 nscroll /= 2;
6540 /* FALL THROUGH */
6541 case KEY_NPAGE:
6542 case CTRL('f'):
6543 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6544 /* can't scroll any further; move cursor down */
6545 if (s->selected < s->ndisplayed - 1)
6546 s->selected += MIN(nscroll,
6547 s->ndisplayed - s->selected - 1);
6548 break;
6550 ref_scroll_down(s, nscroll);
6551 break;
6552 case CTRL('l'):
6553 tog_free_refs();
6554 err = tog_load_refs(s->repo, s->sort_by_date);
6555 if (err)
6556 break;
6557 ref_view_free_refs(s);
6558 err = ref_view_load_refs(s);
6559 break;
6560 case KEY_RESIZE:
6561 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6562 s->selected = view->nlines - 2;
6563 break;
6564 default:
6565 break;
6568 return err;
6571 __dead static void
6572 usage_ref(void)
6574 endwin();
6575 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6576 getprogname());
6577 exit(1);
6580 static const struct got_error *
6581 cmd_ref(int argc, char *argv[])
6583 const struct got_error *error;
6584 struct got_repository *repo = NULL;
6585 struct got_worktree *worktree = NULL;
6586 char *cwd = NULL, *repo_path = NULL;
6587 int ch;
6588 struct tog_view *view;
6589 int *pack_fds = NULL;
6591 while ((ch = getopt(argc, argv, "r:")) != -1) {
6592 switch (ch) {
6593 case 'r':
6594 repo_path = realpath(optarg, NULL);
6595 if (repo_path == NULL)
6596 return got_error_from_errno2("realpath",
6597 optarg);
6598 break;
6599 default:
6600 usage_ref();
6601 /* NOTREACHED */
6605 argc -= optind;
6606 argv += optind;
6608 if (argc > 1)
6609 usage_ref();
6611 error = got_repo_pack_fds_open(&pack_fds);
6612 if (error != NULL)
6613 goto done;
6615 if (repo_path == NULL) {
6616 cwd = getcwd(NULL, 0);
6617 if (cwd == NULL)
6618 return got_error_from_errno("getcwd");
6619 error = got_worktree_open(&worktree, cwd);
6620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6621 goto done;
6622 if (worktree)
6623 repo_path =
6624 strdup(got_worktree_get_repo_path(worktree));
6625 else
6626 repo_path = strdup(cwd);
6627 if (repo_path == NULL) {
6628 error = got_error_from_errno("strdup");
6629 goto done;
6633 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6634 if (error != NULL)
6635 goto done;
6637 init_curses();
6639 error = apply_unveil(got_repo_get_path(repo), NULL);
6640 if (error)
6641 goto done;
6643 error = tog_load_refs(repo, 0);
6644 if (error)
6645 goto done;
6647 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6648 if (view == NULL) {
6649 error = got_error_from_errno("view_open");
6650 goto done;
6653 error = open_ref_view(view, repo);
6654 if (error)
6655 goto done;
6657 if (worktree) {
6658 /* Release work tree lock. */
6659 got_worktree_close(worktree);
6660 worktree = NULL;
6662 error = view_loop(view);
6663 done:
6664 free(repo_path);
6665 free(cwd);
6666 if (repo) {
6667 const struct got_error *close_err = got_repo_close(repo);
6668 if (close_err)
6669 error = close_err;
6671 if (pack_fds) {
6672 const struct got_error *pack_err =
6673 got_repo_pack_fds_close(pack_fds);
6674 if (error == NULL)
6675 error = pack_err;
6677 tog_free_refs();
6678 return error;
6681 static void
6682 list_commands(FILE *fp)
6684 size_t i;
6686 fprintf(fp, "commands:");
6687 for (i = 0; i < nitems(tog_commands); i++) {
6688 const struct tog_cmd *cmd = &tog_commands[i];
6689 fprintf(fp, " %s", cmd->name);
6691 fputc('\n', fp);
6694 __dead static void
6695 usage(int hflag, int status)
6697 FILE *fp = (status == 0) ? stdout : stderr;
6699 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6700 getprogname());
6701 if (hflag) {
6702 fprintf(fp, "lazy usage: %s path\n", getprogname());
6703 list_commands(fp);
6705 exit(status);
6708 static char **
6709 make_argv(int argc, ...)
6711 va_list ap;
6712 char **argv;
6713 int i;
6715 va_start(ap, argc);
6717 argv = calloc(argc, sizeof(char *));
6718 if (argv == NULL)
6719 err(1, "calloc");
6720 for (i = 0; i < argc; i++) {
6721 argv[i] = strdup(va_arg(ap, char *));
6722 if (argv[i] == NULL)
6723 err(1, "strdup");
6726 va_end(ap);
6727 return argv;
6731 * Try to convert 'tog path' into a 'tog log path' command.
6732 * The user could simply have mistyped the command rather than knowingly
6733 * provided a path. So check whether argv[0] can in fact be resolved
6734 * to a path in the HEAD commit and print a special error if not.
6735 * This hack is for mpi@ <3
6737 static const struct got_error *
6738 tog_log_with_path(int argc, char *argv[])
6740 const struct got_error *error = NULL, *close_err;
6741 const struct tog_cmd *cmd = NULL;
6742 struct got_repository *repo = NULL;
6743 struct got_worktree *worktree = NULL;
6744 struct got_object_id *commit_id = NULL, *id = NULL;
6745 struct got_commit_object *commit = NULL;
6746 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6747 char *commit_id_str = NULL, **cmd_argv = NULL;
6748 int *pack_fds = NULL;
6750 cwd = getcwd(NULL, 0);
6751 if (cwd == NULL)
6752 return got_error_from_errno("getcwd");
6754 error = got_repo_pack_fds_open(&pack_fds);
6755 if (error != NULL)
6756 goto done;
6758 error = got_worktree_open(&worktree, cwd);
6759 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6760 goto done;
6762 if (worktree)
6763 repo_path = strdup(got_worktree_get_repo_path(worktree));
6764 else
6765 repo_path = strdup(cwd);
6766 if (repo_path == NULL) {
6767 error = got_error_from_errno("strdup");
6768 goto done;
6771 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6772 if (error != NULL)
6773 goto done;
6775 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6776 repo, worktree);
6777 if (error)
6778 goto done;
6780 error = tog_load_refs(repo, 0);
6781 if (error)
6782 goto done;
6783 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6784 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6785 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6786 if (error)
6787 goto done;
6789 if (worktree) {
6790 got_worktree_close(worktree);
6791 worktree = NULL;
6794 error = got_object_open_as_commit(&commit, repo, commit_id);
6795 if (error)
6796 goto done;
6798 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6799 if (error) {
6800 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6801 goto done;
6802 fprintf(stderr, "%s: '%s' is no known command or path\n",
6803 getprogname(), argv[0]);
6804 usage(1, 1);
6805 /* not reached */
6808 close_err = got_repo_close(repo);
6809 if (error == NULL)
6810 error = close_err;
6811 repo = NULL;
6813 error = got_object_id_str(&commit_id_str, commit_id);
6814 if (error)
6815 goto done;
6817 cmd = &tog_commands[0]; /* log */
6818 argc = 4;
6819 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6820 error = cmd->cmd_main(argc, cmd_argv);
6821 done:
6822 if (repo) {
6823 close_err = got_repo_close(repo);
6824 if (error == NULL)
6825 error = close_err;
6827 if (commit)
6828 got_object_commit_close(commit);
6829 if (worktree)
6830 got_worktree_close(worktree);
6831 if (pack_fds) {
6832 const struct got_error *pack_err =
6833 got_repo_pack_fds_close(pack_fds);
6834 if (error == NULL)
6835 error = pack_err;
6837 free(id);
6838 free(commit_id_str);
6839 free(commit_id);
6840 free(cwd);
6841 free(repo_path);
6842 free(in_repo_path);
6843 if (cmd_argv) {
6844 int i;
6845 for (i = 0; i < argc; i++)
6846 free(cmd_argv[i]);
6847 free(cmd_argv);
6849 tog_free_refs();
6850 return error;
6853 int
6854 main(int argc, char *argv[])
6856 const struct got_error *error = NULL;
6857 const struct tog_cmd *cmd = NULL;
6858 int ch, hflag = 0, Vflag = 0;
6859 char **cmd_argv = NULL;
6860 static const struct option longopts[] = {
6861 { "version", no_argument, NULL, 'V' },
6862 { NULL, 0, NULL, 0}
6865 setlocale(LC_CTYPE, "");
6867 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6868 switch (ch) {
6869 case 'h':
6870 hflag = 1;
6871 break;
6872 case 'V':
6873 Vflag = 1;
6874 break;
6875 default:
6876 usage(hflag, 1);
6877 /* NOTREACHED */
6881 argc -= optind;
6882 argv += optind;
6883 optind = 1;
6884 optreset = 1;
6886 if (Vflag) {
6887 got_version_print_str();
6888 return 0;
6891 #ifndef PROFILE
6892 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6893 NULL) == -1)
6894 err(1, "pledge");
6895 #endif
6897 if (argc == 0) {
6898 if (hflag)
6899 usage(hflag, 0);
6900 /* Build an argument vector which runs a default command. */
6901 cmd = &tog_commands[0];
6902 argc = 1;
6903 cmd_argv = make_argv(argc, cmd->name);
6904 } else {
6905 size_t i;
6907 /* Did the user specify a command? */
6908 for (i = 0; i < nitems(tog_commands); i++) {
6909 if (strncmp(tog_commands[i].name, argv[0],
6910 strlen(argv[0])) == 0) {
6911 cmd = &tog_commands[i];
6912 break;
6917 if (cmd == NULL) {
6918 if (argc != 1)
6919 usage(0, 1);
6920 /* No command specified; try log with a path */
6921 error = tog_log_with_path(argc, argv);
6922 } else {
6923 if (hflag)
6924 cmd->cmd_usage();
6925 else
6926 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6929 endwin();
6930 putchar('\n');
6931 if (cmd_argv) {
6932 int i;
6933 for (i = 0; i < argc; i++)
6934 free(cmd_argv[i]);
6935 free(cmd_argv);
6938 if (error && error->code != GOT_ERR_CANCELLED)
6939 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6940 return 0;