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 case 'u':
2484 nscroll /= 2;
2485 /* FALL THROUGH */
2486 case KEY_PPAGE:
2487 case CTRL('b'):
2488 if (s->first_displayed_entry == NULL)
2489 break;
2490 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2491 s->selected = MAX(0, s->selected - nscroll - 1);
2492 else
2493 log_scroll_up(s, nscroll);
2494 select_commit(s);
2495 break;
2496 case 'j':
2497 case KEY_DOWN:
2498 case '>':
2499 case '.':
2500 case CTRL('n'):
2501 if (s->first_displayed_entry == NULL)
2502 break;
2503 if (s->selected < MIN(view->nlines - 2,
2504 s->commits.ncommits - 1))
2505 s->selected++;
2506 else {
2507 err = log_scroll_down(view, 1);
2508 if (err)
2509 break;
2511 select_commit(s);
2512 break;
2513 case 'G':
2514 case KEY_END: {
2515 /* We don't know yet how many commits, so we're forced to
2516 * traverse them all. */
2517 if (!s->thread_args.log_complete) {
2518 s->thread_args.load_all = 1;
2519 return trigger_log_thread(view, 0);
2522 s->selected = 0;
2523 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2524 for (n = 0; n < view->nlines - 1; n++) {
2525 if (entry == NULL)
2526 break;
2527 s->first_displayed_entry = entry;
2528 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2530 if (n > 0)
2531 s->selected = n - 1;
2532 select_commit(s);
2533 break;
2535 case CTRL('d'):
2536 case 'd':
2537 nscroll /= 2;
2538 /* FALL THROUGH */
2539 case KEY_NPAGE:
2540 case CTRL('f'): {
2541 struct commit_queue_entry *first;
2542 first = s->first_displayed_entry;
2543 if (first == NULL)
2544 break;
2545 err = log_scroll_down(view, nscroll);
2546 if (err)
2547 break;
2548 if (first == s->first_displayed_entry &&
2549 s->selected < MIN(view->nlines - 2,
2550 s->commits.ncommits - 1)) {
2551 /* can't scroll further down */
2552 s->selected += MIN(s->last_displayed_entry->idx -
2553 s->selected_entry->idx, nscroll + 1);
2555 select_commit(s);
2556 break;
2558 case KEY_RESIZE:
2559 if (s->selected > view->nlines - 2)
2560 s->selected = view->nlines - 2;
2561 if (s->selected > s->commits.ncommits - 1)
2562 s->selected = s->commits.ncommits - 1;
2563 select_commit(s);
2564 if (s->commits.ncommits < view->nlines - 1 &&
2565 !s->thread_args.log_complete) {
2566 s->thread_args.commits_needed += (view->nlines - 1) -
2567 s->commits.ncommits;
2568 err = trigger_log_thread(view, 1);
2570 break;
2571 case KEY_ENTER:
2572 case ' ':
2573 case '\r':
2574 if (s->selected_entry == NULL)
2575 break;
2576 if (view_is_parent_view(view))
2577 begin_x = view_split_begin_x(view->begin_x);
2578 err = open_diff_view_for_commit(&diff_view, begin_x,
2579 s->selected_entry->commit, s->selected_entry->id,
2580 view, s->repo);
2581 if (err)
2582 break;
2583 view->focussed = 0;
2584 diff_view->focussed = 1;
2585 if (view_is_parent_view(view)) {
2586 err = view_close_child(view);
2587 if (err)
2588 return err;
2589 view_set_child(view, diff_view);
2590 view->focus_child = 1;
2591 } else
2592 *new_view = diff_view;
2593 break;
2594 case 't':
2595 if (s->selected_entry == NULL)
2596 break;
2597 if (view_is_parent_view(view))
2598 begin_x = view_split_begin_x(view->begin_x);
2599 err = browse_commit_tree(&tree_view, begin_x,
2600 s->selected_entry, s->in_repo_path, s->head_ref_name,
2601 s->repo);
2602 if (err)
2603 break;
2604 view->focussed = 0;
2605 tree_view->focussed = 1;
2606 if (view_is_parent_view(view)) {
2607 err = view_close_child(view);
2608 if (err)
2609 return err;
2610 view_set_child(view, tree_view);
2611 view->focus_child = 1;
2612 } else
2613 *new_view = tree_view;
2614 break;
2615 case KEY_BACKSPACE:
2616 case CTRL('l'):
2617 case 'B':
2618 if (ch == KEY_BACKSPACE &&
2619 got_path_is_root_dir(s->in_repo_path))
2620 break;
2621 err = stop_log_thread(s);
2622 if (err)
2623 return err;
2624 if (ch == KEY_BACKSPACE) {
2625 char *parent_path;
2626 err = got_path_dirname(&parent_path, s->in_repo_path);
2627 if (err)
2628 return err;
2629 free(s->in_repo_path);
2630 s->in_repo_path = parent_path;
2631 s->thread_args.in_repo_path = s->in_repo_path;
2632 } else if (ch == CTRL('l')) {
2633 struct got_object_id *start_id;
2634 err = got_repo_match_object_id(&start_id, NULL,
2635 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2636 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2637 if (err)
2638 return err;
2639 free(s->start_id);
2640 s->start_id = start_id;
2641 s->thread_args.start_id = s->start_id;
2642 } else /* 'B' */
2643 s->log_branches = !s->log_branches;
2645 err = got_repo_open(&s->thread_args.repo,
2646 got_repo_get_path(s->repo), NULL);
2647 if (err)
2648 return err;
2649 tog_free_refs();
2650 err = tog_load_refs(s->repo, 0);
2651 if (err)
2652 return err;
2653 err = got_commit_graph_open(&s->thread_args.graph,
2654 s->in_repo_path, !s->log_branches);
2655 if (err)
2656 return err;
2657 err = got_commit_graph_iter_start(s->thread_args.graph,
2658 s->start_id, s->repo, NULL, NULL);
2659 if (err)
2660 return err;
2661 free_commits(&s->commits);
2662 s->first_displayed_entry = NULL;
2663 s->last_displayed_entry = NULL;
2664 s->selected_entry = NULL;
2665 s->selected = 0;
2666 s->thread_args.log_complete = 0;
2667 s->quit = 0;
2668 s->thread_args.commits_needed = view->nlines;
2669 break;
2670 case 'r':
2671 if (view_is_parent_view(view))
2672 begin_x = view_split_begin_x(view->begin_x);
2673 ref_view = view_open(view->nlines, view->ncols,
2674 view->begin_y, begin_x, TOG_VIEW_REF);
2675 if (ref_view == NULL)
2676 return got_error_from_errno("view_open");
2677 err = open_ref_view(ref_view, s->repo);
2678 if (err) {
2679 view_close(ref_view);
2680 return err;
2682 view->focussed = 0;
2683 ref_view->focussed = 1;
2684 if (view_is_parent_view(view)) {
2685 err = view_close_child(view);
2686 if (err)
2687 return err;
2688 view_set_child(view, ref_view);
2689 view->focus_child = 1;
2690 } else
2691 *new_view = ref_view;
2692 break;
2693 default:
2694 break;
2697 return err;
2700 static const struct got_error *
2701 apply_unveil(const char *repo_path, const char *worktree_path)
2703 const struct got_error *error;
2705 #ifdef PROFILE
2706 if (unveil("gmon.out", "rwc") != 0)
2707 return got_error_from_errno2("unveil", "gmon.out");
2708 #endif
2709 if (repo_path && unveil(repo_path, "r") != 0)
2710 return got_error_from_errno2("unveil", repo_path);
2712 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2713 return got_error_from_errno2("unveil", worktree_path);
2715 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2716 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2718 error = got_privsep_unveil_exec_helpers();
2719 if (error != NULL)
2720 return error;
2722 if (unveil(NULL, NULL) != 0)
2723 return got_error_from_errno("unveil");
2725 return NULL;
2728 static void
2729 init_curses(void)
2732 * Override default signal handlers before starting ncurses.
2733 * This should prevent ncurses from installing its own
2734 * broken cleanup() signal handler.
2736 signal(SIGWINCH, tog_sigwinch);
2737 signal(SIGPIPE, tog_sigpipe);
2738 signal(SIGCONT, tog_sigcont);
2739 signal(SIGINT, tog_sigint);
2740 signal(SIGTERM, tog_sigterm);
2742 initscr();
2743 cbreak();
2744 halfdelay(1); /* Do fast refresh while initial view is loading. */
2745 noecho();
2746 nonl();
2747 intrflush(stdscr, FALSE);
2748 keypad(stdscr, TRUE);
2749 curs_set(0);
2750 if (getenv("TOG_COLORS") != NULL) {
2751 start_color();
2752 use_default_colors();
2756 static const struct got_error *
2757 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2758 struct got_repository *repo, struct got_worktree *worktree)
2760 const struct got_error *err = NULL;
2762 if (argc == 0) {
2763 *in_repo_path = strdup("/");
2764 if (*in_repo_path == NULL)
2765 return got_error_from_errno("strdup");
2766 return NULL;
2769 if (worktree) {
2770 const char *prefix = got_worktree_get_path_prefix(worktree);
2771 char *p;
2773 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2774 if (err)
2775 return err;
2776 if (asprintf(in_repo_path, "%s%s%s", prefix,
2777 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2778 p) == -1) {
2779 err = got_error_from_errno("asprintf");
2780 *in_repo_path = NULL;
2782 free(p);
2783 } else
2784 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2786 return err;
2789 static const struct got_error *
2790 cmd_log(int argc, char *argv[])
2792 const struct got_error *error;
2793 struct got_repository *repo = NULL;
2794 struct got_worktree *worktree = NULL;
2795 struct got_object_id *start_id = NULL;
2796 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2797 char *start_commit = NULL, *label = NULL;
2798 struct got_reference *ref = NULL;
2799 const char *head_ref_name = NULL;
2800 int ch, log_branches = 0;
2801 struct tog_view *view;
2803 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2804 switch (ch) {
2805 case 'b':
2806 log_branches = 1;
2807 break;
2808 case 'c':
2809 start_commit = optarg;
2810 break;
2811 case 'r':
2812 repo_path = realpath(optarg, NULL);
2813 if (repo_path == NULL)
2814 return got_error_from_errno2("realpath",
2815 optarg);
2816 break;
2817 default:
2818 usage_log();
2819 /* NOTREACHED */
2823 argc -= optind;
2824 argv += optind;
2826 if (argc > 1)
2827 usage_log();
2829 if (repo_path == NULL) {
2830 cwd = getcwd(NULL, 0);
2831 if (cwd == NULL)
2832 return got_error_from_errno("getcwd");
2833 error = got_worktree_open(&worktree, cwd);
2834 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2835 goto done;
2836 if (worktree)
2837 repo_path =
2838 strdup(got_worktree_get_repo_path(worktree));
2839 else
2840 repo_path = strdup(cwd);
2841 if (repo_path == NULL) {
2842 error = got_error_from_errno("strdup");
2843 goto done;
2847 error = got_repo_open(&repo, repo_path, NULL);
2848 if (error != NULL)
2849 goto done;
2851 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2852 repo, worktree);
2853 if (error)
2854 goto done;
2856 init_curses();
2858 error = apply_unveil(got_repo_get_path(repo),
2859 worktree ? got_worktree_get_root_path(worktree) : NULL);
2860 if (error)
2861 goto done;
2863 /* already loaded by tog_log_with_path()? */
2864 if (TAILQ_EMPTY(&tog_refs)) {
2865 error = tog_load_refs(repo, 0);
2866 if (error)
2867 goto done;
2870 if (start_commit == NULL) {
2871 error = got_repo_match_object_id(&start_id, &label,
2872 worktree ? got_worktree_get_head_ref_name(worktree) :
2873 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2874 if (error)
2875 goto done;
2876 head_ref_name = label;
2877 } else {
2878 error = got_ref_open(&ref, repo, start_commit, 0);
2879 if (error == NULL)
2880 head_ref_name = got_ref_get_name(ref);
2881 else if (error->code != GOT_ERR_NOT_REF)
2882 goto done;
2883 error = got_repo_match_object_id(&start_id, NULL,
2884 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2885 if (error)
2886 goto done;
2889 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2890 if (view == NULL) {
2891 error = got_error_from_errno("view_open");
2892 goto done;
2894 error = open_log_view(view, start_id, repo, head_ref_name,
2895 in_repo_path, log_branches);
2896 if (error)
2897 goto done;
2898 if (worktree) {
2899 /* Release work tree lock. */
2900 got_worktree_close(worktree);
2901 worktree = NULL;
2903 error = view_loop(view);
2904 done:
2905 free(in_repo_path);
2906 free(repo_path);
2907 free(cwd);
2908 free(start_id);
2909 free(label);
2910 if (ref)
2911 got_ref_close(ref);
2912 if (repo) {
2913 const struct got_error *close_err = got_repo_close(repo);
2914 if (error == NULL)
2915 error = close_err;
2917 if (worktree)
2918 got_worktree_close(worktree);
2919 tog_free_refs();
2920 return error;
2923 __dead static void
2924 usage_diff(void)
2926 endwin();
2927 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2928 "[-w] object1 object2\n", getprogname());
2929 exit(1);
2932 static int
2933 match_line(const char *line, regex_t *regex, size_t nmatch,
2934 regmatch_t *regmatch)
2936 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2939 struct tog_color *
2940 match_color(struct tog_colors *colors, const char *line)
2942 struct tog_color *tc = NULL;
2944 STAILQ_FOREACH(tc, colors, entry) {
2945 if (match_line(line, &tc->regex, 0, NULL))
2946 return tc;
2949 return NULL;
2952 static const struct got_error *
2953 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2954 WINDOW *window, regmatch_t *regmatch)
2956 const struct got_error *err = NULL;
2957 wchar_t *wline;
2958 int width;
2959 char *s;
2961 *wtotal = 0;
2963 s = strndup(line, regmatch->rm_so);
2964 if (s == NULL)
2965 return got_error_from_errno("strndup");
2967 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2968 if (err) {
2969 free(s);
2970 return err;
2972 waddwstr(window, wline);
2973 free(wline);
2974 free(s);
2975 wlimit -= width;
2976 *wtotal += width;
2978 if (wlimit > 0) {
2979 s = strndup(line + regmatch->rm_so,
2980 regmatch->rm_eo - regmatch->rm_so);
2981 if (s == NULL) {
2982 err = got_error_from_errno("strndup");
2983 free(s);
2984 return err;
2986 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2987 if (err) {
2988 free(s);
2989 return err;
2991 wattr_on(window, A_STANDOUT, NULL);
2992 waddwstr(window, wline);
2993 wattr_off(window, A_STANDOUT, NULL);
2994 free(wline);
2995 free(s);
2996 wlimit -= width;
2997 *wtotal += width;
3000 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3001 err = format_line(&wline, &width,
3002 line + regmatch->rm_eo, wlimit, col_tab_align);
3003 if (err)
3004 return err;
3005 waddwstr(window, wline);
3006 free(wline);
3007 *wtotal += width;
3010 return NULL;
3013 static const struct got_error *
3014 draw_file(struct tog_view *view, const char *header)
3016 struct tog_diff_view_state *s = &view->state.diff;
3017 regmatch_t *regmatch = &view->regmatch;
3018 const struct got_error *err;
3019 int nprinted = 0;
3020 char *line;
3021 size_t linesize = 0;
3022 ssize_t linelen;
3023 struct tog_color *tc;
3024 wchar_t *wline;
3025 int width;
3026 int max_lines = view->nlines;
3027 int nlines = s->nlines;
3028 off_t line_offset;
3030 line_offset = s->line_offsets[s->first_displayed_line - 1];
3031 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3032 return got_error_from_errno("fseek");
3034 werase(view->window);
3036 if (header) {
3037 if (asprintf(&line, "[%d/%d] %s",
3038 s->first_displayed_line - 1 + s->selected_line, nlines,
3039 header) == -1)
3040 return got_error_from_errno("asprintf");
3041 err = format_line(&wline, &width, line, view->ncols, 0);
3042 free(line);
3043 if (err)
3044 return err;
3046 if (view_needs_focus_indication(view))
3047 wstandout(view->window);
3048 waddwstr(view->window, wline);
3049 free(wline);
3050 wline = NULL;
3051 if (view_needs_focus_indication(view))
3052 wstandend(view->window);
3053 if (width <= view->ncols - 1)
3054 waddch(view->window, '\n');
3056 if (max_lines <= 1)
3057 return NULL;
3058 max_lines--;
3061 s->eof = 0;
3062 line = NULL;
3063 while (max_lines > 0 && nprinted < max_lines) {
3064 linelen = getline(&line, &linesize, s->f);
3065 if (linelen == -1) {
3066 if (feof(s->f)) {
3067 s->eof = 1;
3068 break;
3070 free(line);
3071 return got_ferror(s->f, GOT_ERR_IO);
3074 tc = match_color(&s->colors, line);
3075 if (tc)
3076 wattr_on(view->window,
3077 COLOR_PAIR(tc->colorpair), NULL);
3078 if (s->first_displayed_line + nprinted == s->matched_line &&
3079 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3080 err = add_matched_line(&width, line, view->ncols, 0,
3081 view->window, regmatch);
3082 if (err) {
3083 free(line);
3084 return err;
3086 } else {
3087 err = format_line(&wline, &width, line, view->ncols, 0);
3088 if (err) {
3089 free(line);
3090 return err;
3092 waddwstr(view->window, wline);
3093 free(wline);
3094 wline = NULL;
3096 if (tc)
3097 wattr_off(view->window,
3098 COLOR_PAIR(tc->colorpair), NULL);
3099 if (width <= view->ncols - 1)
3100 waddch(view->window, '\n');
3101 nprinted++;
3103 free(line);
3104 if (nprinted >= 1)
3105 s->last_displayed_line = s->first_displayed_line +
3106 (nprinted - 1);
3107 else
3108 s->last_displayed_line = s->first_displayed_line;
3110 view_vborder(view);
3112 if (s->eof) {
3113 while (nprinted < view->nlines) {
3114 waddch(view->window, '\n');
3115 nprinted++;
3118 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3119 if (err) {
3120 return err;
3123 wstandout(view->window);
3124 waddwstr(view->window, wline);
3125 free(wline);
3126 wline = NULL;
3127 wstandend(view->window);
3130 return NULL;
3133 static char *
3134 get_datestr(time_t *time, char *datebuf)
3136 struct tm mytm, *tm;
3137 char *p, *s;
3139 tm = gmtime_r(time, &mytm);
3140 if (tm == NULL)
3141 return NULL;
3142 s = asctime_r(tm, datebuf);
3143 if (s == NULL)
3144 return NULL;
3145 p = strchr(s, '\n');
3146 if (p)
3147 *p = '\0';
3148 return s;
3151 static const struct got_error *
3152 get_changed_paths(struct got_pathlist_head *paths,
3153 struct got_commit_object *commit, struct got_repository *repo)
3155 const struct got_error *err = NULL;
3156 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3157 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3158 struct got_object_qid *qid;
3160 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3161 if (qid != NULL) {
3162 struct got_commit_object *pcommit;
3163 err = got_object_open_as_commit(&pcommit, repo,
3164 &qid->id);
3165 if (err)
3166 return err;
3168 tree_id1 = got_object_id_dup(
3169 got_object_commit_get_tree_id(pcommit));
3170 if (tree_id1 == NULL) {
3171 got_object_commit_close(pcommit);
3172 return got_error_from_errno("got_object_id_dup");
3174 got_object_commit_close(pcommit);
3178 if (tree_id1) {
3179 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3180 if (err)
3181 goto done;
3184 tree_id2 = got_object_commit_get_tree_id(commit);
3185 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3186 if (err)
3187 goto done;
3189 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3190 got_diff_tree_collect_changed_paths, paths, 0);
3191 done:
3192 if (tree1)
3193 got_object_tree_close(tree1);
3194 if (tree2)
3195 got_object_tree_close(tree2);
3196 free(tree_id1);
3197 return err;
3200 static const struct got_error *
3201 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3203 off_t *p;
3205 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3206 if (p == NULL)
3207 return got_error_from_errno("reallocarray");
3208 *line_offsets = p;
3209 (*line_offsets)[*nlines] = off;
3210 (*nlines)++;
3211 return NULL;
3214 static const struct got_error *
3215 write_commit_info(off_t **line_offsets, size_t *nlines,
3216 struct got_object_id *commit_id, struct got_reflist_head *refs,
3217 struct got_repository *repo, FILE *outfile)
3219 const struct got_error *err = NULL;
3220 char datebuf[26], *datestr;
3221 struct got_commit_object *commit;
3222 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3223 time_t committer_time;
3224 const char *author, *committer;
3225 char *refs_str = NULL;
3226 struct got_pathlist_head changed_paths;
3227 struct got_pathlist_entry *pe;
3228 off_t outoff = 0;
3229 int n;
3231 TAILQ_INIT(&changed_paths);
3233 if (refs) {
3234 err = build_refs_str(&refs_str, refs, commit_id, repo);
3235 if (err)
3236 return err;
3239 err = got_object_open_as_commit(&commit, repo, commit_id);
3240 if (err)
3241 return err;
3243 err = got_object_id_str(&id_str, commit_id);
3244 if (err) {
3245 err = got_error_from_errno("got_object_id_str");
3246 goto done;
3249 err = add_line_offset(line_offsets, nlines, 0);
3250 if (err)
3251 goto done;
3253 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3254 refs_str ? refs_str : "", refs_str ? ")" : "");
3255 if (n < 0) {
3256 err = got_error_from_errno("fprintf");
3257 goto done;
3259 outoff += n;
3260 err = add_line_offset(line_offsets, nlines, outoff);
3261 if (err)
3262 goto done;
3264 n = fprintf(outfile, "from: %s\n",
3265 got_object_commit_get_author(commit));
3266 if (n < 0) {
3267 err = got_error_from_errno("fprintf");
3268 goto done;
3270 outoff += n;
3271 err = add_line_offset(line_offsets, nlines, outoff);
3272 if (err)
3273 goto done;
3275 committer_time = got_object_commit_get_committer_time(commit);
3276 datestr = get_datestr(&committer_time, datebuf);
3277 if (datestr) {
3278 n = fprintf(outfile, "date: %s UTC\n", datestr);
3279 if (n < 0) {
3280 err = got_error_from_errno("fprintf");
3281 goto done;
3283 outoff += n;
3284 err = add_line_offset(line_offsets, nlines, outoff);
3285 if (err)
3286 goto done;
3288 author = got_object_commit_get_author(commit);
3289 committer = got_object_commit_get_committer(commit);
3290 if (strcmp(author, committer) != 0) {
3291 n = fprintf(outfile, "via: %s\n", committer);
3292 if (n < 0) {
3293 err = got_error_from_errno("fprintf");
3294 goto done;
3296 outoff += n;
3297 err = add_line_offset(line_offsets, nlines, outoff);
3298 if (err)
3299 goto done;
3301 if (got_object_commit_get_nparents(commit) > 1) {
3302 const struct got_object_id_queue *parent_ids;
3303 struct got_object_qid *qid;
3304 int pn = 1;
3305 parent_ids = got_object_commit_get_parent_ids(commit);
3306 STAILQ_FOREACH(qid, parent_ids, entry) {
3307 err = got_object_id_str(&id_str, &qid->id);
3308 if (err)
3309 goto done;
3310 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3311 if (n < 0) {
3312 err = got_error_from_errno("fprintf");
3313 goto done;
3315 outoff += n;
3316 err = add_line_offset(line_offsets, nlines, outoff);
3317 if (err)
3318 goto done;
3319 free(id_str);
3320 id_str = NULL;
3324 err = got_object_commit_get_logmsg(&logmsg, commit);
3325 if (err)
3326 goto done;
3327 s = logmsg;
3328 while ((line = strsep(&s, "\n")) != NULL) {
3329 n = fprintf(outfile, "%s\n", line);
3330 if (n < 0) {
3331 err = got_error_from_errno("fprintf");
3332 goto done;
3334 outoff += n;
3335 err = add_line_offset(line_offsets, nlines, outoff);
3336 if (err)
3337 goto done;
3340 err = get_changed_paths(&changed_paths, commit, repo);
3341 if (err)
3342 goto done;
3343 TAILQ_FOREACH(pe, &changed_paths, entry) {
3344 struct got_diff_changed_path *cp = pe->data;
3345 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3346 if (n < 0) {
3347 err = got_error_from_errno("fprintf");
3348 goto done;
3350 outoff += n;
3351 err = add_line_offset(line_offsets, nlines, outoff);
3352 if (err)
3353 goto done;
3354 free((char *)pe->path);
3355 free(pe->data);
3358 fputc('\n', outfile);
3359 outoff++;
3360 err = add_line_offset(line_offsets, nlines, outoff);
3361 done:
3362 got_pathlist_free(&changed_paths);
3363 free(id_str);
3364 free(logmsg);
3365 free(refs_str);
3366 got_object_commit_close(commit);
3367 if (err) {
3368 free(*line_offsets);
3369 *line_offsets = NULL;
3370 *nlines = 0;
3372 return err;
3375 static const struct got_error *
3376 create_diff(struct tog_diff_view_state *s)
3378 const struct got_error *err = NULL;
3379 FILE *f = NULL;
3380 int obj_type;
3382 free(s->line_offsets);
3383 s->line_offsets = malloc(sizeof(off_t));
3384 if (s->line_offsets == NULL)
3385 return got_error_from_errno("malloc");
3386 s->nlines = 0;
3388 f = got_opentemp();
3389 if (f == NULL) {
3390 err = got_error_from_errno("got_opentemp");
3391 goto done;
3393 if (s->f && fclose(s->f) == EOF) {
3394 err = got_error_from_errno("fclose");
3395 goto done;
3397 s->f = f;
3399 if (s->id1)
3400 err = got_object_get_type(&obj_type, s->repo, s->id1);
3401 else
3402 err = got_object_get_type(&obj_type, s->repo, s->id2);
3403 if (err)
3404 goto done;
3406 switch (obj_type) {
3407 case GOT_OBJ_TYPE_BLOB:
3408 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3409 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3410 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3411 s->repo, s->f);
3412 break;
3413 case GOT_OBJ_TYPE_TREE:
3414 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3415 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3416 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3417 break;
3418 case GOT_OBJ_TYPE_COMMIT: {
3419 const struct got_object_id_queue *parent_ids;
3420 struct got_object_qid *pid;
3421 struct got_commit_object *commit2;
3422 struct got_reflist_head *refs;
3424 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3425 if (err)
3426 goto done;
3427 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3428 /* Show commit info if we're diffing to a parent/root commit. */
3429 if (s->id1 == NULL) {
3430 err = write_commit_info(&s->line_offsets, &s->nlines,
3431 s->id2, refs, s->repo, s->f);
3432 if (err)
3433 goto done;
3434 } else {
3435 parent_ids = got_object_commit_get_parent_ids(commit2);
3436 STAILQ_FOREACH(pid, parent_ids, entry) {
3437 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3438 err = write_commit_info(
3439 &s->line_offsets, &s->nlines,
3440 s->id2, refs, s->repo, s->f);
3441 if (err)
3442 goto done;
3443 break;
3447 got_object_commit_close(commit2);
3449 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3450 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3451 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3452 break;
3454 default:
3455 err = got_error(GOT_ERR_OBJ_TYPE);
3456 break;
3458 if (err)
3459 goto done;
3460 done:
3461 if (s->f && fflush(s->f) != 0 && err == NULL)
3462 err = got_error_from_errno("fflush");
3463 return err;
3466 static void
3467 diff_view_indicate_progress(struct tog_view *view)
3469 mvwaddstr(view->window, 0, 0, "diffing...");
3470 update_panels();
3471 doupdate();
3474 static const struct got_error *
3475 search_start_diff_view(struct tog_view *view)
3477 struct tog_diff_view_state *s = &view->state.diff;
3479 s->matched_line = 0;
3480 return NULL;
3483 static const struct got_error *
3484 search_next_diff_view(struct tog_view *view)
3486 struct tog_diff_view_state *s = &view->state.diff;
3487 int lineno;
3488 char *line = NULL;
3489 size_t linesize = 0;
3490 ssize_t linelen;
3492 if (!view->searching) {
3493 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3494 return NULL;
3497 if (s->matched_line) {
3498 if (view->searching == TOG_SEARCH_FORWARD)
3499 lineno = s->matched_line + 1;
3500 else
3501 lineno = s->matched_line - 1;
3502 } else
3503 lineno = s->first_displayed_line;
3505 while (1) {
3506 off_t offset;
3508 if (lineno <= 0 || lineno > s->nlines) {
3509 if (s->matched_line == 0) {
3510 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3511 break;
3514 if (view->searching == TOG_SEARCH_FORWARD)
3515 lineno = 1;
3516 else
3517 lineno = s->nlines;
3520 offset = s->line_offsets[lineno - 1];
3521 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3522 free(line);
3523 return got_error_from_errno("fseeko");
3525 linelen = getline(&line, &linesize, s->f);
3526 if (linelen != -1 &&
3527 match_line(line, &view->regex, 1, &view->regmatch)) {
3528 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3529 s->matched_line = lineno;
3530 break;
3532 if (view->searching == TOG_SEARCH_FORWARD)
3533 lineno++;
3534 else
3535 lineno--;
3537 free(line);
3539 if (s->matched_line) {
3540 s->first_displayed_line = s->matched_line;
3541 s->selected_line = 1;
3544 return NULL;
3547 static const struct got_error *
3548 close_diff_view(struct tog_view *view)
3550 const struct got_error *err = NULL;
3551 struct tog_diff_view_state *s = &view->state.diff;
3553 free(s->id1);
3554 s->id1 = NULL;
3555 free(s->id2);
3556 s->id2 = NULL;
3557 if (s->f && fclose(s->f) == EOF)
3558 err = got_error_from_errno("fclose");
3559 s->f = NULL;
3560 if (s->f1 && fclose(s->f1) == EOF)
3561 err = got_error_from_errno("fclose");
3562 s->f1 = NULL;
3563 if (s->f2 && fclose(s->f2) == EOF)
3564 err = got_error_from_errno("fclose");
3565 s->f2 = NULL;
3566 free_colors(&s->colors);
3567 free(s->line_offsets);
3568 s->line_offsets = NULL;
3569 s->nlines = 0;
3570 return err;
3573 static const struct got_error *
3574 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3575 struct got_object_id *id2, const char *label1, const char *label2,
3576 int diff_context, int ignore_whitespace, int force_text_diff,
3577 struct tog_view *log_view, struct got_repository *repo)
3579 const struct got_error *err;
3580 struct tog_diff_view_state *s = &view->state.diff;
3582 memset(s, 0, sizeof(*s));
3584 if (id1 != NULL && id2 != NULL) {
3585 int type1, type2;
3586 err = got_object_get_type(&type1, repo, id1);
3587 if (err)
3588 return err;
3589 err = got_object_get_type(&type2, repo, id2);
3590 if (err)
3591 return err;
3593 if (type1 != type2)
3594 return got_error(GOT_ERR_OBJ_TYPE);
3596 s->first_displayed_line = 1;
3597 s->last_displayed_line = view->nlines;
3598 s->selected_line = 1;
3599 s->repo = repo;
3600 s->id1 = id1;
3601 s->id2 = id2;
3602 s->label1 = label1;
3603 s->label2 = label2;
3605 if (id1) {
3606 s->id1 = got_object_id_dup(id1);
3607 if (s->id1 == NULL)
3608 return got_error_from_errno("got_object_id_dup");
3609 s->f1 = got_opentemp();
3610 if (s->f1 == NULL) {
3611 err = got_error_from_errno("got_opentemp");
3612 goto done;
3614 } else
3615 s->id1 = NULL;
3617 s->id2 = got_object_id_dup(id2);
3618 if (s->id2 == NULL) {
3619 err = got_error_from_errno("got_object_id_dup");
3620 goto done;
3623 s->f2 = got_opentemp();
3624 if (s->f2 == NULL) {
3625 err = got_error_from_errno("got_opentemp");
3626 goto done;
3629 s->first_displayed_line = 1;
3630 s->last_displayed_line = view->nlines;
3631 s->diff_context = diff_context;
3632 s->ignore_whitespace = ignore_whitespace;
3633 s->force_text_diff = force_text_diff;
3634 s->log_view = log_view;
3635 s->repo = repo;
3637 STAILQ_INIT(&s->colors);
3638 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3639 err = add_color(&s->colors,
3640 "^-", TOG_COLOR_DIFF_MINUS,
3641 get_color_value("TOG_COLOR_DIFF_MINUS"));
3642 if (err)
3643 goto done;
3644 err = add_color(&s->colors, "^\\+",
3645 TOG_COLOR_DIFF_PLUS,
3646 get_color_value("TOG_COLOR_DIFF_PLUS"));
3647 if (err)
3648 goto done;
3649 err = add_color(&s->colors,
3650 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3651 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3652 if (err)
3653 goto done;
3655 err = add_color(&s->colors,
3656 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3657 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3658 get_color_value("TOG_COLOR_DIFF_META"));
3659 if (err)
3660 goto done;
3662 err = add_color(&s->colors,
3663 "^(from|via): ", TOG_COLOR_AUTHOR,
3664 get_color_value("TOG_COLOR_AUTHOR"));
3665 if (err)
3666 goto done;
3668 err = add_color(&s->colors,
3669 "^date: ", TOG_COLOR_DATE,
3670 get_color_value("TOG_COLOR_DATE"));
3671 if (err)
3672 goto done;
3675 if (log_view && view_is_splitscreen(view))
3676 show_log_view(log_view); /* draw vborder */
3677 diff_view_indicate_progress(view);
3679 err = create_diff(s);
3681 view->show = show_diff_view;
3682 view->input = input_diff_view;
3683 view->close = close_diff_view;
3684 view->search_start = search_start_diff_view;
3685 view->search_next = search_next_diff_view;
3686 done:
3687 if (err)
3688 close_diff_view(view);
3689 return err;
3692 static const struct got_error *
3693 show_diff_view(struct tog_view *view)
3695 const struct got_error *err;
3696 struct tog_diff_view_state *s = &view->state.diff;
3697 char *id_str1 = NULL, *id_str2, *header;
3698 const char *label1, *label2;
3700 if (s->id1) {
3701 err = got_object_id_str(&id_str1, s->id1);
3702 if (err)
3703 return err;
3704 label1 = s->label1 ? : id_str1;
3705 } else
3706 label1 = "/dev/null";
3708 err = got_object_id_str(&id_str2, s->id2);
3709 if (err)
3710 return err;
3711 label2 = s->label2 ? : id_str2;
3713 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3714 err = got_error_from_errno("asprintf");
3715 free(id_str1);
3716 free(id_str2);
3717 return err;
3719 free(id_str1);
3720 free(id_str2);
3722 err = draw_file(view, header);
3723 free(header);
3724 return err;
3727 static const struct got_error *
3728 set_selected_commit(struct tog_diff_view_state *s,
3729 struct commit_queue_entry *entry)
3731 const struct got_error *err;
3732 const struct got_object_id_queue *parent_ids;
3733 struct got_commit_object *selected_commit;
3734 struct got_object_qid *pid;
3736 free(s->id2);
3737 s->id2 = got_object_id_dup(entry->id);
3738 if (s->id2 == NULL)
3739 return got_error_from_errno("got_object_id_dup");
3741 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3742 if (err)
3743 return err;
3744 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3745 free(s->id1);
3746 pid = STAILQ_FIRST(parent_ids);
3747 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3748 got_object_commit_close(selected_commit);
3749 return NULL;
3752 static const struct got_error *
3753 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3755 const struct got_error *err = NULL;
3756 struct tog_diff_view_state *s = &view->state.diff;
3757 struct tog_log_view_state *ls;
3758 struct commit_queue_entry *old_selected_entry;
3759 char *line = NULL;
3760 size_t linesize = 0;
3761 ssize_t linelen;
3762 int i, nscroll = view->nlines - 1;
3764 switch (ch) {
3765 case 'a':
3766 case 'w':
3767 if (ch == 'a')
3768 s->force_text_diff = !s->force_text_diff;
3769 if (ch == 'w')
3770 s->ignore_whitespace = !s->ignore_whitespace;
3771 wclear(view->window);
3772 s->first_displayed_line = 1;
3773 s->last_displayed_line = view->nlines;
3774 s->matched_line = 0;
3775 diff_view_indicate_progress(view);
3776 err = create_diff(s);
3777 break;
3778 case 'g':
3779 case KEY_HOME:
3780 s->first_displayed_line = 1;
3781 break;
3782 case 'G':
3783 case KEY_END:
3784 if (s->eof)
3785 break;
3787 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3788 s->eof = 1;
3789 break;
3790 case 'k':
3791 case KEY_UP:
3792 case CTRL('p'):
3793 if (s->first_displayed_line > 1)
3794 s->first_displayed_line--;
3795 break;
3796 case CTRL('u'):
3797 case 'u':
3798 nscroll /= 2;
3799 /* FALL THROUGH */
3800 case KEY_PPAGE:
3801 case CTRL('b'):
3802 if (s->first_displayed_line == 1)
3803 break;
3804 i = 0;
3805 while (i++ < nscroll && s->first_displayed_line > 1)
3806 s->first_displayed_line--;
3807 break;
3808 case 'j':
3809 case KEY_DOWN:
3810 case CTRL('n'):
3811 if (!s->eof)
3812 s->first_displayed_line++;
3813 break;
3814 case CTRL('d'):
3815 case 'd':
3816 nscroll /= 2;
3817 /* FALL THROUGH */
3818 case KEY_NPAGE:
3819 case CTRL('f'):
3820 case ' ':
3821 if (s->eof)
3822 break;
3823 i = 0;
3824 while (!s->eof && i++ < nscroll) {
3825 linelen = getline(&line, &linesize, s->f);
3826 s->first_displayed_line++;
3827 if (linelen == -1) {
3828 if (feof(s->f)) {
3829 s->eof = 1;
3830 } else
3831 err = got_ferror(s->f, GOT_ERR_IO);
3832 break;
3835 free(line);
3836 break;
3837 case '[':
3838 if (s->diff_context > 0) {
3839 s->diff_context--;
3840 s->matched_line = 0;
3841 diff_view_indicate_progress(view);
3842 err = create_diff(s);
3843 if (s->first_displayed_line + view->nlines - 1 >
3844 s->nlines) {
3845 s->first_displayed_line = 1;
3846 s->last_displayed_line = view->nlines;
3849 break;
3850 case ']':
3851 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3852 s->diff_context++;
3853 s->matched_line = 0;
3854 diff_view_indicate_progress(view);
3855 err = create_diff(s);
3857 break;
3858 case '<':
3859 case ',':
3860 if (s->log_view == NULL)
3861 break;
3862 ls = &s->log_view->state.log;
3863 old_selected_entry = ls->selected_entry;
3865 err = input_log_view(NULL, s->log_view, KEY_UP);
3866 if (err)
3867 break;
3869 if (old_selected_entry == ls->selected_entry)
3870 break;
3872 err = set_selected_commit(s, ls->selected_entry);
3873 if (err)
3874 break;
3876 s->first_displayed_line = 1;
3877 s->last_displayed_line = view->nlines;
3878 s->matched_line = 0;
3880 diff_view_indicate_progress(view);
3881 err = create_diff(s);
3882 break;
3883 case '>':
3884 case '.':
3885 if (s->log_view == NULL)
3886 break;
3887 ls = &s->log_view->state.log;
3888 old_selected_entry = ls->selected_entry;
3890 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3891 if (err)
3892 break;
3894 if (old_selected_entry == ls->selected_entry)
3895 break;
3897 err = set_selected_commit(s, ls->selected_entry);
3898 if (err)
3899 break;
3901 s->first_displayed_line = 1;
3902 s->last_displayed_line = view->nlines;
3903 s->matched_line = 0;
3905 diff_view_indicate_progress(view);
3906 err = create_diff(s);
3907 break;
3908 default:
3909 break;
3912 return err;
3915 static const struct got_error *
3916 cmd_diff(int argc, char *argv[])
3918 const struct got_error *error = NULL;
3919 struct got_repository *repo = NULL;
3920 struct got_worktree *worktree = NULL;
3921 struct got_object_id *id1 = NULL, *id2 = NULL;
3922 char *repo_path = NULL, *cwd = NULL;
3923 char *id_str1 = NULL, *id_str2 = NULL;
3924 char *label1 = NULL, *label2 = NULL;
3925 int diff_context = 3, ignore_whitespace = 0;
3926 int ch, force_text_diff = 0;
3927 const char *errstr;
3928 struct tog_view *view;
3930 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3931 switch (ch) {
3932 case 'a':
3933 force_text_diff = 1;
3934 break;
3935 case 'C':
3936 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3937 &errstr);
3938 if (errstr != NULL)
3939 errx(1, "number of context lines is %s: %s",
3940 errstr, errstr);
3941 break;
3942 case 'r':
3943 repo_path = realpath(optarg, NULL);
3944 if (repo_path == NULL)
3945 return got_error_from_errno2("realpath",
3946 optarg);
3947 got_path_strip_trailing_slashes(repo_path);
3948 break;
3949 case 'w':
3950 ignore_whitespace = 1;
3951 break;
3952 default:
3953 usage_diff();
3954 /* NOTREACHED */
3958 argc -= optind;
3959 argv += optind;
3961 if (argc == 0) {
3962 usage_diff(); /* TODO show local worktree changes */
3963 } else if (argc == 2) {
3964 id_str1 = argv[0];
3965 id_str2 = argv[1];
3966 } else
3967 usage_diff();
3969 if (repo_path == NULL) {
3970 cwd = getcwd(NULL, 0);
3971 if (cwd == NULL)
3972 return got_error_from_errno("getcwd");
3973 error = got_worktree_open(&worktree, cwd);
3974 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3975 goto done;
3976 if (worktree)
3977 repo_path =
3978 strdup(got_worktree_get_repo_path(worktree));
3979 else
3980 repo_path = strdup(cwd);
3981 if (repo_path == NULL) {
3982 error = got_error_from_errno("strdup");
3983 goto done;
3987 error = got_repo_open(&repo, repo_path, NULL);
3988 if (error)
3989 goto done;
3991 init_curses();
3993 error = apply_unveil(got_repo_get_path(repo), NULL);
3994 if (error)
3995 goto done;
3997 error = tog_load_refs(repo, 0);
3998 if (error)
3999 goto done;
4001 error = got_repo_match_object_id(&id1, &label1, id_str1,
4002 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4003 if (error)
4004 goto done;
4006 error = got_repo_match_object_id(&id2, &label2, id_str2,
4007 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4008 if (error)
4009 goto done;
4011 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4012 if (view == NULL) {
4013 error = got_error_from_errno("view_open");
4014 goto done;
4016 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4017 ignore_whitespace, force_text_diff, NULL, repo);
4018 if (error)
4019 goto done;
4020 error = view_loop(view);
4021 done:
4022 free(label1);
4023 free(label2);
4024 free(repo_path);
4025 free(cwd);
4026 if (repo) {
4027 const struct got_error *close_err = got_repo_close(repo);
4028 if (error == NULL)
4029 error = close_err;
4031 if (worktree)
4032 got_worktree_close(worktree);
4033 tog_free_refs();
4034 return error;
4037 __dead static void
4038 usage_blame(void)
4040 endwin();
4041 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4042 getprogname());
4043 exit(1);
4046 struct tog_blame_line {
4047 int annotated;
4048 struct got_object_id *id;
4051 static const struct got_error *
4052 draw_blame(struct tog_view *view)
4054 struct tog_blame_view_state *s = &view->state.blame;
4055 struct tog_blame *blame = &s->blame;
4056 regmatch_t *regmatch = &view->regmatch;
4057 const struct got_error *err;
4058 int lineno = 0, nprinted = 0;
4059 char *line = NULL;
4060 size_t linesize = 0;
4061 ssize_t linelen;
4062 wchar_t *wline;
4063 int width;
4064 struct tog_blame_line *blame_line;
4065 struct got_object_id *prev_id = NULL;
4066 char *id_str;
4067 struct tog_color *tc;
4069 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4070 if (err)
4071 return err;
4073 rewind(blame->f);
4074 werase(view->window);
4076 if (asprintf(&line, "commit %s", id_str) == -1) {
4077 err = got_error_from_errno("asprintf");
4078 free(id_str);
4079 return err;
4082 err = format_line(&wline, &width, line, view->ncols, 0);
4083 free(line);
4084 line = NULL;
4085 if (err)
4086 return err;
4087 if (view_needs_focus_indication(view))
4088 wstandout(view->window);
4089 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4090 if (tc)
4091 wattr_on(view->window,
4092 COLOR_PAIR(tc->colorpair), NULL);
4093 waddwstr(view->window, wline);
4094 if (tc)
4095 wattr_off(view->window,
4096 COLOR_PAIR(tc->colorpair), NULL);
4097 if (view_needs_focus_indication(view))
4098 wstandend(view->window);
4099 free(wline);
4100 wline = NULL;
4101 if (width < view->ncols - 1)
4102 waddch(view->window, '\n');
4104 if (asprintf(&line, "[%d/%d] %s%s",
4105 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4106 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4107 free(id_str);
4108 return got_error_from_errno("asprintf");
4110 free(id_str);
4111 err = format_line(&wline, &width, line, view->ncols, 0);
4112 free(line);
4113 line = NULL;
4114 if (err)
4115 return err;
4116 waddwstr(view->window, wline);
4117 free(wline);
4118 wline = NULL;
4119 if (width < view->ncols - 1)
4120 waddch(view->window, '\n');
4122 s->eof = 0;
4123 while (nprinted < view->nlines - 2) {
4124 linelen = getline(&line, &linesize, blame->f);
4125 if (linelen == -1) {
4126 if (feof(blame->f)) {
4127 s->eof = 1;
4128 break;
4130 free(line);
4131 return got_ferror(blame->f, GOT_ERR_IO);
4133 if (++lineno < s->first_displayed_line)
4134 continue;
4136 if (view->focussed && nprinted == s->selected_line - 1)
4137 wstandout(view->window);
4139 if (blame->nlines > 0) {
4140 blame_line = &blame->lines[lineno - 1];
4141 if (blame_line->annotated && prev_id &&
4142 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4143 !(view->focussed &&
4144 nprinted == s->selected_line - 1)) {
4145 waddstr(view->window, " ");
4146 } else if (blame_line->annotated) {
4147 char *id_str;
4148 err = got_object_id_str(&id_str, blame_line->id);
4149 if (err) {
4150 free(line);
4151 return err;
4153 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4154 if (tc)
4155 wattr_on(view->window,
4156 COLOR_PAIR(tc->colorpair), NULL);
4157 wprintw(view->window, "%.8s", id_str);
4158 if (tc)
4159 wattr_off(view->window,
4160 COLOR_PAIR(tc->colorpair), NULL);
4161 free(id_str);
4162 prev_id = blame_line->id;
4163 } else {
4164 waddstr(view->window, "........");
4165 prev_id = NULL;
4167 } else {
4168 waddstr(view->window, "........");
4169 prev_id = NULL;
4172 if (view->focussed && nprinted == s->selected_line - 1)
4173 wstandend(view->window);
4174 waddstr(view->window, " ");
4176 if (view->ncols <= 9) {
4177 width = 9;
4178 wline = wcsdup(L"");
4179 if (wline == NULL) {
4180 err = got_error_from_errno("wcsdup");
4181 free(line);
4182 return err;
4184 } else if (s->first_displayed_line + nprinted ==
4185 s->matched_line &&
4186 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4187 err = add_matched_line(&width, line, view->ncols - 9, 9,
4188 view->window, regmatch);
4189 if (err) {
4190 free(line);
4191 return err;
4193 width += 9;
4194 } else {
4195 err = format_line(&wline, &width, line,
4196 view->ncols - 9, 9);
4197 waddwstr(view->window, wline);
4198 free(wline);
4199 wline = NULL;
4200 width += 9;
4203 if (width <= view->ncols - 1)
4204 waddch(view->window, '\n');
4205 if (++nprinted == 1)
4206 s->first_displayed_line = lineno;
4208 free(line);
4209 s->last_displayed_line = lineno;
4211 view_vborder(view);
4213 return NULL;
4216 static const struct got_error *
4217 blame_cb(void *arg, int nlines, int lineno,
4218 struct got_commit_object *commit, struct got_object_id *id)
4220 const struct got_error *err = NULL;
4221 struct tog_blame_cb_args *a = arg;
4222 struct tog_blame_line *line;
4223 int errcode;
4225 if (nlines != a->nlines ||
4226 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4227 return got_error(GOT_ERR_RANGE);
4229 errcode = pthread_mutex_lock(&tog_mutex);
4230 if (errcode)
4231 return got_error_set_errno(errcode, "pthread_mutex_lock");
4233 if (*a->quit) { /* user has quit the blame view */
4234 err = got_error(GOT_ERR_ITER_COMPLETED);
4235 goto done;
4238 if (lineno == -1)
4239 goto done; /* no change in this commit */
4241 line = &a->lines[lineno - 1];
4242 if (line->annotated)
4243 goto done;
4245 line->id = got_object_id_dup(id);
4246 if (line->id == NULL) {
4247 err = got_error_from_errno("got_object_id_dup");
4248 goto done;
4250 line->annotated = 1;
4251 done:
4252 errcode = pthread_mutex_unlock(&tog_mutex);
4253 if (errcode)
4254 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4255 return err;
4258 static void *
4259 blame_thread(void *arg)
4261 const struct got_error *err, *close_err;
4262 struct tog_blame_thread_args *ta = arg;
4263 struct tog_blame_cb_args *a = ta->cb_args;
4264 int errcode;
4266 err = block_signals_used_by_main_thread();
4267 if (err)
4268 return (void *)err;
4270 err = got_blame(ta->path, a->commit_id, ta->repo,
4271 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4272 if (err && err->code == GOT_ERR_CANCELLED)
4273 err = NULL;
4275 errcode = pthread_mutex_lock(&tog_mutex);
4276 if (errcode)
4277 return (void *)got_error_set_errno(errcode,
4278 "pthread_mutex_lock");
4280 close_err = got_repo_close(ta->repo);
4281 if (err == NULL)
4282 err = close_err;
4283 ta->repo = NULL;
4284 *ta->complete = 1;
4286 errcode = pthread_mutex_unlock(&tog_mutex);
4287 if (errcode && err == NULL)
4288 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4290 return (void *)err;
4293 static struct got_object_id *
4294 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4295 int first_displayed_line, int selected_line)
4297 struct tog_blame_line *line;
4299 if (nlines <= 0)
4300 return NULL;
4302 line = &lines[first_displayed_line - 1 + selected_line - 1];
4303 if (!line->annotated)
4304 return NULL;
4306 return line->id;
4309 static const struct got_error *
4310 stop_blame(struct tog_blame *blame)
4312 const struct got_error *err = NULL;
4313 int i;
4315 if (blame->thread) {
4316 int errcode;
4317 errcode = pthread_mutex_unlock(&tog_mutex);
4318 if (errcode)
4319 return got_error_set_errno(errcode,
4320 "pthread_mutex_unlock");
4321 errcode = pthread_join(blame->thread, (void **)&err);
4322 if (errcode)
4323 return got_error_set_errno(errcode, "pthread_join");
4324 errcode = pthread_mutex_lock(&tog_mutex);
4325 if (errcode)
4326 return got_error_set_errno(errcode,
4327 "pthread_mutex_lock");
4328 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4329 err = NULL;
4330 blame->thread = NULL;
4332 if (blame->thread_args.repo) {
4333 const struct got_error *close_err;
4334 close_err = got_repo_close(blame->thread_args.repo);
4335 if (err == NULL)
4336 err = close_err;
4337 blame->thread_args.repo = NULL;
4339 if (blame->f) {
4340 if (fclose(blame->f) == EOF && err == NULL)
4341 err = got_error_from_errno("fclose");
4342 blame->f = NULL;
4344 if (blame->lines) {
4345 for (i = 0; i < blame->nlines; i++)
4346 free(blame->lines[i].id);
4347 free(blame->lines);
4348 blame->lines = NULL;
4350 free(blame->cb_args.commit_id);
4351 blame->cb_args.commit_id = NULL;
4353 return err;
4356 static const struct got_error *
4357 cancel_blame_view(void *arg)
4359 const struct got_error *err = NULL;
4360 int *done = arg;
4361 int errcode;
4363 errcode = pthread_mutex_lock(&tog_mutex);
4364 if (errcode)
4365 return got_error_set_errno(errcode,
4366 "pthread_mutex_unlock");
4368 if (*done)
4369 err = got_error(GOT_ERR_CANCELLED);
4371 errcode = pthread_mutex_unlock(&tog_mutex);
4372 if (errcode)
4373 return got_error_set_errno(errcode,
4374 "pthread_mutex_lock");
4376 return err;
4379 static const struct got_error *
4380 run_blame(struct tog_view *view)
4382 struct tog_blame_view_state *s = &view->state.blame;
4383 struct tog_blame *blame = &s->blame;
4384 const struct got_error *err = NULL;
4385 struct got_commit_object *commit = NULL;
4386 struct got_blob_object *blob = NULL;
4387 struct got_repository *thread_repo = NULL;
4388 struct got_object_id *obj_id = NULL;
4389 int obj_type;
4391 err = got_object_open_as_commit(&commit, s->repo,
4392 &s->blamed_commit->id);
4393 if (err)
4394 return err;
4396 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4397 if (err)
4398 goto done;
4400 err = got_object_get_type(&obj_type, s->repo, obj_id);
4401 if (err)
4402 goto done;
4404 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4405 err = got_error(GOT_ERR_OBJ_TYPE);
4406 goto done;
4409 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4410 if (err)
4411 goto done;
4412 blame->f = got_opentemp();
4413 if (blame->f == NULL) {
4414 err = got_error_from_errno("got_opentemp");
4415 goto done;
4417 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4418 &blame->line_offsets, blame->f, blob);
4419 if (err)
4420 goto done;
4421 if (blame->nlines == 0) {
4422 s->blame_complete = 1;
4423 goto done;
4426 /* Don't include \n at EOF in the blame line count. */
4427 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4428 blame->nlines--;
4430 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4431 if (blame->lines == NULL) {
4432 err = got_error_from_errno("calloc");
4433 goto done;
4436 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4437 if (err)
4438 goto done;
4440 blame->cb_args.view = view;
4441 blame->cb_args.lines = blame->lines;
4442 blame->cb_args.nlines = blame->nlines;
4443 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4444 if (blame->cb_args.commit_id == NULL) {
4445 err = got_error_from_errno("got_object_id_dup");
4446 goto done;
4448 blame->cb_args.quit = &s->done;
4450 blame->thread_args.path = s->path;
4451 blame->thread_args.repo = thread_repo;
4452 blame->thread_args.cb_args = &blame->cb_args;
4453 blame->thread_args.complete = &s->blame_complete;
4454 blame->thread_args.cancel_cb = cancel_blame_view;
4455 blame->thread_args.cancel_arg = &s->done;
4456 s->blame_complete = 0;
4458 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4459 s->first_displayed_line = 1;
4460 s->last_displayed_line = view->nlines;
4461 s->selected_line = 1;
4463 s->matched_line = 0;
4465 done:
4466 if (commit)
4467 got_object_commit_close(commit);
4468 if (blob)
4469 got_object_blob_close(blob);
4470 free(obj_id);
4471 if (err)
4472 stop_blame(blame);
4473 return err;
4476 static const struct got_error *
4477 open_blame_view(struct tog_view *view, char *path,
4478 struct got_object_id *commit_id, struct got_repository *repo)
4480 const struct got_error *err = NULL;
4481 struct tog_blame_view_state *s = &view->state.blame;
4483 STAILQ_INIT(&s->blamed_commits);
4485 s->path = strdup(path);
4486 if (s->path == NULL)
4487 return got_error_from_errno("strdup");
4489 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4490 if (err) {
4491 free(s->path);
4492 return err;
4495 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4496 s->first_displayed_line = 1;
4497 s->last_displayed_line = view->nlines;
4498 s->selected_line = 1;
4499 s->blame_complete = 0;
4500 s->repo = repo;
4501 s->commit_id = commit_id;
4502 memset(&s->blame, 0, sizeof(s->blame));
4504 STAILQ_INIT(&s->colors);
4505 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4506 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4507 get_color_value("TOG_COLOR_COMMIT"));
4508 if (err)
4509 return err;
4512 view->show = show_blame_view;
4513 view->input = input_blame_view;
4514 view->close = close_blame_view;
4515 view->search_start = search_start_blame_view;
4516 view->search_next = search_next_blame_view;
4518 return run_blame(view);
4521 static const struct got_error *
4522 close_blame_view(struct tog_view *view)
4524 const struct got_error *err = NULL;
4525 struct tog_blame_view_state *s = &view->state.blame;
4527 if (s->blame.thread)
4528 err = stop_blame(&s->blame);
4530 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4531 struct got_object_qid *blamed_commit;
4532 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4533 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4534 got_object_qid_free(blamed_commit);
4537 free(s->path);
4538 free_colors(&s->colors);
4540 return err;
4543 static const struct got_error *
4544 search_start_blame_view(struct tog_view *view)
4546 struct tog_blame_view_state *s = &view->state.blame;
4548 s->matched_line = 0;
4549 return NULL;
4552 static const struct got_error *
4553 search_next_blame_view(struct tog_view *view)
4555 struct tog_blame_view_state *s = &view->state.blame;
4556 int lineno;
4557 char *line = NULL;
4558 size_t linesize = 0;
4559 ssize_t linelen;
4561 if (!view->searching) {
4562 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4563 return NULL;
4566 if (s->matched_line) {
4567 if (view->searching == TOG_SEARCH_FORWARD)
4568 lineno = s->matched_line + 1;
4569 else
4570 lineno = s->matched_line - 1;
4571 } else
4572 lineno = s->first_displayed_line - 1 + s->selected_line;
4574 while (1) {
4575 off_t offset;
4577 if (lineno <= 0 || lineno > s->blame.nlines) {
4578 if (s->matched_line == 0) {
4579 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4580 break;
4583 if (view->searching == TOG_SEARCH_FORWARD)
4584 lineno = 1;
4585 else
4586 lineno = s->blame.nlines;
4589 offset = s->blame.line_offsets[lineno - 1];
4590 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4591 free(line);
4592 return got_error_from_errno("fseeko");
4594 linelen = getline(&line, &linesize, s->blame.f);
4595 if (linelen != -1 &&
4596 match_line(line, &view->regex, 1, &view->regmatch)) {
4597 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4598 s->matched_line = lineno;
4599 break;
4601 if (view->searching == TOG_SEARCH_FORWARD)
4602 lineno++;
4603 else
4604 lineno--;
4606 free(line);
4608 if (s->matched_line) {
4609 s->first_displayed_line = s->matched_line;
4610 s->selected_line = 1;
4613 return NULL;
4616 static const struct got_error *
4617 show_blame_view(struct tog_view *view)
4619 const struct got_error *err = NULL;
4620 struct tog_blame_view_state *s = &view->state.blame;
4621 int errcode;
4623 if (s->blame.thread == NULL && !s->blame_complete) {
4624 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4625 &s->blame.thread_args);
4626 if (errcode)
4627 return got_error_set_errno(errcode, "pthread_create");
4629 halfdelay(1); /* fast refresh while annotating */
4632 if (s->blame_complete)
4633 halfdelay(10); /* disable fast refresh */
4635 err = draw_blame(view);
4637 view_vborder(view);
4638 return err;
4641 static const struct got_error *
4642 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4644 const struct got_error *err = NULL, *thread_err = NULL;
4645 struct tog_view *diff_view;
4646 struct tog_blame_view_state *s = &view->state.blame;
4647 int begin_x = 0, nscroll = view->nlines - 2;
4649 switch (ch) {
4650 case 'q':
4651 s->done = 1;
4652 break;
4653 case 'g':
4654 case KEY_HOME:
4655 s->selected_line = 1;
4656 s->first_displayed_line = 1;
4657 break;
4658 case 'G':
4659 case KEY_END:
4660 if (s->blame.nlines < view->nlines - 2) {
4661 s->selected_line = s->blame.nlines;
4662 s->first_displayed_line = 1;
4663 } else {
4664 s->selected_line = view->nlines - 2;
4665 s->first_displayed_line = s->blame.nlines -
4666 (view->nlines - 3);
4668 break;
4669 case 'k':
4670 case KEY_UP:
4671 case CTRL('p'):
4672 if (s->selected_line > 1)
4673 s->selected_line--;
4674 else if (s->selected_line == 1 &&
4675 s->first_displayed_line > 1)
4676 s->first_displayed_line--;
4677 break;
4678 case CTRL('u'):
4679 case 'u':
4680 nscroll /= 2;
4681 /* FALL THROUGH */
4682 case KEY_PPAGE:
4683 case CTRL('b'):
4684 if (s->first_displayed_line == 1) {
4685 s->selected_line = MAX(1, s->selected_line - nscroll);
4686 break;
4688 if (s->first_displayed_line > nscroll)
4689 s->first_displayed_line -= nscroll;
4690 else
4691 s->first_displayed_line = 1;
4692 break;
4693 case 'j':
4694 case KEY_DOWN:
4695 case CTRL('n'):
4696 if (s->selected_line < view->nlines - 2 &&
4697 s->first_displayed_line +
4698 s->selected_line <= s->blame.nlines)
4699 s->selected_line++;
4700 else if (s->last_displayed_line <
4701 s->blame.nlines)
4702 s->first_displayed_line++;
4703 break;
4704 case 'b':
4705 case 'p': {
4706 struct got_object_id *id = NULL;
4707 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4708 s->first_displayed_line, s->selected_line);
4709 if (id == NULL)
4710 break;
4711 if (ch == 'p') {
4712 struct got_commit_object *commit, *pcommit;
4713 struct got_object_qid *pid;
4714 struct got_object_id *blob_id = NULL;
4715 int obj_type;
4716 err = got_object_open_as_commit(&commit,
4717 s->repo, id);
4718 if (err)
4719 break;
4720 pid = STAILQ_FIRST(
4721 got_object_commit_get_parent_ids(commit));
4722 if (pid == NULL) {
4723 got_object_commit_close(commit);
4724 break;
4726 /* Check if path history ends here. */
4727 err = got_object_open_as_commit(&pcommit,
4728 s->repo, &pid->id);
4729 if (err)
4730 break;
4731 err = got_object_id_by_path(&blob_id, s->repo,
4732 pcommit, s->path);
4733 got_object_commit_close(pcommit);
4734 if (err) {
4735 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4736 err = NULL;
4737 got_object_commit_close(commit);
4738 break;
4740 err = got_object_get_type(&obj_type, s->repo,
4741 blob_id);
4742 free(blob_id);
4743 /* Can't blame non-blob type objects. */
4744 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4745 got_object_commit_close(commit);
4746 break;
4748 err = got_object_qid_alloc(&s->blamed_commit,
4749 &pid->id);
4750 got_object_commit_close(commit);
4751 } else {
4752 if (got_object_id_cmp(id,
4753 &s->blamed_commit->id) == 0)
4754 break;
4755 err = got_object_qid_alloc(&s->blamed_commit,
4756 id);
4758 if (err)
4759 break;
4760 s->done = 1;
4761 thread_err = stop_blame(&s->blame);
4762 s->done = 0;
4763 if (thread_err)
4764 break;
4765 STAILQ_INSERT_HEAD(&s->blamed_commits,
4766 s->blamed_commit, entry);
4767 err = run_blame(view);
4768 if (err)
4769 break;
4770 break;
4772 case 'B': {
4773 struct got_object_qid *first;
4774 first = STAILQ_FIRST(&s->blamed_commits);
4775 if (!got_object_id_cmp(&first->id, s->commit_id))
4776 break;
4777 s->done = 1;
4778 thread_err = stop_blame(&s->blame);
4779 s->done = 0;
4780 if (thread_err)
4781 break;
4782 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4783 got_object_qid_free(s->blamed_commit);
4784 s->blamed_commit =
4785 STAILQ_FIRST(&s->blamed_commits);
4786 err = run_blame(view);
4787 if (err)
4788 break;
4789 break;
4791 case KEY_ENTER:
4792 case '\r': {
4793 struct got_object_id *id = NULL;
4794 struct got_object_qid *pid;
4795 struct got_commit_object *commit = NULL;
4796 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4797 s->first_displayed_line, s->selected_line);
4798 if (id == NULL)
4799 break;
4800 err = got_object_open_as_commit(&commit, s->repo, id);
4801 if (err)
4802 break;
4803 pid = STAILQ_FIRST(
4804 got_object_commit_get_parent_ids(commit));
4805 if (view_is_parent_view(view))
4806 begin_x = view_split_begin_x(view->begin_x);
4807 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4808 if (diff_view == NULL) {
4809 got_object_commit_close(commit);
4810 err = got_error_from_errno("view_open");
4811 break;
4813 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4814 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4815 got_object_commit_close(commit);
4816 if (err) {
4817 view_close(diff_view);
4818 break;
4820 view->focussed = 0;
4821 diff_view->focussed = 1;
4822 if (view_is_parent_view(view)) {
4823 err = view_close_child(view);
4824 if (err)
4825 break;
4826 view_set_child(view, diff_view);
4827 view->focus_child = 1;
4828 } else
4829 *new_view = diff_view;
4830 if (err)
4831 break;
4832 break;
4834 case CTRL('d'):
4835 case 'd':
4836 nscroll /= 2;
4837 /* FALL THROUGH */
4838 case KEY_NPAGE:
4839 case CTRL('f'):
4840 case ' ':
4841 if (s->last_displayed_line >= s->blame.nlines &&
4842 s->selected_line >= MIN(s->blame.nlines,
4843 view->nlines - 2)) {
4844 break;
4846 if (s->last_displayed_line >= s->blame.nlines &&
4847 s->selected_line < view->nlines - 2) {
4848 s->selected_line +=
4849 MIN(nscroll, s->last_displayed_line -
4850 s->first_displayed_line - s->selected_line + 1);
4852 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4853 s->first_displayed_line += nscroll;
4854 else
4855 s->first_displayed_line =
4856 s->blame.nlines - (view->nlines - 3);
4857 break;
4858 case KEY_RESIZE:
4859 if (s->selected_line > view->nlines - 2) {
4860 s->selected_line = MIN(s->blame.nlines,
4861 view->nlines - 2);
4863 break;
4864 default:
4865 break;
4867 return thread_err ? thread_err : err;
4870 static const struct got_error *
4871 cmd_blame(int argc, char *argv[])
4873 const struct got_error *error;
4874 struct got_repository *repo = NULL;
4875 struct got_worktree *worktree = NULL;
4876 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4877 char *link_target = NULL;
4878 struct got_object_id *commit_id = NULL;
4879 struct got_commit_object *commit = NULL;
4880 char *commit_id_str = NULL;
4881 int ch;
4882 struct tog_view *view;
4884 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4885 switch (ch) {
4886 case 'c':
4887 commit_id_str = optarg;
4888 break;
4889 case 'r':
4890 repo_path = realpath(optarg, NULL);
4891 if (repo_path == NULL)
4892 return got_error_from_errno2("realpath",
4893 optarg);
4894 break;
4895 default:
4896 usage_blame();
4897 /* NOTREACHED */
4901 argc -= optind;
4902 argv += optind;
4904 if (argc != 1)
4905 usage_blame();
4907 if (repo_path == NULL) {
4908 cwd = getcwd(NULL, 0);
4909 if (cwd == NULL)
4910 return got_error_from_errno("getcwd");
4911 error = got_worktree_open(&worktree, cwd);
4912 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4913 goto done;
4914 if (worktree)
4915 repo_path =
4916 strdup(got_worktree_get_repo_path(worktree));
4917 else
4918 repo_path = strdup(cwd);
4919 if (repo_path == NULL) {
4920 error = got_error_from_errno("strdup");
4921 goto done;
4925 error = got_repo_open(&repo, repo_path, NULL);
4926 if (error != NULL)
4927 goto done;
4929 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4930 worktree);
4931 if (error)
4932 goto done;
4934 init_curses();
4936 error = apply_unveil(got_repo_get_path(repo), NULL);
4937 if (error)
4938 goto done;
4940 error = tog_load_refs(repo, 0);
4941 if (error)
4942 goto done;
4944 if (commit_id_str == NULL) {
4945 struct got_reference *head_ref;
4946 error = got_ref_open(&head_ref, repo, worktree ?
4947 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4948 if (error != NULL)
4949 goto done;
4950 error = got_ref_resolve(&commit_id, repo, head_ref);
4951 got_ref_close(head_ref);
4952 } else {
4953 error = got_repo_match_object_id(&commit_id, NULL,
4954 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4956 if (error != NULL)
4957 goto done;
4959 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4960 if (view == NULL) {
4961 error = got_error_from_errno("view_open");
4962 goto done;
4965 error = got_object_open_as_commit(&commit, repo, commit_id);
4966 if (error)
4967 goto done;
4969 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4970 commit, repo);
4971 if (error)
4972 goto done;
4974 error = open_blame_view(view, link_target ? link_target : in_repo_path,
4975 commit_id, repo);
4976 if (error)
4977 goto done;
4978 if (worktree) {
4979 /* Release work tree lock. */
4980 got_worktree_close(worktree);
4981 worktree = NULL;
4983 error = view_loop(view);
4984 done:
4985 free(repo_path);
4986 free(in_repo_path);
4987 free(link_target);
4988 free(cwd);
4989 free(commit_id);
4990 if (commit)
4991 got_object_commit_close(commit);
4992 if (worktree)
4993 got_worktree_close(worktree);
4994 if (repo) {
4995 const struct got_error *close_err = got_repo_close(repo);
4996 if (error == NULL)
4997 error = close_err;
4999 tog_free_refs();
5000 return error;
5003 static const struct got_error *
5004 draw_tree_entries(struct tog_view *view, const char *parent_path)
5006 struct tog_tree_view_state *s = &view->state.tree;
5007 const struct got_error *err = NULL;
5008 struct got_tree_entry *te;
5009 wchar_t *wline;
5010 struct tog_color *tc;
5011 int width, n, i, nentries;
5012 int limit = view->nlines;
5014 s->ndisplayed = 0;
5016 werase(view->window);
5018 if (limit == 0)
5019 return NULL;
5021 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5022 if (err)
5023 return err;
5024 if (view_needs_focus_indication(view))
5025 wstandout(view->window);
5026 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5027 if (tc)
5028 wattr_on(view->window,
5029 COLOR_PAIR(tc->colorpair), NULL);
5030 waddwstr(view->window, wline);
5031 if (tc)
5032 wattr_off(view->window,
5033 COLOR_PAIR(tc->colorpair), NULL);
5034 if (view_needs_focus_indication(view))
5035 wstandend(view->window);
5036 free(wline);
5037 wline = NULL;
5038 if (width < view->ncols - 1)
5039 waddch(view->window, '\n');
5040 if (--limit <= 0)
5041 return NULL;
5042 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5043 if (err)
5044 return err;
5045 waddwstr(view->window, wline);
5046 free(wline);
5047 wline = NULL;
5048 if (width < view->ncols - 1)
5049 waddch(view->window, '\n');
5050 if (--limit <= 0)
5051 return NULL;
5052 waddch(view->window, '\n');
5053 if (--limit <= 0)
5054 return NULL;
5056 if (s->first_displayed_entry == NULL) {
5057 te = got_object_tree_get_first_entry(s->tree);
5058 if (s->selected == 0) {
5059 if (view->focussed)
5060 wstandout(view->window);
5061 s->selected_entry = NULL;
5063 waddstr(view->window, " ..\n"); /* parent directory */
5064 if (s->selected == 0 && view->focussed)
5065 wstandend(view->window);
5066 s->ndisplayed++;
5067 if (--limit <= 0)
5068 return NULL;
5069 n = 1;
5070 } else {
5071 n = 0;
5072 te = s->first_displayed_entry;
5075 nentries = got_object_tree_get_nentries(s->tree);
5076 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5077 char *line = NULL, *id_str = NULL, *link_target = NULL;
5078 const char *modestr = "";
5079 mode_t mode;
5081 te = got_object_tree_get_entry(s->tree, i);
5082 mode = got_tree_entry_get_mode(te);
5084 if (s->show_ids) {
5085 err = got_object_id_str(&id_str,
5086 got_tree_entry_get_id(te));
5087 if (err)
5088 return got_error_from_errno(
5089 "got_object_id_str");
5091 if (got_object_tree_entry_is_submodule(te))
5092 modestr = "$";
5093 else if (S_ISLNK(mode)) {
5094 int i;
5096 err = got_tree_entry_get_symlink_target(&link_target,
5097 te, s->repo);
5098 if (err) {
5099 free(id_str);
5100 return err;
5102 for (i = 0; i < strlen(link_target); i++) {
5103 if (!isprint((unsigned char)link_target[i]))
5104 link_target[i] = '?';
5106 modestr = "@";
5108 else if (S_ISDIR(mode))
5109 modestr = "/";
5110 else if (mode & S_IXUSR)
5111 modestr = "*";
5112 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5113 got_tree_entry_get_name(te), modestr,
5114 link_target ? " -> ": "",
5115 link_target ? link_target : "") == -1) {
5116 free(id_str);
5117 free(link_target);
5118 return got_error_from_errno("asprintf");
5120 free(id_str);
5121 free(link_target);
5122 err = format_line(&wline, &width, line, view->ncols, 0);
5123 if (err) {
5124 free(line);
5125 break;
5127 if (n == s->selected) {
5128 if (view->focussed)
5129 wstandout(view->window);
5130 s->selected_entry = te;
5132 tc = match_color(&s->colors, line);
5133 if (tc)
5134 wattr_on(view->window,
5135 COLOR_PAIR(tc->colorpair), NULL);
5136 waddwstr(view->window, wline);
5137 if (tc)
5138 wattr_off(view->window,
5139 COLOR_PAIR(tc->colorpair), NULL);
5140 if (width < view->ncols - 1)
5141 waddch(view->window, '\n');
5142 if (n == s->selected && view->focussed)
5143 wstandend(view->window);
5144 free(line);
5145 free(wline);
5146 wline = NULL;
5147 n++;
5148 s->ndisplayed++;
5149 s->last_displayed_entry = te;
5150 if (--limit <= 0)
5151 break;
5154 return err;
5157 static void
5158 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5160 struct got_tree_entry *te;
5161 int isroot = s->tree == s->root;
5162 int i = 0;
5164 if (s->first_displayed_entry == NULL)
5165 return;
5167 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5168 while (i++ < maxscroll) {
5169 if (te == NULL) {
5170 if (!isroot)
5171 s->first_displayed_entry = NULL;
5172 break;
5174 s->first_displayed_entry = te;
5175 te = got_tree_entry_get_prev(s->tree, te);
5179 static void
5180 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5182 struct got_tree_entry *next, *last;
5183 int n = 0;
5185 if (s->first_displayed_entry)
5186 next = got_tree_entry_get_next(s->tree,
5187 s->first_displayed_entry);
5188 else
5189 next = got_object_tree_get_first_entry(s->tree);
5191 last = s->last_displayed_entry;
5192 while (next && last && n++ < maxscroll) {
5193 last = got_tree_entry_get_next(s->tree, last);
5194 if (last) {
5195 s->first_displayed_entry = next;
5196 next = got_tree_entry_get_next(s->tree, next);
5201 static const struct got_error *
5202 tree_entry_path(char **path, struct tog_parent_trees *parents,
5203 struct got_tree_entry *te)
5205 const struct got_error *err = NULL;
5206 struct tog_parent_tree *pt;
5207 size_t len = 2; /* for leading slash and NUL */
5209 TAILQ_FOREACH(pt, parents, entry)
5210 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5211 + 1 /* slash */;
5212 if (te)
5213 len += strlen(got_tree_entry_get_name(te));
5215 *path = calloc(1, len);
5216 if (path == NULL)
5217 return got_error_from_errno("calloc");
5219 (*path)[0] = '/';
5220 pt = TAILQ_LAST(parents, tog_parent_trees);
5221 while (pt) {
5222 const char *name = got_tree_entry_get_name(pt->selected_entry);
5223 if (strlcat(*path, name, len) >= len) {
5224 err = got_error(GOT_ERR_NO_SPACE);
5225 goto done;
5227 if (strlcat(*path, "/", len) >= len) {
5228 err = got_error(GOT_ERR_NO_SPACE);
5229 goto done;
5231 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5233 if (te) {
5234 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5235 err = got_error(GOT_ERR_NO_SPACE);
5236 goto done;
5239 done:
5240 if (err) {
5241 free(*path);
5242 *path = NULL;
5244 return err;
5247 static const struct got_error *
5248 blame_tree_entry(struct tog_view **new_view, int begin_x,
5249 struct got_tree_entry *te, struct tog_parent_trees *parents,
5250 struct got_object_id *commit_id, struct got_repository *repo)
5252 const struct got_error *err = NULL;
5253 char *path;
5254 struct tog_view *blame_view;
5256 *new_view = NULL;
5258 err = tree_entry_path(&path, parents, te);
5259 if (err)
5260 return err;
5262 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5263 if (blame_view == NULL) {
5264 err = got_error_from_errno("view_open");
5265 goto done;
5268 err = open_blame_view(blame_view, path, commit_id, repo);
5269 if (err) {
5270 if (err->code == GOT_ERR_CANCELLED)
5271 err = NULL;
5272 view_close(blame_view);
5273 } else
5274 *new_view = blame_view;
5275 done:
5276 free(path);
5277 return err;
5280 static const struct got_error *
5281 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5282 struct tog_tree_view_state *s)
5284 struct tog_view *log_view;
5285 const struct got_error *err = NULL;
5286 char *path;
5288 *new_view = NULL;
5290 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5291 if (log_view == NULL)
5292 return got_error_from_errno("view_open");
5294 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5295 if (err)
5296 return err;
5298 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5299 path, 0);
5300 if (err)
5301 view_close(log_view);
5302 else
5303 *new_view = log_view;
5304 free(path);
5305 return err;
5308 static const struct got_error *
5309 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5310 const char *head_ref_name, struct got_repository *repo)
5312 const struct got_error *err = NULL;
5313 char *commit_id_str = NULL;
5314 struct tog_tree_view_state *s = &view->state.tree;
5315 struct got_commit_object *commit = NULL;
5317 TAILQ_INIT(&s->parents);
5318 STAILQ_INIT(&s->colors);
5320 s->commit_id = got_object_id_dup(commit_id);
5321 if (s->commit_id == NULL)
5322 return got_error_from_errno("got_object_id_dup");
5324 err = got_object_open_as_commit(&commit, repo, commit_id);
5325 if (err)
5326 goto done;
5329 * The root is opened here and will be closed when the view is closed.
5330 * Any visited subtrees and their path-wise parents are opened and
5331 * closed on demand.
5333 err = got_object_open_as_tree(&s->root, repo,
5334 got_object_commit_get_tree_id(commit));
5335 if (err)
5336 goto done;
5337 s->tree = s->root;
5339 err = got_object_id_str(&commit_id_str, commit_id);
5340 if (err != NULL)
5341 goto done;
5343 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5344 err = got_error_from_errno("asprintf");
5345 goto done;
5348 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5349 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5350 if (head_ref_name) {
5351 s->head_ref_name = strdup(head_ref_name);
5352 if (s->head_ref_name == NULL) {
5353 err = got_error_from_errno("strdup");
5354 goto done;
5357 s->repo = repo;
5359 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5360 err = add_color(&s->colors, "\\$$",
5361 TOG_COLOR_TREE_SUBMODULE,
5362 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5363 if (err)
5364 goto done;
5365 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5366 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5367 if (err)
5368 goto done;
5369 err = add_color(&s->colors, "/$",
5370 TOG_COLOR_TREE_DIRECTORY,
5371 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5372 if (err)
5373 goto done;
5375 err = add_color(&s->colors, "\\*$",
5376 TOG_COLOR_TREE_EXECUTABLE,
5377 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5378 if (err)
5379 goto done;
5381 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5382 get_color_value("TOG_COLOR_COMMIT"));
5383 if (err)
5384 goto done;
5387 view->show = show_tree_view;
5388 view->input = input_tree_view;
5389 view->close = close_tree_view;
5390 view->search_start = search_start_tree_view;
5391 view->search_next = search_next_tree_view;
5392 done:
5393 free(commit_id_str);
5394 if (commit)
5395 got_object_commit_close(commit);
5396 if (err)
5397 close_tree_view(view);
5398 return err;
5401 static const struct got_error *
5402 close_tree_view(struct tog_view *view)
5404 struct tog_tree_view_state *s = &view->state.tree;
5406 free_colors(&s->colors);
5407 free(s->tree_label);
5408 s->tree_label = NULL;
5409 free(s->commit_id);
5410 s->commit_id = NULL;
5411 free(s->head_ref_name);
5412 s->head_ref_name = NULL;
5413 while (!TAILQ_EMPTY(&s->parents)) {
5414 struct tog_parent_tree *parent;
5415 parent = TAILQ_FIRST(&s->parents);
5416 TAILQ_REMOVE(&s->parents, parent, entry);
5417 if (parent->tree != s->root)
5418 got_object_tree_close(parent->tree);
5419 free(parent);
5422 if (s->tree != NULL && s->tree != s->root)
5423 got_object_tree_close(s->tree);
5424 if (s->root)
5425 got_object_tree_close(s->root);
5426 return NULL;
5429 static const struct got_error *
5430 search_start_tree_view(struct tog_view *view)
5432 struct tog_tree_view_state *s = &view->state.tree;
5434 s->matched_entry = NULL;
5435 return NULL;
5438 static int
5439 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5441 regmatch_t regmatch;
5443 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5444 0) == 0;
5447 static const struct got_error *
5448 search_next_tree_view(struct tog_view *view)
5450 struct tog_tree_view_state *s = &view->state.tree;
5451 struct got_tree_entry *te = NULL;
5453 if (!view->searching) {
5454 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5455 return NULL;
5458 if (s->matched_entry) {
5459 if (view->searching == TOG_SEARCH_FORWARD) {
5460 if (s->selected_entry)
5461 te = got_tree_entry_get_next(s->tree,
5462 s->selected_entry);
5463 else
5464 te = got_object_tree_get_first_entry(s->tree);
5465 } else {
5466 if (s->selected_entry == NULL)
5467 te = got_object_tree_get_last_entry(s->tree);
5468 else
5469 te = got_tree_entry_get_prev(s->tree,
5470 s->selected_entry);
5472 } else {
5473 if (s->selected_entry)
5474 te = s->selected_entry;
5475 else if (view->searching == TOG_SEARCH_FORWARD)
5476 te = got_object_tree_get_first_entry(s->tree);
5477 else
5478 te = got_object_tree_get_last_entry(s->tree);
5481 while (1) {
5482 if (te == NULL) {
5483 if (s->matched_entry == NULL) {
5484 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5485 return NULL;
5487 if (view->searching == TOG_SEARCH_FORWARD)
5488 te = got_object_tree_get_first_entry(s->tree);
5489 else
5490 te = got_object_tree_get_last_entry(s->tree);
5493 if (match_tree_entry(te, &view->regex)) {
5494 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5495 s->matched_entry = te;
5496 break;
5499 if (view->searching == TOG_SEARCH_FORWARD)
5500 te = got_tree_entry_get_next(s->tree, te);
5501 else
5502 te = got_tree_entry_get_prev(s->tree, te);
5505 if (s->matched_entry) {
5506 s->first_displayed_entry = s->matched_entry;
5507 s->selected = 0;
5510 return NULL;
5513 static const struct got_error *
5514 show_tree_view(struct tog_view *view)
5516 const struct got_error *err = NULL;
5517 struct tog_tree_view_state *s = &view->state.tree;
5518 char *parent_path;
5520 err = tree_entry_path(&parent_path, &s->parents, NULL);
5521 if (err)
5522 return err;
5524 err = draw_tree_entries(view, parent_path);
5525 free(parent_path);
5527 view_vborder(view);
5528 return err;
5531 static const struct got_error *
5532 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5534 const struct got_error *err = NULL;
5535 struct tog_tree_view_state *s = &view->state.tree;
5536 struct tog_view *log_view, *ref_view;
5537 struct got_tree_entry *te;
5538 int begin_x = 0, n, nscroll = view->nlines - 3;
5540 switch (ch) {
5541 case 'i':
5542 s->show_ids = !s->show_ids;
5543 break;
5544 case 'l':
5545 if (!s->selected_entry)
5546 break;
5547 if (view_is_parent_view(view))
5548 begin_x = view_split_begin_x(view->begin_x);
5549 err = log_selected_tree_entry(&log_view, begin_x, s);
5550 view->focussed = 0;
5551 log_view->focussed = 1;
5552 if (view_is_parent_view(view)) {
5553 err = view_close_child(view);
5554 if (err)
5555 return err;
5556 view_set_child(view, log_view);
5557 view->focus_child = 1;
5558 } else
5559 *new_view = log_view;
5560 break;
5561 case 'r':
5562 if (view_is_parent_view(view))
5563 begin_x = view_split_begin_x(view->begin_x);
5564 ref_view = view_open(view->nlines, view->ncols,
5565 view->begin_y, begin_x, TOG_VIEW_REF);
5566 if (ref_view == NULL)
5567 return got_error_from_errno("view_open");
5568 err = open_ref_view(ref_view, s->repo);
5569 if (err) {
5570 view_close(ref_view);
5571 return err;
5573 view->focussed = 0;
5574 ref_view->focussed = 1;
5575 if (view_is_parent_view(view)) {
5576 err = view_close_child(view);
5577 if (err)
5578 return err;
5579 view_set_child(view, ref_view);
5580 view->focus_child = 1;
5581 } else
5582 *new_view = ref_view;
5583 break;
5584 case 'g':
5585 case KEY_HOME:
5586 s->selected = 0;
5587 if (s->tree == s->root)
5588 s->first_displayed_entry =
5589 got_object_tree_get_first_entry(s->tree);
5590 else
5591 s->first_displayed_entry = NULL;
5592 break;
5593 case 'G':
5594 case KEY_END:
5595 s->selected = 0;
5596 te = got_object_tree_get_last_entry(s->tree);
5597 for (n = 0; n < view->nlines - 3; n++) {
5598 if (te == NULL) {
5599 if(s->tree != s->root) {
5600 s->first_displayed_entry = NULL;
5601 n++;
5603 break;
5605 s->first_displayed_entry = te;
5606 te = got_tree_entry_get_prev(s->tree, te);
5608 if (n > 0)
5609 s->selected = n - 1;
5610 break;
5611 case 'k':
5612 case KEY_UP:
5613 case CTRL('p'):
5614 if (s->selected > 0) {
5615 s->selected--;
5616 break;
5618 tree_scroll_up(s, 1);
5619 break;
5620 case CTRL('u'):
5621 case 'u':
5622 nscroll /= 2;
5623 /* FALL THROUGH */
5624 case KEY_PPAGE:
5625 case CTRL('b'):
5626 if (s->tree == s->root) {
5627 if (got_object_tree_get_first_entry(s->tree) ==
5628 s->first_displayed_entry)
5629 s->selected -= MIN(s->selected, nscroll);
5630 } else {
5631 if (s->first_displayed_entry == NULL)
5632 s->selected -= MIN(s->selected, nscroll);
5634 tree_scroll_up(s, MAX(0, nscroll));
5635 break;
5636 case 'j':
5637 case KEY_DOWN:
5638 case CTRL('n'):
5639 if (s->selected < s->ndisplayed - 1) {
5640 s->selected++;
5641 break;
5643 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5644 == NULL)
5645 /* can't scroll any further */
5646 break;
5647 tree_scroll_down(s, 1);
5648 break;
5649 case CTRL('d'):
5650 case 'd':
5651 nscroll /= 2;
5652 /* FALL THROUGH */
5653 case KEY_NPAGE:
5654 case CTRL('f'):
5655 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5656 == NULL) {
5657 /* can't scroll any further; move cursor down */
5658 if (s->selected < s->ndisplayed - 1)
5659 s->selected += MIN(nscroll,
5660 s->ndisplayed - s->selected - 1);
5661 break;
5663 tree_scroll_down(s, nscroll);
5664 break;
5665 case KEY_ENTER:
5666 case '\r':
5667 case KEY_BACKSPACE:
5668 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5669 struct tog_parent_tree *parent;
5670 /* user selected '..' */
5671 if (s->tree == s->root)
5672 break;
5673 parent = TAILQ_FIRST(&s->parents);
5674 TAILQ_REMOVE(&s->parents, parent,
5675 entry);
5676 got_object_tree_close(s->tree);
5677 s->tree = parent->tree;
5678 s->first_displayed_entry =
5679 parent->first_displayed_entry;
5680 s->selected_entry =
5681 parent->selected_entry;
5682 s->selected = parent->selected;
5683 free(parent);
5684 } else if (S_ISDIR(got_tree_entry_get_mode(
5685 s->selected_entry))) {
5686 struct got_tree_object *subtree;
5687 err = got_object_open_as_tree(&subtree, s->repo,
5688 got_tree_entry_get_id(s->selected_entry));
5689 if (err)
5690 break;
5691 err = tree_view_visit_subtree(s, subtree);
5692 if (err) {
5693 got_object_tree_close(subtree);
5694 break;
5696 } else if (S_ISREG(got_tree_entry_get_mode(
5697 s->selected_entry))) {
5698 struct tog_view *blame_view;
5699 int begin_x = view_is_parent_view(view) ?
5700 view_split_begin_x(view->begin_x) : 0;
5702 err = blame_tree_entry(&blame_view, begin_x,
5703 s->selected_entry, &s->parents,
5704 s->commit_id, s->repo);
5705 if (err)
5706 break;
5707 view->focussed = 0;
5708 blame_view->focussed = 1;
5709 if (view_is_parent_view(view)) {
5710 err = view_close_child(view);
5711 if (err)
5712 return err;
5713 view_set_child(view, blame_view);
5714 view->focus_child = 1;
5715 } else
5716 *new_view = blame_view;
5718 break;
5719 case KEY_RESIZE:
5720 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5721 s->selected = view->nlines - 4;
5722 break;
5723 default:
5724 break;
5727 return err;
5730 __dead static void
5731 usage_tree(void)
5733 endwin();
5734 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5735 getprogname());
5736 exit(1);
5739 static const struct got_error *
5740 cmd_tree(int argc, char *argv[])
5742 const struct got_error *error;
5743 struct got_repository *repo = NULL;
5744 struct got_worktree *worktree = NULL;
5745 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5746 struct got_object_id *commit_id = NULL;
5747 struct got_commit_object *commit = NULL;
5748 const char *commit_id_arg = NULL;
5749 char *label = NULL;
5750 struct got_reference *ref = NULL;
5751 const char *head_ref_name = NULL;
5752 int ch;
5753 struct tog_view *view;
5755 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5756 switch (ch) {
5757 case 'c':
5758 commit_id_arg = optarg;
5759 break;
5760 case 'r':
5761 repo_path = realpath(optarg, NULL);
5762 if (repo_path == NULL)
5763 return got_error_from_errno2("realpath",
5764 optarg);
5765 break;
5766 default:
5767 usage_tree();
5768 /* NOTREACHED */
5772 argc -= optind;
5773 argv += optind;
5775 if (argc > 1)
5776 usage_tree();
5778 if (repo_path == NULL) {
5779 cwd = getcwd(NULL, 0);
5780 if (cwd == NULL)
5781 return got_error_from_errno("getcwd");
5782 error = got_worktree_open(&worktree, cwd);
5783 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5784 goto done;
5785 if (worktree)
5786 repo_path =
5787 strdup(got_worktree_get_repo_path(worktree));
5788 else
5789 repo_path = strdup(cwd);
5790 if (repo_path == NULL) {
5791 error = got_error_from_errno("strdup");
5792 goto done;
5796 error = got_repo_open(&repo, repo_path, NULL);
5797 if (error != NULL)
5798 goto done;
5800 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5801 repo, worktree);
5802 if (error)
5803 goto done;
5805 init_curses();
5807 error = apply_unveil(got_repo_get_path(repo), NULL);
5808 if (error)
5809 goto done;
5811 error = tog_load_refs(repo, 0);
5812 if (error)
5813 goto done;
5815 if (commit_id_arg == NULL) {
5816 error = got_repo_match_object_id(&commit_id, &label,
5817 worktree ? got_worktree_get_head_ref_name(worktree) :
5818 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5819 if (error)
5820 goto done;
5821 head_ref_name = label;
5822 } else {
5823 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5824 if (error == NULL)
5825 head_ref_name = got_ref_get_name(ref);
5826 else if (error->code != GOT_ERR_NOT_REF)
5827 goto done;
5828 error = got_repo_match_object_id(&commit_id, NULL,
5829 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5830 if (error)
5831 goto done;
5834 error = got_object_open_as_commit(&commit, repo, commit_id);
5835 if (error)
5836 goto done;
5838 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5839 if (view == NULL) {
5840 error = got_error_from_errno("view_open");
5841 goto done;
5843 error = open_tree_view(view, commit_id, head_ref_name, repo);
5844 if (error)
5845 goto done;
5846 if (!got_path_is_root_dir(in_repo_path)) {
5847 error = tree_view_walk_path(&view->state.tree, commit,
5848 in_repo_path);
5849 if (error)
5850 goto done;
5853 if (worktree) {
5854 /* Release work tree lock. */
5855 got_worktree_close(worktree);
5856 worktree = NULL;
5858 error = view_loop(view);
5859 done:
5860 free(repo_path);
5861 free(cwd);
5862 free(commit_id);
5863 free(label);
5864 if (ref)
5865 got_ref_close(ref);
5866 if (repo) {
5867 const struct got_error *close_err = got_repo_close(repo);
5868 if (error == NULL)
5869 error = close_err;
5871 tog_free_refs();
5872 return error;
5875 static const struct got_error *
5876 ref_view_load_refs(struct tog_ref_view_state *s)
5878 struct got_reflist_entry *sre;
5879 struct tog_reflist_entry *re;
5881 s->nrefs = 0;
5882 TAILQ_FOREACH(sre, &tog_refs, entry) {
5883 if (strncmp(got_ref_get_name(sre->ref),
5884 "refs/got/", 9) == 0 &&
5885 strncmp(got_ref_get_name(sre->ref),
5886 "refs/got/backup/", 16) != 0)
5887 continue;
5889 re = malloc(sizeof(*re));
5890 if (re == NULL)
5891 return got_error_from_errno("malloc");
5893 re->ref = got_ref_dup(sre->ref);
5894 if (re->ref == NULL)
5895 return got_error_from_errno("got_ref_dup");
5896 re->idx = s->nrefs++;
5897 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5900 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5901 return NULL;
5904 void
5905 ref_view_free_refs(struct tog_ref_view_state *s)
5907 struct tog_reflist_entry *re;
5909 while (!TAILQ_EMPTY(&s->refs)) {
5910 re = TAILQ_FIRST(&s->refs);
5911 TAILQ_REMOVE(&s->refs, re, entry);
5912 got_ref_close(re->ref);
5913 free(re);
5917 static const struct got_error *
5918 open_ref_view(struct tog_view *view, struct got_repository *repo)
5920 const struct got_error *err = NULL;
5921 struct tog_ref_view_state *s = &view->state.ref;
5923 s->selected_entry = 0;
5924 s->repo = repo;
5926 TAILQ_INIT(&s->refs);
5927 STAILQ_INIT(&s->colors);
5929 err = ref_view_load_refs(s);
5930 if (err)
5931 return err;
5933 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5934 err = add_color(&s->colors, "^refs/heads/",
5935 TOG_COLOR_REFS_HEADS,
5936 get_color_value("TOG_COLOR_REFS_HEADS"));
5937 if (err)
5938 goto done;
5940 err = add_color(&s->colors, "^refs/tags/",
5941 TOG_COLOR_REFS_TAGS,
5942 get_color_value("TOG_COLOR_REFS_TAGS"));
5943 if (err)
5944 goto done;
5946 err = add_color(&s->colors, "^refs/remotes/",
5947 TOG_COLOR_REFS_REMOTES,
5948 get_color_value("TOG_COLOR_REFS_REMOTES"));
5949 if (err)
5950 goto done;
5952 err = add_color(&s->colors, "^refs/got/backup/",
5953 TOG_COLOR_REFS_BACKUP,
5954 get_color_value("TOG_COLOR_REFS_BACKUP"));
5955 if (err)
5956 goto done;
5959 view->show = show_ref_view;
5960 view->input = input_ref_view;
5961 view->close = close_ref_view;
5962 view->search_start = search_start_ref_view;
5963 view->search_next = search_next_ref_view;
5964 done:
5965 if (err)
5966 free_colors(&s->colors);
5967 return err;
5970 static const struct got_error *
5971 close_ref_view(struct tog_view *view)
5973 struct tog_ref_view_state *s = &view->state.ref;
5975 ref_view_free_refs(s);
5976 free_colors(&s->colors);
5978 return NULL;
5981 static const struct got_error *
5982 resolve_reflist_entry(struct got_object_id **commit_id,
5983 struct tog_reflist_entry *re, struct got_repository *repo)
5985 const struct got_error *err = NULL;
5986 struct got_object_id *obj_id;
5987 struct got_tag_object *tag = NULL;
5988 int obj_type;
5990 *commit_id = NULL;
5992 err = got_ref_resolve(&obj_id, repo, re->ref);
5993 if (err)
5994 return err;
5996 err = got_object_get_type(&obj_type, repo, obj_id);
5997 if (err)
5998 goto done;
6000 switch (obj_type) {
6001 case GOT_OBJ_TYPE_COMMIT:
6002 *commit_id = obj_id;
6003 break;
6004 case GOT_OBJ_TYPE_TAG:
6005 err = got_object_open_as_tag(&tag, repo, obj_id);
6006 if (err)
6007 goto done;
6008 free(obj_id);
6009 err = got_object_get_type(&obj_type, repo,
6010 got_object_tag_get_object_id(tag));
6011 if (err)
6012 goto done;
6013 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6014 err = got_error(GOT_ERR_OBJ_TYPE);
6015 goto done;
6017 *commit_id = got_object_id_dup(
6018 got_object_tag_get_object_id(tag));
6019 if (*commit_id == NULL) {
6020 err = got_error_from_errno("got_object_id_dup");
6021 goto done;
6023 break;
6024 default:
6025 err = got_error(GOT_ERR_OBJ_TYPE);
6026 break;
6029 done:
6030 if (tag)
6031 got_object_tag_close(tag);
6032 if (err) {
6033 free(*commit_id);
6034 *commit_id = NULL;
6036 return err;
6039 static const struct got_error *
6040 log_ref_entry(struct tog_view **new_view, int begin_x,
6041 struct tog_reflist_entry *re, struct got_repository *repo)
6043 struct tog_view *log_view;
6044 const struct got_error *err = NULL;
6045 struct got_object_id *commit_id = NULL;
6047 *new_view = NULL;
6049 err = resolve_reflist_entry(&commit_id, re, repo);
6050 if (err) {
6051 if (err->code != GOT_ERR_OBJ_TYPE)
6052 return err;
6053 else
6054 return NULL;
6057 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6058 if (log_view == NULL) {
6059 err = got_error_from_errno("view_open");
6060 goto done;
6063 err = open_log_view(log_view, commit_id, repo,
6064 got_ref_get_name(re->ref), "", 0);
6065 done:
6066 if (err)
6067 view_close(log_view);
6068 else
6069 *new_view = log_view;
6070 free(commit_id);
6071 return err;
6074 static void
6075 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6077 struct tog_reflist_entry *re;
6078 int i = 0;
6080 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6081 return;
6083 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6084 while (i++ < maxscroll) {
6085 if (re == NULL)
6086 break;
6087 s->first_displayed_entry = re;
6088 re = TAILQ_PREV(re, tog_reflist_head, entry);
6092 static void
6093 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6095 struct tog_reflist_entry *next, *last;
6096 int n = 0;
6098 if (s->first_displayed_entry)
6099 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6100 else
6101 next = TAILQ_FIRST(&s->refs);
6103 last = s->last_displayed_entry;
6104 while (next && last && n++ < maxscroll) {
6105 last = TAILQ_NEXT(last, entry);
6106 if (last) {
6107 s->first_displayed_entry = next;
6108 next = TAILQ_NEXT(next, entry);
6113 static const struct got_error *
6114 search_start_ref_view(struct tog_view *view)
6116 struct tog_ref_view_state *s = &view->state.ref;
6118 s->matched_entry = NULL;
6119 return NULL;
6122 static int
6123 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6125 regmatch_t regmatch;
6127 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6128 0) == 0;
6131 static const struct got_error *
6132 search_next_ref_view(struct tog_view *view)
6134 struct tog_ref_view_state *s = &view->state.ref;
6135 struct tog_reflist_entry *re = NULL;
6137 if (!view->searching) {
6138 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6139 return NULL;
6142 if (s->matched_entry) {
6143 if (view->searching == TOG_SEARCH_FORWARD) {
6144 if (s->selected_entry)
6145 re = TAILQ_NEXT(s->selected_entry, entry);
6146 else
6147 re = TAILQ_PREV(s->selected_entry,
6148 tog_reflist_head, entry);
6149 } else {
6150 if (s->selected_entry == NULL)
6151 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6152 else
6153 re = TAILQ_PREV(s->selected_entry,
6154 tog_reflist_head, entry);
6156 } else {
6157 if (s->selected_entry)
6158 re = s->selected_entry;
6159 else if (view->searching == TOG_SEARCH_FORWARD)
6160 re = TAILQ_FIRST(&s->refs);
6161 else
6162 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6165 while (1) {
6166 if (re == NULL) {
6167 if (s->matched_entry == NULL) {
6168 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6169 return NULL;
6171 if (view->searching == TOG_SEARCH_FORWARD)
6172 re = TAILQ_FIRST(&s->refs);
6173 else
6174 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6177 if (match_reflist_entry(re, &view->regex)) {
6178 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6179 s->matched_entry = re;
6180 break;
6183 if (view->searching == TOG_SEARCH_FORWARD)
6184 re = TAILQ_NEXT(re, entry);
6185 else
6186 re = TAILQ_PREV(re, tog_reflist_head, entry);
6189 if (s->matched_entry) {
6190 s->first_displayed_entry = s->matched_entry;
6191 s->selected = 0;
6194 return NULL;
6197 static const struct got_error *
6198 show_ref_view(struct tog_view *view)
6200 const struct got_error *err = NULL;
6201 struct tog_ref_view_state *s = &view->state.ref;
6202 struct tog_reflist_entry *re;
6203 char *line = NULL;
6204 wchar_t *wline;
6205 struct tog_color *tc;
6206 int width, n;
6207 int limit = view->nlines;
6209 werase(view->window);
6211 s->ndisplayed = 0;
6213 if (limit == 0)
6214 return NULL;
6216 re = s->first_displayed_entry;
6218 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6219 s->nrefs) == -1)
6220 return got_error_from_errno("asprintf");
6222 err = format_line(&wline, &width, line, view->ncols, 0);
6223 if (err) {
6224 free(line);
6225 return err;
6227 if (view_needs_focus_indication(view))
6228 wstandout(view->window);
6229 waddwstr(view->window, wline);
6230 if (view_needs_focus_indication(view))
6231 wstandend(view->window);
6232 free(wline);
6233 wline = NULL;
6234 free(line);
6235 line = NULL;
6236 if (width < view->ncols - 1)
6237 waddch(view->window, '\n');
6238 if (--limit <= 0)
6239 return NULL;
6241 n = 0;
6242 while (re && limit > 0) {
6243 char *line = NULL;
6245 if (got_ref_is_symbolic(re->ref)) {
6246 if (asprintf(&line, "%s -> %s",
6247 got_ref_get_name(re->ref),
6248 got_ref_get_symref_target(re->ref)) == -1)
6249 return got_error_from_errno("asprintf");
6250 } else if (s->show_ids) {
6251 struct got_object_id *id;
6252 char *id_str;
6253 err = got_ref_resolve(&id, s->repo, re->ref);
6254 if (err)
6255 return err;
6256 err = got_object_id_str(&id_str, id);
6257 if (err) {
6258 free(id);
6259 return err;
6261 if (asprintf(&line, "%s: %s",
6262 got_ref_get_name(re->ref), id_str) == -1) {
6263 err = got_error_from_errno("asprintf");
6264 free(id);
6265 free(id_str);
6266 return err;
6268 free(id);
6269 free(id_str);
6270 } else {
6271 line = strdup(got_ref_get_name(re->ref));
6272 if (line == NULL)
6273 return got_error_from_errno("strdup");
6276 err = format_line(&wline, &width, line, view->ncols, 0);
6277 if (err) {
6278 free(line);
6279 return err;
6281 if (n == s->selected) {
6282 if (view->focussed)
6283 wstandout(view->window);
6284 s->selected_entry = re;
6286 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6287 if (tc)
6288 wattr_on(view->window,
6289 COLOR_PAIR(tc->colorpair), NULL);
6290 waddwstr(view->window, wline);
6291 if (tc)
6292 wattr_off(view->window,
6293 COLOR_PAIR(tc->colorpair), NULL);
6294 if (width < view->ncols - 1)
6295 waddch(view->window, '\n');
6296 if (n == s->selected && view->focussed)
6297 wstandend(view->window);
6298 free(line);
6299 free(wline);
6300 wline = NULL;
6301 n++;
6302 s->ndisplayed++;
6303 s->last_displayed_entry = re;
6305 limit--;
6306 re = TAILQ_NEXT(re, entry);
6309 view_vborder(view);
6310 return err;
6313 static const struct got_error *
6314 browse_ref_tree(struct tog_view **new_view, int begin_x,
6315 struct tog_reflist_entry *re, struct got_repository *repo)
6317 const struct got_error *err = NULL;
6318 struct got_object_id *commit_id = NULL;
6319 struct tog_view *tree_view;
6321 *new_view = NULL;
6323 err = resolve_reflist_entry(&commit_id, re, repo);
6324 if (err) {
6325 if (err->code != GOT_ERR_OBJ_TYPE)
6326 return err;
6327 else
6328 return NULL;
6332 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6333 if (tree_view == NULL) {
6334 err = got_error_from_errno("view_open");
6335 goto done;
6338 err = open_tree_view(tree_view, commit_id,
6339 got_ref_get_name(re->ref), repo);
6340 if (err)
6341 goto done;
6343 *new_view = tree_view;
6344 done:
6345 free(commit_id);
6346 return err;
6348 static const struct got_error *
6349 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6351 const struct got_error *err = NULL;
6352 struct tog_ref_view_state *s = &view->state.ref;
6353 struct tog_view *log_view, *tree_view;
6354 struct tog_reflist_entry *re;
6355 int begin_x = 0, n, nscroll = view->nlines - 1;
6357 switch (ch) {
6358 case 'i':
6359 s->show_ids = !s->show_ids;
6360 break;
6361 case 'o':
6362 s->sort_by_date = !s->sort_by_date;
6363 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6364 got_ref_cmp_by_commit_timestamp_descending :
6365 tog_ref_cmp_by_name, s->repo);
6366 if (err)
6367 break;
6368 got_reflist_object_id_map_free(tog_refs_idmap);
6369 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6370 &tog_refs, s->repo);
6371 if (err)
6372 break;
6373 ref_view_free_refs(s);
6374 err = ref_view_load_refs(s);
6375 break;
6376 case KEY_ENTER:
6377 case '\r':
6378 if (!s->selected_entry)
6379 break;
6380 if (view_is_parent_view(view))
6381 begin_x = view_split_begin_x(view->begin_x);
6382 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6383 s->repo);
6384 view->focussed = 0;
6385 log_view->focussed = 1;
6386 if (view_is_parent_view(view)) {
6387 err = view_close_child(view);
6388 if (err)
6389 return err;
6390 view_set_child(view, log_view);
6391 view->focus_child = 1;
6392 } else
6393 *new_view = log_view;
6394 break;
6395 case 't':
6396 if (!s->selected_entry)
6397 break;
6398 if (view_is_parent_view(view))
6399 begin_x = view_split_begin_x(view->begin_x);
6400 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6401 s->repo);
6402 if (err || tree_view == NULL)
6403 break;
6404 view->focussed = 0;
6405 tree_view->focussed = 1;
6406 if (view_is_parent_view(view)) {
6407 err = view_close_child(view);
6408 if (err)
6409 return err;
6410 view_set_child(view, tree_view);
6411 view->focus_child = 1;
6412 } else
6413 *new_view = tree_view;
6414 break;
6415 case 'g':
6416 case KEY_HOME:
6417 s->selected = 0;
6418 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6419 break;
6420 case 'G':
6421 case KEY_END:
6422 s->selected = 0;
6423 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6424 for (n = 0; n < view->nlines - 1; n++) {
6425 if (re == NULL)
6426 break;
6427 s->first_displayed_entry = re;
6428 re = TAILQ_PREV(re, tog_reflist_head, entry);
6430 if (n > 0)
6431 s->selected = n - 1;
6432 break;
6433 case 'k':
6434 case KEY_UP:
6435 case CTRL('p'):
6436 if (s->selected > 0) {
6437 s->selected--;
6438 break;
6440 ref_scroll_up(s, 1);
6441 break;
6442 case CTRL('u'):
6443 case 'u':
6444 nscroll /= 2;
6445 /* FALL THROUGH */
6446 case KEY_PPAGE:
6447 case CTRL('b'):
6448 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6449 s->selected -= MIN(nscroll, s->selected);
6450 ref_scroll_up(s, MAX(0, nscroll));
6451 break;
6452 case 'j':
6453 case KEY_DOWN:
6454 case CTRL('n'):
6455 if (s->selected < s->ndisplayed - 1) {
6456 s->selected++;
6457 break;
6459 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6460 /* can't scroll any further */
6461 break;
6462 ref_scroll_down(s, 1);
6463 break;
6464 case CTRL('d'):
6465 case 'd':
6466 nscroll /= 2;
6467 /* FALL THROUGH */
6468 case KEY_NPAGE:
6469 case CTRL('f'):
6470 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6471 /* can't scroll any further; move cursor down */
6472 if (s->selected < s->ndisplayed - 1)
6473 s->selected += MIN(nscroll,
6474 s->ndisplayed - s->selected - 1);
6475 break;
6477 ref_scroll_down(s, nscroll);
6478 break;
6479 case CTRL('l'):
6480 tog_free_refs();
6481 err = tog_load_refs(s->repo, s->sort_by_date);
6482 if (err)
6483 break;
6484 ref_view_free_refs(s);
6485 err = ref_view_load_refs(s);
6486 break;
6487 case KEY_RESIZE:
6488 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6489 s->selected = view->nlines - 2;
6490 break;
6491 default:
6492 break;
6495 return err;
6498 __dead static void
6499 usage_ref(void)
6501 endwin();
6502 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6503 getprogname());
6504 exit(1);
6507 static const struct got_error *
6508 cmd_ref(int argc, char *argv[])
6510 const struct got_error *error;
6511 struct got_repository *repo = NULL;
6512 struct got_worktree *worktree = NULL;
6513 char *cwd = NULL, *repo_path = NULL;
6514 int ch;
6515 struct tog_view *view;
6517 while ((ch = getopt(argc, argv, "r:")) != -1) {
6518 switch (ch) {
6519 case 'r':
6520 repo_path = realpath(optarg, NULL);
6521 if (repo_path == NULL)
6522 return got_error_from_errno2("realpath",
6523 optarg);
6524 break;
6525 default:
6526 usage_ref();
6527 /* NOTREACHED */
6531 argc -= optind;
6532 argv += optind;
6534 if (argc > 1)
6535 usage_ref();
6537 if (repo_path == NULL) {
6538 cwd = getcwd(NULL, 0);
6539 if (cwd == NULL)
6540 return got_error_from_errno("getcwd");
6541 error = got_worktree_open(&worktree, cwd);
6542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6543 goto done;
6544 if (worktree)
6545 repo_path =
6546 strdup(got_worktree_get_repo_path(worktree));
6547 else
6548 repo_path = strdup(cwd);
6549 if (repo_path == NULL) {
6550 error = got_error_from_errno("strdup");
6551 goto done;
6555 error = got_repo_open(&repo, repo_path, NULL);
6556 if (error != NULL)
6557 goto done;
6559 init_curses();
6561 error = apply_unveil(got_repo_get_path(repo), NULL);
6562 if (error)
6563 goto done;
6565 error = tog_load_refs(repo, 0);
6566 if (error)
6567 goto done;
6569 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6570 if (view == NULL) {
6571 error = got_error_from_errno("view_open");
6572 goto done;
6575 error = open_ref_view(view, repo);
6576 if (error)
6577 goto done;
6579 if (worktree) {
6580 /* Release work tree lock. */
6581 got_worktree_close(worktree);
6582 worktree = NULL;
6584 error = view_loop(view);
6585 done:
6586 free(repo_path);
6587 free(cwd);
6588 if (repo) {
6589 const struct got_error *close_err = got_repo_close(repo);
6590 if (close_err)
6591 error = close_err;
6593 tog_free_refs();
6594 return error;
6597 static void
6598 list_commands(FILE *fp)
6600 size_t i;
6602 fprintf(fp, "commands:");
6603 for (i = 0; i < nitems(tog_commands); i++) {
6604 const struct tog_cmd *cmd = &tog_commands[i];
6605 fprintf(fp, " %s", cmd->name);
6607 fputc('\n', fp);
6610 __dead static void
6611 usage(int hflag, int status)
6613 FILE *fp = (status == 0) ? stdout : stderr;
6615 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6616 getprogname());
6617 if (hflag) {
6618 fprintf(fp, "lazy usage: %s path\n", getprogname());
6619 list_commands(fp);
6621 exit(status);
6624 static char **
6625 make_argv(int argc, ...)
6627 va_list ap;
6628 char **argv;
6629 int i;
6631 va_start(ap, argc);
6633 argv = calloc(argc, sizeof(char *));
6634 if (argv == NULL)
6635 err(1, "calloc");
6636 for (i = 0; i < argc; i++) {
6637 argv[i] = strdup(va_arg(ap, char *));
6638 if (argv[i] == NULL)
6639 err(1, "strdup");
6642 va_end(ap);
6643 return argv;
6647 * Try to convert 'tog path' into a 'tog log path' command.
6648 * The user could simply have mistyped the command rather than knowingly
6649 * provided a path. So check whether argv[0] can in fact be resolved
6650 * to a path in the HEAD commit and print a special error if not.
6651 * This hack is for mpi@ <3
6653 static const struct got_error *
6654 tog_log_with_path(int argc, char *argv[])
6656 const struct got_error *error = NULL, *close_err;
6657 const struct tog_cmd *cmd = NULL;
6658 struct got_repository *repo = NULL;
6659 struct got_worktree *worktree = NULL;
6660 struct got_object_id *commit_id = NULL, *id = NULL;
6661 struct got_commit_object *commit = NULL;
6662 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6663 char *commit_id_str = NULL, **cmd_argv = NULL;
6665 cwd = getcwd(NULL, 0);
6666 if (cwd == NULL)
6667 return got_error_from_errno("getcwd");
6669 error = got_worktree_open(&worktree, cwd);
6670 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6671 goto done;
6673 if (worktree)
6674 repo_path = strdup(got_worktree_get_repo_path(worktree));
6675 else
6676 repo_path = strdup(cwd);
6677 if (repo_path == NULL) {
6678 error = got_error_from_errno("strdup");
6679 goto done;
6682 error = got_repo_open(&repo, repo_path, NULL);
6683 if (error != NULL)
6684 goto done;
6686 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6687 repo, worktree);
6688 if (error)
6689 goto done;
6691 error = tog_load_refs(repo, 0);
6692 if (error)
6693 goto done;
6694 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6695 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6696 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6697 if (error)
6698 goto done;
6700 if (worktree) {
6701 got_worktree_close(worktree);
6702 worktree = NULL;
6705 error = got_object_open_as_commit(&commit, repo, commit_id);
6706 if (error)
6707 goto done;
6709 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6710 if (error) {
6711 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6712 goto done;
6713 fprintf(stderr, "%s: '%s' is no known command or path\n",
6714 getprogname(), argv[0]);
6715 usage(1, 1);
6716 /* not reached */
6719 close_err = got_repo_close(repo);
6720 if (error == NULL)
6721 error = close_err;
6722 repo = NULL;
6724 error = got_object_id_str(&commit_id_str, commit_id);
6725 if (error)
6726 goto done;
6728 cmd = &tog_commands[0]; /* log */
6729 argc = 4;
6730 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6731 error = cmd->cmd_main(argc, cmd_argv);
6732 done:
6733 if (repo) {
6734 close_err = got_repo_close(repo);
6735 if (error == NULL)
6736 error = close_err;
6738 if (commit)
6739 got_object_commit_close(commit);
6740 if (worktree)
6741 got_worktree_close(worktree);
6742 free(id);
6743 free(commit_id_str);
6744 free(commit_id);
6745 free(cwd);
6746 free(repo_path);
6747 free(in_repo_path);
6748 if (cmd_argv) {
6749 int i;
6750 for (i = 0; i < argc; i++)
6751 free(cmd_argv[i]);
6752 free(cmd_argv);
6754 tog_free_refs();
6755 return error;
6758 int
6759 main(int argc, char *argv[])
6761 const struct got_error *error = NULL;
6762 const struct tog_cmd *cmd = NULL;
6763 int ch, hflag = 0, Vflag = 0;
6764 char **cmd_argv = NULL;
6765 static const struct option longopts[] = {
6766 { "version", no_argument, NULL, 'V' },
6767 { NULL, 0, NULL, 0}
6770 setlocale(LC_CTYPE, "");
6772 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6773 switch (ch) {
6774 case 'h':
6775 hflag = 1;
6776 break;
6777 case 'V':
6778 Vflag = 1;
6779 break;
6780 default:
6781 usage(hflag, 1);
6782 /* NOTREACHED */
6786 argc -= optind;
6787 argv += optind;
6788 optind = 1;
6789 optreset = 1;
6791 if (Vflag) {
6792 got_version_print_str();
6793 return 0;
6796 #ifndef PROFILE
6797 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6798 NULL) == -1)
6799 err(1, "pledge");
6800 #endif
6802 if (argc == 0) {
6803 if (hflag)
6804 usage(hflag, 0);
6805 /* Build an argument vector which runs a default command. */
6806 cmd = &tog_commands[0];
6807 argc = 1;
6808 cmd_argv = make_argv(argc, cmd->name);
6809 } else {
6810 size_t i;
6812 /* Did the user specify a command? */
6813 for (i = 0; i < nitems(tog_commands); i++) {
6814 if (strncmp(tog_commands[i].name, argv[0],
6815 strlen(argv[0])) == 0) {
6816 cmd = &tog_commands[i];
6817 break;
6822 if (cmd == NULL) {
6823 if (argc != 1)
6824 usage(0, 1);
6825 /* No command specified; try log with a path */
6826 error = tog_log_with_path(argc, argv);
6827 } else {
6828 if (hflag)
6829 cmd->cmd_usage();
6830 else
6831 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6834 endwin();
6835 putchar('\n');
6836 if (cmd_argv) {
6837 int i;
6838 for (i = 0; i < argc; i++)
6839 free(cmd_argv[i]);
6840 free(cmd_argv);
6843 if (error && error->code != GOT_ERR_CANCELLED)
6844 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6845 return 0;