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, nscroll = view->nlines - 1;
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 CTRL('u'):
2483 nscroll /= 2;
2484 /* FALL THROUGH */
2485 case KEY_PPAGE:
2486 case CTRL('b'):
2487 if (s->first_displayed_entry == NULL)
2488 break;
2489 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2490 s->selected = MAX(0, s->selected - nscroll - 1);
2491 else
2492 log_scroll_up(s, nscroll);
2493 select_commit(s);
2494 break;
2495 case 'j':
2496 case KEY_DOWN:
2497 case '>':
2498 case '.':
2499 case CTRL('n'):
2500 if (s->first_displayed_entry == NULL)
2501 break;
2502 if (s->selected < MIN(view->nlines - 2,
2503 s->commits.ncommits - 1))
2504 s->selected++;
2505 else {
2506 err = log_scroll_down(view, 1);
2507 if (err)
2508 break;
2510 select_commit(s);
2511 break;
2512 case 'G':
2513 case KEY_END: {
2514 /* We don't know yet how many commits, so we're forced to
2515 * traverse them all. */
2516 if (!s->thread_args.log_complete) {
2517 s->thread_args.load_all = 1;
2518 return trigger_log_thread(view, 0);
2521 s->selected = 0;
2522 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2523 for (n = 0; n < view->nlines - 1; n++) {
2524 if (entry == NULL)
2525 break;
2526 s->first_displayed_entry = entry;
2527 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2529 if (n > 0)
2530 s->selected = n - 1;
2531 select_commit(s);
2532 break;
2534 case CTRL('d'):
2535 nscroll /= 2;
2536 /* FALL THROUGH */
2537 case KEY_NPAGE:
2538 case CTRL('f'): {
2539 struct commit_queue_entry *first;
2540 first = s->first_displayed_entry;
2541 if (first == NULL)
2542 break;
2543 err = log_scroll_down(view, nscroll);
2544 if (err)
2545 break;
2546 if (first == s->first_displayed_entry &&
2547 s->selected < MIN(view->nlines - 2,
2548 s->commits.ncommits - 1)) {
2549 /* can't scroll further down */
2550 s->selected += MIN(s->last_displayed_entry->idx -
2551 s->selected_entry->idx, nscroll + 1);
2553 select_commit(s);
2554 break;
2556 case KEY_RESIZE:
2557 if (s->selected > view->nlines - 2)
2558 s->selected = view->nlines - 2;
2559 if (s->selected > s->commits.ncommits - 1)
2560 s->selected = s->commits.ncommits - 1;
2561 select_commit(s);
2562 if (s->commits.ncommits < view->nlines - 1 &&
2563 !s->thread_args.log_complete) {
2564 s->thread_args.commits_needed += (view->nlines - 1) -
2565 s->commits.ncommits;
2566 err = trigger_log_thread(view, 1);
2568 break;
2569 case KEY_ENTER:
2570 case ' ':
2571 case '\r':
2572 if (s->selected_entry == NULL)
2573 break;
2574 if (view_is_parent_view(view))
2575 begin_x = view_split_begin_x(view->begin_x);
2576 err = open_diff_view_for_commit(&diff_view, begin_x,
2577 s->selected_entry->commit, s->selected_entry->id,
2578 view, s->repo);
2579 if (err)
2580 break;
2581 view->focussed = 0;
2582 diff_view->focussed = 1;
2583 if (view_is_parent_view(view)) {
2584 err = view_close_child(view);
2585 if (err)
2586 return err;
2587 view_set_child(view, diff_view);
2588 view->focus_child = 1;
2589 } else
2590 *new_view = diff_view;
2591 break;
2592 case 't':
2593 if (s->selected_entry == NULL)
2594 break;
2595 if (view_is_parent_view(view))
2596 begin_x = view_split_begin_x(view->begin_x);
2597 err = browse_commit_tree(&tree_view, begin_x,
2598 s->selected_entry, s->in_repo_path, s->head_ref_name,
2599 s->repo);
2600 if (err)
2601 break;
2602 view->focussed = 0;
2603 tree_view->focussed = 1;
2604 if (view_is_parent_view(view)) {
2605 err = view_close_child(view);
2606 if (err)
2607 return err;
2608 view_set_child(view, tree_view);
2609 view->focus_child = 1;
2610 } else
2611 *new_view = tree_view;
2612 break;
2613 case KEY_BACKSPACE:
2614 case CTRL('l'):
2615 case 'B':
2616 if (ch == KEY_BACKSPACE &&
2617 got_path_is_root_dir(s->in_repo_path))
2618 break;
2619 err = stop_log_thread(s);
2620 if (err)
2621 return err;
2622 if (ch == KEY_BACKSPACE) {
2623 char *parent_path;
2624 err = got_path_dirname(&parent_path, s->in_repo_path);
2625 if (err)
2626 return err;
2627 free(s->in_repo_path);
2628 s->in_repo_path = parent_path;
2629 s->thread_args.in_repo_path = s->in_repo_path;
2630 } else if (ch == CTRL('l')) {
2631 struct got_object_id *start_id;
2632 err = got_repo_match_object_id(&start_id, NULL,
2633 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2634 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2635 if (err)
2636 return err;
2637 free(s->start_id);
2638 s->start_id = start_id;
2639 s->thread_args.start_id = s->start_id;
2640 } else /* 'B' */
2641 s->log_branches = !s->log_branches;
2643 err = got_repo_open(&s->thread_args.repo,
2644 got_repo_get_path(s->repo), NULL);
2645 if (err)
2646 return err;
2647 tog_free_refs();
2648 err = tog_load_refs(s->repo, 0);
2649 if (err)
2650 return err;
2651 err = got_commit_graph_open(&s->thread_args.graph,
2652 s->in_repo_path, !s->log_branches);
2653 if (err)
2654 return err;
2655 err = got_commit_graph_iter_start(s->thread_args.graph,
2656 s->start_id, s->repo, NULL, NULL);
2657 if (err)
2658 return err;
2659 free_commits(&s->commits);
2660 s->first_displayed_entry = NULL;
2661 s->last_displayed_entry = NULL;
2662 s->selected_entry = NULL;
2663 s->selected = 0;
2664 s->thread_args.log_complete = 0;
2665 s->quit = 0;
2666 s->thread_args.commits_needed = view->nlines;
2667 break;
2668 case 'r':
2669 if (view_is_parent_view(view))
2670 begin_x = view_split_begin_x(view->begin_x);
2671 ref_view = view_open(view->nlines, view->ncols,
2672 view->begin_y, begin_x, TOG_VIEW_REF);
2673 if (ref_view == NULL)
2674 return got_error_from_errno("view_open");
2675 err = open_ref_view(ref_view, s->repo);
2676 if (err) {
2677 view_close(ref_view);
2678 return err;
2680 view->focussed = 0;
2681 ref_view->focussed = 1;
2682 if (view_is_parent_view(view)) {
2683 err = view_close_child(view);
2684 if (err)
2685 return err;
2686 view_set_child(view, ref_view);
2687 view->focus_child = 1;
2688 } else
2689 *new_view = ref_view;
2690 break;
2691 default:
2692 break;
2695 return err;
2698 static const struct got_error *
2699 apply_unveil(const char *repo_path, const char *worktree_path)
2701 const struct got_error *error;
2703 #ifdef PROFILE
2704 if (unveil("gmon.out", "rwc") != 0)
2705 return got_error_from_errno2("unveil", "gmon.out");
2706 #endif
2707 if (repo_path && unveil(repo_path, "r") != 0)
2708 return got_error_from_errno2("unveil", repo_path);
2710 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2711 return got_error_from_errno2("unveil", worktree_path);
2713 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2714 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2716 error = got_privsep_unveil_exec_helpers();
2717 if (error != NULL)
2718 return error;
2720 if (unveil(NULL, NULL) != 0)
2721 return got_error_from_errno("unveil");
2723 return NULL;
2726 static void
2727 init_curses(void)
2730 * Override default signal handlers before starting ncurses.
2731 * This should prevent ncurses from installing its own
2732 * broken cleanup() signal handler.
2734 signal(SIGWINCH, tog_sigwinch);
2735 signal(SIGPIPE, tog_sigpipe);
2736 signal(SIGCONT, tog_sigcont);
2737 signal(SIGINT, tog_sigint);
2738 signal(SIGTERM, tog_sigterm);
2740 initscr();
2741 cbreak();
2742 halfdelay(1); /* Do fast refresh while initial view is loading. */
2743 noecho();
2744 nonl();
2745 intrflush(stdscr, FALSE);
2746 keypad(stdscr, TRUE);
2747 curs_set(0);
2748 if (getenv("TOG_COLORS") != NULL) {
2749 start_color();
2750 use_default_colors();
2754 static const struct got_error *
2755 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2756 struct got_repository *repo, struct got_worktree *worktree)
2758 const struct got_error *err = NULL;
2760 if (argc == 0) {
2761 *in_repo_path = strdup("/");
2762 if (*in_repo_path == NULL)
2763 return got_error_from_errno("strdup");
2764 return NULL;
2767 if (worktree) {
2768 const char *prefix = got_worktree_get_path_prefix(worktree);
2769 char *p;
2771 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2772 if (err)
2773 return err;
2774 if (asprintf(in_repo_path, "%s%s%s", prefix,
2775 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2776 p) == -1) {
2777 err = got_error_from_errno("asprintf");
2778 *in_repo_path = NULL;
2780 free(p);
2781 } else
2782 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2784 return err;
2787 static const struct got_error *
2788 cmd_log(int argc, char *argv[])
2790 const struct got_error *error;
2791 struct got_repository *repo = NULL;
2792 struct got_worktree *worktree = NULL;
2793 struct got_object_id *start_id = NULL;
2794 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2795 char *start_commit = NULL, *label = NULL;
2796 struct got_reference *ref = NULL;
2797 const char *head_ref_name = NULL;
2798 int ch, log_branches = 0;
2799 struct tog_view *view;
2801 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2802 switch (ch) {
2803 case 'b':
2804 log_branches = 1;
2805 break;
2806 case 'c':
2807 start_commit = optarg;
2808 break;
2809 case 'r':
2810 repo_path = realpath(optarg, NULL);
2811 if (repo_path == NULL)
2812 return got_error_from_errno2("realpath",
2813 optarg);
2814 break;
2815 default:
2816 usage_log();
2817 /* NOTREACHED */
2821 argc -= optind;
2822 argv += optind;
2824 if (argc > 1)
2825 usage_log();
2827 if (repo_path == NULL) {
2828 cwd = getcwd(NULL, 0);
2829 if (cwd == NULL)
2830 return got_error_from_errno("getcwd");
2831 error = got_worktree_open(&worktree, cwd);
2832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2833 goto done;
2834 if (worktree)
2835 repo_path =
2836 strdup(got_worktree_get_repo_path(worktree));
2837 else
2838 repo_path = strdup(cwd);
2839 if (repo_path == NULL) {
2840 error = got_error_from_errno("strdup");
2841 goto done;
2845 error = got_repo_open(&repo, repo_path, NULL);
2846 if (error != NULL)
2847 goto done;
2849 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2850 repo, worktree);
2851 if (error)
2852 goto done;
2854 init_curses();
2856 error = apply_unveil(got_repo_get_path(repo),
2857 worktree ? got_worktree_get_root_path(worktree) : NULL);
2858 if (error)
2859 goto done;
2861 /* already loaded by tog_log_with_path()? */
2862 if (TAILQ_EMPTY(&tog_refs)) {
2863 error = tog_load_refs(repo, 0);
2864 if (error)
2865 goto done;
2868 if (start_commit == NULL) {
2869 error = got_repo_match_object_id(&start_id, &label,
2870 worktree ? got_worktree_get_head_ref_name(worktree) :
2871 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2872 if (error)
2873 goto done;
2874 head_ref_name = label;
2875 } else {
2876 error = got_ref_open(&ref, repo, start_commit, 0);
2877 if (error == NULL)
2878 head_ref_name = got_ref_get_name(ref);
2879 else if (error->code != GOT_ERR_NOT_REF)
2880 goto done;
2881 error = got_repo_match_object_id(&start_id, NULL,
2882 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2883 if (error)
2884 goto done;
2887 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2888 if (view == NULL) {
2889 error = got_error_from_errno("view_open");
2890 goto done;
2892 error = open_log_view(view, start_id, repo, head_ref_name,
2893 in_repo_path, log_branches);
2894 if (error)
2895 goto done;
2896 if (worktree) {
2897 /* Release work tree lock. */
2898 got_worktree_close(worktree);
2899 worktree = NULL;
2901 error = view_loop(view);
2902 done:
2903 free(in_repo_path);
2904 free(repo_path);
2905 free(cwd);
2906 free(start_id);
2907 free(label);
2908 if (ref)
2909 got_ref_close(ref);
2910 if (repo) {
2911 const struct got_error *close_err = got_repo_close(repo);
2912 if (error == NULL)
2913 error = close_err;
2915 if (worktree)
2916 got_worktree_close(worktree);
2917 tog_free_refs();
2918 return error;
2921 __dead static void
2922 usage_diff(void)
2924 endwin();
2925 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2926 "[-w] object1 object2\n", getprogname());
2927 exit(1);
2930 static int
2931 match_line(const char *line, regex_t *regex, size_t nmatch,
2932 regmatch_t *regmatch)
2934 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2937 struct tog_color *
2938 match_color(struct tog_colors *colors, const char *line)
2940 struct tog_color *tc = NULL;
2942 STAILQ_FOREACH(tc, colors, entry) {
2943 if (match_line(line, &tc->regex, 0, NULL))
2944 return tc;
2947 return NULL;
2950 static const struct got_error *
2951 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2952 WINDOW *window, regmatch_t *regmatch)
2954 const struct got_error *err = NULL;
2955 wchar_t *wline;
2956 int width;
2957 char *s;
2959 *wtotal = 0;
2961 s = strndup(line, regmatch->rm_so);
2962 if (s == NULL)
2963 return got_error_from_errno("strndup");
2965 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2966 if (err) {
2967 free(s);
2968 return err;
2970 waddwstr(window, wline);
2971 free(wline);
2972 free(s);
2973 wlimit -= width;
2974 *wtotal += width;
2976 if (wlimit > 0) {
2977 s = strndup(line + regmatch->rm_so,
2978 regmatch->rm_eo - regmatch->rm_so);
2979 if (s == NULL) {
2980 err = got_error_from_errno("strndup");
2981 free(s);
2982 return err;
2984 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2985 if (err) {
2986 free(s);
2987 return err;
2989 wattr_on(window, A_STANDOUT, NULL);
2990 waddwstr(window, wline);
2991 wattr_off(window, A_STANDOUT, NULL);
2992 free(wline);
2993 free(s);
2994 wlimit -= width;
2995 *wtotal += width;
2998 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2999 err = format_line(&wline, &width,
3000 line + regmatch->rm_eo, wlimit, col_tab_align);
3001 if (err)
3002 return err;
3003 waddwstr(window, wline);
3004 free(wline);
3005 *wtotal += width;
3008 return NULL;
3011 static const struct got_error *
3012 draw_file(struct tog_view *view, const char *header)
3014 struct tog_diff_view_state *s = &view->state.diff;
3015 regmatch_t *regmatch = &view->regmatch;
3016 const struct got_error *err;
3017 int nprinted = 0;
3018 char *line;
3019 size_t linesize = 0;
3020 ssize_t linelen;
3021 struct tog_color *tc;
3022 wchar_t *wline;
3023 int width;
3024 int max_lines = view->nlines;
3025 int nlines = s->nlines;
3026 off_t line_offset;
3028 line_offset = s->line_offsets[s->first_displayed_line - 1];
3029 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3030 return got_error_from_errno("fseek");
3032 werase(view->window);
3034 if (header) {
3035 if (asprintf(&line, "[%d/%d] %s",
3036 s->first_displayed_line - 1 + s->selected_line, nlines,
3037 header) == -1)
3038 return got_error_from_errno("asprintf");
3039 err = format_line(&wline, &width, line, view->ncols, 0);
3040 free(line);
3041 if (err)
3042 return err;
3044 if (view_needs_focus_indication(view))
3045 wstandout(view->window);
3046 waddwstr(view->window, wline);
3047 free(wline);
3048 wline = NULL;
3049 if (view_needs_focus_indication(view))
3050 wstandend(view->window);
3051 if (width <= view->ncols - 1)
3052 waddch(view->window, '\n');
3054 if (max_lines <= 1)
3055 return NULL;
3056 max_lines--;
3059 s->eof = 0;
3060 line = NULL;
3061 while (max_lines > 0 && nprinted < max_lines) {
3062 linelen = getline(&line, &linesize, s->f);
3063 if (linelen == -1) {
3064 if (feof(s->f)) {
3065 s->eof = 1;
3066 break;
3068 free(line);
3069 return got_ferror(s->f, GOT_ERR_IO);
3072 tc = match_color(&s->colors, line);
3073 if (tc)
3074 wattr_on(view->window,
3075 COLOR_PAIR(tc->colorpair), NULL);
3076 if (s->first_displayed_line + nprinted == s->matched_line &&
3077 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3078 err = add_matched_line(&width, line, view->ncols, 0,
3079 view->window, regmatch);
3080 if (err) {
3081 free(line);
3082 return err;
3084 } else {
3085 err = format_line(&wline, &width, line, view->ncols, 0);
3086 if (err) {
3087 free(line);
3088 return err;
3090 waddwstr(view->window, wline);
3091 free(wline);
3092 wline = NULL;
3094 if (tc)
3095 wattr_off(view->window,
3096 COLOR_PAIR(tc->colorpair), NULL);
3097 if (width <= view->ncols - 1)
3098 waddch(view->window, '\n');
3099 nprinted++;
3101 free(line);
3102 if (nprinted >= 1)
3103 s->last_displayed_line = s->first_displayed_line +
3104 (nprinted - 1);
3105 else
3106 s->last_displayed_line = s->first_displayed_line;
3108 view_vborder(view);
3110 if (s->eof) {
3111 while (nprinted < view->nlines) {
3112 waddch(view->window, '\n');
3113 nprinted++;
3116 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3117 if (err) {
3118 return err;
3121 wstandout(view->window);
3122 waddwstr(view->window, wline);
3123 free(wline);
3124 wline = NULL;
3125 wstandend(view->window);
3128 return NULL;
3131 static char *
3132 get_datestr(time_t *time, char *datebuf)
3134 struct tm mytm, *tm;
3135 char *p, *s;
3137 tm = gmtime_r(time, &mytm);
3138 if (tm == NULL)
3139 return NULL;
3140 s = asctime_r(tm, datebuf);
3141 if (s == NULL)
3142 return NULL;
3143 p = strchr(s, '\n');
3144 if (p)
3145 *p = '\0';
3146 return s;
3149 static const struct got_error *
3150 get_changed_paths(struct got_pathlist_head *paths,
3151 struct got_commit_object *commit, struct got_repository *repo)
3153 const struct got_error *err = NULL;
3154 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3155 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3156 struct got_object_qid *qid;
3158 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3159 if (qid != NULL) {
3160 struct got_commit_object *pcommit;
3161 err = got_object_open_as_commit(&pcommit, repo,
3162 &qid->id);
3163 if (err)
3164 return err;
3166 tree_id1 = got_object_id_dup(
3167 got_object_commit_get_tree_id(pcommit));
3168 if (tree_id1 == NULL) {
3169 got_object_commit_close(pcommit);
3170 return got_error_from_errno("got_object_id_dup");
3172 got_object_commit_close(pcommit);
3176 if (tree_id1) {
3177 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3178 if (err)
3179 goto done;
3182 tree_id2 = got_object_commit_get_tree_id(commit);
3183 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3184 if (err)
3185 goto done;
3187 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3188 got_diff_tree_collect_changed_paths, paths, 0);
3189 done:
3190 if (tree1)
3191 got_object_tree_close(tree1);
3192 if (tree2)
3193 got_object_tree_close(tree2);
3194 free(tree_id1);
3195 return err;
3198 static const struct got_error *
3199 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3201 off_t *p;
3203 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3204 if (p == NULL)
3205 return got_error_from_errno("reallocarray");
3206 *line_offsets = p;
3207 (*line_offsets)[*nlines] = off;
3208 (*nlines)++;
3209 return NULL;
3212 static const struct got_error *
3213 write_commit_info(off_t **line_offsets, size_t *nlines,
3214 struct got_object_id *commit_id, struct got_reflist_head *refs,
3215 struct got_repository *repo, FILE *outfile)
3217 const struct got_error *err = NULL;
3218 char datebuf[26], *datestr;
3219 struct got_commit_object *commit;
3220 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3221 time_t committer_time;
3222 const char *author, *committer;
3223 char *refs_str = NULL;
3224 struct got_pathlist_head changed_paths;
3225 struct got_pathlist_entry *pe;
3226 off_t outoff = 0;
3227 int n;
3229 TAILQ_INIT(&changed_paths);
3231 if (refs) {
3232 err = build_refs_str(&refs_str, refs, commit_id, repo);
3233 if (err)
3234 return err;
3237 err = got_object_open_as_commit(&commit, repo, commit_id);
3238 if (err)
3239 return err;
3241 err = got_object_id_str(&id_str, commit_id);
3242 if (err) {
3243 err = got_error_from_errno("got_object_id_str");
3244 goto done;
3247 err = add_line_offset(line_offsets, nlines, 0);
3248 if (err)
3249 goto done;
3251 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3252 refs_str ? refs_str : "", refs_str ? ")" : "");
3253 if (n < 0) {
3254 err = got_error_from_errno("fprintf");
3255 goto done;
3257 outoff += n;
3258 err = add_line_offset(line_offsets, nlines, outoff);
3259 if (err)
3260 goto done;
3262 n = fprintf(outfile, "from: %s\n",
3263 got_object_commit_get_author(commit));
3264 if (n < 0) {
3265 err = got_error_from_errno("fprintf");
3266 goto done;
3268 outoff += n;
3269 err = add_line_offset(line_offsets, nlines, outoff);
3270 if (err)
3271 goto done;
3273 committer_time = got_object_commit_get_committer_time(commit);
3274 datestr = get_datestr(&committer_time, datebuf);
3275 if (datestr) {
3276 n = fprintf(outfile, "date: %s UTC\n", datestr);
3277 if (n < 0) {
3278 err = got_error_from_errno("fprintf");
3279 goto done;
3281 outoff += n;
3282 err = add_line_offset(line_offsets, nlines, outoff);
3283 if (err)
3284 goto done;
3286 author = got_object_commit_get_author(commit);
3287 committer = got_object_commit_get_committer(commit);
3288 if (strcmp(author, committer) != 0) {
3289 n = fprintf(outfile, "via: %s\n", committer);
3290 if (n < 0) {
3291 err = got_error_from_errno("fprintf");
3292 goto done;
3294 outoff += n;
3295 err = add_line_offset(line_offsets, nlines, outoff);
3296 if (err)
3297 goto done;
3299 if (got_object_commit_get_nparents(commit) > 1) {
3300 const struct got_object_id_queue *parent_ids;
3301 struct got_object_qid *qid;
3302 int pn = 1;
3303 parent_ids = got_object_commit_get_parent_ids(commit);
3304 STAILQ_FOREACH(qid, parent_ids, entry) {
3305 err = got_object_id_str(&id_str, &qid->id);
3306 if (err)
3307 goto done;
3308 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3309 if (n < 0) {
3310 err = got_error_from_errno("fprintf");
3311 goto done;
3313 outoff += n;
3314 err = add_line_offset(line_offsets, nlines, outoff);
3315 if (err)
3316 goto done;
3317 free(id_str);
3318 id_str = NULL;
3322 err = got_object_commit_get_logmsg(&logmsg, commit);
3323 if (err)
3324 goto done;
3325 s = logmsg;
3326 while ((line = strsep(&s, "\n")) != NULL) {
3327 n = fprintf(outfile, "%s\n", line);
3328 if (n < 0) {
3329 err = got_error_from_errno("fprintf");
3330 goto done;
3332 outoff += n;
3333 err = add_line_offset(line_offsets, nlines, outoff);
3334 if (err)
3335 goto done;
3338 err = get_changed_paths(&changed_paths, commit, repo);
3339 if (err)
3340 goto done;
3341 TAILQ_FOREACH(pe, &changed_paths, entry) {
3342 struct got_diff_changed_path *cp = pe->data;
3343 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3344 if (n < 0) {
3345 err = got_error_from_errno("fprintf");
3346 goto done;
3348 outoff += n;
3349 err = add_line_offset(line_offsets, nlines, outoff);
3350 if (err)
3351 goto done;
3352 free((char *)pe->path);
3353 free(pe->data);
3356 fputc('\n', outfile);
3357 outoff++;
3358 err = add_line_offset(line_offsets, nlines, outoff);
3359 done:
3360 got_pathlist_free(&changed_paths);
3361 free(id_str);
3362 free(logmsg);
3363 free(refs_str);
3364 got_object_commit_close(commit);
3365 if (err) {
3366 free(*line_offsets);
3367 *line_offsets = NULL;
3368 *nlines = 0;
3370 return err;
3373 static const struct got_error *
3374 create_diff(struct tog_diff_view_state *s)
3376 const struct got_error *err = NULL;
3377 FILE *f = NULL;
3378 int obj_type;
3380 free(s->line_offsets);
3381 s->line_offsets = malloc(sizeof(off_t));
3382 if (s->line_offsets == NULL)
3383 return got_error_from_errno("malloc");
3384 s->nlines = 0;
3386 f = got_opentemp();
3387 if (f == NULL) {
3388 err = got_error_from_errno("got_opentemp");
3389 goto done;
3391 if (s->f && fclose(s->f) == EOF) {
3392 err = got_error_from_errno("fclose");
3393 goto done;
3395 s->f = f;
3397 if (s->id1)
3398 err = got_object_get_type(&obj_type, s->repo, s->id1);
3399 else
3400 err = got_object_get_type(&obj_type, s->repo, s->id2);
3401 if (err)
3402 goto done;
3404 switch (obj_type) {
3405 case GOT_OBJ_TYPE_BLOB:
3406 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3407 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3408 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3409 s->repo, s->f);
3410 break;
3411 case GOT_OBJ_TYPE_TREE:
3412 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3413 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3414 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3415 break;
3416 case GOT_OBJ_TYPE_COMMIT: {
3417 const struct got_object_id_queue *parent_ids;
3418 struct got_object_qid *pid;
3419 struct got_commit_object *commit2;
3420 struct got_reflist_head *refs;
3422 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3423 if (err)
3424 goto done;
3425 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3426 /* Show commit info if we're diffing to a parent/root commit. */
3427 if (s->id1 == NULL) {
3428 err = write_commit_info(&s->line_offsets, &s->nlines,
3429 s->id2, refs, s->repo, s->f);
3430 if (err)
3431 goto done;
3432 } else {
3433 parent_ids = got_object_commit_get_parent_ids(commit2);
3434 STAILQ_FOREACH(pid, parent_ids, entry) {
3435 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3436 err = write_commit_info(
3437 &s->line_offsets, &s->nlines,
3438 s->id2, refs, s->repo, s->f);
3439 if (err)
3440 goto done;
3441 break;
3445 got_object_commit_close(commit2);
3447 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3448 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3449 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3450 break;
3452 default:
3453 err = got_error(GOT_ERR_OBJ_TYPE);
3454 break;
3456 if (err)
3457 goto done;
3458 done:
3459 if (s->f && fflush(s->f) != 0 && err == NULL)
3460 err = got_error_from_errno("fflush");
3461 return err;
3464 static void
3465 diff_view_indicate_progress(struct tog_view *view)
3467 mvwaddstr(view->window, 0, 0, "diffing...");
3468 update_panels();
3469 doupdate();
3472 static const struct got_error *
3473 search_start_diff_view(struct tog_view *view)
3475 struct tog_diff_view_state *s = &view->state.diff;
3477 s->matched_line = 0;
3478 return NULL;
3481 static const struct got_error *
3482 search_next_diff_view(struct tog_view *view)
3484 struct tog_diff_view_state *s = &view->state.diff;
3485 int lineno;
3486 char *line = NULL;
3487 size_t linesize = 0;
3488 ssize_t linelen;
3490 if (!view->searching) {
3491 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3492 return NULL;
3495 if (s->matched_line) {
3496 if (view->searching == TOG_SEARCH_FORWARD)
3497 lineno = s->matched_line + 1;
3498 else
3499 lineno = s->matched_line - 1;
3500 } else
3501 lineno = s->first_displayed_line;
3503 while (1) {
3504 off_t offset;
3506 if (lineno <= 0 || lineno > s->nlines) {
3507 if (s->matched_line == 0) {
3508 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3509 break;
3512 if (view->searching == TOG_SEARCH_FORWARD)
3513 lineno = 1;
3514 else
3515 lineno = s->nlines;
3518 offset = s->line_offsets[lineno - 1];
3519 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3520 free(line);
3521 return got_error_from_errno("fseeko");
3523 linelen = getline(&line, &linesize, s->f);
3524 if (linelen != -1 &&
3525 match_line(line, &view->regex, 1, &view->regmatch)) {
3526 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3527 s->matched_line = lineno;
3528 break;
3530 if (view->searching == TOG_SEARCH_FORWARD)
3531 lineno++;
3532 else
3533 lineno--;
3535 free(line);
3537 if (s->matched_line) {
3538 s->first_displayed_line = s->matched_line;
3539 s->selected_line = 1;
3542 return NULL;
3545 static const struct got_error *
3546 close_diff_view(struct tog_view *view)
3548 const struct got_error *err = NULL;
3549 struct tog_diff_view_state *s = &view->state.diff;
3551 free(s->id1);
3552 s->id1 = NULL;
3553 free(s->id2);
3554 s->id2 = NULL;
3555 if (s->f && fclose(s->f) == EOF)
3556 err = got_error_from_errno("fclose");
3557 s->f = NULL;
3558 if (s->f1 && fclose(s->f1) == EOF)
3559 err = got_error_from_errno("fclose");
3560 s->f1 = NULL;
3561 if (s->f2 && fclose(s->f2) == EOF)
3562 err = got_error_from_errno("fclose");
3563 s->f2 = NULL;
3564 free_colors(&s->colors);
3565 free(s->line_offsets);
3566 s->line_offsets = NULL;
3567 s->nlines = 0;
3568 return err;
3571 static const struct got_error *
3572 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3573 struct got_object_id *id2, const char *label1, const char *label2,
3574 int diff_context, int ignore_whitespace, int force_text_diff,
3575 struct tog_view *log_view, struct got_repository *repo)
3577 const struct got_error *err;
3578 struct tog_diff_view_state *s = &view->state.diff;
3580 memset(s, 0, sizeof(*s));
3582 if (id1 != NULL && id2 != NULL) {
3583 int type1, type2;
3584 err = got_object_get_type(&type1, repo, id1);
3585 if (err)
3586 return err;
3587 err = got_object_get_type(&type2, repo, id2);
3588 if (err)
3589 return err;
3591 if (type1 != type2)
3592 return got_error(GOT_ERR_OBJ_TYPE);
3594 s->first_displayed_line = 1;
3595 s->last_displayed_line = view->nlines;
3596 s->selected_line = 1;
3597 s->repo = repo;
3598 s->id1 = id1;
3599 s->id2 = id2;
3600 s->label1 = label1;
3601 s->label2 = label2;
3603 if (id1) {
3604 s->id1 = got_object_id_dup(id1);
3605 if (s->id1 == NULL)
3606 return got_error_from_errno("got_object_id_dup");
3607 s->f1 = got_opentemp();
3608 if (s->f1 == NULL) {
3609 err = got_error_from_errno("got_opentemp");
3610 goto done;
3612 } else
3613 s->id1 = NULL;
3615 s->id2 = got_object_id_dup(id2);
3616 if (s->id2 == NULL) {
3617 err = got_error_from_errno("got_object_id_dup");
3618 goto done;
3621 s->f2 = got_opentemp();
3622 if (s->f2 == NULL) {
3623 err = got_error_from_errno("got_opentemp");
3624 goto done;
3627 s->first_displayed_line = 1;
3628 s->last_displayed_line = view->nlines;
3629 s->diff_context = diff_context;
3630 s->ignore_whitespace = ignore_whitespace;
3631 s->force_text_diff = force_text_diff;
3632 s->log_view = log_view;
3633 s->repo = repo;
3635 STAILQ_INIT(&s->colors);
3636 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3637 err = add_color(&s->colors,
3638 "^-", TOG_COLOR_DIFF_MINUS,
3639 get_color_value("TOG_COLOR_DIFF_MINUS"));
3640 if (err)
3641 goto done;
3642 err = add_color(&s->colors, "^\\+",
3643 TOG_COLOR_DIFF_PLUS,
3644 get_color_value("TOG_COLOR_DIFF_PLUS"));
3645 if (err)
3646 goto done;
3647 err = add_color(&s->colors,
3648 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3649 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3650 if (err)
3651 goto done;
3653 err = add_color(&s->colors,
3654 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3655 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3656 get_color_value("TOG_COLOR_DIFF_META"));
3657 if (err)
3658 goto done;
3660 err = add_color(&s->colors,
3661 "^(from|via): ", TOG_COLOR_AUTHOR,
3662 get_color_value("TOG_COLOR_AUTHOR"));
3663 if (err)
3664 goto done;
3666 err = add_color(&s->colors,
3667 "^date: ", TOG_COLOR_DATE,
3668 get_color_value("TOG_COLOR_DATE"));
3669 if (err)
3670 goto done;
3673 if (log_view && view_is_splitscreen(view))
3674 show_log_view(log_view); /* draw vborder */
3675 diff_view_indicate_progress(view);
3677 err = create_diff(s);
3679 view->show = show_diff_view;
3680 view->input = input_diff_view;
3681 view->close = close_diff_view;
3682 view->search_start = search_start_diff_view;
3683 view->search_next = search_next_diff_view;
3684 done:
3685 if (err)
3686 close_diff_view(view);
3687 return err;
3690 static const struct got_error *
3691 show_diff_view(struct tog_view *view)
3693 const struct got_error *err;
3694 struct tog_diff_view_state *s = &view->state.diff;
3695 char *id_str1 = NULL, *id_str2, *header;
3696 const char *label1, *label2;
3698 if (s->id1) {
3699 err = got_object_id_str(&id_str1, s->id1);
3700 if (err)
3701 return err;
3702 label1 = s->label1 ? : id_str1;
3703 } else
3704 label1 = "/dev/null";
3706 err = got_object_id_str(&id_str2, s->id2);
3707 if (err)
3708 return err;
3709 label2 = s->label2 ? : id_str2;
3711 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3712 err = got_error_from_errno("asprintf");
3713 free(id_str1);
3714 free(id_str2);
3715 return err;
3717 free(id_str1);
3718 free(id_str2);
3720 err = draw_file(view, header);
3721 free(header);
3722 return err;
3725 static const struct got_error *
3726 set_selected_commit(struct tog_diff_view_state *s,
3727 struct commit_queue_entry *entry)
3729 const struct got_error *err;
3730 const struct got_object_id_queue *parent_ids;
3731 struct got_commit_object *selected_commit;
3732 struct got_object_qid *pid;
3734 free(s->id2);
3735 s->id2 = got_object_id_dup(entry->id);
3736 if (s->id2 == NULL)
3737 return got_error_from_errno("got_object_id_dup");
3739 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3740 if (err)
3741 return err;
3742 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3743 free(s->id1);
3744 pid = STAILQ_FIRST(parent_ids);
3745 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3746 got_object_commit_close(selected_commit);
3747 return NULL;
3750 static const struct got_error *
3751 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3753 const struct got_error *err = NULL;
3754 struct tog_diff_view_state *s = &view->state.diff;
3755 struct tog_log_view_state *ls;
3756 struct commit_queue_entry *old_selected_entry;
3757 char *line = NULL;
3758 size_t linesize = 0;
3759 ssize_t linelen;
3760 int i, nscroll = view->nlines - 1;
3762 switch (ch) {
3763 case 'a':
3764 case 'w':
3765 if (ch == 'a')
3766 s->force_text_diff = !s->force_text_diff;
3767 if (ch == 'w')
3768 s->ignore_whitespace = !s->ignore_whitespace;
3769 wclear(view->window);
3770 s->first_displayed_line = 1;
3771 s->last_displayed_line = view->nlines;
3772 s->matched_line = 0;
3773 diff_view_indicate_progress(view);
3774 err = create_diff(s);
3775 break;
3776 case 'g':
3777 case KEY_HOME:
3778 s->first_displayed_line = 1;
3779 break;
3780 case 'G':
3781 case KEY_END:
3782 if (s->eof)
3783 break;
3785 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3786 s->eof = 1;
3787 break;
3788 case 'k':
3789 case KEY_UP:
3790 case CTRL('p'):
3791 if (s->first_displayed_line > 1)
3792 s->first_displayed_line--;
3793 break;
3794 case CTRL('u'):
3795 nscroll /= 2;
3796 /* FALL THROUGH */
3797 case KEY_PPAGE:
3798 case CTRL('b'):
3799 if (s->first_displayed_line == 1)
3800 break;
3801 i = 0;
3802 while (i++ < nscroll && s->first_displayed_line > 1)
3803 s->first_displayed_line--;
3804 break;
3805 case 'j':
3806 case KEY_DOWN:
3807 case CTRL('n'):
3808 if (!s->eof)
3809 s->first_displayed_line++;
3810 break;
3811 case CTRL('d'):
3812 nscroll /= 2;
3813 /* FALL THROUGH */
3814 case KEY_NPAGE:
3815 case CTRL('f'):
3816 case ' ':
3817 if (s->eof)
3818 break;
3819 i = 0;
3820 while (!s->eof && i++ < nscroll) {
3821 linelen = getline(&line, &linesize, s->f);
3822 s->first_displayed_line++;
3823 if (linelen == -1) {
3824 if (feof(s->f)) {
3825 s->eof = 1;
3826 } else
3827 err = got_ferror(s->f, GOT_ERR_IO);
3828 break;
3831 free(line);
3832 break;
3833 case '[':
3834 if (s->diff_context > 0) {
3835 s->diff_context--;
3836 s->matched_line = 0;
3837 diff_view_indicate_progress(view);
3838 err = create_diff(s);
3839 if (s->first_displayed_line + view->nlines - 1 >
3840 s->nlines) {
3841 s->first_displayed_line = 1;
3842 s->last_displayed_line = view->nlines;
3845 break;
3846 case ']':
3847 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3848 s->diff_context++;
3849 s->matched_line = 0;
3850 diff_view_indicate_progress(view);
3851 err = create_diff(s);
3853 break;
3854 case '<':
3855 case ',':
3856 if (s->log_view == NULL)
3857 break;
3858 ls = &s->log_view->state.log;
3859 old_selected_entry = ls->selected_entry;
3861 err = input_log_view(NULL, s->log_view, KEY_UP);
3862 if (err)
3863 break;
3865 if (old_selected_entry == ls->selected_entry)
3866 break;
3868 err = set_selected_commit(s, ls->selected_entry);
3869 if (err)
3870 break;
3872 s->first_displayed_line = 1;
3873 s->last_displayed_line = view->nlines;
3874 s->matched_line = 0;
3876 diff_view_indicate_progress(view);
3877 err = create_diff(s);
3878 break;
3879 case '>':
3880 case '.':
3881 if (s->log_view == NULL)
3882 break;
3883 ls = &s->log_view->state.log;
3884 old_selected_entry = ls->selected_entry;
3886 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3887 if (err)
3888 break;
3890 if (old_selected_entry == ls->selected_entry)
3891 break;
3893 err = set_selected_commit(s, ls->selected_entry);
3894 if (err)
3895 break;
3897 s->first_displayed_line = 1;
3898 s->last_displayed_line = view->nlines;
3899 s->matched_line = 0;
3901 diff_view_indicate_progress(view);
3902 err = create_diff(s);
3903 break;
3904 default:
3905 break;
3908 return err;
3911 static const struct got_error *
3912 cmd_diff(int argc, char *argv[])
3914 const struct got_error *error = NULL;
3915 struct got_repository *repo = NULL;
3916 struct got_worktree *worktree = NULL;
3917 struct got_object_id *id1 = NULL, *id2 = NULL;
3918 char *repo_path = NULL, *cwd = NULL;
3919 char *id_str1 = NULL, *id_str2 = NULL;
3920 char *label1 = NULL, *label2 = NULL;
3921 int diff_context = 3, ignore_whitespace = 0;
3922 int ch, force_text_diff = 0;
3923 const char *errstr;
3924 struct tog_view *view;
3926 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3927 switch (ch) {
3928 case 'a':
3929 force_text_diff = 1;
3930 break;
3931 case 'C':
3932 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3933 &errstr);
3934 if (errstr != NULL)
3935 errx(1, "number of context lines is %s: %s",
3936 errstr, errstr);
3937 break;
3938 case 'r':
3939 repo_path = realpath(optarg, NULL);
3940 if (repo_path == NULL)
3941 return got_error_from_errno2("realpath",
3942 optarg);
3943 got_path_strip_trailing_slashes(repo_path);
3944 break;
3945 case 'w':
3946 ignore_whitespace = 1;
3947 break;
3948 default:
3949 usage_diff();
3950 /* NOTREACHED */
3954 argc -= optind;
3955 argv += optind;
3957 if (argc == 0) {
3958 usage_diff(); /* TODO show local worktree changes */
3959 } else if (argc == 2) {
3960 id_str1 = argv[0];
3961 id_str2 = argv[1];
3962 } else
3963 usage_diff();
3965 if (repo_path == NULL) {
3966 cwd = getcwd(NULL, 0);
3967 if (cwd == NULL)
3968 return got_error_from_errno("getcwd");
3969 error = got_worktree_open(&worktree, cwd);
3970 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3971 goto done;
3972 if (worktree)
3973 repo_path =
3974 strdup(got_worktree_get_repo_path(worktree));
3975 else
3976 repo_path = strdup(cwd);
3977 if (repo_path == NULL) {
3978 error = got_error_from_errno("strdup");
3979 goto done;
3983 error = got_repo_open(&repo, repo_path, NULL);
3984 if (error)
3985 goto done;
3987 init_curses();
3989 error = apply_unveil(got_repo_get_path(repo), NULL);
3990 if (error)
3991 goto done;
3993 error = tog_load_refs(repo, 0);
3994 if (error)
3995 goto done;
3997 error = got_repo_match_object_id(&id1, &label1, id_str1,
3998 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3999 if (error)
4000 goto done;
4002 error = got_repo_match_object_id(&id2, &label2, id_str2,
4003 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4004 if (error)
4005 goto done;
4007 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4008 if (view == NULL) {
4009 error = got_error_from_errno("view_open");
4010 goto done;
4012 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4013 ignore_whitespace, force_text_diff, NULL, repo);
4014 if (error)
4015 goto done;
4016 error = view_loop(view);
4017 done:
4018 free(label1);
4019 free(label2);
4020 free(repo_path);
4021 free(cwd);
4022 if (repo) {
4023 const struct got_error *close_err = got_repo_close(repo);
4024 if (error == NULL)
4025 error = close_err;
4027 if (worktree)
4028 got_worktree_close(worktree);
4029 tog_free_refs();
4030 return error;
4033 __dead static void
4034 usage_blame(void)
4036 endwin();
4037 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4038 getprogname());
4039 exit(1);
4042 struct tog_blame_line {
4043 int annotated;
4044 struct got_object_id *id;
4047 static const struct got_error *
4048 draw_blame(struct tog_view *view)
4050 struct tog_blame_view_state *s = &view->state.blame;
4051 struct tog_blame *blame = &s->blame;
4052 regmatch_t *regmatch = &view->regmatch;
4053 const struct got_error *err;
4054 int lineno = 0, nprinted = 0;
4055 char *line = NULL;
4056 size_t linesize = 0;
4057 ssize_t linelen;
4058 wchar_t *wline;
4059 int width;
4060 struct tog_blame_line *blame_line;
4061 struct got_object_id *prev_id = NULL;
4062 char *id_str;
4063 struct tog_color *tc;
4065 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4066 if (err)
4067 return err;
4069 rewind(blame->f);
4070 werase(view->window);
4072 if (asprintf(&line, "commit %s", id_str) == -1) {
4073 err = got_error_from_errno("asprintf");
4074 free(id_str);
4075 return err;
4078 err = format_line(&wline, &width, line, view->ncols, 0);
4079 free(line);
4080 line = NULL;
4081 if (err)
4082 return err;
4083 if (view_needs_focus_indication(view))
4084 wstandout(view->window);
4085 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4086 if (tc)
4087 wattr_on(view->window,
4088 COLOR_PAIR(tc->colorpair), NULL);
4089 waddwstr(view->window, wline);
4090 if (tc)
4091 wattr_off(view->window,
4092 COLOR_PAIR(tc->colorpair), NULL);
4093 if (view_needs_focus_indication(view))
4094 wstandend(view->window);
4095 free(wline);
4096 wline = NULL;
4097 if (width < view->ncols - 1)
4098 waddch(view->window, '\n');
4100 if (asprintf(&line, "[%d/%d] %s%s",
4101 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4102 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4103 free(id_str);
4104 return got_error_from_errno("asprintf");
4106 free(id_str);
4107 err = format_line(&wline, &width, line, view->ncols, 0);
4108 free(line);
4109 line = NULL;
4110 if (err)
4111 return err;
4112 waddwstr(view->window, wline);
4113 free(wline);
4114 wline = NULL;
4115 if (width < view->ncols - 1)
4116 waddch(view->window, '\n');
4118 s->eof = 0;
4119 while (nprinted < view->nlines - 2) {
4120 linelen = getline(&line, &linesize, blame->f);
4121 if (linelen == -1) {
4122 if (feof(blame->f)) {
4123 s->eof = 1;
4124 break;
4126 free(line);
4127 return got_ferror(blame->f, GOT_ERR_IO);
4129 if (++lineno < s->first_displayed_line)
4130 continue;
4132 if (view->focussed && nprinted == s->selected_line - 1)
4133 wstandout(view->window);
4135 if (blame->nlines > 0) {
4136 blame_line = &blame->lines[lineno - 1];
4137 if (blame_line->annotated && prev_id &&
4138 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4139 !(view->focussed &&
4140 nprinted == s->selected_line - 1)) {
4141 waddstr(view->window, " ");
4142 } else if (blame_line->annotated) {
4143 char *id_str;
4144 err = got_object_id_str(&id_str, blame_line->id);
4145 if (err) {
4146 free(line);
4147 return err;
4149 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4150 if (tc)
4151 wattr_on(view->window,
4152 COLOR_PAIR(tc->colorpair), NULL);
4153 wprintw(view->window, "%.8s", id_str);
4154 if (tc)
4155 wattr_off(view->window,
4156 COLOR_PAIR(tc->colorpair), NULL);
4157 free(id_str);
4158 prev_id = blame_line->id;
4159 } else {
4160 waddstr(view->window, "........");
4161 prev_id = NULL;
4163 } else {
4164 waddstr(view->window, "........");
4165 prev_id = NULL;
4168 if (view->focussed && nprinted == s->selected_line - 1)
4169 wstandend(view->window);
4170 waddstr(view->window, " ");
4172 if (view->ncols <= 9) {
4173 width = 9;
4174 wline = wcsdup(L"");
4175 if (wline == NULL) {
4176 err = got_error_from_errno("wcsdup");
4177 free(line);
4178 return err;
4180 } else if (s->first_displayed_line + nprinted ==
4181 s->matched_line &&
4182 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4183 err = add_matched_line(&width, line, view->ncols - 9, 9,
4184 view->window, regmatch);
4185 if (err) {
4186 free(line);
4187 return err;
4189 width += 9;
4190 } else {
4191 err = format_line(&wline, &width, line,
4192 view->ncols - 9, 9);
4193 waddwstr(view->window, wline);
4194 free(wline);
4195 wline = NULL;
4196 width += 9;
4199 if (width <= view->ncols - 1)
4200 waddch(view->window, '\n');
4201 if (++nprinted == 1)
4202 s->first_displayed_line = lineno;
4204 free(line);
4205 s->last_displayed_line = lineno;
4207 view_vborder(view);
4209 return NULL;
4212 static const struct got_error *
4213 blame_cb(void *arg, int nlines, int lineno,
4214 struct got_commit_object *commit, struct got_object_id *id)
4216 const struct got_error *err = NULL;
4217 struct tog_blame_cb_args *a = arg;
4218 struct tog_blame_line *line;
4219 int errcode;
4221 if (nlines != a->nlines ||
4222 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4223 return got_error(GOT_ERR_RANGE);
4225 errcode = pthread_mutex_lock(&tog_mutex);
4226 if (errcode)
4227 return got_error_set_errno(errcode, "pthread_mutex_lock");
4229 if (*a->quit) { /* user has quit the blame view */
4230 err = got_error(GOT_ERR_ITER_COMPLETED);
4231 goto done;
4234 if (lineno == -1)
4235 goto done; /* no change in this commit */
4237 line = &a->lines[lineno - 1];
4238 if (line->annotated)
4239 goto done;
4241 line->id = got_object_id_dup(id);
4242 if (line->id == NULL) {
4243 err = got_error_from_errno("got_object_id_dup");
4244 goto done;
4246 line->annotated = 1;
4247 done:
4248 errcode = pthread_mutex_unlock(&tog_mutex);
4249 if (errcode)
4250 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4251 return err;
4254 static void *
4255 blame_thread(void *arg)
4257 const struct got_error *err, *close_err;
4258 struct tog_blame_thread_args *ta = arg;
4259 struct tog_blame_cb_args *a = ta->cb_args;
4260 int errcode;
4262 err = block_signals_used_by_main_thread();
4263 if (err)
4264 return (void *)err;
4266 err = got_blame(ta->path, a->commit_id, ta->repo,
4267 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4268 if (err && err->code == GOT_ERR_CANCELLED)
4269 err = NULL;
4271 errcode = pthread_mutex_lock(&tog_mutex);
4272 if (errcode)
4273 return (void *)got_error_set_errno(errcode,
4274 "pthread_mutex_lock");
4276 close_err = got_repo_close(ta->repo);
4277 if (err == NULL)
4278 err = close_err;
4279 ta->repo = NULL;
4280 *ta->complete = 1;
4282 errcode = pthread_mutex_unlock(&tog_mutex);
4283 if (errcode && err == NULL)
4284 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4286 return (void *)err;
4289 static struct got_object_id *
4290 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4291 int first_displayed_line, int selected_line)
4293 struct tog_blame_line *line;
4295 if (nlines <= 0)
4296 return NULL;
4298 line = &lines[first_displayed_line - 1 + selected_line - 1];
4299 if (!line->annotated)
4300 return NULL;
4302 return line->id;
4305 static const struct got_error *
4306 stop_blame(struct tog_blame *blame)
4308 const struct got_error *err = NULL;
4309 int i;
4311 if (blame->thread) {
4312 int errcode;
4313 errcode = pthread_mutex_unlock(&tog_mutex);
4314 if (errcode)
4315 return got_error_set_errno(errcode,
4316 "pthread_mutex_unlock");
4317 errcode = pthread_join(blame->thread, (void **)&err);
4318 if (errcode)
4319 return got_error_set_errno(errcode, "pthread_join");
4320 errcode = pthread_mutex_lock(&tog_mutex);
4321 if (errcode)
4322 return got_error_set_errno(errcode,
4323 "pthread_mutex_lock");
4324 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4325 err = NULL;
4326 blame->thread = NULL;
4328 if (blame->thread_args.repo) {
4329 const struct got_error *close_err;
4330 close_err = got_repo_close(blame->thread_args.repo);
4331 if (err == NULL)
4332 err = close_err;
4333 blame->thread_args.repo = NULL;
4335 if (blame->f) {
4336 if (fclose(blame->f) == EOF && err == NULL)
4337 err = got_error_from_errno("fclose");
4338 blame->f = NULL;
4340 if (blame->lines) {
4341 for (i = 0; i < blame->nlines; i++)
4342 free(blame->lines[i].id);
4343 free(blame->lines);
4344 blame->lines = NULL;
4346 free(blame->cb_args.commit_id);
4347 blame->cb_args.commit_id = NULL;
4349 return err;
4352 static const struct got_error *
4353 cancel_blame_view(void *arg)
4355 const struct got_error *err = NULL;
4356 int *done = arg;
4357 int errcode;
4359 errcode = pthread_mutex_lock(&tog_mutex);
4360 if (errcode)
4361 return got_error_set_errno(errcode,
4362 "pthread_mutex_unlock");
4364 if (*done)
4365 err = got_error(GOT_ERR_CANCELLED);
4367 errcode = pthread_mutex_unlock(&tog_mutex);
4368 if (errcode)
4369 return got_error_set_errno(errcode,
4370 "pthread_mutex_lock");
4372 return err;
4375 static const struct got_error *
4376 run_blame(struct tog_view *view)
4378 struct tog_blame_view_state *s = &view->state.blame;
4379 struct tog_blame *blame = &s->blame;
4380 const struct got_error *err = NULL;
4381 struct got_commit_object *commit = NULL;
4382 struct got_blob_object *blob = NULL;
4383 struct got_repository *thread_repo = NULL;
4384 struct got_object_id *obj_id = NULL;
4385 int obj_type;
4387 err = got_object_open_as_commit(&commit, s->repo,
4388 &s->blamed_commit->id);
4389 if (err)
4390 return err;
4392 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4393 if (err)
4394 goto done;
4396 err = got_object_get_type(&obj_type, s->repo, obj_id);
4397 if (err)
4398 goto done;
4400 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4401 err = got_error(GOT_ERR_OBJ_TYPE);
4402 goto done;
4405 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4406 if (err)
4407 goto done;
4408 blame->f = got_opentemp();
4409 if (blame->f == NULL) {
4410 err = got_error_from_errno("got_opentemp");
4411 goto done;
4413 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4414 &blame->line_offsets, blame->f, blob);
4415 if (err)
4416 goto done;
4417 if (blame->nlines == 0) {
4418 s->blame_complete = 1;
4419 goto done;
4422 /* Don't include \n at EOF in the blame line count. */
4423 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4424 blame->nlines--;
4426 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4427 if (blame->lines == NULL) {
4428 err = got_error_from_errno("calloc");
4429 goto done;
4432 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4433 if (err)
4434 goto done;
4436 blame->cb_args.view = view;
4437 blame->cb_args.lines = blame->lines;
4438 blame->cb_args.nlines = blame->nlines;
4439 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4440 if (blame->cb_args.commit_id == NULL) {
4441 err = got_error_from_errno("got_object_id_dup");
4442 goto done;
4444 blame->cb_args.quit = &s->done;
4446 blame->thread_args.path = s->path;
4447 blame->thread_args.repo = thread_repo;
4448 blame->thread_args.cb_args = &blame->cb_args;
4449 blame->thread_args.complete = &s->blame_complete;
4450 blame->thread_args.cancel_cb = cancel_blame_view;
4451 blame->thread_args.cancel_arg = &s->done;
4452 s->blame_complete = 0;
4454 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4455 s->first_displayed_line = 1;
4456 s->last_displayed_line = view->nlines;
4457 s->selected_line = 1;
4459 s->matched_line = 0;
4461 done:
4462 if (commit)
4463 got_object_commit_close(commit);
4464 if (blob)
4465 got_object_blob_close(blob);
4466 free(obj_id);
4467 if (err)
4468 stop_blame(blame);
4469 return err;
4472 static const struct got_error *
4473 open_blame_view(struct tog_view *view, char *path,
4474 struct got_object_id *commit_id, struct got_repository *repo)
4476 const struct got_error *err = NULL;
4477 struct tog_blame_view_state *s = &view->state.blame;
4479 STAILQ_INIT(&s->blamed_commits);
4481 s->path = strdup(path);
4482 if (s->path == NULL)
4483 return got_error_from_errno("strdup");
4485 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4486 if (err) {
4487 free(s->path);
4488 return err;
4491 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4492 s->first_displayed_line = 1;
4493 s->last_displayed_line = view->nlines;
4494 s->selected_line = 1;
4495 s->blame_complete = 0;
4496 s->repo = repo;
4497 s->commit_id = commit_id;
4498 memset(&s->blame, 0, sizeof(s->blame));
4500 STAILQ_INIT(&s->colors);
4501 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4502 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4503 get_color_value("TOG_COLOR_COMMIT"));
4504 if (err)
4505 return err;
4508 view->show = show_blame_view;
4509 view->input = input_blame_view;
4510 view->close = close_blame_view;
4511 view->search_start = search_start_blame_view;
4512 view->search_next = search_next_blame_view;
4514 return run_blame(view);
4517 static const struct got_error *
4518 close_blame_view(struct tog_view *view)
4520 const struct got_error *err = NULL;
4521 struct tog_blame_view_state *s = &view->state.blame;
4523 if (s->blame.thread)
4524 err = stop_blame(&s->blame);
4526 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4527 struct got_object_qid *blamed_commit;
4528 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4529 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4530 got_object_qid_free(blamed_commit);
4533 free(s->path);
4534 free_colors(&s->colors);
4536 return err;
4539 static const struct got_error *
4540 search_start_blame_view(struct tog_view *view)
4542 struct tog_blame_view_state *s = &view->state.blame;
4544 s->matched_line = 0;
4545 return NULL;
4548 static const struct got_error *
4549 search_next_blame_view(struct tog_view *view)
4551 struct tog_blame_view_state *s = &view->state.blame;
4552 int lineno;
4553 char *line = NULL;
4554 size_t linesize = 0;
4555 ssize_t linelen;
4557 if (!view->searching) {
4558 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4559 return NULL;
4562 if (s->matched_line) {
4563 if (view->searching == TOG_SEARCH_FORWARD)
4564 lineno = s->matched_line + 1;
4565 else
4566 lineno = s->matched_line - 1;
4567 } else
4568 lineno = s->first_displayed_line - 1 + s->selected_line;
4570 while (1) {
4571 off_t offset;
4573 if (lineno <= 0 || lineno > s->blame.nlines) {
4574 if (s->matched_line == 0) {
4575 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4576 break;
4579 if (view->searching == TOG_SEARCH_FORWARD)
4580 lineno = 1;
4581 else
4582 lineno = s->blame.nlines;
4585 offset = s->blame.line_offsets[lineno - 1];
4586 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4587 free(line);
4588 return got_error_from_errno("fseeko");
4590 linelen = getline(&line, &linesize, s->blame.f);
4591 if (linelen != -1 &&
4592 match_line(line, &view->regex, 1, &view->regmatch)) {
4593 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4594 s->matched_line = lineno;
4595 break;
4597 if (view->searching == TOG_SEARCH_FORWARD)
4598 lineno++;
4599 else
4600 lineno--;
4602 free(line);
4604 if (s->matched_line) {
4605 s->first_displayed_line = s->matched_line;
4606 s->selected_line = 1;
4609 return NULL;
4612 static const struct got_error *
4613 show_blame_view(struct tog_view *view)
4615 const struct got_error *err = NULL;
4616 struct tog_blame_view_state *s = &view->state.blame;
4617 int errcode;
4619 if (s->blame.thread == NULL && !s->blame_complete) {
4620 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4621 &s->blame.thread_args);
4622 if (errcode)
4623 return got_error_set_errno(errcode, "pthread_create");
4625 halfdelay(1); /* fast refresh while annotating */
4628 if (s->blame_complete)
4629 halfdelay(10); /* disable fast refresh */
4631 err = draw_blame(view);
4633 view_vborder(view);
4634 return err;
4637 static const struct got_error *
4638 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4640 const struct got_error *err = NULL, *thread_err = NULL;
4641 struct tog_view *diff_view;
4642 struct tog_blame_view_state *s = &view->state.blame;
4643 int begin_x = 0, nscroll = view->nlines - 2;
4645 switch (ch) {
4646 case 'q':
4647 s->done = 1;
4648 break;
4649 case 'g':
4650 case KEY_HOME:
4651 s->selected_line = 1;
4652 s->first_displayed_line = 1;
4653 break;
4654 case 'G':
4655 case KEY_END:
4656 if (s->blame.nlines < view->nlines - 2) {
4657 s->selected_line = s->blame.nlines;
4658 s->first_displayed_line = 1;
4659 } else {
4660 s->selected_line = view->nlines - 2;
4661 s->first_displayed_line = s->blame.nlines -
4662 (view->nlines - 3);
4664 break;
4665 case 'k':
4666 case KEY_UP:
4667 case CTRL('p'):
4668 if (s->selected_line > 1)
4669 s->selected_line--;
4670 else if (s->selected_line == 1 &&
4671 s->first_displayed_line > 1)
4672 s->first_displayed_line--;
4673 break;
4674 case CTRL('u'):
4675 nscroll /= 2;
4676 /* FALL THROUGH */
4677 case KEY_PPAGE:
4678 case CTRL('b'):
4679 if (s->first_displayed_line == 1) {
4680 s->selected_line = MAX(1, s->selected_line - nscroll);
4681 break;
4683 if (s->first_displayed_line > nscroll)
4684 s->first_displayed_line -= nscroll;
4685 else
4686 s->first_displayed_line = 1;
4687 break;
4688 case 'j':
4689 case KEY_DOWN:
4690 case CTRL('n'):
4691 if (s->selected_line < view->nlines - 2 &&
4692 s->first_displayed_line +
4693 s->selected_line <= s->blame.nlines)
4694 s->selected_line++;
4695 else if (s->last_displayed_line <
4696 s->blame.nlines)
4697 s->first_displayed_line++;
4698 break;
4699 case 'b':
4700 case 'p': {
4701 struct got_object_id *id = NULL;
4702 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4703 s->first_displayed_line, s->selected_line);
4704 if (id == NULL)
4705 break;
4706 if (ch == 'p') {
4707 struct got_commit_object *commit, *pcommit;
4708 struct got_object_qid *pid;
4709 struct got_object_id *blob_id = NULL;
4710 int obj_type;
4711 err = got_object_open_as_commit(&commit,
4712 s->repo, id);
4713 if (err)
4714 break;
4715 pid = STAILQ_FIRST(
4716 got_object_commit_get_parent_ids(commit));
4717 if (pid == NULL) {
4718 got_object_commit_close(commit);
4719 break;
4721 /* Check if path history ends here. */
4722 err = got_object_open_as_commit(&pcommit,
4723 s->repo, &pid->id);
4724 if (err)
4725 break;
4726 err = got_object_id_by_path(&blob_id, s->repo,
4727 pcommit, s->path);
4728 got_object_commit_close(pcommit);
4729 if (err) {
4730 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4731 err = NULL;
4732 got_object_commit_close(commit);
4733 break;
4735 err = got_object_get_type(&obj_type, s->repo,
4736 blob_id);
4737 free(blob_id);
4738 /* Can't blame non-blob type objects. */
4739 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4740 got_object_commit_close(commit);
4741 break;
4743 err = got_object_qid_alloc(&s->blamed_commit,
4744 &pid->id);
4745 got_object_commit_close(commit);
4746 } else {
4747 if (got_object_id_cmp(id,
4748 &s->blamed_commit->id) == 0)
4749 break;
4750 err = got_object_qid_alloc(&s->blamed_commit,
4751 id);
4753 if (err)
4754 break;
4755 s->done = 1;
4756 thread_err = stop_blame(&s->blame);
4757 s->done = 0;
4758 if (thread_err)
4759 break;
4760 STAILQ_INSERT_HEAD(&s->blamed_commits,
4761 s->blamed_commit, entry);
4762 err = run_blame(view);
4763 if (err)
4764 break;
4765 break;
4767 case 'B': {
4768 struct got_object_qid *first;
4769 first = STAILQ_FIRST(&s->blamed_commits);
4770 if (!got_object_id_cmp(&first->id, s->commit_id))
4771 break;
4772 s->done = 1;
4773 thread_err = stop_blame(&s->blame);
4774 s->done = 0;
4775 if (thread_err)
4776 break;
4777 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4778 got_object_qid_free(s->blamed_commit);
4779 s->blamed_commit =
4780 STAILQ_FIRST(&s->blamed_commits);
4781 err = run_blame(view);
4782 if (err)
4783 break;
4784 break;
4786 case KEY_ENTER:
4787 case '\r': {
4788 struct got_object_id *id = NULL;
4789 struct got_object_qid *pid;
4790 struct got_commit_object *commit = NULL;
4791 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4792 s->first_displayed_line, s->selected_line);
4793 if (id == NULL)
4794 break;
4795 err = got_object_open_as_commit(&commit, s->repo, id);
4796 if (err)
4797 break;
4798 pid = STAILQ_FIRST(
4799 got_object_commit_get_parent_ids(commit));
4800 if (view_is_parent_view(view))
4801 begin_x = view_split_begin_x(view->begin_x);
4802 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4803 if (diff_view == NULL) {
4804 got_object_commit_close(commit);
4805 err = got_error_from_errno("view_open");
4806 break;
4808 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4809 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4810 got_object_commit_close(commit);
4811 if (err) {
4812 view_close(diff_view);
4813 break;
4815 view->focussed = 0;
4816 diff_view->focussed = 1;
4817 if (view_is_parent_view(view)) {
4818 err = view_close_child(view);
4819 if (err)
4820 break;
4821 view_set_child(view, diff_view);
4822 view->focus_child = 1;
4823 } else
4824 *new_view = diff_view;
4825 if (err)
4826 break;
4827 break;
4829 case CTRL('d'):
4830 nscroll /= 2;
4831 /* FALL THROUGH */
4832 case KEY_NPAGE:
4833 case CTRL('f'):
4834 case ' ':
4835 if (s->last_displayed_line >= s->blame.nlines &&
4836 s->selected_line >= MIN(s->blame.nlines,
4837 view->nlines - 2)) {
4838 break;
4840 if (s->last_displayed_line >= s->blame.nlines &&
4841 s->selected_line < view->nlines - 2) {
4842 s->selected_line +=
4843 MIN(nscroll, s->last_displayed_line -
4844 s->first_displayed_line - s->selected_line + 1);
4846 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4847 s->first_displayed_line += nscroll;
4848 else
4849 s->first_displayed_line =
4850 s->blame.nlines - (view->nlines - 3);
4851 break;
4852 case KEY_RESIZE:
4853 if (s->selected_line > view->nlines - 2) {
4854 s->selected_line = MIN(s->blame.nlines,
4855 view->nlines - 2);
4857 break;
4858 default:
4859 break;
4861 return thread_err ? thread_err : err;
4864 static const struct got_error *
4865 cmd_blame(int argc, char *argv[])
4867 const struct got_error *error;
4868 struct got_repository *repo = NULL;
4869 struct got_worktree *worktree = NULL;
4870 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4871 char *link_target = NULL;
4872 struct got_object_id *commit_id = NULL;
4873 struct got_commit_object *commit = NULL;
4874 char *commit_id_str = NULL;
4875 int ch;
4876 struct tog_view *view;
4878 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4879 switch (ch) {
4880 case 'c':
4881 commit_id_str = optarg;
4882 break;
4883 case 'r':
4884 repo_path = realpath(optarg, NULL);
4885 if (repo_path == NULL)
4886 return got_error_from_errno2("realpath",
4887 optarg);
4888 break;
4889 default:
4890 usage_blame();
4891 /* NOTREACHED */
4895 argc -= optind;
4896 argv += optind;
4898 if (argc != 1)
4899 usage_blame();
4901 if (repo_path == NULL) {
4902 cwd = getcwd(NULL, 0);
4903 if (cwd == NULL)
4904 return got_error_from_errno("getcwd");
4905 error = got_worktree_open(&worktree, cwd);
4906 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4907 goto done;
4908 if (worktree)
4909 repo_path =
4910 strdup(got_worktree_get_repo_path(worktree));
4911 else
4912 repo_path = strdup(cwd);
4913 if (repo_path == NULL) {
4914 error = got_error_from_errno("strdup");
4915 goto done;
4919 error = got_repo_open(&repo, repo_path, NULL);
4920 if (error != NULL)
4921 goto done;
4923 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4924 worktree);
4925 if (error)
4926 goto done;
4928 init_curses();
4930 error = apply_unveil(got_repo_get_path(repo), NULL);
4931 if (error)
4932 goto done;
4934 error = tog_load_refs(repo, 0);
4935 if (error)
4936 goto done;
4938 if (commit_id_str == NULL) {
4939 struct got_reference *head_ref;
4940 error = got_ref_open(&head_ref, repo, worktree ?
4941 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4942 if (error != NULL)
4943 goto done;
4944 error = got_ref_resolve(&commit_id, repo, head_ref);
4945 got_ref_close(head_ref);
4946 } else {
4947 error = got_repo_match_object_id(&commit_id, NULL,
4948 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4950 if (error != NULL)
4951 goto done;
4953 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4954 if (view == NULL) {
4955 error = got_error_from_errno("view_open");
4956 goto done;
4959 error = got_object_open_as_commit(&commit, repo, commit_id);
4960 if (error)
4961 goto done;
4963 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4964 commit, repo);
4965 if (error)
4966 goto done;
4968 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4969 commit_id, repo);
4970 if (error)
4971 goto done;
4972 if (worktree) {
4973 /* Release work tree lock. */
4974 got_worktree_close(worktree);
4975 worktree = NULL;
4977 error = view_loop(view);
4978 done:
4979 free(repo_path);
4980 free(in_repo_path);
4981 free(link_target);
4982 free(cwd);
4983 free(commit_id);
4984 if (commit)
4985 got_object_commit_close(commit);
4986 if (worktree)
4987 got_worktree_close(worktree);
4988 if (repo) {
4989 const struct got_error *close_err = got_repo_close(repo);
4990 if (error == NULL)
4991 error = close_err;
4993 tog_free_refs();
4994 return error;
4997 static const struct got_error *
4998 draw_tree_entries(struct tog_view *view, const char *parent_path)
5000 struct tog_tree_view_state *s = &view->state.tree;
5001 const struct got_error *err = NULL;
5002 struct got_tree_entry *te;
5003 wchar_t *wline;
5004 struct tog_color *tc;
5005 int width, n, i, nentries;
5006 int limit = view->nlines;
5008 s->ndisplayed = 0;
5010 werase(view->window);
5012 if (limit == 0)
5013 return NULL;
5015 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5016 if (err)
5017 return err;
5018 if (view_needs_focus_indication(view))
5019 wstandout(view->window);
5020 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5021 if (tc)
5022 wattr_on(view->window,
5023 COLOR_PAIR(tc->colorpair), NULL);
5024 waddwstr(view->window, wline);
5025 if (tc)
5026 wattr_off(view->window,
5027 COLOR_PAIR(tc->colorpair), NULL);
5028 if (view_needs_focus_indication(view))
5029 wstandend(view->window);
5030 free(wline);
5031 wline = NULL;
5032 if (width < view->ncols - 1)
5033 waddch(view->window, '\n');
5034 if (--limit <= 0)
5035 return NULL;
5036 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5037 if (err)
5038 return err;
5039 waddwstr(view->window, wline);
5040 free(wline);
5041 wline = NULL;
5042 if (width < view->ncols - 1)
5043 waddch(view->window, '\n');
5044 if (--limit <= 0)
5045 return NULL;
5046 waddch(view->window, '\n');
5047 if (--limit <= 0)
5048 return NULL;
5050 if (s->first_displayed_entry == NULL) {
5051 te = got_object_tree_get_first_entry(s->tree);
5052 if (s->selected == 0) {
5053 if (view->focussed)
5054 wstandout(view->window);
5055 s->selected_entry = NULL;
5057 waddstr(view->window, " ..\n"); /* parent directory */
5058 if (s->selected == 0 && view->focussed)
5059 wstandend(view->window);
5060 s->ndisplayed++;
5061 if (--limit <= 0)
5062 return NULL;
5063 n = 1;
5064 } else {
5065 n = 0;
5066 te = s->first_displayed_entry;
5069 nentries = got_object_tree_get_nentries(s->tree);
5070 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5071 char *line = NULL, *id_str = NULL, *link_target = NULL;
5072 const char *modestr = "";
5073 mode_t mode;
5075 te = got_object_tree_get_entry(s->tree, i);
5076 mode = got_tree_entry_get_mode(te);
5078 if (s->show_ids) {
5079 err = got_object_id_str(&id_str,
5080 got_tree_entry_get_id(te));
5081 if (err)
5082 return got_error_from_errno(
5083 "got_object_id_str");
5085 if (got_object_tree_entry_is_submodule(te))
5086 modestr = "$";
5087 else if (S_ISLNK(mode)) {
5088 int i;
5090 err = got_tree_entry_get_symlink_target(&link_target,
5091 te, s->repo);
5092 if (err) {
5093 free(id_str);
5094 return err;
5096 for (i = 0; i < strlen(link_target); i++) {
5097 if (!isprint((unsigned char)link_target[i]))
5098 link_target[i] = '?';
5100 modestr = "@";
5102 else if (S_ISDIR(mode))
5103 modestr = "/";
5104 else if (mode & S_IXUSR)
5105 modestr = "*";
5106 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5107 got_tree_entry_get_name(te), modestr,
5108 link_target ? " -> ": "",
5109 link_target ? link_target : "") == -1) {
5110 free(id_str);
5111 free(link_target);
5112 return got_error_from_errno("asprintf");
5114 free(id_str);
5115 free(link_target);
5116 err = format_line(&wline, &width, line, view->ncols, 0);
5117 if (err) {
5118 free(line);
5119 break;
5121 if (n == s->selected) {
5122 if (view->focussed)
5123 wstandout(view->window);
5124 s->selected_entry = te;
5126 tc = match_color(&s->colors, line);
5127 if (tc)
5128 wattr_on(view->window,
5129 COLOR_PAIR(tc->colorpair), NULL);
5130 waddwstr(view->window, wline);
5131 if (tc)
5132 wattr_off(view->window,
5133 COLOR_PAIR(tc->colorpair), NULL);
5134 if (width < view->ncols - 1)
5135 waddch(view->window, '\n');
5136 if (n == s->selected && view->focussed)
5137 wstandend(view->window);
5138 free(line);
5139 free(wline);
5140 wline = NULL;
5141 n++;
5142 s->ndisplayed++;
5143 s->last_displayed_entry = te;
5144 if (--limit <= 0)
5145 break;
5148 return err;
5151 static void
5152 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5154 struct got_tree_entry *te;
5155 int isroot = s->tree == s->root;
5156 int i = 0;
5158 if (s->first_displayed_entry == NULL)
5159 return;
5161 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5162 while (i++ < maxscroll) {
5163 if (te == NULL) {
5164 if (!isroot)
5165 s->first_displayed_entry = NULL;
5166 break;
5168 s->first_displayed_entry = te;
5169 te = got_tree_entry_get_prev(s->tree, te);
5173 static void
5174 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5176 struct got_tree_entry *next, *last;
5177 int n = 0;
5179 if (s->first_displayed_entry)
5180 next = got_tree_entry_get_next(s->tree,
5181 s->first_displayed_entry);
5182 else
5183 next = got_object_tree_get_first_entry(s->tree);
5185 last = s->last_displayed_entry;
5186 while (next && last && n++ < maxscroll) {
5187 last = got_tree_entry_get_next(s->tree, last);
5188 if (last) {
5189 s->first_displayed_entry = next;
5190 next = got_tree_entry_get_next(s->tree, next);
5195 static const struct got_error *
5196 tree_entry_path(char **path, struct tog_parent_trees *parents,
5197 struct got_tree_entry *te)
5199 const struct got_error *err = NULL;
5200 struct tog_parent_tree *pt;
5201 size_t len = 2; /* for leading slash and NUL */
5203 TAILQ_FOREACH(pt, parents, entry)
5204 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5205 + 1 /* slash */;
5206 if (te)
5207 len += strlen(got_tree_entry_get_name(te));
5209 *path = calloc(1, len);
5210 if (path == NULL)
5211 return got_error_from_errno("calloc");
5213 (*path)[0] = '/';
5214 pt = TAILQ_LAST(parents, tog_parent_trees);
5215 while (pt) {
5216 const char *name = got_tree_entry_get_name(pt->selected_entry);
5217 if (strlcat(*path, name, len) >= len) {
5218 err = got_error(GOT_ERR_NO_SPACE);
5219 goto done;
5221 if (strlcat(*path, "/", len) >= len) {
5222 err = got_error(GOT_ERR_NO_SPACE);
5223 goto done;
5225 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5227 if (te) {
5228 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5229 err = got_error(GOT_ERR_NO_SPACE);
5230 goto done;
5233 done:
5234 if (err) {
5235 free(*path);
5236 *path = NULL;
5238 return err;
5241 static const struct got_error *
5242 blame_tree_entry(struct tog_view **new_view, int begin_x,
5243 struct got_tree_entry *te, struct tog_parent_trees *parents,
5244 struct got_object_id *commit_id, struct got_repository *repo)
5246 const struct got_error *err = NULL;
5247 char *path;
5248 struct tog_view *blame_view;
5250 *new_view = NULL;
5252 err = tree_entry_path(&path, parents, te);
5253 if (err)
5254 return err;
5256 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5257 if (blame_view == NULL) {
5258 err = got_error_from_errno("view_open");
5259 goto done;
5262 err = open_blame_view(blame_view, path, commit_id, repo);
5263 if (err) {
5264 if (err->code == GOT_ERR_CANCELLED)
5265 err = NULL;
5266 view_close(blame_view);
5267 } else
5268 *new_view = blame_view;
5269 done:
5270 free(path);
5271 return err;
5274 static const struct got_error *
5275 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5276 struct tog_tree_view_state *s)
5278 struct tog_view *log_view;
5279 const struct got_error *err = NULL;
5280 char *path;
5282 *new_view = NULL;
5284 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5285 if (log_view == NULL)
5286 return got_error_from_errno("view_open");
5288 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5289 if (err)
5290 return err;
5292 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5293 path, 0);
5294 if (err)
5295 view_close(log_view);
5296 else
5297 *new_view = log_view;
5298 free(path);
5299 return err;
5302 static const struct got_error *
5303 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5304 const char *head_ref_name, struct got_repository *repo)
5306 const struct got_error *err = NULL;
5307 char *commit_id_str = NULL;
5308 struct tog_tree_view_state *s = &view->state.tree;
5309 struct got_commit_object *commit = NULL;
5311 TAILQ_INIT(&s->parents);
5312 STAILQ_INIT(&s->colors);
5314 s->commit_id = got_object_id_dup(commit_id);
5315 if (s->commit_id == NULL)
5316 return got_error_from_errno("got_object_id_dup");
5318 err = got_object_open_as_commit(&commit, repo, commit_id);
5319 if (err)
5320 goto done;
5323 * The root is opened here and will be closed when the view is closed.
5324 * Any visited subtrees and their path-wise parents are opened and
5325 * closed on demand.
5327 err = got_object_open_as_tree(&s->root, repo,
5328 got_object_commit_get_tree_id(commit));
5329 if (err)
5330 goto done;
5331 s->tree = s->root;
5333 err = got_object_id_str(&commit_id_str, commit_id);
5334 if (err != NULL)
5335 goto done;
5337 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5338 err = got_error_from_errno("asprintf");
5339 goto done;
5342 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5343 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5344 if (head_ref_name) {
5345 s->head_ref_name = strdup(head_ref_name);
5346 if (s->head_ref_name == NULL) {
5347 err = got_error_from_errno("strdup");
5348 goto done;
5351 s->repo = repo;
5353 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5354 err = add_color(&s->colors, "\\$$",
5355 TOG_COLOR_TREE_SUBMODULE,
5356 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5357 if (err)
5358 goto done;
5359 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5360 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5361 if (err)
5362 goto done;
5363 err = add_color(&s->colors, "/$",
5364 TOG_COLOR_TREE_DIRECTORY,
5365 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5366 if (err)
5367 goto done;
5369 err = add_color(&s->colors, "\\*$",
5370 TOG_COLOR_TREE_EXECUTABLE,
5371 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5372 if (err)
5373 goto done;
5375 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5376 get_color_value("TOG_COLOR_COMMIT"));
5377 if (err)
5378 goto done;
5381 view->show = show_tree_view;
5382 view->input = input_tree_view;
5383 view->close = close_tree_view;
5384 view->search_start = search_start_tree_view;
5385 view->search_next = search_next_tree_view;
5386 done:
5387 free(commit_id_str);
5388 if (commit)
5389 got_object_commit_close(commit);
5390 if (err)
5391 close_tree_view(view);
5392 return err;
5395 static const struct got_error *
5396 close_tree_view(struct tog_view *view)
5398 struct tog_tree_view_state *s = &view->state.tree;
5400 free_colors(&s->colors);
5401 free(s->tree_label);
5402 s->tree_label = NULL;
5403 free(s->commit_id);
5404 s->commit_id = NULL;
5405 free(s->head_ref_name);
5406 s->head_ref_name = NULL;
5407 while (!TAILQ_EMPTY(&s->parents)) {
5408 struct tog_parent_tree *parent;
5409 parent = TAILQ_FIRST(&s->parents);
5410 TAILQ_REMOVE(&s->parents, parent, entry);
5411 if (parent->tree != s->root)
5412 got_object_tree_close(parent->tree);
5413 free(parent);
5416 if (s->tree != NULL && s->tree != s->root)
5417 got_object_tree_close(s->tree);
5418 if (s->root)
5419 got_object_tree_close(s->root);
5420 return NULL;
5423 static const struct got_error *
5424 search_start_tree_view(struct tog_view *view)
5426 struct tog_tree_view_state *s = &view->state.tree;
5428 s->matched_entry = NULL;
5429 return NULL;
5432 static int
5433 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5435 regmatch_t regmatch;
5437 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5438 0) == 0;
5441 static const struct got_error *
5442 search_next_tree_view(struct tog_view *view)
5444 struct tog_tree_view_state *s = &view->state.tree;
5445 struct got_tree_entry *te = NULL;
5447 if (!view->searching) {
5448 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5449 return NULL;
5452 if (s->matched_entry) {
5453 if (view->searching == TOG_SEARCH_FORWARD) {
5454 if (s->selected_entry)
5455 te = got_tree_entry_get_next(s->tree,
5456 s->selected_entry);
5457 else
5458 te = got_object_tree_get_first_entry(s->tree);
5459 } else {
5460 if (s->selected_entry == NULL)
5461 te = got_object_tree_get_last_entry(s->tree);
5462 else
5463 te = got_tree_entry_get_prev(s->tree,
5464 s->selected_entry);
5466 } else {
5467 if (s->selected_entry)
5468 te = s->selected_entry;
5469 else if (view->searching == TOG_SEARCH_FORWARD)
5470 te = got_object_tree_get_first_entry(s->tree);
5471 else
5472 te = got_object_tree_get_last_entry(s->tree);
5475 while (1) {
5476 if (te == NULL) {
5477 if (s->matched_entry == NULL) {
5478 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5479 return NULL;
5481 if (view->searching == TOG_SEARCH_FORWARD)
5482 te = got_object_tree_get_first_entry(s->tree);
5483 else
5484 te = got_object_tree_get_last_entry(s->tree);
5487 if (match_tree_entry(te, &view->regex)) {
5488 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5489 s->matched_entry = te;
5490 break;
5493 if (view->searching == TOG_SEARCH_FORWARD)
5494 te = got_tree_entry_get_next(s->tree, te);
5495 else
5496 te = got_tree_entry_get_prev(s->tree, te);
5499 if (s->matched_entry) {
5500 s->first_displayed_entry = s->matched_entry;
5501 s->selected = 0;
5504 return NULL;
5507 static const struct got_error *
5508 show_tree_view(struct tog_view *view)
5510 const struct got_error *err = NULL;
5511 struct tog_tree_view_state *s = &view->state.tree;
5512 char *parent_path;
5514 err = tree_entry_path(&parent_path, &s->parents, NULL);
5515 if (err)
5516 return err;
5518 err = draw_tree_entries(view, parent_path);
5519 free(parent_path);
5521 view_vborder(view);
5522 return err;
5525 static const struct got_error *
5526 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5528 const struct got_error *err = NULL;
5529 struct tog_tree_view_state *s = &view->state.tree;
5530 struct tog_view *log_view, *ref_view;
5531 struct got_tree_entry *te;
5532 int begin_x = 0, n, nscroll = view->nlines - 3;
5534 switch (ch) {
5535 case 'i':
5536 s->show_ids = !s->show_ids;
5537 break;
5538 case 'l':
5539 if (!s->selected_entry)
5540 break;
5541 if (view_is_parent_view(view))
5542 begin_x = view_split_begin_x(view->begin_x);
5543 err = log_selected_tree_entry(&log_view, begin_x, s);
5544 view->focussed = 0;
5545 log_view->focussed = 1;
5546 if (view_is_parent_view(view)) {
5547 err = view_close_child(view);
5548 if (err)
5549 return err;
5550 view_set_child(view, log_view);
5551 view->focus_child = 1;
5552 } else
5553 *new_view = log_view;
5554 break;
5555 case 'r':
5556 if (view_is_parent_view(view))
5557 begin_x = view_split_begin_x(view->begin_x);
5558 ref_view = view_open(view->nlines, view->ncols,
5559 view->begin_y, begin_x, TOG_VIEW_REF);
5560 if (ref_view == NULL)
5561 return got_error_from_errno("view_open");
5562 err = open_ref_view(ref_view, s->repo);
5563 if (err) {
5564 view_close(ref_view);
5565 return err;
5567 view->focussed = 0;
5568 ref_view->focussed = 1;
5569 if (view_is_parent_view(view)) {
5570 err = view_close_child(view);
5571 if (err)
5572 return err;
5573 view_set_child(view, ref_view);
5574 view->focus_child = 1;
5575 } else
5576 *new_view = ref_view;
5577 break;
5578 case 'g':
5579 case KEY_HOME:
5580 s->selected = 0;
5581 if (s->tree == s->root)
5582 s->first_displayed_entry =
5583 got_object_tree_get_first_entry(s->tree);
5584 else
5585 s->first_displayed_entry = NULL;
5586 break;
5587 case 'G':
5588 case KEY_END:
5589 s->selected = 0;
5590 te = got_object_tree_get_last_entry(s->tree);
5591 for (n = 0; n < view->nlines - 3; n++) {
5592 if (te == NULL) {
5593 if(s->tree != s->root) {
5594 s->first_displayed_entry = NULL;
5595 n++;
5597 break;
5599 s->first_displayed_entry = te;
5600 te = got_tree_entry_get_prev(s->tree, te);
5602 if (n > 0)
5603 s->selected = n - 1;
5604 break;
5605 case 'k':
5606 case KEY_UP:
5607 case CTRL('p'):
5608 if (s->selected > 0) {
5609 s->selected--;
5610 break;
5612 tree_scroll_up(s, 1);
5613 break;
5614 case CTRL('u'):
5615 nscroll /= 2;
5616 /* FALL THROUGH */
5617 case KEY_PPAGE:
5618 case CTRL('b'):
5619 if (s->tree == s->root) {
5620 if (got_object_tree_get_first_entry(s->tree) ==
5621 s->first_displayed_entry)
5622 s->selected -= MIN(s->selected, nscroll);
5623 } else {
5624 if (s->first_displayed_entry == NULL)
5625 s->selected -= MIN(s->selected, nscroll);
5627 tree_scroll_up(s, MAX(0, nscroll));
5628 break;
5629 case 'j':
5630 case KEY_DOWN:
5631 case CTRL('n'):
5632 if (s->selected < s->ndisplayed - 1) {
5633 s->selected++;
5634 break;
5636 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5637 == NULL)
5638 /* can't scroll any further */
5639 break;
5640 tree_scroll_down(s, 1);
5641 break;
5642 case CTRL('d'):
5643 nscroll /= 2;
5644 /* FALL THROUGH */
5645 case KEY_NPAGE:
5646 case CTRL('f'):
5647 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5648 == NULL) {
5649 /* can't scroll any further; move cursor down */
5650 if (s->selected < s->ndisplayed - 1)
5651 s->selected += MIN(nscroll,
5652 s->ndisplayed - s->selected - 1);
5653 break;
5655 tree_scroll_down(s, nscroll);
5656 break;
5657 case KEY_ENTER:
5658 case '\r':
5659 case KEY_BACKSPACE:
5660 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5661 struct tog_parent_tree *parent;
5662 /* user selected '..' */
5663 if (s->tree == s->root)
5664 break;
5665 parent = TAILQ_FIRST(&s->parents);
5666 TAILQ_REMOVE(&s->parents, parent,
5667 entry);
5668 got_object_tree_close(s->tree);
5669 s->tree = parent->tree;
5670 s->first_displayed_entry =
5671 parent->first_displayed_entry;
5672 s->selected_entry =
5673 parent->selected_entry;
5674 s->selected = parent->selected;
5675 free(parent);
5676 } else if (S_ISDIR(got_tree_entry_get_mode(
5677 s->selected_entry))) {
5678 struct got_tree_object *subtree;
5679 err = got_object_open_as_tree(&subtree, s->repo,
5680 got_tree_entry_get_id(s->selected_entry));
5681 if (err)
5682 break;
5683 err = tree_view_visit_subtree(s, subtree);
5684 if (err) {
5685 got_object_tree_close(subtree);
5686 break;
5688 } else if (S_ISREG(got_tree_entry_get_mode(
5689 s->selected_entry))) {
5690 struct tog_view *blame_view;
5691 int begin_x = view_is_parent_view(view) ?
5692 view_split_begin_x(view->begin_x) : 0;
5694 err = blame_tree_entry(&blame_view, begin_x,
5695 s->selected_entry, &s->parents,
5696 s->commit_id, s->repo);
5697 if (err)
5698 break;
5699 view->focussed = 0;
5700 blame_view->focussed = 1;
5701 if (view_is_parent_view(view)) {
5702 err = view_close_child(view);
5703 if (err)
5704 return err;
5705 view_set_child(view, blame_view);
5706 view->focus_child = 1;
5707 } else
5708 *new_view = blame_view;
5710 break;
5711 case KEY_RESIZE:
5712 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5713 s->selected = view->nlines - 4;
5714 break;
5715 default:
5716 break;
5719 return err;
5722 __dead static void
5723 usage_tree(void)
5725 endwin();
5726 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5727 getprogname());
5728 exit(1);
5731 static const struct got_error *
5732 cmd_tree(int argc, char *argv[])
5734 const struct got_error *error;
5735 struct got_repository *repo = NULL;
5736 struct got_worktree *worktree = NULL;
5737 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5738 struct got_object_id *commit_id = NULL;
5739 struct got_commit_object *commit = NULL;
5740 const char *commit_id_arg = NULL;
5741 char *label = NULL;
5742 struct got_reference *ref = NULL;
5743 const char *head_ref_name = NULL;
5744 int ch;
5745 struct tog_view *view;
5747 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5748 switch (ch) {
5749 case 'c':
5750 commit_id_arg = optarg;
5751 break;
5752 case 'r':
5753 repo_path = realpath(optarg, NULL);
5754 if (repo_path == NULL)
5755 return got_error_from_errno2("realpath",
5756 optarg);
5757 break;
5758 default:
5759 usage_tree();
5760 /* NOTREACHED */
5764 argc -= optind;
5765 argv += optind;
5767 if (argc > 1)
5768 usage_tree();
5770 if (repo_path == NULL) {
5771 cwd = getcwd(NULL, 0);
5772 if (cwd == NULL)
5773 return got_error_from_errno("getcwd");
5774 error = got_worktree_open(&worktree, cwd);
5775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5776 goto done;
5777 if (worktree)
5778 repo_path =
5779 strdup(got_worktree_get_repo_path(worktree));
5780 else
5781 repo_path = strdup(cwd);
5782 if (repo_path == NULL) {
5783 error = got_error_from_errno("strdup");
5784 goto done;
5788 error = got_repo_open(&repo, repo_path, NULL);
5789 if (error != NULL)
5790 goto done;
5792 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5793 repo, worktree);
5794 if (error)
5795 goto done;
5797 init_curses();
5799 error = apply_unveil(got_repo_get_path(repo), NULL);
5800 if (error)
5801 goto done;
5803 error = tog_load_refs(repo, 0);
5804 if (error)
5805 goto done;
5807 if (commit_id_arg == NULL) {
5808 error = got_repo_match_object_id(&commit_id, &label,
5809 worktree ? got_worktree_get_head_ref_name(worktree) :
5810 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5811 if (error)
5812 goto done;
5813 head_ref_name = label;
5814 } else {
5815 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5816 if (error == NULL)
5817 head_ref_name = got_ref_get_name(ref);
5818 else if (error->code != GOT_ERR_NOT_REF)
5819 goto done;
5820 error = got_repo_match_object_id(&commit_id, NULL,
5821 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5822 if (error)
5823 goto done;
5826 error = got_object_open_as_commit(&commit, repo, commit_id);
5827 if (error)
5828 goto done;
5830 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5831 if (view == NULL) {
5832 error = got_error_from_errno("view_open");
5833 goto done;
5835 error = open_tree_view(view, commit_id, head_ref_name, repo);
5836 if (error)
5837 goto done;
5838 if (!got_path_is_root_dir(in_repo_path)) {
5839 error = tree_view_walk_path(&view->state.tree, commit,
5840 in_repo_path);
5841 if (error)
5842 goto done;
5845 if (worktree) {
5846 /* Release work tree lock. */
5847 got_worktree_close(worktree);
5848 worktree = NULL;
5850 error = view_loop(view);
5851 done:
5852 free(repo_path);
5853 free(cwd);
5854 free(commit_id);
5855 free(label);
5856 if (ref)
5857 got_ref_close(ref);
5858 if (repo) {
5859 const struct got_error *close_err = got_repo_close(repo);
5860 if (error == NULL)
5861 error = close_err;
5863 tog_free_refs();
5864 return error;
5867 static const struct got_error *
5868 ref_view_load_refs(struct tog_ref_view_state *s)
5870 struct got_reflist_entry *sre;
5871 struct tog_reflist_entry *re;
5873 s->nrefs = 0;
5874 TAILQ_FOREACH(sre, &tog_refs, entry) {
5875 if (strncmp(got_ref_get_name(sre->ref),
5876 "refs/got/", 9) == 0 &&
5877 strncmp(got_ref_get_name(sre->ref),
5878 "refs/got/backup/", 16) != 0)
5879 continue;
5881 re = malloc(sizeof(*re));
5882 if (re == NULL)
5883 return got_error_from_errno("malloc");
5885 re->ref = got_ref_dup(sre->ref);
5886 if (re->ref == NULL)
5887 return got_error_from_errno("got_ref_dup");
5888 re->idx = s->nrefs++;
5889 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5892 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5893 return NULL;
5896 void
5897 ref_view_free_refs(struct tog_ref_view_state *s)
5899 struct tog_reflist_entry *re;
5901 while (!TAILQ_EMPTY(&s->refs)) {
5902 re = TAILQ_FIRST(&s->refs);
5903 TAILQ_REMOVE(&s->refs, re, entry);
5904 got_ref_close(re->ref);
5905 free(re);
5909 static const struct got_error *
5910 open_ref_view(struct tog_view *view, struct got_repository *repo)
5912 const struct got_error *err = NULL;
5913 struct tog_ref_view_state *s = &view->state.ref;
5915 s->selected_entry = 0;
5916 s->repo = repo;
5918 TAILQ_INIT(&s->refs);
5919 STAILQ_INIT(&s->colors);
5921 err = ref_view_load_refs(s);
5922 if (err)
5923 return err;
5925 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5926 err = add_color(&s->colors, "^refs/heads/",
5927 TOG_COLOR_REFS_HEADS,
5928 get_color_value("TOG_COLOR_REFS_HEADS"));
5929 if (err)
5930 goto done;
5932 err = add_color(&s->colors, "^refs/tags/",
5933 TOG_COLOR_REFS_TAGS,
5934 get_color_value("TOG_COLOR_REFS_TAGS"));
5935 if (err)
5936 goto done;
5938 err = add_color(&s->colors, "^refs/remotes/",
5939 TOG_COLOR_REFS_REMOTES,
5940 get_color_value("TOG_COLOR_REFS_REMOTES"));
5941 if (err)
5942 goto done;
5944 err = add_color(&s->colors, "^refs/got/backup/",
5945 TOG_COLOR_REFS_BACKUP,
5946 get_color_value("TOG_COLOR_REFS_BACKUP"));
5947 if (err)
5948 goto done;
5951 view->show = show_ref_view;
5952 view->input = input_ref_view;
5953 view->close = close_ref_view;
5954 view->search_start = search_start_ref_view;
5955 view->search_next = search_next_ref_view;
5956 done:
5957 if (err)
5958 free_colors(&s->colors);
5959 return err;
5962 static const struct got_error *
5963 close_ref_view(struct tog_view *view)
5965 struct tog_ref_view_state *s = &view->state.ref;
5967 ref_view_free_refs(s);
5968 free_colors(&s->colors);
5970 return NULL;
5973 static const struct got_error *
5974 resolve_reflist_entry(struct got_object_id **commit_id,
5975 struct tog_reflist_entry *re, struct got_repository *repo)
5977 const struct got_error *err = NULL;
5978 struct got_object_id *obj_id;
5979 struct got_tag_object *tag = NULL;
5980 int obj_type;
5982 *commit_id = NULL;
5984 err = got_ref_resolve(&obj_id, repo, re->ref);
5985 if (err)
5986 return err;
5988 err = got_object_get_type(&obj_type, repo, obj_id);
5989 if (err)
5990 goto done;
5992 switch (obj_type) {
5993 case GOT_OBJ_TYPE_COMMIT:
5994 *commit_id = obj_id;
5995 break;
5996 case GOT_OBJ_TYPE_TAG:
5997 err = got_object_open_as_tag(&tag, repo, obj_id);
5998 if (err)
5999 goto done;
6000 free(obj_id);
6001 err = got_object_get_type(&obj_type, repo,
6002 got_object_tag_get_object_id(tag));
6003 if (err)
6004 goto done;
6005 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6006 err = got_error(GOT_ERR_OBJ_TYPE);
6007 goto done;
6009 *commit_id = got_object_id_dup(
6010 got_object_tag_get_object_id(tag));
6011 if (*commit_id == NULL) {
6012 err = got_error_from_errno("got_object_id_dup");
6013 goto done;
6015 break;
6016 default:
6017 err = got_error(GOT_ERR_OBJ_TYPE);
6018 break;
6021 done:
6022 if (tag)
6023 got_object_tag_close(tag);
6024 if (err) {
6025 free(*commit_id);
6026 *commit_id = NULL;
6028 return err;
6031 static const struct got_error *
6032 log_ref_entry(struct tog_view **new_view, int begin_x,
6033 struct tog_reflist_entry *re, struct got_repository *repo)
6035 struct tog_view *log_view;
6036 const struct got_error *err = NULL;
6037 struct got_object_id *commit_id = NULL;
6039 *new_view = NULL;
6041 err = resolve_reflist_entry(&commit_id, re, repo);
6042 if (err) {
6043 if (err->code != GOT_ERR_OBJ_TYPE)
6044 return err;
6045 else
6046 return NULL;
6049 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6050 if (log_view == NULL) {
6051 err = got_error_from_errno("view_open");
6052 goto done;
6055 err = open_log_view(log_view, commit_id, repo,
6056 got_ref_get_name(re->ref), "", 0);
6057 done:
6058 if (err)
6059 view_close(log_view);
6060 else
6061 *new_view = log_view;
6062 free(commit_id);
6063 return err;
6066 static void
6067 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6069 struct tog_reflist_entry *re;
6070 int i = 0;
6072 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6073 return;
6075 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6076 while (i++ < maxscroll) {
6077 if (re == NULL)
6078 break;
6079 s->first_displayed_entry = re;
6080 re = TAILQ_PREV(re, tog_reflist_head, entry);
6084 static void
6085 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6087 struct tog_reflist_entry *next, *last;
6088 int n = 0;
6090 if (s->first_displayed_entry)
6091 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6092 else
6093 next = TAILQ_FIRST(&s->refs);
6095 last = s->last_displayed_entry;
6096 while (next && last && n++ < maxscroll) {
6097 last = TAILQ_NEXT(last, entry);
6098 if (last) {
6099 s->first_displayed_entry = next;
6100 next = TAILQ_NEXT(next, entry);
6105 static const struct got_error *
6106 search_start_ref_view(struct tog_view *view)
6108 struct tog_ref_view_state *s = &view->state.ref;
6110 s->matched_entry = NULL;
6111 return NULL;
6114 static int
6115 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6117 regmatch_t regmatch;
6119 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6120 0) == 0;
6123 static const struct got_error *
6124 search_next_ref_view(struct tog_view *view)
6126 struct tog_ref_view_state *s = &view->state.ref;
6127 struct tog_reflist_entry *re = NULL;
6129 if (!view->searching) {
6130 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6131 return NULL;
6134 if (s->matched_entry) {
6135 if (view->searching == TOG_SEARCH_FORWARD) {
6136 if (s->selected_entry)
6137 re = TAILQ_NEXT(s->selected_entry, entry);
6138 else
6139 re = TAILQ_PREV(s->selected_entry,
6140 tog_reflist_head, entry);
6141 } else {
6142 if (s->selected_entry == NULL)
6143 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6144 else
6145 re = TAILQ_PREV(s->selected_entry,
6146 tog_reflist_head, entry);
6148 } else {
6149 if (s->selected_entry)
6150 re = s->selected_entry;
6151 else if (view->searching == TOG_SEARCH_FORWARD)
6152 re = TAILQ_FIRST(&s->refs);
6153 else
6154 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6157 while (1) {
6158 if (re == NULL) {
6159 if (s->matched_entry == NULL) {
6160 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6161 return NULL;
6163 if (view->searching == TOG_SEARCH_FORWARD)
6164 re = TAILQ_FIRST(&s->refs);
6165 else
6166 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6169 if (match_reflist_entry(re, &view->regex)) {
6170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6171 s->matched_entry = re;
6172 break;
6175 if (view->searching == TOG_SEARCH_FORWARD)
6176 re = TAILQ_NEXT(re, entry);
6177 else
6178 re = TAILQ_PREV(re, tog_reflist_head, entry);
6181 if (s->matched_entry) {
6182 s->first_displayed_entry = s->matched_entry;
6183 s->selected = 0;
6186 return NULL;
6189 static const struct got_error *
6190 show_ref_view(struct tog_view *view)
6192 const struct got_error *err = NULL;
6193 struct tog_ref_view_state *s = &view->state.ref;
6194 struct tog_reflist_entry *re;
6195 char *line = NULL;
6196 wchar_t *wline;
6197 struct tog_color *tc;
6198 int width, n;
6199 int limit = view->nlines;
6201 werase(view->window);
6203 s->ndisplayed = 0;
6205 if (limit == 0)
6206 return NULL;
6208 re = s->first_displayed_entry;
6210 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6211 s->nrefs) == -1)
6212 return got_error_from_errno("asprintf");
6214 err = format_line(&wline, &width, line, view->ncols, 0);
6215 if (err) {
6216 free(line);
6217 return err;
6219 if (view_needs_focus_indication(view))
6220 wstandout(view->window);
6221 waddwstr(view->window, wline);
6222 if (view_needs_focus_indication(view))
6223 wstandend(view->window);
6224 free(wline);
6225 wline = NULL;
6226 free(line);
6227 line = NULL;
6228 if (width < view->ncols - 1)
6229 waddch(view->window, '\n');
6230 if (--limit <= 0)
6231 return NULL;
6233 n = 0;
6234 while (re && limit > 0) {
6235 char *line = NULL;
6237 if (got_ref_is_symbolic(re->ref)) {
6238 if (asprintf(&line, "%s -> %s",
6239 got_ref_get_name(re->ref),
6240 got_ref_get_symref_target(re->ref)) == -1)
6241 return got_error_from_errno("asprintf");
6242 } else if (s->show_ids) {
6243 struct got_object_id *id;
6244 char *id_str;
6245 err = got_ref_resolve(&id, s->repo, re->ref);
6246 if (err)
6247 return err;
6248 err = got_object_id_str(&id_str, id);
6249 if (err) {
6250 free(id);
6251 return err;
6253 if (asprintf(&line, "%s: %s",
6254 got_ref_get_name(re->ref), id_str) == -1) {
6255 err = got_error_from_errno("asprintf");
6256 free(id);
6257 free(id_str);
6258 return err;
6260 free(id);
6261 free(id_str);
6262 } else {
6263 line = strdup(got_ref_get_name(re->ref));
6264 if (line == NULL)
6265 return got_error_from_errno("strdup");
6268 err = format_line(&wline, &width, line, view->ncols, 0);
6269 if (err) {
6270 free(line);
6271 return err;
6273 if (n == s->selected) {
6274 if (view->focussed)
6275 wstandout(view->window);
6276 s->selected_entry = re;
6278 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6279 if (tc)
6280 wattr_on(view->window,
6281 COLOR_PAIR(tc->colorpair), NULL);
6282 waddwstr(view->window, wline);
6283 if (tc)
6284 wattr_off(view->window,
6285 COLOR_PAIR(tc->colorpair), NULL);
6286 if (width < view->ncols - 1)
6287 waddch(view->window, '\n');
6288 if (n == s->selected && view->focussed)
6289 wstandend(view->window);
6290 free(line);
6291 free(wline);
6292 wline = NULL;
6293 n++;
6294 s->ndisplayed++;
6295 s->last_displayed_entry = re;
6297 limit--;
6298 re = TAILQ_NEXT(re, entry);
6301 view_vborder(view);
6302 return err;
6305 static const struct got_error *
6306 browse_ref_tree(struct tog_view **new_view, int begin_x,
6307 struct tog_reflist_entry *re, struct got_repository *repo)
6309 const struct got_error *err = NULL;
6310 struct got_object_id *commit_id = NULL;
6311 struct tog_view *tree_view;
6313 *new_view = NULL;
6315 err = resolve_reflist_entry(&commit_id, re, repo);
6316 if (err) {
6317 if (err->code != GOT_ERR_OBJ_TYPE)
6318 return err;
6319 else
6320 return NULL;
6324 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6325 if (tree_view == NULL) {
6326 err = got_error_from_errno("view_open");
6327 goto done;
6330 err = open_tree_view(tree_view, commit_id,
6331 got_ref_get_name(re->ref), repo);
6332 if (err)
6333 goto done;
6335 *new_view = tree_view;
6336 done:
6337 free(commit_id);
6338 return err;
6340 static const struct got_error *
6341 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6343 const struct got_error *err = NULL;
6344 struct tog_ref_view_state *s = &view->state.ref;
6345 struct tog_view *log_view, *tree_view;
6346 struct tog_reflist_entry *re;
6347 int begin_x = 0, n, nscroll = view->nlines - 1;
6349 switch (ch) {
6350 case 'i':
6351 s->show_ids = !s->show_ids;
6352 break;
6353 case 'o':
6354 s->sort_by_date = !s->sort_by_date;
6355 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6356 got_ref_cmp_by_commit_timestamp_descending :
6357 tog_ref_cmp_by_name, s->repo);
6358 if (err)
6359 break;
6360 got_reflist_object_id_map_free(tog_refs_idmap);
6361 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6362 &tog_refs, s->repo);
6363 if (err)
6364 break;
6365 ref_view_free_refs(s);
6366 err = ref_view_load_refs(s);
6367 break;
6368 case KEY_ENTER:
6369 case '\r':
6370 if (!s->selected_entry)
6371 break;
6372 if (view_is_parent_view(view))
6373 begin_x = view_split_begin_x(view->begin_x);
6374 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6375 s->repo);
6376 view->focussed = 0;
6377 log_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, log_view);
6383 view->focus_child = 1;
6384 } else
6385 *new_view = log_view;
6386 break;
6387 case 't':
6388 if (!s->selected_entry)
6389 break;
6390 if (view_is_parent_view(view))
6391 begin_x = view_split_begin_x(view->begin_x);
6392 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6393 s->repo);
6394 if (err || tree_view == NULL)
6395 break;
6396 view->focussed = 0;
6397 tree_view->focussed = 1;
6398 if (view_is_parent_view(view)) {
6399 err = view_close_child(view);
6400 if (err)
6401 return err;
6402 view_set_child(view, tree_view);
6403 view->focus_child = 1;
6404 } else
6405 *new_view = tree_view;
6406 break;
6407 case 'g':
6408 case KEY_HOME:
6409 s->selected = 0;
6410 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6411 break;
6412 case 'G':
6413 case KEY_END:
6414 s->selected = 0;
6415 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6416 for (n = 0; n < view->nlines - 1; n++) {
6417 if (re == NULL)
6418 break;
6419 s->first_displayed_entry = re;
6420 re = TAILQ_PREV(re, tog_reflist_head, entry);
6422 if (n > 0)
6423 s->selected = n - 1;
6424 break;
6425 case 'k':
6426 case KEY_UP:
6427 case CTRL('p'):
6428 if (s->selected > 0) {
6429 s->selected--;
6430 break;
6432 ref_scroll_up(s, 1);
6433 break;
6434 case CTRL('u'):
6435 nscroll /= 2;
6436 /* FALL THROUGH */
6437 case KEY_PPAGE:
6438 case CTRL('b'):
6439 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6440 s->selected -= MIN(nscroll, s->selected);
6441 ref_scroll_up(s, MAX(0, nscroll));
6442 break;
6443 case 'j':
6444 case KEY_DOWN:
6445 case CTRL('n'):
6446 if (s->selected < s->ndisplayed - 1) {
6447 s->selected++;
6448 break;
6450 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6451 /* can't scroll any further */
6452 break;
6453 ref_scroll_down(s, 1);
6454 break;
6455 case CTRL('d'):
6456 nscroll /= 2;
6457 /* FALL THROUGH */
6458 case KEY_NPAGE:
6459 case CTRL('f'):
6460 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6461 /* can't scroll any further; move cursor down */
6462 if (s->selected < s->ndisplayed - 1)
6463 s->selected += MIN(nscroll,
6464 s->ndisplayed - s->selected - 1);
6465 break;
6467 ref_scroll_down(s, nscroll);
6468 break;
6469 case CTRL('l'):
6470 tog_free_refs();
6471 err = tog_load_refs(s->repo, s->sort_by_date);
6472 if (err)
6473 break;
6474 ref_view_free_refs(s);
6475 err = ref_view_load_refs(s);
6476 break;
6477 case KEY_RESIZE:
6478 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6479 s->selected = view->nlines - 2;
6480 break;
6481 default:
6482 break;
6485 return err;
6488 __dead static void
6489 usage_ref(void)
6491 endwin();
6492 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6493 getprogname());
6494 exit(1);
6497 static const struct got_error *
6498 cmd_ref(int argc, char *argv[])
6500 const struct got_error *error;
6501 struct got_repository *repo = NULL;
6502 struct got_worktree *worktree = NULL;
6503 char *cwd = NULL, *repo_path = NULL;
6504 int ch;
6505 struct tog_view *view;
6507 while ((ch = getopt(argc, argv, "r:")) != -1) {
6508 switch (ch) {
6509 case 'r':
6510 repo_path = realpath(optarg, NULL);
6511 if (repo_path == NULL)
6512 return got_error_from_errno2("realpath",
6513 optarg);
6514 break;
6515 default:
6516 usage_ref();
6517 /* NOTREACHED */
6521 argc -= optind;
6522 argv += optind;
6524 if (argc > 1)
6525 usage_ref();
6527 if (repo_path == NULL) {
6528 cwd = getcwd(NULL, 0);
6529 if (cwd == NULL)
6530 return got_error_from_errno("getcwd");
6531 error = got_worktree_open(&worktree, cwd);
6532 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6533 goto done;
6534 if (worktree)
6535 repo_path =
6536 strdup(got_worktree_get_repo_path(worktree));
6537 else
6538 repo_path = strdup(cwd);
6539 if (repo_path == NULL) {
6540 error = got_error_from_errno("strdup");
6541 goto done;
6545 error = got_repo_open(&repo, repo_path, NULL);
6546 if (error != NULL)
6547 goto done;
6549 init_curses();
6551 error = apply_unveil(got_repo_get_path(repo), NULL);
6552 if (error)
6553 goto done;
6555 error = tog_load_refs(repo, 0);
6556 if (error)
6557 goto done;
6559 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6560 if (view == NULL) {
6561 error = got_error_from_errno("view_open");
6562 goto done;
6565 error = open_ref_view(view, repo);
6566 if (error)
6567 goto done;
6569 if (worktree) {
6570 /* Release work tree lock. */
6571 got_worktree_close(worktree);
6572 worktree = NULL;
6574 error = view_loop(view);
6575 done:
6576 free(repo_path);
6577 free(cwd);
6578 if (repo) {
6579 const struct got_error *close_err = got_repo_close(repo);
6580 if (close_err)
6581 error = close_err;
6583 tog_free_refs();
6584 return error;
6587 static void
6588 list_commands(FILE *fp)
6590 size_t i;
6592 fprintf(fp, "commands:");
6593 for (i = 0; i < nitems(tog_commands); i++) {
6594 const struct tog_cmd *cmd = &tog_commands[i];
6595 fprintf(fp, " %s", cmd->name);
6597 fputc('\n', fp);
6600 __dead static void
6601 usage(int hflag, int status)
6603 FILE *fp = (status == 0) ? stdout : stderr;
6605 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6606 getprogname());
6607 if (hflag) {
6608 fprintf(fp, "lazy usage: %s path\n", getprogname());
6609 list_commands(fp);
6611 exit(status);
6614 static char **
6615 make_argv(int argc, ...)
6617 va_list ap;
6618 char **argv;
6619 int i;
6621 va_start(ap, argc);
6623 argv = calloc(argc, sizeof(char *));
6624 if (argv == NULL)
6625 err(1, "calloc");
6626 for (i = 0; i < argc; i++) {
6627 argv[i] = strdup(va_arg(ap, char *));
6628 if (argv[i] == NULL)
6629 err(1, "strdup");
6632 va_end(ap);
6633 return argv;
6637 * Try to convert 'tog path' into a 'tog log path' command.
6638 * The user could simply have mistyped the command rather than knowingly
6639 * provided a path. So check whether argv[0] can in fact be resolved
6640 * to a path in the HEAD commit and print a special error if not.
6641 * This hack is for mpi@ <3
6643 static const struct got_error *
6644 tog_log_with_path(int argc, char *argv[])
6646 const struct got_error *error = NULL, *close_err;
6647 const struct tog_cmd *cmd = NULL;
6648 struct got_repository *repo = NULL;
6649 struct got_worktree *worktree = NULL;
6650 struct got_object_id *commit_id = NULL, *id = NULL;
6651 struct got_commit_object *commit = NULL;
6652 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6653 char *commit_id_str = NULL, **cmd_argv = NULL;
6655 cwd = getcwd(NULL, 0);
6656 if (cwd == NULL)
6657 return got_error_from_errno("getcwd");
6659 error = got_worktree_open(&worktree, cwd);
6660 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6661 goto done;
6663 if (worktree)
6664 repo_path = strdup(got_worktree_get_repo_path(worktree));
6665 else
6666 repo_path = strdup(cwd);
6667 if (repo_path == NULL) {
6668 error = got_error_from_errno("strdup");
6669 goto done;
6672 error = got_repo_open(&repo, repo_path, NULL);
6673 if (error != NULL)
6674 goto done;
6676 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6677 repo, worktree);
6678 if (error)
6679 goto done;
6681 error = tog_load_refs(repo, 0);
6682 if (error)
6683 goto done;
6684 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6685 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6686 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6687 if (error)
6688 goto done;
6690 if (worktree) {
6691 got_worktree_close(worktree);
6692 worktree = NULL;
6695 error = got_object_open_as_commit(&commit, repo, commit_id);
6696 if (error)
6697 goto done;
6699 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6700 if (error) {
6701 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6702 goto done;
6703 fprintf(stderr, "%s: '%s' is no known command or path\n",
6704 getprogname(), argv[0]);
6705 usage(1, 1);
6706 /* not reached */
6709 close_err = got_repo_close(repo);
6710 if (error == NULL)
6711 error = close_err;
6712 repo = NULL;
6714 error = got_object_id_str(&commit_id_str, commit_id);
6715 if (error)
6716 goto done;
6718 cmd = &tog_commands[0]; /* log */
6719 argc = 4;
6720 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6721 error = cmd->cmd_main(argc, cmd_argv);
6722 done:
6723 if (repo) {
6724 close_err = got_repo_close(repo);
6725 if (error == NULL)
6726 error = close_err;
6728 if (commit)
6729 got_object_commit_close(commit);
6730 if (worktree)
6731 got_worktree_close(worktree);
6732 free(id);
6733 free(commit_id_str);
6734 free(commit_id);
6735 free(cwd);
6736 free(repo_path);
6737 free(in_repo_path);
6738 if (cmd_argv) {
6739 int i;
6740 for (i = 0; i < argc; i++)
6741 free(cmd_argv[i]);
6742 free(cmd_argv);
6744 tog_free_refs();
6745 return error;
6748 int
6749 main(int argc, char *argv[])
6751 const struct got_error *error = NULL;
6752 const struct tog_cmd *cmd = NULL;
6753 int ch, hflag = 0, Vflag = 0;
6754 char **cmd_argv = NULL;
6755 static const struct option longopts[] = {
6756 { "version", no_argument, NULL, 'V' },
6757 { NULL, 0, NULL, 0}
6760 setlocale(LC_CTYPE, "");
6762 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6763 switch (ch) {
6764 case 'h':
6765 hflag = 1;
6766 break;
6767 case 'V':
6768 Vflag = 1;
6769 break;
6770 default:
6771 usage(hflag, 1);
6772 /* NOTREACHED */
6776 argc -= optind;
6777 argv += optind;
6778 optind = 1;
6779 optreset = 1;
6781 if (Vflag) {
6782 got_version_print_str();
6783 return 0;
6786 #ifndef PROFILE
6787 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6788 NULL) == -1)
6789 err(1, "pledge");
6790 #endif
6792 if (argc == 0) {
6793 if (hflag)
6794 usage(hflag, 0);
6795 /* Build an argument vector which runs a default command. */
6796 cmd = &tog_commands[0];
6797 argc = 1;
6798 cmd_argv = make_argv(argc, cmd->name);
6799 } else {
6800 size_t i;
6802 /* Did the user specify a command? */
6803 for (i = 0; i < nitems(tog_commands); i++) {
6804 if (strncmp(tog_commands[i].name, argv[0],
6805 strlen(argv[0])) == 0) {
6806 cmd = &tog_commands[i];
6807 break;
6812 if (cmd == NULL) {
6813 if (argc != 1)
6814 usage(0, 1);
6815 /* No command specified; try log with a path */
6816 error = tog_log_with_path(argc, argv);
6817 } else {
6818 if (hflag)
6819 cmd->cmd_usage();
6820 else
6821 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6824 endwin();
6825 putchar('\n');
6826 if (cmd_argv) {
6827 int i;
6828 for (i = 0; i < argc; i++)
6829 free(cmd_argv[i]);
6830 free(cmd_argv);
6833 if (error && error->code != GOT_ERR_CANCELLED)
6834 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6835 return 0;