Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
24 #include <curses.h>
25 #include <panel.h>
26 #include <locale.h>
27 #include <sha1.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <string.h>
34 #include <err.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
42 #include <sched.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
49 #include "got_diff.h"
50 #include "got_opentemp.h"
51 #include "got_utf8.h"
52 #include "got_cancel.h"
53 #include "got_commit_graph.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_path.h"
57 #include "got_worktree.h"
59 #ifndef MIN
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #endif
63 #ifndef MAX
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #endif
67 #define CTRL(x) ((x) & 0x1f)
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 struct tog_cmd {
74 const char *name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 };
79 __dead static void usage(int, int);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_ref(void);
86 static const struct got_error* cmd_log(int, char *[]);
87 static const struct got_error* cmd_diff(int, char *[]);
88 static const struct got_error* cmd_blame(int, char *[]);
89 static const struct got_error* cmd_tree(int, char *[]);
90 static const struct got_error* cmd_ref(int, char *[]);
92 static const struct tog_cmd tog_commands[] = {
93 { "log", cmd_log, usage_log },
94 { "diff", cmd_diff, usage_diff },
95 { "blame", cmd_blame, usage_blame },
96 { "tree", cmd_tree, usage_tree },
97 { "ref", cmd_ref, usage_ref },
98 };
100 enum tog_view_type {
101 TOG_VIEW_DIFF,
102 TOG_VIEW_LOG,
103 TOG_VIEW_BLAME,
104 TOG_VIEW_TREE,
105 TOG_VIEW_REF,
106 };
108 #define TOG_EOF_STRING "(END)"
110 struct commit_queue_entry {
111 TAILQ_ENTRY(commit_queue_entry) entry;
112 struct got_object_id *id;
113 struct got_commit_object *commit;
114 int idx;
115 };
116 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
117 struct commit_queue {
118 int ncommits;
119 struct commit_queue_head head;
120 };
122 struct tog_color {
123 STAILQ_ENTRY(tog_color) entry;
124 regex_t regex;
125 short colorpair;
126 };
127 STAILQ_HEAD(tog_colors, tog_color);
129 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
130 static struct got_reflist_object_id_map *tog_refs_idmap;
132 static const struct got_error *
133 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
134 struct got_reference* re2)
136 const char *name1 = got_ref_get_name(re1);
137 const char *name2 = got_ref_get_name(re2);
138 int isbackup1, isbackup2;
140 /* Sort backup refs towards the bottom of the list. */
141 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
142 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
143 if (!isbackup1 && isbackup2) {
144 *cmp = -1;
145 return NULL;
146 } else if (isbackup1 && !isbackup2) {
147 *cmp = 1;
148 return NULL;
151 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
152 return NULL;
155 static const struct got_error *
156 tog_load_refs(struct got_repository *repo, int sort_by_date)
158 const struct got_error *err;
160 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
161 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
162 repo);
163 if (err)
164 return err;
166 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
167 repo);
170 static void
171 tog_free_refs(void)
173 if (tog_refs_idmap) {
174 got_reflist_object_id_map_free(tog_refs_idmap);
175 tog_refs_idmap = NULL;
177 got_ref_list_free(&tog_refs);
180 static const struct got_error *
181 add_color(struct tog_colors *colors, const char *pattern,
182 int idx, short color)
184 const struct got_error *err = NULL;
185 struct tog_color *tc;
186 int regerr = 0;
188 if (idx < 1 || idx > COLOR_PAIRS - 1)
189 return NULL;
191 init_pair(idx, color, -1);
193 tc = calloc(1, sizeof(*tc));
194 if (tc == NULL)
195 return got_error_from_errno("calloc");
196 regerr = regcomp(&tc->regex, pattern,
197 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
198 if (regerr) {
199 static char regerr_msg[512];
200 static char err_msg[512];
201 regerror(regerr, &tc->regex, regerr_msg,
202 sizeof(regerr_msg));
203 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
204 regerr_msg);
205 err = got_error_msg(GOT_ERR_REGEX, err_msg);
206 free(tc);
207 return err;
209 tc->colorpair = idx;
210 STAILQ_INSERT_HEAD(colors, tc, entry);
211 return NULL;
214 static void
215 free_colors(struct tog_colors *colors)
217 struct tog_color *tc;
219 while (!STAILQ_EMPTY(colors)) {
220 tc = STAILQ_FIRST(colors);
221 STAILQ_REMOVE_HEAD(colors, entry);
222 regfree(&tc->regex);
223 free(tc);
227 struct tog_color *
228 get_color(struct tog_colors *colors, int colorpair)
230 struct tog_color *tc = NULL;
232 STAILQ_FOREACH(tc, colors, entry) {
233 if (tc->colorpair == colorpair)
234 return tc;
237 return NULL;
240 static int
241 default_color_value(const char *envvar)
243 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
244 return COLOR_MAGENTA;
245 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
246 return COLOR_CYAN;
247 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
248 return COLOR_YELLOW;
249 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
250 return COLOR_GREEN;
251 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
252 return COLOR_MAGENTA;
253 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
254 return COLOR_MAGENTA;
255 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
256 return COLOR_CYAN;
257 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
258 return COLOR_GREEN;
259 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
260 return COLOR_GREEN;
261 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
262 return COLOR_CYAN;
263 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
264 return COLOR_YELLOW;
265 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
266 return COLOR_GREEN;
267 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
268 return COLOR_MAGENTA;
269 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
270 return COLOR_YELLOW;
271 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
272 return COLOR_CYAN;
274 return -1;
277 static int
278 get_color_value(const char *envvar)
280 const char *val = getenv(envvar);
282 if (val == NULL)
283 return default_color_value(envvar);
285 if (strcasecmp(val, "black") == 0)
286 return COLOR_BLACK;
287 if (strcasecmp(val, "red") == 0)
288 return COLOR_RED;
289 if (strcasecmp(val, "green") == 0)
290 return COLOR_GREEN;
291 if (strcasecmp(val, "yellow") == 0)
292 return COLOR_YELLOW;
293 if (strcasecmp(val, "blue") == 0)
294 return COLOR_BLUE;
295 if (strcasecmp(val, "magenta") == 0)
296 return COLOR_MAGENTA;
297 if (strcasecmp(val, "cyan") == 0)
298 return COLOR_CYAN;
299 if (strcasecmp(val, "white") == 0)
300 return COLOR_WHITE;
301 if (strcasecmp(val, "default") == 0)
302 return -1;
304 return default_color_value(envvar);
308 struct tog_diff_view_state {
309 struct got_object_id *id1, *id2;
310 const char *label1, *label2;
311 FILE *f, *f1, *f2;
312 int first_displayed_line;
313 int last_displayed_line;
314 int eof;
315 int diff_context;
316 int ignore_whitespace;
317 int force_text_diff;
318 struct got_repository *repo;
319 struct tog_colors colors;
320 size_t nlines;
321 off_t *line_offsets;
322 int matched_line;
323 int selected_line;
325 /* passed from log view; may be NULL */
326 struct tog_view *log_view;
327 };
329 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
331 struct tog_log_thread_args {
332 pthread_cond_t need_commits;
333 pthread_cond_t commit_loaded;
334 int commits_needed;
335 int load_all;
336 struct got_commit_graph *graph;
337 struct commit_queue *commits;
338 const char *in_repo_path;
339 struct got_object_id *start_id;
340 struct got_repository *repo;
341 int *pack_fds;
342 int log_complete;
343 sig_atomic_t *quit;
344 struct commit_queue_entry **first_displayed_entry;
345 struct commit_queue_entry **selected_entry;
346 int *searching;
347 int *search_next_done;
348 regex_t *regex;
349 };
351 struct tog_log_view_state {
352 struct commit_queue commits;
353 struct commit_queue_entry *first_displayed_entry;
354 struct commit_queue_entry *last_displayed_entry;
355 struct commit_queue_entry *selected_entry;
356 int selected;
357 char *in_repo_path;
358 char *head_ref_name;
359 int log_branches;
360 struct got_repository *repo;
361 struct got_object_id *start_id;
362 sig_atomic_t quit;
363 pthread_t thread;
364 struct tog_log_thread_args thread_args;
365 struct commit_queue_entry *matched_entry;
366 struct commit_queue_entry *search_entry;
367 struct tog_colors colors;
368 };
370 #define TOG_COLOR_DIFF_MINUS 1
371 #define TOG_COLOR_DIFF_PLUS 2
372 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
373 #define TOG_COLOR_DIFF_META 4
374 #define TOG_COLOR_TREE_SUBMODULE 5
375 #define TOG_COLOR_TREE_SYMLINK 6
376 #define TOG_COLOR_TREE_DIRECTORY 7
377 #define TOG_COLOR_TREE_EXECUTABLE 8
378 #define TOG_COLOR_COMMIT 9
379 #define TOG_COLOR_AUTHOR 10
380 #define TOG_COLOR_DATE 11
381 #define TOG_COLOR_REFS_HEADS 12
382 #define TOG_COLOR_REFS_TAGS 13
383 #define TOG_COLOR_REFS_REMOTES 14
384 #define TOG_COLOR_REFS_BACKUP 15
386 struct tog_blame_cb_args {
387 struct tog_blame_line *lines; /* one per line */
388 int nlines;
390 struct tog_view *view;
391 struct got_object_id *commit_id;
392 int *quit;
393 };
395 struct tog_blame_thread_args {
396 const char *path;
397 struct got_repository *repo;
398 struct tog_blame_cb_args *cb_args;
399 int *complete;
400 got_cancel_cb cancel_cb;
401 void *cancel_arg;
402 };
404 struct tog_blame {
405 FILE *f;
406 off_t filesize;
407 struct tog_blame_line *lines;
408 int nlines;
409 off_t *line_offsets;
410 pthread_t thread;
411 struct tog_blame_thread_args thread_args;
412 struct tog_blame_cb_args cb_args;
413 const char *path;
414 int *pack_fds;
415 };
417 struct tog_blame_view_state {
418 int first_displayed_line;
419 int last_displayed_line;
420 int selected_line;
421 int blame_complete;
422 int eof;
423 int done;
424 struct got_object_id_queue blamed_commits;
425 struct got_object_qid *blamed_commit;
426 char *path;
427 struct got_repository *repo;
428 struct got_object_id *commit_id;
429 struct tog_blame blame;
430 int matched_line;
431 struct tog_colors colors;
432 };
434 struct tog_parent_tree {
435 TAILQ_ENTRY(tog_parent_tree) entry;
436 struct got_tree_object *tree;
437 struct got_tree_entry *first_displayed_entry;
438 struct got_tree_entry *selected_entry;
439 int selected;
440 };
442 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
444 struct tog_tree_view_state {
445 char *tree_label;
446 struct got_object_id *commit_id;/* commit which this tree belongs to */
447 struct got_tree_object *root; /* the commit's root tree entry */
448 struct got_tree_object *tree; /* currently displayed (sub-)tree */
449 struct got_tree_entry *first_displayed_entry;
450 struct got_tree_entry *last_displayed_entry;
451 struct got_tree_entry *selected_entry;
452 int ndisplayed, selected, show_ids;
453 struct tog_parent_trees parents; /* parent trees of current sub-tree */
454 char *head_ref_name;
455 struct got_repository *repo;
456 struct got_tree_entry *matched_entry;
457 struct tog_colors colors;
458 };
460 struct tog_reflist_entry {
461 TAILQ_ENTRY(tog_reflist_entry) entry;
462 struct got_reference *ref;
463 int idx;
464 };
466 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
468 struct tog_ref_view_state {
469 struct tog_reflist_head refs;
470 struct tog_reflist_entry *first_displayed_entry;
471 struct tog_reflist_entry *last_displayed_entry;
472 struct tog_reflist_entry *selected_entry;
473 int nrefs, ndisplayed, selected, show_ids, sort_by_date;
474 struct got_repository *repo;
475 struct tog_reflist_entry *matched_entry;
476 struct tog_colors colors;
477 };
479 /*
480 * We implement two types of views: parent views and child views.
482 * The 'Tab' key switches focus between a parent view and its child view.
483 * Child views are shown side-by-side to their parent view, provided
484 * there is enough screen estate.
486 * When a new view is opened from within a parent view, this new view
487 * becomes a child view of the parent view, replacing any existing child.
489 * When a new view is opened from within a child view, this new view
490 * becomes a parent view which will obscure the views below until the
491 * user quits the new parent view by typing 'q'.
493 * This list of views contains parent views only.
494 * Child views are only pointed to by their parent view.
495 */
496 TAILQ_HEAD(tog_view_list_head, tog_view);
498 struct tog_view {
499 TAILQ_ENTRY(tog_view) entry;
500 WINDOW *window;
501 PANEL *panel;
502 int nlines, ncols, begin_y, begin_x;
503 int lines, cols; /* copies of LINES and COLS */
504 int focussed; /* Only set on one parent or child view at a time. */
505 int dying;
506 struct tog_view *parent;
507 struct tog_view *child;
509 /*
510 * This flag is initially set on parent views when a new child view
511 * is created. It gets toggled when the 'Tab' key switches focus
512 * between parent and child.
513 * The flag indicates whether focus should be passed on to our child
514 * view if this parent view gets picked for focus after another parent
515 * view was closed. This prevents child views from losing focus in such
516 * situations.
517 */
518 int focus_child;
520 /* type-specific state */
521 enum tog_view_type type;
522 union {
523 struct tog_diff_view_state diff;
524 struct tog_log_view_state log;
525 struct tog_blame_view_state blame;
526 struct tog_tree_view_state tree;
527 struct tog_ref_view_state ref;
528 } state;
530 const struct got_error *(*show)(struct tog_view *);
531 const struct got_error *(*input)(struct tog_view **,
532 struct tog_view *, int);
533 const struct got_error *(*close)(struct tog_view *);
535 const struct got_error *(*search_start)(struct tog_view *);
536 const struct got_error *(*search_next)(struct tog_view *);
537 int search_started;
538 int searching;
539 #define TOG_SEARCH_FORWARD 1
540 #define TOG_SEARCH_BACKWARD 2
541 int search_next_done;
542 #define TOG_SEARCH_HAVE_MORE 1
543 #define TOG_SEARCH_NO_MORE 2
544 #define TOG_SEARCH_HAVE_NONE 3
545 regex_t regex;
546 regmatch_t regmatch;
547 };
549 static const struct got_error *open_diff_view(struct tog_view *,
550 struct got_object_id *, struct got_object_id *,
551 const char *, const char *, int, int, int, struct tog_view *,
552 struct got_repository *);
553 static const struct got_error *show_diff_view(struct tog_view *);
554 static const struct got_error *input_diff_view(struct tog_view **,
555 struct tog_view *, int);
556 static const struct got_error* close_diff_view(struct tog_view *);
557 static const struct got_error *search_start_diff_view(struct tog_view *);
558 static const struct got_error *search_next_diff_view(struct tog_view *);
560 static const struct got_error *open_log_view(struct tog_view *,
561 struct got_object_id *, struct got_repository *,
562 const char *, const char *, int);
563 static const struct got_error * show_log_view(struct tog_view *);
564 static const struct got_error *input_log_view(struct tog_view **,
565 struct tog_view *, int);
566 static const struct got_error *close_log_view(struct tog_view *);
567 static const struct got_error *search_start_log_view(struct tog_view *);
568 static const struct got_error *search_next_log_view(struct tog_view *);
570 static const struct got_error *open_blame_view(struct tog_view *, char *,
571 struct got_object_id *, struct got_repository *);
572 static const struct got_error *show_blame_view(struct tog_view *);
573 static const struct got_error *input_blame_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *close_blame_view(struct tog_view *);
576 static const struct got_error *search_start_blame_view(struct tog_view *);
577 static const struct got_error *search_next_blame_view(struct tog_view *);
579 static const struct got_error *open_tree_view(struct tog_view *,
580 struct got_object_id *, const char *, struct got_repository *);
581 static const struct got_error *show_tree_view(struct tog_view *);
582 static const struct got_error *input_tree_view(struct tog_view **,
583 struct tog_view *, int);
584 static const struct got_error *close_tree_view(struct tog_view *);
585 static const struct got_error *search_start_tree_view(struct tog_view *);
586 static const struct got_error *search_next_tree_view(struct tog_view *);
588 static const struct got_error *open_ref_view(struct tog_view *,
589 struct got_repository *);
590 static const struct got_error *show_ref_view(struct tog_view *);
591 static const struct got_error *input_ref_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *close_ref_view(struct tog_view *);
594 static const struct got_error *search_start_ref_view(struct tog_view *);
595 static const struct got_error *search_next_ref_view(struct tog_view *);
597 static volatile sig_atomic_t tog_sigwinch_received;
598 static volatile sig_atomic_t tog_sigpipe_received;
599 static volatile sig_atomic_t tog_sigcont_received;
600 static volatile sig_atomic_t tog_sigint_received;
601 static volatile sig_atomic_t tog_sigterm_received;
603 static void
604 tog_sigwinch(int signo)
606 tog_sigwinch_received = 1;
609 static void
610 tog_sigpipe(int signo)
612 tog_sigpipe_received = 1;
615 static void
616 tog_sigcont(int signo)
618 tog_sigcont_received = 1;
621 static void
622 tog_sigint(int signo)
624 tog_sigint_received = 1;
627 static void
628 tog_sigterm(int signo)
630 tog_sigterm_received = 1;
633 static int
634 tog_fatal_signal_received()
636 return (tog_sigpipe_received ||
637 tog_sigint_received || tog_sigint_received);
641 static const struct got_error *
642 view_close(struct tog_view *view)
644 const struct got_error *err = NULL;
646 if (view->child) {
647 view_close(view->child);
648 view->child = NULL;
650 if (view->close)
651 err = view->close(view);
652 if (view->panel)
653 del_panel(view->panel);
654 if (view->window)
655 delwin(view->window);
656 free(view);
657 return err;
660 static struct tog_view *
661 view_open(int nlines, int ncols, int begin_y, int begin_x,
662 enum tog_view_type type)
664 struct tog_view *view = calloc(1, sizeof(*view));
666 if (view == NULL)
667 return NULL;
669 view->type = type;
670 view->lines = LINES;
671 view->cols = COLS;
672 view->nlines = nlines ? nlines : LINES - begin_y;
673 view->ncols = ncols ? ncols : COLS - begin_x;
674 view->begin_y = begin_y;
675 view->begin_x = begin_x;
676 view->window = newwin(nlines, ncols, begin_y, begin_x);
677 if (view->window == NULL) {
678 view_close(view);
679 return NULL;
681 view->panel = new_panel(view->window);
682 if (view->panel == NULL ||
683 set_panel_userptr(view->panel, view) != OK) {
684 view_close(view);
685 return NULL;
688 keypad(view->window, TRUE);
689 return view;
692 static int
693 view_split_begin_x(int begin_x)
695 if (begin_x > 0 || COLS < 120)
696 return 0;
697 return (COLS - MAX(COLS / 2, 80));
700 static const struct got_error *view_resize(struct tog_view *);
702 static const struct got_error *
703 view_splitscreen(struct tog_view *view)
705 const struct got_error *err = NULL;
707 view->begin_y = 0;
708 view->begin_x = view_split_begin_x(0);
709 view->nlines = LINES;
710 view->ncols = COLS - view->begin_x;
711 view->lines = LINES;
712 view->cols = COLS;
713 err = view_resize(view);
714 if (err)
715 return err;
717 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
718 return got_error_from_errno("mvwin");
720 return NULL;
723 static const struct got_error *
724 view_fullscreen(struct tog_view *view)
726 const struct got_error *err = NULL;
728 view->begin_x = 0;
729 view->begin_y = 0;
730 view->nlines = LINES;
731 view->ncols = COLS;
732 view->lines = LINES;
733 view->cols = COLS;
734 err = view_resize(view);
735 if (err)
736 return err;
738 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
739 return got_error_from_errno("mvwin");
741 return NULL;
744 static int
745 view_is_parent_view(struct tog_view *view)
747 return view->parent == NULL;
750 static const struct got_error *
751 view_resize(struct tog_view *view)
753 int nlines, ncols;
755 if (view->lines > LINES)
756 nlines = view->nlines - (view->lines - LINES);
757 else
758 nlines = view->nlines + (LINES - view->lines);
760 if (view->cols > COLS)
761 ncols = view->ncols - (view->cols - COLS);
762 else
763 ncols = view->ncols + (COLS - view->cols);
765 if (wresize(view->window, nlines, ncols) == ERR)
766 return got_error_from_errno("wresize");
767 if (replace_panel(view->panel, view->window) == ERR)
768 return got_error_from_errno("replace_panel");
769 wclear(view->window);
771 view->nlines = nlines;
772 view->ncols = ncols;
773 view->lines = LINES;
774 view->cols = COLS;
776 if (view->child) {
777 view->child->begin_x = view_split_begin_x(view->begin_x);
778 if (view->child->begin_x == 0) {
779 view_fullscreen(view->child);
780 if (view->child->focussed)
781 show_panel(view->child->panel);
782 else
783 show_panel(view->panel);
784 } else {
785 view_splitscreen(view->child);
786 show_panel(view->child->panel);
790 return NULL;
793 static const struct got_error *
794 view_close_child(struct tog_view *view)
796 const struct got_error *err = NULL;
798 if (view->child == NULL)
799 return NULL;
801 err = view_close(view->child);
802 view->child = NULL;
803 return err;
806 static void
807 view_set_child(struct tog_view *view, struct tog_view *child)
809 view->child = child;
810 child->parent = view;
813 static int
814 view_is_splitscreen(struct tog_view *view)
816 return view->begin_x > 0;
819 static void
820 tog_resizeterm(void)
822 int cols, lines;
823 struct winsize size;
825 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
826 cols = 80; /* Default */
827 lines = 24;
828 } else {
829 cols = size.ws_col;
830 lines = size.ws_row;
832 resize_term(lines, cols);
835 static const struct got_error *
836 view_search_start(struct tog_view *view)
838 const struct got_error *err = NULL;
839 char pattern[1024];
840 int ret;
842 if (view->search_started) {
843 regfree(&view->regex);
844 view->searching = 0;
845 memset(&view->regmatch, 0, sizeof(view->regmatch));
847 view->search_started = 0;
849 if (view->nlines < 1)
850 return NULL;
852 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
853 wclrtoeol(view->window);
855 nocbreak();
856 echo();
857 ret = wgetnstr(view->window, pattern, sizeof(pattern));
858 cbreak();
859 noecho();
860 if (ret == ERR)
861 return NULL;
863 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
864 err = view->search_start(view);
865 if (err) {
866 regfree(&view->regex);
867 return err;
869 view->search_started = 1;
870 view->searching = TOG_SEARCH_FORWARD;
871 view->search_next_done = 0;
872 view->search_next(view);
875 return NULL;
878 static const struct got_error *
879 view_input(struct tog_view **new, int *done, struct tog_view *view,
880 struct tog_view_list_head *views)
882 const struct got_error *err = NULL;
883 struct tog_view *v;
884 int ch, errcode;
886 *new = NULL;
888 /* Clear "no matches" indicator. */
889 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
890 view->search_next_done == TOG_SEARCH_HAVE_NONE)
891 view->search_next_done = TOG_SEARCH_HAVE_MORE;
893 if (view->searching && !view->search_next_done) {
894 errcode = pthread_mutex_unlock(&tog_mutex);
895 if (errcode)
896 return got_error_set_errno(errcode,
897 "pthread_mutex_unlock");
898 sched_yield();
899 errcode = pthread_mutex_lock(&tog_mutex);
900 if (errcode)
901 return got_error_set_errno(errcode,
902 "pthread_mutex_lock");
903 view->search_next(view);
904 return NULL;
907 nodelay(stdscr, FALSE);
908 /* Allow threads to make progress while we are waiting for input. */
909 errcode = pthread_mutex_unlock(&tog_mutex);
910 if (errcode)
911 return got_error_set_errno(errcode, "pthread_mutex_unlock");
912 ch = wgetch(view->window);
913 errcode = pthread_mutex_lock(&tog_mutex);
914 if (errcode)
915 return got_error_set_errno(errcode, "pthread_mutex_lock");
916 nodelay(stdscr, TRUE);
918 if (tog_sigwinch_received || tog_sigcont_received) {
919 tog_resizeterm();
920 tog_sigwinch_received = 0;
921 tog_sigcont_received = 0;
922 TAILQ_FOREACH(v, views, entry) {
923 err = view_resize(v);
924 if (err)
925 return err;
926 err = v->input(new, v, KEY_RESIZE);
927 if (err)
928 return err;
929 if (v->child) {
930 err = view_resize(v->child);
931 if (err)
932 return err;
933 err = v->child->input(new, v->child,
934 KEY_RESIZE);
935 if (err)
936 return err;
941 switch (ch) {
942 case '\t':
943 if (view->child) {
944 view->focussed = 0;
945 view->child->focussed = 1;
946 view->focus_child = 1;
947 } else if (view->parent) {
948 view->focussed = 0;
949 view->parent->focussed = 1;
950 view->parent->focus_child = 0;
952 break;
953 case 'q':
954 err = view->input(new, view, ch);
955 view->dying = 1;
956 break;
957 case 'Q':
958 *done = 1;
959 break;
960 case 'f':
961 if (view_is_parent_view(view)) {
962 if (view->child == NULL)
963 break;
964 if (view_is_splitscreen(view->child)) {
965 view->focussed = 0;
966 view->child->focussed = 1;
967 err = view_fullscreen(view->child);
968 } else
969 err = view_splitscreen(view->child);
970 if (err)
971 break;
972 err = view->child->input(new, view->child,
973 KEY_RESIZE);
974 } else {
975 if (view_is_splitscreen(view)) {
976 view->parent->focussed = 0;
977 view->focussed = 1;
978 err = view_fullscreen(view);
979 } else {
980 err = view_splitscreen(view);
982 if (err)
983 break;
984 err = view->input(new, view, KEY_RESIZE);
986 break;
987 case KEY_RESIZE:
988 break;
989 case '/':
990 if (view->search_start)
991 view_search_start(view);
992 else
993 err = view->input(new, view, ch);
994 break;
995 case 'N':
996 case 'n':
997 if (view->search_started && view->search_next) {
998 view->searching = (ch == 'n' ?
999 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1000 view->search_next_done = 0;
1001 view->search_next(view);
1002 } else
1003 err = view->input(new, view, ch);
1004 break;
1005 default:
1006 err = view->input(new, view, ch);
1007 break;
1010 return err;
1013 void
1014 view_vborder(struct tog_view *view)
1016 PANEL *panel;
1017 const struct tog_view *view_above;
1019 if (view->parent)
1020 return view_vborder(view->parent);
1022 panel = panel_above(view->panel);
1023 if (panel == NULL)
1024 return;
1026 view_above = panel_userptr(panel);
1027 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1028 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1031 int
1032 view_needs_focus_indication(struct tog_view *view)
1034 if (view_is_parent_view(view)) {
1035 if (view->child == NULL || view->child->focussed)
1036 return 0;
1037 if (!view_is_splitscreen(view->child))
1038 return 0;
1039 } else if (!view_is_splitscreen(view))
1040 return 0;
1042 return view->focussed;
1045 static const struct got_error *
1046 view_loop(struct tog_view *view)
1048 const struct got_error *err = NULL;
1049 struct tog_view_list_head views;
1050 struct tog_view *new_view;
1051 int fast_refresh = 10;
1052 int done = 0, errcode;
1054 errcode = pthread_mutex_lock(&tog_mutex);
1055 if (errcode)
1056 return got_error_set_errno(errcode, "pthread_mutex_lock");
1058 TAILQ_INIT(&views);
1059 TAILQ_INSERT_HEAD(&views, view, entry);
1061 view->focussed = 1;
1062 err = view->show(view);
1063 if (err)
1064 return err;
1065 update_panels();
1066 doupdate();
1067 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1068 /* Refresh fast during initialization, then become slower. */
1069 if (fast_refresh && fast_refresh-- == 0)
1070 halfdelay(10); /* switch to once per second */
1072 err = view_input(&new_view, &done, view, &views);
1073 if (err)
1074 break;
1075 if (view->dying) {
1076 struct tog_view *v, *prev = NULL;
1078 if (view_is_parent_view(view))
1079 prev = TAILQ_PREV(view, tog_view_list_head,
1080 entry);
1081 else if (view->parent)
1082 prev = view->parent;
1084 if (view->parent) {
1085 view->parent->child = NULL;
1086 view->parent->focus_child = 0;
1087 } else
1088 TAILQ_REMOVE(&views, view, entry);
1090 err = view_close(view);
1091 if (err)
1092 goto done;
1094 view = NULL;
1095 TAILQ_FOREACH(v, &views, entry) {
1096 if (v->focussed)
1097 break;
1099 if (view == NULL && new_view == NULL) {
1100 /* No view has focus. Try to pick one. */
1101 if (prev)
1102 view = prev;
1103 else if (!TAILQ_EMPTY(&views)) {
1104 view = TAILQ_LAST(&views,
1105 tog_view_list_head);
1107 if (view) {
1108 if (view->focus_child) {
1109 view->child->focussed = 1;
1110 view = view->child;
1111 } else
1112 view->focussed = 1;
1116 if (new_view) {
1117 struct tog_view *v, *t;
1118 /* Only allow one parent view per type. */
1119 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1120 if (v->type != new_view->type)
1121 continue;
1122 TAILQ_REMOVE(&views, v, entry);
1123 err = view_close(v);
1124 if (err)
1125 goto done;
1126 break;
1128 TAILQ_INSERT_TAIL(&views, new_view, entry);
1129 view = new_view;
1131 if (view) {
1132 if (view_is_parent_view(view)) {
1133 if (view->child && view->child->focussed)
1134 view = view->child;
1135 } else {
1136 if (view->parent && view->parent->focussed)
1137 view = view->parent;
1139 show_panel(view->panel);
1140 if (view->child && view_is_splitscreen(view->child))
1141 show_panel(view->child->panel);
1142 if (view->parent && view_is_splitscreen(view)) {
1143 err = view->parent->show(view->parent);
1144 if (err)
1145 goto done;
1147 err = view->show(view);
1148 if (err)
1149 goto done;
1150 if (view->child) {
1151 err = view->child->show(view->child);
1152 if (err)
1153 goto done;
1155 update_panels();
1156 doupdate();
1159 done:
1160 while (!TAILQ_EMPTY(&views)) {
1161 view = TAILQ_FIRST(&views);
1162 TAILQ_REMOVE(&views, view, entry);
1163 view_close(view);
1166 errcode = pthread_mutex_unlock(&tog_mutex);
1167 if (errcode && err == NULL)
1168 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1170 return err;
1173 __dead static void
1174 usage_log(void)
1176 endwin();
1177 fprintf(stderr,
1178 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1179 getprogname());
1180 exit(1);
1183 /* Create newly allocated wide-character string equivalent to a byte string. */
1184 static const struct got_error *
1185 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1187 char *vis = NULL;
1188 const struct got_error *err = NULL;
1190 *ws = NULL;
1191 *wlen = mbstowcs(NULL, s, 0);
1192 if (*wlen == (size_t)-1) {
1193 int vislen;
1194 if (errno != EILSEQ)
1195 return got_error_from_errno("mbstowcs");
1197 /* byte string invalid in current encoding; try to "fix" it */
1198 err = got_mbsavis(&vis, &vislen, s);
1199 if (err)
1200 return err;
1201 *wlen = mbstowcs(NULL, vis, 0);
1202 if (*wlen == (size_t)-1) {
1203 err = got_error_from_errno("mbstowcs"); /* give up */
1204 goto done;
1208 *ws = calloc(*wlen + 1, sizeof(**ws));
1209 if (*ws == NULL) {
1210 err = got_error_from_errno("calloc");
1211 goto done;
1214 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1215 err = got_error_from_errno("mbstowcs");
1216 done:
1217 free(vis);
1218 if (err) {
1219 free(*ws);
1220 *ws = NULL;
1221 *wlen = 0;
1223 return err;
1226 /* Format a line for display, ensuring that it won't overflow a width limit. */
1227 static const struct got_error *
1228 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1229 int col_tab_align)
1231 const struct got_error *err = NULL;
1232 int cols = 0;
1233 wchar_t *wline = NULL;
1234 size_t wlen;
1235 int i;
1237 *wlinep = NULL;
1238 *widthp = 0;
1240 err = mbs2ws(&wline, &wlen, line);
1241 if (err)
1242 return err;
1244 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1245 wline[wlen - 1] = L'\0';
1246 wlen--;
1248 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1249 wline[wlen - 1] = L'\0';
1250 wlen--;
1253 i = 0;
1254 while (i < wlen) {
1255 int width = wcwidth(wline[i]);
1257 if (width == 0) {
1258 i++;
1259 continue;
1262 if (width == 1 || width == 2) {
1263 if (cols + width > wlimit)
1264 break;
1265 cols += width;
1266 i++;
1267 } else if (width == -1) {
1268 if (wline[i] == L'\t') {
1269 width = TABSIZE -
1270 ((cols + col_tab_align) % TABSIZE);
1271 } else {
1272 width = 1;
1273 wline[i] = L'.';
1275 if (cols + width > wlimit)
1276 break;
1277 cols += width;
1278 i++;
1279 } else {
1280 err = got_error_from_errno("wcwidth");
1281 goto done;
1284 wline[i] = L'\0';
1285 if (widthp)
1286 *widthp = cols;
1287 done:
1288 if (err)
1289 free(wline);
1290 else
1291 *wlinep = wline;
1292 return err;
1295 static const struct got_error*
1296 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1297 struct got_object_id *id, struct got_repository *repo)
1299 static const struct got_error *err = NULL;
1300 struct got_reflist_entry *re;
1301 char *s;
1302 const char *name;
1304 *refs_str = NULL;
1306 TAILQ_FOREACH(re, refs, entry) {
1307 struct got_tag_object *tag = NULL;
1308 struct got_object_id *ref_id;
1309 int cmp;
1311 name = got_ref_get_name(re->ref);
1312 if (strcmp(name, GOT_REF_HEAD) == 0)
1313 continue;
1314 if (strncmp(name, "refs/", 5) == 0)
1315 name += 5;
1316 if (strncmp(name, "got/", 4) == 0 &&
1317 strncmp(name, "got/backup/", 11) != 0)
1318 continue;
1319 if (strncmp(name, "heads/", 6) == 0)
1320 name += 6;
1321 if (strncmp(name, "remotes/", 8) == 0) {
1322 name += 8;
1323 s = strstr(name, "/" GOT_REF_HEAD);
1324 if (s != NULL && s[strlen(s)] == '\0')
1325 continue;
1327 err = got_ref_resolve(&ref_id, repo, re->ref);
1328 if (err)
1329 break;
1330 if (strncmp(name, "tags/", 5) == 0) {
1331 err = got_object_open_as_tag(&tag, repo, ref_id);
1332 if (err) {
1333 if (err->code != GOT_ERR_OBJ_TYPE) {
1334 free(ref_id);
1335 break;
1337 /* Ref points at something other than a tag. */
1338 err = NULL;
1339 tag = NULL;
1342 cmp = got_object_id_cmp(tag ?
1343 got_object_tag_get_object_id(tag) : ref_id, id);
1344 free(ref_id);
1345 if (tag)
1346 got_object_tag_close(tag);
1347 if (cmp != 0)
1348 continue;
1349 s = *refs_str;
1350 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1351 s ? ", " : "", name) == -1) {
1352 err = got_error_from_errno("asprintf");
1353 free(s);
1354 *refs_str = NULL;
1355 break;
1357 free(s);
1360 return err;
1363 static const struct got_error *
1364 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1365 int col_tab_align)
1367 char *smallerthan;
1369 smallerthan = strchr(author, '<');
1370 if (smallerthan && smallerthan[1] != '\0')
1371 author = smallerthan + 1;
1372 author[strcspn(author, "@>")] = '\0';
1373 return format_line(wauthor, author_width, author, limit, col_tab_align);
1376 static const struct got_error *
1377 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1378 struct got_object_id *id, const size_t date_display_cols,
1379 int author_display_cols)
1381 struct tog_log_view_state *s = &view->state.log;
1382 const struct got_error *err = NULL;
1383 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1384 char *logmsg0 = NULL, *logmsg = NULL;
1385 char *author = NULL;
1386 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1387 int author_width, logmsg_width;
1388 char *newline, *line = NULL;
1389 int col, limit;
1390 const int avail = view->ncols;
1391 struct tm tm;
1392 time_t committer_time;
1393 struct tog_color *tc;
1395 committer_time = got_object_commit_get_committer_time(commit);
1396 if (gmtime_r(&committer_time, &tm) == NULL)
1397 return got_error_from_errno("gmtime_r");
1398 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1399 return got_error(GOT_ERR_NO_SPACE);
1401 if (avail <= date_display_cols)
1402 limit = MIN(sizeof(datebuf) - 1, avail);
1403 else
1404 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1405 tc = get_color(&s->colors, TOG_COLOR_DATE);
1406 if (tc)
1407 wattr_on(view->window,
1408 COLOR_PAIR(tc->colorpair), NULL);
1409 waddnstr(view->window, datebuf, limit);
1410 if (tc)
1411 wattr_off(view->window,
1412 COLOR_PAIR(tc->colorpair), NULL);
1413 col = limit;
1414 if (col > avail)
1415 goto done;
1417 if (avail >= 120) {
1418 char *id_str;
1419 err = got_object_id_str(&id_str, id);
1420 if (err)
1421 goto done;
1422 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1423 if (tc)
1424 wattr_on(view->window,
1425 COLOR_PAIR(tc->colorpair), NULL);
1426 wprintw(view->window, "%.8s ", id_str);
1427 if (tc)
1428 wattr_off(view->window,
1429 COLOR_PAIR(tc->colorpair), NULL);
1430 free(id_str);
1431 col += 9;
1432 if (col > avail)
1433 goto done;
1436 author = strdup(got_object_commit_get_author(commit));
1437 if (author == NULL) {
1438 err = got_error_from_errno("strdup");
1439 goto done;
1441 err = format_author(&wauthor, &author_width, author, avail - col, col);
1442 if (err)
1443 goto done;
1444 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1445 if (tc)
1446 wattr_on(view->window,
1447 COLOR_PAIR(tc->colorpair), NULL);
1448 waddwstr(view->window, wauthor);
1449 if (tc)
1450 wattr_off(view->window,
1451 COLOR_PAIR(tc->colorpair), NULL);
1452 col += author_width;
1453 while (col < avail && author_width < author_display_cols + 2) {
1454 waddch(view->window, ' ');
1455 col++;
1456 author_width++;
1458 if (col > avail)
1459 goto done;
1461 err = got_object_commit_get_logmsg(&logmsg0, commit);
1462 if (err)
1463 goto done;
1464 logmsg = logmsg0;
1465 while (*logmsg == '\n')
1466 logmsg++;
1467 newline = strchr(logmsg, '\n');
1468 if (newline)
1469 *newline = '\0';
1470 limit = avail - col;
1471 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1472 if (err)
1473 goto done;
1474 waddwstr(view->window, wlogmsg);
1475 col += logmsg_width;
1476 while (col < avail) {
1477 waddch(view->window, ' ');
1478 col++;
1480 done:
1481 free(logmsg0);
1482 free(wlogmsg);
1483 free(author);
1484 free(wauthor);
1485 free(line);
1486 return err;
1489 static struct commit_queue_entry *
1490 alloc_commit_queue_entry(struct got_commit_object *commit,
1491 struct got_object_id *id)
1493 struct commit_queue_entry *entry;
1495 entry = calloc(1, sizeof(*entry));
1496 if (entry == NULL)
1497 return NULL;
1499 entry->id = id;
1500 entry->commit = commit;
1501 return entry;
1504 static void
1505 pop_commit(struct commit_queue *commits)
1507 struct commit_queue_entry *entry;
1509 entry = TAILQ_FIRST(&commits->head);
1510 TAILQ_REMOVE(&commits->head, entry, entry);
1511 got_object_commit_close(entry->commit);
1512 commits->ncommits--;
1513 /* Don't free entry->id! It is owned by the commit graph. */
1514 free(entry);
1517 static void
1518 free_commits(struct commit_queue *commits)
1520 while (!TAILQ_EMPTY(&commits->head))
1521 pop_commit(commits);
1524 static const struct got_error *
1525 match_commit(int *have_match, struct got_object_id *id,
1526 struct got_commit_object *commit, regex_t *regex)
1528 const struct got_error *err = NULL;
1529 regmatch_t regmatch;
1530 char *id_str = NULL, *logmsg = NULL;
1532 *have_match = 0;
1534 err = got_object_id_str(&id_str, id);
1535 if (err)
1536 return err;
1538 err = got_object_commit_get_logmsg(&logmsg, commit);
1539 if (err)
1540 goto done;
1542 if (regexec(regex, got_object_commit_get_author(commit), 1,
1543 &regmatch, 0) == 0 ||
1544 regexec(regex, got_object_commit_get_committer(commit), 1,
1545 &regmatch, 0) == 0 ||
1546 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1547 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1548 *have_match = 1;
1549 done:
1550 free(id_str);
1551 free(logmsg);
1552 return err;
1555 static const struct got_error *
1556 queue_commits(struct tog_log_thread_args *a)
1558 const struct got_error *err = NULL;
1561 * We keep all commits open throughout the lifetime of the log
1562 * view in order to avoid having to re-fetch commits from disk
1563 * while updating the display.
1565 do {
1566 struct got_object_id *id;
1567 struct got_commit_object *commit;
1568 struct commit_queue_entry *entry;
1569 int errcode;
1571 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1572 NULL, NULL);
1573 if (err || id == NULL)
1574 break;
1576 err = got_object_open_as_commit(&commit, a->repo, id);
1577 if (err)
1578 break;
1579 entry = alloc_commit_queue_entry(commit, id);
1580 if (entry == NULL) {
1581 err = got_error_from_errno("alloc_commit_queue_entry");
1582 break;
1585 errcode = pthread_mutex_lock(&tog_mutex);
1586 if (errcode) {
1587 err = got_error_set_errno(errcode,
1588 "pthread_mutex_lock");
1589 break;
1592 entry->idx = a->commits->ncommits;
1593 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1594 a->commits->ncommits++;
1596 if (*a->searching == TOG_SEARCH_FORWARD &&
1597 !*a->search_next_done) {
1598 int have_match;
1599 err = match_commit(&have_match, id, commit, a->regex);
1600 if (err)
1601 break;
1602 if (have_match)
1603 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1606 errcode = pthread_mutex_unlock(&tog_mutex);
1607 if (errcode && err == NULL)
1608 err = got_error_set_errno(errcode,
1609 "pthread_mutex_unlock");
1610 if (err)
1611 break;
1612 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1614 return err;
1617 static void
1618 select_commit(struct tog_log_view_state *s)
1620 struct commit_queue_entry *entry;
1621 int ncommits = 0;
1623 entry = s->first_displayed_entry;
1624 while (entry) {
1625 if (ncommits == s->selected) {
1626 s->selected_entry = entry;
1627 break;
1629 entry = TAILQ_NEXT(entry, entry);
1630 ncommits++;
1634 static const struct got_error *
1635 draw_commits(struct tog_view *view)
1637 const struct got_error *err = NULL;
1638 struct tog_log_view_state *s = &view->state.log;
1639 struct commit_queue_entry *entry = s->selected_entry;
1640 const int limit = view->nlines;
1641 int width;
1642 int ncommits, author_cols = 4;
1643 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1644 char *refs_str = NULL;
1645 wchar_t *wline;
1646 struct tog_color *tc;
1647 static const size_t date_display_cols = 12;
1649 if (s->selected_entry &&
1650 !(view->searching && view->search_next_done == 0)) {
1651 struct got_reflist_head *refs;
1652 err = got_object_id_str(&id_str, s->selected_entry->id);
1653 if (err)
1654 return err;
1655 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1656 s->selected_entry->id);
1657 if (refs) {
1658 err = build_refs_str(&refs_str, refs,
1659 s->selected_entry->id, s->repo);
1660 if (err)
1661 goto done;
1665 if (s->thread_args.commits_needed == 0)
1666 halfdelay(10); /* disable fast refresh */
1668 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1669 if (asprintf(&ncommits_str, " [%d/%d] %s",
1670 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1671 (view->searching && !view->search_next_done) ?
1672 "searching..." : "loading...") == -1) {
1673 err = got_error_from_errno("asprintf");
1674 goto done;
1676 } else {
1677 const char *search_str = NULL;
1679 if (view->searching) {
1680 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1681 search_str = "no more matches";
1682 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1683 search_str = "no matches found";
1684 else if (!view->search_next_done)
1685 search_str = "searching...";
1688 if (asprintf(&ncommits_str, " [%d/%d] %s",
1689 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1690 search_str ? search_str :
1691 (refs_str ? refs_str : "")) == -1) {
1692 err = got_error_from_errno("asprintf");
1693 goto done;
1697 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1698 if (asprintf(&header, "commit %s %s%s",
1699 id_str ? id_str : "........................................",
1700 s->in_repo_path, ncommits_str) == -1) {
1701 err = got_error_from_errno("asprintf");
1702 header = NULL;
1703 goto done;
1705 } else if (asprintf(&header, "commit %s%s",
1706 id_str ? id_str : "........................................",
1707 ncommits_str) == -1) {
1708 err = got_error_from_errno("asprintf");
1709 header = NULL;
1710 goto done;
1712 err = format_line(&wline, &width, header, view->ncols, 0);
1713 if (err)
1714 goto done;
1716 werase(view->window);
1718 if (view_needs_focus_indication(view))
1719 wstandout(view->window);
1720 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1721 if (tc)
1722 wattr_on(view->window,
1723 COLOR_PAIR(tc->colorpair), NULL);
1724 waddwstr(view->window, wline);
1725 if (tc)
1726 wattr_off(view->window,
1727 COLOR_PAIR(tc->colorpair), NULL);
1728 while (width < view->ncols) {
1729 waddch(view->window, ' ');
1730 width++;
1732 if (view_needs_focus_indication(view))
1733 wstandend(view->window);
1734 free(wline);
1735 if (limit <= 1)
1736 goto done;
1738 /* Grow author column size if necessary. */
1739 entry = s->first_displayed_entry;
1740 ncommits = 0;
1741 while (entry) {
1742 char *author;
1743 wchar_t *wauthor;
1744 int width;
1745 if (ncommits >= limit - 1)
1746 break;
1747 author = strdup(got_object_commit_get_author(entry->commit));
1748 if (author == NULL) {
1749 err = got_error_from_errno("strdup");
1750 goto done;
1752 err = format_author(&wauthor, &width, author, COLS,
1753 date_display_cols);
1754 if (author_cols < width)
1755 author_cols = width;
1756 free(wauthor);
1757 free(author);
1758 ncommits++;
1759 entry = TAILQ_NEXT(entry, entry);
1762 entry = s->first_displayed_entry;
1763 s->last_displayed_entry = s->first_displayed_entry;
1764 ncommits = 0;
1765 while (entry) {
1766 if (ncommits >= limit - 1)
1767 break;
1768 if (ncommits == s->selected)
1769 wstandout(view->window);
1770 err = draw_commit(view, entry->commit, entry->id,
1771 date_display_cols, author_cols);
1772 if (ncommits == s->selected)
1773 wstandend(view->window);
1774 if (err)
1775 goto done;
1776 ncommits++;
1777 s->last_displayed_entry = entry;
1778 entry = TAILQ_NEXT(entry, entry);
1781 view_vborder(view);
1782 done:
1783 free(id_str);
1784 free(refs_str);
1785 free(ncommits_str);
1786 free(header);
1787 return err;
1790 static void
1791 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1793 struct commit_queue_entry *entry;
1794 int nscrolled = 0;
1796 entry = TAILQ_FIRST(&s->commits.head);
1797 if (s->first_displayed_entry == entry)
1798 return;
1800 entry = s->first_displayed_entry;
1801 while (entry && nscrolled < maxscroll) {
1802 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1803 if (entry) {
1804 s->first_displayed_entry = entry;
1805 nscrolled++;
1810 static const struct got_error *
1811 trigger_log_thread(struct tog_view *view, int wait)
1813 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1814 int errcode;
1816 halfdelay(1); /* fast refresh while loading commits */
1818 while (ta->commits_needed > 0 || ta->load_all) {
1819 if (ta->log_complete)
1820 break;
1822 /* Wake the log thread. */
1823 errcode = pthread_cond_signal(&ta->need_commits);
1824 if (errcode)
1825 return got_error_set_errno(errcode,
1826 "pthread_cond_signal");
1829 * The mutex will be released while the view loop waits
1830 * in wgetch(), at which time the log thread will run.
1832 if (!wait)
1833 break;
1835 /* Display progress update in log view. */
1836 show_log_view(view);
1837 update_panels();
1838 doupdate();
1840 /* Wait right here while next commit is being loaded. */
1841 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1842 if (errcode)
1843 return got_error_set_errno(errcode,
1844 "pthread_cond_wait");
1846 /* Display progress update in log view. */
1847 show_log_view(view);
1848 update_panels();
1849 doupdate();
1852 return NULL;
1855 static const struct got_error *
1856 log_scroll_down(struct tog_view *view, int maxscroll)
1858 struct tog_log_view_state *s = &view->state.log;
1859 const struct got_error *err = NULL;
1860 struct commit_queue_entry *pentry;
1861 int nscrolled = 0, ncommits_needed;
1863 if (s->last_displayed_entry == NULL)
1864 return NULL;
1866 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1867 if (s->commits.ncommits < ncommits_needed &&
1868 !s->thread_args.log_complete) {
1870 * Ask the log thread for required amount of commits.
1872 s->thread_args.commits_needed += maxscroll;
1873 err = trigger_log_thread(view, 1);
1874 if (err)
1875 return err;
1878 do {
1879 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1880 if (pentry == NULL)
1881 break;
1883 s->last_displayed_entry = pentry;
1885 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1886 if (pentry == NULL)
1887 break;
1888 s->first_displayed_entry = pentry;
1889 } while (++nscrolled < maxscroll);
1891 return err;
1894 static const struct got_error *
1895 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1896 struct got_commit_object *commit, struct got_object_id *commit_id,
1897 struct tog_view *log_view, struct got_repository *repo)
1899 const struct got_error *err;
1900 struct got_object_qid *parent_id;
1901 struct tog_view *diff_view;
1903 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1904 if (diff_view == NULL)
1905 return got_error_from_errno("view_open");
1907 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1908 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1909 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1910 if (err == NULL)
1911 *new_view = diff_view;
1912 return err;
1915 static const struct got_error *
1916 tree_view_visit_subtree(struct tog_tree_view_state *s,
1917 struct got_tree_object *subtree)
1919 struct tog_parent_tree *parent;
1921 parent = calloc(1, sizeof(*parent));
1922 if (parent == NULL)
1923 return got_error_from_errno("calloc");
1925 parent->tree = s->tree;
1926 parent->first_displayed_entry = s->first_displayed_entry;
1927 parent->selected_entry = s->selected_entry;
1928 parent->selected = s->selected;
1929 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1930 s->tree = subtree;
1931 s->selected = 0;
1932 s->first_displayed_entry = NULL;
1933 return NULL;
1936 static const struct got_error *
1937 tree_view_walk_path(struct tog_tree_view_state *s,
1938 struct got_commit_object *commit, const char *path)
1940 const struct got_error *err = NULL;
1941 struct got_tree_object *tree = NULL;
1942 const char *p;
1943 char *slash, *subpath = NULL;
1945 /* Walk the path and open corresponding tree objects. */
1946 p = path;
1947 while (*p) {
1948 struct got_tree_entry *te;
1949 struct got_object_id *tree_id;
1950 char *te_name;
1952 while (p[0] == '/')
1953 p++;
1955 /* Ensure the correct subtree entry is selected. */
1956 slash = strchr(p, '/');
1957 if (slash == NULL)
1958 te_name = strdup(p);
1959 else
1960 te_name = strndup(p, slash - p);
1961 if (te_name == NULL) {
1962 err = got_error_from_errno("strndup");
1963 break;
1965 te = got_object_tree_find_entry(s->tree, te_name);
1966 if (te == NULL) {
1967 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1968 free(te_name);
1969 break;
1971 free(te_name);
1972 s->first_displayed_entry = s->selected_entry = te;
1974 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1975 break; /* jump to this file's entry */
1977 slash = strchr(p, '/');
1978 if (slash)
1979 subpath = strndup(path, slash - path);
1980 else
1981 subpath = strdup(path);
1982 if (subpath == NULL) {
1983 err = got_error_from_errno("strdup");
1984 break;
1987 err = got_object_id_by_path(&tree_id, s->repo, commit,
1988 subpath);
1989 if (err)
1990 break;
1992 err = got_object_open_as_tree(&tree, s->repo, tree_id);
1993 free(tree_id);
1994 if (err)
1995 break;
1997 err = tree_view_visit_subtree(s, tree);
1998 if (err) {
1999 got_object_tree_close(tree);
2000 break;
2002 if (slash == NULL)
2003 break;
2004 free(subpath);
2005 subpath = NULL;
2006 p = slash;
2009 free(subpath);
2010 return err;
2013 static const struct got_error *
2014 browse_commit_tree(struct tog_view **new_view, int begin_x,
2015 struct commit_queue_entry *entry, const char *path,
2016 const char *head_ref_name, struct got_repository *repo)
2018 const struct got_error *err = NULL;
2019 struct tog_tree_view_state *s;
2020 struct tog_view *tree_view;
2022 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2023 if (tree_view == NULL)
2024 return got_error_from_errno("view_open");
2026 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2027 if (err)
2028 return err;
2029 s = &tree_view->state.tree;
2031 *new_view = tree_view;
2033 if (got_path_is_root_dir(path))
2034 return NULL;
2036 return tree_view_walk_path(s, entry->commit, path);
2039 static const struct got_error *
2040 block_signals_used_by_main_thread(void)
2042 sigset_t sigset;
2043 int errcode;
2045 if (sigemptyset(&sigset) == -1)
2046 return got_error_from_errno("sigemptyset");
2048 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2049 if (sigaddset(&sigset, SIGWINCH) == -1)
2050 return got_error_from_errno("sigaddset");
2051 if (sigaddset(&sigset, SIGCONT) == -1)
2052 return got_error_from_errno("sigaddset");
2053 if (sigaddset(&sigset, SIGINT) == -1)
2054 return got_error_from_errno("sigaddset");
2055 if (sigaddset(&sigset, SIGTERM) == -1)
2056 return got_error_from_errno("sigaddset");
2058 /* ncurses handles SIGTSTP */
2059 if (sigaddset(&sigset, SIGTSTP) == -1)
2060 return got_error_from_errno("sigaddset");
2062 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2063 if (errcode)
2064 return got_error_set_errno(errcode, "pthread_sigmask");
2066 return NULL;
2069 static void *
2070 log_thread(void *arg)
2072 const struct got_error *err = NULL;
2073 int errcode = 0;
2074 struct tog_log_thread_args *a = arg;
2075 int done = 0;
2077 err = block_signals_used_by_main_thread();
2078 if (err)
2079 return (void *)err;
2081 while (!done && !err && !tog_fatal_signal_received()) {
2082 err = queue_commits(a);
2083 if (err) {
2084 if (err->code != GOT_ERR_ITER_COMPLETED)
2085 return (void *)err;
2086 err = NULL;
2087 done = 1;
2088 } else if (a->commits_needed > 0 && !a->load_all)
2089 a->commits_needed--;
2091 errcode = pthread_mutex_lock(&tog_mutex);
2092 if (errcode) {
2093 err = got_error_set_errno(errcode,
2094 "pthread_mutex_lock");
2095 break;
2096 } else if (*a->quit)
2097 done = 1;
2098 else if (*a->first_displayed_entry == NULL) {
2099 *a->first_displayed_entry =
2100 TAILQ_FIRST(&a->commits->head);
2101 *a->selected_entry = *a->first_displayed_entry;
2104 errcode = pthread_cond_signal(&a->commit_loaded);
2105 if (errcode) {
2106 err = got_error_set_errno(errcode,
2107 "pthread_cond_signal");
2108 pthread_mutex_unlock(&tog_mutex);
2109 break;
2112 if (done)
2113 a->commits_needed = 0;
2114 else {
2115 if (a->commits_needed == 0 && !a->load_all) {
2116 errcode = pthread_cond_wait(&a->need_commits,
2117 &tog_mutex);
2118 if (errcode)
2119 err = got_error_set_errno(errcode,
2120 "pthread_cond_wait");
2121 if (*a->quit)
2122 done = 1;
2126 errcode = pthread_mutex_unlock(&tog_mutex);
2127 if (errcode && err == NULL)
2128 err = got_error_set_errno(errcode,
2129 "pthread_mutex_unlock");
2131 a->log_complete = 1;
2132 return (void *)err;
2135 static const struct got_error *
2136 stop_log_thread(struct tog_log_view_state *s)
2138 const struct got_error *err = NULL;
2139 int errcode;
2141 if (s->thread) {
2142 s->quit = 1;
2143 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2144 if (errcode)
2145 return got_error_set_errno(errcode,
2146 "pthread_cond_signal");
2147 errcode = pthread_mutex_unlock(&tog_mutex);
2148 if (errcode)
2149 return got_error_set_errno(errcode,
2150 "pthread_mutex_unlock");
2151 errcode = pthread_join(s->thread, (void **)&err);
2152 if (errcode)
2153 return got_error_set_errno(errcode, "pthread_join");
2154 errcode = pthread_mutex_lock(&tog_mutex);
2155 if (errcode)
2156 return got_error_set_errno(errcode,
2157 "pthread_mutex_lock");
2158 s->thread = NULL;
2161 if (s->thread_args.repo) {
2162 err = got_repo_close(s->thread_args.repo);
2163 s->thread_args.repo = NULL;
2166 if (s->thread_args.pack_fds) {
2167 const struct got_error *pack_err =
2168 got_repo_pack_fds_close(s->thread_args.pack_fds);
2169 if (err == NULL)
2170 err = pack_err;
2171 s->thread_args.pack_fds = NULL;
2174 if (s->thread_args.graph) {
2175 got_commit_graph_close(s->thread_args.graph);
2176 s->thread_args.graph = NULL;
2179 return err;
2182 static const struct got_error *
2183 close_log_view(struct tog_view *view)
2185 const struct got_error *err = NULL;
2186 struct tog_log_view_state *s = &view->state.log;
2187 int errcode;
2189 err = stop_log_thread(s);
2191 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2192 if (errcode && err == NULL)
2193 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2195 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2196 if (errcode && err == NULL)
2197 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2199 free_commits(&s->commits);
2200 free(s->in_repo_path);
2201 s->in_repo_path = NULL;
2202 free(s->start_id);
2203 s->start_id = NULL;
2204 free(s->head_ref_name);
2205 s->head_ref_name = NULL;
2206 return err;
2209 static const struct got_error *
2210 search_start_log_view(struct tog_view *view)
2212 struct tog_log_view_state *s = &view->state.log;
2214 s->matched_entry = NULL;
2215 s->search_entry = NULL;
2216 return NULL;
2219 static const struct got_error *
2220 search_next_log_view(struct tog_view *view)
2222 const struct got_error *err = NULL;
2223 struct tog_log_view_state *s = &view->state.log;
2224 struct commit_queue_entry *entry;
2226 /* Display progress update in log view. */
2227 show_log_view(view);
2228 update_panels();
2229 doupdate();
2231 if (s->search_entry) {
2232 int errcode, ch;
2233 errcode = pthread_mutex_unlock(&tog_mutex);
2234 if (errcode)
2235 return got_error_set_errno(errcode,
2236 "pthread_mutex_unlock");
2237 ch = wgetch(view->window);
2238 errcode = pthread_mutex_lock(&tog_mutex);
2239 if (errcode)
2240 return got_error_set_errno(errcode,
2241 "pthread_mutex_lock");
2242 if (ch == KEY_BACKSPACE) {
2243 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2244 return NULL;
2246 if (view->searching == TOG_SEARCH_FORWARD)
2247 entry = TAILQ_NEXT(s->search_entry, entry);
2248 else
2249 entry = TAILQ_PREV(s->search_entry,
2250 commit_queue_head, entry);
2251 } else if (s->matched_entry) {
2252 if (view->searching == TOG_SEARCH_FORWARD)
2253 entry = TAILQ_NEXT(s->matched_entry, entry);
2254 else
2255 entry = TAILQ_PREV(s->matched_entry,
2256 commit_queue_head, entry);
2257 } else {
2258 entry = s->selected_entry;
2261 while (1) {
2262 int have_match = 0;
2264 if (entry == NULL) {
2265 if (s->thread_args.log_complete ||
2266 view->searching == TOG_SEARCH_BACKWARD) {
2267 view->search_next_done =
2268 (s->matched_entry == NULL ?
2269 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2270 s->search_entry = NULL;
2271 return NULL;
2274 * Poke the log thread for more commits and return,
2275 * allowing the main loop to make progress. Search
2276 * will resume at s->search_entry once we come back.
2278 s->thread_args.commits_needed++;
2279 return trigger_log_thread(view, 0);
2282 err = match_commit(&have_match, entry->id, entry->commit,
2283 &view->regex);
2284 if (err)
2285 break;
2286 if (have_match) {
2287 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2288 s->matched_entry = entry;
2289 break;
2292 s->search_entry = entry;
2293 if (view->searching == TOG_SEARCH_FORWARD)
2294 entry = TAILQ_NEXT(entry, entry);
2295 else
2296 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2299 if (s->matched_entry) {
2300 int cur = s->selected_entry->idx;
2301 while (cur < s->matched_entry->idx) {
2302 err = input_log_view(NULL, view, KEY_DOWN);
2303 if (err)
2304 return err;
2305 cur++;
2307 while (cur > s->matched_entry->idx) {
2308 err = input_log_view(NULL, view, KEY_UP);
2309 if (err)
2310 return err;
2311 cur--;
2315 s->search_entry = NULL;
2317 return NULL;
2320 static const struct got_error *
2321 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2322 struct got_repository *repo, const char *head_ref_name,
2323 const char *in_repo_path, int log_branches)
2325 const struct got_error *err = NULL;
2326 struct tog_log_view_state *s = &view->state.log;
2327 struct got_repository *thread_repo = NULL;
2328 struct got_commit_graph *thread_graph = NULL;
2329 int errcode;
2331 if (in_repo_path != s->in_repo_path) {
2332 free(s->in_repo_path);
2333 s->in_repo_path = strdup(in_repo_path);
2334 if (s->in_repo_path == NULL)
2335 return got_error_from_errno("strdup");
2338 /* The commit queue only contains commits being displayed. */
2339 TAILQ_INIT(&s->commits.head);
2340 s->commits.ncommits = 0;
2342 s->repo = repo;
2343 if (head_ref_name) {
2344 s->head_ref_name = strdup(head_ref_name);
2345 if (s->head_ref_name == NULL) {
2346 err = got_error_from_errno("strdup");
2347 goto done;
2350 s->start_id = got_object_id_dup(start_id);
2351 if (s->start_id == NULL) {
2352 err = got_error_from_errno("got_object_id_dup");
2353 goto done;
2355 s->log_branches = log_branches;
2357 STAILQ_INIT(&s->colors);
2358 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2359 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2360 get_color_value("TOG_COLOR_COMMIT"));
2361 if (err)
2362 goto done;
2363 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2364 get_color_value("TOG_COLOR_AUTHOR"));
2365 if (err) {
2366 free_colors(&s->colors);
2367 goto done;
2369 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2370 get_color_value("TOG_COLOR_DATE"));
2371 if (err) {
2372 free_colors(&s->colors);
2373 goto done;
2377 view->show = show_log_view;
2378 view->input = input_log_view;
2379 view->close = close_log_view;
2380 view->search_start = search_start_log_view;
2381 view->search_next = search_next_log_view;
2383 if (s->thread_args.pack_fds == NULL) {
2384 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2385 if (err)
2386 goto done;
2388 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2389 s->thread_args.pack_fds);
2390 if (err)
2391 goto done;
2392 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2393 !s->log_branches);
2394 if (err)
2395 goto done;
2396 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2397 s->repo, NULL, NULL);
2398 if (err)
2399 goto done;
2401 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2402 if (errcode) {
2403 err = got_error_set_errno(errcode, "pthread_cond_init");
2404 goto done;
2406 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2407 if (errcode) {
2408 err = got_error_set_errno(errcode, "pthread_cond_init");
2409 goto done;
2412 s->thread_args.commits_needed = view->nlines;
2413 s->thread_args.graph = thread_graph;
2414 s->thread_args.commits = &s->commits;
2415 s->thread_args.in_repo_path = s->in_repo_path;
2416 s->thread_args.start_id = s->start_id;
2417 s->thread_args.repo = thread_repo;
2418 s->thread_args.log_complete = 0;
2419 s->thread_args.quit = &s->quit;
2420 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2421 s->thread_args.selected_entry = &s->selected_entry;
2422 s->thread_args.searching = &view->searching;
2423 s->thread_args.search_next_done = &view->search_next_done;
2424 s->thread_args.regex = &view->regex;
2425 done:
2426 if (err)
2427 close_log_view(view);
2428 return err;
2431 static const struct got_error *
2432 show_log_view(struct tog_view *view)
2434 const struct got_error *err;
2435 struct tog_log_view_state *s = &view->state.log;
2437 if (s->thread == NULL) {
2438 int errcode = pthread_create(&s->thread, NULL, log_thread,
2439 &s->thread_args);
2440 if (errcode)
2441 return got_error_set_errno(errcode, "pthread_create");
2442 if (s->thread_args.commits_needed > 0) {
2443 err = trigger_log_thread(view, 1);
2444 if (err)
2445 return err;
2449 return draw_commits(view);
2452 static const struct got_error *
2453 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2455 const struct got_error *err = NULL;
2456 struct tog_log_view_state *s = &view->state.log;
2457 struct tog_view *diff_view = NULL, *tree_view = NULL;
2458 struct tog_view *ref_view = NULL;
2459 struct commit_queue_entry *entry;
2460 int begin_x = 0, n, nscroll = view->nlines - 1;
2462 if (s->thread_args.load_all) {
2463 if (ch == KEY_BACKSPACE)
2464 s->thread_args.load_all = 0;
2465 else if (s->thread_args.log_complete) {
2466 s->thread_args.load_all = 0;
2467 log_scroll_down(view, s->commits.ncommits);
2468 s->selected = MIN(view->nlines - 2,
2469 s->commits.ncommits - 1);
2470 select_commit(s);
2472 return NULL;
2475 switch (ch) {
2476 case 'q':
2477 s->quit = 1;
2478 break;
2479 case 'k':
2480 case KEY_UP:
2481 case '<':
2482 case ',':
2483 case CTRL('p'):
2484 if (s->first_displayed_entry == NULL)
2485 break;
2486 if (s->selected > 0)
2487 s->selected--;
2488 else
2489 log_scroll_up(s, 1);
2490 select_commit(s);
2491 break;
2492 case 'g':
2493 case KEY_HOME:
2494 s->selected = 0;
2495 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2496 select_commit(s);
2497 break;
2498 case CTRL('u'):
2499 case 'u':
2500 nscroll /= 2;
2501 /* FALL THROUGH */
2502 case KEY_PPAGE:
2503 case CTRL('b'):
2504 if (s->first_displayed_entry == NULL)
2505 break;
2506 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2507 s->selected = MAX(0, s->selected - nscroll - 1);
2508 else
2509 log_scroll_up(s, nscroll);
2510 select_commit(s);
2511 break;
2512 case 'j':
2513 case KEY_DOWN:
2514 case '>':
2515 case '.':
2516 case CTRL('n'):
2517 if (s->first_displayed_entry == NULL)
2518 break;
2519 if (s->selected < MIN(view->nlines - 2,
2520 s->commits.ncommits - 1))
2521 s->selected++;
2522 else {
2523 err = log_scroll_down(view, 1);
2524 if (err)
2525 break;
2527 select_commit(s);
2528 break;
2529 case 'G':
2530 case KEY_END: {
2531 /* We don't know yet how many commits, so we're forced to
2532 * traverse them all. */
2533 if (!s->thread_args.log_complete) {
2534 s->thread_args.load_all = 1;
2535 return trigger_log_thread(view, 0);
2538 s->selected = 0;
2539 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2540 for (n = 0; n < view->nlines - 1; n++) {
2541 if (entry == NULL)
2542 break;
2543 s->first_displayed_entry = entry;
2544 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2546 if (n > 0)
2547 s->selected = n - 1;
2548 select_commit(s);
2549 break;
2551 case CTRL('d'):
2552 case 'd':
2553 nscroll /= 2;
2554 /* FALL THROUGH */
2555 case KEY_NPAGE:
2556 case CTRL('f'): {
2557 struct commit_queue_entry *first;
2558 first = s->first_displayed_entry;
2559 if (first == NULL)
2560 break;
2561 err = log_scroll_down(view, nscroll);
2562 if (err)
2563 break;
2564 if (first == s->first_displayed_entry &&
2565 s->selected < MIN(view->nlines - 2,
2566 s->commits.ncommits - 1)) {
2567 /* can't scroll further down */
2568 s->selected += MIN(s->last_displayed_entry->idx -
2569 s->selected_entry->idx, nscroll + 1);
2571 select_commit(s);
2572 break;
2574 case KEY_RESIZE:
2575 if (s->selected > view->nlines - 2)
2576 s->selected = view->nlines - 2;
2577 if (s->selected > s->commits.ncommits - 1)
2578 s->selected = s->commits.ncommits - 1;
2579 select_commit(s);
2580 if (s->commits.ncommits < view->nlines - 1 &&
2581 !s->thread_args.log_complete) {
2582 s->thread_args.commits_needed += (view->nlines - 1) -
2583 s->commits.ncommits;
2584 err = trigger_log_thread(view, 1);
2586 break;
2587 case KEY_ENTER:
2588 case ' ':
2589 case '\r':
2590 if (s->selected_entry == NULL)
2591 break;
2592 if (view_is_parent_view(view))
2593 begin_x = view_split_begin_x(view->begin_x);
2594 err = open_diff_view_for_commit(&diff_view, begin_x,
2595 s->selected_entry->commit, s->selected_entry->id,
2596 view, s->repo);
2597 if (err)
2598 break;
2599 view->focussed = 0;
2600 diff_view->focussed = 1;
2601 if (view_is_parent_view(view)) {
2602 err = view_close_child(view);
2603 if (err)
2604 return err;
2605 view_set_child(view, diff_view);
2606 view->focus_child = 1;
2607 } else
2608 *new_view = diff_view;
2609 break;
2610 case 't':
2611 if (s->selected_entry == NULL)
2612 break;
2613 if (view_is_parent_view(view))
2614 begin_x = view_split_begin_x(view->begin_x);
2615 err = browse_commit_tree(&tree_view, begin_x,
2616 s->selected_entry, s->in_repo_path, s->head_ref_name,
2617 s->repo);
2618 if (err)
2619 break;
2620 view->focussed = 0;
2621 tree_view->focussed = 1;
2622 if (view_is_parent_view(view)) {
2623 err = view_close_child(view);
2624 if (err)
2625 return err;
2626 view_set_child(view, tree_view);
2627 view->focus_child = 1;
2628 } else
2629 *new_view = tree_view;
2630 break;
2631 case KEY_BACKSPACE:
2632 case CTRL('l'):
2633 case 'B':
2634 if (ch == KEY_BACKSPACE &&
2635 got_path_is_root_dir(s->in_repo_path))
2636 break;
2637 err = stop_log_thread(s);
2638 if (err)
2639 return err;
2640 if (ch == KEY_BACKSPACE) {
2641 char *parent_path;
2642 err = got_path_dirname(&parent_path, s->in_repo_path);
2643 if (err)
2644 return err;
2645 free(s->in_repo_path);
2646 s->in_repo_path = parent_path;
2647 s->thread_args.in_repo_path = s->in_repo_path;
2648 } else if (ch == CTRL('l')) {
2649 struct got_object_id *start_id;
2650 err = got_repo_match_object_id(&start_id, NULL,
2651 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2652 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2653 if (err)
2654 return err;
2655 free(s->start_id);
2656 s->start_id = start_id;
2657 s->thread_args.start_id = s->start_id;
2658 } else /* 'B' */
2659 s->log_branches = !s->log_branches;
2661 err = got_repo_open(&s->thread_args.repo,
2662 got_repo_get_path(s->repo), NULL,
2663 s->thread_args.pack_fds);
2664 if (err)
2665 return err;
2666 tog_free_refs();
2667 err = tog_load_refs(s->repo, 0);
2668 if (err)
2669 return err;
2670 err = got_commit_graph_open(&s->thread_args.graph,
2671 s->in_repo_path, !s->log_branches);
2672 if (err)
2673 return err;
2674 err = got_commit_graph_iter_start(s->thread_args.graph,
2675 s->start_id, s->repo, NULL, NULL);
2676 if (err)
2677 return err;
2678 free_commits(&s->commits);
2679 s->first_displayed_entry = NULL;
2680 s->last_displayed_entry = NULL;
2681 s->selected_entry = NULL;
2682 s->selected = 0;
2683 s->thread_args.log_complete = 0;
2684 s->quit = 0;
2685 s->thread_args.commits_needed = view->nlines;
2686 break;
2687 case 'r':
2688 if (view_is_parent_view(view))
2689 begin_x = view_split_begin_x(view->begin_x);
2690 ref_view = view_open(view->nlines, view->ncols,
2691 view->begin_y, begin_x, TOG_VIEW_REF);
2692 if (ref_view == NULL)
2693 return got_error_from_errno("view_open");
2694 err = open_ref_view(ref_view, s->repo);
2695 if (err) {
2696 view_close(ref_view);
2697 return err;
2699 view->focussed = 0;
2700 ref_view->focussed = 1;
2701 if (view_is_parent_view(view)) {
2702 err = view_close_child(view);
2703 if (err)
2704 return err;
2705 view_set_child(view, ref_view);
2706 view->focus_child = 1;
2707 } else
2708 *new_view = ref_view;
2709 break;
2710 default:
2711 break;
2714 return err;
2717 static const struct got_error *
2718 apply_unveil(const char *repo_path, const char *worktree_path)
2720 const struct got_error *error;
2722 #ifdef PROFILE
2723 if (unveil("gmon.out", "rwc") != 0)
2724 return got_error_from_errno2("unveil", "gmon.out");
2725 #endif
2726 if (repo_path && unveil(repo_path, "r") != 0)
2727 return got_error_from_errno2("unveil", repo_path);
2729 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2730 return got_error_from_errno2("unveil", worktree_path);
2732 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2733 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2735 error = got_privsep_unveil_exec_helpers();
2736 if (error != NULL)
2737 return error;
2739 if (unveil(NULL, NULL) != 0)
2740 return got_error_from_errno("unveil");
2742 return NULL;
2745 static void
2746 init_curses(void)
2749 * Override default signal handlers before starting ncurses.
2750 * This should prevent ncurses from installing its own
2751 * broken cleanup() signal handler.
2753 signal(SIGWINCH, tog_sigwinch);
2754 signal(SIGPIPE, tog_sigpipe);
2755 signal(SIGCONT, tog_sigcont);
2756 signal(SIGINT, tog_sigint);
2757 signal(SIGTERM, tog_sigterm);
2759 initscr();
2760 cbreak();
2761 halfdelay(1); /* Do fast refresh while initial view is loading. */
2762 noecho();
2763 nonl();
2764 intrflush(stdscr, FALSE);
2765 keypad(stdscr, TRUE);
2766 curs_set(0);
2767 if (getenv("TOG_COLORS") != NULL) {
2768 start_color();
2769 use_default_colors();
2773 static const struct got_error *
2774 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2775 struct got_repository *repo, struct got_worktree *worktree)
2777 const struct got_error *err = NULL;
2779 if (argc == 0) {
2780 *in_repo_path = strdup("/");
2781 if (*in_repo_path == NULL)
2782 return got_error_from_errno("strdup");
2783 return NULL;
2786 if (worktree) {
2787 const char *prefix = got_worktree_get_path_prefix(worktree);
2788 char *p;
2790 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2791 if (err)
2792 return err;
2793 if (asprintf(in_repo_path, "%s%s%s", prefix,
2794 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2795 p) == -1) {
2796 err = got_error_from_errno("asprintf");
2797 *in_repo_path = NULL;
2799 free(p);
2800 } else
2801 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2803 return err;
2806 static const struct got_error *
2807 cmd_log(int argc, char *argv[])
2809 const struct got_error *error;
2810 struct got_repository *repo = NULL;
2811 struct got_worktree *worktree = NULL;
2812 struct got_object_id *start_id = NULL;
2813 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2814 char *start_commit = NULL, *label = NULL;
2815 struct got_reference *ref = NULL;
2816 const char *head_ref_name = NULL;
2817 int ch, log_branches = 0;
2818 struct tog_view *view;
2819 int *pack_fds = NULL;
2821 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2822 switch (ch) {
2823 case 'b':
2824 log_branches = 1;
2825 break;
2826 case 'c':
2827 start_commit = optarg;
2828 break;
2829 case 'r':
2830 repo_path = realpath(optarg, NULL);
2831 if (repo_path == NULL)
2832 return got_error_from_errno2("realpath",
2833 optarg);
2834 break;
2835 default:
2836 usage_log();
2837 /* NOTREACHED */
2841 argc -= optind;
2842 argv += optind;
2844 if (argc > 1)
2845 usage_log();
2847 error = got_repo_pack_fds_open(&pack_fds);
2848 if (error != NULL)
2849 goto done;
2851 if (repo_path == NULL) {
2852 cwd = getcwd(NULL, 0);
2853 if (cwd == NULL)
2854 return got_error_from_errno("getcwd");
2855 error = got_worktree_open(&worktree, cwd);
2856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2857 goto done;
2858 if (worktree)
2859 repo_path =
2860 strdup(got_worktree_get_repo_path(worktree));
2861 else
2862 repo_path = strdup(cwd);
2863 if (repo_path == NULL) {
2864 error = got_error_from_errno("strdup");
2865 goto done;
2869 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2870 if (error != NULL)
2871 goto done;
2873 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2874 repo, worktree);
2875 if (error)
2876 goto done;
2878 init_curses();
2880 error = apply_unveil(got_repo_get_path(repo),
2881 worktree ? got_worktree_get_root_path(worktree) : NULL);
2882 if (error)
2883 goto done;
2885 /* already loaded by tog_log_with_path()? */
2886 if (TAILQ_EMPTY(&tog_refs)) {
2887 error = tog_load_refs(repo, 0);
2888 if (error)
2889 goto done;
2892 if (start_commit == NULL) {
2893 error = got_repo_match_object_id(&start_id, &label,
2894 worktree ? got_worktree_get_head_ref_name(worktree) :
2895 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2896 if (error)
2897 goto done;
2898 head_ref_name = label;
2899 } else {
2900 error = got_ref_open(&ref, repo, start_commit, 0);
2901 if (error == NULL)
2902 head_ref_name = got_ref_get_name(ref);
2903 else if (error->code != GOT_ERR_NOT_REF)
2904 goto done;
2905 error = got_repo_match_object_id(&start_id, NULL,
2906 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2907 if (error)
2908 goto done;
2911 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2912 if (view == NULL) {
2913 error = got_error_from_errno("view_open");
2914 goto done;
2916 error = open_log_view(view, start_id, repo, head_ref_name,
2917 in_repo_path, log_branches);
2918 if (error)
2919 goto done;
2920 if (worktree) {
2921 /* Release work tree lock. */
2922 got_worktree_close(worktree);
2923 worktree = NULL;
2925 error = view_loop(view);
2926 done:
2927 free(in_repo_path);
2928 free(repo_path);
2929 free(cwd);
2930 free(start_id);
2931 free(label);
2932 if (ref)
2933 got_ref_close(ref);
2934 if (repo) {
2935 const struct got_error *close_err = got_repo_close(repo);
2936 if (error == NULL)
2937 error = close_err;
2939 if (worktree)
2940 got_worktree_close(worktree);
2941 if (pack_fds) {
2942 const struct got_error *pack_err =
2943 got_repo_pack_fds_close(pack_fds);
2944 if (error == NULL)
2945 error = pack_err;
2947 tog_free_refs();
2948 return error;
2951 __dead static void
2952 usage_diff(void)
2954 endwin();
2955 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2956 "[-w] object1 object2\n", getprogname());
2957 exit(1);
2960 static int
2961 match_line(const char *line, regex_t *regex, size_t nmatch,
2962 regmatch_t *regmatch)
2964 return regexec(regex, line, nmatch, regmatch, 0) == 0;
2967 struct tog_color *
2968 match_color(struct tog_colors *colors, const char *line)
2970 struct tog_color *tc = NULL;
2972 STAILQ_FOREACH(tc, colors, entry) {
2973 if (match_line(line, &tc->regex, 0, NULL))
2974 return tc;
2977 return NULL;
2980 static const struct got_error *
2981 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2982 WINDOW *window, regmatch_t *regmatch)
2984 const struct got_error *err = NULL;
2985 wchar_t *wline;
2986 int width;
2987 char *s;
2989 *wtotal = 0;
2991 s = strndup(line, regmatch->rm_so);
2992 if (s == NULL)
2993 return got_error_from_errno("strndup");
2995 err = format_line(&wline, &width, s, wlimit, col_tab_align);
2996 if (err) {
2997 free(s);
2998 return err;
3000 waddwstr(window, wline);
3001 free(wline);
3002 free(s);
3003 wlimit -= width;
3004 *wtotal += width;
3006 if (wlimit > 0) {
3007 s = strndup(line + regmatch->rm_so,
3008 regmatch->rm_eo - regmatch->rm_so);
3009 if (s == NULL) {
3010 err = got_error_from_errno("strndup");
3011 free(s);
3012 return err;
3014 err = format_line(&wline, &width, s, wlimit, col_tab_align);
3015 if (err) {
3016 free(s);
3017 return err;
3019 wattr_on(window, A_STANDOUT, NULL);
3020 waddwstr(window, wline);
3021 wattr_off(window, A_STANDOUT, NULL);
3022 free(wline);
3023 free(s);
3024 wlimit -= width;
3025 *wtotal += width;
3028 if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
3029 err = format_line(&wline, &width,
3030 line + regmatch->rm_eo, wlimit, col_tab_align);
3031 if (err)
3032 return err;
3033 waddwstr(window, wline);
3034 free(wline);
3035 *wtotal += width;
3038 return NULL;
3041 static const struct got_error *
3042 draw_file(struct tog_view *view, const char *header)
3044 struct tog_diff_view_state *s = &view->state.diff;
3045 regmatch_t *regmatch = &view->regmatch;
3046 const struct got_error *err;
3047 int nprinted = 0;
3048 char *line;
3049 size_t linesize = 0;
3050 ssize_t linelen;
3051 struct tog_color *tc;
3052 wchar_t *wline;
3053 int width;
3054 int max_lines = view->nlines;
3055 int nlines = s->nlines;
3056 off_t line_offset;
3058 line_offset = s->line_offsets[s->first_displayed_line - 1];
3059 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3060 return got_error_from_errno("fseek");
3062 werase(view->window);
3064 if (header) {
3065 if (asprintf(&line, "[%d/%d] %s",
3066 s->first_displayed_line - 1 + s->selected_line, nlines,
3067 header) == -1)
3068 return got_error_from_errno("asprintf");
3069 err = format_line(&wline, &width, line, view->ncols, 0);
3070 free(line);
3071 if (err)
3072 return err;
3074 if (view_needs_focus_indication(view))
3075 wstandout(view->window);
3076 waddwstr(view->window, wline);
3077 free(wline);
3078 wline = NULL;
3079 if (view_needs_focus_indication(view))
3080 wstandend(view->window);
3081 if (width <= view->ncols - 1)
3082 waddch(view->window, '\n');
3084 if (max_lines <= 1)
3085 return NULL;
3086 max_lines--;
3089 s->eof = 0;
3090 line = NULL;
3091 while (max_lines > 0 && nprinted < max_lines) {
3092 linelen = getline(&line, &linesize, s->f);
3093 if (linelen == -1) {
3094 if (feof(s->f)) {
3095 s->eof = 1;
3096 break;
3098 free(line);
3099 return got_ferror(s->f, GOT_ERR_IO);
3102 tc = match_color(&s->colors, line);
3103 if (tc)
3104 wattr_on(view->window,
3105 COLOR_PAIR(tc->colorpair), NULL);
3106 if (s->first_displayed_line + nprinted == s->matched_line &&
3107 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3108 err = add_matched_line(&width, line, view->ncols, 0,
3109 view->window, regmatch);
3110 if (err) {
3111 free(line);
3112 return err;
3114 } else {
3115 err = format_line(&wline, &width, line, view->ncols, 0);
3116 if (err) {
3117 free(line);
3118 return err;
3120 waddwstr(view->window, wline);
3121 free(wline);
3122 wline = NULL;
3124 if (tc)
3125 wattr_off(view->window,
3126 COLOR_PAIR(tc->colorpair), NULL);
3127 if (width <= view->ncols - 1)
3128 waddch(view->window, '\n');
3129 nprinted++;
3131 free(line);
3132 if (nprinted >= 1)
3133 s->last_displayed_line = s->first_displayed_line +
3134 (nprinted - 1);
3135 else
3136 s->last_displayed_line = s->first_displayed_line;
3138 view_vborder(view);
3140 if (s->eof) {
3141 while (nprinted < view->nlines) {
3142 waddch(view->window, '\n');
3143 nprinted++;
3146 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3147 if (err) {
3148 return err;
3151 wstandout(view->window);
3152 waddwstr(view->window, wline);
3153 free(wline);
3154 wline = NULL;
3155 wstandend(view->window);
3158 return NULL;
3161 static char *
3162 get_datestr(time_t *time, char *datebuf)
3164 struct tm mytm, *tm;
3165 char *p, *s;
3167 tm = gmtime_r(time, &mytm);
3168 if (tm == NULL)
3169 return NULL;
3170 s = asctime_r(tm, datebuf);
3171 if (s == NULL)
3172 return NULL;
3173 p = strchr(s, '\n');
3174 if (p)
3175 *p = '\0';
3176 return s;
3179 static const struct got_error *
3180 get_changed_paths(struct got_pathlist_head *paths,
3181 struct got_commit_object *commit, struct got_repository *repo)
3183 const struct got_error *err = NULL;
3184 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3185 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3186 struct got_object_qid *qid;
3188 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3189 if (qid != NULL) {
3190 struct got_commit_object *pcommit;
3191 err = got_object_open_as_commit(&pcommit, repo,
3192 &qid->id);
3193 if (err)
3194 return err;
3196 tree_id1 = got_object_id_dup(
3197 got_object_commit_get_tree_id(pcommit));
3198 if (tree_id1 == NULL) {
3199 got_object_commit_close(pcommit);
3200 return got_error_from_errno("got_object_id_dup");
3202 got_object_commit_close(pcommit);
3206 if (tree_id1) {
3207 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3208 if (err)
3209 goto done;
3212 tree_id2 = got_object_commit_get_tree_id(commit);
3213 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3214 if (err)
3215 goto done;
3217 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3218 got_diff_tree_collect_changed_paths, paths, 0);
3219 done:
3220 if (tree1)
3221 got_object_tree_close(tree1);
3222 if (tree2)
3223 got_object_tree_close(tree2);
3224 free(tree_id1);
3225 return err;
3228 static const struct got_error *
3229 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3231 off_t *p;
3233 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3234 if (p == NULL)
3235 return got_error_from_errno("reallocarray");
3236 *line_offsets = p;
3237 (*line_offsets)[*nlines] = off;
3238 (*nlines)++;
3239 return NULL;
3242 static const struct got_error *
3243 write_commit_info(off_t **line_offsets, size_t *nlines,
3244 struct got_object_id *commit_id, struct got_reflist_head *refs,
3245 struct got_repository *repo, FILE *outfile)
3247 const struct got_error *err = NULL;
3248 char datebuf[26], *datestr;
3249 struct got_commit_object *commit;
3250 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3251 time_t committer_time;
3252 const char *author, *committer;
3253 char *refs_str = NULL;
3254 struct got_pathlist_head changed_paths;
3255 struct got_pathlist_entry *pe;
3256 off_t outoff = 0;
3257 int n;
3259 TAILQ_INIT(&changed_paths);
3261 if (refs) {
3262 err = build_refs_str(&refs_str, refs, commit_id, repo);
3263 if (err)
3264 return err;
3267 err = got_object_open_as_commit(&commit, repo, commit_id);
3268 if (err)
3269 return err;
3271 err = got_object_id_str(&id_str, commit_id);
3272 if (err) {
3273 err = got_error_from_errno("got_object_id_str");
3274 goto done;
3277 err = add_line_offset(line_offsets, nlines, 0);
3278 if (err)
3279 goto done;
3281 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3282 refs_str ? refs_str : "", refs_str ? ")" : "");
3283 if (n < 0) {
3284 err = got_error_from_errno("fprintf");
3285 goto done;
3287 outoff += n;
3288 err = add_line_offset(line_offsets, nlines, outoff);
3289 if (err)
3290 goto done;
3292 n = fprintf(outfile, "from: %s\n",
3293 got_object_commit_get_author(commit));
3294 if (n < 0) {
3295 err = got_error_from_errno("fprintf");
3296 goto done;
3298 outoff += n;
3299 err = add_line_offset(line_offsets, nlines, outoff);
3300 if (err)
3301 goto done;
3303 committer_time = got_object_commit_get_committer_time(commit);
3304 datestr = get_datestr(&committer_time, datebuf);
3305 if (datestr) {
3306 n = fprintf(outfile, "date: %s UTC\n", datestr);
3307 if (n < 0) {
3308 err = got_error_from_errno("fprintf");
3309 goto done;
3311 outoff += n;
3312 err = add_line_offset(line_offsets, nlines, outoff);
3313 if (err)
3314 goto done;
3316 author = got_object_commit_get_author(commit);
3317 committer = got_object_commit_get_committer(commit);
3318 if (strcmp(author, committer) != 0) {
3319 n = fprintf(outfile, "via: %s\n", committer);
3320 if (n < 0) {
3321 err = got_error_from_errno("fprintf");
3322 goto done;
3324 outoff += n;
3325 err = add_line_offset(line_offsets, nlines, outoff);
3326 if (err)
3327 goto done;
3329 if (got_object_commit_get_nparents(commit) > 1) {
3330 const struct got_object_id_queue *parent_ids;
3331 struct got_object_qid *qid;
3332 int pn = 1;
3333 parent_ids = got_object_commit_get_parent_ids(commit);
3334 STAILQ_FOREACH(qid, parent_ids, entry) {
3335 err = got_object_id_str(&id_str, &qid->id);
3336 if (err)
3337 goto done;
3338 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3339 if (n < 0) {
3340 err = got_error_from_errno("fprintf");
3341 goto done;
3343 outoff += n;
3344 err = add_line_offset(line_offsets, nlines, outoff);
3345 if (err)
3346 goto done;
3347 free(id_str);
3348 id_str = NULL;
3352 err = got_object_commit_get_logmsg(&logmsg, commit);
3353 if (err)
3354 goto done;
3355 s = logmsg;
3356 while ((line = strsep(&s, "\n")) != NULL) {
3357 n = fprintf(outfile, "%s\n", line);
3358 if (n < 0) {
3359 err = got_error_from_errno("fprintf");
3360 goto done;
3362 outoff += n;
3363 err = add_line_offset(line_offsets, nlines, outoff);
3364 if (err)
3365 goto done;
3368 err = get_changed_paths(&changed_paths, commit, repo);
3369 if (err)
3370 goto done;
3371 TAILQ_FOREACH(pe, &changed_paths, entry) {
3372 struct got_diff_changed_path *cp = pe->data;
3373 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3374 if (n < 0) {
3375 err = got_error_from_errno("fprintf");
3376 goto done;
3378 outoff += n;
3379 err = add_line_offset(line_offsets, nlines, outoff);
3380 if (err)
3381 goto done;
3382 free((char *)pe->path);
3383 free(pe->data);
3386 fputc('\n', outfile);
3387 outoff++;
3388 err = add_line_offset(line_offsets, nlines, outoff);
3389 done:
3390 got_pathlist_free(&changed_paths);
3391 free(id_str);
3392 free(logmsg);
3393 free(refs_str);
3394 got_object_commit_close(commit);
3395 if (err) {
3396 free(*line_offsets);
3397 *line_offsets = NULL;
3398 *nlines = 0;
3400 return err;
3403 static const struct got_error *
3404 create_diff(struct tog_diff_view_state *s)
3406 const struct got_error *err = NULL;
3407 FILE *f = NULL;
3408 int obj_type;
3410 free(s->line_offsets);
3411 s->line_offsets = malloc(sizeof(off_t));
3412 if (s->line_offsets == NULL)
3413 return got_error_from_errno("malloc");
3414 s->nlines = 0;
3416 f = got_opentemp();
3417 if (f == NULL) {
3418 err = got_error_from_errno("got_opentemp");
3419 goto done;
3421 if (s->f && fclose(s->f) == EOF) {
3422 err = got_error_from_errno("fclose");
3423 goto done;
3425 s->f = f;
3427 if (s->id1)
3428 err = got_object_get_type(&obj_type, s->repo, s->id1);
3429 else
3430 err = got_object_get_type(&obj_type, s->repo, s->id2);
3431 if (err)
3432 goto done;
3434 switch (obj_type) {
3435 case GOT_OBJ_TYPE_BLOB:
3436 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3437 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3438 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3439 s->repo, s->f);
3440 break;
3441 case GOT_OBJ_TYPE_TREE:
3442 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3443 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3444 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3445 break;
3446 case GOT_OBJ_TYPE_COMMIT: {
3447 const struct got_object_id_queue *parent_ids;
3448 struct got_object_qid *pid;
3449 struct got_commit_object *commit2;
3450 struct got_reflist_head *refs;
3452 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3453 if (err)
3454 goto done;
3455 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3456 /* Show commit info if we're diffing to a parent/root commit. */
3457 if (s->id1 == NULL) {
3458 err = write_commit_info(&s->line_offsets, &s->nlines,
3459 s->id2, refs, s->repo, s->f);
3460 if (err)
3461 goto done;
3462 } else {
3463 parent_ids = got_object_commit_get_parent_ids(commit2);
3464 STAILQ_FOREACH(pid, parent_ids, entry) {
3465 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3466 err = write_commit_info(
3467 &s->line_offsets, &s->nlines,
3468 s->id2, refs, s->repo, s->f);
3469 if (err)
3470 goto done;
3471 break;
3475 got_object_commit_close(commit2);
3477 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3478 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3479 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3480 break;
3482 default:
3483 err = got_error(GOT_ERR_OBJ_TYPE);
3484 break;
3486 if (err)
3487 goto done;
3488 done:
3489 if (s->f && fflush(s->f) != 0 && err == NULL)
3490 err = got_error_from_errno("fflush");
3491 return err;
3494 static void
3495 diff_view_indicate_progress(struct tog_view *view)
3497 mvwaddstr(view->window, 0, 0, "diffing...");
3498 update_panels();
3499 doupdate();
3502 static const struct got_error *
3503 search_start_diff_view(struct tog_view *view)
3505 struct tog_diff_view_state *s = &view->state.diff;
3507 s->matched_line = 0;
3508 return NULL;
3511 static const struct got_error *
3512 search_next_diff_view(struct tog_view *view)
3514 struct tog_diff_view_state *s = &view->state.diff;
3515 int lineno;
3516 char *line = NULL;
3517 size_t linesize = 0;
3518 ssize_t linelen;
3520 if (!view->searching) {
3521 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3522 return NULL;
3525 if (s->matched_line) {
3526 if (view->searching == TOG_SEARCH_FORWARD)
3527 lineno = s->matched_line + 1;
3528 else
3529 lineno = s->matched_line - 1;
3530 } else
3531 lineno = s->first_displayed_line;
3533 while (1) {
3534 off_t offset;
3536 if (lineno <= 0 || lineno > s->nlines) {
3537 if (s->matched_line == 0) {
3538 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3539 break;
3542 if (view->searching == TOG_SEARCH_FORWARD)
3543 lineno = 1;
3544 else
3545 lineno = s->nlines;
3548 offset = s->line_offsets[lineno - 1];
3549 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3550 free(line);
3551 return got_error_from_errno("fseeko");
3553 linelen = getline(&line, &linesize, s->f);
3554 if (linelen != -1 &&
3555 match_line(line, &view->regex, 1, &view->regmatch)) {
3556 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3557 s->matched_line = lineno;
3558 break;
3560 if (view->searching == TOG_SEARCH_FORWARD)
3561 lineno++;
3562 else
3563 lineno--;
3565 free(line);
3567 if (s->matched_line) {
3568 s->first_displayed_line = s->matched_line;
3569 s->selected_line = 1;
3572 return NULL;
3575 static const struct got_error *
3576 close_diff_view(struct tog_view *view)
3578 const struct got_error *err = NULL;
3579 struct tog_diff_view_state *s = &view->state.diff;
3581 free(s->id1);
3582 s->id1 = NULL;
3583 free(s->id2);
3584 s->id2 = NULL;
3585 if (s->f && fclose(s->f) == EOF)
3586 err = got_error_from_errno("fclose");
3587 s->f = NULL;
3588 if (s->f1 && fclose(s->f1) == EOF)
3589 err = got_error_from_errno("fclose");
3590 s->f1 = NULL;
3591 if (s->f2 && fclose(s->f2) == EOF)
3592 err = got_error_from_errno("fclose");
3593 s->f2 = NULL;
3594 free_colors(&s->colors);
3595 free(s->line_offsets);
3596 s->line_offsets = NULL;
3597 s->nlines = 0;
3598 return err;
3601 static const struct got_error *
3602 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3603 struct got_object_id *id2, const char *label1, const char *label2,
3604 int diff_context, int ignore_whitespace, int force_text_diff,
3605 struct tog_view *log_view, struct got_repository *repo)
3607 const struct got_error *err;
3608 struct tog_diff_view_state *s = &view->state.diff;
3610 memset(s, 0, sizeof(*s));
3612 if (id1 != NULL && id2 != NULL) {
3613 int type1, type2;
3614 err = got_object_get_type(&type1, repo, id1);
3615 if (err)
3616 return err;
3617 err = got_object_get_type(&type2, repo, id2);
3618 if (err)
3619 return err;
3621 if (type1 != type2)
3622 return got_error(GOT_ERR_OBJ_TYPE);
3624 s->first_displayed_line = 1;
3625 s->last_displayed_line = view->nlines;
3626 s->selected_line = 1;
3627 s->repo = repo;
3628 s->id1 = id1;
3629 s->id2 = id2;
3630 s->label1 = label1;
3631 s->label2 = label2;
3633 if (id1) {
3634 s->id1 = got_object_id_dup(id1);
3635 if (s->id1 == NULL)
3636 return got_error_from_errno("got_object_id_dup");
3637 s->f1 = got_opentemp();
3638 if (s->f1 == NULL) {
3639 err = got_error_from_errno("got_opentemp");
3640 goto done;
3642 } else
3643 s->id1 = NULL;
3645 s->id2 = got_object_id_dup(id2);
3646 if (s->id2 == NULL) {
3647 err = got_error_from_errno("got_object_id_dup");
3648 goto done;
3651 s->f2 = got_opentemp();
3652 if (s->f2 == NULL) {
3653 err = got_error_from_errno("got_opentemp");
3654 goto done;
3657 s->first_displayed_line = 1;
3658 s->last_displayed_line = view->nlines;
3659 s->diff_context = diff_context;
3660 s->ignore_whitespace = ignore_whitespace;
3661 s->force_text_diff = force_text_diff;
3662 s->log_view = log_view;
3663 s->repo = repo;
3665 STAILQ_INIT(&s->colors);
3666 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3667 err = add_color(&s->colors,
3668 "^-", TOG_COLOR_DIFF_MINUS,
3669 get_color_value("TOG_COLOR_DIFF_MINUS"));
3670 if (err)
3671 goto done;
3672 err = add_color(&s->colors, "^\\+",
3673 TOG_COLOR_DIFF_PLUS,
3674 get_color_value("TOG_COLOR_DIFF_PLUS"));
3675 if (err)
3676 goto done;
3677 err = add_color(&s->colors,
3678 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3679 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3680 if (err)
3681 goto done;
3683 err = add_color(&s->colors,
3684 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3685 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3686 get_color_value("TOG_COLOR_DIFF_META"));
3687 if (err)
3688 goto done;
3690 err = add_color(&s->colors,
3691 "^(from|via): ", TOG_COLOR_AUTHOR,
3692 get_color_value("TOG_COLOR_AUTHOR"));
3693 if (err)
3694 goto done;
3696 err = add_color(&s->colors,
3697 "^date: ", TOG_COLOR_DATE,
3698 get_color_value("TOG_COLOR_DATE"));
3699 if (err)
3700 goto done;
3703 if (log_view && view_is_splitscreen(view))
3704 show_log_view(log_view); /* draw vborder */
3705 diff_view_indicate_progress(view);
3707 err = create_diff(s);
3709 view->show = show_diff_view;
3710 view->input = input_diff_view;
3711 view->close = close_diff_view;
3712 view->search_start = search_start_diff_view;
3713 view->search_next = search_next_diff_view;
3714 done:
3715 if (err)
3716 close_diff_view(view);
3717 return err;
3720 static const struct got_error *
3721 show_diff_view(struct tog_view *view)
3723 const struct got_error *err;
3724 struct tog_diff_view_state *s = &view->state.diff;
3725 char *id_str1 = NULL, *id_str2, *header;
3726 const char *label1, *label2;
3728 if (s->id1) {
3729 err = got_object_id_str(&id_str1, s->id1);
3730 if (err)
3731 return err;
3732 label1 = s->label1 ? : id_str1;
3733 } else
3734 label1 = "/dev/null";
3736 err = got_object_id_str(&id_str2, s->id2);
3737 if (err)
3738 return err;
3739 label2 = s->label2 ? : id_str2;
3741 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3742 err = got_error_from_errno("asprintf");
3743 free(id_str1);
3744 free(id_str2);
3745 return err;
3747 free(id_str1);
3748 free(id_str2);
3750 err = draw_file(view, header);
3751 free(header);
3752 return err;
3755 static const struct got_error *
3756 set_selected_commit(struct tog_diff_view_state *s,
3757 struct commit_queue_entry *entry)
3759 const struct got_error *err;
3760 const struct got_object_id_queue *parent_ids;
3761 struct got_commit_object *selected_commit;
3762 struct got_object_qid *pid;
3764 free(s->id2);
3765 s->id2 = got_object_id_dup(entry->id);
3766 if (s->id2 == NULL)
3767 return got_error_from_errno("got_object_id_dup");
3769 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3770 if (err)
3771 return err;
3772 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3773 free(s->id1);
3774 pid = STAILQ_FIRST(parent_ids);
3775 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3776 got_object_commit_close(selected_commit);
3777 return NULL;
3780 static const struct got_error *
3781 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3783 const struct got_error *err = NULL;
3784 struct tog_diff_view_state *s = &view->state.diff;
3785 struct tog_log_view_state *ls;
3786 struct commit_queue_entry *old_selected_entry;
3787 char *line = NULL;
3788 size_t linesize = 0;
3789 ssize_t linelen;
3790 int i, nscroll = view->nlines - 1;
3792 switch (ch) {
3793 case 'a':
3794 case 'w':
3795 if (ch == 'a')
3796 s->force_text_diff = !s->force_text_diff;
3797 if (ch == 'w')
3798 s->ignore_whitespace = !s->ignore_whitespace;
3799 wclear(view->window);
3800 s->first_displayed_line = 1;
3801 s->last_displayed_line = view->nlines;
3802 s->matched_line = 0;
3803 diff_view_indicate_progress(view);
3804 err = create_diff(s);
3805 break;
3806 case 'g':
3807 case KEY_HOME:
3808 s->first_displayed_line = 1;
3809 break;
3810 case 'G':
3811 case KEY_END:
3812 if (s->eof)
3813 break;
3815 s->first_displayed_line = (s->nlines - view->nlines) + 2;
3816 s->eof = 1;
3817 break;
3818 case 'k':
3819 case KEY_UP:
3820 case CTRL('p'):
3821 if (s->first_displayed_line > 1)
3822 s->first_displayed_line--;
3823 break;
3824 case CTRL('u'):
3825 case 'u':
3826 nscroll /= 2;
3827 /* FALL THROUGH */
3828 case KEY_PPAGE:
3829 case CTRL('b'):
3830 if (s->first_displayed_line == 1)
3831 break;
3832 i = 0;
3833 while (i++ < nscroll && s->first_displayed_line > 1)
3834 s->first_displayed_line--;
3835 break;
3836 case 'j':
3837 case KEY_DOWN:
3838 case CTRL('n'):
3839 if (!s->eof)
3840 s->first_displayed_line++;
3841 break;
3842 case CTRL('d'):
3843 case 'd':
3844 nscroll /= 2;
3845 /* FALL THROUGH */
3846 case KEY_NPAGE:
3847 case CTRL('f'):
3848 case ' ':
3849 if (s->eof)
3850 break;
3851 i = 0;
3852 while (!s->eof && i++ < nscroll) {
3853 linelen = getline(&line, &linesize, s->f);
3854 s->first_displayed_line++;
3855 if (linelen == -1) {
3856 if (feof(s->f)) {
3857 s->eof = 1;
3858 } else
3859 err = got_ferror(s->f, GOT_ERR_IO);
3860 break;
3863 free(line);
3864 break;
3865 case '[':
3866 if (s->diff_context > 0) {
3867 s->diff_context--;
3868 s->matched_line = 0;
3869 diff_view_indicate_progress(view);
3870 err = create_diff(s);
3871 if (s->first_displayed_line + view->nlines - 1 >
3872 s->nlines) {
3873 s->first_displayed_line = 1;
3874 s->last_displayed_line = view->nlines;
3877 break;
3878 case ']':
3879 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3880 s->diff_context++;
3881 s->matched_line = 0;
3882 diff_view_indicate_progress(view);
3883 err = create_diff(s);
3885 break;
3886 case '<':
3887 case ',':
3888 if (s->log_view == NULL)
3889 break;
3890 ls = &s->log_view->state.log;
3891 old_selected_entry = ls->selected_entry;
3893 err = input_log_view(NULL, s->log_view, KEY_UP);
3894 if (err)
3895 break;
3897 if (old_selected_entry == ls->selected_entry)
3898 break;
3900 err = set_selected_commit(s, ls->selected_entry);
3901 if (err)
3902 break;
3904 s->first_displayed_line = 1;
3905 s->last_displayed_line = view->nlines;
3906 s->matched_line = 0;
3908 diff_view_indicate_progress(view);
3909 err = create_diff(s);
3910 break;
3911 case '>':
3912 case '.':
3913 if (s->log_view == NULL)
3914 break;
3915 ls = &s->log_view->state.log;
3916 old_selected_entry = ls->selected_entry;
3918 err = input_log_view(NULL, s->log_view, KEY_DOWN);
3919 if (err)
3920 break;
3922 if (old_selected_entry == ls->selected_entry)
3923 break;
3925 err = set_selected_commit(s, ls->selected_entry);
3926 if (err)
3927 break;
3929 s->first_displayed_line = 1;
3930 s->last_displayed_line = view->nlines;
3931 s->matched_line = 0;
3933 diff_view_indicate_progress(view);
3934 err = create_diff(s);
3935 break;
3936 default:
3937 break;
3940 return err;
3943 static const struct got_error *
3944 cmd_diff(int argc, char *argv[])
3946 const struct got_error *error = NULL;
3947 struct got_repository *repo = NULL;
3948 struct got_worktree *worktree = NULL;
3949 struct got_object_id *id1 = NULL, *id2 = NULL;
3950 char *repo_path = NULL, *cwd = NULL;
3951 char *id_str1 = NULL, *id_str2 = NULL;
3952 char *label1 = NULL, *label2 = NULL;
3953 int diff_context = 3, ignore_whitespace = 0;
3954 int ch, force_text_diff = 0;
3955 const char *errstr;
3956 struct tog_view *view;
3957 int *pack_fds = NULL;
3959 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3960 switch (ch) {
3961 case 'a':
3962 force_text_diff = 1;
3963 break;
3964 case 'C':
3965 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3966 &errstr);
3967 if (errstr != NULL)
3968 errx(1, "number of context lines is %s: %s",
3969 errstr, errstr);
3970 break;
3971 case 'r':
3972 repo_path = realpath(optarg, NULL);
3973 if (repo_path == NULL)
3974 return got_error_from_errno2("realpath",
3975 optarg);
3976 got_path_strip_trailing_slashes(repo_path);
3977 break;
3978 case 'w':
3979 ignore_whitespace = 1;
3980 break;
3981 default:
3982 usage_diff();
3983 /* NOTREACHED */
3987 argc -= optind;
3988 argv += optind;
3990 if (argc == 0) {
3991 usage_diff(); /* TODO show local worktree changes */
3992 } else if (argc == 2) {
3993 id_str1 = argv[0];
3994 id_str2 = argv[1];
3995 } else
3996 usage_diff();
3998 error = got_repo_pack_fds_open(&pack_fds);
3999 if (error)
4000 goto done;
4002 if (repo_path == NULL) {
4003 cwd = getcwd(NULL, 0);
4004 if (cwd == NULL)
4005 return got_error_from_errno("getcwd");
4006 error = got_worktree_open(&worktree, cwd);
4007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4008 goto done;
4009 if (worktree)
4010 repo_path =
4011 strdup(got_worktree_get_repo_path(worktree));
4012 else
4013 repo_path = strdup(cwd);
4014 if (repo_path == NULL) {
4015 error = got_error_from_errno("strdup");
4016 goto done;
4020 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4021 if (error)
4022 goto done;
4024 init_curses();
4026 error = apply_unveil(got_repo_get_path(repo), NULL);
4027 if (error)
4028 goto done;
4030 error = tog_load_refs(repo, 0);
4031 if (error)
4032 goto done;
4034 error = got_repo_match_object_id(&id1, &label1, id_str1,
4035 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4036 if (error)
4037 goto done;
4039 error = got_repo_match_object_id(&id2, &label2, id_str2,
4040 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4041 if (error)
4042 goto done;
4044 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4045 if (view == NULL) {
4046 error = got_error_from_errno("view_open");
4047 goto done;
4049 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4050 ignore_whitespace, force_text_diff, NULL, repo);
4051 if (error)
4052 goto done;
4053 error = view_loop(view);
4054 done:
4055 free(label1);
4056 free(label2);
4057 free(repo_path);
4058 free(cwd);
4059 if (repo) {
4060 const struct got_error *close_err = got_repo_close(repo);
4061 if (error == NULL)
4062 error = close_err;
4064 if (worktree)
4065 got_worktree_close(worktree);
4066 if (pack_fds) {
4067 const struct got_error *pack_err =
4068 got_repo_pack_fds_close(pack_fds);
4069 if (error == NULL)
4070 error = pack_err;
4072 tog_free_refs();
4073 return error;
4076 __dead static void
4077 usage_blame(void)
4079 endwin();
4080 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4081 getprogname());
4082 exit(1);
4085 struct tog_blame_line {
4086 int annotated;
4087 struct got_object_id *id;
4090 static const struct got_error *
4091 draw_blame(struct tog_view *view)
4093 struct tog_blame_view_state *s = &view->state.blame;
4094 struct tog_blame *blame = &s->blame;
4095 regmatch_t *regmatch = &view->regmatch;
4096 const struct got_error *err;
4097 int lineno = 0, nprinted = 0;
4098 char *line = NULL;
4099 size_t linesize = 0;
4100 ssize_t linelen;
4101 wchar_t *wline;
4102 int width;
4103 struct tog_blame_line *blame_line;
4104 struct got_object_id *prev_id = NULL;
4105 char *id_str;
4106 struct tog_color *tc;
4108 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4109 if (err)
4110 return err;
4112 rewind(blame->f);
4113 werase(view->window);
4115 if (asprintf(&line, "commit %s", id_str) == -1) {
4116 err = got_error_from_errno("asprintf");
4117 free(id_str);
4118 return err;
4121 err = format_line(&wline, &width, line, view->ncols, 0);
4122 free(line);
4123 line = NULL;
4124 if (err)
4125 return err;
4126 if (view_needs_focus_indication(view))
4127 wstandout(view->window);
4128 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4129 if (tc)
4130 wattr_on(view->window,
4131 COLOR_PAIR(tc->colorpair), NULL);
4132 waddwstr(view->window, wline);
4133 if (tc)
4134 wattr_off(view->window,
4135 COLOR_PAIR(tc->colorpair), NULL);
4136 if (view_needs_focus_indication(view))
4137 wstandend(view->window);
4138 free(wline);
4139 wline = NULL;
4140 if (width < view->ncols - 1)
4141 waddch(view->window, '\n');
4143 if (asprintf(&line, "[%d/%d] %s%s",
4144 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4145 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4146 free(id_str);
4147 return got_error_from_errno("asprintf");
4149 free(id_str);
4150 err = format_line(&wline, &width, line, view->ncols, 0);
4151 free(line);
4152 line = NULL;
4153 if (err)
4154 return err;
4155 waddwstr(view->window, wline);
4156 free(wline);
4157 wline = NULL;
4158 if (width < view->ncols - 1)
4159 waddch(view->window, '\n');
4161 s->eof = 0;
4162 while (nprinted < view->nlines - 2) {
4163 linelen = getline(&line, &linesize, blame->f);
4164 if (linelen == -1) {
4165 if (feof(blame->f)) {
4166 s->eof = 1;
4167 break;
4169 free(line);
4170 return got_ferror(blame->f, GOT_ERR_IO);
4172 if (++lineno < s->first_displayed_line)
4173 continue;
4175 if (view->focussed && nprinted == s->selected_line - 1)
4176 wstandout(view->window);
4178 if (blame->nlines > 0) {
4179 blame_line = &blame->lines[lineno - 1];
4180 if (blame_line->annotated && prev_id &&
4181 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4182 !(view->focussed &&
4183 nprinted == s->selected_line - 1)) {
4184 waddstr(view->window, " ");
4185 } else if (blame_line->annotated) {
4186 char *id_str;
4187 err = got_object_id_str(&id_str, blame_line->id);
4188 if (err) {
4189 free(line);
4190 return err;
4192 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4193 if (tc)
4194 wattr_on(view->window,
4195 COLOR_PAIR(tc->colorpair), NULL);
4196 wprintw(view->window, "%.8s", id_str);
4197 if (tc)
4198 wattr_off(view->window,
4199 COLOR_PAIR(tc->colorpair), NULL);
4200 free(id_str);
4201 prev_id = blame_line->id;
4202 } else {
4203 waddstr(view->window, "........");
4204 prev_id = NULL;
4206 } else {
4207 waddstr(view->window, "........");
4208 prev_id = NULL;
4211 if (view->focussed && nprinted == s->selected_line - 1)
4212 wstandend(view->window);
4213 waddstr(view->window, " ");
4215 if (view->ncols <= 9) {
4216 width = 9;
4217 wline = wcsdup(L"");
4218 if (wline == NULL) {
4219 err = got_error_from_errno("wcsdup");
4220 free(line);
4221 return err;
4223 } else if (s->first_displayed_line + nprinted ==
4224 s->matched_line &&
4225 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4226 err = add_matched_line(&width, line, view->ncols - 9, 9,
4227 view->window, regmatch);
4228 if (err) {
4229 free(line);
4230 return err;
4232 width += 9;
4233 } else {
4234 err = format_line(&wline, &width, line,
4235 view->ncols - 9, 9);
4236 waddwstr(view->window, wline);
4237 free(wline);
4238 wline = NULL;
4239 width += 9;
4242 if (width <= view->ncols - 1)
4243 waddch(view->window, '\n');
4244 if (++nprinted == 1)
4245 s->first_displayed_line = lineno;
4247 free(line);
4248 s->last_displayed_line = lineno;
4250 view_vborder(view);
4252 return NULL;
4255 static const struct got_error *
4256 blame_cb(void *arg, int nlines, int lineno,
4257 struct got_commit_object *commit, struct got_object_id *id)
4259 const struct got_error *err = NULL;
4260 struct tog_blame_cb_args *a = arg;
4261 struct tog_blame_line *line;
4262 int errcode;
4264 if (nlines != a->nlines ||
4265 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4266 return got_error(GOT_ERR_RANGE);
4268 errcode = pthread_mutex_lock(&tog_mutex);
4269 if (errcode)
4270 return got_error_set_errno(errcode, "pthread_mutex_lock");
4272 if (*a->quit) { /* user has quit the blame view */
4273 err = got_error(GOT_ERR_ITER_COMPLETED);
4274 goto done;
4277 if (lineno == -1)
4278 goto done; /* no change in this commit */
4280 line = &a->lines[lineno - 1];
4281 if (line->annotated)
4282 goto done;
4284 line->id = got_object_id_dup(id);
4285 if (line->id == NULL) {
4286 err = got_error_from_errno("got_object_id_dup");
4287 goto done;
4289 line->annotated = 1;
4290 done:
4291 errcode = pthread_mutex_unlock(&tog_mutex);
4292 if (errcode)
4293 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4294 return err;
4297 static void *
4298 blame_thread(void *arg)
4300 const struct got_error *err, *close_err;
4301 struct tog_blame_thread_args *ta = arg;
4302 struct tog_blame_cb_args *a = ta->cb_args;
4303 int errcode;
4305 err = block_signals_used_by_main_thread();
4306 if (err)
4307 return (void *)err;
4309 err = got_blame(ta->path, a->commit_id, ta->repo,
4310 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4311 if (err && err->code == GOT_ERR_CANCELLED)
4312 err = NULL;
4314 errcode = pthread_mutex_lock(&tog_mutex);
4315 if (errcode)
4316 return (void *)got_error_set_errno(errcode,
4317 "pthread_mutex_lock");
4319 close_err = got_repo_close(ta->repo);
4320 if (err == NULL)
4321 err = close_err;
4322 ta->repo = NULL;
4323 *ta->complete = 1;
4325 errcode = pthread_mutex_unlock(&tog_mutex);
4326 if (errcode && err == NULL)
4327 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4329 return (void *)err;
4332 static struct got_object_id *
4333 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4334 int first_displayed_line, int selected_line)
4336 struct tog_blame_line *line;
4338 if (nlines <= 0)
4339 return NULL;
4341 line = &lines[first_displayed_line - 1 + selected_line - 1];
4342 if (!line->annotated)
4343 return NULL;
4345 return line->id;
4348 static const struct got_error *
4349 stop_blame(struct tog_blame *blame)
4351 const struct got_error *err = NULL;
4352 int i;
4354 if (blame->thread) {
4355 int errcode;
4356 errcode = pthread_mutex_unlock(&tog_mutex);
4357 if (errcode)
4358 return got_error_set_errno(errcode,
4359 "pthread_mutex_unlock");
4360 errcode = pthread_join(blame->thread, (void **)&err);
4361 if (errcode)
4362 return got_error_set_errno(errcode, "pthread_join");
4363 errcode = pthread_mutex_lock(&tog_mutex);
4364 if (errcode)
4365 return got_error_set_errno(errcode,
4366 "pthread_mutex_lock");
4367 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4368 err = NULL;
4369 blame->thread = NULL;
4371 if (blame->thread_args.repo) {
4372 const struct got_error *close_err;
4373 close_err = got_repo_close(blame->thread_args.repo);
4374 if (err == NULL)
4375 err = close_err;
4376 blame->thread_args.repo = NULL;
4378 if (blame->f) {
4379 if (fclose(blame->f) == EOF && err == NULL)
4380 err = got_error_from_errno("fclose");
4381 blame->f = NULL;
4383 if (blame->lines) {
4384 for (i = 0; i < blame->nlines; i++)
4385 free(blame->lines[i].id);
4386 free(blame->lines);
4387 blame->lines = NULL;
4389 free(blame->cb_args.commit_id);
4390 blame->cb_args.commit_id = NULL;
4391 if (blame->pack_fds) {
4392 const struct got_error *pack_err =
4393 got_repo_pack_fds_close(blame->pack_fds);
4394 if (err == NULL)
4395 err = pack_err;
4396 blame->pack_fds = NULL;
4398 return err;
4401 static const struct got_error *
4402 cancel_blame_view(void *arg)
4404 const struct got_error *err = NULL;
4405 int *done = arg;
4406 int errcode;
4408 errcode = pthread_mutex_lock(&tog_mutex);
4409 if (errcode)
4410 return got_error_set_errno(errcode,
4411 "pthread_mutex_unlock");
4413 if (*done)
4414 err = got_error(GOT_ERR_CANCELLED);
4416 errcode = pthread_mutex_unlock(&tog_mutex);
4417 if (errcode)
4418 return got_error_set_errno(errcode,
4419 "pthread_mutex_lock");
4421 return err;
4424 static const struct got_error *
4425 run_blame(struct tog_view *view)
4427 struct tog_blame_view_state *s = &view->state.blame;
4428 struct tog_blame *blame = &s->blame;
4429 const struct got_error *err = NULL;
4430 struct got_commit_object *commit = NULL;
4431 struct got_blob_object *blob = NULL;
4432 struct got_repository *thread_repo = NULL;
4433 struct got_object_id *obj_id = NULL;
4434 int obj_type;
4435 int *pack_fds = NULL;
4437 err = got_object_open_as_commit(&commit, s->repo,
4438 &s->blamed_commit->id);
4439 if (err)
4440 return err;
4442 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4443 if (err)
4444 goto done;
4446 err = got_object_get_type(&obj_type, s->repo, obj_id);
4447 if (err)
4448 goto done;
4450 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4451 err = got_error(GOT_ERR_OBJ_TYPE);
4452 goto done;
4455 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4456 if (err)
4457 goto done;
4458 blame->f = got_opentemp();
4459 if (blame->f == NULL) {
4460 err = got_error_from_errno("got_opentemp");
4461 goto done;
4463 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4464 &blame->line_offsets, blame->f, blob);
4465 if (err)
4466 goto done;
4467 if (blame->nlines == 0) {
4468 s->blame_complete = 1;
4469 goto done;
4472 /* Don't include \n at EOF in the blame line count. */
4473 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4474 blame->nlines--;
4476 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4477 if (blame->lines == NULL) {
4478 err = got_error_from_errno("calloc");
4479 goto done;
4482 err = got_repo_pack_fds_open(&pack_fds);
4483 if (err)
4484 goto done;
4485 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4486 pack_fds);
4487 if (err)
4488 goto done;
4490 blame->pack_fds = pack_fds;
4491 blame->cb_args.view = view;
4492 blame->cb_args.lines = blame->lines;
4493 blame->cb_args.nlines = blame->nlines;
4494 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4495 if (blame->cb_args.commit_id == NULL) {
4496 err = got_error_from_errno("got_object_id_dup");
4497 goto done;
4499 blame->cb_args.quit = &s->done;
4501 blame->thread_args.path = s->path;
4502 blame->thread_args.repo = thread_repo;
4503 blame->thread_args.cb_args = &blame->cb_args;
4504 blame->thread_args.complete = &s->blame_complete;
4505 blame->thread_args.cancel_cb = cancel_blame_view;
4506 blame->thread_args.cancel_arg = &s->done;
4507 s->blame_complete = 0;
4509 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4510 s->first_displayed_line = 1;
4511 s->last_displayed_line = view->nlines;
4512 s->selected_line = 1;
4514 s->matched_line = 0;
4516 done:
4517 if (commit)
4518 got_object_commit_close(commit);
4519 if (blob)
4520 got_object_blob_close(blob);
4521 free(obj_id);
4522 if (err)
4523 stop_blame(blame);
4524 return err;
4527 static const struct got_error *
4528 open_blame_view(struct tog_view *view, char *path,
4529 struct got_object_id *commit_id, struct got_repository *repo)
4531 const struct got_error *err = NULL;
4532 struct tog_blame_view_state *s = &view->state.blame;
4534 STAILQ_INIT(&s->blamed_commits);
4536 s->path = strdup(path);
4537 if (s->path == NULL)
4538 return got_error_from_errno("strdup");
4540 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4541 if (err) {
4542 free(s->path);
4543 return err;
4546 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4547 s->first_displayed_line = 1;
4548 s->last_displayed_line = view->nlines;
4549 s->selected_line = 1;
4550 s->blame_complete = 0;
4551 s->repo = repo;
4552 s->commit_id = commit_id;
4553 memset(&s->blame, 0, sizeof(s->blame));
4555 STAILQ_INIT(&s->colors);
4556 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4557 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4558 get_color_value("TOG_COLOR_COMMIT"));
4559 if (err)
4560 return err;
4563 view->show = show_blame_view;
4564 view->input = input_blame_view;
4565 view->close = close_blame_view;
4566 view->search_start = search_start_blame_view;
4567 view->search_next = search_next_blame_view;
4569 return run_blame(view);
4572 static const struct got_error *
4573 close_blame_view(struct tog_view *view)
4575 const struct got_error *err = NULL;
4576 struct tog_blame_view_state *s = &view->state.blame;
4578 if (s->blame.thread)
4579 err = stop_blame(&s->blame);
4581 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4582 struct got_object_qid *blamed_commit;
4583 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4584 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4585 got_object_qid_free(blamed_commit);
4588 free(s->path);
4589 free_colors(&s->colors);
4590 return err;
4593 static const struct got_error *
4594 search_start_blame_view(struct tog_view *view)
4596 struct tog_blame_view_state *s = &view->state.blame;
4598 s->matched_line = 0;
4599 return NULL;
4602 static const struct got_error *
4603 search_next_blame_view(struct tog_view *view)
4605 struct tog_blame_view_state *s = &view->state.blame;
4606 int lineno;
4607 char *line = NULL;
4608 size_t linesize = 0;
4609 ssize_t linelen;
4611 if (!view->searching) {
4612 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4613 return NULL;
4616 if (s->matched_line) {
4617 if (view->searching == TOG_SEARCH_FORWARD)
4618 lineno = s->matched_line + 1;
4619 else
4620 lineno = s->matched_line - 1;
4621 } else
4622 lineno = s->first_displayed_line - 1 + s->selected_line;
4624 while (1) {
4625 off_t offset;
4627 if (lineno <= 0 || lineno > s->blame.nlines) {
4628 if (s->matched_line == 0) {
4629 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4630 break;
4633 if (view->searching == TOG_SEARCH_FORWARD)
4634 lineno = 1;
4635 else
4636 lineno = s->blame.nlines;
4639 offset = s->blame.line_offsets[lineno - 1];
4640 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4641 free(line);
4642 return got_error_from_errno("fseeko");
4644 linelen = getline(&line, &linesize, s->blame.f);
4645 if (linelen != -1 &&
4646 match_line(line, &view->regex, 1, &view->regmatch)) {
4647 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4648 s->matched_line = lineno;
4649 break;
4651 if (view->searching == TOG_SEARCH_FORWARD)
4652 lineno++;
4653 else
4654 lineno--;
4656 free(line);
4658 if (s->matched_line) {
4659 s->first_displayed_line = s->matched_line;
4660 s->selected_line = 1;
4663 return NULL;
4666 static const struct got_error *
4667 show_blame_view(struct tog_view *view)
4669 const struct got_error *err = NULL;
4670 struct tog_blame_view_state *s = &view->state.blame;
4671 int errcode;
4673 if (s->blame.thread == NULL && !s->blame_complete) {
4674 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4675 &s->blame.thread_args);
4676 if (errcode)
4677 return got_error_set_errno(errcode, "pthread_create");
4679 halfdelay(1); /* fast refresh while annotating */
4682 if (s->blame_complete)
4683 halfdelay(10); /* disable fast refresh */
4685 err = draw_blame(view);
4687 view_vborder(view);
4688 return err;
4691 static const struct got_error *
4692 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4694 const struct got_error *err = NULL, *thread_err = NULL;
4695 struct tog_view *diff_view;
4696 struct tog_blame_view_state *s = &view->state.blame;
4697 int begin_x = 0, nscroll = view->nlines - 2;
4699 switch (ch) {
4700 case 'q':
4701 s->done = 1;
4702 break;
4703 case 'g':
4704 case KEY_HOME:
4705 s->selected_line = 1;
4706 s->first_displayed_line = 1;
4707 break;
4708 case 'G':
4709 case KEY_END:
4710 if (s->blame.nlines < view->nlines - 2) {
4711 s->selected_line = s->blame.nlines;
4712 s->first_displayed_line = 1;
4713 } else {
4714 s->selected_line = view->nlines - 2;
4715 s->first_displayed_line = s->blame.nlines -
4716 (view->nlines - 3);
4718 break;
4719 case 'k':
4720 case KEY_UP:
4721 case CTRL('p'):
4722 if (s->selected_line > 1)
4723 s->selected_line--;
4724 else if (s->selected_line == 1 &&
4725 s->first_displayed_line > 1)
4726 s->first_displayed_line--;
4727 break;
4728 case CTRL('u'):
4729 case 'u':
4730 nscroll /= 2;
4731 /* FALL THROUGH */
4732 case KEY_PPAGE:
4733 case CTRL('b'):
4734 if (s->first_displayed_line == 1) {
4735 s->selected_line = MAX(1, s->selected_line - nscroll);
4736 break;
4738 if (s->first_displayed_line > nscroll)
4739 s->first_displayed_line -= nscroll;
4740 else
4741 s->first_displayed_line = 1;
4742 break;
4743 case 'j':
4744 case KEY_DOWN:
4745 case CTRL('n'):
4746 if (s->selected_line < view->nlines - 2 &&
4747 s->first_displayed_line +
4748 s->selected_line <= s->blame.nlines)
4749 s->selected_line++;
4750 else if (s->last_displayed_line <
4751 s->blame.nlines)
4752 s->first_displayed_line++;
4753 break;
4754 case 'b':
4755 case 'p': {
4756 struct got_object_id *id = NULL;
4757 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4758 s->first_displayed_line, s->selected_line);
4759 if (id == NULL)
4760 break;
4761 if (ch == 'p') {
4762 struct got_commit_object *commit, *pcommit;
4763 struct got_object_qid *pid;
4764 struct got_object_id *blob_id = NULL;
4765 int obj_type;
4766 err = got_object_open_as_commit(&commit,
4767 s->repo, id);
4768 if (err)
4769 break;
4770 pid = STAILQ_FIRST(
4771 got_object_commit_get_parent_ids(commit));
4772 if (pid == NULL) {
4773 got_object_commit_close(commit);
4774 break;
4776 /* Check if path history ends here. */
4777 err = got_object_open_as_commit(&pcommit,
4778 s->repo, &pid->id);
4779 if (err)
4780 break;
4781 err = got_object_id_by_path(&blob_id, s->repo,
4782 pcommit, s->path);
4783 got_object_commit_close(pcommit);
4784 if (err) {
4785 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4786 err = NULL;
4787 got_object_commit_close(commit);
4788 break;
4790 err = got_object_get_type(&obj_type, s->repo,
4791 blob_id);
4792 free(blob_id);
4793 /* Can't blame non-blob type objects. */
4794 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4795 got_object_commit_close(commit);
4796 break;
4798 err = got_object_qid_alloc(&s->blamed_commit,
4799 &pid->id);
4800 got_object_commit_close(commit);
4801 } else {
4802 if (got_object_id_cmp(id,
4803 &s->blamed_commit->id) == 0)
4804 break;
4805 err = got_object_qid_alloc(&s->blamed_commit,
4806 id);
4808 if (err)
4809 break;
4810 s->done = 1;
4811 thread_err = stop_blame(&s->blame);
4812 s->done = 0;
4813 if (thread_err)
4814 break;
4815 STAILQ_INSERT_HEAD(&s->blamed_commits,
4816 s->blamed_commit, entry);
4817 err = run_blame(view);
4818 if (err)
4819 break;
4820 break;
4822 case 'B': {
4823 struct got_object_qid *first;
4824 first = STAILQ_FIRST(&s->blamed_commits);
4825 if (!got_object_id_cmp(&first->id, s->commit_id))
4826 break;
4827 s->done = 1;
4828 thread_err = stop_blame(&s->blame);
4829 s->done = 0;
4830 if (thread_err)
4831 break;
4832 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4833 got_object_qid_free(s->blamed_commit);
4834 s->blamed_commit =
4835 STAILQ_FIRST(&s->blamed_commits);
4836 err = run_blame(view);
4837 if (err)
4838 break;
4839 break;
4841 case KEY_ENTER:
4842 case '\r': {
4843 struct got_object_id *id = NULL;
4844 struct got_object_qid *pid;
4845 struct got_commit_object *commit = NULL;
4846 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4847 s->first_displayed_line, s->selected_line);
4848 if (id == NULL)
4849 break;
4850 err = got_object_open_as_commit(&commit, s->repo, id);
4851 if (err)
4852 break;
4853 pid = STAILQ_FIRST(
4854 got_object_commit_get_parent_ids(commit));
4855 if (view_is_parent_view(view))
4856 begin_x = view_split_begin_x(view->begin_x);
4857 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4858 if (diff_view == NULL) {
4859 got_object_commit_close(commit);
4860 err = got_error_from_errno("view_open");
4861 break;
4863 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4864 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4865 got_object_commit_close(commit);
4866 if (err) {
4867 view_close(diff_view);
4868 break;
4870 view->focussed = 0;
4871 diff_view->focussed = 1;
4872 if (view_is_parent_view(view)) {
4873 err = view_close_child(view);
4874 if (err)
4875 break;
4876 view_set_child(view, diff_view);
4877 view->focus_child = 1;
4878 } else
4879 *new_view = diff_view;
4880 if (err)
4881 break;
4882 break;
4884 case CTRL('d'):
4885 case 'd':
4886 nscroll /= 2;
4887 /* FALL THROUGH */
4888 case KEY_NPAGE:
4889 case CTRL('f'):
4890 case ' ':
4891 if (s->last_displayed_line >= s->blame.nlines &&
4892 s->selected_line >= MIN(s->blame.nlines,
4893 view->nlines - 2)) {
4894 break;
4896 if (s->last_displayed_line >= s->blame.nlines &&
4897 s->selected_line < view->nlines - 2) {
4898 s->selected_line +=
4899 MIN(nscroll, s->last_displayed_line -
4900 s->first_displayed_line - s->selected_line + 1);
4902 if (s->last_displayed_line + nscroll <= s->blame.nlines)
4903 s->first_displayed_line += nscroll;
4904 else
4905 s->first_displayed_line =
4906 s->blame.nlines - (view->nlines - 3);
4907 break;
4908 case KEY_RESIZE:
4909 if (s->selected_line > view->nlines - 2) {
4910 s->selected_line = MIN(s->blame.nlines,
4911 view->nlines - 2);
4913 break;
4914 default:
4915 break;
4917 return thread_err ? thread_err : err;
4920 static const struct got_error *
4921 cmd_blame(int argc, char *argv[])
4923 const struct got_error *error;
4924 struct got_repository *repo = NULL;
4925 struct got_worktree *worktree = NULL;
4926 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4927 char *link_target = NULL;
4928 struct got_object_id *commit_id = NULL;
4929 struct got_commit_object *commit = NULL;
4930 char *commit_id_str = NULL;
4931 int ch;
4932 struct tog_view *view;
4933 int *pack_fds = NULL;
4935 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4936 switch (ch) {
4937 case 'c':
4938 commit_id_str = optarg;
4939 break;
4940 case 'r':
4941 repo_path = realpath(optarg, NULL);
4942 if (repo_path == NULL)
4943 return got_error_from_errno2("realpath",
4944 optarg);
4945 break;
4946 default:
4947 usage_blame();
4948 /* NOTREACHED */
4952 argc -= optind;
4953 argv += optind;
4955 if (argc != 1)
4956 usage_blame();
4958 error = got_repo_pack_fds_open(&pack_fds);
4959 if (error != NULL)
4960 goto done;
4962 if (repo_path == NULL) {
4963 cwd = getcwd(NULL, 0);
4964 if (cwd == NULL)
4965 return got_error_from_errno("getcwd");
4966 error = got_worktree_open(&worktree, cwd);
4967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4968 goto done;
4969 if (worktree)
4970 repo_path =
4971 strdup(got_worktree_get_repo_path(worktree));
4972 else
4973 repo_path = strdup(cwd);
4974 if (repo_path == NULL) {
4975 error = got_error_from_errno("strdup");
4976 goto done;
4980 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4981 if (error != NULL)
4982 goto done;
4984 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4985 worktree);
4986 if (error)
4987 goto done;
4989 init_curses();
4991 error = apply_unveil(got_repo_get_path(repo), NULL);
4992 if (error)
4993 goto done;
4995 error = tog_load_refs(repo, 0);
4996 if (error)
4997 goto done;
4999 if (commit_id_str == NULL) {
5000 struct got_reference *head_ref;
5001 error = got_ref_open(&head_ref, repo, worktree ?
5002 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5003 if (error != NULL)
5004 goto done;
5005 error = got_ref_resolve(&commit_id, repo, head_ref);
5006 got_ref_close(head_ref);
5007 } else {
5008 error = got_repo_match_object_id(&commit_id, NULL,
5009 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5011 if (error != NULL)
5012 goto done;
5014 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5015 if (view == NULL) {
5016 error = got_error_from_errno("view_open");
5017 goto done;
5020 error = got_object_open_as_commit(&commit, repo, commit_id);
5021 if (error)
5022 goto done;
5024 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5025 commit, repo);
5026 if (error)
5027 goto done;
5029 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5030 commit_id, repo);
5031 if (error)
5032 goto done;
5033 if (worktree) {
5034 /* Release work tree lock. */
5035 got_worktree_close(worktree);
5036 worktree = NULL;
5038 error = view_loop(view);
5039 done:
5040 free(repo_path);
5041 free(in_repo_path);
5042 free(link_target);
5043 free(cwd);
5044 free(commit_id);
5045 if (commit)
5046 got_object_commit_close(commit);
5047 if (worktree)
5048 got_worktree_close(worktree);
5049 if (repo) {
5050 const struct got_error *close_err = got_repo_close(repo);
5051 if (error == NULL)
5052 error = close_err;
5054 if (pack_fds) {
5055 const struct got_error *pack_err =
5056 got_repo_pack_fds_close(pack_fds);
5057 if (error == NULL)
5058 error = pack_err;
5060 tog_free_refs();
5061 return error;
5064 static const struct got_error *
5065 draw_tree_entries(struct tog_view *view, const char *parent_path)
5067 struct tog_tree_view_state *s = &view->state.tree;
5068 const struct got_error *err = NULL;
5069 struct got_tree_entry *te;
5070 wchar_t *wline;
5071 struct tog_color *tc;
5072 int width, n, i, nentries;
5073 int limit = view->nlines;
5075 s->ndisplayed = 0;
5077 werase(view->window);
5079 if (limit == 0)
5080 return NULL;
5082 err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
5083 if (err)
5084 return err;
5085 if (view_needs_focus_indication(view))
5086 wstandout(view->window);
5087 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5088 if (tc)
5089 wattr_on(view->window,
5090 COLOR_PAIR(tc->colorpair), NULL);
5091 waddwstr(view->window, wline);
5092 if (tc)
5093 wattr_off(view->window,
5094 COLOR_PAIR(tc->colorpair), NULL);
5095 if (view_needs_focus_indication(view))
5096 wstandend(view->window);
5097 free(wline);
5098 wline = NULL;
5099 if (width < view->ncols - 1)
5100 waddch(view->window, '\n');
5101 if (--limit <= 0)
5102 return NULL;
5103 err = format_line(&wline, &width, parent_path, view->ncols, 0);
5104 if (err)
5105 return err;
5106 waddwstr(view->window, wline);
5107 free(wline);
5108 wline = NULL;
5109 if (width < view->ncols - 1)
5110 waddch(view->window, '\n');
5111 if (--limit <= 0)
5112 return NULL;
5113 waddch(view->window, '\n');
5114 if (--limit <= 0)
5115 return NULL;
5117 if (s->first_displayed_entry == NULL) {
5118 te = got_object_tree_get_first_entry(s->tree);
5119 if (s->selected == 0) {
5120 if (view->focussed)
5121 wstandout(view->window);
5122 s->selected_entry = NULL;
5124 waddstr(view->window, " ..\n"); /* parent directory */
5125 if (s->selected == 0 && view->focussed)
5126 wstandend(view->window);
5127 s->ndisplayed++;
5128 if (--limit <= 0)
5129 return NULL;
5130 n = 1;
5131 } else {
5132 n = 0;
5133 te = s->first_displayed_entry;
5136 nentries = got_object_tree_get_nentries(s->tree);
5137 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5138 char *line = NULL, *id_str = NULL, *link_target = NULL;
5139 const char *modestr = "";
5140 mode_t mode;
5142 te = got_object_tree_get_entry(s->tree, i);
5143 mode = got_tree_entry_get_mode(te);
5145 if (s->show_ids) {
5146 err = got_object_id_str(&id_str,
5147 got_tree_entry_get_id(te));
5148 if (err)
5149 return got_error_from_errno(
5150 "got_object_id_str");
5152 if (got_object_tree_entry_is_submodule(te))
5153 modestr = "$";
5154 else if (S_ISLNK(mode)) {
5155 int i;
5157 err = got_tree_entry_get_symlink_target(&link_target,
5158 te, s->repo);
5159 if (err) {
5160 free(id_str);
5161 return err;
5163 for (i = 0; i < strlen(link_target); i++) {
5164 if (!isprint((unsigned char)link_target[i]))
5165 link_target[i] = '?';
5167 modestr = "@";
5169 else if (S_ISDIR(mode))
5170 modestr = "/";
5171 else if (mode & S_IXUSR)
5172 modestr = "*";
5173 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5174 got_tree_entry_get_name(te), modestr,
5175 link_target ? " -> ": "",
5176 link_target ? link_target : "") == -1) {
5177 free(id_str);
5178 free(link_target);
5179 return got_error_from_errno("asprintf");
5181 free(id_str);
5182 free(link_target);
5183 err = format_line(&wline, &width, line, view->ncols, 0);
5184 if (err) {
5185 free(line);
5186 break;
5188 if (n == s->selected) {
5189 if (view->focussed)
5190 wstandout(view->window);
5191 s->selected_entry = te;
5193 tc = match_color(&s->colors, line);
5194 if (tc)
5195 wattr_on(view->window,
5196 COLOR_PAIR(tc->colorpair), NULL);
5197 waddwstr(view->window, wline);
5198 if (tc)
5199 wattr_off(view->window,
5200 COLOR_PAIR(tc->colorpair), NULL);
5201 if (width < view->ncols - 1)
5202 waddch(view->window, '\n');
5203 if (n == s->selected && view->focussed)
5204 wstandend(view->window);
5205 free(line);
5206 free(wline);
5207 wline = NULL;
5208 n++;
5209 s->ndisplayed++;
5210 s->last_displayed_entry = te;
5211 if (--limit <= 0)
5212 break;
5215 return err;
5218 static void
5219 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5221 struct got_tree_entry *te;
5222 int isroot = s->tree == s->root;
5223 int i = 0;
5225 if (s->first_displayed_entry == NULL)
5226 return;
5228 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5229 while (i++ < maxscroll) {
5230 if (te == NULL) {
5231 if (!isroot)
5232 s->first_displayed_entry = NULL;
5233 break;
5235 s->first_displayed_entry = te;
5236 te = got_tree_entry_get_prev(s->tree, te);
5240 static void
5241 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5243 struct got_tree_entry *next, *last;
5244 int n = 0;
5246 if (s->first_displayed_entry)
5247 next = got_tree_entry_get_next(s->tree,
5248 s->first_displayed_entry);
5249 else
5250 next = got_object_tree_get_first_entry(s->tree);
5252 last = s->last_displayed_entry;
5253 while (next && last && n++ < maxscroll) {
5254 last = got_tree_entry_get_next(s->tree, last);
5255 if (last) {
5256 s->first_displayed_entry = next;
5257 next = got_tree_entry_get_next(s->tree, next);
5262 static const struct got_error *
5263 tree_entry_path(char **path, struct tog_parent_trees *parents,
5264 struct got_tree_entry *te)
5266 const struct got_error *err = NULL;
5267 struct tog_parent_tree *pt;
5268 size_t len = 2; /* for leading slash and NUL */
5270 TAILQ_FOREACH(pt, parents, entry)
5271 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5272 + 1 /* slash */;
5273 if (te)
5274 len += strlen(got_tree_entry_get_name(te));
5276 *path = calloc(1, len);
5277 if (path == NULL)
5278 return got_error_from_errno("calloc");
5280 (*path)[0] = '/';
5281 pt = TAILQ_LAST(parents, tog_parent_trees);
5282 while (pt) {
5283 const char *name = got_tree_entry_get_name(pt->selected_entry);
5284 if (strlcat(*path, name, len) >= len) {
5285 err = got_error(GOT_ERR_NO_SPACE);
5286 goto done;
5288 if (strlcat(*path, "/", len) >= len) {
5289 err = got_error(GOT_ERR_NO_SPACE);
5290 goto done;
5292 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5294 if (te) {
5295 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5296 err = got_error(GOT_ERR_NO_SPACE);
5297 goto done;
5300 done:
5301 if (err) {
5302 free(*path);
5303 *path = NULL;
5305 return err;
5308 static const struct got_error *
5309 blame_tree_entry(struct tog_view **new_view, int begin_x,
5310 struct got_tree_entry *te, struct tog_parent_trees *parents,
5311 struct got_object_id *commit_id, struct got_repository *repo)
5313 const struct got_error *err = NULL;
5314 char *path;
5315 struct tog_view *blame_view;
5317 *new_view = NULL;
5319 err = tree_entry_path(&path, parents, te);
5320 if (err)
5321 return err;
5323 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5324 if (blame_view == NULL) {
5325 err = got_error_from_errno("view_open");
5326 goto done;
5329 err = open_blame_view(blame_view, path, commit_id, repo);
5330 if (err) {
5331 if (err->code == GOT_ERR_CANCELLED)
5332 err = NULL;
5333 view_close(blame_view);
5334 } else
5335 *new_view = blame_view;
5336 done:
5337 free(path);
5338 return err;
5341 static const struct got_error *
5342 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5343 struct tog_tree_view_state *s)
5345 struct tog_view *log_view;
5346 const struct got_error *err = NULL;
5347 char *path;
5349 *new_view = NULL;
5351 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5352 if (log_view == NULL)
5353 return got_error_from_errno("view_open");
5355 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5356 if (err)
5357 return err;
5359 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5360 path, 0);
5361 if (err)
5362 view_close(log_view);
5363 else
5364 *new_view = log_view;
5365 free(path);
5366 return err;
5369 static const struct got_error *
5370 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5371 const char *head_ref_name, struct got_repository *repo)
5373 const struct got_error *err = NULL;
5374 char *commit_id_str = NULL;
5375 struct tog_tree_view_state *s = &view->state.tree;
5376 struct got_commit_object *commit = NULL;
5378 TAILQ_INIT(&s->parents);
5379 STAILQ_INIT(&s->colors);
5381 s->commit_id = got_object_id_dup(commit_id);
5382 if (s->commit_id == NULL)
5383 return got_error_from_errno("got_object_id_dup");
5385 err = got_object_open_as_commit(&commit, repo, commit_id);
5386 if (err)
5387 goto done;
5390 * The root is opened here and will be closed when the view is closed.
5391 * Any visited subtrees and their path-wise parents are opened and
5392 * closed on demand.
5394 err = got_object_open_as_tree(&s->root, repo,
5395 got_object_commit_get_tree_id(commit));
5396 if (err)
5397 goto done;
5398 s->tree = s->root;
5400 err = got_object_id_str(&commit_id_str, commit_id);
5401 if (err != NULL)
5402 goto done;
5404 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5405 err = got_error_from_errno("asprintf");
5406 goto done;
5409 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5410 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5411 if (head_ref_name) {
5412 s->head_ref_name = strdup(head_ref_name);
5413 if (s->head_ref_name == NULL) {
5414 err = got_error_from_errno("strdup");
5415 goto done;
5418 s->repo = repo;
5420 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5421 err = add_color(&s->colors, "\\$$",
5422 TOG_COLOR_TREE_SUBMODULE,
5423 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5424 if (err)
5425 goto done;
5426 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5427 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5428 if (err)
5429 goto done;
5430 err = add_color(&s->colors, "/$",
5431 TOG_COLOR_TREE_DIRECTORY,
5432 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5433 if (err)
5434 goto done;
5436 err = add_color(&s->colors, "\\*$",
5437 TOG_COLOR_TREE_EXECUTABLE,
5438 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5439 if (err)
5440 goto done;
5442 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5443 get_color_value("TOG_COLOR_COMMIT"));
5444 if (err)
5445 goto done;
5448 view->show = show_tree_view;
5449 view->input = input_tree_view;
5450 view->close = close_tree_view;
5451 view->search_start = search_start_tree_view;
5452 view->search_next = search_next_tree_view;
5453 done:
5454 free(commit_id_str);
5455 if (commit)
5456 got_object_commit_close(commit);
5457 if (err)
5458 close_tree_view(view);
5459 return err;
5462 static const struct got_error *
5463 close_tree_view(struct tog_view *view)
5465 struct tog_tree_view_state *s = &view->state.tree;
5467 free_colors(&s->colors);
5468 free(s->tree_label);
5469 s->tree_label = NULL;
5470 free(s->commit_id);
5471 s->commit_id = NULL;
5472 free(s->head_ref_name);
5473 s->head_ref_name = NULL;
5474 while (!TAILQ_EMPTY(&s->parents)) {
5475 struct tog_parent_tree *parent;
5476 parent = TAILQ_FIRST(&s->parents);
5477 TAILQ_REMOVE(&s->parents, parent, entry);
5478 if (parent->tree != s->root)
5479 got_object_tree_close(parent->tree);
5480 free(parent);
5483 if (s->tree != NULL && s->tree != s->root)
5484 got_object_tree_close(s->tree);
5485 if (s->root)
5486 got_object_tree_close(s->root);
5487 return NULL;
5490 static const struct got_error *
5491 search_start_tree_view(struct tog_view *view)
5493 struct tog_tree_view_state *s = &view->state.tree;
5495 s->matched_entry = NULL;
5496 return NULL;
5499 static int
5500 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5502 regmatch_t regmatch;
5504 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
5505 0) == 0;
5508 static const struct got_error *
5509 search_next_tree_view(struct tog_view *view)
5511 struct tog_tree_view_state *s = &view->state.tree;
5512 struct got_tree_entry *te = NULL;
5514 if (!view->searching) {
5515 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5516 return NULL;
5519 if (s->matched_entry) {
5520 if (view->searching == TOG_SEARCH_FORWARD) {
5521 if (s->selected_entry)
5522 te = got_tree_entry_get_next(s->tree,
5523 s->selected_entry);
5524 else
5525 te = got_object_tree_get_first_entry(s->tree);
5526 } else {
5527 if (s->selected_entry == NULL)
5528 te = got_object_tree_get_last_entry(s->tree);
5529 else
5530 te = got_tree_entry_get_prev(s->tree,
5531 s->selected_entry);
5533 } else {
5534 if (s->selected_entry)
5535 te = s->selected_entry;
5536 else if (view->searching == TOG_SEARCH_FORWARD)
5537 te = got_object_tree_get_first_entry(s->tree);
5538 else
5539 te = got_object_tree_get_last_entry(s->tree);
5542 while (1) {
5543 if (te == NULL) {
5544 if (s->matched_entry == NULL) {
5545 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5546 return NULL;
5548 if (view->searching == TOG_SEARCH_FORWARD)
5549 te = got_object_tree_get_first_entry(s->tree);
5550 else
5551 te = got_object_tree_get_last_entry(s->tree);
5554 if (match_tree_entry(te, &view->regex)) {
5555 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5556 s->matched_entry = te;
5557 break;
5560 if (view->searching == TOG_SEARCH_FORWARD)
5561 te = got_tree_entry_get_next(s->tree, te);
5562 else
5563 te = got_tree_entry_get_prev(s->tree, te);
5566 if (s->matched_entry) {
5567 s->first_displayed_entry = s->matched_entry;
5568 s->selected = 0;
5571 return NULL;
5574 static const struct got_error *
5575 show_tree_view(struct tog_view *view)
5577 const struct got_error *err = NULL;
5578 struct tog_tree_view_state *s = &view->state.tree;
5579 char *parent_path;
5581 err = tree_entry_path(&parent_path, &s->parents, NULL);
5582 if (err)
5583 return err;
5585 err = draw_tree_entries(view, parent_path);
5586 free(parent_path);
5588 view_vborder(view);
5589 return err;
5592 static const struct got_error *
5593 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5595 const struct got_error *err = NULL;
5596 struct tog_tree_view_state *s = &view->state.tree;
5597 struct tog_view *log_view, *ref_view;
5598 struct got_tree_entry *te;
5599 int begin_x = 0, n, nscroll = view->nlines - 3;
5601 switch (ch) {
5602 case 'i':
5603 s->show_ids = !s->show_ids;
5604 break;
5605 case 'l':
5606 if (!s->selected_entry)
5607 break;
5608 if (view_is_parent_view(view))
5609 begin_x = view_split_begin_x(view->begin_x);
5610 err = log_selected_tree_entry(&log_view, begin_x, s);
5611 view->focussed = 0;
5612 log_view->focussed = 1;
5613 if (view_is_parent_view(view)) {
5614 err = view_close_child(view);
5615 if (err)
5616 return err;
5617 view_set_child(view, log_view);
5618 view->focus_child = 1;
5619 } else
5620 *new_view = log_view;
5621 break;
5622 case 'r':
5623 if (view_is_parent_view(view))
5624 begin_x = view_split_begin_x(view->begin_x);
5625 ref_view = view_open(view->nlines, view->ncols,
5626 view->begin_y, begin_x, TOG_VIEW_REF);
5627 if (ref_view == NULL)
5628 return got_error_from_errno("view_open");
5629 err = open_ref_view(ref_view, s->repo);
5630 if (err) {
5631 view_close(ref_view);
5632 return err;
5634 view->focussed = 0;
5635 ref_view->focussed = 1;
5636 if (view_is_parent_view(view)) {
5637 err = view_close_child(view);
5638 if (err)
5639 return err;
5640 view_set_child(view, ref_view);
5641 view->focus_child = 1;
5642 } else
5643 *new_view = ref_view;
5644 break;
5645 case 'g':
5646 case KEY_HOME:
5647 s->selected = 0;
5648 if (s->tree == s->root)
5649 s->first_displayed_entry =
5650 got_object_tree_get_first_entry(s->tree);
5651 else
5652 s->first_displayed_entry = NULL;
5653 break;
5654 case 'G':
5655 case KEY_END:
5656 s->selected = 0;
5657 te = got_object_tree_get_last_entry(s->tree);
5658 for (n = 0; n < view->nlines - 3; n++) {
5659 if (te == NULL) {
5660 if(s->tree != s->root) {
5661 s->first_displayed_entry = NULL;
5662 n++;
5664 break;
5666 s->first_displayed_entry = te;
5667 te = got_tree_entry_get_prev(s->tree, te);
5669 if (n > 0)
5670 s->selected = n - 1;
5671 break;
5672 case 'k':
5673 case KEY_UP:
5674 case CTRL('p'):
5675 if (s->selected > 0) {
5676 s->selected--;
5677 break;
5679 tree_scroll_up(s, 1);
5680 break;
5681 case CTRL('u'):
5682 case 'u':
5683 nscroll /= 2;
5684 /* FALL THROUGH */
5685 case KEY_PPAGE:
5686 case CTRL('b'):
5687 if (s->tree == s->root) {
5688 if (got_object_tree_get_first_entry(s->tree) ==
5689 s->first_displayed_entry)
5690 s->selected -= MIN(s->selected, nscroll);
5691 } else {
5692 if (s->first_displayed_entry == NULL)
5693 s->selected -= MIN(s->selected, nscroll);
5695 tree_scroll_up(s, MAX(0, nscroll));
5696 break;
5697 case 'j':
5698 case KEY_DOWN:
5699 case CTRL('n'):
5700 if (s->selected < s->ndisplayed - 1) {
5701 s->selected++;
5702 break;
5704 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5705 == NULL)
5706 /* can't scroll any further */
5707 break;
5708 tree_scroll_down(s, 1);
5709 break;
5710 case CTRL('d'):
5711 case 'd':
5712 nscroll /= 2;
5713 /* FALL THROUGH */
5714 case KEY_NPAGE:
5715 case CTRL('f'):
5716 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5717 == NULL) {
5718 /* can't scroll any further; move cursor down */
5719 if (s->selected < s->ndisplayed - 1)
5720 s->selected += MIN(nscroll,
5721 s->ndisplayed - s->selected - 1);
5722 break;
5724 tree_scroll_down(s, nscroll);
5725 break;
5726 case KEY_ENTER:
5727 case '\r':
5728 case KEY_BACKSPACE:
5729 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5730 struct tog_parent_tree *parent;
5731 /* user selected '..' */
5732 if (s->tree == s->root)
5733 break;
5734 parent = TAILQ_FIRST(&s->parents);
5735 TAILQ_REMOVE(&s->parents, parent,
5736 entry);
5737 got_object_tree_close(s->tree);
5738 s->tree = parent->tree;
5739 s->first_displayed_entry =
5740 parent->first_displayed_entry;
5741 s->selected_entry =
5742 parent->selected_entry;
5743 s->selected = parent->selected;
5744 free(parent);
5745 } else if (S_ISDIR(got_tree_entry_get_mode(
5746 s->selected_entry))) {
5747 struct got_tree_object *subtree;
5748 err = got_object_open_as_tree(&subtree, s->repo,
5749 got_tree_entry_get_id(s->selected_entry));
5750 if (err)
5751 break;
5752 err = tree_view_visit_subtree(s, subtree);
5753 if (err) {
5754 got_object_tree_close(subtree);
5755 break;
5757 } else if (S_ISREG(got_tree_entry_get_mode(
5758 s->selected_entry))) {
5759 struct tog_view *blame_view;
5760 int begin_x = view_is_parent_view(view) ?
5761 view_split_begin_x(view->begin_x) : 0;
5763 err = blame_tree_entry(&blame_view, begin_x,
5764 s->selected_entry, &s->parents,
5765 s->commit_id, s->repo);
5766 if (err)
5767 break;
5768 view->focussed = 0;
5769 blame_view->focussed = 1;
5770 if (view_is_parent_view(view)) {
5771 err = view_close_child(view);
5772 if (err)
5773 return err;
5774 view_set_child(view, blame_view);
5775 view->focus_child = 1;
5776 } else
5777 *new_view = blame_view;
5779 break;
5780 case KEY_RESIZE:
5781 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5782 s->selected = view->nlines - 4;
5783 break;
5784 default:
5785 break;
5788 return err;
5791 __dead static void
5792 usage_tree(void)
5794 endwin();
5795 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5796 getprogname());
5797 exit(1);
5800 static const struct got_error *
5801 cmd_tree(int argc, char *argv[])
5803 const struct got_error *error;
5804 struct got_repository *repo = NULL;
5805 struct got_worktree *worktree = NULL;
5806 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5807 struct got_object_id *commit_id = NULL;
5808 struct got_commit_object *commit = NULL;
5809 const char *commit_id_arg = NULL;
5810 char *label = NULL;
5811 struct got_reference *ref = NULL;
5812 const char *head_ref_name = NULL;
5813 int ch;
5814 struct tog_view *view;
5815 int *pack_fds = NULL;
5817 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5818 switch (ch) {
5819 case 'c':
5820 commit_id_arg = optarg;
5821 break;
5822 case 'r':
5823 repo_path = realpath(optarg, NULL);
5824 if (repo_path == NULL)
5825 return got_error_from_errno2("realpath",
5826 optarg);
5827 break;
5828 default:
5829 usage_tree();
5830 /* NOTREACHED */
5834 argc -= optind;
5835 argv += optind;
5837 if (argc > 1)
5838 usage_tree();
5840 error = got_repo_pack_fds_open(&pack_fds);
5841 if (error != NULL)
5842 goto done;
5844 if (repo_path == NULL) {
5845 cwd = getcwd(NULL, 0);
5846 if (cwd == NULL)
5847 return got_error_from_errno("getcwd");
5848 error = got_worktree_open(&worktree, cwd);
5849 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5850 goto done;
5851 if (worktree)
5852 repo_path =
5853 strdup(got_worktree_get_repo_path(worktree));
5854 else
5855 repo_path = strdup(cwd);
5856 if (repo_path == NULL) {
5857 error = got_error_from_errno("strdup");
5858 goto done;
5862 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5863 if (error != NULL)
5864 goto done;
5866 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5867 repo, worktree);
5868 if (error)
5869 goto done;
5871 init_curses();
5873 error = apply_unveil(got_repo_get_path(repo), NULL);
5874 if (error)
5875 goto done;
5877 error = tog_load_refs(repo, 0);
5878 if (error)
5879 goto done;
5881 if (commit_id_arg == NULL) {
5882 error = got_repo_match_object_id(&commit_id, &label,
5883 worktree ? got_worktree_get_head_ref_name(worktree) :
5884 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5885 if (error)
5886 goto done;
5887 head_ref_name = label;
5888 } else {
5889 error = got_ref_open(&ref, repo, commit_id_arg, 0);
5890 if (error == NULL)
5891 head_ref_name = got_ref_get_name(ref);
5892 else if (error->code != GOT_ERR_NOT_REF)
5893 goto done;
5894 error = got_repo_match_object_id(&commit_id, NULL,
5895 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5896 if (error)
5897 goto done;
5900 error = got_object_open_as_commit(&commit, repo, commit_id);
5901 if (error)
5902 goto done;
5904 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5905 if (view == NULL) {
5906 error = got_error_from_errno("view_open");
5907 goto done;
5909 error = open_tree_view(view, commit_id, head_ref_name, repo);
5910 if (error)
5911 goto done;
5912 if (!got_path_is_root_dir(in_repo_path)) {
5913 error = tree_view_walk_path(&view->state.tree, commit,
5914 in_repo_path);
5915 if (error)
5916 goto done;
5919 if (worktree) {
5920 /* Release work tree lock. */
5921 got_worktree_close(worktree);
5922 worktree = NULL;
5924 error = view_loop(view);
5925 done:
5926 free(repo_path);
5927 free(cwd);
5928 free(commit_id);
5929 free(label);
5930 if (ref)
5931 got_ref_close(ref);
5932 if (repo) {
5933 const struct got_error *close_err = got_repo_close(repo);
5934 if (error == NULL)
5935 error = close_err;
5937 if (pack_fds) {
5938 const struct got_error *pack_err =
5939 got_repo_pack_fds_close(pack_fds);
5940 if (error == NULL)
5941 error = pack_err;
5943 tog_free_refs();
5944 return error;
5947 static const struct got_error *
5948 ref_view_load_refs(struct tog_ref_view_state *s)
5950 struct got_reflist_entry *sre;
5951 struct tog_reflist_entry *re;
5953 s->nrefs = 0;
5954 TAILQ_FOREACH(sre, &tog_refs, entry) {
5955 if (strncmp(got_ref_get_name(sre->ref),
5956 "refs/got/", 9) == 0 &&
5957 strncmp(got_ref_get_name(sre->ref),
5958 "refs/got/backup/", 16) != 0)
5959 continue;
5961 re = malloc(sizeof(*re));
5962 if (re == NULL)
5963 return got_error_from_errno("malloc");
5965 re->ref = got_ref_dup(sre->ref);
5966 if (re->ref == NULL)
5967 return got_error_from_errno("got_ref_dup");
5968 re->idx = s->nrefs++;
5969 TAILQ_INSERT_TAIL(&s->refs, re, entry);
5972 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5973 return NULL;
5976 void
5977 ref_view_free_refs(struct tog_ref_view_state *s)
5979 struct tog_reflist_entry *re;
5981 while (!TAILQ_EMPTY(&s->refs)) {
5982 re = TAILQ_FIRST(&s->refs);
5983 TAILQ_REMOVE(&s->refs, re, entry);
5984 got_ref_close(re->ref);
5985 free(re);
5989 static const struct got_error *
5990 open_ref_view(struct tog_view *view, struct got_repository *repo)
5992 const struct got_error *err = NULL;
5993 struct tog_ref_view_state *s = &view->state.ref;
5995 s->selected_entry = 0;
5996 s->repo = repo;
5998 TAILQ_INIT(&s->refs);
5999 STAILQ_INIT(&s->colors);
6001 err = ref_view_load_refs(s);
6002 if (err)
6003 return err;
6005 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6006 err = add_color(&s->colors, "^refs/heads/",
6007 TOG_COLOR_REFS_HEADS,
6008 get_color_value("TOG_COLOR_REFS_HEADS"));
6009 if (err)
6010 goto done;
6012 err = add_color(&s->colors, "^refs/tags/",
6013 TOG_COLOR_REFS_TAGS,
6014 get_color_value("TOG_COLOR_REFS_TAGS"));
6015 if (err)
6016 goto done;
6018 err = add_color(&s->colors, "^refs/remotes/",
6019 TOG_COLOR_REFS_REMOTES,
6020 get_color_value("TOG_COLOR_REFS_REMOTES"));
6021 if (err)
6022 goto done;
6024 err = add_color(&s->colors, "^refs/got/backup/",
6025 TOG_COLOR_REFS_BACKUP,
6026 get_color_value("TOG_COLOR_REFS_BACKUP"));
6027 if (err)
6028 goto done;
6031 view->show = show_ref_view;
6032 view->input = input_ref_view;
6033 view->close = close_ref_view;
6034 view->search_start = search_start_ref_view;
6035 view->search_next = search_next_ref_view;
6036 done:
6037 if (err)
6038 free_colors(&s->colors);
6039 return err;
6042 static const struct got_error *
6043 close_ref_view(struct tog_view *view)
6045 struct tog_ref_view_state *s = &view->state.ref;
6047 ref_view_free_refs(s);
6048 free_colors(&s->colors);
6050 return NULL;
6053 static const struct got_error *
6054 resolve_reflist_entry(struct got_object_id **commit_id,
6055 struct tog_reflist_entry *re, struct got_repository *repo)
6057 const struct got_error *err = NULL;
6058 struct got_object_id *obj_id;
6059 struct got_tag_object *tag = NULL;
6060 int obj_type;
6062 *commit_id = NULL;
6064 err = got_ref_resolve(&obj_id, repo, re->ref);
6065 if (err)
6066 return err;
6068 err = got_object_get_type(&obj_type, repo, obj_id);
6069 if (err)
6070 goto done;
6072 switch (obj_type) {
6073 case GOT_OBJ_TYPE_COMMIT:
6074 *commit_id = obj_id;
6075 break;
6076 case GOT_OBJ_TYPE_TAG:
6077 err = got_object_open_as_tag(&tag, repo, obj_id);
6078 if (err)
6079 goto done;
6080 free(obj_id);
6081 err = got_object_get_type(&obj_type, repo,
6082 got_object_tag_get_object_id(tag));
6083 if (err)
6084 goto done;
6085 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6086 err = got_error(GOT_ERR_OBJ_TYPE);
6087 goto done;
6089 *commit_id = got_object_id_dup(
6090 got_object_tag_get_object_id(tag));
6091 if (*commit_id == NULL) {
6092 err = got_error_from_errno("got_object_id_dup");
6093 goto done;
6095 break;
6096 default:
6097 err = got_error(GOT_ERR_OBJ_TYPE);
6098 break;
6101 done:
6102 if (tag)
6103 got_object_tag_close(tag);
6104 if (err) {
6105 free(*commit_id);
6106 *commit_id = NULL;
6108 return err;
6111 static const struct got_error *
6112 log_ref_entry(struct tog_view **new_view, int begin_x,
6113 struct tog_reflist_entry *re, struct got_repository *repo)
6115 struct tog_view *log_view;
6116 const struct got_error *err = NULL;
6117 struct got_object_id *commit_id = NULL;
6119 *new_view = NULL;
6121 err = resolve_reflist_entry(&commit_id, re, repo);
6122 if (err) {
6123 if (err->code != GOT_ERR_OBJ_TYPE)
6124 return err;
6125 else
6126 return NULL;
6129 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6130 if (log_view == NULL) {
6131 err = got_error_from_errno("view_open");
6132 goto done;
6135 err = open_log_view(log_view, commit_id, repo,
6136 got_ref_get_name(re->ref), "", 0);
6137 done:
6138 if (err)
6139 view_close(log_view);
6140 else
6141 *new_view = log_view;
6142 free(commit_id);
6143 return err;
6146 static void
6147 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6149 struct tog_reflist_entry *re;
6150 int i = 0;
6152 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6153 return;
6155 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6156 while (i++ < maxscroll) {
6157 if (re == NULL)
6158 break;
6159 s->first_displayed_entry = re;
6160 re = TAILQ_PREV(re, tog_reflist_head, entry);
6164 static void
6165 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6167 struct tog_reflist_entry *next, *last;
6168 int n = 0;
6170 if (s->first_displayed_entry)
6171 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6172 else
6173 next = TAILQ_FIRST(&s->refs);
6175 last = s->last_displayed_entry;
6176 while (next && last && n++ < maxscroll) {
6177 last = TAILQ_NEXT(last, entry);
6178 if (last) {
6179 s->first_displayed_entry = next;
6180 next = TAILQ_NEXT(next, entry);
6185 static const struct got_error *
6186 search_start_ref_view(struct tog_view *view)
6188 struct tog_ref_view_state *s = &view->state.ref;
6190 s->matched_entry = NULL;
6191 return NULL;
6194 static int
6195 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6197 regmatch_t regmatch;
6199 return regexec(regex, got_ref_get_name(re->ref), 1, &regmatch,
6200 0) == 0;
6203 static const struct got_error *
6204 search_next_ref_view(struct tog_view *view)
6206 struct tog_ref_view_state *s = &view->state.ref;
6207 struct tog_reflist_entry *re = NULL;
6209 if (!view->searching) {
6210 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6211 return NULL;
6214 if (s->matched_entry) {
6215 if (view->searching == TOG_SEARCH_FORWARD) {
6216 if (s->selected_entry)
6217 re = TAILQ_NEXT(s->selected_entry, entry);
6218 else
6219 re = TAILQ_PREV(s->selected_entry,
6220 tog_reflist_head, entry);
6221 } else {
6222 if (s->selected_entry == NULL)
6223 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6224 else
6225 re = TAILQ_PREV(s->selected_entry,
6226 tog_reflist_head, entry);
6228 } else {
6229 if (s->selected_entry)
6230 re = s->selected_entry;
6231 else if (view->searching == TOG_SEARCH_FORWARD)
6232 re = TAILQ_FIRST(&s->refs);
6233 else
6234 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6237 while (1) {
6238 if (re == NULL) {
6239 if (s->matched_entry == NULL) {
6240 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6241 return NULL;
6243 if (view->searching == TOG_SEARCH_FORWARD)
6244 re = TAILQ_FIRST(&s->refs);
6245 else
6246 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6249 if (match_reflist_entry(re, &view->regex)) {
6250 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6251 s->matched_entry = re;
6252 break;
6255 if (view->searching == TOG_SEARCH_FORWARD)
6256 re = TAILQ_NEXT(re, entry);
6257 else
6258 re = TAILQ_PREV(re, tog_reflist_head, entry);
6261 if (s->matched_entry) {
6262 s->first_displayed_entry = s->matched_entry;
6263 s->selected = 0;
6266 return NULL;
6269 static const struct got_error *
6270 show_ref_view(struct tog_view *view)
6272 const struct got_error *err = NULL;
6273 struct tog_ref_view_state *s = &view->state.ref;
6274 struct tog_reflist_entry *re;
6275 char *line = NULL;
6276 wchar_t *wline;
6277 struct tog_color *tc;
6278 int width, n;
6279 int limit = view->nlines;
6281 werase(view->window);
6283 s->ndisplayed = 0;
6285 if (limit == 0)
6286 return NULL;
6288 re = s->first_displayed_entry;
6290 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6291 s->nrefs) == -1)
6292 return got_error_from_errno("asprintf");
6294 err = format_line(&wline, &width, line, view->ncols, 0);
6295 if (err) {
6296 free(line);
6297 return err;
6299 if (view_needs_focus_indication(view))
6300 wstandout(view->window);
6301 waddwstr(view->window, wline);
6302 if (view_needs_focus_indication(view))
6303 wstandend(view->window);
6304 free(wline);
6305 wline = NULL;
6306 free(line);
6307 line = NULL;
6308 if (width < view->ncols - 1)
6309 waddch(view->window, '\n');
6310 if (--limit <= 0)
6311 return NULL;
6313 n = 0;
6314 while (re && limit > 0) {
6315 char *line = NULL;
6317 if (got_ref_is_symbolic(re->ref)) {
6318 if (asprintf(&line, "%s -> %s",
6319 got_ref_get_name(re->ref),
6320 got_ref_get_symref_target(re->ref)) == -1)
6321 return got_error_from_errno("asprintf");
6322 } else if (s->show_ids) {
6323 struct got_object_id *id;
6324 char *id_str;
6325 err = got_ref_resolve(&id, s->repo, re->ref);
6326 if (err)
6327 return err;
6328 err = got_object_id_str(&id_str, id);
6329 if (err) {
6330 free(id);
6331 return err;
6333 if (asprintf(&line, "%s: %s",
6334 got_ref_get_name(re->ref), id_str) == -1) {
6335 err = got_error_from_errno("asprintf");
6336 free(id);
6337 free(id_str);
6338 return err;
6340 free(id);
6341 free(id_str);
6342 } else {
6343 line = strdup(got_ref_get_name(re->ref));
6344 if (line == NULL)
6345 return got_error_from_errno("strdup");
6348 err = format_line(&wline, &width, line, view->ncols, 0);
6349 if (err) {
6350 free(line);
6351 return err;
6353 if (n == s->selected) {
6354 if (view->focussed)
6355 wstandout(view->window);
6356 s->selected_entry = re;
6358 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6359 if (tc)
6360 wattr_on(view->window,
6361 COLOR_PAIR(tc->colorpair), NULL);
6362 waddwstr(view->window, wline);
6363 if (tc)
6364 wattr_off(view->window,
6365 COLOR_PAIR(tc->colorpair), NULL);
6366 if (width < view->ncols - 1)
6367 waddch(view->window, '\n');
6368 if (n == s->selected && view->focussed)
6369 wstandend(view->window);
6370 free(line);
6371 free(wline);
6372 wline = NULL;
6373 n++;
6374 s->ndisplayed++;
6375 s->last_displayed_entry = re;
6377 limit--;
6378 re = TAILQ_NEXT(re, entry);
6381 view_vborder(view);
6382 return err;
6385 static const struct got_error *
6386 browse_ref_tree(struct tog_view **new_view, int begin_x,
6387 struct tog_reflist_entry *re, struct got_repository *repo)
6389 const struct got_error *err = NULL;
6390 struct got_object_id *commit_id = NULL;
6391 struct tog_view *tree_view;
6393 *new_view = NULL;
6395 err = resolve_reflist_entry(&commit_id, re, repo);
6396 if (err) {
6397 if (err->code != GOT_ERR_OBJ_TYPE)
6398 return err;
6399 else
6400 return NULL;
6404 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6405 if (tree_view == NULL) {
6406 err = got_error_from_errno("view_open");
6407 goto done;
6410 err = open_tree_view(tree_view, commit_id,
6411 got_ref_get_name(re->ref), repo);
6412 if (err)
6413 goto done;
6415 *new_view = tree_view;
6416 done:
6417 free(commit_id);
6418 return err;
6420 static const struct got_error *
6421 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6423 const struct got_error *err = NULL;
6424 struct tog_ref_view_state *s = &view->state.ref;
6425 struct tog_view *log_view, *tree_view;
6426 struct tog_reflist_entry *re;
6427 int begin_x = 0, n, nscroll = view->nlines - 1;
6429 switch (ch) {
6430 case 'i':
6431 s->show_ids = !s->show_ids;
6432 break;
6433 case 'o':
6434 s->sort_by_date = !s->sort_by_date;
6435 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6436 got_ref_cmp_by_commit_timestamp_descending :
6437 tog_ref_cmp_by_name, s->repo);
6438 if (err)
6439 break;
6440 got_reflist_object_id_map_free(tog_refs_idmap);
6441 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6442 &tog_refs, s->repo);
6443 if (err)
6444 break;
6445 ref_view_free_refs(s);
6446 err = ref_view_load_refs(s);
6447 break;
6448 case KEY_ENTER:
6449 case '\r':
6450 if (!s->selected_entry)
6451 break;
6452 if (view_is_parent_view(view))
6453 begin_x = view_split_begin_x(view->begin_x);
6454 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6455 s->repo);
6456 view->focussed = 0;
6457 log_view->focussed = 1;
6458 if (view_is_parent_view(view)) {
6459 err = view_close_child(view);
6460 if (err)
6461 return err;
6462 view_set_child(view, log_view);
6463 view->focus_child = 1;
6464 } else
6465 *new_view = log_view;
6466 break;
6467 case 't':
6468 if (!s->selected_entry)
6469 break;
6470 if (view_is_parent_view(view))
6471 begin_x = view_split_begin_x(view->begin_x);
6472 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6473 s->repo);
6474 if (err || tree_view == NULL)
6475 break;
6476 view->focussed = 0;
6477 tree_view->focussed = 1;
6478 if (view_is_parent_view(view)) {
6479 err = view_close_child(view);
6480 if (err)
6481 return err;
6482 view_set_child(view, tree_view);
6483 view->focus_child = 1;
6484 } else
6485 *new_view = tree_view;
6486 break;
6487 case 'g':
6488 case KEY_HOME:
6489 s->selected = 0;
6490 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6491 break;
6492 case 'G':
6493 case KEY_END:
6494 s->selected = 0;
6495 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6496 for (n = 0; n < view->nlines - 1; n++) {
6497 if (re == NULL)
6498 break;
6499 s->first_displayed_entry = re;
6500 re = TAILQ_PREV(re, tog_reflist_head, entry);
6502 if (n > 0)
6503 s->selected = n - 1;
6504 break;
6505 case 'k':
6506 case KEY_UP:
6507 case CTRL('p'):
6508 if (s->selected > 0) {
6509 s->selected--;
6510 break;
6512 ref_scroll_up(s, 1);
6513 break;
6514 case CTRL('u'):
6515 case 'u':
6516 nscroll /= 2;
6517 /* FALL THROUGH */
6518 case KEY_PPAGE:
6519 case CTRL('b'):
6520 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6521 s->selected -= MIN(nscroll, s->selected);
6522 ref_scroll_up(s, MAX(0, nscroll));
6523 break;
6524 case 'j':
6525 case KEY_DOWN:
6526 case CTRL('n'):
6527 if (s->selected < s->ndisplayed - 1) {
6528 s->selected++;
6529 break;
6531 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6532 /* can't scroll any further */
6533 break;
6534 ref_scroll_down(s, 1);
6535 break;
6536 case CTRL('d'):
6537 case 'd':
6538 nscroll /= 2;
6539 /* FALL THROUGH */
6540 case KEY_NPAGE:
6541 case CTRL('f'):
6542 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6543 /* can't scroll any further; move cursor down */
6544 if (s->selected < s->ndisplayed - 1)
6545 s->selected += MIN(nscroll,
6546 s->ndisplayed - s->selected - 1);
6547 break;
6549 ref_scroll_down(s, nscroll);
6550 break;
6551 case CTRL('l'):
6552 tog_free_refs();
6553 err = tog_load_refs(s->repo, s->sort_by_date);
6554 if (err)
6555 break;
6556 ref_view_free_refs(s);
6557 err = ref_view_load_refs(s);
6558 break;
6559 case KEY_RESIZE:
6560 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6561 s->selected = view->nlines - 2;
6562 break;
6563 default:
6564 break;
6567 return err;
6570 __dead static void
6571 usage_ref(void)
6573 endwin();
6574 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6575 getprogname());
6576 exit(1);
6579 static const struct got_error *
6580 cmd_ref(int argc, char *argv[])
6582 const struct got_error *error;
6583 struct got_repository *repo = NULL;
6584 struct got_worktree *worktree = NULL;
6585 char *cwd = NULL, *repo_path = NULL;
6586 int ch;
6587 struct tog_view *view;
6588 int *pack_fds = NULL;
6590 while ((ch = getopt(argc, argv, "r:")) != -1) {
6591 switch (ch) {
6592 case 'r':
6593 repo_path = realpath(optarg, NULL);
6594 if (repo_path == NULL)
6595 return got_error_from_errno2("realpath",
6596 optarg);
6597 break;
6598 default:
6599 usage_ref();
6600 /* NOTREACHED */
6604 argc -= optind;
6605 argv += optind;
6607 if (argc > 1)
6608 usage_ref();
6610 error = got_repo_pack_fds_open(&pack_fds);
6611 if (error != NULL)
6612 goto done;
6614 if (repo_path == NULL) {
6615 cwd = getcwd(NULL, 0);
6616 if (cwd == NULL)
6617 return got_error_from_errno("getcwd");
6618 error = got_worktree_open(&worktree, cwd);
6619 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6620 goto done;
6621 if (worktree)
6622 repo_path =
6623 strdup(got_worktree_get_repo_path(worktree));
6624 else
6625 repo_path = strdup(cwd);
6626 if (repo_path == NULL) {
6627 error = got_error_from_errno("strdup");
6628 goto done;
6632 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6633 if (error != NULL)
6634 goto done;
6636 init_curses();
6638 error = apply_unveil(got_repo_get_path(repo), NULL);
6639 if (error)
6640 goto done;
6642 error = tog_load_refs(repo, 0);
6643 if (error)
6644 goto done;
6646 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6647 if (view == NULL) {
6648 error = got_error_from_errno("view_open");
6649 goto done;
6652 error = open_ref_view(view, repo);
6653 if (error)
6654 goto done;
6656 if (worktree) {
6657 /* Release work tree lock. */
6658 got_worktree_close(worktree);
6659 worktree = NULL;
6661 error = view_loop(view);
6662 done:
6663 free(repo_path);
6664 free(cwd);
6665 if (repo) {
6666 const struct got_error *close_err = got_repo_close(repo);
6667 if (close_err)
6668 error = close_err;
6670 if (pack_fds) {
6671 const struct got_error *pack_err =
6672 got_repo_pack_fds_close(pack_fds);
6673 if (error == NULL)
6674 error = pack_err;
6676 tog_free_refs();
6677 return error;
6680 static void
6681 list_commands(FILE *fp)
6683 size_t i;
6685 fprintf(fp, "commands:");
6686 for (i = 0; i < nitems(tog_commands); i++) {
6687 const struct tog_cmd *cmd = &tog_commands[i];
6688 fprintf(fp, " %s", cmd->name);
6690 fputc('\n', fp);
6693 __dead static void
6694 usage(int hflag, int status)
6696 FILE *fp = (status == 0) ? stdout : stderr;
6698 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6699 getprogname());
6700 if (hflag) {
6701 fprintf(fp, "lazy usage: %s path\n", getprogname());
6702 list_commands(fp);
6704 exit(status);
6707 static char **
6708 make_argv(int argc, ...)
6710 va_list ap;
6711 char **argv;
6712 int i;
6714 va_start(ap, argc);
6716 argv = calloc(argc, sizeof(char *));
6717 if (argv == NULL)
6718 err(1, "calloc");
6719 for (i = 0; i < argc; i++) {
6720 argv[i] = strdup(va_arg(ap, char *));
6721 if (argv[i] == NULL)
6722 err(1, "strdup");
6725 va_end(ap);
6726 return argv;
6730 * Try to convert 'tog path' into a 'tog log path' command.
6731 * The user could simply have mistyped the command rather than knowingly
6732 * provided a path. So check whether argv[0] can in fact be resolved
6733 * to a path in the HEAD commit and print a special error if not.
6734 * This hack is for mpi@ <3
6736 static const struct got_error *
6737 tog_log_with_path(int argc, char *argv[])
6739 const struct got_error *error = NULL, *close_err;
6740 const struct tog_cmd *cmd = NULL;
6741 struct got_repository *repo = NULL;
6742 struct got_worktree *worktree = NULL;
6743 struct got_object_id *commit_id = NULL, *id = NULL;
6744 struct got_commit_object *commit = NULL;
6745 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6746 char *commit_id_str = NULL, **cmd_argv = NULL;
6747 int *pack_fds = NULL;
6749 cwd = getcwd(NULL, 0);
6750 if (cwd == NULL)
6751 return got_error_from_errno("getcwd");
6753 error = got_repo_pack_fds_open(&pack_fds);
6754 if (error != NULL)
6755 goto done;
6757 error = got_worktree_open(&worktree, cwd);
6758 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6759 goto done;
6761 if (worktree)
6762 repo_path = strdup(got_worktree_get_repo_path(worktree));
6763 else
6764 repo_path = strdup(cwd);
6765 if (repo_path == NULL) {
6766 error = got_error_from_errno("strdup");
6767 goto done;
6770 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6771 if (error != NULL)
6772 goto done;
6774 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6775 repo, worktree);
6776 if (error)
6777 goto done;
6779 error = tog_load_refs(repo, 0);
6780 if (error)
6781 goto done;
6782 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6783 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6784 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6785 if (error)
6786 goto done;
6788 if (worktree) {
6789 got_worktree_close(worktree);
6790 worktree = NULL;
6793 error = got_object_open_as_commit(&commit, repo, commit_id);
6794 if (error)
6795 goto done;
6797 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6798 if (error) {
6799 if (error->code != GOT_ERR_NO_TREE_ENTRY)
6800 goto done;
6801 fprintf(stderr, "%s: '%s' is no known command or path\n",
6802 getprogname(), argv[0]);
6803 usage(1, 1);
6804 /* not reached */
6807 close_err = got_repo_close(repo);
6808 if (error == NULL)
6809 error = close_err;
6810 repo = NULL;
6812 error = got_object_id_str(&commit_id_str, commit_id);
6813 if (error)
6814 goto done;
6816 cmd = &tog_commands[0]; /* log */
6817 argc = 4;
6818 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6819 error = cmd->cmd_main(argc, cmd_argv);
6820 done:
6821 if (repo) {
6822 close_err = got_repo_close(repo);
6823 if (error == NULL)
6824 error = close_err;
6826 if (commit)
6827 got_object_commit_close(commit);
6828 if (worktree)
6829 got_worktree_close(worktree);
6830 if (pack_fds) {
6831 const struct got_error *pack_err =
6832 got_repo_pack_fds_close(pack_fds);
6833 if (error == NULL)
6834 error = pack_err;
6836 free(id);
6837 free(commit_id_str);
6838 free(commit_id);
6839 free(cwd);
6840 free(repo_path);
6841 free(in_repo_path);
6842 if (cmd_argv) {
6843 int i;
6844 for (i = 0; i < argc; i++)
6845 free(cmd_argv[i]);
6846 free(cmd_argv);
6848 tog_free_refs();
6849 return error;
6852 int
6853 main(int argc, char *argv[])
6855 const struct got_error *error = NULL;
6856 const struct tog_cmd *cmd = NULL;
6857 int ch, hflag = 0, Vflag = 0;
6858 char **cmd_argv = NULL;
6859 static const struct option longopts[] = {
6860 { "version", no_argument, NULL, 'V' },
6861 { NULL, 0, NULL, 0}
6864 setlocale(LC_CTYPE, "");
6866 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6867 switch (ch) {
6868 case 'h':
6869 hflag = 1;
6870 break;
6871 case 'V':
6872 Vflag = 1;
6873 break;
6874 default:
6875 usage(hflag, 1);
6876 /* NOTREACHED */
6880 argc -= optind;
6881 argv += optind;
6882 optind = 1;
6883 optreset = 1;
6885 if (Vflag) {
6886 got_version_print_str();
6887 return 0;
6890 #ifndef PROFILE
6891 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6892 NULL) == -1)
6893 err(1, "pledge");
6894 #endif
6896 if (argc == 0) {
6897 if (hflag)
6898 usage(hflag, 0);
6899 /* Build an argument vector which runs a default command. */
6900 cmd = &tog_commands[0];
6901 argc = 1;
6902 cmd_argv = make_argv(argc, cmd->name);
6903 } else {
6904 size_t i;
6906 /* Did the user specify a command? */
6907 for (i = 0; i < nitems(tog_commands); i++) {
6908 if (strncmp(tog_commands[i].name, argv[0],
6909 strlen(argv[0])) == 0) {
6910 cmd = &tog_commands[i];
6911 break;
6916 if (cmd == NULL) {
6917 if (argc != 1)
6918 usage(0, 1);
6919 /* No command specified; try log with a path */
6920 error = tog_log_with_path(argc, argv);
6921 } else {
6922 if (hflag)
6923 cmd->cmd_usage();
6924 else
6925 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6928 endwin();
6929 putchar('\n');
6930 if (cmd_argv) {
6931 int i;
6932 for (i = 0; i < argc; i++)
6933 free(cmd_argv[i]);
6934 free(cmd_argv);
6937 if (error && error->code != GOT_ERR_CANCELLED)
6938 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
6939 return 0;