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 };
415 struct tog_blame_view_state {
416 int first_displayed_line;
417 int last_displayed_line;
418 int selected_line;
419 int blame_complete;
420 int eof;
421 int done;
422 struct got_object_id_queue blamed_commits;
423 struct got_object_qid *blamed_commit;
424 char *path;
425 struct got_repository *repo;
426 struct got_object_id *commit_id;
427 struct tog_blame blame;
428 int matched_line;
429 struct tog_colors colors;
430 };
432 struct tog_parent_tree {
433 TAILQ_ENTRY(tog_parent_tree) entry;
434 struct got_tree_object *tree;
435 struct got_tree_entry *first_displayed_entry;
436 struct got_tree_entry *selected_entry;
437 int selected;
438 };
440 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
442 struct tog_tree_view_state {
443 char *tree_label;
444 struct got_object_id *commit_id;/* commit which this tree belongs to */
445 struct got_tree_object *root; /* the commit's root tree entry */
446 struct got_tree_object *tree; /* currently displayed (sub-)tree */
447 struct got_tree_entry *first_displayed_entry;
448 struct got_tree_entry *last_displayed_entry;
449 struct got_tree_entry *selected_entry;
450 int ndisplayed, selected, show_ids;
451 struct tog_parent_trees parents; /* parent trees of current sub-tree */
452 char *head_ref_name;
453 struct got_repository *repo;
454 struct got_tree_entry *matched_entry;
455 struct tog_colors colors;
456 };
458 struct tog_reflist_entry {
459 TAILQ_ENTRY(tog_reflist_entry) entry;
460 struct got_reference *ref;
461 int idx;
462 };
464 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
466 struct tog_ref_view_state {
467 struct tog_reflist_head refs;
468 struct tog_reflist_entry *first_displayed_entry;
469 struct tog_reflist_entry *last_displayed_entry;
470 struct tog_reflist_entry *selected_entry;
471 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
472 struct got_repository *repo;
473 struct tog_reflist_entry *matched_entry;
474 struct tog_colors colors;
475 };
477 /*
478 * We implement two types of views: parent views and child views.
480 * The 'Tab' key switches focus between a parent view and its child view.
481 * Child views are shown side-by-side to their parent view, provided
482 * there is enough screen estate.
484 * When a new view is opened from within a parent view, this new view
485 * becomes a child view of the parent view, replacing any existing child.
487 * When a new view is opened from within a child view, this new view
488 * becomes a parent view which will obscure the views below until the
489 * user quits the new parent view by typing 'q'.
491 * This list of views contains parent views only.
492 * Child views are only pointed to by their parent view.
493 */
494 TAILQ_HEAD(tog_view_list_head, tog_view);
496 struct tog_view {
497 TAILQ_ENTRY(tog_view) entry;
498 WINDOW *window;
499 PANEL *panel;
500 int nlines, ncols, begin_y, begin_x;
501 int lines, cols; /* copies of LINES and COLS */
502 int focussed; /* Only set on one parent or child view at a time. */
503 int dying;
504 struct tog_view *parent;
505 struct tog_view *child;
507 /*
508 * This flag is initially set on parent views when a new child view
509 * is created. It gets toggled when the 'Tab' key switches focus
510 * between parent and child.
511 * The flag indicates whether focus should be passed on to our child
512 * view if this parent view gets picked for focus after another parent
513 * view was closed. This prevents child views from losing focus in such
514 * situations.
515 */
516 int focus_child;
518 /* type-specific state */
519 enum tog_view_type type;
520 union {
521 struct tog_diff_view_state diff;
522 struct tog_log_view_state log;
523 struct tog_blame_view_state blame;
524 struct tog_tree_view_state tree;
525 struct tog_ref_view_state ref;
526 } state;
528 const struct got_error *(*show)(struct tog_view *);
529 const struct got_error *(*input)(struct tog_view **,
530 struct tog_view *, int);
531 const struct got_error *(*close)(struct tog_view *);
533 const struct got_error *(*search_start)(struct tog_view *);
534 const struct got_error *(*search_next)(struct tog_view *);
535 int search_started;
536 int searching;
537 #define TOG_SEARCH_FORWARD 1
538 #define TOG_SEARCH_BACKWARD 2
539 int search_next_done;
540 #define TOG_SEARCH_HAVE_MORE 1
541 #define TOG_SEARCH_NO_MORE 2
542 #define TOG_SEARCH_HAVE_NONE 3
543 regex_t regex;
544 regmatch_t regmatch;
545 };
547 static const struct got_error *open_diff_view(struct tog_view *,
548 struct got_object_id *, struct got_object_id *,
549 const char *, const char *, int, int, int, struct tog_view *,
550 struct got_repository *);
551 static const struct got_error *show_diff_view(struct tog_view *);
552 static const struct got_error *input_diff_view(struct tog_view **,
553 struct tog_view *, int);
554 static const struct got_error* close_diff_view(struct tog_view *);
555 static const struct got_error *search_start_diff_view(struct tog_view *);
556 static const struct got_error *search_next_diff_view(struct tog_view *);
558 static const struct got_error *open_log_view(struct tog_view *,
559 struct got_object_id *, struct got_repository *,
560 const char *, const char *, int);
561 static const struct got_error * show_log_view(struct tog_view *);
562 static const struct got_error *input_log_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error *close_log_view(struct tog_view *);
565 static const struct got_error *search_start_log_view(struct tog_view *);
566 static const struct got_error *search_next_log_view(struct tog_view *);
568 static const struct got_error *open_blame_view(struct tog_view *, char *,
569 struct got_object_id *, struct got_repository *);
570 static const struct got_error *show_blame_view(struct tog_view *);
571 static const struct got_error *input_blame_view(struct tog_view **,
572 struct tog_view *, int);
573 static const struct got_error *close_blame_view(struct tog_view *);
574 static const struct got_error *search_start_blame_view(struct tog_view *);
575 static const struct got_error *search_next_blame_view(struct tog_view *);
577 static const struct got_error *open_tree_view(struct tog_view *,
578 struct got_object_id *, const char *, struct got_repository *);
579 static const struct got_error *show_tree_view(struct tog_view *);
580 static const struct got_error *input_tree_view(struct tog_view **,
581 struct tog_view *, int);
582 static const struct got_error *close_tree_view(struct tog_view *);
583 static const struct got_error *search_start_tree_view(struct tog_view *);
584 static const struct got_error *search_next_tree_view(struct tog_view *);
586 static const struct got_error *open_ref_view(struct tog_view *,
587 struct got_repository *);
588 static const struct got_error *show_ref_view(struct tog_view *);
589 static const struct got_error *input_ref_view(struct tog_view **,
590 struct tog_view *, int);
591 static const struct got_error *close_ref_view(struct tog_view *);
592 static const struct got_error *search_start_ref_view(struct tog_view *);
593 static const struct got_error *search_next_ref_view(struct tog_view *);
595 static volatile sig_atomic_t tog_sigwinch_received;
596 static volatile sig_atomic_t tog_sigpipe_received;
597 static volatile sig_atomic_t tog_sigcont_received;
598 static volatile sig_atomic_t tog_sigint_received;
599 static volatile sig_atomic_t tog_sigterm_received;
601 static void
602 tog_sigwinch(int signo)
604 tog_sigwinch_received = 1;
607 static void
608 tog_sigpipe(int signo)
610 tog_sigpipe_received = 1;
613 static void
614 tog_sigcont(int signo)
616 tog_sigcont_received = 1;
619 static void
620 tog_sigint(int signo)
622 tog_sigint_received = 1;
625 static void
626 tog_sigterm(int signo)
628 tog_sigterm_received = 1;
631 static int
632 tog_fatal_signal_received()
634 return (tog_sigpipe_received ||
635 tog_sigint_received || tog_sigint_received);
639 static const struct got_error *
640 view_close(struct tog_view *view)
642 const struct got_error *err = NULL;
644 if (view->child) {
645 view_close(view->child);
646 view->child = NULL;
648 if (view->close)
649 err = view->close(view);
650 if (view->panel)
651 del_panel(view->panel);
652 if (view->window)
653 delwin(view->window);
654 free(view);
655 return err;
658 static struct tog_view *
659 view_open(int nlines, int ncols, int begin_y, int begin_x,
660 enum tog_view_type type)
662 struct tog_view *view = calloc(1, sizeof(*view));
664 if (view == NULL)
665 return NULL;
667 view->type = type;
668 view->lines = LINES;
669 view->cols = COLS;
670 view->nlines = nlines ? nlines : LINES - begin_y;
671 view->ncols = ncols ? ncols : COLS - begin_x;
672 view->begin_y = begin_y;
673 view->begin_x = begin_x;
674 view->window = newwin(nlines, ncols, begin_y, begin_x);
675 if (view->window == NULL) {
676 view_close(view);
677 return NULL;
679 view->panel = new_panel(view->window);
680 if (view->panel == NULL ||
681 set_panel_userptr(view->panel, view) != OK) {
682 view_close(view);
683 return NULL;
686 keypad(view->window, TRUE);
687 return view;
690 static int
691 view_split_begin_x(int begin_x)
693 if (begin_x > 0 || COLS < 120)
694 return 0;
695 return (COLS - MAX(COLS / 2, 80));
698 static const struct got_error *view_resize(struct tog_view *);
700 static const struct got_error *
701 view_splitscreen(struct tog_view *view)
703 const struct got_error *err = NULL;
705 view->begin_y = 0;
706 view->begin_x = view_split_begin_x(0);
707 view->nlines = LINES;
708 view->ncols = COLS - view->begin_x;
709 view->lines = LINES;
710 view->cols = COLS;
711 err = view_resize(view);
712 if (err)
713 return err;
715 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
716 return got_error_from_errno("mvwin");
718 return NULL;
721 static const struct got_error *
722 view_fullscreen(struct tog_view *view)
724 const struct got_error *err = NULL;
726 view->begin_x = 0;
727 view->begin_y = 0;
728 view->nlines = LINES;
729 view->ncols = COLS;
730 view->lines = LINES;
731 view->cols = COLS;
732 err = view_resize(view);
733 if (err)
734 return err;
736 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
737 return got_error_from_errno("mvwin");
739 return NULL;
742 static int
743 view_is_parent_view(struct tog_view *view)
745 return view->parent == NULL;
748 static const struct got_error *
749 view_resize(struct tog_view *view)
751 int nlines, ncols;
753 if (view->lines > LINES)
754 nlines = view->nlines - (view->lines - LINES);
755 else
756 nlines = view->nlines + (LINES - view->lines);
758 if (view->cols > COLS)
759 ncols = view->ncols - (view->cols - COLS);
760 else
761 ncols = view->ncols + (COLS - view->cols);
763 if (wresize(view->window, nlines, ncols) == ERR)
764 return got_error_from_errno("wresize");
765 if (replace_panel(view->panel, view->window) == ERR)
766 return got_error_from_errno("replace_panel");
767 wclear(view->window);
769 view->nlines = nlines;
770 view->ncols = ncols;
771 view->lines = LINES;
772 view->cols = COLS;
774 if (view->child) {
775 view->child->begin_x = view_split_begin_x(view->begin_x);
776 if (view->child->begin_x == 0) {
777 view_fullscreen(view->child);
778 if (view->child->focussed)
779 show_panel(view->child->panel);
780 else
781 show_panel(view->panel);
782 } else {
783 view_splitscreen(view->child);
784 show_panel(view->child->panel);
788 return NULL;
791 static const struct got_error *
792 view_close_child(struct tog_view *view)
794 const struct got_error *err = NULL;
796 if (view->child == NULL)
797 return NULL;
799 err = view_close(view->child);
800 view->child = NULL;
801 return err;
804 static void
805 view_set_child(struct tog_view *view, struct tog_view *child)
807 view->child = child;
808 child->parent = view;
811 static int
812 view_is_splitscreen(struct tog_view *view)
814 return view->begin_x > 0;
817 static void
818 tog_resizeterm(void)
820 int cols, lines;
821 struct winsize size;
823 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
824 cols = 80; /* Default */
825 lines = 24;
826 } else {
827 cols = size.ws_col;
828 lines = size.ws_row;
830 resize_term(lines, cols);
833 static const struct got_error *
834 view_search_start(struct tog_view *view)
836 const struct got_error *err = NULL;
837 char pattern[1024];
838 int ret;
840 if (view->search_started) {
841 regfree(&view->regex);
842 view->searching = 0;
843 memset(&view->regmatch, 0, sizeof(view->regmatch));
845 view->search_started = 0;
847 if (view->nlines < 1)
848 return NULL;
850 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
851 wclrtoeol(view->window);
853 nocbreak();
854 echo();
855 ret = wgetnstr(view->window, pattern, sizeof(pattern));
856 cbreak();
857 noecho();
858 if (ret == ERR)
859 return NULL;
861 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
862 err = view->search_start(view);
863 if (err) {
864 regfree(&view->regex);
865 return err;
867 view->search_started = 1;
868 view->searching = TOG_SEARCH_FORWARD;
869 view->search_next_done = 0;
870 view->search_next(view);
873 return NULL;
876 static const struct got_error *
877 view_input(struct tog_view **new, int *done, struct tog_view *view,
878 struct tog_view_list_head *views)
880 const struct got_error *err = NULL;
881 struct tog_view *v;
882 int ch, errcode;
884 *new = NULL;
886 /* Clear "no matches" indicator. */
887 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
888 view->search_next_done == TOG_SEARCH_HAVE_NONE)
889 view->search_next_done = TOG_SEARCH_HAVE_MORE;
891 if (view->searching && !view->search_next_done) {
892 errcode = pthread_mutex_unlock(&tog_mutex);
893 if (errcode)
894 return got_error_set_errno(errcode,
895 "pthread_mutex_unlock");
896 sched_yield();
897 errcode = pthread_mutex_lock(&tog_mutex);
898 if (errcode)
899 return got_error_set_errno(errcode,
900 "pthread_mutex_lock");
901 view->search_next(view);
902 return NULL;
905 nodelay(stdscr, FALSE);
906 /* Allow threads to make progress while we are waiting for input. */
907 errcode = pthread_mutex_unlock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode, "pthread_mutex_unlock");
910 ch = wgetch(view->window);
911 errcode = pthread_mutex_lock(&tog_mutex);
912 if (errcode)
913 return got_error_set_errno(errcode, "pthread_mutex_lock");
914 nodelay(stdscr, TRUE);
916 if (tog_sigwinch_received || tog_sigcont_received) {
917 tog_resizeterm();
918 tog_sigwinch_received = 0;
919 tog_sigcont_received = 0;
920 TAILQ_FOREACH(v, views, entry) {
921 err = view_resize(v);
922 if (err)
923 return err;
924 err = v->input(new, v, KEY_RESIZE);
925 if (err)
926 return err;
927 if (v->child) {
928 err = view_resize(v->child);
929 if (err)
930 return err;
931 err = v->child->input(new, v->child,
932 KEY_RESIZE);
933 if (err)
934 return err;
939 switch (ch) {
940 case '\t':
941 if (view->child) {
942 view->focussed = 0;
943 view->child->focussed = 1;
944 view->focus_child = 1;
945 } else if (view->parent) {
946 view->focussed = 0;
947 view->parent->focussed = 1;
948 view->parent->focus_child = 0;
950 break;
951 case 'q':
952 err = view->input(new, view, ch);
953 view->dying = 1;
954 break;
955 case 'Q':
956 *done = 1;
957 break;
958 case 'f':
959 if (view_is_parent_view(view)) {
960 if (view->child == NULL)
961 break;
962 if (view_is_splitscreen(view->child)) {
963 view->focussed = 0;
964 view->child->focussed = 1;
965 err = view_fullscreen(view->child);
966 } else
967 err = view_splitscreen(view->child);
968 if (err)
969 break;
970 err = view->child->input(new, view->child,
971 KEY_RESIZE);
972 } else {
973 if (view_is_splitscreen(view)) {
974 view->parent->focussed = 0;
975 view->focussed = 1;
976 err = view_fullscreen(view);
977 } else {
978 err = view_splitscreen(view);
980 if (err)
981 break;
982 err = view->input(new, view, KEY_RESIZE);
984 break;
985 case KEY_RESIZE:
986 break;
987 case '/':
988 if (view->search_start)
989 view_search_start(view);
990 else
991 err = view->input(new, view, ch);
992 break;
993 case 'N':
994 case 'n':
995 if (view->search_started && view->search_next) {
996 view->searching = (ch == 'n' ?
997 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
998 view->search_next_done = 0;
999 view->search_next(view);
1000 } else
1001 err = view->input(new, view, ch);
1002 break;
1003 default:
1004 err = view->input(new, view, ch);
1005 break;
1008 return err;
1011 void
1012 view_vborder(struct tog_view *view)
1014 PANEL *panel;
1015 const struct tog_view *view_above;
1017 if (view->parent)
1018 return view_vborder(view->parent);
1020 panel = panel_above(view->panel);
1021 if (panel == NULL)
1022 return;
1024 view_above = panel_userptr(panel);
1025 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1026 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1029 int
1030 view_needs_focus_indication(struct tog_view *view)
1032 if (view_is_parent_view(view)) {
1033 if (view->child == NULL || view->child->focussed)
1034 return 0;
1035 if (!view_is_splitscreen(view->child))
1036 return 0;
1037 } else if (!view_is_splitscreen(view))
1038 return 0;
1040 return view->focussed;
1043 static const struct got_error *
1044 view_loop(struct tog_view *view)
1046 const struct got_error *err = NULL;
1047 struct tog_view_list_head views;
1048 struct tog_view *new_view;
1049 int fast_refresh = 10;
1050 int done = 0, errcode;
1052 errcode = pthread_mutex_lock(&tog_mutex);
1053 if (errcode)
1054 return got_error_set_errno(errcode, "pthread_mutex_lock");
1056 TAILQ_INIT(&views);
1057 TAILQ_INSERT_HEAD(&views, view, entry);
1059 view->focussed = 1;
1060 err = view->show(view);
1061 if (err)
1062 return err;
1063 update_panels();
1064 doupdate();
1065 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1066 /* Refresh fast during initialization, then become slower. */
1067 if (fast_refresh && fast_refresh-- == 0)
1068 halfdelay(10); /* switch to once per second */
1070 err = view_input(&new_view, &done, view, &views);
1071 if (err)
1072 break;
1073 if (view->dying) {
1074 struct tog_view *v, *prev = NULL;
1076 if (view_is_parent_view(view))
1077 prev = TAILQ_PREV(view, tog_view_list_head,
1078 entry);
1079 else if (view->parent)
1080 prev = view->parent;
1082 if (view->parent) {
1083 view->parent->child = NULL;
1084 view->parent->focus_child = 0;
1085 } else
1086 TAILQ_REMOVE(&views, view, entry);
1088 err = view_close(view);
1089 if (err)
1090 goto done;
1092 view = NULL;
1093 TAILQ_FOREACH(v, &views, entry) {
1094 if (v->focussed)
1095 break;
1097 if (view == NULL && new_view == NULL) {
1098 /* No view has focus. Try to pick one. */
1099 if (prev)
1100 view = prev;
1101 else if (!TAILQ_EMPTY(&views)) {
1102 view = TAILQ_LAST(&views,
1103 tog_view_list_head);
1105 if (view) {
1106 if (view->focus_child) {
1107 view->child->focussed = 1;
1108 view = view->child;
1109 } else
1110 view->focussed = 1;
1114 if (new_view) {
1115 struct tog_view *v, *t;
1116 /* Only allow one parent view per type. */
1117 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1118 if (v->type != new_view->type)
1119 continue;
1120 TAILQ_REMOVE(&views, v, entry);
1121 err = view_close(v);
1122 if (err)
1123 goto done;
1124 break;
1126 TAILQ_INSERT_TAIL(&views, new_view, entry);
1127 view = new_view;
1129 if (view) {
1130 if (view_is_parent_view(view)) {
1131 if (view->child && view->child->focussed)
1132 view = view->child;
1133 } else {
1134 if (view->parent && view->parent->focussed)
1135 view = view->parent;
1137 show_panel(view->panel);
1138 if (view->child && view_is_splitscreen(view->child))
1139 show_panel(view->child->panel);
1140 if (view->parent && view_is_splitscreen(view)) {
1141 err = view->parent->show(view->parent);
1142 if (err)
1143 goto done;
1145 err = view->show(view);
1146 if (err)
1147 goto done;
1148 if (view->child) {
1149 err = view->child->show(view->child);
1150 if (err)
1151 goto done;
1153 update_panels();
1154 doupdate();
1157 done:
1158 while (!TAILQ_EMPTY(&views)) {
1159 view = TAILQ_FIRST(&views);
1160 TAILQ_REMOVE(&views, view, entry);
1161 view_close(view);
1164 errcode = pthread_mutex_unlock(&tog_mutex);
1165 if (errcode && err == NULL)
1166 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1168 return err;
1171 __dead static void
1172 usage_log(void)
1174 endwin();
1175 fprintf(stderr,
1176 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1177 getprogname());
1178 exit(1);
1181 /* Create newly allocated wide-character string equivalent to a byte string. */
1182 static const struct got_error *
1183 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1185 char *vis = NULL;
1186 const struct got_error *err = NULL;
1188 *ws = NULL;
1189 *wlen = mbstowcs(NULL, s, 0);
1190 if (*wlen == (size_t)-1) {
1191 int vislen;
1192 if (errno != EILSEQ)
1193 return got_error_from_errno("mbstowcs");
1195 /* byte string invalid in current encoding; try to "fix" it */
1196 err = got_mbsavis(&vis, &vislen, s);
1197 if (err)
1198 return err;
1199 *wlen = mbstowcs(NULL, vis, 0);
1200 if (*wlen == (size_t)-1) {
1201 err = got_error_from_errno("mbstowcs"); /* give up */
1202 goto done;
1206 *ws = calloc(*wlen + 1, sizeof(**ws));
1207 if (*ws == NULL) {
1208 err = got_error_from_errno("calloc");
1209 goto done;
1212 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1213 err = got_error_from_errno("mbstowcs");
1214 done:
1215 free(vis);
1216 if (err) {
1217 free(*ws);
1218 *ws = NULL;
1219 *wlen = 0;
1221 return err;
1224 /* Format a line for display, ensuring that it won't overflow a width limit. */
1225 static const struct got_error *
1226 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1227 int col_tab_align)
1229 const struct got_error *err = NULL;
1230 int cols = 0;
1231 wchar_t *wline = NULL;
1232 size_t wlen;
1233 int i;
1235 *wlinep = NULL;
1236 *widthp = 0;
1238 err = mbs2ws(&wline, &wlen, line);
1239 if (err)
1240 return err;
1242 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1243 wline[wlen - 1] = L'\0';
1244 wlen--;
1246 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1247 wline[wlen - 1] = L'\0';
1248 wlen--;
1251 i = 0;
1252 while (i < wlen) {
1253 int width = wcwidth(wline[i]);
1255 if (width == 0) {
1256 i++;
1257 continue;
1260 if (width == 1 || width == 2) {
1261 if (cols + width > wlimit)
1262 break;
1263 cols += width;
1264 i++;
1265 } else if (width == -1) {
1266 if (wline[i] == L'\t') {
1267 width = TABSIZE -
1268 ((cols + col_tab_align) % TABSIZE);
1269 } else {
1270 width = 1;
1271 wline[i] = L'.';
1273 if (cols + width > wlimit)
1274 break;
1275 cols += width;
1276 i++;
1277 } else {
1278 err = got_error_from_errno("wcwidth");
1279 goto done;
1282 wline[i] = L'\0';
1283 if (widthp)
1284 *widthp = cols;
1285 done:
1286 if (err)
1287 free(wline);
1288 else
1289 *wlinep = wline;
1290 return err;
1293 static const struct got_error*
1294 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1295 struct got_object_id *id, struct got_repository *repo)
1297 static const struct got_error *err = NULL;
1298 struct got_reflist_entry *re;
1299 char *s;
1300 const char *name;
1302 *refs_str = NULL;
1304 TAILQ_FOREACH(re, refs, entry) {
1305 struct got_tag_object *tag = NULL;
1306 struct got_object_id *ref_id;
1307 int cmp;
1309 name = got_ref_get_name(re->ref);
1310 if (strcmp(name, GOT_REF_HEAD) == 0)
1311 continue;
1312 if (strncmp(name, "refs/", 5) == 0)
1313 name += 5;
1314 if (strncmp(name, "got/", 4) == 0 &&
1315 strncmp(name, "got/backup/", 11) != 0)
1316 continue;
1317 if (strncmp(name, "heads/", 6) == 0)
1318 name += 6;
1319 if (strncmp(name, "remotes/", 8) == 0) {
1320 name += 8;
1321 s = strstr(name, "/" GOT_REF_HEAD);
1322 if (s != NULL && s[strlen(s)] == '\0')
1323 continue;
1325 err = got_ref_resolve(&ref_id, repo, re->ref);
1326 if (err)
1327 break;
1328 if (strncmp(name, "tags/", 5) == 0) {
1329 err = got_object_open_as_tag(&tag, repo, ref_id);
1330 if (err) {
1331 if (err->code != GOT_ERR_OBJ_TYPE) {
1332 free(ref_id);
1333 break;
1335 /* Ref points at something other than a tag. */
1336 err = NULL;
1337 tag = NULL;
1340 cmp = got_object_id_cmp(tag ?
1341 got_object_tag_get_object_id(tag) : ref_id, id);
1342 free(ref_id);
1343 if (tag)
1344 got_object_tag_close(tag);
1345 if (cmp != 0)
1346 continue;
1347 s = *refs_str;
1348 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1349 s ? ", " : "", name) == -1) {
1350 err = got_error_from_errno("asprintf");
1351 free(s);
1352 *refs_str = NULL;
1353 break;
1355 free(s);
1358 return err;
1361 static const struct got_error *
1362 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1363 int col_tab_align)
1365 char *smallerthan;
1367 smallerthan = strchr(author, '<');
1368 if (smallerthan && smallerthan[1] != '\0')
1369 author = smallerthan + 1;
1370 author[strcspn(author, "@>")] = '\0';
1371 return format_line(wauthor, author_width, author, limit, col_tab_align);
1374 static const struct got_error *
1375 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1376 struct got_object_id *id, const size_t date_display_cols,
1377 int author_display_cols)
1379 struct tog_log_view_state *s = &view->state.log;
1380 const struct got_error *err = NULL;
1381 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1382 char *logmsg0 = NULL, *logmsg = NULL;
1383 char *author = NULL;
1384 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1385 int author_width, logmsg_width;
1386 char *newline, *line = NULL;
1387 int col, limit;
1388 const int avail = view->ncols;
1389 struct tm tm;
1390 time_t committer_time;
1391 struct tog_color *tc;
1393 committer_time = got_object_commit_get_committer_time(commit);
1394 if (gmtime_r(&committer_time, &tm) == NULL)
1395 return got_error_from_errno("gmtime_r");
1396 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1397 return got_error(GOT_ERR_NO_SPACE);
1399 if (avail <= date_display_cols)
1400 limit = MIN(sizeof(datebuf) - 1, avail);
1401 else
1402 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1403 tc = get_color(&s->colors, TOG_COLOR_DATE);
1404 if (tc)
1405 wattr_on(view->window,
1406 COLOR_PAIR(tc->colorpair), NULL);
1407 waddnstr(view->window, datebuf, limit);
1408 if (tc)
1409 wattr_off(view->window,
1410 COLOR_PAIR(tc->colorpair), NULL);
1411 col = limit;
1412 if (col > avail)
1413 goto done;
1415 if (avail >= 120) {
1416 char *id_str;
1417 err = got_object_id_str(&id_str, id);
1418 if (err)
1419 goto done;
1420 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1421 if (tc)
1422 wattr_on(view->window,
1423 COLOR_PAIR(tc->colorpair), NULL);
1424 wprintw(view->window, "%.8s ", id_str);
1425 if (tc)
1426 wattr_off(view->window,
1427 COLOR_PAIR(tc->colorpair), NULL);
1428 free(id_str);
1429 col += 9;
1430 if (col > avail)
1431 goto done;
1434 author = strdup(got_object_commit_get_author(commit));
1435 if (author == NULL) {
1436 err = got_error_from_errno("strdup");
1437 goto done;
1439 err = format_author(&wauthor, &author_width, author, avail - col, col);
1440 if (err)
1441 goto done;
1442 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1443 if (tc)
1444 wattr_on(view->window,
1445 COLOR_PAIR(tc->colorpair), NULL);
1446 waddwstr(view->window, wauthor);
1447 if (tc)
1448 wattr_off(view->window,
1449 COLOR_PAIR(tc->colorpair), NULL);
1450 col += author_width;
1451 while (col < avail && author_width < author_display_cols + 2) {
1452 waddch(view->window, ' ');
1453 col++;
1454 author_width++;
1456 if (col > avail)
1457 goto done;
1459 err = got_object_commit_get_logmsg(&logmsg0, commit);
1460 if (err)
1461 goto done;
1462 logmsg = logmsg0;
1463 while (*logmsg == '\n')
1464 logmsg++;
1465 newline = strchr(logmsg, '\n');
1466 if (newline)
1467 *newline = '\0';
1468 limit = avail - col;
1469 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1470 if (err)
1471 goto done;
1472 waddwstr(view->window, wlogmsg);
1473 col += logmsg_width;
1474 while (col < avail) {
1475 waddch(view->window, ' ');
1476 col++;
1478 done:
1479 free(logmsg0);
1480 free(wlogmsg);
1481 free(author);
1482 free(wauthor);
1483 free(line);
1484 return err;
1487 static struct commit_queue_entry *
1488 alloc_commit_queue_entry(struct got_commit_object *commit,
1489 struct got_object_id *id)
1491 struct commit_queue_entry *entry;
1493 entry = calloc(1, sizeof(*entry));
1494 if (entry == NULL)
1495 return NULL;
1497 entry->id = id;
1498 entry->commit = commit;
1499 return entry;
1502 static void
1503 pop_commit(struct commit_queue *commits)
1505 struct commit_queue_entry *entry;
1507 entry = TAILQ_FIRST(&commits->head);
1508 TAILQ_REMOVE(&commits->head, entry, entry);
1509 got_object_commit_close(entry->commit);
1510 commits->ncommits--;
1511 /* Don't free entry->id! It is owned by the commit graph. */
1512 free(entry);
1515 static void
1516 free_commits(struct commit_queue *commits)
1518 while (!TAILQ_EMPTY(&commits->head))
1519 pop_commit(commits);
1522 static const struct got_error *
1523 match_commit(int *have_match, struct got_object_id *id,
1524 struct got_commit_object *commit, regex_t *regex)
1526 const struct got_error *err = NULL;
1527 regmatch_t regmatch;
1528 char *id_str = NULL, *logmsg = NULL;
1530 *have_match = 0;
1532 err = got_object_id_str(&id_str, id);
1533 if (err)
1534 return err;
1536 err = got_object_commit_get_logmsg(&logmsg, commit);
1537 if (err)
1538 goto done;
1540 if (regexec(regex, got_object_commit_get_author(commit), 1,
1541 &regmatch, 0) == 0 ||
1542 regexec(regex, got_object_commit_get_committer(commit), 1,
1543 &regmatch, 0) == 0 ||
1544 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1545 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1546 *have_match = 1;
1547 done:
1548 free(id_str);
1549 free(logmsg);
1550 return err;
1553 static const struct got_error *
1554 queue_commits(struct tog_log_thread_args *a)
1556 const struct got_error *err = NULL;
1559 * We keep all commits open throughout the lifetime of the log
1560 * view in order to avoid having to re-fetch commits from disk
1561 * while updating the display.
1563 do {
1564 struct got_object_id *id;
1565 struct got_commit_object *commit;
1566 struct commit_queue_entry *entry;
1567 int errcode;
1569 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1570 NULL, NULL);
1571 if (err || id == NULL)
1572 break;
1574 err = got_object_open_as_commit(&commit, a->repo, id);
1575 if (err)
1576 break;
1577 entry = alloc_commit_queue_entry(commit, id);
1578 if (entry == NULL) {
1579 err = got_error_from_errno("alloc_commit_queue_entry");
1580 break;
1583 errcode = pthread_mutex_lock(&tog_mutex);
1584 if (errcode) {
1585 err = got_error_set_errno(errcode,
1586 "pthread_mutex_lock");
1587 break;
1590 entry->idx = a->commits->ncommits;
1591 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1592 a->commits->ncommits++;
1594 if (*a->searching == TOG_SEARCH_FORWARD &&
1595 !*a->search_next_done) {
1596 int have_match;
1597 err = match_commit(&have_match, id, commit, a->regex);
1598 if (err)
1599 break;
1600 if (have_match)
1601 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1604 errcode = pthread_mutex_unlock(&tog_mutex);
1605 if (errcode && err == NULL)
1606 err = got_error_set_errno(errcode,
1607 "pthread_mutex_unlock");
1608 if (err)
1609 break;
1610 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1612 return err;
1615 static void
1616 select_commit(struct tog_log_view_state *s)
1618 struct commit_queue_entry *entry;
1619 int ncommits = 0;
1621 entry = s->first_displayed_entry;
1622 while (entry) {
1623 if (ncommits == s->selected) {
1624 s->selected_entry = entry;
1625 break;
1627 entry = TAILQ_NEXT(entry, entry);
1628 ncommits++;
1632 static const struct got_error *
1633 draw_commits(struct tog_view *view)
1635 const struct got_error *err = NULL;
1636 struct tog_log_view_state *s = &view->state.log;
1637 struct commit_queue_entry *entry = s->selected_entry;
1638 const int limit = view->nlines;
1639 int width;
1640 int ncommits, author_cols = 4;
1641 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1642 char *refs_str = NULL;
1643 wchar_t *wline;
1644 struct tog_color *tc;
1645 static const size_t date_display_cols = 12;
1647 if (s->selected_entry &&
1648 !(view->searching && view->search_next_done == 0)) {
1649 struct got_reflist_head *refs;
1650 err = got_object_id_str(&id_str, s->selected_entry->id);
1651 if (err)
1652 return err;
1653 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1654 s->selected_entry->id);
1655 if (refs) {
1656 err = build_refs_str(&refs_str, refs,
1657 s->selected_entry->id, s->repo);
1658 if (err)
1659 goto done;
1663 if (s->thread_args.commits_needed == 0)
1664 halfdelay(10); /* disable fast refresh */
1666 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1667 if (asprintf(&ncommits_str, " [%d/%d] %s",
1668 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1669 (view->searching && !view->search_next_done) ?
1670 "searching..." : "loading...") == -1) {
1671 err = got_error_from_errno("asprintf");
1672 goto done;
1674 } else {
1675 const char *search_str = NULL;
1677 if (view->searching) {
1678 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1679 search_str = "no more matches";
1680 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1681 search_str = "no matches found";
1682 else if (!view->search_next_done)
1683 search_str = "searching...";
1686 if (asprintf(&ncommits_str, " [%d/%d] %s",
1687 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1688 search_str ? search_str :
1689 (refs_str ? refs_str : "")) == -1) {
1690 err = got_error_from_errno("asprintf");
1691 goto done;
1695 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1696 if (asprintf(&header, "commit %s %s%s",
1697 id_str ? id_str : "........................................",
1698 s->in_repo_path, ncommits_str) == -1) {
1699 err = got_error_from_errno("asprintf");
1700 header = NULL;
1701 goto done;
1703 } else if (asprintf(&header, "commit %s%s",
1704 id_str ? id_str : "........................................",
1705 ncommits_str) == -1) {
1706 err = got_error_from_errno("asprintf");
1707 header = NULL;
1708 goto done;
1710 err = format_line(&wline, &width, header, view->ncols, 0);
1711 if (err)
1712 goto done;
1714 werase(view->window);
1716 if (view_needs_focus_indication(view))
1717 wstandout(view->window);
1718 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1719 if (tc)
1720 wattr_on(view->window,
1721 COLOR_PAIR(tc->colorpair), NULL);
1722 waddwstr(view->window, wline);
1723 if (tc)
1724 wattr_off(view->window,
1725 COLOR_PAIR(tc->colorpair), NULL);
1726 while (width < view->ncols) {
1727 waddch(view->window, ' ');
1728 width++;
1730 if (view_needs_focus_indication(view))
1731 wstandend(view->window);
1732 free(wline);
1733 if (limit <= 1)
1734 goto done;
1736 /* Grow author column size if necessary. */
1737 entry = s->first_displayed_entry;
1738 ncommits = 0;
1739 while (entry) {
1740 char *author;
1741 wchar_t *wauthor;
1742 int width;
1743 if (ncommits >= limit - 1)
1744 break;
1745 author = strdup(got_object_commit_get_author(entry->commit));
1746 if (author == NULL) {
1747 err = got_error_from_errno("strdup");
1748 goto done;
1750 err = format_author(&wauthor, &width, author, COLS,
1751 date_display_cols);
1752 if (author_cols < width)
1753 author_cols = width;
1754 free(wauthor);
1755 free(author);
1756 ncommits++;
1757 entry = TAILQ_NEXT(entry, entry);
1760 entry = s->first_displayed_entry;
1761 s->last_displayed_entry = s->first_displayed_entry;
1762 ncommits = 0;
1763 while (entry) {
1764 if (ncommits >= limit - 1)
1765 break;
1766 if (ncommits == s->selected)
1767 wstandout(view->window);
1768 err = draw_commit(view, entry->commit, entry->id,
1769 date_display_cols, author_cols);
1770 if (ncommits == s->selected)
1771 wstandend(view->window);
1772 if (err)
1773 goto done;
1774 ncommits++;
1775 s->last_displayed_entry = entry;
1776 entry = TAILQ_NEXT(entry, entry);
1779 view_vborder(view);
1780 done:
1781 free(id_str);
1782 free(refs_str);
1783 free(ncommits_str);
1784 free(header);
1785 return err;
1788 static void
1789 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1791 struct commit_queue_entry *entry;
1792 int nscrolled = 0;
1794 entry = TAILQ_FIRST(&s->commits.head);
1795 if (s->first_displayed_entry == entry)
1796 return;
1798 entry = s->first_displayed_entry;
1799 while (entry && nscrolled < maxscroll) {
1800 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1801 if (entry) {
1802 s->first_displayed_entry = entry;
1803 nscrolled++;
1808 static const struct got_error *
1809 trigger_log_thread(struct tog_view *view, int wait)
1811 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1812 int errcode;
1814 halfdelay(1); /* fast refresh while loading commits */
1816 while (ta->commits_needed > 0 || ta->load_all) {
1817 if (ta->log_complete)
1818 break;
1820 /* Wake the log thread. */
1821 errcode = pthread_cond_signal(&ta->need_commits);
1822 if (errcode)
1823 return got_error_set_errno(errcode,
1824 "pthread_cond_signal");
1827 * The mutex will be released while the view loop waits
1828 * in wgetch(), at which time the log thread will run.
1830 if (!wait)
1831 break;
1833 /* Display progress update in log view. */
1834 show_log_view(view);
1835 update_panels();
1836 doupdate();
1838 /* Wait right here while next commit is being loaded. */
1839 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1840 if (errcode)
1841 return got_error_set_errno(errcode,
1842 "pthread_cond_wait");
1844 /* Display progress update in log view. */
1845 show_log_view(view);
1846 update_panels();
1847 doupdate();
1850 return NULL;
1853 static const struct got_error *
1854 log_scroll_down(struct tog_view *view, int maxscroll)
1856 struct tog_log_view_state *s = &view->state.log;
1857 const struct got_error *err = NULL;
1858 struct commit_queue_entry *pentry;
1859 int nscrolled = 0, ncommits_needed;
1861 if (s->last_displayed_entry == NULL)
1862 return NULL;
1864 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1865 if (s->commits.ncommits < ncommits_needed &&
1866 !s->thread_args.log_complete) {
1868 * Ask the log thread for required amount of commits.
1870 s->thread_args.commits_needed += maxscroll;
1871 err = trigger_log_thread(view, 1);
1872 if (err)
1873 return err;
1876 do {
1877 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1878 if (pentry == NULL)
1879 break;
1881 s->last_displayed_entry = pentry;
1883 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1884 if (pentry == NULL)
1885 break;
1886 s->first_displayed_entry = pentry;
1887 } while (++nscrolled < maxscroll);
1889 return err;
1892 static const struct got_error *
1893 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1894 struct got_commit_object *commit, struct got_object_id *commit_id,
1895 struct tog_view *log_view, struct got_repository *repo)
1897 const struct got_error *err;
1898 struct got_object_qid *parent_id;
1899 struct tog_view *diff_view;
1901 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1902 if (diff_view == NULL)
1903 return got_error_from_errno("view_open");
1905 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1906 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1907 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1908 if (err == NULL)
1909 *new_view = diff_view;
1910 return err;
1913 static const struct got_error *
1914 tree_view_visit_subtree(struct tog_tree_view_state *s,
1915 struct got_tree_object *subtree)
1917 struct tog_parent_tree *parent;
1919 parent = calloc(1, sizeof(*parent));
1920 if (parent == NULL)
1921 return got_error_from_errno("calloc");
1923 parent->tree = s->tree;
1924 parent->first_displayed_entry = s->first_displayed_entry;
1925 parent->selected_entry = s->selected_entry;
1926 parent->selected = s->selected;
1927 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1928 s->tree = subtree;
1929 s->selected = 0;
1930 s->first_displayed_entry = NULL;
1931 return NULL;
1934 static const struct got_error *
1935 tree_view_walk_path(struct tog_tree_view_state *s,
1936 struct got_commit_object *commit, const char *path)
1938 const struct got_error *err = NULL;
1939 struct got_tree_object *tree = NULL;
1940 const char *p;
1941 char *slash, *subpath = NULL;
1943 /* Walk the path and open corresponding tree objects. */
1944 p = path;
1945 while (*p) {
1946 struct got_tree_entry *te;
1947 struct got_object_id *tree_id;
1948 char *te_name;
1950 while (p[0] == '/')
1951 p++;
1953 /* Ensure the correct subtree entry is selected. */
1954 slash = strchr(p, '/');
1955 if (slash == NULL)
1956 te_name = strdup(p);
1957 else
1958 te_name = strndup(p, slash - p);
1959 if (te_name == NULL) {
1960 err = got_error_from_errno("strndup");
1961 break;
1963 te = got_object_tree_find_entry(s->tree, te_name);
1964 if (te == NULL) {
1965 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1966 free(te_name);
1967 break;
1969 free(te_name);
1970 s->first_displayed_entry = s->selected_entry = te;
1972 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1973 break; /* jump to this file's entry */
1975 slash = strchr(p, '/');
1976 if (slash)
1977 subpath = strndup(path, slash - path);
1978 else
1979 subpath = strdup(path);
1980 if (subpath == NULL) {
1981 err = got_error_from_errno("strdup");
1982 break;
1985 err = got_object_id_by_path(&tree_id, s->repo, commit,
1986 subpath);
1987 if (err)
1988 break;
1990 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1991 free(tree_id);
1992 if (err)
1993 break;
1995 err = tree_view_visit_subtree(s, tree);
1996 if (err) {
1997 got_object_tree_close(tree);
1998 break;
2000 if (slash == NULL)
2001 break;
2002 free(subpath);
2003 subpath = NULL;
2004 p = slash;
2007 free(subpath);
2008 return err;
2011 static const struct got_error *
2012 browse_commit_tree(struct tog_view **new_view, int begin_x,
2013 struct commit_queue_entry *entry, const char *path,
2014 const char *head_ref_name, struct got_repository *repo)
2016 const struct got_error *err = NULL;
2017 struct tog_tree_view_state *s;
2018 struct tog_view *tree_view;
2020 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2021 if (tree_view == NULL)
2022 return got_error_from_errno("view_open");
2024 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2025 if (err)
2026 return err;
2027 s = &tree_view->state.tree;
2029 *new_view = tree_view;
2031 if (got_path_is_root_dir(path))
2032 return NULL;
2034 return tree_view_walk_path(s, entry->commit, path);
2037 static const struct got_error *
2038 block_signals_used_by_main_thread(void)
2040 sigset_t sigset;
2041 int errcode;
2043 if (sigemptyset(&sigset) == -1)
2044 return got_error_from_errno("sigemptyset");
2046 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2047 if (sigaddset(&sigset, SIGWINCH) == -1)
2048 return got_error_from_errno("sigaddset");
2049 if (sigaddset(&sigset, SIGCONT) == -1)
2050 return got_error_from_errno("sigaddset");
2051 if (sigaddset(&sigset, SIGINT) == -1)
2052 return got_error_from_errno("sigaddset");
2053 if (sigaddset(&sigset, SIGTERM) == -1)
2054 return got_error_from_errno("sigaddset");
2056 /* ncurses handles SIGTSTP */
2057 if (sigaddset(&sigset, SIGTSTP) == -1)
2058 return got_error_from_errno("sigaddset");
2060 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2061 if (errcode)
2062 return got_error_set_errno(errcode, "pthread_sigmask");
2064 return NULL;
2067 static void *
2068 log_thread(void *arg)
2070 const struct got_error *err = NULL;
2071 int errcode = 0;
2072 struct tog_log_thread_args *a = arg;
2073 int done = 0;
2075 err = block_signals_used_by_main_thread();
2076 if (err)
2077 return (void *)err;
2079 while (!done && !err && !tog_fatal_signal_received()) {
2080 err = queue_commits(a);
2081 if (err) {
2082 if (err->code != GOT_ERR_ITER_COMPLETED)
2083 return (void *)err;
2084 err = NULL;
2085 done = 1;
2086 } else if (a->commits_needed > 0 && !a->load_all)
2087 a->commits_needed--;
2089 errcode = pthread_mutex_lock(&tog_mutex);
2090 if (errcode) {
2091 err = got_error_set_errno(errcode,
2092 "pthread_mutex_lock");
2093 break;
2094 } else if (*a->quit)
2095 done = 1;
2096 else if (*a->first_displayed_entry == NULL) {
2097 *a->first_displayed_entry =
2098 TAILQ_FIRST(&a->commits->head);
2099 *a->selected_entry = *a->first_displayed_entry;
2102 errcode = pthread_cond_signal(&a->commit_loaded);
2103 if (errcode) {
2104 err = got_error_set_errno(errcode,
2105 "pthread_cond_signal");
2106 pthread_mutex_unlock(&tog_mutex);
2107 break;
2110 if (done)
2111 a->commits_needed = 0;
2112 else {
2113 if (a->commits_needed == 0 && !a->load_all) {
2114 errcode = pthread_cond_wait(&a->need_commits,
2115 &tog_mutex);
2116 if (errcode)
2117 err = got_error_set_errno(errcode,
2118 "pthread_cond_wait");
2119 if (*a->quit)
2120 done = 1;
2124 errcode = pthread_mutex_unlock(&tog_mutex);
2125 if (errcode && err == NULL)
2126 err = got_error_set_errno(errcode,
2127 "pthread_mutex_unlock");
2129 a->log_complete = 1;
2130 return (void *)err;
2133 static const struct got_error *
2134 stop_log_thread(struct tog_log_view_state *s)
2136 const struct got_error *err = NULL;
2137 int errcode;
2139 if (s->thread) {
2140 s->quit = 1;
2141 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2142 if (errcode)
2143 return got_error_set_errno(errcode,
2144 "pthread_cond_signal");
2145 errcode = pthread_mutex_unlock(&tog_mutex);
2146 if (errcode)
2147 return got_error_set_errno(errcode,
2148 "pthread_mutex_unlock");
2149 errcode = pthread_join(s->thread, (void **)&err);
2150 if (errcode)
2151 return got_error_set_errno(errcode, "pthread_join");
2152 errcode = pthread_mutex_lock(&tog_mutex);
2153 if (errcode)
2154 return got_error_set_errno(errcode,
2155 "pthread_mutex_lock");
2156 s->thread = NULL;
2159 if (s->thread_args.repo) {
2160 err = got_repo_close(s->thread_args.repo);
2161 s->thread_args.repo = NULL;
2164 if (s->thread_args.graph) {
2165 got_commit_graph_close(s->thread_args.graph);
2166 s->thread_args.graph = NULL;
2169 return err;
2172 static const struct got_error *
2173 close_log_view(struct tog_view *view)
2175 const struct got_error *err = NULL;
2176 struct tog_log_view_state *s = &view->state.log;
2177 int errcode;
2179 err = stop_log_thread(s);
2181 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2182 if (errcode && err == NULL)
2183 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2185 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2186 if (errcode && err == NULL)
2187 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2189 free_commits(&s->commits);
2190 free(s->in_repo_path);
2191 s->in_repo_path = NULL;
2192 free(s->start_id);
2193 s->start_id = NULL;
2194 free(s->head_ref_name);
2195 s->head_ref_name = NULL;
2196 return err;
2199 static const struct got_error *
2200 search_start_log_view(struct tog_view *view)
2202 struct tog_log_view_state *s = &view->state.log;
2204 s->matched_entry = NULL;
2205 s->search_entry = NULL;
2206 return NULL;
2209 static const struct got_error *
2210 search_next_log_view(struct tog_view *view)
2212 const struct got_error *err = NULL;
2213 struct tog_log_view_state *s = &view->state.log;
2214 struct commit_queue_entry *entry;
2216 /* Display progress update in log view. */
2217 show_log_view(view);
2218 update_panels();
2219 doupdate();
2221 if (s->search_entry) {
2222 int errcode, ch;
2223 errcode = pthread_mutex_unlock(&tog_mutex);
2224 if (errcode)
2225 return got_error_set_errno(errcode,
2226 "pthread_mutex_unlock");
2227 ch = wgetch(view->window);
2228 errcode = pthread_mutex_lock(&tog_mutex);
2229 if (errcode)
2230 return got_error_set_errno(errcode,
2231 "pthread_mutex_lock");
2232 if (ch == KEY_BACKSPACE) {
2233 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2234 return NULL;
2236 if (view->searching == TOG_SEARCH_FORWARD)
2237 entry = TAILQ_NEXT(s->search_entry, entry);
2238 else
2239 entry = TAILQ_PREV(s->search_entry,
2240 commit_queue_head, entry);
2241 } else if (s->matched_entry) {
2242 if (view->searching == TOG_SEARCH_FORWARD)
2243 entry = TAILQ_NEXT(s->matched_entry, entry);
2244 else
2245 entry = TAILQ_PREV(s->matched_entry,
2246 commit_queue_head, entry);
2247 } else {
2248 entry = s->selected_entry;
2251 while (1) {
2252 int have_match = 0;
2254 if (entry == NULL) {
2255 if (s->thread_args.log_complete ||
2256 view->searching == TOG_SEARCH_BACKWARD) {
2257 view->search_next_done =
2258 (s->matched_entry == NULL ?
2259 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2260 s->search_entry = NULL;
2261 return NULL;
2264 * Poke the log thread for more commits and return,
2265 * allowing the main loop to make progress. Search
2266 * will resume at s->search_entry once we come back.
2268 s->thread_args.commits_needed++;
2269 return trigger_log_thread(view, 0);
2272 err = match_commit(&have_match, entry->id, entry->commit,
2273 &view->regex);
2274 if (err)
2275 break;
2276 if (have_match) {
2277 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2278 s->matched_entry = entry;
2279 break;
2282 s->search_entry = entry;
2283 if (view->searching == TOG_SEARCH_FORWARD)
2284 entry = TAILQ_NEXT(entry, entry);
2285 else
2286 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2289 if (s->matched_entry) {
2290 int cur = s->selected_entry->idx;
2291 while (cur < s->matched_entry->idx) {
2292 err = input_log_view(NULL, view, KEY_DOWN);
2293 if (err)
2294 return err;
2295 cur++;
2297 while (cur > s->matched_entry->idx) {
2298 err = input_log_view(NULL, view, KEY_UP);
2299 if (err)
2300 return err;
2301 cur--;
2305 s->search_entry = NULL;
2307 return NULL;
2310 static const struct got_error *
2311 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2312 struct got_repository *repo, const char *head_ref_name,
2313 const char *in_repo_path, int log_branches)
2315 const struct got_error *err = NULL;
2316 struct tog_log_view_state *s = &view->state.log;
2317 struct got_repository *thread_repo = NULL;
2318 struct got_commit_graph *thread_graph = NULL;
2319 int errcode;
2321 if (in_repo_path != s->in_repo_path) {
2322 free(s->in_repo_path);
2323 s->in_repo_path = strdup(in_repo_path);
2324 if (s->in_repo_path == NULL)
2325 return got_error_from_errno("strdup");
2328 /* The commit queue only contains commits being displayed. */
2329 TAILQ_INIT(&s->commits.head);
2330 s->commits.ncommits = 0;
2332 s->repo = repo;
2333 if (head_ref_name) {
2334 s->head_ref_name = strdup(head_ref_name);
2335 if (s->head_ref_name == NULL) {
2336 err = got_error_from_errno("strdup");
2337 goto done;
2340 s->start_id = got_object_id_dup(start_id);
2341 if (s->start_id == NULL) {
2342 err = got_error_from_errno("got_object_id_dup");
2343 goto done;
2345 s->log_branches = log_branches;
2347 STAILQ_INIT(&s->colors);
2348 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2349 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2350 get_color_value("TOG_COLOR_COMMIT"));
2351 if (err)
2352 goto done;
2353 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2354 get_color_value("TOG_COLOR_AUTHOR"));
2355 if (err) {
2356 free_colors(&s->colors);
2357 goto done;
2359 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2360 get_color_value("TOG_COLOR_DATE"));
2361 if (err) {
2362 free_colors(&s->colors);
2363 goto done;
2367 view->show = show_log_view;
2368 view->input = input_log_view;
2369 view->close = close_log_view;
2370 view->search_start = search_start_log_view;
2371 view->search_next = search_next_log_view;
2373 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2374 if (err)
2375 goto done;
2376 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2377 !s->log_branches);
2378 if (err)
2379 goto done;
2380 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2381 s->repo, NULL, NULL);
2382 if (err)
2383 goto done;
2385 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2386 if (errcode) {
2387 err = got_error_set_errno(errcode, "pthread_cond_init");
2388 goto done;
2390 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2391 if (errcode) {
2392 err = got_error_set_errno(errcode, "pthread_cond_init");
2393 goto done;
2396 s->thread_args.commits_needed = view->nlines;
2397 s->thread_args.graph = thread_graph;
2398 s->thread_args.commits = &s->commits;
2399 s->thread_args.in_repo_path = s->in_repo_path;
2400 s->thread_args.start_id = s->start_id;
2401 s->thread_args.repo = thread_repo;
2402 s->thread_args.log_complete = 0;
2403 s->thread_args.quit = &s->quit;
2404 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2405 s->thread_args.selected_entry = &s->selected_entry;
2406 s->thread_args.searching = &view->searching;
2407 s->thread_args.search_next_done = &view->search_next_done;
2408 s->thread_args.regex = &view->regex;
2409 done:
2410 if (err)
2411 close_log_view(view);
2412 return err;
2415 static const struct got_error *
2416 show_log_view(struct tog_view *view)
2418 const struct got_error *err;
2419 struct tog_log_view_state *s = &view->state.log;
2421 if (s->thread == NULL) {
2422 int errcode = pthread_create(&s->thread, NULL, log_thread,
2423 &s->thread_args);
2424 if (errcode)
2425 return got_error_set_errno(errcode, "pthread_create");
2426 if (s->thread_args.commits_needed > 0) {
2427 err = trigger_log_thread(view, 1);
2428 if (err)
2429 return err;
2433 return draw_commits(view);
2436 static const struct got_error *
2437 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2439 const struct got_error *err = NULL;
2440 struct tog_log_view_state *s = &view->state.log;
2441 struct tog_view *diff_view = NULL, *tree_view = NULL;
2442 struct tog_view *ref_view = NULL;
2443 struct commit_queue_entry *entry;
2444 int begin_x = 0, n;
2446 if (s->thread_args.load_all) {
2447 if (ch == KEY_BACKSPACE)
2448 s->thread_args.load_all = 0;
2449 else if (s->thread_args.log_complete) {
2450 s->thread_args.load_all = 0;
2451 log_scroll_down(view, s->commits.ncommits);
2452 s->selected = MIN(view->nlines - 2,
2453 s->commits.ncommits - 1);
2454 select_commit(s);
2456 return NULL;
2459 switch (ch) {
2460 case 'q':
2461 s->quit = 1;
2462 break;
2463 case 'k':
2464 case KEY_UP:
2465 case '<':
2466 case ',':
2467 case CTRL('p'):
2468 if (s->first_displayed_entry == NULL)
2469 break;
2470 if (s->selected > 0)
2471 s->selected--;
2472 else
2473 log_scroll_up(s, 1);
2474 select_commit(s);
2475 break;
2476 case 'g':
2477 case KEY_HOME:
2478 s->selected = 0;
2479 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2480 select_commit(s);
2481 break;
2482 case KEY_PPAGE:
2483 case CTRL('b'):
2484 if (s->first_displayed_entry == NULL)
2485 break;
2486 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2487 s->selected = 0;
2488 else
2489 log_scroll_up(s, view->nlines - 1);
2490 select_commit(s);
2491 break;
2492 case 'j':
2493 case KEY_DOWN:
2494 case '>':
2495 case '.':
2496 case CTRL('n'):
2497 if (s->first_displayed_entry == NULL)
2498 break;
2499 if (s->selected < MIN(view->nlines - 2,
2500 s->commits.ncommits - 1))
2501 s->selected++;
2502 else {
2503 err = log_scroll_down(view, 1);
2504 if (err)
2505 break;
2507 select_commit(s);
2508 break;
2509 case 'G':
2510 case KEY_END: {
2511 /* We don't know yet how many commits, so we're forced to
2512 * traverse them all. */
2513 if (!s->thread_args.log_complete) {
2514 s->thread_args.load_all = 1;
2515 return trigger_log_thread(view, 0);
2518 s->selected = 0;
2519 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2520 for (n = 0; n < view->nlines - 1; n++) {
2521 if (entry == NULL)
2522 break;
2523 s->first_displayed_entry = entry;
2524 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2526 if (n > 0)
2527 s->selected = n - 1;
2528 select_commit(s);
2529 break;
2531 case KEY_NPAGE:
2532 case CTRL('f'): {
2533 struct commit_queue_entry *first;
2534 first = s->first_displayed_entry;
2535 if (first == NULL)
2536 break;
2537 err = log_scroll_down(view, view->nlines - 1);
2538 if (err)
2539 break;
2540 if (first == s->first_displayed_entry &&
2541 s->selected < MIN(view->nlines - 2,
2542 s->commits.ncommits - 1)) {
2543 /* can't scroll further down */
2544 s->selected = MIN(view->nlines - 2,
2545 s->commits.ncommits - 1);
2547 select_commit(s);
2548 break;
2550 case KEY_RESIZE:
2551 if (s->selected > view->nlines - 2)
2552 s->selected = view->nlines - 2;
2553 if (s->selected > s->commits.ncommits - 1)
2554 s->selected = s->commits.ncommits - 1;
2555 select_commit(s);
2556 if (s->commits.ncommits < view->nlines - 1 &&
2557 !s->thread_args.log_complete) {
2558 s->thread_args.commits_needed += (view->nlines - 1) -
2559 s->commits.ncommits;
2560 err = trigger_log_thread(view, 1);
2562 break;
2563 case KEY_ENTER:
2564 case ' ':
2565 case '\r':
2566 if (s->selected_entry == NULL)
2567 break;
2568 if (view_is_parent_view(view))
2569 begin_x = view_split_begin_x(view->begin_x);
2570 err = open_diff_view_for_commit(&diff_view, begin_x,
2571 s->selected_entry->commit, s->selected_entry->id,
2572 view, s->repo);
2573 if (err)
2574 break;
2575 view->focussed = 0;
2576 diff_view->focussed = 1;
2577 if (view_is_parent_view(view)) {
2578 err = view_close_child(view);
2579 if (err)
2580 return err;
2581 view_set_child(view, diff_view);
2582 view->focus_child = 1;
2583 } else
2584 *new_view = diff_view;
2585 break;
2586 case 't':
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 = browse_commit_tree(&tree_view, begin_x,
2592 s->selected_entry, s->in_repo_path, s->head_ref_name,
2593 s->repo);
2594 if (err)
2595 break;
2596 view->focussed = 0;
2597 tree_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, tree_view);
2603 view->focus_child = 1;
2604 } else
2605 *new_view = tree_view;
2606 break;
2607 case KEY_BACKSPACE:
2608 case CTRL('l'):
2609 case 'B':
2610 if (ch == KEY_BACKSPACE &&
2611 got_path_is_root_dir(s->in_repo_path))
2612 break;
2613 err = stop_log_thread(s);
2614 if (err)
2615 return err;
2616 if (ch == KEY_BACKSPACE) {
2617 char *parent_path;
2618 err = got_path_dirname(&parent_path, s->in_repo_path);
2619 if (err)
2620 return err;
2621 free(s->in_repo_path);
2622 s->in_repo_path = parent_path;
2623 s->thread_args.in_repo_path = s->in_repo_path;
2624 } else if (ch == CTRL('l')) {
2625 struct got_object_id *start_id;
2626 err = got_repo_match_object_id(&start_id, NULL,
2627 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2628 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2629 if (err)
2630 return err;
2631 free(s->start_id);
2632 s->start_id = start_id;
2633 s->thread_args.start_id = s->start_id;
2634 } else /* 'B' */
2635 s->log_branches = !s->log_branches;
2637 err = got_repo_open(&s->thread_args.repo,
2638 got_repo_get_path(s->repo), NULL);
2639 if (err)
2640 return err;
2641 tog_free_refs();
2642 err = tog_load_refs(s->repo, 0);
2643 if (err)
2644 return err;
2645 err = got_commit_graph_open(&s->thread_args.graph,
2646 s->in_repo_path, !s->log_branches);
2647 if (err)
2648 return err;
2649 err = got_commit_graph_iter_start(s->thread_args.graph,
2650 s->start_id, s->repo, NULL, NULL);
2651 if (err)
2652 return err;
2653 free_commits(&s->commits);
2654 s->first_displayed_entry = NULL;
2655 s->last_displayed_entry = NULL;
2656 s->selected_entry = NULL;
2657 s->selected = 0;
2658 s->thread_args.log_complete = 0;
2659 s->quit = 0;
2660 s->thread_args.commits_needed = view->nlines;
2661 break;
2662 case 'r':
2663 if (view_is_parent_view(view))
2664 begin_x = view_split_begin_x(view->begin_x);
2665 ref_view = view_open(view->nlines, view->ncols,
2666 view->begin_y, begin_x, TOG_VIEW_REF);
2667 if (ref_view == NULL)
2668 return got_error_from_errno("view_open");
2669 err = open_ref_view(ref_view, s->repo);
2670 if (err) {
2671 view_close(ref_view);
2672 return err;
2674 view->focussed = 0;
2675 ref_view->focussed = 1;
2676 if (view_is_parent_view(view)) {
2677 err = view_close_child(view);
2678 if (err)
2679 return err;
2680 view_set_child(view, ref_view);
2681 view->focus_child = 1;
2682 } else
2683 *new_view = ref_view;
2684 break;
2685 default:
2686 break;
2689 return err;
2692 static const struct got_error *
2693 apply_unveil(const char *repo_path, const char *worktree_path)
2695 const struct got_error *error;
2697 #ifdef PROFILE
2698 if (unveil("gmon.out", "rwc") != 0)
2699 return got_error_from_errno2("unveil", "gmon.out");
2700 #endif
2701 if (repo_path && unveil(repo_path, "r") != 0)
2702 return got_error_from_errno2("unveil", repo_path);
2704 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2705 return got_error_from_errno2("unveil", worktree_path);
2707 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2708 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2710 error = got_privsep_unveil_exec_helpers();
2711 if (error != NULL)
2712 return error;
2714 if (unveil(NULL, NULL) != 0)
2715 return got_error_from_errno("unveil");
2717 return NULL;
2720 static void
2721 init_curses(void)
2724 * Override default signal handlers before starting ncurses.
2725 * This should prevent ncurses from installing its own
2726 * broken cleanup() signal handler.
2728 signal(SIGWINCH, tog_sigwinch);
2729 signal(SIGPIPE, tog_sigpipe);
2730 signal(SIGCONT, tog_sigcont);
2731 signal(SIGINT, tog_sigint);
2732 signal(SIGTERM, tog_sigterm);
2734 initscr();
2735 cbreak();
2736 halfdelay(1); /* Do fast refresh while initial view is loading. */
2737 noecho();
2738 nonl();
2739 intrflush(stdscr, FALSE);
2740 keypad(stdscr, TRUE);
2741 curs_set(0);
2742 if (getenv("TOG_COLORS") != NULL) {
2743 start_color();
2744 use_default_colors();
2748 static const struct got_error *
2749 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2750 struct got_repository *repo, struct got_worktree *worktree)
2752 const struct got_error *err = NULL;
2754 if (argc == 0) {
2755 *in_repo_path = strdup("/");
2756 if (*in_repo_path == NULL)
2757 return got_error_from_errno("strdup");
2758 return NULL;
2761 if (worktree) {
2762 const char *prefix = got_worktree_get_path_prefix(worktree);
2763 char *p;
2765 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2766 if (err)
2767 return err;
2768 if (asprintf(in_repo_path, "%s%s%s", prefix,
2769 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2770 p) == -1) {
2771 err = got_error_from_errno("asprintf");
2772 *in_repo_path = NULL;
2774 free(p);
2775 } else
2776 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2778 return err;
2781 static const struct got_error *
2782 cmd_log(int argc, char *argv[])
2784 const struct got_error *error;
2785 struct got_repository *repo = NULL;
2786 struct got_worktree *worktree = NULL;
2787 struct got_object_id *start_id = NULL;
2788 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2789 char *start_commit = NULL, *label = NULL;
2790 struct got_reference *ref = NULL;
2791 const char *head_ref_name = NULL;
2792 int ch, log_branches = 0;
2793 struct tog_view *view;
2795 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2796 switch (ch) {
2797 case 'b':
2798 log_branches = 1;
2799 break;
2800 case 'c':
2801 start_commit = optarg;
2802 break;
2803 case 'r':
2804 repo_path = realpath(optarg, NULL);
2805 if (repo_path == NULL)
2806 return got_error_from_errno2("realpath",
2807 optarg);
2808 break;
2809 default:
2810 usage_log();
2811 /* NOTREACHED */
2815 argc -= optind;
2816 argv += optind;
2818 if (argc > 1)
2819 usage_log();
2821 if (repo_path == NULL) {
2822 cwd = getcwd(NULL, 0);
2823 if (cwd == NULL)
2824 return got_error_from_errno("getcwd");
2825 error = got_worktree_open(&worktree, cwd);
2826 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2827 goto done;
2828 if (worktree)
2829 repo_path =
2830 strdup(got_worktree_get_repo_path(worktree));
2831 else
2832 repo_path = strdup(cwd);
2833 if (repo_path == NULL) {
2834 error = got_error_from_errno("strdup");
2835 goto done;
2839 error = got_repo_open(&repo, repo_path, NULL);
2840 if (error != NULL)
2841 goto done;
2843 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2844 repo, worktree);
2845 if (error)
2846 goto done;
2848 init_curses();
2850 error = apply_unveil(got_repo_get_path(repo),
2851 worktree ? got_worktree_get_root_path(worktree) : NULL);
2852 if (error)
2853 goto done;
2855 /* already loaded by tog_log_with_path()? */
2856 if (TAILQ_EMPTY(&tog_refs)) {
2857 error = tog_load_refs(repo, 0);
2858 if (error)
2859 goto done;
2862 if (start_commit == NULL) {
2863 error = got_repo_match_object_id(&start_id, &label,
2864 worktree ? got_worktree_get_head_ref_name(worktree) :
2865 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2866 if (error)
2867 goto done;
2868 head_ref_name = label;
2869 } else {
2870 error = got_ref_open(&ref, repo, start_commit, 0);
2871 if (error == NULL)
2872 head_ref_name = got_ref_get_name(ref);
2873 else if (error->code != GOT_ERR_NOT_REF)
2874 goto done;
2875 error = got_repo_match_object_id(&start_id, NULL,
2876 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2877 if (error)
2878 goto done;
2881 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2882 if (view == NULL) {
2883 error = got_error_from_errno("view_open");
2884 goto done;
2886 error = open_log_view(view, start_id, repo, head_ref_name,
2887 in_repo_path, log_branches);
2888 if (error)
2889 goto done;
2890 if (worktree) {
2891 /* Release work tree lock. */
2892 got_worktree_close(worktree);
2893 worktree = NULL;
2895 error = view_loop(view);
2896 done:
2897 free(in_repo_path);
2898 free(repo_path);
2899 free(cwd);
2900 free(start_id);
2901 free(label);
2902 if (ref)
2903 got_ref_close(ref);
2904 if (repo) {
2905 const struct got_error *close_err = got_repo_close(repo);
2906 if (error == NULL)
2907 error = close_err;
2909 if (worktree)
2910 got_worktree_close(worktree);
2911 tog_free_refs();
2912 return error;
2915 __dead static void
2916 usage_diff(void)
2918 endwin();
2919 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2920 "[-w] object1 object2\n", getprogname());
2921 exit(1);
2924 static int
2925 match_line(const char *line, regex_t *regex, size_t nmatch,
2926 regmatch_t *regmatch)
2928 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2931 struct tog_color *
2932 match_color(struct tog_colors *colors, const char *line)
2934 struct tog_color *tc = NULL;
2936 STAILQ_FOREACH(tc, colors, entry) {
2937 if (match_line(line, &tc->regex, 0, NULL))
2938 return tc;
2941 return NULL;
2944 static const struct got_error *
2945 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2946 WINDOW *window, regmatch_t *regmatch)
2948 const struct got_error *err = NULL;
2949 wchar_t *wline;
2950 int width;
2951 char *s;
2953 *wtotal = 0;
2955 s = strndup(line, regmatch->rm_so);
2956 if (s == NULL)
2957 return got_error_from_errno("strndup");
2959 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2960 if (err) {
2961 free(s);
2962 return err;
2964 waddwstr(window, wline);
2965 free(wline);
2966 free(s);
2967 wlimit -= width;
2968 *wtotal += width;
2970 if (wlimit > 0) {
2971 s = strndup(line + regmatch->rm_so,
2972 regmatch->rm_eo - regmatch->rm_so);
2973 if (s == NULL) {
2974 err = got_error_from_errno("strndup");
2975 free(s);
2976 return err;
2978 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2979 if (err) {
2980 free(s);
2981 return err;
2983 wattr_on(window, A_STANDOUT, NULL);
2984 waddwstr(window, wline);
2985 wattr_off(window, A_STANDOUT, NULL);
2986 free(wline);
2987 free(s);
2988 wlimit -= width;
2989 *wtotal += width;
2992 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2993 err = format_line(&wline, &width,
2994 line + regmatch->rm_eo, wlimit, col_tab_align);
2995 if (err)
2996 return err;
2997 waddwstr(window, wline);
2998 free(wline);
2999 *wtotal += width;
3002 return NULL;
3005 static const struct got_error *
3006 draw_file(struct tog_view *view, const char *header)
3008 struct tog_diff_view_state *s = &view->state.diff;
3009 regmatch_t *regmatch = &view->regmatch;
3010 const struct got_error *err;
3011 int nprinted = 0;
3012 char *line;
3013 size_t linesize = 0;
3014 ssize_t linelen;
3015 struct tog_color *tc;
3016 wchar_t *wline;
3017 int width;
3018 int max_lines = view->nlines;
3019 int nlines = s->nlines;
3020 off_t line_offset;
3022 line_offset = s->line_offsets[s->first_displayed_line - 1];
3023 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3024 return got_error_from_errno("fseek");
3026 werase(view->window);
3028 if (header) {
3029 if (asprintf(&line, "[%d/%d] %s",
3030 s->first_displayed_line - 1 + s->selected_line, nlines,
3031 header) == -1)
3032 return got_error_from_errno("asprintf");
3033 err = format_line(&wline, &width, line, view->ncols, 0);
3034 free(line);
3035 if (err)
3036 return err;
3038 if (view_needs_focus_indication(view))
3039 wstandout(view->window);
3040 waddwstr(view->window, wline);
3041 free(wline);
3042 wline = NULL;
3043 if (view_needs_focus_indication(view))
3044 wstandend(view->window);
3045 if (width <= view->ncols - 1)
3046 waddch(view->window, '\n');
3048 if (max_lines <= 1)
3049 return NULL;
3050 max_lines--;
3053 s->eof = 0;
3054 line = NULL;
3055 while (max_lines > 0 && nprinted < max_lines) {
3056 linelen = getline(&line, &linesize, s->f);
3057 if (linelen == -1) {
3058 if (feof(s->f)) {
3059 s->eof = 1;
3060 break;
3062 free(line);
3063 return got_ferror(s->f, GOT_ERR_IO);
3066 tc = match_color(&s->colors, line);
3067 if (tc)
3068 wattr_on(view->window,
3069 COLOR_PAIR(tc->colorpair), NULL);
3070 if (s->first_displayed_line + nprinted == s->matched_line &&
3071 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3072 err = add_matched_line(&width, line, view->ncols, 0,
3073 view->window, regmatch);
3074 if (err) {
3075 free(line);
3076 return err;
3078 } else {
3079 err = format_line(&wline, &width, line, view->ncols, 0);
3080 if (err) {
3081 free(line);
3082 return err;
3084 waddwstr(view->window, wline);
3085 free(wline);
3086 wline = NULL;
3088 if (tc)
3089 wattr_off(view->window,
3090 COLOR_PAIR(tc->colorpair), NULL);
3091 if (width <= view->ncols - 1)
3092 waddch(view->window, '\n');
3093 nprinted++;
3095 free(line);
3096 if (nprinted >= 1)
3097 s->last_displayed_line = s->first_displayed_line +
3098 (nprinted - 1);
3099 else
3100 s->last_displayed_line = s->first_displayed_line;
3102 view_vborder(view);
3104 if (s->eof) {
3105 while (nprinted < view->nlines) {
3106 waddch(view->window, '\n');
3107 nprinted++;
3110 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3111 if (err) {
3112 return err;
3115 wstandout(view->window);
3116 waddwstr(view->window, wline);
3117 free(wline);
3118 wline = NULL;
3119 wstandend(view->window);
3122 return NULL;
3125 static char *
3126 get_datestr(time_t *time, char *datebuf)
3128 struct tm mytm, *tm;
3129 char *p, *s;
3131 tm = gmtime_r(time, &mytm);
3132 if (tm == NULL)
3133 return NULL;
3134 s = asctime_r(tm, datebuf);
3135 if (s == NULL)
3136 return NULL;
3137 p = strchr(s, '\n');
3138 if (p)
3139 *p = '\0';
3140 return s;
3143 static const struct got_error *
3144 get_changed_paths(struct got_pathlist_head *paths,
3145 struct got_commit_object *commit, struct got_repository *repo)
3147 const struct got_error *err = NULL;
3148 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3149 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3150 struct got_object_qid *qid;
3152 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3153 if (qid != NULL) {
3154 struct got_commit_object *pcommit;
3155 err = got_object_open_as_commit(&pcommit, repo,
3156 &qid->id);
3157 if (err)
3158 return err;
3160 tree_id1 = got_object_id_dup(
3161 got_object_commit_get_tree_id(pcommit));
3162 if (tree_id1 == NULL) {
3163 got_object_commit_close(pcommit);
3164 return got_error_from_errno("got_object_id_dup");
3166 got_object_commit_close(pcommit);
3170 if (tree_id1) {
3171 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3172 if (err)
3173 goto done;
3176 tree_id2 = got_object_commit_get_tree_id(commit);
3177 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3178 if (err)
3179 goto done;
3181 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3182 got_diff_tree_collect_changed_paths, paths, 0);
3183 done:
3184 if (tree1)
3185 got_object_tree_close(tree1);
3186 if (tree2)
3187 got_object_tree_close(tree2);
3188 free(tree_id1);
3189 return err;
3192 static const struct got_error *
3193 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3195 off_t *p;
3197 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3198 if (p == NULL)
3199 return got_error_from_errno("reallocarray");
3200 *line_offsets = p;
3201 (*line_offsets)[*nlines] = off;
3202 (*nlines)++;
3203 return NULL;
3206 static const struct got_error *
3207 write_commit_info(off_t **line_offsets, size_t *nlines,
3208 struct got_object_id *commit_id, struct got_reflist_head *refs,
3209 struct got_repository *repo, FILE *outfile)
3211 const struct got_error *err = NULL;
3212 char datebuf[26], *datestr;
3213 struct got_commit_object *commit;
3214 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3215 time_t committer_time;
3216 const char *author, *committer;
3217 char *refs_str = NULL;
3218 struct got_pathlist_head changed_paths;
3219 struct got_pathlist_entry *pe;
3220 off_t outoff = 0;
3221 int n;
3223 TAILQ_INIT(&changed_paths);
3225 if (refs) {
3226 err = build_refs_str(&refs_str, refs, commit_id, repo);
3227 if (err)
3228 return err;
3231 err = got_object_open_as_commit(&commit, repo, commit_id);
3232 if (err)
3233 return err;
3235 err = got_object_id_str(&id_str, commit_id);
3236 if (err) {
3237 err = got_error_from_errno("got_object_id_str");
3238 goto done;
3241 err = add_line_offset(line_offsets, nlines, 0);
3242 if (err)
3243 goto done;
3245 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3246 refs_str ? refs_str : "", refs_str ? ")" : "");
3247 if (n < 0) {
3248 err = got_error_from_errno("fprintf");
3249 goto done;
3251 outoff += n;
3252 err = add_line_offset(line_offsets, nlines, outoff);
3253 if (err)
3254 goto done;
3256 n = fprintf(outfile, "from: %s\n",
3257 got_object_commit_get_author(commit));
3258 if (n < 0) {
3259 err = got_error_from_errno("fprintf");
3260 goto done;
3262 outoff += n;
3263 err = add_line_offset(line_offsets, nlines, outoff);
3264 if (err)
3265 goto done;
3267 committer_time = got_object_commit_get_committer_time(commit);
3268 datestr = get_datestr(&committer_time, datebuf);
3269 if (datestr) {
3270 n = fprintf(outfile, "date: %s UTC\n", datestr);
3271 if (n < 0) {
3272 err = got_error_from_errno("fprintf");
3273 goto done;
3275 outoff += n;
3276 err = add_line_offset(line_offsets, nlines, outoff);
3277 if (err)
3278 goto done;
3280 author = got_object_commit_get_author(commit);
3281 committer = got_object_commit_get_committer(commit);
3282 if (strcmp(author, committer) != 0) {
3283 n = fprintf(outfile, "via: %s\n", committer);
3284 if (n < 0) {
3285 err = got_error_from_errno("fprintf");
3286 goto done;
3288 outoff += n;
3289 err = add_line_offset(line_offsets, nlines, outoff);
3290 if (err)
3291 goto done;
3293 if (got_object_commit_get_nparents(commit) > 1) {
3294 const struct got_object_id_queue *parent_ids;
3295 struct got_object_qid *qid;
3296 int pn = 1;
3297 parent_ids = got_object_commit_get_parent_ids(commit);
3298 STAILQ_FOREACH(qid, parent_ids, entry) {
3299 err = got_object_id_str(&id_str, &qid->id);
3300 if (err)
3301 goto done;
3302 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3303 if (n < 0) {
3304 err = got_error_from_errno("fprintf");
3305 goto done;
3307 outoff += n;
3308 err = add_line_offset(line_offsets, nlines, outoff);
3309 if (err)
3310 goto done;
3311 free(id_str);
3312 id_str = NULL;
3316 err = got_object_commit_get_logmsg(&logmsg, commit);
3317 if (err)
3318 goto done;
3319 s = logmsg;
3320 while ((line = strsep(&s, "\n")) != NULL) {
3321 n = fprintf(outfile, "%s\n", line);
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;
3332 err = get_changed_paths(&changed_paths, commit, repo);
3333 if (err)
3334 goto done;
3335 TAILQ_FOREACH(pe, &changed_paths, entry) {
3336 struct got_diff_changed_path *cp = pe->data;
3337 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3338 if (n < 0) {
3339 err = got_error_from_errno("fprintf");
3340 goto done;
3342 outoff += n;
3343 err = add_line_offset(line_offsets, nlines, outoff);
3344 if (err)
3345 goto done;
3346 free((char *)pe->path);
3347 free(pe->data);
3350 fputc('\n', outfile);
3351 outoff++;
3352 err = add_line_offset(line_offsets, nlines, outoff);
3353 done:
3354 got_pathlist_free(&changed_paths);
3355 free(id_str);
3356 free(logmsg);
3357 free(refs_str);
3358 got_object_commit_close(commit);
3359 if (err) {
3360 free(*line_offsets);
3361 *line_offsets = NULL;
3362 *nlines = 0;
3364 return err;
3367 static const struct got_error *
3368 create_diff(struct tog_diff_view_state *s)
3370 const struct got_error *err = NULL;
3371 FILE *f = NULL;
3372 int obj_type;
3374 free(s->line_offsets);
3375 s->line_offsets = malloc(sizeof(off_t));
3376 if (s->line_offsets == NULL)
3377 return got_error_from_errno("malloc");
3378 s->nlines = 0;
3380 f = got_opentemp();
3381 if (f == NULL) {
3382 err = got_error_from_errno("got_opentemp");
3383 goto done;
3385 if (s->f && fclose(s->f) == EOF) {
3386 err = got_error_from_errno("fclose");
3387 goto done;
3389 s->f = f;
3391 if (s->id1)
3392 err = got_object_get_type(&obj_type, s->repo, s->id1);
3393 else
3394 err = got_object_get_type(&obj_type, s->repo, s->id2);
3395 if (err)
3396 goto done;
3398 switch (obj_type) {
3399 case GOT_OBJ_TYPE_BLOB:
3400 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3401 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3402 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3403 s->repo, s->f);
3404 break;
3405 case GOT_OBJ_TYPE_TREE:
3406 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3407 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3408 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3409 break;
3410 case GOT_OBJ_TYPE_COMMIT: {
3411 const struct got_object_id_queue *parent_ids;
3412 struct got_object_qid *pid;
3413 struct got_commit_object *commit2;
3414 struct got_reflist_head *refs;
3416 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3417 if (err)
3418 goto done;
3419 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3420 /* Show commit info if we're diffing to a parent/root commit. */
3421 if (s->id1 == NULL) {
3422 err = write_commit_info(&s->line_offsets, &s->nlines,
3423 s->id2, refs, s->repo, s->f);
3424 if (err)
3425 goto done;
3426 } else {
3427 parent_ids = got_object_commit_get_parent_ids(commit2);
3428 STAILQ_FOREACH(pid, parent_ids, entry) {
3429 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3430 err = write_commit_info(
3431 &s->line_offsets, &s->nlines,
3432 s->id2, refs, s->repo, s->f);
3433 if (err)
3434 goto done;
3435 break;
3439 got_object_commit_close(commit2);
3441 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3442 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3443 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3444 break;
3446 default:
3447 err = got_error(GOT_ERR_OBJ_TYPE);
3448 break;
3450 if (err)
3451 goto done;
3452 done:
3453 if (s->f && fflush(s->f) != 0 && err == NULL)
3454 err = got_error_from_errno("fflush");
3455 return err;
3458 static void
3459 diff_view_indicate_progress(struct tog_view *view)
3461 mvwaddstr(view->window, 0, 0, "diffing...");
3462 update_panels();
3463 doupdate();
3466 static const struct got_error *
3467 search_start_diff_view(struct tog_view *view)
3469 struct tog_diff_view_state *s = &view->state.diff;
3471 s->matched_line = 0;
3472 return NULL;
3475 static const struct got_error *
3476 search_next_diff_view(struct tog_view *view)
3478 struct tog_diff_view_state *s = &view->state.diff;
3479 int lineno;
3480 char *line = NULL;
3481 size_t linesize = 0;
3482 ssize_t linelen;
3484 if (!view->searching) {
3485 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3486 return NULL;
3489 if (s->matched_line) {
3490 if (view->searching == TOG_SEARCH_FORWARD)
3491 lineno = s->matched_line + 1;
3492 else
3493 lineno = s->matched_line - 1;
3494 } else
3495 lineno = s->first_displayed_line;
3497 while (1) {
3498 off_t offset;
3500 if (lineno <= 0 || lineno > s->nlines) {
3501 if (s->matched_line == 0) {
3502 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3503 break;
3506 if (view->searching == TOG_SEARCH_FORWARD)
3507 lineno = 1;
3508 else
3509 lineno = s->nlines;
3512 offset = s->line_offsets[lineno - 1];
3513 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3514 free(line);
3515 return got_error_from_errno("fseeko");
3517 linelen = getline(&line, &linesize, s->f);
3518 if (linelen != -1 &&
3519 match_line(line, &view->regex, 1, &view->regmatch)) {
3520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3521 s->matched_line = lineno;
3522 break;
3524 if (view->searching == TOG_SEARCH_FORWARD)
3525 lineno++;
3526 else
3527 lineno--;
3529 free(line);
3531 if (s->matched_line) {
3532 s->first_displayed_line = s->matched_line;
3533 s->selected_line = 1;
3536 return NULL;
3539 static const struct got_error *
3540 close_diff_view(struct tog_view *view)
3542 const struct got_error *err = NULL;
3543 struct tog_diff_view_state *s = &view->state.diff;
3545 free(s->id1);
3546 s->id1 = NULL;
3547 free(s->id2);
3548 s->id2 = NULL;
3549 if (s->f && fclose(s->f) == EOF)
3550 err = got_error_from_errno("fclose");
3551 s->f = NULL;
3552 if (s->f1 && fclose(s->f1) == EOF)
3553 err = got_error_from_errno("fclose");
3554 s->f1 = NULL;
3555 if (s->f2 && fclose(s->f2) == EOF)
3556 err = got_error_from_errno("fclose");
3557 s->f2 = NULL;
3558 free_colors(&s->colors);
3559 free(s->line_offsets);
3560 s->line_offsets = NULL;
3561 s->nlines = 0;
3562 return err;
3565 static const struct got_error *
3566 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3567 struct got_object_id *id2, const char *label1, const char *label2,
3568 int diff_context, int ignore_whitespace, int force_text_diff,
3569 struct tog_view *log_view, struct got_repository *repo)
3571 const struct got_error *err;
3572 struct tog_diff_view_state *s = &view->state.diff;
3574 memset(s, 0, sizeof(*s));
3576 if (id1 != NULL && id2 != NULL) {
3577 int type1, type2;
3578 err = got_object_get_type(&type1, repo, id1);
3579 if (err)
3580 return err;
3581 err = got_object_get_type(&type2, repo, id2);
3582 if (err)
3583 return err;
3585 if (type1 != type2)
3586 return got_error(GOT_ERR_OBJ_TYPE);
3588 s->first_displayed_line = 1;
3589 s->last_displayed_line = view->nlines;
3590 s->selected_line = 1;
3591 s->repo = repo;
3592 s->id1 = id1;
3593 s->id2 = id2;
3594 s->label1 = label1;
3595 s->label2 = label2;
3597 if (id1) {
3598 s->id1 = got_object_id_dup(id1);
3599 if (s->id1 == NULL)
3600 return got_error_from_errno("got_object_id_dup");
3601 s->f1 = got_opentemp();
3602 if (s->f1 == NULL) {
3603 err = got_error_from_errno("got_opentemp");
3604 goto done;
3606 } else
3607 s->id1 = NULL;
3609 s->id2 = got_object_id_dup(id2);
3610 if (s->id2 == NULL) {
3611 err = got_error_from_errno("got_object_id_dup");
3612 goto done;
3615 s->f2 = got_opentemp();
3616 if (s->f2 == NULL) {
3617 err = got_error_from_errno("got_opentemp");
3618 goto done;
3621 s->first_displayed_line = 1;
3622 s->last_displayed_line = view->nlines;
3623 s->diff_context = diff_context;
3624 s->ignore_whitespace = ignore_whitespace;
3625 s->force_text_diff = force_text_diff;
3626 s->log_view = log_view;
3627 s->repo = repo;
3629 STAILQ_INIT(&s->colors);
3630 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3631 err = add_color(&s->colors,
3632 "^-", TOG_COLOR_DIFF_MINUS,
3633 get_color_value("TOG_COLOR_DIFF_MINUS"));
3634 if (err)
3635 goto done;
3636 err = add_color(&s->colors, "^\\+",
3637 TOG_COLOR_DIFF_PLUS,
3638 get_color_value("TOG_COLOR_DIFF_PLUS"));
3639 if (err)
3640 goto done;
3641 err = add_color(&s->colors,
3642 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3643 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3644 if (err)
3645 goto done;
3647 err = add_color(&s->colors,
3648 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3649 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3650 get_color_value("TOG_COLOR_DIFF_META"));
3651 if (err)
3652 goto done;
3654 err = add_color(&s->colors,
3655 "^(from|via): ", TOG_COLOR_AUTHOR,
3656 get_color_value("TOG_COLOR_AUTHOR"));
3657 if (err)
3658 goto done;
3660 err = add_color(&s->colors,
3661 "^date: ", TOG_COLOR_DATE,
3662 get_color_value("TOG_COLOR_DATE"));
3663 if (err)
3664 goto done;
3667 if (log_view && view_is_splitscreen(view))
3668 show_log_view(log_view); /* draw vborder */
3669 diff_view_indicate_progress(view);
3671 err = create_diff(s);
3673 view->show = show_diff_view;
3674 view->input = input_diff_view;
3675 view->close = close_diff_view;
3676 view->search_start = search_start_diff_view;
3677 view->search_next = search_next_diff_view;
3678 done:
3679 if (err)
3680 close_diff_view(view);
3681 return err;
3684 static const struct got_error *
3685 show_diff_view(struct tog_view *view)
3687 const struct got_error *err;
3688 struct tog_diff_view_state *s = &view->state.diff;
3689 char *id_str1 = NULL, *id_str2, *header;
3690 const char *label1, *label2;
3692 if (s->id1) {
3693 err = got_object_id_str(&id_str1, s->id1);
3694 if (err)
3695 return err;
3696 label1 = s->label1 ? : id_str1;
3697 } else
3698 label1 = "/dev/null";
3700 err = got_object_id_str(&id_str2, s->id2);
3701 if (err)
3702 return err;
3703 label2 = s->label2 ? : id_str2;
3705 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3706 err = got_error_from_errno("asprintf");
3707 free(id_str1);
3708 free(id_str2);
3709 return err;
3711 free(id_str1);
3712 free(id_str2);
3714 err = draw_file(view, header);
3715 free(header);
3716 return err;
3719 static const struct got_error *
3720 set_selected_commit(struct tog_diff_view_state *s,
3721 struct commit_queue_entry *entry)
3723 const struct got_error *err;
3724 const struct got_object_id_queue *parent_ids;
3725 struct got_commit_object *selected_commit;
3726 struct got_object_qid *pid;
3728 free(s->id2);
3729 s->id2 = got_object_id_dup(entry->id);
3730 if (s->id2 == NULL)
3731 return got_error_from_errno("got_object_id_dup");
3733 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3734 if (err)
3735 return err;
3736 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3737 free(s->id1);
3738 pid = STAILQ_FIRST(parent_ids);
3739 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3740 got_object_commit_close(selected_commit);
3741 return NULL;
3744 static const struct got_error *
3745 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3747 const struct got_error *err = NULL;
3748 struct tog_diff_view_state *s = &view->state.diff;
3749 struct tog_log_view_state *ls;
3750 struct commit_queue_entry *old_selected_entry;
3751 char *line = NULL;
3752 size_t linesize = 0;
3753 ssize_t linelen;
3754 int i;
3756 switch (ch) {
3757 case 'a':
3758 case 'w':
3759 if (ch == 'a')
3760 s->force_text_diff = !s->force_text_diff;
3761 if (ch == 'w')
3762 s->ignore_whitespace = !s->ignore_whitespace;
3763 wclear(view->window);
3764 s->first_displayed_line = 1;
3765 s->last_displayed_line = view->nlines;
3766 s->matched_line = 0;
3767 diff_view_indicate_progress(view);
3768 err = create_diff(s);
3769 break;
3770 case 'g':
3771 case KEY_HOME:
3772 s->first_displayed_line = 1;
3773 break;
3774 case 'G':
3775 case KEY_END:
3776 if (s->eof)
3777 break;
3779 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3780 s->eof = 1;
3781 break;
3782 case 'k':
3783 case KEY_UP:
3784 case CTRL('p'):
3785 if (s->first_displayed_line > 1)
3786 s->first_displayed_line--;
3787 break;
3788 case KEY_PPAGE:
3789 case CTRL('b'):
3790 if (s->first_displayed_line == 1)
3791 break;
3792 i = 0;
3793 while (i++ < view->nlines - 1 &&
3794 s->first_displayed_line > 1)
3795 s->first_displayed_line--;
3796 break;
3797 case 'j':
3798 case KEY_DOWN:
3799 case CTRL('n'):
3800 if (!s->eof)
3801 s->first_displayed_line++;
3802 break;
3803 case KEY_NPAGE:
3804 case CTRL('f'):
3805 case ' ':
3806 if (s->eof)
3807 break;
3808 i = 0;
3809 while (!s->eof && i++ < view->nlines - 1) {
3810 linelen = getline(&line, &linesize, s->f);
3811 s->first_displayed_line++;
3812 if (linelen == -1) {
3813 if (feof(s->f)) {
3814 s->eof = 1;
3815 } else
3816 err = got_ferror(s->f, GOT_ERR_IO);
3817 break;
3820 free(line);
3821 break;
3822 case '[':
3823 if (s->diff_context > 0) {
3824 s->diff_context--;
3825 s->matched_line = 0;
3826 diff_view_indicate_progress(view);
3827 err = create_diff(s);
3828 if (s->first_displayed_line + view->nlines - 1 >
3829 s->nlines) {
3830 s->first_displayed_line = 1;
3831 s->last_displayed_line = view->nlines;
3834 break;
3835 case ']':
3836 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3837 s->diff_context++;
3838 s->matched_line = 0;
3839 diff_view_indicate_progress(view);
3840 err = create_diff(s);
3842 break;
3843 case '<':
3844 case ',':
3845 if (s->log_view == NULL)
3846 break;
3847 ls = &s->log_view->state.log;
3848 old_selected_entry = ls->selected_entry;
3850 err = input_log_view(NULL, s->log_view, KEY_UP);
3851 if (err)
3852 break;
3854 if (old_selected_entry == ls->selected_entry)
3855 break;
3857 err = set_selected_commit(s, ls->selected_entry);
3858 if (err)
3859 break;
3861 s->first_displayed_line = 1;
3862 s->last_displayed_line = view->nlines;
3863 s->matched_line = 0;
3865 diff_view_indicate_progress(view);
3866 err = create_diff(s);
3867 break;
3868 case '>':
3869 case '.':
3870 if (s->log_view == NULL)
3871 break;
3872 ls = &s->log_view->state.log;
3873 old_selected_entry = ls->selected_entry;
3875 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3876 if (err)
3877 break;
3879 if (old_selected_entry == ls->selected_entry)
3880 break;
3882 err = set_selected_commit(s, ls->selected_entry);
3883 if (err)
3884 break;
3886 s->first_displayed_line = 1;
3887 s->last_displayed_line = view->nlines;
3888 s->matched_line = 0;
3890 diff_view_indicate_progress(view);
3891 err = create_diff(s);
3892 break;
3893 default:
3894 break;
3897 return err;
3900 static const struct got_error *
3901 cmd_diff(int argc, char *argv[])
3903 const struct got_error *error = NULL;
3904 struct got_repository *repo = NULL;
3905 struct got_worktree *worktree = NULL;
3906 struct got_object_id *id1 = NULL, *id2 = NULL;
3907 char *repo_path = NULL, *cwd = NULL;
3908 char *id_str1 = NULL, *id_str2 = NULL;
3909 char *label1 = NULL, *label2 = NULL;
3910 int diff_context = 3, ignore_whitespace = 0;
3911 int ch, force_text_diff = 0;
3912 const char *errstr;
3913 struct tog_view *view;
3915 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3916 switch (ch) {
3917 case 'a':
3918 force_text_diff = 1;
3919 break;
3920 case 'C':
3921 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3922 &errstr);
3923 if (errstr != NULL)
3924 errx(1, "number of context lines is %s: %s",
3925 errstr, errstr);
3926 break;
3927 case 'r':
3928 repo_path = realpath(optarg, NULL);
3929 if (repo_path == NULL)
3930 return got_error_from_errno2("realpath",
3931 optarg);
3932 got_path_strip_trailing_slashes(repo_path);
3933 break;
3934 case 'w':
3935 ignore_whitespace = 1;
3936 break;
3937 default:
3938 usage_diff();
3939 /* NOTREACHED */
3943 argc -= optind;
3944 argv += optind;
3946 if (argc == 0) {
3947 usage_diff(); /* TODO show local worktree changes */
3948 } else if (argc == 2) {
3949 id_str1 = argv[0];
3950 id_str2 = argv[1];
3951 } else
3952 usage_diff();
3954 if (repo_path == NULL) {
3955 cwd = getcwd(NULL, 0);
3956 if (cwd == NULL)
3957 return got_error_from_errno("getcwd");
3958 error = got_worktree_open(&worktree, cwd);
3959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3960 goto done;
3961 if (worktree)
3962 repo_path =
3963 strdup(got_worktree_get_repo_path(worktree));
3964 else
3965 repo_path = strdup(cwd);
3966 if (repo_path == NULL) {
3967 error = got_error_from_errno("strdup");
3968 goto done;
3972 error = got_repo_open(&repo, repo_path, NULL);
3973 if (error)
3974 goto done;
3976 init_curses();
3978 error = apply_unveil(got_repo_get_path(repo), NULL);
3979 if (error)
3980 goto done;
3982 error = tog_load_refs(repo, 0);
3983 if (error)
3984 goto done;
3986 error = got_repo_match_object_id(&id1, &label1, id_str1,
3987 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3988 if (error)
3989 goto done;
3991 error = got_repo_match_object_id(&id2, &label2, id_str2,
3992 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3993 if (error)
3994 goto done;
3996 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3997 if (view == NULL) {
3998 error = got_error_from_errno("view_open");
3999 goto done;
4001 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4002 ignore_whitespace, force_text_diff, NULL, repo);
4003 if (error)
4004 goto done;
4005 error = view_loop(view);
4006 done:
4007 free(label1);
4008 free(label2);
4009 free(repo_path);
4010 free(cwd);
4011 if (repo) {
4012 const struct got_error *close_err = got_repo_close(repo);
4013 if (error == NULL)
4014 error = close_err;
4016 if (worktree)
4017 got_worktree_close(worktree);
4018 tog_free_refs();
4019 return error;
4022 __dead static void
4023 usage_blame(void)
4025 endwin();
4026 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4027 getprogname());
4028 exit(1);
4031 struct tog_blame_line {
4032 int annotated;
4033 struct got_object_id *id;
4036 static const struct got_error *
4037 draw_blame(struct tog_view *view)
4039 struct tog_blame_view_state *s = &view->state.blame;
4040 struct tog_blame *blame = &s->blame;
4041 regmatch_t *regmatch = &view->regmatch;
4042 const struct got_error *err;
4043 int lineno = 0, nprinted = 0;
4044 char *line = NULL;
4045 size_t linesize = 0;
4046 ssize_t linelen;
4047 wchar_t *wline;
4048 int width;
4049 struct tog_blame_line *blame_line;
4050 struct got_object_id *prev_id = NULL;
4051 char *id_str;
4052 struct tog_color *tc;
4054 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4055 if (err)
4056 return err;
4058 rewind(blame->f);
4059 werase(view->window);
4061 if (asprintf(&line, "commit %s", id_str) == -1) {
4062 err = got_error_from_errno("asprintf");
4063 free(id_str);
4064 return err;
4067 err = format_line(&wline, &width, line, view->ncols, 0);
4068 free(line);
4069 line = NULL;
4070 if (err)
4071 return err;
4072 if (view_needs_focus_indication(view))
4073 wstandout(view->window);
4074 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4075 if (tc)
4076 wattr_on(view->window,
4077 COLOR_PAIR(tc->colorpair), NULL);
4078 waddwstr(view->window, wline);
4079 if (tc)
4080 wattr_off(view->window,
4081 COLOR_PAIR(tc->colorpair), NULL);
4082 if (view_needs_focus_indication(view))
4083 wstandend(view->window);
4084 free(wline);
4085 wline = NULL;
4086 if (width < view->ncols - 1)
4087 waddch(view->window, '\n');
4089 if (asprintf(&line, "[%d/%d] %s%s",
4090 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4091 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4092 free(id_str);
4093 return got_error_from_errno("asprintf");
4095 free(id_str);
4096 err = format_line(&wline, &width, line, view->ncols, 0);
4097 free(line);
4098 line = NULL;
4099 if (err)
4100 return err;
4101 waddwstr(view->window, wline);
4102 free(wline);
4103 wline = NULL;
4104 if (width < view->ncols - 1)
4105 waddch(view->window, '\n');
4107 s->eof = 0;
4108 while (nprinted < view->nlines - 2) {
4109 linelen = getline(&line, &linesize, blame->f);
4110 if (linelen == -1) {
4111 if (feof(blame->f)) {
4112 s->eof = 1;
4113 break;
4115 free(line);
4116 return got_ferror(blame->f, GOT_ERR_IO);
4118 if (++lineno < s->first_displayed_line)
4119 continue;
4121 if (view->focussed && nprinted == s->selected_line - 1)
4122 wstandout(view->window);
4124 if (blame->nlines > 0) {
4125 blame_line = &blame->lines[lineno - 1];
4126 if (blame_line->annotated && prev_id &&
4127 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4128 !(view->focussed &&
4129 nprinted == s->selected_line - 1)) {
4130 waddstr(view->window, " ");
4131 } else if (blame_line->annotated) {
4132 char *id_str;
4133 err = got_object_id_str(&id_str, blame_line->id);
4134 if (err) {
4135 free(line);
4136 return err;
4138 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4139 if (tc)
4140 wattr_on(view->window,
4141 COLOR_PAIR(tc->colorpair), NULL);
4142 wprintw(view->window, "%.8s", id_str);
4143 if (tc)
4144 wattr_off(view->window,
4145 COLOR_PAIR(tc->colorpair), NULL);
4146 free(id_str);
4147 prev_id = blame_line->id;
4148 } else {
4149 waddstr(view->window, "........");
4150 prev_id = NULL;
4152 } else {
4153 waddstr(view->window, "........");
4154 prev_id = NULL;
4157 if (view->focussed && nprinted == s->selected_line - 1)
4158 wstandend(view->window);
4159 waddstr(view->window, " ");
4161 if (view->ncols <= 9) {
4162 width = 9;
4163 wline = wcsdup(L"");
4164 if (wline == NULL) {
4165 err = got_error_from_errno("wcsdup");
4166 free(line);
4167 return err;
4169 } else if (s->first_displayed_line + nprinted ==
4170 s->matched_line &&
4171 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4172 err = add_matched_line(&width, line, view->ncols - 9, 9,
4173 view->window, regmatch);
4174 if (err) {
4175 free(line);
4176 return err;
4178 width += 9;
4179 } else {
4180 err = format_line(&wline, &width, line,
4181 view->ncols - 9, 9);
4182 waddwstr(view->window, wline);
4183 free(wline);
4184 wline = NULL;
4185 width += 9;
4188 if (width <= view->ncols - 1)
4189 waddch(view->window, '\n');
4190 if (++nprinted == 1)
4191 s->first_displayed_line = lineno;
4193 free(line);
4194 s->last_displayed_line = lineno;
4196 view_vborder(view);
4198 return NULL;
4201 static const struct got_error *
4202 blame_cb(void *arg, int nlines, int lineno,
4203 struct got_commit_object *commit, struct got_object_id *id)
4205 const struct got_error *err = NULL;
4206 struct tog_blame_cb_args *a = arg;
4207 struct tog_blame_line *line;
4208 int errcode;
4210 if (nlines != a->nlines ||
4211 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4212 return got_error(GOT_ERR_RANGE);
4214 errcode = pthread_mutex_lock(&tog_mutex);
4215 if (errcode)
4216 return got_error_set_errno(errcode, "pthread_mutex_lock");
4218 if (*a->quit) { /* user has quit the blame view */
4219 err = got_error(GOT_ERR_ITER_COMPLETED);
4220 goto done;
4223 if (lineno == -1)
4224 goto done; /* no change in this commit */
4226 line = &a->lines[lineno - 1];
4227 if (line->annotated)
4228 goto done;
4230 line->id = got_object_id_dup(id);
4231 if (line->id == NULL) {
4232 err = got_error_from_errno("got_object_id_dup");
4233 goto done;
4235 line->annotated = 1;
4236 done:
4237 errcode = pthread_mutex_unlock(&tog_mutex);
4238 if (errcode)
4239 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4240 return err;
4243 static void *
4244 blame_thread(void *arg)
4246 const struct got_error *err, *close_err;
4247 struct tog_blame_thread_args *ta = arg;
4248 struct tog_blame_cb_args *a = ta->cb_args;
4249 int errcode;
4251 err = block_signals_used_by_main_thread();
4252 if (err)
4253 return (void *)err;
4255 err = got_blame(ta->path, a->commit_id, ta->repo,
4256 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4257 if (err && err->code == GOT_ERR_CANCELLED)
4258 err = NULL;
4260 errcode = pthread_mutex_lock(&tog_mutex);
4261 if (errcode)
4262 return (void *)got_error_set_errno(errcode,
4263 "pthread_mutex_lock");
4265 close_err = got_repo_close(ta->repo);
4266 if (err == NULL)
4267 err = close_err;
4268 ta->repo = NULL;
4269 *ta->complete = 1;
4271 errcode = pthread_mutex_unlock(&tog_mutex);
4272 if (errcode && err == NULL)
4273 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4275 return (void *)err;
4278 static struct got_object_id *
4279 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4280 int first_displayed_line, int selected_line)
4282 struct tog_blame_line *line;
4284 if (nlines <= 0)
4285 return NULL;
4287 line = &lines[first_displayed_line - 1 + selected_line - 1];
4288 if (!line->annotated)
4289 return NULL;
4291 return line->id;
4294 static const struct got_error *
4295 stop_blame(struct tog_blame *blame)
4297 const struct got_error *err = NULL;
4298 int i;
4300 if (blame->thread) {
4301 int errcode;
4302 errcode = pthread_mutex_unlock(&tog_mutex);
4303 if (errcode)
4304 return got_error_set_errno(errcode,
4305 "pthread_mutex_unlock");
4306 errcode = pthread_join(blame->thread, (void **)&err);
4307 if (errcode)
4308 return got_error_set_errno(errcode, "pthread_join");
4309 errcode = pthread_mutex_lock(&tog_mutex);
4310 if (errcode)
4311 return got_error_set_errno(errcode,
4312 "pthread_mutex_lock");
4313 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4314 err = NULL;
4315 blame->thread = NULL;
4317 if (blame->thread_args.repo) {
4318 const struct got_error *close_err;
4319 close_err = got_repo_close(blame->thread_args.repo);
4320 if (err == NULL)
4321 err = close_err;
4322 blame->thread_args.repo = NULL;
4324 if (blame->f) {
4325 if (fclose(blame->f) == EOF && err == NULL)
4326 err = got_error_from_errno("fclose");
4327 blame->f = NULL;
4329 if (blame->lines) {
4330 for (i = 0; i < blame->nlines; i++)
4331 free(blame->lines[i].id);
4332 free(blame->lines);
4333 blame->lines = NULL;
4335 free(blame->cb_args.commit_id);
4336 blame->cb_args.commit_id = NULL;
4338 return err;
4341 static const struct got_error *
4342 cancel_blame_view(void *arg)
4344 const struct got_error *err = NULL;
4345 int *done = arg;
4346 int errcode;
4348 errcode = pthread_mutex_lock(&tog_mutex);
4349 if (errcode)
4350 return got_error_set_errno(errcode,
4351 "pthread_mutex_unlock");
4353 if (*done)
4354 err = got_error(GOT_ERR_CANCELLED);
4356 errcode = pthread_mutex_unlock(&tog_mutex);
4357 if (errcode)
4358 return got_error_set_errno(errcode,
4359 "pthread_mutex_lock");
4361 return err;
4364 static const struct got_error *
4365 run_blame(struct tog_view *view)
4367 struct tog_blame_view_state *s = &view->state.blame;
4368 struct tog_blame *blame = &s->blame;
4369 const struct got_error *err = NULL;
4370 struct got_commit_object *commit = NULL;
4371 struct got_blob_object *blob = NULL;
4372 struct got_repository *thread_repo = NULL;
4373 struct got_object_id *obj_id = NULL;
4374 int obj_type;
4376 err = got_object_open_as_commit(&commit, s->repo,
4377 &s->blamed_commit->id);
4378 if (err)
4379 return err;
4381 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4382 if (err)
4383 goto done;
4385 err = got_object_get_type(&obj_type, s->repo, obj_id);
4386 if (err)
4387 goto done;
4389 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4390 err = got_error(GOT_ERR_OBJ_TYPE);
4391 goto done;
4394 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4395 if (err)
4396 goto done;
4397 blame->f = got_opentemp();
4398 if (blame->f == NULL) {
4399 err = got_error_from_errno("got_opentemp");
4400 goto done;
4402 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4403 &blame->line_offsets, blame->f, blob);
4404 if (err)
4405 goto done;
4406 if (blame->nlines == 0) {
4407 s->blame_complete = 1;
4408 goto done;
4411 /* Don't include \n at EOF in the blame line count. */
4412 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4413 blame->nlines--;
4415 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4416 if (blame->lines == NULL) {
4417 err = got_error_from_errno("calloc");
4418 goto done;
4421 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4422 if (err)
4423 goto done;
4425 blame->cb_args.view = view;
4426 blame->cb_args.lines = blame->lines;
4427 blame->cb_args.nlines = blame->nlines;
4428 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4429 if (blame->cb_args.commit_id == NULL) {
4430 err = got_error_from_errno("got_object_id_dup");
4431 goto done;
4433 blame->cb_args.quit = &s->done;
4435 blame->thread_args.path = s->path;
4436 blame->thread_args.repo = thread_repo;
4437 blame->thread_args.cb_args = &blame->cb_args;
4438 blame->thread_args.complete = &s->blame_complete;
4439 blame->thread_args.cancel_cb = cancel_blame_view;
4440 blame->thread_args.cancel_arg = &s->done;
4441 s->blame_complete = 0;
4443 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4444 s->first_displayed_line = 1;
4445 s->last_displayed_line = view->nlines;
4446 s->selected_line = 1;
4448 s->matched_line = 0;
4450 done:
4451 if (commit)
4452 got_object_commit_close(commit);
4453 if (blob)
4454 got_object_blob_close(blob);
4455 free(obj_id);
4456 if (err)
4457 stop_blame(blame);
4458 return err;
4461 static const struct got_error *
4462 open_blame_view(struct tog_view *view, char *path,
4463 struct got_object_id *commit_id, struct got_repository *repo)
4465 const struct got_error *err = NULL;
4466 struct tog_blame_view_state *s = &view->state.blame;
4468 STAILQ_INIT(&s->blamed_commits);
4470 s->path = strdup(path);
4471 if (s->path == NULL)
4472 return got_error_from_errno("strdup");
4474 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4475 if (err) {
4476 free(s->path);
4477 return err;
4480 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4481 s->first_displayed_line = 1;
4482 s->last_displayed_line = view->nlines;
4483 s->selected_line = 1;
4484 s->blame_complete = 0;
4485 s->repo = repo;
4486 s->commit_id = commit_id;
4487 memset(&s->blame, 0, sizeof(s->blame));
4489 STAILQ_INIT(&s->colors);
4490 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4491 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4492 get_color_value("TOG_COLOR_COMMIT"));
4493 if (err)
4494 return err;
4497 view->show = show_blame_view;
4498 view->input = input_blame_view;
4499 view->close = close_blame_view;
4500 view->search_start = search_start_blame_view;
4501 view->search_next = search_next_blame_view;
4503 return run_blame(view);
4506 static const struct got_error *
4507 close_blame_view(struct tog_view *view)
4509 const struct got_error *err = NULL;
4510 struct tog_blame_view_state *s = &view->state.blame;
4512 if (s->blame.thread)
4513 err = stop_blame(&s->blame);
4515 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4516 struct got_object_qid *blamed_commit;
4517 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4518 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4519 got_object_qid_free(blamed_commit);
4522 free(s->path);
4523 free_colors(&s->colors);
4525 return err;
4528 static const struct got_error *
4529 search_start_blame_view(struct tog_view *view)
4531 struct tog_blame_view_state *s = &view->state.blame;
4533 s->matched_line = 0;
4534 return NULL;
4537 static const struct got_error *
4538 search_next_blame_view(struct tog_view *view)
4540 struct tog_blame_view_state *s = &view->state.blame;
4541 int lineno;
4542 char *line = NULL;
4543 size_t linesize = 0;
4544 ssize_t linelen;
4546 if (!view->searching) {
4547 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4548 return NULL;
4551 if (s->matched_line) {
4552 if (view->searching == TOG_SEARCH_FORWARD)
4553 lineno = s->matched_line + 1;
4554 else
4555 lineno = s->matched_line - 1;
4556 } else
4557 lineno = s->first_displayed_line - 1 + s->selected_line;
4559 while (1) {
4560 off_t offset;
4562 if (lineno <= 0 || lineno > s->blame.nlines) {
4563 if (s->matched_line == 0) {
4564 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4565 break;
4568 if (view->searching == TOG_SEARCH_FORWARD)
4569 lineno = 1;
4570 else
4571 lineno = s->blame.nlines;
4574 offset = s->blame.line_offsets[lineno - 1];
4575 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4576 free(line);
4577 return got_error_from_errno("fseeko");
4579 linelen = getline(&line, &linesize, s->blame.f);
4580 if (linelen != -1 &&
4581 match_line(line, &view->regex, 1, &view->regmatch)) {
4582 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4583 s->matched_line = lineno;
4584 break;
4586 if (view->searching == TOG_SEARCH_FORWARD)
4587 lineno++;
4588 else
4589 lineno--;
4591 free(line);
4593 if (s->matched_line) {
4594 s->first_displayed_line = s->matched_line;
4595 s->selected_line = 1;
4598 return NULL;
4601 static const struct got_error *
4602 show_blame_view(struct tog_view *view)
4604 const struct got_error *err = NULL;
4605 struct tog_blame_view_state *s = &view->state.blame;
4606 int errcode;
4608 if (s->blame.thread == NULL && !s->blame_complete) {
4609 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4610 &s->blame.thread_args);
4611 if (errcode)
4612 return got_error_set_errno(errcode, "pthread_create");
4614 halfdelay(1); /* fast refresh while annotating */
4617 if (s->blame_complete)
4618 halfdelay(10); /* disable fast refresh */
4620 err = draw_blame(view);
4622 view_vborder(view);
4623 return err;
4626 static const struct got_error *
4627 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4629 const struct got_error *err = NULL, *thread_err = NULL;
4630 struct tog_view *diff_view;
4631 struct tog_blame_view_state *s = &view->state.blame;
4632 int begin_x = 0;
4634 switch (ch) {
4635 case 'q':
4636 s->done = 1;
4637 break;
4638 case 'g':
4639 case KEY_HOME:
4640 s->selected_line = 1;
4641 s->first_displayed_line = 1;
4642 break;
4643 case 'G':
4644 case KEY_END:
4645 if (s->blame.nlines < view->nlines - 2) {
4646 s->selected_line = s->blame.nlines;
4647 s->first_displayed_line = 1;
4648 } else {
4649 s->selected_line = view->nlines - 2;
4650 s->first_displayed_line = s->blame.nlines -
4651 (view->nlines - 3);
4653 break;
4654 case 'k':
4655 case KEY_UP:
4656 case CTRL('p'):
4657 if (s->selected_line > 1)
4658 s->selected_line--;
4659 else if (s->selected_line == 1 &&
4660 s->first_displayed_line > 1)
4661 s->first_displayed_line--;
4662 break;
4663 case KEY_PPAGE:
4664 case CTRL('b'):
4665 if (s->first_displayed_line == 1) {
4666 s->selected_line = 1;
4667 break;
4669 if (s->first_displayed_line > view->nlines - 2)
4670 s->first_displayed_line -=
4671 (view->nlines - 2);
4672 else
4673 s->first_displayed_line = 1;
4674 break;
4675 case 'j':
4676 case KEY_DOWN:
4677 case CTRL('n'):
4678 if (s->selected_line < view->nlines - 2 &&
4679 s->first_displayed_line +
4680 s->selected_line <= s->blame.nlines)
4681 s->selected_line++;
4682 else if (s->last_displayed_line <
4683 s->blame.nlines)
4684 s->first_displayed_line++;
4685 break;
4686 case 'b':
4687 case 'p': {
4688 struct got_object_id *id = NULL;
4689 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4690 s->first_displayed_line, s->selected_line);
4691 if (id == NULL)
4692 break;
4693 if (ch == 'p') {
4694 struct got_commit_object *commit, *pcommit;
4695 struct got_object_qid *pid;
4696 struct got_object_id *blob_id = NULL;
4697 int obj_type;
4698 err = got_object_open_as_commit(&commit,
4699 s->repo, id);
4700 if (err)
4701 break;
4702 pid = STAILQ_FIRST(
4703 got_object_commit_get_parent_ids(commit));
4704 if (pid == NULL) {
4705 got_object_commit_close(commit);
4706 break;
4708 /* Check if path history ends here. */
4709 err = got_object_open_as_commit(&pcommit,
4710 s->repo, &pid->id);
4711 if (err)
4712 break;
4713 err = got_object_id_by_path(&blob_id, s->repo,
4714 pcommit, s->path);
4715 got_object_commit_close(pcommit);
4716 if (err) {
4717 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4718 err = NULL;
4719 got_object_commit_close(commit);
4720 break;
4722 err = got_object_get_type(&obj_type, s->repo,
4723 blob_id);
4724 free(blob_id);
4725 /* Can't blame non-blob type objects. */
4726 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4727 got_object_commit_close(commit);
4728 break;
4730 err = got_object_qid_alloc(&s->blamed_commit,
4731 &pid->id);
4732 got_object_commit_close(commit);
4733 } else {
4734 if (got_object_id_cmp(id,
4735 &s->blamed_commit->id) == 0)
4736 break;
4737 err = got_object_qid_alloc(&s->blamed_commit,
4738 id);
4740 if (err)
4741 break;
4742 s->done = 1;
4743 thread_err = stop_blame(&s->blame);
4744 s->done = 0;
4745 if (thread_err)
4746 break;
4747 STAILQ_INSERT_HEAD(&s->blamed_commits,
4748 s->blamed_commit, entry);
4749 err = run_blame(view);
4750 if (err)
4751 break;
4752 break;
4754 case 'B': {
4755 struct got_object_qid *first;
4756 first = STAILQ_FIRST(&s->blamed_commits);
4757 if (!got_object_id_cmp(&first->id, s->commit_id))
4758 break;
4759 s->done = 1;
4760 thread_err = stop_blame(&s->blame);
4761 s->done = 0;
4762 if (thread_err)
4763 break;
4764 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4765 got_object_qid_free(s->blamed_commit);
4766 s->blamed_commit =
4767 STAILQ_FIRST(&s->blamed_commits);
4768 err = run_blame(view);
4769 if (err)
4770 break;
4771 break;
4773 case KEY_ENTER:
4774 case '\r': {
4775 struct got_object_id *id = NULL;
4776 struct got_object_qid *pid;
4777 struct got_commit_object *commit = NULL;
4778 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4779 s->first_displayed_line, s->selected_line);
4780 if (id == NULL)
4781 break;
4782 err = got_object_open_as_commit(&commit, s->repo, id);
4783 if (err)
4784 break;
4785 pid = STAILQ_FIRST(
4786 got_object_commit_get_parent_ids(commit));
4787 if (view_is_parent_view(view))
4788 begin_x = view_split_begin_x(view->begin_x);
4789 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4790 if (diff_view == NULL) {
4791 got_object_commit_close(commit);
4792 err = got_error_from_errno("view_open");
4793 break;
4795 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4796 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4797 got_object_commit_close(commit);
4798 if (err) {
4799 view_close(diff_view);
4800 break;
4802 view->focussed = 0;
4803 diff_view->focussed = 1;
4804 if (view_is_parent_view(view)) {
4805 err = view_close_child(view);
4806 if (err)
4807 break;
4808 view_set_child(view, diff_view);
4809 view->focus_child = 1;
4810 } else
4811 *new_view = diff_view;
4812 if (err)
4813 break;
4814 break;
4816 case KEY_NPAGE:
4817 case CTRL('f'):
4818 case ' ':
4819 if (s->last_displayed_line >= s->blame.nlines &&
4820 s->selected_line >= MIN(s->blame.nlines,
4821 view->nlines - 2)) {
4822 break;
4824 if (s->last_displayed_line >= s->blame.nlines &&
4825 s->selected_line < view->nlines - 2) {
4826 s->selected_line = MIN(s->blame.nlines,
4827 view->nlines - 2);
4828 break;
4830 if (s->last_displayed_line + view->nlines - 2
4831 <= s->blame.nlines)
4832 s->first_displayed_line +=
4833 view->nlines - 2;
4834 else
4835 s->first_displayed_line =
4836 s->blame.nlines -
4837 (view->nlines - 3);
4838 break;
4839 case KEY_RESIZE:
4840 if (s->selected_line > view->nlines - 2) {
4841 s->selected_line = MIN(s->blame.nlines,
4842 view->nlines - 2);
4844 break;
4845 default:
4846 break;
4848 return thread_err ? thread_err : err;
4851 static const struct got_error *
4852 cmd_blame(int argc, char *argv[])
4854 const struct got_error *error;
4855 struct got_repository *repo = NULL;
4856 struct got_worktree *worktree = NULL;
4857 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4858 char *link_target = NULL;
4859 struct got_object_id *commit_id = NULL;
4860 struct got_commit_object *commit = NULL;
4861 char *commit_id_str = NULL;
4862 int ch;
4863 struct tog_view *view;
4865 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4866 switch (ch) {
4867 case 'c':
4868 commit_id_str = optarg;
4869 break;
4870 case 'r':
4871 repo_path = realpath(optarg, NULL);
4872 if (repo_path == NULL)
4873 return got_error_from_errno2("realpath",
4874 optarg);
4875 break;
4876 default:
4877 usage_blame();
4878 /* NOTREACHED */
4882 argc -= optind;
4883 argv += optind;
4885 if (argc != 1)
4886 usage_blame();
4888 if (repo_path == NULL) {
4889 cwd = getcwd(NULL, 0);
4890 if (cwd == NULL)
4891 return got_error_from_errno("getcwd");
4892 error = got_worktree_open(&worktree, cwd);
4893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4894 goto done;
4895 if (worktree)
4896 repo_path =
4897 strdup(got_worktree_get_repo_path(worktree));
4898 else
4899 repo_path = strdup(cwd);
4900 if (repo_path == NULL) {
4901 error = got_error_from_errno("strdup");
4902 goto done;
4906 error = got_repo_open(&repo, repo_path, NULL);
4907 if (error != NULL)
4908 goto done;
4910 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4911 worktree);
4912 if (error)
4913 goto done;
4915 init_curses();
4917 error = apply_unveil(got_repo_get_path(repo), NULL);
4918 if (error)
4919 goto done;
4921 error = tog_load_refs(repo, 0);
4922 if (error)
4923 goto done;
4925 if (commit_id_str == NULL) {
4926 struct got_reference *head_ref;
4927 error = got_ref_open(&head_ref, repo, worktree ?
4928 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4929 if (error != NULL)
4930 goto done;
4931 error = got_ref_resolve(&commit_id, repo, head_ref);
4932 got_ref_close(head_ref);
4933 } else {
4934 error = got_repo_match_object_id(&commit_id, NULL,
4935 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4937 if (error != NULL)
4938 goto done;
4940 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4941 if (view == NULL) {
4942 error = got_error_from_errno("view_open");
4943 goto done;
4946 error = got_object_open_as_commit(&commit, repo, commit_id);
4947 if (error)
4948 goto done;
4950 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4951 commit, repo);
4952 if (error)
4953 goto done;
4955 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4956 commit_id, repo);
4957 if (error)
4958 goto done;
4959 if (worktree) {
4960 /* Release work tree lock. */
4961 got_worktree_close(worktree);
4962 worktree = NULL;
4964 error = view_loop(view);
4965 done:
4966 free(repo_path);
4967 free(in_repo_path);
4968 free(link_target);
4969 free(cwd);
4970 free(commit_id);
4971 if (commit)
4972 got_object_commit_close(commit);
4973 if (worktree)
4974 got_worktree_close(worktree);
4975 if (repo) {
4976 const struct got_error *close_err = got_repo_close(repo);
4977 if (error == NULL)
4978 error = close_err;
4980 tog_free_refs();
4981 return error;
4984 static const struct got_error *
4985 draw_tree_entries(struct tog_view *view, const char *parent_path)
4987 struct tog_tree_view_state *s = &view->state.tree;
4988 const struct got_error *err = NULL;
4989 struct got_tree_entry *te;
4990 wchar_t *wline;
4991 struct tog_color *tc;
4992 int width, n, i, nentries;
4993 int limit = view->nlines;
4995 s->ndisplayed = 0;
4997 werase(view->window);
4999 if (limit == 0)
5000 return NULL;
5002 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5003 if (err)
5004 return err;
5005 if (view_needs_focus_indication(view))
5006 wstandout(view->window);
5007 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5008 if (tc)
5009 wattr_on(view->window,
5010 COLOR_PAIR(tc->colorpair), NULL);
5011 waddwstr(view->window, wline);
5012 if (tc)
5013 wattr_off(view->window,
5014 COLOR_PAIR(tc->colorpair), NULL);
5015 if (view_needs_focus_indication(view))
5016 wstandend(view->window);
5017 free(wline);
5018 wline = NULL;
5019 if (width < view->ncols - 1)
5020 waddch(view->window, '\n');
5021 if (--limit <= 0)
5022 return NULL;
5023 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5024 if (err)
5025 return err;
5026 waddwstr(view->window, wline);
5027 free(wline);
5028 wline = NULL;
5029 if (width < view->ncols - 1)
5030 waddch(view->window, '\n');
5031 if (--limit <= 0)
5032 return NULL;
5033 waddch(view->window, '\n');
5034 if (--limit <= 0)
5035 return NULL;
5037 if (s->first_displayed_entry == NULL) {
5038 te = got_object_tree_get_first_entry(s->tree);
5039 if (s->selected == 0) {
5040 if (view->focussed)
5041 wstandout(view->window);
5042 s->selected_entry = NULL;
5044 waddstr(view->window, " ..\n"); /* parent directory */
5045 if (s->selected == 0 && view->focussed)
5046 wstandend(view->window);
5047 s->ndisplayed++;
5048 if (--limit <= 0)
5049 return NULL;
5050 n = 1;
5051 } else {
5052 n = 0;
5053 te = s->first_displayed_entry;
5056 nentries = got_object_tree_get_nentries(s->tree);
5057 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5058 char *line = NULL, *id_str = NULL, *link_target = NULL;
5059 const char *modestr = "";
5060 mode_t mode;
5062 te = got_object_tree_get_entry(s->tree, i);
5063 mode = got_tree_entry_get_mode(te);
5065 if (s->show_ids) {
5066 err = got_object_id_str(&id_str,
5067 got_tree_entry_get_id(te));
5068 if (err)
5069 return got_error_from_errno(
5070 "got_object_id_str");
5072 if (got_object_tree_entry_is_submodule(te))
5073 modestr = "$";
5074 else if (S_ISLNK(mode)) {
5075 int i;
5077 err = got_tree_entry_get_symlink_target(&link_target,
5078 te, s->repo);
5079 if (err) {
5080 free(id_str);
5081 return err;
5083 for (i = 0; i < strlen(link_target); i++) {
5084 if (!isprint((unsigned char)link_target[i]))
5085 link_target[i] = '?';
5087 modestr = "@";
5089 else if (S_ISDIR(mode))
5090 modestr = "/";
5091 else if (mode & S_IXUSR)
5092 modestr = "*";
5093 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5094 got_tree_entry_get_name(te), modestr,
5095 link_target ? " -> ": "",
5096 link_target ? link_target : "") == -1) {
5097 free(id_str);
5098 free(link_target);
5099 return got_error_from_errno("asprintf");
5101 free(id_str);
5102 free(link_target);
5103 err = format_line(&wline, &width, line, view->ncols, 0);
5104 if (err) {
5105 free(line);
5106 break;
5108 if (n == s->selected) {
5109 if (view->focussed)
5110 wstandout(view->window);
5111 s->selected_entry = te;
5113 tc = match_color(&s->colors, line);
5114 if (tc)
5115 wattr_on(view->window,
5116 COLOR_PAIR(tc->colorpair), NULL);
5117 waddwstr(view->window, wline);
5118 if (tc)
5119 wattr_off(view->window,
5120 COLOR_PAIR(tc->colorpair), NULL);
5121 if (width < view->ncols - 1)
5122 waddch(view->window, '\n');
5123 if (n == s->selected && view->focussed)
5124 wstandend(view->window);
5125 free(line);
5126 free(wline);
5127 wline = NULL;
5128 n++;
5129 s->ndisplayed++;
5130 s->last_displayed_entry = te;
5131 if (--limit <= 0)
5132 break;
5135 return err;
5138 static void
5139 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5141 struct got_tree_entry *te;
5142 int isroot = s->tree == s->root;
5143 int i = 0;
5145 if (s->first_displayed_entry == NULL)
5146 return;
5148 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5149 while (i++ < maxscroll) {
5150 if (te == NULL) {
5151 if (!isroot)
5152 s->first_displayed_entry = NULL;
5153 break;
5155 s->first_displayed_entry = te;
5156 te = got_tree_entry_get_prev(s->tree, te);
5160 static void
5161 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5163 struct got_tree_entry *next, *last;
5164 int n = 0;
5166 if (s->first_displayed_entry)
5167 next = got_tree_entry_get_next(s->tree,
5168 s->first_displayed_entry);
5169 else
5170 next = got_object_tree_get_first_entry(s->tree);
5172 last = s->last_displayed_entry;
5173 while (next && last && n++ < maxscroll) {
5174 last = got_tree_entry_get_next(s->tree, last);
5175 if (last) {
5176 s->first_displayed_entry = next;
5177 next = got_tree_entry_get_next(s->tree, next);
5182 static const struct got_error *
5183 tree_entry_path(char **path, struct tog_parent_trees *parents,
5184 struct got_tree_entry *te)
5186 const struct got_error *err = NULL;
5187 struct tog_parent_tree *pt;
5188 size_t len = 2; /* for leading slash and NUL */
5190 TAILQ_FOREACH(pt, parents, entry)
5191 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5192 + 1 /* slash */;
5193 if (te)
5194 len += strlen(got_tree_entry_get_name(te));
5196 *path = calloc(1, len);
5197 if (path == NULL)
5198 return got_error_from_errno("calloc");
5200 (*path)[0] = '/';
5201 pt = TAILQ_LAST(parents, tog_parent_trees);
5202 while (pt) {
5203 const char *name = got_tree_entry_get_name(pt->selected_entry);
5204 if (strlcat(*path, name, len) >= len) {
5205 err = got_error(GOT_ERR_NO_SPACE);
5206 goto done;
5208 if (strlcat(*path, "/", len) >= len) {
5209 err = got_error(GOT_ERR_NO_SPACE);
5210 goto done;
5212 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5214 if (te) {
5215 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5216 err = got_error(GOT_ERR_NO_SPACE);
5217 goto done;
5220 done:
5221 if (err) {
5222 free(*path);
5223 *path = NULL;
5225 return err;
5228 static const struct got_error *
5229 blame_tree_entry(struct tog_view **new_view, int begin_x,
5230 struct got_tree_entry *te, struct tog_parent_trees *parents,
5231 struct got_object_id *commit_id, struct got_repository *repo)
5233 const struct got_error *err = NULL;
5234 char *path;
5235 struct tog_view *blame_view;
5237 *new_view = NULL;
5239 err = tree_entry_path(&path, parents, te);
5240 if (err)
5241 return err;
5243 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5244 if (blame_view == NULL) {
5245 err = got_error_from_errno("view_open");
5246 goto done;
5249 err = open_blame_view(blame_view, path, commit_id, repo);
5250 if (err) {
5251 if (err->code == GOT_ERR_CANCELLED)
5252 err = NULL;
5253 view_close(blame_view);
5254 } else
5255 *new_view = blame_view;
5256 done:
5257 free(path);
5258 return err;
5261 static const struct got_error *
5262 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5263 struct tog_tree_view_state *s)
5265 struct tog_view *log_view;
5266 const struct got_error *err = NULL;
5267 char *path;
5269 *new_view = NULL;
5271 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5272 if (log_view == NULL)
5273 return got_error_from_errno("view_open");
5275 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5276 if (err)
5277 return err;
5279 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5280 path, 0);
5281 if (err)
5282 view_close(log_view);
5283 else
5284 *new_view = log_view;
5285 free(path);
5286 return err;
5289 static const struct got_error *
5290 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5291 const char *head_ref_name, struct got_repository *repo)
5293 const struct got_error *err = NULL;
5294 char *commit_id_str = NULL;
5295 struct tog_tree_view_state *s = &view->state.tree;
5296 struct got_commit_object *commit = NULL;
5298 TAILQ_INIT(&s->parents);
5299 STAILQ_INIT(&s->colors);
5301 s->commit_id = got_object_id_dup(commit_id);
5302 if (s->commit_id == NULL)
5303 return got_error_from_errno("got_object_id_dup");
5305 err = got_object_open_as_commit(&commit, repo, commit_id);
5306 if (err)
5307 goto done;
5310 * The root is opened here and will be closed when the view is closed.
5311 * Any visited subtrees and their path-wise parents are opened and
5312 * closed on demand.
5314 err = got_object_open_as_tree(&s->root, repo,
5315 got_object_commit_get_tree_id(commit));
5316 if (err)
5317 goto done;
5318 s->tree = s->root;
5320 err = got_object_id_str(&commit_id_str, commit_id);
5321 if (err != NULL)
5322 goto done;
5324 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5325 err = got_error_from_errno("asprintf");
5326 goto done;
5329 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5330 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5331 if (head_ref_name) {
5332 s->head_ref_name = strdup(head_ref_name);
5333 if (s->head_ref_name == NULL) {
5334 err = got_error_from_errno("strdup");
5335 goto done;
5338 s->repo = repo;
5340 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5341 err = add_color(&s->colors, "\\$$",
5342 TOG_COLOR_TREE_SUBMODULE,
5343 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5344 if (err)
5345 goto done;
5346 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5347 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5348 if (err)
5349 goto done;
5350 err = add_color(&s->colors, "/$",
5351 TOG_COLOR_TREE_DIRECTORY,
5352 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5353 if (err)
5354 goto done;
5356 err = add_color(&s->colors, "\\*$",
5357 TOG_COLOR_TREE_EXECUTABLE,
5358 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5359 if (err)
5360 goto done;
5362 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5363 get_color_value("TOG_COLOR_COMMIT"));
5364 if (err)
5365 goto done;
5368 view->show = show_tree_view;
5369 view->input = input_tree_view;
5370 view->close = close_tree_view;
5371 view->search_start = search_start_tree_view;
5372 view->search_next = search_next_tree_view;
5373 done:
5374 free(commit_id_str);
5375 if (commit)
5376 got_object_commit_close(commit);
5377 if (err)
5378 close_tree_view(view);
5379 return err;
5382 static const struct got_error *
5383 close_tree_view(struct tog_view *view)
5385 struct tog_tree_view_state *s = &view->state.tree;
5387 free_colors(&s->colors);
5388 free(s->tree_label);
5389 s->tree_label = NULL;
5390 free(s->commit_id);
5391 s->commit_id = NULL;
5392 free(s->head_ref_name);
5393 s->head_ref_name = NULL;
5394 while (!TAILQ_EMPTY(&s->parents)) {
5395 struct tog_parent_tree *parent;
5396 parent = TAILQ_FIRST(&s->parents);
5397 TAILQ_REMOVE(&s->parents, parent, entry);
5398 if (parent->tree != s->root)
5399 got_object_tree_close(parent->tree);
5400 free(parent);
5403 if (s->tree != NULL && s->tree != s->root)
5404 got_object_tree_close(s->tree);
5405 if (s->root)
5406 got_object_tree_close(s->root);
5407 return NULL;
5410 static const struct got_error *
5411 search_start_tree_view(struct tog_view *view)
5413 struct tog_tree_view_state *s = &view->state.tree;
5415 s->matched_entry = NULL;
5416 return NULL;
5419 static int
5420 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5422 regmatch_t regmatch;
5424 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5425 0) == 0;
5428 static const struct got_error *
5429 search_next_tree_view(struct tog_view *view)
5431 struct tog_tree_view_state *s = &view->state.tree;
5432 struct got_tree_entry *te = NULL;
5434 if (!view->searching) {
5435 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5436 return NULL;
5439 if (s->matched_entry) {
5440 if (view->searching == TOG_SEARCH_FORWARD) {
5441 if (s->selected_entry)
5442 te = got_tree_entry_get_next(s->tree,
5443 s->selected_entry);
5444 else
5445 te = got_object_tree_get_first_entry(s->tree);
5446 } else {
5447 if (s->selected_entry == NULL)
5448 te = got_object_tree_get_last_entry(s->tree);
5449 else
5450 te = got_tree_entry_get_prev(s->tree,
5451 s->selected_entry);
5453 } else {
5454 if (s->selected_entry)
5455 te = s->selected_entry;
5456 else if (view->searching == TOG_SEARCH_FORWARD)
5457 te = got_object_tree_get_first_entry(s->tree);
5458 else
5459 te = got_object_tree_get_last_entry(s->tree);
5462 while (1) {
5463 if (te == NULL) {
5464 if (s->matched_entry == NULL) {
5465 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5466 return NULL;
5468 if (view->searching == TOG_SEARCH_FORWARD)
5469 te = got_object_tree_get_first_entry(s->tree);
5470 else
5471 te = got_object_tree_get_last_entry(s->tree);
5474 if (match_tree_entry(te, &view->regex)) {
5475 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5476 s->matched_entry = te;
5477 break;
5480 if (view->searching == TOG_SEARCH_FORWARD)
5481 te = got_tree_entry_get_next(s->tree, te);
5482 else
5483 te = got_tree_entry_get_prev(s->tree, te);
5486 if (s->matched_entry) {
5487 s->first_displayed_entry = s->matched_entry;
5488 s->selected = 0;
5491 return NULL;
5494 static const struct got_error *
5495 show_tree_view(struct tog_view *view)
5497 const struct got_error *err = NULL;
5498 struct tog_tree_view_state *s = &view->state.tree;
5499 char *parent_path;
5501 err = tree_entry_path(&parent_path, &s->parents, NULL);
5502 if (err)
5503 return err;
5505 err = draw_tree_entries(view, parent_path);
5506 free(parent_path);
5508 view_vborder(view);
5509 return err;
5512 static const struct got_error *
5513 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5515 const struct got_error *err = NULL;
5516 struct tog_tree_view_state *s = &view->state.tree;
5517 struct tog_view *log_view, *ref_view;
5518 struct got_tree_entry *te;
5519 int begin_x = 0, n;
5521 switch (ch) {
5522 case 'i':
5523 s->show_ids = !s->show_ids;
5524 break;
5525 case 'l':
5526 if (!s->selected_entry)
5527 break;
5528 if (view_is_parent_view(view))
5529 begin_x = view_split_begin_x(view->begin_x);
5530 err = log_selected_tree_entry(&log_view, begin_x, s);
5531 view->focussed = 0;
5532 log_view->focussed = 1;
5533 if (view_is_parent_view(view)) {
5534 err = view_close_child(view);
5535 if (err)
5536 return err;
5537 view_set_child(view, log_view);
5538 view->focus_child = 1;
5539 } else
5540 *new_view = log_view;
5541 break;
5542 case 'r':
5543 if (view_is_parent_view(view))
5544 begin_x = view_split_begin_x(view->begin_x);
5545 ref_view = view_open(view->nlines, view->ncols,
5546 view->begin_y, begin_x, TOG_VIEW_REF);
5547 if (ref_view == NULL)
5548 return got_error_from_errno("view_open");
5549 err = open_ref_view(ref_view, s->repo);
5550 if (err) {
5551 view_close(ref_view);
5552 return err;
5554 view->focussed = 0;
5555 ref_view->focussed = 1;
5556 if (view_is_parent_view(view)) {
5557 err = view_close_child(view);
5558 if (err)
5559 return err;
5560 view_set_child(view, ref_view);
5561 view->focus_child = 1;
5562 } else
5563 *new_view = ref_view;
5564 break;
5565 case 'g':
5566 case KEY_HOME:
5567 s->selected = 0;
5568 if (s->tree == s->root)
5569 s->first_displayed_entry =
5570 got_object_tree_get_first_entry(s->tree);
5571 else
5572 s->first_displayed_entry = NULL;
5573 break;
5574 case 'G':
5575 case KEY_END:
5576 s->selected = 0;
5577 te = got_object_tree_get_last_entry(s->tree);
5578 for (n = 0; n < view->nlines - 3; n++) {
5579 if (te == NULL) {
5580 if(s->tree != s->root) {
5581 s->first_displayed_entry = NULL;
5582 n++;
5584 break;
5586 s->first_displayed_entry = te;
5587 te = got_tree_entry_get_prev(s->tree, te);
5589 if (n > 0)
5590 s->selected = n - 1;
5591 break;
5592 case 'k':
5593 case KEY_UP:
5594 case CTRL('p'):
5595 if (s->selected > 0) {
5596 s->selected--;
5597 break;
5599 tree_scroll_up(s, 1);
5600 break;
5601 case KEY_PPAGE:
5602 case CTRL('b'):
5603 if (s->tree == s->root) {
5604 if (got_object_tree_get_first_entry(s->tree) ==
5605 s->first_displayed_entry)
5606 s->selected = 0;
5607 } else {
5608 if (s->first_displayed_entry == NULL)
5609 s->selected = 0;
5611 tree_scroll_up(s, MAX(0, view->nlines - 3));
5612 break;
5613 case 'j':
5614 case KEY_DOWN:
5615 case CTRL('n'):
5616 if (s->selected < s->ndisplayed - 1) {
5617 s->selected++;
5618 break;
5620 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5621 == NULL)
5622 /* can't scroll any further */
5623 break;
5624 tree_scroll_down(s, 1);
5625 break;
5626 case KEY_NPAGE:
5627 case CTRL('f'):
5628 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5629 == NULL) {
5630 /* can't scroll any further; move cursor down */
5631 if (s->selected < s->ndisplayed - 1)
5632 s->selected = s->ndisplayed - 1;
5633 break;
5635 tree_scroll_down(s, view->nlines - 3);
5636 break;
5637 case KEY_ENTER:
5638 case '\r':
5639 case KEY_BACKSPACE:
5640 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5641 struct tog_parent_tree *parent;
5642 /* user selected '..' */
5643 if (s->tree == s->root)
5644 break;
5645 parent = TAILQ_FIRST(&s->parents);
5646 TAILQ_REMOVE(&s->parents, parent,
5647 entry);
5648 got_object_tree_close(s->tree);
5649 s->tree = parent->tree;
5650 s->first_displayed_entry =
5651 parent->first_displayed_entry;
5652 s->selected_entry =
5653 parent->selected_entry;
5654 s->selected = parent->selected;
5655 free(parent);
5656 } else if (S_ISDIR(got_tree_entry_get_mode(
5657 s->selected_entry))) {
5658 struct got_tree_object *subtree;
5659 err = got_object_open_as_tree(&subtree, s->repo,
5660 got_tree_entry_get_id(s->selected_entry));
5661 if (err)
5662 break;
5663 err = tree_view_visit_subtree(s, subtree);
5664 if (err) {
5665 got_object_tree_close(subtree);
5666 break;
5668 } else if (S_ISREG(got_tree_entry_get_mode(
5669 s->selected_entry))) {
5670 struct tog_view *blame_view;
5671 int begin_x = view_is_parent_view(view) ?
5672 view_split_begin_x(view->begin_x) : 0;
5674 err = blame_tree_entry(&blame_view, begin_x,
5675 s->selected_entry, &s->parents,
5676 s->commit_id, s->repo);
5677 if (err)
5678 break;
5679 view->focussed = 0;
5680 blame_view->focussed = 1;
5681 if (view_is_parent_view(view)) {
5682 err = view_close_child(view);
5683 if (err)
5684 return err;
5685 view_set_child(view, blame_view);
5686 view->focus_child = 1;
5687 } else
5688 *new_view = blame_view;
5690 break;
5691 case KEY_RESIZE:
5692 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5693 s->selected = view->nlines - 4;
5694 break;
5695 default:
5696 break;
5699 return err;
5702 __dead static void
5703 usage_tree(void)
5705 endwin();
5706 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5707 getprogname());
5708 exit(1);
5711 static const struct got_error *
5712 cmd_tree(int argc, char *argv[])
5714 const struct got_error *error;
5715 struct got_repository *repo = NULL;
5716 struct got_worktree *worktree = NULL;
5717 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5718 struct got_object_id *commit_id = NULL;
5719 struct got_commit_object *commit = NULL;
5720 const char *commit_id_arg = NULL;
5721 char *label = NULL;
5722 struct got_reference *ref = NULL;
5723 const char *head_ref_name = NULL;
5724 int ch;
5725 struct tog_view *view;
5727 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5728 switch (ch) {
5729 case 'c':
5730 commit_id_arg = optarg;
5731 break;
5732 case 'r':
5733 repo_path = realpath(optarg, NULL);
5734 if (repo_path == NULL)
5735 return got_error_from_errno2("realpath",
5736 optarg);
5737 break;
5738 default:
5739 usage_tree();
5740 /* NOTREACHED */
5744 argc -= optind;
5745 argv += optind;
5747 if (argc > 1)
5748 usage_tree();
5750 if (repo_path == NULL) {
5751 cwd = getcwd(NULL, 0);
5752 if (cwd == NULL)
5753 return got_error_from_errno("getcwd");
5754 error = got_worktree_open(&worktree, cwd);
5755 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5756 goto done;
5757 if (worktree)
5758 repo_path =
5759 strdup(got_worktree_get_repo_path(worktree));
5760 else
5761 repo_path = strdup(cwd);
5762 if (repo_path == NULL) {
5763 error = got_error_from_errno("strdup");
5764 goto done;
5768 error = got_repo_open(&repo, repo_path, NULL);
5769 if (error != NULL)
5770 goto done;
5772 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5773 repo, worktree);
5774 if (error)
5775 goto done;
5777 init_curses();
5779 error = apply_unveil(got_repo_get_path(repo), NULL);
5780 if (error)
5781 goto done;
5783 error = tog_load_refs(repo, 0);
5784 if (error)
5785 goto done;
5787 if (commit_id_arg == NULL) {
5788 error = got_repo_match_object_id(&commit_id, &label,
5789 worktree ? got_worktree_get_head_ref_name(worktree) :
5790 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5791 if (error)
5792 goto done;
5793 head_ref_name = label;
5794 } else {
5795 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5796 if (error == NULL)
5797 head_ref_name = got_ref_get_name(ref);
5798 else if (error->code != GOT_ERR_NOT_REF)
5799 goto done;
5800 error = got_repo_match_object_id(&commit_id, NULL,
5801 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5802 if (error)
5803 goto done;
5806 error = got_object_open_as_commit(&commit, repo, commit_id);
5807 if (error)
5808 goto done;
5810 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5811 if (view == NULL) {
5812 error = got_error_from_errno("view_open");
5813 goto done;
5815 error = open_tree_view(view, commit_id, head_ref_name, repo);
5816 if (error)
5817 goto done;
5818 if (!got_path_is_root_dir(in_repo_path)) {
5819 error = tree_view_walk_path(&view->state.tree, commit,
5820 in_repo_path);
5821 if (error)
5822 goto done;
5825 if (worktree) {
5826 /* Release work tree lock. */
5827 got_worktree_close(worktree);
5828 worktree = NULL;
5830 error = view_loop(view);
5831 done:
5832 free(repo_path);
5833 free(cwd);
5834 free(commit_id);
5835 free(label);
5836 if (ref)
5837 got_ref_close(ref);
5838 if (repo) {
5839 const struct got_error *close_err = got_repo_close(repo);
5840 if (error == NULL)
5841 error = close_err;
5843 tog_free_refs();
5844 return error;
5847 static const struct got_error *
5848 ref_view_load_refs(struct tog_ref_view_state *s)
5850 struct got_reflist_entry *sre;
5851 struct tog_reflist_entry *re;
5853 s->nrefs = 0;
5854 TAILQ_FOREACH(sre, &tog_refs, entry) {
5855 if (strncmp(got_ref_get_name(sre->ref),
5856 "refs/got/", 9) == 0 &&
5857 strncmp(got_ref_get_name(sre->ref),
5858 "refs/got/backup/", 16) != 0)
5859 continue;
5861 re = malloc(sizeof(*re));
5862 if (re == NULL)
5863 return got_error_from_errno("malloc");
5865 re->ref = got_ref_dup(sre->ref);
5866 if (re->ref == NULL)
5867 return got_error_from_errno("got_ref_dup");
5868 re->idx = s->nrefs++;
5869 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5872 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5873 return NULL;
5876 void
5877 ref_view_free_refs(struct tog_ref_view_state *s)
5879 struct tog_reflist_entry *re;
5881 while (!TAILQ_EMPTY(&s->refs)) {
5882 re = TAILQ_FIRST(&s->refs);
5883 TAILQ_REMOVE(&s->refs, re, entry);
5884 got_ref_close(re->ref);
5885 free(re);
5889 static const struct got_error *
5890 open_ref_view(struct tog_view *view, struct got_repository *repo)
5892 const struct got_error *err = NULL;
5893 struct tog_ref_view_state *s = &view->state.ref;
5895 s->selected_entry = 0;
5896 s->repo = repo;
5898 TAILQ_INIT(&s->refs);
5899 STAILQ_INIT(&s->colors);
5901 err = ref_view_load_refs(s);
5902 if (err)
5903 return err;
5905 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5906 err = add_color(&s->colors, "^refs/heads/",
5907 TOG_COLOR_REFS_HEADS,
5908 get_color_value("TOG_COLOR_REFS_HEADS"));
5909 if (err)
5910 goto done;
5912 err = add_color(&s->colors, "^refs/tags/",
5913 TOG_COLOR_REFS_TAGS,
5914 get_color_value("TOG_COLOR_REFS_TAGS"));
5915 if (err)
5916 goto done;
5918 err = add_color(&s->colors, "^refs/remotes/",
5919 TOG_COLOR_REFS_REMOTES,
5920 get_color_value("TOG_COLOR_REFS_REMOTES"));
5921 if (err)
5922 goto done;
5924 err = add_color(&s->colors, "^refs/got/backup/",
5925 TOG_COLOR_REFS_BACKUP,
5926 get_color_value("TOG_COLOR_REFS_BACKUP"));
5927 if (err)
5928 goto done;
5931 view->show = show_ref_view;
5932 view->input = input_ref_view;
5933 view->close = close_ref_view;
5934 view->search_start = search_start_ref_view;
5935 view->search_next = search_next_ref_view;
5936 done:
5937 if (err)
5938 free_colors(&s->colors);
5939 return err;
5942 static const struct got_error *
5943 close_ref_view(struct tog_view *view)
5945 struct tog_ref_view_state *s = &view->state.ref;
5947 ref_view_free_refs(s);
5948 free_colors(&s->colors);
5950 return NULL;
5953 static const struct got_error *
5954 resolve_reflist_entry(struct got_object_id **commit_id,
5955 struct tog_reflist_entry *re, struct got_repository *repo)
5957 const struct got_error *err = NULL;
5958 struct got_object_id *obj_id;
5959 struct got_tag_object *tag = NULL;
5960 int obj_type;
5962 *commit_id = NULL;
5964 err = got_ref_resolve(&obj_id, repo, re->ref);
5965 if (err)
5966 return err;
5968 err = got_object_get_type(&obj_type, repo, obj_id);
5969 if (err)
5970 goto done;
5972 switch (obj_type) {
5973 case GOT_OBJ_TYPE_COMMIT:
5974 *commit_id = obj_id;
5975 break;
5976 case GOT_OBJ_TYPE_TAG:
5977 err = got_object_open_as_tag(&tag, repo, obj_id);
5978 if (err)
5979 goto done;
5980 free(obj_id);
5981 err = got_object_get_type(&obj_type, repo,
5982 got_object_tag_get_object_id(tag));
5983 if (err)
5984 goto done;
5985 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5986 err = got_error(GOT_ERR_OBJ_TYPE);
5987 goto done;
5989 *commit_id = got_object_id_dup(
5990 got_object_tag_get_object_id(tag));
5991 if (*commit_id == NULL) {
5992 err = got_error_from_errno("got_object_id_dup");
5993 goto done;
5995 break;
5996 default:
5997 err = got_error(GOT_ERR_OBJ_TYPE);
5998 break;
6001 done:
6002 if (tag)
6003 got_object_tag_close(tag);
6004 if (err) {
6005 free(*commit_id);
6006 *commit_id = NULL;
6008 return err;
6011 static const struct got_error *
6012 log_ref_entry(struct tog_view **new_view, int begin_x,
6013 struct tog_reflist_entry *re, struct got_repository *repo)
6015 struct tog_view *log_view;
6016 const struct got_error *err = NULL;
6017 struct got_object_id *commit_id = NULL;
6019 *new_view = NULL;
6021 err = resolve_reflist_entry(&commit_id, re, repo);
6022 if (err) {
6023 if (err->code != GOT_ERR_OBJ_TYPE)
6024 return err;
6025 else
6026 return NULL;
6029 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6030 if (log_view == NULL) {
6031 err = got_error_from_errno("view_open");
6032 goto done;
6035 err = open_log_view(log_view, commit_id, repo,
6036 got_ref_get_name(re->ref), "", 0);
6037 done:
6038 if (err)
6039 view_close(log_view);
6040 else
6041 *new_view = log_view;
6042 free(commit_id);
6043 return err;
6046 static void
6047 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6049 struct tog_reflist_entry *re;
6050 int i = 0;
6052 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6053 return;
6055 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6056 while (i++ < maxscroll) {
6057 if (re == NULL)
6058 break;
6059 s->first_displayed_entry = re;
6060 re = TAILQ_PREV(re, tog_reflist_head, entry);
6064 static void
6065 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6067 struct tog_reflist_entry *next, *last;
6068 int n = 0;
6070 if (s->first_displayed_entry)
6071 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6072 else
6073 next = TAILQ_FIRST(&s->refs);
6075 last = s->last_displayed_entry;
6076 while (next && last && n++ < maxscroll) {
6077 last = TAILQ_NEXT(last, entry);
6078 if (last) {
6079 s->first_displayed_entry = next;
6080 next = TAILQ_NEXT(next, entry);
6085 static const struct got_error *
6086 search_start_ref_view(struct tog_view *view)
6088 struct tog_ref_view_state *s = &view->state.ref;
6090 s->matched_entry = NULL;
6091 return NULL;
6094 static int
6095 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6097 regmatch_t regmatch;
6099 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6100 0) == 0;
6103 static const struct got_error *
6104 search_next_ref_view(struct tog_view *view)
6106 struct tog_ref_view_state *s = &view->state.ref;
6107 struct tog_reflist_entry *re = NULL;
6109 if (!view->searching) {
6110 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6111 return NULL;
6114 if (s->matched_entry) {
6115 if (view->searching == TOG_SEARCH_FORWARD) {
6116 if (s->selected_entry)
6117 re = TAILQ_NEXT(s->selected_entry, entry);
6118 else
6119 re = TAILQ_PREV(s->selected_entry,
6120 tog_reflist_head, entry);
6121 } else {
6122 if (s->selected_entry == NULL)
6123 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6124 else
6125 re = TAILQ_PREV(s->selected_entry,
6126 tog_reflist_head, entry);
6128 } else {
6129 if (s->selected_entry)
6130 re = s->selected_entry;
6131 else if (view->searching == TOG_SEARCH_FORWARD)
6132 re = TAILQ_FIRST(&s->refs);
6133 else
6134 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6137 while (1) {
6138 if (re == NULL) {
6139 if (s->matched_entry == NULL) {
6140 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6141 return NULL;
6143 if (view->searching == TOG_SEARCH_FORWARD)
6144 re = TAILQ_FIRST(&s->refs);
6145 else
6146 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6149 if (match_reflist_entry(re, &view->regex)) {
6150 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6151 s->matched_entry = re;
6152 break;
6155 if (view->searching == TOG_SEARCH_FORWARD)
6156 re = TAILQ_NEXT(re, entry);
6157 else
6158 re = TAILQ_PREV(re, tog_reflist_head, entry);
6161 if (s->matched_entry) {
6162 s->first_displayed_entry = s->matched_entry;
6163 s->selected = 0;
6166 return NULL;
6169 static const struct got_error *
6170 show_ref_view(struct tog_view *view)
6172 const struct got_error *err = NULL;
6173 struct tog_ref_view_state *s = &view->state.ref;
6174 struct tog_reflist_entry *re;
6175 char *line = NULL;
6176 wchar_t *wline;
6177 struct tog_color *tc;
6178 int width, n;
6179 int limit = view->nlines;
6181 werase(view->window);
6183 s->ndisplayed = 0;
6185 if (limit == 0)
6186 return NULL;
6188 re = s->first_displayed_entry;
6190 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6191 s->nrefs) == -1)
6192 return got_error_from_errno("asprintf");
6194 err = format_line(&wline, &width, line, view->ncols, 0);
6195 if (err) {
6196 free(line);
6197 return err;
6199 if (view_needs_focus_indication(view))
6200 wstandout(view->window);
6201 waddwstr(view->window, wline);
6202 if (view_needs_focus_indication(view))
6203 wstandend(view->window);
6204 free(wline);
6205 wline = NULL;
6206 free(line);
6207 line = NULL;
6208 if (width < view->ncols - 1)
6209 waddch(view->window, '\n');
6210 if (--limit <= 0)
6211 return NULL;
6213 n = 0;
6214 while (re && limit > 0) {
6215 char *line = NULL;
6217 if (got_ref_is_symbolic(re->ref)) {
6218 if (asprintf(&line, "%s -> %s",
6219 got_ref_get_name(re->ref),
6220 got_ref_get_symref_target(re->ref)) == -1)
6221 return got_error_from_errno("asprintf");
6222 } else if (s->show_ids) {
6223 struct got_object_id *id;
6224 char *id_str;
6225 err = got_ref_resolve(&id, s->repo, re->ref);
6226 if (err)
6227 return err;
6228 err = got_object_id_str(&id_str, id);
6229 if (err) {
6230 free(id);
6231 return err;
6233 if (asprintf(&line, "%s: %s",
6234 got_ref_get_name(re->ref), id_str) == -1) {
6235 err = got_error_from_errno("asprintf");
6236 free(id);
6237 free(id_str);
6238 return err;
6240 free(id);
6241 free(id_str);
6242 } else {
6243 line = strdup(got_ref_get_name(re->ref));
6244 if (line == NULL)
6245 return got_error_from_errno("strdup");
6248 err = format_line(&wline, &width, line, view->ncols, 0);
6249 if (err) {
6250 free(line);
6251 return err;
6253 if (n == s->selected) {
6254 if (view->focussed)
6255 wstandout(view->window);
6256 s->selected_entry = re;
6258 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6259 if (tc)
6260 wattr_on(view->window,
6261 COLOR_PAIR(tc->colorpair), NULL);
6262 waddwstr(view->window, wline);
6263 if (tc)
6264 wattr_off(view->window,
6265 COLOR_PAIR(tc->colorpair), NULL);
6266 if (width < view->ncols - 1)
6267 waddch(view->window, '\n');
6268 if (n == s->selected && view->focussed)
6269 wstandend(view->window);
6270 free(line);
6271 free(wline);
6272 wline = NULL;
6273 n++;
6274 s->ndisplayed++;
6275 s->last_displayed_entry = re;
6277 limit--;
6278 re = TAILQ_NEXT(re, entry);
6281 view_vborder(view);
6282 return err;
6285 static const struct got_error *
6286 browse_ref_tree(struct tog_view **new_view, int begin_x,
6287 struct tog_reflist_entry *re, struct got_repository *repo)
6289 const struct got_error *err = NULL;
6290 struct got_object_id *commit_id = NULL;
6291 struct tog_view *tree_view;
6293 *new_view = NULL;
6295 err = resolve_reflist_entry(&commit_id, re, repo);
6296 if (err) {
6297 if (err->code != GOT_ERR_OBJ_TYPE)
6298 return err;
6299 else
6300 return NULL;
6304 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6305 if (tree_view == NULL) {
6306 err = got_error_from_errno("view_open");
6307 goto done;
6310 err = open_tree_view(tree_view, commit_id,
6311 got_ref_get_name(re->ref), repo);
6312 if (err)
6313 goto done;
6315 *new_view = tree_view;
6316 done:
6317 free(commit_id);
6318 return err;
6320 static const struct got_error *
6321 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6323 const struct got_error *err = NULL;
6324 struct tog_ref_view_state *s = &view->state.ref;
6325 struct tog_view *log_view, *tree_view;
6326 struct tog_reflist_entry *re;
6327 int begin_x = 0, n;
6329 switch (ch) {
6330 case 'i':
6331 s->show_ids = !s->show_ids;
6332 break;
6333 case 'o':
6334 s->sort_by_date = !s->sort_by_date;
6335 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6336 got_ref_cmp_by_commit_timestamp_descending :
6337 tog_ref_cmp_by_name, s->repo);
6338 if (err)
6339 break;
6340 got_reflist_object_id_map_free(tog_refs_idmap);
6341 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6342 &tog_refs, s->repo);
6343 if (err)
6344 break;
6345 ref_view_free_refs(s);
6346 err = ref_view_load_refs(s);
6347 break;
6348 case KEY_ENTER:
6349 case '\r':
6350 if (!s->selected_entry)
6351 break;
6352 if (view_is_parent_view(view))
6353 begin_x = view_split_begin_x(view->begin_x);
6354 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6355 s->repo);
6356 view->focussed = 0;
6357 log_view->focussed = 1;
6358 if (view_is_parent_view(view)) {
6359 err = view_close_child(view);
6360 if (err)
6361 return err;
6362 view_set_child(view, log_view);
6363 view->focus_child = 1;
6364 } else
6365 *new_view = log_view;
6366 break;
6367 case 't':
6368 if (!s->selected_entry)
6369 break;
6370 if (view_is_parent_view(view))
6371 begin_x = view_split_begin_x(view->begin_x);
6372 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6373 s->repo);
6374 if (err || tree_view == NULL)
6375 break;
6376 view->focussed = 0;
6377 tree_view->focussed = 1;
6378 if (view_is_parent_view(view)) {
6379 err = view_close_child(view);
6380 if (err)
6381 return err;
6382 view_set_child(view, tree_view);
6383 view->focus_child = 1;
6384 } else
6385 *new_view = tree_view;
6386 break;
6387 case 'g':
6388 case KEY_HOME:
6389 s->selected = 0;
6390 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6391 break;
6392 case 'G':
6393 case KEY_END:
6394 s->selected = 0;
6395 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6396 for (n = 0; n < view->nlines - 1; n++) {
6397 if (re == NULL)
6398 break;
6399 s->first_displayed_entry = re;
6400 re = TAILQ_PREV(re, tog_reflist_head, entry);
6402 if (n > 0)
6403 s->selected = n - 1;
6404 break;
6405 case 'k':
6406 case KEY_UP:
6407 case CTRL('p'):
6408 if (s->selected > 0) {
6409 s->selected--;
6410 break;
6412 ref_scroll_up(s, 1);
6413 break;
6414 case KEY_PPAGE:
6415 case CTRL('b'):
6416 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6417 s->selected = 0;
6418 ref_scroll_up(s, MAX(0, view->nlines - 1));
6419 break;
6420 case 'j':
6421 case KEY_DOWN:
6422 case CTRL('n'):
6423 if (s->selected < s->ndisplayed - 1) {
6424 s->selected++;
6425 break;
6427 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6428 /* can't scroll any further */
6429 break;
6430 ref_scroll_down(s, 1);
6431 break;
6432 case KEY_NPAGE:
6433 case CTRL('f'):
6434 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6435 /* can't scroll any further; move cursor down */
6436 if (s->selected < s->ndisplayed - 1)
6437 s->selected = s->ndisplayed - 1;
6438 break;
6440 ref_scroll_down(s, view->nlines - 1);
6441 break;
6442 case CTRL('l'):
6443 tog_free_refs();
6444 err = tog_load_refs(s->repo, s->sort_by_date);
6445 if (err)
6446 break;
6447 ref_view_free_refs(s);
6448 err = ref_view_load_refs(s);
6449 break;
6450 case KEY_RESIZE:
6451 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6452 s->selected = view->nlines - 2;
6453 break;
6454 default:
6455 break;
6458 return err;
6461 __dead static void
6462 usage_ref(void)
6464 endwin();
6465 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6466 getprogname());
6467 exit(1);
6470 static const struct got_error *
6471 cmd_ref(int argc, char *argv[])
6473 const struct got_error *error;
6474 struct got_repository *repo = NULL;
6475 struct got_worktree *worktree = NULL;
6476 char *cwd = NULL, *repo_path = NULL;
6477 int ch;
6478 struct tog_view *view;
6480 while ((ch = getopt(argc, argv, "r:")) != -1) {
6481 switch (ch) {
6482 case 'r':
6483 repo_path = realpath(optarg, NULL);
6484 if (repo_path == NULL)
6485 return got_error_from_errno2("realpath",
6486 optarg);
6487 break;
6488 default:
6489 usage_ref();
6490 /* NOTREACHED */
6494 argc -= optind;
6495 argv += optind;
6497 if (argc > 1)
6498 usage_ref();
6500 if (repo_path == NULL) {
6501 cwd = getcwd(NULL, 0);
6502 if (cwd == NULL)
6503 return got_error_from_errno("getcwd");
6504 error = got_worktree_open(&worktree, cwd);
6505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6506 goto done;
6507 if (worktree)
6508 repo_path =
6509 strdup(got_worktree_get_repo_path(worktree));
6510 else
6511 repo_path = strdup(cwd);
6512 if (repo_path == NULL) {
6513 error = got_error_from_errno("strdup");
6514 goto done;
6518 error = got_repo_open(&repo, repo_path, NULL);
6519 if (error != NULL)
6520 goto done;
6522 init_curses();
6524 error = apply_unveil(got_repo_get_path(repo), NULL);
6525 if (error)
6526 goto done;
6528 error = tog_load_refs(repo, 0);
6529 if (error)
6530 goto done;
6532 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6533 if (view == NULL) {
6534 error = got_error_from_errno("view_open");
6535 goto done;
6538 error = open_ref_view(view, repo);
6539 if (error)
6540 goto done;
6542 if (worktree) {
6543 /* Release work tree lock. */
6544 got_worktree_close(worktree);
6545 worktree = NULL;
6547 error = view_loop(view);
6548 done:
6549 free(repo_path);
6550 free(cwd);
6551 if (repo) {
6552 const struct got_error *close_err = got_repo_close(repo);
6553 if (close_err)
6554 error = close_err;
6556 tog_free_refs();
6557 return error;
6560 static void
6561 list_commands(FILE *fp)
6563 size_t i;
6565 fprintf(fp, "commands:");
6566 for (i = 0; i < nitems(tog_commands); i++) {
6567 const struct tog_cmd *cmd = &tog_commands[i];
6568 fprintf(fp, " %s", cmd->name);
6570 fputc('\n', fp);
6573 __dead static void
6574 usage(int hflag, int status)
6576 FILE *fp = (status == 0) ? stdout : stderr;
6578 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6579 getprogname());
6580 if (hflag) {
6581 fprintf(fp, "lazy usage: %s path\n", getprogname());
6582 list_commands(fp);
6584 exit(status);
6587 static char **
6588 make_argv(int argc, ...)
6590 va_list ap;
6591 char **argv;
6592 int i;
6594 va_start(ap, argc);
6596 argv = calloc(argc, sizeof(char *));
6597 if (argv == NULL)
6598 err(1, "calloc");
6599 for (i = 0; i < argc; i++) {
6600 argv[i] = strdup(va_arg(ap, char *));
6601 if (argv[i] == NULL)
6602 err(1, "strdup");
6605 va_end(ap);
6606 return argv;
6610 * Try to convert 'tog path' into a 'tog log path' command.
6611 * The user could simply have mistyped the command rather than knowingly
6612 * provided a path. So check whether argv[0] can in fact be resolved
6613 * to a path in the HEAD commit and print a special error if not.
6614 * This hack is for mpi@ <3
6616 static const struct got_error *
6617 tog_log_with_path(int argc, char *argv[])
6619 const struct got_error *error = NULL, *close_err;
6620 const struct tog_cmd *cmd = NULL;
6621 struct got_repository *repo = NULL;
6622 struct got_worktree *worktree = NULL;
6623 struct got_object_id *commit_id = NULL, *id = NULL;
6624 struct got_commit_object *commit = NULL;
6625 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6626 char *commit_id_str = NULL, **cmd_argv = NULL;
6628 cwd = getcwd(NULL, 0);
6629 if (cwd == NULL)
6630 return got_error_from_errno("getcwd");
6632 error = got_worktree_open(&worktree, cwd);
6633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6634 goto done;
6636 if (worktree)
6637 repo_path = strdup(got_worktree_get_repo_path(worktree));
6638 else
6639 repo_path = strdup(cwd);
6640 if (repo_path == NULL) {
6641 error = got_error_from_errno("strdup");
6642 goto done;
6645 error = got_repo_open(&repo, repo_path, NULL);
6646 if (error != NULL)
6647 goto done;
6649 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6650 repo, worktree);
6651 if (error)
6652 goto done;
6654 error = tog_load_refs(repo, 0);
6655 if (error)
6656 goto done;
6657 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6658 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6659 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6660 if (error)
6661 goto done;
6663 if (worktree) {
6664 got_worktree_close(worktree);
6665 worktree = NULL;
6668 error = got_object_open_as_commit(&commit, repo, commit_id);
6669 if (error)
6670 goto done;
6672 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6673 if (error) {
6674 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6675 goto done;
6676 fprintf(stderr, "%s: '%s' is no known command or path\n",
6677 getprogname(), argv[0]);
6678 usage(1, 1);
6679 /* not reached */
6682 close_err = got_repo_close(repo);
6683 if (error == NULL)
6684 error = close_err;
6685 repo = NULL;
6687 error = got_object_id_str(&commit_id_str, commit_id);
6688 if (error)
6689 goto done;
6691 cmd = &tog_commands[0]; /* log */
6692 argc = 4;
6693 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6694 error = cmd->cmd_main(argc, cmd_argv);
6695 done:
6696 if (repo) {
6697 close_err = got_repo_close(repo);
6698 if (error == NULL)
6699 error = close_err;
6701 if (commit)
6702 got_object_commit_close(commit);
6703 if (worktree)
6704 got_worktree_close(worktree);
6705 free(id);
6706 free(commit_id_str);
6707 free(commit_id);
6708 free(cwd);
6709 free(repo_path);
6710 free(in_repo_path);
6711 if (cmd_argv) {
6712 int i;
6713 for (i = 0; i < argc; i++)
6714 free(cmd_argv[i]);
6715 free(cmd_argv);
6717 tog_free_refs();
6718 return error;
6721 int
6722 main(int argc, char *argv[])
6724 const struct got_error *error = NULL;
6725 const struct tog_cmd *cmd = NULL;
6726 int ch, hflag = 0, Vflag = 0;
6727 char **cmd_argv = NULL;
6728 static const struct option longopts[] = {
6729 { "version", no_argument, NULL, 'V' },
6730 { NULL, 0, NULL, 0}
6733 setlocale(LC_CTYPE, "");
6735 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6736 switch (ch) {
6737 case 'h':
6738 hflag = 1;
6739 break;
6740 case 'V':
6741 Vflag = 1;
6742 break;
6743 default:
6744 usage(hflag, 1);
6745 /* NOTREACHED */
6749 argc -= optind;
6750 argv += optind;
6751 optind = 1;
6752 optreset = 1;
6754 if (Vflag) {
6755 got_version_print_str();
6756 return 0;
6759 #ifndef PROFILE
6760 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6761 NULL) == -1)
6762 err(1, "pledge");
6763 #endif
6765 if (argc == 0) {
6766 if (hflag)
6767 usage(hflag, 0);
6768 /* Build an argument vector which runs a default command. */
6769 cmd = &tog_commands[0];
6770 argc = 1;
6771 cmd_argv = make_argv(argc, cmd->name);
6772 } else {
6773 size_t i;
6775 /* Did the user specify a command? */
6776 for (i = 0; i < nitems(tog_commands); i++) {
6777 if (strncmp(tog_commands[i].name, argv[0],
6778 strlen(argv[0])) == 0) {
6779 cmd = &tog_commands[i];
6780 break;
6785 if (cmd == NULL) {
6786 if (argc != 1)
6787 usage(0, 1);
6788 /* No command specified; try log with a path */
6789 error = tog_log_with_path(argc, argv);
6790 } else {
6791 if (hflag)
6792 cmd->cmd_usage();
6793 else
6794 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6797 endwin();
6798 putchar('\n');
6799 if (cmd_argv) {
6800 int i;
6801 for (i = 0; i < argc; i++)
6802 free(cmd_argv[i]);
6803 free(cmd_argv);
6806 if (error && error->code != GOT_ERR_CANCELLED)
6807 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6808 return 0;